-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMMC_Pi1MHz.asm
284 lines (247 loc) · 5.16 KB
/
MMC_Pi1MHz.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
\** MMFS ROM by Martin Mather
\** Compiled using BeebAsm V1.04
\** June/July 2011
\\ FRED RAM MMC DEVICE
discaccess = &FCA6
; not used
;byteCounter% = MA+&10FF
;lowByteStore% = MA+&10FE
\\ RESET DEVICE
.MMC_DEVICE_RESET
; setup discaccess buffer in FRED.
; X = 255 on entry
; setup pointer to the command buffer
LDA #0 : STA discaccess
LDY #&F0 : STY discaccess+1
LDX #&FF : STX discaccess+2
; setup read
STA discaccess+3
STA discaccess+3
STA discaccess+3
STA discaccess+3
; setup buffer address we only need 512 bytes
STA discaccess+3
LDY #&E0 : STY discaccess+3
: STX discaccess+3
: STA discaccess+3
LDY #12 : STY discaccess
; setup number of sectors to read or write ( 1 )
LDY #1
STY discaccess+3
STA discaccess+3
STA discaccess+3
STA discaccess+3
; return A=0
RTS
\\ only used during init sequence
\\ mainly for sdhc2 cards and for GetIDCRC so we can just return a constant
;.MMC_GetByte
; LDA #&40 ; this fakes sdhc return type
\\ 16 Clocks is used to skip CRC usually, we don't have CRC so we can ignore
.MMC_16Clocks
\\ Slow clocks is only used during INIT so we can safely ignore this call as well
;.MMC_SlowClocks
RTS
\\ MMC Clocks is used to skip bytes in sector reads so we need to handle this correctly
\\ most of the time Y=0 ie 256 bytes
\\ entry Z flag is setup correctly based on Y
.MMC_Clocks
{
BEQ skip_sector
.count
LDA discaccess+3
DEY
BNE count
RTS ; Don't need to do anything timing wise
.skip_sector
}
\\ **** Read 256 bytes to buffer ****
\\ Don't bother, we can simply skip the first page
.MMC_ReadBuffer
\\ **** Write 256 bytes from buffer ****
\\ Like reading we can simply ignore the 256 bytes we would need to buffer in JIM RAM
.MMC_WriteBuffer
LDA #&E1 : STA discaccess+1
RTS
\\ *** Send command to MMC ***
\\ On exit A=result, Z=result=0
.MMC_DoCommand
{
lda cmdseq%+1
CMP #read_single_block ; Read command
BNE notareadcommand
.setup_read_buffer
; C is set
LDA #0 ; set up read sector A= 0 ( read sector command)
.setup_buffer
LDX #0 : STX discaccess
LDY #&F0 : STY discaccess+1
LDX #&FF : STX discaccess+2
STA discaccess+3 ; setup sector commmand
; setup pointer to LBA in command buffer
LDX #8 : STX discaccess
; Set sector address
LDX cmdseq%+5 : STX discaccess+3 ; Low byte of command sector address
LDX cmdseq%+4 : STX discaccess+3
LDX cmdseq%+3 : STX discaccess+3
LDX cmdseq%+2 : STX discaccess+3 ; High Byte of sector
BCC skip
STY discaccess+4 ; start read sector
.skip
; setup pointer to buffer
LDX #&E0 : STX discaccess+1
LDA #&0 : STA discaccess
.MMC_send_cid
; Pretend we are an SD card
.MMC_send_op
.MMC_set_blk_size
.MMC_return
LDA #0
; Potentially do debug output
; Debug the command sent to the pretend MMC
.dbgMmc
IF _DEBUG_MMC
{
PHP
PHA
LDY #0
.loop
LDA cmdseq%,Y
JSR PrintHex
INY
CPY #7
BNE loop
LDA #':'
JSR OSWRCH
PLA
PHA
JSR PrintHex
JSR OSNEWL
PLA
PLP
}
ENDIF
; Done
RTS
.notareadcommand
; CMP #write_block ; Write command
; BNE setup_commands
LDA #1
CLC
BCC setup_buffer
;.setup_commands
; These are only used during setup
; CMP #go_idle_state : BEQ MMC_go_idle ; Go to Idle command
; CMP #&48 : BEQ MMC_checksdhc ; Check card type
; CMP #send_op_cond : BEQ MMC_send_op ; Initialise command
; CMP #set_blklen : BEQ MMC_set_blk_size ; Set block length
; CMP #send_cid : BEQ MMC_send_cid ; Send CID ; Treat as NOP
; These command are used to check for sdhc
; CMP #&77 : BEQ MMC_return
; CMP #&69 : BEQ MMC_return
; CMP #&7A : BEQ MMC_return
; LDA #4 ; Invalid command response
; BNE dbgMmc
; Fake sdhc
;.MMC_checksdhc
; Pretend that 95 command returned 1
;.MMC_go_idle
; LDA #1
; BNE dbgMmc ; Potentially do debug output
}
\\ **** Complete Write Operation *****
.MMC_EndWrite
LDA #&F0 : STA discaccess+4 ; start write sector
; wait for write to complete
NOP
\\ Wait for sector to be complete
\\ for CIDCRC as the command is dropped above it is already complete
.MMC_WaitForData
{
.loop
LDA discaccess+4
BMI loop
; setup pointer to buffer
LDA #&E0 : STA discaccess+1
LDA #0 : STA discaccess
RTS
}
\\ *** Read 256 bytes to datptr ***
.MMC_Read256
{
LDY TubeNoTransferIf0
BNE rdlT20
.rdl1
LDA discaccess+3
STA (datptr%),Y
INY
BNE rdl1
RTS
.rdlT20
LDX #0
BEQ rdlT2
}
\\ *** Read "byteslastsector" bytes
\\ to datptr ***
.MMC_ReadBLS
{
LDX byteslastsec%
LDY TubeNoTransferIf0
BNE rdlT1
.rdl2
LDA discaccess+3
STA (datptr%),Y
INY
DEX
BNE rdl2
RTS
}
\\ TUBE
\\ 24us delay=48 cycles
\ \ (7)
.rdlT1
.rdlT2
{
.rdlT3
LDA discaccess+3 ; 5
STA TUBE_R3_DATA ; 5
JSR donothing16cycles ; 6+6+4
JSR donothing16cycles ; 6+6+4
NOP ; 2
DEX ; 2
BNE rdlT3 ; 3
RTS
}
.donothing16cycles
NOP
.donothing14cycles
NOP
.MMC_SendingData
.donothing
{
RTS
}
\\ **** Write 256 bytes from dataptr% ****
.MMC_Write256
{
LDY TubeNoTransferIf0
BNE wrT1
.wr1
LDA (datptr%),Y
STA discaccess+3
INY
BNE wr1
RTS
\.wrT1 ; To tube 24us delay
.wrT1
LDY #0
.wrT2
LDA TUBE_R3_DATA
STA discaccess+3
JSR donothing16cycles
JSR donothing16cycles
NOP
INY
BNE wrT2
RTS
}