-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexsim.cpp
More file actions
70 lines (66 loc) · 2.1 KB
/
hexsim.cpp
File metadata and controls
70 lines (66 loc) · 2.1 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
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <exception>
#include <iostream>
#include "hexsim.hpp"
#include "hexsimio.hpp"
//===---------------------------------------------------------------------===//
// Driver
//===---------------------------------------------------------------------===//
static void help(const char *argv[]) {
std::cout << "Hex processor simulator\n\n";
std::cout << "Usage: " << argv[0] << " file\n\n";
std::cout << "Positional arguments:\n";
std::cout << " file A binary file to simulate\n\n";
std::cout << "Optional arguments:\n";
std::cout << " -h,--help Display this message\n";
std::cout << " -d,--dump Dump the binary file contents\n";
std::cout << " -t,--trace Enable instruction tracing\n";
std::cout << " --max-cycles N Limit the number of simulation cycles (default: 0)\n";
}
int main(int argc, const char *argv[]) {
try {
const char *filename = nullptr;
bool dumpBinary = false;
bool trace = false;
size_t maxCycles = 0;
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "-d") == 0 ||
std::strcmp(argv[i], "--dump") == 0) {
dumpBinary = true;
} else if (std::strcmp(argv[i], "-t") == 0 ||
std::strcmp(argv[i], "--trace") == 0) {
trace = true;
} else if (std::strcmp(argv[i], "--max-cycles") == 0) {
maxCycles = std::stoull(argv[++i]);
} else if (std::strcmp(argv[i], "-h") == 0 ||
std::strcmp(argv[i], "--help") == 0) {
help(argv);
return 1;
} else {
if (!filename) {
filename = argv[i];
} else {
throw std::runtime_error("cannot specify more than one file");
}
}
}
// A file must be specified.
if (!filename) {
help(argv);
return 1;
}
hexsim::Processor p(std::cin, std::cout, maxCycles);
p.setTracing(trace);
p.load(filename, dumpBinary);
if (dumpBinary) {
return 0;
}
return p.run();
} catch (std::exception &e) {
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
return 0;
}