From 8a485de092c1ffc79105db34aca8875203921d63 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Fri, 17 Aug 2018 18:49:47 -0700 Subject: [PATCH] Don't increment instret immediately after it is written (#231) This brings Spike into compliance with this clause in the spec: https://github.com/riscv/riscv-isa-manual/blob/master/src/csr.tex#L96 --- riscv/processor.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/riscv/processor.cc b/riscv/processor.cc index 2a4a18c..ecbe3ef 100644 --- a/riscv/processor.cc +++ b/riscv/processor.cc @@ -403,10 +403,16 @@ void processor_t::set_csr(int which, reg_t val) state.minstret = (state.minstret >> 32 << 32) | (val & 0xffffffffU); else state.minstret = val; + // The ISA mandates that if an instruction writes instret, the write + // takes precedence over the increment to instret. However, Spike + // unconditionally increments instret after executing an instruction. + // Correct for this artifact by decrementing instret here. + state.minstret--; break; case CSR_MINSTRETH: case CSR_MCYCLEH: state.minstret = (val << 32) | (state.minstret << 32 >> 32); + state.minstret--; // See comment above. break; case CSR_SCOUNTEREN: state.scounteren = val; -- 2.30.2