Email: bknpk@hotmail.com Phone: +972-54-7649119

 

ASIC/FPGA Design and Verification Out Source Services

GHDL c interfaces and extensions: example of VHDL std_logic_vector to c code string.


  1. 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.

  2. 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:
    1. package body pkg_std_logic_vec is
    2.     function g_v2s(v: std_logic_vector) return string is
    3.       variable result : string (1 to v'length);
    4.       variable r : integer;
    5.     begin
    6.       r := 1;
    7.       for i in v'range loop
    8.         if( v(i) = '1') then result(r) := '1';
    9.         else                 result(r) := '0';
    10.         end if;
    11.         r := r + 1;
    12.       end loop;
    13.       return result;
    14.     end g_v2s;
    15.   function g_vec (s: string) return integer is
    16.   begin
    17.     return getenvS2(s & NUL);
    18.   end g_vec;
    19.   function getenvS2(s : string)
    20.     return integer is
    21.   begin
    22.     assert false report "VHPI" severity failure;
    23.   end getenvS2;
    24. end pkg_std_logic_vec;



  3. Part of the c code is listed below.

    1. struct int_bounds {
    2.   int left;
    3.   int right;
    4.   char dir;
    5.   unsigned int len;
    6. };
    7. struct ghdl_string {
    8.   char *base;
    9.   struct int_bounds *bounds;
    10. };
    11. long int ghdl_envS2(struct ghdl_string *s) {
    12.   long res;
    13.   int ix;
    14.   int pwr;
    15.   char *r = s->base;
    16.   printf("from c start\n");
    17.   printf("str %s\n", s->base);
    18.   printf("left %d\n", (s->bounds)->left);
    19.   printf("right %d\n", (s->bounds)->right);
    20.   printf("dir %d\n", (s->bounds)->dir);
    21.   printf("len %d\n", (s->bounds)->len);
    22.   res=0;
    23.   pwr=1;
    24.   for(ix=(s->bounds)->len - 2; ix >= 0; ix--) {
    25.     if(r[ix] == '1') //no check for U, X, W ...
    26.     res += pwr * ((unsigned char)r[ix] - 48);
    27.   //printf("%c %d %d\n", r[ix], pwr, res);
    28.     pwr *= 2;
    29.   }
    30.   printf("from c end\n");
    31.   return res;
    32. }


  4. The example is tested using the free VHDL simulator GHDL.
    Compilation, elaboration and simulation is done using a makefile.
    It supports, cleanall, all, run.



  5. 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;



  6. For more details please send an e-mail and put in the title: Cpp GHDL std_logic_vec.