]> git.sur5r.net Git - cc65/blob - libsrc/c128/emd/c128-ram.s
Fixed gcc compiler warning (#867)
[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         .macpack        generic
15         .macpack        module
16
17
18 ; ------------------------------------------------------------------------
19 ; Header. Includes jump table
20
21         module_header   _c128_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 BASE    = $400
47 TOPMEM  = $FF00
48 PAGES   = (TOPMEM - BASE) / 256
49
50 ; ------------------------------------------------------------------------
51 ; Data.
52
53 .bss
54 curpage:        .res    2               ; Current page number
55
56 window:         .res    256             ; Memory "window"
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         ldx     #$FF
69         stx     curpage
70         stx     curpage+1               ; Invalidate the current page
71         inx
72         txa                             ; A = X = EM_ERR_OK
73 ;       rts                             ; Run into UNINSTALL instead
74
75 ; ------------------------------------------------------------------------
76 ; UNINSTALL routine. Is called before the driver is removed from memory.
77 ; Can do cleanup or whatever. Must not return anything.
78 ;
79
80 UNINSTALL:
81         rts
82
83
84 ; ------------------------------------------------------------------------
85 ; PAGECOUNT: Return the total number of available pages in a/x.
86 ;
87
88 PAGECOUNT:
89         lda     #<PAGES
90         ldx     #>PAGES
91         rts
92
93 ; ------------------------------------------------------------------------
94 ; MAP: Map the page in a/x into memory and return a pointer to the page in
95 ; a/x. The contents of the currently mapped page (if any) may be discarded
96 ; by the driver.
97 ;
98
99 MAP:    sta     curpage
100         stx     curpage+1               ; Remember the new page
101
102         clc
103         adc     #>BASE
104         sta     ptr1+1
105         ldy     #$00
106         sty     ptr1
107
108         lda     #<ptr1
109         sta     FETVEC
110         sei
111
112 ; Transfer one page
113
114 @L1:    ldx     #MMU_CFG_RAM1
115         jsr     FETCH
116         sta     window,y
117         iny
118         bne     @L1
119         cli
120
121 ; Return the memory window
122
123         lda     #<window
124         ldx     #>window                ; Return the window address
125         rts
126
127 ; ------------------------------------------------------------------------
128 ; USE: Tell the driver that the window is now associated with a given page.
129
130 USE:    sta     curpage
131         stx     curpage+1               ; Remember the page
132         lda     #<window
133         ldx     #>window                ; Return the window
134         rts
135
136 ; ------------------------------------------------------------------------
137 ; COMMIT: Commit changes in the memory window to extended storage.
138
139 COMMIT: lda     curpage                 ; Get the current page
140         ldx     curpage+1
141         bmi     done                    ; Jump if no page mapped
142
143         clc
144         adc     #>BASE
145         sta     ptr1+1
146         ldy     #$00
147         sty     ptr1
148
149         lda     #<ptr1
150         sta     STAVEC
151         sei
152
153 ; Transfer one page. Y must be zero on entry
154
155 @L1:    lda     window,y
156         ldx     #MMU_CFG_RAM1
157         jsr     STASH
158         iny
159         bne     @L1
160         cli
161
162 ; Done
163
164 done:   rts
165
166 ; ------------------------------------------------------------------------
167 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
168 ; describing the request is passed in a/x.
169 ; The function must not return anything.
170 ;
171
172 COPYFROM:
173         sta     ptr3
174         stx     ptr3+1                  ; Save the passed em_copy pointer
175
176         ldy     #EM_COPY::OFFS
177         lda     (ptr3),y
178         sta     ptr1
179         ldy     #EM_COPY::PAGE
180         lda     (ptr3),y
181         clc
182         adc     #>BASE
183         sta     ptr1+1                  ; From
184
185         ldy     #EM_COPY::BUF
186         lda     (ptr3),y
187         sta     ptr2
188         iny
189         lda     (ptr3),y
190         sta     ptr2+1                  ; To
191
192         lda     #<ptr1
193         sta     FETVEC
194
195         ldy     #EM_COPY::COUNT+1
196         lda     (ptr3),y                ; Get number of pages
197         beq     @L2                     ; Skip if no full pages
198         sta     tmp1
199
200 ; Copy full pages
201
202         ldy     #$00
203         sei
204 @L1:    ldx     #MMU_CFG_RAM1
205         jsr     FETCH
206         sta     (ptr2),y
207         iny
208         bne     @L1
209         inc     ptr1+1
210         inc     ptr2+1
211         dec     tmp1
212         bne     @L1
213
214 ; Copy the remainder of the page
215
216 @L2:    ldy     #EM_COPY::COUNT
217         lda     (ptr3),y                ; Get bytes in last page
218         beq     @L4
219         sta     tmp1
220
221         ldy     #$00
222 @L3:    ldx     #MMU_CFG_RAM1
223         jsr     FETCH
224         sta     (ptr2),y
225         iny
226         dec     tmp1
227         bne     @L3
228
229 ; Done
230
231 @L4:    cli
232         rts
233
234 ; ------------------------------------------------------------------------
235 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
236 ; describing the request is passed in a/x.
237 ; The function must not return anything.
238 ;
239
240 COPYTO: sta     ptr3
241         stx     ptr3+1                  ; Save the passed em_copy pointer
242
243         ldy     #EM_COPY::OFFS
244         lda     (ptr3),y
245         sta     ptr1
246         ldy     #EM_COPY::PAGE
247         lda     (ptr3),y
248         clc
249         adc     #>BASE
250         sta     ptr1+1                  ; To
251
252         ldy     #EM_COPY::BUF
253         lda     (ptr3),y
254         sta     ptr2
255         iny
256         lda     (ptr3),y
257         sta     ptr2+1                  ; From
258
259         lda     #<ptr1
260         sta     STAVEC
261
262         ldy     #EM_COPY::COUNT+1
263         lda     (ptr3),y                ; Get number of pages
264         beq     @L2                     ; Skip if no full pages
265         sta     tmp1
266
267 ; Copy full pages
268
269         ldy     #$00
270         sei
271 @L1:    lda     (ptr2),y
272         ldx     #MMU_CFG_RAM1
273         jsr     STASH
274         iny
275         bne     @L1
276         inc     ptr1+1
277         inc     ptr2+1
278         dec     tmp1
279         bne     @L1
280
281 ; Copy the remainder of the page
282
283 @L2:    ldy     #EM_COPY::COUNT
284         lda     (ptr3),y                ; Get bytes in last page
285         beq     @L4
286         sta     tmp1
287
288         ldy     #$00
289 @L3:    lda     (ptr2),y
290         ldx     #MMU_CFG_RAM1
291         jsr     STASH
292         iny
293         dec     tmp1
294         bne     @L3
295
296 ; Done
297
298 @L4:    cli
299         rts