|
|
|
ASIC/FPGA Design and Verification Out Source Services
GHDL c interfaces and extensions: example of
VHDL std_logic_vector to c code string.
-
This page shows a simple example, which uses c code to enhance a VHDL test
bench. This
page
shows some other VHDL to c code interface.
-
First is the VHDL bench passes a vector to the c code:
veco := g_vec( g_v2s(vec) );
Some functions to convert the std_logic_vector to string are required:
- package body pkg_std_logic_vec is
- function g_v2s(v: std_logic_vector) return string is
- variable result : string (1 to v'length);
- variable r : integer;
- begin
- r := 1;
- for i in v'range loop
- if( v(i) = '1') then result(r) := '1';
- else result(r) := '0';
- end if;
- r := r + 1;
- end loop;
- return result;
- end g_v2s;
- function g_vec (s: string) return integer is
- begin
- return getenvS2(s & NUL);
- end g_vec;
- function getenvS2(s : string)
- return integer is
- begin
- assert false report "VHPI" severity failure;
- end getenvS2;
- end pkg_std_logic_vec;
-
Part of the c code is listed below.
- struct int_bounds {
- int left;
- int right;
- char dir;
- unsigned int len;
- };
- struct ghdl_string {
- char *base;
- struct int_bounds *bounds;
- };
- long int ghdl_envS2(struct ghdl_string *s) {
- long res;
- int ix;
- int pwr;
- char *r = s->base;
- printf("from c start\n");
- printf("str %s\n", s->base);
- printf("left %d\n", (s->bounds)->left);
- printf("right %d\n", (s->bounds)->right);
- printf("dir %d\n", (s->bounds)->dir);
- printf("len %d\n", (s->bounds)->len);
- res=0;
- pwr=1;
- for(ix=(s->bounds)->len - 2; ix >= 0; ix--) {
- if(r[ix] == '1') //no check for U, X, W ...
- res += pwr * ((unsigned char)r[ix] - 48);
- //printf("%c %d %d\n", r[ix], pwr, res);
- pwr *= 2;
- }
- printf("from c end\n");
-
- return res;
- }
-
The example is tested using the free VHDL simulator GHDL.
Compilation, elaboration and simulation is done using a makefile.
It supports, cleanall, all, run.
-
A similar example for a memory array also exists. In this case g++ is used
and the cpp code is used. CPP makes it easier to write code for memories
using vectors.
extern "C" long int mem_arr_44_wr_c(int addr, struct ghdl_string *s) {
long res;
-
For more details please send an e-mail and put in the title:
Cpp GHDL std_logic_vec.
|
| |