passport/src/compare.a

248 lines
5.5 KiB
Plaintext
Executable File

WILDCARD = $97
;-------------------------------
; SearchTrack subroutine
; written by qkumba
; search for a string occurring anywhere
; in the data buffer for the current track
; #WILDCARD in search string matches any 1 byte
; (like "." character in regular expressions)
; in: Y = string length
; stack = string to find
; out: C clear if string found
; or set if not found
; If C is clear, then
; A = @gDisplayBytes = sector where string was found
; X = starting offset where string was found
; all other registers and flags clobbered
;-------------------------------
SearchTrack
;set end point for the search
lda #BASEPAGE+$10
sta .endvalue+1
lda #BASEPAGE
bne SearchSectors
;-------------------------------
; SearchSector subroutine
; written by qkumba
; same as SearchTrack, but for a single sector
; in: A = high byte of sector address in data buffer (e.g. $10 for sector 0)
; Y = string length
; stack = string to find
; out: C clear if string found
; or set if not found
; If C is clear, then
; X = starting offset where string was found
; all other registers and flags clobbered
;-------------------------------
SearchSector
;set end point for the search
tax
inx
stx .endvalue+1
; set high part of initial search position
SearchSectors
sta search+2
pla
sta match_buffer1+1
sta match_all+1
pla
sta match_buffer1+2
sta match_all+2
tax
sty match_size1+1
sty match_size2+1
; fetch last byte to improve search speed
match_buffer1
lda $d1d1,y ; modified at runtime
sta check_byte1+1
sta check_byte2+1
; set low part of initial search position
tya
dey
sty cont_search+1
; set return address
clc
adc match_buffer1+1
tay
bcc plus01
inx
plus01
txa
pha
tya
pha
; set match position
inc match_all+1
bne plus02
inc match_all+2
plus02
lda #<cont_search-branch_cont-2
sta branch_cont+1
; search...
cont_search
ldy #$d1 ; modified at runtime
search
lda $d100,y ; modified at runtime
iny
beq check_end
check_byte1
cmp #$d1 ; modified at runtime
bne search
; point to first byte
sty cont_search+1
check_match
tya
match_size1
sbc #$d1 ; modified at runtime
sta match_buffer2+1
ldx search+2
bcs plus03
dex
plus03
stx match_buffer2+2
ldy #$00
match_all
lda $d1d1,y ; modified at runtime
cmp #WILDCARD
beq found_wild
match_buffer2
cmp $d1d1,y ; modified at runtime
branch_cont
bne cont_search
found_wild
iny
match_size2
cpy #$d1 ; modified at runtime
bne match_all
; point to start of match
ldx match_buffer2+1
lda match_buffer2+2
sec
sbc #BASEPAGE
sta gDisplayBytes
clc
rts
; cold path
check_end
inc search+2
ldx search+2
.endvalue
cpx #$D1
bne check_byte1
ldx #<all_done_set-branch_cont-2
stx branch_cont+1
check_byte2
cmp #$d1 ; modified at runtime
beq check_match
all_done_set
sec
rts
; utility functions for common cases
; (from the caller's perspective, these have the side effect of setting Y,
; since the compare routine will 'save' and 'restore' the value we're setting here)
compare3
ldy #$03
!byte $2C
; /!\ execution falls through here
compare2
ldy #$02
!byte $2C
; /!\ execution falls through here
compare1
ldy #$01
; /!\ execution falls through here
;-------------------------------
; compare subroutine
; in: A = sector
; X = offset
; Y = string length
; stack = string to compare
; #WILDCARD in search string matches any 1 byte
; (like "." character in regular expressions)
; out: C = 0 if string matches, and gDisplayBytes set to A
; C = 1 if not matched
; A,X,Y preserved
; $tmp zero page clobbered
; $cmp1 zero page clobbered
; $cmp2 zero page clobbered
; stack set to first instruction after string
;-------------------------------
compare
sta tmpa
clc
adc #BASEPAGE
!byte $2C
CompareMemory
sta tmpa
sta cmp1+1
stx cmp1
sty tmpy
pla
sta cmp2
pla
sta cmp2+1
tax
tya
clc
adc cmp2
bcc +
inx
+ sta modtmp
txa
pha
lda modtmp
pha
@cmp
lda (cmp2),y
dey
bmi @success
cmp (cmp1),y
beq @cmp
cmp #WILDCARD
beq @cmp
sec
!byte $24 ; hide CLC
@success clc
lda tmpa
ldx cmp1
ldy tmpy
bcs +
sta gDisplayBytes
+ rts