Introduction
A HES file holds music from the PC Engine (TurboGrafx-16 in North America). It does not store notes or audio: it stores the original HuC6280 machine code and data that played the tune, taken from a game, plus a 16-byte header that says where the code starts and how the processor’s memory is mapped. A player puts the data into a model of the console’s memory, calls the tune’s init routine with the number of the song in the accumulator, and lets the processor run. The routine sets the timer and the video chip’s vertical-blank interrupt going; the interrupt handlers of the tune then write to the six-voice programmable sound generator (PSG) that makes the sound. A player has to emulate the processor, the timer, the interrupt registers and the PSG to hear the tune.
The format has no author’s specification. It comes from the HES player of the PC Engine ripping tools and is documented only by the players that read it: Game_Music_Emu and NEZplug++, which agree on the layout. The pages follow their sources, and say where the two differ.
Structure
The file starts with the four letters HESM. After them come a version byte (0), the
start song, the 16-bit little-endian init address, and the eight initial values of the
processor’s mapping registers MPR0 to MPR7. Then come one or more blocks, each the four
letters DATA, a 32-bit size, a 32-bit physical load address, four reserved bytes
(zero) and the data. The data of every block is put into a 1 MB ROM image at its physical
address.
0Magic and version HESM and the version, 05 bytes5Start song and init the first song; the init address3 bytes8Mapping registers MPR0 to MPR78 bytes16DATA block header DATA, size, physical address, 4 reserved bytes16 bytes32Block data the program and the music, in 8 KB pagessize bytes…More blocks each with its own headervaries
The header and DATA block pages give every field. Memory and mapping says what the mapping registers select, INIT, timer and interrupts what a player does before and after calling the init routine, The PSG what the sound registers do, and The HuC6280 the processor.
What the explorer shows
The explorer reads the whole file: every byte of the header and of each DATA block is in the
structure tree, with the mapping registers explained, and a field that breaks a rule of the format is
listed as a problem on the field. It then runs the tune. A HuC6280 is emulated instruction by
instruction, each bus access stamped with the cycle it falls in, with the 8 KB of RAM, the eight mapping
registers, the timer and the interrupt registers, and the VDC reduced to its vertical-blank interrupt.
The init routine is called with the song as the player starts it, and the processor then runs, waiting
at $1FFF between interrupts. The six PSG channels (waveform, DDA, noise and the LFO) are
mixed in stereo, played and drawn as a waveform. The Memory tab shows the mapping registers and the
interrupt vectors, the Trace tab the first instructions executed (with the registers after each) and the first
writes to the sound chip, and the Code tab disassembles every 8 KB page of the data as HuC6280 code,
with the names of the hardware registers and the entry point. The song, the length, the tempo and
the mute switches for the six channels are in the options bar.
A HES file does not say how many songs it has or how long a song lasts: the Song option is a number (the start song of the header is the default) and the Length option defaults to 90 seconds.
The processor’s timing, the T flag, decimal arithmetic, block transfers and the slow-speed switch are modelled as the HuC6280 does them (see The HuC6280 and the divergences below); the sound is the PSG core of the VGM viewer, checked against libvgm.
Divergence from the specification
There is no specification, so what follows is where the two player sources and the real hardware (as the Mednafen emulator models it) differ, and what the explorer does.
- Several DATA blocks. Game_Music_Emu loads only the first DATA block; NEZplug++ loads all of them. The explorer loads all of them, in file order, later blocks over earlier ones.
- A wrong block size. Game_Music_Emu takes the block size as given and tolerates a size that runs past the end of the file; many files have one. The explorer cuts the data at the end of the file and lists the size as a problem.
- Decimal mode. The D flag makes ADC and SBC work in packed decimal on the HuC6280. Game_Music_Emu has no decimal mode and adds in binary; the explorer follows Mednafen.
- Branch-on-bit cycles. BBR and BBS take 6 cycles (8 when taken) in the manual and in Mednafen; Game_Music_Emu charges 4 (6 when taken). The explorer uses 6 and 8.
- TSB and TRB flags. N and V come from the memory byte and Z from the result of the AND with A, as Mednafen does; Game_Music_Emu sets them differently.
- Slow mode, the T flag and decimal timing. CSL makes the processor run at 1.79 MHz (every cycle takes four ticks of the 7.16 MHz clock); the T flag adds 3 cycles to ADC, AND, EOR and ORA, and decimal ADC and SBC add 1. Game_Music_Emu ignores all three and always runs at 7.16 MHz. The explorer models them.
- The LFO. Game_Music_Emu has no LFO: it plays channel 2 as an ordinary voice and never modulates channel 1. The explorer’s PSG has the LFO of the hardware (The PSG).
- Noise and level. The noise generator and the overall level of Game_Music_Emu differ from the VGM viewer’s PSG core, which the explorer uses (it follows libvgm and Ootake). The HES player’s level and pitch match Mednafen and libvgm’s MAME core; Game_Music_Emu is about 2.3 times louder by its own gain choice (The PSG, divergence).
- ADPCM. The CD system’s ADPCM unit (The PSG, the ADPCM unit) differs in all three sources: the command bit that starts playback, the sample rate, the decoder (NEZplug++ uses a Y8950 delta-T decoder) and the interrupt flags. The explorer follows Mednafen.
- The video chip and the joypad. A tune sees the video chip only through its vertical-blank interrupt (register 5 bit 3, status read at
$0000). The other VDC registers, the SuperGrafx’s second video chip and the joypad port are accepted and ignored, as Game_Music_Emu does, and read$FF(the status at$0000excepted).
Sources
- Game_Music_Emu 0.6.1 (Shay Green),
gme/Hes_Emu.cpp,Hes_Cpu.cpp,Hes_Apu.cpp,Hes_Apu_Adpcm.cpp: the header, the load, the play model, the timer and the VDC interrupt. - NEZplug++ (AoiMoe),
src/nezvm/m_hes.c,s_hes.c,s_hesad.c: the same format read by a second player; all blocks loaded. - Mednafen’s PC Engine core (as in Beetle PCE),
huc6280.cpp,pce_psg.cpp,pcecd.cppandokiadpcm.cpp: the processor’s flags and cycles, the PSG registers and the ADPCM unit. - The PSG is the core of the VGM viewer, checked against libvgm’s HuC6280.
- The explorer is checked against Game_Music_Emu built from the source above: 90,000 random instructions (registers, cycles and RAM equal) and half-second comparisons of the sound of the sample files. The text fields at offset 0x40 are checked against Game_Music_Emu’s own track information (Header, text at offset 0x40). The sample files are made with a small assembler; the files, their builder and the sources with their SHA-256 are in the site’s research folder.
Links
Software
- Game_Music_Emu – the player library that reads HES, NSF, SPC and others
- Mednafen – multi-system emulator with a PC Engine core whose processor and PSG the explorer follows
Reference
- NEZplug++ – source of the player that reads every DATA block
- Beetle PCE – source of the Mednafen PC Engine core