-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip8.c
239 lines (195 loc) · 6.42 KB
/
chip8.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
/*
* 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 <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <getopt.h>
#include "chip8.h"
#include "chip8_core.h"
#include "chip8_io.h"
#define C8_INSTR_PER_SEC_DEFAULT 300
#define C8_INSTR_PER_SEC_MIN 1
#define C8_SCALE_FACTOR_DEFAULT 8
#define C8_SCALE_FACTOR_MIN 1
#define C8_SCALE_FACTOR_MAX 16
static bool c8_parse_args(Chip8Option *opt, int argc, char *argv[]);
static void c8_print_usage(void);
static bool c8_parse_int(const char *string_value, int *int_ptr);
static bool c8_load(Chip8 *chip8, const char *rom_file_path);
int main(int argc, char *argv[])
{
Chip8Option opt = {
.rom_file_path = NULL,
.scale_factor = C8_SCALE_FACTOR_DEFAULT,
.instr_per_sec = C8_INSTR_PER_SEC_DEFAULT
};
if (!c8_parse_args(&opt, argc, argv)) {
c8_print_usage();
return 1;
}
Chip8 chip8;
c8_init(&chip8);
if (!c8_load(&chip8, opt.rom_file_path)) {
return false;
}
Chip8IO io;
if (!io_init(&io, &chip8, &opt)) {
return 1;
}
int quit = 0;
while (!quit) {
io_reset_instruction_timer(&io);
io_lock_timer(&io);
c8_run_cycle(&chip8);
io_unlock_timer(&io);
io_update_display(&io, &chip8);
io_update_key_states(&chip8, &quit);
io_cycle_time_limit(&io);
}
io_free(&io);
return 0;
}
static bool c8_parse_args(Chip8Option *opt, int argc, char *argv[])
{
struct option chip8_options[] = {
{ "help" , no_argument , 0, 'h' },
{ "instr-rate" , optional_argument, 0, 'r' },
{ "scale-factor", optional_argument, 0, 's' },
{ 0, 0, 0, 0 }
};
int ch;
while ((ch = getopt_long(argc, argv, "hs:r:", chip8_options, NULL)) != -1) {
switch (ch) {
case 'h': {
c8_print_usage();
exit(0);
}
case 'r': {
if (!c8_parse_int(optarg, &opt->instr_per_sec) ||
opt->instr_per_sec < C8_INSTR_PER_SEC_MIN) {
fprintf(stderr,
"Invalid value passed for instr-rate: %s, "
"instr-rate must be an integer greater than %d\n",
optarg, C8_INSTR_PER_SEC_MIN - 1);
return false;
}
break;
}
case 's': {
if (!c8_parse_int(optarg, &opt->scale_factor) ||
opt->scale_factor < C8_SCALE_FACTOR_MIN ||
opt->scale_factor > C8_SCALE_FACTOR_MAX) {
fprintf(stderr,
"Invalid value passed for scale-factor: %s, "
"scale-factor must be an integer between %d and %d inclusive\n",
optarg, C8_SCALE_FACTOR_MIN, C8_SCALE_FACTOR_MAX);
return false;
}
break;
}
case '?': {
return false;
}
default: {
return false;
}
}
}
if (optind < argc) {
opt->rom_file_path = argv[optind];
} else {
fprintf(stderr, "No ROM file path provided\n");
return false;
}
return true;
}
static void c8_print_usage(void)
{
const char *help_msg =
"\n\
CHIP-8 Interpreter\n\
\n\
Usage:\n\
chip8 [OPTIONS] ROMFILE\n\
\n\
ROMFILE:\n\
File path to a CHIP-8 ROM (required).\n\
\n\
OPTIONS:\n\
-h, --help Print this message.\n\
-r, --instr-rate=RATE Run (roughly) RATE instructions per second.\n\
Default: %d, Min: %d.\n\
-s, --scale-factor=FACTOR Scale display resolution by FACTOR.\n\
Default: %d, Min: %d, Max: %d.\n\
\n\
";
printf(help_msg, C8_INSTR_PER_SEC_DEFAULT, C8_INSTR_PER_SEC_MIN,
C8_SCALE_FACTOR_DEFAULT, C8_SCALE_FACTOR_MIN, C8_SCALE_FACTOR_MAX);
}
static bool c8_parse_int(const char *string_value, int *int_ptr)
{
if (string_value == NULL || int_ptr == NULL) {
return false;
}
char *end_ptr;
errno = 0;
long val = strtol(string_value, &end_ptr, 10);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
(errno != 0 && val == 0) || end_ptr == string_value) {
return false;
}
if (val > INT_MAX || val < INT_MIN) {
return false;
}
*int_ptr = (int)val;
return true;
}
static bool c8_load(Chip8 *chip8, const char *rom_file_path)
{
FILE *rom_file = fopen(rom_file_path, "rb");
if (rom_file == NULL) {
fprintf(stderr, "Unable to open file %s for reading - %s\n",
rom_file_path, strerror(errno));
return false;
}
fseek(rom_file, 0, SEEK_END);
long rom_size = ftell(rom_file);
fseek(rom_file, 0, SEEK_SET);
if (ferror(rom_file) || rom_size < 0) {
fprintf(stderr, "Unable to determine size of file %s - %s\n",
rom_file_path, strerror(errno));
return false;
} else if (rom_size > C8_PROGRAM_MEMORY_SIZE) {
fprintf(stderr, "Size of ROM file %s exceeds CHIP-8 "
"program memory space\n", rom_file_path);
return false;
}
size_t read = fread(chip8->memory + C8_PROGRAM_MEMORY_START,
1, C8_PROGRAM_MEMORY_SIZE, rom_file);
bool error = ferror(rom_file);
fclose(rom_file);
if (error) {
fprintf(stderr, "Error when reading file %s - %s\n",
rom_file_path, strerror(errno));
return false;
} else if ((long)read != rom_size) {
fprintf(stderr, "Reading ROM file %s data failed", rom_file_path);
return false;
}
return true;
}