add save/restore and memdump function to microwatt-verilator,
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 21 Jan 2022 13:25:28 +0000 (13:25 +0000)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 21 Jan 2022 13:25:28 +0000 (13:25 +0000)
for save/restore of simulation state

Makefile
verilator/microwatt-verilator.cpp

index 9fc51ca41f6ef5543dae0bbe444005020e2d3928..387ce0099f034cc91d36720444662b293da1d581 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -226,13 +226,18 @@ microwatt.v: $(synth_files) $(RAM_INIT_FILE)
 # Need to investigate why yosys is hitting verilator warnings, and eventually turn on -Wall
 # --top-module toplevel
 microwatt-verilator: microwatt.v verilator/microwatt-verilator.cpp verilator/uart-verilator.c
-       verilator -O3 -CFLAGS "-DCLK_FREQUENCY=$(CLK_FREQUENCY)" --assert --cc microwatt.v --exe verilator/microwatt-verilator.cpp verilator/uart-verilator.c -o $@ -Iuart16550 \
+       verilator -O3 -CFLAGS "-DCLK_FREQUENCY=$(CLK_FREQUENCY)" \
+    --assert \
+    --cc microwatt.v \
+    --exe verilator/microwatt-verilator.cpp verilator/uart-verilator.c \
+    -o $@ -Iuart16550 \
        -Wno-fatal -Wno-CASEOVERLAP -Wno-UNOPTFLAT \
            -Wno-BLKANDNBLK \
            -Wno-COMBDLY  \
            -Wno-CASEINCOMPLETE \
            -Wno-WIDTH \
-       # --trace \
+        --savable \
+        --trace \
        #    --unroll-count 256 \
        #    --output-split 5000 \
        #    --output-split-cfuncs 500 \
index ed6661ac3944e3f2ef1236c7691199fc6940106d..dea7c073bac8044304ea92bc1fad0851852ebf31 100644 (file)
@@ -4,6 +4,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <time.h>
 #include "Vmicrowatt.h"
 #include "verilated.h"
 #include "verilated_vcd_c.h"
@@ -63,6 +64,37 @@ static void ascii_dump(unsigned char *data, int len, FILE *dump)
     putc('\n', dump);
 }
 
+// save/restore of verilator model to/from file.
+void save_model(const char* filenamep, Vmicrowatt* topp)
+{
+    VerilatedSave os;
+    os.open(filenamep);
+    os << main_time;  // user code must save the timestamp, etc
+    os << *topp;
+}
+
+void restore_model(const char* filenamep, Vmicrowatt* topp)
+{
+    VerilatedRestore os;
+    os.open(filenamep);
+    os >> main_time;
+    os >> *topp;
+}
+
+//save bram memory out to a file. use for snapshots
+int memdump(time_t time, unsigned char *mem, size_t sz)
+{
+  int fd = open("bram.snapshot.%ld", time, O_RDWR | O_CREAT, (mode_t)0600);
+  lseek(fd, sz, SEEK_SET);
+  write(fd, "", 1);
+  void *map = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+  memcpy(map, mem, sz);
+  msync(map, sz, MS_SYNC);
+  munmap(map, sz);
+  close(fd);
+  return 0;
+}
+
 // write (masked by sel) to internal mem offset by bram_addr line
 static void mem_write(unsigned char *mem,
                       unsigned long bram_addr, unsigned long long bram_di,
@@ -228,7 +260,7 @@ int main(int argc, char **argv)
                 if (trigger_occurrences == 1) {
                     traceme = true;
                     fprintf(dump, "trace trigger enabled\n");
-                } 
+                }
                 if (trigger_occurrences != 0) {
                     --trigger_occurrences;
                 }