-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add packageless intvector examples (#5 a + c) #11
base: master
Are you sure you want to change the base?
Changes from all commits
64b71a2
07efdff
95a760d
2bfef3e
7762ae1
34f25ab
cfb7846
3df7d0d
96553c5
43efa07
56079f9
c6410b2
7181f2f
201eb51
89d0c62
006b0e9
ad92bd4
2ad5c38
59f039c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,6 @@ ent* | |
tb* | ||
!*.vhd | ||
!*.vhdl | ||
doc/_build/* | ||
.vunit_hash | ||
vhpidirect/arrays/matrices/vunit_axis_vcs/vunit_out/* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#define SIZE_ARRAY (sizeof(intArray)/sizeof(int)) | ||
|
||
int intArray[] = {11, 22, 33, 44, 55, 66}; | ||
|
||
int getIntArrSize(){ | ||
return SIZE_ARRAY; | ||
} | ||
|
||
int* getIntArr_ptr(){ | ||
return intArray; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "caux.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this somehow redundant to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sizeof(intArray) in the subexamples returns sizeof(int*), not sizeof(int[])... In order make it more closely match the original example I can make this array statically defined. then we start to lose out on the extensive main() entry point passing examples to ghdl for various reasons. I think that may be for the better though. Let's simplify this to exactly the same as the intvector example, just with the values being printed out in main before the ghdl_main() call. An we can make the extensive examples of the cosimulation in a different PR?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't understand this sentence. Can you please reword it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. I want to be extra clear so that you can tell me if I have any misunderstandings on the examples you want. The sub example (maindefined) is to show how to use a custom entry point (main.c) to preprocess the VHPIDIRECT shared int[] (in caux.c).
I am proposing that we cut all of the discussion out by making the sub example much more similar to the parent example (exactly the same, except a main.c that wraps ghdl_main. main() does nothing, at most prints out "ghdl simulation\n*****************".) We then 'lose out' on the 3 points above, but I think that they start to get extensive enough that a whole section could be written on them (at least have their own examples in a different section). Those examples can be linked to in this one's documentation.... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think that I am agreeing with your earlier response. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Can you open a new PR with the status of this one when I did that comment? I believe that we can have that merged almost as is. We just want two subcases: set the size in C or set the size in VHDL. In both cases, the allocation is done in C. In the first one, it's hardcoded. In the second one, malloc is called once only, and a second helper function is used to fill it in C. Then, we will have a better perspective about what to add here. I created #14, so this can be about the auto-reallocation logic that you have. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bear this in mind. The question we want to answer is "How do I allocate in C and share a pointer/access while defining the size in one language only?". We need to provide two cases: hardcode the size in C (and pass it to VHDL) and harcode the size in VHDL (and pass it to C). |
||
|
||
int getIntArrSize(){ | ||
return arraySize; | ||
} | ||
|
||
int* getIntArr_ptr(){ | ||
return intArray; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
int arraySize; | ||
int* intArray; | ||
|
||
int getIntArrSize(); | ||
int* getIntArr_ptr(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <stdio.h> | ||
#include <malloc.h> | ||
|
||
#include "caux.h" | ||
|
||
extern int ghdl_main(char argc, char* argv[]); | ||
|
||
int main(char argc, char* argv[]){ | ||
char strIn[3]; | ||
|
||
printf("Enter the length of the integer array [1-99]: "); | ||
fgets(strIn, 3, stdin); | ||
printf("\n"); | ||
sscanf(strIn, "%d", &arraySize); | ||
|
||
intArray = malloc(arraySize*sizeof(int)); | ||
for(int i = 0; i < arraySize; i++){ | ||
intArray[i] = 11*(i+1); | ||
} | ||
|
||
printf("GHDL Simulation Begin\n*****************************************\n"); | ||
int ghdlReturn = ghdl_main(argc, argv); | ||
printf("*****************************************\nGHDL Simulation End\n"); | ||
|
||
free(intArray); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env sh | ||
|
||
set -e | ||
|
||
cd $(dirname "$0") | ||
|
||
echo "Build main.o (with caux.h) [GCC]" | ||
gcc -c main.c | ||
|
||
echo "Analyze tb.vhd [GHDL]" | ||
ghdl -a tb.vhd | ||
|
||
echo "Build tb (with caux.c) [GHDL]" | ||
ghdl -e -Wl,main.o -Wl,caux.c tb | ||
|
||
echo "Execute tb" | ||
./tb |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
entity tb is | ||
end entity tb; | ||
|
||
architecture RTL of tb is | ||
|
||
begin | ||
|
||
process | ||
function c_intArrSize return integer is-- represented C-side with int | ||
begin | ||
assert false report "c_intArrSize VHPI" severity failure; | ||
end; | ||
attribute foreign of c_intArrSize : function is "VHPIDIRECT getIntArrSize"; -- getIntArrSize is the C-side function name | ||
|
||
type int_arr is array(0 to c_intArrSize-1) of integer; | ||
type int_arr_ptr is access int_arr; -- represented C-side with int* | ||
|
||
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 "Array length: " & integer'image(c_intArr.all'length); | ||
|
||
for i in 0 to c_intArr.all'right loop | ||
report "c_intArr[" & integer'image(i) &"] = " & integer'image(c_intArr.all(i)); | ||
end loop; | ||
|
||
wait; | ||
end process; | ||
|
||
end architecture RTL; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
entity tb is | ||
end entity tb; | ||
|
||
architecture RTL of tb is | ||
|
||
begin | ||
|
||
process | ||
function c_intArrSize return integer is-- represented C-side with int | ||
begin | ||
assert false report "c_intArrSize VHPI" severity failure; | ||
end; | ||
attribute foreign of c_intArrSize : function is "VHPIDIRECT getIntArrSize"; -- getIntArrSize is the C-side function name | ||
|
||
type int_arr is array(0 to c_intArrSize-1) of integer; | ||
type int_arr_ptr is access int_arr; -- represented C-side with int* | ||
|
||
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 "Array length: " & integer'image(c_intArr.all'length); | ||
|
||
for i in 0 to c_intArr.all'right loop | ||
report "c_intArr[" & integer'image(i) &"] = " & integer'image(c_intArr.all(i)) & ". Set to: " & integer'image(-2*c_intArr.all(i)); | ||
c_intArr.all(i) := -2*c_intArr.all(i); | ||
end loop; | ||
|
||
wait; | ||
end process; | ||
|
||
end architecture RTL; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <malloc.h> | ||
#include <stddef.h> | ||
|
||
int* intArray; | ||
int* getIntArr_ptr(int arraySize){//function acts like a constructor so initialise the variable | ||
if(intArray != NULL){ | ||
free(intArray); | ||
} | ||
if(arraySize > 0){ | ||
intArray = malloc(arraySize*sizeof(int)); | ||
for (int i = 0; i < arraySize; i++) | ||
{ | ||
intArray[i] = 11*(i+1); | ||
} | ||
} | ||
return intArray; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/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 (-gg_array_size=2)" | ||
./tb -gg_array_size=2 | ||
|
||
echo "Execute tb (-gg_array_size=6)" | ||
./tb -gg_array_size=6 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
entity tb is | ||
generic( | ||
g_array_size : integer := 1 | ||
); | ||
end entity tb; | ||
|
||
architecture RTL of tb is | ||
begin | ||
process | ||
type int_arr is array(0 to g_array_size-1) of integer; | ||
type int_arr_ptr is access int_arr; -- represented C-side with int* | ||
|
||
impure function c_intArr_ptr(arraySize: integer) 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(g_array_size); | ||
begin | ||
report "ArraySize Interface Generic: " & integer'image(g_array_size); | ||
|
||
for i in 0 to g_array_size-1 loop | ||
report "c_intArr[" & integer'image(i) & "] = " & integer'image(c_intArr(i)); | ||
end loop; | ||
|
||
c_intArr := c_intArr_ptr(0); | ||
wait; | ||
end process; | ||
|
||
end architecture RTL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was mislocated as a result of merging/cherry-picking.