-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathmain.c
585 lines (535 loc) · 12.3 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
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
577
578
579
580
581
582
583
584
585
// Commander X16 Emulator
// Copyright (c) 2019 Michael Steil
// All rights reserved. License: 2-clause BSD
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include "cpu/fake6502.h"
#include "disasm.h"
#include "memory.h"
#include "video.h"
#include "ps2.h"
#include "sdcard.h"
#include "loadsave.h"
#include "glue.h"
#include "debugger.h"
#define MHZ 8
//#define TRACE
#define LOAD_HYPERCALLS
// This must match the KERNAL's set!
char *keymaps[] = {
"en-us",
"en-gb",
"de",
"nordic",
"it",
"pl",
"hu",
"es",
"fr",
"de-ch",
"fr-be",
"pt-br",
};
bool debuger_enabled = false;
char *paste_text = NULL;
char paste_text_data[65536];
bool pasting_bas = false;
bool log_video = false;
bool log_speed = false;
bool log_keyboard = false;
bool echo_mode = false;
bool save_on_exit = true;
uint8_t keymap = 0; // KERNAL's default
int window_scale = 1;
#ifdef TRACE
#include "rom_labels.h"
char *
label_for_address(uint16_t address)
{
for (int i = 0; i < sizeof(addresses) / sizeof(*addresses); i++) {
if (address == addresses[i]) {
return labels[i];
}
}
return NULL;
}
#endif
void
machine_reset()
{
video_reset();
reset6502();
}
void
machine_paste(char *s)
{
if (s) {
paste_text = s;
pasting_bas = true;
}
}
static bool
is_kernal()
{
return ROM[0x3ff6] == 'M' && // only for KERNAL
ROM[0x3ff7] == 'I' &&
ROM[0x3ff8] == 'S' &&
ROM[0x3ff9] == 'T';
}
static void
usage()
{
printf("\nCommander X16 Emulator (C)2019 Michael Steil\n");
printf("All rights reserved. License: 2-clause BSD\n\n");
printf("Usage: x16emu [option] ...\n\n");
printf("-rom <rom.bin>\n");
printf("\tOverride KERNAL/BASIC/* ROM file:\n");
printf("\t$0000-$1FFF bank #0 of banked ROM (BASIC)\n");
printf("\t$2000-$3FFF fixed ROM at $E000-$FFFF (KERNAL)\n");
printf("\t$4000-$5FFF bank #1 of banked ROM\n");
printf("\t$6000-$7FFF bank #2 of banked ROM\n");
printf("\t...\n");
printf("\tThe file needs to be at least $4000 bytes in size.\n");
printf("-char <chargen.bin>\n");
printf("\tOverride character ROM file:\n");
printf("\t$0000-$07FF upper case/graphics\n");
printf("\t$0800-$0FFF lower case\n");
printf("-keymap <keymap>\n");
printf("\tEnable a specific keyboard layout decode table.\n");
printf("-sdcard <sdcard.img>\n");
printf("\tSpecify SD card image (partition map + FAT32)\n");
printf("-prg <app.prg>[,<load_addr>]\n");
printf("\tLoad application from the local disk into RAM\n");
printf("\t(.PRG file with 2 byte start address header)\n");
printf("\tThe override load address is hex without a prefix.\n");
printf("-bas <app.txt>\n");
printf("\tInject a BASIC program in ASCII encoding through the\n");
printf("\tkeyboard.\n");
printf("-run\n");
printf("\tStart the -prg/-bas program using RUN or SYS, depending\n");
printf("\ton the load address.\n");
printf("-echo\n");
printf("\tPrint all KERNAL output to the host's stdout.\n");
printf("\tWith the BASIC statement \"LIST\", this can be used\n");
printf("\tto detokenize a BASIC program.\n");
printf("-log {K|S|V}...\n");
printf("\tEnable logging of (K)eyboard, (S)peed, (V)ideo.\n");
printf("\tMultiple characters are possible, e.g. -log KS\n");
printf("-debug\n");
printf("-scale {1|2|3|4}\n");
printf("\tScale output to an integer multiple of 640x480\n");
printf("\tEnable debugger.\n");
#ifdef TRACE
printf("-trace [<address>]\n");
printf("\tPrint instruction trace. Optionally, a trigger address\n");
printf("\tcan be specified.\n");
#endif
printf("\n");
exit(1);
}
void
usage_keymap()
{
printf("The following keymaps are supported:\n");
for (int i = 0; i < sizeof(keymaps)/sizeof(*keymaps); i++) {
printf("\t%s\n", keymaps[i]);
}
exit(1);
}
int
main(int argc, char **argv)
{
#ifdef TRACE
bool trace_mode = false;
uint16_t trace_address = 0;
#endif
char *rom_filename = "/rom.bin";
char *char_filename = "/chargen.bin";
char rom_path_data[PATH_MAX];
char char_path_data[PATH_MAX];
char *rom_path = rom_path_data;
char *char_path = char_path_data;
char *prg_path = NULL;
char *bas_path = NULL;
char *sdcard_path = NULL;
bool run_after_load = false;
#ifdef __APPLE__
// on macOS, double clicking runs an executable in the user's
// home directory, so we prepend the executable's path to
// the rom filenames
if (argv[0][0] == '/') {
*strrchr(argv[0], '/') = 0;
strncpy(rom_path, argv[0], PATH_MAX);
strncpy(rom_path + strlen(rom_path), rom_filename, PATH_MAX - strlen(rom_path));
strncpy(char_path, argv[0], PATH_MAX);
strncpy(char_path + strlen(char_path), char_filename, PATH_MAX - strlen(char_path));
} else
#endif
{
strncpy(rom_path, rom_filename + 1, PATH_MAX);
strncpy(char_path, char_filename + 1, PATH_MAX);
}
argc--;
argv++;
while (argc > 0) {
if (!strcmp(argv[0], "-rom")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
rom_path = argv[0];
argc--;
argv++;
} else if (!strcmp(argv[0], "-char")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
char_path = argv[0];
argc--;
argv++;
} else if (!strcmp(argv[0], "-keymap")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage_keymap();
}
bool found = false;
for (int i = 0; i < sizeof(keymaps)/sizeof(*keymaps); i++) {
if (!strcmp(argv[0], keymaps[i])) {
found = true;
keymap = i;
}
}
if (!found) {
usage_keymap();
}
argc--;
argv++;
} else if (!strcmp(argv[0], "-prg")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
prg_path = argv[0];
argc--;
argv++;
} else if (!strcmp(argv[0], "-run")) {
argc--;
argv++;
run_after_load = true;
} else if (!strcmp(argv[0], "-bas")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
bas_path = argv[0];
argc--;
argv++;
} else if (!strcmp(argv[0], "-sdcard")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
sdcard_path = argv[0];
argc--;
argv++;
} else if (!strcmp(argv[0], "-echo")) {
argc--;
argv++;
echo_mode = true;
} else if (!strcmp(argv[0], "-log")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
for (char *p = argv[0]; *p; p++) {
switch (tolower(*p)) {
case 'k':
log_keyboard = true;
break;
case 's':
log_speed = true;
break;
case 'v':
log_video = true;
break;
default:
usage();
}
}
argc--;
argv++;
} else if (!strcmp(argv[0], "-debug")) {
argc--;
argv++;
debuger_enabled = true;
#ifdef TRACE
} else if (!strcmp(argv[0], "-trace")) {
argc--;
argv++;
if (argc && argv[0][0] != '-') {
trace_mode = false;
trace_address = (uint16_t)strtol(argv[0], NULL, 16);
argc--;
argv++;
} else {
trace_mode = true;
trace_address = 0;
}
#endif
} else if (!strcmp(argv[0], "-scale")) {
argc--;
argv++;
if(!argc || argv[0][0] == '-') {
usage();
}
for(char *p = argv[0]; *p; p++) {
switch(tolower(*p)) {
case '1':
window_scale = 1;
break;
case '2':
window_scale = 2;
break;
case '3':
window_scale = 3;
break;
case '4':
window_scale = 4;
break;
default:
usage();
}
}
argc--;
argv++;
} else {
usage();
}
}
FILE *f = fopen(rom_path, "rb");
if (!f) {
printf("Cannot open %s!\n", rom_path);
exit(1);
}
int rom_size = fread(ROM, 1, ROM_SIZE, f);
(void)rom_size;
fclose(f);
f = fopen(char_path, "rb");
if (!f) {
printf("Cannot open %s!\n", char_path);
exit(1);
}
uint8_t chargen[4096];
int chargen_size = fread(chargen, 1, sizeof(chargen), f);
(void)chargen_size;
fclose(f);
if (sdcard_path) {
sdcard_file = fopen(sdcard_path, "rb");
if (!sdcard_file) {
printf("Cannot open %s!\n", sdcard_path);
exit(1);
}
}
FILE *prg_file = NULL;
int prg_override_start = -1;
if (prg_path) {
char *comma = strchr(prg_path, ',');
if (comma) {
prg_override_start = (uint16_t)strtol(comma + 1, NULL, 16);
*comma = 0;
}
prg_file = fopen(prg_path, "rb");
if (!prg_file) {
printf("Cannot open %s!\n", prg_path);
exit(1);
}
}
if (bas_path) {
FILE *bas_file = fopen(bas_path, "r");
if (!bas_file) {
printf("Cannot open %s!\n", bas_path);
exit(1);
}
paste_text = paste_text_data;
size_t paste_size = fread(paste_text, 1, sizeof(paste_text_data) - 1, bas_file);
if (run_after_load) {
strncpy(paste_text + paste_size, "\rRUN\r", sizeof(paste_text_data) - paste_size);
} else {
paste_text[paste_size] = 0;
}
fclose(bas_file);
}
video_init(chargen, window_scale);
sdcard_init();
machine_reset();
int instruction_counter = 0;
for (;;) {
if (debuger_enabled) {
int dbgCmd = DEBUGGetCurrentStatus();
if (dbgCmd > 0) continue;
if (dbgCmd < 0) break;
}
#ifdef TRACE
if (pc == trace_address && trace_address != 0) {
trace_mode = true;
}
if (trace_mode) {
printf("\t\t\t\t[%6d] ", instruction_counter);
char *label = label_for_address(pc);
int label_len = label ? strlen(label) : 0;
if (label) {
printf("%s", label);
}
for (int i = 0; i < 10 - label_len; i++) {
printf(" ");
}
printf(" .,%04x ", pc);
char disasm_line[15];
int len = disasm(pc, RAM, disasm_line, sizeof(disasm_line));
for (int i = 0; i < len; i++) {
printf("%02x ", read6502(pc + i));
}
for (int i = 0; i < 9 - 3 * len; i++) {
printf(" ");
}
printf("%s", disasm_line);
for (int i = 0; i < 15 - strlen(disasm_line); i++) {
printf(" ");
}
printf("a=$%02x x=$%02x y=$%02x s=$%02x p=", a, x, y, sp);
for (int i = 7; i >= 0; i--) {
printf("%c", (status & (1 << i)) ? "czidb.vn"[i] : '-');
}
// printf(" --- %04x", RAM[0xae] | RAM[0xaf] << 8);
printf("\n");
}
#endif
#ifdef LOAD_HYPERCALLS
if ((pc == 0xffd5 || pc == 0xffd8) && is_kernal() && RAM[0xba] == 1) {
if (pc == 0xffd5) {
LOAD();
} else {
SAVE();
}
pc = (RAM[0x100 + sp + 1] | (RAM[0x100 + sp + 2] << 8)) + 1;
sp += 2;
}
#endif
uint32_t old_clockticks6502 = clockticks6502;
step6502();
uint8_t clocks = clockticks6502 - old_clockticks6502;
bool new_frame = false;
for (uint8_t i = 0; i < clocks; i++) {
ps2_step();
sdcard_step();
new_frame |= video_step(MHZ);
}
instruction_counter++;
if (new_frame) {
if (!video_update()) {
break;
}
static int frames = 0;
frames++;
int32_t diff_time = 1000 * frames / 60 - SDL_GetTicks();
if (diff_time > 0) {
usleep(1000 * diff_time);
}
if (log_speed) {
float frames_behind = -((float)diff_time / 16.666666);
int load = (int)((1 + frames_behind) * 100);
printf("Load: %d%%\n", load > 100 ? 100 : load);
if ((int)frames_behind > 0) {
printf("Rendering is behind %d frames.\n", -(int)frames_behind);
} else {
}
}
}
if (video_get_irq_out()) {
if (!(status & 4)) {
irq6502();
}
}
#if 0
if (clockticks6502 >= 5 * MHZ * 1000 * 1000) {
break;
}
#endif
if (pc == 0xffff) {
if (save_on_exit) {
memory_save();
}
break;
}
if (echo_mode && pc == 0xffd2 && is_kernal()) {
uint8_t c = a;
if (c == 13) {
c = 10;
}
printf("%c", c);
}
if (pc == 0xffcf && is_kernal()) {
// as soon as BASIC starts reading a line...
if (prg_file) {
// ...inject the app into RAM
uint8_t start_lo = fgetc(prg_file);
uint8_t start_hi = fgetc(prg_file);
uint16_t start;
if (prg_override_start >= 0) {
start = prg_override_start;
} else {
start = start_hi << 8 | start_lo;
}
uint16_t end = start + fread(RAM + start, 1, 65536-start, prg_file);
fclose(prg_file);
prg_file = NULL;
if (start == 0x0801) {
// set start of variables
RAM[0x2d] = end & 0xff;
RAM[0x2e] = end >> 8;
}
if (run_after_load) {
if (start == 0x0801) {
paste_text = "RUN\r";
} else {
paste_text = paste_text_data;
snprintf(paste_text, sizeof(paste_text_data), "SYS$%04x\r", start);
}
}
}
if (paste_text) {
// ...paste BASIC code into the keyboard buffer
pasting_bas = true;
}
}
while (pasting_bas && RAM[0xc6] < 10) {
uint8_t c = *paste_text;
if (c) {
if (c == 10) {
c = 13;
}
RAM[0x0277 + RAM[0xc6]] = c;
RAM[0xc6]++;
paste_text++;
} else {
pasting_bas = false;
paste_text = NULL;
}
}
}
video_end();
return 0;
}