-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler.c
259 lines (237 loc) · 7.22 KB
/
assembler.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#define MAXLENGTH 3580
#define MAXINSTRUCLEN 4
#define INSTRUCTIONS 35
const char * asm_lookup[INSTRUCTIONS] = {
"CLS",
"RET",
"SYS",
"JP",
"CALL",
"SE",
"SNE",
"SER",
"LD",
"ADD",
"LDR",
"OR",
"AND",
"XOR",
"ADDR",
"SUB",
"SHR",
"SUBR",
"SHL",
"SNER",
"LDI",
"JPO",
"RND",
"DRW",
"SKP",
"SKNP",
"LDRD",
"LDKP",
"LDDR",
"LDST",
"ADDI",
"LDC",
"LDBR",
"LDIR",
"LDRI"
};
unsigned short parse_line(char *line);
char* process_xy(char *line);
char* process_xyk(char *line);
char* process_xyn(char *line);
char* process_xkk(char *line);
FILE* open_file(char *file);
FILE* open_file(char *file) {
size_t size = 0;
FILE *fp;
if ((fp = fopen(file, "rb")) == NULL) {
perror("File not found.\n");
exit(-1);
}
fseek(fp, 0L, SEEK_END);
size = (size_t)ftell(fp);
fseek(fp, 0L, SEEK_SET);
return fp;
}
unsigned short parse_line(char *line) {
int i = 0, asm_size = 0, asm_value = -1;
char *instruc;
unsigned short opcode;
for (i = 0; i <= sizeof(line); i++) {
if (line[i] == 0x20 || line[i] == 0xA || line[i] == 0xD) {
asm_size = i;
break;
}
}
instruc = (char *)malloc(asm_size * sizeof(char));
strncpy(instruc, line, asm_size);
for (i = 0; i < INSTRUCTIONS; i++) {
if (strncmp(asm_lookup[i], instruc, asm_size) == 0) {
asm_value = i;
break;
}
}
if(asm_value < 0) {
exit(-1);
}
switch (asm_value) {
case 0: // 00E0 - CLS
opcode = 0x00E0;
break;
case 1: // 00EE - RET
opcode = 0x00EE;
break;
case 2: // 0nnn - SYS addr - DEPRECATED
opcode = 0x0000;
break;
case 3: // 1nnn - JP addr;
opcode = 0x1000 | strtol(line + asm_size + 1, NULL, 16);
break;
case 4: // 2nnn - CALL addr
opcode = 0x2000 | strtol(line + asm_size + 1, NULL, 16);
break;
case 5: // 3xkk - SE Vx, byte
opcode = 0x3000 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16);
break;
case 6: // 4xkk - SNE Vx, byte
opcode = 0x4000 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16);
break;
case 7: // 5xy0 - SER Vx, Vy
opcode = 0x5000 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16) << 4;
break;
case 8: // 6xkk - LD Vx, byte
opcode = 0x6000 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16);
break;
case 9: // 7xkk - ADD Vx, byte
opcode = 0x7000 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16);
break;
case 10: // 8xy0 - LDR Vx, Vy
opcode = 0x8000 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16) << 4;
break;
case 11: // 8xy1 - OR Vx, Vy
opcode = 0x8001 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16) << 4;
break;
case 12: // 8xy2 - AND Vx, Vy
opcode = 0x8002 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16) << 4;
break;
case 13: // 8xy3 - XOR Vx, Vy
opcode = 0x8003 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16) << 4;
break;
case 14: // 8xy4 - ADDR Vx, Vy
opcode = 0x8004 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16) << 4;
break;
case 15: // 8xy5 - SUB Vx, Vy
opcode = 0x8005 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16) << 4;
break;
case 16: // 8xy6 - SHR Vx
opcode = 0x8006 | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
case 17: // 8xy7 - SUBN Vx, Vy
opcode = 0x8007 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16) << 4;
break;
case 18: // 8xyE - SHL Vx
opcode = 0x800E | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
case 19: // 9xy0 - SNER Vx, Vy
opcode = 0x9000 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16) << 4;
break;
case 20: // Annn - LDI I, addr
opcode = 0xA000 | strtol(line + asm_size + 1, NULL, 16);
break;
case 21: // Bnnn - JPO V0, addr
opcode = 0xB000 | strtol(line + asm_size + 1, NULL, 16);
break;
case 22: // Cxkk - RND Vx, byte
opcode = 0xC000 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16);
break;
case 23: // Dxyn - DRW Vx, Vy, nibble
opcode = 0xD000 | strtol(line + asm_size + 1, NULL, 16) << 8 | strtol(line + asm_size + 3, NULL, 16) << 4 | strtol(line + asm_size + 7, NULL, 16);
break;
case 24: // Ex9E - SKP Vx
opcode = 0xE09E | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
case 25: // ExA1 - SKNP Vx
opcode = 0xE0A1 | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
case 26: // Fx07 - LDRD Vx, (Vx = DT)
opcode = 0xF007 | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
case 27: // Fx0A - LDKP Vx (Keypress)
opcode = 0xF00A | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
case 28: // Fx15 - LDDR Vx (DT = Vx)
opcode = 0xF015 | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
case 29: // Fx18 - LDST Vx, byte (Set ST = Vx)
opcode = 0xF018 | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
case 30: // Fx1E - ADDI Vx (I = I + Vx)
opcode = 0xF01E | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
case 31: // Fx29 - LDC Vx (I = Location of char Vx)
opcode = 0xF029 | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
case 32: // Fx33 - LDBR Vx (Load BCD of Vx to memory)
opcode = 0xF033 | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
case 33: // Fx55 - LDIR Vx (Store registers through Vx to I)
opcode = 0xF055 | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
case 34: // Fx65 - LDRI Vx (Read registers through Vx from I)
opcode = 0xF065 | strtol(line + asm_size + 1, NULL, 16) << 8;
break;
default:
exit(-1);
}
free(instruc);
return opcode;
}
int main(int argc, char ** argv) {
int count = 0;
char buf[30];
unsigned short opcode = 0x0000;
unsigned char op;
unsigned char opcodes[MAXLENGTH];
char *output = "out.ch8";
FILE* fp = NULL;
FILE* ofp = NULL;
memset(buf, 0, sizeof(buf));
memset(opcodes, 0, sizeof(opcodes));
if (argc < 2) {
printf("[-] Error - Usage is ch8asm [chip8 assembly file].\n");
return -1;
}
fp = fopen(argv[1], "r");
while (fgets(buf, 30, fp) != NULL) {
if (buf[0] == '.') {
char *c = strchr(buf, ',');
while(c != NULL) {
op = strtol(c - 2, NULL, 16);
*(opcodes + count) = op;
count += 1;
c = strchr(c + 1, ',');
}
} else {
opcode = parse_line(buf);
op = (opcode & 0xFF00) >> 8;
*(opcodes + count) = op;
op = opcode & 0x00FF;
*(opcodes + count + 1) = op;
count += 2;
}
}
ofp = fopen(output, "wb");
fwrite(opcodes, 1, count, ofp);
fclose(ofp);
fclose(fp);
return 0;
}