The Memory Map

Reading a memory map is one skill. Changing one is another. Give a new block its addresses without taking a single byte from a block that is already there.

You Will Be Able To

  • Find the first and the last byte of a region from its base and its size.
  • Place a new region that is aligned and overlaps nothing.
  • Tell a hole from a reserved region, and say why a map lives in one file.
Lesson
S4.1
Time
25 min
Runs on
Your browser
Check
5 questions

Your team has built a small recorder for signals inside ap_soc, and the software team wants to read its first register. Before that, the recorder needs an address. Pick the wrong one, and some other block starts answering for it.

Before You Start

You need S0.2: memory-mapped I/O, the memory map, region, base, offset and hexadecimal. You need the two buses and the bridge of S0.5, and the chip ap_soc from S1.1.

Five questions this page answers
Question
1Given a base and a size, which bytes are the first and the last of a region?
2Why is every size a power of two, and every base a multiple of its size?
3What is the difference between a hole and a reserved region, and can the bus tell them apart?
4How do you check that a new region fits?
5Why does a chip keep its memory map in one file?

The Idea: A Map Is a Plan of Land

Think of the addresses of a chip as a long street, and of each block as a plot of land on it. The memory map is the plan of that street. It says where each plot starts and how long it is.

A good plan has three properties. No two plots share any ground. Each plot starts at a round number, so that finding a house inside it is easy. And empty ground is marked: either free, or promised to somebody.

Break the first property, and a store meant for one block changes another. Break the second, and the decoder grows large and slow. Forget the third, and two teams build on the same empty plot. This page takes the three in turn, and then places a new block.

New Words

New words on this page
WordMeaning
KiB1024 bytes. A region of 4 KiB holds 4096 bytes, which is 0x1000 in hexadecimal.
End of a regionBase plus size: the first address after the region. It is not part of the region.
AlignedStarting at a multiple of its own size. A 4 KiB region is aligned when its base ends in three hex zeros.
Address pageIn this map, an aligned region of 4 KiB. The text says "page" for short. Most devices of ap_soc get one each.
HoleA run of addresses that the map gives to nobody.
Reserved regionA run of addresses that the map promises to a later version. Nothing answers there today.
ID registerA register that software can only read. It holds one fixed number that names the block.
The regions of ap_soc v1.0
RegionWhat lives thereBaseSize in bytesBus
Boot ROMmemory that holds the first instructions after reset0x0000_00000x1000AHB
ap_clinta timer for the processor, with its interrupt request0x0200_00000x10000AHB
ap_plicthe block that collects the interrupt requests of all devices0x0C00_00000x400000AHB
UART0the serial port0x1000_00000x1000APB
GPIOsixteen pins that software reads and drives0x1000_10000x1000APB
Timer and PWMa counter that measures time and makes pulses on a pin0x1000_20000x1000APB
Watchdoga counter that resets the chip when software stops answering0x1000_30000x1000APB
ap_sysctlregisters that control the clocks, the resets and the power of the chip0x1000_40000x1000APB
SPIa second kind of serial port, for other chips on the board0x1000_50000x1000APB
DMAa block that copies data by itself, without the processor0x1000_60000x1000APB
Reserved APB expansionkept for device registers of a later version0x1000_80000x8000APB
SRAMmemory for the program and its data0x8000_00000x10000AHB
Reserved SRAM expansionkept for more memory in a later version0x8001_00000x10000AHB

From the map file of ap_soc. Later lessons teach each block. Here you need only its place.

One Picture: The Whole Map, and One Free Page

Three Plots on the StreetStep through the picture. It shows the end of the device registers in ap_soc, and the place for a new block.
  • Peripheral
  • Memory
  • Debug
