]> git.sur5r.net Git - cc65/blob - libsrc/c128/emd/c128-ifnram.s
Started on making c128 function ram emd's.
[cc65] / libsrc / c128 / emd / c128-ifnram.s
1 ;
2 ; Extended memory driver for the C128 Internal Function RAM. Driver works
3 ; without problems when statically linked.
4 ;
5 ; Marco van den Heuvel, 2015-11-30
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_ifnram_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    = $8000
47 PAGES   = 128
48
49 ; ------------------------------------------------------------------------
50 ; Data.
51
52 .bss
53 curpage:        .res    2               ; Current page number
54
55 window:         .res    256             ; Memory "window"
56
57 .code
58
59 ; ------------------------------------------------------------------------
60 ; INSTALL routine. Is called after the driver is loaded into memory. If
61 ; possible, check if the hardware is present and determine the amount of
62 ; memory available.
63 ; Must return an EM_ERR_xx code in a/x.
64 ;
65
66 INSTALL:
67         ldx     #$FF
68         stx     curpage
69         stx     curpage+1               ; Invalidate the current page
70         inx
71         txa                             ; A = X = EM_ERR_OK
72
73 ; ------------------------------------------------------------------------
74 ; UNINSTALL routine. Is called before the driver is removed from memory.
75 ; Can do cleanup or whatever. Must not return anything.
76 ;
77
78 UNINSTALL:
79         rts
80
81
82 ; ------------------------------------------------------------------------
83 ; PAGECOUNT: Return the total number of available pages in a/x.
84 ;
85
86 PAGECOUNT:
87         lda     #<PAGES
88         ldx     #>PAGES
89         rts
90
91 ; ------------------------------------------------------------------------
92 ; MAP: Map the page in a/x into memory and return a pointer to the page in
93 ; a/x. The contents of the currently mapped page (if any) may be discarded
94 ; by the driver.
95 ;
96
97 MAP:    sei
98         sta     curpage
99         stx     curpage+1               ; Remember the new page
100
101         clc
102         adc     #>BASE
103         sta     ptr1+1
104         ldy     #$00
105         sty     ptr1
106
107         lda     #<ptr1
108         sta     FETVEC
109
110 ; Transfer one page
111
112 @L1:    ldx     #MMU_CFG_INT_FROM
113         jsr     FETCH
114         sta     window,y
115         iny
116         bne     @L1
117
118 ; Return the memory window
119
120         lda     #<window
121         ldx     #>window                ; Return the window address
122         cli
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: sei
138         lda     curpage                 ; Get the current page
139         ldx     curpage+1
140         bmi     done                    ; Jump if no page mapped
141
142         clc
143         adc     #>BASE
144         sta     ptr1+1
145         ldy     #$00
146         sty     ptr1
147
148         lda     #<ptr1
149         sta     STAVEC
150
151 ; Transfer one page. Y must be zero on entry
152
153 @L1:    lda     window,y
154         ldx     #MMU_CFG_INT_FROM
155         jsr     STASH
156         iny
157         bne     @L1
158
159 ; Done
160
161 done:   cli
162         rts
163
164 ; ------------------------------------------------------------------------
165 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
166 ; describing the request is passed in a/x.
167 ; The function must not return anything.
168 ;
169
170 COPYFROM:
171         sei
172         sta     ptr3
173         stx     ptr3+1                  ; Save the passed em_copy pointer
174
175         ldy     #EM_COPY::OFFS
176         lda     (ptr3),y
177         sta     ptr1
178         ldy     #EM_COPY::PAGE
179         lda     (ptr3),y
180         clc
181         adc     #>BASE
182         sta     ptr1+1                  ; From
183
184         ldy     #EM_COPY::BUF
185         lda     (ptr3),y
186         sta     ptr2
187         iny
188         lda     (ptr3),y
189         sta     ptr2+1                  ; To
190
191         lda     #<ptr1
192         sta     FETVEC
193
194         ldy     #EM_COPY::COUNT+1
195         lda     (ptr3),y                ; Get number of pages
196         beq     @L2                     ; Skip if no full pages
197         sta     tmp1
198
199 ; Copy full pages
200
201         ldy     #$00
202 @L1:    ldx     #MMU_CFG_INT_FROM
203         jsr     FETCH
204         sta     (ptr2),y
205         iny
206         bne     @L1
207         inc     ptr1+1
208         inc     ptr2+1
209         dec     tmp1
210         bne     @L1
211
212 ; Copy the remainder of the page
213
214 @L2:    ldy     #EM_COPY::COUNT
215         lda     (ptr3),y                ; Get bytes in last page
216         beq     @L4
217         sta     tmp1
218
219         ldy     #$00
220 @L3:    ldx     #MMU_CFG_INT_FROM
221         jsr     FETCH
222         sta     (ptr2),y
223         iny
224         dec     tmp1
225         bne     @L3
226
227 ; Done
228
229 @L4:    cli
230         rts
231
232 ; ------------------------------------------------------------------------
233 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
234 ; describing the request is passed in a/x.
235 ; The function must not return anything.
236 ;
237
238 COPYTO: sei
239         sta     ptr3
240         stx     ptr3+1                  ; Save the passed em_copy pointer
241
242         ldy     #EM_COPY::OFFS
243         lda     (ptr3),y
244         sta     ptr1
245         ldy     #EM_COPY::PAGE
246         lda     (ptr3),y
247         clc
248         adc     #>BASE
249         sta     ptr1+1                  ; To
250
251         ldy     #EM_COPY::BUF
252         lda     (ptr3),y
253         sta     ptr2
254         iny
255         lda     (ptr3),y
256         sta     ptr2+1                  ; From
257
258         lda     #<ptr1
259         sta     STAVEC
260
261         ldy     #EM_COPY::COUNT+1
262         lda     (ptr3),y                ; Get number of pages
263         beq     @L2                     ; Skip if no full pages
264         sta     tmp1
265
266 ; Copy full pages
267
268         ldy     #$00
269 @L1:    lda     (ptr2),y
270         ldx     #MMU_CFG_INT_FROM
271         jsr     STASH
272         iny
273         bne     @L1
274         inc     ptr1+1
275         inc     ptr2+1
276         dec     tmp1
277         bne     @L1
278
279 ; Copy the remainder of the page
280
281 @L2:    ldy     #EM_COPY::COUNT
282         lda     (ptr3),y                ; Get bytes in last page
283         beq     @L4
284         sta     tmp1
285
286         ldy     #$00
287 @L3:    lda     (ptr2),y
288         ldx     #MMU_CFG_INT_FROM
289         jsr     STASH
290         iny
291         dec     tmp1
292         bne     @L3
293
294 ; Done
295
296 @L4:    cli
297         rts