A Bus in Six Wires

A bus is shared wires plus rules about when a value counts. Build the smallest one, and find the single clock edge where a transfer happens.

You Will Be Able To

  • Say what a bus is, and name the two sides of a transfer.
  • Find the clock edge where a transfer happens, with and without waiting.
  • Explain why the data wires alone never show that a transfer happened.
Lesson
S0.3
Time
25 min
Runs on
Your browser
Check
5 questions

In S0.2 the processor put an address on some wires and the lamps lit at once. Real devices are not that quick. A slow device needs time, and the processor must know when an answer is real. The wires need rules.

Before You Start

You need S0.2: addresses, device registers, the decoder. You also need one fact about flip-flops: they take their input at the rising edge of the clock.

Five questions this page answers
Question
1What is a bus, and what are the two sides of a transfer called?
2On which clock edge does a transfer happen?
3What must the asking side do while it waits, and why?
4Why is a value on the data wires not proof that a write or a read has happened?
5Why does a chip use one agreed bus, and not custom wires for every device?

The Idea: Wires Plus Rules

A bus is a set of shared wires together with a set of rules. The wires carry an address and data, as in S0.2. The rules answer the question S0.2 skipped: at what moment does the value on the wires count?

Two extra wires carry the answer. One read or one write is called a transfer. The side that asks raises one wire to say "I want a transfer". The side that answers raises another to say "done". The transfer happens on the clock edge where both are high. Before that edge nothing has happened, whatever the data wires show.

This pair of wires is called a handshake. It exists because devices differ in speed. Memory may answer at once. A slow device may need several clock cycles, because it must finish earlier work or fetch the value from somewhere slower. With a handshake, a slow device keeps its "done" wire low, and the asker waits. Without one, the processor would need to know the speed of every device in advance.

New Words

New words on this page
WordMeaning
BusShared wires plus the rules for using them.
TransferOne read or one write, from the request to the moment it happens.
ManagerThe side that starts a transfer. So far, that is the processor. Older texts say master.
SubordinateThe side that answers: memory or a device. Older texts say slave.
HandshakeTwo wires, one from each side. A transfer happens only when both are high at a clock edge.
Clock cycleThe time from one rising clock edge to the next. Wires are set during a cycle and judged at the rising edge that ends it.
Wait stateA clock cycle in which the manager is asking and the subordinate is not yet done.
SettleA wire needs a little time to reach a new value, and each gate adds more. The value has settled when it has stopped changing.

Our toy bus has six groups of wires and a clock. The manager drives four of them. The subordinate drives two.

The six wires of the toy bus
WireDriven byMeaning
validmanager1 = I am asking for a transfer
addrmanagerwhich address
writemanager1 = write, 0 = read
wdatamanagerthe value to write
readysubordinate1 = done: this is the edge
rdatasubordinatethe value read; correct at the transfer edge of a read, and no promise at any other time

One Picture: The Six Wires and One Write

The Six Wires, and One WriteThe manager writes 9 to the lamps. The lamp register needs two extra cycles. Step through the clock edges.
  • Manager
  • Subordinate
validaddrwritewdatareadyrdataManagerasks: here, the processorSubordinateanswers: here, the lamp register
valid→ Subordinateaddr→ Subordinatewrite→ Subordinatewdata→ Subordinateready→ Managerrdata→ ManagerManagerasks: here, the processorSubordinateanswers: here, the lamp register

Four groups of wires run from the manager to the subordinate. Two run back.

01234clkvalidreadywriteaddr014wdata090rdatalamps09
Scroll horizontally ↔
  1. Cycle 0

    Nothing is asked. valid is 0, so the other wires mean nothing.

  2. Cycle 1

    The manager has raised valid, with addr 14, write 1 and wdata 9. The subordinate keeps ready at 0: it is not done. This edge changes nothing.

  3. Cycle 2

    Still waiting. The manager holds every wire exactly as it was. The lamps still show 0.

  4. Cycle 3

    ready is 1 and valid is 1. This is the edge where the write happens. The lamp register takes 9 here.

  5. Cycle 4

    The manager has lowered valid, so write and addr are leftovers that mean nothing. The lamps now show 9. The value 9 sat on wdata for three edges and was written once.

Recorded from the lab. Columns 0 to 4 are the edges e0 to e4 of the lab output. Each shows what that rising clock edge sees. The lamps row is the register just before the edge.

