2 ; Extended memory driver for the C128 RAM in banks #1, #2 and #3. Driver works without
3 ; problems when statically linked.
5 ; Ullrich von Bassewitz, 2002-12-04
7 ; Updated to use banks 2 and 3 as well by
8 ; Marco van den Heuvel, 2010-01-21
11 .include "zeropage.inc"
13 .include "em-kernel.inc"
14 .include "em-error.inc"
21 ; ------------------------------------------------------------------------
22 ; Header. Includes jump table
28 .byte $65, $6d, $64 ; "emd"
29 .byte EMD_API_VERSION ; EM API version number
42 ; ------------------------------------------------------------------------
47 ; ------------------------------------------------------------------------
51 curpage: .res 2 ; Current page number
52 curbank: .res 1 ; Current bank number
53 copybank: .res 2 ; temp bank number
55 window: .res 256 ; Memory "window"
57 pagecount: .res 2 ; Number of available pages
61 ; ------------------------------------------------------------------------
62 ; INSTALL routine. Is called after the driver is loaded into memory. If
63 ; possible, check if the hardware is present and determine the amount of
65 ; Must return an EM_ERR_xx code in a/x.
105 stx curpage+1 ; Invalidate the current page
107 txa ; A = X = EM_ERR_OK
108 ; rts ; Run into UNINSTALL instead
110 ; ------------------------------------------------------------------------
111 ; UNINSTALL routine. Is called before the driver is removed from memory.
112 ; Can do cleanup or whatever. Must not return anything.
119 ; ------------------------------------------------------------------------
120 ; PAGECOUNT: Return the total number of available pages in a/x.
128 ; ------------------------------------------------------------------------
129 ; MAP: Map the page in a/x into memory and return a pointer to the page in
130 ; a/x. The contents of the currently mapped page (if any) may be discarded
136 stx curpage+1 ; Remember the new page
138 jsr calculate_bank_and_correct_page
158 ; Return the memory window
161 ldx #>window ; Return the window address
165 ; ------------------------------------------------------------------------
166 ; USE: Tell the driver that the window is now associated with a given page.
169 stx curpage+1 ; Remember the page
171 ldx #>window ; Return the window
174 ; ------------------------------------------------------------------------
175 ; COMMIT: Commit changes in the memory window to extended storage.
178 lda curpage ; Get the current page
180 bmi done ; Jump if no page mapped
182 jsr calculate_bank_and_correct_page
194 ; Transfer one page. Y must be zero on entry
208 ; ------------------------------------------------------------------------
209 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
210 ; describing the request is passed in a/x.
211 ; The function must not return anything.
220 ; - ptr1 contains the struct pointer
221 ; - ptr2 contains the linear memory buffer
222 ; - ptr3 contains -(count-1)
223 ; - ptr4 contains the page buffer and offset
224 ; - tmp1 contains the bank
225 ; - tmp2 contains zero (used for linear memory buffer offset)
243 ; Bump count and repeat
265 ; ------------------------------------------------------------------------
266 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
267 ; describing the request is passed in a/x.
268 ; The function must not return anything.
277 ; - ptr1 contains the struct pointer
278 ; - ptr2 contains the linear memory buffer
279 ; - ptr3 contains -(count-1)
280 ; - ptr4 contains the page buffer and offset
281 ; - tmp1 contains the bank
282 ; - tmp2 contains zero (used for linear memory buffer offset)
301 ; Bump count and repeat
322 ; ------------------------------------------------------------------------
323 ; Helper function to calculate the correct bank and page
324 ; when addressing bank 2 or 3
326 calculate_bank_and_correct_page:
328 beq @calculate_bank_3_with_2
330 beq @calculate_bank_2_or_3_with_1
333 bcs @calculate_bank_2_with_0
338 @calculate_bank_3_with_2:
342 @calculate_bank_3_with_1:
346 @calculate_bank_2_or_3_with_1:
349 bcs @calculate_bank_3_with_1
353 @calculate_bank_2_with_0:
357 ; ------------------------------------------------------------------------
358 ; Helper function to get the correct mmu value in x
374 ; ------------------------------------------------------------------------
375 ; Helper function for COPYFROM and COPYTO: Store the pointer to the request
376 ; structure and prepare data for the copy
379 stx ptr1+1 ; Save passed pointer
381 ; Get the page number from the struct and adjust it so that it may be used
382 ; with the hardware. That is: page pointer in ptr4 and bank in tmp1
390 jsr calculate_bank_and_correct_page
396 ; Get the buffer pointer into ptr2
405 ; Get the count, calculate -(count-1) and store it into ptr3
416 ; Get the page offset into the low byte of ptr4 clear tmp2