-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMMC_UserPort.asm
556 lines (517 loc) · 12 KB
/
MMC_UserPort.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
\** MMFS ROM by Martin Mather
\** Compiled using BeebAsm V1.04
\** June/July 2011
\\ mmbeeb USER PORT MMC DEVICE
\ User VIA registers
iorb%=_VIA_BASE
ddrb%=_VIA_BASE + &02
sr% =_VIA_BASE + &0A
acr% =_VIA_BASE + &0B
ifr% =_VIA_BASE + &0D
ier% =_VIA_BASE + &0E
IF _TURBOMMC
temp = &cf
ddrmask = &1F \\ 0001 1111
msbits = &08 \\ 0000 1000
msmask = &E9 \\ 1110 1001
ELSE
ddrmask = &03 \\ 0000 0011
msbits = &00 \\ 0000 0000
msmask = &FD \\ 1111 1101
ENDIF
\\ Read byte (User Port)
\\ Write FF
.MMC_GetByte
.UP_ReadByteX
{
LDA #(3 + msbits) \\ Ensure CLK and MOSI both high
STA iorb%
JSR ShiftRegMode2
LDA #4
.wait
BIT ifr% \\ Bit 2 of IFR is the Shift Reg Interrupt flag
BEQ wait
JSR ShiftRegMode0
LDA sr%
RTS
}
\\ **** Send Data Token to card ****
.MMC_SendingData
{
JSR MMC_16Clocks
LDA #&FE
\\ Fall through to UP_WriteByte
}
\\ Write byte (User Port)
\\ Ignore byte in
.UP_WriteByte
{
IF _TURBOMMC
PHA
JSR ShiftRegMode6
PLA
STA sr%
LDA #4
.wait
BIT ifr%
BEQ wait
JMP ShiftRegMode6Exit
ELSE
ASL A
FOR N, 0, 7
ROL A
AND #msmask
STA iorb%
ORA #2
STA iorb%
NEXT
RTS
ENDIF
}
\\ *** Send &FF to MMC Y times ***
\\ Y=0=256
.MMC_16Clocks
LDY #2
.MMC_Clocks
.MMC_SlowClocks
{
.clku1
JSR UP_ReadByteX ; Writes &FF
DEY
BNE clku1
RTS ; A=SR, X=1, Y=0
}
\\ *** Send command to MMC ***
\\ On exit A=result, Z=result=0
.MMC_DoCommand
LDX #0
{
LDY #7
.dcmdu1
LDA cmdseq%,X
JSR UP_WriteByte
INX
DEY
BNE dcmdu1
\\ wait for response bit
\\ ie for clear bit (User Port only)
.waitresp_up
{
LDX #(1 + msbits)
LDY #0
.wrup
DEY
BEQ wrup_timeout
LDA #(3 + msbits)
STX iorb%
STA iorb%
LDA sr%
AND #1
BNE wrup
.wrup_timeout
LDA #(3 + msbits)
}
\\ This is always entered with X and A with the correct values
.UP_ReadBits7
STX iorb% ;\1
STA iorb%
STX iorb% ;\2
STA iorb%
STX iorb% ;\3
STA iorb%
STX iorb% ;\4
STA iorb%
STX iorb% ;\5
STA iorb%
STX iorb% ;\6
STA iorb%
STX iorb% ;\7
STA iorb%
LDA sr%
IF _DEBUG_MMC
PHP
PHA
LDY #0
.dcmdu2
LDA cmdseq%,Y
JSR PrintHex
INY
CPY #7
BNE dcmdu2
LDA #':'
JSR OSWRCH
PLA
PHA
JSR PrintHex
JSR OSNEWL
PLA
PLP
ELSE
ENDIF
RTS
}
\\ *** Wait for data token ***
.MMC_WaitForData
{
.wlu1
JSR UP_ReadByteX
CMP #&FE
BNE wlu1
RTS
}
\\ The read code below now operates in turbo mode on all hardware
\\ using shift register mode 2.
\\ *** Read 256 bytes to datptr ***
.MMC_Read256
LDX #0
BEQ MMC_ReadX
\\ *** Read "byteslastsector" bytes
\\ to datptr ***
.MMC_ReadBLS
LDX byteslastsec%
.MMC_ReadX
JSR ShiftRegMode2
LDY #0
LDA TubeNoTransferIf0
BNE MMC_ReadToTube
.MMC_ReadToMemory
JSR WaitForShiftDone
STA (datptr%),Y
INY
DEX
BNE MMC_ReadToMemory
RTS
.MMC_ReadToTube
{
JSR WaitForShiftDone
STA TUBE_R3_DATA
NOP
NOP
NOP
INY
DEX
BNE MMC_ReadToTube
.tube_delay
RTS
}
\\ **** Read 256 bytes to buffer ****
.MMC_ReadBuffer
LDX #&FF
STX CurrentCat
INX
JSR ShiftRegMode2
LDY #0
.rdbuf2
JSR WaitForShiftDone
STA buf%, Y
INY
DEX
BNE rdbuf2
RTS
\\ Wait for the shift reg to complete shifing, and return the value in A.
\\
\\ If this is the last byte, return to mode 0 before reading the shift reg.
\\
\\ This could be coded in fewer instructions, but it's done this way to minimise
\\ the time between testing for the interrupt flag, and reading the shift reg
\\ After reading the shift reg, the next byte will be ready 16us later
\\ Which gives us ~32 "free" instruction cycles
\\
\\ Here's the common path, starting with the read of the shift reg
\\ LDA sr% (A) 0
\\ RTS 6 6
\\ STA (datptr%),Y 6 12
\\ INY 2 14
\\ DEX 2 16
\\ BNE MMC_ReadToMemory 3 19
\\.MMC_ReadToMemory
\\ JSR WaitForShiftDone 6 25
\\ LDA #4 2 27
\\ CPX #1 2 29
\\ BEQ lastByte 2 31
\\.notLastByte
\\ BIT ifr% (B) 4 35 IFR should set again just in time
\\ BEQ notLastByte 2 37
\\ LDA sr% (A) 4 41
\\
\\ Note: the above does not account for 1MHz slow down on (A,B)
\\ because it's hard to tell empirically these add 1 or 2 cycles.
\\
\\ It turns out the (A) adds 1 cycle, and (B) adds 2 cycles, giving
\\ total of 44 cycles, or 22us per byte.
\\
\\ I tried optmising this further, by replacing:
\\ BIT ifr%
\\ BEQ notLastByte
\\ with NOPs.
\\
\\ with 3 NOPs, data transfer was reliable, and the code took 21us/byte:
\\
\\ LDA sr% (A) 0
\\ RTS 6 6
\\ STA (datptr%),Y 6 12
\\ INY 2 14
\\ DEX 2 16
\\ BNE MMC_ReadToMemory 3 19
\\.MMC_ReadToMemory
\\ JSR WaitForShiftDone 6 25
\\ LDA #4 2 27
\\ CPX #1 2 29
\\ BEQ lastByte 2 31
\\.notLastByte
\\ NOP 2 33
\\ NOP 2 35
\\ NOP 2 37
\\ LDA sr% (A) 4 41
\\
\\ In this situation (A) is stretched for 1 cycle, giving 42 cycles total.
\\
\\ with 2 NOPs, and data transfer still reliable
\\
\\ LDA sr% (A) 0
\\ RTS 6 6
\\ STA (datptr%),Y 6 12
\\ INY 2 14
\\ DEX 2 16
\\ BNE MMC_ReadToMemory 3 19
\\.MMC_ReadToMemory
\\ JSR WaitForShiftDone 6 25
\\ LDA #4 2 27
\\ CPX #1 2 29
\\ BEQ lastByte 2 31
\\.notLastByte
\\ NOP 2 33
\\ NOP 2 35
\\ LDA sr% (A) 4 39
\\
\\ In this situation (A) is stretched for 1 cycle, giving 40 cycles total.
\\
\\ with 1 NOPs, and data transfer still reliable
\\
\\ LDA sr% (A) 0
\\ RTS 6 6
\\ STA (datptr%),Y 6 12
\\ INY 2 14
\\ DEX 2 16
\\ BNE MMC_ReadToMemory 3 19
\\.MMC_ReadToMemory
\\ JSR WaitForShiftDone 6 25
\\ LDA #4 2 27
\\ CPX #1 2 29
\\ BEQ lastByte 2 31
\\.notLastByte
\\ NOP 2 33
\\ LDA sr% (A) 4 37
\\
\\ In this situation (A) is stretched for 1 cycle, giving 38 cycles total.
\\
\\ with 0 NOPs, data transfer is unreliable, as every other byte is skipped
\\
\\ LDA sr% (A) 0
\\ RTS 6 6
\\ STA (datptr%),Y 6 12
\\ INY 2 14
\\ DEX 2 16
\\ BNE MMC_ReadToMemory 3 19
\\.MMC_ReadToMemory
\\ JSR WaitForShiftDone 6 25
\\ LDA #4 2 27
\\ CPX #1 2 29
\\ BEQ lastByte 2 31
\\.notLastByte
\\ LDA sr% (A) 4 35
\\
\\ In this situation (A) is stretched for 1 cycle, giving 36 cycles total.
\\
\\ I tried adding just one cycle back into the main loop:
\\
\\ LDA sr% (A) 0
\\ RTS 6 6
\\ STA (datptr%),Y 6 12
\\ INY 2 14
\\ DEX 2 16
\\ BNE MMC_ReadToMemory 3 19
\\.MMC_ReadToMemory
\\ JSR WaitForShiftDone 6 25
\\ LDA #4 2 27
\\ CPX #1 2 29
\\ BNE notLastByte 3 32
\\.notLastByte
\\ LDA sr% (A) 4 36
\\
\\ In this situation (A) is stretched for 2 cycle, again giving 38 cycles total, which is the same as one NOP.
\\
\\ So, the limit of the 6522 in the Beeb in SR Mode 2 is 19us/byte.
\\
\\ There is other overhead, between blocks, and interrupts.
\\
\\ 19us/byte for &7000 bytes actually took 680ms. This is excactly the value SWEH measured with the TurboMMC ROM.
\\
\\ I'm goting to return to the code as the top of this thread, as I don't like doing things by dead reconning.
\\
\\ 22us/byte for &7000 bytes actually took 770ms.
.WaitForShiftDone
{
LDA #4 \\ Bit 2 of IFR is the Shift Reg Interrupt flag
CPX #1 \\ test if the last byte
BEQ lastByte \\ so we can return to mode zero before reading it
.notLastByte
BIT ifr% \\ wait for the SR interrupt flag to be set
BEQ notLastByte
LDA sr% \\ read the data byte, and clear the SR interrupt flag
RTS
.lastByte
BIT ifr% \\ wait for the SR interrupt flag to be set
BEQ lastByte
JSR ShiftRegMode0 \\ returning to mode 0 here avoids an addional byte read
LDA sr% \\ read the data byte, and clear the SR interrupt flag
RTS
}
\\ Reset the User VIA
.MMC_DEVICE_RESET
LDA #(3 + msbits)
STA iorb%
LDA ddrb%
ORA #ddrmask
STA ddrb%
LDA #&1C
STA ier%
\\ Fall through to ShiftRegMode0
.ShiftRegMode0
LDA acr% \\ Set SR Mode to mode 0
AND #&E3 \\ 11100011 = SR Mode 0
STA acr% \\ CB1 is now an input
LDA ddrb% \\ Set PB1 to being an output
ORA #&02 \\ 00000010
STA ddrb%
RTS
.ShiftRegMode2
LDA ddrb% \\ Set PB1 to being an input
AND #&FD \\ 11111101
STA ddrb%
LDA acr%
AND #&E3 \\ 11100011
ORA #&08 \\ 00001000 = SR Mode 2
STA acr%
LDA sr% \\ Start the first read
RTS
IF _TURBOMMC
.ShiftRegMode6 \\ Sequence here is important to avoid brief bus conflicts
LDA #&17 \\ 00010111
\\ PB0=1 sets MOSI to 1 (not very important)
\\ PB1=1 sets SCLK to 1 (important to avoid glitches)
\\ PB2=1 disables buffer connecting MISO to CB2
\\ PB3=0 enables buffer connecting CB2 to MOSI
\\ PB4=1 disables buffer connecting PB0 to MOSI
STA iorb% \\ Flip the direction of the data bus
LDA ddrb% \\ Set PB1 to being an input
AND #&FD \\ 11111101
STA ddrb% \\ Briefly the clock will float
LDA acr% \\ Change the SR mode last, to avoid conflicts
AND #&E3 \\ 11100011
ORA #&18 \\ 00011000 = SR Mode 6
STA acr% \\ CB1, CB2 are both outputs
RTS
ENDIF
\\ **** Complete Write Operation *****
.MMC_EndWrite
{
JSR MMC_16Clocks
.ewu1
JSR UP_ReadByteX
TAY
AND #&1F
CMP #&1F
BEQ ewu1
CMP #5
BNE errWrite2
.ewu2
JSR UP_ReadByteX
CMP #&FF
BNE ewu2
RTS
}
\\ **** Write 256 bytes from dataptr% ****
.MMC_Write256
{
IF _TURBOMMC
JSR ShiftRegMode6
ENDIF
LDY TubeNoTransferIf0
BNE wruT1
.wru1
LDA (datptr%),Y
IF _TURBOMMC
STA sr%
LDA #4
.wait
BIT ifr%
BEQ wait
ELSE
JSR UP_WriteByte
ENDIF
INY
BNE wru1
IF _TURBOMMC
BEQ ShiftRegMode6Exit
ELSE
RTS
ENDIF
}
.wruT1
{
LDY #0
.wruT2
LDA TUBE_R3_DATA
IF _TURBOMMC
STA sr%
LDA #4
.wait
BIT ifr%
BEQ wait
ELSE
JSR UP_WriteByte
ENDIF
INY
BNE wruT2
IF _TURBOMMC
BEQ ShiftRegMode6Exit
ELSE
RTS
ENDIF
}
\\ **** Write 256 bytes from buffer ****
.MMC_WriteBuffer
{
LDY #0
IF _TURBOMMC
JSR ShiftRegMode6
ENDIF
.wbu1
LDA buf%,Y
IF _TURBOMMC
STA sr%
LDA #4
.wait
BIT ifr%
BEQ wait
ELSE
JSR UP_WriteByte
ENDIF
INY
BNE wbu1
}
IF _TURBOMMC
.ShiftRegMode6Exit \\ Sequence here is important to avoid brief bus conflicts
JSR ShiftRegMode0 \\ CB1,2 are both inputs
\\ Briefly the clock will float
\\ PB1 is set as an output again
LDA #&0B \\ 00001011
\\ PB0=1 sets MOSI to 1 (not very important)
\\ PB1=1 sets SCLK to 1 (important to avoid glitches)
\\ PB2=0 enables buffer connecting MISO to CB2
\\ PB3=1 disables buffer connecting CB2 to MOSI
\\ PB4=0 enables buffer connecting PB0 to MOSI
STA iorb% \\ Flip the direction of the data bus
ENDIF
RTS