-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisassembler.c
292 lines (278 loc) · 9.46 KB
/
disassembler.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <inttypes.h>
typedef struct {
uint8_t* data;
size_t size;
} Rom;
void RomInit(Rom* rom, const char* filename) {
assert(rom != NULL);
int rom_fd = open(filename, O_RDONLY);
if (rom_fd == -1) {
char* err_msg = (char*)calloc(strlen("open ") + strlen(filename) + 1, sizeof(char));
strcat(err_msg, "open ");
strcat(err_msg, filename);
perror(err_msg);
exit(EXIT_FAILURE);
}
off_t nseek = lseek(rom_fd, 0L, SEEK_END);
if (nseek == -1) {
perror("lseek end");
exit(EXIT_FAILURE);
}
if (lseek(rom_fd, 0L, SEEK_SET) == -1) {
perror("lseek start");
exit(EXIT_FAILURE);
}
rom->data = calloc(nseek, sizeof(uint8_t));
rom->size = nseek;
ssize_t nread = read(rom_fd, rom->data, nseek);
if (nread == -1) {
perror("read rom");
exit(EXIT_FAILURE);
}
if (close(rom_fd) == -1) {
perror("close rom");
exit(EXIT_FAILURE);
}
// CHIP-8 instructions are stored big endian, so we convert the ROM to host byte order.
for (size_t i = 0; i < rom->size; i += 2) {
uint16_t instruction = (rom->data[i] << 0) | (rom->data[i+1] << 8);
instruction = ntohs(instruction);
memcpy(&rom->data[i], &instruction, 2);
}
return ;
}
int main(int argc, char** argv) {
if (argc != 2) {
fprintf(stderr, "usage: disassembler <rom-file>");
fflush(stderr);
exit(EXIT_FAILURE);
}
Rom rom;
RomInit(&rom, argv[1]);
for (size_t i = 0; i < rom.size; i += 2) {
uint16_t instruction = rom.data[i] | (rom.data[i+1] << 8);
printf("0x%04" PRIx16 ": ", instruction);
switch (instruction & 0xF000) {
case 0x0000: {
switch (instruction) {
case 0x00E0: {
printf("CLS\n");
break;
}
case 0x00EE: {
printf("RET\n");
break;
}
default: {
uint16_t addr = instruction & 0x0FFF;
printf("SYS %d\n", addr);
}
}
break;
}
case 0x1000: {
uint16_t addr = instruction & 0x0FFF;
printf("JP %d\n", addr);
break;
}
case 0x2000: {
uint16_t addr = instruction & 0x0FFF;
printf("CAL %d\n", addr);
break;
}
case 0x3000: {
uint8_t reg = (instruction & 0x0F00) >> 8;
uint8_t val = instruction & 0x00FF;
printf("SE V%d, %d\n", reg, val);
break;
}
case 0x4000: {
uint8_t reg = (instruction & 0x0F00) >> 8;
uint8_t val = instruction & 0x00FF;
printf("SNE V%d, %d\n", reg, val);
break;
}
case 0x5000: {
uint8_t lreg = (instruction & 0x0F00) >> 8;
uint8_t rreg = (instruction & 0x00F0) >> 4;
printf("SE V%d, V%d\n", lreg, rreg);
break;
}
case 0x6000: {
uint8_t reg = (instruction & 0x0F00) >> 8;
uint8_t val = instruction & 0x00FF;
printf("LD V%d, %d\n", reg, val);
break;
}
case 0x7000: {
uint8_t reg = (instruction & 0x0F00) >> 8;
uint8_t val = instruction & 0x00FF;
printf("ADD V%d, %d\n", reg, val);
break;
}
case 0x8000: {
uint8_t lreg = (instruction & 0x0F00) >> 8;
uint8_t rreg = (instruction & 0x00F0) >> 4;
switch (instruction & 0x000F) {
case 0x0000: {
printf("LD V%d, V%d\n", lreg, rreg);
break;
}
case 0x0001: {
printf("OR V%d, V%d\n", lreg, rreg);
break;
}
case 0x0002: {
printf("AND V%d, V%d\n", lreg, rreg);
break;
}
case 0x0003: {
printf("XOR V%d, V%d\n", lreg, rreg);
break;
}
case 0x0004: {
printf("ADD V%d, V%d\n", lreg, rreg);
break;
}
case 0x0005: {
printf("SUB V%d, V%d\n", lreg, rreg);
break;
}
case 0x0006: {
printf("SHR V%d {, V%d}\n", lreg, rreg);
break;
}
case 0x0007: {
printf("SUBN V%d, V%d\n", lreg, rreg);
break;
}
case 0x000E: {
printf("SHL V%d {, V%d}\n", lreg, rreg);
break;
}
default: {
fprintf(stderr, "unknown 0x80 instruction: %04" PRIx16 "\n", instruction);
fflush(stderr);
exit(EXIT_FAILURE);
}
}
break;
}
case 0x9000: {
uint8_t lreg = (instruction & 0x0F00) >> 8;
uint8_t rreg = (instruction & 0x00F0) >> 4;
printf("SNE V%d, V%d\n", lreg, rreg);
break;
}
case 0xA000: {
uint16_t addr = instruction & 0x0FFF;
printf("LD I, %d\n", addr);
break;
}
case 0xB000: {
uint16_t addr = instruction & 0x0FFF;
printf("JP V0, %d\n", addr);
break;
}
case 0xC000: {
uint8_t reg = (instruction & 0x0F00) >> 8;
uint8_t val = instruction & 0x00FF;
printf("RND V%d, %d\n", reg, val);
break;
}
case 0xD000: {
uint8_t lreg = (instruction & 0x0F00) >> 8;
uint8_t rreg = (instruction & 0x00F0) >> 4;
uint8_t nbytes = instruction & 0x000F;
printf("DRW V%d, V%d, %d\n", lreg, rreg, nbytes);
break;
}
case 0xE000: {
uint8_t reg = (instruction & 0x0F00) >> 8;
switch (instruction & 0x00FF) {
case 0x009E: {
printf("SKP V%d\n", reg);
break;
}
case 0x00A1: {
printf("SKNP V%d\n", reg);
break;
}
default: {
fprintf(stderr, "unknown 0xE0 instruction: %04" PRIx16 "\n", instruction);
fflush(stderr);
exit(EXIT_FAILURE);
}
}
break;
}
case 0xF000: {
uint8_t reg = (instruction & 0x0F00) >> 8;
switch (instruction & 0x00FF) {
case 0x0007: {
printf("LD V%d, DT\n", reg);
break;
}
case 0x000A: {
printf("LD V%d, K\n", reg);
break;
}
case 0x0015: {
printf("LD DT, V%d\n", reg);
break;
}
case 0x0018: {
printf("LD ST, V%d\n", reg);
break;
}
case 0x001E: {
printf("ADD I, V%d\n", reg);
break;
}
case 0x0029: {
printf("LD F, V%d\n", reg);
break;
}
case 0x0033: {
printf("LD B, V%d\n", reg);
break;
}
case 0x0055: {
printf("LD [I], V%d\n", reg);
break;
}
case 0x0065: {
printf("LD V%d, [I]\n", reg);
break;
}
default: {
fprintf(stderr, "unknown 0xF0 instruction: %04" PRIx16 "\n", instruction);
fflush(stderr);
exit(EXIT_FAILURE);
}
}
break;
}
default: {
fprintf(stderr, "unknown instruction: %04" PRIx16 "\n", instruction);
fflush(stderr);
exit(EXIT_FAILURE);
}
}
}
// char* text = calloc(100, sizeof(char));
// size_t text_size = 100;
// for (size_t i; i < rom.size; ++i) {
// uint16_t inst = UnpackInstruction(rom.data[i]);
// }
}