diff --git a/Makefile b/Makefile index 2780db0..b55b786 100644 --- a/Makefile +++ b/Makefile @@ -88,17 +88,17 @@ fpga/soc_reset_tb.o: fpga/soc_reset.o soc_reset_tb: fpga/soc_reset_tb.o fpga/soc_reset.o $(GHDL) -e $(GHDLFLAGS) --workdir=fpga soc_reset_tb -core_tb: core_tb.o sim_bram_helpers_c.o sim_console_c.o sim_jtag_socket_c.o - $(GHDL) -e $(GHDLFLAGS) -Wl,sim_bram_helpers_c.o -Wl,sim_console_c.o -Wl,sim_jtag_socket_c.o $@ +core_tb: core_tb.o sim_vhpi_c.o sim_bram_helpers_c.o sim_console_c.o sim_jtag_socket_c.o + $(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 $@ fetch_tb: fetch_tb.o $(GHDL) -e $(GHDLFLAGS) $@ -icache_tb: icache_tb.o - $(GHDL) -e $(GHDLFLAGS) -Wl,sim_bram_helpers_c.o $@ +icache_tb: icache_tb.o sim_vhpi_c.o sim_bram_helpers_c.o + $(GHDL) -e $(GHDLFLAGS) -Wl,sim_vhpi_c.o -Wl,sim_bram_helpers_c.o $@ -dcache_tb: dcache_tb.o - $(GHDL) -e $(GHDLFLAGS) -Wl,sim_bram_helpers_c.o $@ +dcache_tb: dcache_tb.o sim_vhpi_c.o sim_bram_helpers_c.o + $(GHDL) -e $(GHDLFLAGS) -Wl,sim_vhpi_c.o -Wl,sim_bram_helpers_c.o $@ plru_tb: plru_tb.o $(GHDL) -e $(GHDLFLAGS) $@ @@ -121,11 +121,11 @@ countzero_tb: countzero_tb.o simple_ram_tb: simple_ram_tb.o $(GHDL) -e $(GHDLFLAGS) $@ -wishbone_bram_tb: sim_bram_helpers_c.o wishbone_bram_tb.o - $(GHDL) -e $(GHDLFLAGS) -Wl,sim_bram_helpers_c.o $@ +wishbone_bram_tb: sim_vhpi_c.o sim_bram_helpers_c.o wishbone_bram_tb.o + $(GHDL) -e $(GHDLFLAGS) -Wl,sim_vhpi_c.o -Wl,sim_bram_helpers_c.o $@ -dmi_dtm_tb: dmi_dtm_tb.o sim_bram_helpers_c.o - $(GHDL) -e $(GHDLFLAGS) -Wl,sim_bram_helpers_c.o $@ +dmi_dtm_tb: dmi_dtm_tb.o sim_vhpi_c.o sim_bram_helpers_c.o + $(GHDL) -e $(GHDLFLAGS) -Wl,sim_vhpi_c.o -Wl,sim_bram_helpers_c.o $@ tests = $(sort $(patsubst tests/%.out,%,$(wildcard tests/*.out))) diff --git a/sim_bram_helpers_c.c b/sim_bram_helpers_c.c index c5678e7..74dca3f 100644 --- a/sim_bram_helpers_c.c +++ b/sim_bram_helpers_c.c @@ -9,91 +9,12 @@ #include #include +#include "sim_vhpi_c.h" + #undef DEBUG #define ALIGN_UP(VAL, SIZE) (((VAL) + ((SIZE)-1)) & ~((SIZE)-1)) -#define vhpi0 2 /* forcing 0 */ -#define vhpi1 3 /* forcing 1 */ - -struct int_bounds -{ - int left; - int right; - char dir; - unsigned int len; -}; - -struct fat_pointer -{ - void *base; - struct int_bounds *bounds; -}; - -static 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; -} - -static 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; -} - -static 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++; - } -} - #define MAX_REGIONS 128 struct ram_behavioural { diff --git a/sim_console_c.c b/sim_console_c.c index 20c1f11..b3920c1 100644 --- a/sim_console_c.c +++ b/sim_console_c.c @@ -6,59 +6,11 @@ #include #include #include +#include "sim_vhpi_c.h" /* Should we exit simulation on ctrl-c or pass it through? */ #define EXIT_ON_CTRL_C -#define vhpi0 2 /* forcing 0 */ -#define vhpi1 3 /* forcing 1 */ - -static 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; -} - -static 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++; - } -} - static struct termios oldt; static void disable_raw_mode(void) diff --git a/sim_jtag_socket_c.c b/sim_jtag_socket_c.c index 651094f..b27f53b 100644 --- a/sim_jtag_socket_c.c +++ b/sim_jtag_socket_c.c @@ -9,60 +9,12 @@ #include #include #include +#include "sim_vhpi_c.h" /* XXX Make that some parameter */ #define TCP_PORT 13245 #define MAX_PACKET 32 -#define vhpi0 2 /* forcing 0 */ -#define vhpi1 3 /* forcing 1 */ - -static 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++; - } -} - -static 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; -} - static int fd = -1; static int cfd = -1; diff --git a/sim_vhpi_c.c b/sim_vhpi_c.c new file mode 100644 index 0000000..c909960 --- /dev/null +++ b/sim_vhpi_c.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include + +#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++; + } +} diff --git a/sim_vhpi_c.h b/sim_vhpi_c.h new file mode 100644 index 0000000..eeedd9b --- /dev/null +++ b/sim_vhpi_c.h @@ -0,0 +1,11 @@ +#include + +#define vhpi0 2 /* forcing 0 */ +#define vhpi1 3 /* forcing 1 */ + +char *from_string(void *__p); + +uint64_t from_std_logic_vector(unsigned char *p, unsigned long len); + +void to_std_logic_vector(unsigned long val, unsigned char *p, + unsigned long len);