Skip to content

Commit

Permalink
Collapse ghdlFromArray2/3D, neaten C tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRoss committed Apr 8, 2020
1 parent 579fd30 commit 83204ac
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 97 deletions.
98 changes: 20 additions & 78 deletions doc/examples/vhpidirect/demo/ghdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ 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;

// 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 All @@ -65,35 +53,6 @@ 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);
}

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 @@ -204,64 +163,47 @@ void ghdlSetRange(range_t* r, int len, bool reversed){
}
}

// @bradleyharden???
// @RocketRoss
/*
* Convert C types representing an unconstrained array with a dimension of type 'natural', to a fat pointer
*/

ghdl_NaturalDimArr_t ghdlFromArray(void* vec, int* len, int dims) {
bounds_t* b = malloc(sizeof(bounds_t));
void* b;
void* a;
assert(b != NULL);
switch (dims) {
case 3:
// TODO
b = malloc(sizeof(bounds3D_t));
a = malloc(sizeof(int)*len[0]*len[1]*len[2]);
memmove(a, vec, sizeof(int)*len[0]*len[1]*len[2]);
vec = a;

ghdlSetRange(&(((bounds3D_t*)b)->dim_1), len[0], false);
ghdlSetRange(&(((bounds3D_t*)b)->dim_2), len[1], false);
ghdlSetRange(&(((bounds3D_t*)b)->dim_3), len[2], false);
break;
case 2:
// TODO
b = malloc(sizeof(bounds2D_t));

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

ghdlSetRange(&(((bounds2D_t*)b)->dim_1), len[0], false);
ghdlSetRange(&(((bounds2D_t*)b)->dim_2), len[1], false);
break;
case 1:
b = malloc(sizeof(bounds_t));
a = malloc(sizeof(int)*len[0]);
memmove(a, vec, sizeof(int)*len[0]);
vec = a;

ghdlSetRange(&(b->dim_1), len[0], false);
ghdlSetRange(&(((bounds_t*)b)->dim_1), len[0], false);
}
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};
}

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
42 changes: 23 additions & 19 deletions doc/examples/vhpidirect/demo/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ typedef struct rec_t {

typedef enum {standby, start, busy, done} enum_t;

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 testCinterface(
char v_logic,
char v_ulogic,
Expand All @@ -36,8 +45,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_Natural3DimArr_t* v_3d_int
ghdl_NaturalDimArr_t* v_mat_int,
ghdl_NaturalDimArr_t* v_3d_int
) {
assert(v_logic == HDL_H);
printf("v_logic : %c\n", HDL_LOGIC_STATE[v_logic]);
Expand Down Expand Up @@ -160,8 +169,10 @@ void testCinterface(
{
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));
int ind[] = {i, j};
int flatIndex = getFlatArrayIndex(ind, len2, 2);
printf("C assert: %d == (val: %d) @ [%d,%d](%d)\n", 11*(flatIndex+1), mat_int[flatIndex+j], i, j, flatIndex+j);
assert(mat_int[flatIndex] == 11*(flatIndex+1));
}
}
printf("v_mat_int : %p [%d,%d]\n\n", mat_int, len2[0], len2[1]);
Expand All @@ -178,8 +189,10 @@ void testCinterface(
{
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));
int ind[] = {i, j, k};
int flatIndex = getFlatArrayIndex(ind, len3, 2);
printf("C assert: %d == (val: %d) @ [%d,%d,%d](%d)\n", 11*(flatIndex+1), d3_int[flatIndex], i, j, k, flatIndex);
assert(d3_int[flatIndex] == 11*(flatIndex+1));
}
}
}
Expand All @@ -203,16 +216,7 @@ void getIntVec(ghdl_NaturalDimArr_t* ptr) {
}
}

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){
void getIntMat(ghdl_NaturalDimArr_t* ptr){
int32_t mat[2][3];
int32_t len[2] = {2, 3};
int x, y;
Expand All @@ -223,7 +227,7 @@ void getIntMat(ghdl_Natural2DimArr_t* ptr){
mat[x][y] = 11*(flatIndex+1);
}
}
*ptr = ghdlFromArray2d(mat, len, 2);
*ptr = ghdlFromArray(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++ ) {
Expand All @@ -233,7 +237,7 @@ void getIntMat(ghdl_Natural2DimArr_t* ptr){
}
}

void getInt3d(ghdl_Natural3DimArr_t* ptr){
void getInt3d(ghdl_NaturalDimArr_t* ptr){
int32_t d3[2][4][3];
int32_t len[3] = {2, 4, 3};
int x, y, z;
Expand All @@ -246,7 +250,7 @@ void getInt3d(ghdl_Natural3DimArr_t* ptr){
}
}
}
*ptr = ghdlFromArray3d(d3, len, 3);
*ptr = ghdlFromArray(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++ ) {
Expand Down

0 comments on commit 83204ac

Please sign in to comment.