4cade/src/textrank.a

195 lines
5.3 KiB
Plaintext
Raw Normal View History

2019-06-18 18:56:05 +00:00
;license:MIT
;(c) 2018-9 by 4am & qkumba
2019-06-18 18:56:05 +00:00
;
; text rank - an implementation of the Quicksilver search rank algorithm
;
; Public functions
; - BuildSearchStore
2019-06-27 02:51:34 +00:00
; - ResetTextRank
2019-06-18 18:56:05 +00:00
; - TextRankCallback (as okvs_iter_values callback)
;
; Public variables
2019-06-27 02:51:34 +00:00
; - InputLength ; [byte] number of characters typed in search mode
; - InputBuffer ; [25 bytes] characters typed in search mode
; - SelectedIndex ; [byte] index in gSearchStore of currently selected game in search mode
2019-06-27 02:51:34 +00:00
; - MatchCount ; [byte] number of games that match InputBuffer
; - BestMatchScore ; [byte] raw ranking score (0-100) of current best match (updated during TextRankCallback)
; - BestMatchIndex ; [byte] index in gSearchStore of current best match (updated during TextRankCallback)
2019-06-18 18:56:05 +00:00
MaxInputLength = 26
2019-06-18 18:56:05 +00:00
InputLength
!byte 0
InputBuffer
!text " "
BuildSearchStore
jsr SwitchToBank2
jsr EnableAcceleratorAndSwitchToBank1
+LDADDR gSearchStore
jsr okvs_init
jsr okvs_iter
!word gGamesListStore
!word @callback
2020-03-19 02:11:28 +00:00
jsr SwitchToBank2
jmp DisableAcceleratorAndSwitchToBank1
@callback
; callback called by okvs_iter on gGamesListStore
; in: A/Y contains address of filename
2020-03-24 20:30:14 +00:00
; $WINDEX contains 0-based index of the current record in gGamesListStore (word)
; out: all registers and flags clobbered
2020-03-24 20:30:14 +00:00
+ST16 @key
jsr GetGameDisplayName
2020-03-24 20:30:14 +00:00
+ST16 @value
@append
jsr okvs_append
!word gSearchStore
@key !word $FDFD ; SMC
@value !word $FDFD ; SMC
!byte 0
rts
2019-06-18 20:52:35 +00:00
;------------------------------------------------------------------------------
; ResetTextRank
; reset the Match variables to allow re-scanning (e.g. because of backspace)
; in: nothing
; out: X, MatchCount, BestMatchScore, BestMatchIndex zeroed
;------------------------------------------------------------------------------
ResetTextRank
ldx #0
stx MatchCount
stx BestMatchScore
2019-06-18 21:07:14 +00:00
dex
2019-06-18 20:52:35 +00:00
stx BestMatchIndex
2020-03-24 20:30:14 +00:00
stx BestMatchIndex+1
2019-06-18 20:52:35 +00:00
rts
2019-06-18 18:56:05 +00:00
;------------------------------------------------------------------------------
; TextRankCallback
; callback called by okvs_iter_values on gSearchStore
; to calculate a ranking score for a single game display name
2019-06-18 18:56:05 +00:00
; against the current InputBuffer
; in: A/Y contains address of game display name
2020-03-24 20:30:14 +00:00
; $WINDEX contains 0-based index of the current record in gSearchStore (word)
2019-06-18 18:56:05 +00:00
; out: all registers and flags clobbered
; MatchCount possibly incremented (if this game was a match at all)
; BestMatchScore and BestMatchIndex possibly updated (if this game
2019-06-18 18:56:05 +00:00
; was the best match so far)
;------------------------------------------------------------------------------
TextRankCallback
2020-03-24 20:30:14 +00:00
+ST16 zpstring ; A/Y = address of this game display name
2019-06-18 18:56:05 +00:00
+LDADDR InputLength
2020-03-24 20:30:14 +00:00
+ST16 zpword
2019-06-30 19:10:09 +00:00
ldy #0
lda (zpstring),y
cmp InputLength
2019-07-05 02:03:14 +00:00
bcc ++
sta gamelength
2019-09-24 00:01:42 +00:00
sty runningscore
sty runningscore+1
iny
2019-06-18 18:56:05 +00:00
sty startat
- sty i
lda (zpword),y
+LOW_ASCII_TO_LOWER
sta tmp
ldy startat
-- lda (zpstring),y
+LOW_ASCII_TO_LOWER
cmp tmp
beq +
cpy gamelength
iny
bcc --
2019-07-05 02:03:14 +00:00
++ rts ; no match :(
+ ldx #80
2019-06-18 18:56:05 +00:00
cpy startat
beq +
ldx #10
cpy #1
beq +
dey
lda (zpstring),y
iny
cmp #' '
bne +
ldx #90
+ txa
clc
adc runningscore
sta runningscore
bcc +
inc runningscore+1
2019-07-05 02:03:14 +00:00
+ tya
2019-06-18 18:56:05 +00:00
ldy i
cpy InputLength
2019-07-05 02:03:14 +00:00
bcs +
2019-06-18 18:56:05 +00:00
iny
2019-07-05 02:03:14 +00:00
sta startat
inc startat
cmp gamelength
2019-06-18 18:56:05 +00:00
bcc -
2019-07-05 02:03:14 +00:00
rts ; no match :(
+ lda runningscore
2019-06-18 18:56:05 +00:00
ldx runningscore+1
ldy gamelength
jsr @div
sta tmp
lda runningscore
ldx runningscore+1
ldy InputLength
jsr @div
clc
adc tmp
lsr
pha
ldy #1
lda (zpstring),y
2019-06-18 20:52:35 +00:00
+LOW_ASCII_TO_LOWER
2019-06-18 18:56:05 +00:00
sta firstletter
pla
ldx InputBuffer
cpx firstletter
bne +
cmp #85
bcs +
adc #15
+ cmp BestMatchScore
2019-06-18 18:56:05 +00:00
bcc +
2019-06-27 20:15:48 +00:00
beq +
2019-06-18 18:56:05 +00:00
sta BestMatchScore
2020-03-24 20:30:14 +00:00
lda WINDEX
sta BestMatchIndex
lda WINDEX+1
sta BestMatchIndex+1
2019-06-18 18:56:05 +00:00
inc MatchCount
+ rts
2019-06-18 18:56:05 +00:00
@div
sta num1
stx num1+1
sty num2
2019-06-18 18:56:05 +00:00
lda #0
sta remainder
sta remainder+1
2019-06-18 18:56:05 +00:00
ldx #16
- asl num1
rol num1+1
rol remainder
rol remainder+1
lda remainder
2019-06-18 18:56:05 +00:00
sec
sbc num2
2019-06-18 18:56:05 +00:00
bcc +
sta remainder
dec remainder+1
inc num1
2019-06-18 18:56:05 +00:00
+ dex
bne -
lda num1
2019-06-18 18:56:05 +00:00
rts