Build an SR latch from NOR cells that sets, resets and holds, and resets when both inputs are 1.
Hardware context: An alarm panel that remembers a door opening, and a lift button whose two contacts bounce. Build a reset-priority SR latch from NOR and inverter cells.
An alarm panel must remember that a door was opened, even after the door closes again. The set line pulses when the door opens; the reset line pulses when the guard clears the alarm. If both come at once, clearing must win.
This is an Implement problem. Build sr_latch from cells of the read-only library. Each cell takes one time unit. The check drives six (s, r) pairs and reads q and qn after each.
Use only cell instances and wire or logic declarations without initial values. Operators, assignments and procedural code are refused before simulation. Use from 2 to 6 cells.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
script | 12 | input of each check |
Full specification
module sr_latch (
input logic s, r,
output wire q, qn
);
// your netlist
endmodule
// 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 NAND2 (output wire y, input wire a, b); nand #1 (y, a, b); initial tb.cells++; endmodule
module NOR2 (output wire y, input wire a, b); nor #1 (y, a, b); initial tb.cells++; endmodule
| s | r | q | qn |
|---|---|---|---|
| 1 | 0 | 1 | 0 |
| 0 | 1 | 0 | 1 |
| 0 | 0 | q (hold) | qn (hold) |
| 1 | 1 | 0 (reset wins) | 1 |
Each pair is held for 10 time units before q and qn are read. A script always starts with a set or a reset. It may drop s and r from 1 to 0 together: the latch must then hold its reset.
Examples
Example 1
Set, Then Hold- Input
set (s=1 r=0), hold (s=0 r=0), hold (s=0 r=0), hold (s=0 r=0), hold (s=0 r=0), hold (s=0 r=0)
- Output
q_step0 = yes, qn_step0 = no, q_step1 = yes, qn_step1 = no, q_step2 = yes, qn_step2 = no, q_step3 = yes, qn_step3 = no, q_step4 = yes, qn_step4 = no, q_step5 = yes, qn_step5 = no, cell_count_out_of_range = no
The set pulse makes q = 1 and qn = 0. With both inputs at 0 the latch keeps that value for the next five steps.
Example 2
Reset, Then Hold- Input
reset (s=0 r=1), hold (s=0 r=0), hold (s=0 r=0), hold (s=0 r=0), hold (s=0 r=0), hold (s=0 r=0)
- Output
q_step0 = no, qn_step0 = yes, q_step1 = no, qn_step1 = yes, q_step2 = no, qn_step2 = yes, q_step3 = no, qn_step3 = yes, q_step4 = no, qn_step4 = yes, q_step5 = no, qn_step5 = yes, cell_count_out_of_range = no
The reset pulse makes q = 0 and qn = 1, and the latch keeps it.
Example 3
Set, Hold, Reset, Hold- Input
set (s=1 r=0), hold (s=0 r=0), reset (s=0 r=1), hold (s=0 r=0), set (s=1 r=0), hold (s=0 r=0)
- Output
q_step0 = yes, qn_step0 = no, q_step1 = yes, qn_step1 = no, q_step2 = no, qn_step2 = yes, q_step3 = no, qn_step3 = yes, q_step4 = yes, qn_step4 = no, q_step5 = yes, qn_step5 = no, cell_count_out_of_range = no
Each pulse moves the latch to its side, and each hold keeps the last value.
Constraints and Contract
- Inputs are known 0/1 values. Keep the module name and the ports.
- From 2 to 6 cells.
Hints
Hint 1
A NOR gate gives 0 when any input is 1. So a 1 on its spare input forces its output to 0, whatever the loop holds.
Hint 2
Feed q into the gate that makes qn, and qn into the gate that makes q. With both spare inputs at 0, each gate is an inverter and the loop holds.
Hint 3
With s = r = 1 the plain NOR latch gives q = qn = 0 and races on release. Pass s on only while r is 0. When both drop together, make sure that gate sees s fall no later than r.
What the grader checks: Eight directed and ten seeded scripts of six (s, r) pairs, each held for 10 time units. After each pair, q and qn are compared with the latch table computed in JavaScript. Some steps drop s and r together. The cell count must be from 2 to 6.
18 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