-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.c
232 lines (224 loc) · 7.23 KB
/
decoder.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include "stack.h"
#include "cpu.h"
#include <assert.h>
void decode(struct chip8 *chip){
while(1){
if(chip->pc >= 0x1000){
perror("Invalid memory address\n");
return;
}
unsigned short instruction = chip->memory[chip->pc] << 8 | chip->memory[chip->pc + 1];
unsigned char opcode = (instruction & 0xf000) >> 12;
switch(opcode){
case 0x0: {
switch(instruction & 0x0fff){
case 0x00e0: {
cls(chip);
}
case 0x00ee: {
ret(chip);
}
}
}
case 0x1: {
unsigned short jump_addr = instruction & 0x0fff;
jmp(chip, jump_addr);
break;
}
case 0x2: {
unsigned short jump_addr = instruction & 0x0fff;
call(chip, jump_addr);
break;
}
case 0x3: {
unsigned short x = (instruction & 0x0f00) >> 8;
unsigned char kk = instruction & 0x00ff;
iskip_on_equal(chip, x, kk);
break;
}
case 0x4: {
unsigned short x = (instruction & 0x0f00) >> 8;
unsigned char kk = instruction & 0x00ff;
iskip_on_not_equal(chip, x, kk);
break;
}
case 0x5: {
unsigned short x = (instruction & 0x0f00) >> 8;
unsigned short y = (instruction & 0x00f0) >> 4;
skip_on_equal(chip, x, y);
break;
}
case 0x6: {
unsigned short x = (instruction & 0x0f00) >> 8;
unsigned char kk = instruction & 0x00ff;
iload(chip, x, kk);
break;
}
case 0x7: {
unsigned short x = (instruction & 0x0f00) >> 8;
unsigned char kk = instruction & 0x00ff;
iadd(chip, x, kk);
break;
}
case 0x8: {
unsigned short x = (instruction & 0x0f00) >> 8;
unsigned short y = (instruction & 0x00f0) >> 4;
switch(instruction & 0xf){
case 0: {
assign(chip, x, y);
break;
}
case 1: {
_or(chip, x, y);
break;
}
case 2: {
_and(chip, x, y);
break;
}
case 3: {
_xor(chip, x, y);
break;
}
case 4: {
add(chip, x, y);
break;
}
case 5: {
sub(chip, x, y);
break;
}
case 6: {
shr(chip, x);
break;
}
case 7: {
subn(chip, x, y);
break;
}
case 0xe: {
shl(chip, x);
break;
}
default: {
perror("Invalid instruction\n");
return;
}
}
break;
}
case 0x9: {
unsigned short x = (instruction & 0x0f00) >> 8;
unsigned short y = (instruction & 0x00f0) >> 4;
skip_on_not_equal(chip, x, y);
break;
}
case 0xA: {
unsigned short nnn = instruction & 0x0fff;;
set_index(chip, nnn);
break;
}
case 0xB: {
unsigned short nnn = instruction & 0x0fff;;
jmp_rel(chip, nnn);
break;
}
case 0xC: {
unsigned short x = (instruction & 0x0f00) >> 8;
unsigned char kk = instruction & 0x00ff;
set_rand(chip, x, kk);
break;
}
case 0xD: {
unsigned short x = (instruction & 0x0f00) >> 8;
unsigned short y = (instruction & 0x00f0) >> 4;
unsigned short n = instruction & 0x000f;
display_sprite(chip, x, y, n);
break;
}
case 0xE: {
switch(instruction & 0xff){
case 0x9e: {
unsigned short x = (instruction & 0x0f00) >> 8;
skip_pressed(chip, x);
break;
}
case 0xa1: {
unsigned short x = (instruction & 0x0f00) >> 8;
skip_not_pressed(chip, x);
break;
}
default: {
perror("Invalid instruction\n");
return;
}
}
break;
}
case 0xF: {
unsigned short x = (instruction & 0x0f00) >> 8;
switch(instruction & 0xff){
case 0x07: {
load_delay(chip, x);
break;
}
case 0x0a: {
wait_for_key(chip, x);
break;
}
case 0x15: {
set_delay(chip, x);
break;
}
case 0x18: {
set_sound(chip, x);
break;
}
case 0x1e: {
add_to_index(chip, x);
break;
}
case 0x29: {
load_location(chip, x);
break;
}
case 0x33: {
store_bcd(chip, x);
break;
}
case 0x55: {
store_registers(chip, x);
break;
}
case 0x65: {
load_registers(chip, x);
}
default: {
perror("Invalid instruction\n");
return;
}
}
break;
}
default: {
perror("Invalid instruction\n");
return;
}
}
}
}
int main(){
struct chip8 *chip = new_chip8();
load_rom(chip, "chip8-test-rom/test_opcode.ch8");
if(errno != EINVAL && errno != ENOMEM){
printf("Successfully loaded ROM in memory\n");
}
decode(chip);
free(chip);
}