S2.29.1 Three Cycles for a Trap

MediumImplement22 minIcarus Verilog · serverReview the topic

A loop makes two ecalls on the five-stage core. Place the first trap in the pipeline, then count what traps, mrets and CSR instructions cost.

Hardware context: A small model of a RISC-V processor with traps, as test code, running ten instructions that trap; beside it the pipeline view of the real five-stage core ap_rv32 for a fault and for two faults next to each other. Writing a trap handler, planning exception tests, and reading a trap in a pipeline trace

The program below runs on the five-stage core of the lesson, from address 0. Its first instruction is in IF in cycle 1. The core takes a trap when the instruction is in MEM. A trap, an mret and every CSR instruction leave from MEM and lose 3 cycles each. A taken branch loses 2. No instruction is a load.

This worksheet uses named constants. The first three entries show how to record your decisions.

Signals in the Timeline

SignalWidthRole
question8input of each check
Full specification
The program
The loop at 0c to 14 runs twice. The handler, at 40 in hex, steps over the ecall. The program ends in the jump to itself at 18.
Address, in hexInstruction
00addi x5, x0, 64
04csrw mtvec, x5
08addi x9, x0, 2
0cecall
10addi x9, x9, -1
14bne x9, x0, 0xc
18jal x0, 0x18
40csrr x7, mepc
44addi x7, x7, 4
48csrw mepc, x7
4cmret

Enter every value in decimal. Count an instruction once for each time it is executed. A trap counts as one trace record, like an instruction.

EntryYour decision
FIRST_ECALL_IN_MEMIn which cycle is the first ecall in MEM?
FLUSHED_AT_A_TRAPHow many fetched instructions are thrown away at one trap?
HANDLER_IN_IFIn which cycle is the first instruction of the handler in IF for the first time?
TRAPSHow many traps are taken in the whole run?
CSR_INSTRUCTIONSHow many CSR instructions are executed?
EXITS_FROM_MEMHow many times does an instruction leave from MEM and lose 3 cycles?
TAKEN_BRANCHESHow many times is the branch at 14 taken?
TRACE_RECORDSHow many trace records does the run have, with the jump at 18 as the last?
CYCLES_LOST_IN_MEM_EXITSHow many cycles are lost to the exits from MEM?
LAST_WBIn which cycle is the jump at 18 in WB?
COST_OF_ONE_SERVED_ECALLHow many cycles does one ecall add to a run, with its handler?
SECOND_ECALL_IN_MEMIn which cycle is the second ecall in MEM?
LAST_WB_FOR_KThe instruction at 08 becomes addi x9, x0, K, so the loop makes K ecalls. Give the cycle in which the jump at 18 is in WB, as an expression in K.

Examples

Example 1

FIRST ECALL IN MEM
Input
In which cycle is the first ecall in MEM?
Output
matches = 1

It is the fourth instruction, so it would be in IF in cycle 4. The csrw before it left from MEM in cycle 5 and lost 3 cycles: IF in cycle 7, MEM in cycle 10.

Example 2

FLUSHED AT A TRAP
Input
How many fetched instructions are thrown away at one trap?
Output
matches = 1

Those in EX, ID and IF.

Example 3

HANDLER IN IF
Input
In which cycle is the first instruction of the handler in IF for the first time?
Output
matches = 1

The cycle after the trap.

Constraints and Contract

  • Keep the parameter and named constants. Use the numeric codes and units on this page.
  • For LAST_WB_FOR_K, enter a constant expression using K. Only this entry changes with the parameter. The supported integer range is 2 through 8, inclusive.

Hints

Hint 1

Mark every trap, every mret and every CSR instruction. Each one loses three fetches.

Hint 2

Count the instructions before it, and add what was lost before it.

Hint 3

Trace records plus 4, plus 2 for each taken branch, plus 3 for each exit from MEM.

What the grader checks: Each check compares the full eight-bit answer. Parameter trials change one condition and keep the other decisions fixed.

13 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 1 problem 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 ECALL IN MEM

Input
In which cycle is the first ecall in MEM?
Expected output
matches = 1

It is the fourth instruction, so it would be in IF in cycle 4. The csrw before it left from MEM in cycle 5 and lost 3 cycles: IF in cycle 7, MEM in cycle 10.

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

Icarus Verilog · server