From 83204ac6c8d2ddd57555b928c07c26b089c7bcc8 Mon Sep 17 00:00:00 2001 From: RocketRoss Date: Wed, 8 Apr 2020 19:19:18 +0200 Subject: [PATCH] Collapse ghdlFromArray2/3D, neaten C tests --- doc/examples/vhpidirect/demo/ghdl.h | 98 ++++++----------------------- doc/examples/vhpidirect/demo/main.c | 42 +++++++------ 2 files changed, 43 insertions(+), 97 deletions(-) diff --git a/doc/examples/vhpidirect/demo/ghdl.h b/doc/examples/vhpidirect/demo/ghdl.h index a24b08722b5..0c77ac748c1 100644 --- a/doc/examples/vhpidirect/demo/ghdl.h +++ b/doc/examples/vhpidirect/demo/ghdl.h @@ -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; @@ -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 */ @@ -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 */ diff --git a/doc/examples/vhpidirect/demo/main.c b/doc/examples/vhpidirect/demo/main.c index 75874f1f2f0..d9b8428d0c0 100644 --- a/doc/examples/vhpidirect/demo/main.c +++ b/doc/examples/vhpidirect/demo/main.c @@ -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, @@ -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]); @@ -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]); @@ -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)); } } } @@ -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; @@ -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