My latest obsession has been developing for the Commodore 64 with 6502 assembly. I missed out on the c64 craze unfortunately. My family’s first computer was a 128k mac. It didn’t ship with BASIC so that delayed my access to programming by a few years.
So, in order to do c64 development I’ve been writing the code on a modern computer then cross compiling with the ACME assembler then running the result with the VICE emulator. I couldn’t bring myself to type out the code on the c64 itself since I’m too used to emacs keybindings.
For this post I’m just going to provide links to the sources I’ve been using and collect some tips I’ve picked up along the way.
- VICE has the ability to monitor your emulator. While it is running if you go into the menu and find the option “Monitor” the machine will pause and you will be provided with a prompt similar to gdb. From there you can print the contents of memory, the values of registers, step over lines of code, see the disassembled code in memory, and add breakpoints. You’ll need to use this at some point.
- Learn to do 16 bit arithmetic. This will come up eventually.
6502 16bit addition1234567clclda xadc ysta rlda x+1adc y+1sta r+1
- At some point in your 6502 code you will accidentally type “$00” when you meant “#$00”. It’s an easy typo to grab the contents of memory location zero instead of the constant zero, it can be difficult to spot so keep an eye out for it.
- Be aware of what machine you are developing for. By default VICE runs as C64:PAL. Since your code will probably be tied to screen refresh interrupts, code that works on PAL might not work as you expected on C64:NTSC.
- C64 computers can have one of 2 sound chips, the 6581 or the 8580. Developing music for one might not sound good on the other. The huge difference everyone mentions is the 6581 has a bug in it where it pops when you set certain parameters, people use this bug in order to playback PCM audio.
- Indirect Addressing (pointers) only work if the address is on the Zero Page (the first 255 bytes of memory). Also the Zero Page is hotly contested real estate, one safe place to put things is $FB and $FC.
- The VIC-II (the video card) can run in one mode at a time:
- hires character mode – a single global and background color, video memory is a series of 40×25 character codes. Character codes are 8×8 bits, each bit deciding if its background or foreground color.
- multicolor character mode – similar to hires character mode, except each character is now 4×8 pixels since each pixel is represented by 2 bits. It will chose from 3 global colors or a 4th color that is an 40×25 array, an element for each character.
- bitmap mode – this can be multicolor or hires as well. This mode lays out all the pixels somewhat sequentially in memory. They are sequentially aligned like characters. So the first 8 bytes represent the colors in the top left square of real estate, instead of the top line as you might have expected.
- There are other modes mentioned elsewhere but these are the ones the old documents talk about. I’m guessing those graphics modes are hacks to get different qualities like more colors.
- Joystick handling is inverted, each button and direction on the joystick has a bit associated with it in the hardware register $dc00, it is being activated when that value is zero, not one.
- All games and thus most users assume the joystick is plugged into port 2 not port 1.
- LDA sets the zero flag and negative flag. I was expecting only math or logical operations to set flags.
- https://github.com/clarkeaa/c64-demos – small simple programs to showcase features of the c64
- http://dustlayer.com/tutorials/ – tutorials I started with, they are nice but don’t isolate well concepts so you are left with a few large demos
- http://1amstudios.com/2014/12/07/c64-smooth-scrolling/ – blog article on how to implement smooth horizontal scrolling
- http://www.obelisk.demon.co.uk/6502/reference.html – 6502 reference
- http://sta.c64.org/cbm64mem.html – c64 memory map
- http://www.zimmers.net/cbmpics/cbm/c64/c64prg.txt – text file with tons of information on how to program the c64
- http://www.rgcd.co.uk/ – publisher of modern c64 games