S2.10.2 Price the Misprediction

MediumImplement22 minIcarus Verilog · serverReview the topic

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

SignalWidthRole
question8input of each check
Full specification
The program
Nine instructions and an ebreak. A target is written as an address in hex. The instruction at 20 is never executed.
Address, in hexInstruction
00addi x5, x0, 4
04addi x6, x0, 0
08andi x7, x5, 1
0cbne x7, x0, 0x14
10addi x6, x6, 10
14addi x5, x5, -1
18bne x5, x0, 0x08
1cjal x0, 0x24
20addi x6, x6, 100
24ebreak

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.

The program
A second program, for the last entry. It is shown for K = 3: the first instruction puts K into x5, and the loop runs K times.
Address, in hexInstruction
00addi x5, x0, 3
04addi x6, x0, 0
08add x6, x6, x5
0caddi x5, x5, -1
10bne x5, x0, 0x08
14addi x7, x6, 1
18ebreak
EntryYour decision
INNER_BRANCH_TAKENHow many times is the branch at address 0c taken?
LOOP_BRANCH_TAKENHow many times is the branch at address 18 taken?
REDIRECTSHow many times in all does the PC take a target?
EXECUTEDHow many instructions are executed, not counting the ebreak?
LOST_CYCLESHow many cycles are lost to redirects?
CYCLESIn which cycle is the last executed instruction, the jal, in WB?
FIRST_REDIRECTIn which cycle does the first redirect happen?
CYCLES_OF_AN_EVEN_ROUNDHow many cycles does a round with x5 even take, when its loop branch is taken?
CYCLES_OF_AN_ODD_ROUNDHow many cycles does a round with x5 odd take, when its loop branch is taken?
X6_AT_STOPWhat does x6 hold when the program stops?
FLUSHED_INSTRUCTIONSHow many fetched instructions are thrown away in the whole run?
LAST_WB_FOR_KThe 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

Report a problem with this page

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

INNER BRANCH TAKEN

Input
How many times is the branch at address 0c taken?
Expected output
matches = 1

It is taken when x5 is odd: in the rounds with x5 at 3 and at 1.

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

Icarus Verilog · server