S2.12.1 Build Gshare

HardImplement60 minIcarus Verilog · serverReview the topic

Build gshare as a block beside a pipeline: 64 counters, a history of six outcomes, a guess in IF and an outcome that comes back two cycles later.

Hardware context: Test code that replays the branches which the real core ap_rv32 executed, with a plain 2-bit table, gshare and a local-history predictor side by side. Choosing the kind and the length of branch history from recorded traces

The lab of the lesson replayed one branch at a time: guess, outcome, next branch. A pipeline does not wait like that. A branch is looked up in IF and decided in EX. In between, other branches are looked up.

So the history may have changed when an outcome arrives. The counter that must learn is the one the guess was read from. Your block gives out the number of that counter with each guess. The pipeline carries it along and hands it back with the outcome.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification
The predictor
PartRule
Counters64 counters of 2 bits. After a reset each is at 1. They count as in S2.11: up for taken, down for not taken, and they stay at 3 and at 0.
HistoryA register of 6 bits. After a reset it is 0. The newest outcome is in bit 0, with 1 for taken.
predict_slotBits 7 to 2 of pc_f, XOR the history. No clock.
predict_taken1 when the counter numbered predict_slot is 2 or 3. No clock.
At a clock edge with update at 1The counter numbered update_slot counts up or down by update_taken. The history shifts by one place, and update_taken enters at bit 0.

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

Examples

Example 1

After One Taken Branch
Input
the branch at address 30 is looked up once and is taken; then address 30 is looked up again, after the outcome has arrived
Output
predict_taken = 0, predict_slot = 13, pc_f = 0x30

The first lookup read counter 12: the address gives 12, and the history was empty. The outcome made counter 12 a 2 and put a 1 into the history. The new lookup reads counter 12 XOR 1, which is 13. That counter is still at 1: not taken.

Example 2

A Full History
Input
the branch at 30 is taken seven times, each time looked up after the outcome before it has arrived; then one more lookup of address 30
Output
predict_taken = 1, predict_slot = 51, pc_f = 0x30

After six taken branches the history is full: 111111, which is 63. From then on a lookup of this address reads counter 12 XOR 63, which is 51. The seventh taken branch was the first to train counter 51, from 1 to 2. So the guess is taken.

Example 3

Two Lookups Before an Outcome
Input
two lookups of address 30 in a row, before any outcome has arrived; this is the second
Output
predict_taken = 0, predict_slot = 12, pc_f = 0x30

No outcome has come back yet, so the history is still empty. Both lookups read counter 12, and both get the guess not taken.

Constraints and Contract

  • The reset is asynchronous and active low.
  • A lookup in the same cycle as an update uses the history and the counters from before the clock edge.
  • The bench plays the pipeline. It hands back, as update_slot, exactly the predict_slot that the branch was given. Mostly that is two cycles later. Some private cases use one cycle or four.
  • While update is 0, update_slot and update_taken may hold anything.
  • In a cycle with no new branch, pc_f still holds some address, and both outputs are still checked.

Hints

Hint 1

Start with the history register alone. When does it change, and which end does the new outcome enter?

Hint 2

Which address bits and history bits select a counter? Send that slot along with the guess.

Hint 3

Last, look at the update. Which input says where the guess of this branch was read from?

What the grader checks: A branch that alternates, and one with a period of three. Two branches in turn with opposite habits. A branch that repeats the outcome of the branch before it. Addresses that differ only above bit 7 or only in bits 1 and 0. Outcomes that come back after one, two and four cycles. Cycles with no new branch while outcomes still arrive. Long runs to both ends of a counter. Two mixed runs. The guess and the slot are 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

After One Taken Branch

Input
the branch at address 30 is looked up once and is taken; then address 30 is looked up again, after the outcome has arrived
Expected output
predict_taken = 0, predict_slot = 13, pc_f = 0x30

The first lookup read counter 12: the address gives 12, and the history was empty. The outcome made counter 12 a 2 and put a 1 into the history. The new lookup reads counter 12 XOR 1, which is 13. That counter is still at 1: not taken.

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

Icarus Verilog · server