Before you build a processor, it helps to see exactly what it will have to run. This article is a tour of the Software panel in ChipVerify Lab, which compiles C for RISC-V, and of everything a build produces: the start-up code, the memory layout, the listing of machine instructions and the memory image your core will load. It follows the lab's own template program from source to the last instruction, counts by hand the instructions it executes, and checks that count against a real run of the core you will build. By the end you will know every instruction your first C program asks of your hardware.
15 min read | Beginner to Intermediate
What You'll Learn
- Use the Software panel to compile a C program for RISC-V and find its outputs
- Explain what the generated start-up code, linker script and header do before and around main
- Read a RISC-V listing and connect each instruction to the C code it came from
- Predict how many instructions a small program executes and check the prediction against a run
Before You Start
This section is hands-on and needs Verilog; if you have not used it yet, start with the Verilog Tutorial. This particular article contains no Verilog: it is about the software side, so that the hardware articles that follow have something concrete to aim at.
Two articles from the first half of this tutorial explain the steps you will see here in more depth: What a Compiler Does and Assembling, Linking and Loading. RISC-V Instruction Formats and Encoding helps with the one instruction this article decodes by hand. Build a RISC-V Core: Section Overview explains how this section works.
The Software Panel
ChipVerify Lab is a browser IDE for hardware design. Its left sidebar has one panel per tool: simulation, synthesis, place and route and so on. The Software panel comes after Place and Route, and it compiles C and assembly for RISC-V. It works in any project; you do not need a processor in the project to use it.
Used on its own, the panel compiles your program and shows the result, and nothing runs. Later, when your own core exists, the panel's Attach to Current Design button connects the program to the hardware in the project: every simulation then compiles the program first and loads it into the memory your core reads. That comes in Branches and Jumps, when your core can run C for the first time. For now, the panel is a window into what the compiler does.
The panel has three groups of settings:
- Optimization: how hard the compiler works to make the code fast or small, from
-O0(no optimization, every C statement turned into instructions literally) to-O2and-Os. Compiler Optimization Levels compares them. The examples here use-O2, as the exercises' programs do. - ISA: which instruction set to compile for. For your core it is
rv32i, the 32-bit base integer set with no multiply instruction. - Memory Map: the regions of memory the program may use. The default is one region named
ramof 64 KB starting at address 0x0000_0000, plus two special addresses described below. The map is saved in the project asmemory_map.json.
A free account can compile up to four C and assembly files per program, with one memory region, and has a daily limit on builds. Everything in this section fits within those limits.
The Template Program
A new project's first C file is a template. Here it is exactly as the lab provides it:
/* main.c: runs on the core in this project. memory_map.h is generated from
memory_map.json, and main's return value ends the simulation. */
#include "memory_map.h"
static const char msg[] = "Hello from my own hardware!\n";
int main(void)
{
cv_puts(msg);
return 42;
}
There is no printf, because there is no operating system and no C library: the program runs on bare hardware, which is called bare-metal code. Instead it calls cv_puts, a three-line helper from the generated header memory_map.h:
#define CV_EXIT_ADDR 0xf0000000u
#define CV_CONSOLE_ADDR 0xf0000004u
static inline void cv_putc(char c) { *(volatile unsigned int *)CV_CONSOLE_ADDR = (unsigned char)c; }
static inline void cv_puts(const char *s) { while (*s) cv_putc(*s++); }
Printing one character is a store to address 0xF000_0004. That address is not memory: in the simulation, the testbench watches for stores to it and prints the stored byte. An address that leads to hardware instead of memory is called a memory-mapped address (Memory, Addresses and Endianness introduces the idea). The keyword volatile tells the compiler that every one of these stores matters, so it may not merge them or leave any out. The second special address, 0xF000_0000, ends the program: whatever is stored there is the program's result, and the simulation stops.
What a Build Does
Pressing Build runs the GNU compiler for RISC-V, riscv64-unknown-elf-gcc version 13.2, with a fixed set of options. The important ones are -march=rv32i -mabi=ilp32 (32-bit base instructions, 32-bit int and pointers), -nostdlib -nostartfiles (no C library and no standard start-up code: the lab provides its own) and -ffreestanding (do not assume a C library exists). Before compiling, the lab generates three files from the memory map, and regenerates them on every build:
| File | What it is for |
|---|---|
memory_map.h | addresses for C: the region base and size, the EXIT and CONSOLE addresses, and the small helpers above |
link.ld | the linker script: where code, constant data, variables and the stack go in memory |
crt0.S | the start-up code that runs before main, in assembly |
The linker script puts everything in the one region: first the start-up code, then the rest of the code, then constant data such as msg, then variables. It also defines _stack_top, the address just past the end of the region, 0x0001_0000. The stack grows down from there.
The start-up code is the first thing your core will ever execute. Its job is to set up what C expects to find when main starts:
_start:
la sp, _stack_top
la t0, _data_load # copy initialized data when it loads from another region
la t1, _data_start
la t2, _data_end
beq t0, t1, 2f
1: bgeu t1, t2, 2f
lw a0, 0(t0)
sw a0, 0(t1)
addi t0, t0, 4
addi t1, t1, 4
j 1b
2:
la t1, _bss_start # clear uninitialized data
la t2, _bss_end
3: bgeu t1, t2, 4f
sw zero, 0(t1)
addi t1, t1, 4
j 3b
4:
call main
li t0, 0xf0000000 # end-of-program port
sw a0, 0(t0)
5: j 5b
It loads the stack pointer sp, copies initialized variables if they were loaded somewhere else (with one region, they are already in place, so the first branch skips the copy), clears the uninitialized variables to zero (here there are none), calls main, stores main's return value, which RISC-V returns in register a0, to the EXIT address, and then loops forever on j 5b. The simulation stops at the EXIT store, so that last loop never matters. The Calling Convention and ABI explains why the return value is in a0.
The Build Outputs
A successful build lists its outputs in the panel under Build Output, and each can be viewed or downloaded:
main.elf: the linked program in the standard ELF format, with debug information.main.lst: the listing, a disassembly of every instruction with its address and its 32-bit encoding. It also appears in the Listing tab above the editor.main.hex: the memory image, one 32-bit word per line, word 0 at address 0. This is what a simulated memory loads with the Verilog system task$readmemh.
For the template program the image is 47 words, 188 bytes: the start-up code, main and the 29 bytes of the message (28 characters and the terminating zero), padded to a whole word.
Reading the Listing
The start of the listing is the start-up code, now as real instructions. The assembler turned each la (load address) into two instructions, auipc and addi, because an address does not fit in one instruction's 12-bit immediate. The second one is shown as mv sp,sp, a nickname the disassembler uses for "add zero":
00000000 <_start>: 0: 00010117 auipc sp,0x10 4: 00010113 mv sp,sp 8: 00000297 auipc t0,0x0 c: 0b428293 addi t0,t0,180 # bc <_bss_end>
Each line has the address, the instruction word in hexadecimal and the instruction in assembly. Take the very first word, 0x00010117. Its low 7 bits are 0010111, the opcode of auipc ("add upper immediate to PC"). Bits 11 to 7 are 00010, register 2, which is sp. The top 20 bits are 0x00010. So the instruction computes PC + (0x00010 shifted left by 12) = 0 + 0x0001_0000 and writes it to sp: the stack pointer now holds the top of memory. The next instruction adds 0 to it, because the low 12 bits of the address happen to be zero. Your decoder and immediate generator will do exactly this decoding, in hardware, for every word.
Then comes main. The compiler has inlined cv_puts and cv_putc (copied their code into main instead of calling them), and has noticed something clever about the message:
00000070: 70: f00006b7 lui a3,0xf0000 74: 000007b7 lui a5,0x0 78: 04800713 li a4,72 7c: 09c78793 addi a5,a5,156 # 9c 80: 00468693 addi a3,a3,4 # f0000004 <_stack_top+0xefff0004> 84: 00178793 addi a5,a5,1 88: 00e6a023 sw a4,0(a3) 8c: 0007c703 lbu a4,0(a5) 90: fe071ae3 bnez a4,84 94: 02a00513 li a0,42 98: 00008067 ret
Line by line: a3 is built as 0xF000_0000 + 4, the CONSOLE address. a5 points at msg, at address 0x9C. The first character is known at compile time (it is 'H', 72 in ASCII), so the compiler loads it as a constant into a4 instead of reading it from memory. The loop at 0x84 then moves the pointer to the next character, stores the current one to CONSOLE, loads the next byte with lbu (load byte unsigned) and branches back while it is not zero. When the zero at the end of the string arrives, main puts 42 in a0 and returns with ret, which is jalr through the return address register.
Quick Check
Q: The store to CONSOLE is sw, a 32-bit store, even though only one character is printed. Why does that work, and which bits of the stored word does the testbench print?
Show Answer
cv_putc stores an unsigned int made from the character, so the character is in the low 8 bits and the upper 24 bits are zero. The testbench prints bits 7 to 0 of the stored word.
Counting Instructions by Hand
Because there is no operating system and no hidden code, you can count exactly how many instructions this program executes, from the first fetch to the EXIT store:
| Part | Instructions | Why |
|---|---|---|
| load the stack pointer | 2 | auipc, addi |
| data copy | 7 | three addresses (6 instructions) and the beq that skips the copy |
| clear uninitialized data | 5 | two addresses (4 instructions) and the bgeu that finds nothing to clear |
| call main | 2 | auipc, jalr |
| set up in main | 5 | the two addresses and the first character |
| the print loop | 112 | 4 instructions for each of the 28 characters |
| return 42 | 2 | li, ret |
| end of program | 2 | lui and the sw to EXIT |
| total | 137 |
This exact image was run on the single-cycle reference core that this section's exercises are checked against. It printed "Hello from my own hardware!", stored 42 to EXIT, and executed 137 instructions in 137 clock cycles. A single-cycle core completes one instruction per cycle, so the two numbers are the same; when you pipeline the core in the last chapter they will not be, and you will be able to say exactly why.
The same run also counted 31 redirects: instructions after which the next instruction is not the one at PC + 4. They are the beq and bgeu that skip ahead in the start-up code, the call and the return, and 27 taken loop branches: the loop branch is taken after each of the first 27 characters and falls through after the last.
Where Does This Matter?
Counting instructions from a listing is a real engineering skill. Firmware engineers do it to check that an interrupt handler fits in its time budget, and processor teams do it to check a new core: if a short program that should take 137 instructions takes 138 on the hardware, something is wrong, and the listing says where to look. The core tests in this section work the same way, comparing your core's behavior instruction by instruction with a reference.
What Your Core Must Run
Look at the kinds of instruction in this one program: lui and auipc for addresses, addi for arithmetic with a constant, lw, lbu and sw for memory, beq, bgeu and bne (shown as bnez) for decisions, and jal (shown as j) and jalr for calls and returns. A ten-line C program already needs a bit of everything: arithmetic, memory and control flow.
The seven C programs the core exercises run use 25 different RV32I instructions between them. Together with the assembly test programs, which were written to fill the gaps, the tests use 37: every RV32I instruction except fence, ecall and ebreak, which the core leaves out. The Core's Specification lists them all.
Quick Check
Q: The message is changed to "Hi\n", three characters. How many instructions does the program execute now, and how many redirects?
Show Answer
The loop runs 3 times instead of 28, so 25 fewer iterations of 4 instructions: 137 - 100 = 37 instructions. The loop branch is taken 2 times instead of 27, so the redirects drop from 31 to 6. Everything outside the loop is unchanged.
Common Mistakes
Mistake #1: Expecting printf to work
There is no C library, so printf, malloc and the other standard functions do not exist and the build fails with an "undefined reference". Print with cv_puts and cv_putc, and end with a return value from main or cv_exit.
Mistake #2: Choosing an ISA your core does not have
The core you build implements rv32i only. If a program is compiled for a larger instruction set, for example one with the multiply extension, it contains instructions your decoder does not know, and the program goes wrong at the first one. Keep the ISA setting at rv32i.
Mistake #3: Reading the C instead of the listing
When a program misbehaves on your core, the C source is not what the core ran; the listing is. Optimized code reorders, inlines and removes statements, as the constant 'H' above shows. Debug from the listing and the waveform.
Your Turn in the Lab
This article has no graded exercise. Do the tour yourself instead:
- In ChipVerify Lab, create a new project and open the Software panel. Leave Optimization at
-O2and ISA atrv32i. - Add a file
main.cwith the template program above and press Build. - Open the Listing tab and find
main. Check the loop: four instructions, ending inbnez. - Change the message to "Hi\n" and build again. Predict the new instruction count with the table above before you look: the loop now runs for 3 characters.
Summary
- The Software panel compiles C and assembly for RISC-V with gcc 13.2 (rv32i, no C library) and produces main.elf, the listing main.lst and the memory image main.hex.
- The lab generates the start-up code, linker script and header from the memory map; the start-up code sets the stack, prepares the data, calls main and stores main's result to the EXIT address.
- Printing is a store to the CONSOLE address 0xF000_0004; ending the program is a store to the EXIT address 0xF000_0000.
- The template program executes 137 instructions, a count you can derive from the listing; the single-cycle reference core runs it in 137 cycles.
- Even a tiny C program needs arithmetic, loads and stores, branches and jumps; the test programs together use 37 RV32I instructions.