R11.1.1 Predict the Print

EasyImplementIcarus Verilog + Yosys · serverReview the topic

Say what $display, $strobe and $monitor print at one clock edge of a small logger, for any values before the edge.

Hardware context: A logger block with one blocking and two nonblocking assignments and three print tasks; and a register pair with load and swap. Write the model of what the three print tasks show at one clock edge.

A logger block mixes one blocking and two nonblocking assignments, then prints with all three print tasks. A colleague reads its log and reports "bugs" that are only prints of half-updated values. You are asked to write down, once and for all, what each task shows.

Write print_model. Its inputs are the values of acc, peak and last before a rising edge, and d at that edge. Its outputs are the three numbers that $display prints and the three that $strobe prints. One more output says whether $monitor prints a line in that time slot.

This is an Implement problem with no clock: the model is combinational. The tests sweep many values, so one recorded run is not enough to copy. Work out the rule for each task from the event regions (lesson R11.1).

Signals in the Timeline

SignalWidthRole
script48input of each check
Full specification
The logger (fixed; you do not edit it)
// The logger: one clocked block and one $monitor.
logic [3:0] acc, peak, last;

always @(posedge clk) begin
  acc  <= acc + d;                          // running sum, 4 bits, wraps
  peak  = (d > peak) ? d : peak;            // largest d so far
  last <= peak;                             // a copy of peak
  $display("D acc=%0d peak=%0d last=%0d", acc, peak, last);
  $strobe ("S acc=%0d peak=%0d last=%0d", acc, peak, last);
end

initial $monitor("M acc=%0d peak=%0d last=%0d", acc, peak, last);
The model you write (keep the name and the ports)
module print_model (
  input  logic [3:0] acc0,       // acc before the edge
  input  logic [3:0] peak0,      // peak before the edge
  input  logic [3:0] last0,      // last before the edge
  input  logic [3:0] d,          // d at the edge
  output logic [3:0] d_acc,      // the three numbers $display prints
  output logic [3:0] d_peak,
  output logic [3:0] d_last,
  output logic [3:0] s_acc,      // the three numbers $strobe prints
  output logic [3:0] s_peak,
  output logic [3:0] s_last,
  output logic       mon_line    // 1 when $monitor prints a line in this time slot
);
  // your model
endmodule
OutputMeaning
d_acc, d_peak, d_lastthe three numbers on the D line of this edge
s_acc, s_peak, s_lastthe three numbers on the S line of this edge
mon_line1 if an M line is printed in this time slot on Icarus 12, 0 if not
One recorded run on Icarus 12. Before the first edge acc = 2, peak = 3 and last = 3. d is 5, then 4, then 0
M acc=2 peak=3 last=3
D acc=2 peak=5 last=3
M acc=7 peak=5 last=5
S acc=7 peak=5 last=5
D acc=7 peak=5 last=5
S acc=11 peak=5 last=5
M acc=11 peak=5 last=5
D acc=11 peak=5 last=5
S acc=11 peak=5 last=5

All values are 4 bits wide and unsigned. The sum wraps: 15 + 1 is 0. The first M line of the log is printed at time 0, before any edge. The order of an S line and an M line in one slot is not fixed.

Each test applies three sets of values, one per nanosecond, and records every output after each. 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.

Examples

Example 1

The Recorded Run
Input
acc=2 peak=3 last=3 d=5; acc=7 peak=5 last=5 d=4; acc=11 peak=5 last=5 d=0
Output
d_acc_set0 = 2, d_peak_set0 = 5, d_last_set0 = 3, s_acc_set0 = 7, s_peak_set0 = 5, s_last_set0 = 5, mon_line_set0 = yes, d_acc_set1 = 7, d_peak_set1 = 5, d_last_set1 = 5, s_acc_set1 = 11, s_peak_set1 = 5, s_last_set1 = 5, mon_line_set1 = yes, d_acc_set2 = 11, d_peak_set2 = 5, d_last_set2 = 5, s_acc_set2 = 11, s_peak_set2 = 5, s_last_set2 = 5, mon_line_set2 = no

These are the three edges of the recorded log. At the third edge d is 0 and last already equals peak, so nothing changes and no M line appears.

Example 2

A New Peak
Input
acc=0 peak=0 last=0 d=1; acc=1 peak=1 last=1 d=9; acc=10 peak=9 last=9 d=15
Output
d_acc_set0 = 0, d_peak_set0 = 1, d_last_set0 = 0, s_acc_set0 = 1, s_peak_set0 = 1, s_last_set0 = 1, mon_line_set0 = yes, d_acc_set1 = 1, d_peak_set1 = 9, d_last_set1 = 1, s_acc_set1 = 10, s_peak_set1 = 9, s_last_set1 = 9, mon_line_set1 = yes, d_acc_set2 = 10, d_peak_set2 = 15, d_last_set2 = 9, s_acc_set2 = 9, s_peak_set2 = 15, s_last_set2 = 15, mon_line_set2 = yes

With d above peak, the D line already shows the new peak. It still shows the old acc and the old last.

Example 3

Nothing Changes
Input
acc=4 peak=6 last=6 d=0; acc=0 peak=0 last=0 d=0; acc=15 peak=15 last=15 d=0
Output
d_acc_set0 = 4, d_peak_set0 = 6, d_last_set0 = 6, s_acc_set0 = 4, s_peak_set0 = 6, s_last_set0 = 6, mon_line_set0 = no, d_acc_set1 = 0, d_peak_set1 = 0, d_last_set1 = 0, s_acc_set1 = 0, s_peak_set1 = 0, s_last_set1 = 0, mon_line_set1 = no, d_acc_set2 = 15, d_peak_set2 = 15, d_last_set2 = 15, s_acc_set2 = 15, s_peak_set2 = 15, s_last_set2 = 15, mon_line_set2 = no

d is 0 and last equals peak in all three sets. The D and S lines print the same numbers, and there is no M line.

Constraints and Contract

  • Inputs are known 0/1 values. Each set is held for one nanosecond and the outputs are read at its end.
  • Keep the module name and the ports. The model has no clock and no memory.

Hints

Hint 1

Take the statements one at a time and ask in which region each one changes its target. Two of the three variables change later than the line that writes them.

Hint 2

The D line is printed where it stands in the block. Which assignments above it have already changed their target at that point?

Hint 3

An M line appears only when the slot ends with some argument different from the last line printed. In the recorded run the third edge has no M line. Find out why from its values.

What the grader checks: Six directed and twelve seeded tests apply three sets of values each. The outputs are compared with an independent model of the event regions. The recorded log comes from a real run of the logger on Icarus 12.

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.

18 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

The Recorded Run

Input
acc=2 peak=3 last=3 d=5; acc=7 peak=5 last=5 d=4; acc=11 peak=5 last=5 d=0
Expected output
d_acc_set0 = 2, d_peak_set0 = 5, d_last_set0 = 3, s_acc_set0 = 7, s_peak_set0 = 5, s_last_set0 = 5, mon_line_set0 = yes, d_acc_set1 = 7, d_peak_set1 = 5, d_last_set1 = 5, s_acc_set1 = 11, s_peak_set1 = 5, s_last_set1 = 5, mon_line_set1 = yes, d_acc_set2 = 11, d_peak_set2 = 5, d_last_set2 = 5, s_acc_set2 = 11, s_peak_set2 = 5, s_last_set2 = 5, mon_line_set2 = no

These are the three edges of the recorded log. At the third edge d is 0 and last already equals peak, so nothing changes and no M line appears.

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

Icarus Verilog + Yosys · server