Look at wdata across the picture. It carries 9 at three edges in a row. Only the last of the three is the write. The handshake wires tell you which edge that is. The data wires cannot.

The Smallest Example, Worked by Hand: A Read

Now the manager reads the lamps back. The lamp register again needs two extra cycles. Apply one test to each edge: are valid and ready both 1?

A read of address 14, edge by edge
EdgevalidreadyrdataBoth 1?What this edge does
e510x (unknown)nonothing: a wait state
e610x (unknown)nonothing: a wait state
e7119yesthe read happens: the manager takes 9
e800x (unknown)nonothing: no request

The read took three edges: two wait states, then the transfer. The manager took rdata at e7 only. At e5 and e6 the subordinate had not promised anything about rdata.

The Rules

  1. Rule 1: The manager starts a transfer by raising valid.

    In the same cycle it drives addr and write. For a write it also drives wdata. It never waits for ready before raising valid: each side would wait for the other for ever.

  2. Rule 2: The subordinate raises ready when it can finish.

    For a read, rdata must be correct in that same cycle. While valid is 0, ready means nothing.

  3. Rule 3: The transfer happens at the rising clock edge where valid and ready are both 1.

    At that edge, and at no other. A write changes the register there. A read is taken there.

  4. Rule 4: While it waits, the manager holds every wire still.

    If valid is 1 and ready is 0, the manager keeps valid at 1 and may not change addr, write or wdata. Each such cycle is one wait state.

  5. Rule 5: One transfer at a time.

    There is one set of wires, so it can carry one request. The manager starts the next transfer only after the current one has happened.

Where These Rules Come From

The toy bus is our own. It is not a standard. It was built to show what the real buses of the next lessons have in common.

Its valid and ready pair is a handshake that real buses use too. Another track of this site, Protocols, studies it in depth in its lesson Handshakes.

Now Run It

The lab at the end of this page is this bus in SystemVerilog. The subordinate is the lamp register. The manager is not a processor. It is test code: two short routines, which SystemVerilog calls tasks, that drive the wires the way a processor would. Engineers call such code a bus-functional model. Later labs use one wherever the real processor is not needed.

Printed by the lab at each rising clock edge. The marks at the right are added by the test code.
e0 valid=0 ready=0 count=0 write=0 addr=0 wdata=0 rdata=x lamps=0
e1 valid=1 ready=0 count=0 write=1 addr=14 wdata=9 rdata=x lamps=0  (wait)
e2 valid=1 ready=0 count=1 write=1 addr=14 wdata=9 rdata=x lamps=0  (wait)
e3 valid=1 ready=1 count=2 write=1 addr=14 wdata=9 rdata=x lamps=0  <- transfer
e4 valid=0 ready=0 count=0 write=1 addr=14 wdata=0 rdata=x lamps=9
e5 valid=1 ready=0 count=0 write=0 addr=14 wdata=0 rdata=x lamps=9  (wait)
e6 valid=1 ready=0 count=1 write=0 addr=14 wdata=0 rdata=x lamps=9  (wait)
e7 valid=1 ready=1 count=2 write=0 addr=14 wdata=0 rdata=9 lamps=9  <- transfer
manager read 9
e8 valid=0 ready=0 count=0 write=0 addr=14 wdata=0 rdata=x lamps=9

Rule 3 at work: the two rows marked "transfer" are e3 and e7, the only edges with valid=1 and ready=1. Rule 4 at work: rows e1 and e2 are identical to e3 in every manager wire.

The count column is the lamp register counting its own wait states. This register was set to need two extra cycles. Its ready wire comes from plain gates: ready is 1 whenever valid is 1 and count is 2. So ready rises during the cycle in which count becomes 2, and e3 sees both.

One more thing to see. Each row is printed at the edge, before any register has changed. A flip-flop takes its input at the edge and shows the new value just after it. That is why e3 shows count=2, ready=1 and lamps=0 together, and why the lamps read 9 only at e4.

Look at the rdata column too. It prints x, for unknown, in every row but one. This subordinate drives a real value only at e7, the edge of the read.

A subordinate that needs no extra cycle keeps ready at 1 as soon as it is asked. Then the first edge that sees valid is already the transfer, with no wait state. Here is the same write with the lamp register set that way.

