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.

One Character, Two PathsStep through the two ways a character leaves a test. It is one store to the mailbox, or a frame of ten bits through UART0.
  • Processor
  • Peripheral
  • Debug
  • Pads
  • Data
  • Debug
One Character, Two PathsStep through the two ways a character leaves a test. It is one store to the mailbox, or a frame of ten bits through UART0. processor tb_putc /uart_putc the same character peripheral TB_PUTC about 13 cycles acharacter debug Mailbox monitor a console line, withits cycle peripheral UART0 wait for room, then10 bits pads Pad IO0 about 88 cycles acharacter at DIV = 8 one storeseenpoll ROOM, storestart, 8 bits, stop
  1. processor tb_putc / uart_putcthe same character
    • one store → TB_PUTC
    • poll ROOM, store → UART0
  2. peripheral TB_PUTCabout 13 cycles a character
    • seen → Mailbox monitor
  3. debug Mailbox monitora console line, with its cycle
  4. peripheral UART0wait for room, then 10 bits
    • start, 8 bits, stop → Pad IO0
  5. pads Pad IO0about 88 cycles a character at DIV = 8
  1. The mailbox

    One store per character; the monitor builds the line. The core moves on at once.

  2. The UART

    Each character waits until the transmitter has room, then leaves as a frame of ten bits, eight cycles each.

  3. 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.

Five questions this page answers
Question
1Why does a chip test need a cheap way to print?
2What does a character cost through the mailbox, and through the UART?
3Why has the kit no printf?
4What does decimal output cost on a core with no divide instruction?
5When 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

New words on this page
WordMeaning
ConsoleThe bench's log of TB_PUTC characters, one line at a time, each with the cycle it ended.
FrameOne UART character on the wire: a start bit, eight data bits and a stop bit.
DIVUART0'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 divisionDivision done by a routine of shifts and subtractions, because the core has no divide instruction.
printfThe C library's formatted print. The kit has no C library, so it has none.
MarkAn 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.

A 40-character line
PathEstimate per characterFor 40 characters
TB_PUTCA store and a loop: about 10 cyclesAbout 400 cycles
UART0, DIV = 8A frame of 80 cycles, plus the waitAbout 3,200 cycles
UART0, DIV = 16 (its reset value)A frame of 160 cyclesAbout 6,400 cycles

Arithmetic from the frame and the divider; the run below measures the first two.

The Rules

  1. Rule 1: Log through the mailbox.

    TB_PUTC costs a store a character, and the bench stamps each line with its cycle.

  2. 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.

  3. Rule 3: Print hex.

    Shifts and masks find hex digits; decimal needs division, which this core does in software.

  4. Rule 4: Print little, and only what helps.

    Every character costs cycles in every run of every test.

  5. 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

COST_C: the same line through both paths, with marks around each.
#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;
}
Measured on our bench (soc_tb, Verilator 5.052 2026-09-05), riscv64-unknown-elf-gcc 14.2.0.
EVENT 210: 00007701
CONSOLE 409: status 0000900d
EVENT 427: 00007702
EVENT 1844: 00007703

The 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.

The hex line, then the decimal line, with their marks.
EVENT 216: 00007711
CONSOLE 333: ee6b2800
EVENT 346: 00007712
CONSOLE 6120: 4000000000
EVENT 6131: 00007713

Why 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

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. One chip, one console.

  1. Logs as numbers

    Large benches log a message ID and its arguments; the bench formats them. Firmware then never builds a string.

  2. Many cores

    With several processors, each core gets its own console channel, so lines do not mix.

  3. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 cycles

Check Yourself

Predict the outputThe slice now reads the marks around the hex line and the decimal line. What is the last line it prints?
// Marks one run wrote to TB_EVENT around each line it printed (cycles of clk_sys), and the characters
// of each line. Cost of a character on each path, and of a log of 4,096 characters.
module tb;
  localparam int N = 2;
  string name [N];
  int    from [N], upto [N], chars [N];
  initial begin
    name[0] = "hex by shifts"; from[0] = 216; upto[0] = 346; chars[0] = 9;
    name[1] = "decimal by division"; from[1] = 346; upto[1] = 6131; chars[1] = 11;
    for (int i = 0; i < N; i++)
      $display("%s: %0d cycles for %0d characters, %0d a character", name[i], upto[i] - from[i], chars[i], (upto[i] - from[i]) / chars[i]);
    for (int i = 0; i < N; i++)
      $display("a log of 4096 characters by %s: %0d cycles", name[i], 4096 * ((upto[i] - from[i]) / chars[i]));
  end
endmodule
ConceptWhy does the kit print hex only?
ConceptA test of the UART's pin mux prints its verdict. Where must the test's bytes go?
ConceptWhich costs fewer cycles per character on this chip?
ConceptWhy has the kit no printf?

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.

Practice

Report a problem with this page