D1311 of 14
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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, and composite is 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?

Name it

What compiled C expects

  • Instructions: the integer ALU operations with registers and immediates, loads and stores of bytes and words, conditional branches, jal and jalr for calls and returns, and lui/auipc to build addresses. ebreak ends 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 f is auipc ra + jalr ra, saving the return address; ret is jalr x0, 0(ra); arguments and results go in a0 to a7.
  • Startup: _start: call main; ebreak, so returning from main stops 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 232.
  • x0 reads 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
  1. Opcode. The low 7 bits are 0x67, jalr.
  2. Registers. Bits 11 to 7 give rd = 1 (ra), and bits 19 to 15 give rs1 = 1 (ra).
  3. Immediate. Bits 31 to 20 give the immediate 12, so the instruction is jalr ra, 12(ra).
  4. Where it fits. It's the second half of call main: the auipc ra, 0 before it put 0 in ra, and this jumps to address 12 while saving 8 as the return address, the ebreak.

Answer jalr ra, 12(ra).

Worked example

How long does the sieve take on a single-cycle core at 50 MHz?

Show the solution
  1. Cycles. It executes 2339 instructions, one per cycle.
  2. Time at 50 MHz. t=233950MHz=46.8μs
  3. 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?

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.