R10.4.1 The Mask Compare That Always Matched

EasyFixIcarus Verilog + Yosys · serverReview the topic

Fix a packet filter that counts almost every packet as a match and raises its alarm a cycle early. Both faults are in how operators were written.

Hardware context: A packet filter with a masked address compare, a match counter and an alarm flag; and a gain stage that scales a signed byte by an unsigned gain in quarters and saturates. Fix the compare that reads by precedence and the increment that makes the alarm early.

A packet filter watches a bus for one device. A packet matches when its address equals base on the bits that mask selects. The filter counts the matches and raises alarm one cycle after the count was 3.

In the lab the count ran far ahead of the real traffic: nearly every valid packet was counted. And when the test bench was tightened, alarm turned out to rise one cycle before the brief says.

This is a Fix problem. The design compiles, and each line reads as its author meant it. Find what the simulator reads instead, and change as little as you can.

Brief: rising-edge clk; rst_n asynchronous and active-low, clearing hits and alarm. match is combinational: valid is 1, and addr equals base on every bit where mask is 1. At an edge with match = 1, hits goes up by 1 and wraps from 15 to 0. At every edge, alarm loads 1 if hits was 3 before that edge, else 0. 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
base8input of each check
mask8input of each check
script108input of each check
Full specification
The filter's ports (keep the name and the ports)
module pkt_filter (
  input  logic       clk,
  input  logic       rst_n,      // asynchronous, active-low
  input  logic       valid,      // 1: addr holds a packet address at this edge
  input  logic [7:0] addr,
  input  logic [7:0] base,       // the address to match
  input  logic [7:0] mask,       // 1: compare this bit; 0: ignore it
  output logic       match,      // combinational: this packet matches
  output logic [3:0] hits,       // matches counted since reset; wraps from 15 to 0
  output logic       alarm       // registered: 1 for the cycle after hits was 3
);
  ...
endmodule
OutputRule
matchvalid = 1 and (addr and base agree on every bit where mask is 1). Bits where mask is 0 are ignored in both.
hitsAt a rising edge out of reset with match = 1: hits + 1, wrapping from 15 to 0.
alarmAt every rising edge out of reset: 1 if hits was 3 before the edge, else 0.

Each test sets base and mask, resets the filter and applies 12 packets, one per edge. A packet shown as -- has valid = 0. The result holds match, hits and alarm 1 ns after each edge.

hits is a register: it must change only after every block has read it at the edge. The fixture watches for an earlier change, and early_hits_write must stay false.

Examples

Example 1

The Top Nibble Selects a Device
Input
base=a0 mask=f0 (hex); packets: a3 53 af b0 a0 -- a1 0a -- a9 aa 5a
Output
match_0 = yes, hits_0 = 1, alarm_0 = no, match_1 = no, hits_1 = 1, alarm_1 = no, match_2 = yes, hits_2 = 2, alarm_2 = no, match_3 = no, hits_3 = 2, alarm_3 = no, match_4 = yes, hits_4 = 3, alarm_4 = no, match_5 = no, hits_5 = 3, alarm_5 = yes, match_6 = yes, hits_6 = 4, alarm_6 = yes, match_7 = no, hits_7 = 4, alarm_7 = no, match_8 = no, hits_8 = 4, alarm_8 = no, match_9 = yes, hits_9 = 5, alarm_9 = no, match_10 = yes, hits_10 = 6, alarm_10 = no, match_11 = no, hits_11 = 6, alarm_11 = no, early_hits_write = no

Only addresses a0 to af match. Six of the ten valid packets do. hits is 3 after step 4, so alarm is 1 at steps 5 and 6. The fourth match at step 6 moves hits on, and alarm falls at step 7.

Example 2

Three Matches in a Row
Input
base=42 mask=ff (hex); packets: 42 42 42 -- -- 43 42 -- -- -- -- --
Output
match_0 = yes, hits_0 = 1, alarm_0 = no, match_1 = yes, hits_1 = 2, alarm_1 = no, match_2 = yes, hits_2 = 3, alarm_2 = no, match_3 = no, hits_3 = 3, alarm_3 = yes, match_4 = no, hits_4 = 3, alarm_4 = yes, match_5 = no, hits_5 = 3, alarm_5 = yes, match_6 = yes, hits_6 = 4, alarm_6 = yes, match_7 = no, hits_7 = 4, alarm_7 = no, match_8 = no, hits_8 = 4, alarm_8 = no, match_9 = no, hits_9 = 4, alarm_9 = no, match_10 = no, hits_10 = 4, alarm_10 = no, match_11 = no, hits_11 = 4, alarm_11 = no, early_hits_write = no

hits is 3 after step 2 and stays 3 until the next match at step 6. alarm is 1 from step 3 to step 6.

Example 3

No Packet Is Valid
Input
base=00 mask=00 (hex); packets: -- -- -- -- -- -- -- -- -- -- -- --
Output
match_0 = no, hits_0 = 0, alarm_0 = no, match_1 = no, hits_1 = 0, alarm_1 = no, match_2 = no, hits_2 = 0, alarm_2 = no, match_3 = no, hits_3 = 0, alarm_3 = no, match_4 = no, hits_4 = 0, alarm_4 = no, match_5 = no, hits_5 = 0, alarm_5 = no, match_6 = no, hits_6 = 0, alarm_6 = no, match_7 = no, hits_7 = 0, alarm_7 = no, match_8 = no, hits_8 = 0, alarm_8 = no, match_9 = no, hits_9 = 0, alarm_9 = no, match_10 = no, hits_10 = 0, alarm_10 = no, match_11 = no, hits_11 = 0, alarm_11 = no, early_hits_write = no

valid is 0 at every step, so nothing matches whatever addr holds, and hits stays 0.

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 the compare apart by precedence. Which binds first in that line: | or ==?

Hint 2

An expression used as a condition is true when it is not zero. What is the width of the compare line's result, and when is it zero?

Hint 3

For alarm, ask which value of hits the test reads at the edge where the third match arrives. An increment written with ++ is a blocking assignment.

What the grader checks: Seven directed and eleven seeded scripts of twelve packets each are compared with an independent model. It checks match, hits and alarm after every edge. The fixture also flags a blocking write to hits. Passing shows the filter follows the brief on these scripts; the synthesis check adds the latch count.

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 Top Nibble Selects a Device

Input
base=a0 mask=f0 (hex); packets: a3 53 af b0 a0 -- a1 0a -- a9 aa 5a
Expected output
match_0 = yes, hits_0 = 1, alarm_0 = no, match_1 = no, hits_1 = 1, alarm_1 = no, match_2 = yes, hits_2 = 2, alarm_2 = no, match_3 = no, hits_3 = 2, alarm_3 = no, match_4 = yes, hits_4 = 3, alarm_4 = no, match_5 = no, hits_5 = 3, alarm_5 = yes, match_6 = yes, hits_6 = 4, alarm_6 = yes, match_7 = no, hits_7 = 4, alarm_7 = no, match_8 = no, hits_8 = 4, alarm_8 = no, match_9 = yes, hits_9 = 5, alarm_9 = no, match_10 = yes, hits_10 = 6, alarm_10 = no, match_11 = no, hits_11 = 6, alarm_11 = no, early_hits_write = no

Only addresses a0 to af match. Six of the ten valid packets do. hits is 3 after step 4, so alarm is 1 at steps 5 and 6. The fourth match at step 6 moves hits on, and alarm falls at step 7.

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

Icarus Verilog + Yosys · server