2010-02-04 19:19:17 +00:00
|
|
|
;
|
|
|
|
; Extended memory driver for the C256K memory expansion
|
|
|
|
; Marco van den Heuvel, 2010-01-27
|
|
|
|
;
|
|
|
|
|
|
|
|
.include "zeropage.inc"
|
|
|
|
|
|
|
|
.include "em-kernel.inc"
|
|
|
|
.include "em-error.inc"
|
|
|
|
|
|
|
|
|
|
|
|
.macpack generic
|
|
|
|
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Header. Includes jump table
|
|
|
|
|
|
|
|
.segment "JUMPTABLE"
|
|
|
|
|
|
|
|
; Driver signature
|
|
|
|
|
|
|
|
.byte $65, $6d, $64 ; "emd"
|
|
|
|
.byte EMD_API_VERSION ; EM API version number
|
|
|
|
|
|
|
|
; Jump table.
|
|
|
|
|
|
|
|
.word INSTALL
|
|
|
|
.word UNINSTALL
|
|
|
|
.word PAGECOUNT
|
|
|
|
.word MAP
|
|
|
|
.word USE
|
|
|
|
.word COMMIT
|
|
|
|
.word COPYFROM
|
|
|
|
.word COPYTO
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Constants
|
|
|
|
|
|
|
|
BASE = $4000
|
|
|
|
PAGES = 3 * 256
|
2010-02-12 10:46:57 +00:00
|
|
|
TARGETLOC = $200 ; Target location for copy/check code
|
2010-02-12 11:07:07 +00:00
|
|
|
PIA = $DFC0
|
2010-02-04 19:19:17 +00:00
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Data.
|
|
|
|
|
|
|
|
.data
|
|
|
|
|
|
|
|
|
|
|
|
; This function is used to copy code from and to the extended memory
|
2010-02-12 10:46:57 +00:00
|
|
|
.proc copy
|
|
|
|
template:
|
|
|
|
.org ::TARGETLOC ; Assemble for target location
|
|
|
|
entry:
|
2010-02-12 11:07:07 +00:00
|
|
|
stx PIA
|
2010-02-12 10:46:57 +00:00
|
|
|
stashop = $91 ; 'sta' opcode
|
|
|
|
operation := * ; Location and opcode is patched at runtime
|
|
|
|
address := *+1
|
2010-02-04 19:19:17 +00:00
|
|
|
lda ($00),y
|
|
|
|
ldx #$dc
|
2010-02-12 11:07:07 +00:00
|
|
|
stx PIA
|
2010-02-04 19:19:17 +00:00
|
|
|
rts
|
|
|
|
.reloc
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
; This function is used to check for the existence of the extended memory
|
2010-02-12 10:46:57 +00:00
|
|
|
.proc check
|
|
|
|
template:
|
|
|
|
.org ::TARGETLOC
|
|
|
|
entry:
|
2010-02-04 19:19:17 +00:00
|
|
|
ldy #$00 ; Assume hardware not present
|
|
|
|
|
|
|
|
lda #$fc
|
2010-02-12 11:07:07 +00:00
|
|
|
sta PIA
|
2010-02-04 19:19:17 +00:00
|
|
|
lda $01
|
|
|
|
tax
|
|
|
|
and #$f8
|
|
|
|
sta $01
|
|
|
|
lda $4000
|
|
|
|
cmp $c000
|
|
|
|
bne done ; Jump if not found
|
|
|
|
inc $c000
|
|
|
|
cmp $4000
|
|
|
|
beq done ; Jump if not found
|
|
|
|
|
|
|
|
; Hardware is present
|
|
|
|
iny
|
|
|
|
done: stx $01
|
|
|
|
ldx #$dc
|
2010-02-12 11:07:07 +00:00
|
|
|
stx PIA
|
2010-02-04 19:19:17 +00:00
|
|
|
rts
|
|
|
|
.reloc
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.bss
|
|
|
|
|
|
|
|
curpage: .res 2 ; Current page number
|
|
|
|
curbank: .res 1 ; Current bank
|
|
|
|
window: .res 256 ; Memory "window"
|
|
|
|
|
2010-02-11 18:56:47 +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.
|
2010-02-12 10:46:57 +00:00
|
|
|
backup: .res .max (.sizeof (copy), .sizeof (check))
|
2010-02-11 18:56:47 +00:00
|
|
|
|
|
|
|
|
2010-02-04 19:19:17 +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:
|
2010-02-12 11:07:07 +00:00
|
|
|
lda PIA+1 ; Select Peripheral Registers
|
2010-02-04 19:19:17 +00:00
|
|
|
ora #4
|
2010-02-12 11:07:07 +00:00
|
|
|
sta PIA+1
|
2010-02-04 19:19:17 +00:00
|
|
|
tax
|
2010-02-12 11:07:07 +00:00
|
|
|
lda PIA+3
|
2010-02-04 19:19:17 +00:00
|
|
|
ora #4
|
2010-02-12 11:07:07 +00:00
|
|
|
sta PIA+3
|
2010-02-04 19:19:17 +00:00
|
|
|
tay
|
|
|
|
|
|
|
|
lda #$DC ; Set the default memory bank data
|
2010-02-12 11:07:07 +00:00
|
|
|
sta PIA
|
2010-02-04 19:19:17 +00:00
|
|
|
lda #$FE
|
2010-02-12 11:07:07 +00:00
|
|
|
sta PIA+2
|
2010-02-04 19:19:17 +00:00
|
|
|
|
|
|
|
txa ; Select Data Direction Registers
|
|
|
|
and #$FB
|
2010-02-12 11:07:07 +00:00
|
|
|
sta PIA+1
|
2010-02-04 19:19:17 +00:00
|
|
|
tya
|
|
|
|
and #$FB
|
2010-02-12 11:07:07 +00:00
|
|
|
sta PIA+3
|
2010-02-04 19:19:17 +00:00
|
|
|
|
|
|
|
lda #$FF ; Set the ports to output
|
2010-02-12 11:07:07 +00:00
|
|
|
sta PIA
|
|
|
|
sta PIA+2
|
2010-02-04 19:19:17 +00:00
|
|
|
|
|
|
|
txa
|
|
|
|
and #$C7
|
|
|
|
ora #$30 ; Set CA1 and
|
2010-02-12 11:07:07 +00:00
|
|
|
sta PIA+1 ; select Peripheral Registers
|
|
|
|
sty PIA+3
|
2010-02-04 19:19:17 +00:00
|
|
|
|
|
|
|
jsr backup_and_setup_check_routine
|
2010-02-12 10:46:57 +00:00
|
|
|
jsr check::entry
|
2010-02-04 19:19:17 +00:00
|
|
|
cli
|
2010-02-12 10:46:57 +00:00
|
|
|
ldx #.sizeof (check) - 1
|
2010-02-04 19:19:17 +00:00
|
|
|
jsr restore_data
|
|
|
|
cpy #$01
|
|
|
|
beq @present
|
|
|
|
lda #<EM_ERR_NO_DEVICE
|
|
|
|
ldx #>EM_ERR_NO_DEVICE
|
|
|
|
rts
|
|
|
|
|
|
|
|
@present:
|
|
|
|
lda #<EM_ERR_OK
|
|
|
|
ldx #>EM_ERR_OK
|
2010-02-12 10:46:57 +00:00
|
|
|
; rts ; Run into UNINSTALL instead
|
2010-02-04 19:19:17 +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:
|
|
|
|
lda #<PAGES
|
|
|
|
ldx #>PAGES
|
|
|
|
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
|
2010-02-12 10:46:57 +00:00
|
|
|
sta curpage ; Remember the new page
|
2010-02-04 19:19:17 +00:00
|
|
|
stx curpage+1
|
|
|
|
jsr adjust_page_and_bank
|
|
|
|
stx curbank
|
|
|
|
clc
|
|
|
|
adc #>BASE
|
|
|
|
sta ptr1+1
|
|
|
|
ldy #0
|
|
|
|
sty ptr1
|
|
|
|
jsr backup_and_setup_copy_routine
|
|
|
|
ldx #<ptr1
|
2010-02-12 10:46:57 +00:00
|
|
|
stx copy::address
|
2010-02-04 19:19:17 +00:00
|
|
|
@L1:
|
|
|
|
ldx curbank
|
2010-02-12 10:46:57 +00:00
|
|
|
jsr copy::entry
|
2010-02-04 19:19:17 +00:00
|
|
|
ldx ptr1
|
|
|
|
sta window,x
|
|
|
|
inc ptr1
|
|
|
|
bne @L1
|
|
|
|
|
|
|
|
; Return the memory window
|
|
|
|
|
2010-02-04 20:08:50 +00:00
|
|
|
jsr restore_copy_routine
|
2010-02-04 19:19:17 +00:00
|
|
|
lda #<window
|
2010-02-12 10:46:57 +00:00
|
|
|
ldx #>window ; Return the window address
|
2010-02-04 19:19:17 +00:00
|
|
|
cli
|
|
|
|
rts
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; USE: Tell the driver that the window is now associated with a given page.
|
|
|
|
|
2010-02-12 10:46:57 +00:00
|
|
|
USE: sta curpage ; Remember the page
|
2010-02-04 19:19:17 +00:00
|
|
|
stx curpage+1
|
|
|
|
lda #<window
|
2010-02-12 10:46:57 +00:00
|
|
|
ldx #>window ; Return the window
|
2010-02-04 19:19:17 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; COMMIT: Commit changes in the memory window to extended storage.
|
|
|
|
|
|
|
|
COMMIT:
|
|
|
|
sei
|
2010-02-12 10:46:57 +00:00
|
|
|
lda curpage ; Get the current page
|
2010-02-04 19:19:17 +00:00
|
|
|
ldx curpage+1
|
|
|
|
|
|
|
|
jsr adjust_page_and_bank
|
|
|
|
stx curbank
|
|
|
|
clc
|
|
|
|
adc #>BASE
|
|
|
|
sta ptr1+1
|
|
|
|
ldy #0
|
|
|
|
sty ptr1
|
|
|
|
jsr backup_and_setup_copy_routine
|
|
|
|
ldx #<ptr1
|
2010-02-12 10:46:57 +00:00
|
|
|
stx copy::address
|
|
|
|
ldx #<copy::stashop
|
|
|
|
stx copy::operation
|
2010-02-04 19:19:17 +00:00
|
|
|
@L1:
|
|
|
|
ldx ptr1
|
|
|
|
lda window,x
|
|
|
|
ldx curbank
|
2010-02-12 10:46:57 +00:00
|
|
|
jsr copy::entry
|
2010-02-04 19:19:17 +00:00
|
|
|
inc ptr1
|
|
|
|
bne @L1
|
|
|
|
|
|
|
|
; Return the memory window
|
|
|
|
|
2010-02-04 20:08:50 +00:00
|
|
|
jsr restore_copy_routine
|
2010-02-04 19:19:17 +00:00
|
|
|
done:
|
|
|
|
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
|
|
|
|
jsr setup
|
|
|
|
jsr backup_and_setup_copy_routine
|
|
|
|
|
|
|
|
; 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)
|
|
|
|
; - tmp2 contains the bank value
|
|
|
|
|
|
|
|
lda #<ptr4
|
2010-02-12 10:46:57 +00:00
|
|
|
sta copy::address
|
2010-02-04 19:19:17 +00:00
|
|
|
jmp @L3
|
|
|
|
|
|
|
|
@L1:
|
|
|
|
ldx tmp2
|
|
|
|
ldy #0
|
2010-02-12 10:46:57 +00:00
|
|
|
jsr copy::entry
|
2010-02-04 19:19:17 +00:00
|
|
|
ldy tmp1
|
|
|
|
sta (ptr2),y
|
|
|
|
inc tmp1
|
|
|
|
bne @L2
|
|
|
|
inc ptr2+1
|
|
|
|
@L2:
|
|
|
|
inc ptr4
|
|
|
|
beq @L4
|
|
|
|
|
|
|
|
; Bump count and repeat
|
|
|
|
|
|
|
|
@L3:
|
|
|
|
inc ptr3
|
|
|
|
bne @L1
|
|
|
|
inc ptr3+1
|
|
|
|
bne @L1
|
2010-02-04 20:08:50 +00:00
|
|
|
jsr restore_copy_routine
|
2010-02-04 19:19:17 +00:00
|
|
|
cli
|
|
|
|
rts
|
|
|
|
|
|
|
|
; Bump page register
|
|
|
|
|
|
|
|
@L4:
|
|
|
|
inc ptr4+1
|
|
|
|
lda ptr4+1
|
|
|
|
cmp #$80
|
|
|
|
bne @L3
|
|
|
|
lda #>BASE
|
|
|
|
sta ptr4+1
|
|
|
|
lda tmp2
|
|
|
|
clc
|
|
|
|
adc #$10
|
|
|
|
sta tmp2
|
|
|
|
jmp @L3
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; 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
|
|
|
|
jsr setup
|
|
|
|
jsr backup_and_setup_copy_routine
|
|
|
|
|
|
|
|
; 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)
|
|
|
|
; - tmp2 contains the bank value
|
|
|
|
|
|
|
|
lda #<ptr4
|
2010-02-12 10:46:57 +00:00
|
|
|
sta copy::address
|
|
|
|
lda #<copy::stashop
|
|
|
|
sta copy::operation
|
2010-02-04 19:19:17 +00:00
|
|
|
jmp @L3
|
|
|
|
|
|
|
|
@L1:
|
|
|
|
ldy tmp1
|
|
|
|
lda (ptr2),y
|
|
|
|
ldx tmp2
|
|
|
|
ldy #0
|
2010-02-12 10:46:57 +00:00
|
|
|
jsr copy::entry
|
2010-02-04 19:19:17 +00:00
|
|
|
inc tmp1
|
|
|
|
bne @L2
|
|
|
|
inc ptr2+1
|
|
|
|
@L2:
|
|
|
|
inc ptr4
|
|
|
|
beq @L4
|
|
|
|
|
|
|
|
; Bump count and repeat
|
|
|
|
|
|
|
|
@L3:
|
|
|
|
inc ptr3
|
|
|
|
bne @L1
|
|
|
|
inc ptr3+1
|
|
|
|
bne @L1
|
2010-02-04 20:08:50 +00:00
|
|
|
jsr restore_copy_routine
|
2010-02-04 19:19:17 +00:00
|
|
|
cli
|
|
|
|
rts
|
|
|
|
|
|
|
|
; Bump page register
|
|
|
|
|
|
|
|
@L4:
|
|
|
|
inc ptr4+1
|
|
|
|
lda ptr4+1
|
|
|
|
cmp #$80
|
|
|
|
bne @L3
|
|
|
|
lda #>BASE
|
|
|
|
sta ptr4+1
|
|
|
|
lda tmp2
|
|
|
|
clc
|
|
|
|
adc #$10
|
|
|
|
sta tmp2
|
|
|
|
jmp @L3
|
|
|
|
|
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Helper function for COPYFROM and COPYTO: Store the pointer to the request
|
|
|
|
; structure and prepare data for the copy
|
|
|
|
|
|
|
|
setup:
|
|
|
|
sta ptr1
|
2010-02-12 10:46:57 +00:00
|
|
|
stx ptr1+1 ; Save passed pointer
|
2010-02-04 19:19:17 +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
|
|
|
|
|
|
|
|
ldy #EM_COPY::PAGE+1
|
|
|
|
lda (ptr1),y
|
|
|
|
tax
|
|
|
|
ldy #EM_COPY::PAGE
|
|
|
|
lda (ptr1),y
|
|
|
|
jsr adjust_page_and_bank
|
|
|
|
clc
|
|
|
|
adc #>BASE
|
|
|
|
sta ptr4+1
|
|
|
|
stx tmp2
|
|
|
|
|
|
|
|
; Get the buffer pointer into ptr2
|
|
|
|
|
|
|
|
ldy #EM_COPY::BUF
|
|
|
|
lda (ptr1),y
|
|
|
|
sta ptr2
|
|
|
|
iny
|
|
|
|
lda (ptr1),y
|
|
|
|
sta ptr2+1
|
|
|
|
|
|
|
|
; Get the count, calculate -(count-1) and store it into ptr3
|
|
|
|
|
|
|
|
ldy #EM_COPY::COUNT
|
|
|
|
lda (ptr1),y
|
|
|
|
eor #$FF
|
|
|
|
sta ptr3
|
|
|
|
iny
|
|
|
|
lda (ptr1),y
|
|
|
|
eor #$FF
|
|
|
|
sta ptr3+1
|
|
|
|
|
|
|
|
; Get the page offset into ptr4 and clear tmp1
|
|
|
|
|
|
|
|
ldy #EM_COPY::OFFS
|
|
|
|
lda (ptr1),y
|
|
|
|
sta ptr4
|
|
|
|
lda #0
|
|
|
|
sta tmp1
|
|
|
|
|
|
|
|
; Done
|
|
|
|
|
|
|
|
rts
|
|
|
|
|
|
|
|
; Helper routines for copying to and from the +256k ram
|
|
|
|
|
|
|
|
backup_and_setup_copy_routine:
|
2010-02-12 10:46:57 +00:00
|
|
|
ldx #.sizeof (copy) - 1
|
2010-02-04 19:19:17 +00:00
|
|
|
@L1:
|
2010-02-12 10:46:57 +00:00
|
|
|
lda copy::entry,x
|
2010-02-04 19:19:17 +00:00
|
|
|
sta backup,x
|
2010-02-12 10:46:57 +00:00
|
|
|
lda copy::template,x
|
|
|
|
sta copy::entry,x
|
2010-02-04 19:19:17 +00:00
|
|
|
dex
|
|
|
|
bpl @L1
|
|
|
|
rts
|
|
|
|
|
|
|
|
backup_and_setup_check_routine:
|
2010-02-12 10:46:57 +00:00
|
|
|
ldx #.sizeof (check) - 1
|
2010-02-04 19:19:17 +00:00
|
|
|
@L1:
|
2010-02-12 10:46:57 +00:00
|
|
|
lda check::entry,x
|
2010-02-04 19:19:17 +00:00
|
|
|
sta backup,x
|
2010-02-12 10:46:57 +00:00
|
|
|
lda check::template,x
|
|
|
|
sta check::entry,x
|
2010-02-04 19:19:17 +00:00
|
|
|
dex
|
|
|
|
bpl @L1
|
|
|
|
rts
|
|
|
|
|
2010-02-04 20:08:50 +00:00
|
|
|
restore_copy_routine:
|
2010-02-12 10:46:57 +00:00
|
|
|
ldx #.sizeof (copy) - 1
|
2010-02-04 19:19:17 +00:00
|
|
|
restore_data:
|
|
|
|
lda backup,x
|
2010-02-12 10:46:57 +00:00
|
|
|
sta TARGETLOC,x
|
2010-02-04 19:19:17 +00:00
|
|
|
dex
|
|
|
|
bpl restore_data
|
|
|
|
rts
|
|
|
|
|
|
|
|
; Helper routine to correct for the bank and page
|
|
|
|
adjust_page_and_bank:
|
|
|
|
sta tmp4
|
|
|
|
lda #$0C
|
|
|
|
sta tmp3
|
|
|
|
lda tmp4
|
|
|
|
and #$c0
|
|
|
|
lsr
|
|
|
|
lsr
|
|
|
|
ora tmp3
|
|
|
|
sta tmp3
|
|
|
|
txa
|
|
|
|
asl
|
|
|
|
asl
|
|
|
|
asl
|
|
|
|
asl
|
|
|
|
asl
|
|
|
|
asl
|
|
|
|
ora tmp3
|
|
|
|
tax
|
|
|
|
lda tmp4
|
|
|
|
and #$3f
|
|
|
|
rts
|