Skip to content

Commit f4b49bb

Browse files
committed
Implement mtval, fix mstatus not being written
1 parent 3ebf3b6 commit f4b49bb

File tree

6 files changed

+65
-32
lines changed

6 files changed

+65
-32
lines changed

riscof/mlogv32/mlogv32_isa.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@ hart0:
88
hw_data_misaligned_support: false
99
pmp_granularity: 0
1010
supported_xlen: [32]
11-
mtval_update: 0
11+
# enabled: address misaligned, access fault, breakpoint
12+
# disabled: illegal instruction
13+
mtval_update: 0b11111011
1214
misa:
1315
# X bit is disabled because riscv-config doesn't seem to like it
14-
# 0b01000000000100000001000100000001
15-
reset-val: 0x40101101
16+
reset-val: 0b01000000000100000001000100000001
1617
rv32:
1718
accessible: true
1819
extensions:
1920
implemented: true
2021
type:
2122
warl:
2223
legal:
23-
- extensions[25:0] in [0x0101101]
24+
- extensions[25:0] in [0b00000100000001000100000001]
2425
wr_illegal:
2526
- unchanged
2627
cycle:

riscof/mlogv32/riscof_mlogv32.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def initialise(self, suite: str, workdir: str, env: str):
8181
)
8282

8383
self.objdump_cmd = (
84-
"riscv{xlen}-unknown-elf-objdump --disassemble {elf} > {dump}"
84+
"riscv{xlen}-unknown-elf-objdump --disassemble-all {elf} > {dump}"
8585
)
8686

8787
# add more utility snippets here
File renamed without changes.

src/config/riscv-arch-test.mlog

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# per-world config options
2+
3+
# loaded at reset only
4+
set MEMORY_X_OFFSET -11 # x offset from this proc to bottom left memory proc
5+
set MEMORY_Y_OFFSET -26 # y offset from this proc to bottom left memory proc
6+
set MEMORY_WIDTH 32 # physical width of the memory procs
7+
8+
set ROM_SIZE 0x480000 # ROM size in bytes (rx)
9+
set RAM_SIZE 0x480000 # RAM size in bytes (rwx)
10+
set ICACHE_SIZE 0xc0000 # icache size in variables, or bytes of memory it can represent; 4x less dense than ROM/RAM
11+
12+
# reloaded after unpausing
13+
set TARGET_IPT 500000
14+
set BREAKPOINT_ADDRESS null
15+
16+
# computed values
17+
op add MEMORY_X @thisx MEMORY_X_OFFSET
18+
op add MEMORY_Y @thisy MEMORY_Y_OFFSET
19+
20+
stop

src/debugger.mlog.jinja

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ loop:
160160
read mscratch CSRS "{{ 'mscratch'|csr }}"
161161
read mepc CSRS "{{ 'mepc'|csr }}"
162162
read mcause CSRS "{{ 'mcause'|csr }}"
163+
read mtval CSRS "{{ 'mtval'|csr }}"
163164

164165
read ipt CPU "IPT"
165166
read state CPU "STATE"
@@ -332,6 +333,11 @@ loop__no_mark_icache:
332333
op add ret @counter 1
333334
jump format_bin always
334335

336+
print "mscratch = {0}\n"
337+
set n mscratch
338+
op add ret @counter 1
339+
jump format_hex always
340+
335341
print "mtvec = {0}\n"
336342
set n mtvec
337343
op add ret @counter 1
@@ -347,8 +353,8 @@ loop__no_mark_icache:
347353
op add ret @counter 1
348354
jump format_hex always
349355

350-
print "mscratch = {0}\n"
351-
set n mscratch
356+
print "mtval = {0}\n"
357+
set n mtval
352358
op add ret @counter 1
353359
jump format_hex always
354360

@@ -378,7 +384,7 @@ loop__no_mark_icache:
378384
print "instret = {0}\n\n"
379385
format instret
380386

381-
#{{'\n'}} {{ print(0, 33) }}
387+
#{{'\n'}} {{ print(0, 34) }}
382388

