-
Notifications
You must be signed in to change notification settings - Fork 43
/
GBA.cpp
3819 lines (3481 loc) · 105 KB
/
GBA.cpp
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 <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <stdarg.h>
#include <string.h>
#include "GBA.h"
#include "GBAcpu.h"
#include "GBAinline.h"
#include "Globals.h"
#include "GBAGfx.h"
#include "EEprom.h"
#include "Flash.h"
#include "Sound.h"
#include "Sram.h"
#include "bios.h"
#include "Cheats.h"
#include "../NLS.h"
#include "elf.h"
#include "../Util.h"
#include "../common/Port.h"
#include "../System.h"
#include "agbprint.h"
#include "GBALink.h"
#include <logger/interface.h>
#include <io/sys.hh>
#include <mach/mach.h>
#include <mach/mach_time.h>
#ifdef PROFILING
#include "prof/prof.h"
#endif
#ifdef __GNUC__
#define _stricmp strcasecmp
#endif
#define PP_DUMMY_MAP(z, n, text) memoryMap{ (u8 *)&dummyAddress, 0, nullptr, nullptr, nullptr },
#define PP_DUMMY_MAP_REPEAT(n) BOOST_PP_REPEAT(n, PP_DUMMY_MAP, )
static int dummyAddress = 0;
static const memoryMap gbaMap[256] =
{
memoryMap{ gGba.mem.bios, 0x3FFF , biosRead8, biosRead16, biosRead32 },
memoryMap{ (u8 *)&dummyAddress, 0, nullptr, nullptr, nullptr },
memoryMap{ gGba.mem.workRAM, 0x3FFFF, nullptr, nullptr, nullptr },
memoryMap{ gGba.mem.internalRAM, 0x7FFF, nullptr, nullptr, nullptr },
memoryMap{ gGba.mem.ioMem.b, 0x3FF , ioMemRead8, ioMemRead16, ioMemRead32 },
memoryMap{ gGba.lcd.paletteRAM, 0x3FF, nullptr, nullptr, nullptr },
memoryMap{ gGba.lcd.vram, 0x1FFFF , vramRead8, vramRead16, vramRead32 },
memoryMap{ gGba.lcd.oam, 0x3FF, nullptr, nullptr, nullptr },
memoryMap{ gGba.mem.rom, 0x1FFFFFF , nullptr, rtcRead16, nullptr },
memoryMap{ gGba.mem.rom, 0x1FFFFFF, nullptr, nullptr, nullptr },
memoryMap{ gGba.mem.rom, 0x1FFFFFF, nullptr, nullptr, nullptr },
memoryMap{ (u8 *)&dummyAddress, 0, nullptr, nullptr, nullptr },
memoryMap{ gGba.mem.rom, 0x1FFFFFF, nullptr, nullptr, nullptr },
memoryMap{ (u8 *)&dummyAddress, 0 , eepromRead32, eepromRead32, eepromRead32 },
memoryMap{ flashSaveMemory, 0xFFFF , flashRead32, flashRead32, flashRead32 },
PP_DUMMY_MAP_REPEAT(241)
};
GBASys gGba;
#ifdef USE_MEM_HANDLERS
u32 biosRead32(ARM7TDMI &cpu, u32 address)
{
auto ® = cpu.reg;
if(reg[15].I >> 24) {
if(address < 0x4000) {
#ifdef GBA_LOGGING
if(systemVerbose & VERBOSE_ILLEGAL_READ) {
log("Illegal word read: %08x at %08x\n", address, armMode ?
armNextPC - 4 : armNextPC - 2);
}
#endif
return armRotLoad32(READ32LE(((u32 *)&cpu.gba->biosProtected)), address);
}
else return unreadableRead32(cpu, address);
} else
return armRotLoad32(READ32LE(((u32 *)&cpu.gba->mem.bios[address & 0x3FFC])), address);
}
u32 ioMemRead32(ARM7TDMI &cpu, u32 address)
{
if((address < 0x4000400) && ioReadable[address & 0x3fc]) {
if(ioReadable[(address & 0x3fc) + 2]) {
if ((address & 0x3fc) == COMM_JOY_RECV_L)
UPDATE_REG(cpu.gba, COMM_JOYSTAT, READ16LE(&cpu.gba->mem.ioMem.b[COMM_JOYSTAT]) & ~JOYSTAT_RECV);
return armRotLoad32(READ32LE(((u32 *)&cpu.gba->mem.ioMem.b[address & 0x3fC])), address);
} else {
return armRotLoad32(READ16LE(((u16 *)&cpu.gba->mem.ioMem.b[address & 0x3fc])), address);
}
}
else
return unreadableRead32(cpu, address);
}
u32 vramRead32(ARM7TDMI &cpu, u32 address)
{
address = (address & 0x1fffc);
if (((cpu.gba->mem.ioMem.DISPCNT & 7) >2) && ((address & 0x1C000) == 0x18000))
{
return 0;
}
if ((address & 0x18000) == 0x18000)
address &= 0x17fff;
return armRotLoad32(READ32LE(((u32 *)&cpu.gba->lcd.vram[address])), address);
}
u32 eepromRead32(ARM7TDMI &cpu, u32 address)
{
if(cpuEEPROMEnabled)
// no need to swap this
return eepromRead(address);
return unreadableRead32(cpu, address);
}
u32 flashRead32(ARM7TDMI &cpu, u32 address)
{
if(cpuFlashEnabled | cpuSramEnabled)
// no need to swap this
return flashRead(address);
else
return unreadableRead32(cpu, address);
}
u32 biosRead16(ARM7TDMI &cpu, u32 address)
{
auto ® = cpu.reg;
if (reg[15].I >> 24) {
if(address < 0x4000) {
#ifdef GBA_LOGGING
if(systemVerbose & VERBOSE_ILLEGAL_READ) {
log("Illegal halfword read: %08x at %08x\n", address, armMode ?
armNextPC - 4 : armNextPC - 2);
}
#endif
return armRotLoad16(READ16LE(((u16 *)&cpu.gba->biosProtected[address&2])), address);
} else return unreadableRead16(cpu, address);
} else
return armRotLoad16(READ16LE(((u16 *)&cpu.gba->mem.bios[address & 0x3FFE])), address);
}
u32 biosRead8(ARM7TDMI &cpu, u32 address)
{
auto ® = cpu.reg;
if (reg[15].I >> 24) {
if(address < 0x4000) {
#ifdef GBA_LOGGING
if(systemVerbose & VERBOSE_ILLEGAL_READ) {
log("Illegal byte read: %08x at %08x\n", address, armMode ?
armNextPC - 4 : armNextPC - 2);
}
#endif
return cpu.gba->biosProtected[address & 3];
} else return unreadableRead8(cpu, address);
}
return cpu.gba->mem.bios[address & 0x3FFF];
}
u32 ioMemRead8(ARM7TDMI &cpu, u32 address)
{
if((address < 0x4000400) && ioReadable[address & 0x3ff])
return cpu.gba->mem.ioMem.b[address & 0x3ff];
else return unreadableRead8(cpu, address);
}
u32 vramRead8(ARM7TDMI &cpu, u32 address)
{
address = (address & 0x1ffff);
if (((cpu.gba->mem.ioMem.DISPCNT & 7) >2) && ((address & 0x1C000) == 0x18000))
return 0;
if ((address & 0x18000) == 0x18000)
address &= 0x17fff;
return cpu.gba->lcd.vram[address];
}
u32 ioMemRead16(ARM7TDMI &cpu, u32 address)
{
auto &cpuTotalTicks = cpu.cpuTotalTicks;
auto &timer0Value = cpu.gba->timers.timer0Value;
auto &timer0On = cpu.gba->timers.timer0On;
auto &timer0Ticks = cpu.gba->timers.timer0Ticks;
auto &timer0Reload = cpu.gba->timers.timer0Reload;
auto &timer0ClockReload = cpu.gba->timers.timer0ClockReload;
auto &timer1Value = cpu.gba->timers.timer1Value;
auto &timer1On = cpu.gba->timers.timer1On;
auto &timer1Ticks = cpu.gba->timers.timer1Ticks;
auto &timer1Reload = cpu.gba->timers.timer1Reload;
auto &timer1ClockReload = cpu.gba->timers.timer1ClockReload;
auto &timer2Value = cpu.gba->timers.timer2Value;
auto &timer2On = cpu.gba->timers.timer2On;
auto &timer2Ticks = cpu.gba->timers.timer2Ticks;
auto &timer2Reload = cpu.gba->timers.timer2Reload;
auto &timer2ClockReload = cpu.gba->timers.timer2ClockReload;
auto &timer3Value = cpu.gba->timers.timer3Value;
auto &timer3On = cpu.gba->timers.timer3On;
auto &timer3Ticks = cpu.gba->timers.timer3Ticks;
auto &timer3Reload = cpu.gba->timers.timer3Reload;
auto &timer3ClockReload = cpu.gba->timers.timer3ClockReload;
if((address < 0x4000400) && ioReadable[address & 0x3fe])
{
if (((address & 0x3fe)>0xFF) && ((address & 0x3fe)<0x10E))
{
if (((address & 0x3fe) == 0x100) && timer0On)
return armRotLoad16(0xFFFF - ((timer0Ticks-cpuTotalTicks) >> timer0ClockReload), address);
else
if (((address & 0x3fe) == 0x104) && timer1On && !(cpu.gba->mem.ioMem.TM1CNT & 4))
return armRotLoad16(0xFFFF - ((timer1Ticks-cpuTotalTicks) >> timer1ClockReload), address);
else
if (((address & 0x3fe) == 0x108) && timer2On && !(cpu.gba->mem.ioMem.TM2CNT & 4))
return armRotLoad16(0xFFFF - ((timer2Ticks-cpuTotalTicks) >> timer2ClockReload), address);
else
if (((address & 0x3fe) == 0x10C) && timer3On && !(cpu.gba->mem.ioMem.TM3CNT & 4))
return armRotLoad16(0xFFFF - ((timer3Ticks-cpuTotalTicks) >> timer3ClockReload), address);
}
return armRotLoad16(READ16LE(((u16 *)&cpu.gba->mem.ioMem.b[address & 0x3fe])), address);
}
else return unreadableRead16(cpu, address);
}
u32 vramRead16(ARM7TDMI &cpu, u32 address)
{
address = (address & 0x1fffe);
if (((cpu.gba->mem.ioMem.DISPCNT & 7) >2) && ((address & 0x1C000) == 0x18000))
{
return 0;
}
if ((address & 0x18000) == 0x18000)
address &= 0x17fff;
return armRotLoad16(READ16LE(((u16 *)&cpu.gba->lcd.vram[address])), address);
}
u32 rtcRead16(ARM7TDMI &cpu, u32 address)
{
if(address == 0x80000c4 || address == 0x80000c6 || address == 0x80000c8)
return armRotLoad16(rtcRead(*cpu.gba, address), address);
else
return armRotLoad16(READ16LE(((u16 *)&cpu.gba->mem.rom[address & 0x1FFFFFE])), address);
}
#endif
u32 mastercode = 0;
int gbaSaveType = 0; // used to remember the save type on reset
#ifdef VBAM_USE_HOLDTYPE
int holdType = 0;
#else
static int holdTypeDummy;
#endif
bool cpuSramEnabled = true;
bool cpuFlashEnabled = true;
bool cpuEEPROMEnabled = true;
bool cpuEEPROMSensorEnabled = false;
#ifdef PROFILING
int profilingTicks = 0;
int profilingTicksReload = 0;
static profile_segment *profilSegment = NULL;
#endif
#ifdef BKPT_SUPPORT
u8 freezeWorkRAM[0x40000];
u8 freezeInternalRAM[0x8000];
u8 freezeVRAM[0x18000];
u8 freezePRAM[0x400];
u8 freezeOAM[0x400];
bool debugger_last;
#endif
void (*cpuSaveGameFunc)(u32,u8) = flashSaveDecide;
static const bool trackOAM = 1, oamUpdated = 1;
static const int TIMER_TICKS[4] = {
0,
6,
8,
10
};
static const u8 gamepakRamWaitState[4] = { 4, 3, 2, 8 };
static const u8 gamepakWaitState[4] = { 4, 3, 2, 8 };
static const u8 gamepakWaitState0[2] = { 2, 1 };
static const u8 gamepakWaitState1[2] = { 4, 1 };
static const u8 gamepakWaitState2[2] = { 8, 1 };
static const bool isInRom [16]=
{ false, false, false, false, false, false, false, false,
true, true, true, true, true, true, false, false };
// The videoMemoryWait constants are used to add some waitstates
// if the opcode access video memory data outside of vblank/hblank
// It seems to happen on only one ticks for each pixel.
// Not used for now (too problematic with current code).
//const u8 videoMemoryWait[16] =
// {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
#ifdef WORDS_BIGENDIAN
bool cpuBiosSwapped = false;
#endif
static const u32 myROM[] = {
0xEA000006,
0xEA000093,
0xEA000006,
0x00000000,
0x00000000,
0x00000000,
0xEA000088,
0x00000000,
0xE3A00302,
0xE1A0F000,
0xE92D5800,
0xE55EC002,
0xE28FB03C,
0xE79BC10C,
0xE14FB000,
0xE92D0800,
0xE20BB080,
0xE38BB01F,
0xE129F00B,
0xE92D4004,
0xE1A0E00F,
0xE12FFF1C,
0xE8BD4004,
0xE3A0C0D3,
0xE129F00C,
0xE8BD0800,
0xE169F00B,
0xE8BD5800,
0xE1B0F00E,
0x0000009C,
0x0000009C,
0x0000009C,
0x0000009C,
0x000001F8,
0x000001F0,
0x000000AC,
0x000000A0,
0x000000FC,
0x00000168,
0xE12FFF1E,
0xE1A03000,
0xE1A00001,
0xE1A01003,
0xE2113102,
0x42611000,
0xE033C040,
0x22600000,
0xE1B02001,
0xE15200A0,
0x91A02082,
0x3AFFFFFC,
0xE1500002,
0xE0A33003,
0x20400002,
0xE1320001,
0x11A020A2,
0x1AFFFFF9,
0xE1A01000,
0xE1A00003,
0xE1B0C08C,
0x22600000,
0x42611000,
0xE12FFF1E,
0xE92D0010,
0xE1A0C000,
0xE3A01001,
0xE1500001,
0x81A000A0,
0x81A01081,
0x8AFFFFFB,
0xE1A0000C,
0xE1A04001,
0xE3A03000,
0xE1A02001,
0xE15200A0,
0x91A02082,
0x3AFFFFFC,
0xE1500002,
0xE0A33003,
0x20400002,
0xE1320001,
0x11A020A2,
0x1AFFFFF9,
0xE0811003,
0xE1B010A1,
0xE1510004,
0x3AFFFFEE,
0xE1A00004,
0xE8BD0010,
0xE12FFF1E,
0xE0010090,
0xE1A01741,
0xE2611000,
0xE3A030A9,
0xE0030391,
0xE1A03743,
0xE2833E39,
0xE0030391,
0xE1A03743,
0xE2833C09,
0xE283301C,
0xE0030391,
0xE1A03743,
0xE2833C0F,
0xE28330B6,
0xE0030391,
0xE1A03743,
0xE2833C16,
0xE28330AA,
0xE0030391,
0xE1A03743,
0xE2833A02,
0xE2833081,
0xE0030391,
0xE1A03743,
0xE2833C36,
0xE2833051,
0xE0030391,
0xE1A03743,
0xE2833CA2,
0xE28330F9,
0xE0000093,
0xE1A00840,
0xE12FFF1E,
0xE3A00001,
0xE3A01001,
0xE92D4010,
0xE3A03000,
0xE3A04001,
0xE3500000,
0x1B000004,
0xE5CC3301,
0xEB000002,
0x0AFFFFFC,
0xE8BD4010,
0xE12FFF1E,
0xE3A0C301,
0xE5CC3208,
0xE15C20B8,
0xE0110002,
0x10222000,
0x114C20B8,
0xE5CC4208,
0xE12FFF1E,
0xE92D500F,
0xE3A00301,
0xE1A0E00F,
0xE510F004,
0xE8BD500F,
0xE25EF004,
0xE59FD044,
0xE92D5000,
0xE14FC000,
0xE10FE000,
0xE92D5000,
0xE3A0C302,
0xE5DCE09C,
0xE35E00A5,
0x1A000004,
0x05DCE0B4,
0x021EE080,
0xE28FE004,
0x159FF018,
0x059FF018,
0xE59FD018,
0xE8BD5000,
0xE169F00C,
0xE8BD5000,
0xE25EF004,
0x03007FF0,
0x09FE2000,
0x09FFC000,
0x03007FE0
};
static bool saveNFlag, saveZFlag;
static const variable_desc saveGameStruct[] = {
{ &gGba.mem.ioMem.DISPCNT , sizeof(u16) },
{ &gGba.mem.ioMem.DISPSTAT , sizeof(u16) },
{ &gGba.mem.ioMem.VCOUNT , sizeof(u16) },
{ &gGba.mem.ioMem.BG0CNT , sizeof(u16) },
{ &gGba.mem.ioMem.BG1CNT , sizeof(u16) },
{ &gGba.mem.ioMem.BG2CNT , sizeof(u16) },
{ &gGba.mem.ioMem.BG3CNT , sizeof(u16) },
{ &gGba.mem.ioMem.BG0HOFS , sizeof(u16) },
{ &gGba.mem.ioMem.BG0VOFS , sizeof(u16) },
{ &gGba.mem.ioMem.BG1HOFS , sizeof(u16) },
{ &gGba.mem.ioMem.BG1VOFS , sizeof(u16) },
{ &gGba.mem.ioMem.BG2HOFS , sizeof(u16) },
{ &gGba.mem.ioMem.BG2VOFS , sizeof(u16) },
{ &gGba.mem.ioMem.BG3HOFS , sizeof(u16) },
{ &gGba.mem.ioMem.BG3VOFS , sizeof(u16) },
{ &gGba.mem.ioMem.BG2PA , sizeof(u16) },
{ &gGba.mem.ioMem.BG2PB , sizeof(u16) },
{ &gGba.mem.ioMem.BG2PC , sizeof(u16) },
{ &gGba.mem.ioMem.BG2PD , sizeof(u16) },
{ &gGba.mem.ioMem.BG2X_L , sizeof(u16) },
{ &gGba.mem.ioMem.BG2X_H , sizeof(u16) },
{ &gGba.mem.ioMem.BG2Y_L , sizeof(u16) },
{ &gGba.mem.ioMem.BG2Y_H , sizeof(u16) },
{ &gGba.mem.ioMem.BG3PA , sizeof(u16) },
{ &gGba.mem.ioMem.BG3PB , sizeof(u16) },
{ &gGba.mem.ioMem.BG3PC , sizeof(u16) },
{ &gGba.mem.ioMem.BG3PD , sizeof(u16) },
{ &gGba.mem.ioMem.BG3X_L , sizeof(u16) },
{ &gGba.mem.ioMem.BG3X_H , sizeof(u16) },
{ &gGba.mem.ioMem.BG3Y_L , sizeof(u16) },
{ &gGba.mem.ioMem.BG3Y_H , sizeof(u16) },
{ &gGba.mem.ioMem.WIN0H , sizeof(u16) },
{ &gGba.mem.ioMem.WIN1H , sizeof(u16) },
{ &gGba.mem.ioMem.WIN0V , sizeof(u16) },
{ &gGba.mem.ioMem.WIN1V , sizeof(u16) },
{ &gGba.mem.ioMem.WININ , sizeof(u16) },
{ &gGba.mem.ioMem.WINOUT , sizeof(u16) },
{ &gGba.mem.ioMem.MOSAIC , sizeof(u16) },
{ &gGba.mem.ioMem.BLDMOD , sizeof(u16) },
{ &gGba.mem.ioMem.COLEV , sizeof(u16) },
{ &gGba.mem.ioMem.COLY , sizeof(u16) },
{ &gGba.mem.ioMem.DM0SAD_L , sizeof(u16) },
{ &gGba.mem.ioMem.DM0SAD_H , sizeof(u16) },
{ &gGba.mem.ioMem.DM0DAD_L , sizeof(u16) },
{ &gGba.mem.ioMem.DM0DAD_H , sizeof(u16) },
{ &gGba.mem.ioMem.DM0CNT_L , sizeof(u16) },
{ &gGba.mem.ioMem.DM0CNT_H , sizeof(u16) },
{ &gGba.mem.ioMem.DM1SAD_L , sizeof(u16) },
{ &gGba.mem.ioMem.DM1SAD_H , sizeof(u16) },
{ &gGba.mem.ioMem.DM1DAD_L , sizeof(u16) },
{ &gGba.mem.ioMem.DM1DAD_H , sizeof(u16) },
{ &gGba.mem.ioMem.DM1CNT_L , sizeof(u16) },
{ &gGba.mem.ioMem.DM1CNT_H , sizeof(u16) },
{ &gGba.mem.ioMem.DM2SAD_L , sizeof(u16) },
{ &gGba.mem.ioMem.DM2SAD_H , sizeof(u16) },
{ &gGba.mem.ioMem.DM2DAD_L , sizeof(u16) },
{ &gGba.mem.ioMem.DM2DAD_H , sizeof(u16) },
{ &gGba.mem.ioMem.DM2CNT_L , sizeof(u16) },
{ &gGba.mem.ioMem.DM2CNT_H , sizeof(u16) },
{ &gGba.mem.ioMem.DM3SAD_L , sizeof(u16) },
{ &gGba.mem.ioMem.DM3SAD_H , sizeof(u16) },
{ &gGba.mem.ioMem.DM3DAD_L , sizeof(u16) },
{ &gGba.mem.ioMem.DM3DAD_H , sizeof(u16) },
{ &gGba.mem.ioMem.DM3CNT_L , sizeof(u16) },
{ &gGba.mem.ioMem.DM3CNT_H , sizeof(u16) },
{ &gGba.mem.ioMem.TM0D , sizeof(u16) },
{ &gGba.mem.ioMem.TM0CNT , sizeof(u16) },
{ &gGba.mem.ioMem.TM1D , sizeof(u16) },
{ &gGba.mem.ioMem.TM1CNT , sizeof(u16) },
{ &gGba.mem.ioMem.TM2D , sizeof(u16) },
{ &gGba.mem.ioMem.TM2CNT , sizeof(u16) },
{ &gGba.mem.ioMem.TM3D , sizeof(u16) },
{ &gGba.mem.ioMem.TM3CNT , sizeof(u16) },
{ &P1 , sizeof(u16) },
{ &gGba.mem.ioMem.IE , sizeof(u16) },
{ &gGba.mem.ioMem.IF , sizeof(u16) },
{ &gGba.mem.ioMem.IME , sizeof(u16) },
{ &gGba.cpu.holdState, sizeof(bool) },
#ifdef VBAM_USE_HOLDTYPE
{ &holdType, sizeof(int) },
#else
{ &holdTypeDummy, sizeof(int) },
#endif
{ &gGba.lcd.lcdTicks, sizeof(int) },
{ &gGba.timers.timer0On , sizeof(bool) },
{ &gGba.timers.timer0Ticks , sizeof(int) },
{ &gGba.timers.timer0Reload , sizeof(int) },
{ &gGba.timers.timer0ClockReload , sizeof(int) },
{ &gGba.timers.timer1On , sizeof(bool) },
{ &gGba.timers.timer1Ticks , sizeof(int) },
{ &gGba.timers.timer1Reload , sizeof(int) },
{ &gGba.timers.timer1ClockReload , sizeof(int) },
{ &gGba.timers.timer2On , sizeof(bool) },
{ &gGba.timers.timer2Ticks , sizeof(int) },
{ &gGba.timers.timer2Reload , sizeof(int) },
{ &gGba.timers.timer2ClockReload , sizeof(int) },
{ &gGba.timers.timer3On , sizeof(bool) },
{ &gGba.timers.timer3Ticks , sizeof(int) },
{ &gGba.timers.timer3Reload , sizeof(int) },
{ &gGba.timers.timer3ClockReload , sizeof(int) },
{ &gGba.dma.dma0Source , sizeof(u32) },
{ &gGba.dma.dma0Dest , sizeof(u32) },
{ &gGba.dma.dma1Source , sizeof(u32) },
{ &gGba.dma.dma1Dest , sizeof(u32) },
{ &gGba.dma.dma2Source , sizeof(u32) },
{ &gGba.dma.dma2Dest , sizeof(u32) },
{ &gGba.dma.dma3Source , sizeof(u32) },
{ &gGba.dma.dma3Dest , sizeof(u32) },
{ &gGba.lcd.fxOn, sizeof(bool) },
{ &gGba.lcd.windowOn, sizeof(bool) },
{ &saveNFlag , sizeof(bool) },
{ &gGba.cpu.C_FLAG , sizeof(bool) },
{ &saveZFlag , sizeof(bool) },
{ &gGba.cpu.V_FLAG , sizeof(bool) },
{ &gGba.cpu.armState , sizeof(bool) },
{ &gGba.cpu.armIrqEnable , sizeof(bool) },
{ &gGba.cpu.armNextPC , sizeof(u32) },
{ &gGba.cpu.armMode , sizeof(int) },
{ &saveType , sizeof(int) },
{ NULL, 0 }
};
static int romSize = 0x2000000;
#ifdef PROFILING
void cpuProfil(profile_segment *seg)
{
profilSegment = seg;
}
void cpuEnableProfiling(int hz)
{
if(hz == 0)
hz = 100;
profilingTicks = profilingTicksReload = 16777216 / hz;
profSetHertz(hz);
}
#endif
inline int CPUUpdateTicks(ARM7TDMI &cpu)
{
#ifdef VBAM_USE_SWITICKS
int &SWITicks = cpu.SWITicks;
#endif
#ifdef VBAM_USE_IRQTICKS
int &IRQTicks = cpu.IRQTicks;
#endif
int cpuLoopTicks = cpu.gba->lcd.lcdTicks;
/*if(soundTicks < cpuLoopTicks)
cpuLoopTicks = soundTicks;*/
auto &timer0On = cpu.gba->timers.timer0On;
auto &timer0Ticks = cpu.gba->timers.timer0Ticks;
auto &timer1On = cpu.gba->timers.timer1On;
auto &timer1Ticks = cpu.gba->timers.timer1Ticks;
auto &timer2On = cpu.gba->timers.timer2On;
auto &timer2Ticks = cpu.gba->timers.timer2Ticks;
auto &timer3On = cpu.gba->timers.timer3On;
auto &timer3Ticks = cpu.gba->timers.timer3Ticks;
if(timer0On && (timer0Ticks < cpuLoopTicks)) {
cpuLoopTicks = timer0Ticks;
}
if(timer1On && !(cpu.gba->mem.ioMem.TM1CNT & 4) && (timer1Ticks < cpuLoopTicks)) {
cpuLoopTicks = timer1Ticks;
}
if(timer2On && !(cpu.gba->mem.ioMem.TM2CNT & 4) && (timer2Ticks < cpuLoopTicks)) {
cpuLoopTicks = timer2Ticks;
}
if(timer3On && !(cpu.gba->mem.ioMem.TM3CNT & 4) && (timer3Ticks < cpuLoopTicks)) {
cpuLoopTicks = timer3Ticks;
}
#ifdef PROFILING
if(profilingTicksReload != 0) {
if(profilingTicks < cpuLoopTicks) {
cpuLoopTicks = profilingTicks;
}
}
#endif
#ifdef VBAM_USE_SWITICKS
if (SWITicks) {
if (SWITicks < cpuLoopTicks)
cpuLoopTicks = SWITicks;
}
#endif
#ifdef VBAM_USE_IRQTICKS
if (IRQTicks) {
if (IRQTicks < cpuLoopTicks)
cpuLoopTicks = IRQTicks;
}
#endif
return cpuLoopTicks;
}
int GetTickCount()
{
// From http://stackoverflow.com/questions/741830/getting-the-time-elapsed-objective-c
static mach_timebase_info_data_t sTimebaseInfo;
uint64_t machTime = mach_absolute_time();
// Convert to nanoseconds - if this is the first time we've run, get the timebase.
if (sTimebaseInfo.denom == 0)
{
mach_timebase_info(&sTimebaseInfo);
}
// Convert the mach time to milliseconds
uint64_t millis = ((machTime / 1000000) * sTimebaseInfo.numer) / sTimebaseInfo.denom;
return millis;
}
static void CPUUpdateWindow0(GBASys &gba)
{
int x00 = gba.mem.ioMem.WIN0H>>8;
int x01 = gba.mem.ioMem.WIN0H & 255;
if(x00 <= x01) {
for(int i = 0; i < 240; i++) {
gba.lcd.gfxInWin0[i] = (i >= x00 && i < x01);
}
} else {
for(int i = 0; i < 240; i++) {
gba.lcd.gfxInWin0[i] = (i >= x00 || i < x01);
}
}
}
static void CPUUpdateWindow1(GBASys &gba)
{
int x00 = gba.mem.ioMem.WIN1H>>8;
int x01 = gba.mem.ioMem.WIN1H & 255;
if(x00 <= x01) {
for(int i = 0; i < 240; i++) {
gba.lcd.gfxInWin1[i] = (i >= x00 && i < x01);
}
} else {
for(int i = 0; i < 240; i++) {
gba.lcd.gfxInWin1[i] = (i >= x00 || i < x01);
}
}
}
static void CPUUpdateRenderBuffers(GBASys &gba, bool force)
{
if(!(gba.lcd.layerEnable & 0x0100) || force) {
gfxClearArray(gba.lcd.line0);
}
if(!(gba.lcd.layerEnable & 0x0200) || force) {
gfxClearArray(gba.lcd.line1);
}
if(!(gba.lcd.layerEnable & 0x0400) || force) {
gfxClearArray(gba.lcd.line2);
}
if(!(gba.lcd.layerEnable & 0x0800) || force) {
gfxClearArray(gba.lcd.line3);
}
}
static bool CPUWriteState(GBASys &gba, gzFile gzFile)
{
utilWriteInt(gzFile, SAVE_GAME_VERSION);
utilGzWrite(gzFile, &gba.mem.rom[0xa0], 16);
utilWriteInt(gzFile, useBios);
utilGzWrite(gzFile, &gba.cpu.reg[0], sizeof(gba.cpu.reg));
saveNFlag = gba.cpu.nFlag();
saveZFlag = gba.cpu.zFlag();
utilWriteData(gzFile, saveGameStruct);
// new to version 0.7.1
utilWriteInt(gzFile, gba.stopState);
// new to version 0.8
#ifdef VBAM_USE_IRQTICKS
utilWriteInt(gzFile, gCpu.IRQTicks);
#else
int dummyIRQTicks = 0;
utilWriteInt(gzFile, dummyIRQTicks);
#endif
utilGzWrite(gzFile, gba.mem.internalRAM, 0x8000);
utilGzWrite(gzFile, gba.lcd.paletteRAM, 0x400);
utilGzWrite(gzFile, gba.mem.workRAM, 0x40000);
utilGzWrite(gzFile, gba.lcd.vram, 0x20000);
utilGzWrite(gzFile, gba.lcd.oam, 0x400);
u32 dummyPix[241*162] = {0};
utilGzWrite(gzFile, dummyPix, 4*241*162);
utilGzWrite(gzFile, gba.mem.ioMem.b, 0x400);
eepromSaveGame(gzFile);
flashSaveGame(gzFile);
soundSaveGame(gzFile);
cheatsSaveGame(gzFile);
// version 1.5
rtcSaveGame(gzFile);
return true;
}
bool CPUWriteState(GBASys &gba, const char *file)
{
gzFile gzFile = utilGzOpen(file, "wb");
if(gzFile == NULL) {
systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), file);
return false;
}
bool res = CPUWriteState(gba, gzFile);
utilGzClose(gzFile);
return res;
}
bool CPUWriteMemState(GBASys &gba, char *memory, int available)
{
gzFile gzFile = utilMemGzOpen(memory, available, "w");
if(gzFile == NULL) {
return false;
}
bool res = CPUWriteState(gba, gzFile);
long pos = utilGzMemTell(gzFile)+8;
if(pos >= (available))
res = false;
utilGzClose(gzFile);
return res;
}
static bool CPUReadState(GBASys &gba, gzFile gzFile)
{
int version = utilReadInt(gzFile);
if(version > SAVE_GAME_VERSION || version < SAVE_GAME_VERSION_1) {
systemMessage(MSG_UNSUPPORTED_VBA_SGM,
N_("Unsupported VisualBoyAdvance save game version %d"),
version);
return false;
}
u8 romname[17];
utilGzRead(gzFile, romname, 16);
if(memcmp(&gba.mem.rom[0xa0], romname, 16) != 0) {
romname[16]=0;
for(int i = 0; i < 16; i++)
if(romname[i] < 32)
romname[i] = 32;
systemMessage(MSG_CANNOT_LOAD_SGM, N_("Cannot load save game for %s"), romname);
return false;
}
bool ub = utilReadInt(gzFile) ? true : false;
if(ub != useBios) {
if(useBios)
systemMessage(MSG_SAVE_GAME_NOT_USING_BIOS,
N_("Save game is not using the BIOS files"));
else
systemMessage(MSG_SAVE_GAME_USING_BIOS,
N_("Save game is using the BIOS file"));
return false;
}
utilGzRead(gzFile, &gba.cpu.reg[0], sizeof(gba.cpu.reg));
utilReadData(gzFile, saveGameStruct);
gba.cpu.updateNZFlags(saveNFlag, saveZFlag);
if(version < SAVE_GAME_VERSION_3)
gba.stopState = false;
else
gba.stopState = utilReadInt(gzFile) ? true : false;
if(version < SAVE_GAME_VERSION_4)
{
#ifdef VBAM_USE_IRQTICKS
gCpu.IRQTicks = 0;
#endif
gba.intState = false;
}
else
{
#ifdef VBAM_USE_IRQTICKS
gCpu.IRQTicks = utilReadInt(gzFile);
if (gCpu.IRQTicks>0)
intState = true;
else
{
intState = false;
gCpu.IRQTicks = 0;
}
#else
utilReadInt(gzFile);
gba.intState = false;
#endif
}
utilGzRead(gzFile, gba.mem.internalRAM, 0x8000);
utilGzRead(gzFile, gba.lcd.paletteRAM, 0x400);
utilGzRead(gzFile, gba.mem.workRAM, 0x40000);
utilGzRead(gzFile, gba.lcd.vram, 0x20000);
utilGzRead(gzFile, gba.lcd.oam, 0x400);
u32 dummyPix[241*162];
if(version < SAVE_GAME_VERSION_6)
utilGzRead(gzFile, dummyPix, 4*240*160);
else
utilGzRead(gzFile, dummyPix, 4*241*162);
utilGzRead(gzFile, gba.mem.ioMem.b, 0x400);
if(skipSaveGameBattery) {
// skip eeprom data
eepromReadGameSkip(gzFile, version);
// skip flash data
flashReadGameSkip(gzFile, version);
} else {
eepromReadGame(gzFile, version);
flashReadGame(gzFile, version);
}
soundReadGame(gba, gzFile, version);
if(version > SAVE_GAME_VERSION_1) {
if(skipSaveGameCheats) {
// skip cheats list data
cheatsReadGameSkip(gzFile, version);
} else {
cheatsReadGame(gzFile, version);
}
}
if(version > SAVE_GAME_VERSION_6) {
rtcReadGame(gzFile);
}
if(version <= SAVE_GAME_VERSION_7) {
u32 temp;
#define SWAP(a,b,c) \
temp = (a);\
(a) = (b)<<16|(c);\
(b) = (temp) >> 16;\
(c) = (temp) & 0xFFFF;
SWAP(gba.dma.dma0Source, gba.mem.ioMem.DM0SAD_H, gba.mem.ioMem.DM0SAD_L);
SWAP(gba.dma.dma0Dest, gba.mem.ioMem.DM0DAD_H, gba.mem.ioMem.DM0DAD_L);
SWAP(gba.dma.dma1Source, gba.mem.ioMem.DM1SAD_H, gba.mem.ioMem.DM1SAD_L);
SWAP(gba.dma.dma1Dest, gba.mem.ioMem.DM1DAD_H, gba.mem.ioMem.DM1DAD_L);
SWAP(gba.dma.dma2Source, gba.mem.ioMem.DM2SAD_H, gba.mem.ioMem.DM2SAD_L);
SWAP(gba.dma.dma2Dest, gba.mem.ioMem.DM2DAD_H, gba.mem.ioMem.DM2DAD_L);
SWAP(gba.dma.dma3Source, gba.mem.ioMem.DM3SAD_H, gba.mem.ioMem.DM3SAD_L);
SWAP(gba.dma.dma3Dest, gba.mem.ioMem.DM3DAD_H, gba.mem.ioMem.DM3DAD_L);
}
if(version <= SAVE_GAME_VERSION_8) {
gba.timers.timer0ClockReload = TIMER_TICKS[gba.mem.ioMem.TM0CNT & 3];
gba.timers.timer1ClockReload = TIMER_TICKS[gba.mem.ioMem.TM1CNT & 3];
gba.timers.timer2ClockReload = TIMER_TICKS[gba.mem.ioMem.TM2CNT & 3];
gba.timers.timer3ClockReload = TIMER_TICKS[gba.mem.ioMem.TM3CNT & 3];
gba.timers.timer0Ticks = ((0x10000 - gba.mem.ioMem.TM0D) << gba.timers.timer0ClockReload) - gba.timers.timer0Ticks;
gba.timers.timer1Ticks = ((0x10000 - gba.mem.ioMem.TM1D) << gba.timers.timer1ClockReload) - gba.timers.timer1Ticks;
gba.timers.timer2Ticks = ((0x10000 - gba.mem.ioMem.TM2D) << gba.timers.timer2ClockReload) - gba.timers.timer2Ticks;
gba.timers.timer3Ticks = ((0x10000 - gba.mem.ioMem.TM3D) << gba.timers.timer3ClockReload) - gba.timers.timer3Ticks;
interp_rate();
}
// set pointers!
gba.lcd.layerEnable = layerSettings & gba.mem.ioMem.DISPCNT;
CPUUpdateRender(gba);
CPUUpdateRenderBuffers(gba, true);
CPUUpdateWindow0(gba);
CPUUpdateWindow1(gba);
gbaSaveType = 0;
switch(saveType) {
case 0:
cpuSaveGameFunc = flashSaveDecide;
break;
case 1:
cpuSaveGameFunc = sramWrite;
gbaSaveType = 1;
break;
case 2:
cpuSaveGameFunc = flashWrite;
gbaSaveType = 2;
break;
case 3:
break;
case 5:
gbaSaveType = 5;
break;
default:
systemMessage(MSG_UNSUPPORTED_SAVE_TYPE,
N_("Unsupported save type %d"), saveType);
break;
}
if(eepromInUse)
gbaSaveType = 3;
systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
if(gba.cpu.armState) {
gba.cpu.ARM_PREFETCH();
} else {
gba.cpu.THUMB_PREFETCH();
}