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

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, 0x20 that 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?

Name it

Devices as addresses

AddressDeviceLoadStore
0x2_0000LEDSwhat's shownlights the 8 LEDs
0x2_0004SWITCHESthe 8 switches(ignored)
0x2_0008TIMERcycles since reset
0x2_0010OUTprints 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 ⌊A/4096⌋ (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
  1. Form the address. lui t0, 0x20 makes 0x2_0000, which is LEDS itself.
  2. Store. sw writes a word there, and the decoder sends it to the LED latch, which keeps the low 8 bits.
  3. RAM. Nothing in RAM changes.
  4. 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
  1. Total time. t=102850MHz=20.6μs for 9 bytes.
  2. Per byte. That's 2.3 µs, or 114 instructions, per byte, most of them in the 8-step bit loop.
  3. 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 28 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?

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.