summaryrefslogtreecommitdiff
path: root/main.c
blob: e7e5d647406ec49b8c42a047b874ccced211beb5 (plain)
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
/*
 * This program 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.
 *
 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <SDL2/SDL.h>

#include "chip8.h"

/* references
 * https://austinmorlan.com/posts/chip8_emulator/
 * http://www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter/
 * http://mattmik.com/files/chip8/mastering/chip8.html (lots of info)
 * http://devernay.free.fr/hacks/chip8/C8TECH10.HTM#0.1
 * http://www.emulator101.com/chip-8-sprites.html
 * https://slack-files.com/T3CH37TNX-F3RKEUKL4-b05ab4930d?nojsmode=1
 */

#define DEFAULT_VIDEO_SCALE 10
#define DEFAULT_FPS 500

SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;

void init_video();
void update_video();
void toggle_pixel(int x, int y);
void quit();
void usage(char *);
int handle_sdl_events();
void handle_key_down(int keycode);
void handle_key_up(int keycode);
uint8_t get_chip8_key(int keycode);
void print_registers();

int video_scale = DEFAULT_VIDEO_SCALE;
int fps = DEFAULT_FPS;
int step_cycle = 0;
char *rom = NULL;

extern uint32_t video[WIDTH*HEIGHT];
extern int draw_flag;
extern uint8_t key[KEY_SIZE];
extern uint8_t V[REGISTER_COUNT];
extern uint16_t I;
extern uint16_t PC;
extern int8_t SP;
extern uint8_t delay_timer;
extern uint8_t sound_timer;
extern uint16_t stack[STACK_SIZE];

void
quit()
{
	SDL_DestroyTexture(texture);
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();
}

void
init_video()
{
	SDL_Init(SDL_INIT_VIDEO);
	window = SDL_CreateWindow("chip8 interpreter", 0, 0, WIDTH*video_scale, HEIGHT*video_scale, SDL_WINDOW_SHOWN);
	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
}

void
update_video()
{
	SDL_UpdateTexture(texture, NULL, video, sizeof(video[0])*WIDTH);
	SDL_RenderClear(renderer);
	SDL_RenderCopy(renderer, texture, NULL, NULL);
	SDL_RenderPresent(renderer);
}

void
toggle_pixel(int x, int y)
{
	// TODO: clean
	uint32_t dos = video[WIDTH*y+x];
	if (dos <= 0)
		video[WIDTH*y+x] = PIXEL_COLOR;
	else
		video[WIDTH*y+x] = 0;
}

void
print_registers()
{
	puts("DUMP");
	puts("--------------------------");
	for (int reg = 0; reg < REGISTER_COUNT; reg++)
	{
		printf("V[0x%01X] = 0x%02X\n", reg, V[reg]);
	}
	// minus 2 on PC because we increment PC by 2 at the start of decoding
	printf("PC = 0x%03X\nI = 0x%03X\nSP = 0x%02X\ndelay_timer = 0x%02X\nsound_timer = 0x%02X\n", PC-2, I, SP, delay_timer, sound_timer);
	puts("STACK:");
	for (int i = 0; i < 16; i++)
	{
		printf("[%d] = 0x%03X", i, stack[i]);
		if (i == 7) puts("");
	}
	puts("");
	puts("--------------------------");
}

void
handle_key_down(int keycode)
{
	switch (keycode)
	{
	/* detect interpreter debug keys */
	case SDLK_p: print_registers(); break;
	case SDLK_PERIOD: step_cycle = 1; break;
	default: break;
	}

	uint8_t k = get_chip8_key(keycode);
	if (k > 0xF) return; /* unknown key */
	key[k] = 1;
}

void
handle_key_up(int keycode)
{
	uint8_t k = get_chip8_key(keycode);
	if (k > 0xF) return; /* unknown key */
	key[k] = 0;
}

uint8_t
get_chip8_key(int keycode)
{
	switch(keycode)
	{
	case SDLK_1: return 0x1;
	case SDLK_2: return 0x2;
	case SDLK_3: return 0x3;
	case SDLK_4: return 0xC;
	case SDLK_q: return 0x4;
	case SDLK_w: return 0x5;
	case SDLK_e: return 0x6;
	case SDLK_r: return 0xD;
	case SDLK_a: return 0x7;
	case SDLK_s: return 0x8;
	case SDLK_d: return 0x9;
	case SDLK_f: return 0xE;
	case SDLK_z: return 0xA;
	case SDLK_x: return 0x0;
	case SDLK_c: return 0xB;
	case SDLK_v: return 0xF;
	default: return 0xFF;
	}
}

int
handle_sdl_events()
{
	SDL_Event e;
	while (SDL_PollEvent(&e) != 0)
	{
		switch (e.type)
		{
		case SDL_QUIT: return 1;
		case SDL_KEYDOWN:
		{
			switch(e.key.keysym.sym)
			{
			case SDLK_ESCAPE: return 1;
			default:
				handle_key_down(e.key.keysym.sym);
				break;
			}
			break;
		}
		case SDL_KEYUP:
			handle_key_up(e.key.keysym.sym);
			break;
		default: break;
		}
	}
	return 0;
}

int main(int argc, char *argv[])
{
	char opt;
	int index;

	while ((opt = getopt(argc, argv, "s:f:")) != -1)
	{
		switch (opt)
		{
		case 's':
			video_scale = atoi(optarg);
			break;
		case 'f':
			fps = atoi(optarg);
			break;
		default: break;
		}
	}

	for (index = optind; index < argc; index++)
	{
		rom = argv[index];
	}


	chip8_init();

	if (!load_rom(rom))
	{
		fprintf(stderr, "cannot start interpreter\n");
		exit(EXIT_FAILURE);
	}

	init_video();

	const uint32_t frame_delay = 1000/fps;
	uint32_t  frame_start;
	uint32_t frame_time;

	int do_quit = 0;

	while(!do_quit)
	{
		frame_start = SDL_GetTicks();

		// logic
		do_quit = handle_sdl_events();
		chip8_cycle();
		chip8_timer_cycle();

		if (draw_flag)
		{
			update_video();
			draw_flag = 0;
		}

		frame_time = SDL_GetTicks() - frame_start;
		if (frame_delay > frame_time)
		{
			SDL_Delay(frame_delay - frame_time);
		}
	}

	quit();
	return EXIT_SUCCESS;
}