blob: 888168c271903a2bff572073c152898a46f8663f [file] [log] [blame]
swissChili6c61a792020-07-28 16:29:20 -07001#include "cpu.h"
swissChili62d6d5d2020-07-30 20:12:47 -07002#include "dbg.h"
swissChili6c61a792020-07-28 16:29:20 -07003
swissChili62d6d5d2020-07-30 20:12:47 -07004#include <bits/getopt_core.h>
swissChili6c61a792020-07-28 16:29:20 -07005#include <ctype.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <unistd.h>
9
10int main(int argc, char **argv)
11{
swissChilida4803e2020-08-06 20:06:04 -070012 bool disflag = false,
13 runflag = false,
14 helpflag = false,
15 debugflag = false,
16 should_read = false;
17
18 int disasm_len = 0;
swissChili62d6d5d2020-07-30 20:12:47 -070019
20 FILE *input = stdin;
swissChili6c61a792020-07-28 16:29:20 -070021
22 char c;
23
swissChilida4803e2020-08-06 20:06:04 -070024 while ((c = getopt(argc, argv, "Ddrhi:n:")) != -1)
swissChili6c61a792020-07-28 16:29:20 -070025 {
26 switch (c)
27 {
swissChili62d6d5d2020-07-30 20:12:47 -070028 case 'd':
29 disflag = true;
30 should_read = true;
31 break;
32 case 'r':
33 runflag = true;
34 should_read = true;
35 break;
36 case 'D':
37 debugflag = true;
38 should_read = true;
39 break;
40 case 'i':
41 input = fopen(optarg, "r");
42 break;
swissChilida4803e2020-08-06 20:06:04 -070043 case 'n':
44 disasm_len = atoi(optarg);
45 break;
swissChili62d6d5d2020-07-30 20:12:47 -070046 case 'h':
47 case '?':
48 helpflag = 1;
49 break;
swissChili6c61a792020-07-28 16:29:20 -070050 }
51 }
52
53 if (helpflag)
54 {
swissChili62d6d5d2020-07-30 20:12:47 -070055 printf("6502 emulator, disassembler and debugger\n"
56 "Usage:\n"
57 " -d disassemble input\n"
58 " -r run input\n"
59 " -D debug input (open debug prompt)\n"
60 " -i <input> set input file, defaults to standard input\n"
swissChilida4803e2020-08-06 20:06:04 -070061 " -n <number> number of instructions to disassemble, 0 for all\n"
swissChili62d6d5d2020-07-30 20:12:47 -070062 " -h, -? show this help page\n");
swissChili6c61a792020-07-28 16:29:20 -070063 return 0;
64 }
65
swissChili62d6d5d2020-07-30 20:12:47 -070066 cpu_t cpu;
67
68 if (should_read)
69 {
70 cpu = new_cpu();
71 fread(cpu.mem, 0xFFFF, 1, input);
72 }
swissChilida4803e2020-08-06 20:06:04 -070073 else
74 {
75 puts("6502 toolchain by swissChili <swisschili.sh>");
76 printf("%s -h for help\n", argv[0]);
77 }
swissChili6c61a792020-07-28 16:29:20 -070078
79 if (disflag)
80 {
81 disas(&cpu);
82 }
swissChilida4803e2020-08-06 20:06:04 -070083 else if (runflag)
84 {
85 run(&cpu);
86 }
swissChili62d6d5d2020-07-30 20:12:47 -070087 else if (debugflag)
88 {
89 debug(&cpu);
90 }
91
92 if (should_read)
93 free_cpu(&cpu);
swissChili6c61a792020-07-28 16:29:20 -070094}