Sub topic 2
Memory-mapped and isolated I/O
Prerequisites and scope
This chapter assumes you know the basics of computer organization (CPU, registers, memory, buses), the instruction cycle, and binary and hexadecimal numbers. It also assumes you have read the previous chapter on I/O interface modules and their status, control and data registers.
The question here is how the CPU names a device register. A load or store instruction must carry an address, so the processor needs a rule that tells the hardware whether an address means "a memory cell" or "a device register". There are two classic answers: memory-mapped I/O and isolated (port-mapped) I/O. GATE questions test both the concepts and the address arithmetic.
The addressing problem
Each interface module exposes a few registers, typically a status register, a control register and one or more data registers. The CPU must be able to read and write them. The CPU has an address bus, a data bus and control lines, and the I/O registers have to be reached over these same buses.
The two designs differ in one thing: whether I/O registers share the memory address space or live in a separate one.
Memory-mapped I/O
In memory-mapped I/O, part of the memory address space is reserved for device registers. Reading or writing an address in that range reaches a device register instead of RAM.
- The CPU uses its ordinary load and store instructions. There are no special I/O instructions.
- The bus has a single read line and a single write line (for example
MEMRandMEMW). - An address decoder watches the address bus. If the address falls in a device's range, the decoder enables that device's interface. If the address falls in the RAM range, it enables the memory chip.
- The address space is shared, so every address given to a device is an address that RAM cannot use.
Worked example: carving out the I/O range
Suppose a CPU has 16 address lines, so it can address 2^16 = 65,536 bytes (64 KB). The designer reserves the top 4 KB, addresses 0xF000 to 0xFFFF, for devices.
- The reserved range holds 0x1000 = 4096 addresses.
- The usable RAM range is
0x0000to0xEFFF, which is 61,440 bytes (60 KB).
The decoder can tell the two regions apart from the top four address bits. If A15 A14 A13 A12 are all 1, the access goes to a device. Otherwise it goes to memory.
#include <stdint.h>
#include <stdio.h>
#define IO_BASE 0xF000u
#define IO_TOP 0xFFFFu
/* Returns 1 if the address is routed to a device, 0 if to RAM. */
int is_io_address(uint16_t addr) {
return (addr & 0xF000u) == 0xF000u; /* top four bits all 1 */
}
int main(void) {
uint16_t samples[] = {0x0000, 0xEFFF, 0xF000, 0xF123, 0xFFFF};
for (int i = 0; i < 5; i++) {
printf("0x%04X -> %s\n", samples[i],
is_io_address(samples[i]) ? "device register" : "RAM");
}
printf("RAM bytes lost to I/O: %u\n", IO_TOP - IO_BASE + 1u);
return 0;
}Running this prints RAM for 0x0000 and 0xEFFF, and device register for the other three addresses. It then reports 4096 bytes lost to I/O.
Advantages
- Any instruction that accesses memory can access a device. This includes arithmetic with a memory operand, bit tests, shifts and compare instructions.
- No separate I/O instruction set is needed, so the instruction set is smaller.
- Programmers can use the full range of addressing modes on device registers.
- In C, a device register can be treated as a pointer dereference, as in the polling example from the programmed I/O chapter.
Disadvantages
- Device registers consume part of the address space. On a small address space this is a real loss. On a 64-bit machine it is negligible.
- It is harder to tell, when reading a program, which instructions touch devices, because they look like ordinary memory accesses.
- The decoder must handle a larger set of addresses, so address decoding is often more complex.
- Caching a device register would be wrong, because the value can change without the CPU writing it. Such address ranges must be marked uncacheable.
Isolated I/O (port-mapped I/O)
In isolated I/O, memory and devices have separate address spaces. The CPU has dedicated instructions, such as IN and OUT, and a control signal that says whether the current access is to memory or to I/O.
- The CPU asserts either a memory control line (
MEMR/MEMW) or an I/O control line (IOR/IOW). Some designs use a single line such asM/IO'to distinguish the two. - The same address bus carries the address in both cases. The same numeric address can therefore refer to a memory byte or a device port, depending on which control line is active.
- The device decoder responds only when an I/O line is active. The memory decoder responds only when a memory line is active.
The x86 family is the standard example. It provides IN and OUT with a 16-bit port number, so there are 2^16 = 65,536 ports (64 K ports), separate from memory.
#include <stdint.h>
/* x86 port I/O using GCC inline assembly (works in privileged code). */
static inline void outb(uint16_t port, uint8_t value) {
__asm__ volatile ("outb %0, %1" : : "a"(value), "Nd"(port));
}
static inline uint8_t inb(uint16_t port) {
uint8_t value;
__asm__ volatile ("inb %1, %0" : "=a"(value) : "Nd"(port));
return value;
}C itself has no way to say "use the I/O address space". The compiler needs these assembly wrappers, which is one practical cost of isolated I/O. Ordinary user programs on a protected operating system cannot execute IN and OUT without permission.
Advantages
- All of memory remains available for programs and data.
- I/O instructions are easy to spot in a program, which helps readability and debugging.
- The I/O address decoder can be simple, since there are usually few ports and the address needed is short.
- Privilege checks are easy: the hardware can restrict
INandOUTto kernel mode.
Disadvantages
- The instruction set needs extra instructions.
- I/O instructions are usually limited. For example, on classic 8085 and 8086 designs,
INandOUTmove data only between a port and the accumulator (ALorAX). You cannot directly add a device register to memory or test a bit in place. - Fewer addressing modes are available for ports.
- The control bus needs additional signals.
Side-by-side comparison
| Feature | Memory-mapped I/O | Isolated I/O |
|---|---|---|
| Address space | Shared with memory | Separate from memory |
| Instructions | Normal load and store | Special `IN` and `OUT` |
| Control lines | One read and one write line | Separate memory and I/O read and write lines |
| Memory available | Reduced by the mapped range | Full address space |
| Addressing modes for devices | All modes available | Limited |
| Decoder | Must separate device ranges from RAM | Selected by control signal, usually simpler |
| Visibility in code | I/O hidden among memory accesses | I/O instructions stand out |
| Typical use | ARM, MIPS, RISC-V, and most modern SoCs | Classic x86, 8085 |
The distinction is about the CPU interface, not the device. The same peripheral chip can be wired either way, depending on how the system designer connects the address decoder and control lines.
How the bus sees an access
The diagram below shows how the control lines steer an access. In an isolated design, the control line chooses between memory and I/O. In a memory-mapped design, there is only the address decoder.
Address decoding for a device
Whichever style you use, each device needs an address decoder. A device with 2^k registers needs k low address lines to select a register inside the device. The remaining high address lines are decoded to produce a chip-select signal.
Worked example: decoding a block
A system has a 16-bit address bus and uses memory-mapped I/O. A device has 8 registers and must occupy 0xA000 to 0xA007.
- Eight registers need 3 address lines to choose among them,
A2 A1 A0. - The other 13 lines,
A15toA3, must match the fixed pattern for0xA000. In binary,0xA000is1010 0000 0000 0000, soA15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3=1010000000000. - The chip select is the AND of those 13 conditions, with inverters where the pattern has a 0.
If the designer used only A15 to A12 for the chip select (partial decoding), the device would also answer at 0xA008, 0xA010 and up to 0xAFFF. These copies are aliases. Partial decoding is cheaper but wastes address space, and it creates conflicts if another device is later placed in an aliased range.
def chip_select_full(addr):
# Device occupies 0xA000-0xA007: top 13 bits must be 1010000000000
return (addr >> 3) == (0xA000 >> 3)
def chip_select_partial(addr):
# Only the top 4 bits are checked
return (addr >> 12) == 0xA
for a in (0xA000, 0xA007, 0xA008, 0xA123, 0xB000):
print(hex(a), chip_select_full(a), chip_select_partial(a))The output shows that 0xA008 and 0xA123 are rejected by full decoding but accepted by partial decoding.
Common errors
- Assuming memory-mapped I/O uses
INandOUT. It does not. It uses ordinary loads and stores.INandOUTbelong to isolated I/O. - Forgetting
volatilein C. Without it the compiler may cache a status register in a CPU register and loop forever on a stale value. Device registers must be declared through avolatilepointer. - Caching device registers. A cache could return an old status value. Device ranges must be marked uncacheable.
- Counting memory capacity wrongly. With memory-mapped I/O, the address space is shared, so RAM capacity plus I/O range cannot exceed the total. With isolated I/O, the two spaces are counted independently.
- Mixing up the two address spaces. In isolated I/O, address
0x20in memory and port0x20are different locations. The control line decides which one is meant. - Confusing the addressing scheme with the transfer technique. Memory-mapped versus isolated is about how registers are named. Programmed I/O, interrupts and DMA are about how data transfer is coordinated. A system can combine any addressing scheme with any transfer technique.
GATE-style patterns
Typical questions fall into four groups.
- Capacity arithmetic. A CPU has
naddress lines and memory-mapped I/O reservesRaddresses. Usable memory is2^n - Rbytes (for byte-addressable memory). Under isolated I/O the memory is the full2^n, and the number of ports depends on the port address width. - Identification. A question describes an instruction sequence and asks which scheme is in use. If the program uses
INorOUT, or the control bus has a separateIO/M'signal, the scheme is isolated. If the program usesLDR,STRorMOVon a device address, it is memory-mapped. - Pros and cons. Statements such as "memory-mapped I/O allows all memory instructions to be used on I/O" are true. "Isolated I/O reduces memory capacity" is false.
- Decoder design. Count how many address bits are fixed and how many select inside the device.
Worked example: capacity
A processor has a 20-bit address bus and memory-mapped I/O. The I/O devices are assigned the range 0xF0000 to 0xFFFFF.
- Total address space is 2^20 = 1,048,576 bytes (1 MB).
- I/O range size is
0xFFFFF - 0xF0000 + 1 = 0x10000 = 65,536bytes (64 KB). - Memory available to programs is 1,048,576 - 65,536 = 983,040 bytes (960 KB).
If the same machine used isolated I/O with 16-bit port numbers, all 1 MB would be available for memory and there would be 65,536 separate ports.
Exercise
A CPU has a 16-bit address bus and uses memory-mapped I/O. Devices occupy addresses 0xC000 to 0xFFFF. (a) How many bytes of RAM can be addressed? (b) A device with 16 registers is placed at 0xE000 and is fully decoded. How many address lines go to its internal register select, and how many into its chip-select logic?
Show answer
(a) The I/O range is 0xFFFF - 0xC000 + 1 = 0x4000 = 16,384 bytes. Total space is 65,536 bytes, so RAM can occupy 65,536 - 16,384 = 49,152 bytes (48 KB), at addresses 0x0000 to 0xBFFF. (b) 16 registers need 4 address lines (A3 to A0) for the register select. The other 12 lines (A15 to A4) go into the chip-select logic and must match the pattern for 0xE000, which is 1110 0000 0000.
Exercise
A programmer writes a polling loop for a device. The code reads the status register through an ordinary C pointer that is not declared volatile, and the loop never exits even though the device sets the ready bit. Name the addressing style that makes this code possible and explain the cause of the failure.
Show answer
The style is memory-mapped I/O, because ordinary pointer reads and writes can reach the register. Under isolated I/O, C could not do this without IN and OUT wrappers. The failure arises because the compiler assumes memory does not change between reads, so it loads the status once into a CPU register and tests that stale copy forever. Declaring the pointer volatile forces a fresh read on every iteration.
Exercise
A system uses isolated I/O. The CPU puts 0x0040 on the address bus and asserts IOW. Which location is written, and does anything in memory at address 0x0040 change?
Show answer
Port 0x0040 is written. The memory chip responds only when MEMR or MEMW is active, so memory at address 0x0040 is unchanged. The two address spaces are separate and the control line selects between them.