-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstructions.lua
More file actions
290 lines (235 loc) · 7.63 KB
/
instructions.lua
File metadata and controls
290 lines (235 loc) · 7.63 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
--[[
This file implements the instructions for the emulator. More information
about the instructions can be found here:
http://devernay.free.fr/hacks/chip8/C8TECH10.HTM#3.1
--]]
local bit = bit32 or bit
-- How many bytes are used for each character in memory.
local FONT_HEIGHT = 5
-- The most significant bit for an 8 bit number.
local MSB = 0x80
-- Number of pixels for the width of a sprite.
local SPRITE_W = 8
-- Number of opcodes to skip to get to the next instruction.
local NEXT_INSTR = 2
-- The number of bits for one hex digit.
local DIGIT = 4
-- The dimensions of the screen.
local DISPLAY_W = 64
local DISPLAY_H = 32
-- Number of digits in base 10.
local DECIMAL = 10
local instructions = {}
instructions[0x0000] = function(self, opcode)
local address = bit.band(opcode, 0x0FFF)
-- CLS
if (address == 0x00E0) then
for i = 0, (DISPLAY_W * DISPLAY_H) - 1 do
self.display[i] = 0
end
-- RET
elseif (address == 0x00EE) then
self.SP = self.SP - 1
return self.stack[self.SP]
end
end
-- JP addr
instructions[0x1000] = function(self, opcode)
return bit.band(opcode, 0x0FFF)
end
-- CALL addr
instructions[0x2000] = function(self, opcode)
self.stack[self.SP] = self.PC
self.SP = self.SP + 1
return bit.band(opcode, 0x0FFF)
end
-- SE Vx, byte
instructions[0x3000] = function(self, opcode)
local x = bit.rshift(bit.band(opcode, 0x0F00), DIGIT * 2)
local value = bit.band(opcode, 0x00FF)
if (self.V[x] == value) then
return self.PC + NEXT_INSTR
end
end
-- SNE Vx, byte
instructions[0x4000] = function(self, opcode)
local x = bit.rshift(bit.band(opcode, 0x0F00), DIGIT * 2)
local value = bit.band(opcode, 0x00FF)
if (self.V[x] ~= value) then
return self.PC + NEXT_INSTR
end
end
-- SE Vx, Vy
instructions[0x5000] = function(self, opcode)
local x = bit.rshift(bit.band(opcode, 0x0F00), DIGIT * 2)
local y = bit.rshift(bit.band(opcode, 0x00F0), DIGIT)
if (self.V[x] == self.V[y]) then
return self.PC + NEXT_INSTR
end
end
-- LD Vx, byte
instructions[0x6000] = function(self, opcode)
local x = bit.rshift(bit.band(opcode, 0x0F00), DIGIT * 2)
local value = bit.band(opcode, 0x00FF)
self.V[x] = value
end
-- ADD Vx, byte
instructions[0x7000] = function(self, opcode)
local x = bit.rshift(bit.band(opcode, 0x0F00), DIGIT * 2)
local value = bit.band(opcode, 0x00FF)
self.V[x] = (self.V[x] + value) % 0x100
end
instructions[0x8000] = function(self, opcode)
local x = bit.rshift(bit.band(opcode, 0x0F00), DIGIT * 2)
local y = bit.rshift(bit.band(opcode, 0x00F0), DIGIT)
local op = bit.band(opcode, 0x000F)
-- LD Vx, Vy
if (op == 0) then
self.V[x] = self.V[y]
-- OR Vx, Vy
elseif (op == 1) then
self.V[x] = bit.bor(self.V[x], self.V[y])
-- AND Vx, Vy
elseif (op == 2) then
self.V[x] = bit.band(self.V[x], self.V[y])
-- XOR Vx, Vy
elseif (op == 3) then
self.V[x] = bit.bxor(self.V[x], self.V[y])
-- ADD Vx, Vy
elseif (op == 4) then
local sum = self.V[x] + self.V[y]
self.V[0xF] = (sum > 0xFF) and 1 or 0
self.V[x] = (self.V[x] + self.V[y]) % 0x100
-- SUB Vx, Vy
elseif (op == 5) then
self.V[0xF] = (self.V[x] > self.V[y]) and 1 or 0
self.V[x] = (self.V[x] - self.V[y]) % 0x100
-- SHR Vx
elseif (op == 6) then
self.V[0xF] = bit.band(self.V[x], 0x1)
self.V[x] = math.floor(self.V[x] / 2) % 0x100
-- SUBN Vx, Vy
elseif (op == 7) then
self.V[0xF] = (self.V[y] > self.V[x]) and 1 or 0
self.V[x] = (self.V[y] - self.V[x]) % 0x100
-- SHL Vx
elseif (op == 0xE) then
self.V[0xF] = (bit.band(self.V[x], 0x80) == 0x80) and 1 or 0
self.V[x] = math.floor(self.V[x] * 2) % 0x100
end
end
-- SNE Vx, Vy
instructions[0x9000] = function(self, opcode)
local x = bit.rshift(bit.band(opcode, 0x0F00), DIGIT * 2)
local y = bit.rshift(bit.band(opcode, 0x00F0), DIGIT)
if (self.V[x] ~= self.V[y]) then
return self.PC + NEXT_INSTR
end
end
-- LD I, addr
instructions[0xA000] = function(self, opcode)
self.I = bit.band(opcode, 0x0FFF)
end
-- JP V0, addr
instructions[0xB000] = function(self, opcode)
local address = bit.band(opcode, 0x0FFF)
return address + self.V[0]
end
-- RND Vx, byte
instructions[0xC000] = function(self, opcode)
local index = bit.rshift(bit.band(opcode, 0x0F00), DIGIT * 2)
local constant = bit.band(opcode, 0x00FF)
self.V[index] = bit.band(math.random(0, 255), constant)
end
-- DRW Vx, Vy, nibble
instructions[0xD000] = function(self, opcode)
local originX = bit.rshift(bit.band(opcode, 0x0F00), DIGIT * 2)
local originY = bit.rshift(bit.band(opcode, 0x00F0), DIGIT)
local height = bit.band(opcode, 0x000F)
local data = 0x0000
local value = 0
local position = 0
self.V[0xF] = 0
for y = 0, height - 1 do
data = self.memory[self.I + y]
for x = 0, SPRITE_W - 1 do
if (bit.band(data, bit.rshift(MSB, x)) > 0) then
position = ((self.V[originX] + x) % DISPLAY_W)
+ (((self.V[originY] + y) % DISPLAY_H) * DISPLAY_W)
value = self.display[position]
if (value == 1) then
self.V[0xF] = 1
end
self.display[position] = bit.bxor(value, 1)
end
end
end
end
instructions[0xE000] = function(self, opcode)
local x = bit.rshift(bit.band(opcode, 0x0F00), DIGIT * 2)
local op = bit.band(opcode, 0x00FF)
-- SKP Vx
if (op == 0x9E and self.keyboard[self.V[x]]) then
return self.PC + NEXT_INSTR
-- SKPN Vx
elseif (op == 0xA1 and not self.keyboard[self.V[x]]) then
return self.PC + NEXT_INSTR
end
end
instructions[0xF000] = function(self, opcode)
local x = bit.rshift(bit.band(opcode, 0x0F00), DIGIT * 2)
local op = bit.band(opcode, 0x00FF)
-- LD Vx, DT
if (op == 0x07) then
self.V[x] = self.DT
-- LD Vx, K
elseif (op == 0x0A) then
local oldSetKeyDown = self.setKeyDown
-- Pause the program.
self.running = false
-- Keep the program paused until a key press.
self.setKeyDown = function(self, key, down)
if (down) then
-- Store the pressed key.
self.V[x] = key
-- Restart the program.
self.setKeyDown = oldSetKeyDown
self.running = true
end
end
-- LD DT, Vx
elseif (op == 0x15) then
self.DT = self.V[x]
-- LD ST, Vx
elseif (op == 0x18) then
self.ST = self.V[x]
-- ADD I, Vx
elseif (op == 0x1E) then
self.I = self.I + self.V[x]
-- LD F, Vx
elseif (op == 0x29) then
self.I = self.V[x] * FONT_HEIGHT
-- LD B, Vx
elseif (op == 0x33) then
local value = self.V[x]
local ones = value % DECIMAL
value = math.floor(value / DECIMAL)
local tens = value % DECIMAL
value = math.floor(value / DECIMAL)
local hundreds = value % DECIMAL
self.memory[self.I] = hundreds
self.memory[self.I + 1] = tens
self.memory[self.I + 2] = ones
-- LD [I], Vx
elseif (op == 0x55) then
for i = 0, x do
self.memory[self.I + i] = self.V[i]
end
-- LD Vx, [I]
elseif (op == 0x65) then
for i = 0, x do
self.V[i] = self.memory[self.I + i]
end
end
end
return instructions