2 ; Extended memory driver for the Double Quick Brown Box cartridge
3 ; Marco van den Heuvel, 2010-01-27
6 .include "zeropage.inc"
8 .include "em-kernel.inc"
9 .include "em-error.inc"
16 ; ------------------------------------------------------------------------
17 ; Header. Includes jump table
19 module_header _c64_dqbb_emd
23 .byte $65, $6d, $64 ; "emd"
24 .byte EMD_API_VERSION ; EM API version number
41 ; ------------------------------------------------------------------------
45 PAGES = ($C000 - BASE) / 256
46 TARGETLOC = $200 ; Target location for copy/check code
49 ; ------------------------------------------------------------------------
54 .org ::TARGETLOC ; Assemble for target location
89 .org ::TARGETLOC ; Assemble for target location
96 address := *+1 ; Patched at runtime
110 address := *+1 ; Patched at runtime
123 curpage: .res 1 ; Current page number
124 window: .res 256 ; Memory "window"
126 ; Since the functions above are copied to $200, the current contents of this
127 ; memory area must be saved into backup storage. Allocate enough space.
128 backup: .res .max (.sizeof (copy), .sizeof (check))
134 ; ------------------------------------------------------------------------
135 ; INSTALL routine. Is called after the driver is loaded into memory. If
136 ; possible, check if the hardware is present and determine the amount of
138 ; Must return an EM_ERR_xx code in a/x.
143 jsr backup_and_setup_check_routine
146 ldx #.sizeof (check) - 1
150 lda #<EM_ERR_NO_DEVICE
151 ldx #>EM_ERR_NO_DEVICE
157 ; rts ; Run into UNINSTALL instead
159 ; ------------------------------------------------------------------------
160 ; UNINSTALL routine. Is called before the driver is removed from memory.
161 ; Can do cleanup or whatever. Must not return anything.
168 ; ------------------------------------------------------------------------
169 ; PAGECOUNT: Return the total number of available pages in a/x.
177 ; ------------------------------------------------------------------------
178 ; MAP: Map the page in a/x into memory and return a pointer to the page in
179 ; a/x. The contents of the currently mapped page (if any) may be discarded
185 sta curpage ; Remember the new page
191 jsr backup_and_setup_copy_routine
193 stx copy::fetch::address
202 ; Return the memory window
204 jsr restore_copy_routine
206 ldx #>window ; Return the window address
210 ; ------------------------------------------------------------------------
211 ; USE: Tell the driver that the window is now associated with a given page.
213 USE: sta curpage ; Remember the page
215 ldx #>window ; Return the window
218 ; ------------------------------------------------------------------------
219 ; COMMIT: Commit changes in the memory window to extended storage.
223 lda curpage ; Get the current page
229 jsr backup_and_setup_copy_routine
231 stx copy::stash::address
240 ; Return the memory window
242 jsr restore_copy_routine
246 ; ------------------------------------------------------------------------
247 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
248 ; describing the request is passed in a/x.
249 ; The function must not return anything.
258 jsr backup_and_setup_copy_routine
266 ; - ptr1 contains the struct pointer
267 ; - ptr2 contains the linear memory buffer
268 ; - ptr3 contains -(count-1)
269 ; - ptr4 contains the page memory buffer plus offset
270 ; - tmp1 contains zero (to be used for linear memory buffer offset)
273 sta copy::fetch::address
289 ; Bump count and repeat
296 jsr restore_copy_routine
306 ; ------------------------------------------------------------------------
307 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
308 ; describing the request is passed in a/x.
309 ; The function must not return anything.
317 jsr backup_and_setup_copy_routine
325 ; - ptr1 contains the struct pointer
326 ; - ptr2 contains the linear memory buffer
327 ; - ptr3 contains -(count-1)
328 ; - ptr4 contains the page memory buffer plus offset
329 ; - tmp1 contains zero (to be used for linear memory buffer offset)
332 sta copy::stash::address
348 ; Bump count and repeat
355 jsr restore_copy_routine
365 ; ------------------------------------------------------------------------
366 ; Helper function for COPYFROM and COPYTO: Store the pointer to the request
367 ; structure and prepare data for the copy
371 stx ptr1+1 ; Save passed pointer
373 ; Get the page number from the struct and adjust it so that it may be used
374 ; with the hardware. That is: ptr4 has the page address and page offset
375 ; tmp2 will hold the bank value
383 ; Get the buffer pointer into ptr2
392 ; Get the count, calculate -(count-1) and store it into ptr3
403 ; Get the page offset into ptr4 and clear tmp1
415 ; Helper routines for copying to and from the +256k ram
417 backup_and_setup_copy_routine:
418 ldx #.sizeof (copy) - 1
428 backup_and_setup_check_routine:
429 ldx #.sizeof (check) - 1
433 lda check::template,x
439 restore_copy_routine:
440 ldx #.sizeof (copy) - 1