2 ; Extended memory driver for the C16 hidden RAM. Driver works without
3 ; problems when statically linked.
5 ; Ullrich von Bassewitz, 2003-12-15
8 .include "zeropage.inc"
10 .include "em-kernel.inc"
11 .include "em-error.inc"
18 ; ------------------------------------------------------------------------
19 ; Header. Includes jump table
21 module_header _c16_ram_emd
25 .byte $65, $6d, $64 ; "emd"
26 .byte EMD_API_VERSION ; EM API version number
43 ; ------------------------------------------------------------------------
48 ; ------------------------------------------------------------------------
52 pages: .res 1 ; Number of pages
53 curpage: .res 1 ; Current page number
54 window: .res 256 ; Memory "window"
58 ; ------------------------------------------------------------------------
59 ; INSTALL routine. Is called after the driver is loaded into memory. If
60 ; possible, check if the hardware is present and determine the amount of
62 ; Must return an EM_ERR_xx code in a/x.
67 ; Determine how much memory is available. We will use all memory above
71 jsr $FF99 ; MEMTOP: Get top memory into Y/X
73 sub #>BASE ; Low 32 K are used
75 beq nomem ; Offering zero pages is a bad idea
79 stx curpage ; Invalidate the current page
81 txa ; A = X = EM_ERR_OK
84 nomem: ldx #>EM_ERR_NO_DEVICE
85 lda #<EM_ERR_NO_DEVICE
86 ; rts ; Run into UNINSTALL instead
88 ; ------------------------------------------------------------------------
89 ; UNINSTALL routine. Is called before the driver is removed from memory.
90 ; Can do cleanup or whatever. Must not return anything.
97 ; ------------------------------------------------------------------------
98 ; PAGECOUNT: Return the total number of available pages in a/x.
103 ldx #$00 ; 128 pages max
106 ; ------------------------------------------------------------------------
107 ; MAP: Map the page in a/x into memory and return a pointer to the page in
108 ; a/x. The contents of the currently mapped page (if any) may be discarded
112 MAP: sta curpage ; Remember the new page
126 jsr transfer ; Transfer one page
128 ; Return the memory window
131 ldx #>window ; Return the window address
134 ; ------------------------------------------------------------------------
135 ; USE: Tell the driver that the window is now associated with a given page.
137 USE: sta curpage ; Remember the page
139 ldx #>window ; Return the window
142 ; ------------------------------------------------------------------------
143 ; COMMIT: Commit changes in the memory window to extended storage.
145 COMMIT: lda curpage ; Get the current page
146 bmi done ; Jump if no page mapped
158 ; Transfer one page. Y must be zero on entry. Because we bank out the
159 ; kernal, we will run the routine with interrupts disabled but leave
160 ; short breath times. Unroll the following loop to make it somewhat faster.
181 ; ------------------------------------------------------------------------
182 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
183 ; describing the request is passed in a/x.
184 ; The function must not return anything.
189 stx ptr3+1 ; Save the passed em_copy pointer
206 common: ldy #EM_COPY::COUNT+1
207 lda (ptr3),y ; Get number of pages
208 beq @L2 ; Skip if no full pages
211 ; Copy full pages allowing interrupts after each page copied
220 ; Copy the remainder of the page
222 @L2: ldy #EM_COPY::COUNT
223 lda (ptr3),y ; Get bytes in last page
228 sta ENABLE_RAM ; Bank out the ROM
230 ; Transfer the bytes in the last page
239 ; Restore the old memory configuration, allow interrupts
248 ; ------------------------------------------------------------------------
249 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
250 ; describing the request is passed in a/x.
251 ; The function must not return anything.
255 stx ptr3+1 ; Save the passed em_copy pointer