Three Plots on the StreetStep through the picture. It shows the end of the device registers in ap_soc, and the place for a new block. component DMA base 0x1000_6000,size 0x1000, lastbyte 0x1000_6FFF component A free page 0x1000_7000 to0x1000_7FFF: noregion in the map component Reserved base 0x1000_8000,size 0x8000:promised to a laterversion component Recorder the new block: itneeds 4 KiB or less end 0x1000_7000end 0x1000_8000base 0x1000_7000
  1. component DMAbase 0x1000_6000, size 0x1000, last byte 0x1000_6FFF
    • end 0x1000_7000 → A free page
  2. component A free page0x1000_7000 to 0x1000_7FFF: no region in the map
    • end 0x1000_8000 → Reserved
  3. component Reservedbase 0x1000_8000, size 0x8000: promised to a later version
  4. component Recorderthe new block: it needs 4 KiB or less
    • base 0x1000_7000 → A free page
  1. A region and its end

    DMA starts at 0x1000_6000 and holds 0x1000 bytes. Its last byte is 0x1000_6FFF. Its end, 0x1000_7000, is the first byte that is not DMA.

  2. Empty ground

    The next region in the map starts at 0x1000_8000. So one page of 0x1000 bytes lies between them, and nobody owns it.

  3. Two kinds of empty

    The free page is a hole: the map says nothing about it. The region after it is reserved: empty today, and already promised.

  4. The new block

    The recorder takes the hole. Its base is the end of DMA, and its own end is the base of the reserved region. It touches both and overlaps neither.

Read the diagram as text
  • DMA: base 0x1000_6000, size 0x1000, last byte 0x1000_6FFF
  • A free page: 0x1000_7000 to 0x1000_7FFF: no region in the map
  • Reserved: base 0x1000_8000, size 0x8000: promised to a later version
  • Recorder: the new block: it needs 4 KiB or less
  • DMA → A free page: end 0x1000_7000
  • A free page → Reserved: end 0x1000_8000
  • Recorder → A free page: base 0x1000_7000

Not to scale. The reserved region is eight times as long as a page.

Where the Address Goes in ap_soc v1.0
Where the Address Goes in ap_soc v1.0Addresses increase upward. Region heights use a compressed scale. In ap_soc, reads and writes to reserved or unmapped addresses return ERROR. AHB IDLE and BUSY receive zero-wait OKAY (IHI 0033 issue C §§3.2, 4.2.1). 0x1000_7000 · Unmapped · default error Exact ranges follow in the table. ROM RAM Device Reservedunmapped active transfer: ERROREnd 0xffff_ffffReserved SRAM expansion, reserved, AHB, 0x8001_0000, 64 KiB Reserved SRAMexpansion 64 KiB 0x8001_0000 SRAM, ram, AHB, 0x8000_0000, 64 KiB SRAM 64 KiB 0x8000_0000 Reserved APB expansion, reserved, APB, 0x1000_8000, 32 KiB Reserved APBexpansion 32 KiB 0x1000_8000 0x1000_7000 · Unmapped · default error DMA, device, APB, 0x1000_6000, 4 KiB DMA 4 KiB 0x1000_6000 SPI, device, APB, 0x1000_5000, 4 KiB SPI 4 KiB 0x1000_5000 ap_sysctl, device, APB, 0x1000_4000, 4 KiB ap_sysctl 4 KiB 0x1000_4000 Watchdog, device, APB, 0x1000_3000, 4 KiB Watchdog 4 KiB 0x1000_3000 Timer and PWM, device, APB, 0x1000_2000, 4 KiB Timer and PWM 4 KiB 0x1000_2000 GPIO, device, APB, 0x1000_1000, 4 KiB GPIO 4 KiB 0x1000_1000 UART0, device, APB, 0x1000_0000, 4 KiB UART0 4 KiB 0x1000_0000 ap_plic, device, AHB, 0x0c00_0000, 4 MiB ap_plic 4 MiB 0x0c00_0000 ap_clint, device, AHB, 0x0200_0000, 64 KiB ap_clint 64 KiB 0x0200_0000 Boot ROM, rom, AHB, 0x0000_0000, 4 KiB Boot ROM 4 KiB 0x0000_0000 AHB APB bridge window 0x1000_0000 · 64 KiB AHB

Addresses increase upward. Region heights use a compressed scale. In ap_soc, reads and writes to reserved or unmapped addresses return ERROR. AHB IDLE and BUSY receive zero-wait OKAY (IHI 0033 issue C §§3.2, 4.2.1).

