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