S6.4.3 The Handler That Never Returned

MediumDebug20 minIcarus Verilog · serverReview the topic

Find the first wrong architectural value in a nested timer handler and repair its five-instruction return sequence.

Hardware context: ap_soc machine trap entry and return. Machine-mode firmware and deadline analysis

A nested trap returned to an outer handler. Its inner return left mepc pointing inside the handler. A saved outer frame survives in SRAM. Run the failing trace and repair the assembly strings in slot order.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification

At sp+0 the frame holds an aligned outer resume PC. At sp+4 it holds the interrupted t0. The frame is eight bytes. Entry status was restored by the preceding code with interrupts masked.

lw reads a frame word into t0. csrw copies t0 into mepc. addi advances sp by its byte count.

Use these assembly spellings: addi sp,sp,4, addi sp,sp,8, csrw mepc,t0, lw t0,0(sp), lw t0,4(sp), mret, nop. The instruction model executes up to five slots and stops on mret.

Diagnose the Failing Run

The test below runs the buggy design. Read its log, then find where it first goes wrong in the waveform.

Log

Waveform

Click a signal at a cycle to select it. Or focus the waveform and use the arrow keys: Left and Right move one cycle, Up and Down one signal.

Step 1 · The first bad cycle and signal
Step 2 · The cause

Step 3 is optional: fix the code in the editor, then run the tests.

Examples

Example 1

First Observation
Input
frame=0x80000100, saved PC=0x84, saved t0=0x55
Output
resume_pc = 0x84, scratch_ok = 1, stack_ok = 1, returned = 1

The outer return resumes the interrupted instruction stream at 0x84.

Example 2

Second Observation
Input
frame=0x80000200, saved PC=0x1000, saved t0=0x0
Output
resume_pc = 0x1000, scratch_ok = 1, stack_ok = 1, returned = 1

Zero is a valid saved register value.

Example 3

Third Observation
Input
frame=0x80000300, saved PC=0x130, saved t0=0xcafe
Output
resume_pc = 0x130, scratch_ok = 1, stack_ok = 1, returned = 1

An unchanged mepc happens to give the right PC for this frame.

Constraints and Contract

  • Use the supplied instruction function. Restore t0, release the entire frame and execute mret with the saved outer address in mepc. Access only the two mapped frame words. The model omits unrelated CSR fields and instruction timing.

Hints

Hint 1

Find the register holding the outer return address after the nested return.

Hint 2

Follow each frame load into its destination.

Hint 3

Compare scratch, stack pointer and resume PC after mret.

What the grader checks: Executes the submitted epilogue against distinct frame contents, checking the resumed PC, preserved scratch, released stack, mapped loads and actual return.

35 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 4 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

First Observation

Input
frame=0x80000100, saved PC=0x84, saved t0=0x55
Expected output
resume_pc = 0x84, scratch_ok = 1, stack_ok = 1, returned = 1

The outer return resumes the interrupted instruction stream at 0x84.

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

Icarus Verilog · server