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