APB, a Few Signals at a Time

Rename the wires of the toy bus and split one of them in two. The result is APB, the real bus built for device registers.

You Will Be Able To

  • Map every wire of the toy bus onto an APB signal.
  • Tell the setup phase from the access phase, and find the edge where a transfer happens.
  • Walk the three APB states, and say what APB is designed for.
Lesson
S0.4
Time
25 min
Runs on
Your browser
Check
5 questions

The toy bus of S0.3 has no name outside this course. Real chips need a bus that every design team agrees on, in every company. The simplest such bus is called APB, and you already know almost all of it.

Before You Start

You need S0.3: manager and subordinate, valid and ready, the transfer edge, wait states. You also need simple state machines.

Five questions this page answers
Question
1Which APB signal matches each wire of the toy bus?
2What are the setup phase and the access phase?
3On which clock edge does an APB transfer happen?
4What are the three APB states, and what moves the bus between them?
5What is APB designed for, and what does it give up?

The Idea: The Toy Bus with One Change

APB stands for Advanced Peripheral Bus. It is a public standard from the company Arm. Arm publishes a family of bus standards under the name AMBA, and APB is one of them. Its job is narrow: it lets a chip reach the registers of its small devices.

APB is the toy bus with one change. On the toy bus, a transfer could happen on the very first edge after the request appeared. On APB it never can. Every transfer starts with one cycle, the setup phase, in which the request is shown and nothing happens. Only then comes the access phase, where the device may answer.

Why add a cycle in which nothing happens? The reason is history, and it is told further down, under "Why It Is Built This Way". For now, take the two phases as the shape of every APB transfer.

New Words

New words on this page
WordMeaning
APBAdvanced Peripheral Bus: a standard bus for reaching device registers.
RequesterAPB's word for the manager: the side that starts transfers.
CompleterAPB's word for the subordinate: the device that answers.
Setup phaseThe first cycle of every transfer. The request is shown, and nothing can happen yet. It is not counted as a wait state.
Access phaseThe cycles after setup, in which the completer may answer. It ends at the edge where the completer says done.

Every APB signal name starts with P. Here is the toy bus beside APB.

From the toy bus to APB
Toy busAPBWhat changed
the clockPCLKonly the name
addrPADDRthe name, and the width: up to 32 bits (8 in this lab)
writePWRITEonly the name: 1 = write, 0 = read
wdataPWDATAonly the name
rdataPRDATAonly the name
readyPREADYsame job, but it counts only in the access phase
validPSEL and PENABLEsplit in two: see below

The one real change is the last row. PSEL means "you are selected, and a transfer is required". It is the part of valid that says a transfer is wanted, and it is high for the whole transfer. The requester side drives one PSEL wire for each completer, and its decoder chooses which one to raise. Inside the chosen completer, PADDR then picks the register. PENABLE is the part of valid that says the setup cycle is over. It is 0 in the setup phase and 1 in the access phase.

One Picture: Setup, Then Access

One APB Write: Setup, Then AccessThe requester writes 9 to the lamp register at address 14. This completer answers at once. Step through the clock edges.
  • Manager
  • Subordinate
PSELPENABLEPADDRPWRITEPWDATAPREADYPRDATARequesterasks: here, test codeCompleteranswers: here, the lamp register
PSEL→ CompleterPENABLE→ CompleterPADDR→ CompleterPWRITE→ CompleterPWDATA→ CompleterPREADY→ RequesterPRDATA→ RequesterRequesterasks: here, test codeCompleteranswers: here, the lamp register

Five groups of wires run from the requester to the completer. Two run back.

0123clkpselpenablepreadypwritepaddr014pwdata09prdatalamps09
Scroll horizontally ↔
  1. Cycle 0

    Idle. PSEL is 0, so no transfer is in progress.

  2. Cycle 1

    Setup phase. PSEL is 1 and PENABLE is 0. The address, the direction and the data are on the wires. This edge can never be the transfer.

  3. Cycle 2

    Access phase. PSEL, PENABLE and PREADY are all 1. This is the edge where the write happens.

  4. Cycle 3

    Idle again. The lamps now show 9.

