Creating a sprite on a Commodore 64 (idea)

On most computer systems, you have a text mode and a graphics mode. In text mode, you can display text, and in graphics mode you can display anything, including text with a bit of effort. This was mostly true for the venerableCommodore 64, too. However, the little VIC-II graphics chip that controlled the display had a neat feature: sprites. VIC-II sprites are hardware controlled graphical objects that can be displayed and moved around on a layer above the usual graphics or text display. You can have up to eight of them on the screen at once.

Even though the C64's BASIC interpreter doesn't directly support them, they are fairly easy to use because the hardware does so much of the work. It is possible to poke the correct values into memory and make the sprites magically appear. Here is a walkthrough of the process:

First you need to find some memory to hold the sprite bitmap. There are two formats for bitmaps: 24x21, with a depth of one bit or 12x21 with a depth of two bits; either way, the sprite needs 63 bytes. In addition, the memory chunk needs to be on a 64 byte boundary, and within the first 16k of memory. The C64 already allocates a fair bit of the lower memory locations, but usually the 13th, 14th and 15th 64-byte pages, located at addresses 832, 896, and 960, respectively, are safe.

Ok, now that we know where the sprite data will go, we must tell the VIC-II chip. It checks a table of 8 one-byte indices at address 2040. We tell it to use page 13 by poking 13 into the first index.

10 poke 2040,13

Next, we have to put the actual sprite bitmap into that memory. The bitmap data will be in some BASICDATA statements.

20 for i=0 to 62:read a:poke 832+i,a:next i

Now, to set the color of the sprite. Even though the bitmap has only one bit per pixel, we get to choose what that one color is. Woohoo. The off pixels in the bitmap will be transparent, and the background text or graphics will show through. If this were a multicolor sprite, it would have 3 colors plus transparent. Another table of bytes exists, starting at address 53287 that controls the color of each of the eight sprites. We just poke 1, for white, into the first slot.

30 poke 53287,1

Now our sprite is almost completely set up. Before it actually shows up on the screen, the graphics chip must be told it is ready. The byte at address 53269 has one bit for each sprite; zero means off, one means on. To turn on the first sprite (and turn off all the others), we can just poke in 1.

40 poke 53269,1

Lastly, we need to tell the VIC-II chip where to show the sprite. At address 53248, we have yet another table, this time of eight two-byte X,Y coordinate pairs. The C64 has a 320x200 screen, so one byte is enough for the Y coordinate, but obviously, we need more for the X coordinate. One more bit per sprite is packed into byte 53264, giving a 9-bit X coordinate. Lets put it in about the middle of the screen.

50 poke 53248,160
60 poke 53249,100
70 poke 53264,0

And finally, the data for the sprite image. Each line of 24 bits is provided by 3 bytes, with the most significant bit of each byte representing the leftmost pixel. (Hey, I'm no artist)

And voila, your very own sprite. You've conquered early 80's technology. The VIC-II chip has a few other functions to play with, like double-size sprites, multicolor sprites, and collision detection, which are equally easy to use.