]> git.sur5r.net Git - cc65/blob - libsrc/c128/c128-reu.s
C64 -> C128 conversion
[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   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) are assumed to be
128 ; dirty and must be saved into secondary storage if this is necessary.
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 ; COMMIT: Commit changes in the memory window to extended storage.
143
144 COMMIT: lda     curpage
145         ldx     curpage+1               ; Do we have a page mapped?
146         bmi     done                    ; Jump if no page mapped
147
148         ldy     #OP_COPYTO
149 common: sty     tmp1
150
151         ldy     #<window
152         sty     REU_C64ADDR
153         ldy     #>window
154         sty     REU_C64ADDR+1
155
156         ldy     #0
157         sty     REU_REUADDR+0
158         sta     REU_REUADDR+1
159         stx     REU_REUADDR+2
160
161         sty     REU_COUNT+0
162         ldy     #1
163         sty     REU_COUNT+1             ; Move 256 bytes
164         bne     transfer1               ; Transfer 256 bytes into REU
165
166 ; ------------------------------------------------------------------------
167 ; COPYFROM: Copy from extended into linear memory. A pointer to a structure
168 ; describing the request is passed in a/x.
169 ; The function must not return anything.
170 ;
171
172 COPYFROM:
173         ldy     #OP_COPYFROM
174         .byte   $2C
175
176 ; ------------------------------------------------------------------------
177 ; COPYTO: Copy from linear into extended 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 COPYTO:
183         ldy     #OP_COPYTO
184         sty     tmp1
185
186 ; Remember the passed pointer
187
188         sta     ptr1
189         stx     ptr1+1          ; Save the pointer
190
191 ; The structure passed to the functions has the same layout as the registers
192 ; of the Commodore REU, so register programming is easy.
193
194         ldy     #7-1
195 @L1:    lda     (ptr1),y
196         sta     REU_C64ADDR,y
197         dey
198         bpl     @L1
199
200 ; Invalidate the page in the memory window
201
202         sty     curpage+1       ; Y = $FF
203
204 ; Reload the REU command and start the transfer
205
206 transfer1:
207         ldy     tmp1
208
209 ; Transfer subroutine for the REU. Expects command in Y.
210
211 transfer:
212         sty     REU_COMMAND     ; Issue command
213
214         ldy     MMU_CR          ; Save the current MMU settings
215         lda     #MMU_CFG_RAM0   ; 
216         sei                     ;
217         sta     MMU_CR          ; Enable RAM in bank #0
218         lda     REU_TRIGGER     ; Don't change $FF00
219         sta     REU_TRIGGER     ; Start the transfer...
220
221         sty     MMU_CR          ; Restore the old configuration
222         cli
223         rts
224