]> git.sur5r.net Git - cc65/blob - libsrc/c64/c64-georam.s
Added em_use
[cc65] / libsrc / c64 / c64-georam.s
1 ;
2 ; Extended memory driver for the GEORAM cartridge
3 ;
4 ; Ullrich von Bassewitz, 2002-11-29
5 ;
6
7         .include        "zeropage.inc"
8
9         .include        "em-kernel.inc"
10         .include        "em-error.inc"
11
12
13         .macpack        generic
14
15
16 ; ------------------------------------------------------------------------
17 ; Header. Includes jump table
18
19 .segment        "JUMPTABLE"
20
21 ; Driver signature
22
23         .byte   $65, $6d, $64           ; "emd"
24         .byte   $00                     ; EM API version number
25
26 ; Jump table.
27
28         .word   INSTALL
29         .word   DEINSTALL
30         .word   PAGECOUNT
31         .word   MAP
32         .word   USE
33         .word   COMMIT
34         .word   COPYFROM
35         .word   COPYTO
36
37 ; ------------------------------------------------------------------------
38 ; Constants
39
40 GR_WINDOW       = $DE00                 ; Address of GEORAM window
41 GR_PAGE_LO      = $DFFE                 ; Page register low
42 GR_PAGE_HI      = $DFFF                 ; Page register high
43
44 ; ------------------------------------------------------------------------
45 ; Data.
46
47 .data
48
49 pagecount:      .word   2048            ; Currently fixed
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         lda     #<EM_ERR_OK
62         ldx     #>EM_ERR_OK
63         rts
64
65 ; ------------------------------------------------------------------------
66 ; DEINSTALL routine. Is called before the driver is removed from memory.
67 ; Can do cleanup or whatever. Must not return anything.
68 ;
69
70 DEINSTALL:
71         rts
72
73
74 ; ------------------------------------------------------------------------
75 ; PAGECOUNT: Return the total number of available pages in a/x.
76 ;
77
78 PAGECOUNT:
79         lda     pagecount
80         ldx     pagecount+1
81         rts
82
83 ; ------------------------------------------------------------------------
84 ; USE: Tell the driver that the window is now associated with a given page.
85 ; The GeoRAM cartridge does not copy but actually map the window, so USE is
86 ; identical to MAP.
87
88 USE     = MAP
89
90 ; ------------------------------------------------------------------------
91 ; MAP: Map the page in a/x into memory and return a pointer to the page in
92 ; a/x. The contents of the currently mapped page (if any) may be discarded
93 ; by the driver.
94 ;
95
96 MAP:    sta     tmp1
97         txa
98         asl     tmp1
99         rol     a
100         asl     tmp1
101         rol     a
102
103         sta     GR_PAGE_HI
104         lda     tmp1
105         lsr     a
106         lsr     a
107         sta     GR_PAGE_LO
108
109         lda     #<GR_WINDOW
110         ldx     #>GR_WINDOW
111
112 ; Use the RTS from COMMIT below to save a precious byte of storage
113
114 ; ------------------------------------------------------------------------
115 ; COMMIT: Commit changes in the memory window to extended storage.
116
117 COMMIT: rts
118
119 ; ------------------------------------------------------------------------
120 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
121 ; describing the request is passed in a/x.
122 ; The function must not return anything.
123 ;
124
125 COPYFROM:
126         jsr     setup
127
128 ; Setup is:
129 ;
130 ;   - ptr1 contains the struct pointer
131 ;   - ptr2 contains the linear memory buffer
132 ;   - ptr3 contains -(count-1)
133 ;   - tmp1 contains the low page register value
134 ;   - tmp2 contains the high page register value
135 ;   - X contains the page offset
136 ;   - Y contains zero
137
138         jmp     @L5
139
140 @L1:    lda     GR_WINDOW,x
141         sta     (ptr2),y
142         iny
143         bne     @L2
144         inc     ptr2+1
145 @L2:    inx
146         beq     @L4
147
148 ; Bump count and repeat
149
150 @L3:    inc     ptr3
151         bne     @L1
152         inc     ptr3+1
153         bne     @L1
154         rts
155
156 ; Bump page register
157
158 @L4:    inc     tmp1            ; Bump low page register
159         bit     tmp1            ; Check for overflow in bit 6
160         bvc     @L6             ; Jump if no overflow
161         inc     tmp2
162 @L5:    lda     tmp2
163         sta     GR_PAGE_HI
164 @L6:    lda     tmp1
165         sta     GR_PAGE_LO
166         jmp     @L3
167
168 ; ------------------------------------------------------------------------
169 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
170 ; describing the request is passed in a/x.
171 ; The function must not return anything.
172 ;
173
174 COPYTO:
175         jsr     setup
176
177 ; Setup is:
178 ;
179 ;   - ptr1 contains the struct pointer
180 ;   - ptr2 contains the linear memory buffer
181 ;   - ptr3 contains -(count-1)
182 ;   - tmp1 contains the low page register value
183 ;   - tmp2 contains the high page register value
184 ;   - X contains the page offset
185 ;   - Y contains zero
186
187         jmp     @L5
188
189 @L1:    lda     (ptr2),y
190         sta     GR_WINDOW,x
191         iny
192         bne     @L2
193         inc     ptr2+1
194 @L2:    inx
195         beq     @L4
196
197 ; Bump count and repeat
198
199 @L3:    inc     ptr3
200         bne     @L1
201         inc     ptr3+1
202         bne     @L1
203         rts
204
205 ; Bump page register
206
207 @L4:    inc     tmp1            ; Bump low page register
208         bit     tmp1            ; Check for overflow in bit 6
209         bvc     @L6             ; Jump if no overflow
210         inc     tmp2
211 @L5:    lda     tmp2
212         sta     GR_PAGE_HI
213 @L6:    lda     tmp1
214         sta     GR_PAGE_LO
215         jmp     @L3
216
217 ; ------------------------------------------------------------------------
218 ; Helper function for COPYFROM and COPYTO: Store the pointer to the request
219 ; structure and prepare data for the copy
220
221 setup:  sta     ptr1
222         stx     ptr1+1          ; Save passed pointer
223
224 ; Get the page number from the struct and adjust it so that it may be used
225 ; with the hardware. That is: lower 6 bits in tmp1, high bits in tmp2.
226
227         ldy     #EM_COPY_PAGE+1
228         lda     (ptr1),y
229         sta     tmp2
230         dey
231         lda     (ptr1),y
232         asl     a
233         rol     tmp2
234         asl     a
235         rol     tmp2
236         lsr     a
237         lsr     a
238         sta     tmp1
239
240 ; Get the buffer pointer into ptr2
241
242         ldy     #EM_COPY_BUF
243         lda     (ptr1),y
244         sta     ptr2
245         iny
246         lda     (ptr1),y
247         sta     ptr2+1
248
249 ; Get the count, calculate -(count-1) and store it into ptr3
250
251         ldy     #EM_COPY_COUNT
252         lda     (ptr1),y
253         eor     #$FF
254         sta     ptr3
255         iny
256         lda     (ptr1),y
257         eor     #$FF
258         sta     ptr3+1
259
260 ; Get the page offset into X and clear Y
261
262         ldy     #EM_COPY_OFFS
263         lda     (ptr1),y
264         tax
265         ldy     #$00
266
267 ; Done
268
269         rts
270
271