Branch Flush Mechanism
When a branch goes the other way than predicted, the instructions fetched after it are wrong and must be cancelled before they change any state. This module models a simplified 3-stage instruction pipeline where invalidation happens via an asynchronous flush signal.
The pipeline consists of three stages. Each stage holds an 8-bit instruction payload and a 1-bit valid flag. During normal operation, instructions and their valid bits propagate from Stage 1 to Stage 3 on each clock cycle. When a branch mispredict is detected, the flush signal is asserted. This must immediately and asynchronously clear the valid bits of Stage 1 and Stage 2 to 0. However, Stage 3 represents an instruction older than the branch, so its valid bit must be allowed to complete normally and is unaffected by the flush. Data payloads continue to propagate through all stages on every clock cycle regardless of the flush signal or valid bits.
Timing and Reset Rules:
- Clock:
clkis positive-edge triggered. - Reset:
rst_nis an asynchronous, active-low reset. When asserted, all valid bits and data payloads across all three stages must be cleared to0. - Flush:
flushis an asynchronous, active-high signal. When asserted, it immediately forces the valid bits of Stage 1 and Stage 2 to0. It does not affect the data payloads, nor does it affect Stage 3. - Priority:
rst_nhas priority overflush.
Worked Trace:
Cycle 1: rst_n=0 → S1=(0,0), S2=(0,0), S3=(0,0), out_valid=0, out_data=0
Cycle 2: rst_n=1, in_valid=1, in_data=11 → S1=(1,11), S2=(0,0), S3=(0,0), out_valid=0, out_data=0
Cycle 3: in_valid=1, in_data=22 → S1=(1,22), S2=(1,11), S3=(0,0), out_valid=0, out_data=0
Cycle 4: in_valid=1, in_data=33 → S1=(1,33), S2=(1,22), S3=(1,11), out_valid=1, out_data=11
Mid-Cycle 4.5: flush=1 → S1=(0,33), S2=(0,22), S3=(1,11) (valid bits clear asynchronously)
Cycle 5: flush=0, in_valid=1, in_data=44 → S1=(1,44), S2=(0,33), S3=(0,22), out_valid=0, out_data=22
| Signal | Direction | Width | Description |
|---|---|---|---|
clk |
input | 1 | Positive-edge triggered clock |
rst_n |
input | 1 | Asynchronous active-low reset; clears all valid bits and data to 0 |
flush |
input | 1 | Asynchronous active-high flush; clears Stage 1 and Stage 2 valid bits to 0 immediately |
in_valid |
input | 1 | Input valid signal for Stage 1 |
in_data |
input | 8 | Input instruction payload for Stage 1 |
out_valid |
output | 1 | Output valid signal from Stage 3 |
out_data |
output | 8 | Output instruction payload from Stage 3 |
Constraints
- The
flushsignal must act asynchronously on the valid bits of Stage 1 and Stage 2. - The
flushsignal must NOT asynchronously clear the data payloads of any stage, nor the valid bit of Stage 3. - Data payloads must continue to propagate synchronously through the pipeline regardless of the valid bits or the
flushsignal. - All registers (both valid and data) must be cleared to
0whenrst_nis0.
Topics
Solve this problem
Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.
This problem is part of Codiode Pro. The statement above is free to read.
The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.
Part of Pipelining
Problem 7 of 20 in the Pipelining track.
Related problems
Browse all problems · Verilog practice problems · Learning tracks