-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
60 lines (51 loc) · 1.86 KB
/
main.cpp
File metadata and controls
60 lines (51 loc) · 1.86 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
#include <iostream>
#include <fstream>
#include "ASM/Compiler.hpp"
#include "Hardware/Processor.hpp"
#include "Hardware/InteractiveCode.hpp"
#include "Hardware/Code.hpp"
using Hardware::WORD;
int main(int argc, const char* argv[]) {
assert(argc == 2 && "-i or --interactive or code file location");
ASM::init();
std::unique_ptr<Hardware::IMemory> code;
std::shared_ptr<ASM::Loader> loader{new ASM::Loader{""}};
ASM::Compiler compiler(loader);
if(std::string{argv[1]} == "-i" || std::string{argv[1]} == "--interactive") { //TODO: made more accurate
auto interactive_shell = [&]() {
std::cout << "enter code:\n";
std::string asm_code, line;
while(std::getline(std::cin, line)) {
if(line == "--end")
break;
asm_code += line + "\n";
}
auto offset = compiler.offset();
compiler.add_source(loader->add(asm_code));
auto const& memory = compiler.get_memory();
return std::vector<WORD>{memory.begin() + offset, memory.begin() + compiler.offset()};
};
code = std::make_unique<Hardware::InteractiveCode>(interactive_shell);
} else {
compiler.add_source(loader->get_id(argv[1]));
code = std::make_unique<Hardware::Code>(compiler.get_memory());
}
Hardware::Computer computer(
std::move(code),
std::make_unique<Hardware::Stack>(),
std::make_unique<Hardware::RAM>(),
std::make_unique<Hardware::IO>(),
std::make_unique<Hardware::Display>()
);
auto execute_step = [&] {
if(computer.processor->complete())
return;
auto segment = compiler.get_mapping(computer.processor->get_IP());
auto code = loader->load(segment.source_id);
std::cerr << "ASM code(source_id = " << segment.source_id << "): " << code.substr(segment.from, segment.to - segment.from) << "\n";
computer.execute_step([&](WORD index) { return compiler.get_mapping(index); });
};
// while(!computer.processor->complete())
// execute_step();
computer.execute(10000);
}