-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcodeaux.cpp
259 lines (227 loc) · 4.56 KB
/
codeaux.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
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
// codeaux.cpp
// Revision 20-nov-2005
#include "codeaux.h"
#include "trace.h"
#include <stdexcept>
namespace pasmo {
namespace impl {
using std::logic_error;
namespace {
logic_error UnexpectedRegisterCode ("Unexpected register code");
logic_error InvalidPrefixUsed ("Invalid use of prefix");
logic_error InvalidRegisterUsed ("Invalid register used");
logic_error InvalidInstructionType ("Invalid instruction type");
logic_error UnexistentMode ("Mode of code generation invalid");
char openIndir= '[';
char closeIndir= ']';
} // namespace
byte getregb86 (regbCode rb)
{
switch (rb)
{
case regA: return reg86AL;
case regB: return reg86CH;
case regC: return reg86CL;
case regD: return reg86DH;
case regE: return reg86DL;
case regH: return reg86BH;
case regL: return reg86BL;
default:
ASSERT (false);
throw UnexpectedRegisterCode;
}
}
string nameHLpref (byte prefix)
{
switch (prefix)
{
case prefixNone: return "HL";
case prefixIX: return "IX";
case prefixIY: return "IY";
default:
throw InvalidPrefixUsed;
}
}
string regwName (regwCode code, bool useSP, byte prefix)
{
ASSERT (code == regHL || prefix == prefixNone);
switch (code)
{
case regBC: return "BC";
case regDE: return "DE";
case regHL: return nameHLpref (prefix);
case regAF: return useSP ? "SP" : "AF";
default:
throw InvalidRegisterUsed;
}
}
string regw8080Name (regwCode code)
{
switch (code)
{
case regBC: return "B";
case regDE: return "D";
case regHL: return "H";
case regSP: return "SP";
default:
throw InvalidRegisterUsed;
}
}
string regw8080NamePSW (regwCode code)
{
switch (code)
{
case regBC: return "B";
case regDE: return "D";
case regHL: return "H";
case regAF: return "PSW";
default:
throw InvalidRegisterUsed;
}
}
string byteinstName (TypeByteInst ti)
{
switch (ti)
{
case tiADDA: return "ADD A,";
case tiADCA: return "ADC A,";
case tiSUB: return "SUB";
case tiSBCA: return "SBC A,";
case tiAND: return "AND";
case tiXOR: return "XOR";
case tiOR: return "OR";
case tiCP: return "CP";
default:
throw InvalidInstructionType;
}
}
string byteinst8080Name (TypeByteInst ti)
{
switch (ti)
{
case tiADDA: return "ADD";
case tiADCA: return "ADC";
case tiSUB: return "SUB";
case tiSBCA: return "SBB";
case tiAND: return "ANA";
case tiXOR: return "XRA";
case tiOR: return "ORA";
case tiCP: return "CPM";
default:
throw InvalidInstructionType;
}
}
string byteinst8080iName (TypeByteInst ti)
{
switch (ti)
{
case tiADDA: return "ADI";
case tiADCA: return "ACI";
case tiSUB: return "SUI";
case tiSBCA: return "SBI";
case tiAND: return "ANI";
case tiXOR: return "XRI";
case tiOR: return "ORI";
case tiCP: return "CPI";
default:
throw InvalidInstructionType;
}
}
byte getbaseByteInst (TypeByteInst ti, GenCodeMode genmode)
{
static byte byte86 []=
{ 0x00, 0x10, 0x28, 0x18, 0x20, 0x30, 0x08, 0x38 };
switch (genmode)
{
case gen80:
return 0x80 | (ti << 3);
case gen86:
return byte86 [ti];
default:
ASSERT (false);
throw UnexistentMode;
}
}
byte getByteInstInmediate (TypeByteInst ti, GenCodeMode genmode)
{
static byte byte86 []=
{ 0x04, 0x14, 0x2C, 0x1C, 0x24, 0x34, 0x0C, 0x3C };
switch (genmode)
{
case gen80:
return 0xC6 | (ti << 3);
case gen86:
return byte86 [ti];
default:
ASSERT (false);
throw UnexistentMode;
}
}
string nameIdesp (byte prefix, bool hasdesp, byte desp)
{
string r (1, openIndir);
r+= nameHLpref (prefix);
if (hasdesp)
{
r+= '+';
r+= hex2str (desp);
}
r+= closeIndir;
return r;
}
string getregbname (regbCode rb, byte prefix,
bool hasdesp, byte desp)
{
ASSERT (! hasdesp || (rb == reg_HL_ && prefix != prefixNone) );
switch (rb)
{
case regA: return "A";
case regB: return "B";
case regC: return "C";
case regD: return "D";
case regE: return "E";
case regH:
switch (prefix)
{
case prefixNone: return "H";
case prefixIX: return "IXH";
case prefixIY: return "IYH";
default:
throw InvalidPrefixUsed;
}
case regL:
switch (prefix)
{
case prefixNone: return "L";
case prefixIX: return "IXL";
case prefixIY: return "IYL";
default:
throw InvalidPrefixUsed;
}
case reg_HL_:
return nameIdesp (prefix, hasdesp, desp);
default:
ASSERT (false);
throw UnexpectedRegisterCode;
}
}
string getregb8080name (regbCode rb)
{
switch (rb)
{
case regA: return "A";
case regB: return "B";
case regC: return "C";
case regD: return "D";
case regE: return "E";
case regH: return "H";
case regL: return "L";
case reg_HL_: return "M";
default:
ASSERT (false);
throw UnexpectedRegisterCode;
}
}
} // namespace impl
} // namespace pasmo
// End of codeaux.cpp