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
| Signal | Width | Role |
|---|---|---|
question | 8 | input of each check |
Full specification
| Address, in hex | Instruction |
|---|---|
| 00 | addi x5, x0, 64 |
| 04 | csrw mtvec, x5 |
| 08 | addi x9, x0, 2 |
| 0c | ecall |
| 10 | addi x9, x9, -1 |
| 14 | bne x9, x0, 0xc |
| 18 | jal x0, 0x18 |
| 40 | csrr x7, mepc |
| 44 | addi x7, x7, 4 |
| 48 | csrw mepc, x7 |
| 4c | mret |
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.
| Entry | Your decision |
|---|---|
| FIRST_ECALL_IN_MEM | In which cycle is the first ecall in MEM? |
| FLUSHED_AT_A_TRAP | How many fetched instructions are thrown away at one trap? |
| HANDLER_IN_IF | In which cycle is the first instruction of the handler in IF for the first time? |
| TRAPS | How many traps are taken in the whole run? |
| CSR_INSTRUCTIONS | How many CSR instructions are executed? |
| EXITS_FROM_MEM | How many times does an instruction leave from MEM and lose 3 cycles? |
| TAKEN_BRANCHES | How many times is the branch at 14 taken? |
| TRACE_RECORDS | How many trace records does the run have, with the jump at 18 as the last? |
| CYCLES_LOST_IN_MEM_EXITS | How many cycles are lost to the exits from MEM? |
| LAST_WB | In which cycle is the jump at 18 in WB? |
| COST_OF_ONE_SERVED_ECALL | How many cycles does one ecall add to a run, with its handler? |
| SECOND_ECALL_IN_MEM | In which cycle is the second ecall in MEM? |
| LAST_WB_FOR_K | The 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