D13 · Lesson 12 · A computer of your own
Giving the processor a way to reach the outside world, devices as addresses, decoding which loads and stores belong to memory and which to a device, LEDs, switches, a timer and an output port, why device registers are volatile in C, and a program that times itself
A processor that only computes in memory is invisible: nothing goes in and nothing comes out. Real computers reach their devices with the same loads and stores they use for memory. Certain addresses are wired to an LED latch, a bank of switches, a timer or a serial port instead of RAM, and the address decoder decides which. In C those addresses are pointers to volatile registers, which is exactly how C12's board works. This lesson gives your core the C8 board's devices and runs a program that reads the switches, lights LEDs, prints and times itself.
- A CRC program that reads the TIMER before and after computing, prints the CRC and how many cycles it took, and copies the switches to the LEDs.
- Set the switches and run: the LEDs follow them. The second printed value is the elapsed time in cycles, 1028 here, measured by the program itself.
- In the listing, find the
lui t0, 0x20that builds the device addresses: each device register is a store or load at an offset from 0x2_0000.
Predict
In C, OUT is defined as *(volatile int32_t *)0x20010. If the volatile were left out and a program printed the same variable twice in a row, what might the compiler do?
To the compiler, a plain memory location that's written twice with nothing reading it in between only needs the last write, so it may delete the first. For a device, each write is an event: two prints must be two stores. volatile tells the compiler every access matters and must happen in order (C12).
Name it
Devices as addresses
| Address | Device | Load | Store |
|---|---|---|---|
| 0x2_0000 | LEDS | what's shown | lights the 8 LEDs |
| 0x2_0004 | SWITCHES | the 8 switches | (ignored) |
| 0x2_0008 | TIMER | cycles since reset | |
| 0x2_0010 | OUT | prints a number |
In hardware an address decoder compares the upper address bits with each device's range and enables that device instead of the RAM (D10): a 16-byte block needs the top 28 bits compared. In a simulator the decoder is an if on the address before falling through to memory.
Reaching the addresses
0x2_0010 doesn't fit in a 12-bit immediate, so the compiler builds it as lui t0, 0x20 (t0 = 0x2_0000) and then sw a0, 16(t0). Any address A is lui with (rounded up if the low part is 0x800 or more) plus a 12-bit offset.
Timing with a timer
Reading a free-running timer before and after a piece of code measures it in cycles, which is how firmware is profiled on real boards. Here the timer counts instructions, so the result is the number of instructions between the two reads.
Worked example
How does the compiled program reach the LEDs, and which bytes does a store there change?
Show the solution
- Form the address.
lui t0, 0x20makes 0x2_0000, which is LEDS itself. - Store.
swwrites a word there, and the decoder sends it to the LED latch, which keeps the low 8 bits. - RAM. Nothing in RAM changes.
- Read back. A load from the same address reads the latch back.
Answer through a sw to 0x2_0000, LEDS itself; the LED latch keeps the low 8 bits, and no byte of RAM changes.
Worked example
The program reports 1028 cycles for the CRC. How long is that at 50 MHz on a single-cycle core, and per byte of the message?
Show the solution
- Total time. for 9 bytes.
- Per byte. That's 2.3 µs, or 114 instructions, per byte, most of them in the 8-step bit loop.
- A faster CRC. A table-driven CRC (one lookup per byte) would cut that several times over.
Answer 20.6 µs, about 2.3 µs or 114 instructions per byte.
Check
Give your core memory-mapped LEDs, switches, a timer and an output port.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A store goes to the address 40000008, whose low 12 bits are under 0x800. What upper value does the lui that builds it load?
Check
A device occupies a block of bytes, aligned to its size. How many of the 32 address bits must the decoder compare to select it?
Check
A program's timer reads show 1670 cycles of a 10 MHz clock. How long did the code take?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
A peripheral's registers start at 40000000, one 32-bit register every 4 bytes. What is the address of register 1?
Check
To set bit 5 of a control register with a read-modify-write, what mask do you OR in?
Explain why
Why are devices reached through ordinary loads and stores instead of special instructions?
Using the address space for devices means the processor needs no extra instructions or wiring for each new device: the address decoder routes some addresses to device registers instead of memory, and every existing load, store and addressing mode works on them. Compiled C can reach a device through a pointer just as it reaches a variable, which is how drivers are written. The cost is that the compiler and processor must not treat those addresses like memory, by caching, reordering or removing accesses, which is what volatile in C and uncached address regions in hardware are for.
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.