Skip to content

Commit 6418433

Browse files
committed
[rtl] Fix incrementing minstret after setting it
According to the Specification Section 6.1 CSR Instructions: Some CSRs, such as the instructions-retired counter, instret, may be modified as side effects of instruction execution. In these cases, if a CSR access instruction reads a CSR, it reads the value prior to the execution of the instruction. If a CSR access instruction writes such a CSR, the explicit write is done instead of the update from the side effect. In particular, a value written to instret by one instruction will be the value read by the following instruction.
1 parent b7d62a2 commit 6418433

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

rtl/ibex_id_stage.sv

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,9 +1076,15 @@ module ibex_id_stage #(
10761076
end
10771077

10781078
// Signal which instructions to count as retired in minstret, all traps along with ebrk and
1079-
// ecall instructions are not counted.
1079+
// ecall instructions are not counted. Writes to minstret/minstreth themselves are also excluded
1080+
// to avoid incrementing right after a write.
1081+
logic minstret_write;
1082+
assign minstret_write = csr_access_o &
1083+
(csr_op_o inside {CSR_OP_WRITE, CSR_OP_SET, CSR_OP_CLEAR}) &
1084+
(csr_addr_o inside {CSR_MINSTRET, CSR_MINSTRETH});
1085+
10801086
assign instr_perf_count_id_o = ~ebrk_insn & ~ecall_insn_dec & ~illegal_insn_dec &
1081-
~illegal_csr_insn_i & ~instr_fetch_err_i;
1087+
~illegal_csr_insn_i & ~instr_fetch_err_i & ~minstret_write;
10821088

10831089
// An instruction is ready to move to the writeback stage (or retire if there is no writeback
10841090
// stage)

0 commit comments

Comments
 (0)