From 1bbbd552047cc75f5582113bc649190934b1b066 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Fri, 21 Jan 2022 13:25:28 +0000 Subject: [PATCH] add save/restore and memdump function to microwatt-verilator, for save/restore of simulation state --- Makefile | 9 ++++++-- verilator/microwatt-verilator.cpp | 34 ++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 9fc51ca..387ce00 100644 --- 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 \ diff --git a/verilator/microwatt-verilator.cpp b/verilator/microwatt-verilator.cpp index ed6661a..dea7c07 100644 --- a/verilator/microwatt-verilator.cpp +++ b/verilator/microwatt-verilator.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #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; } -- 2.30.2