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
46 ; ------------------------------------------------------------------------
51 ; ------------------------------------------------------------------------
55 curpage: .res 2 ; Current page number
56 curbank: .res 1 ; Current bank number
57 copybank: .res 2 ; temp bank number
59 window: .res 256 ; Memory "window"
61 pagecount: .res 2 ; Number of available pages
65 ; ------------------------------------------------------------------------
66 ; INSTALL routine. Is called after the driver is loaded into memory. If
67 ; possible, check if the hardware is present and determine the amount of
69 ; Must return an EM_ERR_xx code in a/x.
109 stx curpage+1 ; Invalidate the current page
111 txa ; A = X = EM_ERR_OK
112 ; rts ; Run into UNINSTALL instead
114 ; ------------------------------------------------------------------------
115 ; UNINSTALL routine. Is called before the driver is removed from memory.
116 ; Can do cleanup or whatever. Must not return anything.
123 ; ------------------------------------------------------------------------
124 ; PAGECOUNT: Return the total number of available pages in a/x.
132 ; ------------------------------------------------------------------------
133 ; MAP: Map the page in a/x into memory and return a pointer to the page in
134 ; a/x. The contents of the currently mapped page (if any) may be discarded
140 stx curpage+1 ; Remember the new page
142 jsr calculate_bank_and_correct_page
162 ; Return the memory window
165 ldx #>window ; Return the window address
169 ; ------------------------------------------------------------------------
170 ; USE: Tell the driver that the window is now associated with a given page.
173 stx curpage+1 ; Remember the page
175 ldx #>window ; Return the window
178 ; ------------------------------------------------------------------------
179 ; COMMIT: Commit changes in the memory window to extended storage.
182 lda curpage ; Get the current page
184 bmi done ; Jump if no page mapped
186 jsr calculate_bank_and_correct_page
198 ; Transfer one page. Y must be zero on entry
212 ; ------------------------------------------------------------------------
213 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
214 ; describing the request is passed in a/x.
215 ; The function must not return anything.
224 ; - ptr1 contains the struct pointer
225 ; - ptr2 contains the linear memory buffer
226 ; - ptr3 contains -(count-1)
227 ; - ptr4 contains the page buffer and offset
228 ; - tmp1 contains the bank
229 ; - tmp2 contains zero (used for linear memory buffer offset)
247 ; Bump count and repeat
269 ; ------------------------------------------------------------------------
270 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
271 ; describing the request is passed in a/x.
272 ; The function must not return anything.
281 ; - ptr1 contains the struct pointer
282 ; - ptr2 contains the linear memory buffer
283 ; - ptr3 contains -(count-1)
284 ; - ptr4 contains the page buffer and offset
285 ; - tmp1 contains the bank
286 ; - tmp2 contains zero (used for linear memory buffer offset)
305 ; Bump count and repeat
326 ; ------------------------------------------------------------------------
327 ; Helper function to calculate the correct bank and page
328 ; when addressing bank 2 or 3
330 calculate_bank_and_correct_page:
332 beq @calculate_bank_3_with_2
334 beq @calculate_bank_2_or_3_with_1
337 bcs @calculate_bank_2_with_0
342 @calculate_bank_3_with_2:
346 @calculate_bank_3_with_1:
350 @calculate_bank_2_or_3_with_1:
353 bcs @calculate_bank_3_with_1
357 @calculate_bank_2_with_0:
361 ; ------------------------------------------------------------------------
362 ; Helper function to get the correct mmu value in x
378 ; ------------------------------------------------------------------------
379 ; Helper function for COPYFROM and COPYTO: Store the pointer to the request
380 ; structure and prepare data for the copy
383 stx ptr1+1 ; Save passed pointer
385 ; Get the page number from the struct and adjust it so that it may be used
386 ; with the hardware. That is: page pointer in ptr4 and bank in tmp1
394 jsr calculate_bank_and_correct_page
400 ; Get the buffer pointer into ptr2
409 ; Get the count, calculate -(count-1) and store it into ptr3
420 ; Get the page offset into the low byte of ptr4 clear tmp2