R9.2.2 Wire It by Name

EasyDebugIcarus · browser samples / server checksReview the topic

A PWM wrapper compiles with no warning and gives the wrong duty cycle. Find the first bad cycle, name the wrong connection, and wire the instance by name.

Hardware context: The read path of a register block with a status register, a data register, an ID constant and a bidirectional pad; and a PWM core inside a top module that renames its ports. Find the first bad cycle of the PWM wrapper, name the wrong connection, and connect the instance by name.

A fan controller wraps a PWM core in a top module that renames its ports. The core is correct and has passed its own tests. The top compiles, Lint is silent, and the fan runs at the wrong speed.

Run the failing test. The bench stores period 5 and duty 2 and runs the timer. Then it stores period 40 and duty 20 and runs it again. It compares pwm and wrap with the brief after every edge.

Step 1: find the first cycle where pwm or wrap shows a value the brief does not give, and name the signal. Step 2: choose the cause from five wrong connections. Each one first goes wrong in a different place.

Step 3 is optional: rewrite the instance so that the mistake cannot happen again, and run the tests.

Signals in the Timeline

SignalWidthRole
period8input of each check
duty8input of each check
mask16input of each check
Full specification
The top module's ports (keep the name and the ports)
module pwm_top (
  input  logic       clk,
  input  logic       rst_n,        // asynchronous, active-low
  input  logic       cfg_we,       // 1: store cfg_period and cfg_duty
  input  logic       run,          // 1: the timer counts
  input  logic [7:0] cfg_period,   // the count wraps after it equals this
  input  logic [7:0] cfg_duty,     // pwm is 1 while the count is below this
  output logic       pwm,
  output logic       wrap          // 1 in the last cycle of a period
);
  // one instance of pwm_core
endmodule
The core (read-only; it is correct)
// pwm_core: the timer inside pwm_top (read-only).
// load = 1 stores period and duty and restarts the count. While en is 1 the count goes 0, 1, ...,
// period, 0, ... out is 1 while the count is below duty; done is 1 in the cycle where the count
// equals period. Both are 0 while en is 0.
module pwm_core (
  input  logic       clk,
  input  logic       rst_n,      // asynchronous, active-low
  input  logic       load,
  input  logic       en,
  input  logic [7:0] period,
  input  logic [7:0] duty,
  output logic       out,
  output logic       done
);
  logic [7:0] per_q, duty_q, cnt;
  always_ff @(posedge clk or negedge rst_n)
    if (!rst_n) begin
      per_q <= 8'd0; duty_q <= 8'd0; cnt <= 8'd0;
    end else if (load) begin
      per_q <= period; duty_q <= duty; cnt <= 8'd0;
    end else if (en) begin
      cnt <= (cnt == per_q) ? 8'd0 : cnt + 8'd1;
    end
  assign out  = en && (cnt < duty_q);
  assign done = en && (cnt == per_q);
endmodule

RuleCorrect top
configureAt an edge with cfg_we = 1 the timer stores cfg_period and cfg_duty and restarts its count at 0.
countAt an edge with run = 1 and cfg_we = 0 the count goes up by 1. After it equals the period it returns to 0.
pwm1 while run is 1 and the count is below the duty.
wrap1 while run is 1 and the count equals the period.
timingpwm and wrap are combinational: they follow run and the count within the same cycle.

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. Inputs change while clk is low.

CyclesWhat the bench applies before the edge that starts them
1reset held low across the edge
2cfg_we with period 5 and duty 2
3 to 11run = 1
12idle
13cfg_we with period 40 and duty 20
14 to 21run = 1
22idle

The fix tests reset the top, store one configuration and apply 16 steps of one edge each. They record pwm and wrap after every edge.

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

Period 5, Duty 2
Input
period=5 duty=2; run mask (step 0 first) 1111111111111111
Output
pwm_step0 = yes, pwm_step1 = no, pwm_step2 = no, pwm_step3 = no, pwm_step4 = no, wrap_step4 = yes, pwm_step5 = yes, pwm_step6 = yes, pwm_step7 = no, pwm_step8 = no, pwm_step9 = no, pwm_step10 = no, wrap_step10 = yes, pwm_step11 = yes, pwm_step12 = yes, pwm_step13 = no, pwm_step14 = no, pwm_step15 = no

The first edge takes the count to 1, and it runs on to 5 and back to 0. pwm is 1 while the count is 0 or 1, and wrap is 1 when it is 5.

Example 2

Period 3, Duty 3, with a Pause
Input
period=3 duty=3; run mask (step 0 first) 1100111100111111
Output
pwm_step0 = yes, pwm_step1 = yes, pwm_step2 = no, pwm_step3 = no, pwm_step4 = no, wrap_step4 = yes, pwm_step5 = yes, pwm_step6 = yes, pwm_step7 = yes, pwm_step8 = no, pwm_step9 = no, pwm_step10 = no, wrap_step10 = yes, pwm_step11 = yes, pwm_step12 = yes, pwm_step13 = yes, pwm_step14 = no, wrap_step14 = yes, pwm_step15 = yes

pwm is 1 for counts 0 to 2 and wrap marks count 3. While run is 0 both outputs are 0 and the count holds.

Example 3

A Long Period
Input
period=40 duty=20; run mask (step 0 first) 1111111111111111
Output
pwm_step0 = yes, pwm_step1 = yes, pwm_step2 = yes, pwm_step3 = yes, pwm_step4 = yes, pwm_step5 = yes, pwm_step6 = yes, pwm_step7 = yes, pwm_step8 = yes, pwm_step9 = yes, pwm_step10 = yes, pwm_step11 = yes, pwm_step12 = yes, pwm_step13 = yes, pwm_step14 = yes, pwm_step15 = yes

With duty 20 the output stays at 1 for all sixteen steps. A duty cut to its low four bits would end the pulse after four.

Constraints and Contract

  • Inputs are known 0/1 values and change while clk is low.
  • Keep the module name and the ports of pwm_top. Do not copy or change pwm_core; the tests supply it.

Hints

Hint 1

With period 5 and duty 2, write down the count, pwm and wrap for cycles 3 to 9 by hand. The first run edge takes the count from 0 to 1. Then compare with the waveform.

Hint 2

Look at how long pwm stays high and how often wrap rises. Which two numbers would give that picture?

Hint 3

Open the port list of pwm_core beside the instance and read both in order. Two neighbours of the same width are the ones to check.

What the grader checks: The failing run is simulated live with the shipped top and the correct core. The bench simulates the correct top and each wrong connection, and the first bad cycle of each comes from those runs. The optional fix is compared with an independent model on eight directed and ten seeded configurations of sixteen steps each. The core is supplied by the tests, so this problem has no synthesis check.

18 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

Period 5, Duty 2

Input
period=5 duty=2; run mask (step 0 first) 1111111111111111
Expected output
pwm_step0 = yes, pwm_step1 = no, pwm_step2 = no, pwm_step3 = no, pwm_step4 = no, wrap_step4 = yes, pwm_step5 = yes, pwm_step6 = yes, pwm_step7 = no, pwm_step8 = no, pwm_step9 = no, pwm_step10 = no, wrap_step10 = yes, pwm_step11 = yes, pwm_step12 = yes, pwm_step13 = no, pwm_step14 = no, pwm_step15 = no

The first edge takes the count to 1, and it runs on to 5 and back to 0. pwm is 1 while the count is 0 or 1, and wrap is 1 when it is 5.

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

Icarus · browser samples / server checks