A6.3.2 Past Global-Clock Functions on a Trace

MediumDebugIcarus · browser samples / server checksReview the topic

Read one vector on a fast global clock and a slow assertion clock, and say when each past function fires.

Hardware context: A four-bit vector watched on a fast global clock and a slow assertion clock. Say when each past global-clock function fires, from the samples one global tick apart.

The main program below has a global clock that ticks every 10 ns and a slow clock clk. One assertion uses the global clock itself. Four more are clocked by clk and each calls one past global-clock function on the vector v.

Write answer(q). For questions 3 to 6 it returns the time of the first failure of one assertion. For question 7 it returns how many times changed_q fails in the whole run.

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;
  logic sys = 0, clk = 0;
  always #5 sys = ~sys;                         // global ticks at 5, 15, 25, ...
  global clocking @(posedge sys); endclocking
  initial begin #10 clk = 1; forever #20 clk = ~clk; end   // clk rises at 10, 50, 90, 130, 170
  logic [3:0] v = 4'd2;

  tick_q:    assert property (@($global_clock) v != 4'd5)        else $display("tick_q fails at %0t", $time);
  rose_q:    assert property (@(posedge clk) !$rose_gclk(v))     else $display("rose_q fails at %0t", $time);
  fell_q:    assert property (@(posedge clk) !$fell_gclk(v))     else $display("fell_q fails at %0t", $time);
  stable_q:  assert property (@(posedge clk) $stable_gclk(v))    else $display("stable_q fails at %0t", $time);
  changed_q: assert property (@(posedge clk) !$changed_gclk(v))  else $display("changed_q fails at %0t", $time);

  initial begin
    #22  v = 4'd5;                              // t=22
    #65  v = 4'd6;                              // t=87
    #40  v = 4'd3;                              // t=127
    #40  v = 4'd1;                              // t=167
    #8   $finish;                               // t=175
  end
endmodule
qReturnProgram
0the value of ticks printed at 38worked example
1the value of $past_gclk(w) printed at 38worked example
2the value of $past(w) printed at 38worked example
3first failure time of tick_qmain, graded
4first failure time of rose_qmain, graded
5first failure time of fell_qmain, graded
6first failure time of stable_qmain, graded
7number of failures of changed_qmain, graded
The worked examples, questions 0 to 2
module tb;
  logic sys = 0, clk = 0;
  always #4 sys = ~sys;                         // global ticks at 4, 12, 20, 28, 36
  global clocking @(posedge sys); endclocking
  initial begin #6 clk = 1; forever #16 clk = ~clk; end    // clk rises at 6 and 38
  logic [3:0] w = 4'd1;
  int ticks = 0;
  always @($global_clock) ticks++;
  always @(posedge clk)
    $display("t=%0t past_gclk=%0d past=%0d ticks=%0d", $time, $past_gclk(w), $past(w), ticks);
  initial begin
    #10 w = 4'd4;                               // t=10
    #20 w = 4'd9;                               // t=30
    #7  w = 4'd7;                               // t=37
    #5  $finish;                                // t=42
  end
endmodule

Simulator note: The key was measured on Verilator 5.052. Your answer function is checked by a small bench on Icarus 12.

Examples

Example 1

Count the Global Ticks
Input
answer(0)
Output
answer = 5

The global clock rises at 4, 12, 20, 28 and 36. Five ticks come before the clk edge at 38.

Example 2

One Global Tick Back
Input
answer(1)
Output
answer = 9

The last global tick before 38 is 36. There w was 9; the change to 7 at 37 comes later.

Example 3

One Tick of the Block's Own Clock Back
Input
answer(2)
Output
answer = 1

The block is clocked by clk. Its last edge was at 6, where w was still 1.

Constraints and Contract

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

Hints

Hint 1

First list the edges of each clock. Then write down the sample of v at every global tick and at every edge of clk.

Hint 2

A past global-clock function at an edge of clk compares with the last global tick before it, 5 ns earlier here.

Hint 3

$rose_gclk and $fell_gclk look at bit 0 only. $stable_gclk and $changed_gclk look at all four bits.

What the grader checks: Eight constant answers: three worked examples and five graded questions, one per construct. They are graded against times and counts measured on Verilator 5.052.

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

Count the Global Ticks

Input
answer(0)
Expected output
answer = 5

The global clock rises at 4, 12, 20, 28 and 36. Five ticks come before the clk edge at 38.

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

Icarus · browser samples / server checks