-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip8_interpreter.c
266 lines (234 loc) · 7.64 KB
/
chip8_interpreter.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
/* José Guilherme de C. Rodrigues - 03/2020 */
#include "chip8_interpreter.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main(int argc, char **argv) {
for (int i = 0; i < argc; ++i) {
if (strcmp(argv[i], "--help") == 0) {
show_help();
return 0;
}
}
if (argc >= 2) {
program_struct ps;
// Default values for args that weren't given.
bool debug = false;
uint8_t scale = 10;
uint32_t cycle_ms = 16;
if (argc >= 3) {
debug = (strcmp(argv[2], "true") == 0) ? true : false;
if (argc >= 4) {
scale = atoi(argv[3]);
if (argc == 5)
cycle_ms = atoi(argv[4]);
}
}
initialize(&ps, argv[1], debug, scale, cycle_ms);
while (ps.running) {
SDL_Delay(ps.cycle_ms);
update(&ps);
if (ps.chip->status.need_redraw)
render(&ps);
}
destroy(&ps);
} else {
show_help();
}
return 0;
}
void initialize(program_struct *ps, const char* program, bool debug, uint8_t scale, uint32_t cycle_ms) {
ps->running = false;
ps->paused = false;
SDL_Init(SDL_INIT_VIDEO);
// Window size
const uint16_t window_width = DISPLAY_WIDTH * scale;
const uint16_t window_height = DISPLAY_HEIGHT * scale;
ps->window = SDL_CreateWindow(
"CHIP 8 Interpreter - José Guilherme de C. Rodrigues", // Window Title
SDL_WINDOWPOS_UNDEFINED, // Initial X position
SDL_WINDOWPOS_UNDEFINED, // Initial Y position
window_width, // WIDTH
window_height, // HEIGHT
SDL_WINDOW_OPENGL // FLAGS
);
// If window successfully created.
if (ps->window) {
ps->renderer = SDL_CreateRenderer(ps->window, -1, 0);
// If renderer successfully created.
if (ps->renderer) {
// Set the size of the pixel_rect.
ps->pixel_rect.w = scale;
ps->pixel_rect.h = scale;
ps->chip = create_chip8(debug);
if (load_program(ps->chip, program)) {
ps->running = true;
ps->cycle_ms = cycle_ms;
}
} else {
fprintf(stderr, "An error occurred when creating the renderer. %s\n", SDL_GetError());
}
} else {
fprintf(stderr, "An error occurred when creating the Window. %s\n", SDL_GetError());
}
}
void update(program_struct *ps) {
// Process SDL events.
while (SDL_PollEvent(&ps->event)) {
if (ps->event.type == SDL_QUIT) {
ps->running = false;
} else if (ps->event.type == SDL_KEYDOWN) { // Key down event.
process_interpreter_key_event(ps, &ps->event.key.keysym);
process_key_event(ps, &ps->event.key.keysym, true);
} else if (ps->event.type == SDL_KEYUP) { // Key up event.
// The key should not be active in chip8.
process_key_event(ps, &ps->event.key.keysym, false);
}
}
if (!ps->paused) {
// Simulates a chip8 cycle.
tick(ps->chip);
// Will wait for a keystroke if the chip8 status need_keystroke is set. (Must be done if using chip8.h)
wait_for_keystroke(ps);
if (ps->chip->status.need_sound) {
// TODO: Play a sound.
ps->chip->status.need_sound = false;
}
}
}
void render(program_struct *ps) {
// Clear the screen with the black color.
SDL_SetRenderDrawColor(ps->renderer, 0x0, 0x0, 0x0, 0xFF);
SDL_RenderClear(ps->renderer);
// Set the color to white to render the active pixels.
SDL_SetRenderDrawColor(ps->renderer, 0xFF, 0xFF, 0xFF, 0xFF);
for (uint8_t y = 0; y < DISPLAY_HEIGHT; ++y) {
for (uint8_t x = 0; x < DISPLAY_WIDTH; ++x) {
if (ps->chip->display[y][x]) {
ps->pixel_rect.x = x * ps->pixel_rect.w;
ps->pixel_rect.y = y * ps->pixel_rect.h;
SDL_RenderFillRect(ps->renderer, &ps->pixel_rect);
}
}
}
// Shows the rendered screen.
SDL_RenderPresent(ps->renderer);
// Disable chip8 need_redraw status. (Should be done if using chip8.h)
ps->chip->status.need_redraw = false;
}
// TODO: Maybe put this functionality inside the event loop in update...
// This function will wait until a key is pressed before letting the interpreter continue.
void wait_for_keystroke(program_struct *ps) {
if (!ps->paused) {
while (ps->running && ps->chip->status.need_keystroke) {
while (SDL_PollEvent(&ps->event)) {
if (ps->event.type == SDL_QUIT) {
ps->running = false;
} else if (ps->event.type == SDL_KEYDOWN /*&& ps->event.key.repeat == 0 Don't need to activate key in case it's a repeated signal */) {
process_key_event(ps, &ps->event.key.keysym, true);
}
}
}
}
}
void process_interpreter_key_event(program_struct *ps, SDL_Keysym *keysim) {
if (ps->event.key.keysym.sym == SDLK_UP) {
INTERPRETER_LOG("Increasing Cycle Ms: {%u}.\n", ++ps->cycle_ms);
} else if (ps->event.key.keysym.sym == SDLK_DOWN) {
if (ps->cycle_ms != 0)
INTERPRETER_LOG("Decreasing Cycle Ms: {%u}.\n", --ps->cycle_ms);
else
INTERPRETER_LOG("Minimum Cycle Ms is 0.\n");
} else if (ps->event.key.keysym.sym == SDLK_SPACE) {
ps->chip->status.debug = !ps->chip->status.debug;
INTERPRETER_LOG("Debug %s.\n", (ps->chip->status.debug) ? "enabled" : "disabled");
} else if (ps->event.key.keysym.sym == SDLK_p) {
ps->paused = !ps->paused;
INTERPRETER_LOG("Pause %s.\n", (ps->paused) ? "enabled" : "disabled");
} else if (ps->event.key.keysym.sym == SDLK_i) {
print_registers(ps->chip);
} else if (ps->event.key.keysym.sym == SDLK_k) {
print_keyboard(ps->chip);
} else if (ps->event.key.keysym.sym == SDLK_m) {
print_memory_in_range(ps->chip, 0x0000, 0xfff);
}
}
/*
MAPS INTO
CHIP 8 Keyboard | QWERTY Keyboard
1 2 3 C | 1 2 3 4
4 5 6 D | Q W E R
7 8 9 E | A S D F
A 0 B F | Z X C V
*/
void process_key_event(program_struct *ps, SDL_Keysym *keysim, bool value) {
if (!ps->paused) {
uint8_t key = 0x10;
// Maps each key to it's respective chip8 keyboard key.
if (keysim->sym == SDLK_1)
key = K_1;
else if (keysim->sym == SDLK_2)
key = K_2;
else if (keysim->sym == SDLK_3)
key = K_3;
else if (keysim->sym == SDLK_4)
key = K_C;
else if (keysim->sym == SDLK_q)
key = K_4;
else if (keysim->sym == SDLK_w)
key = K_5;
else if (keysim->sym == SDLK_e)
key = K_6;
else if (keysim->sym == SDLK_r)
key = K_D;
else if (keysim->sym == SDLK_a)
key = K_7;
else if (keysim->sym == SDLK_s)
key = K_8;
else if (keysim->sym == SDLK_d)
key = K_9;
else if (keysim->sym == SDLK_f)
key = K_E;
else if (keysim->sym == SDLK_z)
key = K_A;
else if (keysim->sym == SDLK_x)
key = K_0;
else if (keysim->sym == SDLK_c)
key = K_B;
else if (keysim->sym == SDLK_v)
key = K_F;
if (key < 0x10)
change_key(ps->chip, key, value);
}
}
void destroy(program_struct *ps) {
delete_chip8(ps->chip);
SDL_DestroyRenderer(ps->renderer);
SDL_DestroyWindow(ps->window);
SDL_Quit();
}
void show_help() {
puts(
"chip8_interpreter program.ch8 <debug> <scale> <cycle_ms>\n"
"Parameters in <> are optional, but if they are given, the order must be followed.\n"
"debug = true to enable or anything else to disable (shows debug information while running).\n"
"scale = int8_t (will scale the dimensions of the original 64x32 chip8 display).\n"
"cycle_ms = int32_t (the amount of time the interpreter will sleep between each cycle).\n"
"--help will show this message and exit the program.\n"
" MAPS INTO\n"
"CHIP 8 Keyboard | QWERTY Keyboard\n"
"1 2 3 C | 1 2 3 4\n"
"4 5 6 D | Q W E R\n"
"7 8 9 E | A S D F\n"
"A 0 B F | Z X C V\n"
"Interpreter keys:\n"
"Up will increase cycle_ms\n"
"Down will decrease cycle_ms\n"
"Space will enable/disable debug\n"
"P will pause the interpreter\n"
"I will print registers state\n"
"K will print keyboard state\n"
"M will print the whole memory (this might be really big for your console's window)\n"
"Made by Jose Guilherme de C. Rodrigues 03/2020."
);
}