This is a pure 6510 assembly program for the Commodore 64 made by Bright Pixel in 2019, because why not.
The C64 was a famous 8-bit machine in the 80s and the highest-selling single computer model ever.
Its hardware and architecture set it appart from other 8-bit personal computers at the time. Unlike most of the others, the C64 had dedicated advanced MOS chips for graphics and sprites (the VIC-II), sound (the SID), I/O (the CIA), and many others.
These chips were not only powerful for the time, but they could perform their tasks autonomously, independently of what the main CPU, a MOS technology 6510 microprocessor, was doing. For instance, the VIC-II could generate interrupts on automatic sprite collisions. The CPU and the other chips also shared common data and memory BUSes.
To cope with all these chips inside 64Kbytes of addressable memory, the C64 had something called memory overlay, in which different chips would access different physical data locations for the same memory address. For instance the $D000-$DFFF block could be used for RAM, I/O or access to Character ROM, by the CPU, depending on a $0001 setting. Chips would have to be turned on or off, or instructed to look for data at specific RAM/ROM locations all the time to make the most of the machine as a whole.
This was impressive in the 80s, for a relatevily cheap mass-market personal computer.
Programming the C64 was more than a lot of fun, it was a form of art. Because of the way all this hardware was packed together, handling the machine meant knowing its memory map and registers by heart, and dominating quite a collection of tricks, some of which weren’t documented at all. What ended up being written for the C64 by the fervent community of developers all over the world went way beyond the imagination of Jack Tramiel.
Today, in 2019, the cult is still alive. There are vast groups of developers still writing C64 games and demos, restoring and using old machines, or using emulators. The SID sound chip was so revolutionary that it still drives a community of chiptune artists all over the world. The High Voltage SID Collection has more than 50,000 songs archived and growing.
At Bright Pixel, we like to go low-level, and we think that understanding how things work down there, even if we’re talking about a 40 years old machine, is enriching, helps us become better computer engineers and better problem solvers. This is especially important in a time when we’re flooded with hundreds of high-level frameworks that just “do the job.” Until they don’t.
This is a simple demo for the C64:
- It was coded entirely in 6510 assembly.
- It makes use of the VIC-II graphics, character ROM and sprites.
- It plays music using the SID chip.
- Uses raster-based interrupts, perfectly timed.
- Implements a random number generator.
You can download the source code for it in this repository, change it and run it a real machine or an emulator. The code is all annotated, and you can use the issue tracker to ask us questions or make suggestions, we’ll be listening.
Setup
Assembler
We used the Kick Assembler to build the PRG from the source. KA is still maintained up until today, with regular releases launched every couple of months. It supports MOS 65xx assembler, macros, pseudo commands and has a couple of helpers to load SID and graphics files into memory. Unfortunately, you need Java to run it, but it’s worth the trouble.
Here’s the setup in OSX.
Install Java
brew update
brew install homebrew/cask/java
Install Kick Assembler
curl http://theweb.dk/KickAssembler/KickAssembler.zip -o /tmp/KickAssembler.zip
sudo unzip /tmp/KickAssembler.zip -d /usr/local/KickAssembler
This should be the contents of /usr/local/KickAssembler/KickAss.cfg
And we have this alias in our ~/.bash_profile for convenience.
alias kick="java -jar /usr/local/KickAssembler/KickAss.jar"
C64 emulator
There are plenty of Commodore 64 emulators out there.
- VICE, the Versatile Commodore Emulator, is a program that runs on a Unix, Win32, or Mac OS X machines and emulates the C64 (and every other 65xx Commodore machine too).
- VirtualC64 is an interesting alternative for OSX written from scratch using C++ and native Cocoa and provides a real-time graphical inspector of the CPU, Memory, and the other Chips, while it’s running.
We used VICE. One more bash alias:
alias c64="/Applications/x64.app/Contents/MacOS/x64"
Debugging
Debugging assembly when things go south can be challenging. Back in the 80s, debugging meant spending hours doing trial and error, rebooting the machine, reloading the code from the cassette (or disk drive, if you were lucky), and writing code to paper just in case you’d lose it in the process.
Luckily, now we have way better tools.
-
The C64 65XE Debugger is a C64 and Atari XL/XE code and memory debugger that works in real-time and embeds the VICE emulator in the same graphical interface. It allows you to see what’s happening with every chip, register, memory blo