]> git.sur5r.net Git - cc65/blob - libsrc/c128/emd/c128-ram.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / c128 / emd / c128-ram.s
1 ;
2 ; Extended memory driver for the C128 RAM in bank #1. Driver works without
3 ; problems when statically linked.
4 ;
5 ; Ullrich von Bassewitz, 2002-12-04
6 ;
7
8         .include        "zeropage.inc"
9
10         .include        "em-kernel.inc"
11         .include        "em-error.inc"
12         .include        "c128.inc"
13
14
15         .macpack        generic
16
17
18 ; ------------------------------------------------------------------------
19 ; Header. Includes jump table
20
21 .segment        "JUMPTABLE"
22
23 ; Driver signature
24
25         .byte   $65, $6d, $64           ; "emd"
26         .byte   EMD_API_VERSION         ; EM API version number
27
28 ; Jump table.
29
30         .word   INSTALL
31         .word   UNINSTALL
32         .word   PAGECOUNT
33         .word   MAP
34         .word   USE
35         .word   COMMIT
36         .word   COPYFROM
37         .word   COPYTO
38
39 ; ------------------------------------------------------------------------
40 ; Constants
41
42 BASE    = $400
43 TOPMEM  = $FF00
44 PAGES   = (TOPMEM - BASE) / 256
45
46 ; ------------------------------------------------------------------------
47 ; Data.
48
49 .bss
50 curpage:        .res    1               ; Current page number
51
52 window:         .res    256             ; Memory "window"
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         ldx     #$FF
65         stx     curpage
66         stx     curpage+1               ; Invalidate the current page
67         inx
68         txa                             ; A = X = EM_ERR_OK
69         rts
70
71 ; ------------------------------------------------------------------------
72 ; UNINSTALL routine. Is called before the driver is removed from memory.
73 ; Can do cleanup or whatever. Must not return anything.
74 ;
75
76 UNINSTALL:
77         rts
78
79
80 ; ------------------------------------------------------------------------
81 ; PAGECOUNT: Return the total number of available pages in a/x.
82 ;
83
84 PAGECOUNT:
85         lda     #<PAGES
86         ldx     #>PAGES
87         rts
88
89 ; ------------------------------------------------------------------------
90 ; MAP: Map the page in a/x into memory and return a pointer to the page in
91 ; a/x. The contents of the currently mapped page (if any) may be discarded
92 ; by the driver.
93 ;
94
95 MAP:    sta     curpage
96         stx     curpage+1               ; Remember the new page
97
98         clc
99         adc     #>BASE
100         sta     ptr1+1
101         ldy     #$00
102         sty     ptr1
103
104         lda     #<ptr1
105         sta     FETVEC
106
107 ; Transfer one page
108
109 @L1:    ldx     #MMU_CFG_RAM1
110         jsr     FETCH
111         sta     window,y
112         iny
113         bne     @L1
114
115 ; Return the memory window
116
117         lda     #<window
118         ldx     #>window                ; Return the window address
119         rts
120
121 ; ------------------------------------------------------------------------
122 ; USE: Tell the driver that the window is now associated with a given page.
123
124 USE:    sta     curpage
125         stx     curpage+1               ; Remember the page
126         lda     #<window
127         ldx     #>window                ; Return the window
128         rts
129
130 ; ------------------------------------------------------------------------
131 ; COMMIT: Commit changes in the memory window to extended storage.
132
133 COMMIT: lda     curpage                 ; Get the current page
134         ldx     curpage+1
135         bmi     done                    ; Jump if no page mapped
136
137         clc
138         adc     #>BASE
139         sta     ptr1+1
140         ldy     #$00
141         sty     ptr1
142
143         lda     #<ptr1
144         sta     STAVEC
145
146 ; Transfer one page. Y must be zero on entry
147
148 @L1:    lda     window,y
149         ldx     #MMU_CFG_RAM1
150         jsr     STASH
151         iny
152         bne     @L1
153
154 ; Done
155
156 done:   rts
157
158 ; ------------------------------------------------------------------------
159 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
160 ; describing the request is passed in a/x.
161 ; The function must not return anything.
162 ;
163
164 COPYFROM:
165         sta     ptr3
166         stx     ptr3+1                  ; Save the passed em_copy pointer
167
168         ldy     #EM_COPY::OFFS
169         lda     (ptr3),y
170         sta     ptr1
171         ldy     #EM_COPY::PAGE
172         lda     (ptr3),y
173         clc
174         adc     #>BASE
175         sta     ptr1+1                  ; From
176
177         ldy     #EM_COPY::BUF
178         lda     (ptr3),y
179         sta     ptr2
180         iny
181         lda     (ptr3),y
182         sta     ptr2+1                  ; To
183
184         lda     #<ptr1
185         sta     FETVEC
186
187         ldy     #EM_COPY::COUNT+1
188         lda     (ptr3),y                ; Get number of pages
189         beq     @L2                     ; Skip if no full pages
190         sta     tmp1
191
192 ; Copy full pages
193
194         ldy     #$00
195 @L1:    ldx     #MMU_CFG_RAM1
196         jsr     FETCH
197         sta     (ptr2),y
198         iny
199         bne     @L1
200         inc     ptr1+1
201         inc     ptr2+1
202         dec     tmp1
203         bne     @L1
204
205 ; Copy the remainder of the page
206
207 @L2:    ldy     #EM_COPY::COUNT
208         lda     (ptr3),y                ; Get bytes in last page
209         beq     @L4
210         sta     tmp1
211
212         ldy     #$00
213 @L3:    ldx     #MMU_CFG_RAM1
214         jsr     FETCH
215         sta     (ptr2),y
216         iny
217         dec     tmp1
218         bne     @L3
219
220 ; Done
221
222 @L4:    rts
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: sta     ptr3
231         stx     ptr3+1                  ; Save the passed em_copy pointer
232
233         ldy     #EM_COPY::OFFS
234         lda     (ptr3),y
235         sta     ptr1
236         ldy     #EM_COPY::PAGE
237         lda     (ptr3),y
238         clc
239         adc     #>BASE
240         sta     ptr1+1                  ; To
241
242         ldy     #EM_COPY::BUF
243         lda     (ptr3),y
244         sta     ptr2
245         iny
246         lda     (ptr3),y
247         sta     ptr2+1                  ; From
248
249         lda     #<ptr1
250         sta     STAVEC
251
252         ldy     #EM_COPY::COUNT+1
253         lda     (ptr3),y                ; Get number of pages
254         beq     @L2                     ; Skip if no full pages
255         sta     tmp1
256
257 ; Copy full pages
258
259         ldy     #$00
260 @L1:    lda     (ptr2),y
261         ldx     #MMU_CFG_RAM1
262         jsr     STASH
263         iny
264         bne     @L1
265         inc     ptr1+1
266         inc     ptr2+1
267         dec     tmp1
268         bne     @L1
269
270 ; Copy the remainder of the page
271
272 @L2:    ldy     #EM_COPY::COUNT
273         lda     (ptr3),y                ; Get bytes in last page
274         beq     @L4
275         sta     tmp1
276
277         ldy     #$00
278 @L3:    lda     (ptr2),y
279         ldx     #MMU_CFG_RAM1
280         jsr     STASH
281         iny
282         dec     tmp1
283         bne     @L3
284
285 ; Done
286
287 @L4:    rts
288