A loop with a branch inside runs on the five-stage core. Count the branches that are taken, and work out what they cost.
Hardware context: The lab pipeline with branches decided in EX: a taken branch redirects the PC and puts bubbles into IF/ID and ID/EX; the flush can be switched off. Counting branch penalties in a pipeline trace and reviewing the flush logic of a core
The first program below starts at address 0 and runs on the five-stage core of the lesson. That core decides a branch in EX and fetches on at the PC plus 4 until then. The first instruction is in IF in cycle 1. No instruction of the program 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, 4 |
| 04 | addi x6, x0, 0 |
| 08 | andi x7, x5, 1 |
| 0c | bne x7, x0, 0x14 |
| 10 | addi x6, x6, 10 |
| 14 | addi x5, x5, -1 |
| 18 | bne x5, x0, 0x08 |
| 1c | jal x0, 0x24 |
| 20 | addi x6, x6, 100 |
| 24 | ebreak |
The loop runs four times, with x5 at 4, 3, 2 and 1. The instruction at 08 puts 1 into x7 when x5 is odd.
Enter every value in decimal. An instruction that is executed in every round counts once for each round. A jal counts as an instruction, and it always goes to its target.
| Address, in hex | Instruction |
|---|---|
| 00 | addi x5, x0, 3 |
| 04 | addi x6, x0, 0 |
| 08 | add x6, x6, x5 |
| 0c | addi x5, x5, -1 |
| 10 | bne x5, x0, 0x08 |
| 14 | addi x7, x6, 1 |
| 18 | ebreak |
| Entry | Your decision |
|---|---|
| INNER_BRANCH_TAKEN | How many times is the branch at address 0c taken? |
| LOOP_BRANCH_TAKEN | How many times is the branch at address 18 taken? |
| REDIRECTS | How many times in all does the PC take a target? |
| EXECUTED | How many instructions are executed, not counting the ebreak? |
| LOST_CYCLES | How many cycles are lost to redirects? |
| CYCLES | In which cycle is the last executed instruction, the jal, in WB? |
| FIRST_REDIRECT | In which cycle does the first redirect happen? |
| CYCLES_OF_AN_EVEN_ROUND | How many cycles does a round with x5 even take, when its loop branch is taken? |
| CYCLES_OF_AN_ODD_ROUND | How many cycles does a round with x5 odd take, when its loop branch is taken? |
| X6_AT_STOP | What does x6 hold when the program stops? |
| FLUSHED_INSTRUCTIONS | How many fetched instructions are thrown away in the whole run? |
| LAST_WB_FOR_K | The second program runs its loop K times. Give the cycle in which its last instruction, at address 14, is in WB, as an expression in K. |
Examples
Example 1
INNER BRANCH TAKEN- Input
How many times is the branch at address 0c taken?
- Output
matches = 1
It is taken when x5 is odd: in the rounds with x5 at 3 and at 1.
Example 2
LOOP BRANCH TAKEN- Input
How many times is the branch at address 18 taken?
- Output
matches = 1
It goes back after the first three rounds. After the fourth, x5 is 0.
Example 3
REDIRECTS- Input
How many times in all does the PC take a target?
- Output
matches = 1
Two and three for the branches, and one for the jal at 1c.
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 3 through 12, inclusive.
Hints
Hint 1
Write down x5 and x7 for each round. They decide both branches.
Hint 2
Only a branch that is taken costs cycles. A jal is always taken.
Hint 3
The cycle count is the number of executed instructions, plus 4, plus 2 for each redirect.
What the grader checks: Each check compares the full eight-bit answer. Parameter trials change one condition and keep the other decisions fixed.
12 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 2 problems on this path