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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
#define GL_SILENCE_DEPRECATION
#ifdef _WIN32
#define NOMINMAX
#undef NOCRYPT
// For mingw-w64, use _aligned_malloc instead of aligned_alloc
#define aligned_alloc(align, size) _aligned_malloc(size, align)
#define aligned_free(ptr) _aligned_free(ptr)
#else
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#define aligned_free(ptr) free(ptr)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#define BUFFER_WIDTH 256
#define BUFFER_HEIGHT 240
#define WINDOW_WIDTH 320 * 3
#define WINDOW_HEIGHT 240 * 3
// #define PRG_ROM_SIZE (2 * 1024 * 1024)
// #define CHR_ROM_SIZE (1 * 1024 * 1024)
#define PRG_ROM_SIZE (512 * 1024)
#define CHR_ROM_SIZE (256 * 1024)
#define PIXELS_SIZE (256 * 240)
#define RAM_SIZE 0x1000 // 0x800 in reality, but for aligned alloc it must be the size of the alignment (4096)
#define SRAM_SIZE 0x2000
#define CIRAM_SIZE 0x1000
#define CHR_RAM_SIZE 0x4000
uint32_t buffer[BUFFER_WIDTH * BUFFER_HEIGHT] __attribute__((section(".bss"), aligned(4096)));
uint32_t display_buffer[BUFFER_WIDTH * BUFFER_HEIGHT] __attribute__((section(".bss"), aligned(4096)));
static void audio_callback(int16_t *data, size_t frames) { }
#define FRAME_INTERVAL_NS (1000000000ULL / 60.0988)
#define DEBUG_PRINT(fmt, ...) printf(fmt, ##__VA_ARGS__)
#ifdef _WIN32
#include "win32_timer.c"
#else
#include "linux_timer.c"
#endif
// #include "audio.c"
#include "incbin.h"
#include "platform_gl_loader.c"
struct main_state {
int32_t filter_override;
float filter_frequency;
uint32_t screen_width;
uint32_t screen_height;
float contrast;
float saturation;
float brightness;
float tone_data[4];
// OpenGL Objects
GLuint shader_program;
GLuint persistence_program;
// GLuint upscale_program;
GLuint upscale_warp_program;
GLuint bloom_extract_program;
GLuint bloom_blur_program;
GLuint bloom_warp_program;
GLuint bloom_composite_program;
GLuint texture;
GLuint persistence_texture;
GLuint persistence_output_texture;
GLuint crt_output_texture;
GLuint bloom_texture;
GLuint bloom_temp_texture;
GLuint bloom_warped_texture;
GLuint upscaled_source_texture;
GLuint persistence_fbo;
GLuint upscaled_source_fbo;
GLuint crt_fbo;
GLuint bloom_fbo;
GLuint bloom_temp_fbo;
GLuint bloom_warp_fbo;
GLuint vao;
GLuint vbo;
GLuint ebo;
// CRT Shader Uniforms
GLuint uniform_resolution;
GLuint uniform_src_image_size;
GLuint uniform_brightness;
GLuint uniform_tone;
GLuint uniform_crt_emulation;
GLuint uniform_apply_mask;
GLuint uniform_sampler_location;
// Bloom Shader Uniforms
GLuint bloom_uniform_threshold;
GLuint bloom_uniform_sampler;
GLuint blur_uniform_horizontal;
GLuint blur_uniform_sampler;
GLuint composite_uniform_bloom_strength;
GLuint composite_uniform_crt_sampler;
GLuint composite_uniform_bloom_sampler;
// Bloom settings
float bloom_threshold;
float bloom_strength;
uint32_t bloom_width;
uint32_t bloom_height;
// Phosphor persistence
float persistence_decay;
struct {
int32_t x;
int32_t y;
int32_t w;
int32_t h;
} viewport;
uint32_t render_width; // The actual remake resolution (e.g., 360)
uint32_t render_height; // The actual remake resolution (e.g., 270)
uint32_t frame_number;
uint8_t running;
uint8_t toggle_crt_emulation;
uint8_t toggle_bloom;
uint8_t fullscreen;
uint8_t viewport_changed;
};
struct main_state state __attribute__((aligned(64)));
#include "mkfw.h"
#include "platform_opengl.c"
static uint32_t frames; // debug information
// static int32_t tas_frame_count;
// #include "smb_tas.h" // REMOVE ME
// NES core
#include "mknes_mapper.h"
#include "mknes.h"
#include "mknes_apu.c"
#include "mknes_ppu.c"
#include "mknes_ppu_registers.c"
#include "mknes_memory.c"
#include "mknes_cpu.c"
#include "mknes_ines2.c"
#include "mknes_mapper.c"
// struct nes_state nstate;
static void framebuffer_callback(struct mkfw_state *mkfw_window, int32_t width, int32_t height, float aspect_ratio) {
// state.screen_width = width;
// state.screen_height = height;
// state.viewport.x = 0;
// state.viewport.y = 0;
// state.viewport.w = width;
// state.viewport.h = height;
// float current_aspect = (float)width / (float)height;
// if(current_aspect > aspect_ratio) { // Window is wider than the desired aspect ratio
// float new_width = height * aspect_ratio; // Compute new width based on the height and the desired aspect ratio
// state.viewport.x = (width - new_width) / 2;
// state.viewport.w = new_width;
// } else if(current_aspect < aspect_ratio) { // Window is taller than the desired aspect ratio
// float new_height = width / aspect_ratio; // Compute new height based on the width and the desired aspect ratio
// state.viewport.y = (height - new_height) / 2;
// state.viewport.h = new_height;
// }
int32_t viewport_x = 0;
int32_t viewport_y = 0;
int32_t viewport_width = width;
int32_t viewport_height = height;
double target_aspect = (aspect_ratio!=0.f) ? (double)aspect_ratio : ((double)width / (double)height);
double current_aspect = (double)width / (double)height;
if(current_aspect>target_aspect) {
int32_t new_width = (int32_t)((double)height * target_aspect + 0.5);
viewport_x = (width - new_width) / 2;
viewport_width = new_width;
} else if(current_aspect<target_aspect) {
int32_t new_height = (int32_t)((double)width / target_aspect + 0.5);
viewport_y = (height - new_height) / 2;
viewport_height = new_height;
} else {
}
viewport_x &= ~1;
viewport_y &= ~1;
state.viewport.x = viewport_x;
state.viewport.y = viewport_y;
state.viewport.w = viewport_width;
state.viewport.h = viewport_height;
state.viewport_changed = 1;
}
int main(int argc, char **argv) {
state.toggle_crt_emulation = 1;
state.toggle_bloom = 1;
setbuf(stdout, 0);
init_opcode_lut();
init_opcode_ud_lut();
// protect_opcode_lut();
struct nes_state *nstate = aligned_alloc(4096, (sizeof(struct nes_state) + 4095) & ~4095);
memset(nstate, 0, sizeof(struct nes_state));
ppu_reset(nstate);
// ines2_load(nstate, "data/0000/10-Yard Fight (USA, Europe).nes");
// ines2_load(nstate, "data/0000/Balloon Fight (USA).nes");
// ines2_load(nstate, "data/0000/Excitebike (Japan, USA).nes");
// ines2_load(nstate, "data/0000/Ice Climber (USA, Europe, Korea).nes");
// ines2_load(nstate, "data/0000/Kung Fu (Japan, USA).nes");
ines2_load(nstate, "data/0000/Super Mario Bros. (World) (HVC-SM).nes");
// ines2_load(nstate, "data/Super Mario Bros. (W) (V1.0) [!].nes");
// ines2_load(nstate, "data/Super Mario Bros. (JU) [!].nes");
// ines2_load(nstate, "data/0000/Urban Champion (World).nes");
// ines2_load(nstate, "data/0000/Wrecking Crew (World).nes");
// ines2_load(nstate, "data/0000/scanline.nes");
// ines2_load(nstate, "data/0000/Sayoonara!.NES");
// ines2_load(nstate, "data/0000/raster_demos/RasterChromaLuma.NES");
// ines2_load(nstate, "data/0000/raster_demos/RasterTest1.NES");
// ines2_load(nstate, "data/0000/raster_demos/RasterTest2.NES");
// ines2_load(nstate, "data/0000/raster_demos/RasterTest3.NES");
// ines2_load(nstate, "data/0000/raster_demos/RasterTest3a.NES");
// ines2_load(nstate, "data/0000/raster_demos/RasterTest3b.NES");
ines2_load(nstate, "data/0000/raster_demos/RasterTest3c.NES");
// ines2_load(nstate, "data/0000/raster_demos/RasterTest3d.NES");
// ines2_load(nstate, "data/0000/raster_demos/RasterTest3e.NES");
// ines2_load(nstate, "data/0000/NEStress.NES");
// ines2_load(nstate, "data/0000/Super Mario Bros. (World) (HVC-SM).zip");
// ines2_load(nstate, "data/0042/Super Mario Bros. + Duck Hunt (USA).zip");
// ines2_load(nstate, "data/0000/Xevious - The Avenger (USA).zip");
// ines2_load(nstate, "data/tv.nes");
// ines2_load(nstate, "data/Life Force (USA).zip"); // 2002
// ines2_load(nstate, "data/0003/Flipull - An Exciting Cube Game (Japan) (En).zip");
// ines2_load(nstate, "data/0003/Friday the 13th (USA).zip");
// ines2_load(nstate, "data/0003/Ghostbusters (Japan).zip");
// ines2_load(nstate, "data/0003/Gradius (USA).zip");
// ines2_load(nstate, "data/0007/Battletoads (USA).zip");
// ines2_load(nstate, "data/0007/Beetlejuice (USA).zip");
// ines2_load(nstate, "data/0007/Cabal (USA).zip");
// ines2_load(nstate, "data/000b/Baby Boomer (USA) (Unl).zip");
// ines2_load(nstate, "data/000b/Captain Comic - The Adventure (USA) (Unl).zip");
// ines2_load(nstate, "data/000b/King Neptune's Adventure (USA) (Unl).zip");
// ines2_load(nstate, "data/2002/Attack Animal Gakuen (Japan).zip");
// ines2_load(nstate, "data/2002/Ballblazer (Japan).zip");
// ines2_load(nstate, "data/2002/Best of the Best - Championship Karate (USA).zip");
// ines2_load(nstate, "data/0001/Kid Icarus (UE) (V1.1) [!].nes");
// ines2_load(nstate, "data/0001/Metroid (U) [!].nes");
// ines2_load(nstate, "data/0001/Legend of Zelda, The (U) (V1.1) [!].nes");
// ines2_load(nstate, "data/Blaster Master (USA).zip"); // mapper 1
// ines2_load(nstate, "AccuracyCoin.nes"); // mapper 1
mapper_setup(nstate);
cpu_reset(nstate);
#if 0
for(uint32_t i = 0; i < 0x5000; ++i) {
while(!nstate->ppu.frame_ready) {
// PROFILE_NAMED("nes emulator");
cpu_tick(nstate);
}
nstate->ppu.frame_ready = 0;
frames++;
}
// for(size_t i = 0; i < 256; ++i) {
// printf("instr %2.2x: %lld\n", i, instr_count[i]);
// }
return 0;
#else
// WINDOW SETUP
struct mkfw_state *window = mkfw_init(WINDOW_WIDTH, WINDOW_HEIGHT);
mkfw_set_window_title(window, "mknes");
mkfw_set_framebuffer_size_callback(window, framebuffer_callback);
mkfw_set_swapinterval(window, 0);
gl_loader();
opengl_setup();
// setup_render_targets();
change_resolution(BUFFER_WIDTH, BUFFER_HEIGHT);
mkfw_show_window(window);
mkfw_set_window_min_size_and_aspect(window, WINDOW_WIDTH, WINDOW_HEIGHT, 4.f, 3.f);
timer_init();
struct timer_handle *timer = timer_new(FRAME_INTERVAL_NS);
uint8_t running = true;
while(running && !mkfw_should_close(window)) {
mkfw_pump_messages(window);
// // Check for ESC key
if(mkfw_is_key_pressed(window, MKS_KEY_ESCAPE)) running = false;
// Joypad input
uint8_t input = 0;
if(window->keyboard_state[MKS_KEY_X]) { input |= (1 << 0); }
if(window->keyboard_state[MKS_KEY_Z]) { input |= (1 << 1); }
if(window->keyboard_state[MKS_KEY_SPACE]) { input |= (1 << 2); }
if(window->keyboard_state[MKS_KEY_RETURN]) { input |= (1 << 3); }
if(window->keyboard_state[MKS_KEY_UP]) { input |= (1 << 4); }
if(window->keyboard_state[MKS_KEY_DOWN]) { input |= (1 << 5); }
if(window->keyboard_state[MKS_KEY_LEFT]) { input |= (1 << 6); }
if(window->keyboard_state[MKS_KEY_RIGHT]) { input |= (1 << 7); }
nstate->ppu.input[0] = input;
// Run NES emulation for one frame
while(!nstate->ppu.frame_ready) {
cpu_tick(nstate);
}
nstate->ppu.frame_ready = 0;
if(nstate->ppu.open_bus > 0) {
int32_t v = nstate->ppu.open_bus;
v -= 5;
if(v < 0) {
v = 0;
}
nstate->ppu.open_bus = v;
}
frames++;
// Convert NES pixels to display buffer
uint32_t * restrict dst = display_buffer;
uint8_t * restrict src = nstate->pixels;
for(uint32_t y = 0; y < 240; ++y) {
for(uint32_t x = 0; x < 256; ++x) {
uint8_t val = *src++;
if(val >= 64) val = 0;
dst[x] = nes_palette[val];
}
dst += BUFFER_WIDTH;
}
mkfw_update_keyboard_state(window);
mkfw_update_modifier_state(window);
mkfw_update_mouse_state(window);
// Render and swap buffers
render_frame();
mkfw_swap_buffers(window);
timer_wait(timer);
}
printf("total frames: %6d total cycles: %12llu\n", frames, (unsigned long long)nstate->cpu.cycles);
timer_destroy(timer);
// free_nes_state(&nstate);
timer_shutdown();
mkfw_cleanup(window);
#endif
return 0;
}
|