diff --git a/Makefile b/Makefile index cab6c71e15f74823f85715b8738cc093976ede4c..b08e60ac176b5259140b81aac8a5318316a5f7a6 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,7 @@ $(SIM_TOP_V): $(SCALA_FILE) $(TEST_FILE) date -R mill XiangShan.test.runMain $(SIMTOP) -X verilog -td $(@D) --full-stacktrace --output-file $(@F) $(SIM_ARGS) sed -i '/module XSSimTop/,/endmodule/d' $(SIM_TOP_V) - sed -i -e 's/$$fatal/$$finish/g' $(SIM_TOP_V) + sed -i -e 's/$$fatal/xs_assert(`__LINE__)/g' $(SIM_TOP_V) date -R EMU_TOP = XSSimSoC @@ -80,7 +80,7 @@ endif # Verilator multi-thread support EMU_THREADS ?= 1 ifneq ($(EMU_THREADS),1) -VEXTRA_FLAGS += --threads $(EMU_THREADS) --threads-dpi none +VEXTRA_FLAGS += --threads $(EMU_THREADS) --threads-dpi all endif # Verilator savable diff --git a/src/test/csrc/assert.cpp b/src/test/csrc/assert.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa09f90dcef1535ddaa66dc4e650541de25036d5 --- /dev/null +++ b/src/test/csrc/assert.cpp @@ -0,0 +1,20 @@ +#include "common.h" +#include "emu.h" + +int assert_count = 0; +static pthread_mutex_t assert_mutex; + +void assert_init() { + pthread_mutex_init(&assert_mutex, 0); +} + +void assert_finish() { + pthread_mutex_destroy(&assert_mutex); +} + +extern "C" void xs_assert(long long line) { + pthread_mutex_lock(&assert_mutex); + printf("Assertion failed at line %lld\n.", line); + assert_count++; + pthread_mutex_unlock(&assert_mutex); +} diff --git a/src/test/csrc/common.h b/src/test/csrc/common.h index c7158c524ee9e185850bc3209dd456f3ec4f1007..2cf7fcfe304d280c5151cd6cf76f4f8146893b36 100644 --- a/src/test/csrc/common.h +++ b/src/test/csrc/common.h @@ -6,6 +6,7 @@ #include #include #include +#include #define ANSI_COLOR_RED "\x1b[31m" #define ANSI_COLOR_GREEN "\x1b[32m" @@ -21,4 +22,8 @@ #include "cosimulation.h" #endif +extern int assert_count; +void assert_init(); +void assert_finish(); + #endif // __COMMON_H diff --git a/src/test/csrc/difftest.h b/src/test/csrc/difftest.h index 3fecfcfa6b7c5d889499080e7144e85abb87e812..780f6359fea6db6c0c15c37a223d8358c3d93796 100644 --- a/src/test/csrc/difftest.h +++ b/src/test/csrc/difftest.h @@ -96,4 +96,7 @@ int difftest_step(DiffState *s); int difftest_store_step(uint64_t *saddr, uint64_t *sdata, uint8_t *smask); void difftest_display(uint8_t mode); +uint64_t get_nemu_this_pc(); +void set_nemu_this_pc(uint64_t pc); + #endif diff --git a/src/test/csrc/emu.cpp b/src/test/csrc/emu.cpp index 59bb8b64169cc0c0db7123ed26504bf6c41aad2f..82747ececa0dc0f7ed24cc16a45d54c5f61e2dff 100644 --- a/src/test/csrc/emu.cpp +++ b/src/test/csrc/emu.cpp @@ -8,12 +8,6 @@ #include "zlib.h" #include "compress.h" -void* get_ram_start(); -long get_ram_size(); -uint64_t get_nemu_this_pc(); -void set_nemu_this_pc(uint64_t pc); - - static inline void print_help(const char *file) { printf("Usage: %s [OPTION...]\n", file); printf("\n"); @@ -90,6 +84,7 @@ Emulator::Emulator(int argc, const char *argv[]): srand(args.seed); srand48(args.seed); Verilated::randReset(2); + assert_init(); // init core reset_ncycles(10); @@ -132,6 +127,7 @@ Emulator::Emulator(int argc, const char *argv[]): Emulator::~Emulator() { ram_finish(); + assert_finish(); #ifdef VM_SAVABLE if (args.enable_snapshot && trapCode != STATE_GOODTRAP && trapCode != STATE_LIMIT_EXCEEDED) { @@ -382,7 +378,7 @@ uint64_t Emulator::execute(uint64_t max_cycle, uint64_t max_instr) { #endif } - if (Verilated::gotFinish()) { + if (assert_count > 0) { difftest_display(dut_ptr->io_difftest_priviledgeMode); eprintf("The simulation stopped. There might be some assertion failed.\n"); trapCode = STATE_ABORT; diff --git a/src/test/csrc/ram.cpp b/src/test/csrc/ram.cpp index fcdddeca6c99e40e5a4428839b930ba3266fd977..b01394fa5e2e4d2688718bf3ea5049100babd5f8 100644 --- a/src/test/csrc/ram.cpp +++ b/src/test/csrc/ram.cpp @@ -12,6 +12,8 @@ CoDRAMsim3 *dram = NULL; static uint64_t *ram; static long img_size = 0; +static pthread_mutex_t ram_mutex; + void* get_img_start() { return &ram[0]; } long get_img_size() { return img_size; } void* get_ram_start() { return &ram[0]; } @@ -155,6 +157,8 @@ void init_ram(const char *img) { dram = new CoDRAMsim3(DRAMSIM3_CONFIG, DRAMSIM3_OUTDIR); #endif + pthread_mutex_init(&ram_mutex, 0); + } void ram_finish() { @@ -162,13 +166,18 @@ void ram_finish() { #ifdef WITH_DRAMSIM3 dramsim3_finish(); #endif + pthread_mutex_destroy(&ram_mutex); } + extern "C" uint64_t ram_read_helper(uint8_t en, uint64_t rIdx) { if (en && rIdx >= EMU_RAM_SIZE / sizeof(uint64_t)) { rIdx %= EMU_RAM_SIZE / sizeof(uint64_t); } - return (en) ? ram[rIdx] : 0; + pthread_mutex_lock(&ram_mutex); + uint64_t rdata = (en) ? ram[rIdx] : 0; + pthread_mutex_unlock(&ram_mutex); + return rdata; } extern "C" void ram_write_helper(uint64_t wIdx, uint64_t wdata, uint64_t wmask, uint8_t wen) { @@ -177,7 +186,9 @@ extern "C" void ram_write_helper(uint64_t wIdx, uint64_t wdata, uint64_t wmask, printf("ERROR: ram wIdx = 0x%lx out of bound!\n", wIdx); assert(wIdx < EMU_RAM_SIZE / sizeof(uint64_t)); } + pthread_mutex_lock(&ram_mutex); ram[wIdx] = (ram[wIdx] & ~wmask) | (wdata & wmask); + pthread_mutex_unlock(&ram_mutex); } } diff --git a/src/test/csrc/ram.h b/src/test/csrc/ram.h index 342036085c1b7dc361f3291b7783ffe4dd37b2ec..f6e224f220a2168b902721c701b60da4aeb623f7 100644 --- a/src/test/csrc/ram.h +++ b/src/test/csrc/ram.h @@ -8,6 +8,8 @@ void init_ram(const char *img); void ram_finish(); +void* get_ram_start(); +long get_ram_size(); #ifdef WITH_DRAMSIM3 // 4*64 bits diff --git a/src/test/vsrc/assert.v b/src/test/vsrc/assert.v new file mode 100644 index 0000000000000000000000000000000000000000..7d4f6f224fd53d3176c4620f165c85761bed577a --- /dev/null +++ b/src/test/vsrc/assert.v @@ -0,0 +1,4 @@ +import "DPI-C" function void xs_assert +( + input longint line +);