TK2048/src/game_vdp_graphics.s
2025-10-13 19:36:20 +02:00

315 lines
No EOL
5.6 KiB
ArmAsm

.rtmodel version,"1"
.rtmodel core,"6502"
.extern _Zp
.extern VDP_MEM, VDP_REG
.extern vdp_point_to_vram_xy, vdp_hide_sprite, vdp_show_sprite, vdp_set_sprite_tile
NUM_TILE_OFFSET: .equ 0x14
.section code,text
vdp_redraw_tiles:
P_GRID_H$: .equ _Zp+9
P_GRID_L$: .equ _Zp+8
lda zp:_Zp+0
sta zp:P_GRID_L$
lda zp:_Zp+1
sta zp:P_GRID_H$
ldy #24
SetSprtLoop$:
lda (zp:P_GRID_L$),y
bne SkipHide$
pha
tya
pha
jsr vdp_hide_sprite
pla
tay
pla
jmp SkipSprite$
SkipHide$:
sta zp:_Zp+0
dec zp:_Zp+0
tya
pha
jsr vdp_set_sprite_tile
pla
tay
pha
jsr vdp_show_sprite
pla
tay
SkipSprite$:
dey
bpl SetSprtLoop$
ldy #24
SetTileLoop$:
tya
pha
lda TileNum_To_X_Map,y
sta zp:_Zp+0
lda TileNum_To_Y_Map,y
sta zp:_Zp+1
lda (zp:P_GRID_L$),y
jsr vdp_draw_numtile
pla
tay
dey
bpl SetTileLoop$
rts
vdp_draw_joystick:
T_NT_IDX$: .equ _Zp+4
P_POS$: .equ _Zp+2
T_Y$: .equ _Zp+1
T_X$: .equ _Zp+0
pha
ldx #0
stx zp:T_NT_IDX$
; Clear the table
lda #29
sta zp:T_X$
lda #16
sta zp:T_Y$
jsr vdp_point_to_vram_xy
lda #0
sta VDP_MEM
lda #29
sta zp:T_X$
lda #18
sta zp:T_Y$
jsr vdp_point_to_vram_xy
lda #0
sta VDP_MEM
lda #28
sta zp:T_X$
lda #17
sta zp:T_Y$
jsr vdp_point_to_vram_xy
lda #0
sta VDP_MEM
nop ; Slow down, we're using this while the IC is rendering
sta VDP_MEM
nop
sta VDP_MEM
; Jump to the correct code to handle joystick drawing
pla
asl a
tax
lda _draw_joystick_jumptable$,x
sta zp:_Zp+0
lda _draw_joystick_jumptable$+1,x
sta zp:_Zp+1
sec
jmp (_Zp+0)
J_Center$:
ldx #29
ldy #17
bcs J_Done$
J_Up$:
ldx #29
ldy #16
bcs J_Done$
J_Down$:
ldx #29
ldy #18
bcs J_Done$
J_Left$:
ldx #28
ldy #17
bcs J_Done$
J_Right$:
ldx #30
ldy #17
J_Done$:
stx zp:T_X$
sty zp:T_Y$
jsr vdp_point_to_vram_xy
lda #120
sta VDP_MEM
rts
_draw_joystick_jumptable$:
.word J_Center$
.word J_Up$
.word J_Down$
.word J_Left$
.word J_Right$
vdp_clear_gamegrid:
T_NT_IDX$: .equ _Zp+4
T_Y$: .equ _Zp+1
T_X$: .equ _Zp+0
lda #0x00
sta zp:T_NT_IDX$
lda #0x01
sta zp:T_X$
lda #19
sta zp:T_Y$
ClearLine$:
jsr vdp_point_to_vram_xy
lda #0x00
ldx #24
ClearTile$:
sta VDP_MEM
dex
bne ClearTile$
dec zp:T_Y$
bne ClearLine$
rts
vdp_clear_dialog:
T_NT_IDX$: .equ _Zp+4
T_Y$: .equ _Zp+1
T_X$: .equ _Zp+0
lda #0x01
sta zp:T_NT_IDX$
lda #0x04
sta zp:T_X$
lda #15
sta zp:T_Y$
ldy #9
ClearLine$:
jsr vdp_point_to_vram_xy
lda #0x00
ldx #24
ClearTile$:
sta VDP_MEM
dex
bne ClearTile$
dec zp:T_Y$
dey
bne ClearLine$
rts
;;; vdp_draw_numtile:
;;; Draws the 2048 tile at specified coordinates
;;; Parameters:
;;; - Type of tile [A]
;;; - X in game grid [Zp[0]]
;;; - Y in game grid [Zp[1]]
;;;
;;; Returns: nothing
;;;
;;; Clobbers:
;;; - A, Y, X
;;; - Zp 0, 1, 2, 3, 4, 5, 6, 7
;;;
vdp_draw_numtile:
T_LINE_COUNT$:.equ _Zp+7
T_NT_IDX$: .equ _Zp+4
T_COUNTER$: .equ _Zp+3
P_TYPE$: .equ _Zp+2
P_Y$: .equ _Zp+1
P_X$: .equ _Zp+0
sta zp:P_TYPE$
lda #0
sta zp:T_NT_IDX$ ; the tiles here will be drawn only on game table
sta zp:T_COUNTER$
lda #3
sta zp:T_LINE_COUNT$
; Convert the coordinates
ldx zp:P_X$
lda X_To_TileNum_Map,x
sta zp:P_X$
ldx zp:P_Y$
lda Y_To_TileNum_Map,x
sta zp:P_Y$
; Calculate the beginning of the tile section we're interested in
clc
lda #NUM_TILE_OFFSET
adc zp:P_TYPE$
asl a
asl a
asl a
sta zp:P_TYPE$
; Draw the tile
DrawNextLine$:
jsr vdp_point_to_vram_xy
ldx zp:T_COUNTER$
ldy #4
SetTile$:
clc
lda TileNum_Offset_Map,x
adc zp:P_TYPE$
sta VDP_MEM
nop
inx
dey
bne SetTile$
stx zp:T_COUNTER$
inc zp:P_Y$
dec zp:T_LINE_COUNT$
bne DrawNextLine$
rts
;;;;;;;;;;;;;;;;;;
.section data,data
TileNum_Offset_Map: ; Offset from the beginning of the selected tilemap section, of the tiles composing the numeric-2048 tile
.byte 0x00, 0x04, 0x04, 0x02 ; First line
.byte 0x04, 0x04, 0x04, 0x04 ; Second
.byte 0x01, 0x04, 0x04, 0x03 ; Third and last
; The following maps convert between X and Y in the game grid into the tile map grid
X_To_TileNum_Map:
.byte 0x01, 0x06, 0x0B, 0x10, 0x15
Y_To_TileNum_Map:
.byte 0x01, 0x05, 0x09, 0x0D, 0x11
; The following maps convert between the tile number and the corresponding X and Y coordinates
TileNum_To_X_Map:
.byte 0x00, 0x01, 0x02, 0x03, 0x04
.byte 0x00, 0x01, 0x02, 0x03, 0x04
.byte 0x00, 0x01, 0x02, 0x03, 0x04
.byte 0x00, 0x01, 0x02, 0x03, 0x04
.byte 0x00, 0x01, 0x02, 0x03, 0x04
TileNum_To_Y_Map:
.byte 0x00, 0x00, 0x00, 0x00, 0x00
.byte 0x01, 0x01, 0x01, 0x01, 0x01
.byte 0x02, 0x02, 0x02, 0x02, 0x02
.byte 0x03, 0x03, 0x03, 0x03, 0x03
.byte 0x04, 0x04, 0x04, 0x04, 0x04
;;;;;;;;;;;;;;;;;;
.public vdp_draw_numtile
.public vdp_clear_gamegrid
.public vdp_clear_dialog
.public vdp_redraw_tiles
.public vdp_draw_joystick