Exact Address Ranges and Descriptions
Where the Address Goes in ap_soc v1.0 · inclusive end addresses
RegionBaseLast AddressSizeBusKindDescription
Boot ROM 0x0000_00000x0000_0fff4 KiB AHBromReset vector and boot code.
Unmapped 0x0000_10000x01ff_ffff32764 KiB —holeReads and writes receive the default error response.
ap_clint 0x0200_00000x0200_ffff64 KiB AHBdeviceCore-local timer and software interrupts.
Unmapped 0x0201_00000x0bff_ffff163776 KiB —holeReads and writes receive the default error response.
ap_plic 0x0c00_00000x0c3f_ffff4 MiB AHBdevicePlatform interrupt controller, 16 sources.
Unmapped 0x0c40_00000x0fff_ffff60 MiB —holeReads and writes receive the default error response.
UART0 0x1000_00000x1000_0fff4 KiB APBdeviceSerial console.
GPIO 0x1000_10000x1000_1fff4 KiB APBdevice16 general-purpose pins.
Timer and PWM 0x1000_20000x1000_2fff4 KiB APBdeviceTimer and pulse-width modulation.
Watchdog 0x1000_30000x1000_3fff4 KiB APBdeviceWatchdog reset control.
ap_sysctl 0x1000_40000x1000_4fff4 KiB APBdeviceChip ID, reset cause, clock control, pin mux and straps.
SPI 0x1000_50000x1000_5fff4 KiB APBdeviceSPI controller.
DMA 0x1000_60000x1000_6fff4 KiB APBdeviceDMA control registers; transfers use its AHB manager port.
Unmapped 0x1000_70000x1000_7fff4 KiB —holeReads and writes receive the default error response.
Reserved APB expansion 0x1000_80000x1000_ffff32 KiB APBreservedReserved for v2 peripherals; accesses return an error.
Unmapped 0x1001_00000x7fff_ffff1834944 KiB —holeReads and writes receive the default error response.
SRAM 0x8000_00000x8000_ffff64 KiB AHBramProgram and data memory.
Reserved SRAM expansion 0x8001_00000x8001_ffff64 KiB AHBreservedReserved for v2 SRAM growth; accesses return an error.
Unmapped 0x8002_00000xffff_ffff2097024 KiB —holeReads and writes receive the default error response.

The bar above is the whole map, drawn from the map file. The highlighted address, 0x1000_7000, is the free page of the first picture.

Zoom in on the Gap After DMA
First byteLast byteAllocation in v1.0
0x100060000x10006FFFDMA
0x100070000x10007FFFUnassigned
0x100080000x1000FFFFReserved APB expansion

The Smallest Example, Worked by Hand: First Byte, Last Byte

UART0 has base 0x1000_0000 and a size of 4 KiB, which is 0x1000 bytes. Its first byte is at offset zero, so its last byte is at offset 0xFFF.

Three regions, worked out
RegionBaseSizeLast byte (base + size - 1)End (base + size)
UART00x1000_00000x10000x1000_0FFF0x1000_1000
GPIO0x1000_10000x10000x1000_1FFF0x1000_2000
SRAM0x8000_00000x1_00000x8000_FFFF0x8001_0000

Look at the end of UART0. It equals the base of GPIO. Two neighbours touch when the end of one is the base of the next, and they share no byte.

Now try four homes for the recorder. A candidate fits when it is aligned and its last byte stays inside the free page.

Four candidates for the recorder
BaseSizeLast byteVerdict
0x1000_70004 KiB0x1000_7FFFfits: the base is a multiple of 0x1000
0x1000_70044 KiB0x1000_8003no: not aligned, and it runs into the reserved region
0x1000_78002 KiB0x1000_7FFFfits: the base is a multiple of 0x800
0x1000_78004 KiB0x1000_87FFno: 0x7800 is not a multiple of 0x1000

