src/platform/commandline.c (view raw)
1#include "commandline.h"
2
3#ifdef USE_CLI_DEBUGGER
4#include "debugger/cli-debugger.h"
5#endif
6
7#ifdef USE_GDB_STUB
8#include "debugger/gdb-stub.h"
9#endif
10
11#include <fcntl.h>
12#include <getopt.h>
13
14static const char* _defaultFilename = "test.rom";
15
16static const struct option _options[] = {
17 { "bios", 1, 0, 'b' },
18 { "debug", 1, 0, 'd' },
19 { "gdb", 1, 0, 'g' },
20 { 0, 0, 0, 0 }
21};
22
23int parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv) {
24 memset(opts, 0, sizeof(*opts));
25 opts->fd = -1;
26 opts->biosFd = -1;
27 opts->width = 240;
28 opts->height = 160;
29 opts->fullscreen = 0;
30
31 int multiplier = 1;
32 int ch;
33 while ((ch = getopt_long(argc, argv, "234b:dfg", _options, 0)) != -1) {
34 switch (ch) {
35 case 'b':
36 opts->biosFd = open(optarg, O_RDONLY);
37 break;
38#ifdef USE_CLI_DEBUGGER
39 case 'd':
40 if (opts->debuggerType != DEBUGGER_NONE) {
41 return 0;
42 }
43 opts->debuggerType = DEBUGGER_CLI;
44 break;
45#endif
46 case 'f':
47 opts->fullscreen = 1;
48 break;
49#ifdef USE_GDB_STUB
50 case 'g':
51 if (opts->debuggerType != DEBUGGER_NONE) {
52 return 0;
53 }
54 opts->debuggerType = DEBUGGER_GDB;
55 break;
56#endif
57 case '2':
58 multiplier = 2;
59 break;
60 case '3':
61 multiplier = 3;
62 break;
63 case '4':
64 multiplier = 4;
65 break;
66 default:
67 return 0;
68 }
69 }
70 argc -= optind;
71 argv += optind;
72 if (argc == 1) {
73 opts->fname = argv[0];
74 } else if (argc == 0) {
75 opts->fname = _defaultFilename;
76 } else {
77 return 0;
78 }
79 opts->fd = open(opts->fname, O_RDONLY);
80 opts->width *= multiplier;
81 opts->height *= multiplier;
82 return 1;
83}
84
85struct ARMDebugger* createDebugger(struct StartupOptions* opts) {
86 union DebugUnion {
87 struct ARMDebugger d;
88#ifdef USE_CLI_DEBUGGER
89 struct CLIDebugger cli;
90#endif
91#ifdef USE_GDB_STUB
92 struct GDBStub gdb;
93#endif
94 };
95
96 union DebugUnion* debugger = malloc(sizeof(union DebugUnion));
97
98 switch (opts->debuggerType) {
99#ifdef USE_CLI_DEBUGGER
100 case DEBUGGER_CLI:
101 CLIDebuggerCreate(&debugger->cli);
102 break;
103#endif
104#ifdef USE_GDB_STUB
105 case DEBUGGER_GDB:
106 GDBStubCreate(&debugger->gdb);
107 GDBStubListen(&debugger->gdb, 2345, 0);
108 break;
109#endif
110 case DEBUGGER_NONE:
111 case DEBUGGER_MAX:
112 free(debugger);
113 return 0;
114 break;
115 }
116
117 return &debugger->d;
118}
119
120void usage(const char* arg0) {
121 printf("usage: %s [option ...] file\n", arg0);
122 printf("\nOptions:\n");
123 printf(" -2 2x viewport\n");
124 printf(" -3 3x viewport\n");
125 printf(" -4 4x viewport\n");
126 printf(" -b, --bios FILE GBA BIOS file to use\n");
127#ifdef USE_CLI_DEBUGGER
128 printf(" -d, --debug Use command-line debugger\n");
129#endif
130 printf(" -f Sart full-screen\n");
131#ifdef USE_GDB_STUB
132 printf(" -g, --gdb Start GDB session (default port 2345)\n");
133#endif
134}