Repository navigation
Expand file tree
/
Copy pathparseargs.cpp
More file actions
375 lines (331 loc) · 14.4 KB
/
Copy pathparseargs.cpp
File metadata and controls
375 lines (331 loc) · 14.4 KB
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
/**
* @file parseargs.cpp
* @author Paul Baxter
*/
#include <chrono>
#include <exception>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <filesystem>
#include <format>
#include <cctype>
#include <algorithm>
#include <unordered_set>
#include "AssemblerParser.h"
#include "PasmTokenizer.hpp"
#include "anonymouslabel.h"
#include "macrodef.h"
#include "multipassassembler.h"
#include "options.h"
#include "sourceManager.h"
#include "opcodedict.h"
#include "utilities.h"
#include "d64.h"
#include "autoloader.h"
/**
* @brief Prints help message.
*/
void help()
{
std::cout <<
R"(Usage:
pasm3 [-h] [-o outfile] [-v] [-vs symfile] [-c64] [-cpu type] [-i directory] [-st symbol] [-d symbol value] [-cart [options]] [-d64 disk] [-al] [-debug] [-run] [-ops] [-mp maxpass] [-xref] inputfile inputfile2 ...
-h Print help.
-o [outfile] Specifies to create an output file.
-v Specifies verbose output.
-vs [file] Create VICE symbol file.
-c64 Specifies Commodore 64 program format.
It places the load address in first two bytes.
-cpu 6502|65c02 Specifies target CPU architecture (default: 6502).
-i directory Specifies include directory. Can be specified more than once.
-st symbol Specifies symbol trace. Displays symbol and value and when modified.
Can be specified multiple times.
-d symbol value Defines a symbol and value.
Can be specified more than once.
-cart [options] Runs cartconv on the outputfile with the specified options enclosed in quotes.
Load address and inputname are auto specified.
VICE must be installed and in the path.
-d64 [diskname] Creates d64 disk and installs the output file.
-al Creates auto loader. This will also create a .d64 disk if -d64 is not specified.
-debug Launch VICE monitor and debug. Can not be used with -launch.
-run Launch in VICE. Can not be used with -debug.
-mp maxpass Sets the max number of passes. Default is 10.
-xref Display a cross reference symbol file.
-ops [op1][op2].. Display allowed opcodes and modes. No argument displays all.
)";
}
/**
* @brief Parses the input arguments.
*
* Parses CLI flags (`-h`, `-o`, `-debug`, `-v`, `-c64`, `-cpu`, `-vs`, `-i`, `-st`, `-d`, `-cart`, `-d64`, `-al`, `-run`, `-mp`, `-xref`, `-ops`)
*
* @param argc Count of command-line arguments.
* @param argv Array of command-line argument strings.
* @return Options Returns Options on successful parsing.
* @exception throws runtime_error on invalid parameters
*/
Options parse_args(int argc, char* argv[])
{
Options options;
auto arg = 1;
while (arg < argc) {
std::string arg_str = std::string(argv[arg]);
if (arg_str[0] != '-') {
options.input_filenames.push_back(argv[arg]);
}
else if (arg_str == "-h") {
help();
exit(0);
}
else if (arg_str == "-v") {
options.verbose = true;
}
else if (arg_str == "-debug") {
options.debug = true;
}
else if (arg_str == "-o") {
options.out = true;
if ((arg + 1) < argc && argv[arg + 1][0] != '-') {
options.outfile = argv[++arg];
}
}
else if (arg_str == "-c64") {
options.c64 = true;
}
else if (arg_str == "-cpu") {
if ((arg + 1) >= argc) {
help();
throw std::runtime_error("CPU architecture type not specified for -cpu");
}
std::string cpu_type = argv[++arg];
// Normalize input string
std::transform(cpu_type.begin(), cpu_type.end(), cpu_type.begin(), ::tolower);
if (cpu_type == "6502") {
options.cpu = CPU::_6502;
} else if (cpu_type == "65c02" || cpu_type == "65c02s" || cpu_type == "w65c02") {
options.cpu = CPU::_65C02;
} else {
help();
throw std::runtime_error(std::format("Unsupported CPU target '{}'. Allowed values are '6502' or '65c02'", cpu_type));
}
}
else if (arg_str == "-vs") {
options.vs = true;
if ((arg + 1) < argc && argv[arg + 1][0] != '-') {
options.vs_name = argv[++arg];
}
}
else if (arg_str == "-mp") {
if ((arg + 1) >= argc) {
help();
throw std::runtime_error("Max pass value not specified for -mp");
}
arg++;
options.max_pass = std::stoi(argv[arg]);
if (options.max_pass <= 1) {
help();
throw std::runtime_error("Invalid value specified for -mp (must be > 1)");
}
}
else if (arg_str == "-al") {
options.autoloader = true;
}
else if (arg_str == "-i") {
if ((arg + 1) >= argc) {
help();
throw std::runtime_error("No directory specified for -i");
}
options.include.push_back(argv[++arg]);
}
else if (arg_str == "-cart") {
options.cart = true;
if ((arg + 1) >= argc || argv[arg + 1][0] == '-') {
options.cart_options = "-t normal -p";
}
else {
options.cart_options = argv[++arg];
}
}
else if (arg_str == "-d64") {
options.d64 = true;
if ((arg + 1) < argc && argv[arg + 1][0] != '-') {
options.d64_diskname = argv[++arg];
}
}
else if (arg_str == "-st") {
if ((arg + 1) >= argc) {
help();
throw std::runtime_error("No symbol defined for -st");
}
options.traced_symbols.push_back(argv[++arg]);
}
else if (arg_str == "-run") {
options.launch = true;
}
else if (arg_str == "-xref") {
options.xref = true;
}
else if (arg_str == "-d") {
if ((arg + 2) >= argc) {
help();
throw std::runtime_error("No symbol or value defined for -d");
}
std::string sym = argv[++arg];
std::string val = argv[++arg];
int symval = 0;
std::stringstream ss;
if (!val.empty() && val[0] == '$') {
ss << std::hex << val.substr(1);
}
else {
ss << std::dec << val;
}
if (!(ss >> symval)) {
help();
throw std::runtime_error(std::format("Invalid numeric value '{}' for symbol '{}'", val, sym));
}
options.defined_symbols.push_back({sym, symval});
}
else if (arg_str == "-ops") {
std::unordered_set<std::string> target_ops;
while (arg + 1 < argc && argv[arg + 1][0] != '-') {
std::string op_arg = argv[++arg];
std::transform(op_arg.begin(), op_arg.end(), op_arg.begin(), ::toupper);
target_ops.insert(op_arg);
}
std::unordered_set<std::string> found_ops;
for (auto& [op, opcodeinfo] : opcodeDict) {
std::string mnemonic_upper(opcodeinfo.mnemonic);
std::transform(mnemonic_upper.begin(), mnemonic_upper.end(), mnemonic_upper.begin(), ::toupper);
if (!target_ops.empty() && !target_ops.contains(mnemonic_upper)) {
continue;
}
found_ops.insert(mnemonic_upper);
std::cout << std::format("\n\n {} ({})\n", opcodeinfo.mnemonic, opcodeinfo.description);
std::cout << std::format(" {:<22} {:<8}{:<8} {}\n", "Addressing Mode", "Hex", "Cycles", "Syntax Example");
std::cout << std::format(" {}\n", std::string(60, '-'));
for (auto& [mode, info] : opcodeinfo.mode_to_opcode) {
uint8_t hex_val = info.first;
int cycles = info.second;
switch (mode) {
case OP_TYPE::Op_Implied:
std::cout << std::format(" {:<22} ${:02X} {:<8} {}\n",
"Implied", hex_val, cycles, opcodeinfo.mnemonic);
break;
case OP_TYPE::Op_Accumulator:
std::cout << std::format(" {:<22} ${:02X} {:<8} {} A\n",
"Accumulator", hex_val, cycles, opcodeinfo.mnemonic);
break;
case OP_TYPE::Op_Immediate:
std::cout << std::format(" {:<22} ${:02X} {:<8} {} #${:02X}\n",
"Immediate", hex_val, cycles, opcodeinfo.mnemonic, 0x10);
break;
case OP_TYPE::Op_ZeroPage:
std::cout << std::format(" {:<22} ${:02X} {:<8} {}${:02X}\n",
"Zero Page", hex_val, cycles, opcodeinfo.mnemonic, 0x15);
break;
case OP_TYPE::Op_ZeroPageX:
std::cout << std::format(" {:<22} ${:02X} {:<8} {}${:02X},X\n",
"Zero Page, X", hex_val, cycles, opcodeinfo.mnemonic, 0x16);
break;
case OP_TYPE::Op_ZeroPageY:
std::cout << std::format(" {:<22} ${:02X} {:<8} {}${:02X},Y\n",
"Zero Page, Y", hex_val, cycles, opcodeinfo.mnemonic, 0x17);
break;
case OP_TYPE::Op_Absolute:
std::cout << std::format(" {:<22} ${:02X} {:<8} {}${:04X}\n",
"Absolute", hex_val, cycles, opcodeinfo.mnemonic, 0x1234);
break;
case OP_TYPE::Op_AbsoluteX:
std::cout << std::format(" {:<22} ${:02X} {:<8} {}${:04X},X\n",
"Absolute, X", hex_val, cycles, opcodeinfo.mnemonic, 0x3456);
break;
case OP_TYPE::Op_AbsoluteY:
std::cout << std::format(" {:<22} ${:02X} {:<8} {}${:04X},Y\n",
"Absolute, Y", hex_val, cycles, opcodeinfo.mnemonic, 0x1246);
break;
case OP_TYPE::Op_Indirect:
std::cout << std::format(" {:<22} ${:02X} {:<8} {} (${:04X})\n",
"Indirect", hex_val, cycles, opcodeinfo.mnemonic, 0x8735);
break;
case OP_TYPE::Op_IndirectX:
std::cout << std::format(" {:<22} ${:02X} {:<8} {} (${:02X},X)\n",
"Indexed Indirect (X)", hex_val, cycles, opcodeinfo.mnemonic, 0x35);
break;
case OP_TYPE::Op_IndirectY:
std::cout << std::format(" {:<22} ${:02X} {:<8} {} (${:02X}),Y\n",
"Indirect Indexed (Y)", hex_val, cycles, opcodeinfo.mnemonic, 0x40);
break;
case OP_TYPE::Op_Relative:
std::cout << std::format(" {:<22} ${:02X} {:<8} {}${:04X}\n",
"Relative", hex_val, cycles, opcodeinfo.mnemonic, 0x8012);
break;
case OP_TYPE::Op_ZeroPageRelative:
std::cout << std::format(" {:<22} ${:02X} {:<8} {} ${:02X},${:04X}\n",
"ZP Relative (65C02)", hex_val, cycles, opcodeinfo.mnemonic, 0x51, 0x8012);
break;
default:
break;
}
}
}
if (!target_ops.empty()) {
for (const auto& requested : target_ops) {
if (!found_ops.contains(requested)) {
std::cerr << std::format("\nError: Unknown opcode '{}'.\n", requested);
}
}
}
exit(0);
}
else {
help();
throw std::runtime_error(std::format("Unknown option '{}'", arg_str));
}
arg++;
}
if (options.launch && options.debug) {
help();
throw std::runtime_error("Launch cannot be used with debug");
}
if (options.input_filenames.empty()) {
help();
throw std::runtime_error("No input file specified");
}
std::string fname = options.out ? options.outfile : options.input_filenames[0];
std::string base_name = RemoveExtension(fname);
if (!options.out && (options.cart || options.launch || options.debug || options.d64 || options.autoloader)) {
options.out = true;
}
if (options.out && options.outfile.empty()) {
if (options.cart) {
options.outfile = std::format("{}.bin", base_name);
}
else {
options.outfile = std::format("{}{}", base_name, (options.c64 ? ".prg" : ".bin"));
if (options.autoloader) {
namespace fs = std::filesystem;
fs::path p(fname);
auto prg_name = GetBasename(fname);
options.outfile = (p.parent_path() / (prg_name + ".prg")).string();
}
}
}
if (options.autoloader && !options.d64) {
options.d64 = true;
}
if (options.debug && !options.vs) {
options.vs = true;
}
if (options.d64 && options.d64_diskname.empty()) {
options.d64_diskname = std::format("{}.d64", base_name);
}
if (options.vs && options.vs_name.empty()) {
options.vs_name = std::format("{}.vs", base_name);
}
return options;
}