]> git.sur5r.net Git - cc65/blob - c64-reu.s
2c8238078a74a5e4be973146ecd25e0b2e2ca598
[cc65] / c64-reu.s
1 ;
2 ; Extended memory driver for the Commodore REU
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 REU_STATUS      = $DF00                 ; Status register
41 REU_COMMAND     = $DF01                 ; Command register
42 REU_C64ADDR     = $DF02                 ; C64 base address register
43 REU_REUADDR     = $DF04                 ; REU base address register
44 REU_COUNT       = $DF07                 ; Transfer count register
45 REU_IRQMASK     = $DF09                 ; IRQ mask register
46 REU_CONTROL     = $DF0A                 ; Control register
47 REU_TRIGGER     = $FF00                 ; REU command trigger
48
49 OP_COPYFROM     = $ED
50 OP_COPYTO       = $EC
51
52
53 ; ------------------------------------------------------------------------
54 ; Data.
55
56 .data
57 pagecount:      .res    2               ; Number of pages available
58 curpage:        .word   $FFFF           ; Current page number (invalid)
59
60 .bss
61 window:         .res    256             ; Memory "window"
62
63 reu_params:     .word   $0000           ; Host address, lo, hi
64                 .word   $0000           ; Exp  address, lo, hi
65                 .byte   $00             ; Expansion  bank no.
66                 .word   $0000           ; # bytes to move, lo, hi
67                 .byte   $00             ; Interrupt mask reg.
68                 .byte   $00             ; Adress control reg.
69
70 .code
71
72 ; ------------------------------------------------------------------------
73 ; INSTALL routine. Is called after the driver is loaded into memory. If
74 ; possible, check if the hardware is present and determine the amount of
75 ; memory available.
76 ; Must return an EM_ERR_xx code in a/x.
77 ;
78
79 INSTALL:
80         lda     #$55
81         sta     REU_REUADDR
82         cmp     REU_REUADDR             ; Check for presence of REU
83         bne     nodevice
84         lda     #$AA
85         sta     REU_REUADDR
86         cmp     REU_REUADDR             ; Check for presence of REU
87         bne     nodevice
88
89         ldx     #>(128*4)               ; Assume 128KB
90         lda     REU_STATUS
91         and     #$10                    ; Check size bit
92         beq     @L1
93         ldx     #>(256*4)               ; 256KB when size bit is set
94 @L1:    stx     pagecount+1
95
96         lda     #<EM_ERR_OK
97         ldx     #>EM_ERR_OK
98         rts
99
100 ; No REU found
101
102 nodevice:
103         lda     #<EM_ERR_NO_DEVICE
104         ldx     #>EM_ERR_NO_DEVICE
105         rts
106
107 ; ------------------------------------------------------------------------
108 ; DEINSTALL routine. Is called before the driver is removed from memory.
109 ; Can do cleanup or whatever. Must not return anything.
110 ;
111
112 DEINSTALL:
113         rts
114
115
116 ; ------------------------------------------------------------------------
117 ; PAGECOUNT: Return the total number of available pages in a/x.
118 ;
119
120 PAGECOUNT:
121         lda     pagecount
122         ldx     pagecount+1
123         rts
124
125 ; ------------------------------------------------------------------------
126 ; MAP: Map the page in a/x into memory and return a pointer to the page in
127 ; a/x.  The contents of the currently mapped page (if any) may be discarded
128 ; by the driver.
129 ;
130
131 MAP:    sta     curpage
132         stx     curpage+1               ; Remember the new page
133
134         ldy     #OP_COPYFROM
135         jsr     common                  ; Copy the window
136
137         lda     #<window
138         ldx     #>window                ; Return the window address
139 done:   rts
140
141 ; ------------------------------------------------------------------------
142 ; USE: Tell the driver that the window is now associated with a given page.
143
144 USE:    sta     curpage
145         stx     curpage+1               ; Remember the page
146         lda     #<window
147         ldx     #>window                ; Return the window
148         rts
149
150 ; ------------------------------------------------------------------------
151 ; COMMIT: Commit changes in the memory window to extended storage.
152
153 COMMIT: lda     curpage
154         ldx     curpage+1               ; Do we have a page mapped?
155         bmi     done                    ; Jump if no page mapped
156
157         ldy     #OP_COPYTO
158 common: sty     tmp1
159
160         ldy     #<window
161         sty     REU_C64ADDR
162         ldy     #>window
163         sty     REU_C64ADDR+1
164
165         ldy     #0
166         sty     REU_REUADDR+0
167         sta     REU_REUADDR+1
168         stx     REU_REUADDR+2
169
170         sty     REU_COUNT+0
171         ldy     #1
172         sty     REU_COUNT+1             ; Move 256 bytes
173         bne     transfer1               ; Transfer 256 bytes into REU
174
175 ; ------------------------------------------------------------------------
176 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
177 ; describing the request is passed in a/x.
178 ; The function must not return anything.
179 ;
180
181 COPYFROM:
182         ldy     #OP_COPYFROM
183         .byte   $2C
184
185 ; ------------------------------------------------------------------------
186 ; COPYTO: Copy from linear into extended memory. A pointer to a structure
187 ; describing the request is passed in a/x.
188 ; The function must not return anything.
189 ;
190
191 COPYTO:
192         ldy     #OP_COPYTO
193         sty     tmp1
194
195 ; Remember the passed pointer
196
197         sta     ptr1
198         stx     ptr1+1          ; Save the pointer
199
200 ; The structure passed to the functions has the same layout as the registers
201 ; of the Commodore REU, so register programming is easy.
202
203         ldy     #7-1
204 @L1:    lda     (ptr1),y
205         sta     REU_C64ADDR,y
206         dey
207         bpl     @L1
208
209 ; Invalidate the page in the memory window
210
211         sty     curpage+1       ; Y = $FF
212
213 ; Reload the REU command and start the transfer
214
215 transfer1:
216         ldy     tmp1
217
218 ; Transfer subroutine for the REU. Expects command in Y.
219
220 transfer:
221         sty     REU_COMMAND     ; Issue command
222
223         ldy     $01             ; Save the value of the c64 control port...
224         tya                     ;
225         and     #$F8            ; Disable ROMs and I/O.
226         sei                     ;
227         sta     $01
228         lda     REU_TRIGGER     ; Don't change $FF00
229         sta     REU_TRIGGER     ; Start the transfer...
230
231         sty     $01             ; Restore the old configuration
232         cli
233         rts
234