forked from ghdl/ghdl-cosim
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vffi_user.h
159 lines (138 loc) · 3.45 KB
/
vffi_user.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef VFFI_TYPES_H
#define VFFI_TYPES_H
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <vhpi_user.h>
/*
* std_logic
*/
static const char std_logic_char[] = { 'U', 'X', '0', '1', 'Z', 'W', 'L', 'H', '-'};
enum std_logic_states {
HDL_U = vhpiU, /* uninitialized */
HDL_X = vhpiX, /* unknown */
HDL_0 = vhpi0, /* forcing 0 */
HDL_1 = vhpi1, /* forcing 1 */
HDL_Z = vhpiZ, /* high impedance */
HDL_W = vhpiW, /* weak unknown */
HDL_L = vhpiL, /* weak 0 */
HDL_H = vhpiH, /* weak 1 */
HDL_D = vhpiDontCare /* don't care */
};
// Compress an array of std_logic (char) to an array of bits (8 times smaller)
void vfficharArr2bitArr(
char* din,
unsigned char* dout,
int blen
) {
for (int i = 0; i < blen; i++) {
char d = 1;
for (int y = 0; y < 8; y++) {
if ((*din == HDL_1) || (*din == HDL_H)) {
*dout |= d;
} else {
*dout &= ~(d);
}
d<<=1;
din++;
}
dout++;
}
return;
}
// Extract an array of bits to an array of std_logic (char) (8 times larger)
void vffibitArr2charArr(
unsigned char* din,
char* dout,
int blen
) {
for (int i = 0; i < blen; i++) {
char d = *din;
for (int y = 0; y < 8; y++) {
*dout++ = (d & 1 == 1) ? HDL_1 : HDL_0;
d>>=1;
}
din++;
}
return;
}
#endif
/*
* Fat pointer types
*/
// 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;
// Unconstrained array with dimensions of type 'natural'
typedef struct {
void* data;
range_t* bounds;
} vffiNaturalDimArr_t;
// Access to an unconstrained array with 1 dimension of type 'natural'
typedef struct {
range_t* range;
uint8_t data[];
} vffiNaturalDimArrAccess_t;
// A line, which is an access to an unconstrained string
typedef struct {
range_t range;
char data[];
} vffiLine_t;
/*
* Convert fat pointers to C types
*/
// Convert a fat pointer of an unconstrained string, to a (null terminated) C string
char* vffiNullTerminatedString(
vffiNaturalDimArr_t* ptr
) {
assert(ptr != NULL);
assert(ptr->bounds != NULL);
int len = ptr->bounds[0].len;
char* str = malloc(sizeof(char) * len + 1);
strncpy(str, ptr->data, len);
str[len] = '\0';
return str;
}
// Convert a fat pointer of an uncontrained array with dimensions of type 'natural', to C types
void vffiNaturalDimArrGet(
vffiNaturalDimArr_t* ptr,
void** vec,
int* len,
int num
) {
assert(ptr != NULL);
range_t* bounds = ptr->bounds;
assert(bounds != NULL);
*vec = ptr->data;
for (int i=0; i<num; i++) {
len[num-1-i] = bounds[i].len;
}
}
// Convert a fat pointer of an access to an unconstrained string, to a (null terminated) C string
char* vffiNullTerminatedStringAccess(
vffiNaturalDimArrAccess_t *ptr
) {
char *string = malloc(ptr->range[0].len + 1);
strncpy(string, ptr->data, ptr->range[0].len);
string[ptr->range[0].len] = '\0';
}
/*
* Convert C types to fat pointers
*/
// Convert a (null terminated) C string to a fat pointer of a line
vffiLine_t* lineFromString(char *str) {
uint32_t length = strlen(str);
vffiLine_t *line = malloc(sizeof(vffiLine_t) + sizeof(char) * length);
line->range.left = 1;
line->range.right = length;
line->range.dir = 0;
line->range.len = length;
strncpy(line->data, str, length);
return line;
}