-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructionset_generator.cc
262 lines (220 loc) · 8.05 KB
/
instructionset_generator.cc
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
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <cctype>
#include <boost/algorithm/string.hpp>
using namespace std;
static map<string,string> byteArguments;
static map<string,string> wordArguments;
static map<string,string> condArguments;
static vector<string> condOps;
string extractInstructionsetName(string fileName)
{
int start = 0;
int end = fileName.length()-1;
// Find end...
for (int i = end; i >= 0; --i) {
char c = fileName[i];
if (c == '/' || c == '\\')
break;
if (c == '.') {
end = i-1;
break;
}
}
// Find start...
for (int i = end; i >= 0; --i) {
char c = fileName[i];
if (c == '/' || c == '\\') {
start = i+1;
break;
}
}
if (start >= end) {
cerr << "Cannot extract instructionset name from string '" << fileName << "'" << endl;
exit(1);
}
return fileName.substr(start, end-start+1);
}
void split(const string &str, char sep, vector<string> &v)
{
v.clear();
int i = 0;
int len = str.size();
while (i < len) {
while (i < len && (str[i] == sep))
++i;
string buf;
while (i < len && !(str[i] == sep)) {
buf += str[i];
++i;
}
v.push_back(buf);
}
}
string codeForArgument(const string &argument)
{
if (byteArguments.find(argument) != byteArguments.end())
return byteArguments[argument];
if (wordArguments.find(argument) != wordArguments.end())
return wordArguments[argument];
cerr << "Cannot find code for argument '" << argument << "'" << endl;
exit(1);
}
string generateInstructionCodeForLine(const string &line)
{
if (line.length() == 0 || line[0] == '#')
return "";
stringstream output;
vector<string> items;
split(line, ' ', items);
if (items.size() < 4) {
cerr << "Error in syntax of '" << line << "'" << endl;
exit(1);
}
vector<string> args;
if (items.size() > 4)
split(items[4], ',', args);
string mnemonic = items[3];
string assembly = items[3] + (items.size() > 4 ? " " + items[4] : "");
string code = items[0];
string length = items[1];
string condition;
vector<string> cycles;
split(items[2], '/', cycles);
string cycles0 = cycles.size() > 0 ? cycles[0] : "0";
string cycles1 = cycles.size() > 1 ? cycles[1] : "0";
if (args.size() > 0 &&
find(condOps.begin(), condOps.end(), mnemonic) != condOps.end() &&
condArguments.find(args[0]) != condArguments.end())
{
condition = condArguments[args[0]];
args.erase(args.begin());
}
string arg;
if (args.size() > 0 && (args[0][0] >= '0' && args[0][0] <= '9')) {
arg = args[0];
args.erase(args.begin());
if (arg[arg.size()-1] == 'H')
arg = "0x" + arg.substr(0, arg.size()-1);
}
string op;
string templateArgument;
if (args.size() > 0) {
// TODO
if (byteArguments.find(args[0]) != byteArguments.end()) {
templateArgument = "<byte>";
op = "op1";
} else {
templateArgument = "<word>";
op = "op2";
}
} else {
op = "op0";
}
output << " // " << line << endl;
output << " " << op << " = new " << mnemonic << "_Instruction" << templateArgument << "();" << endl
<< " " << op << "->cpu = cpu;" << endl
<< " " << op << "->code = 0x" << code << ";" << endl
<< " " << op << "->length = " << length << ";" << endl
<< " " << op << "->cycles0 = " << cycles0 << ";" << endl
<< " " << op << "->cycles1 = " << cycles1 << ";" << endl
<< " " << op << "->mnemonic = \"" << assembly << "\";" << endl;
if (condition != "") {
output << " " << op << "->condition = " << condition << ";" << endl;
}
if (arg != "") {
output << " " << op << "->arg = " << arg << ";" << endl;
}
int i = 0;
for (vector<string>::iterator it = args.begin(); it != args.end(); ++it) {
output << " " << op << "->ref" << i << " = " << codeForArgument(*it) << ";" << endl;
++i;
}
output << " instructionSet->add(" << op << ");" << endl;
return output.str();
}
static void init()
{
byteArguments["(BC)"] = "new MemoryReference<byte>(cpu, cpu->bc)";
byteArguments["(C)"] = "new Memory_SingleRegister_Reference<byte>(cpu, cpu->c)";
byteArguments["(DE)"] = "new MemoryReference<byte>(cpu, cpu->de)";
byteArguments["(HL)"] = "new MemoryReference<byte>(cpu, cpu->hl)";
byteArguments["(HL+)"] = "new Memory_HL_Reference<byte>(cpu, cpu->hl, 1)";
byteArguments["(HL-)"] = "new Memory_HL_Reference<byte>(cpu, cpu->hl, -1)";
byteArguments["(a8)"] = "new Memory_a8_Reference<byte>(cpu, cpu->pc)";
byteArguments["(a16)"] = "new Memory_a16_Reference<byte>(cpu, cpu->pc)";
byteArguments["A"] = "new RegisterReference<byte>(cpu->a)";
byteArguments["B"] = "new RegisterReference<byte>(cpu->b)";
byteArguments["C"] = "new RegisterReference<byte>(cpu->c)";
byteArguments["D"] = "new RegisterReference<byte>(cpu->d)";
byteArguments["E"] = "new RegisterReference<byte>(cpu->e)";
byteArguments["F"] = "new RegisterReference<byte>(cpu->f)";
byteArguments["H"] = "new RegisterReference<byte>(cpu->h)";
byteArguments["L"] = "new RegisterReference<byte>(cpu->l)";
byteArguments["d8"] = "new MemoryReference<byte>(cpu, cpu->pc)";
byteArguments["r8"] = "new MemoryReference<byte>(cpu, cpu->pc)";
wordArguments["SP"] = "new RegisterReference<word>(cpu->sp)";
wordArguments["AF"] = "new RegisterReference<word>(cpu->af)";
wordArguments["BC"] = "new RegisterReference<word>(cpu->bc)";
wordArguments["DE"] = "new RegisterReference<word>(cpu->de)";
wordArguments["HL"] = "new RegisterReference<word>(cpu->hl)";
wordArguments["a16"] = "new MemoryReference<word>(cpu, cpu->pc)";
wordArguments["d16"] = "new MemoryReference<word>(cpu, cpu->pc)";
wordArguments["[a16]"] = "new Memory_a16_Reference<word>(cpu, cpu->pc)";
condArguments["NZ"] = "new NZ_Condition()";
condArguments["NC"] = "new NC_Condition()";
condArguments["Z"] = "new Z_Condition()";
condArguments["C"] = "new C_Condition()";
condOps.push_back("JR");
condOps.push_back("JP");
condOps.push_back("RET");
condOps.push_back("CALL");
}
int main(int argc, char *argv[])
{
if (argc != 3) {
cerr << "Usage: " << argv[0] << " <input file> <output file>" << endl;
return 1;
}
ifstream infile(argv[1]);
if (!infile.is_open()) {
cerr << "Cannot open file " << argv[1] << endl;
return 1;
}
ofstream outfile(argv[2]);
if (!outfile.is_open()) {
cerr << "Cannot open file " << argv[2] << endl;
return 1;
}
init();
string instructionsetName = extractInstructionsetName(argv[2]);
string upperInstructionsetName = boost::to_upper_copy(instructionsetName);
outfile << "#ifndef " << upperInstructionsetName << "_H" << endl
<< "#define " << upperInstructionsetName << "_H" << endl
<< endl
<< "#include \"cpu.h\"" << endl
<< "#include \"instructions.h\"" << endl
<< "#include \"instructionset.h\"" << endl
<< "#include \"references.h\"" << endl
<< endl
<< "void initialize_" << instructionsetName << "(InstructionSet *instructionSet, CPU *cpu) {" << endl
<< " Instruction *op0;" << endl
<< " ReferenceInstruction<byte> *op1;" << endl
<< " ReferenceInstruction<word> *op2;" << endl
<< endl;
string line;
while (infile.good()) {
getline(infile, line);
outfile << generateInstructionCodeForLine(line) << endl;
}
outfile << "}" << endl
<< endl
<< "#endif" << endl;
return 0;
}