]> git.sur5r.net Git - cc65/blob - libsrc/c128/emd/c128-ram.s
bd1984333ce0eaa551bc3ad98d4b8dfda024be1a
[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        "HEADER"
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    1               ; 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
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
111 ; Transfer one page
112
113 @L1:    ldx     #MMU_CFG_RAM1
114         jsr     FETCH
115         sta     window,y
116         iny
117         bne     @L1
118
119 ; Return the memory window
120
121         lda     #<window
122         ldx     #>window                ; Return the window address
123         rts
124
125 ; ------------------------------------------------------------------------
126 ; USE: Tell the driver that the window is now associated with a given page.
127
128 USE:    sta     curpage
129         stx     curpage+1               ; Remember the page
130         lda     #<window
131         ldx     #>window                ; Return the window
132         rts
133
134 ; ------------------------------------------------------------------------
135 ; COMMIT: Commit changes in the memory window to extended storage.
136
137 COMMIT: lda     curpage                 ; Get the current page
138         ldx     curpage+1
139         bmi     done                    ; Jump if no page mapped
140
141         clc
142         adc     #>BASE
143         sta     ptr1+1
144         ldy     #$00
145         sty     ptr1
146
147         lda     #<ptr1
148         sta     STAVEC
149
150 ; Transfer one page. Y must be zero on entry
151
152 @L1:    lda     window,y
153         ldx     #MMU_CFG_RAM1
154         jsr     STASH
155         iny
156         bne     @L1
157
158 ; Done
159
160 done:   rts
161
162 ; ------------------------------------------------------------------------
163 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
164 ; describing the request is passed in a/x.
165 ; The function must not return anything.
166 ;
167
168 COPYFROM:
169         sta     ptr3
170         stx     ptr3+1                  ; Save the passed em_copy pointer
171
172         ldy     #EM_COPY::OFFS
173         lda     (ptr3),y
174         sta     ptr1
175         ldy     #EM_COPY::PAGE
176         lda     (ptr3),y
177         clc
178         adc     #>BASE
179         sta     ptr1+1                  ; From
180
181         ldy     #EM_COPY::BUF
182         lda     (ptr3),y
183         sta     ptr2
184         iny
185         lda     (ptr3),y
186         sta     ptr2+1                  ; To
187
188         lda     #<ptr1
189         sta     FETVEC
190
191         ldy     #EM_COPY::COUNT+1
192         lda     (ptr3),y                ; Get number of pages
193         beq     @L2                     ; Skip if no full pages
194         sta     tmp1
195
196 ; Copy full pages
197
198         ldy     #$00
199 @L1:    ldx     #MMU_CFG_RAM1
200         jsr     FETCH
201         sta     (ptr2),y
202         iny
203         bne     @L1
204         inc     ptr1+1
205         inc     ptr2+1
206         dec     tmp1
207         bne     @L1
208
209 ; Copy the remainder of the page
210
211 @L2:    ldy     #EM_COPY::COUNT
212         lda     (ptr3),y                ; Get bytes in last page
213         beq     @L4
214         sta     tmp1
215
216         ldy     #$00
217 @L3:    ldx     #MMU_CFG_RAM1
218         jsr     FETCH
219         sta     (ptr2),y
220         iny
221         dec     tmp1
222         bne     @L3
223
224 ; Done
225
226 @L4:    rts
227
228 ; ------------------------------------------------------------------------
229 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
230 ; describing the request is passed in a/x.
231 ; The function must not return anything.
232 ;
233
234 COPYTO: sta     ptr3
235         stx     ptr3+1                  ; Save the passed em_copy pointer
236
237         ldy     #EM_COPY::OFFS
238         lda     (ptr3),y
239         sta     ptr1
240         ldy     #EM_COPY::PAGE
241         lda     (ptr3),y
242         clc
243         adc     #>BASE
244         sta     ptr1+1                  ; To
245
246         ldy     #EM_COPY::BUF
247         lda     (ptr3),y
248         sta     ptr2
249         iny
250         lda     (ptr3),y
251         sta     ptr2+1                  ; From
252
253         lda     #<ptr1
254         sta     STAVEC
255
256         ldy     #EM_COPY::COUNT+1
257         lda     (ptr3),y                ; Get number of pages
258         beq     @L2                     ; Skip if no full pages
259         sta     tmp1
260
261 ; Copy full pages
262
263         ldy     #$00
264 @L1:    lda     (ptr2),y
265         ldx     #MMU_CFG_RAM1
266         jsr     STASH
267         iny
268         bne     @L1
269         inc     ptr1+1
270         inc     ptr2+1
271         dec     tmp1
272         bne     @L1
273
274 ; Copy the remainder of the page
275
276 @L2:    ldy     #EM_COPY::COUNT
277         lda     (ptr3),y                ; Get bytes in last page
278         beq     @L4
279         sta     tmp1
280
281         ldy     #$00
282 @L3:    lda     (ptr2),y
283         ldx     #MMU_CFG_RAM1
284         jsr     STASH
285         iny
286         dec     tmp1
287         bne     @L3
288
289 ; Done
290
291 @L4:    rts
292