Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bus - The "Highway System" Inside a Computer

Bus -- The "Highway System" Inside a Computer

Components inside a computer constantly need to exchange information. The CPU fetches instructions from memory and reads/writes data -- how is this information transferred? The answer is -- the bus (Bus).

A bus is a set of common communication lines connecting devices such as the CPU, memory, hard disk, and graphics card -- like a city's road system, with main arteries and side roads, each road having a clear purpose.

This lecture will help you understand the division of labor among the three buses, and use Python to simulate a complete memory read operation.


Road-system analogy: three kinds of roads, three kinds of uses

Imagine you are delivering a package in a big city. The courier needs to know three things: where to deliver, what to deliver, and whether the recipient needs to sign for it.

Inside a computer there are three completely analogous kinds of "roads":

Bus type Function Transfer direction Real-life analogy
Address bus (Address Bus) Transfers the memory address to be accessed CPU -> memory (unidirectional) The "house number" on the shipping label -- tells the recipient "I want this address"
Data bus (Data Bus) Transfers the actual data content Bidirectional The "package" in the courier's hands -- the thing itself, which can be sent out or received
Control bus (Control Bus) Transfers control signals such as read/write and interrupt CPU <-> all parts (bidirectional) The "sign-off sheet" -- tells the other side whether it is pickup or delivery now, and whether the transfer is complete

The width of the address bus determines how much memory the CPU can access. A 32-bit address bus can access 2^32 = 4GB of memory space; a 64-bit address bus can theoretically access 2^64 bytes (about 16EB), which is why 32-bit operating systems can use at most 4GB of memory.


Complete flow of one memory read operation

Suppose the CPU needs to read a piece of data from memory address 100. The whole process has 4 steps, with the three buses working together:

Step breakdown

  1. CPU places the address: the CPU puts the address value 100 onto the address bus. Memory has now received the house number.
  2. CPU sends a signal: the CPU issues a "read" signal (READ) on the control bus. Memory knows the other side wants to fetch something.
  3. Memory places the data: after receiving the address and read signal, memory finds the data stored at address 100 and puts it onto the data bus.
  4. CPU receives the data: the CPU reads the data from the data bus and stores it in an internal register. Transfer complete.

Timing diagram

The ASCII timing diagram below shows how the bus states change during execution of this instruction:



时间  |   地址总线       控制总线        数据总线

------|------------------------------------------

T0    |   100           IDLE            XXXX       ← CPU 放上地址

T1    |   100           READ            XXXX       ← CPU 发出读信号

T2    |   100           READ            0x2A       ← 内存放上数据

T3    |   XXXX          IDLE            0x2A       ← CPU 取走数据

------|------------------------------------------

结果: CPU 从地址 100 读到了数据 0x2A


Code demo: simulate the bus interaction process with Python

The Python code below fully simulates how the address bus, data bus, and control bus cooperate during one memory read operation.

Example

"""
Bus interaction simulation - one complete memory read operation (runoob demo)

This program simulates the complete process of the CPU reading data from memory via the three buses:

  1. Address bus: the CPU issues the memory address to access
  2. Control bus: the CPU issues read/write control signals
  3. Data bus: memory returns/receives data

Every bus state change prints a detailed log, making it easy to observe the cooperation of the three buses.
"""

import

time

class

AddressBus:

   

"""Address bus - unidirectional: the CPU sends the address to memory"""

   

def

init

(

self

)

:

       

self

.

value

=

None

 

The current address value on the bus

       

self

.

width

=

16

   

16-bit address bus, can address 64KB

   

def

set_address

(

self

,

addr

)

:

       

"""CPU places the address onto the address bus"""

       

self

.

value

=

addr

       

print

(

f

" [Address bus] CPU placed address: {addr} (0x{addr:04X})"

)

       

return

addr

   

def

clear

(

self

)

:

       

"""Clear the bus"""

       

self

.

value

=

None

       

print

(

f

" [Address bus] bus cleared, entering idle state"

)

class

DataBus:

   

"""Data bus - bidirectional: both CPU and memory can send and receive data"""

   

def

init

(

self

)

:

       

self

.

value

=

None

 

The current data value on the bus

       

self

.

width

=

8

   

8-bit data bus, transfers one byte at a time

       

self

.

direction

=

None

 

'TO_CPU' or 'TO_MEMORY'

   

def

send_from_cpu

(

self

,

data

)

:

       

"""CPU sends data to the bus"""

       

self

.

value

=

data

       

self

.

direction

=

'TO_MEMORY'

       

