-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.c
1554 lines (1443 loc) · 35.1 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,2022 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 "timing.h"
#include "disasm.h"
#include "files.h"
#include "memory.h"
#include "video.h"
#include "via.h"
#include "serial.h"
#include "i2c.h"
#include "rtc.h"
#include "vera_spi.h"
#include "sdcard.h"
#include "ieee.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"
#include "wav_recorder.h"
#include "testbench.h"
#include "cartridge.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-us-int",
"en-gb",
"sv",
"de",
"da",
"it",
"pl",
"nb",
"hu",
"es",
"fi",
"pt-br",
"cz",
"jp",
"fr",
"de-ch",
"en-us-dvo",
"et",
"fr-be",
"fr-ca",
"is",
"pt",
"hr",
"sk",
"sl",
"lv",
"lt",
};
#ifdef PERFSTAT
uint32_t stat[65536];
#endif
bool debugger_enabled = false;
char *paste_text = NULL;
char paste_text_data[65536];
bool pasting_bas = false;
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;
bool disable_emu_cmd_keys = false;
bool set_system_time = false;
bool has_serial = false;
bool no_ieee_intercept = false;
bool has_via2 = false;
gif_recorder_state_t record_gif = RECORD_GIF_DISABLED;
char *gif_path = NULL;
char *wav_path = NULL;
char *fsroot_path = NULL;
char *startin_path = NULL;
uint8_t keymap = 0; // KERNAL's default
int window_scale = 1;
float screen_x_scale = 1.0;
char *scale_quality = "best";
bool test_init_complete=false;
bool headless = false;
bool testbench = false;
uint8_t MHZ = 8;
#ifdef TRACE
bool trace_mode = false;
uint16_t trace_address = 0;
#endif
int instruction_counter;
SDL_RWops *prg_file;
bool prg_finished_loading;
int prg_override_start = -1;
bool run_after_load = false;
char *nvram_path = NULL;
#ifdef TRACE
#include "rom_labels.h"
#include "rom_lst.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;
}
char *
lst_for_address(uint16_t address)
{
if (address < 0xc000) {
return NULL;
}
char **lst;
switch (memory_get_rom_bank()) {
case 0: lst = lst_bank0; break;
case 2: lst = lst_bank2; break;
case 3: lst = lst_bank3; break;
case 4: lst = lst_bank4; break;
case 5: lst = lst_bank5; break;
default:
return NULL;
}
return lst[address - 0xc000];
}
#endif
void
machine_dump(const char* reason)
{
printf("Dumping system memory. Reason: %s\n", reason);
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 mouse_state_init(void)
{
kernal_mouse_enabled = 0;
SDL_ShowCursor((mouse_grabbed || kernal_mouse_enabled) ? SDL_DISABLE : SDL_ENABLE);
}
void
machine_reset()
{
ieee_init();
memory_reset();
vera_spi_init();
via1_init();
if (has_via2) {
via2_init();
}
video_reset();
mouse_state_init();
reset6502();
}
void
machine_paste(char *s)
{
if (s) {
paste_text = s;
pasting_bas = true;
}
}
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)", VER, VER_NAME);
#ifdef GIT_REV
printf(", "GIT_REV);
#endif
printf("\n(C)2019, 2023 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("-nvram <nvram.bin>\n");
printf("\tSpecify NVRAM image. By default, the machine starts with\n");
printf("\tempty NVRAM and does not save it to disk.\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("-cart <crtfile.crt>\n");
printf("\tLoads a specially-formatted cartridge file.\n");
printf("-cartbin <romfile.bin>\n");
printf("\tLoads a raw cartridge file starting at ROM bank 32. After\n");
printf("\tloading, all of the affected banks will function as RAM.\n");
printf("-serial\n");
printf("\tConnect host fs through Serial Bus [experimental]\n");
printf("-nohostieee\n");
printf("\tDisable host fs through IEEE API interception.\n");
printf("\tIEEE API host fs is normally enabled unless -sdcard or\n");
printf("\t-serial is specified.\n");
printf("-fsroot <directory>\n");
printf("\tSpecify the host filesystem directory path which is to\n");
printf("\tact as the emulated root directory of the Commander X16.\n");
printf("\tDefault is the current working directory.\n");
printf("-startin <directory>\n");
printf("\tSpecify the host filesystem directory path that the\n");
printf("\temulated filesystem starts in. Default is the current\n");
printf("\tworking directory if it lies within the hierarchy of fsroot,\n");
printf("\totherwise it defaults to fsroot itself.\n");
printf("-noemucmdkeys\n");
printf("\tDisable emulator command keys.\n");
printf("-prg <app.prg>[,<load_addr>]\n");
printf("\tLoad application from the *host filesystem* into RAM,\n");
printf("\teven if an SD card is attached.\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\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-15 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("-wav <file.wav>[{,wait|,auto}]\n");
printf("\tRecord a wav for the audio output.\n");
printf("\tUse ,wait to start paused, or ,auto to start paused and automatically begin recording on the first non-zero audio signal.\n");
printf("\tPOKE $9FB6,2 to automatically begin recording on the first non-zero audio signal.\n");
printf("\tPOKE $9FB6,1 to begin recording immediately.\n");
printf("\tPOKE $9FB6,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("-widescreen\n");
printf("\tStretch output to 16:9 resolution to mimic display of a widescreen monitor.\n");
printf("-debug [<address>]\n");
printf("\tEnable debugger. Optionally, set a breakpoint\n");
printf("-randram\n");
printf("\t(deprecated, no effect)\n");
printf("-zeroram\n");
printf("\tSet all RAM to zero instead of uninitialized random values\n");
printf("-wuninit\n");
printf("\tPrints warning to stdout if uninitialized RAM is accessed\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\n");
printf("\tEnable binding a gamepad to SNES controller port 1\n");
printf("-joy2\n");
printf("\tEnable binding a gamepad to SNES controller port 2\n");
printf("-joy3\n");
printf("\tEnable binding a gamepad to SNES controller port 3\n");
printf("-joy4\n");
printf("\tEnable binding a gamepad to SNES controller port 4\n");
printf("-sound <output device>\n");
printf("\tSet the output device used for audio emulation\n");
printf("\tIf output device is 'none', no audio is generated\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");
printf("-rtc\n");
printf("\tSet the real-time-clock to the current system time and date.\n");
printf("-via2\n");
printf("\tInstall the second VIA chip expansion at $9F10\n");
printf("-testbench\n");
printf("\tHeadless mode for unit testing with an external test runner\n");
printf("-mhz <integer>\n");
printf("\tRun the emulator with a system clock speed other than the default of\n");
printf("\t8 MHz. Valid values are in the range of 1-40, inclusive. This option\n");
printf("\tis meant mainly for benchmarking, and may not reflect accurate\n");
printf("\thardware behavior.\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 of 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;
char *cartridge_path = NULL;
bool run_geos = false;
bool run_test = false;
int test_number = 0;
int audio_buffers = 8;
bool zeroram = false;
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));
memory_randomize_ram(true);
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]);
if (!((kb & 7)==0) || kb < 8 || kb > 2048) {
printf("-ram value must be a multiple of 8 in the range of 8-2048.\n");
exit(1);
}
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], "-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], "-nvram")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
nvram_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], "-cart")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
cartridge_path = argv[0];
argc--;
argv++;
} else if (!strcmp(argv[0], "-cartbin")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
cartridge_new();
cartridge_define_bank_range(32, 255, CART_BANK_UNINITIALIZED_RAM);
cartridge_import_files(argv, 1, 32, CART_BANK_INITIALIZED_RAM, 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], "-wav")) {
argc--;
argv++;
// set up for recording
if (!argc || argv[0][0] == '-') {
usage();
}
wav_path = argv[0];
argv++;
argc--;
} else if (!strcmp(argv[0], "-debug")) {
argc--;
argv++;
debugger_enabled = true;
if (argc && argv[0][0] != '-') {
uint32_t bpVal = (uint32_t)strtol(argv[0], NULL, 16);
struct breakpoint bp;
if (bpVal < 0xA000) {
bp.pc = bpVal;
bp.bank = -1;
} else {
bp.pc = bpVal & 0xffff;
bp.bank = bpVal >> 16;
}
DEBUGSetBreakPoint(bp);
argc--;
argv++;
}
} else if (!strcmp(argv[0], "-randram")) {
/* this operation has no effect anymore, randomizing the Ram is now default */
argc--;
argv++;
} else if (!strcmp(argv[0], "-zeroram")) {
argc--;
argv++;
memory_randomize_ram(false);
zeroram = true;
} else if (!strcmp(argv[0], "-wuninit")) {
argc--;
argv++;
memory_report_uninitialized_access(true);
} else if (!strcmp(argv[0], "-joy1")) {
argc--;
argv++;
Joystick_slots_enabled[0] = true;
} else if (!strcmp(argv[0], "-joy2")){
argc--;
argv++;
Joystick_slots_enabled[1] = true;
} else if (!strcmp(argv[0], "-joy3")) {
argc--;
argv++;
Joystick_slots_enabled[2] = true;
} else if (!strcmp(argv[0], "-joy4")) {
argc--;
argv++;
Joystick_slots_enabled[3] = 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 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], "-widescreen")) {
argc--;
argv++;
screen_x_scale = 4.0/3;
} 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], "-rtc")) {
argc--;
argv++;
set_system_time = true;
} else if (!strcmp(argv[0], "-serial")) {
argc--;
argv++;
has_serial = true;
} else if (!strcmp(argv[0], "-nohostieee")) {
argc--;
argv++;
no_ieee_intercept = true;
} else if (!strcmp(argv[0], "-fsroot")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
fsroot_path = argv[0];
argc--;
argv++;
} else if (!strcmp(argv[0], "-startin")) {
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
startin_path = argv[0];
argc--;
argv++;
} else if (!strcmp(argv[0], "-noemucmdkeys")) {
argc--;
argv++;
disable_emu_cmd_keys = true;
} else if (!strcmp(argv[0], "-via2")) {
argc--;
argv++;
has_via2 = true;
} else if (!strcmp(argv[0], "-version")){
printf("%s", VER_INFO);
#ifdef GIT_REV
printf(" "GIT_REV"\n");
#else
printf("\n");
#endif
argc--;
argv++;
exit(0);
} else if (!strcmp(argv[0], "-testbench")){
printf("Testbench mode...\n");
fflush(stdout);
argc--;
argv++;
testbench=true;
headless=true;
} else if (!strcmp(argv[0], "-mhz")){
argc--;
argv++;
if (!argc || argv[0][0] == '-') {
usage();
}
MHZ = (uint8_t)strtol(argv[0], NULL, 10);
if (MHZ < 1 || MHZ > 40) {
usage();
}
argc--;
argv++;
} 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 (nvram_path) {
SDL_RWops *f = SDL_RWFromFile(nvram_path, "rb");
if (f) {
SDL_RWread(f, nvram, 1, sizeof(nvram));
SDL_RWclose(f);
}
}
if (sdcard_path) {