Recorded from the lab. Each column is what one rising clock edge sees. The lamps row is the register just before that edge.

Compare this with the toy bus. There, a completer that answers at once would finish on the first edge. Here the same completer finishes on the second edge. The setup phase costs one cycle on every transfer.

The Smallest Example, Worked by Hand: A Read with a Wait

Now the requester reads the lamps back. This time the lamp register needs one extra access cycle. Apply one test to each edge: are PSEL, PENABLE and PREADY all 1?

A read of address 14, edge by edge
EdgePSELPENABLEPREADYPhaseWhat this edge does
e4100setupnothing: setup is never the transfer
e5110accessnothing: a wait state
e6111accessthe read happens: the requester takes 9
e7000idlenothing: no transfer

The read took three edges: setup, one wait state, and the transfer. With no wait state it would take two. An APB transfer can never take fewer than two.

The Rules

  1. Rule 1: Every transfer starts with a setup phase of exactly one cycle.

    PSEL is 1 and PENABLE is 0. PADDR and PWRITE are on the wires, and PWDATA too for a write.

  2. Rule 2: The access phase follows at once.

    PENABLE becomes 1. PSEL stays 1.

  3. Rule 3: The transfer happens at the rising edge where PSEL, PENABLE and PREADY are all 1.

    A write changes the register there. A read is taken there.

  4. Rule 4: PREADY at 0 in the access phase means wait.

    The requester holds PSEL, PENABLE, PADDR, PWRITE and PWDATA unchanged until the transfer happens.

  5. Rule 5: PREADY means nothing outside the access phase.

    A completer that always finishes in two cycles may simply hold PREADY at 1 for ever.

These five rules make a state machine with three states. The requester runs it. Its first input, "more", is not a wire. It stands for the requester wanting to start another transfer. Step the machine with the inputs shown, or type your own.

The Three Operating States of APB
The Three Operating States of APB000110110001101100100111IDLE00IDLESETUP10SETUPACCESS11ACCESS
  1. IDLE · IDLE00
    • ↓00 → IDLE
    • ↓01 → IDLE
    • ↓10 → SETUP
    • ↓11 → SETUP
  2. SETUP · SETUP10
    • ↓00 → ACCESS
    • ↓01 → ACCESS
    • ↓10 → ACCESS
    • ↓11 → ACCESS
  3. ACCESS · ACCESS11
    • ↓00 → ACCESS
    • ↓10 → ACCESS
    • ↓01 → IDLE
    • ↓11 → SETUP
StateInputNextOutput in state
IDLE00IDLE00
IDLE01IDLE00
IDLE10SETUP00
IDLE11SETUP00
SETUP00ACCESS10
SETUP01ACCESS10
SETUP10ACCESS10
SETUP11ACCESS10
ACCESS00ACCESS11
ACCESS10ACCESS11
ACCESS01IDLE11
ACCESS11SETUP11

Current state: IDLE · Output: 00

Sampled input, state and output waveformStepState0IDLE1SETUP2ACCESS3IDLE4SETUP5ACCESS6ACCESS7SETUP8ACCESS9IDLEmore—100100100pready—001000101psel0110111110penable0010011010
Scroll horizontally ↔
Sampled waveform: Moore output after each edge; Mealy output on the taken arc.
Step0123456789
Input—100001100000110001
StateIDLESETUPACCESSIDLESETUPACCESSACCESSSETUPACCESSIDLE
Output00101100101111101100

  1. Step 0. The bus rests in IDLE. PSEL and PENABLE are both 0.
  2. Step 1. A transfer is wanted, so the next state is SETUP: PSEL is 1 and PENABLE is 0.
  3. Step 2. SETUP lasts exactly one cycle. Whatever the inputs, the next state is ACCESS: PSEL and PENABLE are both 1.
  4. Step 3. In ACCESS, PREADY was 1 and nothing more was wanted. The transfer happened and the bus went back to IDLE.
  5. Step 6. A second transfer reached ACCESS, but PREADY was 0. The bus stays in ACCESS. This is a wait state.
  6. Step 7. PREADY was 1 and another transfer was wanted. The bus goes straight from ACCESS to SETUP, with no IDLE in between.
  7. Step 9. The third transfer ends, nothing more is wanted, and the bus rests in IDLE.

