-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.cpp
576 lines (483 loc) · 14.1 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
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
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/divider.h"
#include "hardware/dma.h"
#include "hardware/pio.h"
#include "hardware/i2c.h"
#include "hardware/interp.h"
#include "hardware/timer.h"
#include "hardware/clocks.h"
#include "hardware/vreg.h"
#include <hardware/sync.h>
#include <pico/multicore.h>
#include <hardware/flash.h>
#include <memory>
#include <math.h>
#include <util/dump_bin.h>
#include <util/exclusive_proc.h>
#include <util/work_meter.h>
#include <string.h>
#include <stdarg.h>
#include <algorithm>
#include <InfoNES.h>
#include <InfoNES_System.h>
#include <InfoNES_pAPU.h>
#include <dvi/dvi.h>
#include <tusb.h>
#include <gamepad.h>
#include "rom_selector.h"
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
#ifndef DVICONFIG
//#define DVICONFIG dviConfig_PicoDVI
#define DVICONFIG dviConfig_PicoDVISock
#endif
namespace
{
constexpr uint32_t CPUFreqKHz = 252000;
constexpr dvi::Config dviConfig_PicoDVI = {
.pinTMDS = {10, 12, 14},
.pinClock = 8,
.invert = true,
};
constexpr dvi::Config dviConfig_PicoDVISock = {
.pinTMDS = {12, 18, 16},
.pinClock = 14,
.invert = false,
};
std::unique_ptr<dvi::DVI> dvi_;
static constexpr uintptr_t NES_FILE_ADDR = 0x10080000;
ROMSelector romSelector_;
util::ExclusiveProc exclProc_;
enum class ScreenMode
{
SCANLINE_8_7,
NOSCANLINE_8_7,
SCANLINE_1_1,
NOSCANLINE_1_1,
MAX,
};
ScreenMode screenMode_{};
bool scaleMode8_7_ = true;
void applyScreenMode()
{
bool scanLine = false;
switch (screenMode_)
{
case ScreenMode::SCANLINE_1_1:
scaleMode8_7_ = false;
scanLine = true;
break;
case ScreenMode::SCANLINE_8_7:
scaleMode8_7_ = true;
scanLine = true;
break;
case ScreenMode::NOSCANLINE_1_1:
scaleMode8_7_ = false;
scanLine = false;
break;
case ScreenMode::NOSCANLINE_8_7:
scaleMode8_7_ = true;
scanLine = false;
break;
}
dvi_->setScanLine(scanLine);
}
}
#define CC(x) (((x >> 1) & 15) | (((x >> 6) & 15) << 4) | (((x >> 11) & 15) << 8))
const WORD __not_in_flash_func(NesPalette)[64] = {
CC(0x39ce), CC(0x1071), CC(0x0015), CC(0x2013), CC(0x440e), CC(0x5402), CC(0x5000), CC(0x3c20),
CC(0x20a0), CC(0x0100), CC(0x0140), CC(0x00e2), CC(0x0ceb), CC(0x0000), CC(0x0000), CC(0x0000),
CC(0x5ef7), CC(0x01dd), CC(0x10fd), CC(0x401e), CC(0x5c17), CC(0x700b), CC(0x6ca0), CC(0x6521),
CC(0x45c0), CC(0x0240), CC(0x02a0), CC(0x0247), CC(0x0211), CC(0x0000), CC(0x0000), CC(0x0000),
CC(0x7fff), CC(0x1eff), CC(0x2e5f), CC(0x223f), CC(0x79ff), CC(0x7dd6), CC(0x7dcc), CC(0x7e67),
CC(0x7ae7), CC(0x4342), CC(0x2769), CC(0x2ff3), CC(0x03bb), CC(0x0000), CC(0x0000), CC(0x0000),
CC(0x7fff), CC(0x579f), CC(0x635f), CC(0x6b3f), CC(0x7f1f), CC(0x7f1b), CC(0x7ef6), CC(0x7f75),
CC(0x7f94), CC(0x73f4), CC(0x57d7), CC(0x5bf9), CC(0x4ffe), CC(0x0000), CC(0x0000), CC(0x0000)};
uint32_t getCurrentNVRAMAddr()
{
if (!romSelector_.getCurrentROM())
{
return {};
}
int slot = romSelector_.getCurrentNVRAMSlot();
if (slot < 0)
{
return {};
}
printf("SRAM slot %d\n", slot);
return NES_FILE_ADDR - SRAM_SIZE * (slot + 1);
}
void saveNVRAM()
{
if (!SRAMwritten)
{
printf("SRAM not updated.\n");
return;
}
printf("save SRAM\n");
exclProc_.setProcAndWait([]
{
static_assert((SRAM_SIZE & (FLASH_SECTOR_SIZE - 1)) == 0);
if (auto addr = getCurrentNVRAMAddr())
{
auto ofs = addr - XIP_BASE;
printf("write flash %x\n", ofs);
{
flash_range_erase(ofs, SRAM_SIZE);
flash_range_program(ofs, SRAM, SRAM_SIZE);
}
} });
printf("done\n");
SRAMwritten = false;
}
void loadNVRAM()
{
if (auto addr = getCurrentNVRAMAddr())
{
printf("load SRAM %x\n", addr);
memcpy(SRAM, reinterpret_cast<void *>(addr), SRAM_SIZE);
}
SRAMwritten = false;
}
void InfoNES_PadState(DWORD *pdwPad1, DWORD *pdwPad2, DWORD *pdwSystem)
{
static constexpr int LEFT = 1 << 6;
static constexpr int RIGHT = 1 << 7;
static constexpr int UP = 1 << 4;
static constexpr int DOWN = 1 << 5;
static constexpr int SELECT = 1 << 2;
static constexpr int START = 1 << 3;
static constexpr int A = 1 << 0;
static constexpr int B = 1 << 1;
static DWORD prevButtons[2]{};
static int rapidFireMask[2]{};
static int rapidFireCounter = 0;
++rapidFireCounter;
bool reset = false;
for (int i = 0; i < 2; ++i)
{
auto &dst = i == 0 ? *pdwPad1 : *pdwPad2;
auto &gp = io::getCurrentGamePadState(i);
int v = (gp.buttons & io::GamePadState::Button::LEFT ? LEFT : 0) |
(gp.buttons & io::GamePadState::Button::RIGHT ? RIGHT : 0) |
(gp.buttons & io::GamePadState::Button::UP ? UP : 0) |
(gp.buttons & io::GamePadState::Button::DOWN ? DOWN : 0) |
(gp.buttons & io::GamePadState::Button::A ? A : 0) |
(gp.buttons & io::GamePadState::Button::B ? B : 0) |
(gp.buttons & io::GamePadState::Button::SELECT ? SELECT : 0) |
(gp.buttons & io::GamePadState::Button::START ? START : 0) |
0;
int rv = v;
if (rapidFireCounter & 2)
{
// 15 fire/sec
rv &= ~rapidFireMask[i];
}
dst = rv;
auto p1 = v;
auto pushed = v & ~prevButtons[i];
if (p1 & SELECT)
{
if (pushed & LEFT)
{
saveNVRAM();
romSelector_.prev();
reset = true;
}
if (pushed & RIGHT)
{
saveNVRAM();
romSelector_.next();
reset = true;
}
if (pushed & START)
{
saveNVRAM();
reset = true;
}
if (pushed & A)
{
rapidFireMask[i] ^= io::GamePadState::Button::A;
}
if (pushed & B)
{
rapidFireMask[i] ^= io::GamePadState::Button::B;
}
if (pushed & UP)
{
screenMode_ = static_cast<ScreenMode>((static_cast<int>(screenMode_) - 1) & 3);
applyScreenMode();
}
else if (pushed & DOWN)
{
screenMode_ = static_cast<ScreenMode>((static_cast<int>(screenMode_) + 1) & 3);
applyScreenMode();
}
}
prevButtons[i] = v;
}
*pdwSystem = reset ? PAD_SYS_QUIT : 0;
}
void InfoNES_MessageBox(const char *pszMsg, ...)
{
printf("[MSG]");
va_list args;
va_start(args, pszMsg);
vprintf(pszMsg, args);
va_end(args);
printf("\n");
}
bool parseROM(const uint8_t *nesFile)
{
memcpy(&NesHeader, nesFile, sizeof(NesHeader));
if (!checkNESMagic(NesHeader.byID))
{
return false;
}
nesFile += sizeof(NesHeader);
memset(SRAM, 0, SRAM_SIZE);
if (NesHeader.byInfo1 & 4)
{
memcpy(&SRAM[0x1000], nesFile, 512);
nesFile += 512;
}
auto romSize = NesHeader.byRomSize * 0x4000;
ROM = (BYTE *)nesFile;
nesFile += romSize;
if (NesHeader.byVRomSize > 0)
{
auto vromSize = NesHeader.byVRomSize * 0x2000;
VROM = (BYTE *)nesFile;
nesFile += vromSize;
}
return true;
}
void InfoNES_ReleaseRom()
{
ROM = nullptr;
VROM = nullptr;
}
void InfoNES_SoundInit()
{
}
int InfoNES_SoundOpen(int samples_per_sync, int sample_rate)
{
return 0;
}
void InfoNES_SoundClose()
{
}
int __not_in_flash_func(InfoNES_GetSoundBufferSize)()
{
return dvi_->getAudioRingBuffer().getFullWritableSize();
}
void __not_in_flash_func(InfoNES_SoundOutput)(int samples, BYTE *wave1, BYTE *wave2, BYTE *wave3, BYTE *wave4, BYTE *wave5)
{
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 w1 = *wave1++;
int w2 = *wave2++;
int w3 = *wave3++;
int w4 = *wave4++;
int w5 = *wave5++;
// w3 = w2 = w4 = w5 = 0;
int l = w1 * 6 + w2 * 3 + w3 * 5 + w4 * 3 * 17 + w5 * 2 * 32;
int r = w1 * 3 + w2 * 6 + w3 * 5 + w4 * 3 * 17 + w5 * 2 * 32;
*p++ = {static_cast<short>(l), static_cast<short>(r)};
// pulse_out = 0.00752 * (pulse1 + pulse2)
// tnd_out = 0.00851 * triangle + 0.00494 * noise + 0.00335 * dmc
// 0.00851/0.00752 = 1.131648936170213
// 0.00494/0.00752 = 0.6569148936170213
// 0.00335/0.00752 = 0.4454787234042554
// 0.00752/0.00851 = 0.8836662749706228
// 0.00494/0.00851 = 0.5804935370152762
// 0.00335/0.00851 = 0.3936545240893067
}
ring.advanceWritePointer(n);
samples -= n;
}
}
extern WORD PC;
void InfoNES_LoadFrame()
{
gpio_put(LED_PIN, hw_divider_s32_quotient_inlined(dvi_->getFrameCounter(), 60) & 1);
// printf("%04x\n", PC);
tuh_task();
}
namespace
{
dvi::DVI::LineBuffer *currentLineBuffer_{};
}
void __not_in_flash_func(drawWorkMeterUnit)(int timing,
[[maybe_unused]] int span,
uint32_t tag)
{
if (timing >= 0 && timing < 640)
{
auto p = currentLineBuffer_->data();
p[timing] = tag; // tag = color
}
}
void __not_in_flash_func(drawWorkMeter)(int line)
{
if (!currentLineBuffer_)
{
return;
}
memset(currentLineBuffer_->data(), 0, 64);
memset(¤tLineBuffer_->data()[320 - 32], 0, 64);
(*currentLineBuffer_)[160] = 0;
if (line == 4)
{
for (int i = 1; i < 10; ++i)
{
(*currentLineBuffer_)[16 * i] = 31;
}
}
constexpr uint32_t clocksPerLine = 800 * 10;
constexpr uint32_t meterScale = 160 * 65536 / (clocksPerLine * 2);
util::WorkMeterEnum(meterScale, 1, drawWorkMeterUnit);
// util::WorkMeterEnum(160, clocksPerLine * 2, drawWorkMeterUnit);
}
void __not_in_flash_func(InfoNES_PreDrawLine)(int line)
{
util::WorkMeterMark(0xaaaa);
auto b = dvi_->getLineBuffer();
util::WorkMeterMark(0x5555);
InfoNES_SetLineBuffer(b->data() + 32, b->size());
// (*b)[319] = line + dvi_->getFrameCounter();
currentLineBuffer_ = b;
}
void __not_in_flash_func(InfoNES_PostDrawLine)(int line)
{
#if !defined(NDEBUG)
util::WorkMeterMark(0xffff);
drawWorkMeter(line);
#endif
assert(currentLineBuffer_);
dvi_->setLineBuffer(line, currentLineBuffer_);
currentLineBuffer_ = nullptr;
}
bool loadAndReset()
{
auto rom = romSelector_.getCurrentROM();
if (!rom)
{
printf("ROM does not exists.\n");
return false;
}
if (!parseROM(rom))
{
printf("NES file parse error.\n");
return false;
}
loadNVRAM();
if (InfoNES_Reset() < 0)
{
printf("NES reset error.\n");
return false;
}
return true;
}
int InfoNES_Menu()
{
// InfoNES_Main() のループで最初に呼ばれる
loadAndReset();
return 0;
}
void __not_in_flash_func(core1_main)()
{
while (true)
{
dvi_->registerIRQThisCore();
dvi_->waitForValidLine();
dvi_->start();
while (!exclProc_.isExist())
{
if (scaleMode8_7_)
{
dvi_->convertScanBuffer12bppScaled16_7(34, 32, 288 * 2);
// 34 + 252 + 34
// 32 + 576 + 32
}
else
{
dvi_->convertScanBuffer12bpp();
}
}
dvi_->unregisterIRQThisCore();
dvi_->stop();
exclProc_.processOrWaitIfExist();
}
}
int main()
{
vreg_set_voltage(VREG_VOLTAGE_1_20);
sleep_ms(10);
set_sys_clock_khz(CPUFreqKHz, true);
stdio_init_all();
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_put(LED_PIN, 1);
tusb_init();
romSelector_.init(NES_FILE_ADDR);
// util::dumpMemory((void *)NES_FILE_ADDR, 1024);
#if 0
//
auto *i2c = i2c0;
static constexpr int I2C_SDA_PIN = 16;
static constexpr int I2C_SCL_PIN = 17;
i2c_init(i2c, 100 * 1000);
gpio_set_function(I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(I2C_SCL_PIN, GPIO_FUNC_I2C);
// gpio_pull_up(I2C_SDA_PIN);
// gpio_pull_up(I2C_SCL_PIN);
i2c_set_slave_mode(i2c, false, 0);
{
constexpr int addrSegmentPointer = 0x60 >> 1;
constexpr int addrEDID = 0xa0 >> 1;
constexpr int addrDisplayID = 0xa4 >> 1;
uint8_t buf[128];
int addr = 0;
do
{
printf("addr: %04x\n", addr);
uint8_t tmp = addr >> 8;
i2c_write_blocking(i2c, addrSegmentPointer, &tmp, 1, false);
tmp = addr & 255;
i2c_write_blocking(i2c, addrEDID, &tmp, 1, true);
i2c_read_blocking(i2c, addrEDID, buf, 128, false);
util::dumpMemory(buf, 128);
printf("\n");
addr += 128;
} while (buf[126]);
}
#endif
//
dvi_ = std::make_unique<dvi::DVI>(pio0, &DVICONFIG,
dvi::getTiming640x480p60Hz());
// dvi_->setAudioFreq(48000, 25200, 6144);
dvi_->setAudioFreq(44100, 28000, 6272);
dvi_->allocateAudioBuffer(256);
// dvi_->setExclusiveProc(&exclProc_);
dvi_->getBlankSettings().top = 4 * 2;
dvi_->getBlankSettings().bottom = 4 * 2;
// dvi_->setScanLine(true);
applyScreenMode();
// 空サンプル詰めとく
dvi_->getAudioRingBuffer().advanceWritePointer(255);
multicore_launch_core1(core1_main);
InfoNES_Main();
return 0;
}