Add Tercel PHY reset synchronization
[microwatt.git] / sim_vhpi_c.c
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5
6 #include "sim_vhpi_c.h"
7
8 struct int_bounds
9 {
10 int left;
11 int right;
12 char dir;
13 unsigned int len;
14 };
15
16 struct fat_pointer
17 {
18 void *base;
19 struct int_bounds *bounds;
20 };
21
22 char *from_string(void *__p)
23 {
24 struct fat_pointer *p = __p;
25 unsigned long len = p->bounds->len;
26 char *m;
27
28 m = malloc(len+1);
29 if (!m) {
30 perror("malloc");
31 exit(1);
32 }
33
34 memcpy(m, p->base, len);
35 m[len] = 0x0;
36
37 return m;
38 }
39
40 uint64_t from_std_logic_vector(unsigned char *p, unsigned long len)
41 {
42 unsigned long ret = 0;
43
44 if (len > 64) {
45 fprintf(stderr, "%s: invalid length %lu\n", __func__, len);
46 exit(1);
47 }
48
49 for (unsigned long i = 0; i < len; i++) {
50 unsigned char bit;
51
52 if (*p == vhpi0) {
53 bit = 0;
54 } else if (*p == vhpi1) {
55 bit = 1;
56 } else {
57 fprintf(stderr, "%s: bad bit %d\n", __func__, *p);
58 bit = 0;
59 }
60
61 ret = (ret << 1) | bit;
62 p++;
63 }
64
65 return ret;
66 }
67
68 void to_std_logic_vector(unsigned long val, unsigned char *p,
69 unsigned long len)
70 {
71 if (len > 64) {
72 fprintf(stderr, "%s: invalid length %lu\n", __func__, len);
73 exit(1);
74 }
75
76 for (unsigned long i = 0; i < len; i++) {
77 if ((val >> (len-1-i) & 1))
78 *p = vhpi1;
79 else
80 *p = vhpi0;
81
82 p++;
83 }
84 }