Skip to content
Device bridge

Device bridge

For hardware designers

device_bridge connects the CPU’s bus to the ROM’s data port and to the devices. It has the address of every device register, answers every access on the bus from its own registers, and tells the devices what to do with strobes. The devices are modules inside the bridge, each with ports that suit it. They know nothing about the bus.

The bridge has no state of a device. It decodes, answers and gives strobes. Each device has its own state and logic: the UART’s buffers, the timer’s count, the cycle counter’s snapshot.

                   +-----------------------------------------------+
 CPU bus --------> | device_bridge                                 |
                   |   decodes the address, answers the bus        |
 <-- ready, error, |                                               |
     read word     |     strobes, data |      ^ outputs            |
                   |                   v      |                    |
 ROM data port <-> |   +------+ +---------------+ +-------+ +-----+ |
                   |   | uart | | cycle_counter | | timer | |board| |
                   |   +------+ +---------------+ +-------+ +-----+ |
                   +------|-----------------------------|-------|---+
                          |                             |       |
                    rx, tx, interrupt 2           interrupt 1  user LED, DIP switches

What goes where

AddressGoes to
0x04000000–0x04000FFFthe ROM’s data port, for a read of any size
the device registers in the I/O devices tablethe register’s device, for a word access
anything else on the busnothing: the bridge answers with error

The rest of the bus’s regions, such as the GPU’s memories and the external SRAM, aren’t built yet. Until then, the bridge answers them with an error.

The device registers are grouped per device, 256 bytes each:

0xFFFF8204 = 1111 1111 1111 1111 1 | 000 0010 | 0000 0100
             bits [31:15] all 1     | [14:8]   | [7:0]
             a device register      | device 2 | register 0x04

The bridge compares the whole address with the address of each register, so the groups only keep each device’s registers together, with room to grow.

Parameters

ParameterDefaultMeaning
CLOCKS_PER_BIT100the UART’s clock cycles per bit: 1 Mbaud at 100 MHz

Ports

The bus side is the CPU’s bus: bus_request, bus_write, bus_addr, bus_write_data and bus_byte_enable in, and bus_ready, bus_error and bus_read_word out. See the CPU’s Interface page for the protocol. The bus’s outputs are registers.

PortDirectionMeaning
rom_addr[11:2]outthe word in the ROM: bits [11:2] of bus_addr
rom_read_word[31:0]inthe ROM’s word, in the cycle after rom_addr
uart_rxinthe UART’s receive line
uart_txoutthe UART’s transmit line
user_ledoutthe user LED, 1 is on
switches[3:0]inthe DIP switches, not synchronized
timer_irqoutthe timer’s interrupt, to the CPU’s interrupt 1
uart_irqoutthe UART’s interrupt, to the CPU’s interrupt 2

Answering an access

The bridge answers an access in the cycle after the request, except for three accesses, which wait:

Accessbus_ready
a register of a device1 cycle after the request
a data read from the ROM2 cycles after the request: the ROM gives its word a cycle after its address
a read of the UART’s RECEIVE, with a byte waiting3 cycles after the request: the bridge tells the UART to take the byte, and the UART’s buffer gives it a cycle later
a write to the UART’s TRANSMIT, with the transmit buffer fullin the cycle after there’s room again
a read of RECEIVE with nothing waiting1 cycle after the request, with 0
an access that nothing takes1 cycle after the request, with error

The bus keeps the address and the data the same until the answer. So a waiting access is handled by looking at the same address again, in every cycle, until it can be answered. The bridge only remembers that an access is waiting.

A read of the timer’s CONTROL register, at 0xFFFF8204:

cycle            1          2
bus_request      1          0
bus_addr         FFFF8204   FFFF8204
bus_ready        0          1
bus_read_word    0          control bits

A data read from the ROM, at 0x04000010:

cycle            1          2          3
bus_request      1          0          0
bus_addr         04000010   04000010   04000010
rom_addr         004        004        004
rom_read_word    -          w          w
waiting          0          1          0
bus_ready        0          0          1
bus_read_word    0          0          w

Telling the devices

A write, or a read that takes something, gives a strobe to the device in the cycle after the request: 1 for one cycle, in the same cycle as bus_ready. The data of the write is in a register of the bridge in that cycle too. So the device does it at the end of that cycle, and the next access on the bus already sees the result.

A write of N to the timer’s COUNT register:

cycle            1          2          3
bus_request      1          0          0
bus_write        1          1          -
bus_addr         FFFF8200   FFFF8200   -
bus_ready        0          1          0
timer start      0          1          0
data             -          N          -
timer count      ...        ...        N

A read gives the device’s outputs as they are in the request’s cycle.

What each device gets from the bridge, and what it gives back:

DeviceFrom the bridgeTo the bridge
uarttransmit with a byte, receive, clear (the errors), configure with the interrupt enabletx_full, received with the byte taken, rx_waiting, overflow, framing_error, irq_enable, irq
cycle_countertake_snapshotsnapshot, 64 bits
timerstart with a count, configure with periodic and the interrupt enable, clearcount, periodic, irq_enable, expired, irq
board_ioset_led with the LEDled, switches (synchronized)

The bridge knows where each bit of a register is. A device only knows what it’s told: the timer gets periodic as a signal of its own, not as bit 0 of a word.

Answering with an error

For an access that nothing takes, the bridge answers in the cycle after the request with ready and error, and a read_word of 0:

  • an address on the bus outside the ROM and the device registers
  • an address in the device registers that isn’t a register
  • a device register accessed with a size other than a word
  • a write to the ROM

The CPU then sets the bad-address flag of its error register. So every access gets an answer, and the CPU never waits forever.

Adding a device

  1. Write the device as a module with ports that suit it: strobes for what it does, and outputs for what software reads. If it can interrupt, it gets a flag, an enable bit and an interrupt output, as described on the I/O devices page.
  2. Give its registers addresses in the next free 256 bytes.
  3. In the bridge: add the addresses, a line per register for writes and one for reads, the strobes, and the instance of the device.
  4. Add its pins and its interrupt output to the bridge’s ports, and connect the interrupt to a free interrupt input of the CPU, raising the CPU’s INTERRUPTS if needed.
  5. Add a model of it to the instruction set simulator, in Tools/b32sim/devices.py.

Modules

The files are in Hardware/io/.

FileWhat it is
device_bridge.vthe device bridge, with the devices inside
uart.vthe UART: two buffers, a receiver and a transmitter, the error flags and the interrupt
uart_rx.vthe UART’s receiver
uart_tx.vthe UART’s transmitter
fifo.va buffer in a small RAM, for the UART: a byte that’s taken comes out in the next cycle
cycle_counter.vthe cycle counter and its snapshot
timer.vthe timer
board_io.vthe user LED, and the DIP switches with a synchronizer
Last updated on