The UART is how cvsoc talks to the outside world: it turns bytes into a stream of bits on one wire that a terminal can read. It is also the first peripheral in this section with real work inside: a serial frame with exact timing, and a small queue so that the program does not have to wait for every bit. This article builds the UART the way it would be built in industry, from two blocks with their own tests and a register block that joins them: a FIFO (S12), a transmitter (S13), and the UART with its registers on APB (S14).
11 min read | Intermediate
What You'll Learn
- Describe an 8N1 serial frame and how a divider sets its bit time
- Build an 8-entry FIFO with full, empty, a count and simultaneous push and pop
- Build a transmitter that sends one byte at a time with a busy flag
- Join FIFO, transmitter and registers into a UART and explain why programs run at the line's pace
Before You Start
This section needs Verilog: see the Verilog Tutorial. Peripherals: UART, Timers and GPIO describes UARTs in real chips, and GPIO explains the APB rules every peripheral here follows. No earlier exercise is needed for S12 and S13; S14 uses your S12 and S13 modules.
The Serial Frame
A UART (universal asynchronous receiver/transmitter) sends bytes over a single wire with no clock next to them. The receiver knows when bits start and how long they last only because both sides agreed on a speed beforehand, and because every byte comes in a fixed frame. The one used here is the common 8N1: 8 data bits, no parity bit, 1 stop bit:
- The line is 1 when idle.
- A start bit, 0, tells the receiver a byte begins.
- The 8 data bits, starting with bit 0, the least significant.
- A stop bit, 1, which returns the line to idle and gives the receiver time before the next start bit.
Every bit lasts the same number of clock cycles, set by the divider DIV. The UART's clock in cvsoc runs at 100 MHz, so the reset value DIV = 16 gives 6.25 million bits per second, far faster than a real terminal, which keeps simulations short. A real terminal at 115,200 bits per second would need DIV = 868, since 100,000,000 / 115,200 is about 868. A frame is 10 bits, so one byte takes 10 x DIV cycles: 160 cycles at the reset value.
Part 1: The FIFO (S12)
A byte takes 160 cycles to leave the line, but the core can write a new one every few cycles. Without a queue the program would have to wait for each byte. A FIFO (first in, first out) holds up to 8 bytes in the order they were written, so the program can write a short message in one go and get on with its work while the line catches up.
module cvsoc_fifo (
input logic clk,
input logic rst_n,
input logic push, // store wdata at the tail (ignored when full)
input logic [7:0] wdata,
input logic pop, // remove the head (ignored when empty)
output logic [7:0] rdata, // the head entry, valid while empty is 0
output logic full, // 8 entries
output logic empty, // 0 entries
output logic [3:0] count // 0 to 8
);
push= 1 at a clock edge storeswdataat the tail, unless the FIFO is full; then the push is ignored, even ifpopis 1 in the same cycle.pop= 1 at a clock edge removes the head, unless the FIFO is empty; then it is ignored.- A push and a pop in the same cycle both happen when neither is ignored, and the count stays the same.
rdatais the head, the oldest byte still queued, combinationally.countis 0 to 8,fullmeans 8,emptymeans 0; after reset the FIFO is empty.
A common way to build it is a circular buffer: eight entries, a write pointer and a read pointer of 3 bits each that wrap around from 7 to 0 by themselves, and the count. The count is the part people get wrong: with a push and a pop in the same cycle, it must not change, and one if-else that only adds or only subtracts gets that case wrong. Decide first whether the push and the pop really happen, then update the pointers and the count from those two decisions.
Quick Check
Q: The FIFO holds 8 bytes. In one cycle, push and pop are both 1. What are the count and the head afterwards?
Show Answer
The FIFO is full, so the push is ignored; the pop happens. The count goes from 8 to 7, and the head becomes the second-oldest byte. The new byte is lost: a full FIFO drops writes, which is why software checks the full flag first.
Part 2: The Transmitter (S13)
The transmitter sends one byte as one frame:
module cvsoc_uart_tx (
input logic clk,
input logic rst_n,
input logic [15:0] div, // clock cycles per bit
input logic start, // send data (ignored while busy)
input logic [7:0] data,
output logic busy, // 1 from the start until the stop bit has ended
output logic tx // the serial line, 1 when idle
);
start= 1 at a clock edge whilebusyis 0 takesdataand begins the start bit at that edge. Whilebusyis 1,startis ignored.- Every bit lasts exactly
divcycles. busyis 1 from that edge until the stop bit has lasteddivcycles; a new byte can start in the next cycle.- After reset
txis 1 andbusyis 0.
Two counters drive it: a cycle counter that counts from 0 to div - 1 and wraps, marking the end of each bit, and a bit counter that says which part of the frame is on the line (start, data bits 0 to 7, stop). A shift register holding the byte and shifting right by one at each bit boundary puts the data bits on the line in the right order. The most common error is a cycle counter that wraps at div instead of div - 1, which makes every bit one cycle too long: the receiver drifts by one cycle per bit and loses the byte after a few bits.
Part 3: The UART on APB (S14)
The UART joins the two blocks behind three registers:
| Offset | Register | Access | Meaning |
|---|---|---|---|
| 0x000 | TXDATA | write only | queue the byte in bits [7:0]; ignored when the FIFO is full |
| 0x004 | STATUS | read only | bit 0: FIFO full; bit 1: busy, a byte is queued or being sent |
| 0x008 | DIV | read/write | clock cycles per bit, bits [15:0]; 16 after reset |
The joining logic is one signal. When the transmitter is not busy and the FIFO is not empty, the head byte must go out: call it send, and connect it to both the transmitter's start and the FIFO's pop. In the same cycle the transmitter takes the byte and the FIFO lets go of it. STATUS reports the FIFO's full in bit 0 and "the transmitter is busy or the FIFO is not empty" in bit 1, so that software can wait until everything has left the line. Writes follow the APB rules from GPIO: only an ACCESS cycle at the right offset writes.
The UART module's ports are the same APB peripheral ports as the GPIO's, with tx instead of the pins. For S14 you paste your FIFO and transmitter into the file and use them inside cvsoc_uart.
Programs Run at the Line's Pace
The program stream sends 256 bytes as fast as the UART allows, the way a data logger would. On the reference design it takes 41,275 cycles at DIV = 16 and 10,558 at DIV = 4. Compare that with the time the line needs, 256 bytes x 10 bits x DIV cycles:
Both runs take only a little longer than the line time. Once the FIFO is full, the program can only write a byte each time one leaves the line, and it spends the time in between reading STATUS: 4,014 reads at DIV = 16, 601 at DIV = 4. A faster core would not help at all; a faster line would. Polling, Interrupts and DMA explains how real systems use that waiting time instead of spending it in a loop.
Where Is This Used?
Nearly every chip has a UART, if only as a debug console: the first message a new board prints during bring-up usually comes through one. Industrial UARTs add a receiver, parity, flow control, deeper FIFOs and interrupts when the FIFO runs low. Many are compatible with the 16550, a UART from the 1980s whose register layout is still copied; its transmit FIFO (16 bytes deep) and its status bits serve the same purpose as yours.
How It Is Tested
- S12: pushes and pops compared with a model after every cycle (order, count, full, empty, head), a push to a full FIFO and a pop from an empty one, push and pop together (also while full), the pointers wrapping around many times, and 3,000 random cycles.
- S13: bytes at DIV = 4, 1 and 7 checked cycle by cycle on the line,
busyfrom the start to the end of the stop bit, astartwhile busy (ignored), and an idle line without one; the hidden test adds 40 random bytes at random DIV. - S14: a terminal model decodes
txand checks every bit's length; the reset values; "Hi!" at DIV = 4; twelve writes in quick succession, of which only nine bytes arrive (one sent at once, eight queued, three dropped), with STATUS reading busy and full; STATUS after the line goes quiet; and DIV = 1, 16 and 5 with 60 random bytes.
Common Mistakes
Mistake #1: Sending the most significant bit first
Many serial protocols send the most significant bit first, but a UART does not. A frame sent backwards still has a correct start and stop bit, so the line looks fine in the waveform; only the decoded characters are wrong.
Mistake #2: Counting push and pop separately
Updating the count with "if push, add one; else if pop, subtract one" loses a byte from the count every time both happen in the same cycle, which in a UART is often: the transmitter takes a byte while the program writes the next. The FIFO then believes it is full when it is not, or empty when it is not.
Mistake #3: Dropping busy when the stop bit starts
If busy falls at the start of the stop bit, the next byte's start bit cuts the stop bit short. A receiver that expects a full stop bit then misreads the next frame. The S13 test checks busy in every cycle of the stop bit.
Your Turn in the Lab
Exercise S12: write cvsoc_fifo. Exercise S13: write cvsoc_uart_tx. Exercise S14: paste both and write cvsoc_uart with its registers. Save all three in your lab project for S16.
Summary
- An 8N1 frame is a start bit (0), 8 data bits from bit 0 up, and a stop bit (1); every bit lasts DIV clock cycles, so a byte takes 10 x DIV cycles.
- The FIFO queues up to 8 bytes: a push to a full FIFO and a pop from an empty one are ignored, and a push and a pop together leave the count unchanged.
- The transmitter starts a frame only while not busy and stays busy until the stop bit has ended.
- The UART joins them with one signal that starts the transmitter and pops the FIFO in the same cycle; STATUS reports full and busy, DIV sets the bit time.
- A program that streams bytes runs at the line's pace:
streamtakes 41,275 cycles at DIV = 16 against 40,960 cycles of line time.