-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcode-gen.hpp
140 lines (121 loc) · 4.74 KB
/
opcode-gen.hpp
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
/*
* MIT License
*
* Copyright(c) 2018 Paul Bernitz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files(the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <utility>
#include <algorithm>
#include "conversion.hpp"
#include "compiler_log.hpp"
namespace c8s
{
// Creating the finished opcodes using `meta opcodes` produced by the meta generator.
std::vector<u16> create_opcodes_from_meta(std::vector<std::string> meta_opcodes)
{
if (meta_opcodes.size() == 0 || compiler_log::read_errors().size() != 0)
return {};
// Remove last (zero) opcode.
if (meta_opcodes.back() == "0")
meta_opcodes.pop_back();
// Replace labels by their calculated `real` memory-offset.
for (unsigned i = 0; i<meta_opcodes.size(); ++i)
{
// Find a target-label.
if (meta_opcodes[i].find("<!") != std::string::npos)
{
// Calculate the `real` distance (skipping entries containing `<!`) from target to start.
auto real_distance = std::count_if(meta_opcodes.begin(), meta_opcodes.begin() + i,
[](const std::string& str_elem) { return str_elem.find("<!") == std::string::npos; });
// Get the value of the label between `<!` and `!>`.
std::string val = meta_opcodes[i].substr(meta_opcodes[i].find("<!") + 2, meta_opcodes[i].find("!>") - 2);
// If the value is smaller than 500 we know it is an if-label.
bool is_if_label = (atoi(val.c_str()) < 500);
// Change start for search based on if its a if- or for-label.
unsigned from = is_if_label ? 0 : i;
unsigned to = is_if_label ? i : meta_opcodes.size();
// Now only target the labels before that and replace them.
for (unsigned j = from; j<to; ++j)
{
// Find a source-label that should be replaced.
if (meta_opcodes[j].find("<" + val + ">") != std::string::npos)
{
// Calculate the `real` offset in memory by adding the starting address 0x200
// for chip-8 ROM's to the `real` distance times the size of each opcode (2 bytes).
unsigned real_address_offset = (0x200 + (real_distance * 2));
// Convert the offset from decimal to a hexadecimal string.
std::string converted_hex_buffer = u16_to_hex_string(real_address_offset);
// Overwrite the current meta-opcode with the real opcode.
meta_opcodes[j] = meta_opcodes[j].substr(0, meta_opcodes[j].find('<')) + converted_hex_buffer;
break;
}
}
}
}
// Remove `<!..!>`-blocks from `meta_opcodes`.
meta_opcodes.erase(std::remove_if(
meta_opcodes.begin(),
meta_opcodes.end(),
[](const std::string& str_elem) {
return str_elem.find('!') != std::string::npos;
}), meta_opcodes.end());
// Convert the finished opcodes to u16.
std::vector<u16> opcodes{};
for (auto& meta : meta_opcodes)
{
if (std::find(meta.begin(), meta.end(), '<') != meta.end())
{
compiler_log::write_error("Opcode generation error! All labels should have been parsed by now!");
return {};
}
try
{
u16 op = 0xFFFF & std::stoul(meta, nullptr, 16);
opcodes.push_back(op);
}
catch (std::invalid_argument ex)
{
compiler_log::write_error("Unable to convert " + meta + " to hexadecimal in " + ex.what());
return {};
}
catch (...)
{
compiler_log::write_error("Unknown error occurred while trying to convert opcode " + meta);
return {};
}
}
return opcodes;
}
// Write the finished opcodes to a ROM file.
void write_opcodes_to_file(std::vector<u16> opcodes, std::string file_path)
{
std::ofstream ofs{ file_path, std::ios::binary };
for (const auto& op : opcodes)
{
u16 rev = (op & 0xFF) << 8 | (op) >> 8;
ofs.write((char*)&rev, sizeof(u16));
}
}
}