about summary refs log tree commit diff stats
path: root/gba/source/libSave.c
blob: d545a5f50f6a3053618aea0e8591dab3afee75cb (plain) (blame)
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/*
	libSave
	Cartridge backup memory save routines. To use, call the required
	routine with a pointer to an appropriately sized array of data to
	be read from or written to the cartridge.
	Data types are from wintermute's gba_types.h libgba library.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//---------------------------------------------------------------------------------
#ifndef	_gba_types_h_
#define	_gba_types_h_
//---------------------------------------------------------------------------------

//---------------------------------------------------------------------------------
// Data	types
//---------------------------------------------------------------------------------
/** Unsigned 8 bit value

*/
typedef	unsigned char			u8;
/** Unsigned 16 bit value

*/
typedef	unsigned short int		u16;
/** Unsigned 32 bit value

*/
typedef	unsigned int			u32;

/** signed 8 bit value

*/
typedef	signed char				s8;
/** Signed 16 bit value

*/
typedef	signed short int		s16;
/** Signed 32 bit value

*/
typedef	signed int				s32;

/** Unsigned volatile 8 bit value

*/
typedef	volatile u8				vu8;
/** Unsigned volatile 16 bit value

*/
typedef	volatile u16			vu16;
/** Unsigned volatile 32 bit value

*/
typedef	volatile u32			vu32;

/** Unsigned volatile 8 bit value

*/
typedef	volatile s8				vs8;
/** Signed volatile 16 bit value

*/
typedef	volatile s16			vs16;
/** Signed volatile 32 bit value

*/
typedef	volatile s32			vs32;

#ifndef __cplusplus
/** C++ compatible bool for C

*/
typedef enum { false, true } bool;
#endif

//---------------------------------------------------------------------------------
#endif // data types
//---------------------------------------------------------------------------------


#define EEPROM_ADDRESS (volatile u16*)0xDFFFF00 
#define SRAM_ADDRESS (volatile u16*)0x0E000000
#define FLASH_1M_ADDRESS (volatile u16*)0x09FE0000
#define REG_EEPROM  (*(volatile u16*)0xDFFFF00) 
#define REG_DM3SAD  (*(volatile u32*)0x40000D4) 
#define REG_DM3DAD  (*(volatile u32*)0x40000D8) 
#define REG_DM3CNT  (*(volatile u32*)0x40000DC)
#define REG_DM3CNT_H (*(volatile u16*)0x40000DE)
#define REG_WAITCNT (*(volatile u16*)0x4000204)

//-----------------------------------------------------------------------
// Common EEPROM Routines
//-----------------------------------------------------------------------

void EEPROM_SendPacket( u16* packet, int size ) 
{ 
   REG_WAITCNT = (REG_WAITCNT & 0xF8FF) | 0x0300;
   REG_DM3SAD = (u32)packet; 
   REG_DM3DAD = (u32)EEPROM_ADDRESS; 
   REG_DM3CNT = 0x80000000 + size; 
   while((REG_DM3CNT_H & 0x8000) != 0) ;
} 

void EEPROM_ReceivePacket( u16* packet, int size ) 
{ 
   REG_WAITCNT = (REG_WAITCNT & 0xF8FF) | 0x0300;
   REG_DM3SAD = (u32)EEPROM_ADDRESS; 
   REG_DM3DAD = (u32)packet; 
   REG_DM3CNT = 0x80000000 + size; 
   while((REG_DM3CNT_H & 0x8000) != 0) ;
} 

//-----------------------------------------------------------------------
// Routines for 512B EEPROM
//-----------------------------------------------------------------------

