Skip to content

Commit 471c7e2

Browse files
Consolidate VHPI code
We had many copies of the VHPI marshalling/unmarshalling code. Signed-off-by: Anton Blanchard <[email protected]>
1 parent 6a9bc46 commit 471c7e2

File tree

6 files changed

+109
-189
lines changed

6 files changed

+109
-189
lines changed

Makefile

+10-10
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,17 @@ fpga/soc_reset_tb.o: fpga/soc_reset.o
8888
soc_reset_tb: fpga/soc_reset_tb.o fpga/soc_reset.o
8989
$(GHDL) -e $(GHDLFLAGS) --workdir=fpga soc_reset_tb
9090

91-
core_tb: core_tb.o sim_bram_helpers_c.o sim_console_c.o sim_jtag_socket_c.o
92-
$(GHDL) -e $(GHDLFLAGS) -Wl,sim_bram_helpers_c.o -Wl,sim_console_c.o -Wl,sim_jtag_socket_c.o $@
91+
core_tb: core_tb.o sim_vhpi_c.o sim_bram_helpers_c.o sim_console_c.o sim_jtag_socket_c.o
92+
$(GHDL) -e $(GHDLFLAGS) -Wl,sim_vhpi_c.o -Wl,sim_bram_helpers_c.o -Wl,sim_console_c.o -Wl,sim_jtag_socket_c.o $@
9393

9494
fetch_tb: fetch_tb.o
9595
$(GHDL) -e $(GHDLFLAGS) $@
9696

97-
icache_tb: icache_tb.o
98-
$(GHDL) -e $(GHDLFLAGS) -Wl,sim_bram_helpers_c.o $@
97+
icache_tb: icache_tb.o sim_vhpi_c.o sim_bram_helpers_c.o
98+
$(GHDL) -e $(GHDLFLAGS) -Wl,sim_vhpi_c.o -Wl,sim_bram_helpers_c.o $@
9999

100-
dcache_tb: dcache_tb.o
101-
$(GHDL) -e $(GHDLFLAGS) -Wl,sim_bram_helpers_c.o $@
100+
dcache_tb: dcache_tb.o sim_vhpi_c.o sim_bram_helpers_c.o
101+
$(GHDL) -e $(GHDLFLAGS) -Wl,sim_vhpi_c.o -Wl,sim_bram_helpers_c.o $@
102102

103103
plru_tb: plru_tb.o
104104
$(GHDL) -e $(GHDLFLAGS) $@
@@ -121,11 +121,11 @@ countzero_tb: countzero_tb.o
121121
simple_ram_tb: simple_ram_tb.o
122122
$(GHDL) -e $(GHDLFLAGS) $@
123123

124-
wishbone_bram_tb: sim_bram_helpers_c.o wishbone_bram_tb.o
125-
$(GHDL) -e $(GHDLFLAGS) -Wl,sim_bram_helpers_c.o $@
124+
wishbone_bram_tb: sim_vhpi_c.o sim_bram_helpers_c.o wishbone_bram_tb.o
125+
$(GHDL) -e $(GHDLFLAGS) -Wl,sim_vhpi_c.o -Wl,sim_bram_helpers_c.o $@
126126

127-
dmi_dtm_tb: dmi_dtm_tb.o sim_bram_helpers_c.o
128-
$(GHDL) -e $(GHDLFLAGS) -Wl,sim_bram_helpers_c.o $@
127+
dmi_dtm_tb: dmi_dtm_tb.o sim_vhpi_c.o sim_bram_helpers_c.o
128+
$(GHDL) -e $(GHDLFLAGS) -Wl,sim_vhpi_c.o -Wl,sim_bram_helpers_c.o $@
129129

