Before you build the blocks of cvsoc, it helps to know how a test runs on the finished SoC: how a C program becomes words in the TCM, how the core starts it, how the testbench sees what the program prints, and how the test knows the program is done. The system exercises in this section, S08 and S16, run exactly this way. This article follows the program hello from its C source to the system report it produces, and shows how to read that report.
14 min read | Beginner to Intermediate
What You'll Learn
- Explain how a C program is compiled, linked and loaded into the TCM for a test
- Describe what the start-up code does between reset and main, and how a program ends a test
- Read the system report: cycles, instructions, bus waiting and accesses by target
- Trace a program's register accesses to the peripherals in its compiled listing
Before You Start
This section needs Verilog: see the Verilog Tutorial if you have not worked through it. This article reads C and a little RISC-V assembly but asks you to write neither. Boot ROM and the Boot Flow explains how a real chip gets its first program into memory, and Memory-Mapped I/O and Registers explains how C reaches a device register. If you built the core in the Computer Architecture tutorial, Lab Tour: Compiling C for RISC-V covers the compiler side in more depth.
The Program
The SoC's test programs are ordinary C, written for this memory map. They do not use a C library; a small header, cvsoc.h, gives names to the register addresses and provides three tiny drivers. This is hello, the program that touches every block once:
#include "cvsoc.h"
#define N 256
static u32 work(volatile u32 *buf) /* fill a buffer, then checksum it */
{
u32 seed = 42;
for (u32 i = 0; i < N; i++)
buf[i] = xs32(&seed);
u32 c = 0;
for (u32 i = 0; i < N; i++)
c = (c << 3 | c >> 29) ^ buf[i];
return c;
}
u32 main(void)
{
uart_puts("Hello from cvsoc!\n");
u32 t0 = TIMER_LO; /* read the timer */
GPIO_OUT = 0xA5; /* drive a pattern on the pins */
u32 in = GPIO_IN; /* read the input pins */
u32 c = work((volatile u32 *)SRAM_BASE);
u32 t1 = TIMER_LO;
uart_puts("checksum ");
uart_hex(c);
uart_puts(", gpio_in ");
uart_hex(in);
uart_puts(", cycles ");
uart_hex(t1 - t0);
uart_puts("\n");
uart_wait_idle(); /* let the last character leave the line */
return c;
}
Each register is a fixed address. TIMER_LO is defined as the word at 0x4000_1000, GPIO_OUT as the word at 0x4000_2000, SRAM_BASE as 0x2000_0000, and every access goes through a volatile pointer so that the compiler performs it exactly as written. Memory-Mapped I/O and Registers explains why volatile matters. The UART driver is a loop: before each character it reads the UART's STATUS register and waits while the FIFO is full, then writes the character to TXDATA.
The program's return value matters. The test knows the correct checksum because the same work function was compiled for a PC and run there; the SoC must produce the same number.
From C to a Memory Image
The program is compiled with the GNU compiler for RISC-V, for the base integer instruction set only (-march=rv32i) and with optimization on (-O2). Two more files go into every build:
- The start-up code,
crt0.S, a few instructions of assembly that run beforemain. - The linker script,
link.ld, which tells the linker where in memory each part of the program goes. Here everything goes into the TCM, starting at address 0: first the start-up code, then the rest of the code, then the constant strings.
The result for hello is 472 bytes of code and 62 bytes of constant strings, 134 words in all. The build turns them into a list of 32-bit words, word 0 being the instruction at address 0. That list is what a test loads.
The start-up code is short enough to read in full in the compiled listing:
00000000 <_start>: 0: 00010137 lui sp,0x10 # stack pointer = 0x0001_0000, the top of the TCM 4: 00000097 auipc ra,0x0 8: 090080e7 jalr 144(ra) # call main c: 4000f2b7 lui t0,0x4000f 10: 00a2a023 sw a0,0(t0) # store main's return value to EXIT (0x4000_F000) 14: 0000006f j 14 # then wait here forever
It sets the stack pointer to the top of the 64 KB TCM, so the stack grows down from there, calls main, and stores main's return value to the EXIT register of the simulation control block. In a real product the last two lines would not exist: main never returns, or returns to a boot loader. In a test, they are how the program says "I am done, and this is my answer".
Loading the Program
A real chip gets its first program from a boot ROM that copies it from flash, as Boot ROM and the Boot Flow describes. A test does not need to simulate that. The testbench writes the image straight into the TCM's storage array before it releases reset, through the design's hierarchy: the array mem inside the instance u_tcm inside the instance u_cpu. This is called backdoor loading, and it saves thousands of cycles in every test. It is also the reason the exercises fix those three names: a test can only load your TCM if it can find it.
Then the testbench releases reset. The core's program counter starts at 0x0000_0000, so the first instruction it fetches is the start-up code's lui sp,0x10.
Printing and Ending
The UART sends each character as a sequence of bits on one wire, uart_tx. The testbench watches that wire the way a terminal would: it waits for the start bit, samples the eight data bits in the middle of each bit time, and prints the character. It follows the program's writes to the UART's DIV register, so it always samples at the right speed. What it prints is the first thing you see in the log:
[uart] --- terminal output --- Hello from cvsoc! checksum 73d8d848, gpio_in 0000c0de, cycles 0000181e [uart] --- end of terminal output (71 bytes) ---
The input pins read 0x0000_C0DE because the testbench drives them with that value, like a board would. The last number is the program's own measurement: the timer counted 0x181e, that is 6,174 clock cycles, between the two reads of TIMER_LO.
When the program stores to the EXIT register at 0x4000_F000, the simulation control block raises sim_done and keeps the stored value on sim_code. The testbench sees that, compares the value with the expected checksum and prints its report. The simulation control block is not part of any real chip; it is the test's way of hearing from the program. It is the one block you do not build: the tests provide it.
Quick Check
Q: A program forgets to call uart_wait_idle() before it returns. What would the terminal output look like, and why?
Show Answer
The last characters would be missing. When the program writes a character to TXDATA, the UART only queues it; it takes many cycles to leave the line. Without the wait, main returns, the start-up code stores to EXIT, and the test ends while bytes are still in the UART's FIFO or on the wire.
The System Report
After the terminal output the testbench prints a report about the run. It counts at the core's data port: every load and store the core performs, by the block it went to, and how many cycles each took until the core could go on. This is the report of hello on the reference design:
==================== cvsoc system report ==================== workload : hello result : 0x73d8d848 (expected 0x73d8d848) PASS cycles : 16265 instructions : 7666 CPI : 2.122 core waiting on the bus: 7046 cycles (43.3%) -------------------- data traffic by target ------------------ target base reads writes cycles/access TCM 0x0000_0000 57 5 1.00 SRAM 0x2000_0000 256 256 4.00 UART 0x4000_0000 1026 71 6.00 TIMER 0x4000_1000 2 0 6.00 GPIO 0x4000_2000 1 1 6.00 SIMCTRL 0x4000_F000 0 1 6.00 unmapped - 0 0 - bus errors : 0 gpio_out : 0x000000a5 first UART byte : cycle 36 after reset ==============================================================
Read it from the top:
- result: the value stored to EXIT and the value the test expected. PASS means they match.
- cycles and instructions: clock cycles from reset to the store to EXIT, and instructions completed in that time. Both include the EXIT store itself. CPI, cycles per instruction, is their ratio.
- core waiting on the bus: cycles in which the core could not go on because a load or store was still travelling on the bus.
- data traffic by target: loads and stores per block, and the average number of cycles from the start of an access until the core could go on. Instruction fetches are not counted here; they all come from the TCM.
- bus errors: responses that were not OKAY. first UART byte: the cycle in which the first start bit appeared on the line.
The cycles per access tell you how far each block is from the core. The TCM sits next to the core and answers in the same cycle. The SRAM is one AXI4-Lite hop away: the manager port, the interconnect and the SRAM each add a cycle or two, and a store or load there takes 4 cycles. The peripherals are one hop further, behind the APB bridge, which adds a SETUP and an ACCESS phase: 6 cycles. You will build every one of those cycles, and in Measuring Your SoC you will account for them.
The counts tell you where the time goes. The 512 SRAM accesses are the buffer: 256 words written and 256 read back. The 1,026 UART reads are the driver checking STATUS: the program writes its 71 characters much faster than the line sends them, so most of the time the FIFO is full and the driver reads STATUS again and again. At 6 cycles per read, that is 6,156 cycles, more than a third of the whole run, spent asking the UART whether it has room. Polling, Interrupts and DMA discusses that cost and the ways around it.
The numbers in Section 1 of this tutorial for the same program were measured with a five-stage core, so their cycle counts differ. The access counts are the same, because the program is the same: the core's pipeline changes when accesses happen, not how many there are.
Tracing an Access in the Listing
The compiled listing shows the program's accesses as the core sees them. This is the UART driver, as the compiler produced it:
00000018: 18: 00054683 lbu a3,0(a0) # next character of the string 1c: 02068663 beqz a3,48 # zero: end of the string 20: 40000737 lui a4,0x40000 24: 40000637 lui a2,0x40000 # a2 = 0x4000_0000, TXDATA 28: 00470713 addi a4,a4,4 # a4 = 0x4000_0004, STATUS 2c: 00150513 addi a0,a0,1 30: 00072783 lw a5,0(a4) # read STATUS 34: 0017f793 andi a5,a5,1 # bit 0: FIFO full 38: fe079ce3 bnez a5,30 # full: read it again 3c: 00d62023 sw a3,0(a2) # write the character to TXDATA 40: 00054683 lbu a3,0(a0) 44: fe0694e3 bnez a3,2c 48: 00008067 ret
The loop from 0x30 to 0x38 is where the 1,026 STATUS reads come from. Each pass sends one load out through the manager port, the interconnect and the bridge to the UART, and the core waits 6 cycles for the answer. When a system test fails, this is how you find your way: pick the access that went wrong in the waveform, find its address, and find the instruction in the listing that made it.
Where Does This Matter?
Every SoC team runs software tests in simulation long before the chip exists, and they work the way this one does: a program image loaded into memory through the hierarchy, a virtual terminal on the UART, and a "test passed" register that real silicon does not have. The same programs later run on an FPGA prototype and finally on the first chips back from the factory, where a debugger or a boot ROM loads them instead. SoC-Level Verification Strategy puts this kind of test in context.
Quick Check
Q: The report says the SRAM was read 256 times and written 256 times. Which lines of hello.c cause them, and what would change if buf were in the TCM instead?
Show Answer
The first loop in work writes the 256 words and the second reads them back. In the TCM, the same 512 accesses would take 1 cycle each instead of 4, so the program would wait for the bus 3 x 512 = 1,536 cycles less. The checksum would be the same.
Common Mistakes
Mistake #1: Renaming the TCM's array or instances
The testbench loads the program through u_cpu.u_tcm.mem. With any other name the load fails to compile, or, worse, a design that keeps an array under a different name runs from an empty memory and never reaches the EXIT store.
Mistake #2: Reading the cycle count as the core's speed alone
The cycle count includes every cycle the core spent waiting for the bus and every cycle the program spent polling a slow device. A faster core would not make hello much faster; a faster UART or a program that does not poll would.
Your Turn in the Lab
There is nothing to submit for this article. Open exercise S16, The Complete SoC, in ChipVerify Lab and read its visible test, testbench.v. Find three things in it: the lines that load the program into u_cpu.u_tcm.mem before reset is released, the terminal that decodes uart_tx, and the check that runs when sim_done rises. You will run that test on your own SoC at the end of chapter 13.
Summary
- Test programs are plain C compiled for rv32i with a start-up file and a linker script that place everything in the TCM from address 0;
hellois 134 words. - The start-up code sets the stack to the top of the TCM, calls
mainand stores its return value to the EXIT register at 0x4000_F000, which ends the test. - The testbench loads the image straight into
u_cpu.u_tcm.mem, decodes the UART line like a terminal and compares the result with the value the same C code gives on a PC. - The system report counts cycles, instructions, bus waiting and data accesses by target: 1 cycle per TCM access, 4 per SRAM access and 6 per peripheral access on the reference design.
- For
hello, polling the UART's STATUS register costs more than a third of the run; the listing shows the loop that does it.