Skip to content

Commit

Permalink
Handle and test 2D int array
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRoss committed Apr 8, 2020
1 parent 3355535 commit 7c6379c
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 6 deletions.
35 changes: 35 additions & 0 deletions doc/examples/vhpidirect/demo/ghdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ typedef struct {
bounds_t* bounds;
} ghdl_NaturalDimArr_t;

// Unconstrained array with 2 dimensions of type 'natural'
typedef struct {
void* array;
bounds2D_t* bounds;
} ghdl_Natural2DimArr_t;

// Access to an unconstrained array with 1 dimension of type 'natural'
typedef struct {
range_t range;
Expand All @@ -53,6 +59,19 @@ void print(ghdl_NaturalDimArr_t* ptr) {
printf("bounds.len: %d\n", ptr->bounds->dim_1.len);
}

void print2d(ghdl_Natural2DimArr_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);
}

/*
* Convert a fat pointer of an unconstrained string, to a (null terminated) C string
*/
Expand Down Expand Up @@ -189,6 +208,22 @@ ghdl_NaturalDimArr_t ghdlFromArray(void* vec, int* len, int dims) {
return (ghdl_NaturalDimArr_t){.array= a, .bounds=b};
}

// @RocketRoss
ghdl_Natural2DimArr_t ghdlFromArray2d(void* vec, int* len, int dims){
bounds2D_t* b = malloc(sizeof(bounds2D_t));
void* a;
assert(b != NULL);

a = malloc(sizeof(int)*len[0]*len[1]);
memmove(a, vec, sizeof(int)*len[0]*len[1]);
vec = a;

ghdlSetRange(&(b->dim_1), len[0], false);
ghdlSetRange(&(b->dim_2), len[1], false);

return (ghdl_Natural2DimArr_t){.array= a, .bounds=b};
}

/*
* Convert an access to an unconstrained string, to a (null terminated) C string
*/
Expand Down
55 changes: 52 additions & 3 deletions doc/examples/vhpidirect/demo/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ void testCinterface(
ghdl_NaturalDimArr_t* v_vec_phy,
ghdl_NaturalDimArr_t* v_vec_rec,
ghdl_NaturalDimArr_t* v_vec_enum,
ghdl_NaturalDimArr_t* v_2vec_real
ghdl_NaturalDimArr_t* v_2vec_real,
ghdl_Natural2DimArr_t* v_mat_int
) {
assert(v_logic == HDL_H);
printf("v_logic : %c\n", HDL_LOGIC_STATE[v_logic]);
Expand Down Expand Up @@ -147,6 +148,22 @@ void testCinterface(
assert(vec2_real[1][1] == 4.25);
assert(vec2_real[1][2] == 5.0);
printf("v_2vec_real : %p [%d, %d]\n", vec_enum, len[1], len[0]);

printf("\nVerify GHDL Matrix in C\n");
//print2d(v_mat_int);
int* len2 = malloc(2 * sizeof(int));

int32_t* mat_int;
ghdlToArray(v_mat_int, (void**)&mat_int, len2, 2);
for (int i = 0; i < len2[1]; i++)
{
for (int j = 0; j < len2[0]; j++)
{
printf("C assert: %d == (val: %d) @ [%d,%d](%d)\n", 11*(i*len2[0]+j+1), mat_int[i*len2[1]+j], i, j, i*len2[1]+j);
assert(mat_int[i*len2[0]+j] == 11*(i*len2[0]+j+1));
}
}
printf("v_mat_int : %p [%d,%d]\n\n", mat_int, len2[0], len2[1]);
}

void getString(ghdl_NaturalDimArr_t* ptr) {
Expand All @@ -157,10 +174,42 @@ void getIntVec(ghdl_NaturalDimArr_t* ptr) {
int32_t vec[6] = {11, 22, 33, 44, 55};
int32_t len[1] = {5};
int x;
*ptr = ghdlFromArray(vec, len, 1);
printf("\n1D Array values [%d]:\n", len[0]);
for ( x=0 ; x<len[0] ; x++ ) {
printf("%d: %d\n", x, vec[x]);
printf("[%d]: %d\n", x, vec[x]);
}
*ptr = ghdlFromArray(vec, len, 1);
}

int getFlatArrayIndex(int* dimIndex, int* lens, int dims){
if(dims == 1){
return dimIndex[0];
}
else{
return dimIndex[dims-1] + (lens[dims-1]*getFlatArrayIndex(dimIndex, lens, dims-1));
}
}

void getIntMat(ghdl_Natural2DimArr_t* ptr){
int32_t mat[2][3];
int32_t len[2] = {2, 3};
int x, y;
for ( x=0 ; x<len[0] ; x++ ) {
for ( y=0 ; y<len[1] ; y++ ) {
int ind[] = {x, y};
int flatIndex = getFlatArrayIndex(ind, len, 2);
mat[x][y] = 11*(flatIndex+1);
}
}
*ptr = ghdlFromArray2d(mat, len, 2);
printf("\n2D Array values [%d,%d]:\n", len[0], len[1]);
for ( x=0 ; x<len[0] ; x++ ) {
for ( y=0 ; y<len[1] ; y++ ) {
printf("mat[%d][%d] = %d\t", x, y, mat[x][y]);
}
printf("\n");
}
printf("\n");
}

ghdl_AccNaturalDimArr_t* getLine() {
Expand Down
25 changes: 22 additions & 3 deletions doc/examples/vhpidirect/demo/tb.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ 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;
begin
process

Expand All @@ -48,7 +49,8 @@ begin
v_vec_time : time_vec_t := (1 ns, 50 ps, 1.34 us);
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_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))
) is
begin assert false report "VHPIDIRECT testCinterface" severity failure; end;
attribute foreign of testCinterface : procedure is "VHPIDIRECT testCinterface";
Expand All @@ -61,12 +63,17 @@ begin
begin assert false report "VHPIDIRECT getIntVec" severity failure; end;
attribute foreign of getIntVec : function is "VHPIDIRECT getIntVec";

function getIntMat return int_2vec_t is
begin assert false report "VHPIDIRECT getIntMat" severity failure; end;
attribute foreign of getIntMat : function is "VHPIDIRECT getIntMat";

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;

variable g_line: line := getLine;

Expand All @@ -82,6 +89,7 @@ begin
begin assert false report "VHPIDIRECT getBitValue" severity failure; end;
attribute foreign of getBitValue : function is "VHPIDIRECT getLogicIntValue";

variable spareInt: integer;
begin

testCinterface(
Expand All @@ -105,7 +113,8 @@ begin
v_vec_time => (1 ns, 50 ps, 1.34 us),
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_2vec_real => ((0.1, 0.25, 0.5),(3.33, 4.25, 5.0)),
v_mat_int => ((11, 22, 33), (44, 55, 66))
);

report "g_str'length: " & integer'image(g_str'length) severity note;
Expand Down Expand Up @@ -147,6 +156,16 @@ begin
assert 0 = getBitValue('0') severity error;
assert 1 = getBitValue('1') severity error;

spareInt := 0;
report "g_int_mat'length: " & integer'image(g_int_mat'length) severity note;
for i in g_int_mat'range(1) loop
for j in g_int_mat'range(2) loop
spareInt := spareInt + 1;
assert g_int_mat(i, j) = 11*spareInt severity error;
report "Asserted Mat [" & integer'image(i) & "," & integer'image(j) & "]: " & integer'image(g_int_mat(i, j)) severity note;
end loop ;
end loop ;

wait;
end process;
end;

0 comments on commit 7c6379c

Please sign in to comment.