]> git.sur5r.net Git - cc65/blob - libsrc/c128/emd/c128-ramcart.s
Fixed _textcolor definition.
[cc65] / libsrc / c128 / emd / c128-ramcart.s
1 ;
2 ; Extended memory driver for the RamCart 64/128KB cartridge. Driver works
3 ; without problems when statically linked.
4 ; Code is based on GEORAM code by Ullrich von Bassewitz.
5 ; Maciej 'YTM/Elysium' Witkowiak <ytm@elysium.pl>
6 ; 06,22.12.2002
7 ;
8
9
10         .include        "zeropage.inc"
11
12         .include        "em-kernel.inc"
13         .include        "em-error.inc"
14
15         .macpack        generic
16         .macpack        module
17
18
19 ; ------------------------------------------------------------------------
20 ; Header. Includes jump table
21
22         module_header   _c128_ramcart_emd
23
24 ; Driver signature
25
26         .byte   $65, $6d, $64           ; "emd"
27         .byte   EMD_API_VERSION         ; EM API version number
28
29 ; Library reference
30
31         .addr   $0000
32
33 ; Jump table
34
35         .addr   INSTALL
36         .addr   UNINSTALL
37         .addr   PAGECOUNT
38         .addr   MAP
39         .addr   USE
40         .addr   COMMIT
41         .addr   COPYFROM
42         .addr   COPYTO
43
44 ; ------------------------------------------------------------------------
45 ; Constants
46
47 RAMC_WINDOW       = $DF00               ; Address of RamCart window
48 RAMC_PAGE_LO      = $DE00               ; Page register low
49 RAMC_PAGE_HI      = $DE01               ; Page register high (only for RC128)
50
51 ; ------------------------------------------------------------------------
52 ; Data.
53
54 .bss
55
56 pagecount:      .res    2               ; Number of pages available
57
58 .code
59
60 ; ------------------------------------------------------------------------
61 ; INSTALL routine. Is called after the driver is loaded into memory. If
62 ; possible, check if the hardware is present and determine the amount of
63 ; memory available.
64 ; Must return an EM_ERR_xx code in a/x.
65 ;
66
67 INSTALL:
68         ldx     RAMC_WINDOW
69         cpx     RAMC_WINDOW
70         bne     @notpresent
71
72         lda     #0
73         sta     RAMC_PAGE_LO
74         sta     RAMC_PAGE_HI
75         ldx     RAMC_WINDOW
76         cpx     RAMC_WINDOW
77         bne     @notpresent
78         lda     #2
79         sta     RAMC_WINDOW
80         cmp     RAMC_WINDOW
81         beq     @cont
82         cpx     RAMC_WINDOW
83         beq     @readonly
84 @cont:  ldy     #1
85         sty     RAMC_PAGE_HI
86         sty     RAMC_WINDOW
87         dey
88         sty     RAMC_PAGE_HI
89         iny
90         cpy     RAMC_WINDOW
91         beq     @rc64
92         ; we're on rc128
93         ldx     #>512
94         bne     @setsize
95 @rc64:  ldx     #>256
96 @setsize:
97         lda     #0
98         sta     pagecount
99         stx     pagecount+1
100         lda     #<EM_ERR_OK
101         ldx     #>EM_ERR_OK
102         rts
103 @notpresent:
104 @readonly:
105         lda     #<EM_ERR_NO_DEVICE
106         ldx     #>EM_ERR_NO_DEVICE
107 ;       rts                             ; Run into UNINSTALL instead
108
109 ; ------------------------------------------------------------------------
110 ; UNINSTALL routine. Is called before the driver is removed from memory.
111 ; Can do cleanup or whatever. Must not return anything.
112 ;
113
114 UNINSTALL:
115         rts
116
117
118 ; ------------------------------------------------------------------------
119 ; PAGECOUNT: Return the total number of available pages in a/x.
120 ;
121
122 PAGECOUNT:
123         lda     pagecount
124         ldx     pagecount+1
125         rts
126
127 ; ------------------------------------------------------------------------
128 ; USE: Tell the driver that the window is now associated with a given page.
129 ; The RamCart cartridge does not copy but actually map the window, so USE is
130 ; identical to MAP.
131
132 USE     = MAP
133
134
135 ; ------------------------------------------------------------------------
136 ; MAP: Map the page in a/x into memory and return a pointer to the page in
137 ; a/x. The contents of the currently mapped page (if any) may be discarded
138 ; by the driver.
139 ;
140
141 MAP:    sta     RAMC_PAGE_LO
142         stx     RAMC_PAGE_HI
143         lda     #<RAMC_WINDOW
144         ldx     #>RAMC_WINDOW
145
146 ; Use the RTS from COMMIT below to save a precious byte of storage
147
148 ; ------------------------------------------------------------------------
149 ; COMMIT: Commit changes in the memory window to extended storage.
150
151 COMMIT: rts
152
153 ; ------------------------------------------------------------------------
154 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
155 ; describing the request is passed in a/x.
156 ; The function must not return anything.
157 ;
158
159 COPYFROM:
160         jsr     setup
161
162 ; Setup is:
163 ;
164 ;   - ptr1 contains the struct pointer
165 ;   - ptr2 contains the linear memory buffer
166 ;   - ptr3 contains -(count-1)
167 ;   - tmp1 contains the low page register value
168 ;   - tmp2 contains the high page register value
169 ;   - X contains the page offset
170 ;   - Y contains zero
171
172         jmp     @L5
173
174 @L1:    lda     RAMC_WINDOW,x
175         sta     (ptr2),y
176         iny
177         bne     @L2
178         inc     ptr2+1
179 @L2:    inx
180         beq     @L4
181
182 ; Bump count and repeat
183
184 @L3:    inc     ptr3
185         bne     @L1
186         inc     ptr3+1
187         bne     @L1
188         rts
189
190 ; Bump page register
191
192 @L4:    inc     tmp1
193         bne     @L5
194         inc     tmp2
195 @L5:    lda     tmp1
196         sta     RAMC_PAGE_LO
197         lda     tmp2
198         sta     RAMC_PAGE_HI
199         jmp     @L3
200
201 ; ------------------------------------------------------------------------
202 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
203 ; describing the request is passed in a/x.
204 ; The function must not return anything.
205 ;
206
207 COPYTO:
208         jsr     setup
209
210 ; Setup is:
211 ;
212 ;   - ptr1 contains the struct pointer
213 ;   - ptr2 contains the linear memory buffer
214 ;   - ptr3 contains -(count-1)
215 ;   - tmp1 contains the low page register value
216 ;   - tmp2 contains the high page register value
217 ;   - X contains the page offset
218 ;   - Y contains zero
219
220         jmp     @L5
221
222 @L1:    lda     (ptr2),y
223         sta     RAMC_WINDOW,x
224         iny
225         bne     @L2
226         inc     ptr2+1
227 @L2:    inx
228         beq     @L4
229
230 ; Bump count and repeat
231
232 @L3:    inc     ptr3
233         bne     @L1
234         inc     ptr3+1
235         bne     @L1
236         rts
237
238 ; Bump page register
239
240 @L4:    inc     tmp1
241         bne     @L5
242         inc     tmp2
243 @L5:    lda     tmp1
244         sta     RAMC_PAGE_LO
245         lda     tmp2
246         sta     RAMC_PAGE_HI
247         jmp     @L3
248
249 ; ------------------------------------------------------------------------
250 ; Helper function for COPYFROM and COPYTO: Store the pointer to the request
251 ; structure and prepare data for the copy
252
253 setup:  sta     ptr1
254         stx     ptr1+1          ; Save passed pointer
255
256 ; Get the page number from the struct and adjust it so that it may be used
257 ; with the hardware. That is: lower 6 bits in tmp1, high bits in tmp2.
258
259         ldy     #EM_COPY::PAGE+1
260         lda     (ptr1),y
261         sta     tmp2
262         dey
263         lda     (ptr1),y
264         sta     tmp1
265
266 ; Get the buffer pointer into ptr2
267
268         ldy     #EM_COPY::BUF
269         lda     (ptr1),y
270         sta     ptr2
271         iny
272         lda     (ptr1),y
273         sta     ptr2+1
274
275 ; Get the count, calculate -(count-1) and store it into ptr3
276
277         ldy     #EM_COPY::COUNT
278         lda     (ptr1),y
279         eor     #$FF
280         sta     ptr3
281         iny
282         lda     (ptr1),y
283         eor     #$FF
284         sta     ptr3+1
285
286 ; Get the page offset into X and clear Y
287
288         ldy     #EM_COPY::OFFS
289         lda     (ptr1),y
290         tax
291         ldy     #$00
292
293 ; Done
294
295         rts
296