void EEPROM_Read_512B( volatile u8 offset, u8* dest ) // dest must point to 8 bytes 
{ 
   u16 packet[68]; 
   u8* out_pos; 
   u16* in_pos; 
   u8 out_byte; 
   int byte, bit; 

   memset( packet, 0, 68*2 );

   // Read request 
   packet[0] = 1; 
   packet[1] = 1; 

   // 6 bits eeprom address (MSB first) 
   packet[2] = (offset>>5)&1; 
   packet[3] = (offset>>4)&1; 
   packet[4] = (offset>>3)&1; 
   packet[5] = (offset>>2)&1; 
   packet[6] = (offset>>1)&1; 
   packet[7] = (offset)&1; 

   // End of request 
   packet[8] = 0; 

   // Do transfers 
   EEPROM_SendPacket( packet, 9 ); 
   memset( packet, 0, 68*2 );
   EEPROM_ReceivePacket( packet, 68 ); 
   
   // Extract data 
   in_pos = &packet[4]; 
   out_pos = dest; 
   for( byte = 7; byte >= 0; --byte ) 
   { 
      out_byte = 0; 
      for( bit = 7; bit >= 0; --bit ) 
      { 
//         out_byte += (*in_pos++)<<bit; 
		  out_byte += ((*in_pos++)&1)<<bit;
      } 
      *out_pos++ = out_byte ; 
   } 
} 

void EEPROM_Write_512B( volatile u8 offset, u8* source ) // source must point to 8 bytes 
{ 
   u16 packet[73]; 
   u8* in_pos; 
   u16* out_pos; 
   u8 in_byte; 
   int byte, bit; 

   memset( packet, 0, 73*2 );

   // Write request 
   packet[0] = 1; 
   packet[1] = 0; 

   // 6 bits eeprom address (MSB first) 
   packet[2] = (offset>>5)&1; 
   packet[3] = (offset>>4)&1; 
   packet[4] = (offset>>3)&1; 
   packet[5] = (offset>>2)&1; 
   packet[6] = (offset>>1)&1; 
   packet[7] = (offset)&1; 

   // Extract data 
   in_pos = source; 
   out_pos = &packet[8]; 
   for( byte = 7; byte >= 0; --byte ) 
   { 
      in_byte = *in_pos++; 
      for( bit = 7; bit >= 0; --bit ) 
      { 
         *out_pos++ = (in_byte>>bit)&1; 
      } 
   } 

   // End of request 
   packet[72] = 0; 

   // Do transfers 
   EEPROM_SendPacket( packet, 73 ); 

   // Wait for EEPROM to finish (should timeout after 10 ms) 
   while( (REG_EEPROM & 1) == 0 ); 
} 

//---------------------------------------------------------------------------------
void GetSave_EEPROM_512B(u8* data)
//---------------------------------------------------------------------------------
{
	volatile u8 x;
	u32 sleep;

	// Set up waitstates for EEPROM access etc. 
	*(volatile unsigned short *)0x04000204 = 0x4317; 

    for (x=0;x<64;++x){
		EEPROM_Read_512B(x,&data[x*8]);
		for(sleep=0;sleep<512000;sleep++);
	}
}

//---------------------------------------------------------------------------------
void PutSave_EEPROM_512B(u8* data)
//---------------------------------------------------------------------------------
{
	volatile u8 x;
	u32 sleep;

	// Set up waitstates for EEPROM access etc. 
	*(volatile unsigned short *)0x04000204 = 0x4317; 

    for (x=0;x<64;x++){ 
		EEPROM_Write_512B(x,&data[x*8]);
		for(sleep=0;sleep<512000;sleep++);
	}
}
//-----------------------------------------------------------------------
// Routines for 8KB EEPROM
//-----------------------------------------------------------------------

void EEPROM_Read_8KB( volatile u16 offset, u8* dest ) // dest must point to 8 bytes 
{ 
   u16 packet[68]; 
   u8* out_pos; 
   u16* in_pos; 
   u8 out_byte; 
   int byte, bit; 

   memset( packet, 0, 68*2 );

   // Read request 
   packet[0] = 1; 
   packet[1] = 1; 

   // 14 bits eeprom address (MSB first) 
   packet[2] = (offset>>13)&1; 
   packet[3] = (offset>>12)&1; 
   packet[4] = (offset>>11)&1; 
   packet[5] = (offset>>10)&1; 
   packet[6] = (offset>>9)&1; 
   packet[7] = (offset>>8)&1;
   packet[8] = (offset>>7)&1; 
   packet[9] = (offset>>6)&1; 
   packet[10] = (offset>>5)&1; 
   packet[11] = (offset>>4)&1; 
   packet[12] = (offset>>3)&1; 
   packet[13] = (offset>>2)&1;
   packet[14] = (offset>>1)&1;
   packet[15] = (offset)&1;

   // End of request 
   packet[16] = 0; 

   // Do transfers 
   EEPROM_SendPacket( packet, 17 ); 
   memset( packet, 0, 68*2 );
   EEPROM_ReceivePacket( packet, 68 ); 
   
   // Extract data 
   in_pos = &packet[4]; 
   out_pos = dest; 
   for( byte = 7; byte >= 0; --byte ) 
   { 
      out_byte = 0; 
      for( bit = 7; bit >= 0; --bit ) 
      { 
//         out_byte += (*in_pos++)<<bit; 
		  out_byte += ((*in_pos++)&1)<<bit;
      } 
      *out_pos++ = out_byte; 
   } 

} 