The same lab with WAITS set to 0. The write happens at e1.
e0 valid=0 ready=0 count=0 write=0 addr=0 wdata=0 rdata=x lamps=0
e1 valid=1 ready=1 count=0 write=1 addr=14 wdata=9 rdata=x lamps=0  <- transfer
e2 valid=0 ready=0 count=0 write=1 addr=14 wdata=0 rdata=x lamps=9

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys
The subordinate says when it is doneEvery transfer takes a fixed number of cyclesFast devices answer fast. Slow devices take the time they need.
Everything is judged at a clock edgeBlocks react whenever a wire changesEvery block agrees on one instant. Wires have the rest of the cycle to settle.
The manager holds still while waitingThe manager may change its requestA slow subordinate may look at the address only in its last cycle, and must still find it there.
One agreed bus for all blocksCustom wires for each deviceA block designed once fits any chip that uses the same bus.

The last row is why standard buses exist. Three of them fill the rest of this track. APB, the Advanced Peripheral Bus, serves small device registers. AHB, the Advanced High-performance Bus, serves memory and fast devices. AXI, the Advanced eXtensible Interface, serves the largest chips. The next two lessons build APB and AHB from this toy bus, a few wires at a time.

The Trap: Trusting the Data Wires

An engineer looks only at wdata in the waveform, sees 9 at e1, and reports that the write happened at e1. The lamps disagree. They still show 0 at e2 and at e3.

Rule 4 has a trap of its own. Picture a device that is still finishing its last job, and looks at addr only in its final cycle. If the manager had changed addr at e2, the device would answer for the wrong address.

The same mistake costs more on a read. Suppose a subordinate happens to show the right value early, while ready is still 0. A manager that takes it early will work in simulation today. It will fail on the day the device answers one cycle later. Take data only on the handshake edge.

At Scale

This page1 manager, 1 subordinate
A real chipseveral managers, dozens of subordinates

This section looks ahead. Real buses keep the idea of this page and add wires for speed and safety.

  1. A subordinate that never answers

    If ready never rises, the manager waits for ever and the whole chip stops responding. Real systems add a rule or a timer to end such a transfer. Lessons S4.3 and S4.15 return to this.

  2. Many subordinates

    With many subordinates, the decoder of S0.2 sends valid to one of them, and a multiplexer returns that one's ready and rdata.

  3. More speed

    On this bus the wires sit idle while the subordinate works. Faster buses start the next request before the current answer arrives. Lesson S0.5 shows how.

Try It

Run the lab. Find the two transfer edges and count the wait states before each.

Set WAITS to 0. Predict the edge of the write before you run it.

Set WAITS to 5. Count how many edges see valid at 1 for the write.

Make the manager cheat: in bus_read, take rdata one edge after valid rises. See what it reads.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: e0 valid=0 ready=0 count=0 write=0 addr=0 wdata=0 rdata=x lamps=0 e1 valid=1 ready=0 count=0 write=1 addr=14 wdata=9 rdata=x lamps=0 (wait) e2 valid=1 ready=0 count=1 write=1 addr=14 wdata=9 rdata=x lamps=0 (wait) e3 valid=1 ready=1 count=2 write=1 addr=14 wdata=9 rdata=x lamps=0 <- transfer e4 valid=0 ready=0 count=0 write=1 addr=14 wdata=0 rdata=x lamps=9 e5 valid=1 ready=0 count=0 write=0 addr=14 wdata=0 rdata=x lamps=9 (wait) e6 valid=1 ready=0 count=1 write=0 addr=14 wdata=0 rdata=x lamps=9 (wait) e7 valid=1 ready=1 count=2 write=0 addr=14 wdata=0 rdata=9 lamps=9 <- transfer manager read 9 e8 valid=0 ready=0 count=0 write=0 addr=14 wdata=0 rdata=x lamps=9

Check Yourself

