Printing Without Paying for a UART
A test that prints through the UART pays for every character in bus cycles and bit times. The mailbox console costs a store. Measure both, learn why the kit has no printf, and know when the UART path is the very thing under test.
You Will Be Able To
- Compare the cost of a character through TB_PUTC and through UART0, from a measured run.
- Explain why the kit prints hex only and has no printf.
- Decide which output of a test must go through the UART and which only has to reach the bench.
- Estimate the cycles a log costs before writing it.
- Lesson
- V4.3
- Time
- 22 min
- Runs on
- Your browser
- Check
- 5 questions
A colleague's DMA test is the slowest in the nightly run, and the DMA work is a small part of it. Most of the time goes into a friendly progress log, printed through the UART one bit time after another. The log was never the test.
- Processor
- Peripheral
- Debug
- Pads
- Data
- Debug
- The mailbox
One store per character; the monitor builds the line. The core moves on at once.
- The UART
Each character waits until the transmitter has room, then leaves as a frame of ten bits, eight cycles each.
- The ratio
About 88 against 13 cycles a character, measured below: the UART is the slow way to print.
Read the diagram as text
- tb_putc / uart_putc: the same character
- TB_PUTC: about 13 cycles a character
- Mailbox monitor: a console line, with its cycle
- UART0: wait for room, then 10 bits
- Pad IO0: about 88 cycles a character at DIV = 8
- tb_putc / uart_putc → TB_PUTC: one store
- TB_PUTC → Mailbox monitor: seen
- tb_putc / uart_putc → UART0: poll ROOM, store
- UART0 → Pad IO0: start, 8 bits, stop
Costs recorded on ap_soc v1.5, riscv64-unknown-elf-gcc 14.2.0; the run is below.
Before You Start
Read The Mailbox first. Serial Peripherals explains the UART frame.
| Question | |
|---|---|
| 1 | Why does a chip test need a cheap way to print? |
| 2 | What does a character cost through the mailbox, and through the UART? |
| 3 | Why has the kit no printf? |
| 4 | What does decimal output cost on a core with no divide instruction? |
| 5 | When must a test still use the UART? |
The Idea: Logs Belong in the Mailbox
A test prints for people: what it is doing, what it saw, where it failed. The bench reads the mailbox console and puts each line in the log with its cycle. A character there is one store.
The UART is a peripheral with its own speed. A character waits for room in the transmitter, then leaves as ten bit times. On this chip's test divider a bit is eight cycles, so a frame alone is eighty. Printing through it makes every test slower, and adds a block the test did not mean to test.
The UART still has its place. When the test is about the UART, its pad or its pin mux, the byte must go through it. The rule is simple: the log goes to the mailbox, the bytes under test go to the pad.
New Words
| Word | Meaning |
|---|---|
| Console | The bench's log of TB_PUTC characters, one line at a time, each with the cycle it ended. |
| Frame | One UART character on the wire: a start bit, eight data bits and a stop bit. |
| DIV | UART0's divider: clk_sys cycles per bit. It resets to 16; the tests here set 8, the bit time the bench's UART model is given. |
| Soft division | Division done by a routine of shifts and subtractions, because the core has no divide instruction. |
| printf | The C library's formatted print. The kit has no C library, so it has none. |
| Mark | An event number a test writes only so that the bench logs the cycle. |
The Smallest Example: Price a Log by Hand
Before measuring, estimate. A frame is ten bits; at DIV = 8 that is 80 cycles, and the wait for room adds a little. A mailbox character is a store, plus the loop around it.
| Path | Estimate per character | For 40 characters |
|---|---|---|
| TB_PUTC | A store and a loop: about 10 cycles | About 400 cycles |
| UART0, DIV = 8 | A frame of 80 cycles, plus the wait | About 3,200 cycles |
| UART0, DIV = 16 (its reset value) | A frame of 160 cycles | About 6,400 cycles |
Arithmetic from the frame and the divider; the run below measures the first two.
The Rules
Rule 1: Log through the mailbox.
TB_PUTC costs a store a character, and the bench stamps each line with its cycle.
Rule 2: Use the UART only for bytes under test.
When the pad, the pin mux or the UART itself is the point, the byte must cross it.
Rule 3: Print hex.
Shifts and masks find hex digits; decimal needs division, which this core does in software.
Rule 4: Print little, and only what helps.
Every character costs cycles in every run of every test.
Rule 5: Let the status word carry the verdict.
The console helps a person; the bench decides on TB_STATUS.
Sources for These Rules
The kit's print functions and its soft division: tools/soc/fwkit/tb.c, soft/arithmetic.c and README.md. The core is RV32I with Zicsr only (plan §3.3).
The UART frame and DIV: docs/soc/ap_soc-facts.md 27.1 and 27.5. The bench's console: facts 27.12.
Keeping the log off the UART in simulation is common practice in chip benches. Open chip projects use a log register for the same reason (plan §7).
Now Run It: Sixteen Characters, Twice
#include "drivers/uart.h"
#include "tb.h"
static const char line[] = "status 0000900d\n"; /* 16 characters */
int main(void)
{
reg_write32(SYSCTL_PINMUX, 2u); /* pad IO0 carries UART TX */
reg_write32(UART0_DIV, 8); /* 8 clk_sys cycles a bit */
tb_event(0x7701); /* mark: the mailbox line starts */
tb_puts(line);
tb_event(0x7702); /* mark: the UART line starts */
for (const char *p = line; *p; p++) uart_putc(*p);
uart_flush();
tb_event(0x7703); /* mark: the last byte is handed over */
return 0;
}EVENT 210: 00007701
CONSOLE 409: status 0000900d
EVENT 427: 00007702
EVENT 1844: 00007703The marks give the costs. The mailbox line took 217 cycles from mark to mark: about 13 a character, rule 1. The same 16 characters through UART0 took 1417 cycles: about 88 a character, eighty of them the frame. The bench saw the same bytes both ways: console_hash and uart_hash are both 461ac026.
Then a number was printed twice. The kit printed 4,000,000,000 in hex, and a small routine printed it in decimal by dividing by ten. Hex cost about 14 cycles a character. Decimal cost about 525: each digit is two calls of the kit's shift-and-subtract division. That is rule 3, and the reason the kit prints hex.
EVENT 216: 00007711
CONSOLE 333: ee6b2800
EVENT 346: 00007712
CONSOLE 6120: 4000000000
EVENT 6131: 00007713Why It Is Built This Way
printf would need a C library, which the kit does not link: no heap, no files, no locale. Even a small printf is large in an image of 8 KiB. Its decimal and padding code is slow on a core with no divide. Three functions, tb_putc, tb_puts and tb_put_hex, cover what a test needs, and each is a few lines you can read.
A decimal printer is easy to add when a report needs one, as the first practice problem shows. Its cost is then a choice you make on purpose.
The Trap: The Log That Changed the Test
A timing-sensitive test of an interrupt handler passes with its log on and fails with it off. The log, printed through the UART inside the handler, made the handler many times longer. With the log on, a second interrupt always arrived after the first was done; without it, they overlapped and exposed a bug.
A print is work the chip does. Through the UART it is a lot of work, and it moves every later event. Keep logs out of handlers, keep them on the mailbox, and know what they cost. The second practice problem gives a test that misses its budget for exactly these reasons.
At Scale
This section looks ahead. One chip, one console.
Logs as numbers
Large benches log a message ID and its arguments; the bench formats them. Firmware then never builds a string.
Many cores
With several processors, each core gets its own console channel, so lines do not mix.
Silicon
On silicon the log channel often becomes a trace buffer read by the debugger, or the real UART again.
Try It
Run the slice: it turns the recorded marks into a cost per character on each path.
Change the characters of the UART line to 32 with the same marks. Why is that answer wrong?
The predict question below feeds it the hex and decimal lines.
TB_PUTC: 217 cycles for 16 characters, 13 a character UART0 at DIV 8: 1417 cycles for 16 characters, 88 a character a log of 4096 characters by TB_PUTC: 53248 cycles a log of 4096 characters by UART0 at DIV 8: 360448 cyclesCheck Yourself
Common Mistakes
Wrong Print the progress log through the UART.
Each character then costs about 88 cycles instead of about 13.
Fix Print through TB_PUTC; keep the UART for bytes under test.
Wrong Print decimal everywhere.
On a core with no divide each digit costs two soft divisions.
Fix Print hex; add decimal only where a person needs it.
Wrong Print inside an interrupt handler.
The print stretches the handler and moves every later event.
Fix Record in the handler; print from main afterwards.
Remember
- A character costs about 13 cycles through TB_PUTC and about 88 through UART0 at DIV = 8, measured.
- The log goes to the mailbox; only bytes under test go to the pad.
- Decimal output costs soft division; the kit prints hex.
- A print is work: it moves every later event of the test.