-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnes.c
More file actions
315 lines (241 loc) · 6.73 KB
/
nes.c
File metadata and controls
315 lines (241 loc) · 6.73 KB
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
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include "cartridge.h"
#include "ppu.h"
#include "cpu.h"
#include "bus.h"
#define NINTENDO_RAM_SZ 0x800
#define NINTENDO_PRG_RAM_SZ 0x2000
#define NINTENDO_PRG_ROM_SZ 0x4000
#define NINTENDO_CHR_ROM_SZ 0x2000
struct nes_emu {
struct cpu_6502 cpu;
struct nes_ppu ppu;
struct nes_cart cart;
struct nes_bus bus;
uint8_t ram[NINTENDO_RAM_SZ];
};
int nes_load_ines_header(FILE *fp, struct nes_cart *cart);
int nes_prg_ram_alloc(struct nes_cart *cart);
void nes_trainer_set(FILE *fp, struct nes_cart *cart);
int nes_prg_rom_load(FILE *fp, struct nes_cart *cart);
int nes_chr_rom_load(FILE *fp, struct nes_cart *cart);
int nes_load_catridge(struct nes_emu *nes,
struct nes_cart *cart,
const char *name);
void nes_init(struct nes_emu *nes);
void nes_ppu_init(struct nes_emu *nes);
void nes_init_bus(struct nes_emu *nes);
int nes_load_catridge(struct nes_emu *nes,
struct nes_cart *cart,
const char *name)
{
FILE *fp;
size_t ret;
if (!name ||name[0] == '\0')
return -1;
fp = fopen(name, "rb");
if (!fp)
return -1;
ret = nes_load_ines_header(fp, cart);
if (ret)
goto cleanup;
nes_trainer_set(fp, cart);
ret = nes_prg_rom_load(fp, cart);
if (ret)
goto cleanup;
ret = nes_chr_rom_load(fp, cart);
if (ret)
goto cleanup;
ret = nes_prg_ram_alloc(cart);
cart->mirroring = cart->header.flags6 & 0x01;
cart->battery = cart->header.flags6 & 0x02;
nes->cart = *cart;
cleanup:
fclose(fp);
return ret;
}
int nes_load_ines_header(FILE *fp, struct nes_cart *cart)
{
size_t ret;
ret = fread(&cart->header, 1, sizeof(struct ines_header), fp);
if (ret != sizeof(struct ines_header))
return -1;
return 0;
}
void nes_trainer_set(FILE *fp, struct nes_cart *cart)
{
cart->trainer_present = cart->header.flags6 & 0x04;
if (cart->trainer_present)
fseek(fp, 512, SEEK_CUR);
}
int nes_prg_ram_alloc(struct nes_cart *cart)
{
cart->prg_ram = NULL;
if (cart->header.flags6 & 0x02) {
// Assuming NES format, and no NES v2 support.
cart->prg_ram = malloc(NINTENDO_PRG_RAM_SZ);
if (!cart->prg_ram)
return -1;
}
return 0;
}
int nes_prg_rom_load(FILE *fp, struct nes_cart *cart)
{
size_t prg_bytes, ret;
cart->prg_rom = NULL;
if (cart->header.prg_rom_size == 0)
return 0;
prg_bytes = cart->header.prg_rom_size * NINTENDO_PRG_ROM_SZ;
cart->prg_rom = malloc(prg_bytes);
if (!cart->prg_rom)
return -1;
ret = fread(cart->prg_rom, 1, prg_bytes, fp);
if (ret != prg_bytes) {
free(cart->prg_rom);
cart->prg_rom = NULL;
return -1;
}
return 0;
}
int nes_chr_rom_load(FILE *fp, struct nes_cart *cart)
{
size_t chr_bytes, ret;
cart->chr_rom = NULL;
if (cart->header.chr_rom_size == 0)
return 0;
chr_bytes = cart->header.chr_rom_size * NINTENDO_CHR_ROM_SZ;
cart->chr_rom = malloc(chr_bytes);
if (!cart->chr_rom)
return -1;
ret = fread(cart->chr_rom, 1, chr_bytes, fp);
if (ret != chr_bytes) {
free(cart->chr_rom);
cart->chr_rom = NULL;
return -1;
}
return 0;
}
int nes_eject_catridge(struct nes_emu *nes, struct nes_cart *cart)
{
if (cart->prg_rom != NULL)
free(cart->prg_rom);
if (cart->chr_rom != NULL)
free(cart->chr_rom);
if (cart->prg_ram != NULL)
free(cart->prg_ram);
return 0;
}
void nes_init_bus(struct nes_emu *nes)
{
nes->bus.cpu = &nes->cpu;
nes->bus.ppu = &nes->ppu;
nes->bus.cart = &nes->cart;
nes->bus.ram = nes->ram;
}
void nes_init(struct nes_emu *nes)
{
memset(nes, 0, sizeof(struct nes_emu));
nes_ppu_init(nes);
nes_init_bus(nes);
}
void nes_ppu_init(struct nes_emu *nes)
{
nes->ppu.cart = &nes->cart;
nes->ppu.cycle = 0;
nes->ppu.scanline = 0;
nes->ppu.palette_table = nes_canonical_palette;
memset(nes->ppu.frame_buffer, 0, sizeof(nes->ppu.frame_buffer));
}
static void simulate_cpu_writes(struct nes_emu *nes)
{
uint16_t tiles, base;
uint8_t tile;
// Fill the first nametable (0x2000) with sequential tile
// indices by simulates the CPU writing to PPUADDR (0x2006)
// and PPUDATA (0x2007).
base = 0x2000;
tiles = 960;
tile = 1;
for (int i = 0; i < tiles; ++i) {
uint16_t addr = base + i;
uint8_t high = (addr >> 8) & 0xff;
uint8_t low = addr & 0xff;
nes_bus_write(&nes->bus, 0x2006, high);
nes_bus_write(&nes->bus, 0x2006, low);
nes_bus_write(&nes->bus, 0x2007, tile);
tile++;
if (tile == 0) tile = 1;
}
uint8_t palvals[4] = { 0x0f, 0x16, 0x27, 0x30 };
for (int i = 0; i < 4; ++i) {
uint16_t addr = 0x3f00 + i;
nes_bus_write(&nes->bus, 0x2006, (addr >> 8) & 0xff);
nes_bus_write(&nes->bus, 0x2006, addr & 0xff);
nes_bus_write(&nes->bus, 0x2007, palvals[i]);
}
}
int main(int argc, char *argv[])
{
struct nes_emu nes;
struct nes_cart cart;
uint8_t running, frame_count, ret;
SDL_Event event;
nes_init(&nes);
ret = nes_load_catridge(&nes, &cart, "roms/tetris.nes");
if (ret < 0)
goto cleanup;
#define SCALE 3
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "SDL init failed: %s\n", SDL_GetError());
return 1;
}
SDL_Window *window = SDL_CreateWindow(
"NES Emulator",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
256 * SCALE,
240 * SCALE,
SDL_WINDOW_SHOWN
);
SDL_Renderer *renderer = SDL_CreateRenderer(
window,
-1,
SDL_RENDERER_ACCELERATED
);
SDL_Texture *texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
256,
240
);
running = 1;
nes.ppu.mask = 0x1e;
simulate_cpu_writes(&nes);
while(running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
running = 0;
}
for (int i = 0; i < 341 * 262; ++i) {
nes_ppu_tick(&nes.ppu);
}
SDL_UpdateTexture(texture, NULL, nes.ppu.frame_buffer, 256 * sizeof(uint32_t));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Delay(16);
}
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
cleanup:
nes_eject_catridge(NULL, &cart);
return 0;
}