]> git.sur5r.net Git - cc65/blob - libsrc/c64/emd/c64-ram.s
Renamed JUMPTABLE and cleaned up module.cfg.
[cc65] / libsrc / c64 / emd / 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        "HEADER"
21
22 ; Driver signature
23
24         .byte   $65, $6d, $64           ; "emd"
25         .byte   EMD_API_VERSION         ; EM API version number
26
27 ; Library reference
28
29         .addr   $0000
30
31 ; Jump table
32
33         .addr   INSTALL
34         .addr   UNINSTALL
35         .addr   PAGECOUNT
36         .addr   MAP
37         .addr   USE
38         .addr   COMMIT
39         .addr   COPYFROM
40         .addr   COPYTO
41
42 ; ------------------------------------------------------------------------
43 ; Constants
44
45 BASE    = $D000
46 PAGES   = ($10000 - BASE) / 256
47
48 ; ------------------------------------------------------------------------
49 ; Data.
50
51 .bss
52 curpage:        .res    1               ; Current page number
53 window:         .res    256             ; Memory "window"
54
55 .code
56
57 ; ------------------------------------------------------------------------
58 ; INSTALL routine. Is called after the driver is loaded into memory. If
59 ; possible, check if the hardware is present and determine the amount of
60 ; memory available.
61 ; Must return an EM_ERR_xx code in a/x.
62 ;
63
64 INSTALL:
65         ldx     #$FF
66         stx     curpage                 ; Invalidate the current page
67         inx                             ; X = 0
68         txa                             ; A = X = EM_ERR_OK
69 ;       rts                             ; Run into UNINSTALL instead
70
71 ; ------------------------------------------------------------------------
72 ; UNINSTALL routine. Is called before the driver is removed from memory.
73 ; Can do cleanup or whatever. Must not return anything.
74 ;
75
76 UNINSTALL:
77         rts
78
79
80 ; ------------------------------------------------------------------------
81 ; PAGECOUNT: Return the total number of available pages in a/x.
82 ;
83
84 PAGECOUNT:
85         lda     #<PAGES
86         ldx     #>PAGES
87         rts
88
89 ; ------------------------------------------------------------------------
90 ; MAP: Map the page in a/x into memory and return a pointer to the page in
91 ; a/x. The contents of the currently mapped page (if any) may be discarded
92 ; by the driver.
93 ;
94
95 MAP:    sta     curpage                 ; Remember the new page
96
97         clc
98         adc     #>BASE
99         sta     ptr1+1
100         ldy     #$00
101         sty     ptr1
102
103         lda     #<window
104         sta     ptr2
105         lda     #>window
106         sta     ptr2+1
107
108 ; Transfer one page
109
110         jsr     transfer                ; Transfer one page
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                 ; Remember the page
122         lda     #<window
123         ldx     #>window                ; Return the window
124         rts
125
126 ; ------------------------------------------------------------------------
127 ; COMMIT: Commit changes in the memory window to extended storage.
128
129 COMMIT: lda     curpage                 ; Get the current page
130         bmi     done                    ; Jump if no page mapped
131
132         clc
133         adc     #>BASE
134         sta     ptr2+1
135         ldy     #$00
136         sty     ptr2
137
138         lda     #<window
139         sta     ptr1
140         lda     #>window
141         sta     ptr1+1
142
143 ; Transfer one page. Y must be zero on entry
144
145 transfer:
146         ldx     $01                     ; Remember c64 control port
147         txa
148         and     #$F8                    ; Bank out ROMs, I/O
149         sei
150         sta     $01
151
152 ; Unroll the following loop
153
154 loop:   .repeat 8
155         lda     (ptr1),y
156         sta     (ptr2),y
157         iny
158         .endrepeat
159
160         bne     loop
161
162 ; Restore the old memory configuration, allow interrupts
163
164         stx     $01                     ; Restore the old configuration
165         cli
166
167 ; Done
168
169 done:   rts               
170
171 ; ------------------------------------------------------------------------
172 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
173 ; describing the request is passed in a/x.
174 ; The function must not return anything.
175 ;
176
177 COPYFROM:
178         sta     ptr3
179         stx     ptr3+1                  ; Save the passed em_copy pointer
180                        
181         ldy     #EM_COPY::OFFS
182         lda     (ptr3),y
183         sta     ptr1
184         ldy     #EM_COPY::PAGE
185         lda     (ptr3),y
186         clc
187         adc     #>BASE
188         sta     ptr1+1                  ; From
189
190         ldy     #EM_COPY::BUF
191         lda     (ptr3),y
192         sta     ptr2
193         iny
194         lda     (ptr3),y
195         sta     ptr2+1                  ; To
196
197 common: ldy     #EM_COPY::COUNT+1
198         lda     (ptr3),y                ; Get number of pages
199         beq     @L2                     ; Skip if no full pages
200         sta     tmp1
201
202 ; Copy full pages allowing interrupts after each page copied
203
204         ldy     #$00
205 @L1:    jsr     transfer
206         inc     ptr1+1
207         inc     ptr2+1
208         dec     tmp1
209         bne     @L1
210
211 ; Copy the remainder of the page
212
213 @L2:    ldy     #EM_COPY::COUNT
214         lda     (ptr3),y                ; Get bytes in last page
215         beq     @L4
216         tax
217
218         lda     $01                     ; Remember c64 control port
219         pha
220         and     #$F8                    ; Bank out ROMs, I/O
221         sei
222         sta     $01
223
224 ; Transfer the bytes in the last page
225
226         ldy     #$00
227 @L3:    lda     (ptr1),y
228         sta     (ptr2),y
229         iny
230         dex
231         bne     @L3
232
233 ; Restore the old memory configuration, allow interrupts
234
235         pla
236         sta     $01                     ; Restore the old configuration
237         cli
238
239 ; Done
240
241 @L4:    rts
242
243 ; ------------------------------------------------------------------------
244 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
245 ; describing the request is passed in a/x.
246 ; The function must not return anything.
247 ;
248
249 COPYTO: sta     ptr3
250         stx     ptr3+1                  ; Save the passed em_copy pointer
251
252         ldy     #EM_COPY::OFFS
253         lda     (ptr3),y
254         sta     ptr2
255         ldy     #EM_COPY::PAGE
256         lda     (ptr3),y
257         clc
258         adc     #>BASE
259         sta     ptr2+1                  ; To
260
261         ldy     #EM_COPY::BUF
262         lda     (ptr3),y
263         sta     ptr1
264         iny
265         lda     (ptr3),y
266         sta     ptr1+1                  ; From
267
268         jmp     common
269
270