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.
| Question | |
|---|---|
| 1 | Given a base and a size, which bytes are the first and the last of a region? |
| 2 | Why is every size a power of two, and every base a multiple of its size? |
| 3 | What is the difference between a hole and a reserved region, and can the bus tell them apart? |
| 4 | How do you check that a new region fits? |
| 5 | Why 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
| Word | Meaning |
|---|---|
| KiB | 1024 bytes. A region of 4 KiB holds 4096 bytes, which is 0x1000 in hexadecimal. |
| End of a region | Base plus size: the first address after the region. It is not part of the region. |
| Aligned | Starting at a multiple of its own size. A 4 KiB region is aligned when its base ends in three hex zeros. |
| Address page | In this map, an aligned region of 4 KiB. The text says "page" for short. Most devices of ap_soc get one each. |
| Hole | A run of addresses that the map gives to nobody. |
| Reserved region | A run of addresses that the map promises to a later version. Nothing answers there today. |
| ID register | A register that software can only read. It holds one fixed number that names the block. |
| Region | What lives there | Base | Size in bytes | Bus |
|---|---|---|---|---|
| Boot ROM | memory that holds the first instructions after reset | 0x0000_0000 | 0x1000 | AHB |
| ap_clint | a timer for the processor, with its interrupt request | 0x0200_0000 | 0x10000 | AHB |
| ap_plic | the block that collects the interrupt requests of all devices | 0x0C00_0000 | 0x400000 | AHB |
| UART0 | the serial port | 0x1000_0000 | 0x1000 | APB |
| GPIO | sixteen pins that software reads and drives | 0x1000_1000 | 0x1000 | APB |
| Timer and PWM | a counter that measures time and makes pulses on a pin | 0x1000_2000 | 0x1000 | APB |
| Watchdog | a counter that resets the chip when software stops answering | 0x1000_3000 | 0x1000 | APB |
| ap_sysctl | registers that control the clocks, the resets and the power of the chip | 0x1000_4000 | 0x1000 | APB |
| SPI | a second kind of serial port, for other chips on the board | 0x1000_5000 | 0x1000 | APB |
| DMA | a block that copies data by itself, without the processor | 0x1000_6000 | 0x1000 | APB |
| Reserved APB expansion | kept for device registers of a later version | 0x1000_8000 | 0x8000 | APB |
| SRAM | memory for the program and its data | 0x8000_0000 | 0x10000 | AHB |
| Reserved SRAM expansion | kept for more memory in a later version | 0x8001_0000 | 0x10000 | AHB |
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
- Peripheral
- Memory
- Debug
- 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.
- 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.
- 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.
- 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.
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
| Region | Base | Last Address | Size | Bus | Kind | Description |
|---|---|---|---|---|---|---|
| Boot ROM | 0x0000_0000 | 0x0000_0fff | 4 KiB | AHB | rom | Reset vector and boot code. |
| Unmapped | 0x0000_1000 | 0x01ff_ffff | 32764 KiB | — | hole | Reads and writes receive the default error response. |
| ap_clint | 0x0200_0000 | 0x0200_ffff | 64 KiB | AHB | device | Core-local timer and software interrupts. |
| Unmapped | 0x0201_0000 | 0x0bff_ffff | 163776 KiB | — | hole | Reads and writes receive the default error response. |
| ap_plic | 0x0c00_0000 | 0x0c3f_ffff | 4 MiB | AHB | device | Platform interrupt controller, 16 sources. |
| Unmapped | 0x0c40_0000 | 0x0fff_ffff | 60 MiB | — | hole | Reads and writes receive the default error response. |
| UART0 | 0x1000_0000 | 0x1000_0fff | 4 KiB | APB | device | Serial console. |
| GPIO | 0x1000_1000 | 0x1000_1fff | 4 KiB | APB | device | 16 general-purpose pins. |
| Timer and PWM | 0x1000_2000 | 0x1000_2fff | 4 KiB | APB | device | Timer and pulse-width modulation. |
| Watchdog | 0x1000_3000 | 0x1000_3fff | 4 KiB | APB | device | Watchdog reset control. |
| ap_sysctl | 0x1000_4000 | 0x1000_4fff | 4 KiB | APB | device | Chip ID, reset cause, clock control, pin mux and straps. |
| SPI | 0x1000_5000 | 0x1000_5fff | 4 KiB | APB | device | SPI controller. |
| DMA | 0x1000_6000 | 0x1000_6fff | 4 KiB | APB | device | DMA control registers; transfers use its AHB manager port. |
| Unmapped | 0x1000_7000 | 0x1000_7fff | 4 KiB | — | hole | Reads and writes receive the default error response. |
| Reserved APB expansion | 0x1000_8000 | 0x1000_ffff | 32 KiB | APB | reserved | Reserved for v2 peripherals; accesses return an error. |
| Unmapped | 0x1001_0000 | 0x7fff_ffff | 1834944 KiB | — | hole | Reads and writes receive the default error response. |
| SRAM | 0x8000_0000 | 0x8000_ffff | 64 KiB | AHB | ram | Program and data memory. |
| Reserved SRAM expansion | 0x8001_0000 | 0x8001_ffff | 64 KiB | AHB | reserved | Reserved for v2 SRAM growth; accesses return an error. |
| Unmapped | 0x8002_0000 | 0xffff_ffff | 2097024 KiB | — | hole | Reads 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 byte | Last byte | Allocation in v1.0 |
|---|---|---|
| 0x10006000 | 0x10006FFF | DMA |
| 0x10007000 | 0x10007FFF | Unassigned |
| 0x10008000 | 0x1000FFFF | Reserved 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.
| Region | Base | Size | Last byte (base + size - 1) | End (base + size) |
|---|---|---|---|---|
| UART0 | 0x1000_0000 | 0x1000 | 0x1000_0FFF | 0x1000_1000 |
| GPIO | 0x1000_1000 | 0x1000 | 0x1000_1FFF | 0x1000_2000 |
| SRAM | 0x8000_0000 | 0x1_0000 | 0x8000_FFFF | 0x8001_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.
| Base | Size | Last byte | Verdict |
|---|---|---|---|
| 0x1000_7000 | 4 KiB | 0x1000_7FFF | fits: the base is a multiple of 0x1000 |
| 0x1000_7004 | 4 KiB | 0x1000_8003 | no: not aligned, and it runs into the reserved region |
| 0x1000_7800 | 2 KiB | 0x1000_7FFF | fits: the base is a multiple of 0x800 |
| 0x1000_7800 | 4 KiB | 0x1000_87FF | no: 0x7800 is not a multiple of 0x1000 |
The Rules
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.
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.
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.
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.
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.
| Address read | sel | Owner that sel names | probe | default | id_data |
|---|---|---|---|---|---|
| 10000000 | 008 | UART0 | 0 | 0 | 00000000 |
| 10006ffc | 200 | DMA | 0 | 0 | 00000000 |
| 10007000 | 000 | nobody | 0 | 1 | 00000000 |
| 10007ffc | 000 | nobody | 0 | 1 | 00000000 |
| 10008000 | 000 | nobody | 0 | 1 | 00000000 |
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.
| Address read | sel | Owner that sel names | probe | default | id_data |
|---|---|---|---|---|---|
| 10000000 | 008 | UART0 | 0 | 0 | 00000000 |
| 10006ffc | 200 | DMA | 0 | 0 | 00000000 |
| 10007000 | 000 | nobody | 1 | 0 | 53434f50 |
| 10007ffc | 000 | nobody | 1 | 0 | 00000000 |
| 10008000 | 000 | nobody | 0 | 1 | 00000000 |
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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Aligned regions with power-of-two sizes | Any base and any size | The 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 device | Exactly as many bytes as the device has registers | Every device decodes the same way, and has room to grow. Most of each page stays empty. |
| Reserved regions written in the map | Leave the space as a hole | Nobody builds there by accident. The promise holds only while people read the map. |
| One map file | A decoder, a software list and a drawing kept by hand | The 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
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.
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.
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.
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
addr=10007000 sel=000 probe=1 default=0 id_data=53434f50
addr=10007ffc sel=000 probe=0 default=1 id_data=00000000addr=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=00000000addr=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=00000000Read 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
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.