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.

Five questions this page answers
Question
1What is memory-mapped I/O, in one sentence?
2What does the address decoder do, and what does the read multiplexer do?
3Why can two reads of the same address return two different values?
4What is a memory map, and which two teams rely on it?
5In 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

New words on this page
WordMeaning
DeviceA circuit beside the processor that touches the world: lamps, switches, a keyboard. Also called a peripheral.
Hardware and softwareHardware is the circuits. Software is the programs: numbers in memory. Usually one team builds each.
AccessOne load or one store: one use of an address.
Device registerA register inside a device that has an address. A write tells the device something. A read reports something.
BlockOne part of a chip with one job: the processor, the memory, or one device.
OwnerThe one block that answers for an address: memory, or one device.
SelectA one-bit wire to one owner. High means "this access is for you".
Address decoderThe circuit that reads an address and raises the select of its owner.
Read multiplexerThe circuit that passes the selected owner's value back to the processor, and nobody else's.
Memory mapThe table of owners: which addresses belong to whom.
Memory-mapped I/OReaching devices by giving them addresses in the memory map.

One Picture: One Owner Becomes Three

From One Owner to ThreeStep through the picture. It starts as the S0.1 computer and grows two devices, a decoder and a multiplexer.
  • Processor
  • Bus
  • Memory
  • Peripheral
CallValueReturnWaiting
From One Owner to ThreeStep through the picture. It starts as the S0.1 computer and grows two devices, a decoder and a multiplexer. component Processor knows only LOAD andSTORE component Address decoder raises one select wire component Read multiplexer passes one owner'svalue back component Memory addresses 0 to 13 component Lamps address 14 component Switches address 15 addressselectselectselectvalue to writevaluevaluevaluevalue read
  1. component Processorknows only LOAD and STORE
    • address → Address decoder
    • value to write → Lamps
  2. component Memoryaddresses 0 to 13
    • value → Read multiplexer
  3. component Lampsaddress 14
    • value → Read multiplexer
  4. component Switchesaddress 15
    • value → Read multiplexer
  5. component Address decoderraises one select wire
    • select → Memory
    • select → Lamps
    • select → Switches
  6. component Read multiplexerpasses one owner's value back
    • value read → Processor
  1. Start with S0.1

    One processor, one memory. Every LOAD and STORE goes to a memory box.

  2. 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.

  3. 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.

  4. 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
    1. data 00000011: Processor → Lamps
  5. 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
    1. 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.

Memory map of the sixteen-address machine
AddressesOwnerA read returnsA write does
0 to 13Memorythe number last stored therestores the number
14Lampswhich lamps are lit nowsets eight lamps, one bit each
15Switcheswhere the eight switches are now, one bit eachnothing

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.

Four rounds, with the decoder shown
RoundInstructionAddressSelect raisedWhat happens
1LOAD 1515switchesA = 00000011: switches 0 and 1 are on
2STORE 1414lampslamps 0 and 1 light
someone turns switch 1 off and switch 2 on
3LOAD 1515switchesA = 00000101
4STORE 1414lampslamps 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

  1. Rule 1: A device is reached through addresses.

    Software uses the ordinary load and store instructions. Nothing else is needed.

  2. 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.

  3. Rule 3: A read returns the selected block's value, and only that.

    The read multiplexer follows the select wires.

  4. 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.

  5. 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.

Printed by the lab. A and the lamps are shown in binary, one bit per switch or lamp.
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 HALT

Rule 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.

Four names in the ap_soc map
NameStands forWhat it is
Boot ROMread-only memory used at start-upMemory 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.
SRAMstatic random-access memoryOrdinary memory that programs read and write. The long name describes how its cells are built, which does not matter here.
UART0a standard name for a text port; the 0 means the first oneA device that sends and receives letters and digits over a pair of wires. Lesson S7.3 explains the name and the device.
GPIOgeneral-purpose input and outputA 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.
Part of the ap_soc v1.0 memory map
First address (base)SizeOwner
0x0000_00000x1000 (4,096 bytes)Boot ROM
0x1000_00000x1000 (4,096 bytes)UART0
0x1000_10000x1000 (4,096 bytes)GPIO
0x8000_00000x1_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.

  1. 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.

  2. 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 choiceThe alternativeWhat the choice buys
