I also wonder how to inject NSF files into Mega Man 2 that I made with Famitracker.
You cannot (barring extraordinary effort*). You can, however, rewrite your song to work with the format that the game already uses.
You can find a commented disassembly of Mega Man 2's music engine here:
http://bisqwit.iki.fi/jutut/megamansource/mm2music.txtHere are some relevant bits for you:
The entire music engine encompasses bank 12 of the ROM. Each bank is 0x4000 bytes long, and the .nes file has a 0x10 bytes long header. This means that it begins from ROM file offset 12*0x4000+0x10 which is 0x30010, and ends at offset 0x34010.
Any addresses listed in this post starting from the next paragraph will be referring to data inside that section, e.g. address 0x8123 would be 0x30010 + 0x8123 − 0x8000 in the file, which is 0x30133.
Address X is ROM file offset 0x30010 + X − 0x8000. The 0x8000 is there because the cartridge maps any ROM data into the 8000-FFFF space rather than 0000-FFFF. You therefore have to subtract the 0x8000 to get the actual data offset.
The list of songs can be found at address 0x8A50. Each song is a word-size pointer to the song data. For example, the pointer for song 2 (Crashman's song) reads as 0x90C8.
Within that address -- 0x90C8 for Crashman -- you will find the song's header. The song's header begins with byte $0F, followed by five pointers. The four first pointers are points to the song's data for each of the four channels. The fifth pointer refers to the list of vibrato controls used for this song.
In case of Crashman, these five pointers read as: 0x90D3, 0x9270, 0x9391, 0x9430, and 0x947F. If any of these pointers reads as 0, then that channel is not used by the song.
Therefore, the song data for channel 0 (first square wave channel) is at 0x90D3; the song data for channel 1 (second square wave channel) is at 0x9270, song data for channel 2 (triangle) is at 0x9391, song data for channel 3 (noise) is at 0x9430, and the vibrato control data is at 0x947F.
The song channel data is a stream of events.
The following commands are defined:
- Two-byte encoding $00 n. Song speed is set as n frames per tick.
- Two-byte encoding $01 n. Adjusts vibrato parameters by n. Affects all following notes.
- Two-byte encoding $02 n. Selects duty cycle settings. Valid values for n: $00,$40,$80,$C0. Only applicable for squarewave channels.
- Two-byte encoding $03 n. Selects volume and envelope settings. Value n is passed directly to the soundchip; see APU documentation for details. Affects all following notes.
- Two-byte encoding $05 n. Sets note base to n. Value n is added to the note index for any notes (excluding pauses) played on this channel from now.
- One-byte encoding $06. Dotted note: The next note will be played 50% longer than otherwise, i.e. 3/2 of its stated duration.
- Three-byte encoding $07 n m. Sets volume curve settings. Byte n controls the attack, and byte m controls the decay. Affects all following notes.
- Two-byte encoding $08 n. Select vibrato entry n from the vibrato table referred to by the song header. Affects all following notes
- One-byte encoding $09. Ends the track. Can be omitted if the track ends in an infinite loop instead.
- Four-byte encoding $04 n w. Ends a loop. If n=0, loop is infinite. Otherwise the marked section plays for n+1 times. w is a 16-bit pointer to the beginning of the loop. Finite loops cannot be nested.
- One-byte encoding $20+n. Note delay (n=0-7): Delays the next note by n ticks, without affecting its overall timing. (I.e. plays silence for the first n ticks of the note.)
- One-byte encoding $30. Triplet: The next note will be played at 2/3 of its stated duration.
- One-byte encoding: m*0x20 + n. Play note (m=2..7). If n=0, plays pause. Otherwise plays note n (note base is added to n). The lowest note that can be played is C-0 (n+base=0). Note or pause length is 2m−1 ticks, possibly altered by the triplet / dotted modifiers. The next event will be read only after this note/pause is done playing.
All songs in Mega Man 2 must adhere to this format.
For example, Crashman song channel 0 begins with: 00 06 03 3C 07 8A 10 02 40 05 17 88 A0 85 A6 87..
Which translates to:
Set speed 06;
Set volume and env as $3C
Set volume curve as $8A,$10
Set dutycycle as $40
Set note base offset as $17 (decimal 23, which is 1*12+11)
Play note 8 (+23 = 31, which is G-2) for 8 ticks
Pause for 16 ticks
Play note 5 (+23 = 28, which is E-2) for 8 ticks
Play note 6 (+23 = 29, which is F-2) for 16 ticks
Play note 7 (+23 = 30, which is F#2) for 8 ticks
If all of this flew straight above your head without being intercepted, you will first need to learn the basics of hexadecimal numbers, hex-editing, and pointers, before you can move on.
(Now
this is the level of documentation I would hope to get for Simon's Quest sound engine...)
P.S. Mega Man 1 follows this exactly same format, but the location is bank 4, and the first $1000 bytes of that bank are occupied by ending data. The song table is at 0x9A60. There are some other Capcom games that also use this engine, but I don't remember which ones they are. Megaman 3-6 use a different engine.
*) The NSF files generated by Famitracker are holistic systems that take up as much space as a small game. You can't just insert an entire game into another game.