okvs_get, handles, and some macros

This commit is contained in:
4am
2018-03-24 15:28:55 -04:00
parent 727886cb58
commit f4a8190cf7
5 changed files with 160 additions and 116 deletions

15
src/macros.a Normal file
View File

@@ -0,0 +1,15 @@
PARAM = $00
!macro PARAMS_ON_STACK .bytes {
pla
sta PARAM
plx
stx PARAM+1
lda #.bytes
clc
adc PARAM
bcc +
inx
+ phx
pha
}

View File

@@ -60,20 +60,22 @@
; ... filler bytes up to value max length (set at append() time) ; ... filler bytes up to value max length (set at append() time)
!zone { !zone {
PARAM = $00
PTR = $02 PTR = $02
SRC = $04 SRC = $04
DEST = $06
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; okvs_init ; okvs_init
; ;
; in: A = store start address [lo] ; in: stack contains 2 bytes of parameters:
; Y = store start address [hi] ; +1 [word] handle to storage space
; out: $02/$03 clobbered ; out: $00/$01 clobbered
; $02/$03 clobbered
; all registers clobbered
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
okvs_init okvs_init
sta PTR +PARAMS_ON_STACK 2
sty PTR+1 jsr SetPTRFromStackParams
lda #0 lda #0
sta (PTR) ; set number of keys sta (PTR) ; set number of keys
rts rts
@@ -81,63 +83,50 @@ okvs_init
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; okvs_len ; okvs_len
; ;
; in: A = store start address [lo] ; in: stack contains 2 bytes of parameters:
; Y = store start address [hi] ; +1 [word] handle to storage space
; out: A contains number of keys in this store ; out: A contains number of keys in this store
; $00/$01 clobbered
; $02/$03 clobbered ; $02/$03 clobbered
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
okvs_len okvs_len
sta PTR +PARAMS_ON_STACK 2
sty PTR+1 jsr SetPTRFromStackParams
lda (PTR) lda (PTR)
rts rts
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; okvs_append ; okvs_append
; ;
; in: A = store start address [lo] ; in: stack contains 7 bytes of parameters:
; Y = store start address [hi] ; +1 [word] handle to storage space
; stack contains 5 bytes of parameters: ; +3 [word] address of key
; +1 [word] address of key ; +5 [word] address of value
; +3 [word] address of value ; +7 [byte] maximum length of value (or 0 to fit)
; +5 [byte] maximum length of value (or 0 to fit)
; out: all registers clobbered ; out: all registers clobbered
; $00/$01 clobbered ; $00/$01 clobbered
; $02/$03 clobbered ; $02/$03 clobbered
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
okvs_append okvs_append
sta PTR +PARAMS_ON_STACK 7
sty PTR+1 jsr SetPTRFromStackParams
lda (PTR)
pla ; get parameters address and update stack
sta PARAM
plx
stx PARAM+1
lda #$05
clc
adc PARAM
bcc +
inx
+ phx
pha
lda (PTR) ; increment number of keys
inc inc
sta (PTR) sta (PTR) ; increment number of keys
tax tax
jsr incptr ; PTR -> first record jsr incptr ; PTR -> first record
- dex - dex
beq + beq +
jsr followptr ; PTR -> next record jsr derefptr ; PTR -> next record
bra - bra -
+ ; PTR -> start of new record + ; PTR -> new record
lda PTR ; save PTR on stack lda PTR ; save PTR on stack
pha pha
lda PTR+1 lda PTR+1
pha pha
jsr incptr jsr incptr
jsr incptr ; PTR -> start of new key jsr incptr ; PTR -> space for new key
ldy #1 ldy #3
lda (PARAM),y ; get source address of new key to copy lda (PARAM),y ; get source address of new key to copy
sta SRC sta SRC
iny iny
@@ -147,7 +136,7 @@ okvs_append
inc inc
sta .keylen sta .keylen
tay tay
- dey ; copy key - dey ; copy new key
lda (SRC),y lda (SRC),y
sta (PTR),y sta (PTR),y
cpy #$FF cpy #$FF
@@ -160,8 +149,8 @@ okvs_append
sta PTR sta PTR
bcc + bcc +
inc PTR+1 inc PTR+1
+ ; PTR -> start of new value + ; PTR -> space for new value
ldy #3 ldy #5
lda (PARAM),y ; get source address of new value lda (PARAM),y ; get source address of new value
sta SRC sta SRC
iny iny
@@ -192,7 +181,7 @@ okvs_append
pla pla
sta PTR+1 sta PTR+1
pla pla
sta PTR ; PTR -> start of this record sta PTR ; PTR -> this record again
lda SRC ; update next-record pointer lda SRC ; update next-record pointer
sta (PTR) sta (PTR)
ldy #1 ldy #1
@@ -200,6 +189,73 @@ okvs_append
sta (PTR),y sta (PTR),y
rts rts
;------------------------------------------------------------------------------
; okvs_get
;
; in: stack contains 4 bytes of parameters:
; +1 [word] handle to storage space
; +3 [word] address of key
; out: if C clear, key was found and A/Y = lo/hi address of value
; if C set, key was not found and all registers are clobbered
; all other flags
; $00/$01 clobbered
; $02/$03 clobbered
; $04/$05 clobbered
;------------------------------------------------------------------------------
okvs_get
+PARAMS_ON_STACK 4
jsr SetPTRFromStackParams
lda (PTR)
beq .fail ; no keys, fail immediately
tax ; X = number of keys
inx
jsr incptr ; PTR -> first record
ldy #3
lda (PARAM),y
sta SRC
iny
lda (PARAM),y
sta SRC+1 ; SRC -> key we want to find
lda (SRC)
inc
sta .matchlen
.matchRecordLoop
lda PTR+1
sta DEST+1
lda PTR
clc
adc #2
sta DEST
bcc +
inc DEST+1 ; DEST -> key of this record
+ ldy #0
.matchKeyLoop
lda (SRC),y
cmp (DEST),y
bne .goToNextRecord
iny
.matchlen=*+1
cpy #$FD ; set at runtime
bne .matchKeyLoop
lda PTR
ldy PTR+1
clc
adc .matchlen
sta PTR
bcc +
iny
+ clc
rts
.goToNextRecord
jsr derefptr ; PTR -> next record
dex
bne .matchRecordLoop
.fail sec
rts
incptr incptr
; preserves all registers ; preserves all registers
phx phx
@@ -211,7 +267,15 @@ incptr
+ plx + plx
rts rts
followptr SetPTRFromStackParams
ldy #1
lda (PARAM),y
sta PTR
iny
lda (PARAM),y
sta PTR+1 ; PTR -> first parameter on stack
; execution falls through here
derefptr
; preserves X ; preserves X
lda (PTR) lda (PTR)
pha pha

View File

@@ -11,6 +11,7 @@
!byte 10 !byte 10
!raw "LIB/WEEGUI" !raw "LIB/WEEGUI"
!source "src/macros.a"
!source "src/WeeGUI_MLI.s" !source "src/WeeGUI_MLI.s"
!source "src/memory.a" !source "src/memory.a"
!source "src/okvs.a" !source "src/okvs.a"

View File

@@ -17,6 +17,8 @@
TRUE = 1 TRUE = 1
FALSE = 0 FALSE = 0
kDefaultGame = 30 ; Zork I kDefaultGame = 30 ; Zork I
gPrefsStore
!word $8000 ; address of storage space for prefs
; global storage for parsed preference values ; global storage for parsed preference values
gForce40 gForce40
@@ -47,31 +49,33 @@ LoadGlobalPreferences
lda #kDefaultGame lda #kDefaultGame
sta gCurrentGame sta gCurrentGame
lda #<kGlobalPrefsStore
ldy #>kGlobalPrefsStore
jsr okvs_init jsr okvs_init
!word gPrefsStore
lda #<kGlobalPrefsStore
ldy #>kGlobalPrefsStore
jsr okvs_append jsr okvs_append
!word gPrefsStore
!word .force40 !word .force40
!word .debug0 !word .debug0
!byte 0 !byte 0
lda #<kGlobalPrefsStore
ldy #>kGlobalPrefsStore
jsr okvs_append jsr okvs_append
!word gPrefsStore
!word .forceupper !word .forceupper
!word .debug0 !word .debug0
!byte 0 !byte 0
lda #<kGlobalPrefsStore
ldy #>kGlobalPrefsStore
jsr okvs_append jsr okvs_append
!word gPrefsStore
!word .sort !word .sort
!word .debugsortvalue !word .debugsortvalue
!byte 0 !byte 0
jsr okvs_get
!word gPrefsStore
!word .sort
bcs .exit
;jmp $ff59
bra .exit bra .exit
; TODO ; TODO
jsr LoadFile ; load prefs file at $2000 jsr LoadFile ; load prefs file at $2000

View File

@@ -52,29 +52,19 @@ PRODOSMLI = $BF00 ; [callable] MLI entry point
;------------------------------- ;-------------------------------
!zone { !zone {
LoadFile LoadFile
pla +PARAMS_ON_STACK 8
sta $00
plx
stx $01
lda #$08
clc
adc $00
bcc +
inx
+ phx
pha
ldy #$01 ldy #$01
lda ($00),y ; lo byte of pathname lda (PARAM),y ; lo byte of pathname
sta mliparam+1 sta mliparam+1
iny iny
lda ($00),y ; hi byte of pathname lda (PARAM),y ; hi byte of pathname
sta mliparam+2 sta mliparam+2
ldy #$07 ldy #$07
lda ($00),y ; lo byte of ProDOS file buffer lda (PARAM),y ; lo byte of ProDOS file buffer
sta mliparam+3 sta mliparam+3
iny iny
lda ($00),y ; hi byte of ProDOS file buffer lda (PARAM),y ; hi byte of ProDOS file buffer
sta mliparam+4 sta mliparam+4
jsr _openfile jsr _openfile
@@ -82,16 +72,16 @@ LoadFile
pha ; push file reference number pha ; push file reference number
ldy #$03 ldy #$03
lda ($00),y ; lo address of data buffer lda (PARAM),y ; lo address of data buffer
sta mliparam+2 sta mliparam+2
iny iny
lda ($00),y ; hi address of data buffer lda (PARAM),y ; hi address of data buffer
sta mliparam+3 sta mliparam+3
iny iny
lda ($00),y ; lo data length lda (PARAM),y ; lo data length
sta mliparam+4 sta mliparam+4
iny iny
lda ($00),y ; hi data length lda (PARAM),y ; hi data length
sta mliparam+5 sta mliparam+5
pla ; pull file reference number pla ; pull file reference number
jsr _readfile jsr _readfile
@@ -126,20 +116,10 @@ LoadFile
;------------------------------- ;-------------------------------
!zone { !zone {
LoadDHRFile LoadDHRFile
pla +PARAMS_ON_STACK 4
sta $00
plx
stx $01
lda #$04
clc
adc $00
bcc +
inx
+ phx
pha
ldy #$04 ldy #$04
- lda ($00),y - lda (PARAM),y
sta mliparam,y sta mliparam,y
dey dey
bne - bne -
@@ -207,58 +187,48 @@ LoadDHRFile
;------------------------------- ;-------------------------------
!zone { !zone {
SaveFile SaveFile
pla +PARAMS_ON_STACK $0B
sta $00
plx
stx $01
lda #$0B
clc
adc $00
bcc +
inx
+ phx
pha
ldy #$01 ldy #$01
lda ($00),y ; lo byte of pathname lda (PARAM),y ; lo byte of pathname
sta mliparam+1 sta mliparam+1
iny iny
lda ($00),y ; hi byte of pathname lda (PARAM),y ; hi byte of pathname
sta mliparam+2 sta mliparam+2
jsr _deletefile ; don't care if this fails jsr _deletefile ; don't care if this fails
ldy #$03 ldy #$03
lda ($00),y ; file type lda (PARAM),y ; file type
sta mliparam+4 sta mliparam+4
iny iny
lda ($00),y ; lo byte of aux file type lda (PARAM),y ; lo byte of aux file type
sta mliparam+5 sta mliparam+5
iny iny
lda ($00),y ; hi byte of aux file type lda (PARAM),y ; hi byte of aux file type
sta mliparam+6 sta mliparam+6
jsr _createfile jsr _createfile
bcs .exit bcs .exit
ldy #$0A ldy #$0A
lda ($00),y ; lo byte of ProDOS file buffer lda (PARAM),y ; lo byte of ProDOS file buffer
sta mliparam+3 sta mliparam+3
iny iny
lda ($00),y ; hi byte of ProDOS file buffer lda (PARAM),y ; hi byte of ProDOS file buffer
sta mliparam+4 sta mliparam+4
jsr _openfile jsr _openfile
bcs .exit bcs .exit
pha ; push file reference number pha ; push file reference number
ldy #$06 ldy #$06
lda ($00),y ; lo address of data buffer lda (PARAM),y ; lo address of data buffer
sta mliparam+2 sta mliparam+2
iny iny
lda ($00),y ; hi address of data buffer lda (PARAM),y ; hi address of data buffer
sta mliparam+3 sta mliparam+3
iny iny
lda ($00),y ; lo data length lda (PARAM),y ; lo data length
sta mliparam+4 sta mliparam+4
iny iny
lda ($00),y ; hi data length lda (PARAM),y ; hi data length
sta mliparam+5 sta mliparam+5
pla ; pull file reference number pla ; pull file reference number
jsr _writefile jsr _writefile
@@ -287,23 +257,13 @@ SaveFile
;------------------------------- ;-------------------------------
!zone { !zone {
SetPrefix SetPrefix
pla +PARAMS_ON_STACK 2
sta $00
plx
stx $01
lda #$02
clc
adc $00
bcc +
inx
+ phx
pha
ldy #$01 ldy #$01
lda ($00),y ; lo byte of pathname lda (PARAM),y ; lo byte of pathname
sta mliparam+1 sta mliparam+1
iny iny
lda ($00),y ; hi byte of pathname lda (PARAM),y ; hi byte of pathname
sta mliparam+2 sta mliparam+2
jmp _setprefix jmp _setprefix