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