Skip to content

Commit 386a483

Browse files
committed
Refactor CSR instructions, use rd instead of result, add next_pc
1 parent c59a1d1 commit 386a483

File tree

4 files changed

+452
-331
lines changed

4 files changed

+452
-331
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,36 @@ Returns 1 if the id was successfully looked up, or 0 if the lookup returned null
176176

177177
Only alignment `topLeft` is supported.
178178

179+
## CSRs
180+
181+
CSR values are stored either in a RAM processor (CSRS) or in a variable in the CPU, depending on the CSR. Following is a table of CSRs, their storage location, and any relevant notes (eg. supported fields).
182+
183+
| CSR | Location | Notes |
184+
| ----------------- | -------- | --------------------------------------------------------------------------------------- |
185+
| `cycle[h]` | CPU | Frequency: ticks \* ipt. Traps in U-mode. |
186+
| `time[h]` | CPU | Frequency: ms. Traps in U-mode. |
187+
| `instret[h]` | CPU | Traps in U-mode. |
188+
| `hpmcounter*[h]` | CSRS | Not implemented. |
189+
| `mvendorid` | CSRS | Read-only zero. |
190+
| `marchid` | CSRS | Read-only zero. |
191+
| `mimpid` | CSRS | Read-only zero. |
192+
| `mhartid` | CSRS | Read-only zero. |
193+
| `mstatus` | CPU | |
194+
| `mstatush` | CSRS | Read-only zero. |
195+
| `misa` | CSRS | Read-only. |
196+
| `mie` | CPU | |
197+
| `mtvec` | CSRS | Bits 1:0 read-only zero. |
198+
| `mcounteren` | CSRS | Read-only zero. |
199+
| `mscratch` | CSRS | |
200+
| `mepc` | CSRS | Bits 1:0 read-only zero. |
201+
| `mcause` | CSRS | Stored in CSRS because the CPU often speculatively sets the corresponding CPU variable. |
202+
| `mtval` | CSRS | Same reasoning as `mcause`. |
203+
| `mip` | CPU | |
204+
| `mcycle[h]` | CPU | See `cycle[h]`. |
205+
| `minstret[h]` | CPU | See `instret[h]`. |
206+
| `mhpmcounter*[h]` | CSRS | See `hpmcounter*[h]`. |
207+
| `mcountinhibit` | CSRS | Not implemented. |
208+
179209
## riscv-arch-test
180210

181211
mlogv32 currently passes all compliance tests for the `RV32IMAUZicsr_Zifencei` ISA.

src/debugger.mlog.jinja

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -155,30 +155,30 @@ loop:
155155
read x30_t5 REGISTERS 30
156156
read x31_t6 REGISTERS 31
157157

158-
read cycle CSRS "{{ 'cycle'|csr }}"
159-
read cycleh CSRS "{{ 'cycleh'|csr }}"
160-
op shl cycleh cycleh 32
161-
op add cycle cycle cycleh
158+
read mcycle CPU "csr_mcycle"
159+
read mcycleh CPU "csr_mcycleh"
160+
op shl mcycleh mcycleh 32
161+
op add mcycle mcycle mcycleh
162162

163-
read time CSRS "{{ 'time'|csr }}"
164-
read timeh CSRS "{{ 'timeh'|csr }}"
165-
op shl timeh timeh 32
166-
op add time time timeh
163+
read mtime CPU "csr_mtime"
164+
read mtimeh CPU "csr_mtimeh"
165+
op shl mtimeh mtimeh 32
166+
op add mtime mtime mtimeh
167167

168-
read mtimecmp CPU "mtimecmp"
169-
read mtimecmph CPU "mtimecmph"
168+
read mtimecmp CPU "csr_mtimecmp"
169+
read mtimecmph CPU "csr_mtimecmph"
170170
op shl mtimecmph mtimecmph 32
171171
op add mtimecmp mtimecmp mtimecmph
172172

173-
read instret CSRS "{{ 'instret'|csr }}"
174-
read instreth CSRS "{{ 'instreth'|csr }}"
175-
op shl instreth instreth 32
176-
op add instret instret instreth
173+
read minstret CPU "csr_minstret"
174+
read minstreth CPU "csr_minstreth"
175+
op shl minstreth minstreth 32
176+
op add minstret minstret minstreth
177177

178-
read mstatus CSRS "{{ 'mstatus'|csr }}"
178+
read mstatus CPU "csr_mstatus"
179179
read mtvec CSRS "{{ 'mtvec'|csr }}"
180-
read mip CSRS "{{ 'mip'|csr }}"
181-
read mie CSRS "{{ 'mie'|csr }}"
180+
read mip CPU "csr_mip"
181+
read mie CPU "csr_mie"
182182
read mscratch CSRS "{{ 'mscratch'|csr }}"
183183
read mepc CSRS "{{ 'mepc'|csr }}"
184184
read mcause CSRS "{{ 'mcause'|csr }}"
@@ -367,14 +367,14 @@ loop__no_mark_icache:
367367
print "mtimecmp = {0}\n"
368368
format mtimecmp
369369

