Jump to content
  • Advertisement
  • entries
    104
  • comments
    102
  • views
    260637

About this blog

Musings of a hobbyist

Entries in this blog

 

A C64 game - Step 3

Quite similar to step 2 this time we set up sprites. Note that I used another selfmade tool (similar to SpritePad). The file J.SPR contains the sprites and is included into the source as binary. The sprites are located "under" the I/O ROM at $D000 onwards.
And another note: Since I'm lazy I built the sprite copy loop to always copy packages of 4 sprites (1 sprite comes as 63 + 1 byte, so 4 make nice 256 byte packages).

The current step should display the known charset garbage and "HELLO" as before, but also show an elegantly handicrafted sprite.


Right below the charset copy routine we add the copy call.

  ;take source address from SPRITES lda #<SPRITES sta ZEROPAGE_POINTER_1 lda #>SPRITES sta ZEROPAGE_POINTER_1 + 1 jsr CopySprites   The sprite copy routine is quite similar to the charset copy but a tad shorter due to the 256 byte packaging: ;------------------------------------------------------------ ;copies sprites from ZEROPAGE_POINTER_1 to ZEROPAGE_POINTER_2 ; sprites are copied in numbers of four ;------------------------------------------------------------ !zone CopySprites CopySprites ldy #$00 ldx #$00 lda #00 sta ZEROPAGE_POINTER_2 lda #$d0 sta ZEROPAGE_POINTER_2 + 1 ;4 sprites per loop .SpriteLoop lda (ZEROPAGE_POINTER_1),y sta (ZEROPAGE_POINTER_2),y iny bne .SpriteLoop inx inc ZEROPAGE_POINTER_1 + 1 inc ZEROPAGE_POINTER_2 + 1 cpx #NUMBER_OF_SPRITES_DIV_4 bne .SpriteLoop rts   In front of GameLoop we put the sprite display code. The sprite is positioned at coordinates 100,100, the sprite pointer set to the correct image and the sprite is enabled. ;set sprite 1 pos lda #100 sta VIC_SPRITE_X_POS sta VIC_SPRITE_Y_POS ;set sprite image lda #SPRITE_PLAYER sta SPRITE_POINTER_BASE ;enable sprite 1 lda #1 sta VIC_SPRITE_ENABLE   step3.zip

Previous Step 2 Next Step 4

Endurion

Endurion

 

A c64 game - Step 2

And onwards we stumble!

In the first part we prepared everything for the VIC, now we set up our modified charset. Note that I used a selfmade tool (similar to CharPad), which is included (Windows binary). The file J.CHR contains the charset and is included into the source as binary. The memory layout of the game expects the modified charset at $f000. Since the C64 can't load files to locations after $C000 we have to copy the charset to the target memory at $f000. To be able to properly write at those addresses we need to switch off the ROM overlay.

The current step should display "HELLO". The rest of the screen depends on the current memory setup of your emulator/C64


First code piece we add is the copy routine. Interrupts are blocked because we turn off the kernal ROM. If we didn't the IRQ code would jump in the middle of uninitialised RAM, likely resulting in a crash. The RAM/ROM layout is influenced by memory address $1. ;---------------------- ;copy charset to target ;---------------------- ;block interrupts ;since we turn ROMs off this would result in crashes if we did not sei ;save old configuration lda $1 sta PARAM1 ;only RAM ;to copy under the IO rom lda #%00110000 sta $1 ;take source address from CHARSET LDA #<CHARSET STA ZEROPAGE_POINTER_1 LDA #>CHARSET STA ZEROPAGE_POINTER_1 + 1 ;now copy jsr CopyCharSet ;restore ROMs lda PARAM1 sta $1 cli   The actual copy routine. Note that we only copy 254 characters. The last two characters are omitted to not overwrite the default IRQ vectors residing at $fffb. Since we deal with a 8 bit machine there is an extra loop taking care of the high bytes of our addresses. At the end of the copy routine we include the binary charset data. !zone CopyCharSet CopyCharSet ;set target address ($F000) lda #$00 sta ZEROPAGE_POINTER_2 lda #$F0 sta ZEROPAGE_POINTER_2 + 1 ldx #$00 ldy #$00 lda #0 sta PARAM2 .NextLine lda (ZEROPAGE_POINTER_1),Y sta (ZEROPAGE_POINTER_2),Y inx iny cpx #$8 bne .NextLine cpy #$00 bne .PageBoundaryNotReached ;we reached the next 256 bytes, inc high byte inc ZEROPAGE_POINTER_1 + 1 inc ZEROPAGE_POINTER_2 + 1 .PageBoundaryNotReached ;only copy 254 chars to keep irq vectors intact inc PARAM2 lda PARAM2 cmp #254 beq .CopyCharsetDone ldx #$00 jmp .NextLine .CopyCharsetDone rts CHARSET !binary "j.chr"   To display HELLO on the screen we simple poke the character codes on the screen and also set the characters colors to white. ;test charset lda #'H' sta SCREEN_CHAR lda #'E' sta SCREEN_CHAR + 1 lda #'L' sta SCREEN_CHAR + 2 sta SCREEN_CHAR + 3 lda #'O' sta SCREEN_CHAR + 4 lda #1 sta SCREEN_COLOR sta SCREEN_COLOR + 1 sta SCREEN_COLOR + 2 sta SCREEN_COLOR + 3 sta SCREEN_COLOR + 4
Clarifications:

