R11.1.2 The Race That Changed with the Order

MediumFixIcarus Verilog + Yosys · serverReview the topic

A register pair gives different results depending on which of its two blocks the simulator runs first. Make the result the same under every order.

Hardware context: A logger block with one blocking and two nonblocking assignments and three print tasks; and a register pair with load and swap. Fix the register pair so its result is the same under every order of its blocks.

A register pair keeps the newest word in a and the word before it in b. load shifts a new word in. swap exchanges the two words. The block passed its tests for months. Then a tool upgrade changed the order in which the simulator runs the two blocks, and half the regression failed.

This is a Fix problem: the starter is the shipped design. The tests reset the pair, then apply ten steps of one rising edge each and read a and b after every edge. This simulator does not keep one order from edge to edge, so one test meets both orders.

Fix the design so that its result cannot depend on the order of the blocks. A change that only moves one block behind the other is not a fix: each block reads the other's register. Synthesis check: the design must build on the site's synthesis flow with no latch. The cell and level counts come from an open-source flow and are a proxy for area and depth, not a foundry result.

Signals in the Timeline

SignalWidthRole
script60input of each check
Full specification
The pair's ports (keep the name and the ports)
module pair_reg (
  input  logic       clk,
  input  logic       rst_n,      // asynchronous, active-low
  input  logic       load,       // 1: a takes din, b takes the old a; wins over swap
  input  logic       swap,       // 1: a and b exchange their values
  input  logic [3:0] din,
  output logic [3:0] a,
  output logic [3:0] b
);
  ...
endmodule
At a rising edgea becomesb becomes
rst_n = 0 (asynchronous)00
load = 1 (with swap 0 or 1)dinthe value a held before the edge
load = 0, swap = 1the value b held before the edgethe value a held before the edge
load = 0, swap = 0unchangedunchanged

The result is {b, a} after each of the ten edges. Step 0 gives a_step0 (bits 3:0) and b_step0 (bits 7:4), and so on up to step 9.

Examples

Example 1

Loads in a Row
Input
load 1, load 2, load 3, load 4, load 5, load 6, load 7, load 8, load 9, load a
Output
a_step0 = 0x1, b_step0 = 0x0, a_step1 = 0x2, b_step1 = 0x1, a_step2 = 0x3, b_step2 = 0x2, a_step3 = 0x4, b_step3 = 0x3, a_step4 = 0x5, b_step4 = 0x4, a_step5 = 0x6, b_step5 = 0x5, a_step6 = 0x7, b_step6 = 0x6, a_step7 = 0x8, b_step7 = 0x7, a_step8 = 0x9, b_step8 = 0x8, a_step9 = 0xa, b_step9 = 0x9

Each load puts din in a and moves the old a to b, so b is always one word behind a.

Example 2

Swaps in a Row
Input
load 3, load c, swap, swap, swap, swap, swap, swap, swap, swap
Output
a_step0 = 0x3, b_step0 = 0x0, a_step1 = 0xc, b_step1 = 0x3, a_step2 = 0x3, b_step2 = 0xc, a_step3 = 0xc, b_step3 = 0x3, a_step4 = 0x3, b_step4 = 0xc, a_step5 = 0xc, b_step5 = 0x3, a_step6 = 0x3, b_step6 = 0xc, a_step7 = 0xc, b_step7 = 0x3, a_step8 = 0x3, b_step8 = 0xc, a_step9 = 0xc, b_step9 = 0x3

After two loads a is c and b is 3. Each swap exchanges them, so the two words alternate.

Example 3

Loads and Swaps in Turn
Input
load 5, swap, load 9, swap, load 2, swap, load e, swap, load 7, swap
Output
a_step0 = 0x5, b_step0 = 0x0, a_step1 = 0x0, b_step1 = 0x5, a_step2 = 0x9, b_step2 = 0x0, a_step3 = 0x0, b_step3 = 0x9, a_step4 = 0x2, b_step4 = 0x0, a_step5 = 0x0, b_step5 = 0x2, a_step6 = 0xe, b_step6 = 0x0, a_step7 = 0x0, b_step7 = 0xe, a_step8 = 0x7, b_step8 = 0x0, a_step9 = 0x0, b_step9 = 0x7

A load after a swap shifts the swapped word into b. Every edge needs both old values.

Constraints and Contract

  • Inputs are known 0/1 values and change while clk is low.
  • Keep the module name and the ports.

Hints

Hint 1

Take one swap edge and work it out by hand twice. Run the a block first, then the b block first. Neither order gives the table's row.

Hint 2

Both blocks need the value the other register held before the edge. Ask in which event region a register should change, so that every reader is done first.

Hint 3

Write both registers with nonblocking assignments. Then the right sides are read in the Active region and both registers change together in the NBA region.

What the grader checks: Six directed and ten seeded scripts of ten edges each are compared with an independent model. Icarus 12 alternates the order of the two blocks between edges, so the scripts meet both orders. Passing shows the design is right on these scripts under the orders this simulator used.

The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.

16 checks on Icarus Verilog + Yosys · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Loads in a Row

Input
load 1, load 2, load 3, load 4, load 5, load 6, load 7, load 8, load 9, load a
Expected output
a_step0 = 0x1, b_step0 = 0x0, a_step1 = 0x2, b_step1 = 0x1, a_step2 = 0x3, b_step2 = 0x2, a_step3 = 0x4, b_step3 = 0x3, a_step4 = 0x5, b_step4 = 0x4, a_step5 = 0x6, b_step5 = 0x5, a_step6 = 0x7, b_step6 = 0x6, a_step7 = 0x8, b_step7 = 0x7, a_step8 = 0x9, b_step8 = 0x8, a_step9 = 0xa, b_step9 = 0x9

Each load puts din in a and moves the old a to b, so b is always one word behind a.

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

Icarus Verilog + Yosys · server