Skip to content
Tutorials Center
My learning

Topics

Lesson 42 of 57 ยท Memory and Programmable Logic

ROM, RAM and Memory Organization

Understand how memory is built from latches and decoders: ROM as a lookup table, RAM read and write, SRAM versus DRAM, register files and a memory map, with Python models.

A flip-flop remembers one bit. A computer needs to remember billions. The trick that makes this possible is organization: instead of wiring each bit to the outside world, we store bits in a regular grid of cells and use an address to pick which group of cells to read or write. The same idea gives us the lookup tables inside an FPGA, the registers of a CPU, the cache and the gigabytes of main memory.

In this lesson you will see how a memory is organized (cells, words, address lines), how a ROM is simply a stored truth table, how RAM adds writing, why SRAM and DRAM differ, what a register file is, and how several memory chips share one address space in a memory map. All examples are Python models of the hardware.

From a cell to a memory array

A memory cell stores one bit. In a static memory this is a latch, a pair of cross-coupled gates that holds its value as long as power is on. Cells are grouped into words (for example 8 bits per word), and a word is the unit you read or write in one operation. A memory with k address lines and w bits per word holds 2^k words, so its capacity is 2^k * w bits.

To read a word, the k address bits go into a decoder. The decoder raises exactly one of its 2^k output lines, the word line, and that line connects the selected word's cells to the w data lines. Writing works the same way, except that the data lines drive the cells instead of reading them.

Anatomy of a memory

Address k bits choose one of 2^k words.
Word w bits read or written together.
Decoder Turns the address into one word line.
Cells One latch or capacitor per bit.
1 / 2

Large memories arrange the cells as a two-dimensional grid: part of the address selects a row, the rest selects a column. A 1 million-word memory then needs two decoders of about 1,000 outputs each instead of one with a million, which is the only practical way to build it.

ROM: a stored truth table

ROM (read-only memory) holds fixed contents that are written once, at manufacturing or at programming time, and survive power-off (it is non-volatile). Look at what a ROM does: the address goes in, a stored word comes out, with no clock and no memory of previous reads. That is exactly a combinational circuit. A ROM with k address bits and w data bits implements any set of w Boolean functions of k variables: you write the truth table into the cells.

Each output bit is the OR of the stored 1s of the word lines that are active. Because the decoder generates every minterm of the address variables, a ROM is a big sum-of-products circuit in disguise. The model below makes that visible and stores two functions: a full adder and a 2-bit by 2-bit multiplier.

Python A ROM is a truth table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last lines show why ROMs are not used for every circuit: a 32-bit adder as a single ROM would need 2^64 words. ROMs are great for small functions, tables (character fonts, sine tables, microcode) and boot code. Their programmable cousins appear in PLAs, FPGAs and Lookup Tables.

The ROM family tree

ROMs differ in who writes the data and whether it can be changed:

Type Programmed by Erasable? Typical use
Mask ROM (MROM) the chip factory, using a custom photo-mask no huge production runs with fixed code (old game cartridges, character fonts)
PROM the user, once, by blowing fuses with a programmer no prototypes and small runs
EPROM the user, with a programmer yes, the whole chip at once, by ultraviolet light through a quartz window development before flash existed
EEPROM the user, in the circuit yes, byte by byte, electrically settings and calibration data
Flash the user, in the circuit yes, in blocks, electrically firmware, USB sticks, SSDs, phones

Mask ROM is the cheapest per chip but needs a large order, because each design costs a new mask. The programmable types trade a higher chip price for flexibility. All of them are non-volatile (the data survives power loss), but not equally permanent: EEPROM and flash cells wear out after many thousands of erase cycles, and charge can slowly leak over years. A ROM is also the simplest programmable logic device: a fixed AND plane (the address decoder) followed by a programmable OR plane.

Advantages of ROM: the contents are safe from accidental overwriting and survive power loss, and reads are fast and simple. Disadvantages: mask ROM can never be changed; erasable types write slowly and wear out; and a ROM is wasteful for logic with many inputs, as the 32-bit adder example showed.

RAM: reading and writing

