Five registers catch a trap and let the handler return. Build them: the entry, mret, the bits each CSR really has, and the address of the handler.
Hardware context: A small model of a RISC-V processor with traps, as test code: seven machine CSRs, the entry of a trap and mret; every program also runs on the real core ap_rv32. Writing start-up code and trap handlers, and reading a trap in a trace
The lab of the lesson was a model that printed what the CSRs do. In a processor they are flip-flops with rules. Each has bits that can be written and bits that cannot.
Write the block. The rest of the processor tells it when a trap is taken, when an mret is executed, and when a CSR instruction writes. Your block keeps the five CSRs and says where the PC goes.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
trial | 10 | input of each check |
Full specification
| Address, in hex | Name | Bits that can be written | Every other bit | After reset |
|---|---|---|---|---|
| 300 | mstatus | Bit 3 (MIE) and bit 7 (MPIE) | Bits 12 and 11 read 11. The rest read 0 | 0000_1800 |
| 305 | mtvec | Bits 31 to 2 (the base) and bit 0 (the mode) | Bit 1 reads 0 | 0 |
| 341 | mepc | Bits 31 to 2 | Bits 1 and 0 read 0 | 0 |
| 342 | mcause | All 32 | 0 | |
| 343 | mtval | All 32 | 0 |
| Input at 1 | What changes |
|---|---|
trap | mepc takes trap_pc, mcause takes trap_cause, mtval takes trap_value. MPIE takes MIE, and MIE becomes 0. |
mret | MIE takes MPIE, and MPIE becomes 1. |
csr_we | The CSR at csr_addr takes csr_wdata, in the bits that can be written. |
| Case | next_pc |
|---|---|
mret is 1 | mepc |
Otherwise, bit 0 of mtvec is 1 and bit 31 of trap_cause is 1 | The base of mtvec, plus 4 times the low 30 bits of trap_cause |
| Otherwise | The base of mtvec: its bits 31 to 2, with 00 below them |
How to read a result: a public case shows the last cycle of a short sequence. It gives csr_rdata and next_pc in hex. A private case runs a longer sequence and checks both outputs in every cycle.
Examples
Example 1
The Address of the Fault- Input
mtvec is written with 100 in hex; a trap at address 24 with cause 2; then mepc is read
- Output
csr_rdata = 0x24, next_pc = 0x100
mepc holds the address of the trap, 24. With no mret, next_pc is the handler address: the base in mtvec.
Example 2
The Enable Bit Inside the Handler- Input
MIE is set; a trap at address 24; then mstatus is read while mret is 1
- Output
csr_rdata = 0x1880, next_pc = 0x24
The entry moved MIE to MPIE and closed MIE: bit 7 is 1 and bit 3 is 0. Bits 12 and 11 always read 11. With mret at 1, next_pc is mepc.
Example 3
A Handler Table- Input
mtvec is written with 201 in hex; then the handler address for a timer interrupt, cause 8000_0007, is asked
- Output
csr_rdata = 0x201, next_pc = 0x21c
Bit 0 of mtvec is 1: a table. An interrupt goes to the base plus 4 times its number: 200 plus 1c in hex.
Constraints and Contract
- The reset is asynchronous and active low.
- csr_rdata and next_pc have no clock. The bench reads them 1 ns after it has set the inputs.
- At one clock edge the bench gives at most one of trap, mret and csr_we.
- The bench also shows a trap_cause, or an mret, with no clock edge after it. Then nothing may change, and next_pc must still answer.
- trap_pc and a value written to mepc may hold any low bits.
Hints
Hint 1
Give every CSR its own flip-flops, and only for the bits that can be written. mstatus needs two.
Hint 2
Write the read of each CSR from those flip-flops and from constants. Then the three kinds of clock edge, one at a time.
Hint 3
For next_pc, take the cases of the third table in order. Which bits of mtvec are an address, and which bit is not?
What the grader checks: An exception and its return. The enable bit through two traps and three mrets. All ones and all zeros written to every CSR. A handler table and a single handler address, for interrupts and exceptions. Addresses that are not a multiple of 4. Addresses that are no CSR of this block. A handler that changes mepc and mstatus. An mret with no trap before it. A cause with every bit set and a high handler address. Three mixed runs. Both outputs are checked in every cycle, and all five CSRs are read after every change.
16 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 3 problems on this path