forked from antonblanchard/microwatt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_vhpi_c.c
84 lines (68 loc) · 1.22 KB
/
sim_vhpi_c.c
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
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "sim_vhpi_c.h"
struct int_bounds
{
int left;
int right;
char dir;
unsigned int len;
};
struct fat_pointer
{
void *base;
struct int_bounds *bounds;
};
char *from_string(void *__p)
{
struct fat_pointer *p = __p;
unsigned long len = p->bounds->len;
char *m;
m = malloc(len+1);
if (!m) {
perror("malloc");
exit(1);
}
memcpy(m, p->base, len);
m[len] = 0x0;
return m;
}
uint64_t from_std_logic_vector(unsigned char *p, unsigned long len)
{
unsigned long ret = 0;
if (len > 64) {
fprintf(stderr, "%s: invalid length %lu\n", __func__, len);
exit(1);
}
for (unsigned long i = 0; i < len; i++) {
unsigned char bit;
if (*p == vhpi0) {
bit = 0;
} else if (*p == vhpi1) {
bit = 1;
} else {
fprintf(stderr, "%s: bad bit %d\n", __func__, *p);
bit = 0;
}
ret = (ret << 1) | bit;
p++;
}
return ret;
}
void to_std_logic_vector(unsigned long val, unsigned char *p,
unsigned long len)
{
if (len > 64) {
fprintf(stderr, "%s: invalid length %lu\n", __func__, len);
exit(1);
}
for (unsigned long i = 0; i < len; i++) {
if ((val >> (len-1-i) & 1))
*p = vhpi1;
else
*p = vhpi0;
p++;
}
}