-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
446 lines (408 loc) · 13.2 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
//
// Created by valerino on 06/01/19.
//
#include <stdio.h>
#include <SDL.h>
#include <CBuffer.h>
#include <CMOS65xx.h>
#include <getopt.h>
#include "CDisplay.h"
#include "CInput.h"
#include "CAudio.h"
#include "CMemory.h"
#include "CCIA1.h"
#include "CCIA2.h"
#include "CVICII.h"
#include "CSID.h"
#include "CPLA.h"
/**
* globals
*/
CMOS65xx *cpu = nullptr;
CMemory *mem = nullptr;
CDisplay *display = nullptr;
CInput *input = nullptr;
CAudio *audio = nullptr;
CVICII *vic = nullptr;
CCIA1 *cia1 = nullptr;
CCIA2 *cia2 = nullptr;
CSID *sid = nullptr;
CPLA *pla = nullptr;
bool running = true;
bool debugger = false;
bool hotkeyDbgBreak = false;
int64_t totalCycles = 0;
char *path = nullptr;
bool joy2HackEnabled = false;
int joyNum = 0;
int64_t frames = 0;
/**
* shows banner
*/
void banner() {
printf("vc64 - a c64 emulator\n"
"\t(c)opyleft, valerino, y2k19\n");
}
/**
* a callback for memory writes
*/
void cpuCallbackWrite(uint16_t address, uint8_t val) {
int mapType = pla->mapAddressToType(address);
// write to ram or chips
if (address >= VIC_REGISTERS_START && address <= VIC_REGISTERS_END) {
if (mapType == PLA_MAP_IO_DEVICES) {
// access VIC registers
vic->write(address, val);
return;
}
} else if (address >= CIA2_REGISTERS_START &&
address <= CIA2_REGISTERS_END) {
if (mapType == PLA_MAP_IO_DEVICES) {
// access CIA2 registers
cia2->write(address, val);
return;
}
} else if (address >= CIA1_REGISTERS_START &&
address <= CIA1_REGISTERS_END) {
if (mapType == PLA_MAP_IO_DEVICES) {
// access CIA1 registers
cia1->write(address, val);
return;
}
}
// default, write to ram
mem->writeByte(address, val);
}
/**
* a callback for memory reads
*/
void cpuCallbackRead(uint16_t address, uint8_t *val) {
int mapType = pla->mapAddressToType(address);
// read from ram or chips
if (address >= VIC_REGISTERS_START && address <= VIC_REGISTERS_END) {
if (mapType == PLA_MAP_IO_DEVICES) {
// access VIC registers
vic->read(address, val);
return;
}
} else if (address >= CIA2_REGISTERS_START &&
address <= CIA2_REGISTERS_END) {
if (mapType == PLA_MAP_IO_DEVICES) {
// access CIA2 registers
cia2->read(address, val);
return;
}
} else if (address >= CIA1_REGISTERS_START &&
address <= CIA1_REGISTERS_END) {
if (mapType == PLA_MAP_IO_DEVICES) {
// access CIA1 registers
cia1->read(address, val);
return;
}
}
// default, read from ram
mem->readByte(address, val);
}
/**
* @brief poll for SDL events (input, etc...) and takes the appropriate
* action
*/
void pollSdlEvents() {
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT) {
// SDL window closed, application must quit asap
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "QUIT requested!");
running = false;
break;
}
switch (ev.type) {
case SDL_KEYUP:
case SDL_KEYDOWN: {
// process input
uint32_t hotkeys = 0;
input->update(&ev, &hotkeys);
if (hotkeys == HOTKEY_DEBUGGER && debugger) {
// we must break!
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"HOTKEY DEBUGBREAK requested!");
hotkeyDbgBreak = true;
} else if (hotkeys == HOTKEY_PASTE_TEXT) {
// fill the clipboard queue to be processed in the main loop
input->fillClipboardQueue();
} else if (hotkeys == HOTKEY_JOY2_HACK_SWITCH) {
if (joyNum == 2) {
// enable/disable joy2 hack
joy2HackEnabled = !joy2HackEnabled;
cia1->enableJoy2Hack(joy2HackEnabled);
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"HOTKEY JOY2HACK, setting status=%s",
joy2HackEnabled ? "enabled" : "disabled");
}
} else if (hotkeys == HOTKEY_FORCE_EXIT) {
// force exit
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "HOTKEY FORCE exit!");
running = false;
}
break;
}
default:
break;
}
}
}
/**
* @brief prints usage
* @param argv
*/
void usage(char **argv) {
printf("usage: %s -f <file> [-dsh]\n"
"\t-f: file to be loaded (PRG only is supported as now)\n"
"\t-t: run cpu test in test/6502_functional_test.bin\n"
"\t-j: 1|2, joystick in port 1 or 2 (default is 0, no joystick. "
"either, arrows=directions, leftshift=fire).\n\t\t"
"when joy2 is enabled, press ctrl-j to switch on/off keyboard (due "
"to a dirty hack i used!).\n"
"\t-d: debugger (if enabled, you may also use ctrl-d to break "
"while "
"running)\n"
"\t-s: fullscreen\n"
"\t-c: off|nospr|nobck (to disable hw collisions sprite/sprite, "
"sprite/background, all. default is all collisions enabled)\n"
"\t-h: this help\n",
argv[0]);
}
/**
* just test the cpu
*/
void testCpuMain(bool debugger) {
while (running) {
// step the cpu
int cycles = cpu->step(debugger, debugger ? hotkeyDbgBreak : false);
if (cycles == -1) {
// exit loop
running = false;
continue;
}
totalCycles += cycles;
// reset hotkey dbg-break status if any
hotkeyDbgBreak = false;
}
}
/**
* @brief load .PRG in memory and inject RUN in the keyboard buffer
*/
void handlePrgLoading() {
// enough cycles passed....
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "cycle=%lld, loading prg at %s",
totalCycles, path);
// TODO: determine if it's a prg, either fail....
int res = mem->loadPrg(path);
if (res == 0) {
// inject run in the keyboard buffer
input->injectKeyboardBuffer("RUN\r");
}
// we don't need this to trigger anymore
path = nullptr;
}
int main(int argc, char **argv) {
// prints title
banner();
bool fullScreen = false;
bool isTestCpu = false;
char *collisionDisableType = nullptr;
// parse commandline
while (1) {
int option = getopt(argc, argv, "dshtc:f:j:");
if (option == -1) {
break;
}
switch (option) {
case 'j':
joyNum = atoi(optarg);
if (joyNum < 1 || joyNum > 2) {
// default, no joystick
joyNum = 0;
}
if (joyNum == 2) {
// set initial status enabled
joy2HackEnabled = true;
}
break;
case '?':
case 'h':
usage(argv);
return 1;
case 't':
isTestCpu = true;
break;
case 'f':
path = optarg;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "file to load=%s", path);
break;
case 'c':
collisionDisableType = optarg;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "hw collision disable=%s",
collisionDisableType);
break;
case 'd':
debugger = true;
break;
case 's':
fullScreen = true;
break;
default:
break;
}
}
bool sdlInitialized = false;
uint32_t startTime = SDL_GetTicks();
int res = 0;
do {
// initialize sdl
res = SDL_Init(SDL_INIT_EVERYTHING);
if (res != 0) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL_Init(): %s",
SDL_GetError());
break;
}
sdlInitialized = true;
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "SDL initialized OK!");
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG);
// create memory
pla = new CPLA();
mem = new CMemory(pla);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "memory initialized OK!");
// create cpu
cpu = new CMOS65xx(mem, cpuCallbackRead, cpuCallbackWrite);
if (cpu->reset(isTestCpu) != 0) {
// failed to load bios
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "failed to load bios files!");
break;
}
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "CPU initialized OK!");
if (debugger) {
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
"debugging mode ACTIVE!");
}
// create additional chips
cia1 = new CCIA1(cpu, pla);
cia2 = new CCIA2(cpu, pla);
vic = new CVICII(cpu, cia2, pla);
sid = new CSID(cpu);
// create the subsystems (display, input, audio)
try {
display = new CDisplay(vic, "vc64-emu", fullScreen);
} catch (std::exception ex) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "display->init(): %s",
ex.what());
break;
}
input = new CInput(cia1, joyNum);
audio = new CAudio(sid);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "display initialized OK!");
int timeNow = SDL_GetTicks();
if (isTestCpu) {
// running test
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
"running CPU in test mode!");
testCpuMain(debugger);
res = 0;
break;
}
// disable collisons in vic if we're told so
if (collisionDisableType) {
// void setCollisionHandling(bool enableSpriteSprite,
// bool enableBackgroundSprite);//
bool enableSprSpr = true;
bool enableSprBck = true;
if (strcmp(collisionDisableType, "off") == 0) {
// all off
enableSprBck = false;
enableSprSpr = false;
} else if (strcmp(collisionDisableType, "nospr") == 0) {
enableSprSpr = false;
} else if (strcmp(collisionDisableType, "nobck") == 0) {
enableSprSpr = false;
}
vic->setCollisionHandling(enableSprSpr, enableSprBck);
}
// 312 lines * 63 Cycles = 19656
int cyclesPerFrame = 19656;
int msecPerFrame = 20; // (50 : 1 = 1: x) * 1000
int cycleCounter = cyclesPerFrame;
while (running) {
// step the cpu
int cycles = cpu->step(debugger, debugger ? hotkeyDbgBreak : false);
if (cycles == -1) {
// exit loop
running = false;
continue;
}
totalCycles += cycles;
// reset hotkey-dbgbreak status if any (for the debugger)
hotkeyDbgBreak = false;
// update i/o chips
cia1->update(totalCycles);
cia2->update(totalCycles);
// updating vic takes into account the less scanlines for
// badlines (23 vs 63)
int c = vic->update(totalCycles);
cycles += c;
// @fixme: this is wrong, cycles should be added .... but doing
// it screws all (probably vic cycle counting is wrong)
// totalCycles += c;
// update audio chip
sid->update(totalCycles);
// update cyclecounter
cycleCounter -= cycles;
if (cycleCounter <= 0) {
// draw a frame
frames++;
display->update();
// SDL_Log("totalCycles=%lld, frames=%lld", totalCycles,
// frames);
cycleCounter += cyclesPerFrame;
// poll events
pollSdlEvents();
// sleep for the remaining time, if any
int timeThen = SDL_GetTicks();
int diff = timeThen - timeNow;
if (diff < msecPerFrame) {
SDL_Delay(msecPerFrame - diff);
}
timeNow = timeThen;
// handle clipboard, if any
input->checkClipboard(totalCycles, cyclesPerFrame, 5);
}
// once the cpu has reached enough cycles to have loaded the
// BASIC interpreter, issue a load of our prg. this trigger only
// once!
if (totalCycles > 2570000 && path) {
// if (frames > 1150 && path) {
handlePrgLoading();
path = nullptr;
}
}
} while (0);
// calculate some statistics
uint32_t endTime = SDL_GetTicks() - startTime;
printf("done, running for %02d:%02d:%02d, total CPU cycles=%lld, "
"frames=%lld\n",
endTime / 1000 / 60 / 60, endTime / 1000 / 60, (endTime / 1000) % 60,
totalCycles, frames);
// done
SAFE_DELETE(cia1)
SAFE_DELETE(cia2)
SAFE_DELETE(vic)
SAFE_DELETE(sid)
SAFE_DELETE(display)
SAFE_DELETE(input)
SAFE_DELETE(audio)
SAFE_DELETE(mem)
SAFE_DELETE(cpu)
if (sdlInitialized) {
SDL_Quit();
}
return 0;
}