From c14960ee1b88c3a89aaf1c560c18abd6c7a5d47d Mon Sep 17 00:00:00 2001 From: Luke Wren Date: Thu, 22 Jul 2021 17:31:26 +0100 Subject: [PATCH] Add mtime/mtimecmp to openocd testbench --- test/sim/openocd/tb.cpp | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/test/sim/openocd/tb.cpp b/test/sim/openocd/tb.cpp index 5bf9a14..26a0c8e 100644 --- a/test/sim/openocd/tb.cpp +++ b/test/sim/openocd/tb.cpp @@ -19,9 +19,13 @@ uint8_t mem[MEM_SIZE]; static const unsigned int IO_BASE = 0x80000000; static const unsigned int IO_MASK = 0xffffff00; enum { - IO_PRINT_CHAR = 0, - IO_PRINT_U32 = 4, - IO_EXIT = 8 + IO_PRINT_CHAR = 0x000, + IO_PRINT_U32 = 0x004, + IO_EXIT = 0x008, + IO_MTIME = 0x100, + IO_MTIMEH = 0x104, + IO_MTIMECMP = 0x108, + IO_MTIMECMPH = 0x10c }; static const int TCP_BUF_SIZE = 256; @@ -151,6 +155,9 @@ int main(int argc, char **argv) { top.p_i__hready.set(true); top.p_d__hready.set(true); + uint64_t mtime = 0; + uint64_t mtimecmp = 0; + // Reset + initial clock pulse top.step(); top.p_clk.set(true); @@ -222,6 +229,10 @@ int main(int argc, char **argv) { } } + // Default update logic for mtime, mtimecmp + ++mtime; + top.p_timer__irq.set(mtime >= mtimecmp); + if (top.p_d__hready.get()) { // Clear bus error by default top.p_d__hresp.set(false); @@ -248,6 +259,18 @@ int main(int argc, char **argv) { printf("Ran for %ld cycles\n", cycle + 1); break; } + else if (bus_addr == IO_BASE + IO_MTIME) { + mtime = (mtime & 0xffffffff00000000u) | wdata; + } + else if (bus_addr == IO_BASE + IO_MTIMEH) { + mtime = (mtime & 0x00000000ffffffffu) | ((uint64_t)wdata << 32); + } + else if (bus_addr == IO_BASE + IO_MTIMECMP) { + mtimecmp = (mtimecmp & 0xffffffff00000000u) | wdata; + } + else if (bus_addr == IO_BASE + IO_MTIMECMPH) { + mtimecmp = (mtimecmp & 0x00000000ffffffffu) | ((uint64_t)wdata << 32); + } else { bus_err = true; } @@ -261,6 +284,18 @@ int main(int argc, char **argv) { mem[bus_addr + 2] << 16 | mem[bus_addr + 3] << 24; } + else if (bus_addr == IO_BASE + IO_MTIME) { + rdata = mtime; + } + else if (bus_addr == IO_BASE + IO_MTIMEH) { + rdata = mtime >> 32; + } + else if (bus_addr == IO_BASE + IO_MTIMECMP) { + rdata = mtimecmp; + } + else if (bus_addr == IO_BASE + IO_MTIMECMPH) { + rdata = mtimecmp >> 32; + } else { bus_err = true; }