From 07efdff172680efc6d47ef78ab6f1d613b96cc32 Mon Sep 17 00:00:00 2001 From: RocketRoss Date: Fri, 17 Apr 2020 14:01:23 +0200 Subject: [PATCH] Add packageless intvector generic example (#5 c) --- vhpidirect/quickstart/intvectorgeneric/caux.c | 11 +++++++ vhpidirect/quickstart/intvectorgeneric/run.sh | 15 +++++++++ vhpidirect/quickstart/intvectorgeneric/tb.vhd | 31 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100755 vhpidirect/quickstart/intvectorgeneric/caux.c create mode 100755 vhpidirect/quickstart/intvectorgeneric/run.sh create mode 100755 vhpidirect/quickstart/intvectorgeneric/tb.vhd diff --git a/vhpidirect/quickstart/intvectorgeneric/caux.c b/vhpidirect/quickstart/intvectorgeneric/caux.c new file mode 100755 index 00000000..bd71724c --- /dev/null +++ b/vhpidirect/quickstart/intvectorgeneric/caux.c @@ -0,0 +1,11 @@ +#include +#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; +} \ No newline at end of file diff --git a/vhpidirect/quickstart/intvectorgeneric/run.sh b/vhpidirect/quickstart/intvectorgeneric/run.sh new file mode 100755 index 00000000..13326ec3 --- /dev/null +++ b/vhpidirect/quickstart/intvectorgeneric/run.sh @@ -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 + diff --git a/vhpidirect/quickstart/intvectorgeneric/tb.vhd b/vhpidirect/quickstart/intvectorgeneric/tb.vhd new file mode 100755 index 00000000..1f82b377 --- /dev/null +++ b/vhpidirect/quickstart/intvectorgeneric/tb.vhd @@ -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;