Metastability Injection for Simulation
Standard RTL simulation does not model metastability. When a signal crosses clock domains without proper synchronization, standard flip-flop models will simply latch the old or new value flawlessly, hiding critical Clock Domain Crossing (CDC) bugs that will inevitably fail in silicon. To catch these bugs during simulation, verification engineers use behavioral flip-flop models that actively monitor timing and inject unknown states (1'bx) when timing is violated, forcing downstream logic to prove it can handle the metastability.
The meta_ff module models a D flip-flop with configurable SETUP_TIME and HOLD_TIME parameters. It tracks the simulation time of d transitions and clk positive edges. If the d input changes too close to the clock edge, the output q is driven to 1'bx to simulate a metastable state.
clkis a positive-edge triggered clock.rst_nis an asynchronous, active-low reset. When asserted,qimmediately becomes0and any pending timing violations are cleared.- A setup violation occurs if
dtransitions strictly less thanSETUP_TIMEbefore aposedge clk. Upon a setup violation,qbecomes1'bxat the clock edge. - A hold violation occurs if
dtransitions strictly less thanHOLD_TIMEafter aposedge clk. Upon a hold violation,qbecomes1'bximmediately when thedtransition occurs. - If no violation occurs,
qtakes the value ofdat theposedge clk. - You may assume
dwill never transition at the exact same simulation time step as the clock edge.
| Signal | Direction | Width | Description | |--------|-----------|-------|-------------| | clk | input | 1 | Positive-edge triggered clock | | rst_n | input | 1 | Asynchronous active-low reset; clears output to 0 | | d | input | 1 | Data input | | q | output | 1 | Registered data output; becomes 1'bx on timing violation |
Constraints
- The module must be named
solution. - The module must declare two parameters:
SETUP_TIME(default 2) andHOLD_TIME(default 2). - Timing checks must use strictly less than (
<). A transition exactly at the setup or hold time boundary is safe and does not cause a violation. - Hold violations must corrupt the output to
1'bximmediately upon the illegaldtransition, not at the next clock edge. - Reset is asynchronous and must immediately override any
1'bxstate.
Topics
Solve this problem
Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against a real testbench — you get the waveform back, not a stored answer.
This problem is part of Codiode Pro. The statement above is free to read.
The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.