Bring an asynchronous input into a clock domain with two flip-flops, then flag its rising edges, with no x reaching the logic.
Hardware context: A ready line from a sensor in another clock domain that must be read safely by a state machine. Build a two-flop synchronizer and a rising-edge detector from flip-flop cells with a metastability model.
A sensor in another clock domain raises and lowers a ready line at any time. A plain flip-flop that samples it can go metastable. The block needs a clean copy of the line, and a one-period pulse each time the line rises.
This is an Implement problem. Build sync2 from the read-only cells. MDFF is a flip-flop with a bench model of metastability. If its input changes just before the edge, its output is x for up to 7 units. Then it settles to 0 or 1.
The check reads q and rise after every rising edge. Neither may be x. q must not move at the first edge after a change and must show the new value by the third. rise must be 1 exactly when q has just risen. Use 3 to 7 cells.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
script | 48 | input of each check |
Full specification
module sync2 (
input logic clk,
input logic d_async, // from another clock domain: changes at any time
output wire q, // d_async, safe to use in the clk domain
output wire rise // 1 for one clock period after q rises
);
// your netlist
endmodule
// cells.sv: the cell library (read-only). Ports are (output, inputs...).
// MDFF is a bench model of a metastable flip-flop, not a circuit: clock-to-Q 1. If d changed less than 2 units
// before the rising edge, q goes x and settles 1 to 7 units later to 0 or 1 (the bench picks which).
module MDFF (output reg q, input wire d, input wire clk);
time t_d = -100;
initial tb.cells++;
always @(d) t_d = $time;
always @(posedge clk)
if (d !== 1'b0 && d !== 1'b1) q <= #1 1'bx;
else if ($time - t_d < 2) begin
int r; bit pick;
r = tb.settle(); pick = tb.coin();
q <= #1 1'bx;
q <= #(1 + r) (pick ? d : ~d);
end
else q <= #1 d;
endmodule
module INV (output wire y, input wire a); not #1 (y, a); initial tb.cells++; endmodule
module AND2 (output wire y, input wire a, b); and #1 (y, a, b); initial tb.cells++; endmodule
| Rule | Detail |
|---|---|
| Clock | Period 10; the clock rises 5 units after it falls. |
| x | q is read 4 units after each rising edge and must be 0 or 1 every time. |
| Not early | At the first rising edge after d_async changes, q still shows the old value. |
| Not late | By the third rising edge after the change, q shows the new value. |
| rise | At each read, rise = q AND NOT (q at the read before). |
| Cells | From 3 to 7 cell instances. |
Each case makes six changes of d_async, each 1 to 5 units before a rising edge, and runs five periods after each. The result counts the x reads, the early moves, the late arrivals and the wrong rise values; all must be 0.
Examples
Example 1
Changes Far from the Edges- Input
input changes 5, 4, 3, 5, 4, 3 units before a rising edge; coin seed 11
- Output
x_reads = 0, early_moves = 0, late_arrivals = 0, wrong_rise_reads = 0, cell_count_out_of_range = no
No change comes within 2 units of an edge, so no flop goes metastable. q follows d_async two edges later.
Example 2
Changes 1 Unit Before the Edges- Input
input changes 1, 1, 1, 1, 1, 1 units before a rising edge; coin seed 5a
- Output
x_reads = 0, early_moves = 0, late_arrivals = 0, wrong_rise_reads = 0, cell_count_out_of_range = no
Every change comes 1 unit before an edge, so the first flop goes x every time. q must still never be x.
Example 3
Changes 1 and 2 Units Before the Edges- Input
input changes 2, 1, 2, 1, 2, 1 units before a rising edge; coin seed c3
- Output
x_reads = 0, early_moves = 0, late_arrivals = 0, wrong_rise_reads = 0, cell_count_out_of_range = no
Mixed: some changes hit the window and some just miss it. Either way q arrives by the third edge.
Constraints and Contract
- Keep the module name and the ports.
- From 3 to 7 cells; MDFF is a model of a flop, not a circuit.
Hints
Hint 1
One MDFF on d_async can go x right after an edge. Nothing in the clk domain may read that output directly.
Hint 2
A second MDFF, on the same rising edge, samples the first one a whole period later. The model lets the first settle within 8 units, so the second sees a clean value.
Hint 3
Keep the path between the two flops empty. Build the edge detector only from the second flop's output: a third flop remembers it for one period.
What the grader checks: Four directed and ten seeded cases of six input changes, at chosen points of the clock period. MDFF is a model, not a circuit. It turns a change inside the window into an x of 1 to 7 units and a random settled value. The check counts x reads, early moves, late arrivals and wrong rise values.
14 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