2 ; Extended memory driver for the C256K memory expansion
3 ; Marco van den Heuvel, 2010-01-27
6 .include "zeropage.inc"
8 .include "em-kernel.inc"
9 .include "em-error.inc"
15 ; ------------------------------------------------------------------------
16 ; Header. Includes jump table
22 .byte $65, $6d, $64 ; "emd"
23 .byte EMD_API_VERSION ; EM API version number
36 ; ------------------------------------------------------------------------
46 ; ------------------------------------------------------------------------
52 ; This function is used to copy code from and to the extended memory
54 .org ::TRANSFERC256K ; Assemble for target location
56 ::STASHC256K := * ; Location and opcode is patched at runtime
65 ; This function is used to check for the existence of the extended memory
68 ldy #$00 ; Assume hardware not present
78 bne done ; Jump if not found
81 beq done ; Jump if not found
92 ; Since the functions above are copied to $200, the current contents of this
93 ; memory area must be saved into backup storage. Calculate the amount of
95 .if .sizeof (c256kcopycode) > .sizeof (c256kcheckcode)
96 backupspace = .sizeof (c256kcopycode)
98 backupspace = .sizeof (c256kcheckcode)
104 curpage: .res 2 ; Current page number
105 curbank: .res 1 ; Current bank
106 backup: .res backupspace ; Backup area of data in the location
107 ; where the copy and check routines will be
108 window: .res 256 ; Memory "window"
112 ; ------------------------------------------------------------------------
113 ; INSTALL routine. Is called after the driver is loaded into memory. If
114 ; possible, check if the hardware is present and determine the amount of
116 ; Must return an EM_ERR_xx code in a/x.
120 lda pia+1 ; Select Peripheral Registers
129 lda #$DC ; Set the default memory bank data
134 txa ; Select Data Direction Registers
141 lda #$FF ; Set the ports to output
147 ora #$30 ; Set CA1 and
148 sta pia+1 ; select Peripheral Registers
151 jsr backup_and_setup_check_routine
154 ldx #.sizeof (c256kcheckcode) - 1
158 lda #<EM_ERR_NO_DEVICE
159 ldx #>EM_ERR_NO_DEVICE
165 ; rts ; Run into UNINSTALL instead
167 ; ------------------------------------------------------------------------
168 ; UNINSTALL routine. Is called before the driver is removed from memory.
169 ; Can do cleanup or whatever. Must not return anything.
176 ; ------------------------------------------------------------------------
177 ; PAGECOUNT: Return the total number of available pages in a/x.
185 ; ------------------------------------------------------------------------
186 ; MAP: Map the page in a/x into memory and return a pointer to the page in
187 ; a/x. The contents of the currently mapped page (if any) may be discarded
193 sta curpage ; Remember the new page
195 jsr adjust_page_and_bank
202 jsr backup_and_setup_copy_routine
213 ; Return the memory window
215 jsr restore_copy_routine
217 ldx #>window ; Return the window address
221 ; ------------------------------------------------------------------------
222 ; USE: Tell the driver that the window is now associated with a given page.
224 USE: sta curpage ; Remember the page
227 ldx #>window ; Return the window
230 ; ------------------------------------------------------------------------
231 ; COMMIT: Commit changes in the memory window to extended storage.
235 lda curpage ; Get the current page
238 jsr adjust_page_and_bank
245 jsr backup_and_setup_copy_routine
258 ; Return the memory window
260 jsr restore_copy_routine
265 ; ------------------------------------------------------------------------
266 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
267 ; describing the request is passed in a/x.
268 ; The function must not return anything.
275 jsr backup_and_setup_copy_routine
279 ; - ptr1 contains the struct pointer
280 ; - ptr2 contains the linear memory buffer
281 ; - ptr3 contains -(count-1)
282 ; - ptr4 contains the page memory buffer plus offset
283 ; - tmp1 contains zero (to be used for linear memory buffer offset)
284 ; - tmp2 contains the bank value
303 ; Bump count and repeat
310 jsr restore_copy_routine
329 ; ------------------------------------------------------------------------
330 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
331 ; describing the request is passed in a/x.
332 ; The function must not return anything.
338 jsr backup_and_setup_copy_routine
342 ; - ptr1 contains the struct pointer
343 ; - ptr2 contains the linear memory buffer
344 ; - ptr3 contains -(count-1)
345 ; - ptr4 contains the page memory buffer plus offset
346 ; - tmp1 contains zero (to be used for linear memory buffer offset)
347 ; - tmp2 contains the bank value
368 ; Bump count and repeat
375 jsr restore_copy_routine
394 ; ------------------------------------------------------------------------
395 ; Helper function for COPYFROM and COPYTO: Store the pointer to the request
396 ; structure and prepare data for the copy
400 stx ptr1+1 ; Save passed pointer
402 ; Get the page number from the struct and adjust it so that it may be used
403 ; with the hardware. That is: ptr4 has the page address and page offset
404 ; tmp2 will hold the bank value
411 jsr adjust_page_and_bank
417 ; Get the buffer pointer into ptr2
426 ; Get the count, calculate -(count-1) and store it into ptr3
437 ; Get the page offset into ptr4 and clear tmp1
449 ; Helper routines for copying to and from the +256k ram
451 backup_and_setup_copy_routine:
452 ldx #.sizeof (c256kcopycode) - 1
462 backup_and_setup_check_routine:
463 ldx #.sizeof (c256kcheckcode) - 1
473 restore_copy_routine:
474 ldx #.sizeof (c256kcopycode) - 1
482 ; Helper routine to correct for the bank and page
483 adjust_page_and_bank: