Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Address decoding and memory-mapped I/O

How address decoding maps CPU addresses to memory and devices, and how to design logic that generates chip-select (CS) for memory-mapped I/O.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Memory and I/O devices share the CPU's address space. An engineer uses address decoding to make sure each memory chip and each peripheral responds only to the addresses assigned to it. Correct decoding prevents bus conflicts, avoids "aliasing" (unintended multiple responses), and keeps the system stable and fast.

Key ideas

  • Addressable location — a location that can be selected by one unique address. If each location stores one byte, the CPU can address 2^n bytes with n address lines. If the CPU is word-addressable or uses byte-enable signals on a wider data bus, A0 may select a byte lane rather than a new address, so adjust offset-bit counting accordingly.

  • Memory map — a table or diagram that shows which address ranges are assigned to RAM, ROM, peripherals, reserved areas, etc.

  • Memory-mapped I/O — a scheme where peripheral registers appear at addresses in the same address space used for memory. The CPU reads/writes device registers with the same load/store instructions used for memory.

  • Port-mapped I/O — (contrast) uses a separate I/O address space and special instructions. Not the focus here; mentioned so you do not confuse the two.

  • Decoder (address decoder) — combinational logic that asserts a device's chip-select (CS) signal when the address on the bus matches the device's assigned address range. A correct decoder guarantees at most one device active for any valid address (unless intentionally shared); unmapped addresses should select no device.

  • Full decoding — the decoder checks all relevant address lines so only addresses in the intended range enable the device. This prevents aliases.

  • Partial decoding — the decoder ignores some high-order or low-order bits. It is cheaper but causes address aliasing: multiple different addresses map to the same device.

  • Exact statement (number of addressable locations): For n address lines the number of distinct addresses N is N = 2^n. Assumption: each address line is binary and independent.

  • Exact statement (block selection): If a device occupies a block of size S bytes, where S = 2^k, then the k least-significant address bits are the byte offset within the block. The device select depends only on the remaining (n − k) high-order bits. This holds only if the block's base address is aligned to S (its lowest k address bits are 0).

    Derivation (commonly asked): Given n address lines and a block size S = 2^k, total addresses 2^n form 2^(n−k) blocks. Each block is identified by a unique value of the top (n − k) bits. Therefore device-select logic is an equality check of those (n − k) bits with the block's base-address high bits.

  • How to draw the common diagram (draw this on exam paper):

    • Draw a rectangle labelled CPU on the left. Draw two buses to the right from CPU: an address bus (label: A(n−1)..A0) and a data bus (label: D(m−1)..D0, where m is data width in bits).
    • On the address bus draw lines A(n−1) down to A0. Show a small combinational block labelled Decoder. From the decoder draw CS lines to each memory/peripheral rectangle (label devices: ROM, RAM, UART, Timer).
    • Also draw control signal lines from CPU: RD (or /RD), WR (or /WR). Show these feeding each device as well as the decoder output CS.
    • Label device rectangles with their address ranges (hex) and their size in bytes.
  • Control signals: commonly used names are /CS or CS (chip-select), /RD (read strobe), /WR (write strobe). Whether signals are active-high or active-low must be specified; exams commonly ask to indicate active polarity.

Formulas

N = 2^n — N is the number of distinct addresses; n is number of address lines; applies to binary address buses. Units: addresses (count).

S = 2^k — S is block size in bytes when block is power-of-two; k is number of offset bits. Units: bytes.

#blocks = 2^(n-k) — number of distinct blocks of size S in the full address space. Units: count.

CS = (A(n−1)==B(n−1)) AND (A(n−2)==B(n−2)) AND ... — informal equality-check expression: device CS is true when the high (n−k) address bits equal the device's assigned high bits. Applies when the block size is a power of two and aligned to that size; for non–power-of-two or unaligned ranges, use comparators or range checks rather than simple equality on high bits.

(When bits are compared using gates, equality of a bit equals Ai for target 1, and NOT Ai for target 0; combine with AND.)

Worked examples

  1. 16-bit CPU, 8 KB ROM placed at 0x8000. Show CS expression.

    • Given: n = 16 -> addresses 0x0000..0xFFFF. Block size S = 8 KB = 8192 = 2^13, so k = 13.
    • High bits count = n − k = 16 − 13 = 3. The device is selected when A15..A13 equal the binary of 0x8000 >> 13.
    • 0x8000 in binary (top 4 bits) = 1000; top 3 bits A15..A13 = 100.
    • Therefore CS = A15 · (not A14) · (not A13). (Assume active-high CS.)
    • Check range: any address with bits A15..A13 = 100 corresponds to addresses 0x8000..0x9FFF (8 KB). This is correct because 0x8000 .. 0x9FFF covers two top nibbles 8 and 9 but same top 3 bits.
    • Final: CS = A15 & ~A14 & ~A13.
  2. Memory-mapped UART at 0xFF00 occupying 256 bytes (one 8-bit register block). Show chip-select and read/write sequence.

    • Given: n = 16, S = 256 = 2^8 -> k = 8, high bits count = 8.
    • Device selected when A15..A8 = 0xFF (11111111).
    • CS = A15 & A14 & A13 & A12 & A11 & A10 & A9 & A8.
    • Read action (CPU): Put address 0xFF02 on address bus, assert /RD (active-low), decoder asserts CS, UART places data on D7..D0. CPU samples data when /RD is valid.
    • Write action: Put address, put data on D bus, assert /WR, decoder asserts CS, UART latches data on /WR edge.
    • Note: If UART registers are fewer than 256 bytes, full decoding (above) prevents aliasing. For this 256-byte region, checking only A15..A10 would cause mirrors every 256 bytes (0xFC00, 0xFD00, 0xFE00, 0xFF00) — a bug. For a 64-byte device decoded only with A15..A10, the aliasing period would be 64 bytes.
  3. Minimal decoder size: if you need to select up to 8 devices using top 3 bits, you can use a 3-to-8 decoder. Show how devices aligned on 2^k boundaries map to decoder outputs.

Common mistakes

  • Partial decoding without noting aliases: students forget to mention that other addresses will also select the device unless all relevant bits are checked.
  • Mixing endianness with address decoding — endianness affects byte ordering inside words, not which addresses map to which device.
  • Forgetting to show offset bits counted out of the address when deriving CS.
  • Not specifying active-high or active-low for CS, RD, WR signals.

For your exam

Typical question types: definition, short explanation of full vs partial decoding, derive CS logic for a given address and size, draw the address-bus/decoder/memory diagram, numerical where you compute decoder gates required, and a small design problem: place several memories/peripherals and show mapping.

What a full-marks answer includes:

  • Clear definitions of address space, decoder, chip-select, memory-mapped I/O.
  • Correct counting of address bits and derivation of k and n−k.
  • Correct CS boolean expression with stated active polarity.
  • A labelled diagram showing address bus, decoder, data bus, control signals, and device address ranges.
  • Mention of aliasing risk if partial decoding used and how to avoid it.

Quick check

  1. For a 20-bit address bus, how many distinct byte addresses are there?
  2. A device occupies 4 KB in a 16-bit address space. How many high-order bits must a decoder check to uniquely select the device?
  3. What is the chip-select logic for a device occupying addresses 0xC000–0xC7FF in a 16-bit system?
    Answers: 1) 2^20 = 1,048,576 addresses. 2) 4 KB = 4096 = 2^12 so check n − k = 16 − 12 = 4 high-order bits. 3) 0xC000–0xC7FF is 2 KB = 2^11, so bits A15..A11 must equal 11000; CS = A15 & A14 & ~A13 & ~A12 & ~A11 (if active-high).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?