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