2010-02-12 11:09:23 +00:00
|
|
|
;
|
|
|
|
; Extended memory driver for the Double Quick Brown Box cartridge
|
|
|
|
; Marco van den Heuvel, 2010-01-27
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.include "zeropage.inc"
|
2010-02-12 11:09:23 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.include "em-kernel.inc"
|
|
|
|
.include "em-error.inc"
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.macpack generic
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Header. Includes jump table
|
|
|
|
|
2014-05-01 19:43:34 +00:00
|
|
|
.segment "HEADER"
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Driver signature
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.byte $65, $6d, $64 ; "emd"
|
|
|
|
.byte EMD_API_VERSION ; EM API version number
|
2010-02-12 11:09:23 +00:00
|
|
|
|
2013-05-31 22:11:31 +00:00
|
|
|
; Library reference
|
|
|
|
|
|
|
|
.addr $0000
|
|
|
|
|
|
|
|
; Jump table
|
|
|
|
|
|
|
|
.addr INSTALL
|
|
|
|
.addr UNINSTALL
|
|
|
|
.addr PAGECOUNT
|
|
|
|
.addr MAP
|
|
|
|
.addr USE
|
|
|
|
.addr COMMIT
|
|
|
|
.addr COPYFROM
|
|
|
|
.addr COPYTO
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Constants
|
|
|
|
|
|
|
|
BASE = $8000
|
|
|
|
PAGES = ($C000 - BASE) / 256
|
|
|
|
TARGETLOC = $200 ; Target location for copy/check code
|
|
|
|
CONTROL = $DE00
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Data.
|
|
|
|
|
|
|
|
.proc check
|
|
|
|
template:
|
|
|
|
.org ::TARGETLOC ; Assemble for target location
|
|
|
|
entry:
|
|
|
|
lda $01
|
|
|
|
pha
|
|
|
|
lda #$37
|
|
|
|
sta $01
|
|
|
|
ldx #$14
|
|
|
|
ldy #$90
|
|
|
|
sty CONTROL
|
|
|
|
lda $8000
|
|
|
|
stx CONTROL
|
|
|
|
cmp $8000
|
|
|
|
bne present
|
|
|
|
sty CONTROL
|
|
|
|
inc $8000
|
|
|
|
stx CONTROL
|
|
|
|
cmp $8000
|
|
|
|
beq present
|
|
|
|
dec $8000
|
|
|
|
ldy #$00
|
|
|
|
|
|
|
|
done: pla
|
|
|
|
sta $01
|
|
|
|
rts
|
|
|
|
|
|
|
|
present:
|
|
|
|
sty CONTROL
|
|
|
|
ldy #$01
|
|
|
|
bne done
|
|
|
|
.reloc
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
|
|
|
|
.proc copy
|
|
|
|
template:
|
|
|
|
.org ::TARGETLOC ; Assemble for target location
|
|
|
|
entry:
|
|
|
|
.proc fetch
|
|
|
|
stx CONTROL
|
|
|
|
ldx $01
|
|
|
|
lda #$37
|
|
|
|
sta $01
|
|
|
|
address := *+1 ; Patched at runtime
|
|
|
|
lda ($00),y
|
|
|
|
stx $01
|
|
|
|
ldx #$90
|
|
|
|
stx CONTROL
|
|
|
|
rts
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
.proc stash
|
|
|
|
stx CONTROL
|
|
|
|
ldx $01
|
|
|
|
ldy #$37
|
|
|
|
sty $01
|
|
|
|
ldy #$00
|
|
|
|
address := *+1 ; Patched at runtime
|
|
|
|
sta ($00),y
|
|
|
|
stx $01
|
|
|
|
ldx #$90
|
|
|
|
stx CONTROL
|
|
|
|
rts
|
|
|
|
.endproc
|
|
|
|
.reloc
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
|
|
|
|
.bss
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
curpage: .res 1 ; Current page number
|
|
|
|
window: .res 256 ; Memory "window"
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Since the functions above are copied to $200, the current contents of this
|
|
|
|
; memory area must be saved into backup storage. Allocate enough space.
|
2013-05-09 11:56:54 +00:00
|
|
|
backup: .res .max (.sizeof (copy), .sizeof (check))
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.code
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; INSTALL routine. Is called after the driver is loaded into memory. If
|
|
|
|
; possible, check if the hardware is present and determine the amount of
|
|
|
|
; memory available.
|
|
|
|
; Must return an EM_ERR_xx code in a/x.
|
|
|
|
;
|
|
|
|
|
|
|
|
INSTALL:
|
|
|
|
sei
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr backup_and_setup_check_routine
|
|
|
|
jsr check::entry
|
2010-02-12 11:09:23 +00:00
|
|
|
cli
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #.sizeof (check) - 1
|
|
|
|
jsr restore_data
|
|
|
|
cpy #$01
|
|
|
|
beq @present
|
|
|
|
lda #<EM_ERR_NO_DEVICE
|
|
|
|
ldx #>EM_ERR_NO_DEVICE
|
2010-02-12 11:09:23 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
@present:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #<EM_ERR_OK
|
|
|
|
ldx #>EM_ERR_OK
|
|
|
|
; rts ; Run into UNINSTALL instead
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; UNINSTALL routine. Is called before the driver is removed from memory.
|
|
|
|
; Can do cleanup or whatever. Must not return anything.
|
|
|
|
;
|
|
|
|
|
|
|
|
UNINSTALL:
|
|
|
|
rts
|
|
|
|
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; PAGECOUNT: Return the total number of available pages in a/x.
|
|
|
|
;
|
|
|
|
|
|
|
|
PAGECOUNT:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #<PAGES
|
|
|
|
ldx #>PAGES
|
2010-02-12 11:09:23 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; MAP: Map the page in a/x into memory and return a pointer to the page in
|
|
|
|
; a/x. The contents of the currently mapped page (if any) may be discarded
|
|
|
|
; by the driver.
|
|
|
|
;
|
|
|
|
|
|
|
|
MAP:
|
|
|
|
sei
|
2013-05-09 11:56:54 +00:00
|
|
|
sta curpage ; Remember the new page
|
2010-02-12 11:09:23 +00:00
|
|
|
clc
|
2013-05-09 11:56:54 +00:00
|
|
|
adc #>BASE
|
|
|
|
sta ptr1+1
|
|
|
|
ldy #0
|
|
|
|
sty ptr1
|
|
|
|
jsr backup_and_setup_copy_routine
|
|
|
|
ldx #<ptr1
|
2010-02-12 11:09:23 +00:00
|
|
|
stx copy::fetch::address
|
|
|
|
@L1:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #$14
|
|
|
|
jsr copy::fetch
|
|
|
|
ldx ptr1
|
|
|
|
sta window,x
|
|
|
|
inc ptr1
|
|
|
|
bne @L1
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Return the memory window
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr restore_copy_routine
|
|
|
|
lda #<window
|
|
|
|
ldx #>window ; Return the window address
|
2010-02-12 11:09:23 +00:00
|
|
|
cli
|
|
|
|
rts
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; USE: Tell the driver that the window is now associated with a given page.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
USE: sta curpage ; Remember the page
|
|
|
|
lda #<window
|
|
|
|
ldx #>window ; Return the window
|
2010-02-12 11:09:23 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; COMMIT: Commit changes in the memory window to extended storage.
|
|
|
|
|
|
|
|
COMMIT:
|
|
|
|
sei
|
2013-05-09 11:56:54 +00:00
|
|
|
lda curpage ; Get the current page
|
2010-02-12 11:09:23 +00:00
|
|
|
clc
|
2013-05-09 11:56:54 +00:00
|
|
|
adc #>BASE
|
|
|
|
sta ptr1+1
|
|
|
|
ldy #0
|
|
|
|
sty ptr1
|
|
|
|
jsr backup_and_setup_copy_routine
|
|
|
|
ldx #<ptr1
|
2010-02-12 11:09:23 +00:00
|
|
|
stx copy::stash::address
|
|
|
|
@L1:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx ptr1
|
|
|
|
lda window,x
|
|
|
|
ldx #$14
|
|
|
|
jsr copy::stash
|
|
|
|
inc ptr1
|
|
|
|
bne @L1
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Return the memory window
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr restore_copy_routine
|
2010-02-12 11:09:23 +00:00
|
|
|
cli
|
|
|
|
rts
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; COPYFROM: Copy from extended into linear memory. A pointer to a structure
|
|
|
|
; describing the request is passed in a/x.
|
|
|
|
; The function must not return anything.
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
COPYFROM:
|
|
|
|
sei
|
|
|
|
pha
|
|
|
|
txa
|
|
|
|
pha
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr backup_and_setup_copy_routine
|
2010-02-12 11:09:23 +00:00
|
|
|
pla
|
|
|
|
tax
|
|
|
|
pla
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr setup
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Setup is:
|
|
|
|
;
|
|
|
|
; - ptr1 contains the struct pointer
|
|
|
|
; - ptr2 contains the linear memory buffer
|
|
|
|
; - ptr3 contains -(count-1)
|
|
|
|
; - ptr4 contains the page memory buffer plus offset
|
|
|
|
; - tmp1 contains zero (to be used for linear memory buffer offset)
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #<ptr4
|
2010-02-12 11:09:23 +00:00
|
|
|
sta copy::fetch::address
|
2013-05-09 11:56:54 +00:00
|
|
|
jmp @L3
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
@L1:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #$14
|
|
|
|
ldy #0
|
|
|
|
jsr copy::fetch
|
|
|
|
ldy tmp1
|
|
|
|
sta (ptr2),y
|
|
|
|
inc tmp1
|
|
|
|
bne @L2
|
|
|
|
inc ptr2+1
|
2010-02-12 11:09:23 +00:00
|
|
|
@L2:
|
2013-05-09 11:56:54 +00:00
|
|
|
inc ptr4
|
|
|
|
beq @L4
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Bump count and repeat
|
|
|
|
|
|
|
|
@L3:
|
2013-05-09 11:56:54 +00:00
|
|
|
inc ptr3
|
|
|
|
bne @L1
|
|
|
|
inc ptr3+1
|
|
|
|
bne @L1
|
|
|
|
jsr restore_copy_routine
|
2010-02-12 11:09:23 +00:00
|
|
|
cli
|
|
|
|
rts
|
|
|
|
|
|
|
|
; Bump page register
|
|
|
|
|
|
|
|
@L4:
|
2013-05-09 11:56:54 +00:00
|
|
|
inc ptr4+1
|
|
|
|
jmp @L3
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; COPYTO: Copy from linear into extended memory. A pointer to a structure
|
|
|
|
; describing the request is passed in a/x.
|
|
|
|
; The function must not return anything.
|
|
|
|
;
|
|
|
|
|
|
|
|
COPYTO:
|
|
|
|
sei
|
|
|
|
pha
|
|
|
|
txa
|
|
|
|
pha
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr backup_and_setup_copy_routine
|
2010-02-12 11:09:23 +00:00
|
|
|
pla
|
|
|
|
tax
|
|
|
|
pla
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr setup
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Setup is:
|
|
|
|
;
|
|
|
|
; - ptr1 contains the struct pointer
|
|
|
|
; - ptr2 contains the linear memory buffer
|
|
|
|
; - ptr3 contains -(count-1)
|
|
|
|
; - ptr4 contains the page memory buffer plus offset
|
|
|
|
; - tmp1 contains zero (to be used for linear memory buffer offset)
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #<ptr4
|
2010-02-12 11:09:23 +00:00
|
|
|
sta copy::stash::address
|
2013-05-09 11:56:54 +00:00
|
|
|
jmp @L3
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
@L1:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy tmp1
|
|
|
|
lda (ptr2),y
|
|
|
|
ldx #$14
|
|
|
|
ldy #0
|
|
|
|
jsr copy::stash
|
|
|
|
inc tmp1
|
|
|
|
bne @L2
|
|
|
|
inc ptr2+1
|
2010-02-12 11:09:23 +00:00
|
|
|
@L2:
|
2013-05-09 11:56:54 +00:00
|
|
|
inc ptr4
|
|
|
|
beq @L4
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Bump count and repeat
|
|
|
|
|
|
|
|
@L3:
|
2013-05-09 11:56:54 +00:00
|
|
|
inc ptr3
|
|
|
|
bne @L1
|
|
|
|
inc ptr3+1
|
|
|
|
bne @L1
|
|
|
|
jsr restore_copy_routine
|
2010-02-12 11:09:23 +00:00
|
|
|
cli
|
|
|
|
rts
|
|
|
|
|
|
|
|
; Bump page register
|
|
|
|
|
|
|
|
@L4:
|
2013-05-09 11:56:54 +00:00
|
|
|
inc ptr4+1
|
|
|
|
jmp @L3
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Helper function for COPYFROM and COPYTO: Store the pointer to the request
|
|
|
|
; structure and prepare data for the copy
|
|
|
|
|
|
|
|
setup:
|
2013-05-09 11:56:54 +00:00
|
|
|
sta ptr1
|
|
|
|
stx ptr1+1 ; Save passed pointer
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Get the page number from the struct and adjust it so that it may be used
|
|
|
|
; with the hardware. That is: ptr4 has the page address and page offset
|
|
|
|
; tmp2 will hold the bank value
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #EM_COPY::PAGE
|
|
|
|
lda (ptr1),y
|
2010-02-12 11:09:23 +00:00
|
|
|
clc
|
2013-05-09 11:56:54 +00:00
|
|
|
adc #>BASE
|
|
|
|
sta ptr4+1
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Get the buffer pointer into ptr2
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #EM_COPY::BUF
|
|
|
|
lda (ptr1),y
|
|
|
|
sta ptr2
|
2010-02-12 11:09:23 +00:00
|
|
|
iny
|
2013-05-09 11:56:54 +00:00
|
|
|
lda (ptr1),y
|
|
|
|
sta ptr2+1
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Get the count, calculate -(count-1) and store it into ptr3
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #EM_COPY::COUNT
|
|
|
|
lda (ptr1),y
|
|
|
|
eor #$FF
|
|
|
|
sta ptr3
|
2010-02-12 11:09:23 +00:00
|
|
|
iny
|
2013-05-09 11:56:54 +00:00
|
|
|
lda (ptr1),y
|
|
|
|
eor #$FF
|
|
|
|
sta ptr3+1
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Get the page offset into ptr4 and clear tmp1
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #EM_COPY::OFFS
|
|
|
|
lda (ptr1),y
|
|
|
|
sta ptr4
|
|
|
|
lda #0
|
|
|
|
sta tmp1
|
2010-02-12 11:09:23 +00:00
|
|
|
|
|
|
|
; Done
|
|
|
|
|
|
|
|
rts
|
|
|
|
|
|
|
|
; Helper routines for copying to and from the +256k ram
|
|
|
|
|
|
|
|
backup_and_setup_copy_routine:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #.sizeof (copy) - 1
|
2010-02-12 11:09:23 +00:00
|
|
|
@L1:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda copy::entry,x
|
|
|
|
sta backup,x
|
|
|
|
lda copy::template,x
|
|
|
|
sta copy::entry,x
|
2010-02-12 11:09:23 +00:00
|
|
|
dex
|
2013-05-09 11:56:54 +00:00
|
|
|
bpl @L1
|
2010-02-12 11:09:23 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
backup_and_setup_check_routine:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #.sizeof (check) - 1
|
2010-02-12 11:09:23 +00:00
|
|
|
@L1:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda check::entry,x
|
|
|
|
sta backup,x
|
|
|
|
lda check::template,x
|
|
|
|
sta check::entry,x
|
2010-02-12 11:09:23 +00:00
|
|
|
dex
|
2013-05-09 11:56:54 +00:00
|
|
|
bpl @L1
|
2010-02-12 11:09:23 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
restore_copy_routine:
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #.sizeof (copy) - 1
|
2010-02-12 11:09:23 +00:00
|
|
|
restore_data:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda backup,x
|
|
|
|
sta TARGETLOC,x
|
2010-02-12 11:09:23 +00:00
|
|
|
dex
|
2013-05-09 11:56:54 +00:00
|
|
|
bpl restore_data
|
2010-02-12 11:09:23 +00:00
|
|
|
rts
|