Lesson 14 of 15
SR Latch
SR Latch
A latch is the simplest sequential (memory) element. Unlike combinational circuits, its output depends on both current inputs and previous state.
The SR latch has two inputs:
- S (Set): forces output Q to
1 - R (Reset): forces output Q to
0
| S | R | Q (next) | Notes |
|---|---|---|---|
| 0 | 0 | Q (hold) | no change |
| 1 | 0 | 1 | set |
| 0 | 1 | 0 | reset |
| 1 | 1 | ? | invalid (forbidden) |
The SR latch holds its state when both inputs are 0. This is how single-bit memory works in digital circuits.
A function model takes the current state q and returns the next state:
function srLatch(s, r, q) {
if (s === 1 && r === 0) return 1; // set
if (s === 0 && r === 1) return 0; // reset
if (s === 0 && r === 0) return q; // hold
return -1; // invalid
}
Your Task
Implement srLatch(s, r, q) that returns the next Q state (or -1 for invalid S=R=1).
Then simulate a sequence: start Q=0, apply Set, hold, hold, Reset, hold.
JavaScript loading...
Loading...
Click "Run" to execute your code.