Lite / Engineering Complex Systems / Week 33
From source code to running hardware, and testing the join
The code compiled, the tests passed, and the robot still twitched. Where in the chain did it go wrong?
Why should I care?
Optimising a system means changing code and measuring hardware. Knowing what happens between the two (and where timing, memory and configuration can silently change) is what makes the measurements trustworthy.
- Trace the build chain: source, compiler, assembler, linker, binary image, flashing, boot.
- Explain how optimisation settings change speed, size and debuggability.
- Plan integration tests that check hardware and software together, including timing.
Predict
You switch the compiler from -O0 (no optimisation) to -O2. What usually happens?
- The program does something different
- It runs faster and is often smaller, but is harder to step through in a debugger
- Nothing changes
- It always gets bigger and slower
Guess first, then reveal
Optimisation keeps the program's defined behaviour but reorders, inlines and removes work. Code gets faster (often smaller), and variables may vanish from the debugger. Code with undefined behaviour or missing volatile can then 'break', revealing latent bugs.
Discover
Write the multiplication program for the Nibble machine and step through fetch, decode and execute. Count the instructions executed for 3 × 4. Then think about what a compiler would have to produce from 'c = a * b' for this machine.
What the interactive shows (text version)
In the virtual computer Build's Level 4 the learner writes a short program (LOAD, ADD, STORE, JNZ, HALT) that multiplies by repeated addition and steps through fetch, decode and execute for each instruction. Counting instructions executed links source code to the work the hardware actually does.
Understand
The chain
Compiler: C or Rust → assembly for the target. Assembler: assembly → machine-code object files. Linker: combines objects and libraries, places code and data at real addresses using a linker script (flash here, RAM there). Image: a binary or hex file. Flashing: a programmer writes it into the chip's flash. Boot: the reset vector jumps to start-up code that sets up the stack and memory, then calls main.
Optimisation levels are trade-offs
-O0: slow, large, every variable visible to the debugger. -O2: fast, usually smaller. -Os: smallest. -O3: aggressive inlining, can be larger. On a microcontroller with 64 KB of flash, size may decide. Benchmark the configuration you will ship: timings measured on a debug build can differ greatly.
What changes between builds
Library versions, compiler versions, flags, linker scripts and configuration headers can all change behaviour. Reproducible builds pin them all and record a version or commit hash in the firmware itself, so a measurement can always be traced to the exact binary that produced it.
Integration testing
Unit tests check functions in isolation on a computer. Hardware-in-the-loop tests run the real firmware on the real board while a test rig feeds sensor signals and measures outputs and timing. Test the joins: clock configuration, interrupt latency under load, behaviour at power-up, and what happens when a sensor is unplugged.
Explain it another way
Intuitive
A recipe (source) becomes a set of kitchen instructions (assembly), which become a printed card filed in the right drawer (linking), which a chef follows from the first step (boot). A mistake can creep in at any stage.
Technical
Toolchain: preprocessor → compiler (IR, optimisation passes) → assembler → linker (sections .text, .data, .bss; memory map) → objcopy to .bin/.hex → flash via SWD/JTAG or bootloader. Map files show where every symbol lives and how much memory is used.
Applied
Spacecraft flight software teams keep their whole toolchain under configuration control and re-run full integration test campaigns after any compiler change, because a subtle change in generated code could alter timing.
Worked example 1
A firmware image is 58 KB at -O2 and 49 KB at -Os on a chip with 64 KB flash. The -Os build runs a control loop 12% slower. The loop has 30% timing margin. Which do you ship, and what do you record?
-Os leaves room for growth and the 12% slowdown fits within the 30% margin. Record the compiler version, flags and commit hash in the image, and re-run the timing integration test on the -Os build.
Common mix-ups
If it compiles and the unit tests pass, the firmware works.
✓ Configuration, timing and hardware interaction need integration tests on the real target.Compiler optimisation changes what the program does.
✓ It preserves defined behaviour; failures after optimising usually expose bugs such as missing volatile or undefined behaviour.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Write an integration test plan (five tests) for the greenhouse controller firmware: what is stimulated, what is measured, the pass criterion with a number, and which build configuration is tested.
Reflect
Which stage of the build chain did you know least about before this lesson?
Write your answer on paper or in your own notes. Lite never stores what you write.