Registers
How a circuit remembers. Everything sequential (counters, state machines, CPUs) is registers underneath.
How a circuit remembers. Everything sequential (counters, state machines, CPUs) is registers underneath.
A combinational circuit is a pure function: outputs depend only on current inputs, change every input and the output changes immediately. There’s nowhere for state to live. The smallest piece of hardware that does have a place for state is the D flip-flop: one bit of memory.
It has one input (d), one output (q), and a clock. Whatever value is on dwhen the clock ticks is captured and held on quntil the next tick. Between ticks the input can change all it wants, q doesn’t.
Toggle d below and watch nothing happen. Press the step button to advance the clock one cycle. The output catches up.
A flip-flop captures its input on every clock edge. That’s often more than you want: a CPU register, for instance, should hold its value across many cycles and only update when an instruction tells it to. The fix is one extra input: write-enable (we). When we is high on the clock edge, the register captures the new value. When it’s low, the register holds whatever was there before.
A register is also parameterized by width. Register({ width: 1 }) is essentially a D flip-flop with write-enable. Register({ width: 8 }) holds eight bits. Register({ width: 32 }) holds a 32-bit word. Same primitive, same semantics: the bit width changes, the structure doesn’t.
Type a value into data (or click to scrub), toggle we, then step the clock. Notice that the stored value only changes when both we is high and the clock ticks.
Once you have a register, you can wire its output back to its own input, through some logic that transforms the value on the way. The simplest version: pipe the register’s q through an adder that adds 1, and feed the sum back into data. Hold we high so the new value gets captured on every tick. The result is a counter: each clock tick advances the stored value by one.
This is the basic shape of every CPU program counter, every frequency divider, every state machine’s state register: a register, plus combinational logic that decides what its next value should be, with the result looping back through itself.
Hit the play button to auto-step, or step one cycle at a time. The count display shows the register’s current value.