2 ; Extended memory driver for the VDC RAM available on all C128 machines
3 ; (based on code by Ullrich von Bassewitz)
4 ; Maciej 'YTM/Elysium' Witkowiak <ytm@elysium.pl>
8 ; Marco van den Heuvel, 2010-01-22
11 .include "zeropage.inc"
13 .include "em-kernel.inc"
14 .include "em-error.inc"
20 ; ------------------------------------------------------------------------
21 ; Header. Includes jump table
27 .byte $65, $6d, $64 ; "emd"
28 .byte EMD_API_VERSION ; EM API version number
41 ; ------------------------------------------------------------------------
44 VDC_ADDR_REG = $D600 ; VDC address
45 VDC_DATA_REG = $D601 ; VDC data
47 VDC_DATA_HI = 18 ; used registers
52 ; ------------------------------------------------------------------------
57 pagecount: .word 64 ; $0000-$3fff as 16k default
58 curpage: .word $ffff ; currently mapped-in page (invalid)
62 window: .res 256 ; memory window
66 ; ------------------------------------------------------------------------
67 ; INSTALL routine. Is called after the driver is loaded into memory. If
68 ; possible, check if the hardware is present and determine the amount of
70 ; Must return an EM_ERR_xx code in a/x.
76 lda #VDC_CSET ; determine size of RAM...
85 lda #<EM_ERR_NO_DEVICE
86 ldx #>EM_ERR_NO_DEVICE
90 ldx #VDC_CSET ; determine size of RAM...
94 jsr vdcputreg ; turn on 64k
96 jsr settestadr1 ; save original value of test byte
100 lda #$55 ; write $55 here
102 jsr test64k ; read it here and there
103 lda #$aa ; write $aa here
105 jsr test64k ; read it here and there
109 jsr vdcputbyte ; restore original value of test byte
111 lda ptr1 ; do bytes match?
120 jsr vdcputreg ; restore 16/64k flag
121 jmp @endok ; and leave default values for 16k
140 jsr vdcputbyte ; write $55
142 jsr vdcgetbyte ; read here
145 jsr vdcgetbyte ; and there
154 ldy #$02 ; test page 2 (here)
157 ldy #$42 ; or page 64+2 (there)
161 ; ------------------------------------------------------------------------
162 ; UNINSTALL routine. Is called before the driver is removed from memory.
163 ; Can do cleanup or whatever. Must not return anything.
167 ;on C128 restore font and clear the screen?
170 ; ------------------------------------------------------------------------
171 ; PAGECOUNT: Return the total number of available pages in a/x.
179 ; ------------------------------------------------------------------------
180 ; MAP: Map the page in a/x into memory and return a pointer to the page in
181 ; a/x. The contents of the currently mapped page (if any) may be discarded
202 ; copy a single page from (ptr1):VDCRAM to (ptr2):RAM
207 jsr vdcsetsrcaddr ; set source address in VDC
211 @L0: bit VDC_ADDR_REG
213 lda VDC_DATA_REG ; get 2 bytes at a time to speed-up
214 sta (ptr2),y ; (in fact up to 8 bytes could be fetched with special VDC config)
222 ; ------------------------------------------------------------------------
223 ; USE: Tell the driver that the window is now associated with a given page.
226 stx curpage+1 ; Remember the page
228 ldx #>window ; Return the window
231 ; ------------------------------------------------------------------------
232 ; COMMIT: Commit changes in the memory window to extended storage.
235 lda curpage ; jump if no page mapped
247 ; fall through to transferout
249 ; copy a single page from (ptr2):RAM to (ptr1):VDCRAM
254 jsr vdcsetsrcaddr ; set source address in VDC
258 @L0: bit VDC_ADDR_REG
260 lda (ptr2),y ; speedup does not work for writing
266 ; ------------------------------------------------------------------------
267 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
268 ; describing the request is passed in a/x.
269 ; The function must not return anything.
274 beq @L2 ; Skip if no full pages
284 ; Copy the remainder of the page
286 @L2: ldy #EM_COPY::COUNT
287 lda (ptr3),y ; Get bytes in last page
291 ; Transfer the bytes in the last page
302 ; ------------------------------------------------------------------------
303 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
304 ; describing the request is passed in a/x.
305 ; The function must not return anything.
310 beq @L2 ; Skip if no full pages
320 ; Copy the remainder of the page
322 @L2: ldy #EM_COPY::COUNT
323 lda (ptr3),y ; Get bytes in last page
327 ; Transfer the bytes in the last page
338 ;-------------------------------------------------------------------------
339 ; Helper functions to handle VDC ram
345 @L0: bit VDC_ADDR_REG
358 @L0: bit VDC_ADDR_REG
367 @L0: bit VDC_ADDR_REG
372 ; ------------------------------------------------------------------------
373 ; Helper function for COPYFROM and COPYTO: Store the pointer to the request
374 ; structure and prepare data for the copy
379 stx ptr3+1 ; Save the passed em_copy pointer
395 ldy #EM_COPY::COUNT+1
396 lda (ptr3),y ; Get number of pages