How sm1 plays music¶
sm1 is the audio engine of the S5L8702, at 0x38500000. It turns a compressed AAC frame into sound
on the dock line-out, and the CPU has a lot to do around it, in a strict order. Every step here was
read in RetailOS 2.0.1's own code or traced while it runs, then reproduced by Chikuma on a real iPod
Classic, where it plays music.
Addresses are Ghidra project addresses in the 2.0.1 OS image (see
Address spaces). Function names are the project's own, given after
reading each body. FUN_xxxxxxxx is a function not named yet.
What sm1 is¶
sm1 is a programmable DSP. Its PrimeCell ID registers (+0xFD0..+0xFFC) read part 0x902,
designer 0x3B (ARM), class 0xD, which makes it an ARM OptimoDE-class data engine. It has no codec of
its own. Before every track the CPU copies a program and its constant tables into the chip's
internal SRAM, and the engine runs that program.
So the ARM code holds no decoder. It configures, feeds and collects. The decoder sits in the image as data, written for the DSP and loaded by the ARM.
Three memories¶
All three are mapped into the CPU's SRAM window:
| Memory | CPU address | Shape | Holds |
|---|---|---|---|
| Program | 0x22020000..0x2202FFFF |
4 lanes × 32 bits = one 128-bit instruction row; lane n at n*0x4000 |
the codec program |
| X data | 0x22030000..0x22036FFF (AAC) |
24-bit values in 32-bit words, X:a at 0x22030000 + a*4 |
Q23 constants: twiddles, the n^(4/3) dequantisation table |
| Y data | 0x22038000..0x2203FFFF |
16-bit, Y:a at 0x22038000 + a*2 |
scale-factor band tables, state, and the output |
The engine reads compressed input from DRAM by DMA, as a bus master. It writes decoded PCM back into its own Y memory, where the CPU picks it up.
Whose program runs on the DSP
Everything the CPU does on this page is reproduced in Chikuma from the original's behaviour. The
DSP program is Chikuma's own. sosumi holds clean-room AAC,
MP3 and ALAC decoders, written in the assembly of x17, the
assembler and emulator whose every encoding was measured by probe programs on silicon.
-Dsm1_sosumi_image links them. A test build can still load the image's own program
(SM1_APPLE_IMAGE=1, generated from the user's own OS image at build time, never committed) as
the oracle for the path.
The codec objects¶
audio_codec_setup (0x2208F458) picks a codec object by codec mode through a jump table. Each
getter is two instructions (ldr r0,[pc]; bx lr) and returns a pointer into a 2 MB non-ARM region of
the image.
| Mode | Codec | Getter | Program rows | X / Y units |
|---|---|---|---|---|
| 0 | AAC | codec_object_aac 0x220AFDBC |
10 stub + 2038 | 0x685 / 0x1DC4 |
| 1 | MP3 | codec_object_mp3 0x2209DB60 |
10 + 1514 | 0x50F / 0x49A |
| 3 | ALAC | 0x220A737C |
10 + 764 | - / 6 |
| 5 | Audible .aa only |
0x220AFDC8 |
10 + 1020 | 0x80 / 0x119F |
| 2, 4, 6+ | uncompressed: WAV 2, AIFF 4, raw PCM 6 | 0x220C0880 |
14 + 96 | - / 0x1019 |
The modes come from the readers that fill a track's entry. An .aa file goes to mode 5, or to MP3
for one of its codec words.
The uncompressed modes have a program, yet the original never fed the engine a WAV or an AIFF track in any run. Its PCM player converts them on the CPU and hands them straight to the output.
Loading an object¶
An object is a list of records, each {kind, selector, unit count, node list}. Kind 0 is
program, 1 is X, 2 is Y. The selector says which pass loads it (see below). Each node carries a
start address, a count and a pointer to its words.
codec_object_apply (0x220A911C) walks the list, and FUN_2208F1BC places each item:
| Kind | Placement |
|---|---|
| program | node index = lane. Word i of a lane goes to row i (pass 1) or row 0x40 + i (pass 2) |
| X | word store at 0x22030000 + a*4 |
| Y | halfword store at 0x22038000 + a*2 |
A table of 18 entries drives the placement, and the same table also goes to the engine (next section), so the engine and the loader agree on the address map.
The store widths are part of the contract. RetailOS never writes a byte into engine memory, and on silicon a byte-wise copy is a different write.
Configuring a track¶
This runs once per track, in this order. Offsets are from 0x38500000.
sequenceDiagram
participant CPU
participant sm1
CPU->>sm1: limits +0xA90, +0xB10
CPU->>sm1: command 0x70 (4, wait for 7, 0x10, 0x60)
CPU->>sm1: drain mailbox (+0x160 count, +0x15C pop)
CPU->>sm1: +0x99C = 3, clear pending (+0x860), +0x858 = 7
CPU->>sm1: 18 table words, +0x938..+0x97C
CPU->>sm1: pass 1: stub program, X data, Y data into SRAM
CPU->>sm1: arm, +0x984 = 0
sm1-->>CPU: event bit 2 in +0xA98
CPU->>sm1: clear, +0xC48 = 4
CPU->>sm1: pass 2: main program, rows 0x40+
CPU->>sm1: parameter block at 0x2203FF00
CPU->>sm1: +0x988 = 0x100, +0x98C = 1, +0x118 = 0, +0xA8C = 4
CPU->>sm1: first packet
The 0x70 command is a sequence¶
sm1_command (0x220C90AC) writes 4, then polls the unit status at +0x114 for 7, ten reads at
most. Then it writes 0x10 and 0x60. A plain store of 0x70 leaves the status at 0.
The table¶
It comes from a template plus per-mode patches, packed by sm1_set_descriptor_word
(0x220C5184). For AAC only four words are non-zero:
| Word | Value |
|---|---|
+0x940 |
0x84006E00 |
+0x960 |
0xC600B800 |
+0x964 |
0x848CBA5D |
+0x968 |
the shared entry that maps the parameter block |
The first three were read off the engine during a real play. The codec type is written nowhere else. Across the whole register file, an AAC track and an MP3 podcast differ only in these words.
The arm is answered by the program¶
Without pass 1 in SRAM the engine never raises the arm event. With it, the event arrives after about seven polls on silicon.
The parameter block¶
It is a set of 16-bit words at 0x2203FF00. Every codec sets FF00 |= 1. AAC adds FF00 |= 6 and
writes the sample-rate index to FF06 (44100 Hz is 4). Without the block, the engine runs but never
finishes the first packet.
The same page is also the boot loader's reboot-reason page. Apple time-shares it, so nothing else should live there.
Feeding a packet¶
The feeder, audio_feed_chunk (0x22006AD0), runs one pass per compressed frame. AAC frames come
straight from the MP4 sample table, with no ADTS header.
sequenceDiagram
participant Feeder
participant sm1
participant ISR as sm1_isr (VIC0 line 18)
Feeder->>sm1: hand over: +0x100C = 0, +0x1008 = 0, +0x400 = 0, wait ready = 0
Feeder->>sm1: re-arm +0x98C = 1 (from packet 2)
Feeder->>sm1: gate: +0x114 & 7 must be 0
Feeder->>sm1: descriptor head into +0x100
sm1->>sm1: DMA the packet, decode, fill a slot
sm1-->>ISR: line 18
ISR->>sm1: +0x400 = 1, wait, +0x1008 = 1
ISR-->>Feeder: wake
ISR->>sm1: ack +0xC48 = 4
Feeder->>sm1: collect the slot, RELEASE it
Feeder->>Feeder: copy PCM out
Feeder->>sm1: hand back: +0x100C = 1
The descriptor¶
It is 16 bytes in DRAM, packed by sm1_descriptor_pack (0x220C8F7C):
The CPU writes the descriptor and the packet through the uncached view. The engine reads DRAM
directly and never sees the D-cache. The chain head written to +0x100 is the descriptor's
physical address with tag 1 (sm1_list_start 0x220839FC).
The rest of a pass¶
| Piece | Behaviour |
|---|---|
| Feed gate | if +0x114 & 7 is not zero, the engine is still busy with the last chain. The original returns and offers the same packet on the next pass. Dropping it loses audio |
| Bit 12 | before it waits, audio_feed_wait (0x220991D0) checks event bit 12 in +0xA98 and, if set, writes +0x804 = 1 and +0x858 = 1 |
| Give-up | the wait is 500 ms. Two timeouts, 10 ms apart, end the track (audio_track_finished 0x220067F0) |
| Handshake | sm1_host_handshake (0x22093C5C). Argument 1, before the packet, hands the engine to the DSP. Argument 0, after the PCM is copied, hands it back. Both go through sm1_set_flags (0x2209FF20), which sets the three scratch words at +0x1004/+0x1008/+0x100C |
The interrupt¶
sm1_isr (0x2202377C) handles VIC0 line 18.
ev = +0xA98 & 0x7FFFFFF
if ev & 4:
if sm1_ready_set(1) == 0: +0x400 = 1, confirmed by re-reading
spin until +0x400 & 3 == 1
+0x1008 = 1
wake the feeder RetailOS: a semaphore
+0xC48 = 4 the ack, on every run
The ack is +0xC48. Acking anything else leaves the line asserted, and the handler runs over and
over: 9,012 times for two events in one measured case.
Collecting the output¶
The output layout belongs to the DSP program. No CPU register write ever carries these addresses.
| Address | Meaning |
|---|---|
0x2203FF28 |
status header: bit 0 = output ready, and the halfword at +2 must be 0 |
0x22038000 |
entry table: +2 ring size, +4 units per buffer |
0x22038000 + i*4 |
slot i: +0xA = filled (1), +0xC = length |
0x22038100 + i*units*2 |
slot i's PCM, 16-bit interleaved stereo |
audio_output_ready (0x220B25D0) reads one slot and then gives it back:
if status bit 0 clear, or header+2 != 0, or entry+0xA != 1: nothing yet
buffer = 0x22038100 + idx * units * 2
length = entry+0xC << 1
entry+0xA = 0 0x220B2678: the slot belongs to the engine again
idx = (idx + 1) % ring size
The release is what makes it play
Without entry+0xA = 0, two packets decode. At the third, the first full AAC frame, the engine
reuses slot 0 and clears bit 22 of +0xA98. The next hand-back store (+0x100C = 1) never
completes, the bus hangs, and the chip resets about a second later. A dump of engine SRAM in the
emulator at that moment, RetailOS against Chikuma, shows everything identical except entry+0xA
of slots 0 to 2 (RetailOS 0, Chikuma 1). With the release, music plays on the device.
The release and the index advance only happen for codec modes 0, 1 and 5. ALAC (mode 3) does neither, and mode 6 uses status bit 1 instead of bit 0.
From PCM to the line-out¶
The CPU copies each slot's PCM into one of two 4096-byte halves at 0x22008C70 and 0x22009C70.
Each half is 1024 stereo frames, 23.2 ms at 44.1 kHz. The UI's clicks are mixed in on top
(audio_mixer_resample_16_mono 0x2210AD84). From there it is fixed hardware.
DMA¶
DMAC0 channel 1 (0x38200120) streams a half to the I2S transmitter, with CONTROL 0x84249800
(2048 16-bit transfers) and CONFIG 0x8A81. Its linked-list node lives at 0x22010020 in uncached
SRAM and points at itself. After a half is refilled, word 0 of the node is pointed at it. The
terminal-count interrupt is VIC0 line 16.
I2S¶
| Register | Value |
|---|---|
| IIS0 base | 0x3CA00000 |
| CLKCON | 1 |
| TXCON | 0x0B100019 |
| divider | 272 for 44.1 kHz |
| TXCOM | 6 ORed in |
| data register | 0x3CA00010 |
| power gate | bit 0x80 of PWRCON1 |
MCLK¶
MCLK is clock domain 6 in CLKCON3 (0x3C50000C). audio_set_sample_rate (0x2209C2A0) gates it
off, sets source and divider, and gates it on again. With bit 15 left set the transmitter never
clocks, and the DMA moves eight words and stops.
The codec chip¶
Which chip is fitted is in the SysCfg Codc record, which the boot loader copies to SysInfo +0x104
(audio_driver_select_by_id 0x220629C0).
Codc |
Part | I2C address | Notes |
|---|---|---|---|
0x4253 |
Cirrus CS42L55 (the test unit) | 0x4A | reset on GPIO 0x1B, then a fixed register sequence (cs42l55_set_power 0x22093D10) and the curve volume_curve_cs42l55 (0x2207DA50) |
0x8970 |
Wolfson-style part | 0x1A |
Drive the wrong one and there is no sound and no error.
Bringing the engine up¶
This happens once per boot, before everything above:
- Open sm1's power gates: bit 2 of PWRCON0, bit 3 of PWRCON1.
- Write its clock divider,
+0x1000 = 3(from the clock setterFUN_2200280C). - Check the signature,
+0xC80 == 0x004E7309. - Enable VIC0 lines 18 and 16.
What only silicon shows¶
The QEMU model of sm1 decodes packets on the host, so it plays without any of the handshake. The path comes from recording every store the original makes to sm1 on the emulator, diffing that against Chikuma, then confirming on the device. Several things only the real chip shows:
- the command dispatcher (the model answers 7 to any command)
- the D-cache between the CPU and a bus master (the emulator has none)
- the DSP program having to sit in SRAM before the arm is answered
- the MCLK gate, and the DMA node in cached memory
- the slot release, which the emulator's SRAM confirms once the device has shown where to look
- a codec change, which must release the old track's last hand-back before the engine is reconfigured, or the engine refuses every later write and each track after is silent
Engine state not yet traced¶
RetailOS saves the engine state after configuring and can restore it instead of reloading. Chikuma reloads on every track. A trace on the device shows the original taking the full setup too for a Next between two AAC tracks, so when the restore is used is still to be found.
RetailOS also skips the hand-over before the first packet after a configure, and the flag that decides it is not traced. Event bits 1, 22 and 26 and the mailbox contents are seen but not understood.