Skip to content

Commit

Permalink
Handle and test 3D int arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRoss committed Apr 8, 2020
1 parent 7c6379c commit 579fd30
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 5 deletions.
38 changes: 38 additions & 0 deletions doc/examples/vhpidirect/demo/ghdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
51 changes: 49 additions & 2 deletions doc/examples/vhpidirect/demo/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<len[0] ; x++ ) {
for ( y=0 ; y<len[1] ; y++ ) {
for ( z=0 ; z<len[2] ; z++ ) {
int ind[] = {x, y, z};
int flatIndex = getFlatArrayIndex(ind, len, 3);
d3[x][y][z] = 11*(flatIndex+1);
}
}
}
*ptr = ghdlFromArray3d(d3, len, 3);
printf("\n3D Array values [%d,%d,%d]:\n", len[0], len[1], len[2]);
for ( x=0 ; x<len[0] ; x++ ) {
for ( y=0 ; y<len[1] ; y++ ) {
for ( z=0 ; z<len[2] ; z++ ) {
printf("d3[%d][%d][%d] = %d\t", x, y, z, d3[x][y][z]);
}
printf("\n");
}
printf("\n");
}
}

ghdl_AccNaturalDimArr_t* getLine() {
Expand Down
26 changes: 23 additions & 3 deletions doc/examples/vhpidirect/demo/tb.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ architecture arch of tb is
type enum_vec_t is array(natural range <>) 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

Expand All @@ -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";
Expand All @@ -67,13 +69,18 @@ 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";

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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

0 comments on commit 579fd30

Please sign in to comment.