-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpayload.c
933 lines (818 loc) · 21.2 KB
/
payload.c
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
#define AGB_ROM ((unsigned char*)0x8000000)
#define AGB_SRAM ((volatile unsigned char*)0xE000000)
#define SRAM_SIZE 64
#define AGB_SRAM_SIZE SRAM_SIZE*1024
#define SRAM_BANK_SEL (*(volatile unsigned short*) 0x09000000)
#define _FLASH_WRITE(pa, pd) { *(((unsigned short *)AGB_ROM)+((pa)/2)) = pd; __asm("nop"); }
asm(R"(.text
original_entrypoint:
.word 0x080000c0
flush_mode:
.word 0
save_size:
.word 0x20000
.word patched_entrypoint
.word write_sram_patched + 1
.word write_eeprom_patched + 1
.word write_flash_patched + 1
.word write_eeprom_v111_posthook + 1
.thumb
# If you are writing a manual batteryless save patch, you can branch here
# Return via LR, only LR is trashed
.type flush_sram_manual_entry, %function
flush_sram_manual_entry:
push {r0, r1, r2, r3, r4}
push {lr}
ldr r4, =0x04000208
ldrh r0, [r4]
push {r0}
mov r0, # 0
strh r0, [r4]
adr r0, flush_sram
mov lr, pc
bx r0
pop {r0}
strh r0, [r4]
pop {r0}
mov lr, r0
pop {r0, r1, r2, r3, r4}
# feel free to put any housekeeping before you return to your injected branch here
nop
nop
nop
nop
bx lr
nop
nop
nop
nop
.ltorg
.arm
patched_entrypoint:
mov r0, # 0x04000000
ldr r1, flush_mode
cmp r1, # 0
adr r1, idle_irq_handler
adrne r1, keypad_irq_handler
str r1, [r0, # -4]
adrl r0, flash_save_sector
mov r1, # 0x0e000000
ldr r2, save_size
add r2, r1
mov r3, # 0x09000000
# Lock 369in1 mapper
mov r4, # 0x80
strb r4, [r1, # 3]
sram_init_loop:
lsr r4, r1, # 16
and r4, # 1
strh r4, [r3]
nop
ldrb r4, [r0], # 1
strb r4, [r1], # 1
cmp r1, r2
blo sram_init_loop
# Set bank to 0 for banking-unaware software
mov r4, # 0
strh r4, [r3]
ldr pc, original_entrypoint
.thumb
# r0 = sector number, # r1 = source data 0x1000 bytes
.type write_flash_patched, %function
write_flash_patched:
lsl r0, # 12
mov r2, # 0x0e
lsl r2, # 24
orr r0, r2
mov r2, # 0x1
lsl r2, # 12
mov r3, r0
mov r0, r1
mov r1, r3
b write_sram_patched
# r0 = src, r1 = dst, r2 = size. Check if change before writing, only install irq if change
# unoptimised as hell, but I don't care for now.
.type write_sram_patched, %function
write_sram_patched:
push {lr}
push {r4, r5, r6, r7}
# Disable interrupts while writing - just in case
ldr r6, =0x04000208
ldrh r7, [r6]
mov r3, # 0
strh r3, [r6]
# Writes will never span both SRAM banks, so only needed to write once.
mov r4, # 0x09
lsl r4, # 24
lsr r5, r1, # 16
mov r3, # 1
and r5, r3
strh r5, [r4]
add r2, r0
write_sram_patched_loop:
# Check if the each byte to write to sram is different - if it is, write it then set a flag
ldrb r4, [r0]
ldrb r5, [r1]
cmp r4, r5
beq (.+6)
mov r3, # 1
strb r4, [r1]
add r0, # 1
add r1, # 1
cmp r0, r2
blo write_sram_patched_loop
# If the flag was not set, the function had no effect. Short circuit
cmp r3, # 0
beq write_sram_patched_exit
# Install the chosen irq handler and initialise countdown value if needed.
mov r0, pc
sub r0, # . + 2 - flush_mode
ldrh r0, [r0]
cmp r0, # 0
bne write_sram_patched_exit
bl install_countdown_handler
write_sram_patched_exit:
strh r7, [r6]
mov r0, # 0
pop {r4, r5, r6, r7}
pop {r1}
bx r1
.ltorg
# r0 = eeprom address, r1 = src data (needs byte swapping, 8 bytes)
.type write_eeprom_patched, %function
write_eeprom_patched:
push {r4, lr}
mov r2, r1
add r2, # 8
mov r3, sp
write_eeprom_patched_byte_swap_loop:
ldrb r4, [r1]
add r1, # 1
sub r3, # 1
strb r4, [r3]
cmp r1, r2
bne write_eeprom_patched_byte_swap_loop
mov r1, # 0x0e
lsl r1, # 24
lsl r0, # 3
add r1, r0
mov r2, # 8
mov r0, r3
mov sp, r3
bl write_sram_patched
add sp, # 8
pop {r4, pc}
.type write_eeprom_patched, %function
write_eeprom_v111_posthook:
push {r0}
bl install_countdown_handler
pop {r0}
bx r0
install_countdown_handler:
adr r0, countdown_irq_handler
mov r1, # 0x04
lsl r1, # 24
sub r1, # 0x10
mov r2, # 102
strh r2, [r1, # 0x0a]
str r0, [r1, # 0x0c]
# Set green swap as a visual indicator that the countdown has begun
strh r2, [r1, # 0x12]
bx lr
.arm
# IRQ handlers are called with 0x04000000 in r0 which is handy!
keypad_irq_handler:
# Check keypad register for L+R+START+SELECT
# May need to be changed to ldrh
ldr r3, [r0, # 0x130]
teq r3, # 0xf3
ldrne pc, [r0, # - 12]
# Enable green swap
mov r1, # 1
strh r1, [r0, # 2]
# Switch to system mode to get lots of stack
mov r3, # 0x9f
msr cpsr, r3
push {lr}
bl flush_sram
pop {lr}
# return to irq mode
mov r3, # 0x92
msr cpsr, r3
# Disable green swap
mov r0, # 0x04000000
strh r0, [r0, # 2]
# Wait until keypad register is no longer L+R+START+SELECT
ldr r3, [r0, # 0x130]
teq r3, # 0xf3
beq (.-8)
ldr pc, [r0, # - 12]
countdown_irq_handler:
# if not vblank IF then user handler
ldr r1, [r0, # 0x200]
tst r1, # 0x00010000
ldreq pc, [r0, # -12]
# if (--counter) then user handler
ldrh r1, [r0, # - 6]
subs r1, # 1
strh r1, [r0, # - 6]
ldrne pc, [r0, # -12]
# Switch to system mode to get lots of stack
mov r3, # 0x9f
msr cpsr, r3
push {lr}
bl flush_sram
pop {lr}
# return to irq mode
mov r3, # 0x92
msr cpsr, r3
# Disable green swap
mov r0, # 0x04000000
strh r0, [r0, # 0x02]
# Uninstall countdown irq handler
adr r1, idle_irq_handler
str r1, [r0, # - 4]
# Continue IRQ handler
b idle_irq_handler
idle_irq_handler:
ldr pc, [r0, # -12]
# Ensure interrupts are disabled and there is plenty of stack space before calling
flush_sram:
mov r0, # 0x04000000
# save sound state then disable it
ldrh r2, [r0, # 0x0080]
ldrh r3, [r0, # 0x0084]
push {r2, r3}
strh r0, [r0, # 0x0084]
# save DMAs state then disable them
ldrh r3, [r0, # 0x00BA]
push {r3}
strh r0, [r0, # 0x00BA]
ldrh r3, [r0, # 0x00C6]
push {r3}
strh r0, [r0, # 0x00C6]
ldrh r3, [r0, # 0x00d2]
push {r3}
strh r0, [r0, # 0x00d2]
ldrh r3, [r0, # 0x00de]
push {r3}
strh r0, [r0, # 0x00de]
push {lr}
# Try flushing for various flash chips
push {r4, r5, r6, r7}
adrl r4, flash_save_sector
sub r4, # 0x08000000
ldr r5, save_size
adr r6, flash_fn_table
adr r7, original_entrypoint
try_flash:
ldm r6!, {r2, r3}
cmp r2, # 0
beq flush_sram_done
add r2, r7
add r3, r7
bl run_from_ram
cmp r0, #0
bne found_flash
add r6, # 16
b try_flash
found_flash:
ldm r6!, {r2, r3}
mov r0, r4
mov r1, r5
add r2, r7
add r3, r7
bl run_from_ram
ldm r6!, {r2, r3}
mov r0, r4
mov r1, r5
add r2, r7
add r3, r7
bl run_from_ram
flush_sram_done:
pop {r4, r5, r6, r7}
pop {lr}
mov r0, #0x04000000
# restore DMAs state
pop {r3}
strh r3, [r0, # 0x00de]
pop {r3}
strh r3, [r0, # 0x00d2]
pop {r3}
strh r3, [r0, # 0x00c6]
pop {r3}
strh r3, [r0, # 0x00ba]
# restore sound state
pop {r2, r3}
strh r3, [r0, # 0x0084]
strh r2, [r0, # 0x0080]
bx lr
flash_fn_table:
.word identify_flash_1
.word identify_flash_1_end
.word erase_flash_1
.word erase_flash_1_end
.word program_flash_1
.word program_flash_1_end
.word identify_flash_4
.word identify_flash_4_end
.word erase_flash_4
.word erase_flash_4_end
.word program_flash_4
.word program_flash_4_end
.word identify_flash_2
.word identify_flash_2_end
.word erase_flash_2
.word erase_flash_2_end
.word program_flash_2
.word program_flash_2_end
.word identify_flash_3
.word identify_flash_3_end
.word erase_flash_3
.word erase_flash_3_end
.word program_flash_3
.word program_flash_3_end
.zero 12
run_from_ram:
push {r4, r5, lr}
mov r4, sp
bic r2, # 1
run_from_ram_loop:
ldr r5, [r3, # -4]!
push {r5}
cmp r2, r3
bne run_from_ram_loop
add r2, sp, # 1
mov lr, pc
bx r2
mov sp, r4
pop {r4, r5, lr}
bx lr
)");
int identify_flash_1()
{
unsigned rom_data, data;
//stop_dma_interrupts();
rom_data = *(unsigned *)AGB_ROM;
// Type 1 or 4
_FLASH_WRITE(0, 0xFF);
_FLASH_WRITE(0, 0x90);
data = *(unsigned *)AGB_ROM;
_FLASH_WRITE(0, 0xFF);
if (rom_data != data) {
// Check if the chip is responding to this command
// which then needs a different write command later
_FLASH_WRITE(0x59, 0x42);
data = *(unsigned char *)(AGB_ROM+0xB2);
_FLASH_WRITE(0x59, 0x96);
_FLASH_WRITE(0, 0xFF);
if (data != 0x96) {
//resume_interrupts();
for (volatile int i = 0; i < 1024; ++i)
__asm("nop");
return 0;
}
//resume_interrupts();
return 1;
}
return 0;
}
asm("identify_flash_1_end:");
void erase_flash_1(unsigned sa, unsigned save_size)
{
// Erase flash sector
_FLASH_WRITE(sa, 0xFF);
_FLASH_WRITE(sa, 0x60);
_FLASH_WRITE(sa, 0xD0);
_FLASH_WRITE(sa, 0x20);
_FLASH_WRITE(sa, 0xD0);
while (1) {
__asm("nop");
if (*(((unsigned short *)AGB_ROM)+(sa/2)) == 0x80) {
break;
}
}
_FLASH_WRITE(sa, 0xFF);
}
asm("erase_flash_1_end:");
void program_flash_1(unsigned sa, unsigned save_size)
{
// Write data
SRAM_BANK_SEL = 0;
for (int i=0; i<save_size; i+=2) {
if (i == AGB_SRAM_SIZE)
SRAM_BANK_SEL = 1;
_FLASH_WRITE(sa+i, 0x40);
_FLASH_WRITE(sa+i, (*(unsigned char *)(AGB_SRAM+i+1)) << 8 | (*(unsigned char *)(AGB_SRAM+i)));
while (1) {
__asm("nop");
if (*(((unsigned short *)AGB_ROM)+(sa/2)) == 0x80) {
break;
}
}
}
_FLASH_WRITE(sa, 0xFF);
}
asm("program_flash_1_end:");
int identify_flash_2()
{
unsigned rom_data, data;
//stop_dma_interrupts();
rom_data = *(unsigned *)AGB_ROM;
_FLASH_WRITE(0, 0xF0);
_FLASH_WRITE(0xAAA, 0xA9);
_FLASH_WRITE(0x555, 0x56);
_FLASH_WRITE(0xAAA, 0x90);
data = *(unsigned *)AGB_ROM;
_FLASH_WRITE(0, 0xF0);
if (rom_data != data) {
//resume_interrupts();
return 1;
}
return 0;
}
asm("identify_flash_2_end:");
void erase_flash_2(unsigned sa, unsigned save_size)
{
// Erase flash sector
_FLASH_WRITE(sa, 0xF0);
_FLASH_WRITE(0xAAA, 0xA9);
_FLASH_WRITE(0x555, 0x56);
_FLASH_WRITE(0xAAA, 0x80);
_FLASH_WRITE(0xAAA, 0xA9);
_FLASH_WRITE(0x555, 0x56);
_FLASH_WRITE(sa, 0x30);
while (1) {
__asm("nop");
if (*(((unsigned short *)AGB_ROM)+(sa/2)) == 0xFFFF) {
break;
}
}
_FLASH_WRITE(sa, 0xF0);
}
asm("erase_flash_2_end:");
void program_flash_2(unsigned sa, unsigned save_size)
{
// Write data
SRAM_BANK_SEL = 0;
for (int i=0; i<save_size; i+=2) {
if (i == AGB_SRAM_SIZE)
SRAM_BANK_SEL = 1;
_FLASH_WRITE(0xAAA, 0xA9);
_FLASH_WRITE(0x555, 0x56);
_FLASH_WRITE(0xAAA, 0xA0);
_FLASH_WRITE(sa+i, (*(unsigned char *)(AGB_SRAM+i+1)) << 8 | (*(unsigned char *)(AGB_SRAM+i)));
while (1) {
__asm("nop");
if (*(((unsigned short *)AGB_ROM)+((sa+i)/2)) == ((*(unsigned char *)(AGB_SRAM+i+1)) << 8 | (*(unsigned char *)(AGB_SRAM+i)))) {
break;
}
}
}
_FLASH_WRITE(sa, 0xF0);
}
asm("program_flash_2_end:");
int identify_flash_3()
{
unsigned rom_data, data;
//stop_dma_interrupts();
rom_data = *(unsigned *)AGB_ROM;
_FLASH_WRITE(0, 0xF0);
_FLASH_WRITE(0xAAA, 0xAA);
_FLASH_WRITE(0x555, 0x55);
_FLASH_WRITE(0xAAA, 0x90);
data = *(unsigned *)AGB_ROM;
_FLASH_WRITE(0, 0xF0);
if (rom_data != data) {
//resume_interrupts();
return 1;
}
return 0;
}
asm("identify_flash_3_end:");
void erase_flash_3(unsigned sa, unsigned save_size)
{
// Erase flash sector
_FLASH_WRITE(sa, 0xF0);
_FLASH_WRITE(0xAAA, 0xAA);
_FLASH_WRITE(0x555, 0x55);
_FLASH_WRITE(0xAAA, 0x80);
_FLASH_WRITE(0xAAA, 0xAA);
_FLASH_WRITE(0x555, 0x55);
_FLASH_WRITE(sa, 0x30);
while (1) {
__asm("nop");
if (*(((unsigned short *)AGB_ROM)+(sa/2)) == 0xFFFF) {
break;
}
}
_FLASH_WRITE(sa, 0xF0);
}
asm("erase_flash_3_end:");
void program_flash_3(unsigned sa, unsigned save_size)
{
// Write data
SRAM_BANK_SEL = 0;
for (int i=0; i<save_size; i+=2) {
if (i == AGB_SRAM_SIZE)
SRAM_BANK_SEL = 1;
_FLASH_WRITE(0xAAA, 0xAA);
_FLASH_WRITE(0x555, 0x55);
_FLASH_WRITE(0xAAA, 0xA0);
_FLASH_WRITE(sa+i, (*(unsigned char *)(AGB_SRAM+i+1)) << 8 | (*(unsigned char *)(AGB_SRAM+i)));
while (1) {
__asm("nop");
if (*(((unsigned short *)AGB_ROM)+((sa+i)/2)) == ((*(unsigned char *)(AGB_SRAM+i+1)) << 8 | (*(unsigned char *)(AGB_SRAM+i)))) {
break;
}
}
}
_FLASH_WRITE(sa, 0xF0);
}
asm("program_flash_3_end:");
int identify_flash_4()
{
unsigned rom_data, data;
//stop_dma_interrupts();
rom_data = *(unsigned *)AGB_ROM;
// Type 1 or 4
_FLASH_WRITE(0, 0xFF);
_FLASH_WRITE(0, 0x90);
data = *(unsigned *)AGB_ROM;
_FLASH_WRITE(0, 0xFF);
if (rom_data != data) {
// Check if the chip is responding to this command
// which then needs a different write command later
_FLASH_WRITE(0x59, 0x42);
data = *(unsigned char *)(AGB_ROM+0xB2);
_FLASH_WRITE(0x59, 0x96);
_FLASH_WRITE(0, 0xFF);
if (data != 0x96) {
//resume_interrupts();
for (volatile int i = 0; i < 1024; ++i)
__asm("nop");
return 1;
}
}
return 0;
}
asm("identify_flash_4_end:");
void erase_flash_4(unsigned sa, unsigned save_size)
{
// Erase flash sector
_FLASH_WRITE(sa, 0xFF);
_FLASH_WRITE(sa, 0x60);
_FLASH_WRITE(sa, 0xD0);
_FLASH_WRITE(sa, 0x20);
_FLASH_WRITE(sa, 0xD0);
while (1) {
__asm("nop");
if ((*(((unsigned short *)AGB_ROM)+(sa/2)) & 0x80) == 0x80) {
break;
}
}
_FLASH_WRITE(sa, 0xFF);
for (volatile int i = 0; i < 1024; ++i)
__asm("nop");
}
asm("erase_flash_4_end:");
void program_flash_4(unsigned sa, unsigned save_size)
{
// Write data
int c = 0;
SRAM_BANK_SEL = 0;
while (c < save_size) {
if (c == AGB_SRAM_SIZE)
SRAM_BANK_SEL = 1;
_FLASH_WRITE(sa+c, 0xEA);
while (1) {
__asm("nop");
if ((*(((unsigned short *)AGB_ROM)+((sa+c)/2)) & 0x80) == 0x80) {
break;
}
}
_FLASH_WRITE(sa+c, 0x1FF);
for (int i=0; i<1024; i+=2) {
_FLASH_WRITE(sa+c+i, (*(unsigned char *)(AGB_SRAM+c+i+1)) << 8 | (*(unsigned char *)(AGB_SRAM+c+i)));
}
_FLASH_WRITE(sa+c, 0xD0);
while (1) {
__asm("nop");
if ((*(((unsigned short *)AGB_ROM)+((sa+c)/2)) & 0x80) == 0x80) {
break;
}
}
_FLASH_WRITE(sa+c, 0xFF);
c += 1024;
}
for (volatile int i = 0; i < 1024; ++i)
__asm("nop");
}
asm("program_flash_4_end:");
/*
void flush_sram_ram(unsigned sa, unsigned save_size)
{
unsigned flash_type = 0;
unsigned rom_data, data;
//stop_dma_interrupts();
rom_data = *(unsigned *)AGB_ROM;
// Type 1 or 4
_FLASH_WRITE(0, 0xFF);
_FLASH_WRITE(0, 0x90);
data = *(unsigned *)AGB_ROM;
_FLASH_WRITE(0, 0xFF);
if (rom_data != data) {
// Check if the chip is responding to this command
// which then needs a different write command later
_FLASH_WRITE(0x59, 0x42);
data = *(unsigned char *)(AGB_ROM+0xB2);
_FLASH_WRITE(0x59, 0x96);
_FLASH_WRITE(0, 0xFF);
if (data != 0x96) {
//resume_interrupts();
flash_type = 4;
goto identified;
}
//resume_interrupts();
flash_type = 1;
goto identified;
}
// Type 2
_FLASH_WRITE(0, 0xF0);
_FLASH_WRITE(0xAAA, 0xA9);
_FLASH_WRITE(0x555, 0x56);
_FLASH_WRITE(0xAAA, 0x90);
data = *(unsigned *)AGB_ROM;
_FLASH_WRITE(0, 0xF0);
if (rom_data != data) {
//resume_interrupts();
flash_type = 2;
goto identified;
}
// Type 3
_FLASH_WRITE(0, 0xF0);
_FLASH_WRITE(0xAAA, 0xAA);
_FLASH_WRITE(0x555, 0x55);
_FLASH_WRITE(0xAAA, 0x90);
data = *(unsigned *)AGB_ROM;
_FLASH_WRITE(0, 0xF0);
if (rom_data != data) {
//resume_interrupts();
flash_type = 3;
goto identified;
}
identified:
if (flash_type == 0) return;
for (volatile int i = 0; i < 1024; ++i)
__asm("nop");
if (flash_type == 1) {
// Erase flash sector
_FLASH_WRITE(sa, 0xFF);
_FLASH_WRITE(sa, 0x60);
_FLASH_WRITE(sa, 0xD0);
_FLASH_WRITE(sa, 0x20);
_FLASH_WRITE(sa, 0xD0);
while (1) {
__asm("nop");
if (*(((unsigned short *)AGB_ROM)+(sa/2)) == 0x80) {
break;
}
}
_FLASH_WRITE(sa, 0xFF);
// Write data
SRAM_BANK_SEL = 0;
for (int i=0; i<save_size; i+=2) {
if (i == AGB_SRAM_SIZE)
SRAM_BANK_SEL = 1;
_FLASH_WRITE(sa+i, 0x40);
_FLASH_WRITE(sa+i, (*(unsigned char *)(AGB_SRAM+i+1)) << 8 | (*(unsigned char *)(AGB_SRAM+i)));
while (1) {
__asm("nop");
if (*(((unsigned short *)AGB_ROM)+(sa/2)) == 0x80) {
break;
}
}
}
_FLASH_WRITE(sa, 0xFF);
} else if (flash_type == 2) {
// Erase flash sector
_FLASH_WRITE(sa, 0xF0);
_FLASH_WRITE(0xAAA, 0xA9);
_FLASH_WRITE(0x555, 0x56);
_FLASH_WRITE(0xAAA, 0x80);
_FLASH_WRITE(0xAAA, 0xA9);
_FLASH_WRITE(0x555, 0x56);
_FLASH_WRITE(sa, 0x30);
while (1) {
__asm("nop");
if (*(((unsigned short *)AGB_ROM)+(sa/2)) == 0xFFFF) {
break;
}
}
_FLASH_WRITE(sa, 0xF0);
// Write data
SRAM_BANK_SEL = 0;
for (int i=0; i<save_size; i+=2) {
if (i == AGB_SRAM_SIZE)
SRAM_BANK_SEL = 1;
_FLASH_WRITE(0xAAA, 0xA9);
_FLASH_WRITE(0x555, 0x56);
_FLASH_WRITE(0xAAA, 0xA0);
_FLASH_WRITE(sa+i, (*(unsigned char *)(AGB_SRAM+i+1)) << 8 | (*(unsigned char *)(AGB_SRAM+i)));
while (1) {
__asm("nop");
if (*(((unsigned short *)AGB_ROM)+((sa+i)/2)) == ((*(unsigned char *)(AGB_SRAM+i+1)) << 8 | (*(unsigned char *)(AGB_SRAM+i)))) {
break;
}
}
}
_FLASH_WRITE(sa, 0xF0);
} else if (flash_type == 3) {
// Erase flash sector
_FLASH_WRITE(sa, 0xF0);
_FLASH_WRITE(0xAAA, 0xAA);
_FLASH_WRITE(0x555, 0x55);
_FLASH_WRITE(0xAAA, 0x80);
_FLASH_WRITE(0xAAA, 0xAA);
_FLASH_WRITE(0x555, 0x55);
_FLASH_WRITE(sa, 0x30);
while (1) {
__asm("nop");
if (*(((unsigned short *)AGB_ROM)+(sa/2)) == 0xFFFF) {
break;
}
}
_FLASH_WRITE(sa, 0xF0);
// Write data
SRAM_BANK_SEL = 0;
for (int i=0; i<save_size; i+=2) {
if (i == AGB_SRAM_SIZE)
SRAM_BANK_SEL = 1;
_FLASH_WRITE(0xAAA, 0xAA);
_FLASH_WRITE(0x555, 0x55);
_FLASH_WRITE(0xAAA, 0xA0);
_FLASH_WRITE(sa+i, (*(unsigned char *)(AGB_SRAM+i+1)) << 8 | (*(unsigned char *)(AGB_SRAM+i)));
while (1) {
__asm("nop");
if (*(((unsigned short *)AGB_ROM)+((sa+i)/2)) == ((*(unsigned char *)(AGB_SRAM+i+1)) << 8 | (*(unsigned char *)(AGB_SRAM+i)))) {
break;
}
}
}
_FLASH_WRITE(sa, 0xF0);
} else if (flash_type == 4) {
// Erase flash sector
_FLASH_WRITE(sa, 0xFF);
_FLASH_WRITE(sa, 0x60);
_FLASH_WRITE(sa, 0xD0);
_FLASH_WRITE(sa, 0x20);
_FLASH_WRITE(sa, 0xD0);
while (1) {
__asm("nop");
if ((*(((unsigned short *)AGB_ROM)+(sa/2)) & 0x80) == 0x80) {
break;
}
}
_FLASH_WRITE(sa, 0xFF);
// Write data
int c = 0;
SRAM_BANK_SEL = 0;
while (c < save_size) {
if (c == AGB_SRAM_SIZE)
SRAM_BANK_SEL = 1;
_FLASH_WRITE(sa+c, 0xEA);
while (1) {
__asm("nop");
if ((*(((unsigned short *)AGB_ROM)+((sa+c)/2)) & 0x80) == 0x80) {
break;
}
}
_FLASH_WRITE(sa+c, 0x1FF);
for (int i=0; i<1024; i+=2) {
_FLASH_WRITE(sa+c+i, (*(unsigned char *)(AGB_SRAM+c+i+1)) << 8 | (*(unsigned char *)(AGB_SRAM+c+i)));
}
_FLASH_WRITE(sa+c, 0xD0);
while (1) {
__asm("nop");
if ((*(((unsigned short *)AGB_ROM)+((sa+c)/2)) & 0x80) == 0x80) {
break;
}
}
_FLASH_WRITE(sa+c, 0xFF);
c += 1024;
}
}
//resume_interrupts();
}
asm("flush_sram_ram_end:");
*/
asm(R"(
# The following footer must come last.
.balign 4
.ascii "<3 from Maniac"
# Size of payload
.hword (.+2)
.balign 4
flash_save_sector:
.end
)");