R9.3.2 The Typedef That Changed with File Order

MediumDebugIcarus Verilog + Yosys · serverReview the topic

A packet length counter worked alone and failed in the full build. Find the first bad cycle, name the cause among five real bugs, and fix it for any file order.

Hardware context: An 8-bit ALU on a shared package (a word type, an opcode enum with gaps in its encoding, a 9-bit add function); and a packet length counter compiled after another team's type file. Find the first bad cycle of the length counter in the full build, name the cause, and fix it for any file order.

A block counts the bytes of each packet. count shows the bytes so far, len holds the length of the last finished packet, and done pulses once per packet. Its own test passed. In the full build, long packets came out short. Nobody had changed pkt_len.sv.

The build compiles a list of files as one unit, as the simulators here do. The file uart_types.sv, owned by another team, was added to the list ahead of pkt_len.sv last week. The specification shows it. You cannot change it or its place in the list.

Step 1: run the failing test. Find the first cycle where a signal shows a value the brief does not give, and name the signal. Step 2: choose the cause from five real bugs. Each one first goes wrong in a different place.

Step 3 is optional: fix pkt_len.sv so that no other file can change its types. The fix tests compile the same earlier file first, and they send packets of up to 63 bytes.

Signals in the Timeline

SignalWidthRole
script40input of each check
Full specification
File 1 of the build list (read-only; compiled before yours)
// ---- uart_types.sv: earlier in the build list, owned by another team ----
`ifndef LEN_T_DEFINED
`define LEN_T_DEFINED
typedef logic [3:0] len_t;         // a UART frame: up to 15 bits
`endif

The counter's ports (keep the name and the ports)
module pkt_len (
  input  logic       clk,
  input  logic       rst_n,        // asynchronous, active-low
  input  logic       valid,        // 1: a byte of the packet at this edge
  input  logic       last,         // with valid: this byte ends the packet
  output logic [7:0] count,        // bytes seen so far in the packet in progress
  output logic [7:0] len,          // length of the last finished packet
  output logic       done          // 1 for one cycle after a packet ends
);
  ...
endmodule
RuleCorrect counter
resetWhile rst_n is 0: count 0, len 0, done 0.
a byteAt an edge with valid = 1 and last = 0, count goes up by 1.
the last byteAt an edge with valid = 1 and last = 1: len becomes count + 1 and count becomes 0. done is 1 for the next cycle.
idleAt an edge with valid = 0 nothing changes and done is 0. last has no meaning without valid.
sizeA packet has 1 to 255 bytes.

Cycle 0 is the time before the first rising edge. Each rising edge starts the next cycle. A signal's value in cycle N is its value at the end of that cycle. The bench sends packets of 3, 20 and 5 bytes. The 20-byte packet has one idle cycle in it, during which last is 1.

For the fix: move len_t into a package named pkt_pkg, keep it 8 bits, and import it in pkt_len. A compile check looks for pkt_pkg::len_t at 8 bits. Each fix test resets the counter, sends up to four packets with gaps, and runs 96 edges in all. It records len after each packet, the number of done cycles and the largest count. 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.

Diagnose the Failing Run

The test below runs the buggy design. Read its log, then find where it first goes wrong in the waveform.

Log

Waveform

Click a signal at a cycle to select it. Or focus the waveform and use the arrow keys: Left and Right move one cycle, Up and Down one signal.

Step 1 · The first bad cycle and signal
Step 2 · The cause

Step 3 is optional: fix the code in the editor, then run the tests.

Examples

Example 1

Four Short Packets
Input
1 idle, 3 bytes; 1 bytes; 2 idle, 5 bytes; 2 bytes
Output
len_packet0 = 3, len_packet1 = 1, len_packet2 = 5, len_packet3 = 2, done_cycles = 4, largest_count = 4

Packets of 3, 1, 5 and 2 bytes. Each len is its packet's byte count, done is 1 in four cycles, and count never passes 4.

Example 2

A Packet of 20 Bytes
Input
1 idle, 4 bytes; 1 idle, 20 bytes; 2 bytes; 0 idle
Output
len_packet0 = 4, len_packet1 = 20, len_packet2 = 2, len_packet3 = 0, done_cycles = 3, largest_count = 19

count must reach 19 before the last byte, and len must read 20. A counter that is 4 bits wide cannot hold either.

Example 3

A Stray Last While Idle
Input
2 idle (last = 1 on the first), 3 bytes; 3 idle (last = 1 on the first), 6 bytes; 1 idle (last = 1 on the first), 1 bytes; 2 idle (last = 1 on the first)
Output
len_packet0 = 3, len_packet1 = 6, len_packet2 = 1, len_packet3 = 0, done_cycles = 3, largest_count = 5

last is 1 in idle cycles before three packets and after the last one. Without valid it means nothing, so done is 1 in three cycles only.

Constraints and Contract

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

Hints

Hint 1

The log names the first failing check. Look at count in that cycle and in the cycle before it. What number could it not reach?

Hint 2

The counter is declared with a type, not with a width. Find every place in the build where that type name is declared, and read the lines around each one.

Hint 3

A guard of the form ifndef, define, endif keeps the first definition the compiler meets and skips the rest. Which file does the compiler meet first?

What the grader checks: The failing run is simulated live with the shipped file after the earlier file. The bench runs the correct counter and each buggy one. The first bad cycle and every option's first wrong value come from those runs. The optional fix is compared with an independent model on six directed and ten seeded scripts, compiled after the same earlier file. A compile check confirms that pkt_pkg::len_t exists and is 8 bits wide. It cannot see whether pkt_len uses that type for its 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.

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

Four Short Packets

Input
1 idle, 3 bytes; 1 bytes; 2 idle, 5 bytes; 2 bytes
Expected output
len_packet0 = 3, len_packet1 = 1, len_packet2 = 5, len_packet3 = 2, done_cycles = 4, largest_count = 4

Packets of 3, 1, 5 and 2 bytes. Each len is its packet's byte count, done is 1 in four cycles, and count never passes 4.

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

Icarus Verilog + Yosys · server