forked from commanderx16/x16-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1164 lines (1072 loc) · 24.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
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Commander X16 Emulator
// Copyright (c) 2019 Michael Steil
// All rights reserved. License: 2-clause BSD
#ifndef __APPLE__
#define _XOPEN_SOURCE 600
#define _POSIX_C_SOURCE 1
#endif
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#ifdef __MINGW32__
#include <ctype.h>
#endif
#include "cpu/fake6502.h"
#include "disasm.h"
#include "memory.h"
#include "video.h"
#include "via.h"
#include "ps2.h"
#include "spi.h"
#include "vera_spi.h"
#include "sdcard.h"
#include "loadsave.h"
#include "glue.h"
#include "debugger.h"
#include "utf8.h"
#include "joystick.h"
#include "utf8_encode.h"
#include "rom_symbols.h"
#include "ym2151.h"
#include "audio.h"
#include "version.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <pthread.h>
#endif
void *emulator_loop(void *param);
void emscripten_main_loop(void);
// 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",
};
#ifdef PERFSTAT
uint32_t stat[65536];
#endif
bool debugger_enabled = false;
char *paste_text = NULL;
char paste_text_data[65536];
bool pasting_bas = false;
char *keybuf = NULL;
uint16_t num_ram_banks = 64; // 512 KB default
bool log_video = false;
bool log_speed = false;
bool log_keyboard = false;
bool dump_cpu = false;
bool dump_ram = true;
bool dump_bank = true;
bool dump_vram = false;
bool warp_mode = false;
echo_mode_t echo_mode;
bool save_on_exit = true;
gif_recorder_state_t record_gif = RECORD_GIF_DISABLED;
char *gif_path = NULL;
uint8_t keymap = 0; // KERNAL's default
int window_scale = 1;
char *scale_quality = "best";
int frames;
int32_t sdlTicks_base;
int32_t last_perf_update;
int32_t perf_frame_count;
char window_title[30];
#ifdef TRACE
bool trace_mode = false;
uint16_t trace_address = 0;
#endif
int instruction_counter;
SDL_RWops *prg_file ;
int prg_override_start = -1;
bool run_after_load = false;
#ifdef TRACE
#include "rom_labels.h"
char *
label_for_address(uint16_t address)
{
uint16_t *addresses;
char **labels;
int count;
switch (memory_get_rom_bank()) {
case 0:
addresses = addresses_bank0;
labels = labels_bank0;
count = sizeof(addresses_bank0) / sizeof(uint16_t);
break;
case 1:
addresses = addresses_bank1;
labels = labels_bank1;
count = sizeof(addresses_bank1) / sizeof(uint16_t);
break;
case 2:
addresses = addresses_bank2;
labels = labels_bank2;
count = sizeof(addresses_bank2) / sizeof(uint16_t);
break;
case 3:
addresses = addresses_bank3;
labels = labels_bank3;
count = sizeof(addresses_bank3) / sizeof(uint16_t);
break;
case 4:
addresses = addresses_bank4;
labels = labels_bank4;
count = sizeof(addresses_bank4) / sizeof(uint16_t);
break;
case 5:
addresses = addresses_bank5;
labels = labels_bank5;
count = sizeof(addresses_bank5) / sizeof(uint16_t);
break;
case 6:
addresses = addresses_bank6;
labels = labels_bank6;
count = sizeof(addresses_bank6) / sizeof(uint16_t);
break;
#if 0
case 7:
addresses = addresses_bank7;
labels = labels_bank7;
count = sizeof(addresses_bank7) / sizeof(uint16_t);
break;
#endif
default:
addresses = NULL;
labels = NULL;
}
if (!addresses) {
return NULL;
}
for (int i = 0; i < count; i++) {
if (address == addresses[i]) {
return labels[i];
}
}
return NULL;
}
#endif
void
machine_dump()
{
int index = 0;
char filename[22];
for (;;) {
if (!index) {
strcpy(filename, "dump.bin");
} else {
sprintf(filename, "dump-%i.bin", index);
}
if (access(filename, F_OK) == -1) {
break;
}
index++;
}
SDL_RWops *f = SDL_RWFromFile(filename, "wb");
if (!f) {
printf("Cannot write to %s!\n", filename);
return;
}
if (dump_cpu) {
SDL_RWwrite(f, &a, sizeof(uint8_t), 1);
SDL_RWwrite(f, &x, sizeof(uint8_t), 1);
SDL_RWwrite(f, &y, sizeof(uint8_t), 1);
SDL_RWwrite(f, &sp, sizeof(uint8_t), 1);
SDL_RWwrite(f, &status, sizeof(uint8_t), 1);
SDL_RWwrite(f, &pc, sizeof(uint16_t), 1);
}
memory_save(f, dump_ram, dump_bank);
if (dump_vram) {
video_save(f);
}
SDL_RWclose(f);
printf("Dumped system to %s.\n", filename);
}
void
machine_reset()
{
vera_spi_init();
via1_init();
via2_init();
video_reset();
reset6502();
}
void
machine_paste(char *s)
{
if (s) {
paste_text = s;
pasting_bas = true;
}
}
void
timing_init() {
frames = 0;
sdlTicks_base = SDL_GetTicks();
last_perf_update = 0;
perf_frame_count = 0;
}
void
timing_update()
{
frames++;
int32_t sdlTicks = SDL_GetTicks() - sdlTicks_base;
int32_t diff_time = 1000 * frames / 60 - sdlTicks;
if (!warp_mode && diff_time > 0) {
usleep(1000 * diff_time);
}
if (sdlTicks - last_perf_update > 5000) {
int32_t frameCount = frames - perf_frame_count;
int perf = frameCount / 3;
if (perf < 100 || warp_mode) {
sprintf(window_title, "Commander X16 (%d%%)", perf);
video_update_title(window_title);
} else {
video_update_title("Commander X16");
}
perf_frame_count = frames;
last_perf_update = sdlTicks;
}
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 {
}
}
}
void
machine_toggle_warp()
{
warp_mode = !warp_mode;
timing_init();
}
uint8_t
iso8859_15_from_unicode(uint32_t c)
{
// line feed -> carriage return
if (c == '\n') {
return '\r';
}
// translate Unicode characters not part of Latin-1 but part of Latin-15
switch (c) {
case 0x20ac: // '€'
return 0xa4;
case 0x160: // 'Š'
return 0xa6;
case 0x161: // 'š'
return 0xa8;
case 0x17d: // 'Ž'
return 0xb4;
case 0x17e: // 'ž'
return 0xb8;
case 0x152: // 'Œ'
return 0xbc;
case 0x153: // 'œ'
return 0xbd;
case 0x178: // 'Ÿ'
return 0xbe;
}
// remove Unicode characters part of Latin-1 but not part of Latin-15
switch (c) {
case 0xa4: // '¤'
case 0xa6: // '¦'
case 0xa8: // '¨'
case 0xb4: // '´'
case 0xb8: // '¸'
case 0xbc: // '¼'
case 0xbd: // '½'
case 0xbe: // '¾'
return '?';
}
// all other Unicode characters are also unsupported
if (c >= 256) {
return '?';
}
// everything else is Latin-15 already
return c;
}
uint32_t
unicode_from_iso8859_15(uint8_t c)
{
// translate Latin-15 characters not part of Latin-1
switch (c) {
case 0xa4:
return 0x20ac; // '€'
case 0xa6:
return 0x160; // 'Š'
case 0xa8:
return 0x161; // 'š'
case 0xb4:
return 0x17d; // 'Ž'
case 0xb8:
return 0x17e; // 'ž'
case 0xbc:
return 0x152; // 'Œ'
case 0xbd:
return 0x153; // 'œ'
case 0xbe:
return 0x178; // 'Ÿ'
default:
return c;
}
}
// converts the character to UTF-8 and prints it
static void
print_iso8859_15_char(char c)
{
char utf8[5];
utf8_encode(utf8, unicode_from_iso8859_15(c));
printf("%s", utf8);
}
static bool
is_kernal()
{
return read6502(0xfff6) == 'M' && // only for KERNAL
read6502(0xfff7) == 'I' &&
read6502(0xfff8) == 'S' &&
read6502(0xfff9) == 'T';
}
static void
usage()
{
printf("\nCommander X16 Emulator r%s (%s)\n", VER, VER_NAME);
printf("(C)2019,2020 Michael Steil et al.\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("-ram <ramsize>\n");
printf("\tSpecify banked RAM size in KB (8, 16, 32, ..., 2048).\n");
printf("\tThe default is 512.\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("-keybuf <some-text>\n");
printf("\tInject <some-text> into the keyboard buffer after loading\n");
printf("\tand running a program with -prg/-bas and -run.\n");
printf("\tEnter control characters in hex with \\X, e.g. \\X0D for CR.\n");
printf("-geos\n");
printf("\tLaunch GEOS at startup.\n");
printf("-warp\n");
printf("\tEnable warp mode, run emulator as fast as possible.\n");
printf("-echo [{iso|raw}]\n");
printf("\tPrint all KERNAL output to the host's stdout.\n");
printf("\tBy default, everything but printable ASCII characters get\n");
printf("\tescaped. \"iso\" will escape everything but non-printable\n");
printf("\tISO-8859-1 characters and convert the output to UTF-8.\n");
printf("\t\"raw\" will not do any substitutions.\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("-gif <file.gif>[,wait]\n");
printf("\tRecord a gif for the video output.\n");
printf("\tUse ,wait to start paused.\n");
printf("\tPOKE $9FB5,2 to start recording.\n");
printf("\tPOKE $9FB5,1 to capture a single frame.\n");
printf("\tPOKE $9FB5,0 to pause.\n");
printf("-scale {1|2|3|4}\n");
printf("\tScale output to an integer multiple of 640x480\n");
printf("-quality {nearest|linear|best}\n");
printf("\tScaling algorithm quality\n");
printf("-debug [<address>]\n");
printf("\tEnable debugger. Optionally, set a breakpoint\n");
printf("-dump {C|R|B|V}...\n");
printf("\tConfigure system dump: (C)PU, (R)AM, (B)anked-RAM, (V)RAM\n");
printf("\tMultiple characters are possible, e.g. -dump CV ; Default: RB\n");
printf("-joy1 {NES | SNES}\n");
printf("\tChoose what type of joystick to use, e.g. -joy1 SNES\n");
printf("-joy2 {NES | SNES}\n");
printf("\tChoose what type of joystick to use, e.g. -joy2 SNES\n");
printf("-sound <output device>\n");
printf("\tSet the output device used for audio emulation\n");
printf("-abufs <number of audio buffers>\n");
printf("\tSet the number of audio buffers used for playback. (default: 8)\n");
printf("\tIncreasing this will reduce stutter on slower computers,\n");
printf("\tbut will increase audio latency.\n");
#ifdef TRACE
printf("-trace [<address>]\n");
printf("\tPrint instruction trace. Optionally, a trigger address\n");
printf("\tcan be specified.\n");
#endif
printf("-version\n");
printf("\tPrint additional version information the emulator and ROM.\n");
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)
{
char *rom_filename = "rom.bin";
char rom_path_data[PATH_MAX];
char *rom_path = rom_path_data;
char *prg_path = NULL;
char *bas_path = NULL;
char *sdcard_path = NULL;
bool run_geos = false;
bool run_test = false;
int test_number = 0;
int audio_buffers = 8;
const char *audio_dev_name = NULL;
run_after_load = false;
char *base_path = SDL_GetBasePath();
// This causes the emulator to load ROM data from the executable's directory when
// no ROM file is specified on the command line.
memcpy(rom_path, base_path, strlen(base_path) + 1);
strncpy(rom_path + strlen(rom_path), rom_filename, PATH_MAX - strlen(rom_path));
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], "-ram")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
int kb = atoi(argv[0]);
bool found = false;
for (int cmp = 8; cmp <= 2048; cmp *= 2) {
if (kb == cmp) {
found = true;
}
}
if (!found) {
usage();
}
num_ram_banks = kb /8;
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], "-keybuf")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
keybuf = argv[0];
argc--;
argv++;
} 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], "-geos")) {
argc--;
argv++;
run_geos = true;
} else if (!strcmp(argv[0], "-test")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
test_number = atoi(argv[0]);
run_test = true;
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], "-warp")) {
argc--;
argv++;
warp_mode = true;
} else if (!strcmp(argv[0], "-echo")) {
argc--;
argv++;
if (argc && argv[0][0] != '-') {
if (!strcmp(argv[0], "raw")) {
echo_mode = ECHO_MODE_RAW;
} else if (!strcmp(argv[0], "iso")) {
echo_mode = ECHO_MODE_ISO;
} else {
usage();
}
argc--;
argv++;
} else {
echo_mode = ECHO_MODE_COOKED;
}
} 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], "-dump")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
dump_cpu = false;
dump_ram = false;
dump_bank = false;
dump_vram = false;
for (char *p = argv[0]; *p; p++) {
switch (tolower(*p)) {
case 'c':
dump_cpu = true;
break;
case 'r':
dump_ram = true;
break;
case 'b':
dump_bank = true;
break;
case 'v':
dump_vram = true;
break;
default:
usage();
}
}
argc--;
argv++;
} else if (!strcmp(argv[0], "-gif")) {
argc--;
argv++;
// set up for recording
record_gif = RECORD_GIF_PAUSED;
if (!argc || argv[0][0] == '-') {
usage();
}
gif_path = argv[0];
argv++;
argc--;
} else if (!strcmp(argv[0], "-debug")) {
argc--;
argv++;
debugger_enabled = true;
if (argc && argv[0][0] != '-') {
DEBUGSetBreakPoint((uint16_t)strtol(argv[0], NULL, 16));
argc--;
argv++;
}
} else if (!strcmp(argv[0], "-joy1")) {
argc--;
argv++;
if (!strcmp(argv[0], "NES")) {
joy1_mode = NES;
argc--;
argv++;
} else if (!strcmp(argv[0], "SNES")) {
joy1_mode = SNES;
argc--;
argv++;
}
} else if (!strcmp(argv[0], "-joy2")){
argc--;
argv++;
if (!strcmp(argv[0], "NES")){
joy2_mode = NES;
argc--;
argv++;
} else if (!strcmp(argv[0], "SNES")){
joy2_mode = SNES;
argc--;
argv++;
}
#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 if (!strcmp(argv[0], "-quality")) {
argc--;
argv++;
if(!argc || argv[0][0] == '-') {
usage();
}
if (!strcmp(argv[0], "nearest") ||
!strcmp(argv[0], "linear") ||
!strcmp(argv[0], "best")) {
scale_quality = argv[0];
} else {
usage();
}
argc--;
argv++;
} else if (!strcmp(argv[0], "-sound")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
audio_usage();
}
audio_dev_name = argv[0];
argc--;
argv++;
} else if (!strcmp(argv[0], "-abufs")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
audio_buffers = (int)strtol(argv[0], NULL, 10);
argc--;
argv++;
} else if (!strcmp(argv[0], "-version")){
printf("%s", VER_INFO);
argc--;
argv++;
exit(0);
} else {
usage();
}
}
SDL_RWops *f = SDL_RWFromFile(rom_path, "rb");
if (!f) {
printf("Cannot open %s!\n", rom_path);
exit(1);
}
size_t rom_size = SDL_RWread(f, ROM, ROM_SIZE, 1);
(void)rom_size;
SDL_RWclose(f);
if (sdcard_path) {
sdcard_file = SDL_RWFromFile(sdcard_path, "r+b");
if (!sdcard_file) {
printf("Cannot open %s!\n", sdcard_path);
exit(1);
}
sdcard_attach();
}
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 = SDL_RWFromFile(prg_path, "rb");
if (!prg_file) {
printf("Cannot open %s!\n", prg_path);
exit(1);
}
}
if (bas_path) {
SDL_RWops *bas_file = SDL_RWFromFile(bas_path, "r");
if (!bas_file) {
printf("Cannot open %s!\n", bas_path);
exit(1);
}
paste_text = paste_text_data;
size_t paste_size = SDL_RWread(bas_file, paste_text, 1, sizeof(paste_text_data) - 1);
if (run_after_load) {
strncpy(paste_text + paste_size, "\rRUN\r", sizeof(paste_text_data) - paste_size);
if (keybuf) {
paste_size = strlen(paste_text);
strncpy(paste_text + paste_size, keybuf, sizeof(paste_text_data) - paste_size);
}
} else {
paste_text[paste_size] = 0;
}
SDL_RWclose(bas_file);
}
if (run_geos) {
paste_text = "GEOS\r";
}
if (run_test) {
paste_text = paste_text_data;
snprintf(paste_text, sizeof(paste_text_data), "TEST %d\r", test_number);
}
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO);
audio_init(audio_dev_name, audio_buffers);
memory_init();
video_init(window_scale, scale_quality);
joystick_init();
machine_reset();
timing_init();
instruction_counter = 0;
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(emscripten_main_loop, 0, 1);
#else
emulator_loop(NULL);
#endif
audio_close();
video_end();
SDL_Quit();
#ifdef PERFSTAT
for (int pc = 0xc000; pc < sizeof(stat)/sizeof(*stat); pc++) {
if (stat[pc] == 0) {
continue;
}
char *label = label_for_address(pc);
if (!label) {
continue;
}
char *original_label = label;
uint16_t pc2 = pc;
if (label[0] == '@') {
label = NULL;
while (!label || label[0] == '@') {
pc2--;
label = label_for_address(pc2);
}
}
printf("%d\t $%04X %s+%d", stat[pc], pc, label, pc-pc2);
if (pc-pc2 != 0) {
printf(" (%s)", original_label);
}
printf("\n");
}
#endif
return 0;
}
void
emscripten_main_loop(void) {
emulator_loop(NULL);
}
void*
emulator_loop(void *param)
{
for (;;) {
if (debugger_enabled) {
int dbgCmd = DEBUGGetCurrentStatus();
if (dbgCmd > 0) continue;
if (dbgCmd < 0) break;
}
#ifdef PERFSTAT
// if (memory_get_rom_bank() == 3) {
// stat[pc]++;
// }
if (memory_get_rom_bank() == 3) {
static uint8_t old_sp;
static uint16_t base_pc;
if (sp < old_sp) {
base_pc = pc;
}
old_sp = sp;
stat[base_pc]++;
}
#endif
#ifdef TRACE
if (pc == trace_address && trace_address != 0) {
trace_mode = true;
}
if (trace_mode) {
//printf("\t\t\t\t");
printf("[%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 < 20 - label_len; i++) {
printf(" ");
}
printf(" %02x:.,%04x ", memory_get_rom_bank(), pc);
char disasm_line[15];
int len = disasm(pc, RAM, disasm_line, sizeof(disasm_line), false, 0);
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] : '-');
}
#if 0
printf(" ---");
for (int i = 0; i < 6; i++) {
printf(" r%i:%04x", i, RAM[2 + i*2] | RAM[3 + i*2] << 8);
}
for (int i = 14; i < 16; i++) {
printf(" r%i:%04x", i, RAM[2 + i*2] | RAM[3 + i*2] << 8);
}
printf(" RAM:%01x", memory_get_ram_bank());
printf(" px:%d py:%d", RAM[0xa0e8] | RAM[0xa0e9] << 8, RAM[0xa0ea] | RAM[0xa0eb] << 8);
// printf(" c:%d", RAM[0xa0e2]);
// printf("-");
// for (int i = 0; i < 10; i++) {