owned this note
owned this note
Published
Linked with GitHub
# m64 format
Disassembler: https://gist.github.com/simonlindholm/307e73aefed6e1a358931522c291f878
Disassembly of SM64 US sequence 0: https://paste.rs/6za
## Legend
* `u8` means a single byte, interpreted as an unsigned integer
* `s8` means a single byte, interpreted as a signed integer
* `u16` means two bytes, interpreted as a big-endian unsigned integer
* `var` means an unsigned integer which is either one or two bytes depending on the top bit of the first byte. If the bit is unset, it's the same as `u8`; if set, it's the same as `u16` with that bit ignored.
* `addr` means the same as `u16`, interpreted as as index into the sequence data.
* X, Y, Z are the arguments passed to the command
* N is the lower 4 bites of the command, or lower 6 bits for notes
## Sequences
| hex | mnemonic |description|
|--------------------|---------------------------|-----------|
| 0xff | end | End of script, loop or function |
| 0xfe | delay1 | Delay for 1 tick |
| 0xfd, var | delay | Delay for X ticks |
| 0xfc, addr | call | Call a function |
| 0xfb, addr | jump | Jump to a point in the sequence script |
| 0xfa, addr | beqz | Jump if Q == 0, where Q is an s8 used to hold temporary sequence script state |
| 0xf9, addr | bltz | Jump if Q < 0 |
| 0xf8, u8 | loop | Repeat until `loopend` for X iterations (256 if X = 0) |
| 0xf7 | loopend | Loop end marker |
| 0xf5, addr | bgez | Jump if Q >= 0 |
| 0xf2, u8 | reservenotes | Reserve X notes for exclusive use by this sequence (dropping earlier reservations). A limited number of notes can be played at once (configurable per level, usually 16); if more than that are played, an existing one will be stopped and reused. This command can prevent that from happening across sequences. |
| 0xf1 | unreservenotes | Drop all earlier reservations made by this sequence. |
| 0xdf, s8 | transpose | Set transposition in semitones. |
| 0xde, s8 | transposerel | Change transposition by some delta. |
| 0xdd, u8 | settempo | Set tempo in BPM. |
| 0xdc, s8 | addtempo | Change tempo by some delta. |
| 0xdb, u8 | setvol | Set volume scale (0..255 = none to twice normal). |
| 0xda, s8 | changevol | Change volume scale by some delta. |
| 0xd7, u16 | initchannels | Initialize channels (bitmask). This copies mute behavior and note allocation policy. |
| 0xd6, u16 | disablechannels | Uninitialize channels (bitmask). |
| 0xd5, s8 | setmutescale | Set volume multiplier for muted mode (0..127). |
| 0xd4 | mute | Set sequence to muted mode. Depending on what behavior has been set this will do different things; from lowering volume to not running scripts. |
| 0xd3, u8 | setmutebhv | Set mute behavior (bitmask). Bit 0x20 lowers volume, bit 0x40 does something to layers, and bit 0x80 pauses the sequence script. Default mute behavior is to have all of those bits set. |
| 0xd2, addr | setshortnotevelocitytable | Set velocity table for short notes (see further down). Target should be an array of bytes in the sequence data of length 16. |
| 0xd1, addr | setshortnotedurationtable | Set duration table for short notes (see further down). Target should be an array of bytes in the sequence data of length 16. |
| 0xd0, u8 | setnoteallocationpolicy | Set note allocation policy (bitmask). If bit 0x1 is set, it will try to steal notes back from what was previously played on the same layer. If bit 0x2 is set, notes will be allocated exclusively from the channel's reserved notes, else if 0x4 is set, from the channel's or sequence's reserved notes, else if 0x8 is set, from unreserved notes, otherwise from all sources. Not used by the game. |
| 0xcc, u8 | setval | Set Q to a constant. |
| 0xc9, u8 | bitand | Bitand Q by a constant. |
| 0xc8, u8 | subtract | Subtract a constant from Q. |
| 0x90-0x9f, addr | startchannel | Start channel N, with a given channel script. |
| 0x80-0x8f | getvariation | Set Q to the variation bit, which is initially either 0 or 0x80 (i.e. -128, since Q is s8). This bit comes from the sequence load command. For instance, to play the Koopa shell music, the game plays sequence 0x8E, which is the same as sequence 0xE but with the variation bit set. |
| 0x70-0x7f | setvariation | Set the variation bit to Q. |
| 0x50-0x5f | subvariation | Subtract the variation bit from Q. |
| 0x00-0x0f | testchdisabled | Set Q to 1 or 0, depending on whether channel N has been disabled by channel script. |
## Channels
| hex | mnemonic |description|
|--------------------|---------------------------|-----------|
| 0xff | end | End of script, loop or function |
| 0xfe | delay1 | Delay for 1 tick |
| 0xfd, var | delay | Delay for X ticks |
| 0xfc, addr | call | Call a function |
| 0xfb, addr | jump | Jump to a point in the channel script |
| 0xfa, addr | beqz | Jump if Q == 0, where Q is an s8 used to hold temporary channel script state |
| 0xf9, addr | bltz | Jump if Q < 0 |
| 0xf8, u8 | loop | Repeat until `loopend` for X iterations (256 if X = 0) |
| 0xf7 | loopend | Loop end marker |
| 0xf6 | break | Decrease loop/function stack depth by 1. Combine with a jump to break out of a loop. |
| 0xf5, addr | bgez | Jump if Q >= 0 |
| 0xf3 | hang | Infinite delay. |
| 0xf2, u8 | reservenotes | Reserve X notes for exclusive use by this channel (dropping earlier reservations). |
| 0xf1 | unreservenotes | Drop all earlier reservations made by this channel. |
| 0xe4 | dyncall | If Q != -1, make a function call to position dyntable[Q], where dyntable is a channel-specific u16 array which can set by `setdyntable`/`dynsetdyntable`. |
| 0xe3, u8 | setvibratodelay | Set delay until vibrato starts for each note. |
| 0xe2, u8, u8, u8 | setvibratoextentlinear | Set vibrato extent for each note as a function that starts at X, goes up linearly to Y over a time span given by Z, then stays there. |
| 0xe1, u8, u8, u8 | setvibratoratelinear | Like above except for vibrato rate. |
| 0xe0, u8 | setvolscale | Set scale factor for volume (0..255 = none to twice normal). |
| 0xdf, u8 | setvol | Set scale factor #2 for volume (0..255 = none to twice normal). The two scale factors are separately controlled by outer audio logic, e.g. when changing between rooms, when playing sound effects, etc. The game almost exclusively uses setvol, so that should probably be preferred, but setvolscale shows up in a few locations in the sound effect sequence. |
| 0xde, u16 | freqscale | Pitch bend using a raw frequency multiplier X/2^15. |
| 0xdd, u8 | setpan | Set the pan for this channel (0..128) |
| 0xdc, u8 | setpanchanweight | The pan for each note will be computed as W * channel pan + (1 - W) * note pan. This sets W (0..128 maps to 0..1). W defaults to 1.0. |
| 0xdb, s8 | transpose | Set transposition in semitones. |
| 0xda, addr | setenvelope | Set ADSR envelope. See below for format. |
| 0xd9, u8 | setdecayrelease | Set ADSR decay/release rate. |
| 0xd8, u8 | setvibratoextent | Set vibrato extent. |
| 0xd7, u8 | setvibratorate | Set vibrato rate. |
| 0xd6, u8 | setupdatesperframe | Seems like it was meant to set the number of updates per frame? But it doesn't actually do anything. |
| 0xd4, u8 | setreverb | Set amount of reverb (or "dry/wet mix"?). |
| 0xd3, s8 | pitchbend | Pitch bend by <= 1 octave in either direction (-127..127). |
| 0xd2, u8 | setsustain | Set ADSR sustain volume. |
| 0xd1, u8 | setnoteallocationpolicy | Set note allocation policy for channel. See the description for sequence scripts. |
| 0xd0, u8 | stereoheadseteffects | Enable (1) or disable (0) some pan/volume effects related to stereo/headset. I don't know which ones precisely -- pan *is* still taken into account even with this disabled. With this disabled, stereo/headset gets the same output. |
| 0xcc, u8 | setval | Set Q to a constant. |
| 0xcb, addr | readseq | Set Q to sequenceData[X + Q]. |
| 0xca, u8 | setmutebhv | Set mute behavior for this channel. |
| 0xc9, u8 | bitand | Bitand Q by a constant. |
| 0xc8, u8 | subtract | Subtract a constant from Q. |
| 0xc7, u8, addr | writeseq | Overwrite the byte at the given position in the sequence script by (Q + X). |
| 0xc6, u8 | setbank | Switch bank within instrument bank sets, to the X'th bank, 0-indexed, counting from the end. |
| 0xc5 | dynsetdyntable | If Q != -1, set dyntable to point to position dyntable[Q] in the sequence data. |
| 0xc4 | largenoteson | Set notes for this channel to use "large notes" format. SM64 does this for all channels. (See `note` vs. `shortnote` in the layer section below.) |
| 0xc3 | largenotesoff | Set notes for this channel to use "short notes" format. |
| 0xc2, addr | setdyntable | Set dyntable to point to a given u16 array from the sequence data. |
| 0xc1, u8 | setinstr | Set instrument (program?) within bank. 0x80-0x83 set raw waves (sawtooth, triangle, sine, square), 0x7f is special, otherwise 0-indexed. |
| 0xb0-0xbf | dynsetlayer | If Q != -1, start layer N, with layer script given by dyntable[Q]. |
| 0xa0-0xaf | freelayer | Stop layer N. |
| 0x90-0x9f, addr | setlayer | Start layer N, with layer script starting at address X. |
| 0x80-0x8f | ioreadval | Set Q to IO[N], where IO is an array of bytes that can be read/written by both the game and the channel script. If N < 4, IO[N] is then set to -1. |
| 0x70-0x7f | iowriteval | Set IO[N] to Q. |
| 0x60-0x6f | setnotepriority | Set note priority for later notes played on this channel to N. When the game wants to play a note and there aren't any free notes available, a note with smaller or equal priority is discarded. Should be >= 2; 0 and 1 have internal meaning. Defaults to 3. |
| 0x50-0x5f | ioreadvalsub | Decrease Q by IO[N]. |
| 0x40-0x4f, u8 | ioreadval2 | Set Q to IO2[X], where IO2 is the IO array for channel N. |
| 0x30-0x3f, u8 | iowriteval2 | Set IO2[X] to Q, where IO2 is the IO array for channel N. |
| 0x20-0x2f | disablechannel | Disable channel N for the parent sequence. |
| 0x10-0x1f, addr | startchannel | Start channel N for the parent sequence, with channel script starting at address X. |
| 0x00-0x0f | testlayerfinished | Set Q to 1 or 0, depending on whether layer N has been disabled (either forcibly or by finishing its script). |
## Layers
(is this the right name? I've seen "track" used as well)
| hex | mnemonic |description|
|-------------------------|-----------------------------------|-----------|
| 0xff | end | End of script, loop or function |
| 0xfc, addr | call | Call a function |
| 0xfb, addr | jump | Jump to a point in the layer script |
| 0xf8, u8 | loop | Repeat until `loopend` for X iterations (256 if X = 0) |
| 0xf7 | loopend | Loop end marker |
| 0xe0-0xef | setshortnotedurationfromtable | Set the duration for future short notes to table[N], where table defaults to `{229, 203, 177, 151, 139, 126, 113, 100, 87, 74, 61, 48, 36, 23, 10, 0}` and can be set by `setshortnotedurationtable`. |
| 0xd0-0xdf | setshortnotevelocityfromtable | Set the velocity for future short notes to table[N], where table defaults to `{12, 25, 38, 51, 57, 64, 71, 76, 83, 89, 96, 102, 109, 115, 121, 127}` and can be set by `setshortnotevelocitytable`. |
| 0xca, u8 | setpan | Set the pan for this layer (0..128). |
| 0xc9, u8 | setshortnoteduration | Set the duration for future short notes. |
| 0xc8 | disableportamento | Disable portamento for later notes. |
| 0xc7, u8, u8, u8/var | portamento | Enable portamento (aka glissando; continuously sliding pitch). X describes the mode of operation somehow using its 0x80 bit and a value 0-5 in its lower-order bits. Y describes the target pitch. Z gives the duration, and is a u8 if the 0x80 bit is set, otherwise a var. |
| 0xc6, u8 | setinstr | Set instrument/program. Similar to the channel command, except arguments 0x7f and above are not supported. |
| 0xc5 | somethingoff | Turn off some bool related to how notes decay somehow? (This is the default.) |
| 0xc4 | somethingon | Turn on the same thing. |
| 0xc3, var | setshortnotedefaultplaypercentage | Set the default play percentage for short notes (see below). |
| 0xc2, u8 | transpose | Set transposition in semitones. |
| 0xc1, u8 | setshortnotevelocity | Set velocity for future short notes. |
| 0xc0, var | delay | Delay for N ticks |
| 0x00-0x3f, var, u8, u8 | note0 | Play note with play percentage X, velocity Y, duration Z and pitch N. (Is "play percentage" the right term?). Only valid if channel is set to "large notes". |
| 0x40-0x7f, var, u8 | note1 | Play note with play percentage X, velocity Y, duration 0 and pitch N. Only valid if channel is set to "large notes". |
| 0x80-0xbf, u8, u8 | note2 | Play note with velocity X, duration Y, pitch N and the last used play percentage. Only valid if channel is set to "large notes". |
| 0x00-0x3f, var | smallnote0 | Play note with play percentage X and pitch N, with velocity and duration taken from the short notes settings set by setshortnote{velocity,duration} (or the fromtable variants), defaulting to 0 and 0x80 respectively if not set. Only valid if channel is set to "small notes". |
| 0x40-0x7f | smallnote1 | Play note with pitch N and the default play percentage, as set by setshortnotedefaultplaypercentage (there is no built-in default; using it without that command will read uninitialized memory). Velocity and duration is set like in smallnote0. Only valid if channel is set to "small notes". |
| 0x80-0xbf | smallnote2 | Play note with pitch N and the previous play percentage. Velocity and duration is set like in smallnote0. Only valid if channel is set to "small notes". |
## ADSR envelope
ADSR envelopes describe how note volume changes over time, and have the format of any number of pairs of u16's, which have to be 2-byte aligned. Volume starts at 0. Each pair is of one of the following forms:
* 0, anything: stop the note
* -1, anything: sustain the note indefinitely at the same volume
* -2, index: goto that envelope table row
* -3, anything: restart the envelope loop from the beginning, with volume 0
* time, volume: transition to that volume over the given amount of time