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