]> git.sur5r.net Git - cc65/blob - libsrc/geos-cbm/emd/geos-vdc.s
Fixed _textcolor definition.
[cc65] / libsrc / geos-cbm / emd / geos-vdc.s
1 ;
2 ; Extended memory driver for the VDC RAM available on all C128 machines
3 ; version for GEOS enters safe I/O config on C64 (transparent on C128)
4 ;
5 ; Maciej 'YTM/Elysium' Witkowiak <ytm@elysium.pl>
6 ; 06,20,25.12.2002
7
8             .include "zeropage.inc"
9             .include "em-kernel.inc"
10             .include "em-error.inc"
11
12             .macpack generic
13             .macpack module
14
15
16 ; ------------------------------------------------------------------------
17 ; Header. Includes jump table
18
19         module_header   _geos_vdc_emd
20
21 ; Driver signature
22
23         .byte $65, $6d, $64     ; "emd"
24         .byte EMD_API_VERSION   ; EM API version number
25
26 ; Library reference
27
28         .addr   $0000
29
30 ; Jump table
31
32         .addr   INSTALL
33         .addr   UNINSTALL
34         .addr   PAGECOUNT
35         .addr   MAP
36         .addr   USE
37         .addr   COMMIT
38         .addr   COPYFROM
39         .addr   COPYTO
40
41 ; ------------------------------------------------------------------------
42 ; Constants
43
44 VDC_ADDR_REG    = $D600         ; VDC address
45 VDC_DATA_REG    = $D601         ; VDC data
46
47 VDC_DATA_HI     = 18            ; used registers
48 VDC_DATA_LO     = 19
49 VDC_CSET        = 28
50 VDC_DATA        = 31
51
52 ; ------------------------------------------------------------------------
53 ; Data.
54
55 .data
56
57 pagecount:
58         .word 64                ; $0000-$3fff as 16k default
59 curpage:
60         .word $ffff             ; currently mapped-in page (invalid)
61
62 .bss
63
64 window:
65         .res 256                ; memory window
66
67 .code
68
69 ; ------------------------------------------------------------------------
70 ; INSTALL routine. Is called after the driver is loaded into memory. If
71 ; possible, check if the hardware is present and determine the amount of
72 ; memory available.
73 ; Must return an EM_ERR_xx code in a/x.
74 ;
75
76 INSTALL:
77         ; do test for VDC presence here???
78
79         php
80         sei
81         lda $01
82         pha
83         lda #$35
84         sta $01
85             
86         ldx #VDC_CSET           ; determine size of RAM...
87         jsr vdcgetreg
88         sta tmp1
89         ora #%00010000
90         jsr vdcputreg           ; turn on 64k
91             
92         jsr settestadr1         ; save original value of test byte
93         jsr vdcgetbyte
94         sta tmp2
95             
96         lda #$55                ; write $55 here
97         ldy #ptr1
98         jsr test64k             ; read it here and there
99         lda #$aa                ; write $aa here
100         ldy #ptr2
101         jsr test64k             ; read it here and there
102             
103         jsr settestadr1
104         lda tmp2
105         jsr vdcputbyte          ; restore original value of test byte
106             
107         lda ptr1                ; do bytes match?
108         cmp ptr1+1
109         bne @have64k
110         lda ptr2
111         cmp ptr2+1
112         bne @have64k
113             
114         ldx #VDC_CSET
115         lda tmp1
116         jsr vdcputreg           ; restore 16/64k flag
117         jmp @endok              ; and leave default values for 16k
118             
119 @have64k:   
120         lda #<256
121         ldx #>256
122         sta pagecount
123         stx pagecount+1
124 @endok:     
125         pla
126         sta $01
127         plp
128         lda #<EM_ERR_OK
129         ldx #>EM_ERR_OK
130         rts 
131             
132 test64k:    
133         sta tmp1
134         sty ptr3
135         lda #0
136         sta ptr3+1
137         jsr settestadr1
138         lda tmp1
139         jsr vdcputbyte          ; write $55
140         jsr settestadr1
141         jsr vdcgetbyte          ; read here
142         pha
143         jsr settestadr2
144         jsr vdcgetbyte          ; and there
145         ldy #1
146         sta (ptr3),y
147         pla
148         dey
149         sta (ptr3),y
150         rts
151
152 settestadr1:
153         ldy #$02                ; test page 2 (here)
154         .byte $2c
155 settestadr2:
156         ldy #$42                ; or page 64+2 (there)
157         lda #0
158         jmp vdcsetsrcaddr
159
160 ; ------------------------------------------------------------------------
161 ; UNINSTALL routine. Is called before the driver is removed from memory.
162 ; Can do cleanup or whatever. Must not return anything.
163 ;
164
165 UNINSTALL:
166         ;on C128 restore font and clear the screen?
167         rts
168
169 ; ------------------------------------------------------------------------
170 ; PAGECOUNT: Return the total number of available pages in a/x.
171 ;
172
173 PAGECOUNT:
174         lda pagecount
175         ldx pagecount+1
176         rts
177
178 ; ------------------------------------------------------------------------
179 ; MAP: Map the page in a/x into memory and return a pointer to the page in
180 ; a/x. The contents of the currently mapped page (if any) may be discarded
181 ; by the driver.
182 ;
183
184 MAP:    sta curpage
185         stx curpage+1
186         sta ptr1+1
187         ldy #0
188         sty ptr1
189             
190         lda #<window
191         sta ptr2
192         lda #>window
193         sta ptr2+1
194             
195         jsr transferin
196             
197         lda #<window
198         ldx #>window
199         rts
200
201 ; copy a single page from (ptr1):VDCRAM to (ptr2):RAM
202
203 transferin:
204         php
205         sei
206         lda $01
207         pha
208         lda #$35
209         sta $01
210         lda ptr1
211         ldy ptr1+1
212         jsr vdcsetsrcaddr       ; set source address in VDC
213         ldy #0
214         ldx #VDC_DATA
215         stx VDC_ADDR_REG
216 @L0:    bit VDC_ADDR_REG
217         bpl @L0
218         lda VDC_DATA_REG        ; get 2 bytes at a time to speed-up
219         sta (ptr2),y            ; (in fact up to 8 bytes could be fetched with special VDC config)
220         iny
221         lda VDC_DATA_REG
222         sta (ptr2),y
223         iny
224         bne @L0
225         pla
226         sta $01
227         plp
228         rts
229
230 ; ------------------------------------------------------------------------
231 ; USE: Tell the driver that the window is now associated with a given page.
232
233 USE:    sta curpage
234         stx curpage+1           ; Remember the page
235         lda #<window
236         ldx #>window            ; Return the window
237 done:   rts
238
239 ; ------------------------------------------------------------------------
240 ; COMMIT: Commit changes in the memory window to extended storage.
241
242 COMMIT:
243         lda curpage             ; jump if no page mapped
244         ldx curpage+1
245         bmi done
246         sta ptr1+1
247         ldy #0
248         sty ptr1
249
250         lda #<window
251         sta ptr2
252         lda #>window
253         sta ptr2+1
254
255 ; fall through to transferout
256
257 ; copy a single page from (ptr2):RAM to (ptr1):VDCRAM
258
259 transferout:
260         php
261         sei
262         lda $01
263         pha
264         lda #$35
265         sta $01
266         lda ptr1
267         ldy ptr1+1
268         jsr vdcsetsrcaddr       ; set source address in VDC
269         ldy #0
270         ldx #VDC_DATA
271         stx VDC_ADDR_REG
272 @L0:    bit VDC_ADDR_REG
273         bpl @L0
274         lda (ptr2),y            ; speedup does not work for writing
275         sta VDC_DATA_REG
276         iny
277         bne @L0
278         pla
279         sta $01
280         plp
281         rts
282
283 ; ------------------------------------------------------------------------
284 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
285 ; describing the request is passed in a/x.
286 ; The function must not return anything.
287 ;
288
289 COPYFROM:
290         jsr setup
291         beq @L2                 ; Skip if no full pages
292
293 ; Copy full pages
294
295 @L1:    jsr transferin
296         inc ptr1+1
297         inc ptr2+1
298         dec tmp1
299         bne @L1
300
301 ; Copy the remainder of the page
302                  
303 @L2:    ldy #EM_COPY::COUNT
304         lda (ptr3),y            ; Get bytes in last page
305         beq @L4
306         sta tmp1
307
308 ; Transfer the bytes in the last page
309         php
310         sei
311         lda $01
312         pha
313         lda #$35
314         sta $01
315         ldy #0
316 @L3:    jsr vdcgetbyte
317         sta (ptr2),y
318         iny
319         dec tmp1
320         lda tmp1
321         bne @L3
322         pla
323         sta $01
324         plp
325 @L4:    rts
326
327 ; ------------------------------------------------------------------------
328 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
329 ; describing the request is passed in a/x.
330 ; The function must not return anything.
331 ;
332
333 COPYTO:
334         jsr setup
335         beq @L2                 ; Skip if no full pages
336
337 ; Copy full pages
338
339 @L1:    jsr transferout
340         inc ptr1+1
341         inc ptr2+1
342         dec tmp1
343         bne @L1
344
345 ; Copy the remainder of the page
346
347 @L2:    ldy #EM_COPY::COUNT
348         lda (ptr3),y            ; Get bytes in last page
349         beq @L4
350         sta tmp1
351
352 ; Transfer the bytes in the last page
353         php
354         sei
355         lda $01
356         pha
357         lda #$35
358         sta $01
359         ldy #0
360 @L3:    lda (ptr2),y
361         jsr vdcputbyte
362         iny
363         dec tmp1
364         lda tmp1
365         bne @L3
366         pla
367         sta $01
368         plp
369 @L4:    rts
370
371 ;-------------------------------------------------------------------------
372 ; Helper functions to handle VDC ram
373 ;
374
375 vdcsetsrcaddr:
376         ldx #VDC_DATA_LO
377         stx VDC_ADDR_REG
378 @L0:    bit VDC_ADDR_REG
379         bpl @L0
380         sta VDC_DATA_REG
381         dex
382         tya
383         stx VDC_ADDR_REG
384         sta VDC_DATA_REG
385         rts
386
387 vdcgetbyte:
388         ldx #VDC_DATA
389 vdcgetreg:
390         stx VDC_ADDR_REG
391 @L0:    bit VDC_ADDR_REG
392         bpl @L0
393         lda VDC_DATA_REG
394         rts 
395
396 vdcputbyte: 
397         ldx #VDC_DATA
398 vdcputreg:
399         stx VDC_ADDR_REG
400 @L0:    bit VDC_ADDR_REG
401         bpl @L0
402         sta VDC_DATA_REG
403         rts
404
405 ; ------------------------------------------------------------------------
406 ; Helper function for COPYFROM and COPYTO: Store the pointer to the request
407 ; structure and prepare data for the copy
408 ;
409
410 setup:
411         sta ptr3
412         stx ptr3+1              ; Save the passed em_copy pointer
413
414         ldy #EM_COPY::OFFS
415         lda (ptr3),y
416         sta ptr1
417         ldy #EM_COPY::PAGE
418         lda (ptr3),y
419         sta ptr1+1              ; From
420
421         ldy #EM_COPY::BUF
422         lda (ptr3),y
423         sta ptr2
424         iny
425         lda (ptr3),y
426         sta ptr2+1              ; To
427
428         ldy #EM_COPY::COUNT+1
429         lda (ptr3),y            ; Get number of pages
430         sta tmp1
431         rts