-
Notifications
You must be signed in to change notification settings - Fork 0
/
NES.cpp
157 lines (132 loc) · 5.42 KB
/
NES.cpp
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
#include "NES.h"
NES::NES(const char* rom_file, Display &display) :
rom(),
cpu_mem(CPU_MEM_SIZE),
ppu(),
mapper(cpu_mem, ppu, controller_1),
cpu(mapper),
controller_1(),
display(display) {
try {
rom.load_ROM(rom_file);
}
catch(const char* ex) {
cerr << ex << endl;
exit(-1);
}
ppu.setup_mirroring(rom.get_mirroring());
// fill ram with PRG bank(s)
cpu_mem.fast_write(rom.get_PRG_bank_1(), 0x8000, PRG_BANK_SIZE);
// some games only have 1 16k PRG bank, so the banks should be mirrored
if(rom.get_num_PRG_banks() == 1)
cpu_mem.fast_write(rom.get_PRG_bank_1(), 0xC000, PRG_BANK_SIZE);
// otherwise write the second PRG bank
else
cpu_mem.fast_write(rom.get_PRG_bank_2(), 0xC000, PRG_BANK_SIZE);
// fill PPU ram with CHRROM
if(rom.get_num_CHR_banks() > 0)
ppu.load_CHR_bank(rom.get_CHR_bank());
run();
}
void NES::run() {
cpu.reset();
ppu.reset();
// Actually 113.66666666666667
const int cpu_cycles_per_scanline = 113; // make into a global const
// Tracks cycles used/remaining
long cpu_cycles_remaining = 0;
SDL_Event event;
// Main emulation loop
for(long frame = 0; ; frame++) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE)
return;
else if(event.type == SDL_KEYDOWN)
handle_key_input(event.key.keysym.sym, PRESSED);
else if(event.type == SDL_KEYUP)
handle_key_input(event.key.keysym.sym, RELEASED);
if(event.type == SDL_JOYBUTTONDOWN
|| event.type == SDL_JOYBUTTONUP
|| event.type == SDL_JOYAXISMOTION)
handle_joy_input(event);
}
for(int scanline = 0; scanline < 262; scanline++) {
if(ppu.VBlank_occurring()) cpu.set_interrupt(NMI);
cpu_cycles_remaining = cpu.emulate(cpu_cycles_per_scanline
+ cpu_cycles_remaining
// This accounts for the remainder cycles - but needs checking
+ ((frame * scanline) % 3 == 0 ? 2 : 0));
ppu.emulate();
}
display.show(ppu.get_framebuffer());
}
}
void NES::handle_key_input(int key, int state) {
switch(key) {
case SDLK_s: controller_1.set_button_state(BUTTON_A, state); break;
case SDLK_a: controller_1.set_button_state(BUTTON_B, state); break;
case SDLK_RETURN: controller_1.set_button_state(BUTTON_START, state); break;
case SDLK_TAB: controller_1.set_button_state(BUTTON_SELECT, state); break;
case SDLK_UP: controller_1.set_button_state(BUTTON_UP, state); break;
case SDLK_DOWN: controller_1.set_button_state(BUTTON_DOWN, state); break;
case SDLK_LEFT: controller_1.set_button_state(BUTTON_LEFT, state); break;
case SDLK_RIGHT: controller_1.set_button_state(BUTTON_RIGHT, state); break;
}
}
void NES::handle_joy_input(const SDL_Event &event) {
switch(event.type) {
case SDL_JOYAXISMOTION: {
switch(event.jaxis.axis) {
case 0: {
if(event.jaxis.value < -32000)
controller_1.set_button_state(BUTTON_LEFT, PRESSED);
else if(event.jaxis.value > 32000)
controller_1.set_button_state(BUTTON_RIGHT, PRESSED);
else {
controller_1.set_button_state(BUTTON_LEFT, RELEASED);
controller_1.set_button_state(BUTTON_RIGHT, RELEASED);
}
break;
}
case 1: {
if(event.jaxis.value < -32000)
controller_1.set_button_state(BUTTON_UP, PRESSED);
else if(event.jaxis.value > 32000)
controller_1.set_button_state(BUTTON_DOWN, PRESSED);
else {
controller_1.set_button_state(BUTTON_UP, RELEASED);
controller_1.set_button_state(BUTTON_DOWN, RELEASED);
}
break;
}
}
break;
}
case SDL_JOYBUTTONDOWN: {
switch(event.jbutton.button) {
case 0: controller_1.set_button_state(BUTTON_B, PRESSED); break;
case 1: controller_1.set_button_state(BUTTON_A, PRESSED); break;
case 2: controller_1.set_button_state(BUTTON_SELECT, PRESSED); break;
case 3: controller_1.set_button_state(BUTTON_START, PRESSED); break;
}
break;
}
case SDL_JOYBUTTONUP: {
switch(event.jbutton.button) {
case 0: controller_1.set_button_state(BUTTON_B, RELEASED); break;
case 1: controller_1.set_button_state(BUTTON_A, RELEASED); break;
case 2: controller_1.set_button_state(BUTTON_SELECT, RELEASED); break;
case 3: controller_1.set_button_state(BUTTON_START, RELEASED); break;
}
break;
}
}
}
void NES::print_ascii() {
for(int i = 0; i < 240; i++) {
for(int j = 0; j < 256; j++) {
printf("%d", ppu.get_framebuffer()[i][j]);
}
puts("");
}
}