From 579fd30e60bde66a9d53603e412727204788ae67 Mon Sep 17 00:00:00 2001 From: RocketRoss Date: Wed, 8 Apr 2020 18:38:48 +0200 Subject: [PATCH] Handle and test 3D int arrays. --- doc/examples/vhpidirect/demo/ghdl.h | 38 +++++++++++++++++++++ doc/examples/vhpidirect/demo/main.c | 51 +++++++++++++++++++++++++++-- doc/examples/vhpidirect/demo/tb.vhd | 26 +++++++++++++-- 3 files changed, 110 insertions(+), 5 deletions(-) diff --git a/doc/examples/vhpidirect/demo/ghdl.h b/doc/examples/vhpidirect/demo/ghdl.h index 164b8d767c..a24b08722b 100644 --- a/doc/examples/vhpidirect/demo/ghdl.h +++ b/doc/examples/vhpidirect/demo/ghdl.h @@ -40,6 +40,12 @@ typedef struct { bounds2D_t* bounds; } ghdl_Natural2DimArr_t; +// Unconstrained array with 3 dimensions of type 'natural' +typedef struct { + void* array; + bounds3D_t* bounds; +} ghdl_Natural3DimArr_t; + // Access to an unconstrained array with 1 dimension of type 'natural' typedef struct { range_t range; @@ -72,6 +78,22 @@ void print2d(ghdl_Natural2DimArr_t* ptr) { printf("bounds2.len: %d\n", ptr->bounds->dim_2.len); } +void print3d(ghdl_Natural3DimArr_t* ptr) { + printf("array: %p\n", ptr->array); + printf("bounds: %p\n", ptr->bounds); + printf("bounds1.left: %d\n", ptr->bounds->dim_1.left); + printf("bounds1.right: %d\n", ptr->bounds->dim_1.right); + printf("bounds1.dir: %d\n", ptr->bounds->dim_1.dir); + printf("bounds1.len: %d\n", ptr->bounds->dim_1.len); + printf("bounds2.left: %d\n", ptr->bounds->dim_2.left); + printf("bounds2.right: %d\n", ptr->bounds->dim_2.right); + printf("bounds2.dir: %d\n", ptr->bounds->dim_2.dir); + printf("bounds2.len: %d\n", ptr->bounds->dim_2.len); + printf("bounds3.left: %d\n", ptr->bounds->dim_3.left); + printf("bounds3.right: %d\n", ptr->bounds->dim_3.right); + printf("bounds3.dir: %d\n", ptr->bounds->dim_3.dir); + printf("bounds3.len: %d\n", ptr->bounds->dim_3.len); +} /* * Convert a fat pointer of an unconstrained string, to a (null terminated) C string */ @@ -224,6 +246,22 @@ ghdl_Natural2DimArr_t ghdlFromArray2d(void* vec, int* len, int dims){ return (ghdl_Natural2DimArr_t){.array= a, .bounds=b}; } +ghdl_Natural3DimArr_t ghdlFromArray3d(void* vec, int* len, int dims){ + bounds3D_t* b = malloc(sizeof(bounds3D_t)); + void* a; + assert(b != NULL); + + a = malloc(sizeof(int)*len[0]*len[1]*len[2]); + memmove(a, vec, sizeof(int)*len[0]*len[1]*len[2]); + vec = a; + + ghdlSetRange(&(b->dim_1), len[0], false); + ghdlSetRange(&(b->dim_2), len[1], false); + ghdlSetRange(&(b->dim_3), len[2], false); + + return (ghdl_Natural3DimArr_t){.array= a, .bounds=b}; +} + /* * Convert an access to an unconstrained string, to a (null terminated) C string */ diff --git a/doc/examples/vhpidirect/demo/main.c b/doc/examples/vhpidirect/demo/main.c index 9106039683..75874f1f2f 100644 --- a/doc/examples/vhpidirect/demo/main.c +++ b/doc/examples/vhpidirect/demo/main.c @@ -36,7 +36,8 @@ void testCinterface( ghdl_NaturalDimArr_t* v_vec_rec, ghdl_NaturalDimArr_t* v_vec_enum, ghdl_NaturalDimArr_t* v_2vec_real, - ghdl_Natural2DimArr_t* v_mat_int + ghdl_Natural2DimArr_t* v_mat_int, + ghdl_Natural3DimArr_t* v_3d_int ) { assert(v_logic == HDL_H); printf("v_logic : %c\n", HDL_LOGIC_STATE[v_logic]); @@ -164,6 +165,27 @@ void testCinterface( } } printf("v_mat_int : %p [%d,%d]\n\n", mat_int, len2[0], len2[1]); + + printf("\nVerify the 3D GHDL array in C\n"); + //print3d(v_3d_int); + int* len3 = malloc(3 * sizeof(int)); + + int32_t* d3_int; + ghdlToArray(v_3d_int, (void**)&d3_int, len3, 3); + for(int i = 0; i < len3[0]; i++) + { + for (int j = 0; j < len3[1]; j++) + { + for (int k = 0; k < len3[2]; k++) + { + printf("C assert: %d == (val: %d) @ [%d,%d,%d](%d)\n", 11*(i*len3[1]*len3[2]+j*len3[2]+k+1), d3_int[i*len3[1]*len3[2]+j*len3[2]+k], i, j, k, i*len3[1]*len3[2]+j*len3[2]+k); + assert(d3_int[i*len3[1]*len3[2]+j*len3[2]+k] == 11*(i*len3[1]*len3[2]+j*len3[2]+k+1)); + } + } + } + printf("v_3d_int : %p [%d,%d,%d]\n\n", d3_int, len3[0], len3[1], len3[2]); + + printf("end testCinterface\n\n"); } void getString(ghdl_NaturalDimArr_t* ptr) { @@ -209,7 +231,32 @@ void getIntMat(ghdl_Natural2DimArr_t* ptr){ } printf("\n"); } - printf("\n"); +} + +void getInt3d(ghdl_Natural3DimArr_t* ptr){ + int32_t d3[2][4][3]; + int32_t len[3] = {2, 4, 3}; + int x, y, z; + for ( x=0 ; x) of enum_t; type real_2vec_t is array (natural range <>, natural range <>) of real; - + type int_2vec_t is array(natural range <>, natural range <>) of integer; + type int_3vec_t is array(natural range <>, natural range <>, natural range <>) of integer; begin process @@ -50,7 +51,8 @@ begin v_vec_rec : rec_vec_t := (('x', 17),('y', 25)); v_vec_enum : enum_vec_t := (start, busy, standby); v_2vec_real : real_2vec_t := ((0.1, 0.25, 0.5),(3.33, 4.25, 5.0)); - v_mat_int : int_2vec_t := ((11, 22, 33), (44, 55, 66)) + v_mat_int : int_2vec_t := ((11, 22, 33), (44, 55, 66)); + v_3d_int : int_3vec_t := ( ((11, 22, 33), (44, 55, 66)), ((77, 88, 99), (110, 121, 132)) ) ) is begin assert false report "VHPIDIRECT testCinterface" severity failure; end; attribute foreign of testCinterface : procedure is "VHPIDIRECT testCinterface"; @@ -67,6 +69,10 @@ begin begin assert false report "VHPIDIRECT getIntMat" severity failure; end; attribute foreign of getIntMat : function is "VHPIDIRECT getIntMat"; + function getInt3d return int_3vec_t is + begin assert false report "VHPIDIRECT getInt3d" severity failure; end; + attribute foreign of getInt3d : function is "VHPIDIRECT getInt3d"; + function getLine return line is begin assert false report "VHPIDIRECT getLine" severity failure; end; attribute foreign of getLine : function is "VHPIDIRECT getLine"; @@ -74,6 +80,7 @@ begin constant g_str: string := getString; constant g_int_vec: int_vec_t := getIntVec; constant g_int_mat: int_2vec_t := getIntMat; + constant g_int_3d: int_3vec_t := getInt3d; variable g_line: line := getLine; @@ -114,7 +121,8 @@ begin v_vec_rec => (('x', 17),('y', 25)), v_vec_enum => (start, busy, standby), v_2vec_real => ((0.1, 0.25, 0.5),(3.33, 4.25, 5.0)), - v_mat_int => ((11, 22, 33), (44, 55, 66)) + v_mat_int => ((11, 22, 33), (44, 55, 66)), + v_3d_int => ( ((11, 22, 33), (44, 55, 66)), ((77, 88, 99), (110, 121, 132)) ) ); report "g_str'length: " & integer'image(g_str'length) severity note; @@ -166,6 +174,18 @@ begin end loop ; end loop ; + spareInt := 0; + report "g_int_3d'length: " & integer'image(g_int_3d'length) severity note; + for i in g_int_3d'range(1) loop + for j in g_int_3d'range(2) loop + for k in g_int_3d'range(3) loop + spareInt := spareInt + 1; + assert g_int_3d(i, j, k) = 11*spareInt severity error; + report "Asserted 3D [" & integer'image(i) & "," & integer'image(j) & "," & integer'image(k) & "]: " & integer'image(g_int_3d(i, j, k)) severity note; + end loop; + end loop ; + end loop ; + wait; end process; end;