void EEPROM_Write_8KB( volatile u16 offset, u8* source ) // source must point to 8 bytes 
{ 
   u16 packet[81]; 
   u8* in_pos; 
   u16* out_pos; 
   u8 in_byte; 
   int byte, bit; 

   memset( packet, 0, 81*2 );
   
   // Write request 
   packet[0] = 1; 
   packet[1] = 0; 

   // 14 bits eeprom address (MSB first) 
   packet[2] = (offset>>13)&1; 
   packet[3] = (offset>>12)&1; 
   packet[4] = (offset>>11)&1; 
   packet[5] = (offset>>10)&1; 
   packet[6] = (offset>>9)&1; 
   packet[7] = (offset>>8)&1;
   packet[8] = (offset>>7)&1; 
   packet[9] = (offset>>6)&1; 
   packet[10] = (offset>>5)&1; 
   packet[11] = (offset>>4)&1; 
   packet[12] = (offset>>3)&1; 
   packet[13] = (offset>>2)&1;
   packet[14] = (offset>>1)&1;
   packet[15] = (offset)&1;

   // Extract data 
   in_pos = source; 
   out_pos = &packet[16]; 
   for( byte = 7; byte >= 0; --byte ) 
   { 
      in_byte = *in_pos++; 
      for( bit = 7; bit >= 0; --bit ) 
      { 
         *out_pos++ = (in_byte>>bit)&1; 
      } 
   } 

   // End of request 
   packet[80] = 0; 

   // Do transfers 
   EEPROM_SendPacket( packet, 81 ); 

   // Wait for EEPROM to finish (should timeout after 10 ms) 
   while( (REG_EEPROM & 1) == 0 ); 
} 

//---------------------------------------------------------------------------------
void GetSave_EEPROM_8KB(u8* data)
//---------------------------------------------------------------------------------
{
	volatile u16 x;
	u32 sleep;

	// Set up waitstates for EEPROM access etc. 
	*(volatile unsigned short *)0x04000204 = 0x4317; 

    for (x=0;x<1024;x++){ 
		EEPROM_Read_8KB(x,&data[x*8]);
		for(sleep=0;sleep<512000;sleep++);
	}

}

//---------------------------------------------------------------------------------
void PutSave_EEPROM_8KB(u8* data)
//---------------------------------------------------------------------------------
{
	volatile u16 x;
	u32 sleep;

	// Set up waitstates for EEPROM access etc. 
	*(volatile unsigned short *)0x04000204 = 0x4317; 

    for (x=0;x<1024;x++){ 
		EEPROM_Write_8KB(x,&data[x*8]);
		for(sleep=0;sleep<512000;sleep++);
	}
}

//---------------------------------------------------------------------------------
void GetSave_SRAM_32KB(u8* data)
//---------------------------------------------------------------------------------
{
	volatile u8 *sram= (u8*) 0x0E000000;
	volatile u16 x;

	for (x = 0; x < 0x8000; ++x)
	{
		data[x] = sram[x];
	}

}

//---------------------------------------------------------------------------------
void PutSave_SRAM_32KB(u8* data)
//---------------------------------------------------------------------------------
{
	volatile u8 *sram= (u8*) 0x0E000000;
	volatile u16 x;

	for (x = 0; x < 0x8000; ++x)
	{
		sram[x] = data[x];
	}
}

