Codiode

Simultaneous Stall and Flush Priority

HardVerilog / SystemVerilogBuild1 solved

A stall and a flush can arrive in the same cycle. If the stall wins, a wrong-path instruction survives the flush. The priority between the two has to be stated explicitly.

The module tracks the validity of instructions across a 4-stage pipeline. Under normal operation, the input valid signal shifts into the first stage (bit 0), and existing valid bits propagate to subsequent stages (shifted left towards bit 3) on each clock cycle. When a stall is asserted, the pipeline freezes, holding its current valid state and ignoring the input. When a flush is asserted, all pipeline stages are immediately invalidated on the next clock edge. If both stall and flush are asserted on the same cycle, the flush must override the stall to ensure the pipeline is safely cleared.

  • Clock edge: posedge clk
  • Reset type: Asynchronous, active-low (rst_n)
  • Output reset value: valid_stages goes to 4'b0000
  • Priority: flush > stall > normal operation
  • All outputs are registered

Cycle 1: rst_n=0 → valid_stages=4'b0000
Cycle 2: rst_n=1, valid_in=1, stall=0, flush=0 → valid_stages=4'b0001
Cycle 3: valid_in=1, stall=0, flush=0 → valid_stages=4'b0011
Cycle 4: valid_in=0, stall=1, flush=0 → valid_stages=4'b0011 (hold due to stall)
Cycle 5: valid_in=1, stall=1, flush=1 → valid_stages=4'b0000 (flush overrides stall)
Cycle 6: valid_in=1, stall=0, flush=0 → valid_stages=4'b0001 (recovery)

Signal Direction Width Description
clk input 1 Positive-edge triggered clock
rst_n input 1 Asynchronous active-low reset; clears all valid bits to 0
valid_in input 1 Incoming valid bit for the first pipeline stage
stall input 1 Freezes the pipeline state when asserted
flush input 1 Clears all valid bits to 0 when asserted
valid_stages output 4 Registered valid bits for pipeline stages 1 through 4 (bits 0 to 3)

Constraints

  • The reset must be asynchronous and active-low
  • The valid_stages output must be updated only on the positive edge of clk or the negative edge of rst_n
  • When flush and stall are both 1, the pipeline must clear to 4'b0000
  • The shift direction must move data from bit 0 (stage 1) towards bit 3 (stage 4)

Topics

PipeliningState ManagementControl Logic

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 8 of 20 in the Pipelining track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.