-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEmulatorCore.as
455 lines (398 loc) · 16.1 KB
/
EmulatorCore.as
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package {
import flash.display.Stage;
import flash.utils.Timer;
import flash.events.TimerEvent;
import FileLoader.LocalLoader;
import utils.ArrayHelper;
import flash.utils.ByteArray;
import flash.geom.Rectangle;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class EmulatorCore extends MovieClip{
public var SKIPBoot:Boolean;
public var dynarecEnabled:Boolean;
public var dynarecTHUMB:Boolean;
public var dynarecARM:Boolean;
public var emulatorSpeed:int;
public var timerIntervalRate:int;
public var graphicsFound:Boolean;
public var audioFound:Boolean;
public var romFound:Boolean;
public var faultFound:Boolean;
public var paused:Boolean;
public var audioVolume:int;
public var audioBufferUnderrunLimit:int;
public var audioBufferSize:int;
public var offscreenWidth:int;
public var offscreenHeight:int;
public var BIOS:Array;
public var offscreenRGBCount:int;
public var offscreenRGBACount:int;
public var frameBuffer:Array;
public var swizzledFrame:Array;
public var drewFrame:Boolean;
public var audioUpdateState:Boolean;
//Cycles - these were not included in clear function
public var clockCyclesSinceStart:int;
public var CPUCyclesPerIteration:int;
public var CPUCyclesTotal:int;
public var clocksPerSecond:int;
//TIMER
public var metricStart:Date;
public var mainTimer:Timer;
//Peripherals
public var IOCore;
public var ROM;
public var fr:LocalLoader;
//Canvas data
public var canvas:GameBoyAdvanceCanvas;
public var canvasLastWidth;
public var canvasLastHeight;
public var onscreenWidth;
public var onscreenHeight;
public function EmulatorCore() {
// constructor code
this.SKIPBoot = false; //Skip the BIOS boot screen.
this.dynarecEnabled = false; //Use the dynarec engine?
this.dynarecTHUMB = true; //Enable THUMB compiling.
this.dynarecARM = false; //Enable ARM compiling.
this.emulatorSpeed = 1; //Speed multiplier of the emulator.
this.timerIntervalRate = 16; //How often the emulator core is called into (in milliseconds).
this.graphicsFound = false; //Do we have graphics output sink found yet?
this.audioFound = false; //Do we have audio output sink found yet?
this.romFound = false; //Do we have a ROM loaded in?
this.faultFound = false; //Did we run into a fatal error?
this.paused = true; //Are we paused?
this.audioVolume = 1; //Starting audio volume.
this.audioBufferUnderrunLimit = 8; //Audio buffer minimum span amount over x interpreter iterations.
this.audioBufferSize = 20; //Audio buffer maximum span amount over x interpreter iterations.
this.offscreenWidth = 240; //Width of the GBA screen.
this.offscreenHeight = 160; //Height of the GBA screen.
this.BIOS = []; //Initialize BIOS as not existing.
//Cache some frame buffer lengths:
this.offscreenRGBCount = this.offscreenWidth * this.offscreenHeight * 3;
this.offscreenRGBACount = this.offscreenWidth * this.offscreenHeight * 4;
//Graphics buffers to generate in advance:
this.frameBuffer = ArrayHelper.buildArray(this.offscreenRGBCount);
this.swizzledFrame = ArrayHelper.buildArray(this.offscreenRGBCount); //The swizzled output buffer that syncs to the internal framebuffer on v-blank.
this.initializeGraphicsBuffer(); //Pre-set the swizzled buffer for first frame.
this.drewFrame = false; //Did we draw the last iteration?
this.audioUpdateState = false; //Do we need to update the sound core with new info?
//Calculate some multipliers against the core emulator timer:
clockCyclesSinceStart = 0;
CPUCyclesTotal = 0;
this.calculateTimings();
}
//First to be called after constructor
public function init(){
this.canvas = new GameBoyAdvanceCanvas(offscreenWidth, offscreenHeight);
addChild(this.canvas);
fr = new LocalLoader();
fr.loadROM(romLoadComplete);
}
public function romLoadComplete(dat:ByteArray){
attachCanvas();
attachROM(dat);
playEmulator();
}
public function playEmulator():void {
if (this.paused) {
this.startTimer();
this.paused = false;
}
}
public function pauseEmulator():void {
if (!this.paused) {
this.clearTimer();
this.save();
this.paused = true;
}
}
public function stopEmulator():void {
this.faultFound = false;
this.romFound = false;
this.pauseEmulator();
}
public function statusClear():void {
this.faultFound = false;
this.pauseEmulator();
}
public function restart():void {
this.faultFound = false;
this.pauseEmulator();
this.initializeCore();
this.resetMetrics();
this.reinitializeAudio();
}
public function clearTimer():void {
//clearInterval(this.timer);
if(mainTimer == null)
return;
mainTimer.removeEventListener(TimerEvent.TIMER, this.timerCallback);
this.resetMetrics();
}
public function startTimer():void {
//timerIntervalRate
mainTimer = new Timer(1);
this.clearTimer();
var parentObj = this;
//trace("interval: " + (this.timerIntervalRate));
mainTimer.start();
//this.addEventListener(Event.ENTER_FRAME, this.timerCallback);
mainTimer.addEventListener(TimerEvent.TIMER, this.timerCallback);
initKeyEvents(this);
}
public function initKeyEvents(po){
var parentObj = po;
stage.addEventListener(KeyboardEvent.KEY_DOWN, function(keyEvent:KeyboardEvent){parentObj.curKeyDown(keyEvent.keyCode)});
stage.addEventListener(KeyboardEvent.KEY_UP, function(keyEvent:KeyboardEvent){parentObj.curKeyUp(keyEvent.keyCode)});
}
//event:TimerEvent
public function timerCallback(e:TimerEvent):void {
//trace("Percent: " + this.getSpeedPercentage() );
//Check to see if web view is not hidden, if hidden don't run due to JS timers being inaccurate on page hide:
//trace("emulator speed: " + this.emulatorSpeed + " clock/sec: " + this.clocksPerSecond);
if (!this.faultFound && this.romFound) { //Any error pending or no ROM loaded is a show-stopper!
this.iterationStartSequence(); //Run start of iteration stuff.
this.IOCore.iterate(); //Step through the emulation core loop.
this.iterationEndSequence(); //Run end of iteration stuff. Including rendering the frame
}
else {
this.pauseEmulator(); //Some pending error is preventing execution, so pause.
}
}
public function iterationStartSequence():void {
this.faultFound = true; //If the end routine doesn't unset this, then we are marked as having crashed.
this.drewFrame = false; //Graphics has not drawn yet for this iteration block.
this.audioUnderrunAdjustment(); //If audio is enabled, look to see how much we should overclock by to maintain the audio buffer.
this.audioPushNewState(); //Check to see if we need to update the audio core for any output changes.
}
public function iterationEndSequence():void {
this.requestDraw(); //If drewFrame is true, blit buffered frame out.
this.faultFound = false; //If core did not throw while running, unset the fatal error flag.
this.clockCyclesSinceStart += this.CPUCyclesTotal; //Accumulate tracking.
}
public function attachROM(ROM):void {
this.stop();
this.ROM = ROM;
this.initializeCore();
this.romFound = true;
}
public function attachBIOS(BIOS):void {
this.statusClear();
this.BIOS = BIOS;
}
public function save():void {
//Nothing yet...
}
public function setSpeed(speed):void {
this.emulatorSpeed = Math.min(Math.max(parseFloat(speed), 0.01), 10);
this.calculateTimings();
this.reinitializeAudio();
}
public function changeCoreTimer(newTimerIntervalRate):void {
this.timerIntervalRate = Math.max(parseInt(newTimerIntervalRate), 1);
if (!this.paused) { //Set up the timer again if running.
this.clearTimer();
this.startTimer();
}
this.calculateTimings();
this.reinitializeAudio();
}
public function resetMetrics():void {
this.clockCyclesSinceStart = 0;
this.metricStart = new Date();
}
public function calculateTimings():void {
this.clocksPerSecond = this.emulatorSpeed * 0x1000000 / 2;
this.CPUCyclesTotal = this.CPUCyclesPerIteration = Math.min(this.clocksPerSecond / 1000 * this.timerIntervalRate, 0x7FFFFFFF) | 0;
//trace("Total: " + this.CPUCyclesTotal);
}
public function getSpeedPercentage():String {
var metricEnd:Date = new Date();
return (((this.timerIntervalRate * this.clockCyclesSinceStart / (metricEnd.time - this.metricStart.time)) / this.CPUCyclesPerIteration) * 100) + "%";
}
public function initializeCore():void {
//Setup a new instance of the i/o core:
this.IOCore = new GameBoyAdvanceIO(this);
}
public function curKeyDown(keyPressed):void {
if (!this.paused) {
this.IOCore.joypad.keyPress(keyPressed);
}
}
public function curKeyUp(keyReleased):void {
if (!this.paused) {
this.IOCore.joypad.keyRelease(keyReleased);
}
}
public function attachCanvas():void {
this.graphicsFound = true;
this.initializeCanvasTarget();
}
public function recomputeDimension():void {
//Cache some dimension info:
/*this.canvasLastWidth = this.canvas.canvasWidth;
this.canvasLastHeight = this.canvas.canvasHeight;
//Set target canvas as scaled:
this.onscreenWidth = this.canvas.canvasWidth;
this.onscreenHeight = this.canvas.canvasHeight;*/
}
public function initializeCanvasTarget() {
//Obtain dimensional information:
this.recomputeDimension();
//Initialising buffer
//this.canvasBuffer = new ByteArray();
//Get handles on the canvases:
//this.canvasOffscreen.width = this.offscreenWidth;
//this.canvasOffscreen.height = this.offscreenHeight;
/*for (var indexGFXIterate = 3; indexGFXIterate < this.offscreenRGBACount; indexGFXIterate += 4) {
canvasBuffer[indexGFXIterate] = 0xFF;
//this.canvasBuffer.data[indexGFXIterate] = 0xFF;
}*/
/*for(var indexGFXIterate = 0; indexGFXIterate < this.offscreenRGBACount;){
this.canvas.setSeg(indexGFXIterate,255); //a
this.canvas.setSeg(indexGFXIterate++, 0xFF); //r
this.canvas.setSeg(indexGFXIterate++, 0xFF); //g
this.canvas.setSeg(indexGFXIterate++, 0xFF); //b
}*/
//Initialize Alpha Channel:
/*for (var indexGFXIterate = 3; indexGFXIterate < this.offscreenRGBACount; indexGFXIterate += 4) {
this.canvasBuffer.data[indexGFXIterate] = 0xFF;
}*/
//Draw swizzled buffer out as a test:
this.drewFrame = true;
this.requestDraw();
}
public function initializeGraphicsBuffer():void {
//Initialize the first frame to a white screen:
var bufferIndex = 0;
while (bufferIndex < this.offscreenRGBCount) {
this.swizzledFrame[bufferIndex++] = 0xF8;
}
}
public function swizzleFrameBuffer():void {
//Convert our dirty 15-bit (15-bit, with internal render flags above it) framebuffer to an 8-bit buffer with separate indices for the RGB channels:
//trace("swizzling buffer");
var bufferIndex = 0;
for (var canvasIndex = 0; canvasIndex < this.offscreenRGBCount;) {
this.swizzledFrame[canvasIndex++] = (this.frameBuffer[bufferIndex] & 0x1F) << 3; //Red
this.swizzledFrame[canvasIndex++] = (this.frameBuffer[bufferIndex] & 0x3E0) >> 2; //Green
this.swizzledFrame[canvasIndex++] = (this.frameBuffer[bufferIndex++] & 0x7C00) >> 7; //Blue
}
}
public function prepareFrame():void {
//Copy the internal frame buffer to the output buffer:
this.swizzleFrameBuffer();
this.drewFrame = true;
}
public var zero = 0;
public var undef = 0;
public function requestDraw():void {
//this.drewFrame
if(this.drewFrame){
var bufferIndex = 0;
for (var canvasIndex = 0; canvasIndex < this.offscreenRGBACount;) {
var r = this.swizzledFrame[bufferIndex++];
var g = this.swizzledFrame[bufferIndex++];
var b = this.swizzledFrame[bufferIndex++];
canvas.setSeg(canvasIndex++, 255);
canvas.setSeg(canvasIndex++, r);
canvas.setSeg(canvasIndex++, g);
canvas.setSeg(canvasIndex++, b);
}
this.graphicsBlit();
}
}
public function graphicsBlit():void {
this.canvas.refresh();
}
public function enableAudio():void {
/*if (!this.audioFound) {
//Calculate the variables for the preliminary downsampler first:
this.audioResamplerFirstPassFactor = Math.max(Math.min(Math.floor(this.clocksPerSecond / 44100), Math.floor(0x7FFFFFFF / 0x3FF)), 1);
this.audioDownSampleInputDivider = (2 / 0x3FF) / this.audioResamplerFirstPassFactor;
this.audioSetState(true); //Set audio to 'found' by default.
//Attempt to enable audio:
var parentObj = this;
this.audio = new XAudioServer(2, this.clocksPerSecond / this.audioResamplerFirstPassFactor, 0, Math.max(this.CPUCyclesPerIteration * this.audioBufferSize / this.audioResamplerFirstPassFactor, 8192) << 1, null, this.audioVolume, function () {
//Disable audio in the callback here:
parentObj.disableAudio();
});
if (this.audioFound) {
//Only run this if audio was found to save memory on disabled output:
this.initializeAudioBuffering();
}
}*/
}
public function disableAudio():void {
/*if (this.audioFound) {
this.audio.changeVolume(0);
this.audioSetState(false);
}*/
}
public function initializeAudioBuffering():void {
/*this.audioDestinationPosition = 0;
this.audioBufferContainAmount = Math.max(this.CPUCyclesPerIteration * this.audioBufferUnderrunLimit / this.audioResamplerFirstPassFactor, 4096) << 1;
this.audioNumSamplesTotal = (this.CPUCyclesPerIteration / this.audioResamplerFirstPassFactor) << 1;
this.audioBuffer = getFloat32Array(this.audioNumSamplesTotal);*/
}
public function changeVolume(newVolume):void {
/*this.audioVolume = Math.min(Math.max(parseFloat(newVolume), 0), 1);
if (this.audioFound) {
this.audio.changeVolume(this.audioVolume);
}*/
}
public function outputAudio(downsampleInputLeft, downsampleInputRight):void {
/*downsampleInputLeft = downsampleInputLeft | 0;
downsampleInputRight = downsampleInputRight | 0;
this.audioBuffer[this.audioDestinationPosition++] = (downsampleInputLeft * this.audioDownSampleInputDivider) - 1;
this.audioBuffer[this.audioDestinationPosition++] = (downsampleInputRight * this.audioDownSampleInputDivider) - 1;
if (this.audioDestinationPosition == this.audioNumSamplesTotal) {
this.audio.writeAudioNoCallback(this.audioBuffer);
this.audioDestinationPosition = 0;
}*/
}
public function audioUnderrunAdjustment():void {
/*this.CPUCyclesTotal = this.CPUCyclesPerIteration | 0;
if (this.audioFound) {
var underrunAmount = this.audio.remainingBuffer();
if (typeof underrunAmount == "number") {
underrunAmount = this.audioBufferContainAmount - Math.max(underrunAmount, 0);
if (underrunAmount > 0) {
this.CPUCyclesTotal = Math.min((this.CPUCyclesTotal | 0) + (Math.min((underrunAmount >> 1) * this.audioResamplerFirstPassFactor, 0x7FFFFFFF) | 0), 0x7FFFFFFF) | 0;
}
}
}*/
}
public function audioPushNewState():void {
/*if (this.audioUpdateState) {
this.IOCore.sound.initializeOutput(this.audioFound, this.audioResamplerFirstPassFactor);
this.audioUpdateState = false;
}*/
}
public function audioSetState(audioFound):void {
if (this.audioFound != audioFound) {
this.audioFound = audioFound;
this.audioUpdateState = true;
}
}
public function reinitializeAudio():void {
if (this.audioFound) { //Set up the audio again if enabled.
this.disableAudio();
this.enableAudio();
}
}
public function toggleSkipBootROM(skipBoot):void {
this.SKIPBoot = !!skipBoot;
if (this.romFound && this.paused) {
this.initializeCore();
}
}
public function toggleDynarec(dynarecEnabled):void {
//Keep disabled by force until we rewrite the jit:
this.dynarecEnabled = !!dynarecEnabled;
}
}
}