]> git.sur5r.net Git - cc65/blob - libsrc/apple2/apple2-auxmem.s
7a72cfc8577195ec2bc6fb80e75c2149042742c4
[cc65] / libsrc / apple2 / apple2-auxmem.s
1 ;
2 ; Extended memory driver for the Apple II auxiliary memory
3 ;
4 ; Stefan Haubenthal, 2003-12-12
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 ; Header. Includes jump table
15
16 .segment        "JUMPTABLE"
17
18 ; Driver signature
19
20         .byte   $65, $6d, $64           ; "emd"
21         .byte   EMD_API_VERSION         ; EM API version number
22
23 ; Jump table.
24
25         .word   INSTALL
26         .word   DEINSTALL
27         .word   PAGECOUNT
28         .word   MAP
29         .word   USE
30         .word   COMMIT
31         .word   COPYFROM
32         .word   COPYTO
33
34 ; ------------------------------------------------------------------------
35 ; Constants
36
37 BASE    = $0200
38 AUXCARD = $C30C                         ; Card signature
39 AUXMOVE = $C311                         ; Auxiliary move routine
40 PAGES   = ($C000 - BASE) / 256
41
42 ; ------------------------------------------------------------------------
43 ; Data.
44
45 .data
46 curpage:        .byte   $FF             ; Current page number (invalid)
47
48 .bss
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     #0
62         lda     AUXCARD
63         and     #$f0
64         cmp     #$80
65         bne     @L1
66         lda     #EM_ERR_OK
67         rts
68 @L1:    lda     #EM_ERR_NO_DEVICE
69 ;       rts
70
71 ; ------------------------------------------------------------------------
72 ; DEINSTALL routine. Is called before the driver is removed from memory.
73 ; Can do cleanup or whatever. Must not return anything.
74 ;
75
76 DEINSTALL:
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         clc                             ; Direction flag
111         jsr     transfer                ; Transfer one page
112
113 ; Return the memory window
114
115         lda     #<window
116         ldx     #>window                ; Return the window address
117
118 ; Done
119
120 done:   rts
121
122 ; ------------------------------------------------------------------------
123 ; USE: Tell the driver that the window is now associated with a given page.
124
125 USE:    sta     curpage                 ; Remember the page
126         lda     #<window
127         ldx     #>window                ; Return the window
128         rts
129
130 ; ------------------------------------------------------------------------
131 ; COMMIT: Commit changes in the memory window to extended storage.
132
133 COMMIT: lda     curpage                 ; Get the current page
134         cmp     #$FF
135         beq     done                    ; Jump if no page mapped
136
137         clc
138         adc     #>BASE
139         sta     ptr2+1
140         ldy     #$00
141         sty     ptr2
142
143         lda     #<window
144         sta     ptr1
145         lda     #>window
146         sta     ptr1+1
147         lda     #<$FF
148         sta     ptr4
149         lda     #>$FF
150         sta     ptr4+1
151         sec                             ; Direction flag
152
153 ; Transfer one page/all bytes
154
155 transfer:
156         php
157         clc
158         lda     ptr1
159         sta     $3C
160         adc     ptr4
161         sta     $3E
162         lda     ptr1+1
163         sta     $3D
164         adc     ptr4+1
165         sta     $3F
166         lda     ptr2
167         sta     $42
168         lda     ptr2+1
169         sta     $43
170         plp
171         jmp     AUXMOVE
172
173 ; ------------------------------------------------------------------------
174 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
175 ; describing the request is passed in a/x.
176 ; The function must not return anything.
177 ;
178
179 COPYFROM:
180         sta     ptr3
181         stx     ptr3+1                  ; Save the passed em_copy pointer
182
183         ldy     #EM_COPY::OFFS
184         lda     (ptr3),y
185         sta     ptr1
186         ldy     #EM_COPY::PAGE
187         lda     (ptr3),y
188         clc
189         adc     #>BASE
190         sta     ptr1+1                  ; From
191
192         ldy     #EM_COPY::BUF
193         lda     (ptr3),y
194         sta     ptr2
195         iny
196         lda     (ptr3),y
197         sta     ptr2+1                  ; To
198         clc                             ; Direction flag
199
200 common: ldy     #EM_COPY::COUNT
201         lda     (ptr3),y                ; Get bytes in last page
202         sta     ptr4
203         iny
204         lda     (ptr3),y                ; Get number of pages
205         sta     ptr4+1
206
207         jmp     transfer
208
209 ; ------------------------------------------------------------------------
210 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
211 ; describing the request is passed in a/x.
212 ; The function must not return anything.
213 ;
214
215 COPYTO: sta     ptr3
216         stx     ptr3+1                  ; Save the passed em_copy pointer
217
218         ldy     #EM_COPY::OFFS
219         lda     (ptr3),y
220         sta     ptr2
221         ldy     #EM_COPY::PAGE
222         lda     (ptr3),y
223         clc
224         adc     #>BASE
225         sta     ptr2+1                  ; To
226
227         ldy     #EM_COPY::BUF
228         lda     (ptr3),y
229         sta     ptr1
230         iny
231         lda     (ptr3),y
232         sta     ptr1+1                  ; From
233
234         sec                             ; Direction flag
235         jmp     common