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
|
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include "lodepng.h"
#include "gb.h"
#define NUM_FRAMES 600
// #define SKIP_STEPS 1000000
#define NUM_STEPS 300000
#define UNUSED(a) ((void) (a))
static uint32_t
file_size(
const char * const path
) {
struct stat st;
if (stat(path, &st)) {
perror("stat():");
exit(EXIT_FAILURE);
}
return st.st_size;
}
static const uint8_t *
load(
const char * const path,
uint32_t * const r_file_size
) {
const uint32_t rom_size = file_size(path);
uint8_t *rom_data = malloc(rom_size);
if (!rom_data) {
perror("malloc():");
exit(EXIT_FAILURE);
}
FILE *fh = fopen(path, "rb");
if (!fh) {
perror("fopen():");
exit(EXIT_FAILURE);
}
if (!fread(rom_data, rom_size, 1, fh)) {
perror("fread():");
exit(EXIT_FAILURE);
}
fclose(fh);
if (r_file_size) {
*r_file_size = (uint32_t) rom_size;
}
return rom_data;
}
// buffer for rendered frames
static uint8_t frames[NUM_FRAMES * GB_FRAME_SIZE];
static void
on_set_rom_bank(
const gb_t * const ctx,
const uint16_t bank
) {
printf("set rom bank: bank = %u, PC = %04x\n", bank, ctx->cpu.rs[5]);
}
static void
on_set_ram_bank(
const gb_t * const ctx,
const uint16_t bank
) {
printf("set ram bank: bank = %u, PC = %04x\n", bank, ctx->cpu.rs[5]);
}
static void
on_vblank(
const gb_t * const ctx
) {
printf("vblank: PC = %04x\n", ctx->cpu.rs[5]);
}
static void
on_hblank(
const gb_t * const ctx
) {
printf("hblank: PC = %04x\n", ctx->cpu.rs[5]);
}
static void
on_timer(
const gb_t * const ctx
) {
printf("timer: PC = %04x\n", ctx->cpu.rs[5]);
}
/*
* static void
* on_gpu_step(
* const gb_t * const ctx
* ) {
* printf("gpu step: gpu clock = 0x%04x\n", ctx->gpu.clock);
* }
*/
static void
on_gpu_set_mode(
const gb_t * const ctx,
const uint8_t mode
) {
UNUSED(ctx);
printf("gpu mode: %u, line = %u\n", mode, ctx->gpu.line);
}
static void
on_set_cpu_state(
const gb_t * const ctx,
const gb_cpu_state_t state
) {
printf("cpu state: PC = %04x, state = %d\n", ctx->cpu.rs[5], state);
}
static void
on_rst(
const gb_t * const ctx,
const uint16_t addr
) {
UNUSED(ctx);
printf("rst: addr = %04x\n", addr);
}
static const gb_config_t
EXECUTE_CONFIG = {
.on_set_rom_bank = on_set_rom_bank,
.on_set_ram_bank = on_set_ram_bank,
.on_vblank = on_vblank,
.on_hblank = on_hblank,
.on_timer = on_timer,
// .on_gpu_step = on_gpu_step,
.on_gpu_set_mode = on_gpu_set_mode,
.on_set_cpu_state = on_set_cpu_state,
.on_rst = on_rst,
};
static void
test_render_frames(
const char * const png_path,
const char * const rom_path
) {
// load rom data
uint32_t rom_size = 0;
const uint8_t *rom_data = load(rom_path, &rom_size);
fprintf(stderr, "loaded \"%s\" (%u bytes)\n", rom_path, rom_size);
// init context
gb_t ctx;
gb_init(&ctx, NULL, rom_data, rom_size);
fprintf(stderr, "gb context initialized\n");
// render frames
for (size_t j = 0; j < NUM_FRAMES; j++) {
// render frame
gb_frame(&ctx);
// copy frame to buffer
memcpy(frames + GB_FRAME_SIZE * j, gb_get_rgb_frame(&ctx), GB_FRAME_SIZE);
}
// save png
if (lodepng_encode24_file(png_path, frames, 160, 144 * NUM_FRAMES)) {
fprintf(stderr, "lode_png_encode24_file() failed\n");
exit(EXIT_FAILURE);
}
fprintf(stderr, "%d frames rendered\n", NUM_FRAMES);
// free rom data
free((void*) rom_data);
}
const char * const RWS[] = {
"AF",
"BC",
"DE",
"HL",
"SP",
"PC",
NULL
};
static void
gb_dump_context(
gb_t * const ctx
) {
for (int i = 0; RWS[i]; i++) {
printf("%s:%04X ", RWS[i], ctx->cpu.rs[i]);
}
char buf[128];
size_t buf_len = sizeof(buf);
if (gb_disasm(ctx, buf, &buf_len)) {
printf("%s\n", buf);
} else {
printf("\n");
}
}
static void
test_execute_steps(
const char * const rom_path
) {
// load rom data
uint32_t rom_size = 0;
const uint8_t *rom_data = load(rom_path, &rom_size);
fprintf(stderr, "loaded \"%s\" (%u bytes)\n", rom_path, rom_size);
// init context
gb_t ctx;
gb_init(&ctx, &EXECUTE_CONFIG, rom_data, rom_size);
fprintf(stderr, "gb context initialized\n");
#ifdef SKIP_STEPS
for (size_t i = 0; i < SKIP_STEPS; i++) {
// step cpu
gb_step(&ctx);
}
#endif /* SKIP_STEPS */
// run cpu
for (size_t i = 0; i < NUM_STEPS; i++) {
// print context
gb_dump_context(&ctx);
// step cpu
gb_step(&ctx);
}
// free rom data
free((void*) rom_data);
}
int main(int argc, char *argv[]) {
// render 600 frames
for (int i = 2; i < argc; i++) {
if (argv[1][0] == 'f') {
test_render_frames("out.png", argv[i]);
} else if (argv[1][0] == 'e') {
test_execute_steps(argv[i]);
} else {
fprintf(stderr, "unknown test: %s\n", argv[i]);
exit(EXIT_FAILURE);
}
}
// return success
return 0;
}
|