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
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
218 ldx #>window ; Return the window address
222 ; ------------------------------------------------------------------------
223 ; USE: Tell the driver that the window is now associated with a given page.
225 USE: sta curpage ; Remember the page
228 ldx #>window ; Return the window
231 ; ------------------------------------------------------------------------
232 ; COMMIT: Commit changes in the memory window to extended storage.
236 lda curpage ; Get the current page
239 jsr adjust_page_and_bank
246 jsr backup_and_setup_copy_routine
259 ; Return the memory window
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
332 ; ------------------------------------------------------------------------
333 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
334 ; describing the request is passed in a/x.
335 ; The function must not return anything.
341 jsr backup_and_setup_copy_routine
345 ; - ptr1 contains the struct pointer
346 ; - ptr2 contains the linear memory buffer
347 ; - ptr3 contains -(count-1)
348 ; - ptr4 contains the page memory buffer plus offset
349 ; - tmp1 contains zero (to be used for linear memory buffer offset)
350 ; - tmp2 contains the bank value
371 ; Bump count and repeat
398 ; ------------------------------------------------------------------------
399 ; Helper function for COPYFROM and COPYTO: Store the pointer to the request
400 ; structure and prepare data for the copy
404 stx ptr1+1 ; Save passed pointer
406 ; Get the page number from the struct and adjust it so that it may be used
407 ; with the hardware. That is: ptr4 has the page address and page offset
408 ; tmp2 will hold the bank value
415 jsr adjust_page_and_bank
421 ; Get the buffer pointer into ptr2
430 ; Get the count, calculate -(count-1) and store it into ptr3
441 ; Get the page offset into ptr4 and clear tmp1
453 ; Helper routines for copying to and from the +256k ram
455 backup_and_setup_copy_routine:
456 ldx #.sizeof (c256kcopycode) - 1
466 backup_and_setup_check_routine:
467 ldx #.sizeof (c256kcheckcode) - 1
484 ; Helper routine to correct for the bank and page
485 adjust_page_and_bank: