The register bits are on APU registers. This page gives the rules of the three units that shape a channel’s volume and pitch (envelope, sweep, length counter), then the formulae that turn the channel levels into one signal.
Volume envelope
The pulse channels and the noise channel have an envelope unit. It makes either a falling saw (the decay phase of an ADSR), optionally looping, or a constant volume that software can shape. Volume values are practically linear. Each unit holds a start flag, a divider and a decay level counter. It is set by $4000 and $4004 (ddLC VVVV) and $400C (--LC VVVV):
- L (bit 5): length counter halt, also the envelope loop flag.
- C (bit 4): constant volume flag. 0: the volume is the envelope’s decay level; 1: the volume is V.
- V (bits 3 to 0): the constant volume, and the reload value of the divider (the period is V + 1 quarter frames).
- Writing the length register (
$4003,$4007,$400F) sets the start flag.
When the frame counter gives a quarter-frame clock:
- If the start flag is clear, clock the divider.
- Otherwise clear the start flag, set the decay level to 15 and reload the divider’s period at once.
- When the divider is clocked while it is 0, it is reloaded with V and clocks the decay level counter: if the counter is not zero it is decremented; otherwise, if the loop flag is set, it is loaded with 15.
The constant volume flag only picks the volume source: the decay level keeps updating when constant volume is selected. The output of each envelope then passes through further gates: the sweep unit (pulse only), the waveform generator (sequencer or LFSR) and the length counter.
Length counter
It sets how long a channel sounds. Once loaded it counts down unless halted, and the channel is silenced when it reaches 0.
| 0 1 2 3 4 5 6 7 8 9 A B C D E F
-----+----------------------------------------------------------------
00-0F 10,254, 20, 2, 40, 4, 80, 6, 160, 8, 60, 10, 14, 12, 26, 14,
10-1F 12, 16, 24, 18, 48, 20, 96, 22, 192, 24, 72, 26, 16, 28, 32, 30
- The index is the 5-bit value written in the top bits of
$4003,$4007,$400Band$400F. In the table above the column is the low 4 bits of the index and the row is the high bit (row00-0Fis indices 0 to 15, row10-1Fis 16 to 31). The same table is on APU registers. - Clearing a channel’s enable bit in
$4015forces its length counter to 0 and it cannot be changed until enabled again (the old value is lost). Setting the bit has no immediate effect. - When the frame counter clocks it, the counter is decremented unless it is 0 or the halt flag is set.
- In the real APU the channel is silenced when the counter is clocked while already 0 (halt clear). The table values are the real values plus one, so that a model that silences the channel when the counter becomes 0 is right.
- The triangle’s linear counter works differently: it halts the channel when it reaches 0.
Sweep unit (pulse channels)
$4001 and $4005 are EPPP NSSS: E enable, PPP the clock rate (period), N negate (0 adds to the period, sweeping towards lower frequencies; 1 subtracts, towards higher), SSS the shift count. A write also sets the sweep reload flag; it takes effect at the next half-frame clock, not at once.
Target period
Whenever the current period changes (by a register write or by the sweep), the target period is computed at once:
- The change amount is the current 11-bit period shifted right by the shift count, made negative if N is set.
- The target period is the current period plus the change amount, set to 0 if negative.
The two pulse channels have their adders wired differently when the amount is negative. Pulse 1 adds the ones’ complement (-c - 1): negating 20 gives -21. Pulse 2 adds the two’s complement (-c): negating 20 gives -20.
- If the target period is above
$7FFthe channel is silenced. This happens whether or not the sweep is enabled. To switch the sweep off fully, also set the negate flag (for instance write$08), so the target cannot exceed the current period. - A period below 8, set by a write or the sweep, also silences the channel.
Half-frame clock (about 120 Hz NTSC, about 96 Hz PAL per the wiki)
- If the sweep’s divider counter is 0, the sweep is enabled, the shift count is not 0 and the channel is not being silenced by the sweep, the current period is set to the target period.
- If the divider counter is 0 or the reload flag is set, the counter is reloaded with P and the reload flag is cleared; otherwise the counter is decremented.
Pulse sequencer
The sequencer’s timer period is the 11-bit period t plus 1, counted every APU cycle (every second CPU cycle); the 8-step waveform therefore lasts 16 × (t + 1) CPU cycles, and f = fCPU / (16 × (t + 1)). The duty sequences read from the table in the order 0, 7, 6, 5, 4, 3, 2, 1, which is why the output looks as printed:
| Duty | Lookup table | Output waveform |
|---|---|---|
| 0 | 0 0 0 0 0 0 0 1 | 0 1 0 0 0 0 0 0 (12.5%) |
| 1 | 0 0 0 0 0 0 1 1 | 0 1 1 0 0 0 0 0 (25%) |
| 2 | 0 0 0 0 1 1 1 1 | 0 1 1 1 1 0 0 0 (50%) |
| 3 | 1 1 1 1 1 1 0 0 | 1 0 0 1 1 1 1 1 (25% negated) |
A write to $4003 or $4007 resets the sequence position and restarts the envelope. A few famiclones have the two duty bits swapped. A pulse channel sends 0 to the mixer if the sequencer output is 0, the sweep overflow is silencing it, the length counter is 0 or the period is below 8; otherwise it sends the envelope volume (0 to 15).
The mixer
Each channel has its own digital-to-analogue converter, built in a way that makes the output non-linear, so channels affect each other. Games such as Super Mario Bros. and StarTropics use the DMC level ($4011) as a crude volume control for the triangle and noise. The values pulse1, pulse2, triangle and noise are 0 to 15, and dmc is 0 to 127. The output is between 0.0 and 1.0:
output = pulse_out + tnd_out
95.88
pulse_out = ------------------------------
(8128 / (pulse1 + pulse2)) + 100
159.79
tnd_out = -----------------------------------------------------------
1
------------------------------------------------- + 100
(triangle / 8227) + (noise / 12241) + (dmc / 22638)
If all values of a group are 0, the result for that group is 0 (not undefined from the division by zero).
Lookup tables
A 31-entry table for the two pulse channels and a 203-entry table for the rest are fast. The tnd one is within 4% of the formula, using a base unit near the DMC’s DAC, and its numerator is adjusted a little to keep the range:
pulse_table[n] = 95.52 / (8128.0 / n + 100) n = pulse1 + pulse2 (0 to 30)
pulse_out = pulse_table[pulse1 + pulse2]
tnd_table[n] = 163.67 / (24329.0 / n + 100) n = 3 * triangle + 2 * noise + dmc (0 to 202)
tnd_out = tnd_table[3 * triangle + 2 * noise + dmc]
Linear approximation
pulse_out = 0.00752 * (pulse1 + pulse2)
tnd_out = 0.00851 * triangle + 0.00494 * noise + 0.00335 * dmc
This makes DMC samples slightly louder, is otherwise fairly accurate (the wave channels use a small part of the curve) and lowers overall volume to leave headroom for the DMC.
Output filters
- NES: a first-order high-pass filter at 90 Hz, a first-order low-pass filter at 14 kHz, and a second-order low-pass above the audible range (not needed for emulation).
- Famicom: only a first-order high-pass filter at 37 Hz, followed by the properties of the RF modulator and demodulator, which are unknown and vary.
Expansion audio from the cartridge is added after this; see expansion audio. The DMC channel and the register bits are on APU registers.
Sources
- NESdev Wiki: APU Mixer, which cites blargg’s
apu_ref.txt - NESdev Wiki: APU Envelope
- NESdev Wiki: APU Pulse (the “APU Sweep” page redirects to its sweep section)
- NESdev Wiki: APU Length Counter
The NESdev Wiki text is available under CC BY-SA 3.0. This page is written in our own words from the raw wikitext kept in samples/nes/sources/hardware. Not copied: the picture of the analogue output circuit, the wiki’s triangle linear counter text (see APU registers) and the length-table rearrangement by internal ROM order.