-
Notifications
You must be signed in to change notification settings - Fork 7
/
opcodes.ino
265 lines (249 loc) · 8.46 KB
/
opcodes.ino
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
/*
This file is part of evm-esp32.
evm-esp32 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
evm-esp32 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with evm-esp32. If not, see <http://www.gnu.org/licenses/>.
*/
#include "include/opcodes.h"
#include <analogWrite.h>
void parse_opcode(struct vm *VM, uint16_t opcode)
{
uint8_t i, vx, vy;
switch (opcode) {
case 0x00EE:
/* Returns from a subroutine. */
VM->registers.sp--;
if (VM->registers.sp < 0) {
printf("Fatal error: Stack underflow\n");
VM->halt = 1;
return;
}
VM->registers.pc = VM->stack[VM->registers.sp];
return;
break;
case 0xE0A0:
delay(VM->registers.v[2]);
return;
break;
case 0xE0A2:
/* Read from input (first register) into output (second register) */
VM->registers.v[1] = analogRead(VM->registers.v[0]);
return;
break;
case 0xE0A3:
/* Write to pin (first register) a value (second register) */
analogWrite(VM->registers.v[0], VM->registers.v[1]);
return;
break;
default:
break;
}
switch (opcode & 0xF000) {
case 0x1000:
/* Jumps to address NNN. */
VM->registers.pc = opcode & 0x0FFF;
return;
break;
case 0x2000:
/* Calls subroutine at NNN. */
if (VM->registers.sp > sizeof(VM->stack) / sizeof(uint16_t)) {
printf("Fatal error: Stack overflow\n");
VM->halt = 1;
}
VM->stack[VM->registers.sp] = VM->registers.pc;
VM->registers.sp++;
VM->registers.pc = opcode & 0x0FFF;
return;
break;
case 0x3000:
/* Skips the next instruction if VX equals NN. */
if (VM->registers.v[(opcode & 0x0F00) >> 8] == (opcode & 0x00FF)) {
VM->registers.pc += 2;
}
return;
break;
case 0x4000:
/* Skips the next instruction if VX doesn't equal NN. */
if (VM->registers.v[(opcode & 0x0F00) >> 8] != (opcode & 0x00FF)) {
VM->registers.pc += 2;
}
return;
break;
case 0x6000:
/* Sets VX to NN. */
VM->registers.v[(opcode & 0x0F00) >> 8] = opcode & 0x00FF;
return;
break;
case 0x7000:
/* Adds NN to VX. */
VM->registers.v[(opcode & 0x0F00) >> 8] += opcode & 0x00FF;
return;
break;
case 0xA000:
/* Sets I to the address NNN. */
VM->registers.I = opcode & 0x0FFF;
return;
break;
case 0xB000:
/* Jumps to the address NNN plus V0. */
VM->registers.pc = (opcode & 0x0FFF) + VM->registers.v[0];
return;
break;
case 0xC000:
/* Sets VX to a random number and NN. */
VM->registers.v[(opcode & 0x0F00) >> 8] = (clock() % 255) & (opcode & 0x00FF);
return;
break;
default:
break;
}
switch (opcode & 0xF00F) {
case 0x5000:
/* Skips the next instruction if VX equals VY. */
if (VM->registers.v[(opcode & 0x0F00) >> 8] == VM->registers.v[(opcode & 0x00F0) >> 4]) {
VM->registers.pc += 2;
}
return;
break;
case 0x8000:
/* Sets VX to the value of VY. */
VM->registers.v[(opcode & 0x0F00) >> 8] = VM->registers.v[(opcode & 0x00F0) >> 4];
return;
break;
case 0x8001:
/* Sets VX to VX or VY. */
VM->registers.v[(opcode & 0x0F00) >> 8] |= VM->registers.v[(opcode & 0x00F0) >> 4];
return;
break;
case 0x8002:
/* Sets VX to VX and VY. */
VM->registers.v[(opcode & 0x0F00) >> 8] &= VM->registers.v[(opcode & 0x00F0) >> 4];
return;
break;
case 0x8003:
/* Sets VX to VX xor VY. */
VM->registers.v[(opcode & 0x0F00) >> 8] ^= VM->registers.v[(opcode & 0x00F0) >> 4];
return;
break;
case 0x8004:
/* Adds VY to VX. VF is set to 1 when there's a carry, and to 0 when there isn't. */
vx = VM->registers.v[(opcode & 0x0F00) >> 8];
vy = VM->registers.v[(opcode & 0x00F0) >> 4];
if (vx + vy > 0xFF) {
VM->registers.v[15] = 1;
} else {
VM->registers.v[15] = 0;
}
VM->registers.v[(opcode & 0x0F00) >> 8] += vy;
return;
break;
case 0x8005:
/* VY is subtracted from VX. VF is set to 0 when there's a borrow, and 1 when there isn't. */
vx = VM->registers.v[(opcode & 0x0F00) >> 8];
vy = VM->registers.v[(opcode & 0x00F0) >> 4];
if (vx - vy >= 0) {
VM->registers.v[15] = 1;
} else {
VM->registers.v[15] = 0;
}
VM->registers.v[(opcode & 0x0F00) >> 8] -= vy;
return;
break;
case 0x8006:
/* Shifts VX right by one. VF is set to the value of the least significant bit of VX before the shift. */
VM->registers.v[15] = VM->registers.v[(opcode & 0x0F00) >> 8] & 1;
VM->registers.v[(opcode & 0x0F00) >> 8] >>= 1;
return;
break;
case 0x8007:
/* Sets VX to VY minus VX. VF is set to 0 when there's a borrow, and 1 when there isn't. */
vx = VM->registers.v[(opcode & 0x0F00) >> 8];
vy = VM->registers.v[(opcode & 0x00F0) >> 4];
if (vy - vx >= 0) {
VM->registers.v[15] = 1;
} else {
VM->registers.v[15] = 0;
}
VM->registers.v[(opcode & 0x0F00) >> 8] = vy - VM->registers.v[(opcode & 0x0F00) >> 8];
return;
break;
case 0x800E:
/* Shifts VX left by one. VF is set to the value of the most significant bit of VX before the shift. */
VM->registers.v[15] = (VM->registers.v[(opcode & 0x0F00) >> 8] >> 7) & 0x80;
VM->registers.v[(opcode & 0x0F00) >> 8] <<= 1;
return;
break;
case 0x9000:
/* Skips the next instruction if VX doesn't equal VY. */
if (VM->registers.v[(opcode & 0x0F00) >> 8] != VM->registers.v[(opcode & 0x00F0) >> 4]) {
VM->registers.pc += 2;
}
return;
break;
default:
break;
}
switch (opcode & 0xF0FF) {
case 0xE0A1:
/* Sets pinmode */
pinMode(VM->registers.v[0], (opcode & 0x0F00) >> 8);
return;
break;
case 0xF007:
/* Sets VX to the value of the delay timer. */
VM->registers.v[(opcode & 0x0F00) >> 8] = VM->delay_timer;
return;
break;
case 0xF015:
/* Sets the delay timer to VX. */
VM->delay_timer = VM->registers.v[(opcode & 0x0F00) >> 8];
return;
break;
case 0xF01E:
/* Adds VX to I. */
if ((VM->registers.I += VM->registers.v[(opcode & 0x0F00) >> 8]) > 0x0FFF) {
VM->registers.v[15] = 1;
} else {
VM->registers.v[15] = 0;
}
return;
break;
case 0xF033:
/* Stores the Binary-coded decimal representation of VX, with the most significant
of three digits at the address in I, the middle digit at I plus 1, and the least
significant digit at I plus 2. (In other words, take the decimal representation of VX,
place the hundreds digit in VM at location in I, the tens digit at location I+1,
and the ones digit at location I+2.) */
vx = VM->registers.v[(opcode & 0x0F00) >> 8];
VM->memory[VM->registers.I] = (vx / 100) % 10;
VM->memory[VM->registers.I + 1] = (vx / 10) % 10;
VM->memory[VM->registers.I + 2] = vx % 10;
return;
break;
case 0xF055:
/* Stores V0 to VX in VM starting at address I. */
for (i = 0; i <= ((opcode & 0x0F00) >> 8); i++) {
VM->memory[VM->registers.I++] = VM->registers.v[i];
}
return;
break;
case 0xF065:
/* Fills V0 to VX with values from VM starting at address I. */
for (i = 0; i <= ((opcode & 0x0F00) >> 8); i++) {
VM->registers.v[i] = VM->memory[VM->registers.I++];
}
return;
break;
default:
break;
}
printf("Unknown opcode %X\n", opcode);
VM->halt = 1;
}