S2.11.1 Two-Bit Counter Table

MediumImplement40 minIcarus Verilog · serverReview the topic

Build the table that a fetch stage would ask: sixteen 2-bit counters, looked up by the address of the instruction and updated when a branch is decided.

Hardware context: Test code that replays the branches which the real core ap_rv32 executed for three programs, and asks four predictors before each one: not taken, backward taken, a 1-bit entry and a 2-bit counter for each branch. Estimating the accuracy of a direction predictor from a branch trace before any hardware is built

The lab of the lesson kept one entry for each branch, as bench code. Hardware has a fixed table. Each address uses one of its entries, chosen by a few bits of the address.

Write that table as a module. The fetch stage gives an address and gets a guess in the same cycle. When a branch is decided, the pipeline reports its address and what it did.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification
The table
PartRule
EntriesSixteen counters of 2 bits each.
Which entry an address usesThe number in bits 5 to 2 of the address. Bits 1 and 0 are always 0 for an instruction.
After a resetEvery counter is at 1.
The guesspredict_taken is 1 when the counter of pc_f is 2 or 3.
The updateAt a clock edge with update at 1, the counter of update_pc counts up when update_taken is 1, and down when it is 0.
The endsA counter at 3 stays at 3 when it should count up. A counter at 0 stays at 0 when it should count down.

How to read a result: a public case shows the guess and the low eight bits of the address, in the last cycle of a short run. A private case runs many cycles and checks the guess in every one.

Examples

Example 1

One Taken Branch
Input
the branch at address 10 is taken once after a reset; then the lookup of address 10
Output
predict_taken = 1, pc_f = 0x10

After the reset the counter is 1. One taken branch makes it 2, and from 2 on the guess is taken.

Example 2

A Counter That Stops at 3
Input
taken three times, then not taken once; then the lookup
Output
predict_taken = 1, pc_f = 0x10

The counter stops at 3. One branch that is not taken brings it to 2. The guess is still taken.

Example 3

Two Addresses, One Entry
Input
address 10 is trained to taken; then the lookup of address 50
Output
predict_taken = 1, pc_f = 0x50

Bits 5 to 2 of 10 and of 50 are the same, so the two addresses use the same counter. The guess for 50 is taken.

Constraints and Contract

  • The reset is asynchronous and active low.
  • predict_taken has no clock: it follows pc_f and the stored counters. The bench reads it 1 ns after it has set the inputs.
  • A lookup in the same cycle as an update gets the count from before the clock edge, also for the same entry.
  • While update is 0, update_pc and update_taken may hold anything.

Hints

Hint 1

The starter already has the table, the reset and the guess. What is missing is the effect of a decided branch.

Hint 2

Work out which entry the decided branch uses. It need not be the entry of the address in IF.

Hint 3

Write the two ends as their own cases: what happens at 3 on a taken branch, and at 0 on one that is not taken?

What the grader checks: One branch trained up and down through all four counts. Long runs to both ends, and the step back from each end. A branch that alternates. All sixteen entries, each trained on its own. Addresses that share an entry, 40 apart in hex and far apart. Addresses that differ only in bits 1 and 0. A lookup in the cycle of an update, for the same entry and for another. Inputs that change while update is 0. Two mixed runs. The guess is checked in every cycle, 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

One Taken Branch

Input
the branch at address 10 is taken once after a reset; then the lookup of address 10
Expected output
predict_taken = 1, pc_f = 0x10

After the reset the counter is 1. One taken branch makes it 2, and from 2 on the guess is taken.

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

Icarus Verilog · server