//---------------------------------------------------------------------------------
void GetSave_FLASH_64KB(u8* data)
//---------------------------------------------------------------------------------
{
	volatile u8 *sram= (u8*) 0x0E000000;
	volatile u32 x;

	for (x = 0; x < 0x10000; ++x)
	{
		data[x] = sram[x];
	}
}

//---------------------------------------------------------------------------------
void PutSave_FLASH_64KB(u8* foo)
//---------------------------------------------------------------------------------
{
	volatile u8 *fctrl0 = (u8*) 0xE005555; 
	volatile u8 *fctrl1 = (u8*) 0xE002AAA; 
	volatile u8 *fctrl2 = (u8*) 0xE000000; 

	//init flash 
	*fctrl0 = 0xAA; 
	*fctrl1 = 0x55; 
	*fctrl0 = 0x90; 
	*fctrl2 = 0xF0; 

	//erase chip 
	*fctrl0 = 0xAA; 
	*fctrl1 = 0x55; 
	*fctrl0 = 0x80; 
	*fctrl0 = 0xAA; 
	*fctrl1 = 0x55; 
	*fctrl0 = 0x10; 

	//wait for erase done 
	u8 val1;
	u8 val2; 
	val1 = *fctrl2;
	val2 = *fctrl2; 
	while (val1 != val2) {
		val1 = *fctrl2;
		val2 = *fctrl2;
	}
	val1 = *fctrl2;
	val2 = *fctrl2; 
	while (val1 != val2) {
		val1 = *fctrl2;
		val2 = *fctrl2; 
	}

	volatile u8 *data = fctrl2; 
	u32 i; 
	//write data 
	for (i=0; i<65536; i++) { 
		*fctrl0 = 0xAA; 
		*fctrl1 = 0x55; 
		*fctrl0 = 0xA0; 
		data [i] = foo [ i ]; 
		val1 = data [ i ]; 
		val2 = data [ i ]; 
	
		while (val1 != val2) {
			val1 = data [ i ]; 
			val2 = data [ i ]; 
		}
		val1 = data [ i ]; 
		val2 = data [ i ]; 
		while (val1 != val2) {
			val1 = data [ i ];
			val2 = data [ i ]; 
		} 
		val1 = data [ i ]; 
		val2 = data [ i ]; 
		while (val1 != val2) {
			val1 = data [ i ]; 
			val2 = data [ i ]; 
		} 
	}
}

//---------------------------------------------------------------------------------
void GetSave_FLASH_128KB(u8* data)
//---------------------------------------------------------------------------------
{
	const u32 size = 0x10000;
	
	volatile u8 *fctrl0 = (u8*) 0xE005555; 
	volatile u8 *fctrl1 = (u8*) 0xE002AAA; 
	volatile u8 *fctrl2 = (u8*) 0xE000000; 
	volatile u32 i; 
	volatile u8 *sram= (u8*) 0x0E000000;
	
	//init flash 
	*fctrl0 = 0xAA; 
	*fctrl1 = 0x55; 
	*fctrl0 = 0x90; 
	*fctrl2 = 0xF0; 

	// read first bank
	*fctrl0 = 0xAA;
	*fctrl1 = 0x55;
	*fctrl0 = 0xB0;
	*fctrl2 = 0x00;
	
	for (i=0; i<size; i++){
		data[i] = sram[i];
	}

	// read second bank
	*fctrl0 = 0xAA;
	*fctrl1 = 0x55;
	*fctrl0 = 0xB0;
	*fctrl2 = 0x01;
	
	for (i=0; i<size; i++){
		data[i + size] = sram[i];
	}

}

