; Sprite demo for the 65C02 + TMS9918 machine. ; Four 16x16 sprites bounce around the screen; positions are updated once per ; frame from the VBlank IRQ. ; ; Build: asm/build.sh (ca65 + ld65, output roms/sprites.bin) .setcpu "65C02" VDPD = $7F00 ; VDP data port VDPC = $7F01 ; VDP control/status port ; VRAM layout (matches the register values below) NAMETAB = $0800 ; R2 = $02 COLTAB = $2000 ; R3 = $80 PATTAB = $0000 ; R4 = $00 SPRATTR = $1B00 ; R5 = $36 SPRPAT = $3800 ; R6 = $07 NSPR = 4 MAXX = 240 ; 256 - 16 MAXY = 176 ; 192 - 16 .segment "ZEROPAGE" frame: .res 1 ; incremented by the IRQ handler ptr: .res 2 ; source pointer for vcopy cnt: .res 2 ; byte count for vcopy state: .res NSPR*4 ; sx[4], sy[4], dx[4], dy[4] sx = state sy = state + NSPR dx = state + NSPR*2 dy = state + NSPR*3 .segment "CODE" ; ----------------------------------------------------------------- macros .macro SETWRITE addr lda #<(addr) sta VDPC lda #(>(addr)) | $40 sta VDPC .endmacro .macro SETREG reg, val lda #val sta VDPC lda #$80 | reg sta VDPC .endmacro ; ----------------------------------------------------------------- reset reset: sei cld ldx #$FF txs ; --- VDP registers: Graphics I, 16x16 sprites, display on, IRQ on SETREG 0, $00 SETREG 1, $E2 ; 16K, display, IE, 16x16 sprites SETREG 2, NAMETAB >> 10 SETREG 3, COLTAB >> 6 SETREG 4, PATTAB >> 11 SETREG 5, SPRATTR >> 7 SETREG 6, SPRPAT >> 11 SETREG 7, $04 ; backdrop: dark blue ; --- clear all 16 KB of VRAM SETWRITE 0 ldx #$40 ldy #0 lda #0 @clr: sta VDPD iny bne @clr dex bne @clr ; --- background patterns (tile 0 blank, tile 1 = dot) SETWRITE PATTAB lda #tiles sta ptr+1 lda #<(tiles_end - tiles) sta cnt lda #>(tiles_end - tiles) sta cnt+1 jsr vcopy ; --- color table: 32 entries SETWRITE COLTAB ldx #32 lda #$E4 ; gray on dark blue @col: sta VDPD dex bne @col ; --- name table: 768 x tile 1 SETWRITE NAMETAB ldx #3 ldy #0 lda #1 @nam: sta VDPD iny bne @nam dex bne @nam ; --- sprite patterns SETWRITE SPRPAT lda #sprites sta ptr+1 lda #<(sprites_end - sprites) sta cnt lda #>(sprites_end - sprites) sta cnt+1 jsr vcopy ; --- initial sprite state (ROM -> zero page) ldx #NSPR*4-1 @ini: lda init_state,x sta state,x dex bpl @ini cli ; ----------------------------------------------------------------- main loop main: lda frame @wait: cmp frame ; wait for the next VBlank IRQ beq @wait jsr move jsr write_sprites bra main ; ----------------------------------------------------------------- IRQ irq: pha lda VDPC ; read status: acknowledges the VDP interrupt inc frame pla rti nmi: rti ; ----------------------------------------------------------------- vcopy ; Copy cnt bytes from (ptr) to VRAM (write address already set). vcopy: ldy #0 @loop: lda (ptr),y sta VDPD iny bne @nowrap inc ptr+1 @nowrap: lda cnt bne @dec dec cnt+1 @dec: dec cnt lda cnt ora cnt+1 bne @loop rts ; ----------------------------------------------------------------- move ; Advance all sprites, bouncing off the screen edges. move: ldx #NSPR-1 @next: ; --- X axis lda sx,x clc adc dx,x cmp #MAXX ; negative positions wrap to >= MAXX too bcc @xok lda dx,x ; hit an edge: reverse direction (negate) eor #$FF inc a sta dx,x bra @ypos @xok: sta sx,x @ypos: ; --- Y axis lda sy,x clc adc dy,x cmp #MAXY bcc @yok lda dy,x eor #$FF inc a sta dy,x bra @done @yok: sta sy,x @done: dex bpl @next rts ; ----------------------------------------------------------------- write_sprites ; Write the sprite attribute table (Y, X, name, color) + terminator. write_sprites: SETWRITE SPRATTR ldx #0 @loop: lda sy,x sta VDPD lda sx,x sta VDPD lda spr_name,x sta VDPD lda spr_color,x sta VDPD inx cpx #NSPR bne @loop lda #$D0 ; end of sprite list sta VDPD rts ; ----------------------------------------------------------------- data .segment "RODATA" ; sx[4], sy[4], dx[4], dy[4] (negative speeds as two's complement) init_state: .byte 20, 100, 180, 60 ; sx .byte 20, 120, 60, 150 ; sy .byte 1, $FE, 2, $FF ; dx: +1, -2, +2, -1 .byte 1, 1, $FF, 2 ; dy: +1, +1, -1, +2 spr_name: .byte 0, 4, 8, 12 ; 16x16 pattern numbers (multiples of 4) spr_color: .byte 8, 10, 3, 15 ; red, yellow, green, white tiles: .byte 0,0,0,0,0,0,0,0 ; tile 0: blank .byte 0,0,0,%00011000,%00011000,0,0,0 ; tile 1: dot tiles_end: ; 16x16 sprite: 16 rows of 16 bits. VDP order is: left-top, left-bottom, ; right-top, right-bottom (8 bytes each). .macro SPRITE16 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15 .byte >r0,>r1,>r2,>r3,>r4,>r5,>r6,>r7 .byte >r8,>r9,>r10,>r11,>r12,>r13,>r14,>r15 .byte