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)
!zone {
PARAM = $00
PTR = $02
SRC = $04
DEST = $06
;------------------------------------------------------------------------------
; okvs_init
;
; in: A = store start address [lo]
; Y = store start address [hi]
; out: $02/$03 clobbered
; in: stack contains 2 bytes of parameters:
; +1 [word] handle to storage space
; out: $00/$01 clobbered
; $02/$03 clobbered
; all registers clobbered
;------------------------------------------------------------------------------
okvs_init
sta PTR
sty PTR+1
+PARAMS_ON_STACK 2
jsr SetPTRFromStackParams
lda #0
sta (PTR) ; set number of keys
rts
@ -81,63 +83,50 @@ okvs_init
;------------------------------------------------------------------------------
; okvs_len
;
; in: A = store start address [lo]
; Y = store start address [hi]
; in: stack contains 2 bytes of parameters:
; +1 [word] handle to storage space
; out: A contains number of keys in this store
; $00/$01 clobbered
; $02/$03 clobbered
;------------------------------------------------------------------------------
okvs_len
sta PTR
sty PTR+1
+PARAMS_ON_STACK 2
jsr SetPTRFromStackParams
lda (PTR)
rts
;------------------------------------------------------------------------------
; okvs_append
;
; in: A = store start address [lo]
; Y = store start address [hi]
; stack contains 5 bytes of parameters:
; +1 [word] address of key
; +3 [word] address of value
; +5 [byte] maximum length of value (or 0 to fit)
; in: stack contains 7 bytes of parameters:
; +1 [word] handle to storage space
; +3 [word] address of key
; +5 [word] address of value
; +7 [byte] maximum length of value (or 0 to fit)
; out: all registers clobbered
; $00/$01 clobbered
; $02/$03 clobbered
;------------------------------------------------------------------------------
okvs_append
sta PTR
sty PTR+1
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
+PARAMS_ON_STACK 7
jsr SetPTRFromStackParams
lda (PTR)
inc
sta (PTR)
sta (PTR) ; increment number of keys
tax
jsr incptr ; PTR -> first record
- dex
beq +
jsr followptr ; PTR -> next record
jsr derefptr ; PTR -> next record
bra -
+ ; PTR -> start of new record
+ ; PTR -> new record
lda PTR ; save PTR on stack
pha
lda PTR+1
pha
jsr incptr
jsr incptr ; PTR -> start of new key
ldy #1
jsr incptr ; PTR -> space for new key
ldy #3
lda (PARAM),y ; get source address of new key to copy
sta SRC
iny
@ -147,7 +136,7 @@ okvs_append
inc
sta .keylen
tay
- dey ; copy key
- dey ; copy new key
lda (SRC),y
sta (PTR),y
cpy #$FF
@ -160,8 +149,8 @@ okvs_append
sta PTR
bcc +
inc PTR+1
+ ; PTR -> start of new value
ldy #3
+ ; PTR -> space for new value
ldy #5
lda (PARAM),y ; get source address of new value
sta SRC
iny
@ -192,7 +181,7 @@ okvs_append
pla
sta PTR+1
pla
sta PTR ; PTR -> start of this record
sta PTR ; PTR -> this record again
lda SRC ; update next-record pointer
sta (PTR)
ldy #1
@ -200,6 +189,73 @@ okvs_append
sta (PTR),y
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
; preserves all registers
phx
@ -211,7 +267,15 @@ incptr
+ plx
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
lda (PTR)
pha

View File

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

View File

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

View File

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