-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
57 lines (44 loc) · 1.15 KB
/
main.cpp
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
#include <iostream>
#include <memory>
#include <fstream>
#include <string>
#include <cinttypes>
#include "src/lc3.hpp"
#include "src/devices/memory.hpp"
#include "src/devices/iodevice.hpp"
std::vector<uint16_t> loadBinary(std::string filename)
{
std::ifstream input(filename, std::ios::binary);
std::vector<char> bytes(
std::istreambuf_iterator<char>(input),
(std::istreambuf_iterator<char>())
);
input.close();
std::vector<uint16_t> result(bytes.size() / 2);
for (size_t i = 0; i < bytes.size() - 1; i += 2)
result.push_back((bytes[i + 1] << 8) | bytes[i]);
return result;
}
int main(int argc, char** argv)
{
if (argc < 2) {
std::cout << "Error: required path to programm to execute as first argument.\n";
return 1;
}
std::vector<uint16_t> program;
try {
program = loadBinary(argv[1]);
}
catch(const std::ifstream::failure&) {
std::cout << "Error: exception while opening file with path : \'" << argv[1] << "\'.\n";
return 1;
}
auto memory = std::make_shared<lc3::Memory>(program.data(), program.size());
auto io = std::make_shared<lc3::IODevice>();
lc3::LC3 cpu(
std::move(memory),
std::move(io)
);
cpu.run();
return 0;
}