Give each device addressesA special instruction and special wires per deviceA new device needs no change in the processor.
One decoder decides ownershipEvery device checks the whole address itselfOne place to check that no two owners overlap.
A multiplexer returns the read valueEvery owner drives one shared wireTwo 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 page3 owners
A phone-class chiphundreds of owners

This section looks ahead. The sixteen-address map and a real one differ in size, and in the trouble that size brings.

  1. 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.

  2. 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.

  3. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 HALT

Check Yourself

Predict the outputThis program stores 8 to the switch address, reads that address back, and shows the result on the lamps. The switches are at 00000011. What is the last line the lab prints?
// The S0.1 computer with two devices. Boxes 0 to 13 are memory.
// Address 14 is a row of eight lamps. Address 15 is a row of eight switches.
module tb;
  logic clk = 0;
  always #5 clk = ~clk;

  logic [7:0] ram [0:13];              // addresses 0 to 13: ordinary memory
  logic [7:0] lamps    = 0;            // address 14: write here to light lamps
  logic [7:0] switches = 8'b0000_0011; // address 15: read here to see the switches
  logic [3:0] pc = 0;
  logic [7:0] a  = 0;

  localparam HALT = 0, LOAD = 1, ADD = 2, STORE = 3;   // named constants for the four kinds
  logic [7:0] word;
  logic [3:0] kind, addr;

  // The decoder: look at the address, raise one select.
  // always_comb describes plain gates: its outputs follow its inputs at once.
  logic  sel_ram, sel_lamps, sel_switches;
  string owner;                        // a text variable, for printing only: the selected owner's name
  always_comb begin
    sel_ram      = (addr <= 13);
    sel_lamps    = (addr == 14);
    sel_switches = (addr == 15);
    owner        = sel_ram ? "memory" : sel_lamps ? "lamps" : "switches";
  end

  // The read multiplexer: only the selected owner's value goes back.
  logic [7:0] read_value;
  always_comb begin
    if      (sel_ram)   read_value = ram[addr];
    else if (sel_lamps) read_value = lamps;
    else                read_value = switches;
  end

  initial begin
    for (int i = 0; i < 14; i++) ram[i] = 0;
    ram[0] = LOAD  * 16 + 13;   // box 13 holds 8
    ram[1] = STORE * 16 + 15;   // store A to the switch address
    ram[2] = LOAD  * 16 + 15;   // read the switch address back
    ram[3] = STORE * 16 + 14;   // show A on the lamps
    ram[4] = HALT  * 16;
    ram[13] = 8;
    forever begin                      // repeat until the HALT case ends the simulation
      @(posedge clk);

      word = ram[pc];                  // a fetch always selects memory here, so the lab reads ram directly
      kind = word / 16;
      addr = word % 16;
      #1;                              // let the decoder and the multiplexer settle
      case (kind)
        LOAD:  begin a = read_value;         // %08b prints eight binary digits
                 $display("pc=%0d LOAD %0d    select=%s  A=%08b", pc, addr, owner, a); end
        ADD:   begin a = a + read_value;
                 $display("pc=%0d ADD %0d     select=%s  A=%08b", pc, addr, owner, a); end
        STORE: begin
                 if (sel_ram)   ram[addr] = a;
                 if (sel_lamps) lamps     = a;   // a write to the switches changes nothing
                 $display("pc=%0d STORE %0d   select=%s  lamps=%08b", pc, addr, owner, lamps); end
        default: begin $display("pc=%0d HALT", pc);
                 $display("lamps=%08b", lamps);
                 $finish; end          // $finish ends the simulation
      endcase
      pc = pc + 1;
    end
  end
endmodule
ConceptWhat makes address 14 mean "the lamps" in the small machine?
ConceptTwo LOAD 15 instructions return different values. What is the reason?
ConceptIn ap_soc, which owner and offset does address 0x1000_0008 select?
ConceptThe read multiplexer has three inputs. What chooses the one that reaches the processor?

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.

Practice

Report a problem with this page