aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/ds/arm9/source/fat/io_scsd_asm.s
blob: 498cbb96c233c288689eae52ed8c140398fd4c21 (plain)
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
    .TEXT
@--------------------------------sd data--------------------------------
.equ sd_comadd,0x9800000
.equ sd_dataadd,0x9000000  
.equ sd_dataradd,0x9100000
@-----------------viod sd_data_write_s(u16 *buff,u16* crc16buff)-------------------
    .ALIGN
    .GLOBAL sd_data_write_s	
    .CODE 32
sd_data_write_s:
	stmfd   r13!,{r4-r5}
	mov	r5,#512
	mov	r2,#sd_dataadd
sd_data_write_busy:
	ldrh	r3,[r2]   
	tst	r3,#0x100
	beq	sd_data_write_busy

	ldrh	r3,[r2]   

	mov	r3,#0 @star bit
	strh	r3,[r2]
sd_data_write_loop:
	ldrh	r3,[r0],#2
	add	r3,r3,r3,lsl #20
	mov	r4,r3,lsl #8
	stmia   r2,{r3-r4}  
 
        subs    r5, r5, #2                 
        bne     sd_data_write_loop  

	cmp	r1,#0
	movne	r0,r1
	movne	r1,#0
	movne	r5,#8
	bne	sd_data_write_loop

	mov	r3,#0xff @end bit
	strh	r3,[r2]
sd_data_write_loop2:
	ldrh	r3,[r2]   
	tst	r3,#0x100
	bne	sd_data_write_loop2

	ldmia   r2,{r3-r4} 

	ldmfd	r13!,{r4-r5}
	bx      r14
@-----------------end sd_data_write_s-------------------

@----------void sd_data_read_s(u16 *buff)-------------
    .ALIGN
    .GLOBAL sd_data_read_s	
    .CODE 32
sd_data_read_s:
	stmfd   r13!,{r4}
	mov	r1,#sd_dataradd
sd_data_read_loop1:
	ldrh	r3,[r1]   @star bit
	tst	r3,#0x100
	bne	sd_data_read_loop1
	mov	r2,#512
sd_data_read_loop:
	ldmia	r1,{r3-r4} 
	mov	r3,r4,lsr #16
	strh	r3 ,[r0],#2

	ldmia	r1,{r3-r4} 
	mov	r3,r4,lsr #16
	strh	r3 ,[r0],#2

	ldmia	r1,{r3-r4} 
	mov	r3,r4,lsr #16
	strh	r3 ,[r0],#2

	ldmia	r1,{r3-r4} 
	mov	r3,r4,lsr #16
	strh	r3 ,[r0],#2

	ldmia	r1,{r3-r4} 
	mov	r3,r4,lsr #16
	strh	r3 ,[r0],#2

	ldmia	r1,{r3-r4} 
	mov	r3,r4,lsr #16
	strh	r3 ,[r0],#2

	ldmia	r1,{r3-r4} 
	mov	r3,r4,lsr #16
	strh	r3 ,[r0],#2

	ldmia	r1,{r3-r4} 
	mov	r3,r4,lsr #16
	strh	r3 ,[r0],#2

        subs    r2, r2, #16                
        bne     sd_data_read_loop 

	ldmia	r1,{r3-r4} @crc 16
	ldmia	r1,{r3-r4}  
	ldmia	r1,{r3-r4}  
	ldmia	r1,{r3-r4}  
	ldrh	r3,[r1]    @end bit
	ldmfd	r13!,{r4}  
	bx      r14
@----------end sd_data_read_s-------------

@------void sd_com_crc16_s(u16* buff,u16 num,u16* crc16buff)
    .ALIGN
    .GLOBAL	sd_crc16_s 
    .CODE 32
sd_crc16_s:
	stmfd   r13!,{r4-r9}
	mov	r9,r2

	mov	r3,#0  
	mov	r4,#0  
	mov	r5,#0  
	mov	r6,#0  

	ldr	r7,=0x80808080
	ldr	r8,=0x1021
	mov	r1,r1,lsl #3
sd_crc16_loop:

	tst	r7,#0x80
	ldrneb	r2,[r0],#1

	mov	r3,r3,lsl #1
	tst	r3,#0x10000
	eorne	r3,r3,r8
	tst	r2,r7,lsr #24
	eorne	r3,r3,r8
	
	mov	r4,r4,lsl #1
	tst	r4,#0x10000
	eorne	r4,r4,r8
	tst	r2,r7,lsr #25
	eorne	r4,r4,r8
	
	mov	r5,r5,lsl #1
	tst	r5,#0x10000
	eorne	r5,r5,r8
	tst	r2,r7,lsr #26
	eorne	r5,r5,r8
	
	mov	r6,r6,lsl #1
	tst	r6,#0x10000
	eorne	r6,r6,r8
	tst	r2,r7,lsr #27
	eorne	r6,r6,r8

	mov	r7,r7,ror #4
	subs	r1,r1,#4
        bne     sd_crc16_loop 

	mov	r2,r9
	mov	r8,#16
