Skip to content

Commit

Permalink
Add packageless intvector generic example (umarcor#5 c)
Browse files Browse the repository at this point in the history
  • Loading branch information
radonnachie committed Apr 17, 2020
1 parent 0d9977c commit 07efdff
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
11 changes: 11 additions & 0 deletions vhpidirect/quickstart/intvectorgeneric/caux.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>
#define SIZE_ARRAY (sizeof(intArray)/sizeof(int))

int intArray[5];
int* getIntArr_ptr(){//function acts like a constructor so initialise the variable
for (int i = 0; i < SIZE_ARRAY; i++)
{
intArray[i] = 11*(i+1);
}
return intArray;
}
15 changes: 15 additions & 0 deletions vhpidirect/quickstart/intvectorgeneric/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env sh

set -e

cd $(dirname "$0")

echo "Analyze tb.vhd"
ghdl -a tb.vhd

echo "Build tb (with caux.c) [GHDL]"
ghdl -e -Wl,caux.c tb

echo "Execute tb (-gArraySize=5)"
./tb -gArraySize=5

31 changes: 31 additions & 0 deletions vhpidirect/quickstart/intvectorgeneric/tb.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
entity tb is
generic(
arraySize : integer := 1
);
end entity tb;

architecture RTL of tb is
begin
process
type int_arr is array(0 to arraySize-1) of integer;
type int_arr_ptr is access int_arr; -- represented C-side with int*

impure function c_intArr_ptr return int_arr_ptr is
begin
assert false report "c_intArr_ptr VHPI" severity failure;
end;
attribute foreign of c_intArr_ptr : function is "VHPIDIRECT getIntArr_ptr";

variable c_intArr : int_arr_ptr := c_intArr_ptr;
begin
report "ArraySize Interface Generic: " & integer'image(arraySize);

for i in 0 to arraySize-1 loop
report "c_intArr[" & integer'image(i) & "] = " & integer'image(c_intArr(i)) & ". Set to: " & integer'image(-2*c_intArr(i));
c_intArr(i) := -2*c_intArr(i);
end loop;

wait;
end process;

end architecture RTL;

0 comments on commit 07efdff

Please # to comment.