print

(

f

" [Data bus] CPU -> bus, data: {data} (0x{data:02X})"

)

       

return

data

   

def

send_from_memory

(

self

,

data

)

:

       

"""Memory sends data to the bus"""

       

self

.

value

=

data

       

self

.

direction

=

'TO_CPU'

       

print

(

f

" [Data bus] memory -> bus, data: {data} (0x{data:02X})"

)

       

return

data

   

def

read_to_cpu

(

self

)

:

       

"""CPU reads data from the data bus"""

        data

=

self

.

value

       

print

(

f

" [Data bus] CPU reads data from bus: {data} (0x{data:02X})"

)

       

self

.

clear

(

)

       

return

data

   

def

read_to_memory

(

self

)

:

       

"""Memory reads data from the data bus (used in write operations)"""

        data

=

self

.

value

       

print

(

f

" [Data bus] memory reads data from bus: {data} (0x{data:02X})"

)

       

self

.

clear

(

)

       

return

data

   

def

clear

(

self

)

:

       

"""Clear the bus"""

       

self

.

value

=

None

       

self

.

direction

=

None

       

print

(

f

" [Data bus] bus cleared, entering idle state"

)

class

ControlBus:

   

"""Control bus - bidirectional: transfers read/write signals, interrupt signals, etc."""

   

Control signal type

    READ

=

'MEM_READ'

       

Memory read signal

    WRITE

=

'MEM_WRITE'

     

Memory write signal

    IDLE

=

'IDLE'

           

Idle

   

def

init

(

self

)

:

       

self

.

signal

=

self

.

IDLE

   

def

set_signal

(

self

,

signal

)

:

       

"""Set the control signal"""

       

self

.

signal

=

signal

        signal_name

=

{

           

self

.

READ

:

'Memory read (MEM_READ)'

,

           

self

.

WRITE

:

'Memory write (MEM_WRITE)'

,

           

self

.

IDLE

:

'Idle (IDLE)'

       

}

       

print

(

f

" [Control bus] CPU issued signal: {signal_name.get(signal, signal)}"

)

   

def

clear

(

self

)

:

       

"""Clear the signal"""

       

self

.

signal

=

self

.

IDLE

       

print

(

f

" [Control bus] signal cleared, entering idle state"

)

class

SimpleMemory:

   

"""Simplified memory module - a storage space of 256 bytes"""

   

def

init

(

self

,

size

=

256

)

:

       

self

.

size

=

size

       

self

.

data

=

{

}

       

Initialize some demo data

       

for

i

in

range

(

size

)

:

           

self

.

data

[

i

]

=

i %

256

 

Address i stores the value i % 256

       

Place some special values at specific addresses as a demo

       

self

.

data

[

100

]

=

42

       

Address 100: store 42 (0x2A)

       

self

.

data

[

200

]

=

0xAB

     

Address 200: store 0xAB (171)

       

self

.

data

[

50

]

=

ord

(

'R'

)

   

Address 50: store the ASCII code of the letter 'R'

       

self

.

data

[

51

]

=

ord

(

'U'

)

       

self

.

data

[

52

]

=

ord

(

'N'

)

       

self

.

data

[

53

]

=

ord

(

'O'

)

       

self

.

data

[

54

]

=

ord

(

'O'

)

       

self

.

data

[

55

]

=

ord

(

'B'

)

   

def

read

(

self

,

addr

)

:

       

"""Read data from a specified address"""

       

if

0

<=

addr

<

self

.

size

:

           

return

self

.

data

.

get

(

addr

,

0

)

       

else

:

           

raise

ValueError

(

f

"Address {addr} out of memory range (0-{self.size-1})"

)

   

def

write

(

self

,

addr

,

data

)

:

       

"""Write data to a specified address"""

       

if

0

<=

addr

<

self

.

size

:

           

self

.

data

[

addr

]

=

data

       

else

:

           

raise

ValueError

(

f

"Address {addr} out of memory range (0-{self.size-1})"

)

class

CPU:

   

"""Simplified CPU - interacts with memory via buses"""

   

def

init

(

self

,

addr_bus

,

data_bus

,

ctrl_bus

,

memory

)

:

       

self

.

addr_bus

=

addr_bus  

Address bus

       

self

.

data_bus

=

data_bus  

Data bus

       

self

.

ctrl_bus

=

ctrl_bus  

Control bus

       

self

.

memory

=

memory      

Memory

       

CPU internal registers

       

self

.

mdr

=

0

             

Data register (Memory Data Register)

       

