S2.13.1 Take the Trap

HardImplement60 minIcarus Verilog · serverReview the topic

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

SignalWidthRole
trial10input of each check
Full specification
The five CSRs
Any other address reads 0, and a write to it changes nothing.
Address, in hexNameBits that can be writtenEvery other bitAfter reset
300mstatusBit 3 (MIE) and bit 7 (MPIE)Bits 12 and 11 read 11. The rest read 00000_1800
305mtvecBits 31 to 2 (the base) and bit 0 (the mode)Bit 1 reads 00
341mepcBits 31 to 2Bits 1 and 0 read 00
342mcauseAll 320
343mtvalAll 320
What happens at a clock edge
A value that goes into mepc loses its two low bits, whoever writes it.
Input at 1What changes
trapmepc takes trap_pc, mcause takes trap_cause, mtval takes trap_value. MPIE takes MIE, and MIE becomes 0.
mretMIE takes MPIE, and MPIE becomes 1.
csr_weThe CSR at csr_addr takes csr_wdata, in the bits that can be written.
next_pc
next_pc has no clock. It follows the inputs and the CSRs as they are now, before the edge.
Casenext_pc
mret is 1mepc
Otherwise, bit 0 of mtvec is 1 and bit 31 of trap_cause is 1The base of mtvec, plus 4 times the low 30 bits of trap_cause
OtherwiseThe 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

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

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
Expected 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.

Run samples executes all 3 public checks. Check all tests runs 16 checks on the server.

Icarus Verilog · server