-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
516 lines (405 loc) · 13.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
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
#include <unordered_map>
#include <SDL.h>
#include "ARMv6MCore.h"
#include "GDBServer.h"
#include "Logging.h"
#include "board/Pico.h"
#include "board/PicoSystem.h"
#include "board/Tufty2040.h"
using Logging::logf;
using LogLevel = Logging::Level;
constexpr auto logComponent = Logging::Component::Main;
enum class BoardId
{
Unknown = -1,
Pico = 0,
PimoroniPicoSystem,
PimoroniTufty2040,
};
static bool quit = false;
static MemoryBus mem;
static ARMv6MCore cpuCores[2]{mem, mem};
static GDBServer gdbServer;
static uint8_t bootROM[0x4000];
static std::ifstream uf2File;
static BoardId boardId = BoardId::Unknown;
static Board *board = nullptr;
static SDL_Renderer *renderer = nullptr;
static SDL_Texture *texture = nullptr;
static int screenWidth = 240;
static int screenHeight = 240;
static SDL_AudioDeviceID audioDevice = 0;
static const uint32_t uf2MagicStart0 = 0x0A324655, uf2MagicStart1 = 0x9E5D5157, uf2MagicEnd = 0x0AB16F30;
struct UF2Block
{
uint32_t magicStart[2];
uint32_t flags;
uint32_t addr;
uint32_t payloadSize;
uint32_t blockNo;
uint32_t numBlocks;
uint32_t familyIdSize;
uint8_t payload[476];
uint32_t magicEnd;
};
static_assert(sizeof(UF2Block) == 512);
static BoardId stringToBoard(std::string_view str)
{
if(str == "pico")
return BoardId::Pico;
if(str == "pimoroni_picosystem")
return BoardId::PimoroniPicoSystem;
if(str == "pimoroni_tufty2040")
return BoardId::PimoroniTufty2040;
logf(LogLevel::Warning, logComponent, "Unknown board \"%.*s\", falling back to \"pico\"", int(str.length()), str.data());
return BoardId::Pico;
}
static bool parseUF2(std::ifstream &file)
{
while(!file.eof())
{
UF2Block block;
file.read(reinterpret_cast<char *>(&block), sizeof(block));
if(block.magicStart[0] != uf2MagicStart0 || block.magicStart[1] != uf2MagicStart1 || block.magicEnd != uf2MagicEnd)
{
logf(LogLevel::Error, logComponent, "Bad UF2 block magic!");
return false;
}
auto ptr = mem.mapAddress(block.addr);
if(ptr)
memcpy(ptr, block.payload, block.payloadSize);
else
logf(LogLevel::Warning, logComponent, "Can't write UF2 payload to %08X!", block.addr);
}
// parse binary info
const uint32_t biStart = 0x7188EBF2, biEnd = 0xE71AA390;
const uint16_t biTypeIdAndString = 6;
const uint16_t biTagRaspberryPi = 0x5052;
static const std::map<uint32_t, const char *> raspberryPiIdMap{
{0x02031c86, "program_name"},
{0x11a9bc3a, "program_version"},
{0x9da22254, "program_build_date"},
{0x68f465de, "program_binary_end"},
{0x1856239a, "program_url"},
{0xb6a07c19, "program_description"},
{0xa1f4b453, "program_feature"},
{0x4275f0d3, "program_build_attribute"},
{0x5360b3ab, "sdk_version"},
{0xb63cffbb, "pico_board"},
{0x7f8882e1, "boot2_name"}};
auto flash = mem.mapAddress(0x10000000);
auto ptr = reinterpret_cast<uint32_t *>(flash + 256);
uint32_t infoStartAddr = 0, infoEndAddr;
for (int i = 0; i < 256 / 4; i++, ptr++)
{
if (ptr[0] == biStart && ptr[4] == biEnd)
{
infoStartAddr = ptr[1];
infoEndAddr = ptr[2];
// infoMapAddr = ptr[3];
}
}
if (infoStartAddr)
{
logf(LogLevel::Info, logComponent, "binary_info:");
for (auto addr = infoStartAddr; addr < infoEndAddr; addr += 4)
{
auto infoPtr = *reinterpret_cast<uint32_t *>(flash + addr - 0x10000000);
auto infoData = flash + infoPtr - 0x10000000;
auto type = *reinterpret_cast<uint16_t *>(infoData);
auto tag = *reinterpret_cast<uint16_t *>(infoData + 2);
if (tag != biTagRaspberryPi)
continue;
if (type == biTypeIdAndString)
{
auto id = *reinterpret_cast<uint32_t *>(infoData + 4);
auto charPtr = *reinterpret_cast<uint32_t *>(infoData + 8);
auto str = reinterpret_cast<char *>(flash + charPtr - 0x10000000);
auto idStr = raspberryPiIdMap.find(id);
if(idStr != raspberryPiIdMap.end())
logf(LogLevel::Info, logComponent, "\t%s: %s", idStr->second, str);
// detect board
if(id == 0xb63cffbb/*pico_board*/ && boardId == BoardId::Unknown)
boardId = stringToBoard(str);
}
}
}
return true;
}
static void runGDBServer()
{
gdbServer.setCPUs(cpuCores, 2);
if(!gdbServer.start()) {
logf(LogLevel::Error, logComponent, "Failed to start GDB server!");
return;
}
while(!quit)
{
if(!gdbServer.update(true))
logf(LogLevel::Error, logComponent, "Failed to update GDB server!");
}
}
static void pollEvents()
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
board->handleEvent(event);
switch(event.type)
{
case SDL_QUIT:
quit = true;
break;
}
}
}
static void handleLogArg(const char *arg)
{
bool enable = true;
if(arg[0] == '-')
{
enable = false;
arg++;
}
std::string_view str(arg);
auto level = Logging::stringToLevel(str);
if(level != LogLevel::Invalid)
{
Logging::setEnabled(level, enable);
return;
}
auto component = Logging::stringToComponent(str);
if(component != Logging::Component::Invalid)
{
Logging::setEnabled(component, enable);
return;
}
}
void updateScreenSettings()
{
if(!renderer)
return;
// check for size change
int newWidth, newHeight;
board->getScreenSize(newWidth, newHeight);
if(newWidth != screenWidth || newHeight != screenHeight)
{
screenWidth = newWidth;
screenHeight = newHeight;
// update renderer
SDL_RenderSetLogicalSize(renderer, screenWidth, screenHeight);
SDL_RenderSetIntegerScale(renderer, SDL_TRUE);
}
texture = SDL_CreateTexture(renderer, board->getScreenFormat(), SDL_TEXTUREACCESS_STREAMING, screenWidth, screenHeight);
}
void queueAudio(const int16_t *samples, unsigned int numSamples)
{
SDL_QueueAudio(audioDevice, samples, numSamples * sizeof(int16_t));
}
int main(int argc, char *argv[])
{
int screenScale = 5;
std::thread gdbServerThread;
bool usbEnabled = false;
bool gdbEnabled = false;
std::string romFilename;
// disable some log levels by default
Logging::setEnabled(LogLevel::Debug, false);
Logging::setEnabled(LogLevel::NotImplemented, false); // very noisy (for now?)
int i = 1;
for(; i < argc; i++)
{
std::string arg(argv[i]);
if(arg == "--scale" && i + 1 < argc)
screenScale = std::stoi(argv[++i]);
else if(arg == "--picosystem-sdk")
{
// picosystem SDK does not require the correct board to be set... so most uf2s don't
boardId = BoardId::PimoroniPicoSystem;
logf(LogLevel::Warning, logComponent, "Use --board pimoroni_picosystem instead of --picosystem-sdk");
}
else if(arg == "--board" && i + 1 < argc)
boardId = stringToBoard(argv[++i]);
else if(arg == "--usb")
usbEnabled = true;
else if(arg == "--usbip")
{
usbEnabled = true;
mem.getUSB().setUSBIPEnabled(true);
}
else if(arg == "--log" && i + 1 < argc)
handleLogArg(argv[++i]);
else if(arg == "--gdb")
gdbEnabled = true;
else if(arg == "--iolog" && i + 1 < argc)
mem.getGPIO().openLogFile(argv[++i]);
else
break;
}
if(i == argc)
logf(LogLevel::Info, logComponent, "No file specified!");
else
romFilename = argv[i];
// get base path
std::string basePath;
auto tmp = SDL_GetBasePath();
if(tmp)
{
basePath = tmp;
SDL_free(tmp);
}
if(!romFilename.empty())
{
uf2File.open(romFilename, std::ifstream::in | std::ifstream::binary);
if(!uf2File || !parseUF2(uf2File))
{
logf(LogLevel::Error, logComponent, "Failed to open UF2 \"%s\"", romFilename.c_str());
return 1;
}
}
// default board if still not set
if(boardId == BoardId::Unknown)
{
logf(LogLevel::Info, logComponent, "Board not specified, falling back to \"pico\"");
boardId = BoardId::Pico;
}
// emu init
mem.setCPUs(cpuCores);
std::ifstream bootROMFile(basePath + "bootrom.bin", std::ifstream::in | std::ifstream::binary);
if(bootROMFile)
{
bootROMFile.read(reinterpret_cast<char *>(bootROM), sizeof(bootROM));
mem.setBootROM(bootROM);
}
else
{
logf(LogLevel::Error, logComponent, "bootrom.bin not found!");
return 1;
}
auto &clocks = mem.getClocks();
for(auto &core : cpuCores)
clocks.addClockTarget(5, core.getClock());
mem.reset();
// create board
switch(boardId)
{
case BoardId::PimoroniPicoSystem:
board = new PicoSystemBoard(mem);
break;
case BoardId::PimoroniTufty2040:
board = new Tufty2040Board(mem);
break;
default:
board = new PicoBoard(mem);
}
board->getScreenSize(screenWidth, screenHeight);
if(gdbEnabled)
gdbServerThread = std::thread(runGDBServer);
// SDL init
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0)
{
logf(LogLevel::Error, logComponent, "Failed to init SDL!");
return 1;
}
bool boardHasScreen = screenWidth && screenHeight;
SDL_Window *window = nullptr;
if(boardHasScreen)
{
window = SDL_CreateWindow("DaftBoySDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
screenWidth * screenScale, screenHeight * screenScale,
SDL_WINDOW_RESIZABLE);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
SDL_RenderSetLogicalSize(renderer, screenWidth, screenHeight);
SDL_RenderSetIntegerScale(renderer, SDL_TRUE);
texture = SDL_CreateTexture(renderer, board->getScreenFormat(), SDL_TEXTUREACCESS_STREAMING, screenWidth, screenHeight);
}
if(board->hasAudio())
{
SDL_AudioSpec spec{};
spec.freq = 48000;
spec.format = AUDIO_S16;
spec.channels = 1;
spec.samples = 512;
audioDevice = SDL_OpenAudioDevice(nullptr, false, &spec, nullptr, 0);
if(!audioDevice)
{
logf(LogLevel::Error, logComponent, "Failed to open audio: %s", SDL_GetError());
quit = true;
}
SDL_PauseAudioDevice(audioDevice, 0);
}
auto lastTick = SDL_GetTicks();
auto lastFreqUpdate = lastTick;
uint32_t cpuCycles = 0;
while(!quit)
{
pollEvents();
auto now = SDL_GetTicks();
// update freq info
if(now - lastFreqUpdate >= 1000)
{
if(window)
{
auto time = now - lastFreqUpdate;
int speedPercent = static_cast<uint64_t>(cpuCycles) * 1000 / time * 100 / clocks.getClockFrequency(5);
char buf[50];
snprintf(buf, sizeof(buf), "DERP | SYS: %iMHz (%i%%)", cpuCycles / (1000 * time), speedPercent);
SDL_SetWindowTitle(window, buf);
}
cpuCycles = 0;
lastFreqUpdate = now;
}
auto elapsed = now - lastTick;
// clamp if running behind
if(elapsed > 30)
elapsed = 30;
// lock cpu access around updates
if(gdbEnabled)
gdbServer.getCPUMutex().unlock();
cpuCycles += cpuCores[0].run(elapsed);
// sync core 1
// TODO: more sync
auto time = cpuCores[0].getClock().getTime();
cpuCores[1].update(time);
// sync peripherals
mem.peripheralUpdate(time);
if(gdbEnabled)
gdbServer.getCPUMutex().unlock();
board->update(time);
// attempt to connect USB
if(usbEnabled)
{
auto &usb = mem.getUSB();
if(usb.getEnabled() && !usb.getConfigured())
usb.startEnumeration();
usb.usbipUpdate();
}
// adjust timers to stay in range
clocks.adjustClocks();
lastTick = now;
if(renderer)
{
// TODO: sync
SDL_UpdateTexture(texture, nullptr, board->getScreenData(), screenWidth * 2);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
}
}
if(texture)
SDL_DestroyTexture(texture);
if(renderer)
SDL_DestroyRenderer(renderer);
if(window)
SDL_DestroyWindow(window);
if(audioDevice)
SDL_CloseAudioDevice(audioDevice);
delete board;
if(gdbEnabled)
gdbServerThread.join();
return 0;
}