-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
545 lines (519 loc) · 20 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
#include "rom.h"
#include "mem.h"
#include "ppu3.h"
#include "cpu2.h"
#include "apu.h"
#include "util.h"
using namespace std;
#define ORIGIN 2
#ifdef _WIN32
//Windows doesn't provide getopt, so this is a simple implementation of it.
#include <ctime>
#include <string.h>
#include <stdio.h>
int opterr = 1, /* if error message should be printed */
optind = 1, /* index into parent argv vector */
optopt, /* character checked for validity */
optreset; /* reset getopt */
char *optarg; /* argument associated with option */
#define BADCH (int)'?'
#define BADARG (int)':'
#define EMSG ""
/*
* getopt -- Parse argc/argv argument vector.
*/
int getopt(int nargc, char * const nargv[], const char *ostr) {
static char *place = EMSG; /* option letter processing */
char *oli; /* option letter list index */
if (optreset || !*place) { /* update scanning pointer */
optreset = 0;
if (optind >= nargc || *(place = nargv[optind]) != '-') {
place = EMSG;
return (-1);
}
if (place[1] && *++place == '-') { /* found "--" */
++optind;
place = EMSG;
return (-1);
}
} /* option letter okay? */
if ((optopt = (int)*place++) == (int)':' ||
!(oli = (char *)(strchr(ostr, optopt)))) {
/*
* if the user didn't specify '-' as an option,
* assume it means -1.
*/
if (optopt == (int)'-')
return (-1);
if (!*place)
++optind;
if (opterr && *ostr != ':')
(void)printf("illegal option -- %c\n", optopt);
return (BADCH);
}
if (*++oli != ':') { /* don't need argument */
optarg = NULL;
if (!*place)
++optind;
}
else { /* need an argument */
if (*place) /* no white space */
optarg = place;
else if (nargc <= ++optind) { /* no arg */
place = EMSG;
if (*ostr == ':')
return (BADARG);
if (opterr)
(void)printf("option requires an argument -- %c\n", optopt);
return (BADCH);
}
else /* white space */
optarg = nargv[optind];
place = EMSG;
++optind;
}
return (optopt); /* dump back option letter */
}
#else
//Linux provides getopt, so include the header.
#include <getopt.h>
#endif
int run_nsf(rom& cart, apu& apui, mem& memi, cpu& cpui);
int main(int argc, char ** argv) {
opterr = 0;
char option;
int res=1;
int mapper=0;
//cout<<"Argc: "<<argc<<endl;
if(argc==1) {
cout<<"Usage:\n\n./"<<argv[0]<<" [-d #] [-m #] <filename>\n\n\tOptions:\n\t-d Enables debug output. Options for # are:\n\t\t1\tCPU\n\t\t2\tMAIN\n\t\t3\tMEM\n\t\t4\tPPU\n\t\t5\tROM\n\t\t6\tUTIL\n\t\t7\tWAV\n\t\t7\tAPU\n";
cout<<"\n\t-m manually specifies an iNES mapper number to use.\n";
}
while(argc>3&&(option=getopt(argc,argv,"d:x:m:"))!=-1) {
switch(option) {
case 'd':
util::toggle_log(atoi(optarg));
break;
case 'x':
res=atoi(optarg);
break;
case 'm':
mapper=atoi(optarg);
break;
default:
cerr<<"Unkown option '"<<optopt<<".\n";
return 1;
}
}
if(res==0)
res=1;
std::string filename;
if(optind<argc||argc==2) {
filename=std::string(argv[optind]);
cout<<"Loading "<<filename<<"..."<<endl;
}
else {
cout<<"Give a single filename, like this: \n\n\t"<<argv[0]<<" rom.nes"<<endl;
return 1;
}
// if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0 ) {
bool headless = false;
if( SDL_Init(SDL_INIT_EVERYTHING|SDL_INIT_JOYSTICK|SDL_INIT_GAMECONTROLLER|SDL_INIT_NOPARACHUTE) < 0 ) {
if( SDL_Init(SDL_INIT_NOPARACHUTE|SDL_INIT_AUDIO) < 0) {
fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
headless = true;
cout<<"Running in headless mode due to difficulty initializing some SDL subsystems."<<endl;
}
//cout<<"Trying to open the joystick (if any)"<<endl;
SDL_Joystick * js = NULL;
int jscount = SDL_NumJoysticks();
if(jscount > 0) {
for(int i=0;i<jscount;++i) {
const char * name = SDL_JoystickNameForIndex(i);
cout<<"Found joystick: "<<name<<endl;
}
cout<<"Just opening joystick 0 though"<<endl;
js = SDL_JoystickOpen(0);
}
//cout<<"Done with joystick stuff"<<endl;
//cout<<"Going to load cartridge"<<endl;
rom cart(filename, mapper);
//cout<<"Cart loaded. Going to start ppu"<<endl;
ppu ppui(cart,res);
//cout<<"PPU started. Going to start apu."<<endl;
if(ppui.dummy) {
headless = true;
cout<<"PPU started in dummy mode, so audio will too."<<endl;
}
apu apui(headless);
SDL_PauseAudioDevice(apui.get_id(), true);
//cout<<"APU started. Going to bring up memory map"<<endl;
mem memi(cart,ppui,apui);
apui.setmem(&memi);
//cout<<"Memory started. Going to start CPU"<<endl;
cpu cpui(&memi,&apui,memi.get_rst_addr(), cart.isNSF());
//cout<<"CPU started."<<endl;
if(cart.isNSF()) {
cout<<"Starting NSF player instead of main emulator"<<endl;
return run_nsf(cart, apui, memi, cpui);
}
bool paused=false;
//int time_stop;
//long total_time;
int pstart = SDL_GetTicks();
//int delay = 0;
//long delay_count = 0;
//long delay_total = 0;
SDL_PauseAudioDevice(apui.get_id(), false);
while (1==1) {
//cout<<"Start of event loop."<<endl;
//int frame_start = SDL_GetTicks();
SDL_Event event;
//int curx,cury;
int newx,newy;
//bool changed;
//cout<<"Polling and handling events"<<endl;
while(SDL_PollEvent(&event)){ /* Loop until there are no events left on the queue */
switch(event.type){ /* Process the appropiate event type */
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
{
SDL_Scancode sc;
int button = int(event.jbutton.button);
int state = int(event.jbutton.state);
//cout<<"Button: "<<state<<" state: "<<state<<endl;
cout<<"Saw button: "<<button<<endl;
switch(button) {
case 0: sc = SDL_SCANCODE_L; break;
case 1: sc = SDL_SCANCODE_K; break;
case 4: sc = SDL_SCANCODE_G; break;
case 5: sc = SDL_SCANCODE_H; break;
case 8: sc = SDL_SCANCODE_W; break;
case 9: sc = SDL_SCANCODE_S; break;
case 10: sc = SDL_SCANCODE_A; break;
case 11: sc = SDL_SCANCODE_D; break;
default: sc = SDL_SCANCODE_UNKNOWN;
}
if(state == 1)
memi.sendkeydown(sc);
else
memi.sendkeyup(sc);
}
break;
case SDL_JOYDEVICEADDED:
case SDL_CONTROLLERDEVICEADDED:
cout<<"Added joystick "<<event.jdevice.which<<": "<<SDL_JoystickNameForIndex(event.jdevice.which)<<endl;
if(js) {
SDL_JoystickClose(js);
js = NULL;
}
if(!js) {
cout<<"No joystick previously connected, so I'm using that one."<<endl;
js = SDL_JoystickOpen(event.jdevice.which);
}
break;
case SDL_JOYDEVICEREMOVED:
case SDL_CONTROLLERDEVICEREMOVED:
cout<<"Removed joystick: "<<SDL_JoystickNameForIndex(event.jdevice.which)<<endl;
if(js) {
SDL_JoystickClose(js);
js = NULL;
}
break;
case SDL_KEYDOWN: /* Handle a KEYDOWN event */
if(event.key.keysym.scancode==SDL_SCANCODE_Q||
(event.key.keysym.scancode==SDL_SCANCODE_C&&(event.key.keysym.mod==KMOD_RCTRL))||
(event.key.keysym.scancode==SDL_SCANCODE_C&&(event.key.keysym.mod==KMOD_LCTRL))) {
printf("You pressed q or ctrl-c. Exiting.\n");
cpui.print_details(filename);
printf("%d frames rendered in %f seconds. (%f FPS)\n",ppui.get_frame(),float(clock())/float(CLOCKS_PER_SEC),float(ppui.get_frame())/(float(clock())/float(CLOCKS_PER_SEC)));
//SDL_PauseAudio(true);
cout<<"Calling SDL_Quit()"<<endl;
SDL_Quit();
cout<<"Called SDL_Quit()"<<endl;
return 0;
}
else if(event.key.keysym.scancode==SDL_SCANCODE_R) {
cpui.reset(memi.get_rst_addr());
}
else if(event.key.keysym.scancode==SDL_SCANCODE_P) {
paused=!paused;
if(paused) {
SDL_PauseAudioDevice(apui.get_id(), true);
printf("Paused emulation.\n");
}
else {
SDL_PauseAudioDevice(apui.get_id(), false);
printf("Unpaused emulation.\n");
}
}
else if(event.key.keysym.scancode >= SDL_SCANCODE_1 &&
event.key.keysym.scancode <= SDL_SCANCODE_0) {
std::cout<<"Saw scancode "<<event.key.keysym.scancode<<endl;
printf(" debug output #%d",int(event.key.keysym.scancode- (SDL_SCANCODE_1 - 1)));
//util::toggle_log(event.key.keysym.scancode-48);
printf("\n");
}
else if(event.key.keysym.scancode==SDL_SCANCODE_TAB) {
ppui.toggle_table_view();
}
else {
//printf("Keydown\n");
//printf("Time: %d\n",SDL_GetTicks());
memi.sendkeydown(event.key.keysym.scancode);
}
break;
case SDL_KEYUP: /* Handle a KEYUP event*/
//printf("Keyup\n");
memi.sendkeyup(event.key.keysym.scancode);
break;
case SDL_MOUSEMOTION:
memi.sendmousepos(event.motion.x, event.motion.y);
break;
case SDL_MOUSEBUTTONDOWN:
memi.sendmousedown(event.button.x,event.button.y);
break;
case SDL_MOUSEBUTTONUP:
memi.sendmouseup(event.button.x,event.button.y);
break;
//case SDL_VIDEORESIZE:
case SDL_WINDOWEVENT:
switch(event.window.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED:
newx=event.window.data1;
newy=event.window.data2;
if(newx > 0 && newy > 0) {
//changed=true;
}
//ppui.resize(newx,newy);
break;
case SDL_WINDOWEVENT_EXPOSED:
//ppui.resize(1,1);
break;
}
break;
case SDL_QUIT:
//SDL_PauseAudio(true);
SDL_Quit();
cpui.print_details(filename);
printf("%d frames rendered in %f seconds. (%f FPS)\n",ppui.get_frame(),float(clock())/float(CLOCKS_PER_SEC),float(ppui.get_frame())/(float(clock())/float(CLOCKS_PER_SEC)));
return 0;
break;
default: /* Report an unhandled event */
//printf("I don't know what this event is! Flushing it.\n");
SDL_FlushEvent(event.type);
break;
}
}
if(!paused) {
//cout<<"Running the CPU"<<endl;
int cpu_ret=cpui.run_ops();
//cout<<"Ran the CPU"<<endl;
if(cpu_ret < 0) {
return 1;
}
else {
//cout<<"Frame "<<ppui.get_frame()<<endl;
int apu_id = apui.get_id();
if(apu_id > 0) {
//cout<<"Locking audio...";
SDL_LockAudioDevice(apu_id);
//cout<<"done."<<endl;
apui.set_frame(ppui.get_frame());
apui.gen_audio();
//apui.dat.buffered += 735; Dude, do this in gen_audio, not here =/
//cout<<"Unlocking audio...";
SDL_UnlockAudioDevice(apu_id);
//cout<<"done."<<endl;
}
//cout<<SDL_GetTicks()<<": Frame: "<<ppui.get_frame()<<" Write pos: "<<apui.write_pos<<endl;
//cout<<"Running the PPU"<<endl;
int ppu_ret=ppui.calc();
//cout<<"Ran the PPU"<<endl;
if(ppu_ret>0) {
if(ppu_ret==1) {
//cout<<"Triggering NMI in CPU"<<endl;
cpui.trigger_nmi();
}
}
}
}
//Calculates the time that the frame should end at, and delays until that time
//cout<<"Timing two"<<endl;
int frames = ppui.get_frame();
int now = SDL_GetTicks();
int delay = pstart + int(double(frames) * double(1000) / double(60)) - now;
//cout<<"Frame: "<<frames<<" now: "<<now<<" delay: "<<delay<<endl;
if(delay > 0) {
SDL_Delay(delay);
}
}
return 0;
}
unsigned int get_choice(rom& cart) {
unsigned int choice = 0;
while(choice < 1 || choice > cart.get_song_count()) {
cout<<"Choose a song number between 1 and "<<dec<<cart.get_song_count()<<": ";
cout.flush();
cin>>choice;
}
return choice;
}
bool init_nsf(rom& cart, mem& memi, cpu& cpui, unsigned int choice = 1) {
cout<<"Playing song "<<dec<<choice<<" out of "<<cart.get_song_count()<<endl;
cpui.reset(cart.get_rst_addr());
cpui.set_acc(choice-1);
cpui.set_x(0);
for(int i=0;i<0x800;++i)
memi.write(i,0);
for(int i=0x6000;i<0x8000;++i)
memi.write(i,0);
for(int i=0x4000;i<0x4014;++i)
memi.write(i,0);
memi.write(0x4015, 0x0f);
memi.write(0x4017, 0x40);
cart.reset_map();
int ret = 1;
while(ret) {
ret = cpui.run_next_op();
if(ret < 0) return false;
}
cpui.reset(cart.get_nmi_addr());
cout<<"I think I init'ed the program properly."<<endl;
return true;
}
int run_nsf(rom& cart, apu& apui, mem& memi, cpu& cpui) {
bool paused=false;
int frame = 0;
int choice = cart.get_default_song() + 1; //get_choice(cart);
bool success = init_nsf(cart, memi, cpui, choice);
if(!success) {
cout<<"Failed to init nsf."<<endl;
return 1;
}
SDL_PauseAudioDevice(apui.get_id(), false);
int pstart = SDL_GetTicks();
while (1==1) {
exit_poll_loop:
SDL_Event event;
while(SDL_PollEvent(&event)){ /* Loop until there are no events left on the queue */
switch(event.type){ /* Process the appropiate event type */
case SDL_KEYDOWN: /* Handle a KEYDOWN event */
if(event.key.keysym.scancode==SDL_SCANCODE_Q||
(event.key.keysym.scancode==SDL_SCANCODE_C&&(event.key.keysym.mod==KMOD_RCTRL))||
(event.key.keysym.scancode==SDL_SCANCODE_C&&(event.key.keysym.mod==KMOD_LCTRL))) {
printf("You pressed q or ctrl-c. Exiting.\n");
cpui.print_details("");
//SDL_PauseAudio(true);
cout<<"Calling SDL_Quit()"<<endl;
SDL_Quit();
cout<<"Called SDL_Quit()"<<endl;
return 0;
}
else if(event.key.keysym.scancode==SDL_SCANCODE_A) {
choice--;
if(choice<1) choice = cart.get_song_count();
SDL_PauseAudioDevice(apui.get_id(), true);
init_nsf(cart, memi, cpui, choice);
SDL_PauseAudioDevice(apui.get_id(), false);
goto exit_poll_loop;
}
else if(event.key.keysym.scancode==SDL_SCANCODE_D) {
choice++;
if(choice > (int)cart.get_song_count()) choice = 1;
SDL_PauseAudioDevice(apui.get_id(), true);
init_nsf(cart, memi, cpui, choice);
SDL_PauseAudioDevice(apui.get_id(), false);
goto exit_poll_loop;
}
else if(event.key.keysym.scancode==SDL_SCANCODE_R) {
SDL_PauseAudioDevice(apui.get_id(), true);
init_nsf(cart,memi,cpui, choice);
SDL_PauseAudioDevice(apui.get_id(), false);
goto exit_poll_loop;
}
else if(event.key.keysym.scancode==SDL_SCANCODE_P) {
paused=!paused;
if(paused) {
printf("Paused emulation.\n");
SDL_PauseAudioDevice(apui.get_id(), true);
}
else {
printf("Unpaused emulation.\n");
SDL_PauseAudioDevice(apui.get_id(), false);
}
}
else {
//printf("Keydown\n");
//printf("Time: %d\n",SDL_GetTicks());
}
break;
case SDL_KEYUP: /* Handle a KEYUP event*/
//printf("Keyup\n");
break;
case SDL_QUIT:
//SDL_PauseAudio(true);
SDL_Quit();
cpui.print_details("");
return 0;
break;
default: /* Report an unhandled event */
//printf("I don't know what this event is! Flushing it.\n");
SDL_FlushEvent(event.type);
break;
}
}
if(!paused) {
//cout<<"Running the CPU"<<endl;
int cpu_ret = 1;
int cycle_count = 0;
bool reset = true;
//cout<<"Frame "<<dec<<frame<<" start"<<endl;
while(cpu_ret && cpu_ret > 0) {
cpu_ret=cpui.run_next_op();
cycle_count += (3*cpu_ret);
cpui.set_ppu_cycle(cycle_count);
if(cycle_count >= CLK_PER_FRAME) {
cycle_count -= CLK_PER_FRAME;
reset = false;
break;
}
}
//cout<<"Frame "<<dec<<frame<<" end"<<endl;
if(reset) {
cpui.reset(cart.get_nmi_addr());
}
//cout<<"Ran the CPU"<<endl;
if(cpu_ret < 0) {
return 1;
}
else {
//cout<<"Frame "<<ppui.get_frame()<<endl;
int apu_id = apui.get_id();
if(apu_id > 0) {
//cout<<"Locking audio...";
SDL_LockAudioDevice(apu_id);
//cout<<"done."<<endl;
apui.set_frame(frame);
apui.gen_audio();
//apui.dat.buffered += 735; Dude, do this in gen_audio, not here =/
//cout<<"Unlocking audio...";
SDL_UnlockAudioDevice(apu_id);
//cout<<"done."<<endl;
}
}
}
//Calculates the time that the frame should end at, and delays until that time
//cout<<"Timing two"<<endl;
int now = SDL_GetTicks();
frame++;
cpui.increment_frame();
int delay = pstart + int(double(frame) * double(1000) / double(60)) - now;
//cout<<"Frame: "<<frames<<" now: "<<now<<" delay: "<<delay<<endl;
if(delay > 0) {
SDL_Delay(delay);
}
}
}