Raw and GYMX files
A GYM file is one of three things, and a player has to tell which from the first bytes.
- Raw. No header: the commands start at byte 0. The only test is that the first byte is 0 to 3 (Game_Music_Emu calls a file whose first byte is above 3 the wrong type). The Genecyst emulator first wrote the format this way (Sega Retro).
- GYMX, unpacked. The 428-byte header below, then the commands as they are. The packed length is 0.
- GYMX, packed. The same header, then the commands packed as one zlib stream, or as EZPK (the data begins with the letters
EZPK). The packed length is not 0.
The 428 bytes
Numbers are little-endian. Text fields are Latin-1 and zero padded. The commands start at byte 428.
| Offset | Size | Field | Meaning |
|---|---|---|---|
| 0 | 4 | Tag | The ASCII letters GYMX. |
| 4 | 32 | Song | The name of the tune. |
| 36 | 32 | Game | The game it is from. |
| 68 | 32 | Copyright | The publisher or the copyright line. |
| 100 | 32 | Emulator | The program that made the log. |
| 132 | 32 | Dumper | The person who made the log. |
| 164 | 256 | Comment | Free text. |
| 420 | 4 | Loop start | How many frames into the song the loop begins, with the loop ending at the end of the data; 0 means no loop. See Frames, loop and length. |
| 424 | 4 | Packed length | 0 when the commands follow as they are. Otherwise the commands are packed (one zlib stream, or EZPK) and this field is the length of the command stream after unpacking it. |
The text fields
Game_Music_Emu ignores a field that holds the placeholder text that Gens and the YMAMP plug-in write when they know nothing: “Unknown Song”, “Unknown Game”, “Unknown Publisher”, “Unknown Person” (in the dumper field) and “Header added by YMAMP” (in the comment). The explorer shows every field as it is stored.
Packed data
When the packed length is not 0, the bytes from 428 to the end of the file are a zlib stream (RFC 1950: two header bytes, usually 78 DA, deflate data and an Adler-32).
Inflated, they are the command stream of the commands page. The explorer inflates them in the browser; if they cannot be inflated, it lists the problem and shows no commands.
If the data begins with the letters EZPK, it is the EZPK packing below instead.
EZPK
EZPK is the packing of the YMAMP 2.0b plug-in. It is a bzip2 stream whose magic numbers have been disguised, holding the command stream cut into sections, so that the streams of one kind (all the register numbers, all the data bytes) compress better together.
| Offset in the data | Size | Field | Meaning |
|---|---|---|---|
| 0 | 4 | Tag | The ASCII letters EZPK. |
| 4 | 4 | Block length | Little-endian. The low 28 bits are the length of the whole block, these 8 bytes included (in every file read, 428 plus this value is the size of the file). The top four bits are 0 for a bzip2 stream; any other value means a zlib stream (the tests of two readers). |
| 8 | to the end | Stream | bzip2 with changes (below). |
The bzip2 stream
- The three letters
BZhare written as three spaces (20 20 20); the block size digit that follows is as in bzip2. - Each magic number is written with 5 added to every byte: the block magic
31 41 59 26 53 59becomes36 46 5E 2B 58 5E, and the end magic17 72 45 38 50 90becomes1C 77 4A 3D 55 95. The magics fall at any bit position, as in bzip2. - Everything else (the block CRC, the combined CRC, the Huffman tables, the Burrows-Wheeler data) is ordinary bzip2.
The unpacked stream
A header of 36 (0x24) bytes of little-endian numbers, then seven sections one after the other.
| Offset | Meaning |
|---|---|
| 0x00 | Length of the type section: the command byte of every command (00, 01, 02, 03). |
| 0x04 | Length of the reg section: the register byte of every 01 and 02 command. |
| 0x08 | Length of datm: the data byte of every 01 command (YM2612 port 0). |
| 0x0C | Length of dats: the data byte of every 02 command (port 1). |
| 0x10 | Length of sng: the data byte of every 03 command (SN76489). |
| 0x14 | Length of the packed DAC block (0 when there is none). |
| 0x18 | Length of dacm, once unpacked: the data bytes of the 01 commands that write register 2A. |
| 0x1C | Length of dacs, once unpacked: the same for the 02 commands. |
| 0x20 | Offset in the unpacked stream of the DAC block. |
The sections follow the header in the order type, reg, datm, dats, sng, dacm, dacs. When dacm and dacs are not empty, the bytes at the offset in 0x20 are a second EZPK block (the tag, the length word, a stream) that unpacks to dacm and dacs, which replace it.
To rebuild the command stream, read the type section a byte at a time. Copy the byte. For 01 or 02, copy one byte from reg, then one data byte from datm (01) or dats (02), or from dacm (01) or dacs (02) when that register is 2A and the file has a DAC block. For 03, copy one byte from sng. A 00 takes no operands. The result is as long as the packed length of the header says.
Example, the file ss2-007.gym (Modland, Super Shinobi 2): the block length is 1,607 (the file is 428 + 1,607 = 2,035 bytes), the stream inflates to 4,394 bytes, the section lengths are 1,930, 1,212, 801, 411, 4, 0, 0, and the rebuilt commands are 4,358 bytes, the number in the packed field.
The explorer checks every block CRC and the combined CRC, then that the sections are exactly used up and that the result has the length of the packed field; each failure is listed as a problem on the data node.
Rules for a valid file
- A GYMX file is at least 428 bytes long; a raw file starts with a byte from 0 to 3.
- The packed length is 0, or the data unpacks to exactly that many bytes.
- The loop start is 0 or a number of frames less than the number of frames in the song.
- The data is made of whole commands and ends with a frame end (00); see Commands.
In the explorer
The structure tree shows the header with each field. The Tune tab shows the text fields that are not empty, the length, the loop and the counts of commands.
Divergence from the specification
- The vgmrips description words the packed length as the size of the data “if GZipped”. Every packed file read has the length of the data inflated there, and the data is zlib, not gzip. The explorer takes the field as the inflated length and lists a mismatch.
- Game_Music_Emu refuses a packed file. No document describes EZPK; the layout above is read from the code of two readers (MasterBoy’s foobar2000 GYM decoder and gme.library) and checked on 13 real files, of which two unpack to files with the sha256 that gme.library lists. The DAC block (dacm and dacs, a second EZPK block) is read as those two readers read it.
- The two readers differ in one thing: MasterBoy always reads the outer stream as bzip2; gme.library tests the top four bits of the length word. The explorer follows gme.library.
- Raw files have no tag, so a file that starts with 0 to 3 is only likely to be a GYM file. The registry asks for the extension .gym as well and then parses the opening 64 KB as commands.
Sources
- vgmrips, “GYM File Format” (Internet Archive copy of 30 September 2020): the field layout.
- Game_Music_Emu,
gme/Gym_Emu.cpp(check_header,track_info_,load_mem_): the first-byte test, the refusal of packed files and the ignored placeholder texts. - The Modland files
art-alive,fantasia-00,awesome-possum-boss,mega-net-bgm1andbattletech-02: five zlib-packed files, each with the inflated length in the packed field. - A GitHub pull request for the Linamp player (number 235) states the same for Modland’s packed files: strip the 428 bytes, inflate the rest with zlib.
- MasterBoy’s foobar2000 GYM decoder,
psp/gym/gym.cpp(load_ezpk_gym,decomp_dac) and its bzlib (HEADER_COFS = 5); gme.library,src/ezpk.candtools/unezpk.py, withtools/ezpk-expected.sha256: EZPK. - The xbmc4xbox GYMCodec readme: names kpigym 1.0r6+ (2002) and YMAMP 2.0 (1999 to 2000) as the sources of its EZPK support.
- Thirteen Modland files from Super Shinobi 2 (
ss2-001toss2-013): EZPK, each unpacking with every CRC right and no section left over. The hashes are in research/gym/SOURCES.md.