APU Envelope
The NES APU volume envelope unit generates a constant volume or a decreasing saw envelope with optional looping.
Each volume envelope unit contains the following: start flag, divider, counter.
$4000 | ddLC.NNNN | Pulse channel 1 duty and envelope (write) |
$4004 | ddLC.NNNN | Pulse channel 2 duty and envelope (write) |
$400C | --LC.NNNN | Noise channel envelope (write) |
bit 5 | --L- ---- | Loop flag (this bit is also the length counter's halt flag) |
bit 4 | ---C ---- | Constant volume flag |
bits 3-0 | ---- NNNN | Envelope parameter. The divider's period is set to N + 1. |
$4003 | LLLL.Lttt | Pulse channel 1 length counter load and timer (write) |
$4007 | LLLL.Lttt | Pulse channel 2 length counter load and timer (write) |
$400F | LLLL.L--- | Noise channel length counter load (write) |
Side effects | Sets start flag |
When clocked by the frame counter, one of two actions occurs: if the start flag is clear, the divider is clocked, otherwise the start flag is cleared, the counter is loaded with 15, and the divider's period is immediately reloaded.
When the divider outputs a clock, one of two actions occurs: If the counter is non-zero, it is decremented, otherwise if the loop flag is set, the counter is loaded with 15.
The envelope unit's volume output depends on the constant volume flag: if set, the envelope parameter directly sets the volume, otherwise the counter's value is the current volume.