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.

Five questions this page answers
Question
1Which bits of an address does a decoder compare, and which does it ignore?
2Why does a decoder raise at most one select?
3What is an alias, and what causes one?
4How many addresses reach a register when its decoder ignores some bits?
5How 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

New words on this page
WordMeaning
Full decodeComparing every address bit above the offset. Each register then has exactly one address.
Partial decodeComparing only some of those bits. The bits left out are ignored.
AliasA second address that reaches the same register, because the decoder ignores a bit. Also called a mirror.
One-hotA group of wires in which exactly one is 1.
Staged decodeDecoding 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

From One Address to One SelectStep through the picture. An address goes in at the left. At most one select comes out at the right.
  • Processor
  • Bus
  • Peripheral
From One Address to One SelectStep through the picture. An address goes in at the left. At most one select comes out at the right. component Address 32 bits, here0x1000_1004 component UART0comparison are the top 20 bits0x10000? component GPIO comparison are the top 20 bits0x10001? component SRAM comparison are the top 16 bits0x8000? component GPIO receives its select,and the low 12 bits asthe offset component Nobody true when nocomparison is true high bitshigh bitshigh bitsselectno match anywhere
  1. component Address32 bits, here 0x1000_1004
    • high bits → UART0 comparison
    • high bits → GPIO comparison
    • high bits → SRAM comparison
  2. component UART0 comparisonare the top 20 bits 0x10000?
  3. component GPIO comparisonare the top 20 bits 0x10001?
    • select → GPIO
  4. component SRAM comparisonare the top 16 bits 0x8000?
    • no match anywhere → Nobody
  5. component GPIOreceives its select, and the low 12 bits as the offset
  6. component Nobodytrue when no comparison is true
  1. 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.

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

  3. One select

    The high bits of 0x1000_1004 are 0x10001. Only the GPIO comparison is true, so only the GPIO select is 1.

  4. Nobody

    For an address in a hole, no comparison is true. A good decoder says so with a wire of its own.

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

Three addresses through both decoders
AddressIn binaryFull decode: high bits 0100?Partial decode: bits 6 to 4 are 100?
0x430100 0011yes: lamps, offset 3yes: lamps, offset 3
0xC31100 0011noyes: lamps, offset 3. An alias of 0x43
0x530101 0011nono

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

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

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

  3. Rule 3: Full decode compares every high bit.

    Then each register has exactly one address, and every other address reaches nobody.

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

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

Printed by the lab. Each group of five bits is nobody, SRAM, GPIO, UART0, ROM, in that order. The name after it is the owner those bits mean.
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=1

Rule 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 choiceThe alternativeWhat the choice buys, and costs
Full decodePartial decodeNo aliases. The saving of partial decode is small: see the measured numbers below.
A wire for "nobody"Let an unowned address select nothingThe bus can answer a stray access with an error. The next lesson builds that answer.
Staged decodeOne wide comparison for every deviceThe 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.

The ap_soc decoder, synthesised twice
MeasureFull decodeBit 20 ignored
Cells6262
Logic levels on the longest path99

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 page4 regions, 1 stage
A large chiphundreds of regions, several stages

This section looks ahead. The lab decoder has four regions. A large chip decodes hundreds, in several stages.

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

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

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

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

Check Yourself

Predict the outputThis short decoder ignores two bits: bit 20 and bit 13. The search starts from 0x1000_1004, as before. What is the last line the lab prints?
// The address decoder of four ap_soc regions, built twice.
// The full decoder compares every address bit above the offset. The short one leaves bits out.
module tb;
  logic [31:0] addr;                           // the address under test

  // ---- The full decoder ----
  // A region of 0x1000 bytes keeps its low 12 bits for the offset. The other 20 bits name the region.
  wire rom_f  = (addr[31:12] == 20'h00000);    // Boot ROM  base 0x0000_0000, size 0x1000
  wire uart_f = (addr[31:12] == 20'h10000);    // UART0     base 0x1000_0000, size 0x1000
  wire gpio_f = (addr[31:12] == 20'h10001);    // GPIO      base 0x1000_1000, size 0x1000
  wire sram_f = (addr[31:16] == 16'h8000);     // SRAM      base 0x8000_0000, size 0x1_0000: 16 offset bits
  wire none_f = !(rom_f | uart_f | gpio_f | sram_f);   // no region matched

  // ---- The short decoder: the same comparisons on an address with bits missing ----
  localparam logic [31:0] SEEN = 32'hFFEF_DFFF;   // the bits this decoder looks at: all but bits 20 and 13
  wire [31:0] a = addr & SEEN;                 // "&" is AND, bit by bit: a bit that is 0 in SEEN reads as 0
  wire rom_s  = (a[31:12] == 20'h00000);
  wire uart_s = (a[31:12] == 20'h10000);
  wire gpio_s = (a[31:12] == 20'h10001);
  wire sram_s = (a[31:16] == 16'h8000);
  wire none_s = !(rom_s | uart_s | gpio_s | sram_s);

  // The five selects of each decoder as one row of bits, and the name of the owner.
  wire [4:0] full  = {none_f, sram_f, gpio_f, uart_f, rom_f};
  wire [4:0] short = {none_s, sram_s, gpio_s, uart_s, rom_s};
  function string owner(input logic [4:0] sel);    // a function turns the bits into a name, for printing
    case (sel)
      5'b00001: return "ROM";
      5'b00010: return "UART0";
      5'b00100: return "GPIO";
      5'b01000: return "SRAM";
      5'b10000: return "nobody";
      default:  return "MORE THAN ONE";
    endcase
  endfunction

  task look(input logic [31:0] address);
    addr = address;
    #1;                                        // "#1" is a tiny delay: let the comparisons settle
    $display("addr=%h full=%b %s | short=%b %s%s", addr, full, owner(full), short, owner(short),
             (short != full) ? "  <- the decoders disagree" : "");
  endtask

  int aliases_full = 0, aliases_short = 0;     // counters for the search below
  initial begin
    look(32'h1000_1004);
    look(32'h1000_3004);
    // The search for aliases: start from a GPIO address and flip one bit above the offset at a time.
    // GPIO must stop answering every time. A decoder that still says GPIO has an alias there.
    for (int b = 12; b < 32; b++) begin
      addr = 32'h1000_1004 ^ (32'h1 << b);     // "^" flips bit b; "<<" moves the 1 to position b
      #1;
      if (gpio_f) aliases_full++;
      if (gpio_s) begin
        aliases_short++;
        $display("flip bit %0d: addr=%h still selects GPIO in the short decoder", b, addr);
      end
    end
    $display("aliases of GPIO found: full=%0d short=%0d", aliases_full, aliases_short);
  end
endmodule
ConceptA region holds 0x1000 bytes. Which address bits does its decoder compare?
ConceptWhy is at most one comparison true for any address?
ConceptWhat causes an alias?
ConceptA full decoder is tested with every address that has an owner, and passes. What is still unknown?

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.

Practice

Report a problem with this page