initial fce ultra 0.81 import
[fceu.git] / Documentation / tech / nsfspec.txt
CommitLineData
c62d2810 1 NES Music Format Spec
2 ---------------------
3
4
5By: Kevin Horton khorton@iquest.net
6
7
8NOTE:
9-----
10
11
12Remember that I am very willing to add stuff and update this spec. If
13you find a new sound chip or other change let me know and I will get back
14with you. E-mail to the above address.
15
16
17V1.61 - 06/27/2000 Updated spec a bit
18V1.60 - 06/01/2000 Updated Sunsoft, MMC5, and Namco chip information
19V1.50 - 05/28/2000 Updated FDS, added Sunsoft and Namco chips
20V1.32 - 11/27/1999 Added MMC5 register locations
21V1.30 - 11/14/1999 Added MMC5 audio bit, added some register info
22V1.20 - 09/12/1999 VRC and FDS prelim sound info added
23V1.00 - 05/11/1999 First official NSF specification file
24
25
26
27This file encompasses a way to transfer NES music data in a small, easy to
28use format.
29
30The basic idea is one rips the music/sound code from an NES game and prepends
31a small header to the data.
32
33A program of some form (6502/sound emulator) then takes the data and loads
34it into the proper place into the 6502's address space, then inits and plays
35the tune.
36
37Here's an overview of the header:
38
39offset # of bytes Function
40----------------------------
41
420000 5 STRING "NESM",01Ah ; denotes an NES sound format file
430005 1 BYTE Version number (currently 01h)
440006 1 BYTE Total songs (1=1 song, 2=2 songs, etc)
450007 1 BYTE Starting song (1= 1st song, 2=2nd song, etc)
460008 2 WORD (lo/hi) load address of data (8000-FFFF)
47000a 2 WORD (lo/hi) init address of data (8000-FFFF)
48000c 2 WORD (lo/hi) play address of data (8000-FFFF)
49000e 32 STRING The name of the song, null terminated
50002e 32 STRING The artist, if known, null terminated
51004e 32 STRING The Copyright holder, null terminated
52006e 2 WORD (lo/hi) speed, in 1/1000000th sec ticks, NTSC (see text)
530070 8 BYTE Bankswitch Init Values (see text, and FDS section)
540078 2 WORD (lo/hi) speed, in 1/1000000th sec ticks, PAL (see text)
55007a 1 BYTE PAL/NTSC bits:
56 bit 0: if clear, this is an NTSC tune
57 bit 0: if set, this is a PAL tune
58 bit 1: if set, this is a dual PAL/NTSC tune
59 bits 2-7: not used. they *must* be 0
60007b 1 BYTE Extra Sound Chip Support
61 bit 0: if set, this song uses VRCVI
62 bit 1: if set, this song uses VRCVII
63 bit 2: if set, this song uses FDS Sound
64 bit 3: if set, this song uses MMC5 audio
65 bit 4: if set, this song uses Namco 106
66 bit 5: if set, this song uses Sunsoft FME-07
67 bits 6,7: future expansion: they *must* be 0
68007c 4 ---- 4 extra bytes for expansion (must be 00h)
690080 nnn ---- The music program/data follows
70
71This may look somewhat familiar; if so that's because this is somewhat
72sorta of based on the PSID file format for C64 music/sound.
73
74
75Loading a tune into RAM
76-----------------------
77
78If offsets 0070h to 0077h have 00h in them, then bankswitching is *not*
79used. If one or more bytes are something other than 00h then bankswitching
80is used. If bankswitching is used then the load address is still used,
81but you now use (ADDRESS AND 0FFFh) to determine where on the first bank
82to load the data.
83
84
85Each bank is 4K in size, and that means there are 8 of them for the
86entire 08000h-0ffffh range in the 6502's address space. You determine where
87in memory the data goes by setting bytes 070h thru 077h in the file.
88These determine the inital bank values that will be used, and hence where
89the data will be loaded into the address space.
90
91Here's an example:
92
93METROID.NSF will be used for the following explaination.
94
95The file is set up like so: (starting at 070h in the file)
96
97
980070: 05 05 05 05 05 05 05 05 - 00 00 00 00 00 00 00 00
990080: ... music data goes here...
100
101Since 0070h-0077h are something other than 00h, then we know that this
102tune uses bankswitching. The load address for the data is specified as
10308000h. We take this AND 0fffh and get 0000h, so we will load data in
104at byte 0 of bank 0, since data is loaded into the banks sequentially
105starting from bank 0 up until the music data is fully loaded.
106
107Metroid has 6 4K banks in it, numbered 0 through 5. The 6502's address
108space has 8 4K bankswitchable blocks on it, starting at 08000h-08fffh,
10909000h-09fffh, 0a000h-0afffh ... 0f000h-0ffffh. Each one of these is 4K in
110size, and the current bank is controlled by writes to 05ff8h thru 05fffh,
111one byte per bank. So, 05ff8h controls the 08000h-08fffh range, 05ff9h
112controls the 09000h-09fffh range, etc. up to 05fffh which controls the
1130f000h-0ffffh range. When the song is loaded into RAM, it is loaded into
114the banks and not the 6502's address space. Once this is done, then the
115bank control registers are written to set up the inital bank values.
116To do this, the value at 0070h in the file is written to 05ff8h, 0071h
117is written to 05ff9h, etc. all the way to 0077h is written to 05fffh.
118This is should be done before every call to the init routine.
119
120If the tune was not bankswitched, then it is simply loaded in at the
121specified load address, until EOF
122
123
124Initalizing a tune
125------------------
126
127This is pretty simple. Load the desired song # into the accumulator,
128minus 1 and set the X register to specify PAL (X=1) or NTSC (X=0).
129If this is a single standard tune (i.e. PAL *or* NTSC but not both)
130then the X register contents should not matter. Once the song # and
131optional PAL/NTSC standard are loaded, simply call the INIT address.
132Once init is done, it should perform an RTS.
133
134
135Playing a tune
136--------------
137
138Once the tune has been initalized, it can now be played. To do this,
139simply call the play address several times a second. How many times
140per second is determined by offsets 006eh and 006fh in the file.
141These bytes denote the speed of playback in 1/1000000ths of a second.
142For the "usual" 60Hz playback rate, set this to 411ah.
143
144To generate a differing playback rate, use this formula:
145
146
147 1000000
148PBRATE= ---------
149 speed
150
151Where PBRATE is the value you stick into 006e/006fh in the file, and
152speed is the desired speed in hertz.
153
154
155"Proper" way to load the tune
156-----------------------------
157
1581) If the tune is bankswitched, go to #3.
159
1602) Load the data into the 6502's address space starting at the specified
161 load address. Go to #4.
162
1633) Load the data into a RAM area, starting at (start_address AND 0fffh).
164
1654) Tune load is done.
166
167
168"Proper" way to init a tune
169---------------------------
170
1711) Clear all RAM at 0000h-07ffh.
172
1732) Clear all RAM at 6000h-7fffh.
174
1753) Init the sound registers by writing 00h to 04000-0400Fh, 10h to 4010h,
176 and 00h to 4011h-4013h.
177
1784) Set volume register 04015h to 00fh.
179
1805) If this is a banked tune, load the bank values from the header into
181 5ff8-5fffh.
182
1836) Set the accumulator and X registers for the desired song.
184
1857) Call the music init routine.
186
187
188"Proper" way to play a tune
189---------------------------
190
1911) Call the play address of the music at periodic intervals determined
192 by the speed words. Which word to use is determined by which mode
193 you are in- PAL or NTSC.
194
195
196Sound Chip Support
197------------------
198
199Byte 007bh of the file stores the sound chip flags. If a particular flag
200is set, those sound registers should be enabled. If the flag is clear,
201then those registers should be disabled.
202
203* VRCVI Uses registers 9000-9002, A000-A002, and B000-B002, write only.
204
205Caveats: 1) The above registers are *write only* and must not disrupt music
206 code that happens to be stored there.
207
208 2) Major caveat: The A0 and A1 lines are flipped on a few games!!
209 If you rip the music and it sounds all funny, flip around
210 the xxx1 and xxx2 register pairs. (i.e. 9001 and 9002) 9000
211 and 9003 can be left untouched. I decided to do this since it
212 would make things easier all around, and this means you only
213 will have to change the music code in a very few places (6).
214 Esper2 and Madara will need this change, while Castlevania 3j
215 will not for instance.
216
217 3) See my VRCVI.TXT doc for a complete register description.
218
219* VRCVII Uses registers 9010 and 9030, write only.
220
221Caveats: 1) Same caveat as #1, above.
222
223 2) See my VRCVII.TXT doc for a complete register description.
224
225* FDS Sound uses registers from 4040 through 4092.
226
227Caveats: 1) 6000-DFFF is assumed to be RAM, since 6000-DFFF is RAM on the
228 FDS. E000-FFFF is usually not included in FDS games because
229 it is the BIOS ROM. However, it can be used on FDS rips to help
230 the ripper (for modified play/init addresses).
231
232 2) Bankswitching operates slightly different on FDS tunes.
233 5FF6 and 5FF7 control the banks 6000-6FFF and 7000-7FFF
234 respectively. NSF header offsets 76h and 77h correspond to
235 *both* 6000-7FFF *AND* E000-FFFF. Keep this in mind!
236
237* MMC5 Sound Uses registers 5000-5015, write only as well as 5205 and 5206,
238 and 5C00-5FF5
239
240Caveats: 1) Generating a proper doc file. Be patient.
241
242 2) 5205 and 5206 are a hardware 8*8 multiplier. The idea being
243 you write your two bytes to be multiplied into 5205 and 5206
244 and after doing so, you read the result back out. Still working
245 on what exactly triggers it (I think a write to either 5205
246 or 5206 triggers the multiply).
247
248 3) 5C00-5FF5 should be RAM to emulate EXRAM while in MMC5 mode.
249
250Note: Thanks to Mamiya for the EXRAM info.
251
252
253* Namco 106 Sound Uses registers 4800 and F800.
254
255 This works similar to VRC7. 4800 is the "data" port which is
256 readable and writable, while F800 is the "address" port and is
257 writable only.
258
259 The address is 7 bits plus a "mode" bit. Bit 7 controls
260 address auto-incrementing. If bit 7 is set, the address will
261 auto-increment after a byte of data is read or written from/to
262 4800.
263
264 $40 ffffffff f:frequency L
265 $42 ffffffff f:frequency M
266 $44 ---sssff f:frequency H s:tone length (8-s)*4 in 4bit-samples
267 $46 tttttttt t:tone address(4bit-address,$41 means high-4bits of $20)
268 $47 -cccvvvv v:linear volume 1+c:number of channels in use($7F only)
269 $40-47:ch1 $48-4F:ch2 ... $78-7F:ch8
270 ch2-ch8 same to ch1
271
272 $00-3F(8ch)...77(1ch) hhhhllll tone data
273 h:odd address data(signed 4bit)
274 l:even address data(signed 4bit)
275
276 real frequency = (f * NES_BASECYCLES) / (40000h * (c+1) * (8-s)*4 * 45)
277 NES_BASECYCLES 21477270(Hz)
278
279Note: Very Special thanks to Mamiya for this information!
280
281
282* Sunsoft FME-07 Sound uses registers C000 and E000
283
284 This is similar to the common AY 3-8910 sound chip that is
285 used on tons of arcade machines, and in the Intellivision.
286
287 C000 is the address port
288 E000 is the data port
289
290 Both are write-only, and behave like the AY 3-8910.
291
292Note: Special thanks to Mamiya for this information as well
293
294
295Caveats
296-------
297
2981) The starting song number and maximum song numbers start counting at
299 1, while the init address of the tune starts counting at 0. To
300 "fix", simply pass the desired song number minus 1 to the init
301 routine.
302
3032) The NTSC speed word is used *only* for NTSC tunes, or dual PAL/NTSC tunes.
304 The PAL speed word is used *only* for PAL tunes, or dual PAL/NTSC tunes.
305
3063) The length of the text in the name, artist, and copyright fields must
307 be 31 characters or less! There has to be at least a single NULL byte
308 (00h) after the text, between fields.
309
3104) If a field is not known (name, artist, copyright) then the field must
311 contain the string "<?>" (without quotes).
312
3135) There should be 8K of RAM present at 6000-7FFFh. MMC5 tunes need RAM at
314 5C00-5FF7 to emulate its EXRAM. 8000-FFFF Should be read-only (not
315 writable) after a tune has loaded. The only time this area should be
316 writable is if an FDS tune is being played.
317
3186) Do not assume the state of *anything* on entry to the init routine
319 except A and X. Y can be anything, as can the flags.
320
3217) Do not assume the state of *anything* on entry to the play routine either.
322 Flags, X, A, and Y could be at any state. I've fixed about 10 tunes
323 because of this problem and the problem, above.
324
3258) The stack sits at 1FFh and grows down. Make sure the tune does not
326 attempt to use 1F0h-1FFh for variables. (Armed Dragon Villigust did and
327 I had to relocate its RAM usage to 2xx)
328
3299) Variables should sit in the 0000h-07FFh area *only*. If the tune writes
330 outside this range, say 1400h this is bad and should be relocated.
331 (Terminator 3 did this and I relocated it to 04xx).
332
333That's it!
334
335
336