aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/ds/arm9/source/fat/disc_io.c
blob: 74fc8fb09b7f7d6339e040b504096eb68ddd6395 (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
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
557
/*

	disc_io.c

	uniformed io-interface to work with Chishm's FAT library

	Written by MightyMax

	Modified by Chishm:
	2005-11-06
		* Added WAIT_CR modifications for NDS

	Modified by www.neoflash.com:
	2006-02-03
		* Added SUPPORT_* defines, comment out any of the SUPPORT_* defines in disc_io.h to remove support
		  for the given interface and stop code being linked to the binary

	    * Added support for MK2 MMC interface

		* Added disc_Cache* functions

	Modified by Chishm:
	2006-02-05
		* Added Supercard SD support

	Modified by CyteX:
	2006-02-26
		* Added EFA2 support
*/

#ifdef NDS
	#include <nds.h>
#endif

#include "disc_io.h"


// Include known io-interfaces:
#ifdef SUPPORT_MPCF
 #include "io_mpcf.h"
#endif

#ifdef SUPPORT_M3CF
 #include "io_m3cf.h"
#endif

#ifdef SUPPORT_M3SD
 #include "io_m3sd.h"
#endif

#ifdef SUPPORT_SCCF
 #include "io_sccf.h"
#endif

#ifdef SUPPORT_SCSD
 #include "io_scsd.h"
#endif

#ifdef SUPPORT_FCSR
 #include "io_fcsr.h"
#endif

#ifdef SUPPORT_NMMC
 #include "io_nmmc.h"
#endif

#ifdef SUPPORT_EFA2
 #include "io_efa2.h"
#endif

#ifdef SUPPORT_NJSD
 #include "io_njsd.h"
#endif

#ifdef SUPPORT_MMCF
 #include "io_mmcf.h"
#endif

#include "io_dldi.h"

// Keep a pointer to the active interface
LPIO_INTERFACE active_interface = 0;


/*

	Disc Cache functions
	2006-02-03:
		Added by www.neoflash.com

*/
int discDetect = 0;

int dldiFound = FALSE;

#ifdef DISC_CACHE

#include <string.h>

#define CACHE_FREE 0xFFFFFFFF

static u8 cacheBuffer[ DISC_CACHE_COUNT * 512 ];

static struct {
	u32 sector;
	u32 dirty;
	u32 count;
} cache[ DISC_CACHE_COUNT ];

FATDevice currentDevice;

static u32 disc_CacheFind(u32 sector) {
	u32 i;

	for( i = 0; i < DISC_CACHE_COUNT; i++ )	{
		if( cache[ i ].sector == sector )
			return i;
	}

	return CACHE_FREE;
}

static u32 disc_CacheFindFree(void) {

	u32 i = 0, j;
	u32 count = -1;

	for( j = 0; j < DISC_CACHE_COUNT; j++ )	{

		if( cache[ j ].sector == CACHE_FREE ) {
			i = j;
			break;
		}

		if( cache[ j ].count < count ) {
			count = cache[ j ].count;
			i = j;
		}
	}
	/*
	if( cache[ i ].sector != CACHE_FREE && cache[i].dirty != 0 ) {

		active_interface->fn_WriteSectors( cache[ i ].sector, 1, &cacheBuffer[ i * 512 ] );
		// todo: handle write error here

		cache[ i ].sector = CACHE_FREE;
		cache[ i ].dirty = 0;
		cache[ i ].count = 0;
	}*/

	return i;
}

void disc_CacheInit(void)	{

	u32 i;

	for( i = 0; i < DISC_CACHE_COUNT; i++ )	{
		cache[ i ].sector = CACHE_FREE;
		cache[ i ].dirty = 0;
		cache[ i ].count = 0;
	}

}

bool disc_CacheFlush(void)	{

	u32 i;

	if( !active_interface )	return false;

	for( i = 0; i < DISC_CACHE_COUNT; i++ )	{
		if( cache[ i ].sector != CACHE_FREE && cache[ i ].dirty != 0 )	{
			if( active_interface->fn_WriteSectors( cache[ i ].sector, 1, &cacheBuffer[ i * 512 ] ) == false )
				return false;

			cache[ i ].dirty = 0;
		}
	}
	return true;
}

bool disc_CacheReadSector( void *buffer, u32 sector) {
	u32 i = disc_CacheFind( sector );
	if( i == CACHE_FREE ) {
		i = disc_CacheFindFree();
		cache[ i ].sector = sector;
		if( active_interface->fn_ReadSectors( sector, 1, &cacheBuffer[ i * 512 ] ) == false )
			return false;
	}
#ifdef DISK_CACHE_DMA
		DMA3_SRC = (u32)&cacheBuffer[ i * 512 ]
		DMA3_DEST = (u32)buffer;
		DMA3_CR = 128 | DMA_COPY_WORDS;
#else
	memcpy( buffer, &cacheBuffer[ i * 512 ], 512 );
#endif
	cache[ i ].count++;
	return true;
}

bool disc_CacheWriteSector( void *buffer, u32 sector ) {
	u32 i = disc_CacheFind( sector );
	if( i == CACHE_FREE ) {
		i = disc_CacheFindFree();
		cache [ i ].sector = sector;
	}
#ifdef DISK_CACHE_DMA
		DMA3_SRC = (u32)buffer;
		DMA3_DEST = (u32)&cacheBuffer[ i * 512 ];
		DMA3_CR = 128 | DMA_COPY_WORDS;
#else
	memcpy( &cacheBuffer[ i * 512 ], buffer, 512 );
#endif
	cache[ i ].dirty=1;
	cache[ i ].count++;
	return true;
}

#endif

/*

	Hardware level disc funtions

*/

void disc_setEnable(int disc) {
	discDetect = disc;
}

bool disc_setGbaSlotInterface (void)
{
	// If running on an NDS, make sure the correct CPU can access
	// the GBA cart. First implemented by SaTa.
#ifdef NDS
 #ifdef ARM9
//	WAIT_CR &= ~(0x8080);
 #endif
 #ifdef ARM7
//	WAIT_CR |= (0x8080);
 #endif
#endif


#ifdef SUPPORT_M3SD
	if (discDetect == 1)	{
		// check if we have a M3 perfect SD plugged in
		active_interface = M3SD_GetInterface() ;
		if (active_interface->fn_StartUp())
		{
			// set M3 SD as default IO
			currentDevice = DEVICE_M3SD;
			return true ;
		} ;
	}
#endif




#ifdef SUPPORT_MMCF
	// check if we have a GBA Flash Cart plugged in
	active_interface = MMCF_GetInterface() ;
	if (active_interface->fn_StartUp())
	{
		// set MMCF as default IO
		currentDevice = DEVICE_MMCF;
		return true ;
	} ;
#endif



#ifdef SUPPORT_M3CF
	// check if we have a M3 perfect CF plugged in
	active_interface = M3CF_GetInterface() ;
	if (active_interface->fn_StartUp())
	{
		// set M3 CF as default IO
		currentDevice = DEVICE_M3CF;
		return true ;
	} ;
#endif


#ifdef SUPPORT_MPCF
	// check if we have a GBA Movie Player plugged in
	active_interface = MPCF_GetInterface() ;
	if (active_interface->fn_StartUp())
	{
		// set GBAMP as default IO
		currentDevice = DEVICE_MPCF;
		return true ;
	} ;
#endif


#ifdef SUPPORT_SCCF
	// check if we have a SuperCard CF plugged in
	active_interface = SCCF_GetInterface() ;
	if (active_interface->fn_StartUp())
	{
		// set SC CF as default IO
		currentDevice = DEVICE_SCCF;
		return true ;
	} ;
#endif



#ifdef SUPPORT_EFA2
	// check if we have a EFA2 plugged in
	active_interface = EFA2_GetInterface() ;
	if (active_interface->fn_StartUp())
	{
		return true ;
	} ;
#endif


#ifdef SUPPORT_FCSR
	// check if we have a GBA Flash Cart plugged in
	active_interface = FCSR_GetInterface() ;
	if (active_interface->fn_StartUp())
	{
		// set FC as default IO
		return true ;
	} ;
#endif




	return false;
}


FATDevice disc_getDeviceId() {
	return currentDevice;
}

void disc_getDldiId(char* id) {
	char* driverId = (char *) &_io_dldi;
	id[0] = driverId[0];
	id[1] = driverId[1];
	id[2] = driverId[2];
	id[3] = driverId[3];
	id[4] = '\0';
}

#ifdef NDS
// Check the DS card slot for a valid memory card interface
// If an interface is found, it is set as the default interace
// and it returns true. Otherwise the default interface is left
// untouched and it returns false.


bool disc_setDsSlotInterface (void)
{
#ifdef ARM9
	REG_EXEMEMCNT &= ~(1<<11);
#endif
#ifdef ARM7
	REG_EXEMEMCNT |= (1<<11);
#endif

	active_interface = DLDI_GetInterface();

	if (strcasecmp((char *)(&_dldi_driver_name), "Default (No interface)")) {
		char name[48];
		memcpy(name, &_dldi_driver_name, 48);
		name[47] = '\0';
		consolePrintf("DLDI Device:\n'%s'\n", name);
		dldiFound = TRUE;
	} else {
		consolePrintf("DLDI Driver not patched!\n");
		dldiFound = FALSE;
	}

	if (active_interface->fn_StartUp()) {
		consolePrintf("DLDI Driver Initialised OK!\n");
		currentDevice = DEVICE_DLDI;
		return true;
	} else {
		consolePrintf("DLDI Initialise failed.\n");
	}

#ifdef SUPPORT_SCSD
	// check if we have a SuperCard SD plugged in
	if (discDetect == 2) {
		active_interface = SCSD_GetInterface() ;
		consolePrintf("SCSD!");
		if (active_interface->fn_StartUp())
		{
			// set SC SD as default IO
			currentDevice = DEVICE_SCSD;
			return true ;
		} ;
	}
#endif

#ifdef SUPPORT_NJSD
	// check if we have a GBA Flash Cart plugged in
	active_interface = NJSD_GetInterface() ;
	if (active_interface->fn_StartUp())
	{
		// set NJSD as default IO
		currentDevice = DEVICE_NJSD;
		return true ;
	} ;
#endif

#ifdef SUPPORT_NMMC
	// check if we have a Neoflash MK2 / MK3 plugged in
	active_interface = NMMC_GetInterface() ;
	if (active_interface->fn_StartUp())
	{
		// set Neoflash MK2 / MK3 as default IO
		currentDevice = DEVICE_NMMC;
		return true ;
	} ;
#endif




	return false;
}
#endif


bool disc_Init(void)
{
#ifdef DISC_CACHE
	disc_CacheInit();
#endif


	if (active_interface != 0) {
		return true;
	}

#ifdef NDS
	if (disc_setDsSlotInterface()) {
		return true;
	}
#endif

	if (disc_setGbaSlotInterface()) {
		return true;
	}

	// could not find a working IO Interface
	active_interface = 0 ;
	return false ;
}

bool disc_IsInserted(void)
{
	if (active_interface) return active_interface->fn_IsInserted() ;
	return false ;
}

bool disc_ReadSectors(u32 sector, u8 numSecs, void* buffer)
{
#ifdef DISC_CACHE
	u8 *p=(u8*)buffer;
	u32 i;
	u32 inumSecs=numSecs;
	if(numSecs==0)
		inumSecs=256;
	for( i = 0; i<inumSecs; i++)	{
		if( disc_CacheReadSector( &p[i*512], sector + i ) == false )
			return false;
	}
	return true;
#else
	if (active_interface) return active_interface->fn_ReadSectors(sector,numSecs,buffer) ;
	return false ;
#endif
}

bool disc_WriteSectors(u32 sector, u8 numSecs, void* buffer)
{
/*#ifdef DISC_CACHE
	u8 *p=(u8*)buffer;
	u32 i;
	u32 inumSecs=numSecs;
	if(numSecs==0)
		inumSecs=256;
	for( i = 0; i<inumSecs; i++)	{
		if( disc_CacheWriteSector( &p[i*512], sector + i ) == false )
			return false;
	}
	return true;
#else*/
#ifdef DISC_CACHE
	disc_CacheInit();
#endif

#define MISALIGNMENT_BODGE

#ifdef MISALIGNMENT_BODGE
	// This bodge works around problems with some card reader drivers which require data to be
	// aligned to 2- or 4-byte boundaries it varies which one they require.  This bodge sorts
	// it but also reduces write speed as it doesn't use the multi-sector write capability any
	// more.  A better fix will be written for a future version.

	if (active_interface) {
		u8 sectorBuffer[512];
		int r;

		for (r = 0; r < numSecs; r++) {
			memcpy(sectorBuffer, &((char *)buffer)[r * 512], 512);

			if (!active_interface->fn_WriteSectors(sector + r, 1, sectorBuffer))
			{
				return false;
			}
		}


		return true;
	}

#else
	if (active_interface) return active_interface->fn_WriteSectors(sector,numSecs,buffer) ;
#endif
//#endif
	return false ;
}

bool disc_ClearStatus(void)
{
	if (active_interface) return active_interface->fn_ClearStatus() ;
	return false ;
}

bool disc_Shutdown(void)
{
#ifdef DISC_CACHE
	disc_CacheFlush();
#endif
	if (active_interface) active_interface->fn_Shutdown() ;
	active_interface = 0 ;
	return true ;
}

u32	disc_HostType (void)
{
	if (active_interface) {
		return active_interface->ul_ioType;
	} else {
		return 0;
	}
}