The Rules

  1. Rule 1: A region runs from its base to base plus size minus one.

    Base plus size is the end. It is the first byte of whatever comes next.

  2. Rule 2: No two regions share a byte.

    Sort the regions by base. The end of each one must be at or below the base of the next.

  3. Rule 3: Every size is a power of two, and every base is a multiple of its size.

    Then the low bits of an address are the offset, and the high bits alone name the region.

  4. Rule 4: An address with no owner gets an error.

    A hole and a reserved region answer the same way on the bus. Only the map tells them apart.

  5. Rule 5: The map lives in one file.

    The decoder, the address list for software and the drawings are all made from that file.

Sources for These Rules

Rules 1, 2 and 5 are arithmetic and working practice. No specification states them.

Rule 3 is a rule of the ap_soc map. The AHB specification is looser. It asks only that a region is at least 1 KB long and starts and ends on a 1 KB boundary. Arm AMBA AHB Protocol Specification, document IHI 0033, issue C, section 4.2.

Rule 4: the same document, section 4.2.1. The next lesson follows that error on the bus.

A device reached through addresses: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 9.2.

Now Run It

The lab at the end of this page holds the decoder made from the map file. It has one select bit for each region that has a block. Beside it sits the recorder, switched off at first. The test code reads five addresses and prints what the decoder says.

Five reads on the frozen map
Address readselOwner that sel namesprobedefaultid_data
10000000008UART00000000000
10006ffc200DMA0000000000
10007000000nobody0100000000
10007ffc000nobody0100000000
10008000000nobody0100000000

Recorded from the lab. sel is in hexadecimal, with one bit for each block. The third column is our reading of that bit. default is 1 when no select is raised.

Rule 1 at work: 10006ffc is still DMA, close to its last byte. Rule 4 at work: 10007000 and 10008000 both raise default. The first is a hole and the second is reserved, and the wires show no difference.

Now switch the recorder on. The lab has a constant called PRESENT: set it to 1. A constant called BYTES sets the size of the region, and BASE sets its base.

The same reads with the recorder at 0x1000_7000
Address readselOwner that sel namesprobedefaultid_data
10000000008UART00000000000
10006ffc200DMA0000000000
10007000000nobody1053434f50
10007ffc000nobody1000000000
10008000000nobody0100000000

Recorded from the lab with PRESENT set to 1. probe is the select of the recorder. id_data is what it returns.

At 10007000 the recorder answers with 53434f50, its ID. At 10007ffc it still owns the address, but returns zero: only offset 0 holds a register. At 10008000 nothing changed, so the reserved region is untouched.

This lab leaves the bus out. It checks addresses only, with no clock and no handshake. The page One Access, End to End shows the handshake.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Aligned regions with power-of-two sizesAny base and any sizeThe decoder compares a few high bits with a constant. Any size would need two magnitude comparisons for each region.
A whole 4 KiB page for a small deviceExactly as many bytes as the device has registersEvery device decodes the same way, and has room to grow. Most of each page stays empty.
Reserved regions written in the mapLeave the space as a holeNobody builds there by accident. The promise holds only while people read the map.
One map fileA decoder, a software list and a drawing kept by handThe three cannot disagree. A mistake in the file reaches all three at once.

Empty pages cost almost nothing. A 32-bit address names about four thousand million bytes, and ap_soc uses a tiny part of them.

The Trap: A Copy of the Map

The lab returns 53434f50 from the recorder, yet the software on the board reads zero. The hardware team checks the recorder for a day and finds nothing wrong.

The fault is in the address. The software was built with an address list copied by hand from last month's map. It reads a page where the recorder used to be planned. Check the address that the bus took before you open the block.

One map file prevents this, and brings a quieter danger. Files made from one map always agree with each other, even when the map itself is wrong. So check the map against what the blocks really are.

At Scale

ap_soc13 regions
A phone-class chiphundreds of regions

This section looks ahead. The map of ap_soc has thirteen regions and one team. A large chip has hundreds of regions, many teams, and boards that still run last year's software.

  1. Sort by address

    Overlaps hide in a list sorted by team. Sort by base, and compare each end with the next base. Include the reserved regions.

  2. Replay old addresses

    Run last year's address list against the new decoder. An address that was empty must still be empty, unless the map says otherwise.

  3. Two addresses, one register

    A decoder that compares too few bits lets one register answer at two addresses. The next lesson, on address decode, builds that fault and finds it.

