-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
298 lines (270 loc) · 10.6 KB
/
Copy pathmain.cpp
File metadata and controls
298 lines (270 loc) · 10.6 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
/**
* @file main.cpp
* @brief Main entry point for the PASM 6502 multi-pass assembler CLI tool.
* @author Paul Baxter
* @details Handles command-line option parsing, source loading, tokenization,
* parsing, multi-pass binary assembly, and binary/PRG output generation.
*/
#include <chrono>
#include <exception>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <filesystem>
#include <format> // <-- Added for std::format
#include <cctype>
#define GEN_RULEMAP
#include "ruletype.h"
#undef GEN_RULEMAP
#define GEN_TOKMAP
#include "tokenkind.h"
#undef GEN_TOKMAP
#include "AssemblerParser.h"
#include "PasmTokenizer.hpp"
#include "anonymouslabel.h"
#include "macrodef.h"
#include "multipassassembler.h"
#include "options.h"
#include "sourceManager.h"
#include "utilities.h"
#include "d64.h"
#include "autoloader.h"
/**
* @brief get the uppercase base name from a path.
*
* Used to create a Commodore 64 file name
*/
std::string GetUppercaseBasename(const std::string& filepath) {
namespace fs = std::filesystem;
// 1. Strip directory and extension
std::string name = fs::path(filepath).filename().stem().string();
// 2. Convert to uppercase
std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c) {
return static_cast<char>(std::toupper(c));
});
return name;
}
/**
* @brief prints help message.
*/
void help()
{
std::cout <<
R"(Usage:
pasm3 [-o outfile] [-c64] [-d64 disk] [-al] [-i directory] [-st symbol] [-d symbol value] [-h] [-cart options] inputfile inputfile2 ...
-h Print help.
-o outfile Specifies the output file name.
-v Specifies verbose output.
-c64 Specifies Commodore 64 program format.
It places the load address in first two bytes.
-i directory Specifies include directory. Can be specified more than once.
-st symbol Specifies symbol trace. Displays symbol and value and when modified.
This can change because of macros etc.
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.
-o outfile MUST be specified
-d64 disk Creates d64 disk and installs the output file
-o outfile MUST be specified
-al Creates auto loader. Can only be used with -d64
-o outfile MUST be specified
)";
}
/**
* @brief Parses the input arguments.
*
* Parses CLI flags (`-h`, `-o`, `-v`, `-c64`, `-i`, `-st`, `-d`, `-cart`, `-d64`)
*
* @param argc Count of command-line arguments.
* @param argv Array of command-line argument strings.
* @return Options Returns Options on successful assembly
* @exception throws runtime exception 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 == "-o") {
arg++;
if (arg >= argc) {
help();
throw std::runtime_error("No output file specified for -o");
}
options.outfile = argv[arg];
}
else if (arg_str == "-c64") {
options.c64 = true;
}
else if (arg_str == "-al") {
options.autoloader = true;
}
else if (arg_str == "-i") {
arg++;
if (arg >= argc) {
help();
throw std::runtime_error("No directory specified for -i");
}
options.include.push_back(argv[arg]);
}
else if (arg_str == "-cart") {
arg++;
if (arg >= argc) {
help();
throw std::runtime_error("No options specified for -cart"); // Fixed message
}
options.cart = true;
options.cart_options = argv[arg];
}
else if (arg_str == "-d64") {
arg++;
if (arg >= argc) {
help();
throw std::runtime_error("No disk name specified for -d64"); // Fixed typo
}
options.d64 = true;
options.d64_diskname = argv[arg];
}
else if (arg_str == "-st") {
arg++;
if (arg >= argc) {
help();
throw std::runtime_error("No symbol defined for -st");
}
options.traced_symbols.push_back(argv[arg]);
}
else if (arg_str == "-d") {
arg++;
if ((arg + 1) >= argc) {
help();
throw std::runtime_error("No symbol or value defined for -d");
}
std::string sym = argv[arg];
arg++;
std::string val = argv[arg];
int symval;
std::stringstream ss;
if (val[0] == '$') {
ss << std::hex << val.substr(1);
}
else {
ss << std::dec << val;
}
ss >> symval;
options.defined_symbols.push_back({sym, symval});
}
else {
help();
throw std::runtime_error(std::format("Unknown option '{}'", arg_str));
}
arg++;
}
if (options.input_filenames.empty()) {
help();
throw std::runtime_error("No input file specified");
}
if (options.cart && options.outfile.length() == 0) {
help();
throw std::runtime_error("Outfile must be specified when creating a cart.");
}
if (options.d64 && options.outfile.length() == 0) {
help();
throw std::runtime_error("Outfile must be specified when creating a d64 disk."); // Added validation
}
if (options.autoloader && ! options.d64) {
help();
throw std::runtime_error("d64 disk must be specified when creating an autoloader."); // Added validation
}
return options;
}
/**
* @brief Application entry point for the 6502 cross-assembler.
*
* tokenizes and parses code statements, builds symbol tables over multiple
* passes, and outputs binary files and listings.
*
* @param argc Count of command-line arguments.
* @param argv Array of command-line argument strings.
* @return int Returns 0 on successful assembly, or non-zero on invalid usage or runtime exceptions.
*/
int main(int argc, char* argv[])
{
try {
Options options = parse_args(argc, argv);
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
SourceManager src_mgr;
PasmTokenizer tokenizer;
std::unordered_map<std::string, MacroDef> macros_;
std::vector<AnonymousLabel> anonymous_labels;
std::vector<PasmTokenizer::Token> tokens;
for (const auto& root_file : options.input_filenames) {
auto file_tokens = LoadAndTokenizeFile(root_file, src_mgr, tokenizer);
tokens.insert(tokens.end(), file_tokens.begin(), file_tokens.end());
}
AssemblerParser parser(tokens, options);
auto statements = parser.ParseProgram(src_mgr, macros_, tokenizer);
MultiPassAssembler assembler(options);
assembler.Assemble(statements, anonymous_labels, src_mgr);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << assembler.listing_file << "\n";
if (options.outfile.length() > 0) {
if (options.c64) {
auto lo = static_cast<uint8_t>(assembler.load_address & 0xFF);
auto hi = static_cast<uint8_t>((assembler.load_address >> 8) & 0xFF);
// The two bytes you want to add to the start
uint8_t prefix[2] = {lo, hi};
// Insert the 2 bytes at the beginning of the vector
assembler.binary_output.insert(assembler.binary_output.begin(), std::begin(prefix), std::end(prefix));
}
std::ofstream out(options.outfile, std::ios::out | std::ios::binary);
auto sz = assembler.binary_output.size();
out.write(reinterpret_cast<const char*>(assembler.binary_output.data()), assembler.binary_output.size());
out.close();
std::cout << "Wrote " << sz << " bytes to " << options.outfile << "\n";
}
std::chrono::duration<double> elapsed_seconds = end - begin;
std::cout << "Elapsed time: " << elapsed_seconds.count() << " seconds\n";
if (options.cart) {
std::string command = std::format("cartconv -i {} -l {} {} -o {}.crt",
options.outfile, assembler.load_address, options.cart_options, options.outfile);
std::cout << command << "\n";
int exitCode = std::system(command.c_str());
if (exitCode != 0) {
throw std::runtime_error("Error running cart convert");
}
}
if (options.d64) {
d64 disk;
auto name = GetUppercaseBasename(options.outfile);
if (options.autoloader) {
auto loader_code = CreateAutoLoader(name, assembler.load_address);
disk.addFile("LOADER", c64FileType(d64FileTypes::PRG), loader_code);
std::cout << "Added LOADER.PRG to disk " << options.d64_diskname << "\n";
}
disk.addFile(name, c64FileType(d64FileTypes::PRG), assembler.binary_output);
disk.save(options.d64_diskname);
std::cout << "Added " << name << ".PRG to disk " << options.d64_diskname << "\n";
}
}
catch (std::exception& ex) {
std::cerr << "Error " << ex.what() << "\n";
}
return 0;
}