]> git.sur5r.net Git - cc65/blob - libsrc/apple2/emd/a2.auxmem.s
Merge pull request #114 from greg-king5/phantom
[cc65] / libsrc / apple2 / emd / a2.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        "HEADER"
17
18 ; Driver signature
19
20         .byte   $65, $6d, $64           ; "emd"
21         .byte   EMD_API_VERSION         ; EM API version number
22
23 ; Library reference
24
25         .addr   $0000
26
27 ; Jump table
28
29         .addr   INSTALL
30         .addr   UNINSTALL
31         .addr   PAGECOUNT
32         .addr   MAP
33         .addr   USE
34         .addr   COMMIT
35         .addr   COPYFROM
36         .addr   COPYTO
37
38 ; ------------------------------------------------------------------------
39 ; Constants
40
41 BASE    = $0200
42 AUXCARD = $C30C                         ; Card signature
43 AUXMOVE = $C311                         ; Auxiliary move routine
44 PAGES   = ($C000 - BASE) / 256
45
46 ; ------------------------------------------------------------------------
47 ; Data.
48
49 .data
50 curpage:        .byte   $FF             ; Current page number (invalid)
51
52 .bss
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     #0
66         lda     AUXCARD
67         and     #$f0
68         cmp     #$80
69         bne     @L1
70         lda     #EM_ERR_OK
71         rts
72 @L1:    lda     #EM_ERR_NO_DEVICE
73 ;       rts
74
75 ; ------------------------------------------------------------------------
76 ; UNINSTALL routine. Is called before the driver is removed from memory.
77 ; Can do cleanup or whatever. Must not return anything.
78 ;
79
80 UNINSTALL:
81         rts
82
83
84 ; ------------------------------------------------------------------------
85 ; PAGECOUNT: Return the total number of available pages in a/x.
86 ;
87
88 PAGECOUNT:
89         lda     #<PAGES
90         ldx     #>PAGES
91         rts
92
93 ; ------------------------------------------------------------------------
94 ; MAP: Map the page in a/x into memory and return a pointer to the page in
95 ; a/x. The contents of the currently mapped page (if any) may be discarded
96 ; by the driver.
97 ;
98
99 MAP:    sta     curpage                 ; Remember the new page
100
101         clc
102         adc     #>BASE
103         sta     ptr1+1
104         ldy     #$00
105         sty     ptr1
106
107         lda     #<window
108         sta     ptr2
109         lda     #>window
110         sta     ptr2+1
111
112 ; Transfer one page
113
114         clc                             ; Direction flag
115         jsr     transfer                ; Transfer one page
116
117 ; Return the memory window
118
119         lda     #<window
120         ldx     #>window                ; Return the window address
121
122 ; Done
123
124 done:   rts
125
126 ; ------------------------------------------------------------------------
127 ; USE: Tell the driver that the window is now associated with a given page.
128
129 USE:    sta     curpage                 ; Remember the page
130         lda     #<window
131         ldx     #>window                ; Return the window
132         rts
133
134 ; ------------------------------------------------------------------------
135 ; COMMIT: Commit changes in the memory window to extended storage.
136
137 COMMIT: lda     curpage                 ; Get the current page
138         cmp     #$FF
139         beq     done                    ; Jump if no page mapped
140
141         clc
142         adc     #>BASE
143         sta     ptr2+1
144         ldy     #$00
145         sty     ptr2
146
147         lda     #<window
148         sta     ptr1
149         lda     #>window
150         sta     ptr1+1
151         lda     #<$FF
152         sta     ptr4
153         lda     #>$FF
154         sta     ptr4+1
155         sec                             ; Direction flag
156
157 ; Transfer one page/all bytes
158
159 transfer:
160         php
161         clc
162         lda     ptr1
163         sta     $3C
164         adc     ptr4
165         sta     $3E
166         lda     ptr1+1
167         sta     $3D
168         adc     ptr4+1
169         sta     $3F
170         lda     ptr2
171         sta     $42
172         lda     ptr2+1
173         sta     $43
174         plp
175         jmp     AUXMOVE
176
177 ; ------------------------------------------------------------------------
178 ; COPYFROM: Copy from extended into linear 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 COPYFROM:
184         sta     ptr3
185         stx     ptr3+1                  ; Save the passed em_copy pointer
186
187         ldy     #EM_COPY::OFFS
188         lda     (ptr3),y
189         sta     ptr1
190         ldy     #EM_COPY::PAGE
191         lda     (ptr3),y
192         clc
193         adc     #>BASE
194         sta     ptr1+1                  ; From
195
196         ldy     #EM_COPY::BUF
197         lda     (ptr3),y
198         sta     ptr2
199         iny
200         lda     (ptr3),y
201         sta     ptr2+1                  ; To
202         clc                             ; Direction flag
203
204 common: ldy     #EM_COPY::COUNT
205         lda     (ptr3),y                ; Get bytes in last page
206         sta     ptr4
207         iny
208         lda     (ptr3),y                ; Get number of pages
209         sta     ptr4+1
210
211         jmp     transfer
212
213 ; ------------------------------------------------------------------------
214 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
215 ; describing the request is passed in a/x.
216 ; The function must not return anything.
217 ;
218
219 COPYTO: sta     ptr3
220         stx     ptr3+1                  ; Save the passed em_copy pointer
221
222         ldy     #EM_COPY::OFFS
223         lda     (ptr3),y
224         sta     ptr2
225         ldy     #EM_COPY::PAGE
226         lda     (ptr3),y
227         clc
228         adc     #>BASE
229         sta     ptr2+1                  ; To
230
231         ldy     #EM_COPY::BUF
232         lda     (ptr3),y
233         sta     ptr1
234         iny
235         lda     (ptr3),y
236         sta     ptr1+1                  ; From
237
238         sec                             ; Direction flag
239         jmp     common