A6.8.2 The Asynchronous Compare Pitfall

MediumDebugIcarus · browser samples / server checksReview the topic

Three assertions are clocked by a code and its delayed copy. Say when each one fails, and what it read.

Hardware context: An error code bus and a halt line with no common clock. Find what an assertion clocked by the bus itself really reads, and when it reports.

The rule: whenever code holds the error code 14, halt is high. The main program writes it three ways. naive is clocked by code itself. kept and fixed are clocked by a copy of code delayed by 2.

Write answer(q). Questions 3 to 7 ask for failure times, a printed value and a count.

Questions 0 to 2 are worked examples on a smaller program. Questions 3 to 7 are graded only by the full check.

Signals in the Timeline

SignalWidthRole
question4input of each check
Full specification
The main program, questions 3 to 7
module tb;
  localparam logic [3:0] ERR = 4'd14;
  logic [3:0] code = 4'd0;
  logic halt = 0;
  logic [3:0] code_d;
  assign #2 code_d = code;                      // code, two time units later

  naive: assert property (@(code) code == ERR |-> halt)
    else $display("naive fails at %0t: code=%0d", $time, $sampled(code));
  kept:  assert property (@(code_d) code_d == ERR |-> halt)
    else $display("kept fails at %0t", $time);
  fixed: assert property (@(code_d) code == ERR |-> halt)
    else $display("fixed fails at %0t", $time);

  initial begin
    #10 code = ERR; halt = 1;                   // t=10: legal
    #10 code = 4'd2; halt = 0;                  // t=20
    #10 code = ERR;                             // t=30: no halt
    #5  halt = 1;                               // t=35
    #5  code = 4'd5;                            // t=40
    #5  halt = 0;                               // t=45
    #5  code = ERR;                             // t=50: no halt
    #10 code = 4'd7;                            // t=60
    #10 $finish;                                // t=70
  end
endmodule
qReturnProgram
0the time of the first failure of busy_ruleworked example
1the value of st it printsworked example
2how many times busy_rule failsworked example
3the time of the first failure of naivemain, graded
4the value of code it printsmain, graded
5how many times naive failsmain, graded
6the time of the first failure of fixedmain, graded
7the time of the first failure of keptmain, graded
The worked examples, questions 0 to 2
module tb;
  logic [1:0] st = 2'd0;
  logic ready = 1;
  busy_rule: assert property (@(st) st == 2'd2 |-> !ready)
    else $display("busy_rule fails at %0t: st=%0d", $time, $sampled(st));
  initial begin
    #8  st = 2'd2; ready = 0;                   // t=8: legal
    #6  st = 2'd1;                              // t=14
    #2  ready = 1;                              // t=16
    #4  st = 2'd2;                              // t=20: ready still high
    #9  st = 2'd3;                              // t=29
    #6  $finish;                                // t=35
  end
endmodule

Simulator note: The key was measured on Verilator 5.052 and agrees with IEEE 1800-2023 16.5.1. Your answer function is checked by a small bench on Icarus 12.

Examples

Example 1

Find the Failing Change
Input
answer(0)
Output
answer = 29

At 29, st leaves 2 while ready is high. The assertion reads st as 2 there.

Example 2

The Value It Read
Input
answer(1)
Output
answer = 2

$sampled(st) in the action block is the value st had before the change: 2.

Example 3

Count Them
Input
answer(2)
Output
answer = 1

At 20, st becomes 2 with ready high, but the assertion reads the old value 1 and stays vacuous.

Constraints and Contract

  • Return one value per question from answer(q).
  • Keep the function header.

Hints

Hint 1

List every change of code and of code_d. Each one starts an attempt of the assertions it clocks.

Hint 2

An assertion reads sampled values: the values from before the time step of its event.

Hint 3

At a change of code, the sampled code is the value it is leaving.

What the grader checks: Eight constant answers: three worked examples and five graded questions. They are graded against values measured on Verilator 5.052 and derived by hand from the standard.

8 checks on Icarus · browser samples / server checks. 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

Find the Failing Change

Input
answer(0)
Expected output
answer = 29

At 29, st leaves 2 while ready is high. The assertion reads st as 2 there.

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

Icarus · browser samples / server checks