S2.9.1 Insert the Bubble

MediumImplement40 minIcarus Verilog · serverReview the topic

An instruction right behind a load needs a value that does not exist yet. Write the logic that sees this, holds the instruction in ID for one cycle, and sends a bubble to EX.

Hardware context: The lab pipeline with forwarding and a load-use stall: the PC and IF/ID hold for one cycle and ID/EX takes a bubble; the stall can be switched off. Counting stall cycles in a pipeline trace and reviewing the hazard unit of a core

The lab of the lesson let its bench decide when an instruction must wait. A real pipeline has logic for that, at the border between ID and EX. It is the heart of the hazard unit.

Write it. Your block sees the instruction in ID. It owns three fields of ID/EX: the valid bit, the load flag and the rd of the instruction in EX. From these it decides whether the instruction in ID may move on.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification
When the instruction in ID must wait
All of these holdWhy
ID holds an instruction, and EX holds an instructionAn empty ID has nothing to wait for. A bubble in EX loads nothing.
The instruction in EX is a loadOnly a load has its value one stage too late. Every other result can be forwarded.
Its rd is not x0A load into x0 is thrown away.
The instruction in ID reads that register, as rs1 or as rs2A field that is not read may hold any bits.
What a wait does, in that cycle
In every other cycle hold is 0. The three fields of ID/EX then take valid_d, mem_read_d and rd_d at the clock edge.
SignalValue
hold1. The PC and IF/ID keep their values, so the same instruction is in ID in the next cycle.
valid_e, at the clock edge0. EX gets a bubble in place of the instruction.

How to read a result: a public case shows hold and the three fields of ID/EX in one cycle. For a bubble, mem_read_e and rd_e are shown as 0. A private case runs many cycles and checks every one.

Examples

Example 1

A Load and Its Reader
Input
lw x6, 0(x10) and then add x7, x6, x5: the cycle in which the add is first in ID
Output
hold = 1, valid_e = 1, mem_read_e = 1, rd_e = 6

The load is in EX and writes x6. The add in ID reads x6. So hold is 1. EX holds the load: valid_e is 1, mem_read_e is 1 and rd_e is 6.

Example 2

One Cycle Later
Input
the same two instructions, one cycle later
Output
hold = 0, valid_e = 0, mem_read_e = 0, rd_e = 0

The add is still in ID. EX holds the bubble that the stall sent: valid_e is 0. Nothing in EX is a load now, so hold is 0 and the add moves on.

Example 3

A Load and Another Instruction
Input
lw x6, 0(x10) and then add x7, x5, x5: the cycle in which the add is in ID
Output
hold = 0, valid_e = 1, mem_read_e = 1, rd_e = 6

The add does not read x6. Nothing waits: hold is 0, and EX holds the load.

Constraints and Contract

  • The reset is asynchronous and active low. After it, EX holds a bubble.
  • hold has no clock: it follows the inputs and the three fields. The bench reads it, and the fields, 1 ns after it has set the inputs.
  • The bench plays the PC and IF/ID. It shows an instruction in ID for a second cycle exactly when that instruction must wait.
  • mem_read_e and rd_e are checked only while valid_e must be 1. In a bubble they may hold anything.

Hints

Hint 1

Write one signal that is 1 exactly when the instruction in ID must wait. Each row of the first table is one part of it.

Hint 2

That one signal does both jobs of the second table.

Hint 3

Then ask what EX holds in the cycle after a wait. Can your signal be 1 again for the same instruction?

What the grader checks: A reader of rs1, of rs2 and of both behind a load. Instructions that do not read the loaded register, and fields that equal rd without being read. Loads into x0. Writers that are not loads. Loads in a row, each using the one before, also on its own register. An empty ID before and after a load. The register numbers 1, 15, 16 and 31. Two mixed streams. Every cycle is checked, from the first after a reset.

14 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

A Load and Its Reader

Input
lw x6, 0(x10) and then add x7, x6, x5: the cycle in which the add is first in ID
Expected output
hold = 1, valid_e = 1, mem_read_e = 1, rd_e = 6

The load is in EX and writes x6. The add in ID reads x6. So hold is 1. EX holds the load: valid_e is 1, mem_read_e is 1 and rd_e is 6.

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

Icarus Verilog · server