The charset of the C64 is using 8 bytes per character. This totals at 256 characters a 8 bytes = 2048 bytes. A custom character set can be positioned almost everywhere in RAM (at 2048 interval steps).

In hires text mode every bit corresponds to a pixel. In multicolor text mode pixels are doubling width, so two bits make up one pixel. In multicolor mode two colors are shared by all multi-color characters, one is the background color and one is the current char color.


The memory layout looks like this (nicked from www.c64-wiki.de): $FFFF = 65535 ????????????????????????????????? ?---------------?|||||||||||||||? ||| = read by PEEK ?---------------?|||||||||||||||? --- = written to by POKE ?---------------?|||||||||||||||? +++ = read and write ?---------------?||| KERNAL- |||? other = not reachable from BASIC ?---------------?||| ROM |||? ?---------------?|||||||||||||||? ?---------------?|||||||||||||||? $E000 = 57344 ????????????????????????????????????????????????? ? ? ?+++++++++++++++? ? ? CHAR ROM ?+++++ I/O +++++? ? ? ?+++++++++++++++? $D000 = 53248 ????????????????????????????????????????????????? ?+++++++++++++++? ?+++++++++++++++? ?+++++++++++++++? $C000 = 49152 ????????????????????????????????? ?---------------?|||||||||||||||? ?---------------?|||||||||||||||? ?---------------?||| BASIC- ||||? ?---------------?||| ROM ||||? ?---------------?|||||||||||||||? ?---------------?|||||||||||||||? ?---------------?|||||||||||||||? $A000 = 40960 ????????????????????????????????? ?+++++++++++++++? ?+++ BASIC- ++++? ?+++ RAM ++++? . . ?+++ BASIC- ++++? ?+++ RAM ++++? $800 = 2048 ?+++++++++++++++?-? $400 = 1024 ?+++++++++++++++?-?Default Screen Address $0000 ?????????????????-?Zeropage and Enhanced Zeropage
step2.zip

Previous Step 1b Next Step 3

Endurion

Endurion

 

First step explained in detail

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

Previous Step 1 Next Step 2

Endurion

Endurion

 

A c64 game in several steps (lots of 'em)

Welcome!
Today's development is heaps and bounds beyond imagination from 20 years ago. I've always had a soft spot for the C64 after all this years. So I sat down and tried to start assembly programming on a C64.

Today I'll start with a sort of tutorial on how to write a C64 game. I have prepared 36 steps for now, planned are probably a few more.

I'll start out very small but there will be bigger steps later on. The code is supposed to be heavily commented but is probably not clear for everyone. I'll be happy to answer questions regarding the code. The code is written for the ACME cross compiler, which allows to compile the code on any bigger OS.


Step #1 is a simple base for a game. It provides a Basic start (10 SYS 2064), sets up the VIC relocation and shows a simple synchronized game loop.

To show the loop running the border color is flashed and the top left char is rotating throughout all characters.

The not too eye-popping result looks like this:


Find here the source code and binary for use in an emulator of your choice (I recommend WinVICE):
step1.zip

Next Step 1b

Endurion

Endurion

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!