/
main.c
1164 lines (1051 loc) · 22.7 KB
/
main.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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "includes.h"
//#define UI_TILEMAP_NUMBER 30
//#define SCREENBASE (u16*)(MEM_VRAM+UI_TILEMAP_NUMBER*2048)
//#define FONT_MEM (u16*)(MEM_VRAM+0x4000)
#define COLOR_ZERO_TILES (u16*)(MEM_VRAM+0xC000)
#if MOVIEPLAYER
int usinggbamp;
int usingcache;
//File rom_file=NO_FILE;
char save_slot;
#endif
//EWRAM_BSS int ui_y;
EWRAM_BSS u32 oldinput;
EWRAM_BSS u8 *textstart;//points to first GB rom (initialized by boot.s)
EWRAM_BSS u8 *ewram_start;//points to first NES rom (initialized by boot.s)
EWRAM_BSS int roms;//total number of roms
EWRAM_BSS int selectedrom=0;
#if POGOSHELL
EWRAM_BSS char pogoshell_romname[32]; //keep track of rom name (for state saving, etc)
EWRAM_BSS char pogoshell=0;
#endif
#if RTCSUPPORT
EWRAM_BSS char rtc=0;
#endif
EWRAM_BSS char gameboyplayer=0;
EWRAM_BSS char gbaversion;
EWRAM_BSS u32 max_multiboot_size; //largest possible multiboot transfer (init'd by boot.s)
#define TRIM 0x4D495254
#if FLASHCART
u32 total_rom_size = 0;
u32 flash_size = 0;
u32 flash_sram_area = 0;
u8 flash_type = 0;
#endif
//82048 is an upper bound on the save state size
//The formula is an upper bound LZO estimate on worst case compression
//#define WORSTCASE ((82048)+(82048)/64+16+4+64)
#if GCC
EWRAM_BSS u32 copiedfromrom=0;
int main()
{
//this function does what boot.s used to do
extern u8 __rom_end__[]; //using this instead of __end__, because it's also cart compatible
extern u8 __eheap_start[];
u32 end_addr = (u32)(&__rom_end__);
textstart = (u8*)(end_addr);
u32 heap_addr = (u32)(&__eheap_start);
ewram_start = (u8*)heap_addr;
extern u8 MULTIBOOT_LIMIT[];
u32 multiboot_limit = (u32)(&MULTIBOOT_LIMIT);
max_multiboot_size = multiboot_limit - heap_addr;
if (end_addr < 0x08000000 && copiedfromrom)
{
textstart += (0x08000000 - 0x02000000);
}
bool is_multiboot = (copiedfromrom == 0) && (end_addr < 0x08000000);
if (is_multiboot)
{
//copy appended data to __iwram_lma
u8* append_src=(u8*)end_addr;
u8* append_dest=(u8*)ewram_start;
u8* EWRAM_end=(u8*)0x02040000;
memmove(append_dest,append_src,EWRAM_end-append_src);
textstart=append_dest;
ewram_start=append_dest;
max_multiboot_size=((u32)(MULTIBOOT_LIMIT))-((u32)(ewram_start));
}
ewram_canary_1 = 0xDEADBEEF;
ewram_canary_2 = 0xDEADBEEF;
C_entry();
return 0;
/*
u32 end_addr=(u32)(&__load_stop_iwram9);
extern u32 MULTIBOOT_LIMIT;
bool is_multiboot = (copiedfromrom==0)&&(end_addr<0x08000000);
if (is_multiboot)
{
//copy appended data to __iwram_lma
u8* append_src=(u8*)end_addr;
u8* append_dest=(u8*)(&__iwram_lma);
u8* EWRAM_end=(u8*)0x02040000;
memmove(append_dest,append_src,EWRAM_end-append_src);
textstart=append_dest;
ewram_start=append_dest;
max_multiboot_size=((u32)(MULTIBOOT_LIMIT))-((u32)(ewram_start));
}
else //running from cart
{
//is it compiled for multiboot?
if (end_addr<0x08000000)
{
textstart=(u8*)((end_addr)-0x02000000+0x08000000);
ewram_start=(u8*)(((u32)__iwram_lma));
max_multiboot_size=((u32)(MULTIBOOT_LIMIT))-((u32)(ewram_start));
}
else
{
textstart=(u8*)(end_addr);
ewram_start=(u8*)0x02000000;
max_multiboot_size=0;
}
}
max_multiboot_size=((u32)(MULTIBOOT_LIMIT))-((u32)(ewram_start));
C_entry();
return 0;
*/
}
#endif
/*
void loadfont()
{
LZ77UnCompVram(&font_lz77,FONT_MEM);
}
*/
#if LITTLESOUNDDJ
EWRAM_BSS vu16 *const SC_UNLOCK = (vu16*)0x09FFFFFE;
EWRAM_BSS vu16 *const M3_UNLOCK = (vu16*)0x09FFEFFE;
bool test_mem()
{
u32 oldbyte;
u32 deadbeef=0xDEADBEEF;
vu32* address=(u32*)0x09876540;
bool success;
oldbyte=*address;
*address=deadbeef;
success=*address==deadbeef;
*address=oldbyte;
return success;
}
__inline bool unlock_sc()
{
//unlock SuperCard
*SC_UNLOCK = 0xA55A;
*SC_UNLOCK = 0xA55A;
*SC_UNLOCK = 0x0005;
*SC_UNLOCK = 0x0005;
return test_mem();
}
__inline bool unlock_g6()
{
//unlock G6
*SC_UNLOCK = 0xAA55;
return test_mem();
}
__inline bool unlock_m3()
{
//unlock M3
*M3_UNLOCK = 0xAA55;
return test_mem();
}
int enable_ram()
{
int cardtype;
cardtype=0;
if (test_mem())
{
cardtype=4;
}
else if (unlock_sc())
{
cardtype=1;
}
else if (unlock_g6())
{
cardtype=2;
}
else if (unlock_m3())
{
cardtype=3;
}
return cardtype;
}
#endif
void C_entry()
{
int i,j;
#if RTCSUPPORT
vu16 *timeregs=(u16*)0x080000c8;
#endif
#if POGOSHELL
u32 temp=(u32)(*(u8**)0x0203FBFC);
pogoshell=((temp & 0xFE000000) == 0x08000000)?1:0;
if(pogoshell)
{
char *d=(char*)0x203fc08;
do d++; while(*d);
do d++; while(*d);
do d--; while(*d!='/');
d++; //d=GB rom name
roms=1;
textstart=(*(u8**)0x0203FBFC);
memcpy(pogoshell_romname,d,32);
}
#endif
#if !GCC
ewram_start=(u8*)&Image$$RO$$Limit;
if (ewram_start>=(u8*)0x08000000)
{
ewram_start=(u8*)0x02000000;
}
#endif
#if LITTLESOUNDDJ
enable_ram();
#endif
#if RTCSUPPORT
*timeregs=1;
if(*timeregs & 1) rtc=1;
#endif
gbaversion=CheckGBAVersion();
vblankfptr=&vbldummy;
GFX_init_irq();
// vcountfptr=&vbldummy;
#if RUMBLE
SerialIn = 0;
#endif
// The maximal space
#if CARTSRAM
{
//buffer1=(ewram_start);
//buffer2=(ewram_start+0x10000);
//buffer3=(ewram_start+0x20000);
}
#endif
#if POGOSHELL
if(!pogoshell)
#endif
{
int gbx_id=0x6666edce;
u8 *p;
u8 *q;
#if MOVIEPLAYER
if (!disc_IsInserted())
{
#endif
#if SPLASH
bool wantToSplash = false;
const u16 *splashImage = NULL;
//splash screen present?
p=textstart;
#if USETRIM
if(*((u32*)p)==TRIM) p+=((u32*)p)[2];
#endif
if(*(u32*)(p+0x104)!=gbx_id) {
wantToSplash = true;
splashImage = (const u16*)textstart;
//splash();
textstart+=76800;
}
#endif
i=-1;
p=textstart;
//count roms
do
{
#if USETRIM
if(*((u32*)p)==TRIM)
{
q=p+((u32*)p)[2];
p=p+((u32*)p)[1];
}
else
#endif
{
q=p;
p+=(0x8000<<(*(p+0x148)));
}
i++;
} while (*(u32*)(q+0x104)==gbx_id);
roms=i;
#if FLASHCART
flash_type = get_flash_type();
total_rom_size=((u32)(q))-0x8000000;
flash_sram_area = 0;
// Override SRAM flash locationif ROM is appended with "SVLC" followed by the address to use
if (*(u32 *)(AGB_ROM+total_rom_size) == 0x434C5653) {
flash_sram_area = *(u32 *)(AGB_ROM+total_rom_size+4);
}
if (flash_type > 0) {
// Determine the size of the flash chip by checking for ROM loops,
// then set the SRAM storage area 0x40000 bytes before the end.
// This is due to different sector sizes of different flash chips,
// and should hopefully cover all cases.
if (memcmp(AGB_ROM+4, AGB_ROM+4+0x400000, 0x40) == 0) {
flash_size = 0x400000;
} else if (memcmp(AGB_ROM+4, AGB_ROM+4+0x800000, 0x40) == 0) {
flash_size = 0x800000;
} else if (memcmp(AGB_ROM+4, AGB_ROM+4+0x1000000, 0x40) == 0) {
flash_size = 0x1000000;
} else {
flash_size = 0x2000000;
}
if (flash_sram_area == 0) {
flash_sram_area = flash_size - 0x40000;
}
// RIP if the selected storage area is within the Goomba Color ROM...
if (total_rom_size > flash_sram_area) {
get_ready_to_display_text();
cls(3);
ui_x=0;
move_ui();
drawtext(7,"The Goomba Color compilation",0);
drawtext(8," is too large for saving",0);
drawtext(9," SRAM data in Flash ROM.",0);
drawtext(11,"Please remove some ROMs from",0);
drawtext(12," the compilation.",0);
strmerge(str,"Goomba Color ", VERSION);
drawtext(18,str,0);
drawtext(19,"by Flubba, Dwedit, Lesserkuma",0);
while (1) waitframe();
}
// Finally, restore the SRAM data and proceed.
bytecopy(AGB_SRAM, ((u8*)AGB_ROM+flash_sram_area), AGB_SRAM_SIZE);
} else { // Emulator mode?
if (flash_sram_area == 0) {
if ((*(u32*)(AGB_ROM+0x400000-0x40000) == STATEID) || (*(u32*)(AGB_ROM+0x400000-0x40000) == STATEID2)) {
flash_sram_area = 0x400000-0x40000;
} else if ((*(u32*)(AGB_ROM+0x800000-0x40000) == STATEID) || (*(u32*)(AGB_ROM+0x800000-0x40000) == STATEID2)) {
flash_sram_area = 0x800000-0x40000;
} else if ((*(u32*)(AGB_ROM+0x1000000-0x40000) == STATEID) || (*(u32*)(AGB_ROM+0x1000000-0x40000) == STATEID2)) {
flash_sram_area = 0x1000000-0x40000;
} else if ((*(u32*)(AGB_ROM+0x2000000-0x40000) == STATEID) || (*(u32*)(AGB_ROM+0x2000000-0x40000) == STATEID2)) {
flash_sram_area = 0x2000000-0x40000;
}
}
if (flash_sram_area != 0) {
bytecopy(AGB_SRAM, ((u8*)AGB_ROM+flash_sram_area), AGB_SRAM_SIZE);
bytecopy(AGB_SRAM, ((u8*)AGB_ROM+flash_sram_area), AGB_SRAM_SIZE/2); // some emulators don't like 64 KB of SRAM, so at least give them the first 32 KB again
}
}
// Failsafe: Holding SELECT+UP+B on boot will invalidate SRAM
if (((*(u16 *)(0x4000130))) == 0x03B9) {
*(u32 *)AGB_SRAM = 0xFFFFFFFF;
}
#endif
if(!i)roms=1; //Stop Goomba from crashing if there are no ROMs
if (i == 0)
{
get_ready_to_display_text();
cls(3);
ui_x=0;
move_ui();
drawtext( 1," No Game Boy ROMs found.",0);
drawtext( 3,"You can build a compilation",0);
drawtext( 4," using Goomba Front.",0);
#if FLASHCART
drawtext( 9,"This version of Goomba Color",0);
drawtext(10," supports saving on",0);
drawtext(11,"batteryless repro flashcarts.",0);
drawtext(13," https://git.io/JtmcB",0);
#endif
strmerge(str,"Goomba Color ", VERSION);
drawtext(18,str,0);
drawtext(19,"by Flubba, Dwedit, Lesserkuma",0);
while (1)
{
waitframe();
}
}
if (wantToSplash)
{
splash(splashImage);
}
#if MOVIEPLAYER
}
#endif
}
//Fade either from white, or from whatever bitmap is visible
if(REG_DISPCNT==FORCE_BLANK) //is screen OFF?
{
REG_DISPCNT=0; //screen ON
}
//start up graphics
*MEM_PALETTE=0x7FFF; //white background
REG_BLDMOD=0x00ff; //brightness decrease all
for (i=0;i<17;i++)
{
REG_BLDY=i;
waitframe();
}
*MEM_PALETTE=0; //black background (avoids blue flash when doing multiboot)
REG_DISPCNT=0; //screen ON, MODE0
//clear VRAM
memset32(MEM_VRAM,0,0x18000);
//new: load VRAM code
extern u8 __vram1_start[], __vram1_lma[], __vram1_end[];
int vram1_size = ((((u8*)__vram1_end - (u8*)__vram1_start) - 1) | 3) + 1;
memcpy32((u32*)__vram1_start,(const u32*)__vram1_lma,vram1_size);
//Start up interrupt system
GFX_init();
vblankfptr=&vblankinterrupt;
// vcountfptr=&vcountinterrupt;
#if CARTSRAM
lzo_init(); //init compression lib for savestates
#endif
//make 16 solid tiles
{
u32* p=(u32*)COLOR_ZERO_TILES;
for (i=0;i<16;i++)
{
u32 val=(u32)i*(u32)0x11111111;
for (j=0;j<8;j++)
{
*p=val;
p++;
}
}
}
//load font+palette
loadfont();
loadfontpal();
#if CARTSRAM
readconfig();
#endif
jump_to_rommenu();
}
#if SPLASH
//show splash screen
void splash(const u16 *splashImage)
{
int i;
REG_DISPCNT=FORCE_BLANK; //screen OFF
memcpy((u16*)MEM_VRAM,splashImage,240*160*2);
waitframe();
REG_BG2CNT=0x0000;
REG_DISPCNT=BG2_EN|MODE3;
for(i=16;i>=0;i--) { //fade from white
setbrightnessall(i);
waitframe();
}
for(i=0;i<150;i++) { //wait 2.5 seconds
waitframe();
if (REG_P1==0x030f)
{
gameboyplayer=1;
gbaversion=3;
}
}
}
#endif
#if MOVIEPLAYER
int get_saved_sram()
{
return get_saved_sram_CF(SramName);
}
int get_saved_sram_CF(char* sramname)
{
if(g_cartflags&2 && g_rammask!=0)
{ //if rom uses SRAM
File file;
file=FAT_fopen(sramname,"r");
if (file!=NO_FILE)
{
#if !RESIZABLE
#define XGB_sram XGB_SRAM
#endif
FAT_fread(XGB_sram,1,g_rammask+1,file);
FAT_fclose(file);
return 1;
}
return 2;
}
else
{
return 0;
}
}
int save_sram_CF(char* sramname)
{
if(g_cartflags&2 && g_rammask!=0)
{ //if rom uses SRAM
File file;
file=FAT_fopen(sramname,"r+");
if (file==NO_FILE)
file=FAT_fopen(sramname,"w");
if (file!=NO_FILE)
{
FAT_fwrite(XGB_sram,1,g_rammask+1,file);
FAT_fclose(file);
}
return 1;
}
return 0;
}
#endif
#if FLASHCART
// This function will auto-detect four common
// types of reproduction flash cartridges.
// Must run in EWRAM because ROM data is
// not visible to the system while checking.
__attribute__((section(".ewram")))
u32 get_flash_type() {
u32 rom_data, data;
u16 ie = REG_IE;
//stop_dma_interrupts();
REG_IE = ie & 0xFFFE;
rom_data = *(u32 *)AGB_ROM;
// Type 1 or 4
_FLASH_WRITE(0, 0xFF);
_FLASH_WRITE(0, 0x90);
data = *(u32 *)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 = *(u8 *)(AGB_ROM+0xB2);
_FLASH_WRITE(0x59, 0x96);
_FLASH_WRITE(0, 0xFF);
if (data != 0x96) {
REG_IE = ie;
//resume_interrupts();
return 4;
}
REG_IE = ie;
//resume_interrupts();
return 1;
}
// Type 2
_FLASH_WRITE(0, 0xF0);
_FLASH_WRITE(0xAAA, 0xA9);
_FLASH_WRITE(0x555, 0x56);
_FLASH_WRITE(0xAAA, 0x90);
data = *(u32 *)AGB_ROM;
_FLASH_WRITE(0, 0xF0);
if (rom_data != data) {
REG_IE = ie;
//resume_interrupts();
return 2;
}
// Type 3
_FLASH_WRITE(0, 0xF0);
_FLASH_WRITE(0xAAA, 0xAA);
_FLASH_WRITE(0x555, 0x55);
_FLASH_WRITE(0xAAA, 0x90);
data = *(u32 *)AGB_ROM;
_FLASH_WRITE(0, 0xF0);
if (rom_data != data) {
REG_IE = ie;
//resume_interrupts();
return 3;
}
REG_IE = ie;
//resume_interrupts();
return 0;
}
// This function will issue a flash sector erase
// operation at the given sector address and then
// write 64 kilobytes of SRAM data to Flash ROM.
// Must run in EWRAM because ROM data is
// not visible to the system while erasing/writing.
__attribute__((section(".ewram")))
void flash_write(u8 flash_type, u32 sa)
{
if (flash_type == 0) return;
u16 ie = REG_IE;
//stop_dma_interrupts();
REG_IE = ie & 0xFFFE;
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 (*(((u16 *)AGB_ROM)+(sa/2)) == 0x80) {
break;
}
}
_FLASH_WRITE(sa, 0xFF);
// Write data
for (int i=0; i<AGB_SRAM_SIZE; i+=2) {
_FLASH_WRITE(sa+i, 0x40);
_FLASH_WRITE(sa+i, (*(u8 *)(AGB_SRAM+i+1)) << 8 | (*(u8 *)(AGB_SRAM+i)));
while (1) {
__asm("nop");
if (*(((u16 *)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 (*(((u16 *)AGB_ROM)+(sa/2)) == 0xFFFF) {
break;
}
}
_FLASH_WRITE(sa, 0xF0);
// Write data
for (int i=0; i<AGB_SRAM_SIZE; i+=2) {
_FLASH_WRITE(0xAAA, 0xA9);
_FLASH_WRITE(0x555, 0x56);
_FLASH_WRITE(0xAAA, 0xA0);
_FLASH_WRITE(sa+i, (*(u8 *)(AGB_SRAM+i+1)) << 8 | (*(u8 *)(AGB_SRAM+i)));
while (1) {
__asm("nop");
if (*(((u16 *)AGB_ROM)+((sa+i)/2)) == ((*(u8 *)(AGB_SRAM+i+1)) << 8 | (*(u8 *)(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 (*(((u16 *)AGB_ROM)+(sa/2)) == 0xFFFF) {
break;
}
}
_FLASH_WRITE(sa, 0xF0);
// Write data
for (int i=0; i<AGB_SRAM_SIZE; i+=2) {
_FLASH_WRITE(0xAAA, 0xAA);
_FLASH_WRITE(0x555, 0x55);
_FLASH_WRITE(0xAAA, 0xA0);
_FLASH_WRITE(sa+i, (*(u8 *)(AGB_SRAM+i+1)) << 8 | (*(u8 *)(AGB_SRAM+i)));
while (1) {
__asm("nop");
if (*(((u16 *)AGB_ROM)+((sa+i)/2)) == ((*(u8 *)(AGB_SRAM+i+1)) << 8 | (*(u8 *)(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 ((*(((u16 *)AGB_ROM)+(sa/2)) & 0x80) == 0x80) {
break;
}
}
_FLASH_WRITE(sa, 0xFF);
// Write data
int c = 0;
while (c < AGB_SRAM_SIZE) {
_FLASH_WRITE(sa+c, 0xEA);
while (1) {
__asm("nop");
if ((*(((u16 *)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, (*(u8 *)(AGB_SRAM+c+i+1)) << 8 | (*(u8 *)(AGB_SRAM+c+i)));
}
_FLASH_WRITE(sa+c, 0xD0);
while (1) {
__asm("nop");
if ((*(((u16 *)AGB_ROM)+((sa+c)/2)) & 0x80) == 0x80) {
break;
}
}
_FLASH_WRITE(sa+c, 0xFF);
c += 1024;
}
}
REG_IE = ie;
//resume_interrupts();
}
void save_sram_FLASH()
{
if (flash_type == 0) return;
flash_write(flash_type, flash_sram_area);
}
#endif
void jump_to_rommenu(void)
{
#if GCC
extern u8 __sp_usr[];
u32 newstack=(u32)(&__sp_usr);
__asm__ volatile ("mov sp,%0": :"r"(newstack));
#else
__asm {mov r0,#0x3007f00} //stack reset
__asm {mov sp,r0}
#endif
rommenu();
run(1);
while (true);
}
void rommenu(void)
{
cls(3);
ui_x=0x100;
setdarkness(16);
// resetSIO((joycfg&~0xff000000) + 0x40000000);//back to 1P
#if MOVIEPLAYER
usingcache=MOVIEPLAYERDEBUG;
usinggbamp=0;
#endif
cls(3);
make_ui_visible();
#if CARTSRAM
backup_gb_sram(0); //includes emergency delete menu
#endif
#if FLASHCART
if (flash_type == 0) {
drawtext( 8+32," Flash ROM not detected.",0);
drawtext(10+32," Batteryless SRAM saving",0);
drawtext(11+32," will be disabled.",0);
drawtext(19+32,"Batteryless mod by Lesserkuma",0);
for(int i=0;i<150;i++) { //wait 2.5 seconds
waitframe();
}
cls(3);
}
#endif
#if MOVIEPLAYER
if (disc_IsInserted())
{
File file;
usinggbamp=1;
if (rom_file!=NO_FILE)
{
FAT_fclose(rom_file);
}
file=cfmenu();
rom_file=file;
cls(3);
loadcart(0,g_emuflags&0x300);
// get_saved_sram_CF(SramName);
}
else
#endif
#if POGOSHELL
if(pogoshell)
{
loadcart(0,g_emuflags&0x300);
//#if CARTSRAM
// get_saved_sram();
//#endif
}
else
#endif
{
int i,lastselected=-1;
int key;
int romz=roms; //globals=bigger code :P
int sel=selectedrom;
oldinput=AGBinput=~REG_P1;
if(romz>1){
i=drawmenu(sel);
loadcart(sel,i|(g_emuflags&0x300)); //(keep old gfxmode)
//#if CARTSRAM
// get_saved_sram();
//#endif
lastselected=sel;
for(i=0;i<8;i++)
{
ui_x=224-i*32;
move_ui_wait();
}
waitframe();
setdarkness(7); //Lighten screen
}
do {
key=getinput();
if(key&RIGHT) {
sel+=10;
if(sel>romz-1) sel=romz-1;
}
if(key&LEFT) {
sel-=10;
if(sel<0) sel=0;
}
if(key&UP)
sel=sel+romz-1;
if(key&DOWN)
sel++;
selectedrom=sel%=romz;
if(lastselected!=sel) {
i=drawmenu(sel);
loadcart(sel,i|(g_emuflags&0x300)); //(keep old gfxmode)
//#if CARTSRAM
// get_saved_sram();
//#endif
lastselected=sel;
}
run(0);
} while(romz>1 && !(key&(A_BTN+B_BTN+START)));
for(i=1;i<9;i++)
{
setdarkness(8-i); //Lighten screen
ui_x=i*32;
move_ui_scroll();
run(0);
move_ui_expose();
}
cls(3); //leave BG2 on for debug output
while(AGBinput&(A_BTN+B_BTN+START)) {
AGBinput=0;
run(0);
}
}
#if CARTSRAM
if(autostate)quickload();
#endif
setdarkness(0);
make_ui_invisible();
//run(1);
}
#if MOVIEPLAYER
u8 *findrom(int n)
{
return cache_location[0];
}
u8 *findrom2(int n)
{
return cache_location[0];
}
#else
//returns the start address of the ROM (including TRIM header)
u8 *findrom2(int n)
{
u8 *p=textstart;
#if POGOSHELL
if (pogoshell) return p;
#endif
while(n--)
{
#if USETRIM
if (*((u32*)p)==TRIM) //trimmed
{
p+=((u32*)p)[1];
}
else
{
p+=(0x8000<<(*(p+0x148)));
}
#else
p+=(0x8000<<(*(p+0x148)));
#endif
}
return p;
}
//returns the first page of the ROM
u8 *findrom(int n)
{
u8 *p=findrom2(n);
#if USETRIM
if (*((u32*)p)==TRIM) //trimmed
{
p+=((u32*)p)[2];
}
#endif
return p;
}
#endif
char *getcartname(u8 *rom_base)
{
//assigns cartridge name (with gamecode removed) to global variable, str
bool gbcmode;
char *cartname;
int i;
bool anyzeroes;
cartname=str;
strncpy(cartname,(char*)(rom_base+0x134),16);
cartname[16]=0;
gbcmode=rom_base[0x143] >= 0x80;
if (gbcmode)
{
cartname[15]=0;
anyzeroes=false;
for (i=11;i<=14;i++)
{
if (cartname[i]==0)
{
anyzeroes=true;
break;
}
}
if (!anyzeroes)
{
for (i=11;i<=14;i++)
{
cartname[i]=0;
}
}