383389
# column 2
384390

@@ -492,7 +498,7 @@ done_privilege_mode:
492498

493499
#{{'\n'}} {{ print(36, 17) }}
494500

495-
wait 0.00001
501+
wait 0.02
496502
jump loop always
497503

498504
format_hex:

src/main.mlog.jinja

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ reset:
4040
read kbconv_rptr {{KBCONV_DATA}} 63
4141

4242
# read initial CSR values
43-
read mtime {{CSRS}} "{{ 'time'|csr }}"
44-
read mtimeh {{CSRS}} "{{ 'timeh'|csr }}"
45-
read mcycle {{CSRS}} "{{ 'mcycle'|csr }}"
46-
read mcycleh {{CSRS}} "{{ 'mcycleh'|csr }}"
4743
read minstret {{CSRS}} "{{ 'minstret'|csr }}"
4844
read minstreth {{CSRS}} "{{ 'minstreth'|csr }}"
4945

@@ -147,10 +143,13 @@ main:
147143
read mie {{CSRS}} "{{ 'mie'|csr }}"
148144
op and mtie mie mtip
149145
set mcause 0x80000007 # machine timer interrupt
146+
set mtval 0
150147
jump trap notEqual mtie 0
151148

152149
main__skip_time_update:
153150

151+
set mtval pc
152+
154153
# get the current instruction cache processor and variable
155154
jump main__read_icache notEqual icache_var null
156155

@@ -335,6 +334,7 @@ default_mtvec_handler:
335334

336335
illegal_instruction:
337336
set mcause 2
337+
set mtval 0 # TODO: return faulting instruction bits?
338338
# continue into trap
339339

340340
trap:
@@ -347,11 +347,13 @@ trap:
347347
op and mstatus mstatus 0b11111111111111111110011101110111
348348
op or mstatus mstatus mpie
349349
op or mstatus mstatus mpp
350+
write mstatus {{CSRS}} "{{ 'mstatus'|csr }}"
350351

351352
# set mcause
352353
write mcause {{CSRS}} "{{ 'mcause'|csr }}"
353354

354-
# TODO: set mtval?
355+
# set mtval
356+
write mtval {{CSRS}} "{{ 'mtval'|csr }}"
355357

356358
# set mepc to pc
357359
write pc {{CSRS}} "{{ 'mepc'|csr }}"
@@ -459,7 +461,7 @@ lookup_variable:
459461

460462
# loads the word from memory that contains the specified address
461463
# mcause is required in order to raise the correct exception in case of access fault
462-
# address, mcause -> result
464+
# address, mcause, mtval -> result
463465
load_word:
464466
jump load_rom_word_unchecked lessThan address ROM_SIZE
465467

@@ -541,7 +543,7 @@ load_rom_word__loop:
541543
set _rom null # avoid issues with the vars menu and block data size
542544
set @counter ret
543545

544-
# address, mcause -> result
546+
# address, mcause, mtval -> result
545547
load_mmio_word:
546548
op sub _jump address {{MMIO_START}}
547549
op idiv _jump _jump 4
@@ -1002,6 +1004,7 @@ unconditional_jump:
10021004

10031005
op mod remainder target 4
10041006
set mcause 0 # instruction address misaligned
1007+
set mtval target
10051008
jump trap notEqual remainder 0
10061009

10071010
# write pc+4 to rd
@@ -1030,6 +1033,7 @@ load_value:
10301033
op idiv _bytes length 8
10311034
op mod _alignment address _bytes
10321035
set mcause 4 # load address misaligned
1036+
set mtval address
10331037
jump trap notEqual _alignment 0
10341038

10351039
# load the word containing the address
@@ -1095,6 +1099,7 @@ store_value:
10951099
op idiv _bytes length 8
10961100
op mod _alignment address _bytes
10971101
set mcause 6 # store/AMO address misaligned
1102+
set mtval address
10981103
jump trap notEqual _alignment 0
10991104

11001105
# create a bitmask with 0 in the section we want to replace and 1 everywhere else
@@ -1197,6 +1202,7 @@ LR.W:
11971202

