-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
192 lines (158 loc) · 3.42 KB
/
index.js
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
var CPU = require("./system/cpu");
var APU = require("./system/apu");
var PPU = require("./system/ppu");
var Cartridge = require("./system/cartridge");
var Controllers = require("./system/controllers");
var Output = require("./system/output");
var Memory = require("./system/memory");
var utils = require("./utils");
var Input = require("./system/input");
var config = require("./config.json");
function System( el ) {
this.config = config;
// system timing flags
this.frameEnded = false;
this.tickAPU = false;
// IO
this.controllers = new Controllers();
this.input = new Input( this );
this.output = new Output();
// video output
if ( el ) {
this.output.video.setElement( el );
}
// reserve for timing
this.interval = null;
this.running = false;
this.paused = true;
// reserve for system core
this.cartridge = null;
this.cpu = null;
this.apu = null;
this.ppu = null;
this.memory = null;
Object.preventExtensions( this );
}
System.prototype = {
/**
* Load a ROM and optionally run it.
* @param {string} filename - Path of ROM to run.
* @param autorun - If true, run ROM when loaded. If a function, call that function.
*/
load: function( filename, autorun ) {
var self = this;
utils.readFile( filename, function( data ) {
self.initCartridge( data );
if ( typeof autorun === "function" ) {
autorun();
} else if ( autorun === true ) {
self.run();
}
});
},
/**
* Turn on and run emulator.
*/
run: function() {
if ( this.interval ) {
// once is enough
return;
}
var self = this;
this.interval = setInterval( function() {
if ( !self.paused) {
self.runFrame();
}
}, 1000 / 60 );
this.output.video.run();
this.running = true;
this.paused = false;
},
/**
* Run a single frame (1/60s NTCS, 1/50s PAL).
*/
runFrame: function() {
var cpu = this.cpu,
ppu = this.ppu,
apu = this.apu;
while ( !ppu.frameEnded ) {
cpu.tick();
ppu.tick();
ppu.tick();
ppu.tick();
if ( this.tickAPU ) {
apu.tick();
}
this.tickAPU = !this.tickAPU;
}
ppu.frameEnded = false;
},
/**
* Synchronously simulate running for a number of milliseconds.
* @param {number} milliseconds - The number of milliseconds to simulate.
*/
simulate: function( milliseconds ) {
var i,
frames = ( milliseconds / 1000 ) * 60;
for ( i = 0; i < frames; i++ ) {
this.runFrame();
}
},
/**
* Resume running.
*/
play: function() {
this.paused = false;
if ( !this.running ) {
this.run();
}
},
/**
* Stop running.
*/
pause: function() {
this.paused = true;
},
/**
* On/off switch.
*/
toggle: function() {
if ( this.paused ) {
this.play();
} else {
this.pause();
}
return this.paused;
},
/**
* Initialize cartridge and hook into system.
*/
initCartridge: function( data ) {
this.cartridge = new Cartridge( data, this );
this.initCore();
this.reset();
},
loadCartridge: function( cartridge ) {
//this.cartridge = cartridge;
this.memory.loadCartridge( cartridge );
this.ppu.memory.loadCartridge( cartridge );
},
/**
* Initialize the core of our emulator (processor etc).
*/
initCore: function() {
this.cpu = new CPU( this );
this.apu = new APU( this );
this.ppu = new PPU( this );
this.memory = new Memory( this );
this.loadCartridge( this.cartridge );
},
/**
* Reset the console.
*/
reset: function() {
this.cpu.reset();
this.apu.reset();
}
};
module.exports = System;