The task of positioning sprites on the C64 holds one particular oddity that I want to show. To be able to position a sprite on screen, you must master binary logic on the C64, and binary logic works in the same way as on the Commodore PET, shown here. Also, you need a visible sprite. The following code does the trick. If you don’t own a Commodore 64 and don’t have an emulator, the code also works in JaC64, an on-line C64 emulator implemented as an Java Applet. However, I can’t get the cursor keys to work in JaC64, so don’t mistype anything! 😉
10 FOR I=0 TO 62 20 POKE 704+I, 255 30 NEXT I 40 POKE 2040, 11 50 POKE 53287, 1 60 POKE 53248, 200 70 POKE 53249, 200 80 POKE 53269, 1
The Y position of the sprite is stored at D001 (53249). You have 256 possible Y positions, but the visible area of the screen is only 200 pixels high. All you need to make the sprite cycle over the screen is some iteration that copies the values 0 to 255 to the D001 address.
The X position however, is a bit more tricky. The X coordinate is stored at D000 (53248). But we need more than 256 possible positions to be able to place the sprite on the right side on the screen. Commodore did not waste a whole byte on solving this problem. At D010 (53264), the first bit keeps track on if D000 holds a value that represent the left part of the screen, or the right part of the screen for the first sprite (sprite 0). We have eight sprites and eight available bits at D010, so the second bit keeps track on if D002 (53250) holds a value that represent the left part of the screen, or the right part of the screen for the second sprite (sprite 1), and so on. So this is what we must do to make the sprite we displayed in the above program run over the screen:
Iterate from 0 to 255, set the value to D000 (line 10-30). Set the first bit at D010 to 1 (line 40). Iterate from 0 to 255, set the value to D000 (line 50-70). Restore the first bit at D010 to 0 (line 80). Repeat from the beginning (line 90).
Type NEW to delete your current program.
10 FOR I=0 TO 255 20 POKE 53248, I 30 NEXT 40 POKE 53264, PEEK(53264) OR 1 50 FOR I=0 TO 255 60 POKE 53248, I 70 NEXT 80 POKE 53264, PEEK(53264) AND 254 90 GOTO 10
If you ran the first program, and typed in this program correctly, you should see a sprite that flies from the left to the right across your screen.