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