G5.4.2 Debounce a Switch with an SR Latch

EasyImplementIcarus · browser samples / server checksReview the topic

Turn the two bouncing contact lines of a push button into one clean edge per press with a latch of NOR gates.

Hardware context: An alarm panel that remembers a door opening, and a lift button whose two contacts bounce. Debounce a two-contact button with a latch of NOR gates on the inverted contact lines.

A lift button counts presses. Its blade rests on contact A and moves to contact B when pressed. Each contact bounces a few times before it settles, and the counter saw three presses for every push.

This is an Implement problem. Build debounce from the read-only library, which has only inverters and NOR gates. Each contact pulls its line to 0 while the blade touches it; between contacts both lines are 1. The blade never touches both.

clean must be 1 from the first touch of B until the first touch of A. It may rise once and fall once per press. Use only cell instances, from 2 to 6 of them.

Signals in the Timeline

SignalWidthRole
script24input of each check
Full specification
The module (keep the name and the ports)
module debounce (
  input  logic a_n,     // 0 while the blade touches contact A (released)
  input  logic b_n,     // 0 while the blade touches contact B (pressed)
  output wire  clean    // 1 while the button is pressed
);
  // your netlist
endmodule
cells.sv: the cell library (read-only). Ports are (y, inputs...): the output comes first
// cells.sv: the unit-delay cell library (read-only).
// Every cell takes 1 time unit, whatever its fan-in. Ports are (y, inputs...): output first.
// Each cell adds 1 to the bench's cell counter when the simulation starts.
module INV   (output wire y, input wire a);                not #1 (y, a);             initial tb.cells++; endmodule
module NOR2  (output wire y, input wire a, b);             nor #1 (y, a, b);          initial tb.cells++; endmodule

Bladea_nb_nclean
touches A (released)010
touches B (pressed)101
touches neither (moving or bouncing)11unchanged

Each case starts at rest and plays twelve contact states, each held 4 time units. The result shows clean after each state and how many times clean changed. The last field says whether the cell count is out of range.

Examples

Example 1

A Clean Press and Release
Input
contacts: A--BBBB--AAA (A = touching A, B = touching B, - = touching neither)
Output
clean_state0 = no, clean_state1 = no, clean_state2 = no, clean_state3 = yes, clean_state4 = yes, clean_state5 = yes, clean_state6 = yes, clean_state7 = yes, clean_state8 = yes, clean_state9 = no, clean_state10 = no, clean_state11 = no, clean_changes = 2, cell_count_out_of_range = no

The blade leaves A, reaches B, stays, then goes back to A. clean rises at the first B and falls at the first A: two changes.

Example 2

A Press with Two Bounces
Input
contacts: A--B-B-BBBBB (A = touching A, B = touching B, - = touching neither)
Output
clean_state0 = no, clean_state1 = no, clean_state2 = no, clean_state3 = yes, clean_state4 = yes, clean_state5 = yes, clean_state6 = yes, clean_state7 = yes, clean_state8 = yes, clean_state9 = yes, clean_state10 = yes, clean_state11 = yes, clean_changes = 1, cell_count_out_of_range = no

The blade touches B, lifts off twice, and settles. clean must rise once and stay 1 through the bounces.

Example 3

A Release with Bounces on A
Input
contacts: BB--A-A-AAAA (A = touching A, B = touching B, - = touching neither)
Output
clean_state0 = yes, clean_state1 = yes, clean_state2 = yes, clean_state3 = yes, clean_state4 = no, clean_state5 = no, clean_state6 = no, clean_state7 = no, clean_state8 = no, clean_state9 = no, clean_state10 = no, clean_state11 = no, clean_changes = 2, cell_count_out_of_range = no

From pressed, the blade returns to A and bounces there. clean falls once and stays 0.

Constraints and Contract

  • The blade never touches A and B at the same time: a_n = b_n = 0 never occurs.
  • Keep the module name and the ports. From 2 to 6 cells.

Hints

Hint 1

While the blade bounces, both lines are 1. The output must hold its value then, so the circuit needs memory: a latch.

Hint 2

A NOR latch acts on 1s, but the contact lines are active-low. Invert them first.

Hint 3

The inverted B line sets: it goes into the NOR gate that drives clean_n. The inverted A line clears: it goes into the gate that drives clean.

What the grader checks: Six directed and ten seeded contact sequences of twelve states, each held 4 time units. clean is compared after every state with a JavaScript model of the latch, and its number of changes must match. The cell count must be from 2 to 6. Contact bounce is modelled as a clean 0/1 change with no analogue level.

16 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 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

A Clean Press and Release

Input
contacts: A--BBBB--AAA (A = touching A, B = touching B, - = touching neither)
Expected output
clean_state0 = no, clean_state1 = no, clean_state2 = no, clean_state3 = yes, clean_state4 = yes, clean_state5 = yes, clean_state6 = yes, clean_state7 = yes, clean_state8 = yes, clean_state9 = no, clean_state10 = no, clean_state11 = no, clean_changes = 2, cell_count_out_of_range = no

The blade leaves A, reaches B, stays, then goes back to A. clean rises at the first B and falls at the first A: two changes.

Run samples executes all 3 public checks. Check all tests runs 16 checks on the server.

Icarus · browser samples / server checks