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