130130
tests = $(sort $(patsubst tests/%.out,%,$(wildcard tests/*.out)))
131131

sim_bram_helpers_c.c

+2-81
Original file line numberDiff line numberDiff line change
@@ -9,91 +9,12 @@
99
#include <sys/types.h>
1010
#include <sys/stat.h>
1111

12+
#include "sim_vhpi_c.h"
13+
1214
#undef DEBUG
1315

1416
#define ALIGN_UP(VAL, SIZE) (((VAL) + ((SIZE)-1)) & ~((SIZE)-1))
1517

16-
#define vhpi0 2 /* forcing 0 */
17-
#define vhpi1 3 /* forcing 1 */
18-
19-
struct int_bounds
20-
{
21-
int left;
22-
int right;
23-
char dir;
24-
unsigned int len;
25-
};
26-
27-
struct fat_pointer
28-
{
29-
void *base;
30-
struct int_bounds *bounds;
31-
};
32-
33-
static char *from_string(void *__p)
34-
{
35-
struct fat_pointer *p = __p;
36-
unsigned long len = p->bounds->len;
37-
char *m;
38-
39-
m = malloc(len+1);
40-
if (!m) {
41-
perror("malloc");
42-
exit(1);
43-
}
44-
45-
memcpy(m, p->base, len);
46-
m[len] = 0x0;
47-
48-
return m;
49-
}
50-
51-
static uint64_t from_std_logic_vector(unsigned char *p, unsigned long len)
52-
{
53-
unsigned long ret = 0;
54-
55-
if (len > 64) {
56-
fprintf(stderr, "%s: invalid length %lu\n", __func__, len);
57-
exit(1);
58-
}
59-
60-
for (unsigned long i = 0; i < len; i++) {
61-
unsigned char bit;
62-
63-
if (*p == vhpi0) {
64-
bit = 0;
65-
} else if (*p == vhpi1) {
66-
bit = 1;
67-
} else {
68-
fprintf(stderr, "%s: bad bit %d\n", __func__, *p);
69-
bit = 0;
70-
}
71-
72-
ret = (ret << 1) | bit;
73-
p++;
74-
}
75-
76-
return ret;
77-
}
78-
79-
static void to_std_logic_vector(unsigned long val, unsigned char *p,
80-
unsigned long len)
81-
{
82-
if (len > 64) {
83-
fprintf(stderr, "%s: invalid length %lu\n", __func__, len);
84-
exit(1);
85-
}
86-
87-
for (unsigned long i = 0; i < len; i++) {
88-
if ((val >> (len-1-i) & 1))
89-
*p = vhpi1;
90-
else
91-
*p = vhpi0;
92-
93-
p++;
94-
}
95-
}
96-
9718
#define MAX_REGIONS 128
9819

9920
struct ram_behavioural {

sim_console_c.c

+1-49
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,11 @@
66
#include <termios.h>
77
#include <unistd.h>
88
#include <poll.h>
9+
#include "sim_vhpi_c.h"
910

1011
/* Should we exit simulation on ctrl-c or pass it through? */
1112
#define EXIT_ON_CTRL_C
1213

13-
#define vhpi0 2 /* forcing 0 */
14-
#define vhpi1 3 /* forcing 1 */
15-
16-
static uint64_t from_std_logic_vector(unsigned char *p, unsigned long len)
17-
{
18-
unsigned long ret = 0;
19-
20-
if (len > 64) {
21-
fprintf(stderr, "%s: invalid length %lu\n", __func__, len);
22-
exit(1);
23-
}
24-
25-
for (unsigned long i = 0; i < len; i++) {
26-
unsigned char bit;
27-
28-
if (*p == vhpi0) {
29-
bit = 0;
30-
} else if (*p == vhpi1) {
31-
bit = 1;
32-
} else {
33-
fprintf(stderr, "%s: bad bit %d\n", __func__, *p);
34-
bit = 0;
35-
}
36-
37-
ret = (ret << 1) | bit;
38-
p++;
39-
}
40-
41-
return ret;
42-
}
43-
44-
static void to_std_logic_vector(unsigned long val, unsigned char *p,
45-
unsigned long len)
46-
{
47-
if (len > 64) {
48-
fprintf(stderr, "%s: invalid length %lu\n", __func__, len);
49-
exit(1);
50-
}
51-
52-
for (unsigned long i = 0; i < len; i++) {
53-
if ((val >> (len-1-i) & 1))
54-
*p = vhpi1;
55-
else
56-
*p = vhpi0;
57-
58-
p++;
59-
}
60-
}
61-
6214
static struct termios oldt;
6315

6416
static void disable_raw_mode(void)

sim_jtag_socket_c.c

+1-49
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,12 @@
99
#include <sys/types.h>
1010
#include <sys/socket.h>
1111
#include <netinet/in.h>
12+
#include "sim_vhpi_c.h"
1213

1314
/* XXX Make that some parameter */
1415
#define TCP_PORT 13245
1516
#define MAX_PACKET 32
1617

17-
#define vhpi0 2 /* forcing 0 */
18-
#define vhpi1 3 /* forcing 1 */
19-
20-
static void to_std_logic_vector(unsigned long val, unsigned char *p,
21-
unsigned long len)
22-
{
23-
if (len > 64) {
24-
fprintf(stderr, "%s: invalid length %lu\n", __func__, len);
25-
exit(1);
26-
}
27-
28-
for (unsigned long i = 0; i < len; i++) {
29-
if ((val >> (len-1-i) & 1))
30-
*p = vhpi1;
31-
else
32-
*p = vhpi0;
33-
34-
p++;
35-
}
36-
}
37-
38-
static uint64_t from_std_logic_vector(unsigned char *p, unsigned long len)
39-
{
40-
unsigned long ret = 0;
41-
42-
if (len > 64) {
43-
fprintf(stderr, "%s: invalid length %lu\n", __func__, len);
44-
exit(1);
45-
}
46-
47-
for (unsigned long i = 0; i < len; i++) {
48-
unsigned char bit;
49-
50-
if (*p == vhpi0) {
51-
bit = 0;
52-
} else if (*p == vhpi1) {
53-
bit = 1;
54-
} else {
55-
fprintf(stderr, "%s: bad bit %d\n", __func__, *p);
56-
bit = 0;
57-
}
58-
59-
ret = (ret << 1) | bit;
60-
p++;
61-
}
62-
63-
return ret;
64-
}
65-
6618
static int fd = -1;
6719
static int cfd = -1;
6820

sim_vhpi_c.c

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

sim_vhpi_c.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdint.h>
2+
3+
#define vhpi0 2 /* forcing 0 */
4+
#define vhpi1 3 /* forcing 1 */
5+
6+
char *from_string(void *__p);
7+
8+
uint64_t from_std_logic_vector(unsigned char *p, unsigned long len);
9+
10+
void to_std_logic_vector(unsigned long val, unsigned char *p,
11+
unsigned long len);

0 commit comments

Comments
 (0)