|
| 1 | +# APU details |
| 2 | + |
| 3 | +## Pitfalls |
| 4 | + |
| 5 | +- Enabling or disabling a DAC (resetting NR30 bit 7 or writing %0000 |
| 6 | + 0XXX to NRx2 for other channels), adding or removing it using NR51, |
| 7 | + or changing the volume in NR50, will cause an audio pop. (This |
| 8 | + causes a change in DC offset, which is smoothed out by a high-pass |
| 9 | + circuit over time, but still creates a pop) |
| 10 | +- The final output goes through a high-pass filter, which is more |
| 11 | + aggressive on GBA than on GBC, which is more aggressive than on DMG. |
| 12 | + (What this means is that the output is "pulled" towards 0V with |
| 13 | + various degrees of "aggressiveness") |
| 14 | +- When first starting up a pulse channel, it will *always* output a |
| 15 | + (digital) zero. |
| 16 | +- The pulse channels' "duty step" (at which position in the duty |
| 17 | + cycle they are) can't be reset. The exception to this is turning |
| 18 | + off the APU, which causes them to start over from 0 when turning it |
| 19 | + on. |
| 20 | +- Restarting a pulse channel causes its "duty step timer" to reset, |
| 21 | + meaning that "tickling" a pulse channel regularly enough will |
| 22 | + cause its "duty step" to never advance. |
| 23 | +- When restarting CH3, it resumes playing the last 4-bit sample it |
| 24 | + read from wave RAM, or 0 if no sample has been read since APU reset. |
| 25 | + (Sample latching is independent of output level control in NR32.) |
| 26 | + After the latched sample completes, it starts with the second sample |
| 27 | + in wave RAM (low 4 bits of $FF30). The first sample (high 4 bits of |
| 28 | + $FF30) is played last. |
| 29 | +- CH3 output level control does not, in fact, alter the output level. |
| 30 | + It shifts the **digital** value CH3 is outputting (read below), not |
| 31 | + the analog value. |
| 32 | +- On GBA, CH3 is inverted. This causes the channel to output a loud |
| 33 | + spike when disabled; it's a good idea to "remove" the channel |
| 34 | + using NR51 before refreshing wave RAM. |
| 35 | + |
| 36 | +## APU detailed architecture |
| 37 | + |
| 38 | +{{#include imgs/apu_detailed.svg:2:}} |
| 39 | + |
| 40 | +*Source: Lior "LIJI32" Halphon* |
| 41 | + |
| 42 | + |
| 43 | +Not addressed in this infographic is the Game Boy's Channel 5, which is an analog channel driven directly by the VIN pin in the cartridge connector. |
| 44 | + |
| 45 | +The digital outputs of the 4 channels can be read using two undocumented debug registers that were given the unofficial names PCM12 and PCM34, and were introduced in the Game Boy Color. |
| 46 | + |
| 47 | +The digital behavior of the APU can be relatively easily verified due to the fact that on CGB and newer models, all of its outputs (digital waveforms, DAC enable bits, NR50, NR51) can be read in real time. |
| 48 | + |
| 49 | +Can be used to tell if a channel is active. |
| 50 | +A channel is activated by a write to NRx4's MSB. |
| 51 | +The length timer, frequency sweep, and turning off its DAC can all deactivate a channel. |
| 52 | + |
| 53 | +A simple analog amplifier. |
| 54 | +Two 3-bit values from NR50 are used to amplify the two inputs. |
| 55 | +A value of 0 is treated as a volume of 1, and a value of 7 is treated as 8, so the amplifier never mutes a non-silent input. |
| 56 | + |
| 57 | +The 4 DACs are enabled by NR12, NR22, NR30, and NR42. |
| 58 | +If a DAC is enabled, the digital range 0 to F is lineraly translated to the analog range -1 to 1, in arbitrary units. |
| 59 | +If a DAC is disabled, it fades to an analog value of 0. |
| 60 | +The nature of this fade is not entirely deterministic and varies between models. |
| 61 | +On the AGB and newer, the DACs are emulated digitally such that a disabled DAC outputs the same amplitude as an enabled DAC with an input of 0. |
| 62 | +Additionally, CH3's output is inverted on these models. |
| 63 | + |
| 64 | +The mixer takes an 8-bit bitmap from NR51 to sum the 4 channels into two outputs—left and right. |
| 65 | +Each output channel goes through a high-pass filter to remove the DC offset created by inactive channels with an enabled DAC, and off-center waveforms. |
| 66 | + |
| 67 | +### Game Boy, Game Boy Color |
| 68 | + |
| 69 | +Each of the 4 channels work pretty identically. First, there's a |
| 70 | +"generation" circuit, which usually outputs either a 0 or another |
| 71 | +value (CH3 differs in that it can output multiple values, but |
| 72 | +regardless). That value is digital, and can range between 0 and 0xF. |
| 73 | +This is then fed to a |
| 74 | +[DAC](https://en.wikipedia.org/wiki/Digital-to-analog_converter), which |
| 75 | +maps this to an analog value; 7 maps to the lowest (negative) voltage, 0 |
| 76 | +to the highest (positive) one. Finally, all channels are mixed through |
| 77 | +NR51, scaled through NR50, and sent to the output. |
| 78 | + |
| 79 | +Each DAC is controlled independently from the generation circuit. For |
| 80 | +CH3, the DAC is controlled by NR30 bit 7; for other channels, the DAC is |
| 81 | +turned on unless bits 3-7 of NRx2 are reset, and the envelope will be |
| 82 | +set to `[NRx2] >> 4`. (Note: the envelope sweep function changes the |
| 83 | +envelope, but not the value in NRx2! It won't disable the DAC, either.) |
| 84 | +The generation circuits are turned on by restarting them for the first |
| 85 | +time, and this is what sets the corresponding bit in NR52. Yes, it's |
| 86 | +possible to turn on a DAC but not the generation circuit. Finally, |
| 87 | +disabling a DAC also kills the generation circuit. |
| 88 | + |
| 89 | +Note that each DAC has a DC offset, so enabling, disabling, adding to or |
| 90 | +removing from NR51, will all cause an audio pop; changing the volume in |
| 91 | +NR50 will as well. |
| 92 | + |
| 93 | +Finally, all the output goes through a high-pass filter to remove the DC |
| 94 | +offsets from the DACs. |
| 95 | + |
| 96 | +### Game Boy Advance |
| 97 | + |
| 98 | +The APU was reworked pretty heavily for the GBA. Instead of mixing being |
| 99 | +done analogically, it's instead done digitally; then, sound is |
| 100 | +converted to an analog signal and an offset is added (see `SOUNDBIAS` in |
| 101 | +[GBATEK](http://problemkaputt.de/gbatek.htm#gbasoundcontrolregisters) |
| 102 | +for more details). |
| 103 | + |
| 104 | +This means that the GBA APU has no DACs. |
| 105 | +This can be retro-fit into the GB APU model as if all DACs were always on. |
| 106 | + |
| 107 | +## Finer technical explanation |
| 108 | + |
| 109 | +### DIV-APU |
| 110 | + |
| 111 | +A "DIV-APU" counter is increased every time `DIV`'s bit 4 (5 in [double-speed mode](<#FF4D - KEY1 - CGB Mode Only - Prepare Speed Switch>)) goes from 1 to 0, therefore at a frequency of 512 Hz (regardless of whether double-speed is active). |
| 112 | +Thus, the counter can be made to increase faster by writing to `DIV` while the relevant bit is set (which clears `DIV`, and triggers the falling edge). |
| 113 | + |
| 114 | +Some events are tied to the DIV-APU counter: |
| 115 | + |
| 116 | +Event | Every N ticks | Frequency[^div_apu_freq] |
| 117 | +---------------|---------------|------------------------- |
| 118 | +Envelope sweep | 8 | 64 Hz |
| 119 | +Sound length | 2 | 256 Hz |
| 120 | +CH1 freq sweep | 4 | 128 Hz |
| 121 | + |
| 122 | +[^div_apu_freq]: |
| 123 | +Indicated values are under normal operation; the frequencies will obviously differ if writing to `DIV` to increase the counter faster. |
0 commit comments