The device models¶
Chikuma can only be checked against the original firmware when both run on a machine that behaves
like the iPod's hardware. That machine is qemu-ipod/, a QEMU port for the S5L8702, with a model for
every peripheral the firmware touches. On the other side of the bus, Chikuma has a driver for each
piece of hardware.
Why the machine is QEMU, and why the models are in C¶
Before QEMU, the oracle was a Python emulator built on Unicorn, measured against the device. Unicorn shares QEMU's CPU core, so the processor and the memory management behave the same in both.
Interrupt timing is the difference. Unicorn delivers an interrupt only at the boundary of an execution slice. Real hardware raises the line the instant a device asserts it, and QEMU does the same.
Slice-boundary delivery hides any case where an interrupt handler runs in the middle of something else. An audio interrupt can acknowledge an event a polling loop still waits for. A bus-completion signal has to arrive at the same instant on two paths. An interrupt can land in the middle of a kernel service. Under Unicorn none of these shows.
QEMU is also faster while staying exact. With instruction-counted timing and an inline counter it runs faster than the real device, and far faster than Unicorn's accurate mode.
QEMU has one structural cost. A QEMU tracing plugin can observe a memory access but cannot supply
the value a read returns. So the device models cannot live in a plugin, and qemu-ipod/devices/
holds them as C devices compiled into the machine.
The models¶
Each model carries the measured behaviour the Unicorn emulator had captured.
| Block | What the model covers |
|---|---|
| PMU | the Philips/NXP power-management chip, an I2C slave, with its real-time-clock block and the backlight and battery paths |
| I2C | the on-chip controller, publishing completion two ways from one state machine: a status register for a poller and an interrupt line for a driver waiting on the message, both at the same instant |
| LCD panel | both command sets the firmware uses: the MIPI display-command set the bootloader speaks, and the single-register set the diagnostics use |
| Display pipe | a multi-layer compositor with per-window alpha, and the panel tear-effect interrupt firing at 216 Hz |
| Touchwheel | the polled command/response path, and the unsolicited interrupt packet reporting buttons, scroll position and touch |
| ATA | the disk, backed by a real image file, with programmed I/O and DMA, and a mode that refuses any command not covering a whole 4 KiB physical sector, as the development unit's drive does |
| CE-ATA | the disk behind the SD controller, for boards whose strap says they carry one |
| DWC2 USB | the Synopsys DesignWare USB 2.0 OTG controller, as the device (gadget) half instead of QEMU's stock host controller |
| Interrupt controllers | the vectored interrupt controller, and the external one delivering each group on its own line |
| Timer | the 16-bit and 32-bit blocks on two separate interrupt lines, as on the hardware |
| The rest | DMA controller, AES, SHA-1, SPI NOR, the audio and video decoders, I2S, the dock UART, the headphone-remote chip, the piezo |
Accessories and iAP covers what runs on top of USB.
The drivers¶
Chikuma's drivers live in chikuma/drivers/hw/, in Zig. They were ported from C one file at a time,
each one keeping its name and signature so it could replace its predecessor in place.
The same drivers run on the real iPod, which is the other witness. Where the device and the model disagree, the model gets tightened until it refuses what the hardware refuses. Emulator vs hardware keeps that ledger.
I2C¶
The bus has a mutex, declared OURS, because the firmware's own lock for this bus was not recovered. Without it, another task can preempt a battery reading mid-transaction, and the battery indicator reads empty, then flashes full.
A transfer sleeps on the controller's completion interrupt, as the firmware's does. Only the transfers made before the scheduler starts still poll.
RTC¶
The clock comes from the PMU's real-time-clock registers, seven of them, BCD-encoded. The clock screens get the correct time from them.
Clockgate¶
Clockgate turns individual hardware blocks on and off. A cleared bit runs a block, an inversion that had to be confirmed by behaviour.
Battery and charger¶
The battery driver takes the raw gauge reading, a small integer and no voltage, all the way to the icon frame. Every constant comes from the image, except one inversion declared OURS.
The charger driver reports external power and charge state. A charger and a computer are different bits in the PMU, and only the computer wakes the USB stack. The charger chip's pins, as the firmware drives them, agree with its public datasheet.
Rules for the models¶
A model must not invent behaviour the firmware never asks for. Where a bit's meaning has not been read out of the firmware, the model stores it and reads it back without acting on it, and says so at the site.
The second rule dates from the Unicorn emulator. A field that nothing in the image writes belongs to the hardware, and the model must publish it. When the firmware reads a value it never wrote, that value is an input the model owes it. Two audio crashes on the media page come from breaking it.
The gate runs here¶
Chikuma's make verify runs on this machine. The gate drivers in qemu-ipod/tools/ and
qemu-ipod/gates/ boot the machine, press buttons, turn the wheel, and assert against recovered
constants.
Any fault fails the gate and gets printed. That covers a data abort, and any complaint from a device model about how the guest programmed it.
Running it in the emulator shows how to drive the machine by hand, and emulator vs hardware lists where the models still differ from the measured hardware.