Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksToolsOpen the lab →

How a register file works: four 4-bit registers, live

A register file is the small, fast set of registers inside a CPU, with ports to write one register and read another. This live one holds four 4-bit registers: a write address WA1 WA0 and a write enable WE pick the register that loads D3 to D0 on the clock edge, and a read address RA1 RA0 picks the register whose contents appear on Q3 to Q0.

It is built from three parts you can try on their own. The write side is a 2-to-4 decoder with an enable: WE AND the decoded write address drives the load input of exactly one register, so only that register changes on the next rising edge of CLK while the other three keep their values. Each register is the 4-bit register with load, four D flip-flops that either reload their own outputs or take new data. The read side is four 4-to-1 multiplexers, one per bit, all steered by the read address. Reading needs no clock: change RA and Q changes at once. Real processors use the same structure with more and wider registers, and usually two read ports, so an instruction can read both of its operands while it writes its result; in this site's course the CPU's registers feed the ALU the same way. Here CLK is a plain switch. The circuit opens with WE = 1, so you can write register 0 straight away: set D, turn CLK on, then change RA to read the registers back. The registers start unknown until they are written, as in real hardware. Before the page is built, the 4-bit register is checked on its own (load, hold, load) and the register file through a 27-step sequence: four writes, four reads, an overwrite, and a write with WE = 0 that must change nothing.

Big circuit: pinch, scroll or tap + to zoom in, then drag to move around.

Live circuit: click the inputs to toggle them and watch the outputs update. It runs the real simulator, right here on the page.

Open this circuit in the lab →

What you need

Step by step

  1. Place inputs D3 to D0, WA1, WA0, WE, CLK, RA1, RA0 and probes Q3 to Q0.
  2. Place four 4-bit registers with load, all on CLK, all with D3 to D0 as their data inputs.
  3. Decode WA1 WA0 into four lines, AND each with WE, and wire line k to the load input of register k.
  4. For each bit i, a 4-to-1 multiplexer chooses bit i of register RA1 RA0 and drives Qi.
  5. Write a value: set WA and D, keep WE = 1, pulse CLK. Then set RA to the same address and read it back.

Ports and what they do

InputsWhenWhat happens
WA1 WA0, D3..D0, WE = 1rising edge of CLKRegister WA loads D; the other three keep their values
WE = 0rising edge of CLKNo register changes
RA1 RA0any timeQ3..Q0 show register RA at once (no clock needed)

The checked sequence

StepOperationResult
1 to 4Write R0 = 0011, R1 = 0101, R2 = 1001, R3 = 1110One register loads per clock
5 to 8Read RA = 0, 1, 2, 3Q shows 0011, 0101, 1001, 1110
9Write R2 = 0110Reading R2 shows 0110; R1 still shows 0101
10Write 1111 to R1 with WE = 0R1 still reads 0101, R3 still 1110

Reading is combinational and writing is clocked, which is why a CPU can read registers early in an instruction and write the result at the clock edge that ends it. Widen every register to 8 bits and you have the register file of an 8-bit processor; add more registers and the decoder and multiplexers grow with them.

You can also read the full theory in the lesson on the register file. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.

Build more parts from gates

half adderfull adderhalf subtractorfull subtractor2:1 multiplexer4:1 multiplexerXOR gate from NAND gates2-to-4 decoderSR latchD flip-flopJK flip-flopT flip-flop4-bit shift register4-bit ripple carry adderD latch1-to-4 demultiplexer4-bit magnitude comparator4-bit ring counter4-bit Johnson counter4-to-2 priority encoder4-bit synchronous counter3-to-8 decoder8-to-1 multiplexer4-bit carry lookahead adderhalf adder from NAND gates2-bit comparatorBCD to 7-segment decoder4-bit up/down counterdecade counter2-bit multiplier4-bit ALU4-bit array multiplierNOT, AND, OR and NOR from NANDNOT, OR, AND and XOR from NORfull adder from NAND gatesSR latch from NAND gatesparity generator and checker4-bit adder/subtractorBCD adder4-bit universal shift registermod-6 counter4-bit ripple counterbinary to Gray code converterhalf adder from NOR gatesfull adder from NOR gates4-bit register with parallel load1011 sequence detectorbinary to BCD converterD flip-flop from NAND gates1-bit ALUCMOS inverterCMOS NAND gateCMOS NOR gatePISO shift registerSR flip-flop from NAND gatesBCD to excess-3 code converterHamming (7,4) code encoder and correctortraffic light controller4-bit barrel shifterCMOS AND gateCMOS OR gateCMOS XOR gate1-bit magnitude comparatorhalf subtractor from NAND gatesfull subtractor from NAND gates8-to-3 encoderGray to binary converter4-to-16 decoderfull adder using 4:1 multiplexersedge detectorserial adder4-bit carry-select adder4-bit Kogge-Stone adder4-bit carry-skip adder4x4 Wallace tree multiplier8-bit adderfrequency divider (by 2, 4 and 3)switch debouncer (SR latch)3-bit Gray code counterMealy machine (1011 detector)master-slave JK flip-flop (NAND)half subtractor from NOR gates1-to-8 demultiplexerexcess-3 to BCD code convertervending machine FSM4-bit carry-save adder3-bit synchronous counter (JK)CMOS transmission gatetri-state buffer (CMOS)2-to-1 mux from transmission gatesmod-5 counterflip-flop conversion (JK to D)8-bit ALUdecimal to BCD encoderBCD to decimal decoder4-to-1 mux from 2-to-1 muxesXOR gate from NOR gates4-bit LFSR74LS138 decoderBCD subtractor74181 ALUCMOS AOI gate (AOI21)D latch vs D flip-flop74LS47 BCD to 7-segment decoder74HC595 shift register7400 quad NAND gate chip7402 quad NOR gate chip7404 hex inverter chip7408 quad AND gate chip7432 quad OR gate chip7486 quad XOR gate chip7474 dual D flip-flop74HC165 shift registerCD4017 decade counter74LS283 4-bit adder74LS90 decade counter74HC161 4-bit counter74HC151 8-to-1 multiplexer74HC157 quad 2-to-1 multiplexer74HC139 dual 2-to-4 decoderCD4511 BCD to 7-segment decoder7492 divide-by-12 counter7493 4-bit binary counter74HC244 octal bufferRace-around condition (JK latch)74HC373 octal latch74HC374 octal D flip-flopTurnstile state machine1-to-2 demultiplexerhalf adder using 2:1 multiplexersdigital PWM generatorCD4013 dual D flip-flop74LS76 dual JK flip-flop7410 triple 3-input NAND chip7411 triple 3-input AND chip7420 dual 4-input NAND chipCD4011 quad 2-input NAND chip

Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.

Open the register file (4 x 4-bit) in the lab →

Frequently asked

What is a register file?

The set of general-purpose registers inside a CPU, organized like a tiny, very fast memory: an address chooses which register is written on a clock edge, and other addresses choose which registers are read. This one has four 4-bit registers, one write port and one read port.

How does a register file work?

A decoder turns the write address into one load signal, gated by a write enable, so only that register captures the data on the clock edge. Multiplexers, one per bit, turn the read address into a selection of one register's outputs, with no clock needed for reading.

How many read and write ports does a register file have?

It depends on the processor. A typical RISC design reads two registers and writes one in each instruction, so it has two read ports and one write port; a read port is just another set of multiplexers. This example has one of each to keep it small.

What is the difference between a register file and RAM?

Both store words at addresses, but a register file is tiny and built from flip-flops right next to the ALU, with several ports that work in the same cycle. RAM is much larger, denser and slower, and usually has one port.