self

.

mar

=

0

             

Address register (Memory Address Register)

   

def

memory_read

(

self

,

addr

)

:

       

"""
        Perform one complete memory read operation

        Flow:
        T0: MAR <- addr (internal preparation)
        T1: address bus <- addr, control bus <- READ
        T2: memory reads data, data bus <- data
        T3: MDR <- data bus, all buses cleared

        Return: the data value read
        """

       

print

(

f

"\n{'='*55}"

)

       

print

(

f

"Operation start: CPU reads data from memory address {addr}"

)

       

print

(

f

"{'='*55}"

)

       

T0: CPU internal preparation -- load the address into MAR

       

self

.

mar

=

addr

       

print

(

f

"\n[T0 - CPU internal preparation]"

)

       

print

(

f

" [CPU internal] MAR (memory address register) <- {addr}"

)

       

T1: CPU places the address on the address bus and simultaneously issues a read signal on the control bus

       

print

(

f

"\n[T1 - issue address and read signal]"

)

       

self

.

addr_bus

.

set_address

(

addr

)

       

self

.

ctrl_bus

.

set_signal

(

ControlBus.

READ

)

       

Simulate signal transmission delay

       

time

.

sleep

(

0.001

)

       

T2: memory responds -- finds the data according to the address on the address bus and places it on the data bus

       

print

(

f

"\n[T2 - memory response]"

)

        data

=

self

.

memory

.

read

(

addr

)

       

self

.

data_bus

.

send_from_memory

(

data

)

       

Simulate signal transmission delay

       

time

.

sleep

(

0.001

)

       

T3: CPU reads data from the data bus and stores it in MDR. All buses cleared

       

print

(

f

"\n[T3 - CPU receives data]"

)

       

self

.

mdr

=

self

.

data_bus

.

read_to_cpu

(

)

       

self

.

ctrl_bus

.

clear

(

)

       

self

.

addr_bus

.

clear

(

)

       

Output result

       

print

(

f

"\n{'='*55}"

)

       

print

(

f

"Operation complete: read data {self.mdr} (0x{self.mdr:02X}) from address {addr} (0x{addr:04X})"

)

       

print

(

f

"{'='*55}"

)

       

return

self

.

mdr

   

def

memory_write

(

self

,

addr

,

data

)

:

       

"""
        Perform one complete memory write operation

        Flow:
        T0: MAR ← addr, MDR ← data
        T1: address bus <- addr, data bus <- data, control bus <- WRITE
        T2: memory takes data from the data bus and writes it to address addr
        T3: all buses cleared
        """

       

print

(

f

"\n{'='*55}"

)

       

print

(

f

"Operation start: CPU writes data {data} to memory address {addr}"

)

       

print

(

f

"{'='*55}"

)

       

T0: CPU internal preparation

       

self

.

mar

=

addr

       

self

.

mdr

=

data

       

print

(

f

"\n[T0 - CPU internal preparation]"

)

       

print

(

f

" [CPU internal] MAR <- {addr}, MDR <- {data}"

)

       

T1: CPU places address and data on the bus and issues a write signal

       

print

(

f

"\n[T1 - issue address, data, and write signal]"

)

       

self

.

addr_bus

.

set_address

(

addr

)

       

self

.

data_bus

.

send_from_cpu

(

data

)

       

self

.

ctrl_bus

.

set_signal

(

ControlBus.

WRITE

)

       

time

.

sleep

(

0.001

)

       

T2: memory takes data from the data bus and writes it

       

print

(

f

"\n[T2 - memory write]"

)

        received

=

self

.

data_bus

.

read_to_memory

(

)

       

self

.

memory

.

write

(

addr

,

received

)

       

print

(

f

" [Memory internal] address {addr} (0x{addr:04X}) <- {received} (0x{received:02X})"

)

       

time

.

sleep

(

0.001

)

       

T3: all buses cleared

       

print

(

f

"\n[T3 - clear bus]"

)

       

self

.

ctrl_bus

.

clear

(

)

       

self

.

addr_bus

.

clear

(

)

       

print

(

f

"\n{'='*55}"

)

       

print

(

f

"Operation complete: {data} written to address {addr}"

)

       

print

(

f

"{'='*55}"

)

===== Main program: run the demo =====

if

name

==

"main"

:

   

print

(

"="

60

)

   

print

(

" Bus interaction simulation -- RUNOOB computer organization demo"

)

   

print

(

"="

60

)

   

print

(

)

   

1. Initialize the buses

   

print

