A PWM generator gives the right pulse for three periods and a pulse too long in the last. Find the first bad cycle and signal, name the bug, and fix it.
Hardware context: Small designs with one output that comes out wrong: a UART transmitter, a packet adder and a pulse generator. Finding the cycle a register took a wrong value.
pwm makes a pulse in every period of ten clocks: out is 1 for duty clocks, then 0. A new duty may arrive at any time, but it must only take effect from the next period.
The bench runs four periods with duty 3, then changes duty to 7 in the middle of the last period. It counts the clocks out is 1 in each period. Run it and read its log and its waveform; the waveform also shows cnt and duty_q inside the design.
Step 1: find the first cycle where out, cnt or duty_q is not what the specification says, and name the signal. Step 2: choose the cause. Step 3, optional: fix pwm; the fix is graded by the normal rows.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
scenario | 8 | input of each check |
Full specification
// pwm: a counter runs 0 to 9 and wraps. out is 1 while the counter is below duty_q.
// duty_q takes the input duty only in the clock the counter wraps, so a period never changes halfway.
module pwm (input logic clk, rst, input logic [3:0] duty, output logic out);
...
endmodule
After reset, cnt is 0 and duty_q is 0. Cycle N is the stretch after the N-th falling edge of the bench's clock.
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 3 is optional: fix the code in the editor, then run the tests.
Examples
Example 1
Duty 3 Throughout- Input
duty 3 throughout
- Output
high1 = 3, high2 = 3, high3 = 3, high4 = 3
duty 3 throughout: out is 1 for 3 clocks in each of the four counted periods.
Example 2
Duty 0, Then 10- Input
duty 0, then 10
- Output
high1 = 0, high2 = 0, high3 = 10, high4 = 10
duty changes from 0 to 10 halfway through the third period. The new value starts with the fourth: 0, 0, 0, 10.
Example 3
Duty Changes from 2 to 8 in the Middle of a Period- Input
duty changes from 2 to 8 in the middle of a period
- Output
high1 = 2, high2 = 2, high3 = 8, high4 = 8
A change in the middle of a period waits for the wrap: 2, 2, then 8.
Constraints and Contract
- Keep the module name, the ports and the names cnt and duty_q.
- Reset is synchronous and active high.
Hints
Hint 1
Which period fails, and in which clock of it did duty change?
Hint 2
Watch duty_q: when may it change, and when does it?
Hint 3
Each cause shows first in a different cycle or on a different signal.
What the grader checks: The failing run is simulated live with the shipped generator. The fix is graded on five scripts of duty values, fifty clocks each after reset. Each row reports the clocks out is 1 in each period after the first.
5 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 problems on this path