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"
15 ; ------------------------------------------------------------------------
16 ; Header. Includes jump table
22 .byte $65, $6d, $64 ; "emd"
23 .byte EMD_API_VERSION ; EM API version number
36 ; ------------------------------------------------------------------------
40 PAGES = ($C000 - BASE) / 256
41 TARGETLOC = $200 ; Target location for copy/check code
44 ; ------------------------------------------------------------------------
49 .org ::TARGETLOC ; Assemble for target location
84 .org ::TARGETLOC ; Assemble for target location
91 address := *+1 ; Patched at runtime
105 address := *+1 ; Patched at runtime
118 curpage: .res 1 ; Current page number
119 window: .res 256 ; Memory "window"
121 ; Since the functions above are copied to $200, the current contents of this
122 ; memory area must be saved into backup storage. Allocate enough space.
123 backup: .res .max (.sizeof (copy), .sizeof (check))
129 ; ------------------------------------------------------------------------
130 ; INSTALL routine. Is called after the driver is loaded into memory. If
131 ; possible, check if the hardware is present and determine the amount of
133 ; Must return an EM_ERR_xx code in a/x.
138 jsr backup_and_setup_check_routine
141 ldx #.sizeof (check) - 1
145 lda #<EM_ERR_NO_DEVICE
146 ldx #>EM_ERR_NO_DEVICE
152 ; rts ; Run into UNINSTALL instead
154 ; ------------------------------------------------------------------------
155 ; UNINSTALL routine. Is called before the driver is removed from memory.
156 ; Can do cleanup or whatever. Must not return anything.
163 ; ------------------------------------------------------------------------
164 ; PAGECOUNT: Return the total number of available pages in a/x.
172 ; ------------------------------------------------------------------------
173 ; MAP: Map the page in a/x into memory and return a pointer to the page in
174 ; a/x. The contents of the currently mapped page (if any) may be discarded
180 sta curpage ; Remember the new page
186 jsr backup_and_setup_copy_routine
188 stx copy::fetch::address
197 ; Return the memory window
199 jsr restore_copy_routine
201 ldx #>window ; Return the window address
205 ; ------------------------------------------------------------------------
206 ; USE: Tell the driver that the window is now associated with a given page.
208 USE: sta curpage ; Remember the page
210 ldx #>window ; Return the window
213 ; ------------------------------------------------------------------------
214 ; COMMIT: Commit changes in the memory window to extended storage.
218 lda curpage ; Get the current page
224 jsr backup_and_setup_copy_routine
226 stx copy::stash::address
235 ; Return the memory window
237 jsr restore_copy_routine
241 ; ------------------------------------------------------------------------
242 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
243 ; describing the request is passed in a/x.
244 ; The function must not return anything.
253 jsr backup_and_setup_copy_routine
261 ; - ptr1 contains the struct pointer
262 ; - ptr2 contains the linear memory buffer
263 ; - ptr3 contains -(count-1)
264 ; - ptr4 contains the page memory buffer plus offset
265 ; - tmp1 contains zero (to be used for linear memory buffer offset)
268 sta copy::fetch::address
284 ; Bump count and repeat
291 jsr restore_copy_routine
301 ; ------------------------------------------------------------------------
302 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
303 ; describing the request is passed in a/x.
304 ; The function must not return anything.
312 jsr backup_and_setup_copy_routine
320 ; - ptr1 contains the struct pointer
321 ; - ptr2 contains the linear memory buffer
322 ; - ptr3 contains -(count-1)
323 ; - ptr4 contains the page memory buffer plus offset
324 ; - tmp1 contains zero (to be used for linear memory buffer offset)
327 sta copy::stash::address
343 ; Bump count and repeat
350 jsr restore_copy_routine
360 ; ------------------------------------------------------------------------
361 ; Helper function for COPYFROM and COPYTO: Store the pointer to the request
362 ; structure and prepare data for the copy
366 stx ptr1+1 ; Save passed pointer
368 ; Get the page number from the struct and adjust it so that it may be used
369 ; with the hardware. That is: ptr4 has the page address and page offset
370 ; tmp2 will hold the bank value
378 ; Get the buffer pointer into ptr2
387 ; Get the count, calculate -(count-1) and store it into ptr3
398 ; Get the page offset into ptr4 and clear tmp1
410 ; Helper routines for copying to and from the +256k ram
412 backup_and_setup_copy_routine:
413 ldx #.sizeof (copy) - 1
423 backup_and_setup_check_routine:
424 ldx #.sizeof (check) - 1
428 lda check::template,x
434 restore_copy_routine:
435 ldx #.sizeof (copy) - 1