#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include // code is partially taken from here: // https://github.com/ghdl/ghdl/pull/1059/commits/9f99a982927a8a09392ffabaee42299aa0024cd1 // std_logic values typedef enum { _U, _X, _0, _1, _Z, _W, _L, _H, _DASH } std_logic_t; char stdLogicToChar(std_logic_t logic) { switch(logic) { case _U: return 'U'; case _X: return 'X'; case _0: return '0'; case _1: return '1'; case _Z: return 'Z'; case _W: return 'W'; case _L: return 'L'; case _H: return 'H'; case _DASH: return '-'; } } // Range/bounds of a dimension of an unconstrained array with dimensions of type 'natural' typedef struct { int32_t left; int32_t right; int32_t dir; int32_t len; } range_t; // Range/bounds of an unconstrained array with 1, 2 or 3 dimensions of type 'natural' typedef struct { range_t dim_1; } bounds_t; typedef struct { range_t dim_1; range_t dim_2; } bounds2D_t; typedef struct { range_t dim_1; range_t dim_2; range_t dim_3; } bounds3D_t; // Unconstrained array with dimensions of type 'natural' typedef struct { void* array; bounds_t* bounds; } ghdl_NaturalDimArr_t; // Access to an unconstrained array with 1 dimension of type 'natural' typedef struct { range_t range; uint8_t array[]; } ghdl_AccNaturalDimArr_t; // Print custom types void print(ghdl_NaturalDimArr_t* ptr) { printf("array: %p\n", ptr->array); printf("bounds: %p\n", ptr->bounds); printf("bounds.left: %d\n", ptr->bounds->dim_1.left); printf("bounds.right: %d\n", ptr->bounds->dim_1.right); printf("bounds.dir: %d\n", ptr->bounds->dim_1.dir); printf("bounds.len: %d\n", ptr->bounds->dim_1.len); } // Convert a fat pointer of an unconstrained string, to a (null terminated) C string // @umarcor char* ghdlToString(ghdl_NaturalDimArr_t* ptr) { assert(ptr != NULL); assert(ptr->bounds != NULL); int len = ptr->bounds->dim_1.len; char* str = malloc(sizeof(char) * len + 1); strncpy(str, ptr->array, len); str[len] = '\0'; return str; } // string with length of 10 characters void constrained_string_proc(char str[10]) { printf("C-side constrained_string_proc: (%s)\n",str); } void string_proc(ghdl_NaturalDimArr_t* ptr) { char *str = ghdlToString(ptr); printf("C-side string_proc: %s\n",str); free(str); } int integer_fun(int i_arg) { int result = 1234; printf("C-side integer_fun: i_arg = %d, returning %d\n", i_arg, result); return result; } char bit_fun(char b_arg) { char result = b_arg; printf("C-side bit_fun: b_arg = %d, returning %d\n", b_arg, result); return result; } char std_logic_fun(char std_logic_arg) { char result = std_logic_arg; printf("C-side std_logic_fun: std_logic_arg = %d = \'%c\', returning %d\n", std_logic_arg, stdLogicToChar(std_logic_arg), result); return result; } void std_logic_vector_proc(ghdl_NaturalDimArr_t* ptr) { printf("C-side std_logic_proc: std_logic_vector_arg = "); char* array = (char*)ptr->array; for (int i = 0; i < ptr->bounds->dim_1.len; i++) { if (i%8 == 0) printf(" "); printf( "%c", stdLogicToChar(array[i]) ); } printf("\n"); } void std_logic_vector_proc_out(ghdl_NaturalDimArr_t* ptr) { printf("C-side std_logic_proc: std_logic_vector_arg = "); char* array = (char*)ptr->array; for (int i = 0; i < ptr->bounds->dim_1.len; i++) { if (i%8 == 0) printf(" "); array[i] = _1; printf( "%c", stdLogicToChar(array[i]) ); } printf("\n"); } typedef struct { int i; std_logic_t l; } simple_record_t; void simple_record_proc(simple_record_t *simple_rec) { printf("C-side simple_record_proc : %d \'%c\'\n", simple_rec->i, stdLogicToChar(simple_rec->l)); }