Consolidate VHPI code
authorAnton Blanchard <anton@linux.ibm.com>
Wed, 22 Jan 2020 03:50:45 +0000 (14:50 +1100)
committerAnton Blanchard <anton@ozlabs.org>
Wed, 22 Jan 2020 03:50:45 +0000 (14:50 +1100)
We had many copies of the VHPI marshalling/unmarshalling code.

Signed-off-by: Anton Blanchard <anton@linux.ibm.com>
Makefile
sim_bram_helpers_c.c
sim_console_c.c
sim_jtag_socket_c.c
sim_vhpi_c.c [new file with mode: 0644]
sim_vhpi_c.h [new file with mode: 0644]

index 2780db061cf1d18be4ab1672ea657edada40f697..b55b78655690794d6d5a39f23a8480834ea75058 100644 (file)
--- 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)))
 
index c5678e72d0e511cb6f6d1c99c936e26f46202d5e..74dca3fbc73bfba848fb494cb36ec662dcdbb3cd 100644 (file)
@@ -9,91 +9,12 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#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 {
index 20c1f11ee00b431f425992f26d9cca9a2abc3c19..b3920c1e276219ec078431c4f90aecff4a1e1fe6 100644 (file)
@@ -6,59 +6,11 @@
 #include <termios.h>
 #include <unistd.h>
 #include <poll.h>
+#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)
index 651094f786e2298974bb1fad96fd71dd2006376b..b27f53bd087cdf48fd8048a3ecba43a6d4e4557c 100644 (file)
@@ -9,60 +9,12 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#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 (file)
index 0000000..c909960
--- /dev/null
@@ -0,0 +1,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++;
+       }
+}
diff --git a/sim_vhpi_c.h b/sim_vhpi_c.h
new file mode 100644 (file)
index 0000000..eeedd9b
--- /dev/null
@@ -0,0 +1,11 @@
+#include <stdint.h>
+
+#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);