PurrFX project update 4

Project status overview as of August 18, 2020.

Commits: August 18, 2020.

The library requires an NSF file to generate audio data. So, we need to get experience in creating such a file. We will use the convenient HEX-editor HxD for this:
https://mh-nexus.de/en/downloads.php?product=HxD20

Let's try to make the simplest NSF according to the documentation, step by step:
https://wiki.nesdev.com /w/index.php/NSF


Look at the description of NSF file structure:


 

So let's get started! “NESM”, 0x1A and version number. Everything is simple so far:


 

Let's go further. The number of tracks and the number of starting track. In our case it is 1 and 1:


Let's go further. Load address, initialization address, play address. I assume the first two addresses may be the same. Following documentation we can use $8000 address, so let it be 0x8000 and 0x8000. The initialization function will contain only one command (exit from the function). This means that the following address is $8001 so we can use it as the address of the playback function:


Moving on. The name of the track is 32 bytes long. Let it be “CatNose blank NSF”:


Let's go further. The name of the author and copyright (32 bytes per field). Likewise, nothing complicated:


Playback speed in NTSC mode (2 bytes). The documentation page recommends using $411A value:


Bankswitch init values ​​(8 bytes). Not interested. So let’s write down zeros:


Moving on. Playback speed in PAL mode (2 bytes). The documentation page recommends using $4E1D value:


Almost there! One byte indicating the operating mode (PAL or NTSC). Let it be NTSC mode:


1 byte to indicate extra sound chip Support. We only need APU, so we can use 0x00:


Finally there is one byte reserved for NSF2 format and 3 bytes of data size, which can be left blank:


Next comes the program code. In our case it has two functions. Each functions is represented by a single command (return from subroutine):


So the end of our file will be like this:


Let's try to open this file in NsfLog project, making the necessary changes to the code:


After examining the “out.log” file, we make sure that everything works as it should:


Before moving on, let's make a slightly more complicated NSF file. Let’s program the “slide” effect for the Pulse1 channel. All we need is to turn on the channel and set up its registers in the “init” function, and then in the “play” function do the increment of the pitch value. I prepared the code and it is quite simple:


We have changed the address of the play function, changed the function code and the name of the track. Let's make a copy of the previously created NSF file and make all the necessary changes:


This miniature file (only 169 bytes in size, only 31 bytes of real code) is a real valid NSF file. And your NSF player should be able to play it correctly!

With the experience of creating NSF files, we can go further. We will be able to generate files of the content we need and give them for processing to the “Game Music Emu” library. To do this, I added the ability to load an NSF file from memory to the PurrFX library (after all, we do not want to create a file every time).



Related information:

https://wiki.nesdev.com/w/index.php/NSF

http://obelisk.me.uk/6502/reference.html

 

Project page: https://github.com/TheCatNose/PurrFX

Comments