aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/ds/arm9/source/fat/io_fcsr.c
blob: d576dfbb0f9e91ad89cc5a5dff7f1391f66ce5a9 (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
/*
	io_fcsr.c based on

	compact_flash.c
	By chishm (Michael Chisholm)

	Hardware Routines for using a GBA Flash Cart and SRAM as a 
	block device.

	This software is completely free. No warranty is provided.
	If you use it, please give me credit and email me about your
	project at chishm@hotmail.com

	See gba_nds_fat.txt for help and license details.

	The file system must be 512 byte aligned, in cart address space.
	SRAM is supported.
*/


#include "io_fcsr.h"

#ifdef SUPPORT_FCSR
#include <string.h>

//---------------------------------------------------------------
// DMA
#ifdef _CF_USE_DMA
 #ifndef NDS
  #include "gba_dma.h"
 #else
  #include <nds/dma.h>
  #ifdef ARM9
   #include <nds/arm9/cache.h>
  #endif
 #endif
#endif

#ifdef NDS
 #define SRAM_START 0x0A000000
#else
 #define SRAM_START 0x0E000000
#endif

#define NO_SRAM 0xFFFFFFFF

#define FCSR 0x52534346
const char FCSR_LabelString[] = " Chishm FAT";

u8* FCSR_FileSysPointer = 0;
u8* FCSR_SramSectorPointer[4] = {0,0,0,0};
u32 FCSR_SramSectorStart[4] = {0,0,0,0};
u32 FCSR_SramSectorEnd[4] = {0,0,0,0};

/*-----------------------------------------------------------------
FCSR_IsInserted
Is a GBA Flash Cart with a valid file system inserted?
bool return OUT:  true if a GBA FC card is inserted
-----------------------------------------------------------------*/
bool FCSR_IsInserted (void) 
{
	bool flagFoundFileSys = false;

	u32* fileSysPointer = (u32*)0x08000100;		// Start at beginning of cart address space, offset by expected location of string

	// Search for file system
	while ((fileSysPointer < (u32*)0x0A000000) && !flagFoundFileSys)	// Only search while not at end of cart address space
	{
		while ((*fileSysPointer != FCSR) && (fileSysPointer < (u32*)0x0A000000))
			fileSysPointer += 0x40;
		if ((strncmp(FCSR_LabelString, (char*)(fileSysPointer + 1), 12) == 0) && (fileSysPointer < (u32*)0x0A000000))
		{
			flagFoundFileSys = true;
		} else {
			fileSysPointer += 0x80;
		}
	}

	return flagFoundFileSys;
}


/*-----------------------------------------------------------------
FCSR_ClearStatus
Finish any pending operations
bool return OUT:  always true for GBA FC
-----------------------------------------------------------------*/
bool FCSR_ClearStatus (void) 
{
	return true;
}


/*-----------------------------------------------------------------
FCSR_ReadSectors
Read 512 byte sector numbered "sector" into "buffer"
u32 sector IN: address of first 512 byte sector on Flash Cart to read
u8 numSecs IN: number of 512 byte sectors to read,
 1 to 256 sectors can be read, 0 = 256
void* buffer OUT: pointer to 512 byte buffer to store data in
bool return OUT: true if successful
-----------------------------------------------------------------*/
bool FCSR_ReadSectors (u32 sector, u8 numSecs, void* buffer)
{	
	int i;
	bool flagSramSector = false;
	int numSectors = (numSecs > 0 ? numSecs : 256);
	int readLength = numSectors * BYTE_PER_READ;
	u8* src;;
	u8* dst;

	// Find which region this read is in
	for (i = 0; (i < 4) && !flagSramSector; i++)
	{
		if ((sector >= FCSR_SramSectorStart[i]) && (sector < FCSR_SramSectorEnd[i]))
		{
			flagSramSector = true;
			break;
		}
	}

	// Make sure read will be completely in SRAM range if it is partially there
	if ( flagSramSector && ((sector + numSectors) > FCSR_SramSectorEnd[i]))
		return false;

	// Copy data to buffer
	if (flagSramSector)
	{
		src = FCSR_SramSectorPointer[i] + (sector - FCSR_SramSectorStart[i]) * BYTE_PER_READ;
	} else {
		src = FCSR_FileSysPointer + sector * BYTE_PER_READ;
	}
	dst = (u8*)buffer;

	if (flagSramSector)
	{
		while (readLength--)
		{
			*dst++ = *src++;
		}
	} else {	// Reading from Cart ROM

#ifdef _CF_USE_DMA
 #ifdef NDS
  #ifdef ARM9
		DC_FlushRange( buffer, readLength);
  #endif	// ARM9
		DMA3_SRC = (u32)src;
		DMA3_DEST = (u32)buffer;
		DMA3_CR = (readLength >> 1) | DMA_COPY_HALFWORDS;
 #else	// ! NDS
		DMA3COPY ( src, buffer, (readLength >> 1) | DMA16 | DMA_ENABLE);
 #endif	// NDS
#else	// !_CF_USE_DMA
		memcpy (buffer, src, readLength);
#endif	// _CF_USE_DMA

	}	// if (flagSramSector)

	return true;
}

/*-----------------------------------------------------------------
FCSR_WriteSectors
Write 512 byte sector numbered "sector" from "buffer"
u32 sector IN: address of 512 byte sector on Flash Cart to read
u8 numSecs IN: number of 512 byte sectors to read,
 1 to 256 sectors can be read, 0 = 256
void* buffer IN: pointer to 512 byte buffer to read data from
bool return OUT: true if successful
-----------------------------------------------------------------*/
bool FCSR_WriteSectors (u32 sector, u8 numSecs, void* buffer)
{
	int i;
	bool flagSramSector = false;
	int writeLength = (numSecs > 0 ? numSecs : 256) * BYTE_PER_READ;
	u8* src = (u8*) buffer;
	u8* dst;

	// Find which region this sector belongs in
	for (i = 0; (i < 4) && !flagSramSector; i++)
	{
		if ((sector >= FCSR_SramSectorStart[i]) && (sector < FCSR_SramSectorEnd[i]))
		{
			flagSramSector = true;
			break;
		}
	}

	if (!flagSramSector)
		return false;

	// Entire write must be within an SRAM region
	if ((sector + (numSecs > 0 ? numSecs : 256)) > FCSR_SramSectorEnd[i])
		return false;

	// Copy data to SRAM
	dst = FCSR_SramSectorPointer[i] + (sector - FCSR_SramSectorStart[i]) * BYTE_PER_READ;
	while (writeLength--)
	{
		*dst++ = *src++;
	}

	return true;
}

/*-----------------------------------------------------------------
FCSR_Shutdown
unload the Flash Cart interface
-----------------------------------------------------------------*/
bool FCSR_Shutdown(void) 
{
	int i;
	if (FCSR_ClearStatus() == false)
		return false;

	FCSR_FileSysPointer = 0;
	
	for (i=0; i < 4; i++)
	{
		FCSR_SramSectorPointer[i] = 0;
		FCSR_SramSectorStart[i] = 0;
		FCSR_SramSectorEnd[i] = 0;
	}
	return true;
}

/*-----------------------------------------------------------------
FCSR_StartUp
initializes the Flash Cart interface, returns true if successful,
otherwise returns false
-----------------------------------------------------------------*/
bool FCSR_StartUp(void)
{
	bool flagFoundFileSys = false;
	int i;
	int SramRegionSize[4];
	u8* srcByte;
	u8* destByte;

	u32* fileSysPointer = (u32*)0x08000100;		// Start at beginning of cart address space, offset by expected location of string

	// Search for file system
	while ((fileSysPointer < (u32*)0x0A000000) && !flagFoundFileSys)	// Only search while not at end of cart address space
	{
		while ((*fileSysPointer != FCSR) && (fileSysPointer < (u32*)0x0A000000))
			fileSysPointer += 0x40;
		if ((strncmp(FCSR_LabelString, (char*)(fileSysPointer + 1), 12) == 0) && (fileSysPointer < (u32*)0x0A000000))
		{
			flagFoundFileSys = true;
		} else {
			fileSysPointer += 0x80;
		}
	}

	if (!flagFoundFileSys)
		return false;

	// Flash cart file system pointer has been found
	FCSR_FileSysPointer = (u8*)(fileSysPointer - 0x40);

	// Get SRAM sector regions from header block
	for (i = 0; i < 4; i++)
	{
		FCSR_SramSectorStart[i] = fileSysPointer[i+4];	
		SramRegionSize[i] = fileSysPointer[i+8];
		FCSR_SramSectorEnd[i] = FCSR_SramSectorStart[i] + SramRegionSize[i];
	}
	
	// Calculate SRAM region pointers
	FCSR_SramSectorPointer[0] = (u8*)(SRAM_START + 4);
	for (i = 1; i < 4; i++)
	{
		FCSR_SramSectorPointer[i] = FCSR_SramSectorPointer[i-1] + (SramRegionSize[i-1] * BYTE_PER_READ);
	}

	// Initialise SRAM with overlay if it hasn't been done so
	if ( (*((u8*)SRAM_START) != 'F')  || (*((u8*)(SRAM_START+1)) != 'C') || (*((u8*)(SRAM_START+2)) != 'S') || (*((u8*)(SRAM_START+3)) != 'R') )
	{
		*((u8*)SRAM_START) = 'F';
		*((u8*)(SRAM_START+1)) = 'C';
		*((u8*)(SRAM_START+2)) = 'S';
		*((u8*)(SRAM_START+3)) = 'R';

		for (i = 0; i < 4; i++)
		{
			srcByte = FCSR_FileSysPointer + (FCSR_SramSectorStart[i] * BYTE_PER_READ);
			destByte = FCSR_SramSectorPointer[i];
			while (srcByte < FCSR_FileSysPointer + (FCSR_SramSectorEnd[i] * BYTE_PER_READ) )
				*destByte++ = *srcByte++;
		}
	}

		// Get SRAM sector regions from header block
	for (i = 0; i < 4; i++)
	{
		if (SramRegionSize[i] == 0)
		{
			FCSR_SramSectorStart[i] = NO_SRAM;
			FCSR_SramSectorEnd[i] = NO_SRAM;
		}
	}

	return true;
}

/*-----------------------------------------------------------------
the actual interface structure
-----------------------------------------------------------------*/
IO_INTERFACE io_fcsr = {
	DEVICE_TYPE_FCSR,	// 'FCSR'
	FEATURE_MEDIUM_CANREAD | FEATURE_MEDIUM_CANWRITE | FEATURE_SLOT_GBA,
	(FN_MEDIUM_STARTUP)&FCSR_StartUp,
	(FN_MEDIUM_ISINSERTED)&FCSR_IsInserted,
	(FN_MEDIUM_READSECTORS)&FCSR_ReadSectors,
	(FN_MEDIUM_WRITESECTORS)&FCSR_WriteSectors,
	(FN_MEDIUM_CLEARSTATUS)&FCSR_ClearStatus,
	(FN_MEDIUM_SHUTDOWN)&FCSR_Shutdown
} ;

/*-----------------------------------------------------------------
FCSR_GetInterface
returns the interface structure to host
-----------------------------------------------------------------*/
LPIO_INTERFACE FCSR_GetInterface(void) {
	return &io_fcsr ;
} ;

#endif // SUPPORT_FCSR