Codiode

Glitch Filtering on Asynchronous Inputs

MediumVerilog / SystemVerilogBuild

Switches bounce and long external wires pick up noise. Such an input needs synchronizing and then filtering, so that a short spike never reaches the logic.

The module acts as a digital low-pass filter. It samples the incoming signal on every clock cycle. The output updates to a new logic level only when the input has remained perfectly stable at that new level for four consecutive clock cycles. If the input fluctuates before reaching four stable cycles, the output holds its current state.

Timing and Reset Rules:

  • Clock edge: posedge clk
  • Reset type: Asynchronous, active-low (rst_n)
  • On reset, both the internal sampling history and the sync_out register are cleared to 0.
  • When the internal 4-bit history contains all 1s, sync_out updates to 1.
  • When the internal 4-bit history contains all 0s, sync_out updates to 0.
  • For any mixed history (e.g., 1011 or 0100), sync_out holds its previous value.

Worked Trace:
Cycle 1: rst_n=0, async_in=0 → history=0000, sync_out=0
Cycle 2: rst_n=1, async_in=1 → history=0001, sync_out=0
Cycle 3: async_in=1 → history=0011, sync_out=0
Cycle 4: async_in=0 → history=0110, sync_out=0 (glitch rejected)
Cycle 5: async_in=1 → history=1101, sync_out=0
Cycle 6: async_in=1 → history=1011, sync_out=0
Cycle 7: async_in=1 → history=0111, sync_out=0
Cycle 8: async_in=1 → history=1111, sync_out=1 (stable for 4 cycles)

Signal Direction Width Description
clk input 1 Positive-edge triggered clock
rst_n input 1 Asynchronous active-low reset
async_in input 1 Noisy asynchronous input signal
sync_out output 1 Debounced and synchronized output

Constraints

  • The sync_out signal must be a registered output driven by a flip-flop, not a combinational wire.
  • You must use a 4-bit shift register to track the history of async_in.
  • The output must not update until the shift register contains exactly four identical bits.
  • On reset, the output and all internal shift register bits must clear to 0.

Topics

Shift RegisterCDCDebounce

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's 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.

Part of Clock Domain Crossing and Synchronization

Problem 31 of 36 in the Clock Domain Crossing and Synchronization track.

Related problems

Browse all problems · Clock domain crossing (CDC) problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.