A D flip-flop stores the value of D every time the clock comes.
But to hold on to a result for a long time, we need a way to keep the value even
when the clock comes.
Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.
Set D to 1 and raise CLK from 0 to 1. Q stores 1.
Now suppose a different value arrives at D. Change D to 0, then lower and
raise CLK again: the stored 1 is overwritten with 0.
A flip-flop takes a fresh D every time the clock comes. So when D changes, the
stored value changes at the next clock too, and a value survives only one clock.
The only way to keep the 1 is to hold D at 1. Storing a value should mean taking
a new value only when we want, whatever arrives at D.
The write signal
Put a MUX in front of the flip-flop. The MUX uses a write signal
W to choose what goes into the flip-flop: when W is 1, the new value IN; when W
is 0, the currently stored Q again. While W is 0, the same value is stored again
even when the clock comes, so Q stays put.
Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.
Change IN to 1, then raise CLK to 1 and lower it back to 0. W is 0, so Q
stays 0.
Change W to 1 and raise and lower CLK again. Q becomes 1.
Set W back to 0, change IN to 0, then raise and lower CLK. The flip-flop
alone would have been overwritten with 0, but Q keeps its 1.
Registers
Wrapping this circuit into a single box (D, W, CLK → Q) gives a 1-bit
register. To store several bits together, place boxes side by side that
share W and CLK. Below is a 4-bit register; an 8-bit register would have eight.
Set a number on D0~D3, raise W to 1, and when the clock comes, all four bits are
stored at once.
Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.
Change D2 and D0 to 1 so D3~D0 reads 0101, raise W to 1, then raise and lower
CLK. Q3~Q0 becomes 0101.
Lower W to 0, change D, then raise and lower CLK. Q keeps 0101.
Wrapping up
One register holds just one value. To work with many values, we need several
registers and a way to pick one of them to read or write. The
next post shows how to choose a register by its number.
Challenge
We want to build the write signal with an AND instead of a MUX: feed CLK ∧ W into the flip-flop's CLK so that when W is 0, no edge appears even when the clock comes. What happens if W rises from 0 to 1 while CLK is already 1?