A clock-gated register misses a load that its enable asked for. Find the first step where the gate goes wrong, name the cause, and fix the gate.
Hardware context: Register on a gated clock beside the same register behind an enable mux. Find the first step where the gated register diverges and name the clock-gate fault.
A register sits behind a latch-based clock gate. It must load exactly what the same register behind an enable mux would load. Lesson W2.3 proved that a correct gate does.
The design works in steps of half a clock period, as in lesson W2.3. phase is the level of the design clock in each step. The bench keeps its own enable-mux register and prints an ERROR when q differs from it.
Step 1: find the first cycle where a signal shows a value a correct gate would not, and name the signal. The ERROR line is not that place. Step 2: choose the cause from four real bugs. Step 3 is optional: fix the design.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
script | 60 | input of each check |
Full specification
module cg_reg (
input logic clk, // the step clock: each rising edge ends one half-period step
input logic rst, // synchronous, active-high
input logic phase, // the level of the design clock in this step: 1 = high
input logic en, // the enable of the register
input logic [3:0] d,
output logic en_lat, // the enable as the clock gate's latch passes it
output logic gclk, // the gated design clock
output logic [3:0] q
);
// the clock gate and the register
endmodule
| Rule | Correct gate |
|---|---|
| Step | Each rising edge of clk ends a step. A step is half a period of the design clock. phase is 1 in a high step and 0 in a low step. |
| Latch | en_lat follows en while the design clock is low, and holds its value while the clock is high. |
| Gated clock | gclk is 1 in a high step whose en_lat is 1. |
| Register | At the end of a step where gclk rose, q loads d as it was one step earlier. |
| Reset | Synchronous, active-high: the latch, the edge detector and q clear to 0. |
Cycle N is the step where the bench's cycle counter shows N. The bench drives en and d at the start of the stretch and checks q 1 ns later. The edge that ends the step comes 4 ns in. A signal is wrong in cycle N if a correct gate shows another value at the end of the stretch.
The tests apply a reset step, then twelve steps of (en, d), starting with a high step. They record q after the edge that ends each step: q_after_step0 to q_after_step11.
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
The Enable Changes Just After Each Rise- Input
H:en 1, L:en 2, H:-- 3, L:-- 4, H:en 5, L:en 6, H:en 7, L:en 8, H:-- 9, L:-- a, H:en b, L:-- c
- Output
q_after_step0 = 0x0, q_after_step1 = 0x0, q_after_step2 = 0x2, q_after_step3 = 0x2, q_after_step4 = 0x2, q_after_step5 = 0x2, q_after_step6 = 0x6, q_after_step7 = 0x6, q_after_step8 = 0x8, q_after_step9 = 0x8, q_after_step10 = 0x8, q_after_step11 = 0x8
Enables driven by flops on the design clock change just after it rises. A correct gate holds the enable of the low step through the high step. So q follows the mux register: 1 after the first high step it applies to, and so on.
Example 2
The Enable Changes in the Low Half- Input
H:-- 1, L:en 2, H:en 3, L:-- 4, H:-- 5, L:en 6, H:en 7, L:-- 8, H:-- 9, L:en a, H:-- b, L:en c
- Output
q_after_step0 = 0x0, q_after_step1 = 0x0, q_after_step2 = 0x2, q_after_step3 = 0x2, q_after_step4 = 0x2, q_after_step5 = 0x2, q_after_step6 = 0x6, q_after_step7 = 0x6, q_after_step8 = 0x6, q_after_step9 = 0x6, q_after_step10 = 0xa, q_after_step11 = 0xa
Here the enable also changes while the clock is low. The latch passes it straight through, and the next high step uses it.
Example 3
A One-Step Enable in Each Half- Input
H:-- 3, L:en 4, H:-- 5, L:-- 6, H:en 7, L:-- 8, H:-- 9, L:en a, H:-- b, L:-- c, H:en d, L:-- e
- Output
q_after_step0 = 0x0, q_after_step1 = 0x0, q_after_step2 = 0x4, q_after_step3 = 0x4, q_after_step4 = 0x4, q_after_step5 = 0x4, q_after_step6 = 0x4, q_after_step7 = 0x4, q_after_step8 = 0xa, q_after_step9 = 0xa, q_after_step10 = 0xa, q_after_step11 = 0xa
Short enable pulses, one step each. q must match the enable-mux register after every step, so the gate may neither miss a pulse nor add an edge.
Constraints and Contract
- The enable may change in any step, in the high half or the low half.
- Keep the module name and the ports.
Hints
Hint 1
Find the ERROR, then walk back. Which gclk pulse should have loaded the value the bench expected?
Hint 2
Before that, compare en and en_lat in every low step. A latch that is open while the clock is low passes en straight through.
Hint 3
In a high step, a correct en_lat keeps the value en had at the end of the low step before. It ignores what en does now.
What the grader checks: The page simulates the failing run live with the shipped design. The first bad cycle and each option's first wrong value come from runs of each design on the same bench. The optional fix runs five directed and eight seeded scripts of twelve half-period steps. A model of the enable-mux register gives the expected values. The model has two steps per clock period: a glitch shorter than half a period is outside it.
13 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 1 steps and the final capstone