The chip
The sound generator is part of the HuC6280 processor. It has six channels. Each plays a repeating 32-sample waveform of 5-bit values
from its own wave memory, at a 12-bit frequency, with its own volume and left/right balance. Channels 4 and 5 can play noise instead,
and any channel can be switched to direct output, where the tune writes the sample values itself (used for PCM speech and drums). Channel 1 can modulate
the frequency of channel 0 through the low-frequency oscillator. The registers are at the hardware page
(Memory and mapping) offsets $0800 to $0809.
Registers
| Offset | Register | Bits |
|---|---|---|
$0800 | Channel select | Bits 0 to 2: the channel (0 to 5) that the registers $0802 to $0807 now address. Values 6 and 7 select nothing; writes to the per-channel registers are ignored. |
$0801 | Global balance | Bits 4 to 7 the left volume, bits 0 to 3 the right volume (0 to 15). Mednafen, libvgm’s MAME-derived core and Game_Music_Emu map the 16 steps through the table 0, 3, 5, 7, 9, 11, 13, 15, 16, 19, 21, 23, 25, 27, 29, 31 in units of 1.5 dB, so a step is about 3 dB. The explorer’s core (from Ootake) uses two steps of its own volume table, 2.17 dB. |
$0802, $0803 | Frequency | The low 8 bits, then the high 4 bits, of a 12-bit period. In Mednafen and Game_Music_Emu the waveform advances one sample every period ticks of the chip’s 3.58 MHz clock (the processor clock divided by two), so the pitch is 3,579,545 / (32 × period); Mednafen treats a period of 0 as 4,096 and Game_Music_Emu as the shortest possible. The explorer’s core divides by period − 1 (the pitch is 3,579,545 / (32 × (period − 1))), takes a period of 0 as 4,095 and plays nothing for a period of 1. |
$0804 | Channel control | Bit 7 on: the channel plays. Bit 6: direct output (the channel outputs the last value written to $0806 instead of reading its wave). Bits 7 and 6 together select the mode: 00 write the wave, 01 reset the wave index, 10 play the wave, 11 direct output. Mednafen, Game_Music_Emu and libvgm’s MAME core reset the index when bit 6 goes from 1 to 0; the explorer’s core resets it when bits 7 and 6 are written as 01. Bits 0 to 4: the volume, 0 to 31 (1.5 dB a step in Mednafen and Game_Music_Emu, 1.087 dB in the explorer’s core; 31 is the loudest and 0 the quietest, and in the explorer’s core a volume of 0 is silent). |
$0805 | Channel balance | Left volume in the high nibble, right in the low nibble, like the global balance. The left level of a channel is the sum of the attenuations of global left, channel left and channel volume, clamped at the quietest step (libvgm’s MAME core says this is verified from the patent and the manual). |
$0806 | Wave data | A 5-bit sample (bits 0 to 4) stored at the channel’s write index, which then increases by one (modulo 32). Mednafen, Game_Music_Emu and libvgm’s MAME core store it only while bit 6 of the control register is clear, so a tune writes control $40 (reset the index), then $00, then the 32 values, then control with bit 7 set to play. The explorer’s core stores the value whenever bit 7 is clear, and while the channel plays it overwrites the sample at the current position. With bit 6 set and bit 7 set the value written is the output level. |
$0807 | Noise | Channels 4 and 5 only. Bit 7 on: noise instead of the waveform. Bits 0 to 4: the noise frequency, inverted (31 is the highest rate, 0 the lowest). The shift period in clock cycles is 32 for value 31 and (31 − value) × 64 otherwise in Mednafen, (32 − value) × 64 in Game_Music_Emu and (31 − value) × 64, not below 64, in the explorer’s core. The generator is an 18-bit linear-feedback shift register in Mednafen and libvgm’s MAME core (new bit 17 is the exclusive-or of bits 0, 1, 11, 12 and 17, output bit 0), a 16-bit one with mask $E008 in Game_Music_Emu (output bit 1) and a 15-bit one in the explorer’s core (new bit 14 is bit 0 exclusive-or bit 1, started at $0100). The output is 0 or 31 before the sample is centred. |
$0808 | LFO frequency | An 8-bit multiplier of the modulator’s period: channel 1 steps through its wave once every (its period × this value) ticks. Mednafen treats 0 as 256; the explorer’s core, with a product of 0, leaves the modulator standing still. |
$0809 | LFO control | Bits 0 and 1: the mode (0 off; 1, 2 and 3 add the current sample of channel 1, centred, to channel 0’s period shifted left by 0, 2 and 4 bits, that is times 1, 4 and 16; Mednafen and libvgm’s MAME core subtract 16 from the value, the explorer’s core subtracts the value from 17, so the two modulate in opposite directions). Bit 7: halt, which resets the channel 1 waveform position (Mednafen also stops it while the bit is set). The explorer’s core turns the LFO off when bit 2 is set. While the LFO is on, channel 1 is not heard in libvgm’s cores and in the explorer: it only modulates channel 0. Game_Music_Emu has no LFO. |
Mixing
Each channel’s sample (0 to 31) is centred, scaled by its volume, its balance and the global balance, separately for the left and the right output, and the six channels are added. libvgm’s MAME core centres by subtracting 16; the
explorer’s core stores a wave value v as 17 − v (so the wave is inverted and runs from −14 to 17), a direct-output value as 11 − v (with v not below 6) and noise as 0 or −18. The HES player (explorer/formats/hes/psg.js) changes this to the level of Mednafen and libvgm’s MAME core: the total attenuation is (31 − the scale-table value of the global balance nibble) + (31 − that of the channel balance nibble) + (31 − the channel volume), at most 31 steps of 1.5 dB, a zero volume or a zero balance nibble silences that side, a wave is not lowered for a small period and noise is 0 or −31 at every rate. The full-scale level is 4/3 of the VGM viewer’s, because a full-swing wave is 7,936 units peak to peak there and 10,581 (Mednafen: 62 × 128 × 8/6) or 10,583 (libvgm’s MAME core: 31 × 65,536 / 6 / 32) in the others; scripts/hes/psg-levels.mjs measures the HES player within 1.5 % of the MAME core on the register-level cases of every channel, of volumes 4 to 31, of the balance and main balance nibbles down to 0xBB, of direct output and of noise; at the quietest settings (volume 1, nibbles 8 and 4) the output is a few units and the two differ by up to 7 % (volume 1: 87 %) through rounding. The VGM viewer’s own core, which keeps the Ootake values (steps of 1.087 dB, a direct-output scale of about 1.23, noise at the highest rate halved, a wave of period below 128 at 0.75), is not changed. A channel that has just left direct output fades its last level out to zero over the next ticks. Real hardware adds a high-pass
behaviour and a low-pass filter on the output that the explorer does not model; the explorer’s output is the straight sum, with the chip’s ticks (3,579,545 / 16 a second) averaged into each 44.1 kHz sample.
The ADPCM unit
The CD-ROM system adds a second sound source to the PSG: 64 KB of sample memory and an OKI MSM5205 decoder that turns 4-bit codes into a level. It sits at the
hardware page offsets $1800 to $18FF, so only the HES files of CD games use it. The tune fills the memory through $180A, sets the
addresses, the length and the rate, then starts the playback; the unit reads the memory itself, high nibble first, and adds its output to the PSG’s. The decoder keeps a 12-bit level
(starting at $800) and a step index from 0 to 48 (starting at 0). A code adds or subtracts (bit 3 set) a change worked out from the step: the step divided by 8, plus the step
for bit 2, half of it for bit 1 and a quarter of it for bit 0, each shifted down and truncated by itself (so it is not (2 × magnitude + 1) × step / 8). The index then moves by
−1 for magnitudes 0 to 3 and by +2, +4, +6, +8 for 4 to 7. The 49 steps run from 16 to 1,552 (16, 17, 19, 21, 23, 25, 28 and on, about 1.1 times larger each). The level wraps at 12 bits; the output
drops its two lowest bits because the MSM5205’s converter has 10.
| Offset | Register | What it does (Mednafen) |
|---|---|---|
$1802, $1803 | Interrupt mask and status | A bit of the mask in $1802 lets the matching bit of the status in $1803 raise IRQ2: bit 2 (value 4) is set when the memory is less than half full, bit 3 (value 8) at the end. Reading $1803 flips its bit 1 (the left/right switch of the CD audio). |
$1808, $1809 | Address | The low and high byte of a 16-bit address register. While bit 4 of the last command is set, every write also copies the address into the length counter. |
$180A | Data | A write stores the byte at the write address, which then moves on; the store completes 11 processor ticks later, and a second write inside that time replaces the first. A read returns the byte fetched earlier and starts the next fetch (19 ticks), moving the read address on and counting the length down. |
$180B | DMA | Only the value is kept: the transfer from the CD drive is not emulated. |
$180C | Status (read) | Bit 0: the end was reached. Bit 3: playing. Bit 2: a write is still being stored. Bit 7: a read is still being fetched. |
$180D | Command | Bit 7: reset the unit (the registers, the flags, the level and the index). Bit 5: play; a rising edge starts at the current read address with the level $800, index 0, high nibble first; clearing it stops. Bit 4: copy the address into the length counter now, and keep it there while the bit is set (the length then does not count down); it clears the end flag. Bit 6: stop at the end instead of reading on. A rising edge of bit 3 sets the read address to the address register (bit 2 set) or to one less (bit 2 clear); a rising edge of bit 1 sets the write address to the address register (bit 0 set) or to one less. |
$180E | Rate | Bits 0 to 3 the value n: 32,087.5 / (16 − n) samples a second, from 2,005 to 32,087. |
$180F | Fade | Bit 3 starts a fade of the volume from full to nothing, in 65,536 steps over 6 seconds (bit 2 clear) or 2.5 seconds (bit 2 set); bit 1 chooses the ADPCM (set) or the CD audio (clear). Clearing bit 3 cancels the fade and restores full volume. |
While playing, the unit fetches a byte every second sample. If the length is 0 at that fetch the end flag is set (and, with bit 6 of the command set, the playback stops before decoding), otherwise the length counts down; the half flag follows whether the length is below 32,768. A length of 255 therefore plays 255 bytes, 510 samples.
The explorer is a port of this model (explorer/formats/hes/adpcm.js). The output is the level, centred, times 0.42735 / 8,192 and the fade, held for each sample (Mednafen’s
value for a step, with the gain of its output filter taken into account). Mednafen also smooths the steps with a band-limiting filter and a low-pass filter; the explorer does not, so the samples
have more high-frequency content. A tune that is playing ADPCM keeps IRQ2 working: the processor takes it while a flag allowed by $1802 is set and $1402 bit 0 is clear.
The other players differ. Game_Music_Emu (Hes_Apu_Adpcm.cpp) plays with bit 6 of the command (bit 5 is “repeat”, which it stores but does not use), stores a written byte at once,
takes the rate as 7,159,091 / (32,000 / (16 − n)) ticks a sample, clips the level to −2,048 to 2,047 instead of wrapping, never raises IRQ2, returns bit 0 of $180C set while stopped and
bit 3 while playing, and fades with its own constants (100, 1,500 or 5,000 steps for the values 0/8/12, 14 and 10 in the low nibble of $180F). NEZplug++ (s_hesad.c) uses the
same command bits as Game_Music_Emu but decodes with the Y8950 delta-T unit of its FM core at 7,111 / (16 − n) samples a second, a different decoder from the MSM5205.
In the explorer
The Playback tab renders the song to stereo audio and the options bar has a mute for each of the six channels. The Trace tab lists the first 40 PSG register writes with the tick at which each was made, so the first notes of a tune can be read as the register values above. The sound core is the one the VGM viewer uses for HuC6280 streams: a port of the Ootake core in libvgm, compared with the compiled C code tick by tick.
Divergence from the specification
The open-source emulators do not agree on the noise generator, the exact wave write sequence or the output scaling, and the explorer uses the core of Ootake as libvgm carries it (Ootake_PSG.c), the same one the VGM viewer plays HuC6280 streams with; the HES player uses that core with four changes (period N instead of N − 1, 1.5 dB level steps, Mednafen’s level, full-range waves and noise), listed at the top of explorer/formats/hes/psg.js. Mednafen’s
pce_psg.cpp (whose author tested the hardware) and libvgm’s other core (c6280_mame.c) differ from it in the places named in the register table: the period offset, the noise generator and rate, the direction of the LFO, the index reset and the level steps. The two HES players differ again:
- Game_Music_Emu (through its
Hes_Apu) has no LFO at all, plays the noise with a different shift register, and produces a waveform about 2.3 times louder than Mednafen’s, libvgm’s MAME core and the explorer’s (a full-volume square wave: RMS 0.367 against 0.161, scripts/hes/psg-levels.mjs). Its own source sets the level:Hes_Apu::volume(1.8 / 6)and the gain 1.11 ofHes_Emugive 1.8 / 6 × 1.11 = 0.333 of full scale for one voice; the measured amplitude is about 13 % above that through the band-limited step of its Blip_Buffer. The explorer does not follow that level, because the CD ADPCM unit is scaled to Mednafen’s. - NEZplug++ (
s_hes.c) has an LFO that shifts the modulating sample left by 0, 4 or 8 bits (the other cores 0, 2 and 4), keeps channel 1 audible, stops the modulation while the halt bit is set, and uses a 32-bit shift register whose new bit is bit 17 exclusive-or bit 14 (output bit 17). Its noise rates and levels are its own and it is not used as the explorer’s reference.
A tune that uses neither noise nor the LFO has the same pitch and timing in all of them; the explorer’s timer-tone test file agrees with Game_Music_Emu on pitch and a sawtooth made by direct output agrees closely.
Sources
- libvgm,
emu/cores/Ootake_PSG.c: the core of the explorer (throughexplorer/formats/vgm/huc6280.js, see research/vgm/SOURCES.md). - Mednafen,
pce/pce_psg.cpp: register behaviour, wave memory, noise, LFO, channel numbers above 5. - libvgm,
emu/cores/c6280_mame.c: the register table in its comments and the second reading of the LFO and noise. - Game_Music_Emu 0.6.1,
Hes_Apu.cppandHes_Emu.cpp: the no-LFO divergence and the levels. - NEZplug++,
s_hes.c: the LFO and noise of the third player. - Mednafen,
pce/pcecd.cppandsound/okiadpcm.cpp,okiadpcm.h,okiadpcm-deltatable.h(the Beetle PCE copies are saved in research/hes/dl): the ADPCM registers, flags, delays, fader and decoder. The explorer’s step table equals Mednafen’s 784 entries (scripts/hes/adpcm-check.mjs). - Game_Music_Emu 0.6.1,
Hes_Apu_Adpcm.cpp, and NEZplug++,s_hesad.c: the two other ADPCM implementations.