Full picture
12

Universal Asynchronous Receiver Transmitter (UART)

12.1 Overview

In the absence of any output signal, like a video signal, or input signal, like a keyboard, a computer needs a way to communicate with the user or another computer. As we just saw, I/Os are for this. However, we also need a protocol to describe how communications will occur between these entities.

The most used communication protocol is the Universal Asynchronous Receiver Transmitter or UART. Let's see how it works in our case.

12.2 Hardware implementation

In the Z80 PIO chapter, we saw that two I/Os of the system port are dedicated to UART, we have:

  • TX, the output, is driven by Zeal 8-bit Computer. It will be used to output a byte, generally an ASCII character, to be processed or displayed by a terminal program on the other end, that may be running on a host computer.
  • RX, the input, is driven by that computer on the other end, that sends bytes to Zeal 8-bit Computer. In the case where a user is typing characters in the terminals, they will be forwarded through this line.

Usually, UART protocol may include more lines, like $\overline{CTS}$ or $\overline{RTS}$, but we will omit them on purpose since Zeal 8-bit Computer doesn't have them.

Both RX and TX lines use the same protocol but are independent, we can send bytes on TX line regardless of RX state, and we can receive bytes on RX line regardless. However, on Zeal 8-bit Computer, we do not have a dedicated UART integrated circuit, instead, the protocol is simulated in software, which means that the software has to manually set the TX pin levels or read the RX line level at the right time to generate or determine a byte to send or to receive.

This implementation presents several drawbacks, like being slower than having a dedicated chip, or not being able to receive bytes asynchronously, but this choice of implementation was made for several reasons:

  • The main goal of Zeal 8-bit Computer is to be used with a video card, which will be the main display, the main way to show text or graphics to the user.
  • The way to receive inputs from the user is to use the PS/2 keyboard port, which can trigger asynchronous interrupts. As such, having a synchronous UART RX signal is not critical.
  • Adding a UART chip on the board would require more space on the PCB and result in a more complex routing.

Overall, the UART is supposed to main a secondary feature mainly used to synchronously send or receive large amounts of data to and from another computer.

This small diagram shows how to set up the UART:

UART connection
Connecting UART on Zeal 8-bit Computer

12.3 Protocol description

The UART protocol describes one transfer as follows:

  • The transfer line (TX and RX) is high when idling.
  • To initiate a transfer a start bit is sent, which means that the line line is set to low for a duration of $τ$.
  • Send each bit of the byte on the line where a 0 is represented by a low logic level and a 1 by a high logic level. The least significant bit is sent first. Each bit lasts $τ$. Even if most of the time a byte is composed of 8 bits, UART protocol says that it varies from 5 to 9 bits.
  • Then follow an optional parity bit, again, it lasts for a duration of $τ$.
  • Finally, one or two stop bits are sent, which means that the line is set to high for a duration of one or two $τ$ respectively.

We can note that there is no clock signal, which means that the sender and the recipient must agree, beforehand, on the number of data bits, the number of stop bits and whether a parity bit will be present for each transfer.

Of course, the last main parameter is $τ$, the time each bit lasts. In the case of the UART, this is determined by the baudrate, which states the number of bits sent over a second.

For example, if the baud rate chosen is 115200, each bit will last $\frac{1}{115200} * 1000000 = 8.68 \mu s$, and so $τ=8.68 \mu s$

The following diagram sums up the protocol:

UART protocol
UART protocol

⚠️ Warning

On Zeal 8-bit Computer, high logic levels are 5V, this also applies to the UART. Before connecting it to another device, make sure that the latter is 5V I/O tolerant. If that device is 5V I/O tolerant but generates 3.3V logic levels, then it is fully compatible. Any voltage higher or equal to 2V will be interpreted as a high logic level by the Z80 PIO behind the UART RX line.

12.4 Limitation

As the UART is simulated from the software on Zeal 8-bit Computer, in practice, the limited baudrate on TX is 115200 and the limit on RX is 57600.

On RX line, in theory, it is also possible to reach 115200 but when a large amount of data is being sent, the program may not have enough time to process the data that was just received and prepare itself to receive the next one, as such a reasonable limit is 57600.

An open-source programming example of how to generate compliant TX and RX signals at different baudrates can be found in Zeal 8-bit Bootloader source code.