370-
print "time = {0}\n"
371-
format time
370+
print "mtime = {0}\n"
371+
format mtime
372372

373-
print "cycle = {0}\n"
374-
format cycle
373+
print "mcycle = {0}\n"
374+
format mcycle
375375

376-
print "instret = {0}\n\n"
377-
format instret
376+
print "minstret = {0}\n\n"
377+
format minstret
378378

379379
#{{'\n'}} {{ print(0, 17) }}
380380

src/init.mlog.jinja

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,42 +89,37 @@ init_incr:
8989
# ISA ZYXWVUTSRQPONMLKJIHGFEDCBA
9090
write 0b01000000100100000001000100000001 CSRS "{{ 'misa'|csr }}"
9191

92-
# mstatush
93-
# WPRI -------------------------- ----
94-
# MBE -
95-
# SBE -
96-
write 0b00000000000000000000000000000000 CSRS "{{ 'mstatush'|csr }}"
97-
9892
# mlogv32 only has one hart, so mhartid is required to be zero
9993
write 0 CSRS "{{ 'mhartid'|csr }}"
10094

101-
# unimplemented fields
95+
# unimplemented CSRs
10296
write 0 CSRS "{{ 'mvendorid'|csr }}"
10397
write 0 CSRS "{{ 'marchid'|csr }}"
10498
write 0 CSRS "{{ 'mimpid'|csr }}"
10599

106100
# default values for writable CSRs
107101
write 0 CSRS "{{ 'mepc'|csr }}"
108102
write 0 CSRS "{{ 'mscratch'|csr }}"
109-
write 0 CSRS "{{ 'mie'|csr }}"
103+
write 0 CSRS "{{ 'mcause'|csr }}"
104+
write 0 CSRS "{{ 'mtval'|csr }}"
110105

111106
# init UARTs
112-
write 0 UART0 {{UART_RX_RPTR}}
113-
write 0 UART0 {{UART_RX_WPTR}}
114-
write 0 UART0 {{UART_TX_RPTR}}
115-
write 0 UART0 {{UART_TX_WPTR}}
116-
write 0 UART1 {{UART_RX_RPTR}}
117-
write 0 UART1 {{UART_RX_WPTR}}
118-
write 0 UART1 {{UART_TX_RPTR}}
119-
write 0 UART1 {{UART_TX_WPTR}}
120-
write 0 UART2 {{UART_RX_RPTR}}
121-
write 0 UART2 {{UART_RX_WPTR}}
122-
write 0 UART2 {{UART_TX_RPTR}}
123-
write 0 UART2 {{UART_TX_WPTR}}
124-
write 0 UART3 {{UART_RX_RPTR}}
125-
write 0 UART3 {{UART_RX_WPTR}}
126-
write 0 UART3 {{UART_TX_RPTR}}
127-
write 0 UART3 {{UART_TX_WPTR}}
107+
write 0 UART0 {{UART_RX_READ}}
108+
write 0 UART0 {{UART_RX_WRITE}}
109+
write 0 UART0 {{UART_TX_READ}}
110+
write 0 UART0 {{UART_TX_WRITE}}
111+
write 0 UART1 {{UART_RX_READ}}
112+
write 0 UART1 {{UART_RX_WRITE}}
113+
write 0 UART1 {{UART_TX_READ}}
114+
write 0 UART1 {{UART_TX_WRITE}}
115+
write 0 UART2 {{UART_RX_READ}}
116+
write 0 UART2 {{UART_RX_WRITE}}
117+
write 0 UART2 {{UART_TX_READ}}
118+
write 0 UART2 {{UART_TX_WRITE}}
119+
write 0 UART3 {{UART_RX_READ}}
120+
write 0 UART3 {{UART_RX_WRITE}}
121+
write 0 UART3 {{UART_TX_READ}}
122+
write 0 UART3 {{UART_TX_WRITE}}
128123

129124
setrate 1
130125
stop
@@ -150,8 +145,8 @@ set {{LOOKUP_PROC_SIZE}} null
150145
set {{ROM_PROC_BYTES}} null
151146
set {{RAM_PROC_BYTES}} null
152147
set {{RAM_PROC_VARS}} null
153-
set {{UART_RX_RPTR}} null
154-
set {{UART_RX_WPTR}} null
155-
set {{UART_TX_RPTR}} null
156-
set {{UART_TX_WPTR}} null
148+
set {{UART_RX_READ}} null
149+
set {{UART_RX_WRITE}} null
150+
set {{UART_TX_READ}} null
151+
set {{UART_TX_WRITE}} null
157152
# {% endraw %}

0 commit comments

Comments
 (0)