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