2021-08-24 20:45:33 +00:00
|
|
|
; A simple scripting engine
|
|
|
|
;
|
|
|
|
; The scripting engine is driven by the GTE Timers, which are
|
|
|
|
; driven by the VBL Tick count.
|
|
|
|
;
|
|
|
|
; The scripting engine is composed of a sequence of simple commands, defined as follows
|
|
|
|
;
|
|
|
|
; COMMAND ARG1 ARG2 ARG3
|
|
|
|
;
|
|
|
|
; The COMMAND word has its bits defined as:
|
|
|
|
;
|
2021-10-06 12:10:09 +00:00
|
|
|
; bit 15 = 1 if the end of a sequence
|
2021-08-24 20:45:33 +00:00
|
|
|
; bit 14 = 0 proceed to next action, 1 jump
|
|
|
|
; bit 13 = 0 (Reserved)
|
|
|
|
; bit 12 = 0 (Reserved)
|
|
|
|
; bit 11 - 8 = signed jump displacement
|
|
|
|
; bit 8 - 0 = command number
|
|
|
|
;
|
|
|
|
; The defined commands are
|
|
|
|
;
|
|
|
|
; COMMANDS ARG1 ARG2 ARG3
|
|
|
|
; -----------------------------------------------------
|
|
|
|
; $0002 SET_PALETTE_ENTRY ADDR COLOR ---- : Set the palette entry at ARG1 to the color in ARG2
|
|
|
|
; $0004 SWAP_PALETTE_ENTRY ADDR1 ADDR2 ---- : Swap the palette entries in ADDR1 <-> ADDR2
|
2021-10-06 12:10:09 +00:00
|
|
|
; $0006 SET_DYN_TILE TILE_ID DTILE_ID ---- : Copy data from TileData into Dynamic Tile Area
|
2021-08-24 20:45:33 +00:00
|
|
|
; $0010 CALLBACK LONG_ADDR PARAM : Call a user-defined function (JSL) with a parameter value in accumulator
|
|
|
|
|
|
|
|
; Start a new script
|
|
|
|
;
|
|
|
|
; A = low word of script command array
|
|
|
|
; X = high word of script command array
|
|
|
|
; Y = number of ticks between each command step
|
2021-08-25 14:28:53 +00:00
|
|
|
;
|
|
|
|
; A pointer to the current command instruction is stored in the first 4 bytes of the
|
|
|
|
; timer's user data section.
|
2022-06-27 03:08:42 +00:00
|
|
|
_StartScript phx ; Save the script array address
|
2021-08-24 20:45:33 +00:00
|
|
|
pha
|
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
lda #_DoScriptSeq ; Try to create a timer for this script
|
2021-08-25 14:28:53 +00:00
|
|
|
ldx #^_DoScriptSeq
|
2021-08-24 20:45:33 +00:00
|
|
|
clc
|
2022-06-27 03:08:42 +00:00
|
|
|
jsr _AddTimer
|
2021-10-06 12:10:09 +00:00
|
|
|
bcs :err ; No timer slots available :(
|
2021-08-24 20:45:33 +00:00
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
tax ; Initialize the UserData with the command pointer
|
2021-08-24 20:45:33 +00:00
|
|
|
pla
|
|
|
|
sta Timers+8,x
|
|
|
|
pla
|
|
|
|
sta Timers+10,x
|
2022-06-27 03:08:42 +00:00
|
|
|
rts
|
2021-08-24 20:45:33 +00:00
|
|
|
|
|
|
|
:err
|
2021-10-06 12:10:09 +00:00
|
|
|
pla ; Pop the values and return with the carry flag set
|
2021-08-24 20:45:33 +00:00
|
|
|
pla
|
2022-06-27 03:08:42 +00:00
|
|
|
rts
|
2021-08-24 20:45:33 +00:00
|
|
|
|
2021-08-25 14:28:53 +00:00
|
|
|
; This routine executes script command until it encounters one with the STOP bit set. In some
|
2021-10-06 12:10:09 +00:00
|
|
|
; sense, the stop bit acts like a "yield" in high-level languages.
|
|
|
|
|
|
|
|
ARG1 equ 2
|
|
|
|
ARG2 equ 4
|
|
|
|
ARG3 equ 6
|
|
|
|
|
2021-08-25 14:28:53 +00:00
|
|
|
_DoScriptSeq
|
2021-10-06 12:10:09 +00:00
|
|
|
phx ; save the timer index; will need to update user data at the end
|
|
|
|
phb ; save the current data bank
|
2021-08-25 14:28:53 +00:00
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
sep #$20 ; push the bank byte of the command list pointer on the stack
|
2021-08-24 20:45:33 +00:00
|
|
|
lda Timers+10,x
|
2021-08-25 14:28:53 +00:00
|
|
|
pha
|
|
|
|
rep #$20
|
2021-08-24 20:45:33 +00:00
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
lda Timers+8,x ; get the current address of the command sequence
|
2021-08-24 20:45:33 +00:00
|
|
|
tax
|
2021-10-06 12:10:09 +00:00
|
|
|
plb ; pop the bank
|
2021-08-25 14:28:53 +00:00
|
|
|
|
|
|
|
; Now we are ready to process commands until reaching one with the STOP bit set. Each command
|
|
|
|
; is 8 bytes, so we just have to do a very simple fetch/execute/increment loop. The only
|
|
|
|
; exception is handling the JUMP bit which requires moving the script pc stored in the
|
|
|
|
; x-register.
|
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
_dss_loop phx ; Save the command address
|
|
|
|
txy ; Cache in the y-register
|
2021-08-25 14:28:53 +00:00
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
lda: 0,x ; Load the command word
|
2022-06-27 04:32:15 +00:00
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
pha ; Stash it
|
2021-08-25 14:28:53 +00:00
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
and #$001E ; Only have 16 built-in commands. Use the _UserCallback
|
|
|
|
tax ; command for custom functionality
|
|
|
|
jmp (_dss_commands,x)
|
2021-08-24 20:45:33 +00:00
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
_dss_commands dw _Null,_SetPalEntry,_SwapPalEntry,_SetDTile,_Null,_Null,_Null,_Null
|
|
|
|
dw _UserCallback,_Null,_Null,_Null,_Null,_Null,_Null,_Null
|
|
|
|
|
|
|
|
_dss_cmd_rtn
|
|
|
|
lda 1,s ; Reload the command word
|
2021-08-25 14:28:53 +00:00
|
|
|
|
|
|
|
; Move to the next instruction. If the JUMP bit is set, we move the address forward or
|
|
|
|
; backward N commands (8 bytes at a time). If the JUMP bit is not set, then just move
|
|
|
|
; to the next entry.
|
2021-10-06 12:10:09 +00:00
|
|
|
bit #JUMP ; Just do a fall through and set the jump offset to
|
|
|
|
bne :move_addr ; a hard-coded value of 1 if the jump bit is not set
|
2022-06-27 03:08:42 +00:00
|
|
|
:retry lda #$0040
|
|
|
|
:move_addr and #$0FC0 ; mask out the number of commands to move
|
2021-10-06 12:10:09 +00:00
|
|
|
beq :retry ; Don't allow zeros; will cause infinite loop. Just advance by one.
|
2021-08-25 14:28:53 +00:00
|
|
|
|
2022-06-27 03:08:42 +00:00
|
|
|
cmp #$0800 ; Sign-extend the 6-bit value
|
2021-08-25 14:28:53 +00:00
|
|
|
bcc *+5
|
2022-06-27 03:08:42 +00:00
|
|
|
ora #$F000
|
|
|
|
|
|
|
|
cmp #$8000 ; make it a multiple of 8 (preserve sign)
|
|
|
|
ror
|
|
|
|
cmp #$8000
|
|
|
|
ror
|
|
|
|
cmp #$8000
|
|
|
|
ror
|
2021-08-25 14:28:53 +00:00
|
|
|
clc
|
2021-10-06 12:10:09 +00:00
|
|
|
adc 3,s ; add it to the saved command address
|
2021-08-25 14:28:53 +00:00
|
|
|
sta 3,s
|
|
|
|
|
|
|
|
; Check to see if we stop on this instruction, or continue executing commands
|
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
pla ; Reload the command word
|
|
|
|
plx ; Pop off the update command address
|
2021-08-25 14:28:53 +00:00
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
bit #YIELD ; If the stop bit is set, we're done with this sequence
|
|
|
|
beq _dss_loop ; Otherwise, keep going and fetch the next command word
|
2021-08-25 14:28:53 +00:00
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
txa ; save the current command address
|
|
|
|
plb ; restore the data bank and the timer index
|
2021-08-25 14:28:53 +00:00
|
|
|
plx
|
2021-10-06 12:10:09 +00:00
|
|
|
sta Timers+8,x ; store the command address back into the timer user data space
|
2021-08-24 20:45:33 +00:00
|
|
|
|
|
|
|
rtl
|
|
|
|
|
|
|
|
; Implementation of the built-in commands
|
2021-10-06 12:10:09 +00:00
|
|
|
_Null brl _dss_cmd_rtn
|
2021-08-24 20:45:33 +00:00
|
|
|
|
|
|
|
_SetPalEntry
|
|
|
|
ldx: ARG1,y
|
|
|
|
lda: ARG2,y
|
|
|
|
stal SHR_PALETTES,x
|
2021-10-06 12:10:09 +00:00
|
|
|
brl _dss_cmd_rtn
|
2021-08-24 20:45:33 +00:00
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
_SwapPalEntry
|
|
|
|
ldx: ARG1,y ; Load palette values
|
2021-08-24 20:45:33 +00:00
|
|
|
ldal SHR_PALETTES,x
|
|
|
|
pha
|
2021-08-26 13:52:43 +00:00
|
|
|
ldx: ARG2,y
|
2021-08-24 20:45:33 +00:00
|
|
|
ldal SHR_PALETTES,x
|
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
ldx: ARG1,y ; and swap
|
2021-08-24 20:45:33 +00:00
|
|
|
stal SHR_PALETTES,x
|
|
|
|
|
2021-08-26 13:52:43 +00:00
|
|
|
ldx: ARG2,y
|
2021-08-24 20:45:33 +00:00
|
|
|
pla
|
|
|
|
stal SHR_PALETTES,x
|
2021-10-06 12:10:09 +00:00
|
|
|
brl _dss_cmd_rtn
|
|
|
|
|
|
|
|
_SetDTile
|
|
|
|
ldx: ARG1,y
|
|
|
|
lda: ARG2,y
|
|
|
|
tay
|
2022-06-27 04:32:15 +00:00
|
|
|
jsr CopyTileToDyn
|
2021-10-06 12:10:09 +00:00
|
|
|
brl _dss_cmd_rtn
|
2021-08-24 20:45:33 +00:00
|
|
|
|
2021-10-06 12:10:09 +00:00
|
|
|
_UserCallback
|
|
|
|
lda: ARG1,y
|
2022-05-19 02:00:06 +00:00
|
|
|
stal :dispatch+1
|
2021-10-06 12:10:09 +00:00
|
|
|
lda: ARG1+1,y
|
2022-05-19 02:00:06 +00:00
|
|
|
stal :dispatch+2
|
2021-10-06 12:10:09 +00:00
|
|
|
lda: ARG3,y
|
2021-08-24 20:45:33 +00:00
|
|
|
:dispatch jsl $000000
|
2021-10-06 12:10:09 +00:00
|
|
|
brl _dss_cmd_rtn
|