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
|
function NES() {
this.opts = {
preferredFrameRate: 60,
fpsInterval: 500, // Time between updating FPS in ms
showDisplay: true,
emulateSound: false,
sampleRate: 44100, // Sound sample rate in hz
CPU_FREQ_NTSC: 1789772.5,//1789772.72727272d;
CPU_FREQ_PAL: 1773447.4,
}
this.frameTime = 1000/this.opts.preferredFrameRate;
this.cpu = new NES.CPU(this);
this.ppu = new NES.PPU(this);
this.papu = new NES.PAPU(this);
this.mmap = null; // set in loadRom()
this.keyboard = new NES.Keyboard();
$("#status").text("Initialised. Ready to load a ROM.");
}
NES.prototype = {
isRunning: false,
fpsFrameCount: 0,
limitFrames: true,
romData: null,
// Resets the system.
reset: function() {
if(this.mmap != null) {
this.mmap.reset();
}
this.cpu.reset();
this.ppu.reset();
this.papu.reset();
},
start: function() {
var self = this;
if(this.rom != null && this.rom.valid) {
if (!this.isRunning) {
this.isRunning = true;
this.frameInterval = setInterval(function() {
self.frame();
}, this.frameTime/2);
this.resetFps();
this.printFps();
this.fpsInterval = setInterval(function() {
self.printFps();
}, this.opts.fpsInterval);
}
}
else {
alert("There is no ROM loaded, or it is invalid.");
}
},
frame: function() {
this.ppu.startFrame();
var cycles = 0;
var emulateSound = nes.opts.emulateSound;
var cpu = this.cpu;
var ppu = this.ppu;
var papu = this.papu;
FRAMELOOP: for (;;) {
if (cpu.cyclesToHalt == 0) {
// Execute a CPU instruction
cycles = cpu.emulate();
if (emulateSound) {
papu.clockFrameCounter(cycles);
}
cycles *= 3;
}
else {
if (cpu.cyclesToHalt > 8) {
cycles = 24;
if (emulateSound) {
papu.clockFrameCounter(8);
}
cpu.cyclesToHalt -= 8;
}
else {
cycles = cpu.cyclesToHalt * 3;
if (emulateSound) {
papu.clockFrameCounter(cpu.cyclesToHalt);
}
cpu.cyclesToHalt = 0;
}
}
for(;cycles>0;cycles--){
if(ppu.curX == ppu.spr0HitX
&& ppu.f_spVisibility==1
&& ppu.scanline-21 == ppu.spr0HitY){
// Set sprite 0 hit flag:
ppu.setStatusFlag(ppu.STATUS_SPRITE0HIT,true);
}
if(ppu.requestEndFrame){
ppu.nmiCounter--;
if(ppu.nmiCounter == 0){
ppu.requestEndFrame = false;
ppu.startVBlank();
break FRAMELOOP;
}
}
ppu.curX++;
if(ppu.curX==341){
ppu.curX = 0;
ppu.endScanline();
}
}
}
if (this.limitFrames) {
if (this.lastFrameTime) {
while ((new Date()).getTime() - this.lastFrameTime < this.frameTime) {
// twiddle thumbs
}
}
}
this.fpsFrameCount++;
this.lastFrameTime = (new Date()).getTime();
},
printFps: function() {
var now = (new Date()).getTime();
var s = 'Running';
if (this.lastFpsTime) {
s += ': '+(this.fpsFrameCount/((now-this.lastFpsTime)/1000)).toFixed(2)+' FPS';
}
$("#status").text(s);
this.fpsFrameCount = 0;
this.lastFpsTime = now;
},
stop: function() {
//$("#status").text("Stopped.");
clearInterval(this.frameInterval);
clearInterval(this.fpsInterval);
this.isRunning = false;
},
reloadRom: function() {
if(this.romData != null){
this.loadRom(this.romData);
}
},
// Loads a ROM file into the CPU and PPU.
// The ROM file is validated first.
loadRom: function(data) {
if (this.isRunning) {
this.stop();
}
$("#status").text("Loading...");
// Load ROM file:
this.rom = new NES.ROM(this);
this.rom.load(data);
if (this.rom.valid) {
this.reset();
this.mmap = this.rom.createMapper();
if (!this.mmap) {
return;
}
this.mmap.loadROM();
this.ppu.setMirroring(this.rom.getMirroringType());
this.romData = data;
$("#status").text("Successfully loaded. Ready to be started.");
}
else {
$("#status").text("Invalid ROM!");
}
return this.rom.valid;
},
resetFps: function() {
this.lastFpsTime = null;
this.fpsFrameCount = 0;
},
setFramerate: function(rate){
this.nes.opts.preferredFrameRate = rate;
this.nes.frameTime = 1000/rate;
papu.setSampleRate(this.opts.sampleRate, false);
},
setLimitFrames: function(limit) {
this.limitFrames = limit;
this.lastFrameTime = null;
}
}
|