sd_crc16_write_data:
	mov	r7,r7,lsl #4
	tst	r3,#0x8000
	orrne	r7,r7,#8
	tst	r4,#0x8000
	orrne	r7,r7,#4
	tst	r5,#0x8000
	orrne	r7,r7,#2
	tst	r6,#0x8000
	orrne	r7,r7,#1

	mov	r3,r3,lsl #1
	mov	r4,r4,lsl #1
	mov	r5,r5,lsl #1
	mov	r6,r6,lsl #1

	sub	r8,r8,#1
	tst	r8,#1
	streqb	r7,[r2],#1
	cmp	r8,#0
	bne	sd_crc16_write_data

	ldmfd	r13!,{r4-r9}
	bx      r14
@------end sd_com_crc16_s-----------------------------------

@--------------u8 sd_crc7_s(u16* buff,u16 num)----------------------------
    .ALIGN
    .GLOBAL	sd_crc7_s 
    .CODE 32
sd_crc7_s:
	stmfd   r13!,{r4}

	mov	r3,#0
	ldr	r4,=0x80808080
	mov	r1,r1,lsl #3 @ *8
sd_crc7_loop:
	tst	r4,#0x80
	ldrneb	r2,[r0],#1

	mov	r3,r3,lsl #1

	tst	r3,#0x80
	eorne	r3,r3,#0x9

	tst	r2,r4,lsr #24
	eorne	r3,r3,#0x9

	mov	r4,r4,ror #1
	subs	r1,r1,#1
        bne     sd_crc7_loop 

	mov	r3,r3,lsl #1
	add	r0,r3,#1
	ldmfd	r13!,{r4}
	bx      r14
@--------------end sd_crc7_s----------------------------

@--------------sd_com_read_s(u16* buff,u32 num)------------------
		.ALIGN
		.GLOBAL	 sd_com_read_s 
		.CODE 32

sd_com_read_s:
	stmfd   r13!,{r4-r6}
	mov	r2,#sd_comadd
sd_com_read_loop1:
	ldrh	r3,[r2] @star bit
	tst	r3,#1
	bne	sd_com_read_loop1

sd_com_read_loop:
	ldmia   r2,{r3-r6}
        subs    r1, r1, #1                  
        bne     sd_com_read_loop  
	ldmfd	r13!,{r4-r6}
	bx      r14
@--------------end sd_com_read_s------------------

@-------------------void sd_com_write_s(u16* buff,u32 num)-----------------

		.ALIGN
		.GLOBAL	 sd_com_write_s
		.CODE 32
sd_com_write_s:
	stmfd   r13!,{r4-r6}
	
	mov	r2,#sd_comadd
sd_com_write_busy:
	ldrh	r3,[r2]   
	tst	r3,#0x1
	beq	sd_com_write_busy

	ldrh	r3,[r2]  

sd_com_write_loop:
        ldrb   r3,[r0],#1
	add	r3,r3,r3,lsl #17
	mov	r4,r3,lsl #2
	mov	r5,r4,lsl #2
	mov	r6,r5,lsl #2
        stmia   r2,{r3-r6}  
        subs    r1, r1, #1                  
        bne     sd_com_write_loop  
	ldmfd   r13!,{r4-r6}

	bx      r14
@-------------------end sd_com_write_s-----------------

@-----------------void send_clk(u32 num)---------
		.ALIGN
		.GLOBAL	 send_clk 
		.CODE 32

send_clk:
	mov	r1,#sd_comadd
send_clk_loop1:
	ldrh	r3,[r1]
	subs	r0,r0,#1
	bne	send_clk_loop1
	bx	r14
@-----------------end send_clk---------

@---------------------------void SDCommand(u8 command,u8 num,u32 sector)--------------------
		.ALIGN
		.GLOBAL	 SDCommand 
		.CODE 32
