Alex 6 年 前
コミット
8795f83f75
共有2 個のファイルを変更した407 個の追加0 個の削除を含む
  1. 5
    0
      Makefile
  2. 402
    0
      main.c

+ 5
- 0
Makefile ファイルの表示

@@ -0,0 +1,5 @@
1
+OBJS = main.c
2
+OBJ_NAME = chip8
3
+
4
+all : $(OBJS)
5
+	gcc -g $(OBJS) -lSDL2 -o $(OBJ_NAME)

+ 402
- 0
main.c ファイルの表示

@@ -0,0 +1,402 @@
1
+#include <stdint.h>
2
+#include <stdio.h>
3
+#include <string.h>
4
+#include <stdlib.h>
5
+#include <time.h>
6
+#include <SDL2/SDL.h>
7
+
8
+#define DEBUG 1
9
+#define TDISP 0
10
+
11
+typedef uint8_t		byte;
12
+typedef uint16_t 	word;
13
+
14
+typedef struct {
15
+	byte memory[0x1000];
16
+	byte screen[64][32];
17
+	byte registers[0x10];
18
+	word stack[24];
19
+	byte input[0x10];
20
+
21
+	word address_I;
22
+	word program_counter;
23
+	byte stack_pointer;
24
+} emulator_state_s;
25
+
26
+byte keymap[0x10] = { 	SDL_SCANCODE_X, SDL_SCANCODE_1, SDL_SCANCODE_2, SDL_SCANCODE_3, SDL_SCANCODE_Q, 
27
+						SDL_SCANCODE_W, SDL_SCANCODE_E, SDL_SCANCODE_A, SDL_SCANCODE_S, SDL_SCANCODE_D, 
28
+						SDL_SCANCODE_Z, SDL_SCANCODE_C, SDL_SCANCODE_4, SDL_SCANCODE_R, SDL_SCANCODE_F, SDL_SCANCODE_V };
29
+
30
+int main(int argc, char **argv) {
31
+	SDL_Window *window = NULL;
32
+	SDL_Renderer *renderer = NULL;
33
+	SDL_Init(SDL_INIT_VIDEO);
34
+
35
+	window = SDL_CreateWindow("Chip8 Emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 64 * 8, 32 * 8, SDL_WINDOW_SHOWN);
36
+	renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED);
37
+	if (renderer == NULL) printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() ); 
38
+	SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
39
+
40
+	srand(time(NULL));
41
+	
42
+	emulator_state_s state = { 0 }; // initialize everything to 0
43
+	state.program_counter = 0x200; // chip8 programs start at 0x200 address space
44
+	
45
+	FILE *f = fopen(argv[1], "rb");
46
+	fread(&state.memory[0x200], 0x1000 - 0x200, 1, f); // see above comment
47
+	fclose(f);
48
+
49
+	SDL_Event e;
50
+
51
+	int delay = 0;
52
+	int sound = 0;
53
+	int frame = 0;
54
+	int press = 0;
55
+	int wait = 0;
56
+
57
+	while (1) {
58
+		if (DEBUG) printf("0x%x ", state.program_counter);
59
+	
60
+		if (frame == 10)
61
+		{
62
+			frame = 0;
63
+			if (delay > 0) --delay;
64
+			if (sound > 0) --sound;
65
+			memset(state.input, 0, 0x10);
66
+			press = 0;
67
+		}
68
+
69
+		clock_t start = clock() / (CLOCKS_PER_SEC/1000);
70
+
71
+		// opcodes are 16 bits
72
+		word opcode = (state.memory[state.program_counter++] << 8) | (state.memory[state.program_counter++]);
73
+
74
+		SDL_PollEvent(&e);
75
+		if (e.type == SDL_QUIT) break;
76
+
77
+		// SDL_PumpEvents();
78
+		const byte *keystate = SDL_GetKeyboardState(NULL);
79
+
80
+		for (int i = 0; i < 0x10 && !press; ++i) {
81
+			if (keystate[keymap[i]]){
82
+				state.input[i] = 1;
83
+				press = 1;
84
+			}
85
+
86
+		}
87
+
88
+		if (wait && !press) continue;
89
+		if (wait && press) 
90
+		{
91
+			for (int i = 0; i < 0x10; ++i) {
92
+				if (state.input[i] != 0)
93
+				{
94
+					state.registers[0xF] = i;
95
+					break;
96
+				}
97
+			}
98
+			wait = 0;
99
+		}
100
+
101
+		switch (opcode & 0xF000) 
102
+		{
103
+			case 0x0000: // screen clear or function return 
104
+				if (opcode == 0x00E0){
105
+					memset(state.screen, 0, 64 * 32);
106
+					SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
107
+					SDL_RenderClear(renderer);
108
+					if (DEBUG) printf("Clearing screen ");
109
+				}
110
+				
111
+				else if (opcode = 0x00EE) {
112
+					state.program_counter = state.stack[--state.stack_pointer];
113
+					if (DEBUG) printf("Returning to %x ", state.stack[state.stack_pointer]);
114
+				}
115
+
116
+				break;
117
+			
118
+			case 0x1000: // goto statement
119
+				state.program_counter = opcode & 0x0FFF;
120
+				if (DEBUG) printf("Jumping to %x ", state.program_counter);
121
+
122
+				break;
123
+
124
+			case 0x2000: // CALL
125
+				state.stack[state.stack_pointer++] = state.program_counter;
126
+				state.program_counter = opcode & 0x0FFF;
127
+				if (DEBUG) printf("Calling subroutine at %x (returning to %x)", state.program_counter, state.stack[state.stack_pointer - 1]);
128
+
129
+				break;
130
+
131
+			case 0x3000:; // skip if reg 0x0F00 is equal to constant 0x00FF 
132
+				int reg = (opcode & 0x0F00) >> 8;
133
+				if (DEBUG) printf("Comparing (==) register %i (%x) to %x ", reg, state.registers[reg], (byte) opcode);
134
+				if (state.registers[reg] == (byte) opcode){
135
+					state.program_counter += 2;
136
+					if (DEBUG) printf("and incrementing program_counter ");
137
+				}
138
+
139
+				break;
140
+
141
+			case 0x4000: // skip if not equal
142
+				reg = (opcode & 0x0F00) >> 8;
143
+				if (DEBUG) printf("Comparing (!=) register %i (%x) to %x ", reg, state.registers[reg], (byte) opcode);
144
+				if (state.registers[reg] != (byte) opcode){
145
+					state.program_counter += 2;
146
+					if (DEBUG) printf("and incrementing program_counter ");
147
+				}
148
+
149
+				break;
150
+
151
+			case 0x5000:; // skip if reg 0x0F00 is equal to 0x00F0
152
+				int first = (opcode & 0x0F00) >> 8;
153
+				int second = (opcode & 0x00F0) >> 4;
154
+				if (DEBUG) printf("Comparing (==) register %i (%x) to %i (%x) ", first, state.registers[first], second, state.registers[second]);
155
+				if (state.registers[first] == state.registers[second]){
156
+					state.program_counter += 2;
157
+					if (DEBUG) printf("and incrementing program_counter ");
158
+				}
159
+
160
+				break;
161
+
162
+			case 0x6000: // set reg to constant
163
+				reg = (opcode & 0x0F00) >> 8;
164
+				state.registers[reg] = opcode & 0x00FF;
165
+				if (DEBUG) printf("Assigning %hx to %i", state.registers[reg], reg);
166
+
167
+				break;
168
+
169
+			case 0x7000: // add constant to reg
170
+				reg = (opcode & 0x0F00) >> 8;
171
+				state.registers[reg] += opcode & 0x00FF;
172
+				if (DEBUG) printf("Add %hi to reg %i (%hi)", opcode & 0x00FF, reg, state.registers[reg]);
173
+				
174
+				break;
175
+
176
+			case 0x8000:; // math/bitop operators
177
+				first = (opcode & 0x0F00) >> 8; // all use XY regs
178
+				second = (opcode & 0x00F0) >> 4;
179
+
180
+				switch (opcode & 0x000F) 
181
+				{
182
+					case 0: // copy 1 register to another
183
+						state.registers[first] = state.registers[second];
184
+						if (DEBUG) printf("Copied register %i to %i (%hx)", first, second, state.registers[first]);
185
+						break;
186
+
187
+					case 1: // X = X | Y
188
+						state.registers[first] |= state.registers[second];
189
+						if (DEBUG) printf("OR register %i and %i (%hx)", first, second, state.registers[first]);
190
+						break;
191
+
192
+					case 2: // X = X & Y
193
+						state.registers[first] &= state.registers[second];
194
+						if (DEBUG) printf("AND register %i and %i (%hx)", first, second, state.registers[first]);
195
+						break;
196
+
197
+					case 3: // X = X ^ Y
198
+						state.registers[first] ^= state.registers[second];
199
+						if (DEBUG) printf("XOR register %i to %i (%hx)", first, second, state.registers[first]);
200
+						break;
201
+
202
+					case 4:; // X = X + Y, VF = X + 5 > 255
203
+						unsigned int add = state.registers[first] + state.registers[second];
204
+						if (add > 255) state.registers[0xF] = 1;
205
+						else state.registers[0xF] = 0;
206
+						state.registers[first] = add & 0x00FF;
207
+						if (DEBUG) printf("ADD register %i to %i (%hx) ", second, first, state.registers[first]);
208
+						if (DEBUG) printf("VF is %hx", state.registers[0xF]);
209
+						break;
210
+
211
+					case 5: // X = X - Y, VF = X - Y < 0
212
+						state.registers[first] -= state.registers[second];
213
+						if (state.registers[first] > state.registers[second]) state.registers[0xF] = 1;
214
+						else state.registers[0xF] = 0;
215
+						if (DEBUG) printf("SUB register %i from %i (%hx) ", second, first, state.registers[first]);
216
+						if (DEBUG) printf("VF is %hx", state.registers[0xF]);
217
+						break;
218
+
219
+					case 6: // set least significant bit of X in VF, bitshift right
220
+						state.registers[0xF] = state.registers[first] & 0x000F;
221
+						state.registers[first] /= 2;
222
+						if (DEBUG) printf("RS register %i (%hx) ", first, second, state.registers[first]);
223
+						if (DEBUG) printf("VF is %hx", state.registers[0xF]);
224
+						break;
225
+
226
+					case 7: // X = Y - X, VF = Y - X < 0
227
+						state.registers[first] = state.registers[second] - state.registers[first];
228
+						if (state.registers[second] > state.registers[first]) state.registers[0xF] = 1;
229
+						else state.registers[0xF] = 0;
230
+						if (DEBUG) printf("SUB %i from %i (%hx) ", first, second, state.registers[first]);
231
+						if (DEBUG) printf("VF is %hx", state.registers[0xF]);
232
+						break;
233
+
234
+					case 0xE:; // set msot significant bit of X in VF, bitshift left
235
+						state.registers[0xF] = (state.registers[first] & 0xF000) >> 12;
236
+						state.registers[first] *= 2;
237
+						if (DEBUG) printf("LS register %i (%hx) ", first, second, state.registers[first]);
238
+						if (DEBUG) printf("VF is %hx", state.registers[0xF]);
239
+						break;
240
+
241
+					default: break;
242
+
243
+				} break;
244
+
245
+			case 0x9000: // skip if two regs are not equal
246
+				first = (opcode & 0x0F00) >> 8;
247
+				second = (opcode & 0x00F0) >> 4;
248
+				if (DEBUG) printf("Comparing (!=) register %i (%hx) to %i (%hx) ", first, state.registers[first], second, state.registers[second]);
249
+				if (state.registers[first] != state.registers[second]) {
250
+					state.program_counter += 2;
251
+					if (DEBUG) printf("and incrementing program_counter");
252
+				}
253
+				break;
254
+
255
+			case 0xA000: // assign I address
256
+				state.address_I = opcode & 0x0FFF;
257
+				if (DEBUG) printf("Assigning %x to I address ", opcode & 0x0FFF);
258
+
259
+				break;
260
+
261
+			case 0xB000: // jump to 0x0FFF + V0
262
+				state.program_counter = (opcode & 0x0FFF) + state.registers[0x0];
263
+				if (DEBUG) printf("Jumping to %x + %hx (%x)", opcode & 0x0FFF, state.registers[0], (opcode & 0x0FFF) + state.registers[0x0]);
264
+
265
+				break;
266
+
267
+			case 0xC000: // random generator
268
+				reg = (opcode & 0x0F00) >> 8;
269
+				state.registers[reg] = (rand() % 255) & ((byte) opcode);
270
+				if (DEBUG) printf("Storing random in %i (%x)\n", reg, state.registers[reg]);
271
+
272
+				break;
273
+
274
+			case 0xD000: // draw sprite
275
+				first = (opcode & 0x0F00) >> 8;
276
+				second = (opcode & 0x00F0) >> 4;
277
+
278
+				int x = state.registers[first];
279
+				int y = state.registers[second];
280
+				int h = (opcode & 0x000F);
281
+
282
+				for (int line = 0; line < h; ++line)
283
+				{
284
+					byte data = state.memory[state.address_I + line];
285
+					for (int xpos = 0; xpos < 8; ++xpos)
286
+					{
287
+						if (!(data & (1 << xpos))) continue;
288
+						if (state.screen[x + (7 - xpos)][y + line]) state.registers[0xF] = 1;
289
+						state.screen[x + (7 - xpos)][y + line] ^= 1;
290
+					}
291
+				}
292
+
293
+				if (DEBUG) printf("Drawing");
294
+				if (!TDISP) break;
295
+				printf("\n");
296
+				for (int y = 0; y < 32; ++y) {
297
+					for (int x = 0; x < 64; ++x) {
298
+						printf(state.screen[x][y] ? "O" : " ");
299
+					}
300
+					printf("\n");
301
+				}
302
+
303
+				break;
304
+
305
+			case 0xE000: // input handling
306
+				reg = (opcode & 0x0F00) >> 8;
307
+				switch ((byte) opcode)
308
+				{
309
+					case 0x9E: // skip if button pressed
310
+						if (DEBUG) printf("Testing if %i is pressed", state.registers[reg]);
311
+						if (state.input[state.registers[reg]]) state.program_counter += 2;
312
+						break;
313
+
314
+					case 0xA1: // skip if button *not* pressed
315
+						if (DEBUG) printf("Testing if %i is not pressed", state.registers[reg]);
316
+						if (!state.input[state.registers[reg]]) state.program_counter += 2;
317
+						break;
318
+				}
319
+				break;
320
+
321
+			case 0xF000: // misc (timer, input, sound, memory)
322
+				reg = (opcode & 0x0F00) >> 8;
323
+				switch ((byte) opcode)
324
+				{
325
+					case 0x07:
326
+						if (DEBUG) printf("Copying delay timer to reg %i (%hx)", reg, state.registers[reg]);
327
+						state.registers[reg] = delay;
328
+						break;
329
+
330
+					case 0x0A:
331
+						wait = 1;
332
+						break;
333
+
334
+					case 0x15:
335
+						delay = state.registers[reg];
336
+						break;
337
+
338
+					case 0x18:
339
+						sound = state.registers[reg];
340
+						break;
341
+
342
+					case 0x1E:
343
+						state.address_I += state.registers[reg];
344
+						if (DEBUG) printf("Adding reg %i (%hx) to I (%x)", reg, state.registers[reg], state.address_I);
345
+						break;
346
+
347
+					case 0x29:
348
+						break; // fontset not implemented yet
349
+
350
+					case 0x33:;
351
+						int value = state.registers[reg];
352
+						state.memory[state.address_I] = value / 100;
353
+						state.memory[state.address_I + 1] = (value / 10) % 10;
354
+						state.memory[state.address_I + 2] = value % 10;
355
+						break;
356
+
357
+					case 0x55:
358
+						for (int i = 0; i < (reg + 1); ++i)
359
+							state.memory[state.address_I + i] = state.registers[i];
360
+						break;
361
+
362
+					case 0x65:
363
+						for (int i = 0; i < (reg + 1); ++i)
364
+							state.registers[i] = state.memory[state.address_I + i];
365
+						break;
366
+
367
+					default:
368
+						break;
369
+				}
370
+				break;
371
+			default: 
372
+				if (DEBUG) printf("%x", opcode);
373
+				break;
374
+		}
375
+		if (DEBUG) printf("\n");
376
+
377
+		for (int x = 0; x < 64; ++x) {
378
+			for (int y = 0; y < 32; ++y)
379
+			{
380
+				SDL_Rect pixel = { .x = (x * 8), .y = (y * 8), .w = 8, .h = 8};
381
+				if (state.screen[x][y])
382
+				{
383
+					SDL_SetRenderDrawColor(renderer, 0xff, 0x69, 0xb4, 0xFF);
384
+					SDL_RenderFillRect(renderer, &pixel);
385
+				}
386
+				else 
387
+				{
388
+					SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF);
389
+					SDL_RenderFillRect(renderer, &pixel);
390
+				}
391
+			}
392
+		}
393
+
394
+		SDL_RenderPresent(renderer);
395
+
396
+		clock_t stop = clock() / (CLOCKS_PER_SEC/1000);
397
+		SDL_Delay(1);
398
+		++frame;
399
+		// SDL_DestroyWindow(window);
400
+		// SDL_Quit();
401
+	}
402
+}