(

"[System init] create address bus, data bus, control bus, memory module"

)

    addr_bus

=

AddressBus

(

)

    data_bus

=

DataBus

(

)

    ctrl_bus

=

ControlBus

(

)

    memory

=

SimpleMemory

(

)

   

print

(

)

   

2. View the initial memory state

   

print

(

"[Memory initial state] preview of data at key addresses:"

)

   

for

addr

in

[

50

,

51

,

52

,

53

,

54

,

55

,

100

,

200

]

:

        ch

=

chr

(

memory.

data

[

addr

]

)

if

32

<=

memory.

data

[

addr

]

<

127

else

'?'

print

(

f

" address {addr} (0x{addr:04X}): {memory.data[addr]:3d} (0x{memory.data[addr]:02X}) '{ch}'"

)

   

print

(

)

   

3. Initialize CPU

    cpu

=

CPU

(

addr_bus

,

data_bus

,

ctrl_bus

,

memory

)

   

4. Demo: read data from memory

   

print

(

"\n"

"▼"

60

)

   

print

(

"Demo 1: read data from memory address 100"

)

   

print

(

"▼"

60

)

    val1

=

cpu.

memory_read

(

100

)

   

print

(

"\n"

"▼"

60

)

   

print

(

"Demo 2: read data from memory address 200"

)

   

print

(

"▼"

60

)

    val2

=

cpu.

memory_read

(

200

)

   

print

(

"\n"

"▼"

60

)

   

print

(

"Demo 3: read continuously from memory addresses 50-55 and assemble characters (RUNOOB)"

)

   

print

(

"▼"

60

)

    chars

=

[

]

   

for

addr

in

range

(

50

,

56

)

:

        val

=

cpu.

memory_read

(

addr

)

        chars.

append

(

chr

(

val

)

)

   

print

(

f

"\nAssembled string: {''.join(chars)}"

)

   

5. Demo: write data to memory

   

print

(

"\n"

"▼"

60

)

   

print

(

"Demo 4: write data 77 to memory address 10"

)

   

print

(

"▼"

60

)

    cpu.

memory_write

(

10

,

77

)

   

6. Verify the write result

   

print

(

"\n"

"▼"

60

)

   

print

(

"Demo 5: verify write -- read data back from address 10"

)

   

print

(

"▼"

60

)

    val3

=

cpu.

memory_read

(

10

)

   

print

(

f

"\nVerify: wrote 77, read {val3}, {'success' if val3 == 77 else 'failure'}"

)

   

print

(

"\n"

"="

60

)

   

print

(

" All demos complete!"

)

   

print

(

"="

60

)


Bus width and actual performance

The width of the address bus determines "how much memory it can manage"

Each line of the address bus transfers one binary bit. N address lines can represent 2^N different addresses.

Address bus width Addressable space Typical CPU
16-bit 2^16 = 64 KB Intel 8086 (1978)
20-bit 2^20 = 1 MB Intel 8088 (IBM PC, 1981)
24-bit 2^24 = 16 MB Intel 80286 (1982)
32-bit 2^32 = 4 GB Intel 80386 (1985)
64-bit 2^64 = about 16 EB Modern x86-64 processors

Note: modern 64-bit CPUs usually have fewer than 64 actual address lines (e.g. 48), because 2^64 bytes far exceeds the real needs of current physical memory. 48 address lines can already address 256 TB, enough for current use.

The width of the data bus determines "how much data can be transferred at once"

An 8-bit data bus transfers 1 byte at a time; a 64-bit data bus transfers 8 bytes at a time. This is why, for the same task, a 64-bit system is often faster than a 32-bit system -- the amount of data transferred at once is doubled.

Types of signals on the control bus

The control bus is not as neatly uniform as the address and data buses. It is a collection of many signal lines, including:


Summary and check

In one sentence:The address bus handles "where to go", the data bus handles "what to carry", and the control bus handles "what to do" -- the three buses each do their own job and cooperate to complete every data transfer.

Self-test questions

  1. When the CPU wants to read a piece of data from memory, who puts the information on the address bus? And who puts the information on the data bus?
  2. If a computer's address bus has only 16 lines, what is the maximum amount of memory it can manage?
  3. What is the biggest difference between the control bus and the address/data buses? (Hint: think about the "composition" of the control bus)

Reference answers: 1. The address is placed by the CPU, the data by memory. 2. 2^16 = 65536 bytes = 64KB. 3. The address and data buses carry neat digital signals (address values / data values), while the control bus is a set of distinct discrete signal lines, each with its own independent function.