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