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.
| Question | |
|---|---|
| 1 | What is a bus, and what are the two sides of a transfer called? |
| 2 | On which clock edge does a transfer happen? |
| 3 | What must the asking side do while it waits, and why? |
| 4 | Why is a value on the data wires not proof that a write or a read has happened? |
| 5 | Why 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
| Word | Meaning |
|---|---|
| Bus | Shared wires plus the rules for using them. |
| Transfer | One read or one write, from the request to the moment it happens. |
| Manager | The side that starts a transfer. So far, that is the processor. Older texts say master. |
| Subordinate | The side that answers: memory or a device. Older texts say slave. |
| Handshake | Two wires, one from each side. A transfer happens only when both are high at a clock edge. |
| Clock cycle | The 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 state | A clock cycle in which the manager is asking and the subordinate is not yet done. |
| Settle | A 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.
| Wire | Driven by | Meaning |
|---|---|---|
| valid | manager | 1 = I am asking for a transfer |
| addr | manager | which address |
| write | manager | 1 = write, 0 = read |
| wdata | manager | the value to write |
| ready | subordinate | 1 = done: this is the edge |
| rdata | subordinate | the 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
- Manager
- Subordinate
Four groups of wires run from the manager to the subordinate. Two run back.
- Cycle 0
Nothing is asked. valid is 0, so the other wires mean nothing.
- 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.
- Cycle 2
Still waiting. The manager holds every wire exactly as it was. The lamps still show 0.
- Cycle 3
ready is 1 and valid is 1. This is the edge where the write happens. The lamp register takes 9 here.
- 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?
| Edge | valid | ready | rdata | Both 1? | What this edge does |
|---|---|---|---|---|---|
| e5 | 1 | 0 | x (unknown) | no | nothing: a wait state |
| e6 | 1 | 0 | x (unknown) | no | nothing: a wait state |
| e7 | 1 | 1 | 9 | yes | the read happens: the manager takes 9 |
| e8 | 0 | 0 | x (unknown) | no | nothing: 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
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.
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.
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.
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.
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.
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=9Rule 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.
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=9Why It Is Built This Way
| Design choice | The alternative | What the choice buys |
|---|---|---|
| The subordinate says when it is done | Every transfer takes a fixed number of cycles | Fast devices answer fast. Slow devices take the time they need. |
| Everything is judged at a clock edge | Blocks react whenever a wire changes | Every block agrees on one instant. Wires have the rest of the cycle to settle. |
| The manager holds still while waiting | The manager may change its request | A slow subordinate may look at the address only in its last cycle, and must still find it there. |
| One agreed bus for all blocks | Custom wires for each device | A 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 section looks ahead. Real buses keep the idea of this page and add wires for speed and safety.
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.
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.
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.
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=9Check Yourself
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.