|
| 1 | +#include <stdlib.h> |
| 2 | +#include <stdint.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdbool.h> |
| 5 | +#include <assert.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +#include <ghdl.h> |
| 9 | + |
| 10 | +typedef struct rec_t { |
| 11 | + char r_char; |
| 12 | + int32_t r_int; |
| 13 | +} rec_t; |
| 14 | + |
| 15 | +typedef enum {standby, start, busy, done} enum_t; |
| 16 | + |
| 17 | +void testCinterface( |
| 18 | + char v_char, |
| 19 | + int32_t v_int, |
| 20 | + uint32_t v_nat, |
| 21 | + uint32_t v_pos, |
| 22 | + double v_real, |
| 23 | + bool v_bool, |
| 24 | + bool v_bit, |
| 25 | + int64_t v_time, |
| 26 | + rec_t* v_rec, |
| 27 | + uint8_t v_enum, |
| 28 | + ghdl_NaturalDimArr_t* v_str, |
| 29 | + ghdl_NaturalDimArr_t* v_vec_int, |
| 30 | + ghdl_NaturalDimArr_t* v_vec_real, |
| 31 | + ghdl_NaturalDimArr_t* v_vec_bool, |
| 32 | + ghdl_NaturalDimArr_t* v_vec_bit, |
| 33 | + ghdl_NaturalDimArr_t* v_vec_phy, |
| 34 | + ghdl_NaturalDimArr_t* v_vec_rec, |
| 35 | + ghdl_NaturalDimArr_t* v_vec_enum, |
| 36 | + ghdl_NaturalDimArr_t* v_2vec_real |
| 37 | +) { |
| 38 | + assert(v_char == 'k'); |
| 39 | + printf("v_char : %c\n", v_char); |
| 40 | + |
| 41 | + assert(v_int == -6); |
| 42 | + printf("v_int : %d\n", v_int); |
| 43 | + |
| 44 | + assert(v_nat == 9); |
| 45 | + printf("v_nat : %d\n", v_nat); |
| 46 | + |
| 47 | + assert(v_pos == 3); |
| 48 | + printf("v_pos : %d\n", v_pos); |
| 49 | + |
| 50 | + assert(v_real == 3.34); |
| 51 | + printf("v_real : %f\n", v_real); |
| 52 | + |
| 53 | + assert(v_bool == true); |
| 54 | + printf("v_bool : %d\n", v_bool); |
| 55 | + |
| 56 | + assert(v_bit == true); |
| 57 | + printf("v_bit : %d\n", v_bit); |
| 58 | + |
| 59 | + assert(v_time == 20e6); |
| 60 | + printf("v_time : %d\n", v_time); |
| 61 | + |
| 62 | + assert(v_rec != NULL); |
| 63 | + assert(v_rec->r_char == 'y'); |
| 64 | + assert(v_rec->r_int == 5); |
| 65 | + printf("v_rec : %p %c %d\n", v_rec, v_rec->r_char, v_rec->r_int); |
| 66 | + |
| 67 | + assert(v_enum == busy); |
| 68 | + printf("v_enum : %d %d\n", v_enum, busy); |
| 69 | + |
| 70 | + char* str = ghdlToString(v_str); |
| 71 | + printf("v_str : %p '%s' [%d]\n", v_str->array, str, strlen(str)); |
| 72 | + |
| 73 | + int* len = malloc(2 * sizeof(int)); |
| 74 | + |
| 75 | + int32_t* vec_int; |
| 76 | + ghdlToArray(v_vec_int, (void**)&vec_int, len, 1); |
| 77 | + assert(vec_int[0] == 11); |
| 78 | + assert(vec_int[1] == 22); |
| 79 | + assert(vec_int[2] == 33); |
| 80 | + assert(vec_int[3] == 44); |
| 81 | + assert(vec_int[4] == 55); |
| 82 | + printf("v_vec_int : %p [%d]\n", vec_int, len[0]); |
| 83 | + |
| 84 | + double* vec_real; |
| 85 | + ghdlToArray(v_vec_real, (void**)&vec_real, len, 1); |
| 86 | + assert(vec_real[0] == 0.5); |
| 87 | + assert(vec_real[1] == 1.75); |
| 88 | + assert(vec_real[2] == 3.33); |
| 89 | + assert(vec_real[3] == -0.125); |
| 90 | + assert(vec_real[4] == -0.67); |
| 91 | + assert(vec_real[5] == -2.21); |
| 92 | + printf("v_vec_real : %p [%d]\n", vec_real, len[0]); |
| 93 | + |
| 94 | + bool* vec_bool; |
| 95 | + ghdlToArray(v_vec_bool, (void**)&vec_bool, len, 1); |
| 96 | + assert(vec_bool[0] == 0); |
| 97 | + assert(vec_bool[1] == 1); |
| 98 | + assert(vec_bool[2] == 1); |
| 99 | + assert(vec_bool[3] == 0); |
| 100 | + printf("v_vec_bool : %p [%d]\n", vec_bool, len[0]); |
| 101 | + |
| 102 | + bool* vec_bit; |
| 103 | + ghdlToArray(v_vec_bit, (void**)&vec_bit, len, 1); |
| 104 | + assert(vec_bit[0] == 1); |
| 105 | + assert(vec_bit[1] == 0); |
| 106 | + assert(vec_bit[2] == 1); |
| 107 | + assert(vec_bit[3] == 0); |
| 108 | + printf("v_vec_bit : %p [%d]\n", vec_bit, len[0]); |
| 109 | + |
| 110 | + int64_t* vec_phy; |
| 111 | + ghdlToArray(v_vec_phy, (void**)&vec_phy, len, 1); |
| 112 | + assert(vec_phy[0] == 1e6); |
| 113 | + assert(vec_phy[1] == 50e3); |
| 114 | + assert(vec_phy[2] == 1.34e9); |
| 115 | + printf("v_vec_phy : %p [%d]\n", vec_phy, len[0]); |
| 116 | + |
| 117 | + rec_t* vec_rec; |
| 118 | + ghdlToArray(v_vec_rec, (void**)&vec_rec, len, 1); |
| 119 | + assert(vec_rec[0].r_char == 'x'); |
| 120 | + assert(vec_rec[0].r_int == 17); |
| 121 | + assert(vec_rec[1].r_char == 'y'); |
| 122 | + assert(vec_rec[1].r_int == 25); |
| 123 | + printf("v_vec_rec : %p [%d]\n", vec_rec, len[0]); |
| 124 | + |
| 125 | + uint8_t* vec_enum; |
| 126 | + ghdlToArray(v_vec_enum, (void**)&vec_enum, len, 1); |
| 127 | + assert(vec_enum[0] == start); |
| 128 | + assert(vec_enum[1] == busy); |
| 129 | + assert(vec_enum[2] == standby); |
| 130 | + printf("v_vec_enum : %p [%d]\n", vec_enum, len[0]); |
| 131 | + |
| 132 | + double* vec2_real_base; |
| 133 | + ghdlToArray(v_2vec_real, (void**)&vec2_real_base, len, 2); |
| 134 | + double (*vec2_real)[len[0]] = (double(*)[len[0]])vec2_real_base; |
| 135 | + assert(vec2_real[0][0] == 0.1); |
| 136 | + assert(vec2_real[0][1] == 0.25); |
| 137 | + assert(vec2_real[0][2] == 0.5); |
| 138 | + assert(vec2_real[1][0] == 3.33); |
| 139 | + assert(vec2_real[1][1] == 4.25); |
| 140 | + assert(vec2_real[1][2] == 5.0); |
| 141 | + printf("v_2vec_real : %p [%d, %d]\n", vec_enum, len[1], len[0]); |
| 142 | +} |
| 143 | + |
| 144 | +void getString(ghdl_NaturalDimArr_t* ptr) { |
| 145 | + *ptr = ghdlFromString("HELLO WORLD"); |
| 146 | +} |
| 147 | + |
| 148 | +void getIntVec(ghdl_NaturalDimArr_t* ptr) { |
| 149 | + int32_t vec[6] = {11, 22, 33, 44, 55}; |
| 150 | + int32_t len[1] = {5}; |
| 151 | + int x; |
| 152 | + for ( x=0 ; x<len[0] ; x++ ) { |
| 153 | + printf("%d: %d\n", x, vec[x]); |
| 154 | + } |
| 155 | + *ptr = ghdlFromArray(vec, len, 1); |
| 156 | +} |
| 157 | + |
| 158 | +ghdl_AccNaturalDimArr_t* getLine() { |
| 159 | + return ghdlAccFromString("HELLO WORLD"); |
| 160 | +} |
0 commit comments