@void SDCommand(u8 command,u8 num,u32 sector )
@{
@	u8 databuff[6];
@	register u8 *char1;
@	register u8 *char2;
@
@	char1=(u8*)(((u32)&sector)+3);
@	char2=(u8*)databuff;
@	*char2++=coma+0x40;
@	*char2++=*char1--;
@	*char2++=*char1--;
@	*char2++=*char1--;
@	*char2++=*char1;
@	*char2=sd_crc7_s((u32)databuff,5);
@
@	sd_com_write_s((u32)databuff,6);
@
@}
SDCommand:
	stmfd   r13!,{r14}

	sub	r13,r13,#16
	add	r0,r0,#0x40
	strb	r0,[r13,#4]

	mov	r1,r2,lsr #24
	strb	r1,[r13,#5]
	mov	r1,r2,lsr #16
	strb	r1,[r13,#6]
	mov	r1,r2,lsr #8
	strb	r1,[r13,#7]
	strb	r2,[r13,#8]
	add	r0,r13,#4
	mov	r1,#5
	bl	sd_crc7_s
	strb	r0,[r13,#9]
	add	r0,r13,#4
	mov	r1,#6
	bl	sd_com_write_s

	add	r13,r13,#16
	ldmfd   r13!,{r15}
@	bx	r14
@---------------------------end SDCommand--------------------

@----------void ReadSector(u16 *buff,u32 sector,u8 readnum)-------------
		.ALIGN
		.GLOBAL	 ReadSector @ r0:Srcp r1:num ok
		.CODE 32

@void ReadSector(u16 *buff,u32 sector,u8 readnum)
@{
@	register u16 i,j;
@	i=readnum;
@	sectno<<=9;
@	SDCommand(18,0,sector); 
@	for (j=0;j<i ; j++)
@	{
@		sd_data_read_s((u32)buff+j*512);
@	}
@	SDCommand(12,0,0); 
@	get_resp();
@	send_clk(0x10);
@
@}
ReadSector:
	stmfd   r13!,{r4-r6,r14}

	mov	r4,r0
	mov	r5,r2

	mov	r2,r1,lsl #9
	mov	r0,#18
	mov	r1,#0
	bl	SDCommand
	mov	r6,#0
beginforj_ReadSector:
	cmp	r6,r5
	bge	endforj_ReadSector
	mov	r0,r4
	add	r0,r0,r6,lsl #9 
	bl	sd_data_read_s
	add	r6,r6,#1
	b	beginforj_ReadSector
endforj_ReadSector:
	mov	r0,#12
	mov	r1,#0
	mov	r2,#0
	bl	SDCommand
	bl	get_resp
	mov	r0,#0x10
	bl	send_clk
	mov	r0,#1
	ldmfd   r13!,{r4-r6,r15}
@	bx	r14
@----------end ReadSector------------

@-----------void get_resp(void)-------------------


		.ALIGN
		.GLOBAL	 get_resp @ r0:Srcp r1:num ok
		.CODE 32
get_resp:

	stmfd   r13!,{r14}
	mov	r1,#6
	bl	sd_com_read_s
	ldmfd   r13!,{r15}
@-----------end get_resp-------------------


@---------------void WriteSector(u16 *buff,u32 sector,u8 writenum)---------------------
		.ALIGN
		.GLOBAL	 WriteSector @ r0:Srcp r1:num ok
		.CODE 32

@void WriteSector(u16 *buff,u32 sector,u8 writenum)
@{
@	register u16 i,j;
@	u16 crc16[5];
@	i=writenum;
@
@	sectno<<=9;
@
@	SDCommand(25,0,sector); 
@	get_resp();
@	send_clk(0x10); 
@
@	for (j=0;j<i ; j++)
@	{
@		sd_crc16_s((u32)(u32)buff+j*512,512,(u32)crc16);
@		sd_data_write_s((u32)buff+j*512,(u32)crc16);
@		send_clk(0x10); 
@	}
@	SDCommand(12,0,0); 
@	get_resp();
@	send_clk(0x10);
@	while((*(u16*)sd_dataadd &0x0100)==0);
@
@}
@
WriteSector:
	stmfd   r13!,{r4-r6,r14}

	sub	r13,r13,#20

	mov	r4,r0
	mov	r5,r2

	mov	r2,r1,lsl #9
	mov	r0,#25
	mov	r1,#0
	bl	SDCommand
	bl	get_resp
	mov	r0,#0x10
	bl	send_clk
	mov	r6,#0

beginforj_WriteSector:
	cmp	r6,r5
	bge	endforj_WriteSector
	mov	r0,r4
	add	r0,r0,r6,lsl #9
	mov	r1,#512
	add	r2,r13,#4
	bl	sd_crc16_s
	mov	r0,r4
	add	r0,r0,r6,lsl #9 
	add	r1,r13,#4
	bl	sd_data_write_s
	mov	r0,#0x10
	bl	send_clk
	add	r6,r6,#1
	b	beginforj_WriteSector
endforj_WriteSector:
	mov	r0,#12
	mov	r1,#0
	mov	r2,#0
	bl	SDCommand
	bl	get_resp
	mov	r0,#0x10
	bl	send_clk
	ldr	r0,=sd_dataadd
beginwhile_WriteSector:
	ldrh	r1,[r0]
	tst	r1,#0x0100
	beq	beginwhile_WriteSector
	mov	r0,#1
	add	r13,r13,#20
	ldmfd   r13!,{r4-r6,r15}
@---------------end WriteSector--------------------

@----------------void InitSCMode(void)---------------
		.ALIGN
		.GLOBAL	 InitSCMode
		.CODE 32
InitSCMode:
	mvn     r0,#0x0F6000000 
	sub     r0,r0,#0x01
	mov     r1,#0x0A500
	add     r1,r1,#0x5A
	strh    r1,[r0]
	strh    r1,[r0]
	mov	r2,#3
	strh    r2,[r0]
	strh    r2,[r0]
	bx	r14
@----------------end InitSCMode ---------------

@----------------bool MemoryCard_IsInserted(void)---------------
		.ALIGN
		.GLOBAL	 MemoryCard_IsInserted
		.CODE 32

MemoryCard_IsInserted:
	ldr	r0,=sd_comadd
	ldrh	r1,[r0]
	tst	r1,#0x300
	moveq	r0,#1
	movne	r0,#0
	bx	r14
@----------------end MemoryCard_IsInserted---------------

    .END