]> git.sur5r.net Git - cc65/blob - libsrc/cbm510/emd/cbm510-ram.s
df7e17dc75f9075e4f58bcc7eb8a6fc1e778ec36
[cc65] / libsrc / cbm510 / emd / cbm510-ram.s
1 ;
2 ; Extended memory driver for the CBM510 additional RAM banks. Driver works
3 ; without problems when linked statically.
4 ;
5 ; Ullrich von Bassewitz, 2002-12-09, 2003-12-27
6 ;
7
8         .include        "zeropage.inc"
9
10         .include        "em-kernel.inc"
11         .include        "em-error.inc"
12         .include        "cbm510.inc"
13
14         .macpack        generic
15
16
17 ; ------------------------------------------------------------------------
18 ; Header. Includes jump table
19
20 .segment        "JUMPTABLE"
21
22 ; Driver signature
23
24         .byte   $65, $6d, $64           ; "emd"
25         .byte   EMD_API_VERSION         ; EM API version number
26
27 ; Library reference
28
29         .addr   $0000
30
31 ; Jump table
32
33         .addr   INSTALL
34         .addr   UNINSTALL
35         .addr   PAGECOUNT
36         .addr   MAP
37         .addr   USE
38         .addr   COMMIT
39         .addr   COPYFROM
40         .addr   COPYTO
41
42 ; ------------------------------------------------------------------------
43 ; Constants
44
45 RAMBANK = 1
46 OFFS    = 2
47
48 ; ------------------------------------------------------------------------
49 ; Data.
50
51 .bss
52 curpage:        .res    1               ; Current page number
53
54 window:         .res    256             ; Memory "window"
55 pagecount:      .res    1               ; Number of available pages
56
57
58 .code
59
60 ; ------------------------------------------------------------------------
61 ; INSTALL routine. Is called after the driver is loaded into memory. If
62 ; possible, check if the hardware is present and determine the amount of
63 ; memory available.
64 ; Must return an EM_ERR_xx code in a/x.
65 ;
66
67 INSTALL:
68         lda     #$FF
69         sta     curpage                 ; Invalidate the current page
70         sta     pagecount               ; Assume all memory available
71
72         sec
73         jsr     $FF99                   ; MEMTOP
74
75         cmp     #RAMBANK                ; Top of memory in bank 2?
76         bne     @L1                     ; No: We can use all the memory
77         txa
78         sub     #OFFS
79         tya
80         sbc     #$00
81         sta     pagecount
82
83 @L1:    lda     #<EM_ERR_OK
84         ldx     #>EM_ERR_OK
85 ;       rts                             ; Run into UNINSTALL instead
86
87 ; ------------------------------------------------------------------------
88 ; UNINSTALL routine. Is called before the driver is removed from memory.
89 ; Can do cleanup or whatever. Must not return anything.
90 ;
91
92 UNINSTALL:
93         rts
94
95
96 ; ------------------------------------------------------------------------
97 ; PAGECOUNT: Return the total number of available pages in a/x.
98 ;
99
100 PAGECOUNT:
101         lda     pagecount
102         ldx     #0
103         rts
104
105 ; ------------------------------------------------------------------------
106 ; MAP: Map the page in a/x into memory and return a pointer to the page in
107 ; a/x. The contents of the currently mapped page (if any) may be discarded
108 ; by the driver.
109 ;
110
111 MAP:    sta     curpage                 ; Remember the new page
112
113         sta     ptr1+1
114         lda     #OFFS
115         sta     ptr1
116
117 ; Transfer one page
118
119         ldx     IndReg
120         lda     #RAMBANK
121         sta     IndReg
122
123         ldy     #$00
124 @L1:    .repeat 2
125         lda     (ptr1),y
126         sta     window,y
127         iny
128         .endrepeat
129         bne     @L1
130
131         stx     IndReg
132
133 ; Return the memory window
134
135         lda     #<window
136         ldx     #>window                ; Return the window address
137         rts
138
139 ; ------------------------------------------------------------------------
140 ; USE: Tell the driver that the window is now associated with a given page.
141
142 USE:    sta     curpage                 ; Remember the page
143         lda     #<window
144         ldx     #>window                ; Return the window
145         rts
146
147 ; ------------------------------------------------------------------------
148 ; COMMIT: Commit changes in the memory window to extended storage.
149
150 COMMIT: lda     curpage                 ; Get the current page
151         cmp     #$FF
152         beq     done                    ; Jump if no page mapped
153
154         sta     ptr1+1
155         lda     #OFFS
156         sta     ptr1
157
158 ; Transfer one page
159
160         ldx     IndReg
161         lda     #RAMBANK
162         sta     IndReg
163
164         ldy     #$00
165 @L1:    .repeat 2
166         lda     window,y
167         sta     (ptr1),y
168         iny
169         .endrepeat
170         bne     @L1
171
172         stx     IndReg
173
174 ; Done
175
176 done:   rts
177
178 ; ------------------------------------------------------------------------
179 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
180 ; describing the request is passed in a/x.
181 ; The function must not return anything.
182 ;
183
184 COPYFROM:
185         jsr     setup
186
187 ; Setup the buffer address in this bank.
188
189         sta     copyfrom_buf
190         stx     copyfrom_buf+1
191
192 ; Check if we must copy full pages
193
194         ldx     ptr2+1
195         beq     @L2
196
197 ; Copy full pages
198
199         ldx     #$00
200 @L1:    jsr     copyfrom
201         inc     ptr1+1
202         inc     copyfrom_buf+1
203 @L2:    dec     ptr2+1
204         bne     @L1
205
206 ; Copy the remaining page
207
208         ldx     ptr2
209         beq     @L3
210
211         jsr     copyfrom
212
213 ; Restore the indirect segment
214
215 @L3:    lda     ExecReg
216         sta     IndReg
217
218 ; Done
219
220         rts
221
222
223 ; ------------------------------------------------------------------------
224 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
225 ; describing the request is passed in a/x.
226 ; The function must not return anything.
227 ;
228
229 COPYTO: jsr     setup
230
231 ; Setup the buffer address in this bank.
232
233         sta     copyto_buf
234         stx     copyto_buf+1
235
236 ; Check if we must copy full pages
237
238         ldx     ptr2+1
239         beq     @L2
240
241 ; Copy full pages
242
243         ldx     #$00
244 @L1:    jsr     copyto
245         inc     ptr1+1
246         inc     copyto_buf+1
247 @L2:    dec     ptr2+1
248         bne     @L1
249
250 ; Copy the remaining page
251
252         ldx     ptr2
253         beq     @L3
254
255         jsr     copyto
256
257 ; Restore the indirect segment
258
259 @L3:    lda     ExecReg
260         sta     IndReg
261
262 ; Done
263
264         rts
265
266 ; ------------------------------------------------------------------------
267 ; setup: Helper function for COPYFROM and COPYTO, will setup parameters.
268 ;
269
270 setup:  sta     ptr3
271         stx     ptr3+1                  ; Save the passed em_copy pointer
272
273         ldy     #EM_COPY::OFFS
274         lda     (ptr3),y
275         add     #OFFS
276         sta     ptr1
277         ldy     #EM_COPY::PAGE
278         lda     (ptr3),y
279         adc     #$00
280         sta     ptr1+1
281
282         ldy     #EM_COPY::COUNT
283         lda     (ptr3),y
284         sta     ptr2
285         iny
286         lda     (ptr3),y
287         sta     ptr2+1                  ; Get count into ptr2
288
289         ldy     #EM_COPY::BUF+1
290         lda     (ptr3),y
291         tax
292         dey
293         lda     (ptr3),y                ; Get the buffer pointer into a/x
294
295         ldy     #RAMBANK
296         sty     IndReg
297
298         ldy     #$00
299
300         rts
301
302 ; ------------------------------------------------------------------------
303 ; copyfrom
304
305 .data
306 copyfrom:
307         lda     (ptr1),y
308 copyfrom_buf = * + 1
309         sta     $0000,y
310         iny
311         dex
312         bne     copyfrom
313         rts
314
315 ; ------------------------------------------------------------------------
316 ; copyto
317
318 .data
319 copyto:
320 copyto_buf = * + 1
321         lda     $0000,y
322         sta     (ptr1),y
323         iny
324         dex
325         bne     copyto
326         rts
327