-
Notifications
You must be signed in to change notification settings - Fork 0
/
opcodes.lua
executable file
·264 lines (252 loc) · 7.89 KB
/
opcodes.lua
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
263
264
local bit = bit or require "bit32"
local opcodes = {}
opcodes[0xD3] = {instruction = "OUT", imm = 1}
opcodes[0xDB] = {instruction = "IN", imm = 1}
-- http://www.classiccmp.org/dunfield/r/8080.txt
local registers = {
[0]="b",
"c",
"d",
"e",
"h",
"l",
"m",
"a"
}
local register_pairs = {
[0]="bc",
"de",
"hl",
"sp"
}
local condition_codes = {
[0]="NZ",
"Z",
"NC",
"C",
"PO",
"PE",
"P",
"M"
}
for opcode = 0x00, 0xFF do
local d = registers[bit.rshift(bit.band(opcode, 0x38), 3)]
local rp = register_pairs[bit.rshift(bit.band(opcode, 0x30), 4)]
local s = registers[bit.band(opcode, 0x07)]
local foo = {}
if bit.band(opcode, 0xC0) == 0x00 then
if opcode == 0x00 then
foo.instruction = "NOP"
elseif bit.band(opcode, 0x07) == 0x06 then
foo.instruction = "MVI"
foo.op1 = d
foo.imm = 1
elseif bit.band(opcode, 0x07) == 0x01 then
if bit.band(opcode, 0x08) == 0x00 then
foo.instruction = "LXI"
foo.op1 = rp
foo.imm = 2
else
foo.instruction = "DAD"
foo.op1 = rp
end
elseif bit.band(opcode, 0x07) == 0x02 then
if opcode == 0x22 then
foo.instruction = "SHLD"
foo.imm = 2
elseif opcode == 0x2A then
foo.instruction = "LHLD"
foo.imm = 2
elseif opcode == 0x32 then
foo.instruction = "STA"
foo.imm = 2
elseif opcode == 0x3A then
foo.instruction = "LDA"
foo.imm = 2
elseif bit.band(opcode, 0x08) == 0x08 then
foo.instruction = "LDAX"
foo.op1 = rp
-- TODO only BC and DE are allowed
assert(rp == "bc" or rp == "de")
elseif bit.band(opcode, 0x08) == 0x00 then
foo.instruction = "STAX"
foo.op1 = rp
-- TODO only BC and DE are allowed
assert(rp == "bc" or rp == "de")
end
elseif bit.band(opcode, 0x07) == 0x03 then
if bit.band(opcode, 0x08) == 0x00 then
foo.instruction = "INX"
foo.op1 = rp
else
foo.instruction = "DCX"
foo.op1 = rp
end
elseif bit.band(opcode, 0x07) == 0x04 then
foo.instruction = "INR"
foo.op1 = d
elseif bit.band(opcode, 0x07) == 0x05 then
foo.instruction = "DCR"
foo.op1 = d
elseif bit.band(opcode, 0x07) == 0x06 then
-- none?
elseif bit.band(opcode, 0x07) == 0x07 then
local op = bit.rshift(opcode, 3)
if op == 0 then
foo.instruction = "RLC"
elseif op == 1 then
foo.instruction = "RRC"
elseif op == 2 then
foo.instruction = "RAL"
elseif op == 3 then
foo.instruction = "RAR"
elseif op == 4 then
foo.instruction = "DAA"
elseif op == 5 then
foo.instruction = "CMA"
elseif op == 6 then
foo.instruction = "STC"
elseif op == 7 then
foo.instruction = "CMC"
end
end
elseif bit.band(opcode, 0xC0) == 0x40 then
if opcode == 0x76 then
foo.instruction = "HLT"
else
foo.instruction = "MOV"
foo.op1 = d
foo.op2 = s
end
elseif bit.band(opcode, 0xC0) == 0x80 then
local op = bit.rshift(opcode - 0x80, 3)
if op == 0 then
foo.instruction = "ADD"
foo.op1 = s
elseif op == 1 then
foo.instruction = "ADC"
foo.op1 = s
elseif op == 2 then
foo.instruction = "SUB"
foo.op1 = s
elseif op == 3 then
foo.instruction = "SBB"
foo.op1 = s
elseif op == 4 then
foo.instruction = "ANA"
foo.op1 = s
elseif op == 5 then
foo.instruction = "XRA"
foo.op1 = s
elseif op == 6 then
foo.instruction = "ORA"
foo.op1 = s
elseif op == 7 then
foo.instruction = "CMP"
foo.op1 = s
end
elseif bit.band(opcode, 0xC0) == 0xC0 then
local op = bit.rshift(opcode - 0xC0, 3)
if bit.band(opcode, 0x07) == 0x00 then
foo.instruction = "R" .. condition_codes[op]
elseif bit.band(opcode, 0x07) == 0x01 then
if bit.band(opcode, 0x08) == 0x00 then
foo.instruction = "POP"
foo.op1 = rp
if rp == "sp" then
foo.op1 = "psw"
end
else
if op == 1 then
foo.instruction = "RET"
elseif op == 5 then
foo.instruction = "PCHL"
elseif op == 7 then
foo.instruction = "SPHL"
end
end
elseif bit.band(opcode, 0x07) == 0x02 then
foo.instruction = "J" .. condition_codes[op]
foo.imm = 2
elseif bit.band(opcode, 0x07) == 0x03 then
if op == 0 then
foo.instruction = "JMP"
foo.imm = 2
elseif op == 2 then
foo.instruction = "OUT"
foo.imm = 1
elseif op == 3 then
foo.instruction = "IN"
foo.imm = 1
elseif op == 4 then
foo.instruction = "XTHL"
elseif op == 5 then
foo.instruction = "XCHG"
elseif op == 6 then
foo.instruction = "DI"
elseif op == 7 then
foo.instruction = "EI"
end
elseif bit.band(opcode, 0x07) == 0x04 then
foo.instruction = "C" .. condition_codes[op]
foo.imm = 2
elseif bit.band(opcode, 0x07) == 0x05 then
if bit.band(opcode, 0x08) == 0x00 then
foo.instruction = "PUSH"
foo.op1 = rp
if rp == "sp" then
foo.op1 = "psw"
end
else
foo.instruction = "CALL"
foo.imm = 2
end
elseif bit.band(opcode, 0x07) == 0x06 then
if op == 0 then
foo.instruction = "ADI"
foo.imm = 1
elseif op == 1 then
foo.instruction = "ACI"
foo.imm = 1
elseif op == 2 then
foo.instruction = "SUI"
foo.imm = 1
elseif op == 3 then
foo.instruction = "SBI"
foo.imm = 1
elseif op == 4 then
foo.instruction = "ANI"
foo.imm = 1
elseif op == 5 then
foo.instruction = "XRI"
foo.imm = 1
elseif op == 6 then
foo.instruction = "ORI"
foo.imm = 1
elseif op == 7 then
foo.instruction = "CPI"
foo.imm = 1
end
elseif bit.band(opcode, 0x07) == 0x07 then
foo.instruction = "RST"
foo.op1 = op
end
end
if not opcodes[opcode] then
opcodes[opcode] = foo
end
end
-- Undocumented opcodes
opcodes[0x10] = {instruction = "NOP"}
opcodes[0x20] = {instruction = "NOP"}
opcodes[0x30] = {instruction = "NOP"}
opcodes[0x08] = {instruction = "NOP"}
opcodes[0x18] = {instruction = "NOP"}
opcodes[0x28] = {instruction = "NOP"}
opcodes[0x38] = {instruction = "NOP"}
opcodes[0xD9] = {instruction = "RET"}
opcodes[0xCB] = {instruction = "JMP"}
opcodes[0xDD] = {instruction = "CALL"}
opcodes[0xED] = {instruction = "CALL"}
opcodes[0xFD] = {instruction = "CALL"}
return opcodes