Inputs are written as two bits: "more" (another transfer is wanted) and then PREADY. Outputs are PSEL and then PENABLE.

Sources for These Rules

Arm AMBA APB Protocol Specification, document IHI 0024, issue E. Purpose and the two-cycle minimum: section 1.1. Signals: section 2.1. Write and read transfers and wait states: sections 3.1 and 3.3. The three states: section 4.1.

The history of PENABLE, told further down this page, comes from an early APB specification. It is the Arm AMBA Specification revision 2.0 (document IHI 0011), sections 2.4 and 5.5.2. It calls PENABLE the APB strobe, used to time all accesses. It let a device capture write data on the rising edge of PENABLE itself.

The current specification states what APB is for: low cost and low complexity. It gives no reason for the setup cycle. The history told on this page is our own reading of the two documents.

The first versions of APB had no PREADY at all, so every transfer took exactly two cycles. PREADY arrived with the third version of AMBA (document IHI 0024, "APB Revisions").

Now Run It

The lab at the end of this page puts the lamp register of S0.3 on APB. As before, the requester is test code: a bus-functional model. Each printed row ends with the phase.

The task apb_write walks the three states. Its first line is SETUP. The line that raises penable enters ACCESS. Its last line leaves ACCESS: for IDLE, or straight for another SETUP if the next task starts at once.

This lab has one completer with one register, so the lamp register does not look at PADDR. The address 14 is carried only to match S0.3.

Printed by the lab at each rising clock edge. The phase at the right is added by the test code.
e0 psel=0 penable=0 pready=1 count=0 pwrite=0 paddr=0 pwdata=0 prdata=x lamps=0  idle
e1 psel=1 penable=0 pready=1 count=0 pwrite=1 paddr=14 pwdata=9 prdata=x lamps=0  setup
e2 psel=1 penable=1 pready=1 count=0 pwrite=1 paddr=14 pwdata=9 prdata=x lamps=0  access  <- transfer
e3 psel=0 penable=0 pready=1 count=0 pwrite=1 paddr=14 pwdata=9 prdata=x lamps=9  idle
e4 psel=1 penable=0 pready=0 count=0 pwrite=0 paddr=14 pwdata=9 prdata=9 lamps=9  setup
e5 psel=1 penable=1 pready=0 count=0 pwrite=0 paddr=14 pwdata=9 prdata=9 lamps=9  access, wait
e6 psel=1 penable=1 pready=1 count=1 pwrite=0 paddr=14 pwdata=9 prdata=9 lamps=9  access  <- transfer
requester read 9
e7 psel=0 penable=0 pready=0 count=0 pwrite=0 paddr=14 pwdata=9 prdata=x lamps=9  idle

The count column is the lamp register counting its own wait states, as in S0.3. Rule 3 at work: the two transfers are at e2 and e6. No other edge has PSEL, PENABLE and PREADY all at 1. Rule 5 at work: PREADY reads 1 at e0 and e3 while the bus is idle, and that means nothing.

Look at e4. PRDATA already shows 9 in the setup phase, two edges before the read happens. This lamp register drives PRDATA whenever it is selected for a read, which APB allows. It is the trap of S0.3 again. The value is early, and nothing promises that it will be early next time.

PWDATA still shows 9 in the read rows. That is a leftover from the write. On a read nobody looks at PWDATA, so it does no harm.

Why It Is Built This Way

So why does every transfer start with a cycle in which nothing happens? The answer is history. APB was first built around a strobe. First the request is set up: the select, the address and the data are placed on the wires and held still. Then one wire, PENABLE, rises to say "now". That rise is the strobe.

An early APB device could use the strobe directly. It used PENABLE as the clock of its register, in place of PCLK. So it captured the write data at the moment PENABLE rose. That works only if every other wire has already stopped changing when the strobe rises. The setup cycle guarantees it.