Try It

Run the lab with PRESENT at 0. Then set it to 1 and compare the rows for 10007000 and 10007ffc.

Set BYTES to 2048. Predict which of the five reads the recorder still answers.

Now also set BASE to 32'h10007800, and add a read of that address. Find the ID again.

Check the Two Smaller Regions
Recorded with 2 KiB at the first base. The upper half of the page is a hole again.
addr=10007000 sel=000 probe=1 default=0 id_data=53434f50
addr=10007ffc sel=000 probe=0 default=1 id_data=00000000
Recorded with 2 KiB in the upper half. The ID is now at 10007800.
addr=10007000 sel=000 probe=0 default=1 id_data=00000000
addr=10007800 sel=000 probe=1 default=0 id_data=53434f50
addr=10007ffc sel=000 probe=1 default=0 id_data=00000000
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: addr=10000000 sel=008 probe=0 default=0 id_data=00000000 addr=10006ffc sel=200 probe=0 default=0 id_data=00000000 addr=10007000 sel=000 probe=0 default=1 id_data=00000000 addr=10007ffc sel=000 probe=0 default=1 id_data=00000000 addr=10008000 sel=000 probe=0 default=1 id_data=00000000
Read included file: full_decode.sv
module \full_decode (input wire [31:0] addr, output wire [10:0] sel, output wire error_sel);
  // sel[0]: Boot ROM
  assign sel[0] = ((addr & 32'hfffff000) == 32'h00000000);
  // sel[1]: ap_clint
  assign sel[1] = ((addr & 32'hffff0000) == 32'h02000000);
  // sel[2]: ap_plic
  assign sel[2] = ((addr & 32'hffc00000) == 32'h0c000000);
  // sel[3]: UART0
  assign sel[3] = ((addr & 32'hfffff000) == 32'h10000000);
  // sel[4]: GPIO
  assign sel[4] = ((addr & 32'hfffff000) == 32'h10001000);
  // sel[5]: Timer and PWM
  assign sel[5] = ((addr & 32'hfffff000) == 32'h10002000);
  // sel[6]: Watchdog
  assign sel[6] = ((addr & 32'hfffff000) == 32'h10003000);
  // sel[7]: ap_sysctl
  assign sel[7] = ((addr & 32'hfffff000) == 32'h10004000);
  // sel[8]: SPI
  assign sel[8] = ((addr & 32'hfffff000) == 32'h10005000);
  // sel[9]: DMA
  assign sel[9] = ((addr & 32'hfffff000) == 32'h10006000);
  // sel[10]: SRAM
  assign sel[10] = ((addr & 32'hffff0000) == 32'h80000000);
  assign error_sel = ~|sel;
endmodule
Read included file: map_probe.sv
// A read-only identification register for a proposed peripheral in the lab.
// BASE and BYTES describe a naturally aligned window inside the free APB page.
module map_probe #(
  parameter bit PRESENT = 0,
  parameter logic [31:0] BASE = 32'h10007000,
  parameter integer BYTES = 4096
) (
  input  wire [31:0] addr,
  output wire selected,
  output wire [31:0] data
);
  initial begin
    if (BYTES <= 0 || BYTES > 4096 || (BYTES & (BYTES-1)) != 0 ||
        BASE[31:12] != 20'h10007 || (BASE & (BYTES-1)) != 0)
      $fatal(1, "Choose an aligned window within the unassigned APB page");
  end
  assign selected = PRESENT && ((addr & ~(BYTES-1)) == BASE);
  assign data = selected && addr == BASE ? 32'h53434f50 : 32'd0;
endmodule

Check Yourself

Predict the outputThe recorder is switched on, with 4 KiB at 0x1000_7000. What is the last line the lab prints?
module tb;
  // Add one region to your lab copy: change PRESENT to 1, then run again.
  localparam bit PRESENT = 1;
  localparam logic [31:0] BASE = 32'h10007000;
  localparam integer BYTES = 4096;
  logic [31:0] addr;
  wire [10:0] sel;
  wire miss, probe;
  wire [31:0] data;
  full_decode frozen(addr, sel, miss);
  map_probe #(PRESENT, BASE, BYTES) device_id(addr, probe, data);

  task automatic read_word(input logic [31:0] address);
    addr = address;
    #1;
    $display("addr=%08h sel=%03h probe=%b default=%b id_data=%08h",
             addr, sel, probe, miss && !probe, data);
  endtask
  initial begin
    read_word(32'h10000000);
    read_word(32'h10006ffc);
    read_word(32'h10007000);
    read_word(32'h10007ffc);
    read_word(32'h10008000);
  end
endmodule
Read included file: full_decode.sv
module \full_decode (input wire [31:0] addr, output wire [10:0] sel, output wire error_sel);
  // sel[0]: Boot ROM
  assign sel[0] = ((addr & 32'hfffff000) == 32'h00000000);
  // sel[1]: ap_clint
  assign sel[1] = ((addr & 32'hffff0000) == 32'h02000000);
  // sel[2]: ap_plic
  assign sel[2] = ((addr & 32'hffc00000) == 32'h0c000000);
  // sel[3]: UART0
  assign sel[3] = ((addr & 32'hfffff000) == 32'h10000000);
  // sel[4]: GPIO
  assign sel[4] = ((addr & 32'hfffff000) == 32'h10001000);
  // sel[5]: Timer and PWM
  assign sel[5] = ((addr & 32'hfffff000) == 32'h10002000);
  // sel[6]: Watchdog
  assign sel[6] = ((addr & 32'hfffff000) == 32'h10003000);
  // sel[7]: ap_sysctl
  assign sel[7] = ((addr & 32'hfffff000) == 32'h10004000);
  // sel[8]: SPI
  assign sel[8] = ((addr & 32'hfffff000) == 32'h10005000);
  // sel[9]: DMA
  assign sel[9] = ((addr & 32'hfffff000) == 32'h10006000);
  // sel[10]: SRAM
  assign sel[10] = ((addr & 32'hffff0000) == 32'h80000000);
  assign error_sel = ~|sel;
endmodule
Read included file: map_probe.sv
// A read-only identification register for a proposed peripheral in the lab.
// BASE and BYTES describe a naturally aligned window inside the free APB page.
module map_probe #(
  parameter bit PRESENT = 0,
  parameter logic [31:0] BASE = 32'h10007000,
  parameter integer BYTES = 4096
) (
  input  wire [31:0] addr,
  output wire selected,
  output wire [31:0] data
);
  initial begin
    if (BYTES <= 0 || BYTES > 4096 || (BYTES & (BYTES-1)) != 0 ||
        BASE[31:12] != 20'h10007 || (BASE & (BYTES-1)) != 0)
      $fatal(1, "Choose an aligned window within the unassigned APB page");
  end
  assign selected = PRESENT && ((addr & ~(BYTES-1)) == BASE);
  assign data = selected && addr == BASE ? 32'h53434f50 : 32'd0;
endmodule
ConceptA region has base 0x2000 and size 0x800. Which address is its end, the first one after it?
ConceptWhich of these regions is aligned?
ConceptA read of a hole and a read of a reserved region reach the bus. What tells them apart?
ConceptWhy does a chip keep its map in one file?

Common Mistakes

Wrong Treat base plus size as the last byte.

That address is the end. It may already be the first byte of the next region.

Fix Count offsets from zero up to size minus one.

Wrong Treat every error as free space.

A reserved region also answers with an error.

Fix Read the map before you promise an address.

Wrong Start a region wherever the last one stopped.

A base that is not a multiple of the size spoils the simple decoder.

Fix Round the base up to a multiple of the size.

Remember

  • A region runs from base to base plus size minus one; base plus size is the end.
  • Neighbours touch when the end of one is the base of the next; they never share a byte.
  • Sizes are powers of two and bases are multiples of the size, so the low bits are the offset.
  • A hole and a reserved region look the same on the bus; only the map tells them apart.
  • One map file makes the decoder and the software address list agree.

Practice

Report a problem with this page