Offthread gfx #22

Merged
merged 16 commits into from Jan 24, 2016
View
@@ -21,20 +21,15 @@ function GameBoyAdvanceEmulator() {
};
this.audioFound = 0; //Do we have audio output sink found yet?
this.emulatorStatus = 0x10; //{paused, saves loaded, fault found, loaded}
- this.offscreenWidth = 240; //Width of the GBA screen.
- this.offscreenHeight = 160; //Height of the GBA screen.
this.BIOS = []; //Initialize BIOS as not existing.
this.ROM = []; //Initialize BIOS as not existing.
- //Cache some frame buffer lengths:
- this.offscreenRGBCount = ((this.offscreenWidth | 0) * (this.offscreenHeight | 0) * 3) | 0;
- //Graphics buffers to generate in advance:
- this.frameBuffer = getInt32Array(this.offscreenRGBCount | 0); //The internal buffer to composite to.
- this.swizzledFrame = getUint8Array(this.offscreenRGBCount | 0); //The swizzled output buffer that syncs to the internal framebuffer on v-blank.
this.audioUpdateState = 1; //Do we need to update the sound core with new info?
this.saveExportHandler = null; //Save export handler attached by GUI.
this.saveImportHandler = null; //Save import handler attached by GUI.
this.speedCallback = null; //Speed report handler attached by GUI.
- this.graphicsHandle = null; //Graphics blitter handler attached by GUI.
+ this.startCallbacks = []; //Some jobs to run at iteration head.
+ this.endCallbacks = []; //Some jobs to run at iteration end.
+ this.terminationCallbacks = []; //Some jobs to run if the emulation core is killed.
this.timerIntervalRate = 4; //How often the emulator core is called into (in milliseconds).
this.lastTimestamp = 0; //Track the last time given in milliseconds.
this.dynamicSpeedRefresh = false; //Whether speed is allowed to be changed dynamically in the current cycle.
@@ -44,12 +39,18 @@ function GameBoyAdvanceEmulator() {
GameBoyAdvanceEmulator.prototype.generateCoreExposed = function () {
var parentObj = this;
this.coreExposed = {
- "outputAudio":function (l, r) {
+ outputAudio:function (l, r) {
parentObj.outputAudio(l, r);
},
- "frameBuffer":parentObj.frameBuffer,
- "prepareFrame":function () {
- parentObj.prepareFrame();
+ graphicsHandle:null,
+ appendStartIterationSync:function (callback) {
+ parentObj.startCallbacks.push(callback);
+ },
+ appendEndIterationSync:function (callback) {
+ parentObj.endCallbacks.push(callback);
+ },
+ appendTerminationSync:function (callback) {
+ parentObj.terminationCallbacks.push(callback);
}
}
}
@@ -104,11 +105,41 @@ GameBoyAdvanceEmulator.prototype.iterationStartSequence = function () {
this.emulatorStatus = this.emulatorStatus | 0x2; //If the end routine doesn't unset this, then we are marked as having crashed.
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.
+ this.runStartJobs(); //Run various callbacks assigned from internal components.
}
GameBoyAdvanceEmulator.prototype.iterationEndSequence = function () {
this.emulatorStatus = this.emulatorStatus & 0x1D; //If core did not throw while running, unset the fatal error flag.
this.clockCyclesSinceStart = ((this.clockCyclesSinceStart | 0) + (this.CPUCyclesTotal | 0)) | 0; //Accumulate tracking.
this.submitAudioBuffer(); //Flush audio buffer to output.
+ this.runEndJobs(); //Run various callbacks assigned from internal components.
+}
+GameBoyAdvanceEmulator.prototype.runStartJobs = function () {
+ var length = this.startCallbacks.length | 0;
+ //Loop through all jobs:
+ for (var index = 0; (index | 0) < (length | 0); index = ((index | 0) + 1) | 0) {
+ //Run job:
+ this.startCallbacks[index | 0]();
+ }
+}
+GameBoyAdvanceEmulator.prototype.runEndJobs = function () {
+ var length = this.endCallbacks.length | 0;
+ //Loop through all jobs:
+ for (var index = 0; (index | 0) < (length | 0); index = ((index | 0) + 1) | 0) {
+ //Run job:
+ this.endCallbacks[index | 0]();
+ }
+}
+GameBoyAdvanceEmulator.prototype.runTerminationJobs = function () {
+ var length = this.terminationCallbacks.length | 0;
+ //Loop through all jobs:
+ for (var index = 0; (index | 0) < (length | 0); index = ((index | 0) + 1) | 0) {
+ //Run job:
+ this.terminationCallbacks[index | 0]();
+ }
+ //Remove old jobs:
+ this.startCallbacks = [];
+ this.endCallbacks = [];
+ this.terminationCallbacks = [];
}
GameBoyAdvanceEmulator.prototype.attachROM = function (ROM) {
this.stop();
@@ -253,6 +284,8 @@ GameBoyAdvanceEmulator.prototype.calculateSpeedPercentage = function () {
}
}
GameBoyAdvanceEmulator.prototype.initializeCore = function () {
+ //Wrap up any old internal instance callbacks:
+ this.runTerminationJobs();
//Setup a new instance of the i/o core:
this.IOCore = new GameBoyAdvanceIO(this.settings.SKIPBoot, this.coreExposed, this.BIOS, this.ROM);
}
@@ -270,37 +303,14 @@ GameBoyAdvanceEmulator.prototype.keyUp = function (keyReleased) {
}
GameBoyAdvanceEmulator.prototype.attachGraphicsFrameHandler = function (handler) {
if (typeof handler == "object") {
- this.graphicsHandle = handler;
+ this.coreExposed.graphicsHandle = handler;
}
}
GameBoyAdvanceEmulator.prototype.attachAudioHandler = function (mixerInputHandler) {
if (mixerInputHandler) {
this.audio = mixerInputHandler;
}
}
-GameBoyAdvanceEmulator.prototype.swizzleFrameBuffer = function () {
- //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:
- var bufferIndex = 0;
- for (var canvasIndex = 0; (canvasIndex | 0) < (this.offscreenRGBCount | 0); bufferIndex = ((bufferIndex | 0) + 1) | 0) {
- this.swizzledFrame[canvasIndex | 0] = (this.frameBuffer[bufferIndex | 0] & 0x1F) << 3; //Red
- canvasIndex = ((canvasIndex | 0) + 1) | 0;
- this.swizzledFrame[canvasIndex | 0] = (this.frameBuffer[bufferIndex | 0] & 0x3E0) >> 2; //Green
- canvasIndex = ((canvasIndex | 0) + 1) | 0;
- this.swizzledFrame[canvasIndex | 0] = (this.frameBuffer[bufferIndex | 0] & 0x7C00) >> 7; //Blue
- canvasIndex = ((canvasIndex | 0) + 1) | 0;
- }
-}
-GameBoyAdvanceEmulator.prototype.prepareFrame = function () {
- //Copy the internal frame buffer to the output buffer:
- this.swizzleFrameBuffer();
- this.requestDraw();
-}
-GameBoyAdvanceEmulator.prototype.requestDraw = function () {
- if (this.graphicsHandle) {
- //We actually updated the graphics internally, so copy out:
- this.graphicsHandle.copyBuffer(this.swizzledFrame);
- }
-}
GameBoyAdvanceEmulator.prototype.enableAudio = function () {
if ((this.audioFound | 0) == 0 && this.audio) {
this.audioFound = 1; //Set audio to 'found' by default.
View
@@ -2102,7 +2102,7 @@ GameBoyAdvanceMemory.prototype.writeOBJ16 = function (address, data) {
data = data | 0;
this.IOCore.updateGraphicsClocking();
this.wait.OAMAccess();
- this.gfxRenderer.writeOAM16(address & 0x3FE, data & 0xFFFF);
+ this.gfxRenderer.writeOAM16(address | 0, data | 0);
}
GameBoyAdvanceMemory.prototype.writePalette32 = function (address, data) {
address = address | 0;
@@ -2123,7 +2123,7 @@ GameBoyAdvanceMemory.prototype.writeOBJ32 = function (address, data) {
data = data | 0;
this.IOCore.updateGraphicsClocking();
this.wait.OAMAccess();
- this.gfxRenderer.writeOAM32(address & 0x3FC, data | 0);
+ this.gfxRenderer.writeOAM32(address | 0, data | 0);
}
GameBoyAdvanceMemory.prototype.writeROM8 = function (address, data) {
address = address | 0;
@@ -1,56 +1,46 @@
"use strict";
/*
- Copyright (C) 2012-2015 Grant Galitz
+ Copyright (C) 2012-2016 Grant Galitz
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
-importScripts("../IodineGBA/includes/TypedArrayShim.js");
-importScripts("../IodineGBA/core/Cartridge.js");
-importScripts("../IodineGBA/core/DMA.js");
-importScripts("../IodineGBA/core/Emulator.js");
-importScripts("../IodineGBA/core/Graphics.js");
-importScripts("../IodineGBA/core/RunLoop.js");
-importScripts("../IodineGBA/core/Memory.js");
-importScripts("../IodineGBA/core/IRQ.js");
-importScripts("../IodineGBA/core/JoyPad.js");
-importScripts("../IodineGBA/core/Serial.js");
-importScripts("../IodineGBA/core/Sound.js");
-importScripts("../IodineGBA/core/Timer.js");
-importScripts("../IodineGBA/core/Wait.js");
-importScripts("../IodineGBA/core/CPU.js");
-importScripts("../IodineGBA/core/Saves.js");
-importScripts("../IodineGBA/core/sound/FIFO.js");
-importScripts("../IodineGBA/core/sound/Channel1.js");
-importScripts("../IodineGBA/core/sound/Channel2.js");
-importScripts("../IodineGBA/core/sound/Channel3.js");
-importScripts("../IodineGBA/core/sound/Channel4.js");
-importScripts("../IodineGBA/core/CPU/ARM.js");
-importScripts("../IodineGBA/core/CPU/THUMB.js");
-importScripts("../IodineGBA/core/CPU/CPSR.js");
-importScripts("../IodineGBA/core/graphics/Renderer.js");
-importScripts("../IodineGBA/core/graphics/RendererProxy.js");
-importScripts("../IodineGBA/core/graphics/BGTEXT.js");
-importScripts("../IodineGBA/core/graphics/BG2FrameBuffer.js");
-importScripts("../IodineGBA/core/graphics/BGMatrix.js");
-importScripts("../IodineGBA/core/graphics/AffineBG.js");
-importScripts("../IodineGBA/core/graphics/ColorEffects.js");
-importScripts("../IodineGBA/core/graphics/Mosaic.js");
-importScripts("../IodineGBA/core/graphics/OBJ.js");
-importScripts("../IodineGBA/core/graphics/OBJWindow.js");
-importScripts("../IodineGBA/core/graphics/Window.js");
-importScripts("../IodineGBA/core/graphics/Compositor.js");
-importScripts("../IodineGBA/core/memory/DMA0.js");
-importScripts("../IodineGBA/core/memory/DMA1.js");
-importScripts("../IodineGBA/core/memory/DMA2.js");
-importScripts("../IodineGBA/core/memory/DMA3.js");
-importScripts("../IodineGBA/core/cartridge/SaveDeterminer.js");
-importScripts("../IodineGBA/core/cartridge/SRAM.js");
-importScripts("../IodineGBA/core/cartridge/FLASH.js");
-importScripts("../IodineGBA/core/cartridge/EEPROM.js");
+importScripts("../includes/TypedArrayShim.js");
+importScripts("Cartridge.js");
+importScripts("DMA.js");
+importScripts("Emulator.js");
+importScripts("Graphics.js");
+importScripts("RunLoop.js");
+importScripts("Memory.js");
+importScripts("IRQ.js");
+importScripts("JoyPad.js");
+importScripts("Serial.js");
+importScripts("Sound.js");
+importScripts("Timer.js");
+importScripts("Wait.js");
+importScripts("CPU.js");
+importScripts("Saves.js");
+importScripts("sound/FIFO.js");
+importScripts("sound/Channel1.js");
+importScripts("sound/Channel2.js");
+importScripts("sound/Channel3.js");
+importScripts("sound/Channel4.js");
+importScripts("CPU/ARM.js");
+importScripts("CPU/THUMB.js");
+importScripts("CPU/CPSR.js");
+importScripts("graphics/RendererProxy.js");
+importScripts("graphics/RendererShim.js");
+importScripts("memory/DMA0.js");
+importScripts("memory/DMA1.js");
+importScripts("memory/DMA2.js");
+importScripts("memory/DMA3.js");
+importScripts("cartridge/SaveDeterminer.js");
+importScripts("cartridge/SRAM.js");
+importScripts("cartridge/FLASH.js");
+importScripts("cartridge/EEPROM.js");
var Iodine = new GameBoyAdvanceEmulator();
//Save callbacks waiting to be satisfied:
var saveImportPool = [];
@@ -140,6 +130,7 @@ self.onmessage = function (event) {
}
}
var graphicsFrameHandler = {
+ //Function only called if graphics is THIS thread:
copyBuffer:function (swizzledFrame) {
//Push a frame of graphics to the blitter handle:
//Load the counter values:
@@ -1,11 +1,11 @@
"use strict";
/*
Copyright (C) 2012-2015 Grant Galitz
-
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
function GameBoyAdvanceOBJRenderer(gfx) {
@@ -293,7 +293,7 @@ GameBoyAdvanceOBJRenderer.prototype.computeCycles = function (cycles, matrix2D,
cyclesToSubtract = cyclesToSubtract << 1;
cyclesToSubtract = ((cyclesToSubtract | 0) + 10) | 0;
cycles = ((cycles | 0) - (cyclesToSubtract | 0)) | 0;
-
+
}
else {
//Regular Scrolling:
@@ -896,7 +896,7 @@ if (__LITTLE_ENDIAN__) {
default:
this.OBJMatrixParameters[address >> 2] = (data << 16) >> 16;
}
- this.OAMRAM16[address | 0] = data | 0;
+ this.OAMRAM16[address | 0] = data & 0xFFFF;
}
GameBoyAdvanceOBJRenderer.prototype.writeOAM32 = function (address, data) {
address = address | 0;
@@ -930,17 +930,18 @@ if (__LITTLE_ENDIAN__) {
}
GameBoyAdvanceOBJRenderer.prototype.readOAM16 = function (address) {
address = address | 0;
- return this.OAMRAM16[(address >> 1) & 0x1FF] | 0;
+ return this.OAMRAM16[address & 0x1FF] | 0;
}
GameBoyAdvanceOBJRenderer.prototype.readOAM32 = function (address) {
address = address | 0;
- return this.OAMRAM32[(address >> 2) & 0xFF] | 0;
+ return this.OAMRAM32[address & 0xFF] | 0;
}
}
else {
GameBoyAdvanceOBJRenderer.prototype.writeOAM16 = function (address, data) {
address = address | 0;
data = data | 0;
+ address = address & 0x1FF;
var OAMTable = this.OAMTable[address >> 2];
switch (address & 0x3) {
//Attrib 0:
@@ -978,6 +979,7 @@ else {
GameBoyAdvanceOBJRenderer.prototype.writeOAM32 = function (address, data) {
address = address | 0;
data = data | 0;
+ address = address & 0xFF;
var OAMTable = this.OAMTable[address >> 1];
if ((address & 0x1) == 0) {
//Attrib 0:
@@ -1010,11 +1012,13 @@ else {
this.OAMRAM[address | 3] = data >>> 24;
}
GameBoyAdvanceOBJRenderer.prototype.readOAM16 = function (address) {
- address &= 0x3FE;
+ address &= 0x1FF;
+ address <<= 1;
return this.OAMRAM[address] | (this.OAMRAM[address | 1] << 8);
}
GameBoyAdvanceOBJRenderer.prototype.readOAM32 = function (address) {
- address &= 0x3FC;
+ address &= 0xFF;
+ address <<= 2;
return this.OAMRAM[address] | (this.OAMRAM[address | 1] << 8) | (this.OAMRAM[address | 2] << 16) | (this.OAMRAM[address | 3] << 24);
}
-}
+}
Oops, something went wrong.