Devices today clock their registers with PCLK, like the lamp register in this lesson. For them the setup cycle buys little. It stays because every APB device and every APB requester agrees on it. APB keeps one fixed, simple shape, and pays one cycle per transfer for it.

Design choiceThe alternativeWhat the choice buys, and costs
Set up first, then say "now"Allow a transfer on the first edgeThe earliest devices could capture data with the strobe alone. Today it buys a fixed shape, and costs one cycle on every transfer.
One transfer at a time, start to finishStart the next request earlyNo device has to remember an earlier request. It costs speed.
PREADY may be held at 1Every device must drive a handshakeThe smallest device needs no handshake logic at all.

So APB trades speed for simplicity, every time. That is the right trade for a register that software touches now and then. It is the wrong trade for memory, which the processor uses in almost every cycle. The next lesson builds the bus that makes the opposite trade.

At Scale

This page1 completer
A real chipdozens of completers on one APB

This section looks ahead. Real APB carries a few more wires than this page, and a real chip puts many completers on one APB.

  1. One PSEL per completer

    With many completers, the decoder of S0.2 raises the PSEL of one of them. All the other wires are shared. A multiplexer returns that one's PREADY and PRDATA.

  2. An error wire

    A completer can report that a transfer failed by raising a signal named PSLVERR on the transfer edge. The lesson Errors: PSLVERR covers it.

  3. The full protocol

    The Protocols track teaches APB signal by signal, starting at APB in one picture.

  4. Who the requester really is

    In S0.3 the manager was the processor itself. A real chip puts one block in between. The processor speaks a faster bus, and a block called a bridge passes each transfer on to APB. The bridge is the APB requester. The next lesson explains why.

Try It

Run the lab. Name the phase of every row before you read it off the right-hand column.

Set WAITS to 3 before the write. Predict the edge where the write happens.

Remove the quiet cycle between the write and the read. Predict which phase follows the write's access phase.

Break rule 1: in apb_write, set penable to 1 at once. Find every edge at which the lamp register now takes the value.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: e0 psel=0 penable=0 pready=1 count=0 pwrite=0 paddr=0 pwdata=0 prdata=x lamps=0 idle e1 psel=1 penable=0 pready=1 count=0 pwrite=1 paddr=14 pwdata=9 prdata=x lamps=0 setup e2 psel=1 penable=1 pready=1 count=0 pwrite=1 paddr=14 pwdata=9 prdata=x lamps=0 access <- transfer e3 psel=0 penable=0 pready=1 count=0 pwrite=1 paddr=14 pwdata=9 prdata=x lamps=9 idle e4 psel=1 penable=0 pready=0 count=0 pwrite=0 paddr=14 pwdata=9 prdata=9 lamps=9 setup e5 psel=1 penable=1 pready=0 count=0 pwrite=0 paddr=14 pwdata=9 prdata=9 lamps=9 access, wait e6 psel=1 penable=1 pready=1 count=1 pwrite=0 paddr=14 pwdata=9 prdata=9 lamps=9 access <- transfer requester read 9 e7 psel=0 penable=0 pready=0 count=0 pwrite=0 paddr=14 pwdata=9 prdata=x lamps=9 idle

Check Yourself

