UARTs: Two Serial Ports Behind One Door
A Next can fetch a web page, chat over the Internet and talk to a Raspberry Pi, yet it has no network chip of the kind a PC has. What it has is older than the Spectrum itself: a serial port, a wire on which bits follow one another at an agreed pace. Behind that wire sits a Wi-Fi module that does the networking, and behind another one a Pi that can do almost anything. How does a Z80 keep up with a line that delivers a byte every 87 microseconds, and what happens when it doesn’t?
I think the serial port is the most practical piece of the Next for anyone who wants it to talk to the world. A file from the Internet, a game played across two machines, a MIDI keyboard, a debugger on a PC: all of them arrive as bytes on a serial line. The hardware that receives them is simple, but its buffers, flags and timing decide whether your program gets every byte or silently loses some, and that is worth understanding before you write the loop that reads them.
You should know two chapters first. Interrupts: Fourteen Voices, Two Wires explains the UARTs’ interrupts and their place in the daisy chain, which this chapter only sums up. CTC: Four Timers in a Ring explains the master clock and the VGA modes that the baud rate is counted from.
The road ahead
We start with the two UARTs and the four ports they share, then look at what actually travels on the wire: a frame of bits. Next comes the baud rate, set by a prescaler of the master clock, and the frame register that chooses the frame’s shape. The heart of the chapter is the two buffers and the status register: how bytes wait to be sent, how they wait to be read, and what the hardware does when a buffer fills or a frame arrives broken; if you read only one section, read that one. After that we look at break, hardware flow control, interrupts, the joystick port and resets.
The facts are for core 3.02.01. The master clock is 28 MHz, as in VGA mode 0, and the examples use 115,200 baud, 8 data bits, no parity, one stop bit, the setting after power-on. Every fact was checked against the Next’s hardware design.
Two UARTs, four ports
A UART, a universal asynchronous receiver-transmitter, turns bytes into a stream of bits on one wire and a stream of bits on another wire back into bytes. The Next has two of them, identical in every way, and I call them the twins:
- UART 0 is wired to the Wi-Fi module, an ESP chip on the board, which does the networking and talks to the Next through text commands on this line; the Wi-Fi chapter tells its story.
- UART 1 is wired to the Raspberry Pi’s connector, on GPIO 14 and 15, once bit 5 of the PI
Peripheral Enable register, NextReg
$A0, gives those pins to the UART.
The twins share one door: four ports, and a switch that says which twin is behind it.
- Port
$153B, UART select. Bit 6 chooses the twin: 0 for UART 0, 1 for UART 1. Every other port talks to the chosen one. Bit 4 and bits 2–0 are part of the baud rate, as the next sections explain. - Port
$163B, UART frame: the shape of the frame, and three switches. - Port
$133B, UART TX: a write sends a byte; a read gives the status. - Port
$143B, UART RX: a read takes a received byte; a write sets part of the baud rate.
Bit 4 of NextReg $83 switches all four ports off: they then read $FF and ignore writes.
A frame on the wire
A serial line has no clock wire. The two ends agree on a speed, the baud rate, in bits a second, and the receiver times the bits itself. A line at rest is high, at 1. A byte travels as a frame:
- The start bit, a 0, ends the rest and tells the receiver that a frame begins.
- The data bits, 5 to 8 of them, least significant first.
- A parity bit, if the two ends agreed on one: it makes the number of 1s even or odd, so that one flipped bit is noticed.
- One or two stop bits, 1s, before the next start bit can come.
The receiver’s part is the clever one. It sees the falling edge of the start bit, waits half a bit, checks that the line is still low, and then takes each bit one bit period later, in its middle, as far as possible from the edges where the level changes. The Next’s receiver does better still: every time the line changes level, it moves its sampling moment back to the middle of the bit, so a sender whose clock is a little fast or slow is followed through the whole frame. It also ignores pulses shorter than 4 master clocks, 143 ns, which are noise, not bits. I find this a lovely piece of engineering for so small a block.
With 8 data bits, no parity and one stop bit, written 8N1, a frame is 10 bits, so 115,200 baud carries 11,520 bytes a second, one every 86.8 µs. That is the answer to the opening puzzle: at 3.5 MHz the CPU has about 300 T-states per byte, which is plenty for a loop, and nothing like enough if the loop has to wait for the screen or a disk.
The baud rate
Each twin times its bits with a prescaler: a bit lasts exactly the prescaler’s number of master clocks, so the baud rate is 28,000,000 ÷ prescaler. The prescaler has 17 bits, written in three pieces:
port $143B, bit 7 = 0: bits 6-0 -> prescaler bits 6-0
port $143B, bit 7 = 1: bits 6-0 -> prescaler bits 13-7
port $153B, bit 4 = 1: bits 2-0 -> prescaler bits 16-14The $143B writes go to the twin that is selected. The $153B write is a little different: its bits 2–0
go to the twin that bit 6 of the same byte selects, so one write can choose a twin and set its top bits
together. A read of $153B gives bit 6, which twin is selected, and that twin’s bits 16–14; the lower 14
bits cannot be read back.
The prescaler is a whole number, so the rate is rarely exact. Here are some common ones at 28 MHz:
- 9,600 baud: prescaler 2,917, 9,599 baud.
- 31,250 baud, MIDI’s rate: prescaler 896, exact.
- 115,200 baud: prescaler 243, 115,226 baud, 0.02% fast. This is the value after power-on.
- 230,400 baud: prescaler 122, 229,508 baud, 0.4% slow.
- 921,600 baud: prescaler 30, 933,333 baud, 1.3% fast, about as far as I would trust a serial line.
- 2,000,000 baud: prescaler 14, exact.
The largest prescaler, 131,071, gives about 214 baud. A UART at the other end is happy with an error of a
couple of per cent, and the re-centring receiver tolerates more. Two more facts matter in practice. The
prescaler counts the master clock, not the CPU’s, so the baud rate does not change with the CPU speed;
but it does change with the VGA mode, which runs the master clock faster, so a program that cares reads
NextReg $11 and computes its prescaler from that mode’s clock. And both ends of a frame take the prescaler
when the frame starts, so a new one applies from the next frame, never to the middle of one.
Here is UART 0 set to 230,400 baud:
ld bc,$153b
ld a,%00010000 ; UART 0, bits 16-14 follow:
out (c),a ; 000
ld b,$14 ; $143B
ld a,122 ; bit 7 = 0: bits 6-0
out (c),a
ld a,%10000000 ; bit 7 = 1: bits 13-7 = 0
out (c),aThe frame register
Port $163B holds the frame’s shape for the selected twin, and three switches. It reads back all eight
bits:
$163B: R B F N N P O S
R = 1: reset the twin and hold it while the bit is set
B = 1: send a break once the transmitter is idle
F = 1: hardware flow control
N N = data bits: 11 8, 10 7, 01 6, 00 5
P = 1: a parity bit
O = parity: 0 even, 1 odd
S = stop bits: 0 one, 1 twoThe value after power-on is $18: 8 data bits, no parity, one stop bit, everything else off.
Bit 7 deserves a warning, because it does more than its name suggests. While it is 1, both buffers are
empty and both state machines are held in reset: a byte being sent stops in the middle, nothing is
received and nothing can be sent. It is not a pulse. Write the register with bit 7 set to clear the twin,
then write it again with bit 7 clear, or the twin stays dead. My habit: $98, then $18.
Two buffers and the status
Between the CPU and each wire sits a buffer, a FIFO, first in, first out:
- The transmit buffer holds 64 bytes. A write to
$133Bputs a byte in it, and the transmitter takes them out one by one, as fast as the baud rate allows. A byte written while the buffer is full is lost. - The receive buffer holds 512 bytes, and one more waits behind it while it is full. A read of
$143Btakes the oldest; a read of an empty buffer gives 0, which could also be a byte, so check the status first.
The status, read from $133B, says how both buffers and the line are doing:
- Bit 0: the receive buffer has at least one byte.
- Bit 1: the transmit buffer is full. Wait before writing.
- Bit 2: the receive buffer overflowed: a byte arrived while the 512 bytes and the one waiting behind them were all there, and it was lost.
- Bit 3: the receive buffer is near full, at least 384 bytes, three quarters.
- Bit 4: the transmit buffer is empty and the transmitter has finished its last frame: the line is quiet. This is the bit to wait for before changing the baud rate or the frame.
- Bit 5: the byte at the head of the receive buffer arrived after an error, an overflow or a broken frame. Bytes are stored with this mark, so you can tell exactly where data went missing.
- Bit 6: a framing error (a stop bit that was 0) or a parity error happened.
- Bit 7: the line is in break, as the next section explains.
Bits 6 and 2 are cleared by reading the status: each read reports what happened since the last one. And here is the rule that surprises people: a frame with an error is thrown away. A byte whose stop bit or parity is wrong never reaches the buffer. Bit 6 tells you it existed, and bit 5 marks the next byte that made it through.
Two loops do most of the work. This one sends a text, waiting whenever the transmit buffer is full:
; send the zero-terminated text at HL on UART 0
Send: ld bc,$153b
xor a ; UART 0, prescaler untouched
out (c),a
ld b,$13 ; $133B: TX and status
Next: ld a,(hl)
or a
ret z ; end of the text
Full: in a,(c) ; status
bit 1,a ; TX buffer full?
jr nz,Full
ld a,(hl)
out (c),a ; into the TX buffer
inc hl
jr NextAnd this one empties the receive buffer into memory at DE, as long as it has bytes:
; copy what the selected UART has received to (DE)
Drain: ld bc,$133b
Loop: in a,(c) ; status
rra ; bit 0 into carry
ret nc ; the buffer is empty
inc b ; $143B: RX
in a,(c) ; the oldest byte
dec b
ld (de),a
inc de
jr LoopMy advice for receiving is to drain the whole buffer every time you look, not to take one byte. At 115,200 baud the 512 bytes fill in 44 ms, a little over two frames, so a program that reads the UART once a frame has time to spare, and one that stops reading while it loads a level or waits for a key does not.
Break
A break is the line held low for longer than a frame: no start bit, data and stop bit can look like that, so both ends know it is a signal, not data. It is how some devices are reset or woken.
- Sending a break: set bit 6 of the frame register. The transmitter finishes the frame it is sending, then holds the line low and sends nothing more until the bit is cleared.
- Receiving a break: the frame fails its stop bit, a framing error that sets status bit 6, and when the line stays low for eight more bit periods, status bit 7 says break for as long as it stays low.
Hardware flow control
The receive buffer can fill, so a fast sender needs a way to be told to wait. Hardware flow control, bit 5 of the frame register, adds two signals beside the data lines, both active low:
- CTS, clear to send, is an input. The twin starts a frame only while the other end holds it low; while it is high, the transmitter takes the next byte from its buffer and waits with it in hand.
- RTR, ready to receive, is an output. The twin drops it when its receive buffer holds 510 bytes, two short of full, so that a sender who stops at once loses nothing.
With flow control, a program can stop reading for as long as it likes, and no byte is lost: the sender is paused instead. For the Pi, the two signals are GPIO 16 and 17. The Wi-Fi module takes part in flow control only on issue 4 boards, so on an earlier board a program must read fast enough.
Interrupts
Each twin can ring the CPU, and the interrupts chapter tells the whole story. In short, from the UART’s side:
- Receive: when the buffer gets its first byte, or, with the “near full only” bit, when it reaches 384.
The enables are bits 0 and 1 of NextReg
$C6for UART 0, bits 4 and 5 for UART 1. - Transmit: when the transmit buffer becomes empty, which is a frame before the line falls silent, because the last byte is still being sent. The enables are bit 2 for UART 0 and bit 6 for UART 1.
- The receivers stand high in the daisy chain, right after the line interrupt, and the transmitters last, after the ULA: a byte arriving is urgent, a buffer to refill is not.
- The requests are edges: a byte arriving into a buffer that is not empty raises nothing new. That is the second reason to drain the whole buffer in the handler.
The joystick port
Either twin can leave its own pins and use a joystick port instead, through the Joystick I/O Mode
register, NextReg $0B: bit 7 turns the I/O mode on, bits 5–4 = 10 choose the left port and 11 the right
one, and bit 0 chooses the twin, 0 for UART 0 and 1 for UART 1. The twin then sends on pin 7 and receives on
pin 9, and with flow control it takes CTS from pin 6; there is no pin for RTR. Its own lines rest high
meanwhile. A cable from a joystick port to another machine is then a serial link with no expansion at all.
Resets
A soft reset selects UART 0, empties both buffers in both twins, clears their errors and stops whatever
was being sent. It keeps each twin’s prescaler and frame register. Only loading the core, which a hard
reset through NextReg $02 does, sets them back to 115,200 baud and $18. So a program that changed the
baud rate leaves it changed for the next program after a soft reset, and here is my last piece of advice:
set the prescaler and the frame yourself, even when you want the defaults.
What to take home
The Next has two identical UARTs, the twins: UART 0 to the Wi-Fi module and UART 1 to the Pi’s connector,
behind one set of ports. Port $153B bit 6 chooses the twin; $163B is its frame register; $133B sends
a byte and reads the status; $143B takes a received byte. A byte travels as a frame: a start bit, 5 to 8
data bits with the least significant first, an optional parity bit and one or two stop bits, sampled in the
middle of each bit by a receiver that re-centres on every edge and ignores short glitches.
The baud rate is the master clock divided by a 17-bit prescaler, written in pieces to $143B and $153B:
243 gives 115,200 baud at 28 MHz, the power-on value. It does not follow the CPU’s speed, but it does follow
the VGA mode. The frame register’s bit 7 holds the twin in reset for as long as it is set, bit 6 sends a
break and bit 5 switches on hardware flow control.
The transmit buffer holds 64 bytes; the receive buffer 512 and one more, near full at 384, with RTR raised at 510. A frame with an error is thrown away, and the status says so: bit 6 for the error, bit 5 for the next good byte, bit 2 for an overflow, bit 7 for a break. Interrupts come on the first byte or at near full, and when the transmit buffer empties; they are edges, so a handler drains everything. Either twin can move to a joystick port. A soft reset empties the buffers but keeps the baud rate. Read fast, or let flow control make the other end wait, and the twins will not lose a byte.