-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
498 lines (457 loc) · 14.5 KB
/
main.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
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/divider.h"
#include "hardware/clocks.h"
#include "hardware/vreg.h"
#include "hardware/watchdog.h"
#include "util/work_meter.h"
#include "ff.h"
#include "tusb.h"
#include "gamepad.h"
#include "menu.h"
#include "nespad.h"
#include "wiipad.h"
#include "FrensHelpers.h"
#include "settings.h"
#include "FrensFonts.h"
#include "shared.h"
#include "mytypes.h"
bool isFatalError = false;
static FATFS fs;
char *romName;
static bool fps_enabled = false;
static uint32_t start_tick_us = 0;
static uint32_t fps = 0;
static char fpsString[3] = "00";
#define fpsfgcolor 0; // black
#define fpsbgcolor 0xFFF; // white
#define MARGINTOP 24
#define MARGINBOTTOM 4
#define FPSSTART (((MARGINTOP + 7) / 8) * 8)
#define FPSEND ((FPSSTART) + 8)
bool reset = false;
// The Sega Master system color palette converted to RGB444
// so it can be used with the DVI library.
// from https://segaretro.org/Palette
WORD SMSPaletteRGB444[64] = {
0x0, 0x500, 0xA00, 0xF00, 0x50, 0x550, 0xA50, 0xF50,
0xA0, 0x5A0, 0xAA0, 0xFA0, 0xF0, 0x5F0, 0xAF0, 0xFF0,
0x5, 0x505, 0xA05, 0xF05, 0x55, 0x555, 0xA55, 0xF55,
0xA5, 0x5A5, 0xAA5, 0xFA5, 0xF5, 0x5F5, 0xAF5, 0xFF5,
0xA, 0x50A, 0xA0A, 0xF0A, 0x5A, 0x55A, 0xA5A, 0xF5A,
0xAA, 0x5AA, 0xAAA, 0xFAA, 0xFA, 0x5FA, 0xAFA, 0xFFA,
0xF, 0x50F, 0xA0F, 0xF0F, 0x5F, 0x55F, 0xA5F, 0xF5F,
0xAF, 0x5AF, 0xAAF, 0xFAF, 0xFF, 0x5FF, 0xAFF, 0xFFF};
namespace
{
constexpr uint32_t CPUFreqKHz = 252000;
}
int sampleIndex = 0;
void in_ram(processaudio)(int offset)
{
int samples = 4; // 735/192 = 3.828125 192*4=768 735/3=245
if (offset == (IS_GG ? 24 : 0))
{
sampleIndex = 0;
}
else
{
sampleIndex += samples;
if (sampleIndex >= 735)
{
return;
}
}
short *p1 = snd.buffer[0] + sampleIndex;
short *p2 = snd.buffer[1] + sampleIndex;
while (samples)
{
auto &ring = dvi_->getAudioRingBuffer();
auto n = std::min<int>(samples, ring.getWritableSize());
if (!n)
{
return;
}
auto p = ring.getWritePointer();
int ct = n;
while (ct--)
{
int l = *p1++; // (*p1++ << 16) + *p2++;
// works also : int l = (*p1++ + *p2++) / 2;
int r = *p2++;
// int l = *wave1++;
*p++ = {static_cast<short>(l), static_cast<short>(r)};
}
ring.advanceWritePointer(n);
samples -= n;
}
}
extern "C" void in_ram(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;
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;
}
extern "C" void in_ram(sms_palette_sync)(int index)
{
#if 1
// 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 0
// 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;
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
return;
}
extern "C" void in_ram(sms_render_line)(int line, const uint8_t *buffer)
{
// DVI top margin has #MARGINTOP lines
// DVI bottom margin has #MARGINBOTTOM lines
// DVI usable screen estate: MARGINTOP .. (240 - #MARGINBOTTOM)
// 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
// Audio needs to be processed per scanline
processaudio(line);
// Adjust line number to center the emulator display
line += MARGINTOP;
// Only render lines that are visible on the screen, keeping into account top and bottom margins
if (line < MARGINTOP || line >= 240 - MARGINBOTTOM)
return;
auto b = dvi_->getLineBuffer();
uint16_t *sbuffer;
if (buffer)
{
uint16_t *sbuffer = b->data() + 32 + (IS_GG ? 48 : 0);
for (int i = screenCropX; i < BMP_WIDTH - screenCropX; i++)
{
sbuffer[i - screenCropX] = palette444[(buffer[i + BMP_X_OFFSET]) & 31];
}
}
else
{
sbuffer = b->data() + 32;
__builtin_memset(sbuffer, 0, 512);
}
// Display frame rate
if (fps_enabled && line >= FPSSTART && line < FPSEND)
{
WORD *fpsBuffer = b->data() + 40;
int rowInChar = line % 8;
for (auto i = 0; i < 2; i++)
{
char firstFpsDigit = fpsString[i];
char fontSlice = getcharslicefrom8x8font(firstFpsDigit, rowInChar);
for (auto bit = 0; bit < 8; bit++)
{
if (fontSlice & 1)
{
*fpsBuffer++ = fpsfgcolor;
}
else
{
*fpsBuffer++ = fpsbgcolor;
}
fontSlice >>= 1;
}
}
}
dvi_->setLineBuffer(line, b);
}
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 ProcessAfterFrameIsRendered()
{
#if NES_PIN_CLK != -1
nespad_read_start();
#endif
auto count = dvi_->getFrameCounter();
auto onOff = hw_divider_s32_quotient_inlined(count, 60) & 1;
Frens::blinkLed(onOff);
#if NES_PIN_CLK != -1
nespad_read_finish(); // Sets global nespad_state var
#endif
// nespad_read_finish(); // Sets global nespad_state var
tuh_task();
// Frame rate calculation
if (fps_enabled)
{
// calculate fps and round to nearest value (instead of truncating/floor)
uint32_t tick_us = Frens::time_us() - start_tick_us;
fps = (1000000 - 1) / tick_us + 1;
start_tick_us = Frens::time_us();
fpsString[0] = '0' + (fps / 10);
fpsString[1] = '0' + (fps % 10);
}
return count;
}
static DWORD prevButtons[2]{};
static DWORD prevButtonssystem[2]{};
static DWORD prevOtherButtons[2]{};
#define OTHER_BUTTON1 (0b1)
#define OTHER_BUTTON2 (0b10)
#define NESPAD_SELECT (0x04)
#define NESPAD_START (0x08)
#define NESPAD_UP (0x10)
#define NESPAD_DOWN (0x20)
#define NESPAD_LEFT (0x40)
#define NESPAD_RIGHT (0x80)
#define NESPAD_A (0x01)
#define NESPAD_B (0x02)
static int rapidFireMask[2]{};
static int rapidFireCounter = 0;
void processinput(DWORD *pdwPad1, DWORD *pdwPad2, DWORD *pdwSystem, bool ignorepushed, char *gamepadType)
{
// 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;
bool usbConnected = false;
for (int i = 0; i < 2; i++)
{
int nespadbuttons = 0;
auto &dst = (i == 0) ? *pdwPad1 : *pdwPad2;
auto &gp = io::getCurrentGamePadState(i);
if ( i == 0 )
{
usbConnected = gp.isConnected();
if (gamepadType) {
strcpy(gamepadType, gp.GamePadName);
}
}
int smsbuttons = (gp.buttons & io::GamePadState::Button::LEFT ? INPUT_LEFT : 0) |
(gp.buttons & io::GamePadState::Button::RIGHT ? INPUT_RIGHT : 0) |
(gp.buttons & io::GamePadState::Button::UP ? INPUT_UP : 0) |
(gp.buttons & io::GamePadState::Button::DOWN ? INPUT_DOWN : 0) |
(gp.buttons & io::GamePadState::Button::A ? INPUT_BUTTON1 : 0) |
(gp.buttons & io::GamePadState::Button::B ? INPUT_BUTTON2 : 0) | 0;
int otherButtons = (gp.buttons & io::GamePadState::Button::X ? OTHER_BUTTON1 : 0) |
(gp.buttons & io::GamePadState::Button::Y ? OTHER_BUTTON2 : 0) | 0;
smssystem[i] =
(gp.buttons & io::GamePadState::Button::SELECT ? INPUT_PAUSE : 0) |
(gp.buttons & io::GamePadState::Button::START ? INPUT_START : 0) |
0;
#if NES_PIN_CLK != -1
// When USB controller is connected both NES ports act as controller 2
if (usbConnected)
{
if (i == 1)
{
nespadbuttons= nespadbuttons | nespad_states[1] | nespad_states[0];
}
}
else
{
nespadbuttons |= nespad_states[i];
}
#endif
// When USB controller is connected wiipad acts as controller 2
#if WII_PIN_SDA >= 0 and WII_PIN_SCL >= 0
if (usbConnected)
{
if (i == 1)
{
nespadbuttons |= wiipad_read();
}
}
else // if no USB controller is connected, wiipad acts as controller 1
{
if (i == 0)
{
nespadbuttons |= wiipad_read();
}
}
#endif
if (nespadbuttons > 0)
{
smsbuttons |= ((nespadbuttons & NESPAD_UP ? INPUT_UP : 0) |
(nespadbuttons & NESPAD_DOWN ? INPUT_DOWN : 0) |
(nespadbuttons & NESPAD_LEFT ? INPUT_LEFT : 0) |
(nespadbuttons & NESPAD_RIGHT ? INPUT_RIGHT : 0) |
(nespadbuttons & NESPAD_A ? INPUT_BUTTON1 : 0) |
(nespadbuttons & NESPAD_B ? INPUT_BUTTON2 : 0) | 0);
smssystem[i] |= ((nespadbuttons & NESPAD_SELECT ? INPUT_PAUSE : 0) |
(nespadbuttons & NESPAD_START ? 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)
{
reset = true;
printf("Reset pressed\n");
}
}
if (p1 & INPUT_START)
{
// Toggle frame rate display
if (pushed & INPUT_BUTTON1)
{
fps_enabled = !fps_enabled;
printf("FPS: %s\n", fps_enabled ? "ON" : "OFF");
}
if (pushed & INPUT_UP)
{
Frens::screenMode(-1);
}
else if (pushed & INPUT_DOWN)
{
Frens::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)
{
printf("Other 1\n");
}
if (pushedother & OTHER_BUTTON2)
{
printf("Other 2\n");
}
}
}
input.system = *pdwSystem = smssystem[0] | smssystem[1];
// return only on first push
if (pushedsystem)
{
*pdwSystem = smssystem[0] | smssystem[1];
}
}
void in_ram(process)(void)
{
DWORD pdwPad1, pdwPad2, pdwSystem; // have only meaning in menu
while (reset == false)
{
processinput(&pdwPad1, &pdwPad2, &pdwSystem, false, nullptr);
sms_frame(0);
ProcessAfterFrameIsRendered();
}
}
/// @brief
/// Start emulator. Emulator does not run well in DEBUG mode, lots of red screen flicker. In order to keep it running fast enough, we need to run it in release mode or in
/// RelWithDebugInfo mode.
/// @return
int main()
{
char selectedRom[FF_MAX_LFN];
romName = selectedRom;
ErrorMessage[0] = selectedRom[0] = 0;
int fileSize = 0;
bool isGameGear = false;
// Set voltage and clock frequency
vreg_set_voltage(VREG_VOLTAGE_1_20);
sleep_ms(10);
set_sys_clock_khz(CPUFreqKHz, true);
stdio_init_all();
sleep_ms(500);
printf("Starting Master System Emulator\n");
printf("CPU freq: %d\n", clock_get_hz(clk_sys));
printf("Starting Tinyusb subsystem\n");
tusb_init();
isFatalError = !Frens::initAll(selectedRom, CPUFreqKHz, MARGINTOP, MARGINBOTTOM );
bool showSplash = true;
while (true)
{
if (strlen(selectedRom) == 0 || reset == true)
{
menu("Pico-SMS+", ErrorMessage, isFatalError, showSplash, ".sms .gg"); // never returns, but reboots upon selecting a game
}
reset = false;
FRESULT fr;
FIL file;
fr = f_open(&file, selectedRom, FA_READ);
if (fr != FR_OK)
{
snprintf(ErrorMessage, 40, "Cannot open rom %d", fr);
printf("%s\n", ErrorMessage);
selectedRom[0] = 0;
continue;
}
fileSize = f_size(&file);
f_close(&file);
isGameGear = Frens::cstr_endswith(selectedRom, ".gg");
printf("Now playing: %s (%d bytes)\n", selectedRom, fileSize);
if (isGameGear)
{
printf("Game Gear rom detected\n");
} else {
printf("Master System rom detected\n");
}
load_rom(ROM_FILE_ADDR, fileSize, isGameGear);
// Initialize all systems and power on
system_init(SMS_AUD_RATE);
// load state if any
// system_load_state();
system_reset();
printf("Starting game\n");
process();
system_shutdown();
selectedRom[0] = 0;
showSplash = false;
}
return 0;
}