4cade/src/textrank.a

240 lines
6.2 KiB
Plaintext
Raw Normal View History

2019-06-18 18:56:05 +00:00
;license:MIT
2020-04-01 15:57:21 +00:00
;(c) 2018-2020 by 4am & qkumba
2019-06-18 18:56:05 +00:00
;
; text rank - an implementation of the Quicksilver search rank algorithm
;
; Public functions
2021-10-28 05:26:21 +00:00
; - ReloadSearchIndex
2019-06-27 02:51:34 +00:00
; - ResetTextRank
2021-11-06 23:12:40 +00:00
; - FindTitleInCache
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
2020-04-01 15:57:21 +00:00
;
; Zero page variables
; - 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 " "
2020-04-01 15:57:21 +00:00
;------------------------------------------------------------------------------
2021-10-28 05:26:21 +00:00
; ReloadSearchIndex
;
; Load indexes to support search UI
2020-04-01 15:57:21 +00:00
;
; in: none
; out: gSearchStore populated
;------------------------------------------------------------------------------
2021-10-28 05:26:21 +00:00
ReloadSearchIndex
jsr LoadIndexedFile ; load appropriate search cache into $B000
2021-11-06 23:12:40 +00:00
!word gSearchCache
!word kSearchCacheRecord
ReloadSearchIndexOnly
jsr LoadIndexedFile ; load appropriate search index into $6000
SearchIndexHandle
!word gSearchIndex
!word kSearchIndexRecord
2021-11-06 23:12:40 +00:00
rts
FindTitleInCache
ldx InputLength
cpx #5
bcs @nomatch
lda #$20
sta InputBuffer, x
+LDADDR gSearchCache
+ST16 PTR
ldx #$FF
-- inx
ldy #0
- lda (PTR), y
beq @nomatch
cmp InputBuffer, x
beq @matchchar
iny
iny
iny
bne - ; always branches
@matchchar
iny
lda (PTR), y
pha
iny
lda (PTR), y
bpl @foundindex
sta PTR+1
pla
sta PTR
cpx InputLength
bcc --
@nomatch
sec
rts
@foundindex
sta BestMatchIndex+1
pla
sta BestMatchIndex
clc
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
2020-11-09 20:22:38 +00:00
tax
dex
cpx 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
2020-11-09 20:22:38 +00:00
jsr tolower
2019-06-18 18:56:05 +00:00
sta tmp
ldy startat
-- lda (zpstring),y
2020-11-09 20:22:38 +00:00
jsr tolower
2019-06-18 18:56:05 +00:00
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
2020-04-23 21:10:01 +00:00
adc #0 ; round fractions up
2019-06-18 18:56:05 +00:00
pha
ldy #1
lda (zpstring),y
2020-11-09 20:22:38 +00:00
jsr tolower
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
2020-11-09 20:22:38 +00:00
tolower
cmp #$41
bcc +
cmp #$5B
bcs +
ora #$20
+ rts