-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip8_io.c
311 lines (250 loc) · 8.48 KB
/
chip8_io.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright (C) 2015 Richard Burke
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <math.h>
#include "chip8_io.h"
#include "chip8.h"
#define C8_CYCLE_TIME_MS (1000.0 / C8_TIMER_FREQ_HZ)
#define C8_AUDIO_AMPLITUDE 28000
#define C8_SAMPLE_FRAMES_FREQUENCY 44100
#define C8_AUDIO_FREQUENCY 880
#define C8_PI 3.14159265358979323846
static void io_wait_for_keypress(Chip8 *chip8, int *quit);
static int io_chip8_key_index(uint8_t keyboard_key);
static void io_audio_callback(void *user_data, uint8_t *audio_stream, int length);
static void io_update_audio_state(Chip8IO *io, uint8_t sound_timer);
static const SDL_Scancode io_keyboard_keys[C8_KEY_NUM] = {
SDL_SCANCODE_1, SDL_SCANCODE_2, SDL_SCANCODE_3, SDL_SCANCODE_4,
SDL_SCANCODE_Q, SDL_SCANCODE_W, SDL_SCANCODE_E, SDL_SCANCODE_R,
SDL_SCANCODE_A, SDL_SCANCODE_S, SDL_SCANCODE_D, SDL_SCANCODE_F,
SDL_SCANCODE_Z, SDL_SCANCODE_X, SDL_SCANCODE_C, SDL_SCANCODE_V
};
int io_init(Chip8IO *io, Chip8 *chip8, const Chip8Option *opt)
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0) {
C8_LOG_ERROR("Unable to init SDL %s", SDL_GetError());
return 0;
}
memset(io, 0, sizeof(Chip8IO));
io->timer_args = (Chip8TimerArgs) {
.chip8 = chip8,
.io = io
};
io->scale_factor = opt->scale_factor;
io->instr_per_sec = opt->instr_per_sec;
uint16_t pixel_width = chip8->display_width * opt->scale_factor;
uint16_t pixel_height = chip8->display_height * opt->scale_factor;
io->window = SDL_CreateWindow("CHIP-8 Interpreter", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, pixel_width,
pixel_height, 0);
if (io->window == NULL) {
C8_LOG_ERROR("Unable to create window %s", SDL_GetError());
io_free(io);
return 0;
}
io->renderer = SDL_CreateRenderer(io->window, -1, SDL_RENDERER_ACCELERATED);
if (io->renderer == NULL) {
C8_LOG_ERROR("Unable to create renderer %s", SDL_GetError());
io_free(io);
return 0;
}
io->texture = SDL_CreateTexture(io->renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC, chip8->display_width,
chip8->display_height);
if (io->texture == NULL) {
C8_LOG_ERROR("Unable to create texture %s", SDL_GetError());
io_free(io);
return 0;
}
io->delay_sound_timer = SDL_AddTimer(C8_CYCLE_TIME_MS, io_update_delay_sound_timers, &io->timer_args);
if (io->delay_sound_timer == 0) {
C8_LOG_ERROR("Unable to create timer %s", SDL_GetError());
io_free(io);
return 0;
}
io->timer_lock = SDL_CreateSemaphore(1);
if (io->timer_lock == NULL) {
C8_LOG_ERROR("Unable to create semaphore %s", SDL_GetError());
io_free(io);
return 0;
}
SDL_AudioSpec audio_want, audio_have;
SDL_zero(audio_want);
audio_want.freq = C8_SAMPLE_FRAMES_FREQUENCY;
audio_want.format = AUDIO_S16SYS;
audio_want.channels = 1;
audio_want.samples = 2048;
audio_want.callback = io_audio_callback;
io->audio_dev = SDL_OpenAudioDevice(NULL, 0, &audio_want, &audio_have, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
if (io->audio_dev == 0) {
C8_LOG_ERROR("Unable to open audio: %s\n", SDL_GetError());
io_free(io);
return 0;
}
size_t pixel_bytes = sizeof(uint32_t) * chip8->display_width * chip8->display_height;
io->pixels = malloc(pixel_bytes);
if (io->pixels == NULL) {
C8_LOG_ERROR("Unable to allocate %zu bytes for pixel memory", pixel_bytes);
io_free(io);
return 0;
}
memset(io->pixels, 0, pixel_bytes);
io->draw_rect.w = chip8->display_width * opt->scale_factor;
io->draw_rect.h = chip8->display_height * opt->scale_factor;
return 1;
}
void io_free(Chip8IO *io)
{
if (io == NULL) {
return;
}
free(io->pixels);
if (io->texture != NULL) {
SDL_DestroyTexture(io->texture);
}
if (io->renderer != NULL) {
SDL_DestroyRenderer(io->renderer);
}
if (io->window != NULL) {
SDL_DestroyWindow(io->window);
}
if (io->delay_sound_timer != 0) {
SDL_RemoveTimer(io->delay_sound_timer);
}
if (io->timer_lock != NULL) {
SDL_DestroySemaphore(io->timer_lock);
}
if (io->audio_dev != 0) {
SDL_CloseAudioDevice(io->audio_dev);
}
SDL_Quit();
}
uint32_t io_update_delay_sound_timers(uint32_t interval, void *param)
{
Chip8TimerArgs *timer_args = param;
io_lock_timer(timer_args->io);
c8_update_timers(timer_args->chip8);
uint8_t sound_timer = timer_args->chip8->register_sound_timer;
io_unlock_timer(timer_args->io);
io_update_audio_state(timer_args->io, sound_timer);
return interval;
}
void io_update_display(Chip8IO *io, Chip8 *chip8)
{
if (!chip8->update_display) {
return;
}
for (int k = 0; k < (chip8->display_width * chip8->display_height); k++) {
io->pixels[k] = chip8->display[k] ? 0x00FFFFFF : 0;
}
SDL_UpdateTexture(io->texture, NULL, io->pixels, chip8->display_width * sizeof(uint32_t));
SDL_RenderClear(io->renderer);
SDL_RenderCopy(io->renderer, io->texture, NULL, &io->draw_rect);
SDL_RenderPresent(io->renderer);
chip8->update_display = false;
}
void io_update_key_states(Chip8 *chip8, int *quit)
{
SDL_Event event;
if (chip8->wait_key_V_reg != -1) {
io_wait_for_keypress(chip8, quit);
}
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
*quit = 1;
return;
}
}
const uint8_t *key_states = SDL_GetKeyboardState(NULL);
for (int k = 0; k < C8_KEY_NUM; k++) {
chip8->input_keys[k] = key_states[io_keyboard_keys[k]];
}
}
static void io_wait_for_keypress(Chip8 *chip8, int *quit)
{
SDL_Event event;
while (SDL_WaitEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
*quit = 1;
return;
} else if (event.type == SDL_KEYDOWN) {
int key_index = io_chip8_key_index(event.key.keysym.scancode);
if (key_index != -1) {
chip8->register_V[chip8->wait_key_V_reg] = key_index;
chip8->wait_key_V_reg = -1;
return;
}
}
}
}
static int io_chip8_key_index(uint8_t keyboard_key)
{
for (int k = 0; k < C8_KEY_NUM; k++) {
if (keyboard_key == io_keyboard_keys[k]) {
return k;
}
}
return -1;
}
void io_reset_instruction_timer(Chip8IO *io)
{
io->instruction_timer = SDL_GetTicks();
}
void io_cycle_time_limit(const Chip8IO *io)
{
uint32_t current_time = SDL_GetTicks();
uint32_t cycle_time = 1000 / io->instr_per_sec;
uint32_t sleep_time = 0;
if (current_time - io->instruction_timer < cycle_time) {
sleep_time = cycle_time - (current_time - io->instruction_timer);
}
if (sleep_time > 0) {
SDL_Delay(sleep_time);
}
}
void io_lock_timer(Chip8IO *io)
{
SDL_SemWait(io->timer_lock);
}
void io_unlock_timer(Chip8IO *io)
{
SDL_SemPost(io->timer_lock);
}
static void io_audio_callback(void *user_data, uint8_t *audio_stream, int length)
{
(void)user_data;
static double v = 0;
int16_t *stream = (int16_t *)audio_stream;
length /= 2;
for (int k = 0; k < length; k++) {
stream[k] = C8_AUDIO_AMPLITUDE * sin(v * 2 * C8_PI / C8_SAMPLE_FRAMES_FREQUENCY);
v += C8_AUDIO_FREQUENCY;
}
}
static void io_update_audio_state(Chip8IO *io, uint8_t sound_timer)
{
if (sound_timer > 0) {
if (!io->audio_playing) {
SDL_PauseAudioDevice(io->audio_dev, 0);
io->audio_playing = true;
}
} else if (io->audio_playing) {
SDL_PauseAudioDevice(io->audio_dev, 1);
io->audio_playing = false;
}
}