Predict the outputWAITS is 1. The manager writes 3 to the lamps, rests for one cycle, and reads the lamps back. The last line prints the value read and the edge where the read happened. What is that line?
// A toy bus: six groups of wires and one clock. One side asks, one side answers.
module tb;
  logic clk = 0;
  always #5 clk = ~clk;

  // Driven by the manager (the side that asks).
  logic       valid = 0;   // "I am asking for a transfer"
  logic [3:0] addr  = 0;   // which address
  logic       write = 0;   // 1 = write, 0 = read
  logic [7:0] wdata = 0;   // the value to write
  // Driven by the subordinate (the side that answers).
  logic       ready;       // "done: this is the edge"
  logic [7:0] rdata;       // the value read

  // The subordinate: a lamp register that needs WAITS extra cycles for every transfer.
  int         WAITS = 1;
  int         count = 0;
  logic [7:0] lamps = 0;
  // "assign" describes plain gates: ready follows valid and count within the same cycle.
  assign ready = valid && (count == WAITS);
  assign rdata = (ready && !write) ? lamps : 8'hxx;   // 8'hxx: all eight bits x (unknown) until a read is done
  // "<=" updates a register just after the clock edge, as a real flip-flop does.
  always @(posedge clk) begin
    if (valid && !ready) count <= count + 1;  // still working
    else                 count <= 0;
    if (valid && ready && write) lamps <= wdata;   // the write happens on this edge only
  end

  // The manager: raise valid, hold everything still, leave at the edge where ready is 1.
  // A task is a named piece of test code. Calling bus_write(14, 9) runs these lines
  // with a = 14 and d = 9.
  task bus_write(input logic [3:0] a, input logic [7:0] d);
    valid = 1; addr = a; write = 1; wdata = d;
    @(posedge clk);
    while (!ready) @(posedge clk);       // keep waiting, one edge at a time, until an edge sees ready at 1
    #1 valid = 0; wdata = 0;             // "#1" is a tiny delay: just after that edge, stop asking
  endtask
  // bus_read hands the value it read back through d.
  task bus_read(input logic [3:0] a, output logic [7:0] d);
    valid = 1; addr = a; write = 0;
    @(posedge clk);
    while (!ready) @(posedge clk);       // wait for the handshake edge
    d = rdata;                           // take the value at that edge, not before
    #1 valid = 0;
  endtask

  // Print what each rising clock edge sees.
  int    edge_no = 0;                  // counts the rising edges
  int    last_transfer = 0;            // the number of the latest edge where a transfer happened
  string note;                         // a text variable: a mark printed at the end of a row
  always @(posedge clk) begin
    if (valid && ready) begin note = "  <- transfer"; last_transfer = edge_no; end
    else if (valid)     note = "  (wait)";
    else                note = "";
    $display("e%0d valid=%b ready=%b count=%0d write=%b addr=%0d wdata=%0d rdata=%0d lamps=%0d%s",
      edge_no, valid, ready, count, write, addr, wdata, rdata, lamps, note);
    edge_no++;
  end

  logic [7:0] got;
  initial begin
    @(posedge clk); #1;
    bus_write(14, 3);
    @(posedge clk); #1;               // one quiet cycle
    bus_read(14, got);
    @(posedge clk); #1;
    $display("read %0d at edge %0d", got, last_transfer);
    $finish;
  end
endmodule
ConceptAt a clock edge, valid is 1, ready is 0 and wdata is 9. What has that edge done to the register?
ConceptWhile the manager waits for ready, which of its wires may it change?
Read the waveformA manager reads from a subordinate. At which column does the read happen?
One read
01234validreadyrdatax7x
Scroll horizontally ↔

Columns are numbered 0 to 4 from the left. Each column is one clock edge.

ConceptWhy does a chip use one agreed bus for all its blocks?

Common Mistakes

Wrong Decide that a write happened when wdata first shows the value.

The value sits on wdata through every wait state. Nothing is written until ready is 1.

Fix Find the edge where valid and ready are both 1.

Wrong Take rdata as soon as it looks right.

The subordinate promises rdata only in the cycle where ready is 1.

Fix Take rdata at the handshake edge, and at no other.

Wrong Change the address while waiting.

A slow subordinate may look at the request only in its last cycle.

Fix Hold addr, write and wdata still until the transfer has happened.

Remember

  • A bus is shared wires plus rules; the manager asks and the subordinate answers.
  • A transfer happens at the one rising clock edge where valid and ready are both 1.
  • Each cycle with valid at 1 and ready at 0 is a wait state, and the manager holds every wire still.
  • Data wires show a value long before it counts; only the handshake marks the edge.
  • An agreed bus lets blocks designed apart connect without custom wiring.

Practice

Report a problem with this page