RAM (random-access memory) can be read and written, and every word takes the same time to reach, whatever its address. It is volatile: when the power goes off, the contents are lost. A RAM has the address lines and data lines of a ROM, plus a write enable (WE) input. With WE low, the addressed word appears on the output. With WE high, the word on the input is stored at the clock edge.

Python A small RAM with address, data and write enable

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The capacity table shows why 32-bit addresses were limited to 4 GiB: 32 address lines select 2^32 byte-sized words.

SRAM versus DRAM

There are two main ways to store a bit in RAM.

  • SRAM (static RAM) stores a bit in a latch of about six transistors. It keeps its value while powered and needs no refresh. It is very fast and uses more silicon per bit, so it is used for registers and CPU caches.
  • DRAM (dynamic RAM) stores a bit as charge on a tiny capacitor with one transistor. The charge leaks away in milliseconds, so every row must be read and rewritten periodically (refresh). It is much denser and cheaper per bit, but slower, so it is the choice for main memory.

SRAM

Fast Nanoseconds, no waiting.
6 transistors A latch per bit.
Used for Registers and caches.
1 / 2

Both are volatile. Non-volatile storage (flash, hard drives) keeps data without power but is far slower than either.

Register files

A CPU needs a handful of very fast storage locations, each holding one operand. A register file is a small SRAM with several ports: typically two read ports and one write port. This lets an instruction such as add r3, r1, r2 read two registers and write the result back in the same cycle. Reads are combinational (a multiplexer per port), writes happen at the clock edge. Some designs hard-wire register 0 to the constant 0, a trick that makes moves and comparisons easy.

Python A register file with two read ports and one write port

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The memory map

A processor sees one big address space, but behind it are several devices: ROM with the boot code, RAM for data, and input/output registers. Each device only has to answer to its own slice of the addresses. Extra logic, an address decoder looking at the top address bits, raises a chip select for the one device the address belongs to. The layout is called the memory map.

flowchart LR
    CPU["CPU<br/>16-bit address"] --> DEC["Address decoder<br/>top address bits"]
    DEC -->|"CS_ROM"| ROM["ROM<br/>0000-3FFF"]
    DEC -->|"CS_RAM"| RAM["RAM<br/>4000-7FFF"]
    DEC -->|"CS_IO"| IO["I/O<br/>8000-80FF"]

One address, one chip select. The address bits not used by the decoder select the word inside the chosen chip.

Python A memory map with three devices

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real systems must be careful with the gaps: an address with no device behind it either returns whatever floats on the bus, or, in better designs, raises an error. Overlapping regions are worse, because two chips would drive the data lines at once, so a good address decoder guarantees that at most one chip select is active.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A memory has 12 address lines and 8 data lines. How many bits does it store?

    12 address lines select 2^12 = 4,096 words of 8 bits each, so 4,096 x 8 = 32,768 bits (4 KiB).
  2. 2. Why can a ROM implement any combinational function of its address bits?

    The address decoder selects one word line per input combination, and the word stored there is the function's output for that combination. That is a truth table in silicon.
  3. 3. What is the main difference between SRAM and DRAM?

    An SRAM cell is a latch and holds its data while powered. A DRAM cell is a capacitor that leaks, so it needs periodic refresh, but it is smaller and cheaper per bit.
  4. 4. What does a register file with two read ports and one write port let a CPU do in one cycle?

    Two read ports supply both operands of an instruction like add, and the write port stores the result back in the same cycle.
  5. 5. A 16-bit address uses its top two bits to pick one of four 16 KiB blocks. How many address bits go to the chip inside a block?

    16 - 2 = 14 bits remain, and 2^14 = 16,384 bytes = 16 KiB.

Summary

  • A memory is an array of cells grouped into words; an address of k bits selects one of 2^k words through a decoder.
  • A ROM is a stored truth table: it implements any function of its address bits, but grows as 2^k.
  • RAM adds a write enable and loses its contents when power is removed; SRAM uses latches (fast, cache and registers), DRAM uses refreshed capacitors (dense, main memory).
  • A register file is a small multi-port SRAM; two read ports and one write port serve most instructions.
  • Chip selects generated by an address decoder map several devices into a single address space.

Next: PLAs, FPGAs and Lookup Tables.