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
40 ; ------------------------------------------------------------------------
45 TARGETLOC = $200 ; Target location for copy/check code
48 ; ------------------------------------------------------------------------
54 ; This function is used to copy code from and to the extended memory
57 .org ::TARGETLOC ; Assemble for target location
60 stashop = $91 ; 'sta' opcode
61 operation := * ; Location and opcode is patched at runtime
70 ; This function is used to check for the existence of the extended memory
75 ldy #$00 ; Assume hardware not present
85 bne done ; Jump if not found
88 beq done ; Jump if not found
103 curpage: .res 2 ; Current page number
104 curbank: .res 1 ; Current bank
105 window: .res 256 ; Memory "window"
107 ; Since the functions above are copied to $200, the current contents of this
108 ; memory area must be saved into backup storage. Allocate enough space.
109 backup: .res .max (.sizeof (copy), .sizeof (check))
114 ; ------------------------------------------------------------------------
115 ; INSTALL routine. Is called after the driver is loaded into memory. If
116 ; possible, check if the hardware is present and determine the amount of
118 ; Must return an EM_ERR_xx code in a/x.
122 lda PIA+1 ; Select Peripheral Registers
131 lda #$DC ; Set the default memory bank data
136 txa ; Select Data Direction Registers
143 lda #$FF ; Set the ports to output
149 ora #$30 ; Set CA1 and
150 sta PIA+1 ; select Peripheral Registers
153 jsr backup_and_setup_check_routine
156 ldx #.sizeof (check) - 1
160 lda #<EM_ERR_NO_DEVICE
161 ldx #>EM_ERR_NO_DEVICE
167 ; rts ; Run into UNINSTALL instead
169 ; ------------------------------------------------------------------------
170 ; UNINSTALL routine. Is called before the driver is removed from memory.
171 ; Can do cleanup or whatever. Must not return anything.
178 ; ------------------------------------------------------------------------
179 ; PAGECOUNT: Return the total number of available pages in a/x.
187 ; ------------------------------------------------------------------------
188 ; MAP: Map the page in a/x into memory and return a pointer to the page in
189 ; a/x. The contents of the currently mapped page (if any) may be discarded
195 sta curpage ; Remember the new page
197 jsr adjust_page_and_bank
204 jsr backup_and_setup_copy_routine
215 ; Return the memory window
217 jsr restore_copy_routine
219 ldx #>window ; Return the window address
223 ; ------------------------------------------------------------------------
224 ; USE: Tell the driver that the window is now associated with a given page.
226 USE: sta curpage ; Remember the page
229 ldx #>window ; Return the window
232 ; ------------------------------------------------------------------------
233 ; COMMIT: Commit changes in the memory window to extended storage.
237 lda curpage ; Get the current page
240 jsr adjust_page_and_bank
247 jsr backup_and_setup_copy_routine
260 ; Return the memory window
262 jsr restore_copy_routine
267 ; ------------------------------------------------------------------------
268 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
269 ; describing the request is passed in a/x.
270 ; The function must not return anything.
277 jsr backup_and_setup_copy_routine
281 ; - ptr1 contains the struct pointer
282 ; - ptr2 contains the linear memory buffer
283 ; - ptr3 contains -(count-1)
284 ; - ptr4 contains the page memory buffer plus offset
285 ; - tmp1 contains zero (to be used for linear memory buffer offset)
286 ; - tmp2 contains the bank value
305 ; Bump count and repeat
312 jsr restore_copy_routine
331 ; ------------------------------------------------------------------------
332 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
333 ; describing the request is passed in a/x.
334 ; The function must not return anything.
340 jsr backup_and_setup_copy_routine
344 ; - ptr1 contains the struct pointer
345 ; - ptr2 contains the linear memory buffer
346 ; - ptr3 contains -(count-1)
347 ; - ptr4 contains the page memory buffer plus offset
348 ; - tmp1 contains zero (to be used for linear memory buffer offset)
349 ; - tmp2 contains the bank value
370 ; Bump count and repeat
377 jsr restore_copy_routine
396 ; ------------------------------------------------------------------------
397 ; Helper function for COPYFROM and COPYTO: Store the pointer to the request
398 ; structure and prepare data for the copy
402 stx ptr1+1 ; Save passed pointer
404 ; Get the page number from the struct and adjust it so that it may be used
405 ; with the hardware. That is: ptr4 has the page address and page offset
406 ; tmp2 will hold the bank value
413 jsr adjust_page_and_bank
419 ; Get the buffer pointer into ptr2
428 ; Get the count, calculate -(count-1) and store it into ptr3
439 ; Get the page offset into ptr4 and clear tmp1
451 ; Helper routines for copying to and from the +256k ram
453 backup_and_setup_copy_routine:
454 ldx #.sizeof (copy) - 1
464 backup_and_setup_check_routine:
465 ldx #.sizeof (check) - 1
469 lda check::template,x
475 restore_copy_routine:
476 ldx #.sizeof (copy) - 1
484 ; Helper routine to correct for the bank and page
485 adjust_page_and_bank: