Address Decode and Aliases
A decoder turns an address into one select by comparing bits. Leave one bit out of the comparison, and a register quietly answers at two addresses.
You Will Be Able To
- Say which bits of an address a decoder compares, and which it leaves to the device.
- Explain how a decoder that compares too few bits creates an alias.
- Find an alias by flipping one address bit at a time.
- Lesson
- S4.2
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
The pin register of GPIO lives at 0x1000_1004. Now read 0x1010_1004, which differs in one bit. The map says nobody lives there. If GPIO answers anyway, any stray address in the software can reach real pins.
Before You Start
You need the decoder, the select and the read multiplexer of S0.2, with region, base and offset. You need the aligned regions and the hole of The Memory Map.
| Question | |
|---|---|
| 1 | Which bits of an address does a decoder compare, and which does it ignore? |
| 2 | Why does a decoder raise at most one select? |
| 3 | What is an alias, and what causes one? |
| 4 | How many addresses reach a register when its decoder ignores some bits? |
| 5 | How do you find an alias in a decoder? |
The Idea: Decoding Is Comparing
The decoder answers one question for every access: who owns this address? The answer is not a search through the map. Each region has one small comparison of its own, and all the comparisons run at once.
Aligned regions make each comparison simple. The low bits of the address are the offset, and the decoder never looks at them. The high bits must equal one constant. A decoder that checks every one of those high bits is doing full decode.
Now leave one high bit out of a comparison, to save a gate or by mistake. That is partial decode. Two addresses that differ only in that bit now look the same. The region answers at both. The second address is an alias of the first, also called a mirror.
An alias is a quiet fault. Every legal address still works, so ordinary tests pass. The harm comes later, when a wrong address in the software lands on the alias and changes a real device.
New Words
| Word | Meaning |
|---|---|
| Full decode | Comparing every address bit above the offset. Each register then has exactly one address. |
| Partial decode | Comparing only some of those bits. The bits left out are ignored. |
| Alias | A second address that reaches the same register, because the decoder ignores a bit. Also called a mirror. |
| One-hot | A group of wires in which exactly one is 1. |
| Staged decode | Decoding in two steps: a first decoder picks a group of regions, and a second one picks inside the group. |
One Picture: From One Address to One Select
- Processor
- Bus
- Peripheral
- Split the address
GPIO holds 0x1000 bytes, so the low 12 bits are the offset. The decoder ignores them. The 20 bits above them are what it looks at.
- One comparison for each region
Each comparison checks the high bits against one constant, the base of its region. All of them work at the same time.
- One select
The high bits of 0x1000_1004 are 0x10001. Only the GPIO comparison is true, so only the GPIO select is 1.
- Nobody
For an address in a hole, no comparison is true. A good decoder says so with a wire of its own.
- A comparison that looks at fewer bits
Suppose the GPIO comparison skips bit 20. Then 0x1000_1004 and 0x1010_1004 look the same to it, and GPIO answers both.
Read the diagram as text
- Address: 32 bits, here 0x1000_1004
- UART0 comparison: are the top 20 bits 0x10000?
- GPIO comparison: are the top 20 bits 0x10001?
- SRAM comparison: are the top 16 bits 0x8000?
- GPIO: receives its select, and the low 12 bits as the offset
- Nobody: true when no comparison is true
- Address → UART0 comparison: high bits
- Address → GPIO comparison: high bits
- Address → SRAM comparison: high bits
- GPIO comparison → GPIO: select
- SRAM comparison → Nobody: no match anywhere
Only three of the comparisons are drawn. The low 12 bits travel to every device on shared wires.
Count the wire "nobody" as a select, and the outputs are one-hot: for every address, exactly one of them is 1.
The Smallest Example, Worked by Hand: Eight Address Bits
Take a machine with addresses of eight bits. A lamp device owns the sixteen addresses 0x40 to 0x4F. Its offset is the low four bits. Its high four bits are always 0100.
A full decoder checks all four high bits. A partial decoder skips the top one, bit 7, and checks only that bits 6 to 4 are 100.
| Address | In binary | Full decode: high bits 0100? | Partial decode: bits 6 to 4 are 100? |
|---|---|---|---|
| 0x43 | 0100 0011 | yes: lamps, offset 3 | yes: lamps, offset 3 |
| 0xC3 | 1100 0011 | no | yes: lamps, offset 3. An alias of 0x43 |
| 0x53 | 0101 0011 | no | no |
With full decode, sixteen addresses reach the lamps. With bit 7 ignored, thirty-two do: 0x40 to 0x4F, and 0xC0 to 0xCF. Every ignored bit doubles the count.
The Rules
Rule 1: The low bits are the offset, and the decoder ignores them.
A region of 0x1000 bytes has 12 offset bits. They go to the device, which uses them to pick a register.
Rule 2: Each region has one comparison of the high bits against its base.
Regions do not overlap, so at most one comparison is true. Add a wire for "nobody", and the selects are one-hot.
Rule 3: Full decode compares every high bit.
Then each register has exactly one address, and every other address reaches nobody.
Rule 4: Every bit left out doubles the addresses that reach the region.
The extra addresses are aliases. They sit where the map shows a hole, or another region.
Rule 5: A second decoder may skip the bits a first decoder already checked.
This is staged decode. It is safe only when something really did check those bits.
Sources for These Rules
Arm AMBA AHB Protocol Specification, document IHI 0033, issue C, section 4.2. It gives the decoder one select for each subordinate, and encourages simple decoding schemes. Its smallest region is 1 KB.
Arm AMBA APB Protocol Specification, document IHI 0024, issue E, section 2.1: one select for each completer.
Rules 3 to 5 are arithmetic and working practice. The specifications do not use the words full decode, partial decode or alias.
Now Run It
The lab at the end of this page builds the decoder of four ap_soc regions twice. The full one compares every high bit. The short one never sees bit 20. Test code feeds six addresses to both, and then searches for aliases.
addr=10001004 full=00100 GPIO | short=00100 GPIO
addr=10101004 full=10000 nobody | short=00100 GPIO <- the decoders disagree
addr=10001ffc full=00100 GPIO | short=00100 GPIO
addr=10007000 full=10000 nobody | short=10000 nobody
addr=8000fffc full=01000 SRAM | short=01000 SRAM
addr=80100000 full=10000 nobody | short=01000 SRAM <- the decoders disagree
flip bit 20: addr=10101004 still selects GPIO in the short decoder
aliases of GPIO found: full=0 short=1Rule 2 at work: every group of five bits has exactly one 1. Rule 3 at work: the full decoder sends 10101004 to nobody. Rule 4 at work: the short decoder sends it to GPIO. The map calls that address a hole.
The last address shows that the fault is not GPIO's alone. 80100000 lies beyond SRAM, yet the short decoder selects SRAM. One ignored bit gives every region a mirror.
The last two lines are the search. Starting from 10001004, the test code flips one high bit at a time. GPIO should stop answering each time. In the short decoder it keeps answering once, at bit 20.
This lab leaves the bus out. It has no clock and no handshake, and its four regions are a part of the real map.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Full decode | Partial decode | No aliases. The saving of partial decode is small: see the measured numbers below. |
| A wire for "nobody" | Let an unowned address select nothing | The bus can answer a stray access with an error. The next lesson builds that answer. |
| Staged decode | One wide comparison for every device | The first decoder stays small and fast. Each stage must be sure the other checked the remaining bits. |
How much does a skipped bit save? We built the full decoder of ap_soc, with eleven selects, in a synthesis tool. Then we built it again without bit 20.
| Measure | Full decode | Bit 20 ignored |
|---|---|---|
| Cells | 62 | 62 |
| Logic levels on the longest path | 9 | 9 |
Measured with Yosys 0.70+290, generic cells. A cell is one small logic element. The numbers belong to this tool and this decoder.
The numbers are the same. The tool shares logic between the comparisons, so one bit more costs nothing here. The alias bought no saving at all.
Partial decode is not always a fault. In ap_soc the bridge is selected for every address that starts 0x1000. The decoder behind it checks only the next hex digit. That is staged decode, and it is safe because the first stage checked the rest.
The Trap: One Select Proves Little
A team tests its decoder with every address in the map. Each one raises exactly one select, and the right one. The decoder is signed off.
Months later a program follows a wrong address into what the map calls empty space. A device changes its output. Nothing in the test could have shown this, because the test only used addresses that have an owner.
A decoder is correct when the right addresses reach a device and every other address reaches nobody. Test the second half too. Flip each high bit of a known address, as the lab does, and expect the select to fall.
At Scale
This section looks ahead. The lab decoder has four regions. A large chip decodes hundreds, in several stages.
Who answers for nobody
An access to a hole still needs an answer, or the manager waits for ever. The next lesson builds the block that gives it.
Proving it
Flipping bits one at a time finds single mistakes. A proof tool can show that the selects are one-hot for every one of the four thousand million addresses. Problem S4.2.3 does that.
Decode and speed
The address must pass the decoder before a subordinate can start work. On a wide bus, the decoder is often on the slowest path. The Protocols track shows the decoder on AHB.
Try It
Run the lab. For each of the six addresses, work out the owner by hand before you read the row.
Change SEEN to all ones, eight hex digits F. The short decoder is now a full one. Predict the last line.
Change SEEN so that it hides bit 12 and nothing else. Find who answers 0x1000_1004 now. Say why this is worse than a mirror in a hole.
Add a fifth region for the timer at 0x1000_2000 to both decoders. Remember the function that names the owner.
addr=10001004 full=00100 GPIO | short=00100 GPIO addr=10101004 full=10000 nobody | short=00100 GPIO <- the decoders disagree addr=10001ffc full=00100 GPIO | short=00100 GPIO addr=10007000 full=10000 nobody | short=10000 nobody addr=8000fffc full=01000 SRAM | short=01000 SRAM addr=80100000 full=10000 nobody | short=01000 SRAM <- the decoders disagree flip bit 20: addr=10101004 still selects GPIO in the short decoder aliases of GPIO found: full=0 short=1Check Yourself
Common Mistakes
Wrong Leave an address bit out because every device today has it at 0.
That bit is what separates a device from the hole above it.
Fix Compare every high bit, or name the earlier stage that checks it.
Wrong Accept "exactly one select" as proof of a correct decoder.
An alias also raises exactly one select. It is the select of the wrong owner.
Fix Also test addresses that should reach nobody.
Wrong Let the decoder look at the offset bits.
Then only some registers of the device can be reached.
Fix Compare the bits above the offset, and pass the offset to the device.
Remember
- A decoder compares the address bits above the offset with one constant for each region.
- Regions do not overlap, so the selects, with "nobody", are one-hot.
- Full decode gives every register exactly one address.
- Each ignored bit doubles the addresses that reach a region; the extra ones are aliases.
- Test a decoder with addresses that should reach nobody, by flipping one high bit at a time.