From: Luke Kenneth Casson Leighton Date: Fri, 21 Jan 2022 13:38:50 +0000 (+0000) Subject: add snapshot-saving currently every 1000 cycles for test purposes X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=6f6e4c9d482a1281f93cd77327826908d5eea3b3;p=microwatt.git add snapshot-saving currently every 1000 cycles for test purposes --- diff --git a/verilator/microwatt-verilator.cpp b/verilator/microwatt-verilator.cpp index 0dbbf29..0b6f6b6 100644 --- a/verilator/microwatt-verilator.cpp +++ b/verilator/microwatt-verilator.cpp @@ -70,36 +70,46 @@ static void ascii_dump(unsigned char *data, int len, FILE *dump) } // save/restore of verilator model to/from file. -void save_model(const char* filenamep, Vmicrowatt* topp) +void save_model(vluint64_t time, Vmicrowatt* topp) { + char fname[128]; + sprintf(fname, "verilator.save.%ld", time); VerilatedSave os; - os.open(filenamep); + os.open(fname); os << main_time; // user code must save the timestamp, etc os << *topp; } -void restore_model(const char* filenamep, Vmicrowatt* topp) +void restore_model(vluint64_t time, Vmicrowatt* topp) { + char fname[128]; + sprintf(fname, "verilator.save.%ld", time); VerilatedRestore os; - os.open(filenamep); + os.open(fname); 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 memdump(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; + char fname[128]; + sprintf(fname, "bram.snapshot.%ld", main_time); + int fd = open(fname, 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; } +// save-trigger offsets +vluint64_t save_offset = 1000; +vluint64_t save_countdown = save_offset; + // 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, @@ -242,6 +252,17 @@ int main(int argc, char **argv) while(!Verilated::gotFinish()) { + // check if a snapshot of the model should be actioned, + // otherwise continue counting down + if (save_countdown == 0) { + fprintf(dump, "snapshot saving at %ld\n", main_time); + fflush(dump); + save_model(main_time, top); + save_countdown = save_offset; // loop for next snapshot + } else { + --save_countdown; + } + tick(top, traceme); // read/write the memory to/from the mmap'd file (if given)