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
40 ; ------------------------------------------------------------------------
44 PAGES = ($C000 - BASE) / 256
45 TARGETLOC = $200 ; Target location for copy/check code
48 ; ------------------------------------------------------------------------
53 .org ::TARGETLOC ; Assemble for target location
88 .org ::TARGETLOC ; Assemble for target location
95 address := *+1 ; Patched at runtime
109 address := *+1 ; Patched at runtime
122 curpage: .res 1 ; Current page number
123 window: .res 256 ; Memory "window"
125 ; Since the functions above are copied to $200, the current contents of this
126 ; memory area must be saved into backup storage. Allocate enough space.
127 backup: .res .max (.sizeof (copy), .sizeof (check))
133 ; ------------------------------------------------------------------------
134 ; INSTALL routine. Is called after the driver is loaded into memory. If
135 ; possible, check if the hardware is present and determine the amount of
137 ; Must return an EM_ERR_xx code in a/x.
142 jsr backup_and_setup_check_routine
145 ldx #.sizeof (check) - 1
149 lda #<EM_ERR_NO_DEVICE
150 ldx #>EM_ERR_NO_DEVICE
156 ; rts ; Run into UNINSTALL instead
158 ; ------------------------------------------------------------------------
159 ; UNINSTALL routine. Is called before the driver is removed from memory.
160 ; Can do cleanup or whatever. Must not return anything.
167 ; ------------------------------------------------------------------------
168 ; PAGECOUNT: Return the total number of available pages in a/x.
176 ; ------------------------------------------------------------------------
177 ; MAP: Map the page in a/x into memory and return a pointer to the page in
178 ; a/x. The contents of the currently mapped page (if any) may be discarded
184 sta curpage ; Remember the new page
190 jsr backup_and_setup_copy_routine
192 stx copy::fetch::address
201 ; Return the memory window
203 jsr restore_copy_routine
205 ldx #>window ; Return the window address
209 ; ------------------------------------------------------------------------
210 ; USE: Tell the driver that the window is now associated with a given page.
212 USE: sta curpage ; Remember the page
214 ldx #>window ; Return the window
217 ; ------------------------------------------------------------------------
218 ; COMMIT: Commit changes in the memory window to extended storage.
222 lda curpage ; Get the current page
228 jsr backup_and_setup_copy_routine
230 stx copy::stash::address
239 ; Return the memory window
241 jsr restore_copy_routine
245 ; ------------------------------------------------------------------------
246 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
247 ; describing the request is passed in a/x.
248 ; The function must not return anything.
257 jsr backup_and_setup_copy_routine
265 ; - ptr1 contains the struct pointer
266 ; - ptr2 contains the linear memory buffer
267 ; - ptr3 contains -(count-1)
268 ; - ptr4 contains the page memory buffer plus offset
269 ; - tmp1 contains zero (to be used for linear memory buffer offset)
272 sta copy::fetch::address
288 ; Bump count and repeat
295 jsr restore_copy_routine
305 ; ------------------------------------------------------------------------
306 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
307 ; describing the request is passed in a/x.
308 ; The function must not return anything.
316 jsr backup_and_setup_copy_routine
324 ; - ptr1 contains the struct pointer
325 ; - ptr2 contains the linear memory buffer
326 ; - ptr3 contains -(count-1)
327 ; - ptr4 contains the page memory buffer plus offset
328 ; - tmp1 contains zero (to be used for linear memory buffer offset)
331 sta copy::stash::address
347 ; Bump count and repeat
354 jsr restore_copy_routine
364 ; ------------------------------------------------------------------------
365 ; Helper function for COPYFROM and COPYTO: Store the pointer to the request
366 ; structure and prepare data for the copy
370 stx ptr1+1 ; Save passed pointer
372 ; Get the page number from the struct and adjust it so that it may be used
373 ; with the hardware. That is: ptr4 has the page address and page offset
374 ; tmp2 will hold the bank value
382 ; Get the buffer pointer into ptr2
391 ; Get the count, calculate -(count-1) and store it into ptr3
402 ; Get the page offset into ptr4 and clear tmp1
414 ; Helper routines for copying to and from the +256k ram
416 backup_and_setup_copy_routine:
417 ldx #.sizeof (copy) - 1
427 backup_and_setup_check_routine:
428 ldx #.sizeof (check) - 1
432 lda check::template,x
438 restore_copy_routine:
439 ldx #.sizeof (copy) - 1