Predict the outputThe requester writes 6 with no wait state. With no quiet cycle, it then reads the lamps back, and the read needs two wait states. The last line prints the value read and the edge where the read happened. What is that line?
// The lamp register of S0.3, now on APB. The requester is test code: two tasks.
module tb;
  logic pclk = 0;
  always #5 pclk = ~pclk;

  // Driven by the requester (the asking side).
  logic       psel    = 0;   // "you are selected: a transfer is required"
  logic       penable = 0;   // 0 in the setup phase, 1 in the access phase
  logic [7:0] paddr   = 0;   // which address
  logic       pwrite  = 0;   // 1 = write, 0 = read
  logic [7:0] pwdata  = 0;   // the value to write
  // Driven by the completer (the answering side).
  logic       pready;        // judged only in the access phase: 1 = done
  logic [7:0] prdata;        // the value read

  // The completer: the lamp register. It needs WAITS extra access cycles.
  int         WAITS = 0;
  int         count = 0;
  logic [7:0] lamps = 0;
  assign pready = (count == WAITS);
  assign prdata = (psel && !pwrite) ? lamps : 8'hxx;   // 8'hxx: all eight bits x (unknown) unless a read is in progress
  // "<=" updates a register just after the clock edge, as a real flip-flop does.
  always @(posedge pclk) begin
    if (psel && penable && !pready) count <= count + 1;   // an access cycle that must wait
    else                            count <= 0;
    if (psel && penable && pready && pwrite) lamps <= pwdata;   // the write happens on this edge only
  end

  // A task is a named piece of test code. apb_write(14, 9) runs these lines with a = 14, d = 9.
  task apb_write(input logic [7:0] a, input logic [7:0] d);
    psel = 1; penable = 0; paddr = a; pwrite = 1; pwdata = d;   // setup phase: one cycle, always
    @(posedge pclk);
    #1 penable = 1;                                             // access phase begins
    @(posedge pclk);
    while (!pready) @(posedge pclk);                            // stay in access until an edge sees pready at 1
    #1 psel = 0; penable = 0;
  endtask
  // apb_read hands the value it read back through d.
  task apb_read(input logic [7:0] a, output logic [7:0] d);
    psel = 1; penable = 0; paddr = a; pwrite = 0;
    @(posedge pclk);
    #1 penable = 1;
    @(posedge pclk);
    while (!pready) @(posedge pclk);
    d = prdata;                                                 // take the value at that edge, not before
    #1 psel = 0; penable = 0;
  endtask

  // Print what each rising clock edge sees, and name the phase.
  int    edge_no = 0;                  // counts the rising edges
  int    last_transfer = 0;            // the latest edge where a transfer happened
  string phase;                        // a text variable, for printing only
  always @(posedge pclk) begin
    if (!psel)         phase = "idle";
    else if (!penable) phase = "setup";
    else if (!pready)  phase = "access, wait";
    else begin         phase = "access  <- transfer"; last_transfer = edge_no; end
    $display("e%0d psel=%b penable=%b pready=%b count=%0d pwrite=%b paddr=%0d pwdata=%0d prdata=%0d lamps=%0d  %s",
      edge_no, psel, penable, pready, count, pwrite, paddr, pwdata, prdata, lamps, phase);
    edge_no++;
  end

  logic [7:0] got;
  initial begin
    @(posedge pclk); #1;
    apb_write(14, 6);
    WAITS = 2;
    apb_read(14, got);
    @(posedge pclk); #1;
    $display("read %0d at edge %0d", got, last_transfer);
    $finish;                           // $finish ends the simulation
  end
endmodule
ConceptPSEL is 1 and PENABLE is 0 at a clock edge. Which phase is the bus in?
ConceptWhich toy-bus wire did APB split into two signals?
ConceptIn the ACCESS state, PREADY is 1 and another transfer is wanted. Which state comes next?
ConceptWhat does APB give up to stay simple?

Common Mistakes

Wrong Let a device act whenever PSEL is 1.

PSEL is 1 in the setup phase and in every wait state too. The device would act several times.

Fix Act only at the edge where PSEL, PENABLE and PREADY are all 1.

Wrong Read PREADY in the setup phase.

PREADY has no meaning until PENABLE is 1.

Fix Judge PREADY only in the access phase.

Wrong Expect an APB transfer to finish in one cycle.

The setup phase always takes a cycle of its own.

Fix Count at least two cycles for every transfer, plus one for each wait state.

Remember

  • APB is the toy bus with real names; valid becomes PSEL and PENABLE.
  • Every transfer has a one-cycle setup phase (PSEL 1, PENABLE 0), then an access phase (both 1).
  • The transfer happens at the edge where PSEL, PENABLE and PREADY are all 1.
  • The bus has three states: IDLE, SETUP and ACCESS; PREADY at 0 holds it in ACCESS.
  • APB is built for device registers: simple and cheap, and never faster than two cycles per transfer.

Practice

Report a problem with this page