A C64 Game - Step 1


As threatened the first step detailed. I reckon that the first step is overwhelming if it's your first voyage into C64 game programming. There's quite a few assumptions made about the viewer's knowledge and the later steps won't get exactly easier.



A note about the !zone macro. ACME allows for global and local labels. A local label is starting with a . and is only visible inside a zone. This allows for easier reuse of common names like loop or retry.

This snippet tells the ACME cross compiler to assemble the result into the file "jmain.prg" with the cbm (Commodore Business Machines) type. This basically boils down to the well known .prg format which contains a word with the loading address at the start followed by the assembly.

;compile to this filename
!to "jmain.prg",cbm



The next snipped just defines a constant. I try to use them throughout so you can understand where I'm putting bytes at. The value 52224 is the address of the screen buffer, where 25 lines a 40 characters are stored continously. This is not the default memory location for the screen, a part of this base code relocates the screen.

;define constants here
;address of the screen buffer
SCREEN_CHAR = 52224



Now a very interesting piece which took me longer to work out than it should have. A C64 has two types of files, Basic files and machine code files. A Basic file can be started by RUN, a machine code file just contains the code and usually must be jumped at with the SYS command. Any half decent game will provide a proper Basic kick start that jumps directly at the machine code.

To allow for this we set the file start address to $801 (2049), the default Basic start. The file content starts out with the tokenized bytes of a simple Basic line calling SYS for us. The line is built by a word containing the address of the next Basic line. Following is a word with the line number (10 in our sample). After that the token for the SYS command ($9e) followed by a space ($20) and the ASCII representation of the target address (2064 in our sample). After that there is one zero byte marking the end of the line. The next zero word represents the end of the Basic file. I've got some extra zero bytes which are actually wrong but also don't really hurt.

;this creates a basic start
*=$801 ;SYS 2064
!byte $0C,$8,$0A,$00,$9E,$20,$32,$30,$36,$34,$00,$00,$00,$00,$00



The next snippet disables any visible sprites, relocates the VICs memory bank (resulting in a relocated screen buffer and charset address).

    ;init sprite registers
;no visible sprites
lda #0
sta VIC_SPRITE_ENABLE

;set charset
lda #$3c
sta VIC_MEMORY_CONTROL

;VIC bank
lda CIA_PRA
and #$fc
sta CIA_PRA



This piece is the main game loop. It's rather easy, we increase the border color (resulting in flashing), increase the top left character on the screen, wait for the vertical blank (not exactly but to the effect) and rerun the loop.

          ;the main game loop
GameLoop
;border flashing
inc VIC_BORDER_COLOR

;top left char
inc SCREEN_CHAR
jsr WaitFrame
jmp GameLoop



This snippet is quite interesting. The C64 allows you to read the current raster line on the screen that is currently being redrawn. The code checks for a certain raster position at the bottom of the screen to sync the game to the computer's display speed.

In detail we're waiting for the raster line to NOT be the position we want to wait for. Once we are on any line but the wanted we now really wait for our raster line to appear. This avoids the problem when the routine is called too fast in succession and we end up on the same raster line.

!zone WaitFrame
;wait for the raster to reach line $f8
;this is keeping our timing stable

;are we on line $F8 already? if so, wait for the next full screen
;prevents mistimings if called too fast
WaitFrame
lda $d012
cmp #$F8
beq WaitFrame

;wait for the raster to reach line $f8 (should be closer to the start of this line this way)
.WaitStep2
lda $d012
cmp #$F8
bne .WaitStep2

rts




step1.zip