2 ; Extended memory driver for the C64 D2TV (the second or PAL version).
3 ; Driver works without problems when statically linked.
5 ; Ullrich von Bassewitz, 2005-11-27
9 .include "zeropage.inc"
11 .include "em-kernel.inc"
12 .include "em-error.inc"
19 ; ------------------------------------------------------------------------
20 ; Header. Includes jump table
26 .byte $65, $6d, $64 ; "emd"
27 .byte EMD_API_VERSION ; EM API version number
44 ; ------------------------------------------------------------------------
47 OP_COPYFROM = %00001101
50 START_BANK = 2 ; Start at $20000
51 PAGES = (2048 - 128) * 4
54 ; ------------------------------------------------------------------------
58 window: .res 256 ; Memory "window"
62 ; The MAP and COMMIT entries will actually call COPYFROM/COPYTO with
63 ; a pointer to the following data structure:
65 dma_params: .word window ; Host address
66 .byte 0 ; Offset in page
67 curpage: .word $0000 ; Page
68 .word .sizeof (window); # bytes to move, lo, hi
72 ; ------------------------------------------------------------------------
73 ; INSTALL routine. Is called after the driver is loaded into memory. If
74 ; possible, check if the hardware is present and determine the amount of
76 ; Must return an EM_ERR_xx code in a/x.
95 lda #<EM_ERR_NO_DEVICE
96 ldx #>EM_ERR_NO_DEVICE
101 stx curpage+1 ; Invalidate curpage
103 txa ; A/X = EM_ERR_OK
105 ; rts ; Run into UNINSTALL instead
107 ; ------------------------------------------------------------------------
108 ; UNINSTALL routine. Is called before the driver is removed from memory.
109 ; Can do cleanup or whatever. Must not return anything.
115 ; ------------------------------------------------------------------------
116 ; PAGECOUNT: Return the total number of available pages in a/x.
124 ; ------------------------------------------------------------------------
125 ; MAP: Map the page in a/x into memory and return a pointer to the page in
126 ; a/x. The contents of the currently mapped page (if any) may be discarded
131 stx curpage+1 ; Remember the new page
135 jsr COPYFROM ; Copy data into the window
138 ldx #>window ; Return the window address
141 ; ------------------------------------------------------------------------
142 ; USE: Tell the driver that the window is now associated with a given page.
145 stx curpage+1 ; Remember the page
147 ldx #>window ; Return the window
150 ; ------------------------------------------------------------------------
151 ; COMMIT: Commit changes in the memory window to extended storage.
153 COMMIT: lda curpage+1 ; Do we have a page mapped?
154 bmi done ; Jump if no page mapped
161 ; ------------------------------------------------------------------------
162 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
163 ; describing the request is passed in a/x.
164 ; The function must not return anything.
168 stx ptr1+1 ; Save the pointer
170 ldx #OP_COPYTO ; Load the command
173 ; ------------------------------------------------------------------------
174 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
175 ; describing the request is passed in a/x.
176 ; The function must not return anything.
181 stx ptr1+1 ; Save the pointer
185 ; DTV DMA transfer routine. Expects the command in X.
186 ; NOTE: We're using knowledge about field order in the EM_COPY struct here!
189 jsr WAIT ; Wait until DMA is finished
196 ; Setup the target address and the source and target steps. Y contains zero,
197 ; which is EM_COPY::BUF.
199 sty $d307 ; Source step high = 0
200 sty $d309 ; Dest step high = 0
202 sta $d303 ; Dest address low
204 sty $d306 ; Source step low = 1
205 sty $d308 ; Dest step low = 1
208 lda #$40 ; Dest is always RAM, start at $00000
211 ; Setup the source address. Incrementing Y will make it point to EM_COPY::OFFS.
212 ; We will allow page numbers higher than PAGES and map them to ROM. This will
213 ; allow reading the ROM by specifying a page starting with PAGES.
223 adc #START_BANK ; Carry clear here from WAIT
225 cmp #>PAGES+START_BANK ; Valid range?
227 ora #$40 ; Address RAM
243 ; Wait until DMA is done