CodeRigor

LESSON

Registers and RAM

Build a decoder, a register file, a RAM from latch rows, a direct-mapped cache and a tri-state bus, and check them against a dictionary memory model.

A latch holds one bit. Put eight side by side and you have a register; put thousands side by side and a new problem appears: which one do you mean? This lesson is about picking one word out of many, about when a write is allowed to take effect, and about why a small fast copy in front of a large memory pays off. This lesson pairs with Simulating 256 Bytes of RAM by Sebastian Lague and 8-bit computer RAM intro by Ben Eater, linked under Watch & read. Both videos are about building memory from storage cells and decoding an address to choose one word, and the first also stops to talk about caches. Here you build a decoder, a register file, a RAM made of rows, a direct-mapped cache and a bus, all in plain Python, and test them against a dictionary.

Start with the picker. An address of n bits names one of 2ⁿ rows, and a decoder turns it into 2ⁿ lines of which exactly one is 1. Build it one bit at a time: each existing line splits in two, the line AND NOT bit and the line AND bit. The first bit you consume ends up most significant, so the bits 1, 1, 0 mean address 6.

Python
def decoder(bits):
    lines = [1]
    for b in bits:
        lines = [out for line in lines for out in (line & (1 - b), line & b)]
    return lines

lines = decoder([1, 1, 0])
print(len(lines), lines.index(1))
print(lines)

A RAM is a column of rows plus that decoder. To read, AND every row with its decoder line and OR the results together: the unselected rows contribute zeros, so only the chosen word comes out. To write, the decoder line is the permission for one row to take the incoming value. Here the read needs no clock, so it is asynchronous, while the write is synchronous, and your Ram follows that split.

Reading one word from an 8-word RAM
start / 4
The decoder turns three address bits into eight lines, one per row. Each row is ANDed with its line and the results are ORed, so only the selected row reaches the output.
Reading one word from an 8-word RAM
The decoder turns three address bits into eight lines, one per row. Each row is ANDed with its line and the results are ORed, so only the selected row reaches the output.

The register file is the same idea with a different promise. It has two read ports and one write port, and the write happens at the clock edge. During a cycle, write only presents a request; clock stores it. Until the edge, every read returns what the previous edge left behind. A real circuit behaves this way because the stored value only changes when the clock says so. It also makes things like a counter, which reads a register and writes back the new value, behave well.

Python
stored = {1: 5}
pending = (1, 9)                   # a write presented this cycle
print(stored[1])                   # a read in the same cycle
stored[pending[0]] = pending[1]    # the clock edge
print(stored[1])

Many devices can share one set of wires if only one of them drives it at a time. bus takes (enable, value) pairs and returns the one enabled value. Two enabled drivers fight, so you raise ValueError. No enabled driver leaves the wires floating, and that is None, which is not the same thing as a driver putting 0 on the bus.

A cache keeps copies of recently used words in a small table. In a direct-mapped cache an address has exactly one possible line, address % lines, and many addresses share it, so each line stores a tag, address // lines, to say whose copy it holds. A read hits only when the line is valid and the tag matches. Take 4 lines and read addresses 1, 5, 1, 2, 2. Address 1 misses and fills line 1. Address 5 also lives on line 1 with a different tag, so it misses and evicts. Address 1 then misses again, 2 misses, and the second 2 hits: one hit and four misses.

Four cache lines, five reads
start / 5
Each cell shows tag:word for a line. Addresses 1 and 5 take turns on line 1 and keep evicting each other.
Four cache lines, five reads
Each cell shows tag:word for a line. Addresses 1 and 5 take turns on line 1 and keep evicting each other.

Writes use write-through: the word goes to the RAM first, and the cache line changes only if it already holds that address, so the cache never disagrees with the RAM.

Three mistakes account for most failures. Leaving the tag compare out makes any valid line count as a hit, so address 5 quietly returns the word of address 1. Letting a write change storage immediately makes a read in the same cycle see the new value. And a decoder that consumes the bits in the wrong order selects row 3 when you meant row 6, while a palindromic address like 101 hides it.

The tests compare every component with a dictionary over a few hundred seeded random operations, and require the cache to return exactly what the RAM holds. That shows the logic is consistent. It does not show timing: Python has no gate delays, so nothing here proves a real circuit would settle before the next edge. The rows are plain integers as well, so only the decoder is built from gate-level operations.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Builds memory in a logic simulator from a grid of latches, with a decoder choosing the cell, then moves to larger RAM, asynchronous and synchronous versions, and a short note on caches. Watch it first, then write the same pieces in code.

Reviews registers, then introduces RAM: how the memory is structured, types of RAM, static RAM, and address decoding logic. A second, hardware-flavoured view of the same address-to-row selection you build here.