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
|
.set noreorder
.include "hwregs_a.h"
.section .text
_CardCSum:
# a0 - base csum
# a1 - data pointer
# a2 - length
lbu $v0, 0($a1)
addi $a2, -1
xor $a0, $v0
bgtz $a2, _CardCSum
addiu $a1, 1
jr $ra
move $v0, $a0
.global _CardWrite
.type _CardWrite, @function
_CardWrite:
# a0 - port number
# a1 - pointer to 128 byte buffer
# a2 - sector number
# return values:
# 0 - ok
# 1 - no device
# 2 - timeout
# 3 - bad checksum
# 4 - bad sector
# note: you must wait at least two vsyncs between each sector write
addiu $sp, -4
sw $ra, 0($sp)
lui $t0, IOBASE
li $v0, 0x1003 # TX Enable, Joypad port select
andi $a0, 1
sll $a0, 13
or $v0, $a0 # Select port 2 if a0 is 1
sh $v0, JOY_CTRL($t0) # Set to Joypad control interface
jal _wait # Delay for analog pads
li $v0, 310 # (needs optimization testing)
# May cause issues with third party adapters such as Brook wireless
#.Lread_empty_fifo_write: # Flush the RX FIFO just in case
# lbu $v1, JOY_TXRX($t0)
# lhu $v0, JOY_STAT($t0)
# nop
# andi $v0, 0x2
# bnez $v0, .Lread_empty_fifo_write
# nop
lhu $v1, JOY_CTRL($t0)
nop
or $v1, 0x10
sh $v1, JOY_CTRL($t0)
jal _CardExchng # Send device check byte
li $a0, 0x81
andi $v1, $v0, 0x100 # No card if exchange timed out
bnez $v1, .Lno_device_write
addiu $v0, $0 , 1
jal _wait # 1st exchange needs 27microsec after ACK
li $v0, 190 # (e.g. 7 as usual + an extra 20)
jal _CardExchng # Send write command
li $a0, 0x57
andi $v1, $v0, 0x100 # No card if exchange timed out
bnez $v1, .Lno_device_write
addiu $v0, $0 , 1
jal _CardExchng # Receive card ID bytes
move $a0, $0
andi $v1, $v0, 0x100
bnez $v1, .Lwrite_timeout
addiu $v0, $0 , 2
jal _CardExchng
move $a0, $0
andi $v1, $v0, 0x100
bnez $v1, .Lwrite_timeout
addiu $v0, $0 , 2
jal _CardExchng # Send address bytes
srl $a0, $a2, 8
andi $v1, $v0, 0x100
bnez $v1, .Lwrite_timeout
addiu $v0, $0 , 2
jal _CardExchng
andi $a0, $a2, 0xFF
andi $v1, $v0, 0x100
bnez $v1, .Lwrite_timeout
addiu $v0, $0 , 2
srl $t1, $a2, 8 # Checksum address by MSB xor LSB
andi $v0, $a2, 0xFF
xor $t1, $v0
move $a2, $0 # Send data and compute checksum
.Lwrite_loop:
lbu $a0, 0($a1)
addiu $a1, 1
jal _CardExchng
xor $t1, $a0
addiu $a2, 1
blt $a2, 128, .Lwrite_loop
nop
jal _CardExchng # Send checksum byte
move $a0, $t1
andi $v1, $v0, 0x100
bnez $v1, .Lwrite_timeout
addiu $v0, $0 , 2
jal _CardExchng # Receive card acknowledge bytes
move $a0, $0
andi $v1, $v0, 0x100
bnez $v1, .Lwrite_timeout
addiu $v0, $0 , 2
jal _CardExchng
move $a0, $0
andi $v1, $v0, 0x100
bnez $v1, .Lwrite_timeout
addiu $v0, $0 , 2
sb $0 , JOY_TXRX($t0) # Gets end byte
nop
.Lsend_wait_end_write:
lhu $v0, JOY_STAT($t0)
nop
andi $v0, 0x6
bne $v0, 0x6, .Lsend_wait_end_write
nop
lbu $v0, JOY_TXRX($t0)
nop
beq $v0, 0x4e, .Lwrite_timeout # Bad checksum
addiu $v0, $0 , 3
beq $v0, 0xff, .Lwrite_timeout # Bad sector
addiu $v0, $0 , 4
move $v0, $0
.Lwrite_timeout:
.Lno_device_write:
sh $0 , JOY_CTRL($t0) # Apparently required
lw $ra, 0($sp)
addiu $sp, 4
jr $ra
nop
.global _CardRead
.type _CardRead, @function
_CardRead:
# a0 - port number
# a1 - pointer to 128 byte buffer
# a2 - sector number
addiu $sp, -12
sw $ra, 0($sp)
sw $a1, 4($sp)
sw $a2, 8($sp)
lui $t0, IOBASE
li $v0, 0x1003 # TX Enable, Joypad port select
andi $a0, 1
sll $a0, 13
or $v0, $a0 # Select port 2 if a0 is 1
sh $v0, JOY_CTRL($t0) # Set to Joypad control interface
jal _wait # Delay for analog pads (needs testing)
li $v0, 310
# May cause issues with third party adapters such as Brook wireless
#.Lread_empty_fifo: # Flush the RX FIFO just in case
# lbu $v1, JOY_TXRX($t0)
# lhu $v0, JOY_STAT($t0)
# nop
# andi $v0, 0x2
# bnez $v0, .Lread_empty_fifo
# nop
lhu $v1, JOY_CTRL($t0)
nop
or $v1, 0x10
sh $v1, JOY_CTRL($t0)
jal _CardExchng # Send device check byte
li $a0, 0x81
andi $v1, $v0, 0x100 # No card if exchange timed out
bnez $v1, .Lno_device
addiu $v0, $0 , 1
jal _wait # 1st exchange needs 27microsec after ACK
li $v0, 190 # (e.g. 7 as usual + an extra 20)
jal _CardExchng # Send read command
li $a0, 0x52
andi $v1, $v0, 0x100 # No card if exchange timed out
bnez $v1, .Lno_device
addiu $v0, $0 , 2
jal _CardExchng # Receive card ID bytes
move $a0, $0
andi $v1, $v0, 0x100
bnez $v1, .Lno_device
addiu $v0, $0 , 3
jal _CardExchng
move $a0, $0
andi $v1, $v0, 0x100
bnez $v1, .Lno_device
addiu $v0, $0 , 4
jal _CardExchng # Send address
srl $a0, $a2, 8
andi $v1, $v0, 0x100
bnez $v1, .Lno_device
addiu $v0, $0 , 5
jal _CardExchng
andi $a0, $a2, 0xFF
andi $v1, $v0, 0x100
bnez $v1, .Lno_device
addiu $v0, $0 , 6
sb $0 , JOY_TXRX($t0) # Receive command acknowledge 1
nop
.Lsend_wait:
lhu $v0, JOY_STAT($t0)
nop
andi $v0, 0x4
beqz $v0, .Lsend_wait
nop
move $v1, $0
lui $a0, 0xBFC0
.Lwait_ack:
bgt $v1, 30000, .Lread_timeout
addiu $v0, $0, 10
lhu $v0, JOY_STAT($t0)
lw $0 , 4($a0)
lw $0 , 0($a0)
andi $v0, 0x202
bne $v0, 0x202, .Lwait_ack
addiu $v1, 1
lhu $v1, JOY_CTRL($t0)
lbu $v0, JOY_TXRX($t0)
or $v1, 0x10
sh $v1, JOY_CTRL($t0)
jal _CardExchng # Receive command acknowledge 2
move $a0, $0
andi $v1, $v0, 0x100
bnez $v1, .Lno_device
addiu $v0, $0 , 7
jal _CardExchng # Receive confirmed address MSB
move $a0, $0
andi $v1, $v0, 0x100
bnez $v1, .Lno_device
addiu $v0, $0 , 8
jal _CardExchng # Receive confirmed address LSB
move $a0, $0
andi $v1, $v0, 0x100
bnez $v1, .Lno_device
addiu $v0, $0 , 9
move $a2, $0
.Ltransfer_loop:
jal _CardExchng
move $a0, $0
sb $v0, 0($a1)
addiu $a2, 1
blt $a2, 128, .Ltransfer_loop
addiu $a1, 1
jal _CardExchng # Gets checksum byte
move $a0, $0
move $a3, $v0
sb $0 , JOY_TXRX($t0) # Gets end byte
nop
.Lsend_wait_end:
lhu $v1, JOY_STAT($t0)
nop
andi $v1, 0x6
bne $v1, 0x6, .Lsend_wait_end
nop
lbu $v1, JOY_TXRX($t0)
nop
lw $a2, 8($sp)
lw $a1, 4($sp)
andi $v1, $a2, 0xff
srl $a2, 8
xor $a0, $a2, $v1
jal _CardCSum
li $a2, 128
bne $v0, $a3, .Lno_device
addiu $v0, $0 , -1
move $v0, $0
.Lread_timeout:
.Lno_device:
sh $0 , JOY_CTRL($t0) # Apparently required
lw $ra, 0($sp)
addiu $sp, 12
jr $ra
nop
.global _CardExchng
.type _CardExchng, @function
_CardExchng:
lui $t0, IOBASE
sb $a0, JOY_TXRX($t0)
nop
.Lsend_wait_exchg:
lhu $v0, JOY_STAT($t0)
nop
andi $v0, 0x4
beqz $v0, .Lsend_wait_exchg
nop
lui $a0, 0xBFC0
move $v1, $0
.Lwait_ack_exchg:
bgt $v1, 500, .Ltimeout
lhu $v0, JOY_STAT($t0)
lw $0 , 4($a0)
lw $0 , 0($a0)
andi $v0, 0x202
bne $v0, 0x202, .Lwait_ack_exchg
addiu $v1, 1
b .Ldone
nop
.Ltimeout:
lbu $v0, JOY_TXRX($t0)
nop
b .Lexit_exchg
ori $v0, 0x100
.Ldone:
lhu $v1, JOY_CTRL($t0)
lbu $v0, JOY_TXRX($t0)
or $v1, 0x10
sh $v1, JOY_CTRL($t0)
.Lexit_exchg:
jr $ra
nop
|