Skip to main content

Homebrew game for C64 - part 3: generating assembly straight from sheet music

T
his post is going to combine musical notation, music XML, cocoa programming (objective-C for MacOS) and retro game programming (specifically the music) in assembly for C64.

That might qualify this as an extremely niche post, so if you're reading because you're interested in all or some of those things, or if there's some other reason why you'd like to convert sheet music to another format, do let me know if the comments so that I know I'm not entirely alone!

In the last couple of posts in this series I wrote about how I was tinkering around with a little tune for a game I'm writing for C64. Writing out the notation is something I'm very comfortable with.

I eventually arrived at a great and efficient way to store and play the music within my game. This involved using a kind of 'bytecode', combining instructions and data. (A development of code that came with Derek Morris' book). That looks like this:
That looks pretty laborious to write out by hand, particularly looking up the high and low oscillator settings for each note. And I can tell you that it is.

Anyway, for this game that's done now,  I have something I'm pretty happy with, other than the voice settings. You can hear that in the last part of this series if you're interested.

But with future composing efforts in mind, it occurred to me that I could seriously turbocharge this process, and pretty much generate the assembly language straight from the sheet music.

First step: parse the music XML (which any music notation software should be able to export to). At first, to save time, I downloaded an open-source class for turning XML into NSDictionary. That appeared to work but after some work I realised that it wasn't handling self-closing tags in the XML, which my musicXML was full of. So that wasn't a time-saver and to be fair the author of the xml -> NSDictionary class has made it clear that it's now out of date and unsupported.

So back to square one and writing code using NSXMLParser. That involves writing a lot of code, but it's useful in some ways - it serialises the data, you can take what you want from it and ignore what you don't want. It was probably no more code than I had to write to extract the data after the XML had been converted to an NSDictionary, and there are no dependencies on 3rd party / open source code.

Here's where we are, in one afternoon (including the abandoned effort and restarting) I can open any musicXML file and see this:
[later update]
... and here we are with the app now converting the notes to high and low frequency values and generating the assembly. Sweet!
Yes I did have to type in the table of values for each note from good old Appendix E (just the decimal value, the high and low values are calculated from that) but now that I've done that, it's the last time I'll have to refer to that table.

It looks like a lot of code, but each of those lines of assembly represents just two bytes in the compiled code, so my entire tune with three voices takes around 1.8k.

Comments

Popular posts from this blog

Driving NeoPixels with Z80

I 've long been thinking about a version two   RC2014 LED matrix module . I've had a matrix with a MAX 7219 on a module. It's a nice enhancement. But there's only so much you can do with a single-colour LED array right? Wouldn't it be cool to have RGB LEDs?  At Liverpool MakeFest I saw a wall-sized ping-pong ball NeoPixel display and picked up some NeoPixels with the intention of making one. Possibly driven by my RC2014.  I enjoy learning about protocols and have had some SPI devices working with the RC2014 - bit-banging SPI works really well because it doesn't care about timing. NeoPixels really do care about timing though. From Adafruit's web page about their 8x8  NeoPixel matrix: If there's one thing I want to get across in this blog post, it's don't just accept what you're told . Question everything. Learn about what's going on and find out why you're being told something isn't possible. Get creative with workarounds. I'

ZX81 reversible internal 16k upgrade

T his post is an upvote for Tynemouth Software's  ZX81 reversible Internal 16K RAM upgrade . Their instructions are easy enough for even me to follow and don't involve cutting tracks. This is the ZX81 I've had out on display and used whenever I wanted to. It's an issue 1 and was probably a kit judging by some very untidy assembly. It has a ZX8-CCB  composite video mod and an external keyboard fitted. On board it has two 1k x 4-bit chips.  The ZX81 originally came with 1k on board. Thanks to a trick with compressing the display in ram, that was enough to type and run a small program but you soon felt the limitations. Back in the early 80s, the solution was a 16k ram pack which plugged into the back[1] and this is the way I've been using this particular machine. These ram packs are notorious for 'ram pack wobble'. Even if fastened into place, you can still randomly find your work disappearing. This is a very reliable solution using a more modern 32k chip (half

Making new ROMs for the Vic20 / Vicky Twenty

M y Vicky Twenty is very nearly complete.  As things stand, the board and every single component is new*. The processor and VIAs are newly-manufactured (W65C02 and W65C22).  Obviously the Vic1 chip isn't manufactured today, but there is 'new old' stock about. I have been able to buy a Vic 1, date code 1987 (which seems very late). It obviously hasn't been in a computer before, passes the acetone test and works. The same goes for two of the ROMs - character and BASIC. But I haven't been able to buy a new-old Kernal ROM (901486-07). I am able to borrow one - all of the boards I have, have this particular ROM socketed. I don't know whether all of this indicates that the Kernal has proved less reliable than the other two. I recently bought a TL866 for another project. Of all the retro-computing hardware things I've had to learn to do, making ROMs has been one of the simplest. So far, everything has been very easy and worked first time.  I'm not sure that it&