//---------------------------------------------------------------------------------
void PutSave_FLASH_128KB(u8* foo)
//---------------------------------------------------------------------------------
{
	volatile u8 *fctrl0 = (u8*) 0xE005555; 
	volatile u8 *fctrl1 = (u8*) 0xE002AAA; 
	volatile u8 *fctrl2 = (u8*) 0xE000000; 

	u8 val1;
	u8 val2; 

	//init flash 
	*fctrl0 = 0xAA; 
	*fctrl1 = 0x55; 
	*fctrl0 = 0x90; 
	*fctrl2 = 0xF0; 

	//erase chip 
	*fctrl0 = 0xAA; 
	*fctrl1 = 0x55; 
	*fctrl0 = 0x80; 
	*fctrl0 = 0xAA; 
	*fctrl1 = 0x55; 
	*fctrl0 = 0x10; 
	
	//wait for erase done 
	val1 = *fctrl2;
	val2 = *fctrl2; 
	while (val1 != val2) {
		val1 = *fctrl2;
		val2 = *fctrl2;
	}
	val1 = *fctrl2;
	val2 = *fctrl2; 
	while (val1 != val2) {
		val1 = *fctrl2;
		val2 = *fctrl2; 
	}

	volatile u8 *data = fctrl2; 
	volatile u32 i; 
	// change to bank 0
	*fctrl0 = 0xAA;
	*fctrl1 = 0x55;
	*fctrl0 = 0xB0;
	*fctrl2 = 0x00;
		
	//write data 
	for (i=0; i<65536; i++) { 
		*fctrl0 = 0xAA; 
		*fctrl1 = 0x55; 
		*fctrl0 = 0xA0; 
		data [i] = foo [ i ]; 
		val1 = data [ i ]; 
		val2 = data [ i ]; 
	
		while (val1 != val2) {
			val1 = data [ i ]; 
			val2 = data [ i ]; 
		}
		val1 = data [ i ]; 
		val2 = data [ i ]; 
		while (val1 != val2) {
			val1 = data [ i ];
			val2 = data [ i ]; 
		} 
		val1 = data [ i ]; 
		val2 = data [ i ]; 
		while (val1 != val2) {
			val1 = data [ i ]; 
			val2 = data [ i ]; 
		} 
	}

	// Change to bank 1
	*fctrl0 = 0xAA;
	*fctrl1 = 0x55;
	*fctrl0 = 0xB0;
	*fctrl2 = 0x01;
	
	//write data 
	for (i=0; i<65536; i++) { 
		*fctrl0 = 0xAA; 
		*fctrl1 = 0x55; 
		*fctrl0 = 0xA0; 
		data [i] = foo [ i + 0x10000]; 
		val1 = data [ i ]; 
		val2 = data [ i ]; 
	
		while (val1 != val2) {
			val1 = data [ i ]; 
			val2 = data [ i ]; 
		}
		val1 = data [ i ]; 
		val2 = data [ i ]; 
		while (val1 != val2) {
			val1 = data [ i ];
			val2 = data [ i ]; 
		} 
		val1 = data [ i ]; 
		val2 = data [ i ]; 
		while (val1 != val2) {
			val1 = data [ i ]; 
			val2 = data [ i ]; 
		} 
	}

}

//---------------------------------------------------------------------------------
u32 SaveSize(u8* data, s32 gamesize)
//---------------------------------------------------------------------------------
{
	if(gamesize < 0)
	 return 0;

	u32 *pak= ((u32*)0x08000000);
	s32 x;
	u16 i;
	s32 size = gamesize/4;


	for (x=size-1;x>=0;x--){
		switch (pak[x]) {
		case 0x53414C46:
			if (pak[x+1] == 0x5F4D3148){
				return 0x20000;					// FLASH_128KB
			} else if ((pak[x+1] & 0x0000FFFF) == 0x00005F48){
				return 0x10000;					// FLASH_64KB
			} else if (pak[x+1] == 0x32313548){
				return 0x10000;					// FLASH_64KB
			}
			break;
		case 0x52504545:
			if ((pak[x+1] & 0x00FFFFFF) == 0x005F4D4F){
				GetSave_EEPROM_8KB(data);
				for(i = 8; i < 0x800; i += 8) {
					if(memcmp(data, data+i, 8) != 0)
						return 0x2000;			// EEPROM_8KB
				}
				return 0x200;					// EEPROM_512B
			}
			break;
		case 0x4D415253:
			if ((pak[x+1] & 0x000000FF) == 0x0000005F){
				return 0x8000;					// SRAM_32KB
			}
		}
	}
	return 0;
}