Addresses and Memory-Mapped I/O
A processor can only load and store. Give a lamp or a switch an address, and loading and storing become seeing and doing.
You Will Be Able To
- Explain memory-mapped I/O and why chips use it.
- Say what the address decoder and the read multiplexer each do.
- Find the owner and the offset of an address in a real memory map.
- Lesson
- S0.2
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
The machine in S0.1 added 5 and 7, and nobody ever knew. Its answer stayed in box 12. To light a lamp or read a switch, the processor must reach circuits that are not memory. It manages that without learning a single new instruction.
Before You Start
You need S0.1: memory boxes, addresses, LOAD and STORE. You also need the multiplexer, and a circuit that checks whether two numbers are equal.
| Question | |
|---|---|
| 1 | What is memory-mapped I/O, in one sentence? |
| 2 | What does the address decoder do, and what does the read multiplexer do? |
| 3 | Why can two reads of the same address return two different values? |
| 4 | What is a memory map, and which two teams rely on it? |
| 5 | In a real map, how do you find the owner of an address and the register inside that owner? |
The Idea: Give the Device an Address
The processor has exactly two ways to touch anything. It can LOAD from an address, and it can STORE to an address. So the trick is simple: give each device, such as a lamp or a switch, an address of its own.
A small circuit, the address decoder, watches the address of every load and store. For a memory address it picks memory. For the lamp address it picks the lamps. The program lights a lamp with an ordinary STORE, and reads a switch with an ordinary LOAD. This is memory-mapped I/O: devices appear as addresses.
Why do it this way? Because adding a device then changes nothing in the processor. There is no new instruction and no new wire into the processor. Software, meaning the programs, reaches every device the same way it reaches memory.
It also gives hardware, meaning the circuits, and software one thing to agree on: the list of who owns which addresses. That list is the memory map. Hardware builds the decoder from it. Software takes its addresses from it.
New Words
| Word | Meaning |
|---|---|
| Device | A circuit beside the processor that touches the world: lamps, switches, a keyboard. Also called a peripheral. |
| Hardware and software | Hardware is the circuits. Software is the programs: numbers in memory. Usually one team builds each. |
| Access | One load or one store: one use of an address. |
| Device register | A register inside a device that has an address. A write tells the device something. A read reports something. |
| Block | One part of a chip with one job: the processor, the memory, or one device. |
| Owner | The one block that answers for an address: memory, or one device. |
| Select | A one-bit wire to one owner. High means "this access is for you". |
| Address decoder | The circuit that reads an address and raises the select of its owner. |
| Read multiplexer | The circuit that passes the selected owner's value back to the processor, and nobody else's. |
| Memory map | The table of owners: which addresses belong to whom. |
| Memory-mapped I/O | Reaching devices by giving them addresses in the memory map. |
One Picture: One Owner Becomes Three
- Processor
- Bus
- Memory
- Peripheral
- Start with S0.1
One processor, one memory. Every LOAD and STORE goes to a memory box.
- Add two devices
A row of lamps and a row of switches join the same wires. Each has one register. Nothing in the processor changes.
- The decoder picks one owner
The decoder reads the address of every access. It raises one select wire: memory for 0 to 13, lamps for 14, switches for 15.
- STORE 14 lights the lamps
The address is 14, so only the lamp select is high. The lamps take the value. Memory ignores it.
Follow the calls, values and waits
- data 00000011: Processor → Lamps
- LOAD 15 reads the switches
The address is 15, so only the switch select is high. The multiplexer passes the switch value back to the processor.
Follow the calls, values and waits
- data 00000011: Read multiplexer → Processor
Read the diagram as text
- Processor: knows only LOAD and STORE
- Address decoder: raises one select wire
- Read multiplexer: passes one owner's value back
- Memory: addresses 0 to 13
- Lamps: address 14
- Switches: address 15
- Processor → Address decoder: address
- Address decoder → Memory: select
- Address decoder → Lamps: select
- Address decoder → Switches: select
- Processor → Lamps: value to write
- Memory → Read multiplexer: value
- Lamps → Read multiplexer: value
- Switches → Read multiplexer: value
- Read multiplexer → Processor: value read
The value to write also reaches memory. That wire is left out to keep the picture readable.
The processor column never changed between step 1 and step 5. That is the whole point. All the new knowledge sits in the decoder, the multiplexer and the map.
A fetch is an access too. The PC is the address, the decoder selects memory, and the multiplexer returns the instruction. The picture and the tables below show only the access that each instruction then makes.
The Smallest Example, Worked by Hand
Take the S0.1 machine and change the meaning of its last two addresses. This is its memory map.
| Addresses | Owner | A read returns | A write does |
|---|---|---|---|
| 0 to 13 | Memory | the number last stored there | stores the number |
| 14 | Lamps | which lamps are lit now | sets eight lamps, one bit each |
| 15 | Switches | where the eight switches are now, one bit each | nothing |
The program copies the switches to the lamps, twice. It uses the instructions you already know. Each bit of a value is one switch or one lamp. Bit 0, the rightmost bit, is switch 0 or lamp 0.
| Round | Instruction | Address | Select raised | What happens |
|---|---|---|---|---|
| 1 | LOAD 15 | 15 | switches | A = 00000011: switches 0 and 1 are on |
| 2 | STORE 14 | 14 | lamps | lamps 0 and 1 light |
| someone turns switch 1 off and switch 2 on | ||||
| 3 | LOAD 15 | 15 | switches | A = 00000101 |
| 4 | STORE 14 | 14 | lamps | lamps 0 and 2 light |
Rounds 1 and 3 are the same instruction, and they return different numbers. Memory never does that. A memory box returns what was last stored. A device register reports the world, and the world changed between the two reads.
The Rules
Rule 1: A device is reached through addresses.
Software uses the ordinary load and store instructions. Nothing else is needed.
Rule 2: Every address has at most one owner.
The decoder raises at most one select for any access. In the small machine every address has an owner. Real maps also have empty addresses.
Rule 3: A read returns the selected block's value, and only that.
The read multiplexer follows the select wires.
Rule 4: A device register is not memory.
A read may return a value nobody stored. A write may cause an action. A write may be ignored.
Rule 5: The memory map is the agreement.
Hardware builds the decoder from it. Software takes its addresses from it. If they disagree, nothing works.
Sources for These Rules
The decoder and the read multiplexer: the textbook "Digital Design and Computer Architecture" by Harris and Harris, section 9.2.
The lamp and switch machine is our own teaching example. The real map further down this page belongs to the chip this track studies.
Now Run It
The lab at the end of this page is the S0.1 machine with the decoder, the multiplexer and the two devices added. Each printed row names the select that went high.
pc=0 LOAD 15 select=switches A=00000011
pc=1 STORE 14 select=lamps lamps=00000011
(someone flips two switches)
pc=2 LOAD 15 select=switches A=00000101
pc=3 STORE 14 select=lamps lamps=00000101
pc=4 HALTRule 2 at work: every row shows exactly one select. Rule 4 at work: the two LOAD 15 rows return 00000011 and then 00000101, because the switches moved in between.
The Same Idea at Real Size
A real chip has billions of addresses, so engineers write them in hexadecimal. It counts in sixteens and is marked by the prefix 0x. One hex digit stands for four bits. The sixteen digits are 0 to 9, then A to F for ten to fifteen. The underscore is only a spacer, like the comma in 1,000.
The chip this track studies is called ap_soc. Its addresses are 32 bits wide, which is eight hex digits. Each address names one byte, which is eight bits. Four names from its map need a line each.
| Name | Stands for | What it is |
|---|---|---|
| Boot ROM | read-only memory used at start-up | Memory whose content is fixed when the chip is made. It holds the program the chip runs first. It sits at address 0 because the PC of this chip starts at 0. |
| SRAM | static random-access memory | Ordinary memory that programs read and write. The long name describes how its cells are built, which does not matter here. |
| UART0 | a standard name for a text port; the 0 means the first one | A device that sends and receives letters and digits over a pair of wires. Lesson S7.3 explains the name and the device. |
| GPIO | general-purpose input and output | A device that drives and reads the pins of the chip: the metal contacts that join it to a board. It is the real version of our lamps and switches. |
| First address (base) | Size | Owner |
|---|---|---|
| 0x0000_0000 | 0x1000 (4,096 bytes) | Boot ROM |
| 0x1000_0000 | 0x1000 (4,096 bytes) | UART0 |
| 0x1000_1000 | 0x1000 (4,096 bytes) | GPIO |
| 0x8000_0000 | 0x1_0000 (65,536 bytes) | SRAM |
Four of the eleven entries in the map of ap_soc v1.0. Lesson S4.1 studies the whole map.
Each entry is a run of addresses called a region. The region starts at its base and holds as many addresses as its size. Now find the owner of address 0x1000_1004 by hand, in two steps.
Find the region that contains it
GPIO starts at 0x1000_1000 and holds 0x1000 addresses. Its last address is 0x1000_1FFF. Our address lies inside, so GPIO is the owner.
Subtract the base
0x1000_1004 minus 0x1000_1000 leaves 0x004. That remainder is the offset. It picks one register inside GPIO.
Why offset 0x004 and not 0x001? In ap_soc one load or store moves 32 bits at once. That is four bytes, starting at an address that is a multiple of four. A register is also 32 bits wide, so one access reads or writes a whole register. The bottom two address bits would only pick a byte inside the register, so they stay at 00. Registers therefore sit at offsets 0x000, 0x004, 0x008 and so on. In GPIO, the register at offset 0x004 reports the pins.
The hardware does the same two steps with wires. A region of 0x1000 addresses uses the bottom three hex digits as the offset. The decoder compares the digits that are left, the top five: 0x10001. GPIO then uses the bottom three to find the register. A larger region leaves fewer digits to compare.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys |
|---|---|---|
| Give each device addresses | A special instruction and special wires per device | A new device needs no change in the processor. |
| One decoder decides ownership | Every device checks the whole address itself | One place to check that no two owners overlap. |
| A multiplexer returns the read value | Every owner drives one shared wire | Two gate outputs tied to one wire could force it to different values at once. Old circuit boards did share wires, with care. Inside a chip, a multiplexer is the rule. |
At Scale
This section looks ahead. The sixteen-address map and a real one differ in size, and in the trouble that size brings.
The map becomes a file
Nobody types a map with hundreds of regions twice. Teams keep the map in one file, and build both the decoder and the software address list from that file. Then the two cannot disagree.
Addresses nobody owns
Most of a 32-bit map is empty. An address with no owner is called a hole. Lesson S4.12 shows how a chip answers an access to one.
Decoding in stages
A large chip decodes in steps: first a group of devices, then one device inside the group. Lesson S1.2 follows one access through two stages.
Try It
Run the lab. Match each row to a round of the hand table.
Change the first instruction to LOAD 14. Predict which select goes high, and what A receives.
Add a third device at address 13: a second row of lamps. Memory then ends at address 12. You must change the decoder, the multiplexer, the STORE case and the printed owner name.
pc=0 LOAD 15 select=switches A=00000011 pc=1 STORE 14 select=lamps lamps=00000011 (someone flips two switches) pc=2 LOAD 15 select=switches A=00000101 pc=3 STORE 14 select=lamps lamps=00000101 pc=4 HALTCheck Yourself
Common Mistakes
Wrong Expect a device register to read back what you wrote.
A device register reports the device. It may ignore a write completely.
Fix Read what the map says a read returns and what a write does.
Wrong Think the processor knows it is talking to a lamp.
The processor issues a STORE to address 14, like any other STORE.
Fix Look to the decoder and the map for the meaning of an address.
Wrong Let two owners answer one address.
A read would then have two values and only one wire to carry them.
Fix Check that no two regions in the map overlap.
Remember
- Memory-mapped I/O gives each device addresses, so ordinary loads and stores reach it.
- The address decoder raises one select; the read multiplexer returns only the selected owner's value.
- A device register is not memory: reads report the device, and writes act or are ignored.
- The memory map lists who owns which addresses; hardware and software both build from it.
- In a real map, the region gives the owner, and address minus base gives the offset inside it.