Just keeping it real, part 8

As I already said in the first part of this series, one of my first computers was the Commodore 64, and I would love to know more about programming it at a low level. I think I will follow roughly the same path as I did with the Amiga, and give a brief explanation of the hardware, and some sources to valuable documentation on programming the C64 hardware.

Meet Sid and Vic

The main CPU in a C64 is a MOS 6510, running at around 1 MHz (much like the Amiga, the exact clockspeed differs slightly between PAL and NTSC models, for the simple reason that the entire system is synchronized to the refresh rate of the display, and PAL refreshes at 50 Hz where NTSC refreshes at 60 Hz). The 6510 is compatible with the 6502 instructionset, but has some special I/O functionality which is used in the C64. So CPU-wise it only had a very modest 8-bit CPU. Much like the Commodore Amiga, the strength of the design comes from its custom chipset.

The custom chip for audio is known as SID, which I already mentioned briefly in my post about X’2012 earlier. So, it is just a simple 3-channel audio chip, based on subtractive synthesis. And although it was not designed to handle digital audio, clever programmers found various ways of making the SID able to play digital samples, and integrate them in a musical way. We will be seeing a lot more of clever programmers doing things the C64 was not designed to do. Although on paper 3 channels were nothing special back in 1982, when the C64 launched, the SID is without a shadow of a doubt the most revered sound chip of its generation. The chip was designed by Bob Yannes, who later co-founded the Ensoniq synthesizer company. A chip designer with knowledge of music, and it showed. The SID chip has some simple but effective filters, and an ADSR envelope (Attack, Decay, Sustain, Release), which could really make music come to life. Where most 8-bit machines at the time could mainly produce blip-blop-like sounds, in the hands of a skilled composer, the SID chip could conjure up all kinds of moods, from gentle and happy to eerie, dark, evil and haunting.

The custom chip for video is known as VIC-II (the first version of the VIC was used in the VIC-20 computer). It is a very primitive chip. It has a hardwired palette of 16 colours. The maximum resolution is 320×200. However, you can’t just assign individual colours to each pixel, as you could with the Amiga or CGA/Tandy/EGA/VGA adapters. There are a few different modes, with various limitations on how you can assign colours to pixels, generally related to the character positions on screen (it has a 40×25 character textmode, where each character is an 8×8 block). Generally one of the 160×200 modes is used for games and demos, since the 320×200 mode is very limited as far as colour usage goes. I will probably get into more detail on these modes later, when I cover some graphics programming on the C64. If you are curious, here is an overview of various graphics modes, often using some hacks to enable more colours and/or detail.

The VIC-II supports 8 hardware sprites, which was quite a lot at the time. The sprites are limited to a resolution of 24×21 in monochrome, or 12×21 in multicolour. The sprites can be scaled up 2x horizontally and/or vertically.

The VIC-II also has limited hardware scrolling support, by allowing you to shift the screen 0-7 pixels horizontally or vertically (so moving within the space of a single 8×8 character, basically). Unlike the Amiga, you cannot have a larger playfield than the actual screen, so more elaborate scrolling requires moving the data around in memory.

Another important feature of the VIC-II is the raster interrupt. This raster interrupt basically works as a very primitive version of the Amiga copper: you can set the interrupt to trigger when a given scanline starts. Since the CPU is so slow however, the accuracy is quite poor. It may take a few cycles for the CPU to start the interrupt handler, and an entire scanline is only 63 cycles long (an entire character is output at every CPU cycle, which is 8 hires pixels or 4 lores pixels). A lot of tricks on C64 will require much better accuracy than what the raster interrupt can give us. I will probably get into that as well, once I cover the actual programming.

Getting started

For now, I think I will leave you with a few important resources if you want to get into programming the C64. A reference for the 6502 instructionset is a good place to start, because you will be doing everything in assembly language of course. And Commodore’s own Programmer’s Reference Guide is an excellent starting point for getting to know the hardware.

Once you’ve familiarized yourself a bit with the C64, Codebase 64 is a nice site to check out. You can find example code and tutorials here, mostly for somewhat more advanced topics, such as how to do graphics effects. There is also some advanced information on the VIC-II available, which deals with all sorts of quirks and undocumented features, used for various tricks. And another useful resource while programming is the All About Your C64 site, which contains a nice reference for all SID, VIC and CIA registers, among others.

For an assembler I am using KickAssembler, which is written by Mads Nielsen, aka Slammer of Camelot. This is a cross-assembler for 6502, written in Java. I have to admit, it’s not keeping it entirely real. With all the previous development, I actually used tools from the proper era, and developed on the actual target platform (SAS/C and DevPac on Amiga, Turbo C++ 3.1 on MS-DOS, and Visual Studio 6 on Windows 98 SE). But the tools available on the C64 are VERY primitive. I have given a few of them a try, such as Turbo Assembler, and the amazing Power C, but they are just very limited, with the low resolution text mode, the very limited amount of memory, the C64’s quirky keyboard and the slow assembly or compile times. With KickAssembler I can just use a contemporary text editor, and assemble my code in no time. Then I can have it launched in VICE immediately. Once I have something somewhat decent, I transfer it to my real C64 via an X1541 cable (which I made myself, many years ago, still works, but only on old computers. Good thing I still have my 486 and my Pentium Pro), so I still get the full experience.

Advertisements
This entry was posted in Oldskool/retro programming, Software development and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Just keeping it real, part 8

  1. Pingback: Just keeping it real, part 9 | Scali's OpenBlog™

  2. Pingback: Revision 2013 is keeping it real | Scali's OpenBlog™

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s