]> git.sur5r.net Git - cc65/blob - libsrc/c64/c64-ram.s
Removed the HORLINE entry point
[cc65] / libsrc / c64 / c64-ram.s
1 ;
2 ; Extended memory driver for the C64 hidden RAM. Driver works without
3 ; problems when statically linked.
4 ;
5 ; Ullrich von Bassewitz, 2002-12-02
6 ;
7
8         .include        "zeropage.inc"
9
10         .include        "em-kernel.inc"
11         .include        "em-error.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   UNINSTALL
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    = $D000
42 PAGES   = ($10000 - BASE) / 256
43
44 ; ------------------------------------------------------------------------
45 ; Data.
46
47 .bss
48 curpage:        .res    1               ; Current page number
49 window:         .res    256             ; Memory "window"
50
51 .code
52
53 ; ------------------------------------------------------------------------
54 ; INSTALL routine. Is called after the driver is loaded into memory. If
55 ; possible, check if the hardware is present and determine the amount of
56 ; memory available.
57 ; Must return an EM_ERR_xx code in a/x.
58 ;
59
60 INSTALL:
61         ldx     #$FF
62         stx     curpage                 ; Invalidate the current page
63         inx                             ; X = 0
64         txa                             ; A = X = EM_ERR_OK
65         rts
66
67 ; ------------------------------------------------------------------------
68 ; UNINSTALL routine. Is called before the driver is removed from memory.
69 ; Can do cleanup or whatever. Must not return anything.
70 ;
71
72 UNINSTALL:
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                 ; Remember the new page
92
93         clc
94         adc     #>BASE
95         sta     ptr1+1
96         ldy     #$00
97         sty     ptr1
98
99         lda     #<window
100         sta     ptr2
101         lda     #>window
102         sta     ptr2+1
103
104 ; Transfer one page
105
106         jsr     transfer                ; Transfer one page
107
108 ; Return the memory window
109
110         lda     #<window
111         ldx     #>window                ; Return the window address
112         rts
113
114 ; ------------------------------------------------------------------------
115 ; USE: Tell the driver that the window is now associated with a given page.
116
117 USE:    sta     curpage                 ; Remember the page
118         lda     #<window
119         ldx     #>window                ; Return the window
120         rts
121
122 ; ------------------------------------------------------------------------
123 ; COMMIT: Commit changes in the memory window to extended storage.
124
125 COMMIT: lda     curpage                 ; Get the current page
126         bmi     done                    ; Jump if no page mapped
127
128         clc
129         adc     #>BASE
130         sta     ptr2+1
131         ldy     #$00
132         sty     ptr2
133
134         lda     #<window
135         sta     ptr1
136         lda     #>window
137         sta     ptr1+1
138
139 ; Transfer one page. Y must be zero on entry
140
141 transfer:
142         ldx     $01                     ; Remember c64 control port
143         txa
144         and     #$F8                    ; Bank out ROMs, I/O
145         sei
146         sta     $01
147
148 ; Unroll the following loop
149
150 loop:   lda     (ptr1),y
151         sta     (ptr2),y
152         iny
153
154         lda     (ptr1),y
155         sta     (ptr2),y
156         iny
157
158         lda     (ptr1),y
159         sta     (ptr2),y
160         iny
161
162         lda     (ptr1),y
163         sta     (ptr2),y
164         iny
165
166         lda     (ptr1),y
167         sta     (ptr2),y
168         iny
169
170         lda     (ptr1),y
171         sta     (ptr2),y
172         iny
173
174         lda     (ptr1),y
175         sta     (ptr2),y
176         iny
177
178         lda     (ptr1),y
179         sta     (ptr2),y
180         iny
181
182         bne     loop
183
184 ; Restore the old memory configuration, allow interrupts
185
186         stx     $01                     ; Restore the old configuration
187         cli
188
189 ; Done
190
191 done:   rts
192
193 ; ------------------------------------------------------------------------
194 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
195 ; describing the request is passed in a/x.
196 ; The function must not return anything.
197 ;
198
199 COPYFROM:
200         sta     ptr3
201         stx     ptr3+1                  ; Save the passed em_copy pointer
202
203         ldy     #EM_COPY_OFFS
204         lda     (ptr3),y
205         sta     ptr1
206         ldy     #EM_COPY_PAGE
207         lda     (ptr3),y
208         clc
209         adc     #>BASE
210         sta     ptr1+1                  ; From
211
212         ldy     #EM_COPY_BUF
213         lda     (ptr3),y
214         sta     ptr2
215         iny
216         lda     (ptr3),y
217         sta     ptr2+1                  ; To
218
219 common: ldy     #EM_COPY_COUNT+1
220         lda     (ptr3),y                ; Get number of pages
221         beq     @L2                     ; Skip if no full pages
222         sta     tmp1
223
224 ; Copy full pages allowing interrupts after each page copied
225
226         ldy     #$00
227 @L1:    jsr     transfer
228         inc     ptr1+1
229         inc     ptr2+1
230         dec     tmp1
231         bne     @L1
232
233 ; Copy the remainder of the page
234
235 @L2:    ldy     #EM_COPY_COUNT
236         lda     (ptr3),y                ; Get bytes in last page
237         beq     @L4
238         tax
239
240         lda     $01                     ; Remember c64 control port
241         pha
242         and     #$F8                    ; Bank out ROMs, I/O
243         sei
244         sta     $01
245
246 ; Transfer the bytes in the last page
247
248         ldy     #$00
249 @L3:    lda     (ptr1),y
250         sta     (ptr2),y
251         iny
252         dex
253         bne     @L3
254
255 ; Restore the old memory configuration, allow interrupts
256
257         pla
258         sta     $01                     ; Restore the old configuration
259         cli
260
261 ; Done
262
263 @L4:    rts
264
265 ; ------------------------------------------------------------------------
266 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
267 ; describing the request is passed in a/x.
268 ; The function must not return anything.
269 ;
270
271 COPYTO: sta     ptr3
272         stx     ptr3+1                  ; Save the passed em_copy pointer
273
274         ldy     #EM_COPY_OFFS
275         lda     (ptr3),y
276         sta     ptr2
277         ldy     #EM_COPY_PAGE
278         lda     (ptr3),y
279         clc
280         adc     #>BASE
281         sta     ptr2+1                  ; To
282
283         ldy     #EM_COPY_BUF
284         lda     (ptr3),y
285         sta     ptr1
286         iny
287         lda     (ptr3),y
288         sta     ptr1+1                  ; From
289
290         jmp     common
291
292