-
-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathcore.c
More file actions
1264 lines (1139 loc) · 39.3 KB
/
core.c
File metadata and controls
1264 lines (1139 loc) · 39.3 KB
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
/* Copyright (c) 2013-2016 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <mgba/gb/core.h>
#include <mgba/core/core.h>
#include <mgba/internal/debugger/symbols.h>
#include <mgba/internal/gb/cheats.h>
#include <mgba/internal/gb/debugger/debugger.h>
#include <mgba/internal/gb/debugger/symbols.h>
#include <mgba/internal/gb/extra/cli.h>
#include <mgba/internal/gb/io.h>
#include <mgba/internal/gb/gb.h>
#include <mgba/internal/gb/mbc.h>
#include <mgba/internal/gb/overrides.h>
#include <mgba/internal/gb/renderers/software.h>
#include <mgba/internal/gb/renderers/proxy.h>
#include <mgba/internal/gb/serialize.h>
#include <mgba/internal/sm83/sm83.h>
#include <mgba/internal/sm83/debugger/debugger.h>
#include <mgba-util/crc32.h>
#include <mgba-util/memory.h>
#include <mgba-util/patch.h>
#include <mgba-util/vfs.h>
static const struct mCoreChannelInfo _GBVideoLayers[] = {
{ GB_LAYER_BACKGROUND, "bg", "Background", NULL },
{ GB_LAYER_WINDOW, "bgwin", "Window", NULL },
{ GB_LAYER_OBJ, "obj", "Objects", NULL },
};
static const struct mCoreChannelInfo _GBAudioChannels[] = {
{ 0, "ch1", "Channel 1", "Square/Sweep" },
{ 1, "ch2", "Channel 2", "Square" },
{ 2, "ch3", "Channel 3", "PCM" },
{ 3, "ch4", "Channel 4", "Noise" },
};
static const struct mCoreMemoryBlock _GBMemoryBlocks[] = {
{ -1, "mem", "All", "All", 0, 0x10000, 0x10000, mCORE_MEMORY_VIRTUAL },
{ GB_REGION_CART_BANK0, "cart0", "ROM Bank", "Game Pak (32kiB)", GB_BASE_CART_BANK0, GB_BASE_CART_BANK0 + GB_SIZE_CART_BANK0 * 2, 0x800000, mCORE_MEMORY_READ | mCORE_MEMORY_WORM | mCORE_MEMORY_MAPPED, 511, GB_BASE_CART_BANK0 + GB_SIZE_CART_BANK0 },
{ GB_REGION_VRAM, "vram", "VRAM", "Video RAM (8kiB)", GB_BASE_VRAM, GB_BASE_VRAM + GB_SIZE_VRAM, GB_SIZE_VRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
{ GB_REGION_EXTERNAL_RAM, "sram", "SRAM", "External RAM (8kiB)", GB_BASE_EXTERNAL_RAM, GB_BASE_EXTERNAL_RAM + GB_SIZE_EXTERNAL_RAM, GB_SIZE_EXTERNAL_RAM * 4, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED, 3 },
{ GB_REGION_WORKING_RAM_BANK0, "wram", "WRAM", "Working RAM (8kiB)", GB_BASE_WORKING_RAM_BANK0, GB_BASE_WORKING_RAM_BANK0 + GB_SIZE_WORKING_RAM_BANK0 * 2 , GB_SIZE_WORKING_RAM_BANK0 * 2, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
{ GB_BASE_OAM, "oam", "OAM", "OBJ Attribute Memory", GB_BASE_OAM, GB_BASE_OAM + GB_SIZE_OAM, GB_SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
{ GB_BASE_IO, "io", "MMIO", "Memory-Mapped I/O", GB_BASE_IO, GB_BASE_IO + GB_SIZE_IO, GB_SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
{ GB_BASE_HRAM, "hram", "HRAM", "High RAM", GB_BASE_HRAM, GB_BASE_HRAM + GB_SIZE_HRAM, GB_SIZE_HRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
};
static const struct mCoreMemoryBlock _GBCMemoryBlocks[] = {
{ -1, "mem", "All", "All", 0, 0x10000, 0x10000, mCORE_MEMORY_VIRTUAL },
{ GB_REGION_CART_BANK0, "cart0", "ROM Bank", "Game Pak (32kiB)", GB_BASE_CART_BANK0, GB_BASE_CART_BANK0 + GB_SIZE_CART_BANK0 * 2, 0x800000, mCORE_MEMORY_READ | mCORE_MEMORY_WORM | mCORE_MEMORY_MAPPED, 511, GB_BASE_CART_BANK0 + GB_SIZE_CART_BANK0 },
{ GB_REGION_VRAM, "vram", "VRAM", "Video RAM (8kiB)", GB_BASE_VRAM, GB_BASE_VRAM + GB_SIZE_VRAM, GB_SIZE_VRAM * 2, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED, 1 },
{ GB_REGION_EXTERNAL_RAM, "sram", "SRAM", "External RAM (8kiB)", GB_BASE_EXTERNAL_RAM, GB_BASE_EXTERNAL_RAM + GB_SIZE_EXTERNAL_RAM, GB_SIZE_EXTERNAL_RAM * 4, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED, 3 },
{ GB_REGION_WORKING_RAM_BANK0, "wram", "WRAM", "Working RAM (8kiB)", GB_BASE_WORKING_RAM_BANK0, GB_BASE_WORKING_RAM_BANK0 + GB_SIZE_WORKING_RAM_BANK0 * 2, GB_SIZE_WORKING_RAM_BANK0 * 8, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED, 7, GB_BASE_WORKING_RAM_BANK0 + GB_SIZE_WORKING_RAM_BANK0 },
{ GB_BASE_OAM, "oam", "OAM", "OBJ Attribute Memory", GB_BASE_OAM, GB_BASE_OAM + GB_SIZE_OAM, GB_SIZE_OAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
{ GB_BASE_IO, "io", "MMIO", "Memory-Mapped I/O", GB_BASE_IO, GB_BASE_IO + GB_SIZE_IO, GB_SIZE_IO, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
{ GB_BASE_HRAM, "hram", "HRAM", "High RAM", GB_BASE_HRAM, GB_BASE_HRAM + GB_SIZE_HRAM, GB_SIZE_HRAM, mCORE_MEMORY_RW | mCORE_MEMORY_MAPPED },
};
struct mVideoLogContext;
struct GBCore {
struct mCore d;
struct GBVideoRenderer dummyRenderer;
struct GBVideoSoftwareRenderer renderer;
#ifndef MINIMAL_CORE
struct GBVideoProxyRenderer proxyRenderer;
struct mVideoLogContext* logContext;
#endif
struct mCoreCallbacks logCallbacks;
uint8_t keys;
struct mCPUComponent* components[CPU_COMPONENT_MAX];
const struct Configuration* overrides;
struct mDebuggerPlatform* debuggerPlatform;
struct mCheatDevice* cheatDevice;
struct mCoreMemoryBlock memoryBlocks[8];
};
static bool _GBCoreInit(struct mCore* core) {
struct GBCore* gbcore = (struct GBCore*) core;
struct SM83Core* cpu = anonymousMemoryMap(sizeof(struct SM83Core));
struct GB* gb = anonymousMemoryMap(sizeof(struct GB));
if (!cpu || !gb) {
free(cpu);
free(gb);
return false;
}
core->cpu = cpu;
core->board = gb;
core->timing = &gb->timing;
gbcore->overrides = NULL;
gbcore->debuggerPlatform = NULL;
gbcore->cheatDevice = NULL;
#ifndef MINIMAL_CORE
gbcore->logContext = NULL;
#endif
memcpy(gbcore->memoryBlocks, _GBMemoryBlocks, sizeof(_GBMemoryBlocks));
GBCreate(gb);
memset(gbcore->components, 0, sizeof(gbcore->components));
SM83SetComponents(cpu, &gb->d, CPU_COMPONENT_MAX, gbcore->components);
SM83Init(cpu);
mRTCGenericSourceInit(&core->rtc, core);
gb->memory.rtc = &core->rtc.d;
GBVideoDummyRendererCreate(&gbcore->dummyRenderer);
GBVideoAssociateRenderer(&gb->video, &gbcore->dummyRenderer);
GBVideoSoftwareRendererCreate(&gbcore->renderer);
gbcore->renderer.outputBuffer = NULL;
#ifndef MINIMAL_CORE
gbcore->proxyRenderer.logger = NULL;
#endif
gbcore->keys = 0;
gb->keySource = &gbcore->keys;
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
mDirectorySetInit(&core->dirs);
#endif
return true;
}
static void _GBCoreDeinit(struct mCore* core) {
SM83Deinit(core->cpu);
GBDestroy(core->board);
mappedMemoryFree(core->cpu, sizeof(struct SM83Core));
mappedMemoryFree(core->board, sizeof(struct GB));
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
mDirectorySetDeinit(&core->dirs);
#endif
#ifdef USE_DEBUGGERS
if (core->symbolTable) {
mDebuggerSymbolTableDestroy(core->symbolTable);
}
#endif
struct GBCore* gbcore = (struct GBCore*) core;
free(gbcore->debuggerPlatform);
if (gbcore->cheatDevice) {
mCheatDeviceDestroy(gbcore->cheatDevice);
}
mCoreConfigFreeOpts(&core->opts);
free(core);
}
static enum mPlatform _GBCorePlatform(const struct mCore* core) {
UNUSED(core);
return mPLATFORM_GB;
}
static bool _GBCoreSupportsFeature(const struct mCore* core, enum mCoreFeature feature) {
UNUSED(core);
switch (feature) {
default:
return false;
}
}
static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
struct GB* gb = core->board;
gb->sync = sync;
}
static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
UNUSED(config);
struct GB* gb = core->board;
if (core->opts.mute) {
gb->audio.masterVolume = 0;
} else {
gb->audio.masterVolume = core->opts.volume;
}
gb->video.frameskip = core->opts.frameskip;
int color;
if (mCoreConfigGetIntValue(config, "gb.pal[0]", &color)) {
GBVideoSetPalette(&gb->video, 0, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[1]", &color)) {
GBVideoSetPalette(&gb->video, 1, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[2]", &color)) {
GBVideoSetPalette(&gb->video, 2, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[3]", &color)) {
GBVideoSetPalette(&gb->video, 3, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[4]", &color)) {
GBVideoSetPalette(&gb->video, 4, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[5]", &color)) {
GBVideoSetPalette(&gb->video, 5, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[6]", &color)) {
GBVideoSetPalette(&gb->video, 6, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[7]", &color)) {
GBVideoSetPalette(&gb->video, 7, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[8]", &color)) {
GBVideoSetPalette(&gb->video, 8, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[9]", &color)) {
GBVideoSetPalette(&gb->video, 9, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[10]", &color)) {
GBVideoSetPalette(&gb->video, 10, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[11]", &color)) {
GBVideoSetPalette(&gb->video, 11, color);
}
mCoreConfigCopyValue(&core->config, config, "gb.bios");
mCoreConfigCopyValue(&core->config, config, "sgb.bios");
mCoreConfigCopyValue(&core->config, config, "gbc.bios");
mCoreConfigCopyValue(&core->config, config, "gb.model");
mCoreConfigCopyValue(&core->config, config, "sgb.model");
mCoreConfigCopyValue(&core->config, config, "cgb.model");
mCoreConfigCopyValue(&core->config, config, "cgb.hybridModel");
mCoreConfigCopyValue(&core->config, config, "cgb.sgbModel");
mCoreConfigCopyValue(&core->config, config, "gb.colors");
mCoreConfigCopyValue(&core->config, config, "useCgbColors");
mCoreConfigCopyValue(&core->config, config, "allowOpposingDirections");
int fakeBool = 0;
mCoreConfigGetIntValue(config, "allowOpposingDirections", &fakeBool);
gb->allowOpposingDirections = fakeBool;
if (mCoreConfigGetIntValue(config, "sgb.borders", &fakeBool)) {
gb->video.sgbBorders = fakeBool;
gb->video.renderer->enableSGBBorder(gb->video.renderer, fakeBool);
}
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
struct GBCore* gbcore = (struct GBCore*) core;
gbcore->overrides = mCoreConfigGetOverridesConst(config);
#endif
}
static void _GBCoreReloadConfigOption(struct mCore* core, const char* option, const struct mCoreConfig* config) {
struct GB* gb = core->board;
if (!config) {
config = &core->config;
}
if (!option) {
// Reload options from opts
if (core->opts.mute) {
gb->audio.masterVolume = 0;
} else {
gb->audio.masterVolume = core->opts.volume;
}
gb->video.frameskip = core->opts.frameskip;
return;
}
int fakeBool;
if (strcmp("mute", option) == 0) {
if (mCoreConfigGetIntValue(config, "mute", &fakeBool)) {
core->opts.mute = fakeBool;
if (core->opts.mute) {
gb->audio.masterVolume = 0;
} else {
gb->audio.masterVolume = core->opts.volume;
}
}
return;
}
if (strcmp("volume", option) == 0) {
if (mCoreConfigGetIntValue(config, "volume", &core->opts.volume) && !core->opts.mute) {
gb->audio.masterVolume = core->opts.volume;
}
return;
}
if (strcmp("frameskip", option) == 0) {
if (mCoreConfigGetIntValue(config, "frameskip", &core->opts.frameskip)) {
gb->video.frameskip = core->opts.frameskip;
}
return;
}
if (strcmp("allowOpposingDirections", option) == 0) {
if (config != &core->config) {
mCoreConfigCopyValue(&core->config, config, "allowOpposingDirections");
}
if (mCoreConfigGetIntValue(config, "allowOpposingDirections", &fakeBool)) {
gb->allowOpposingDirections = fakeBool;
}
return;
}
if (strcmp("sgb.borders", option) == 0) {
if (mCoreConfigGetIntValue(config, "sgb.borders", &fakeBool)) {
gb->video.sgbBorders = fakeBool;
gb->video.renderer->enableSGBBorder(gb->video.renderer, fakeBool);
}
}
if (strcmp("gb.pal", option) == 0) {
int color;
if (mCoreConfigGetIntValue(config, "gb.pal[0]", &color)) {
GBVideoSetPalette(&gb->video, 0, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[1]", &color)) {
GBVideoSetPalette(&gb->video, 1, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[2]", &color)) {
GBVideoSetPalette(&gb->video, 2, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[3]", &color)) {
GBVideoSetPalette(&gb->video, 3, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[4]", &color)) {
GBVideoSetPalette(&gb->video, 4, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[5]", &color)) {
GBVideoSetPalette(&gb->video, 5, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[6]", &color)) {
GBVideoSetPalette(&gb->video, 6, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[7]", &color)) {
GBVideoSetPalette(&gb->video, 7, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[8]", &color)) {
GBVideoSetPalette(&gb->video, 8, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[9]", &color)) {
GBVideoSetPalette(&gb->video, 9, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[10]", &color)) {
GBVideoSetPalette(&gb->video, 10, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[11]", &color)) {
GBVideoSetPalette(&gb->video, 11, color);
}
if (gb->model < GB_MODEL_SGB) {
GBVideoWritePalette(&gb->video, GB_REG_BGP, gb->memory.io[GB_REG_BGP]);
GBVideoWritePalette(&gb->video, GB_REG_OBP0, gb->memory.io[GB_REG_OBP0]);
GBVideoWritePalette(&gb->video, GB_REG_OBP1, gb->memory.io[GB_REG_OBP1]);
}
}
}
static void _GBCoreDesiredVideoDimensions(const struct mCore* core, unsigned* width, unsigned* height) {
const struct GB* gb = core->board;
if (gb && (!(gb->model & GB_MODEL_SGB) || !gb->video.sgbBorders)) {
*width = GB_VIDEO_HORIZONTAL_PIXELS;
*height = GB_VIDEO_VERTICAL_PIXELS;
} else {
*width = 256;
*height = 224;
}
}
static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
struct GBCore* gbcore = (struct GBCore*) core;
gbcore->renderer.outputBuffer = buffer;
gbcore->renderer.outputBufferStride = stride;
}
static void _GBCoreSetVideoGLTex(struct mCore* core, unsigned texid) {
UNUSED(core);
UNUSED(texid);
}
static void _GBCoreGetPixels(struct mCore* core, const void** buffer, size_t* stride) {
struct GBCore* gbcore = (struct GBCore*) core;
gbcore->renderer.d.getPixels(&gbcore->renderer.d, stride, buffer);
}
static void _GBCorePutPixels(struct mCore* core, const void* buffer, size_t stride) {
struct GBCore* gbcore = (struct GBCore*) core;
gbcore->renderer.d.putPixels(&gbcore->renderer.d, stride, buffer);
}
static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
struct GB* gb = core->board;
switch (ch) {
case 0:
return gb->audio.left;
case 1:
return gb->audio.right;
default:
return NULL;
}
}
static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
struct GB* gb = core->board;
GBAudioResizeBuffer(&gb->audio, samples);
}
static size_t _GBCoreGetAudioBufferSize(struct mCore* core) {
struct GB* gb = core->board;
return gb->audio.samples;
}
static void _GBCoreAddCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) {
struct GB* gb = core->board;
*mCoreCallbacksListAppend(&gb->coreCallbacks) = *coreCallbacks;
}
static void _GBCoreClearCoreCallbacks(struct mCore* core) {
struct GB* gb = core->board;
mCoreCallbacksListClear(&gb->coreCallbacks);
}
static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
struct GB* gb = core->board;
gb->stream = stream;
if (stream && stream->videoDimensionsChanged) {
unsigned width, height;
core->desiredVideoDimensions(core, &width, &height);
stream->videoDimensionsChanged(stream, width, height);
}
}
static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
return GBLoadROM(core->board, vf);
}
static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
UNUSED(type);
GBLoadBIOS(core->board, vf);
return true;
}
static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
return GBLoadSave(core->board, vf);
}
static bool _GBCoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
struct GB* gb = core->board;
GBSavedataMask(gb, vf, false);
return true; // TODO: Return a real value
}
static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
if (!vf) {
return false;
}
struct Patch patch;
if (!loadPatch(vf, &patch)) {
return false;
}
GBApplyPatch(core->board, &patch);
return true;
}
static void _GBCoreUnloadROM(struct mCore* core) {
struct GBCore* gbcore = (struct GBCore*) core;
struct SM83Core* cpu = core->cpu;
if (gbcore->cheatDevice) {
SM83HotplugDetach(cpu, CPU_COMPONENT_CHEAT_DEVICE);
cpu->components[CPU_COMPONENT_CHEAT_DEVICE] = NULL;
mCheatDeviceDestroy(gbcore->cheatDevice);
gbcore->cheatDevice = NULL;
}
return GBUnloadROM(core->board);
}
static void _GBCoreChecksum(const struct mCore* core, void* data, enum mCoreChecksumType type) {
struct GB* gb = (struct GB*) core->board;
switch (type) {
case mCHECKSUM_CRC32:
memcpy(data, &gb->romCrc32, sizeof(gb->romCrc32));
break;
}
return;
}
static void _GBCoreReset(struct mCore* core) {
struct GBCore* gbcore = (struct GBCore*) core;
struct GB* gb = (struct GB*) core->board;
if (gbcore->renderer.outputBuffer) {
GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
}
if (gb->memory.rom) {
int doColorOverride = GB_COLORS_NONE;
mCoreConfigGetIntValue(&core->config, "gb.colors", &doColorOverride);
if (doColorOverride == GB_COLORS_NONE) {
// Backwards compat for renamed setting
mCoreConfigGetIntValue(&core->config, "useCgbColors", &doColorOverride);
}
struct GBCartridgeOverride override;
const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
override.headerCrc32 = doCrc32(cart, sizeof(*cart));
bool modelOverride = GBOverrideFind(gbcore->overrides, &override) || (doColorOverride && GBOverrideColorFind(&override, doColorOverride));
if (modelOverride) {
GBOverrideApply(gb, &override);
}
if (!modelOverride || override.model == GB_MODEL_AUTODETECT) {
const char* modelGB = mCoreConfigGetValue(&core->config, "gb.model");
const char* modelSGB = mCoreConfigGetValue(&core->config, "sgb.model");
const char* modelCGB = mCoreConfigGetValue(&core->config, "cgb.model");
const char* modelCGBHybrid = mCoreConfigGetValue(&core->config, "cgb.hybridModel");
const char* modelCGBSGB = mCoreConfigGetValue(&core->config, "cgb.sgbModel");
if (modelGB || modelCGB || modelSGB || modelCGBHybrid || modelCGBSGB) {
int models = GBValidModels(gb->memory.rom);
switch (models) {
case GB_MODEL_SGB | GB_MODEL_MGB:
if (modelSGB) {
gb->model = GBNameToModel(modelSGB);
}
break;
case GB_MODEL_MGB:
if (modelGB) {
gb->model = GBNameToModel(modelGB);
}
break;
case GB_MODEL_MGB | GB_MODEL_CGB:
if (modelCGBHybrid) {
gb->model = GBNameToModel(modelCGBHybrid);
}
break;
case GB_MODEL_SGB | GB_MODEL_CGB: // TODO: Do these even exist?
case GB_MODEL_MGB | GB_MODEL_SGB | GB_MODEL_CGB:
if (modelCGBSGB) {
gb->model = GBNameToModel(modelCGBSGB);
}
break;
case GB_MODEL_CGB:
if (modelCGB) {
gb->model = GBNameToModel(modelCGB);
}
break;
}
}
}
}
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
if (!gb->biosVf && core->opts.useBios) {
struct VFile* bios = NULL;
bool found = false;
if (core->opts.bios) {
bios = VFileOpen(core->opts.bios, O_RDONLY);
if (bios && GBIsBIOS(bios)) {
found = true;
} else if (bios) {
bios->close(bios);
bios = NULL;
}
}
if (!found) {
GBDetectModel(gb);
const char* configPath = NULL;
switch (gb->model) {
case GB_MODEL_DMG:
case GB_MODEL_MGB: // TODO
configPath = mCoreConfigGetValue(&core->config, "gb.bios");
break;
case GB_MODEL_SGB:
case GB_MODEL_SGB2: // TODO
configPath = mCoreConfigGetValue(&core->config, "sgb.bios");
break;
case GB_MODEL_CGB:
case GB_MODEL_AGB:
case GB_MODEL_SCGB:
configPath = mCoreConfigGetValue(&core->config, "gbc.bios");
break;
default:
break;
};
if (configPath) {
bios = VFileOpen(configPath, O_RDONLY);
}
if (bios && GBIsBIOS(bios)) {
found = true;
} else if (bios) {
bios->close(bios);
bios = NULL;
}
}
if (!found) {
char path[PATH_MAX];
mCoreConfigDirectory(path, PATH_MAX);
switch (gb->model) {
case GB_MODEL_DMG:
case GB_MODEL_MGB: // TODO
strncat(path, PATH_SEP "gb_bios.bin", PATH_MAX - strlen(path));
break;
case GB_MODEL_SGB:
case GB_MODEL_SGB2: // TODO
strncat(path, PATH_SEP "sgb_bios.bin", PATH_MAX - strlen(path));
break;
case GB_MODEL_CGB:
case GB_MODEL_AGB:
case GB_MODEL_SCGB:
strncat(path, PATH_SEP "gbc_bios.bin", PATH_MAX - strlen(path));
break;
default:
break;
};
bios = VFileOpen(path, O_RDONLY);
if (bios && GBIsBIOS(bios)) {
found = true;
} else if (bios) {
bios->close(bios);
bios = NULL;
}
}
if (found && bios) {
GBLoadBIOS(gb, bios);
}
}
#endif
if (gb->model < GB_MODEL_CGB) {
memcpy(gbcore->memoryBlocks, _GBMemoryBlocks, sizeof(_GBMemoryBlocks));
} else {
memcpy(gbcore->memoryBlocks, _GBCMemoryBlocks, sizeof(_GBCMemoryBlocks));
}
size_t i;
for (i = 0; i < sizeof(gbcore->memoryBlocks) / sizeof(*gbcore->memoryBlocks); ++i) {
if (gbcore->memoryBlocks[i].id == GB_REGION_CART_BANK0) {
gbcore->memoryBlocks[i].maxSegment = gb->memory.romSize / GB_SIZE_CART_BANK0;
} else if (gbcore->memoryBlocks[i].id == GB_REGION_EXTERNAL_RAM) {
gbcore->memoryBlocks[i].maxSegment = gb->sramSize / GB_SIZE_EXTERNAL_RAM;
} else {
continue;
}
if (gbcore->memoryBlocks[i].maxSegment) {
--gbcore->memoryBlocks[i].maxSegment;
}
}
SM83Reset(core->cpu);
if (core->opts.skipBios) {
GBSkipBIOS(core->board);
}
}
static void _GBCoreRunFrame(struct mCore* core) {
struct GB* gb = core->board;
int32_t frameCounter = gb->video.frameCounter;
while (gb->video.frameCounter == frameCounter) {
SM83Run(core->cpu);
}
}
static void _GBCoreRunLoop(struct mCore* core) {
SM83Run(core->cpu);
}
static void _GBCoreStep(struct mCore* core) {
struct SM83Core* cpu = core->cpu;
do {
SM83Tick(cpu);
} while (cpu->executionState != SM83_CORE_FETCH);
}
static size_t _GBCoreStateSize(struct mCore* core) {
UNUSED(core);
return sizeof(struct GBSerializedState);
}
static bool _GBCoreLoadState(struct mCore* core, const void* state) {
return GBDeserialize(core->board, state);
}
static bool _GBCoreSaveState(struct mCore* core, void* state) {
struct SM83Core* cpu = core->cpu;
while (cpu->executionState != SM83_CORE_FETCH) {
SM83Tick(cpu);
}
GBSerialize(core->board, state);
return true;
}
static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
struct GBCore* gbcore = (struct GBCore*) core;
gbcore->keys = keys;
GBTestKeypadIRQ(core->board);
}
static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
struct GBCore* gbcore = (struct GBCore*) core;
gbcore->keys |= keys;
GBTestKeypadIRQ(core->board);
}
static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
struct GBCore* gbcore = (struct GBCore*) core;
gbcore->keys &= ~keys;
}
static int32_t _GBCoreFrameCounter(const struct mCore* core) {
const struct GB* gb = core->board;
return gb->video.frameCounter;
}
static int32_t _GBCoreFrameCycles(const struct mCore* core) {
UNUSED(core);
return GB_VIDEO_TOTAL_LENGTH;
}
static int32_t _GBCoreFrequency(const struct mCore* core) {
UNUSED(core);
// TODO: GB differences
return DMG_SM83_FREQUENCY;
}
static void _GBCoreGetGameTitle(const struct mCore* core, char* title) {
GBGetGameTitle(core->board, title);
}
static void _GBCoreGetGameCode(const struct mCore* core, char* title) {
GBGetGameCode(core->board, title);
}
static void _GBCoreSetPeripheral(struct mCore* core, int type, void* periph) {
struct GB* gb = core->board;
switch (type) {
case mPERIPH_ROTATION:
gb->memory.rotation = periph;
break;
case mPERIPH_RUMBLE:
gb->memory.rumble = periph;
break;
case mPERIPH_IMAGE_SOURCE:
gb->memory.cam = periph;
break;
default:
return;
}
}
static uint32_t _GBCoreBusRead8(struct mCore* core, uint32_t address) {
struct SM83Core* cpu = core->cpu;
return cpu->memory.load8(cpu, address);
}
static uint32_t _GBCoreBusRead16(struct mCore* core, uint32_t address) {
struct SM83Core* cpu = core->cpu;
return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8);
}
static uint32_t _GBCoreBusRead32(struct mCore* core, uint32_t address) {
struct SM83Core* cpu = core->cpu;
return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8) |
(cpu->memory.load8(cpu, address + 2) << 16) | (cpu->memory.load8(cpu, address + 3) << 24);
}
static void _GBCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
struct SM83Core* cpu = core->cpu;
cpu->memory.store8(cpu, address, value);
}
static void _GBCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
struct SM83Core* cpu = core->cpu;
cpu->memory.store8(cpu, address, value);
cpu->memory.store8(cpu, address + 1, value >> 8);
}
static void _GBCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
struct SM83Core* cpu = core->cpu;
cpu->memory.store8(cpu, address, value);
cpu->memory.store8(cpu, address + 1, value >> 8);
cpu->memory.store8(cpu, address + 2, value >> 16);
cpu->memory.store8(cpu, address + 3, value >> 24);
}
static uint32_t _GBCoreRawRead8(struct mCore* core, uint32_t address, int segment) {
struct SM83Core* cpu = core->cpu;
return GBView8(cpu, address, segment);
}
static uint32_t _GBCoreRawRead16(struct mCore* core, uint32_t address, int segment) {
struct SM83Core* cpu = core->cpu;
return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8);
}
static uint32_t _GBCoreRawRead32(struct mCore* core, uint32_t address, int segment) {
struct SM83Core* cpu = core->cpu;
return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8) |
(GBView8(cpu, address + 2, segment) << 16) | (GBView8(cpu, address + 3, segment) << 24);
}
static void _GBCoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
struct SM83Core* cpu = core->cpu;
GBPatch8(cpu, address, value, NULL, segment);
}
static void _GBCoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
struct SM83Core* cpu = core->cpu;
GBPatch8(cpu, address, value, NULL, segment);
GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
}
static void _GBCoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
struct SM83Core* cpu = core->cpu;
GBPatch8(cpu, address, value, NULL, segment);
GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
GBPatch8(cpu, address + 2, value >> 16, NULL, segment);
GBPatch8(cpu, address + 3, value >> 24, NULL, segment);
}
size_t _GBListMemoryBlocks(const struct mCore* core, const struct mCoreMemoryBlock** blocks) {
struct GBCore* gbcore = (struct GBCore*) core;
*blocks = gbcore->memoryBlocks;
return sizeof(gbcore->memoryBlocks) / sizeof(*gbcore->memoryBlocks);
}
void* _GBGetMemoryBlock(struct mCore* core, size_t id, size_t* sizeOut) {
struct GB* gb = core->board;
bool isCgb = gb->model >= GB_MODEL_CGB;
switch (id) {
default:
return NULL;
case GB_REGION_CART_BANK0:
*sizeOut = gb->memory.romSize;
return gb->memory.rom;
case GB_REGION_VRAM:
*sizeOut = GB_SIZE_VRAM_BANK0 * (isCgb ? 1 : 2);
return gb->video.vram;
case GB_REGION_EXTERNAL_RAM:
*sizeOut = gb->sramSize;
return gb->memory.sram;
case GB_REGION_WORKING_RAM_BANK0:
*sizeOut = GB_SIZE_WORKING_RAM_BANK0 * (isCgb ? 8 : 2);
return gb->memory.wram;
case GB_BASE_OAM:
*sizeOut = GB_SIZE_OAM;
return gb->video.oam.raw;
case GB_BASE_HRAM:
*sizeOut = GB_SIZE_HRAM;
return gb->memory.hram;
}
}
#ifdef USE_DEBUGGERS
static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
UNUSED(core);
switch (type) {
case DEBUGGER_CUSTOM:
case DEBUGGER_CLI:
return true;
default:
return false;
}
}
static struct mDebuggerPlatform* _GBCoreDebuggerPlatform(struct mCore* core) {
struct GBCore* gbcore = (struct GBCore*) core;
struct GB* gb = core->board;
if (!gbcore->debuggerPlatform) {
struct SM83Debugger* platform = (struct SM83Debugger*) GBDebuggerCreate(gb);
gbcore->debuggerPlatform = &platform->d;
}
return gbcore->debuggerPlatform;
}
static struct CLIDebuggerSystem* _GBCoreCliDebuggerSystem(struct mCore* core) {
return GBCLIDebuggerCreate(core);
}
static void _GBCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
struct SM83Core* cpu = core->cpu;
if (core->debugger) {
SM83HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
}
cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
SM83HotplugAttach(cpu, CPU_COMPONENT_DEBUGGER);
core->debugger = debugger;
}
static void _GBCoreDetachDebugger(struct mCore* core) {
struct SM83Core* cpu = core->cpu;
if (core->debugger) {
SM83HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
}
cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
core->debugger = NULL;
}
static void _GBCoreLoadSymbols(struct mCore* core, struct VFile* vf) {
core->symbolTable = mDebuggerSymbolTableCreate();
#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
if (!vf) {
vf = mDirectorySetOpenSuffix(&core->dirs, core->dirs.base, ".sym", O_RDONLY);
}
#endif
if (!vf) {
return;
}
GBLoadSymbols(core->symbolTable, vf);
}
static bool _GBCoreLookupIdentifier(struct mCore* core, const char* name, int32_t* value, int* segment) {
UNUSED(core);
*segment = -1;
int i;
for (i = 0; i < GB_REG_MAX; ++i) {
const char* reg = GBIORegisterNames[i];
if (reg && strcasecmp(reg, name) == 0) {
*value = GB_BASE_IO | i;
return true;
}
}
return false;
}
#endif
static struct mCheatDevice* _GBCoreCheatDevice(struct mCore* core) {
struct GBCore* gbcore = (struct GBCore*) core;
if (!gbcore->cheatDevice) {
gbcore->cheatDevice = GBCheatDeviceCreate();
((struct SM83Core*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbcore->cheatDevice->d;
SM83HotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
gbcore->cheatDevice->p = core;
}
return gbcore->cheatDevice;
}
static size_t _GBCoreSavedataClone(struct mCore* core, void** sram) {
struct GB* gb = core->board;
struct VFile* vf = gb->sramVf;
if (vf) {
*sram = malloc(vf->size(vf));
vf->seek(vf, 0, SEEK_SET);
return vf->read(vf, *sram, vf->size(vf));
}
if (gb->sramSize) {
*sram = malloc(gb->sramSize);
memcpy(*sram, gb->memory.sram, gb->sramSize);
return gb->sramSize;
}
*sram = NULL;
return 0;
}
static bool _GBCoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
struct GB* gb = core->board;
if (!writeback) {
struct VFile* vf = VFileMemChunk(sram, size);
GBSavedataMask(gb, vf, true);
return true;
}
struct VFile* vf = gb->sramVf;
if (vf) {
vf->seek(vf, 0, SEEK_SET);
return vf->write(vf, sram, size) > 0;
}
if (size > 0x20000) {
size = 0x20000;
}
GBResizeSram(gb, size);
memcpy(gb->memory.sram, sram, size);
return true;
}
static size_t _GBCoreListVideoLayers(const struct mCore* core, const struct mCoreChannelInfo** info) {
UNUSED(core);
if (info) {
*info = _GBVideoLayers;
}
return sizeof(_GBVideoLayers) / sizeof(*_GBVideoLayers);
}
static size_t _GBCoreListAudioChannels(const struct mCore* core, const struct mCoreChannelInfo** info) {
UNUSED(core);
if (info) {
*info = _GBAudioChannels;
}
return sizeof(_GBAudioChannels) / sizeof(*_GBAudioChannels);
}
static void _GBCoreEnableVideoLayer(struct mCore* core, size_t id, bool enable) {
struct GB* gb = core->board;
switch (id) {
case GB_LAYER_BACKGROUND:
gb->video.renderer->disableBG = !enable;
break;
case GB_LAYER_WINDOW:
gb->video.renderer->disableWIN = !enable;
break;
case GB_LAYER_OBJ:
gb->video.renderer->disableOBJ = !enable;
break;
default:
break;
}
}
static void _GBCoreEnableAudioChannel(struct mCore* core, size_t id, bool enable) {
struct GB* gb = core->board;
switch (id) {