-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmsPlus64.cpp
857 lines (792 loc) · 23.9 KB
/
smsPlus64.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
#include "shared.h"
#include "common.h"
#include "libdragon.h"
#include "menu.h"
#include "FrensHelpers.h"
#ifndef USEMENU
#include "builtinrom.h"
#endif
#include <stdarg.h>
#include "libcart/cart.h"
#define ERRORMESSAGESIZE 40
#define GAMESAVEDIR "/SAVES"
#define FRAMEBUFFERS 3
#ifndef USEMENU
#define USEMENU 1
#endif
/* hardware definitions */
// Pad buttons
#define A_BUTTON(a) ((a) & 0x8000)
#define B_BUTTON(a) ((a) & 0x4000)
#define Z_BUTTON(a) ((a) & 0x2000)
#define START_BUTTON(a) ((a) & 0x1000)
// D-Pad
#define DU_BUTTON(a) ((a) & 0x0800)
#define DD_BUTTON(a) ((a) & 0x0400)
#define DL_BUTTON(a) ((a) & 0x0200)
#define DR_BUTTON(a) ((a) & 0x0100)
// Triggers
#define TL_BUTTON(a) ((a) & 0x0020)
#define TR_BUTTON(a) ((a) & 0x0010)
// Yellow C buttons
#define CU_BUTTON(a) ((a) & 0x0008)
#define CD_BUTTON(a) ((a) & 0x0004)
#define CL_BUTTON(a) ((a) & 0x0002)
#define CR_BUTTON(a) ((a) & 0x0001)
#define PAD_DEADZONE 5
#define PAD_ACCELERATION 10
#define PAD_CHECK_TIME 40
surface_t *_dc;
#define SOUNDISENABLED 1
int soundEnabled = SOUNDISENABLED;
char *ErrorMessage;
bool isFatalError = false;
char romName[256];
static bool fps_enabled = false;
timer_link_t *fpstimer = nullptr;
static bool hideFrameRate = false;
bool reset = false;
bool controller1IsInserted = false;
bool controller2IsInserted = false;
bool loadedFromFlashcartMenu = false;
// Sega header https://www.smspower.org/Development/ROMHeader
struct SegaHeader
{
// 0x7FF0
char signature[8];
// 0x7FF8
uint16_t reserverd;
// 0x7FFA
uint16_t checksum;
// 0x7FFC
uint8_t product_code[2];
// 0x7FFE
uint8_t ProductCodeAndVersion;
// 0x7FFF
uint8_t sizeAndRegion;
} header;
extern "C" void sms_palette_syncGG(int index)
{
// The GG has a different palette format
int r = ((vdp.cram[(index << 1) | 0] >> 1) & 7) << 5;
int g = ((vdp.cram[(index << 1) | 0] >> 5) & 7) << 5;
int b = ((vdp.cram[(index << 1) | 1] >> 1) & 7) << 5;
#if 0
int r444 = ((r << 4) + 127) >> 8; // equivalent to (r888 * 15 + 127) / 255
int g444 = ((g << 4) + 127) >> 8; // equivalent to (g888 * 15 + 127) / 255
int b444 = ((b << 4) + 127) >> 8;
palette444[index] = (r444 << 8) | (g444 << 4) | b444;
#endif
palette444[index] = RGB888_TO_RGB5551(r, g, b);
return;
}
extern "C" void sms_palette_sync(int index)
{
#if 0
// Get SMS palette color index from CRAM
WORD r = ((vdp.cram[index] >> 0) & 3);
WORD g = ((vdp.cram[index] >> 2) & 3);
WORD b = ((vdp.cram[index] >> 4) & 3);
WORD tableIndex = b << 4 | g << 2 | r;
// Get the RGB444 color from the SMS RGB444 palette
palette444[index] = SMSPaletteRGB444[tableIndex];
#endif
#if 1
// Alternative color rendering below
WORD r = ((vdp.cram[index] >> 0) & 3) << 6;
WORD g = ((vdp.cram[index] >> 2) & 3) << 6;
WORD b = ((vdp.cram[index] >> 4) & 3) << 6;
#if 0
int r444 = ((r << 4) + 127) >> 8; // equivalent to (r888 * 15 + 127) / 255
int g444 = ((g << 4) + 127) >> 8; // equivalent to (g888 * 15 + 127) / 255
int b444 = ((b << 4) + 127) >> 8;
palette444[index] = (r444 << 8) | (g444 << 4) | b444;
#endif
palette444[index] = RGB888_TO_RGB5551(r, g, b);
#endif
return;
}
extern "C" void sms_render_line(int line, const uint8_t *buffer)
{
// SMS has 192 lines
// GG has 144 lines
// gg : Line starts at line 24
// sms: Line starts at line 0
// Emulator loops from scanline 0 to 261
if (IS_GG)
{
if (line < 24 || line >= 168)
{
return;
}
}
else
{
if (line >= 192)
{
return;
}
}
// center more or less screen
line += 24;
// debugf("\tLine %d, ISGG: %d\n", line, IS_GG);
if (buffer)
{
WORD *framebufferline = ((WORD *)(_dc)->buffer) + (line << 8) + (IS_GG ? 48 : 0);
for (int i = screenCropX; i < BMP_WIDTH - screenCropX; i++)
{
framebufferline[i - screenCropX] = palette444[(buffer[i + BMP_X_OFFSET]) & 31];
}
}
}
void system_load_sram(void)
{
printf("system_load_sram: TODO\n");
// TODO
}
void system_save_sram()
{
printf("system_save_sram: saving sram TODO\n");
// TODO
}
void system_load_state()
{
// TODO
}
void system_save_state()
{
// TODO
}
int framecounter = 0;
int framedisplay = 0;
int totalfames = 0;
int ProcessAfterFrameIsRendered(surface_t *display, bool fromMenu)
{
char buffer[15];
if (fps_enabled)
{
char sound = soundEnabled ? 'S' : 'M';
sprintf(buffer, "%c %04d", sound, framedisplay);
// debugf("Frame %d\n", totalfames);
graphics_set_color(CBLACK, CWHITE);
if (IS_GG && fromMenu == false)
{
graphics_draw_text(display, 48, 24, buffer);
}
else
{
graphics_draw_text(display, 10, 5, buffer);
}
// Frame rate calculation
}
framecounter++;
return totalfames++;
}
void frameratecalc(int ovfl)
{
// debugf("FPS: %d\n", framecounter);
framedisplay = framecounter;
framecounter = 0;
}
void enableordisableTimer()
{
if (fps_enabled)
{
fpstimer = new_timer(TIMER_TICKS(1000000), TF_CONTINUOUS, frameratecalc);
framecounter = framedisplay = 0;
}
else
{
if (fpstimer != nullptr)
{
delete_timer(fpstimer);
fpstimer = nullptr;
}
}
}
#define OTHER_BUTTON1 (0b1)
#define OTHER_BUTTON2 (0b10)
static DWORD prevButtons[2]{};
static DWORD prevButtonssystem[2]{};
static DWORD prevOtherButtons[2]{};
struct controller_data gKeys;
static int rapidFireMask[2]{};
static int rapidFireCounter = 0;
void processinput(DWORD *pdwPad1, DWORD *pdwPad2, DWORD *pdwSystem, bool ignorepushed)
{
controller_scan();
gKeys = get_keys_pressed();
// pwdPad1 and pwdPad2 are only used in menu and are only set on first push
*pdwPad1 = *pdwPad2 = *pdwSystem = 0;
int smssystem[2]{};
unsigned long pushed, pushedsystem, pushedother;
for (int i = 0; i < 1; i++)
{
if ((i == 0 && controller1IsInserted == false) ||
(i == 1 && controller2IsInserted == false))
{
continue;
}
auto &dst = (i == 0) ? *pdwPad1 : *pdwPad2;
auto gp = gKeys.c[i].data >> 16;
int smsbuttons = (DL_BUTTON(gp) ? INPUT_LEFT : 0) |
(DR_BUTTON(gp) ? INPUT_RIGHT : 0) |
(DU_BUTTON(gp) ? INPUT_UP : 0) |
(DD_BUTTON(gp) ? INPUT_DOWN : 0) |
(A_BUTTON(gp) ? INPUT_BUTTON1 : 0) |
(B_BUTTON(gp) ? INPUT_BUTTON2 : 0) | 0;
int otherButtons = (CL_BUTTON(gp) ? OTHER_BUTTON1 : 0) |
(CU_BUTTON(gp) ? OTHER_BUTTON2 : 0) | 0;
smssystem[i] =
(Z_BUTTON(gp) ? INPUT_PAUSE : 0) |
(START_BUTTON(gp) ? INPUT_START : 0) |
0;
// if (gp.buttons & io::GamePadState::Button::SELECT) printf("SELECT\n");
// if (gp.buttons & io::GamePadState::Button::START) printf("START\n");
input.pad[i] = smsbuttons;
auto p1 = smssystem[i];
if (ignorepushed == false)
{
pushed = smsbuttons & ~prevButtons[i];
pushedsystem = smssystem[i] & ~prevButtonssystem[i];
pushedother = otherButtons & ~prevOtherButtons[i];
}
else
{
pushed = smsbuttons;
pushedsystem = smssystem[i];
pushedother = otherButtons;
}
if (p1 & INPUT_PAUSE)
{
if (pushedsystem & INPUT_START)
{
if (!loadedFromFlashcartMenu)
{
reset = true;
debugf("Reset pressed\n");
}
else
{
debugf("Reset ignored\n");
}
}
// Toggle frame rate display
if (pushed & INPUT_BUTTON1)
{
fps_enabled = !fps_enabled;
enableordisableTimer();
debugf("FPS: %s\n", fps_enabled ? "ON" : "OFF");
if (fps_enabled == false)
{
hideFrameRate = true;
debugf("Hiding frame rate\n");
}
}
if (pushed & INPUT_BUTTON2)
{
snd.enabled = soundEnabled = !soundEnabled;
debugf("Toggle sound (%d)\n", soundEnabled);
}
}
if (p1 & INPUT_START)
{
if (pushed & INPUT_UP)
{
// screenMode(-1);
}
else if (pushed & INPUT_DOWN)
{
// screenMode(+1);
}
}
prevButtons[i] = smsbuttons;
prevButtonssystem[i] = smssystem[i];
prevOtherButtons[i] = otherButtons;
// return only on first push
if (pushed)
{
dst = smsbuttons;
}
if (pushedother)
{
if (pushedother & OTHER_BUTTON1)
{
debugf("Other 1\n");
}
if (pushedother & OTHER_BUTTON2)
{
debugf("Other 2\n");
}
}
}
input.system = *pdwSystem = smssystem[0] | smssystem[1];
// return only on first push
if (pushedsystem)
{
*pdwSystem = smssystem[0] | smssystem[1];
}
}
void process(void)
{
DWORD pdwPad1, pdwPad2, pdwSystem; // have only meaning in menu
while (reset == false)
{
// debugf("Frame %d\n", framecounter);
processinput(&pdwPad1, &pdwPad2, &pdwSystem, false);
_dc = display_get();
if (hideFrameRate)
{
hideFrameRate = false;
// Clear all the framebuffers
for (int i = 0; i < FRAMEBUFFERS; i++)
{
debugf("Clear framebuffer %d\n", i + 1);
graphics_fill_screen(_dc, 1);
display_show(_dc);
_dc = display_get();
}
}
sms_frame(0);
ProcessAfterFrameIsRendered(_dc, false);
display_show(_dc);
//
#if 0
short *p = audio_write_begin();
//debugf("Audio buffer length: %d\n", snd.bufsize );
int i = 0;
for (int x = 0; x < snd.bufsize; x++)
{
// audio_buffer[x] = (snd.buffer[0][x] << 16) + snd.buffer[1][x];
*p++ = (snd.buffer[0][x] << 16) + snd.buffer[1][x];
}
audio_write_end();
#endif
}
}
void checkcontrollers()
{
controller1IsInserted = controller2IsInserted = false;
int controllers = get_controllers_present();
if (controllers & CONTROLLER_1_INSERTED)
{
debugf("Controller 1 inserted\n");
controller1IsInserted = true;
}
if (controllers & CONTROLLER_2_INSERTED)
{
debugf("Controller 2 inserted\n");
controller2IsInserted = true;
}
}
size_t find_sequence(uint8_t *buffer, size_t buffer_size, const char *sequence)
{
size_t seq_len = strlen(sequence);
debugf("Finding Sequence: %s\n", sequence);
// Iterate through each position in the buffer
for (size_t i = 0; i <= buffer_size - seq_len; i++)
{
// Compare the current part of the buffer with the sequence
if (memcmp(buffer + i, sequence, seq_len) == 0)
{
// print found sequence in hex at offset
debugf("Found sequence at offset %x: ", i);
for (size_t j = 0; j < seq_len; j++)
{
debugf("%c", buffer[i + j]);
}
debugf("\n");
return i; // Return the starting index of the match
}
}
return -1; // Return -1 if not found
}
// debug_init_sdfs is only available when NDEBUG is not defined
// We need sdfs to access the everdrive SD filesystem. So make it also available when NDEBUG is defined.
// #ifdef NDEBUG
#undef debug_init_sdfs
#ifdef __cplusplus
extern "C"
{
#endif
bool debug_init_sdfs(const char *prefix, int npart);
bool init_sdfs(const char *prefix, int npart)
{
return debug_init_sdfs(prefix, npart);
}
#ifdef __cplusplus
}
#endif
uint32_t GetRomAddress()
{
switch (cart_type)
{
case CART_CI:
case CART_SC:
return 0x10200000;
case CART_EDX:
case CART_ED:
return 0xB0200000;
default:
return 0; // This is an emulator or something else, load the built in ROM.
}
}
// create a wrapper for debugf to stdout
void debugstdout(const char *fmt, ...)
{
#ifndef NDEBUG
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
// vfprintf(stderr, fmt, args);
va_end(args);
#endif
}
// Checks if a rom is injected by the everdrive/N64Flashcart menu
// The rom is injected at GetRomAddress() and has a Sega header
// The Sega header is at 0x7FF0
// Some roms have a 512 byte header, so we also check at 0x7FF0 + 512
bool IsRomInjected(RomInfo *info, bool withOffset)
{
bool rval = false;
int offset = withOffset ? 512 : 0;
debugstdout("Searching for Sega header at %x\n", GetRomAddress() + 0x7FF0 + offset);
dma_read_async(&header, GetRomAddress() + 0x7FF0 + offset, sizeof(header));
dma_wait();
if (strncmp(header.signature, "TMR SEGA", 8) == 0)
{
debugstdout(" --->Sega header found\n");
info->isGameGear = false;
uint8_t romsize = header.sizeAndRegion & 0b00001111;
uint8_t region = (header.sizeAndRegion >> 4) & 0b00001111;
// https://www.smspower.org/Development/ROMHeader
switch (romsize)
{
case 0: // 256KB
info->size = 512 * 1024; // 512KB and 1MB Roms are reported in the header as 256KB.
// Setting Rom size to 512KB also works for 256KB roms.
break; // Setting rom size to 1MB for 256 or 512KB games does not work.
// Only a small set of roms are 1MB.
case 1:
info->size = 512 * 1024;
break;
case 2:
info->size = 1024 * 1024;
break;
case 0xa:
info->size = 8 * 1024;
break;
case 0xb:
info->size = 16 * 1024;
break;
case 0xc:
info->size = 32 * 1024;
break;
case 0xd:
info->size = 48 * 1024;
break;
case 0xe:
info->size = 64 * 1024;
break;
case 0xf:
info->size = 128 * 1024;
break;
default:
debugstdout("Unknown romsize %x\n", romsize);
info->size = 0; // unknown size
break;
}
info->isGameGear = false;
debugstdout("Romsize %x = %d bytes\n", romsize, info->size);
debugstdout("Region: %x - ", region);
switch (region)
{
case 3:
debugstdout("SMS Japan\n");
info->isGameGear = false;
break;
case 4:
debugstdout("SMS Export\n");
info->isGameGear = false;
break;
case 5:
debugstdout("GG USA\n");
info->isGameGear = true;
break;
case 6:
debugstdout("GG Export\n");
info->isGameGear = true;
break;
case 7:
debugstdout("GG International\n");
info->isGameGear = true;
break;
default:
debugstdout("Unknown\n");
break;
}
if (info->size > 0)
{
rval = true;
}
}
return rval;
}
static const char *format_cart_type()
{
switch (cart_type)
{
case CART_CI:
return "64drive";
case CART_EDX:
return "Series X EverDrive-64";
case CART_ED:
return "Series V EverDrive-64";
case CART_SC:
return "SummerCart64";
default: // Probably emulator
return "Emulator?";
}
}
int main()
{
char errMSG[ERRORMESSAGESIZE];
errMSG[0] = romName[0] = 0;
int fileSize = 0;
bool isGameGear = false;
char mountPoint[20];
size_t tmpSize;
bool dfsStarted = false;
ErrorMessage = errMSG;
RomInfo info;
debug_init(DEBUG_FEATURE_LOG_ISVIEWER | DEBUG_FEATURE_LOG_USB);
debugf("Starting SMSPlus64, a Sega Master System emulator for the Nintendo 64 - https://github.com/fhoedemakers/smsplus64\n");
debugf("Built on %s %s using libdragon - https://github.com/DragonMinded/libdragon\n", __DATE__, __TIME__);
cart_init();
controller_init();
timer_init();
enableordisableTimer();
struct controller_data output;
get_accessories_present(&output);
int accessory = identify_accessory(0);
switch (accessory)
{
case ACCESSORY_MEMPAK:
debugf("Accessory: Memory Pak\n");
break;
case ACCESSORY_RUMBLEPAK:
debugf("Accessory: Rumble Pak\n");
break;
case ACCESSORY_TRANSFERPAK:
debugf("Accessory: Transfer Pak\n");
break;
case ACCESSORY_VRU:
debugf("Accessory: VRU\n");
break;
default:
debugf("Accessory: None\n");
break;
}
while (true)
{
int offset = 0;
checkcontrollers();
// #ifndef NDEBUG
// console must be initialized for the user to press Z to skip to menu
console_init();
console_set_render_mode(RENDER_MANUAL);
console_clear();
// #endif
#ifndef NDEBUG
#if 0
// allocate MB of memory for the rom
int size = 2 * 1024 * 1024;
uint8_t *rom = (uint8_t *)malloc(size);
if (rom == nullptr)
{
debugstdout("Error allocating memory for rom\n");
strcpy(ErrorMessage, "Error allocating memory for rom");
console_render();
break;
}
// read the rom from the filesystem
debugstdout("Searching for rom at range GetRomAddress() - %x\n", GetRomAddress() + size);
dma_read_async(rom, GetRomAddress(), size);
dma_wait();
debugstdout("Rom read\n");
// check if the rom is a game gear rom);
int offs = 0;
if ((offs =find_sequence(rom, size, "TMR SEGA")) != -1)
{
debugstdout("Rom found at offset %x, (%x) \n", offs, GetRomAddress() + offs);
}
else
{
debugstdout("Rom not found\n");
}
free(rom);
#endif
#endif
// Wait for Z button to be pressed until counter is reached
bool zPressed = false;
debugstdout("Press Z button to skip to menu\n");
int counter = 0;
while (counter < 10)
{
zPressed = get_keys_pressed().c[0].Z;
if (zPressed)
{
break;
}
wait_ms(10);
controller_scan();
counter++;
}
// Check whether rom is started via Everdrive/N64Flashcartmenu
// Those roms are injected at GetRomAddress() and have a Sega header
if (!zPressed)
{
debugstdout("Detecting flash cart type\n");
if (cart_type != CART_NULL)
{
debugstdout("Cart type: %s\n", format_cart_type());
debugstdout("Cart size: %d\n", cart_size);
debugstdout("Cart ROM address: %x\n", GetRomAddress());
debugstdout("Check if game is started via Everdrive/FlashCartMenu\n");
} else {
debugstdout("No flash cart detected\n");
}
}
else
{
debugstdout("Z button pressed, skipping to menu\n");
}
loadedFromFlashcartMenu = false;
if (!zPressed && cart_type != CART_NULL && (loadedFromFlashcartMenu = IsRomInjected(&info, false)) == false)
{
if ((loadedFromFlashcartMenu = IsRomInjected(&info, true)) == true)
{
offset = 512;
}
else
{
debugstdout("No Sega header found\n");
}
}
if (loadedFromFlashcartMenu)
{
debugstdout("Allocating memory for rom\n");
info.rom = (uint8_t *)malloc(info.size);
debugstdout("Reading rom at %x\n", GetRomAddress() + offset);
dma_read_async(info.rom, GetRomAddress() + offset, info.size);
debugstdout("Waiting for dma\n");
dma_wait();
strcpy(info.title, "Everdrive/Flashcart");
#ifndef NDEBUG
debugstdout("Press A button to continue\n");
controller_scan();
console_render();
while (!get_keys_pressed().c[0].A)
{
wait_ms(10);
controller_scan();
}
#endif
console_close();
}
else
{
debugstdout("Will start menu\n");
if (dfsStarted == false)
{
debugstdout("Mounting rom file system...");
if (dfs_init(DFS_DEFAULT_LOCATION) == DFS_ESUCCESS)
{
dfsStarted = true;
debugstdout("mounted.\nTrying to mount SD card...");
if (!init_sdfs("sd:/", -1))
{
debugstdout("Error opening SD, using rom:/ filesystem...\n");
strcpy(mountPoint, "rom:/");
}
else
{
debugstdout("SD card mounted\n");
strcpy(mountPoint, "sd:/smsPlus64");
}
}
else
{
debugstdout("rom filesystem failed to start!\n");
debugstdout("Exit program\n");
isFatalError = true;
strcpy(ErrorMessage, "Error opening rom filesystem.");
}
}
#ifndef NDEBUG
debugstdout("Press A button to continue\n");
controller_scan();
console_render();
while (!get_keys_pressed().c[0].A)
{
wait_ms(10);
controller_scan();
}
console_close();
#endif
#if USEMENU == 1
header.signature[0] = 0;
header.signature[1] = 0;
header.signature[2] = 0;
header.signature[3] = 0;
header.signature[4] = 0;
header.signature[5] = 0;
header.signature[6] = 0;
header.signature[7] = 0;
debugf("Killing header\n");
dma_write_raw_async(&header, GetRomAddress() + 0x7FF0, sizeof(header));
dma_wait();
dma_write_raw_async(&header, GetRomAddress() + 0x7FF0 + 512, sizeof(header));
dma_wait();
debugf("Killed\n");
display_init(RESOLUTION_320x240, DEPTH_16_BPP, 3, GAMMA_NONE, FILTERS_RESAMPLE);
info = menu(mountPoint, 0, ErrorMessage, isFatalError, reset);
display_close();
#else
info.rom = builtinrom;
info.size = builtinrom_len;
info.isGameGear = builtinrom_isgg;
strcpy(info.title, GetBuiltinROMName());
#endif
}
/* Initialize display */
display_init(RESOLUTION_256x240, DEPTH_16_BPP, FRAMEBUFFERS, GAMMA_NONE, FILTERS_RESAMPLE);
checkcontrollers();
// dump info
debugf("Starting game:\n");
debugf("- ROM: %s\n", info.title);
debugf("- Size: %d\n", info.size);
debugf("- Address: %p\n", info.rom);
debugf("- isGameGear: %d\n", info.isGameGear);
reset = false;
debugf("Init audio\n");
audio_init(44100, 4);
load_rom(info.rom, info.size, info.isGameGear);
// Initialize all systems and power on
system_init(SMS_AUD_RATE);
// load state if any
// system_load_state();
system_reset();
debugf("Starting game\n");
process();
romName[0] = 0;
display_close();
debugf("Closing audio\n");
audio_close();
#ifdef USEMENU
debugf("Freeing rom\n");
free(info.rom);
#endif
}
return 0;
}