I’ve kind of been neglecting my Commodore stuff lately. That said, even though I haven’t done much new, I’ve finished cleaning up and commenting some of the earliest C64 code I wrote that’s still good enough to use. These are a simple bitmap library and a simple music playroutine. Both date from when I was writing things that would have been simple BASIC programs in a more sophisticated BASIC than the C64 had and wanted some better primitives.
The Bitmap Library
Bitmapped graphics on the C64 manage the difficult trick of being simultaneously perfectly logical and a gigantic nightmare. It’s a 320×200 bitmap, and to set a point (x, y), you first compute the relevant memory address A=320*INT(Y/8)+8*INT(X/8)+(Y%8). Then you must POKE A, PEEK(A) OR (2^(7-INT(X%8))). So that’s the nightmare part. The logical part is that this is how you would set up a bitmap if every 8×8 block were a custom character, so if you’ve already done some work setting up graphics for work with custom character graphics, you can translate it to the bitmap screen with no work whatsoever.
This bitmap library basically just does a bunch of unoptimized 16-bit math to compute that and then does a similarly straightforward hand-translation of Bresenham’s algorithm for drawing lines. Over the years I’d made little variants of it to experiment with ways to make it smaller or faster, but it doesn’t seem like I ever really found any refactorings that I liked enough to keep. Most of them revolved around not having to recompute the pixel masks or addresses, but these kept either being not much smaller, or being just a little smaller and much slower.
I haven’t really revisited this routine with an eye to optimizing it, because it works well enough, at 600-odd bytes it’s not a huge imposition for a program that needs it, and most work in bitmaps doesn’t really want arbitrary line-drawing anyway because you can’t do it with acceptable speed on a 1MHz machine.
The music player
It’s been said that a rite of passage for every Commodore programmer is writing their own music-playing routine. This was mine, I guess. Pretty much every program I’ve written that needs to play a tune of some kind has included this in it, but if I ever make a proper demo, I sure won’t be using this.
The basic notion is straightforward; each voice gets an array of notes and durations and it just steps through the arrays as needed, and at different rates. The “at different rates” is a little interesting because most modern playroutines work like the old Amiga trackers and keep the time index fixed for all voices. This doesn’t do that which makes the results a lot easier to write out, either by hand or with the help of a script.
Also amusing, though I never actually did anything with it, is the fact that the three voices don’t have too all be of the same length, because they can loop independently as well.
The reasons not to use this library mostly boil down to instrument control. The note arrays also include control commands that let you set the waveform characteristics of the notes, and also how much time to give to the “release” of each note so that it has a bit of time to fade out before the next note starts. A big reason that all the respectable players are tracker-based is that tracker-based systems let you alter the timbre and effects on a note while it’s still being held, and that’s a very important part of SID music. This system is fundamentally incapable of making that happen.
Still, to date I’ve never needed anything more powerful than this, so it’s crept into about a dozen programs over the years. Never underestimate the utility of being Basically Good Enough As It Is.