-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
459 lines (377 loc) · 10.9 KB
/
main.c
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
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <signal.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include "cpu.h"
#include "mem.h"
#include "panic.h"
#include "ines.h"
#include "ppu.h"
#include "apu.h"
#include "fds.h"
#include "kbd.h"
#include "gui.h"
#include "cli.h"
#include "tas.h"
#define DEBUGGER_KEYBOARD_INJECT_FILE "input.txt"
#define DEBUGGER_CASSETTE_LOAD_FILE "cas.wav"
#define DEBUGGER_CASSETTE_SAVE_FILE "cas.wav"
static cpu_t main_cpu;
static mem_t main_mem;
static ppu_t main_ppu;
static apu_t main_apu;
static fds_t main_fds;
static bool debugger_break = false;
static bool nmi_break = false;
static bool saved_state = false;
static cpu_t save_cpu;
static mem_t save_mem;
static ppu_t save_ppu;
static apu_t save_apu;
static fds_t save_fds;
static bool debugger(void)
{
int i;
char cmd[16];
int result;
fprintf(stdout, "\n");
while (1) {
fprintf(stdout, "%08d:%04x> ", main_ppu.frame_no, main_cpu.pc);
if (fgets(cmd, sizeof(cmd), stdin) == NULL) {
if (feof(stdin)) {
exit(EXIT_SUCCESS);
}
continue;
}
switch (cmd[0]) {
case '?':
case 'h':
fprintf(stdout, "Commands:\n");
fprintf(stdout, " q - Quit\n");
fprintf(stdout, " h - Help\n");
fprintf(stdout, " c - Continue\n");
fprintf(stdout, " n - Continue until next NMI\n");
fprintf(stdout, " s - Step\n");
fprintf(stdout, " w - Warp mode toggle\n");
fprintf(stdout, " 1 - Dump CPU Trace\n");
fprintf(stdout, " 2 - Dump ZP/Stack/Vectors\n");
fprintf(stdout, " 3 - Dump PPU NT/AT/RAM\n");
fprintf(stdout, " 4 - Dump PPU Pattern Tables\n");
fprintf(stdout, " 5 - Dump APU\n");
fprintf(stdout, " 6 - Dump other RAM\n");
fprintf(stdout, " 7 - Dump FDS\n");
fprintf(stdout, "BASIC Mode Commands:\n");
fprintf(stdout, " t - Inject \""
DEBUGGER_KEYBOARD_INJECT_FILE "\" text file as keyboard input.\n");
fprintf(stdout, " l - Load \""
DEBUGGER_CASSETTE_LOAD_FILE "\" cassette WAV file.\n");
fprintf(stdout, " k - Start saving \""
DEBUGGER_CASSETTE_SAVE_FILE "\" cassette WAV file.\n");
fprintf(stdout, " K - Stop saving cassette WAV file.\n");
break;
case 'c': /* Continue */
return false;
case 's': /* Step */
return true;
case 'n': /* Continue until next NMI. */
nmi_break = true;
return false;
case 'w':
if (gui_warp_mode_get()) {
gui_warp_mode_set(false);
fprintf(stdout, "Warp mode disabled.\n");
} else {
gui_warp_mode_set(true);
fprintf(stdout, "Warp mode enabled.\n");
}
break;
case 't':
result = cli_text_inject(DEBUGGER_KEYBOARD_INJECT_FILE);
if (result != 0) {
fprintf(stdout, "Failed to inject text file! Error Code: %d\n",
result);
} else {
fprintf(stdout, "Injecting text file...\n");
}
break;
case 'l':
result = kbd_cassette_load_file(DEBUGGER_CASSETTE_LOAD_FILE);
if (result != 0) {
fprintf(stdout, "Failed to start load WAV file! Code: %d\n",
result);
} else {
fprintf(stdout, "Cassette WAV file playback...\n");
}
break;
case 'k':
result = kbd_cassette_save_file_start(DEBUGGER_CASSETTE_SAVE_FILE);
if (result != 0) {
fprintf(stdout, "Failed to start saving of WAV file! Code: %d\n",
result);
} else {
fprintf(stdout, "Cassette WAV file recording...\n");
}
break;
case 'K':
result = kbd_cassette_save_file_stop();
if (result != 0) {
fprintf(stdout, "Failed to stop saving of WAV file! Code: %d\n",
result);
} else {
fprintf(stdout, "Cassette WAV file recording finished.\n");
}
break;
case 'q': /* Quit */
exit(EXIT_SUCCESS);
break;
case '1':
fprintf(stdout, "CPU Trace:\n");
cpu_trace_dump(stdout);
break;
case '2':
fprintf(stdout, "Zero Page:\n");
mem_dump(stdout, &main_mem, 0, 0xFF);
fprintf(stdout, "\nStack:\n");
mem_dump(stdout, &main_mem, MEM_PAGE_STACK, MEM_PAGE_STACK + 0xFF);
fprintf(stdout, "\nCartridge Vectors:\n");
mem_dump(stdout, &main_mem, 0xFFF0, 0xFFFF);
break;
case '3':
fprintf(stdout, "PPU Dump:\n");
ppu_dump(stdout, &main_ppu);
for (i = 0; i < 4; i++) {
fprintf(stdout, "\nPPU Name Table #%d:\n", i);
ppu_name_table_dump(stdout, &main_ppu, i);
fprintf(stdout, "\nPPU Attribute Table #%d:\n", i);
ppu_attribute_table_dump(stdout, &main_ppu, i);
}
fprintf(stdout, "\nPPU Palette RAM:\n");
ppu_palette_ram_dump(stdout, &main_ppu);
fprintf(stdout, "\nPPU Sprite RAM:\n");
ppu_sprite_ram_dump(stdout, &main_ppu);
break;
case '4':
for (i = 0; i < 256; i++) {
fprintf(stdout, "--- 0:0x%02x\n", i);
ppu_pattern_table_dump(stdout, &main_ppu, 0, i);
}
for (i = 0; i < 256; i++) {
fprintf(stdout, "--- 1:0x%02x\n", i);
ppu_pattern_table_dump(stdout, &main_ppu, 1, i);
}
break;
case '5':
apu_dump(stdout, &main_apu);
break;
case '6':
fprintf(stdout, "Other RAM:\n");
mem_dump(stdout, &main_mem, 0x200, 0x7FF);
break;
case '7':
fds_dump(stdout, &main_fds);
break;
default:
continue;
}
}
}
static void sig_handler(int sig)
{
(void)sig;
debugger_break = true;
}
void panic(const char *format, ...)
{
va_list args;
cli_pause(); /* Turn off to let text appear. */
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
debugger_break = true;
}
void debug(void)
{
debugger_break = true;
}
static void display_help(const char *progname)
{
fprintf(stdout, "Usage: %s <options> [rom]\n", progname);
fprintf(stdout, "Options:\n"
" -h Display this help.\n"
" -d Break into debugger on start.\n"
" -v Disable SDL video.\n"
" -a Disable SDL audio.\n"
" -k Disable terminal output.\n"
" -c Disable terminal colors.\n"
" -j NO Use SDL joystick NO instead of 0.\n"
" -t FILE Use FM2 FILE as input for TAS.\n"
" -f FILE Enable Famicom Disk System and use FILE as FDS BIOS.\n"
" -b Enable BASIC mode with keyboard and data recorder."
"\n");
}
int main(int argc, char *argv[])
{
int c;
char *rom_filename = NULL;
char *tas_filename = NULL;
char *fds_bios_filename = NULL;
bool disable_video = false;
bool disable_audio = false;
bool disable_terminal = false;
bool enable_colors = true;
bool basic_mode = false;
int joystick_no = 0;
while ((c = getopt(argc, argv, "hdvakcj:t:f:b")) != -1) {
switch (c) {
case 'h':
display_help(argv[0]);
return EXIT_SUCCESS;
case 'd':
debugger_break = true;
break;
case 'v':
disable_video = true;
break;
case 'a':
disable_audio = true;
break;
case 'k':
disable_terminal = true;
break;
case 'c':
enable_colors = false;
break;
case 'j':
joystick_no = atoi(optarg);
break;
case 't':
tas_filename = optarg;
break;
case 'f':
fds_bios_filename = optarg;
break;
case 'b':
basic_mode = true;
break;
case '?':
default:
display_help(argv[0]);
return EXIT_FAILURE;
}
}
if (argc <= optind) {
display_help(argv[0]);
return EXIT_FAILURE;
} else {
rom_filename = argv[optind];
}
cpu_trace_init();
signal(SIGINT, sig_handler);
mem_init(&main_mem);
ppu_init(&main_ppu, &main_mem);
apu_init(&main_apu, &main_mem);
kbd_init();
if (fds_bios_filename != NULL) {
/* Famicom Disk System */
fds_init(&main_fds, &main_mem, &main_ppu);
if (fds_bios_load(fds_bios_filename, &main_mem) != 0) {
fprintf(stderr, "Unable to load FDS BIOS: %s\n", fds_bios_filename);
return EXIT_FAILURE;
}
if (fds_image_load(rom_filename, &main_fds) != 0) {
fprintf(stderr, "Unable to load FDS image: %s\n", rom_filename);
return EXIT_FAILURE;
}
} else {
/* Regular ROM */
if (ines_load(rom_filename, &main_mem, &main_ppu) != 0) {
fprintf(stderr, "Unable to load ROM: %s\n", rom_filename);
return EXIT_FAILURE;
}
}
if (tas_filename != NULL) {
if (tas_init(tas_filename) != 0) {
fprintf(stderr, "Failed to load TAS file: %s\n", tas_filename);
return EXIT_FAILURE;
}
}
if (gui_init(joystick_no, disable_video, disable_audio, basic_mode) != 0) {
fprintf(stderr, "Failed to initialize GUI!\n");
return EXIT_FAILURE;
}
if (! disable_terminal) {
if (cli_init(enable_colors, basic_mode) != 0) {
fprintf(stderr, "Failed to initialize CLI!\n");
return EXIT_FAILURE;
}
}
cpu_reset(&main_cpu, &main_mem);
while (1) {
cpu_trace_add(&main_cpu, &main_mem);
/* Let the PPU execute for 2 frames before the CPU starts. */
if (main_ppu.frame_no < 2) {
ppu_execute(&main_ppu);
} else {
cpu_execute(&main_cpu, &main_mem);
}
/* Limit PPU execution to three times per CPU cycle spent. */
while (main_cpu.cycles > 0) {
ppu_execute(&main_ppu);
ppu_execute(&main_ppu);
ppu_execute(&main_ppu);
fds_execute(&main_fds);
kbd_cassette_execute(main_apu.keyboard_cassette_dac,
&main_apu.keyboard_cassette_adc);
main_cpu.cycles--;
}
apu_execute(&main_apu);
/* Trigger pending IRQ from FDS once CPU is ready. */
if (main_fds.trigger_irq && (main_cpu.sr.i == false)) {
cpu_irq(&main_cpu, &main_mem);
main_fds.trigger_irq = false;
}
/* Check for vertical blank NMI from PPU. */
if (main_ppu.trigger_nmi) {
cpu_nmi(&main_cpu, &main_mem);
main_ppu.trigger_nmi = false;
kbd_key_clear();
gui_update();
#ifdef EXTRA_INFO
cli_update(&main_mem, &main_ppu, &main_apu);
#else
cli_update();
#endif
tas_update(main_ppu.frame_no);
if (nmi_break) {
nmi_break = false;
debugger_break = true;
}
if (gui_save_state_requested()) {
memcpy(&save_cpu, &main_cpu, sizeof(cpu_t));
memcpy(&save_mem, &main_mem, sizeof(mem_t));
memcpy(&save_ppu, &main_ppu, sizeof(ppu_t));
memcpy(&save_apu, &main_apu, sizeof(apu_t));
memcpy(&save_fds, &main_fds, sizeof(fds_t));
saved_state = true;
} else if (gui_load_state_requested() && saved_state) {
memcpy(&main_cpu, &save_cpu, sizeof(cpu_t));
memcpy(&main_mem, &save_mem, sizeof(mem_t));
memcpy(&main_ppu, &save_ppu, sizeof(ppu_t));
memcpy(&main_apu, &save_apu, sizeof(apu_t));
memcpy(&main_fds, &save_fds, sizeof(fds_t));
}
}
if (debugger_break) {
cli_pause();
debugger_break = debugger();
if (! debugger_break) {
cli_resume();
}
}
}
return EXIT_SUCCESS;
}