11981203
atomic_validate_address:
11991204
set address rs1
1205+
set mtval address
12001206

12011207
op mod alignment address 4
12021208
jump trap notEqual alignment 0
@@ -1592,10 +1598,12 @@ ECALL:
15921598
# 0b1000 (8) = ecall from U-mode
15931599
# 0b1011 (11) = ecall from S-mode
15941600
op and mcause 0b1000 privilege_mode
1601+
set mtval 0
15951602
jump trap always
15961603

15971604
EBREAK:
15981605
set mcause 3 # breakpoint
1606+
# mtval is set to pc by main
15991607
jump trap always
16001608

16011609
MRET:
@@ -1611,6 +1619,7 @@ MRET:
16111619
op and mstatus mstatus 0b11111111111111111110011111110111
16121620
op or mstatus mstatus mie
16131621
op or mstatus mstatus 0b10000000 # MPIE
1622+
write mstatus {{CSRS}} "{{ 'mstatus'|csr }}"
16141623

16151624
# set pc to MEPC
16161625
read pc {{CSRS}} "{{ 'mepc'|csr }}"
@@ -1621,37 +1630,35 @@ MRET:
16211630

16221631
jump end_instruction always
16231632

1624-
CSRRW:
1625-
# CSR-type: arg1=rs1, arg2=csr, arg3=rd
1626-
set csr_op 0 # read/write
1627-
jump read_modify_write_csr always
1628-
1629-
CSRRS:
1630-
# CSR-type: arg1=rs1, arg2=csr, arg3=rd
1631-
set csr_op 2 # read and set bits
1632-
jump read_modify_write_csr always
1633-
1634-
CSRRC:
1635-
# CSR-type: arg1=rs1, arg2=csr, arg3=rd
1636-
set csr_op 4 # read and clear bits
1637-
jump read_modify_write_csr always
1638-
16391633
CSRRWI:
16401634
# CSRI-type: arg1=uimm, arg2=csr, arg3=rd
16411635
set rs1 arg1
1636+
# continue into CSRRW
1637+
1638+
CSRRW:
1639+
# CSR-type: arg1=rs1, arg2=csr, arg3=rd
16421640
set csr_op 0 # read/write
16431641
jump read_modify_write_csr always
16441642

16451643
CSRRSI:
16461644
# CSRI-type: arg1=uimm, arg2=csr, arg3=rd
16471645
set rs1 arg1
1646+
# continue into CSRRS
1647+
1648+
CSRRS:
1649+
# CSR-type: arg1=rs1, arg2=csr, arg3=rd
16481650
set csr_op 2 # read and set bits
16491651
jump read_modify_write_csr always
16501652

16511653
CSRRCI:
16521654
# CSRI-type: arg1=uimm, arg2=csr, arg3=rd
16531655
set rs1 arg1
1656+
# continue into CSRRC
1657+
1658+
CSRRC:
1659+
# CSR-type: arg1=rs1, arg2=csr, arg3=rd
16541660
set csr_op 4 # read and clear bits
1661+
# continue into read_modify_write_csr
16551662

16561663
read_modify_write_csr:
16571664
set csr arg2
@@ -1691,7 +1698,6 @@ read_modify_write_csr__always_write:
16911698
jump read_modify_write_csr__mie equal variable "{{ 'mie'|csr }}"
16921699
jump read_modify_write_csr__mepc equal variable "{{ 'mepc'|csr }}"
16931700
jump read_modify_write_csr__mtvec equal variable "{{ 'mtvec'|csr }}"
1694-
jump end_instruction_with_result equal variable "{{ 'mtval'|csr }}" # read-only zero
16951701
jump read_modify_write_csr__mcycle equal variable "{{ 'mcycle'|csr }}"
16961702
jump read_modify_write_csr__mcycleh equal variable "{{ 'mcycleh'|csr }}"
16971703
jump read_modify_write_csr__minstret equal variable "{{ 'minstret'|csr }}"

0 commit comments

Comments
 (0)