@@ -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
152149main__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
336335illegal_instruction:
337336 set mcause 2
337+ set mtval 0 # TODO: return faulting instruction bits?
338338 # continue into trap
339339
340340trap:
@@ -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
463465load_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
545547load_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
11981203atomic_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
15971604EBREAK:
15981605 set mcause 3 # breakpoint
1606+ # mtval is set to pc by main
15991607 jump trap always
16001608
16011609MRET:
@@ -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-
16391633CSRRWI:
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
16451643CSRRSI:
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
16511653CSRRCI:
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
16561663read_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