Skip to content

Commit 206094b

Browse files
authored
Fix incorrect performance counter (#140)
The issue occurs when the CSR undergoes sign-extension. Specifically, the values of CSR_CYCLE and CSR_CYCLEH are assigned as 0xC00 and 0xC80 respectively. During the decoding immediate stage, these values are unintentionally sign-extended to 0xFFFFFC00 and 0xFFFFFC80. To mitigate this problem, a solution is only the last 12 bits of CSR are considered, disregarding the sign extension. Close #139
1 parent bc07e87 commit 206094b

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/emulate.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ static inline void update_time(riscv_t *rv)
123123
/* get a pointer to a CSR */
124124
static uint32_t *csr_get_ptr(riscv_t *rv, uint32_t csr)
125125
{
126-
switch (csr) {
126+
/* csr & 0xFFF prevent sign-extension in decode stage */
127+
switch (csr & 0xFFF) {
127128
case CSR_MSTATUS: /* Machine Status */
128129
return (uint32_t *) (&rv->csr_mstatus);
129130
case CSR_MTVEC: /* Machine Trap Handler */

0 commit comments

Comments
 (0)