D13 · Lesson 11 · A computer of your own
Turning D10's single-cycle core into a processor that runs real compiled C, the instructions a C compiler actually emits, the memory map and calling convention the code assumes, little-endian memory and sign extension, and a Python core checked by running a prime sieve and a CRC
In D10 you built a single-cycle RISC-V core for a dozen instructions and a hand-written program. A computer runs other people's programs, which in practice means whatever a C compiler produces. This project takes the course's own C compiler, the one behind C12's board, compiles small programs with it, and builds the processor that runs them: first the core (this lesson), then its input and output (lesson 12), then a measurement of where its time goes (lesson 13).
- The sieve of Eratosthenes in C, compiled by the course's compiler at -O1, running on the course's RISC-V simulator. Step it and watch the C line and the instruction it became.
print(i)is a store to the address 0x2_0010, andcompositeis an array at 0x1000 in data memory: watch its bytes flip to 1 as the multiples are crossed out.- Count the kinds of instruction in the listing: compiled C uses a small part of the instruction set, and that's all your core has to run.
Predict
The compiled sieve and CRC programs together use how many different RISC-V instructions?
The sieve uses 12 (add, addi, auipc, bge, bne, ebreak, jal, jalr, lbu, lui, sb, sw) and the CRC 15; with the Fibonacci program of lesson 12, 19 in all, under half of RV32I's 40 or so. Compilers lean on a few general instructions, which is the RISC idea D10 and C8 describe.
Name it
What compiled C expects
- Instructions: the integer ALU operations with registers and immediates, loads and stores of bytes and words, conditional branches,
jalandjalrfor calls and returns, andlui/auipcto build addresses.ebreakends the run. - Memory map: program at 0, data at 0x1000, stack growing down from 0x1_0000, devices at 0x2_0000 (lesson 12).
- Calling convention: sp starts at 0x1_0000;
call fisauipc ra+jalr ra, saving the return address;retisjalr x0, 0(ra); arguments and results go in a0 to a7. - Startup:
_start: call main; ebreak, so returning frommainstops the machine with main's result in a0.
Details that bite
- Memory is little-endian: the word 0x1234_5678 at address A has 0x78 at A and 0x12 at A + 3.
- Immediates are sign-extended: a 12-bit field of 0xFFF is −1, not 4095. Branch and jump offsets are relative to the instruction's own address.
- Registers hold 32-bit two's-complement values: wrap results to 32 bits, and compare unsigned (
bgeu,sltu) on the value modulo . x0reads as 0 whatever is written to it.
Checking it
The course's simulator runs the same machine code, so your core must match it exactly: the same final memory, the same values written, the same instruction count.
Worked example
What instruction is 0x00C0_80E7?
Show the solution
- Opcode. The low 7 bits are 0x67,
jalr. - Registers. Bits 11 to 7 give rd = 1 (ra), and bits 19 to 15 give rs1 = 1 (ra).
- Immediate. Bits 31 to 20 give the immediate 12, so the instruction is
jalr ra, 12(ra). - Where it fits. It's the second half of
call main: theauipc ra, 0before it put 0 in ra, and this jumps to address 12 while saving 8 as the return address, theebreak.
Answer jalr ra, 12(ra).
Worked example
How long does the sieve take on a single-cycle core at 50 MHz?
Show the solution
- Cycles. It executes 2339 instructions, one per cycle.
- Time at 50 MHz.
- Next. Lesson 13 asks how a pipelined core would do.
Answer 46.8 µs.
Check
Build a RISC-V core that runs the compiled sieve and CRC programs.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The word DEADBEEF is stored at address A in little-endian memory. What byte is at A + 0?
Check
A 12-bit immediate field holds 800. What value does the instruction use?
Check
A single-cycle core at 10 MHz runs a program of 2339 instructions. How long does it take?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
A multicycle RISC-V core takes 4 cycles for ALU instructions, 5 for loads and 3 for branches and stores. A program is 50% ALU, 20% loads and the rest branches and stores, and runs at 100 MHz.
(a)
What is the average CPI?
(b)
How many million instructions per second is that?
Write units the way you normally would: 1.5 MHz, 1500 kHz and 0.0015 GHz are the same answer.
Check
A program runs 1000 instructions at CPI 1.2 on a 50 MHz core. How long does it take?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Explain why
Why does a processor that runs compiled C need a stack pointer set up before main starts?
Compiled C keeps function return addresses, saved registers and local variables that don't fit in registers on the stack, addressing them relative to sp. The very first thing main or any function may do is subtract from sp and store there. If sp started at 0 or some arbitrary value, those stores would land on top of the program or the data, or outside memory altogether, and the program would corrupt itself. Starting sp at the top of a reserved region of RAM, 0x1_0000 here, with the stack growing down toward the data, gives every function call a safe place to work, which is part of the contract between the compiler and the machine.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.