Episode 2-5: Understanding and including music
Topics: The last chapter of this introduction episode to C64 coding will be about including SID music into the intro.
Download via dust: $ dust tutorials (select 'first intro')
Github Repository: First Intro on Github
- Episode 2-1: Let's compile and run C64 code
- Episode 2-2: Writing to the C64 Screen
- Episode 2-3: Getting into interrupts
- Episode 2-4: Effects using a table of data
- Episode 2-5: Understanding and including music
Music is essential to a great C64 experience
If you are a C64 enthusiast - and why would you read this blog if this is not the case - you of course love the music generated by the SID-Chip. To me music is equally important to a game or demo as are great looking effects.
Our intro would not be half as cool if I had not included a catchy tune so let's take a look how this music got into the finished intro.
Loading resources
Let's have a look at code/load_resource.asm
We need to get our music into the intro. For this we need to specify a loading address, that is where is the SID stored in memory. I already set up a symbol earlier in init_symbols.asm which assigns $1000 to the symbol address_music . With * = address_music we therefor tell ACME that the next instruction should be put in memory at $1000.
This instruction is once again a very handy pseudo opcode by ACME. !bin loads a file from your harddisk into your C64 code. It also allows for a few parameters we need to work with SID files as provided on the net, e.g. from the HVSC database. Those SIDs come with a special header with some extra information for various cross platform music players which we need to remove when importing the file into our codebase. Luckily the !bin command lets us remove that extra information.
Once we loaded the resource, how do we playback the SID? This is a two-step process. First we need to initialize the music replay routine and then we need to trigger the actual music player routine with every screen refresh. Do do either, we need to know the start address of the initialization routine and the start address of the replay routine. That information is encoded within the SID. A very common init address for SIDs is $1000 and for the playback routine $1003. The command line tool sidreloc which is installed by DUST can be used to move the SID so init address and play routine address can be adjusted to what makes sense for your demo. I will write a an article on sidreloc at some other time.
With this information the whole music playing is actually dead simple. We jump once to the sub routine at $1000 - this is done in code/main.asm - and from that point on we execute the actual replay subroutine with every screen refresh in our custom interrupt. And if you wonder where the actual code for the playback resides - it is always supplied with the SID file itself so all we need to know is where to jump to within the memory we put the SID into.
That's it! Incredibly easy, is it not?
This closes the first tutorial episode on coding the Commodore 64. It was meant to get you a bit excited about coding and we will go deeper in many areas which have only been mentioned briefly in later tutorials. If you have comments please feel free to send fedback either below the chapters or on Facebook.
-act