S2.2.3 The Branch That Jumped Too Far

MediumDebug25 minIcarus Verilog · serverReview the topic

A block that works out the next PC was ported from another instruction set. Loops overshoot. Find what it assumes, and repair it.

Hardware context: Real RV32I instruction words from the assembler, cut at the six fixed field positions by test code; one 12-bit field widened by zero extension and by sign extension. Reading instruction words in a trace or a memory dump, and specifying an instruction decoder

A team moves a small core from another instruction set to RISC-V. The block that works out the next PC was copied as it was. The immediate generator in front of it is new, and correct.

With the copied block, the first loop of the boot code never comes back to its start. Repair the block. It has no clock.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification
What the next PC must be
The target is worked out as RISC-V defines it. The lesson gives the rule.
InstructionNext PC
jalthe target
a branch whose test holdsthe target
a branch whose test failsthe address of the following instruction
anything elsethe address of the following instruction

The input imm is the output of a correct immediate generator: the whole offset, 32 bits wide, as a number of bytes.

How to read a result: a public case shows next in hex. A private case tries many inputs of one kind, and all_match is 1 when every output was right.

Examples

Example 1

A Taken Branch
Input
bne at address 10 in hex, offset -8, taken
Output
next = 0x8

The loop of the lesson: the branch at 10 goes back to 8.

Example 2

The Same Branch, Not Taken
Input
the same bne, not taken
Output
next = 0x14

The PC goes on by 4, to 14.

Example 3

A Jump
Input
jal at address 4, offset 12
Output
next = 0x10

A jump always goes to its target: 4 plus 12 is 10 in hex. The input taken does not matter.

Constraints and Contract

  • The output depends only on the inputs of the same moment. The bench waits 1 ns after it sets them.
  • is_branch and is_jal are never both 1. When is_branch is 0, taken may hold any value.
  • The sum wraps round at 32 bits. A target that is not a multiple of 4 is passed on as it is: another block reports it.

Hints

Hint 1

Work out the first public case by hand, and compare it with what the block gives.

Hint 2

From which address does RISC-V count an offset? And in which unit is the immediate already?

Hint 3

Look at the public case where the test of the branch fails.

What the grader checks: Taken and not-taken branches with offsets from -4096 to 4094. A jal with offsets up to 1 MiB each way. Other instructions with taken at both values. Addresses at both ends of the 32-bit space, and 60 random cases.

9 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 3 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

A Taken Branch

Input
bne at address 10 in hex, offset -8, taken
Expected output
next = 0x8

The loop of the lesson: the branch at 10 goes back to 8.

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

Icarus Verilog · server