passport/src/patchers/t00_nibtable.a
2021-06-21 11:57:02 -04:00

158 lines
3.3 KiB
Plaintext
Executable File

;-------------------------------
; #NIBTABLE
; detect and fix non-standard
; nibble-to-byte translation
; e.g. Travels with Za-Zoom
;-------------------------------
!zone {
_nibtable
; gTrack = 0
; Caller found DOS 3.3 RWTS
lda #$02
ldx #$6D
jsr compare3 ; and T00,S02,$6D ==
!byte $BD,$29,$BA
bcs .checkwrite
ldx #$82
jsr compare3 ; and T00,S02,$82 ==
!byte $BD,$29,$BA
bcs .checkwrite
ldx #$95
jsr compare3 ; and T00,S02,$95 ==
!byte $BD,$29,$BA
bcs .checkwrite
jsr .fixwrite
.checkwrite
lda #$03
ldx #$09
jsr compare3 ; if T00,S03,$09 ==
!byte $59,$00,$BA
bcs .exit
ldx #$1A
jsr compare3 ; and T00,S03,$1A ==
!byte $59,$00,$BA
bcs .exit
ldx #$2A
jsr compare3 ; and T00,S03,$2A ==
!byte $D9,$00,$BA
bcs .exit
jsr .fixread
.exit
jmp .exit2
.fixread
lda #$6A
sta .loopcheck+1
lda #.nibcheck-.noskip
sta .nibskip+1 ; enable wildcard
lda #$96
ldx #<(gNIBTableBase+$16)
ldy #>(gNIBTableBase+$16)
bne .common
.fixwrite
lda #$16
sta modsrc
lda #$20
ldx #$00
tay
jsr build6and2 ; to $2000
lda #$40
sta .loopcheck+1
lda #$29
ldx #$00
stx .nibskip+1 ; disable wildcard
ldy #$20
.common
sta nibdestindex
stx .nibload1+1
sty .nibload1+2
stx .nibload2+1
sty .nibload2+2
lda #$00
sta nibsrcindex
.modloop
ldy nibsrcindex
ldx nibdestindex
;loop while match succeeds
.nibload1
lda $D1D1,y
.nibskip
bmi .noskip
.noskip
cmp (BASEPAGE*256)+$400,x
beq .nibcheck
;count mismatches until match or end
inx
iny
cpy .loopcheck+1
bne .nibload1
;if we saw mismatches...
.nibcheck
cpy nibsrcindex
beq .advance
;remember where
txa
pha
tya
pha
;determine how many
sec
sbc nibsrcindex
pha
tax
;construct dynamic data to apply mods
lda #$20
sta $300
lda #<modify
sta $301
lda #>modify
sta $302
lda #$60
sta $303,x
.nibdec
dex
dey
.nibload2
lda $D1D1,y
sta $303,x
txa
bne .nibdec
pla
tay
lda #$04
ldx nibdestindex
jsr $300
;restore position, then resume
pla
sta nibsrcindex
pla
sta nibdestindex
.advance
inc nibsrcindex
inc nibdestindex
lda nibsrcindex
.loopcheck
cmp #$D1
bcc .modloop
rts
.exit2
}