diff --git a/dist/jsgbc-core.js b/dist/jsgbc-core.js index 40fd952..810cd23 100644 --- a/dist/jsgbc-core.js +++ b/dist/jsgbc-core.js @@ -1 +1 @@ -!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["jsgbc-core"]=e():t["jsgbc-core"]=e()}("undefined"!=typeof self?self:this,function(){return function(t){var e={};function i(r){if(e[r])return e[r].exports;var s=e[r]={i:r,l:!1,exports:{}};return t[r].call(s.exports,s,s.exports,i),s.l=!0,s.exports}return i.m=t,i.c=e,i.d=function(t,e,r){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)i.d(r,s,function(e){return t[e]}.bind(null,s));return r},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=3)}([function(t,e){t.exports=require("events")},function(t,e){t.exports=require("file-saver")},function(t,e){t.exports=require("jszip")},function(t,e,i){"use strict";i.r(e);var r={};i.r(r),i.d(r,"toTypedArray",function(){return l}),i.d(r,"fromTypedArray",function(){return c}),i.d(r,"getTypedArray",function(){return m}),i.d(r,"stringToArrayBuffer",function(){return u}),i.d(r,"fetchFileAsArrayBuffer",function(){return g}),i.d(r,"concatArrayBuffers",function(){return d}),i.d(r,"saveAs",function(){return y}),i.d(r,"readBlob",function(){return f}),i.d(r,"readCartridgeROM",function(){return C}),i.d(r,"hasExtension",function(){return p}),i.d(r,"debounce",function(){return B});var s={soundOn:!0,bootBootRomFirst:!0,gbHasPriority:!1,soundVolume:.7,colorizeGBMode:!0,runInterval:8,minAudioBufferSpanAmountOverXInterpreterIterations:10,maxAudioBufferSpanAmountOverXInterpreterIterations:20,alwaysAllowRWtoBanks:!1,forceGBBootRom:!1,enabledChannels:[!0,!0,!0,!0]},h=i(2),a=i.n(h),n=i(1),o=function(t,e,i,r){return new(i||(i=Promise))(function(s,h){function a(t){try{o(r.next(t))}catch(t){h(t)}}function n(t){try{o(r.throw(t))}catch(t){h(t)}}function o(t){t.done?s(t.value):new i(function(e){e(t.value)}).then(a,n)}o((r=r.apply(t,e||[])).next())})};function l(t,e){try{if(!(t&&t.length<1))return[];var i=t.length;let s;switch(e){case"uint8":s=new Uint8Array(i);break;case"int8":s=new Int8Array(i);break;case"int32":s=new Int32Array(i);break;case"float32":s=new Float32Array(i)}for(var r=0;r{if(t){const i=new FileReader;i.addEventListener("load",function(){2===this.readyState&&e(this.result)}),i.readAsArrayBuffer(t)}else i()})})}function C(t,e=""){return o(this,void 0,void 0,function*(){let i=yield f(t);if(p(e,"zip")){const t=yield a.a.loadAsync(i),e=Object.keys(t.files).filter(t=>p(t,"gbc")||p(t,"gb"));i=e.length>0?yield t.file(e[0]).async("arraybuffer"):null}return i})}function p(t,e){return t=t.toLowerCase(),e="."+e.toLowerCase(),t.lastIndexOf(e)===t.length-e.length}function B(t,e,i){var r,s,h,a,n;function o(){var l=Date.now()-a;l=0?r=setTimeout(o,e-l):(r=null,i||(n=t.apply(h,s),h=s=null))}null==e&&(e=100);var l=function(){h=this,s=arguments,a=Date.now();var l=i&&!r;return r||(r=setTimeout(o,e)),l&&(n=t.apply(h,s),h=s=null),n};return l.clear=function(){r&&(clearTimeout(r),r=null)},l.flush=function(){r&&(n=t.apply(h,s),h=s=null,clearTimeout(r),r=null)},l}var R=function(t,e,i,r){return new(i||(i=Promise))(function(s,h){function a(t){try{o(r.next(t))}catch(t){h(t)}}function n(t){try{o(r.throw(t))}catch(t){h(t)}}function o(t){t.done?s(t.value):new i(function(e){e(t.value)}).then(a,n)}o((r=r.apply(t,e||[])).next())})};class F{findState(t){return this.find("state-"+t)}findSRAM(t){return this.find("sram-"+t)}findRTC(t){return this.find("rtc-"+t)}setState(t,e){return R(this,void 0,void 0,function*(){return yield this.set("state-"+t,e)})}setSRAM(t,e){return R(this,void 0,void 0,function*(){return yield this.set("sram-"+t,e)})}setRTC(t,e){return R(this,void 0,void 0,function*(){return yield this.set("rtc-"+t,e)})}find(t){return function(t){if(!t||t.length<=0)return null;t=atob(t);const e=new Uint8Array(t.length);for(let i=0;i127,this.RTCHalt=64==(64&t),this.RTCDays=(1&t)<<8|255&this.RTCDays}writeHours(t){t<24?this.RTCHours=t:console.log("(Bank #"+this.mbc.currentMBCRAMBank+") RTC write out of range: "+t)}readSeconds(){return this.latchedSeconds}readMinutes(){return this.latchedMinutes}readHours(){return this.latchedHours}readDaysLow(){return this.latchedLDays}readDaysHigh(){return(this.RTCDayOverFlow?128:0)+(this.RTCHalt?64:0)+this.latchedHDays}writeLatch(t,e){0===e?this.RTCisLatched=!1:this.RTCisLatched||(this.RTCisLatched=!0,this.latchedSeconds=0|this.RTCSeconds,this.latchedMinutes=this.RTCMinutes,this.latchedHours=this.RTCHours,this.latchedLDays=255&this.RTCDays,this.latchedHDays=this.RTCDays>>8)}get(){const t=Math.round(this.lastTime/1e3),e=t>>0&65535,i=t>>16&65535;return new Uint32Array([this.RTCSeconds,this.RTCMinutes,this.RTCHours,this.RTCDays,this.RTCDayOverFlow,this.latchedSeconds,this.latchedMinutes,this.latchedHours,this.latchedLDays,this.latchedHDays,e,i])}load(t){const e=this.extract(t);this.RTCSeconds=e.seconds,this.RTCMinutes=e.minutes,this.RTCHours=e.hours,this.RTCDays=e.daysLow,this.RTCDayOverFlow=e.daysHigh,this.latchedSeconds=e.latchedSeconds,this.latchedMinutes=e.latchedMinutes,this.latchedHours=e.latchedHours,this.latchedLDays=e.latchedDaysLow,this.latchedHDays=e.latchedDaysHigh,this.lastTime=e.lastTime}cutBatteryFileArray(t){return new Uint32Array(t.slice(this.mbc.ramSize,this.mbc.ramSize+48))}extract(t){const e=t[0],i=t[1],r=t[2],s=t[3],h=t[4],a=t[5],n=t[6],o=t[7],l=t[8],c=t[9],m=t[10],u=t[11];let g=m;return m&&u&&(g=u<<16|m),{seconds:e,minutes:i,hours:r,daysLow:s,daysHigh:h,latchedSeconds:a,latchedMinutes:n,latchedHours:o,latchedDaysLow:l,latchedDaysHigh:c,lastTime:1e3*g}}saveState(){return[this.lastTime,this.RTCisLatched,this.latchedSeconds,this.latchedMinutes,this.latchedHours,this.latchedLDays,this.latchedHDays,this.RTCSeconds,this.RTCMinutes,this.RTCHours,this.RTCDays,this.RTCDayOverFlow,this.RTCHalt]}loadState(t){let e=0;this.lastTime=t[e++],this.RTCisLatched=t[e++],this.latchedSeconds=t[e++],this.latchedMinutes=t[e++],this.latchedHours=t[e++],this.latchedLDays=t[e++],this.latchedHDays=t[e++],this.RTCSeconds=t[e++],this.RTCMinutes=t[e++],this.RTCHours=t[e++],this.RTCDays=t[e++],this.RTCDayOverFlow=t[e++],this.RTCHalt=t[e]}updateClock(){const t=(new Date).getTime(),e=t-this.lastTime;if(this.lastTime=t,!this.RTCHalt)for(this.RTCSeconds+=e/1e3;this.RTCSeconds>=60;)this.RTCSeconds-=60,++this.RTCMinutes,this.RTCMinutes>=60&&(this.RTCMinutes-=60,++this.RTCHours,this.RTCHours>=24&&(this.RTCHours-=24,++this.RTCDays,this.RTCDays>=512&&(this.RTCDays-=512,this.RTCDayOverFlow=!0)))}}class M extends b{constructor(t){super(t),this.rtc=new A(this)}writeROMBank(t,e){this.ROMBank1Offset=127&e,this.setCurrentROMBank()}writeRAMBank(t,e){this.currentMBCRAMBank=e,e<4&&(this.currentRAMBankPosition=(this.currentMBCRAMBank<<13)-40960)}writeRAM(t,e){if(this.MBCRAMBanksEnabled||s.alwaysAllowRWtoBanks)switch(this.currentMBCRAMBank){case 0:case 1:case 2:case 3:this.emit("ramWrite"),this.RAM[t+this.currentRAMBankPosition]=e;break;case 8:this.rtc&&this.rtc.writeSeconds(e);break;case 9:this.rtc&&this.rtc.writeMinutes(e);break;case 10:this.rtc&&this.rtc.writeHours(e);break;case 11:this.rtc&&this.rtc.writeDaysLow(e);break;case 12:this.rtc&&this.rtc.writeDaysHigh(e);break;default:console.log("Invalid MBC3 bank address selected: "+this.currentMBCRAMBank)}}readRAM(t){if(this.MBCRAMBanksEnabled||s.alwaysAllowRWtoBanks)switch(this.currentMBCRAMBank){case 0:case 1:case 2:case 3:return this.RAM[t+this.currentRAMBankPosition];case 8:if(this.rtc)return this.rtc.readSeconds();break;case 9:if(this.rtc)return this.rtc.readMinutes();break;case 10:if(this.rtc)return this.rtc.readHours();break;case 11:if(this.rtc)return this.rtc.readDaysLow();break;case 12:if(this.rtc)return this.rtc.readDaysHigh()}return 255}}class T extends b{setCurrentROMBank(){this.currentROMBank=this.ROMBank1Offset%this.ROMBankEdge-1<<14}writeROMBankLow(t,e){this.ROMBank1Offset=256&this.ROMBank1Offset|e,this.setCurrentROMBank()}writeROMBankHigh(t,e){this.ROMBank1Offset=(1&e)<<8|255&this.ROMBank1Offset,this.setCurrentROMBank()}writeRAMBank(t,e){this.currentMBCRAMBank=15&e,this.currentRAMBankPosition=(this.currentMBCRAMBank<<13)-40960}}class P extends b{constructor(){super(...arguments),this.highX=127,this.lowX=127,this.highY=127,this.lowY=127}applyGyroEvent(t,e){t*=-100,t+=2047,this.highX=t>>8,this.lowX=255&t,e*=-100,e+=2047,this.highY=e>>8,this.lowY=255&e}read(t){if(this.MBCRAMBanksEnabled||s.alwaysAllowRWtoBanks)switch(t){case 40960:case 41056:case 41072:case 41088:return 0;case 41040:return this.highY;case 41024:return this.lowY;case 41008:return this.highX;case 40992:return this.lowX;default:return this.RAM[t+this.currentRAMBankPosition]}return 255}}class v extends T{writeRAMBank(t,e){this.currentMBCRAMBank=3&e,this.currentRAMBankPosition=(this.currentMBCRAMBank<<13)-40960,8&e&&this.emit("rumble")}}const w="Game and Watch 50";class W{constructor(t){this.hasMBC1=!1,this.hasMBC2=!1,this.hasMBC3=!1,this.hasMBC5=!1,this.hasMBC7=!1,this.hasSRAM=!1,this.hasRUMBLE=!1,this.hasCamera=!1,this.hasTAMA5=!1,this.hasHuC3=!1,this.hasHuC1=!1,this.hasMMMO1=!1,this.hasRTC=!1,this.hasBattery=!1,this.rom=t instanceof S?t:new S(t)}connect(t){this.gameboy=t}interpret(){if(this.name=this.rom.getString(308,318),this.gameCode=this.rom.getString(319,322),this.colorCompatibilityByte=this.rom.getByte(323),this.type=this.rom.getByte(327),this.setTypeName(),this.name&&console.log("Game Title: "+this.name),this.gameCode&&console.log("Game Code: "+this.gameCode),this.colorCompatibilityByte&&console.log("Color Compatibility Byte: "+this.colorCompatibilityByte),this.type&&console.log("Cartridge Type: "+this.type),this.typeName&&console.log("Cartridge Type Name: "+this.typeName),this.romSizeType=this.rom.getByte(328),this.ramSizeType=this.rom.getByte(329),this.gameboy.usedBootROM)console.log("used boot rom"),this.useGBCMode=this.gameboy.usedGBCBootROM;else switch(this.colorCompatibilityByte){case 0:this.useGBCMode=!1;break;case 50:s.gbHasPriority||this.name+this.gameCode+this.colorCompatibilityByte!==w?this.useGBCMode=!1:(this.useGBCMode=!0,console.log("Created a boot exception for Game and Watch Gallery 2 (GBC ID byte is wrong on the cartridge)."));break;case 128:this.useGBCMode=!s.gbHasPriority;break;case 192:this.useGBCMode=!0;break;default:this.useGBCMode=!1,console.warn("Unknown GameBoy game type code #"+this.colorCompatibilityByte+", defaulting to GB mode (Old games don't have a type code).")}const t=this.rom.getByte(331),e=65280&this.rom.getByte(324)|255&this.rom.getByte(325);51!==t?(this.hasNewLicenseCode=!1,this.licenseCode=t):(this.hasNewLicenseCode=!0,this.licenseCode=e)}setGBCMode(t){this.useGBCMode=0==(1&t),this.name+this.gameCode+this.colorCompatibilityByte===w&&(this.useGBCMode=!0,console.log("Created a boot exception for Game and Watch Gallery 2 (GBC ID byte is wrong on the cartridge).")),console.log("Booted to GBC Mode: "+this.useGBCMode)}setTypeName(){switch(this.type){case 0:this.typeName="ROM";break;case 1:this.hasMBC1=!0,this.typeName="MBC1";break;case 2:this.hasMBC1=!0,this.hasSRAM=!0,this.typeName="MBC1 + SRAM";break;case 3:this.hasMBC1=!0,this.hasSRAM=!0,this.hasBattery=!0,this.typeName="MBC1 + SRAM + Battery";break;case 5:this.hasMBC2=!0,this.typeName="MBC2";break;case 6:this.hasMBC2=!0,this.hasBattery=!0,this.typeName="MBC2 + Battery";break;case 8:this.hasSRAM=!0,this.typeName="ROM + SRAM";break;case 9:this.hasSRAM=!0,this.hasBattery=!0,this.typeName="ROM + SRAM + Battery";break;case 11:this.hasMMMO1=!0,this.typeName="MMMO1";break;case 12:this.hasMMMO1=!0,this.hasSRAM=!0,this.typeName="MMMO1 + SRAM";break;case 13:this.hasMMMO1=!0,this.hasSRAM=!0,this.hasBattery=!0,this.typeName="MMMO1 + SRAM + Battery";break;case 15:this.hasMBC3=!0,this.hasRTC=!0,this.hasBattery=!0,this.typeName="MBC3 + RTC + Battery";break;case 16:this.hasMBC3=!0,this.hasRTC=!0,this.hasBattery=!0,this.hasSRAM=!0,this.typeName="MBC3 + RTC + Battery + SRAM";break;case 17:this.hasMBC3=!0,this.typeName="MBC3";break;case 18:this.hasMBC3=!0,this.hasSRAM=!0,this.typeName="MBC3 + SRAM";break;case 19:this.hasMBC3=!0,this.hasSRAM=!0,this.hasBattery=!0,this.typeName="MBC3 + SRAM + Battery";break;case 25:this.hasMBC5=!0,this.typeName="MBC5";break;case 26:this.hasMBC5=!0,this.hasSRAM=!0,this.typeName="MBC5 + SRAM";break;case 27:this.hasMBC5=!0,this.hasSRAM=!0,this.hasBattery=!0,this.typeName="MBC5 + SRAM + Battery";break;case 28:this.hasRUMBLE=!0,this.typeName="RUMBLE";break;case 29:this.hasRUMBLE=!0,this.hasSRAM=!0,this.typeName="RUMBLE + SRAM";break;case 30:this.hasRUMBLE=!0,this.hasSRAM=!0,this.hasBattery=!0,this.typeName="RUMBLE + SRAM + Battery";break;case 31:this.hasCamera=!0,this.typeName="GameBoy Camera";break;case 34:this.hasMBC7=!0,this.hasSRAM=!0,this.hasBattery=!0,this.typeName="MBC7 + SRAM + Battery";break;case 253:this.hasTAMA5=!0,this.typeName="TAMA5";break;case 254:this.hasHuC3=!0,this.typeName="HuC3";break;case 255:this.hasHuC1=!0,this.typeName="HuC1";break;default:throw new Error("Unknown Cartridge Type")}this.hasMBC1&&(this.mbc1=new k(this)),this.hasMBC2&&(this.mbc2=new L(this)),this.hasMBC3&&(this.mbc3=new M(this)),this.hasMBC5&&(this.mbc5=new T(this)),this.hasMBC7&&(this.mbc7=new P(this)),this.hasRUMBLE&&(this.mbc5=this.rumble=new v(this)),this.mbc=this.mbc1||this.mbc2||this.mbc3||this.mbc5||this.mbc7||this.rumble||null}setupRAM(){this.mbc&&this.mbc.setupRAM(),this.gameboy.api.loadSRAM(),this.gameboy.api.loadRTC()}}class G extends H.EventEmitter{constructor(){super(...arguments),this.map={}}register(t){return this.map[t]=!0,this}getAll(){return Object.keys(this.map)}is(t){return!!this.map[t]}down(t,e){this.emit("down-"+t,e)}change(t,e){this.emit("change-"+t,e)}up(t,e){this.emit("up-"+t,e)}}var O=[4,12,8,8,4,4,8,4,20,8,8,8,4,4,8,4,4,12,8,8,4,4,8,4,12,8,8,8,4,4,8,4,8,12,8,8,4,4,8,4,8,8,8,8,4,4,8,4,8,12,8,8,12,12,12,4,8,8,8,8,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,8,8,8,8,8,8,4,8,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,8,12,12,16,12,16,8,16,8,16,12,0,12,24,8,16,8,12,12,4,12,16,8,16,8,16,12,4,12,4,8,16,12,12,8,4,4,16,8,16,16,4,16,4,4,4,8,16,12,12,8,4,4,16,8,16,12,8,16,4,0,4,8,16];class E{constructor(t,e,i,r,s){this.fromSampleRate=t,this.toSampleRate=e,this.channels=0|i,this.outputBufferSize=r,this.noReturn=!!s,this.initialize()}initialize(){if(!(this.fromSampleRate>0&&this.toSampleRate>0&&this.channels>0))throw new Error("Invalid settings specified for the resampler.");this.fromSampleRate===this.toSampleRate?(this.resampler=this.bypassResampler,this.ratioWeight=1):(this.ratioWeight=this.fromSampleRate/this.toSampleRate,this.fromSampleRate 0) { \t\t\tvar weight = this.lastWeight; \t\t\tvar firstWeight = 0; \t\t\tvar secondWeight = 0; \t\t\tvar sourceOffset = 0; \t\t\tvar outputOffset = 0; \t\t\tvar outputBuffer = this.outputBuffer; \t\t\tfor (; weight < 1; weight += "+this.ratioWeight+") { \t\t\t\tsecondWeight = weight % 1; \t\t\t\tfirstWeight = 1 - secondWeight;";for(let e=0;e0?" + "+e:"")+"] * firstWeight) + (buffer[sourceOffset + "+(this.channels+e)+"] * secondWeight);";t+="weight += "+this.ratioWeight+"; \t\t\t\tsourceOffset = Math.floor(weight) * "+this.channels+"; \t\t\t}";for(let e=0;e 0) { \t\t\tvar weight = 0;";for(let e=0;e0?" + "+e:"")+"] * weight;";t+="currentPosition += weight; \t\t\t\t\t\tweight = 0; \t\t\t\t\t\tbreak; \t\t\t\t\t} \t\t\t\t} \t\t\t\tif (weight <= 0) {";for(let e=0;ethis.minBufferSize+this.channelsAllocated?t&-this.channelsAllocated:this.minBufferSize*this.channelsAllocated}writeAudio(t){let e=0;for(;ethis.processAudio(t)),this.audioNode.connect(this.context.destination),this.resetAudioBuffer(this.context.sampleRate))})}processAudio(t){const e=[];let i=0;for(;i0){const t=this.resampleControl.resampler(this.getBufferSamples()),e=this.resampleControl.outputBuffer;for(let i=0;i0;){for(e=Math.min(this.audioClocksUntilNextEventCounter,this.sequencerClocks,t),this.audioClocksUntilNextEventCounter-=e,this.sequencerClocks-=e,t-=e;e>0;){const t=Math.min(e,this.resamplerFirstPassFactor-this.audioIndex);e-=t,this.audioIndex+=t,this.downsampleInput+=this.mixerOutputCache*t,this.audioIndex===this.resamplerFirstPassFactor&&(this.audioIndex=0,this.outputAudio())}0===this.sequencerClocks&&(this.audioComputeSequencer(),this.sequencerClocks=8192),0===this.audioClocksUntilNextEventCounter&&this.computeChannels()}else for(;t>0;){const e=Math.min(t,this.resamplerFirstPassFactor-this.audioIndex);t-=e,this.audioIndex+=e,this.audioIndex===this.resamplerFirstPassFactor&&(this.audioIndex=0,this.outputAudio())}}generateFake(t){if(this.gameboy.soundMasterEnabled&&!this.gameboy.CPUStopped){let e=0;for(;t>0;)e=Math.min(this.audioClocksUntilNextEventCounter,this.sequencerClocks,t),this.audioClocksUntilNextEventCounter-=e,this.sequencerClocks-=e,t-=e,0===this.sequencerClocks&&(this.audioComputeSequencer(),this.sequencerClocks=8192),0===this.audioClocksUntilNextEventCounter&&this.computeChannels()}}runJIT(){s.soundOn?this.generate(this.audioTicks):this.generateFake(this.audioTicks),this.audioTicks=0}clockAudioEnvelope(){this.channel1envelopeSweepsLast>-1&&(this.channel1envelopeSweeps>0?--this.channel1envelopeSweeps:this.channel1envelopeType?this.channel1envelopeVolume<15?(++this.channel1envelopeVolume,this.channel1envelopeSweeps=this.channel1envelopeSweepsLast,this.cacheChannel1OutputLevel()):this.channel1envelopeSweepsLast=-1:this.channel1envelopeVolume>0?(--this.channel1envelopeVolume,this.channel1envelopeSweeps=this.channel1envelopeSweepsLast,this.cacheChannel1OutputLevel()):this.channel1envelopeSweepsLast=-1),this.channel2envelopeSweepsLast>-1&&(this.channel2envelopeSweeps>0?--this.channel2envelopeSweeps:this.channel2envelopeType?this.channel2envelopeVolume<15?(++this.channel2envelopeVolume,this.channel2envelopeSweeps=this.channel2envelopeSweepsLast,this.cacheChannel2OutputLevel()):this.channel2envelopeSweepsLast=-1:this.channel2envelopeVolume>0?(--this.channel2envelopeVolume,this.channel2envelopeSweeps=this.channel2envelopeSweepsLast,this.cacheChannel2OutputLevel()):this.channel2envelopeSweepsLast=-1),this.channel4envelopeSweepsLast>-1&&(this.channel4envelopeSweeps>0?--this.channel4envelopeSweeps:this.channel4envelopeType?this.channel4envelopeVolume<15?(this.channel4currentVolume=++this.channel4envelopeVolume<0?(this.channel4currentVolume=--this.channel4envelopeVolume<0&&!this.channel1decreaseSweep){const t=this.channel1ShadowFrequency+(this.channel1ShadowFrequency>>this.channel1frequencySweepDivider);t<=2047?t+(t>>this.channel1frequencySweepDivider)>2047&&(this.channel1SweepFault=!0,this.checkChannel1Enable(),this.memory[65318]&=254):(this.channel1SweepFault=!0,this.checkChannel1Enable(),this.memory[65318]&=254)}}audioComputeSequencer(){switch(this.sequencePosition++){case 0:this.clockAudioLength();break;case 2:this.clockAudioLength(),this.clockAudioSweep();break;case 4:this.clockAudioLength();break;case 6:this.clockAudioLength(),this.clockAudioSweep();break;case 7:this.clockAudioEnvelope(),this.sequencePosition=0}}clockAudioLength(){this.channel1totalLength>1?--this.channel1totalLength:1===this.channel1totalLength&&(this.channel1totalLength=0,this.checkChannel1Enable(),this.memory[65318]&=254),this.channel2totalLength>1?--this.channel2totalLength:1===this.channel2totalLength&&(this.channel2totalLength=0,this.checkChannel2Enable(),this.memory[65318]&=253),this.channel3totalLength>1?--this.channel3totalLength:1===this.channel3totalLength&&(this.channel3totalLength=0,this.checkChannel3Enable(),this.memory[65318]&=251),this.channel4totalLength>1?--this.channel4totalLength:1===this.channel4totalLength&&(this.channel4totalLength=0,this.checkChannel4Enable(),this.memory[65318]&=247)}clockAudioSweep(){!this.channel1SweepFault&&this.channel1timeSweep>0&&0==--this.channel1timeSweep&&this.runAudioSweep()}runAudioSweep(){this.channel1lastTimeSweep>0&&(this.channel1frequencySweepDivider>0?(this.channel1Swept=!0,this.channel1decreaseSweep?(this.channel1ShadowFrequency-=this.channel1ShadowFrequency>>this.channel1frequencySweepDivider,this.channel1frequency=2047&this.channel1ShadowFrequency,this.channel1FrequencyTracker=2048-this.channel1frequency<<2):(this.channel1ShadowFrequency+=this.channel1ShadowFrequency>>this.channel1frequencySweepDivider,this.channel1frequency=this.channel1ShadowFrequency,this.channel1ShadowFrequency<=2047?(this.channel1FrequencyTracker=2048-this.channel1frequency<<2,this.channel1ShadowFrequency+(this.channel1ShadowFrequency>>this.channel1frequencySweepDivider)>2047&&(this.channel1SweepFault=!0,this.checkChannel1Enable(),this.memory[65318]&=254)):(this.channel1frequency&=2047,this.channel1SweepFault=!0,this.checkChannel1Enable(),this.memory[65318]&=254)),this.channel1timeSweep=this.channel1lastTimeSweep):(this.channel1SweepFault=!0,this.checkChannel1Enable()))}computeChannels(){this.channel1FrequencyCounter-=this.audioClocksUntilNextEvent,this.channel2FrequencyCounter-=this.audioClocksUntilNextEvent,this.channel3Counter-=this.audioClocksUntilNextEvent,this.channel4Counter-=this.audioClocksUntilNextEvent,0===this.channel1FrequencyCounter&&(this.channel1FrequencyCounter=this.channel1FrequencyTracker,this.channel1DutyTracker=this.channel1DutyTracker+1&7,this.cacheChannel1OutputLevelTrimary()),0===this.channel2FrequencyCounter&&(this.channel2FrequencyCounter=this.channel2FrequencyTracker,this.channel2DutyTracker=this.channel2DutyTracker+1&7,this.cacheChannel2OutputLevelTrimary()),0===this.channel3Counter&&(this.channel3canPlay&&(this.channel3lastSampleLookup=this.channel3lastSampleLookup+1&31),this.channel3Counter=this.channel3FrequencyPeriod,this.cacheChannel3Update()),0===this.channel4Counter&&(this.channel4lastSampleLookup=this.channel4lastSampleLookup+1&this.channel4BitRange,this.channel4Counter=this.channel4FrequencyPeriod,this.cacheChannel4Update()),this.audioClocksUntilNextEventCounter=this.audioClocksUntilNextEvent=Math.min(this.channel1FrequencyCounter,this.channel2FrequencyCounter,this.channel3Counter,this.channel4Counter)}checkChannel1Enable(){this.channel1Enabled=(this.channel1consecutive||this.channel1totalLength>0)&&!this.channel1SweepFault&&this.channel1canPlay,this.cacheChannel1OutputLevelSecondary()}cacheChannel1OutputLevel(){this.channel1currentSampleLeft=this.leftChannel1?this.channel1envelopeVolume:0,this.channel1currentSampleRight=this.rightChannel1?this.channel1envelopeVolume:0,this.cacheChannel1OutputLevelSecondary()}checkChannel1VolumeEnable(){this.channel1canPlay=this.memory[65298]>7,this.checkChannel1Enable(),this.cacheChannel1OutputLevelSecondary()}cacheChannel1OutputLevelSecondary(){this.channel1Enabled?(this.channel1currentSampleLeftSecondary=this.channel1currentSampleLeft,this.channel1currentSampleRightSecondary=this.channel1currentSampleRight):(this.channel1currentSampleLeftSecondary=0,this.channel1currentSampleRightSecondary=0),this.cacheChannel1OutputLevelTrimary()}cacheChannel1OutputLevelTrimary(){this.channel1CachedDuty[this.channel1DutyTracker]&&s.enabledChannels[0]?(this.channel1currentSampleLeftTrimary=this.channel1currentSampleLeftSecondary,this.channel1currentSampleRightTrimary=this.channel1currentSampleRightSecondary):(this.channel1currentSampleLeftTrimary=0,this.channel1currentSampleRightTrimary=0),this.cacheMixerOutputLevel()}checkChannel2Enable(){this.channel2Enabled=(this.channel2consecutive||this.channel2totalLength>0)&&this.channel2canPlay,this.cacheChannel2OutputLevelSecondary()}cacheChannel2OutputLevel(){this.channel2currentSampleLeft=this.leftChannel2?this.channel2envelopeVolume:0,this.channel2currentSampleRight=this.rightChannel2?this.channel2envelopeVolume:0,this.cacheChannel2OutputLevelSecondary()}checkChannel2VolumeEnable(){this.channel2canPlay=this.memory[65303]>7,this.checkChannel2Enable(),this.cacheChannel2OutputLevelSecondary()}cacheChannel2OutputLevelSecondary(){this.channel2Enabled?(this.channel2currentSampleLeftSecondary=this.channel2currentSampleLeft,this.channel2currentSampleRightSecondary=this.channel2currentSampleRight):(this.channel2currentSampleLeftSecondary=0,this.channel2currentSampleRightSecondary=0),this.cacheChannel2OutputLevelTrimary()}cacheChannel2OutputLevelTrimary(){this.channel2CachedDuty[this.channel2DutyTracker]&&s.enabledChannels[1]?(this.channel2currentSampleLeftTrimary=this.channel2currentSampleLeftSecondary,this.channel2currentSampleRightTrimary=this.channel2currentSampleRightSecondary):(this.channel2currentSampleLeftTrimary=0,this.channel2currentSampleRightTrimary=0),this.cacheMixerOutputLevel()}cacheChannel3Update(){this.cachedChannel3Sample=this.channel3PCM[this.channel3lastSampleLookup]>>this.channel3patternType,this.cacheChannel3OutputLevel()}checkChannel3Enable(){this.channel3Enabled=this.channel3consecutive||this.channel3totalLength>0,this.channel3OutputLevelSecondaryCache()}cacheChannel3OutputLevel(){this.channel3currentSampleLeft=this.leftChannel3?this.cachedChannel3Sample:0,this.channel3currentSampleRight=this.rightChannel3?this.cachedChannel3Sample:0,this.channel3OutputLevelSecondaryCache()}channel3OutputLevelSecondaryCache(){this.channel3Enabled&&s.enabledChannels[2]?(this.channel3currentSampleLeftSecondary=this.channel3currentSampleLeft,this.channel3currentSampleRightSecondary=this.channel3currentSampleRight):(this.channel3currentSampleLeftSecondary=0,this.channel3currentSampleRightSecondary=0),this.cacheMixerOutputLevel()}checkChannel4Enable(){this.channel4Enabled=(this.channel4consecutive||this.channel4totalLength>0)&&this.channel4canPlay,this.cacheChannel4OutputLevelSecondary()}cacheChannel4Update(){this.cachedChannel4Sample=this.noiseSampleTable[this.channel4currentVolume|this.channel4lastSampleLookup],this.cacheChannel4OutputLevel()}cacheChannel4OutputLevel(){this.channel4currentSampleLeft=this.leftChannel4?this.cachedChannel4Sample:0,this.channel4currentSampleRight=this.rightChannel4?this.cachedChannel4Sample:0,this.cacheChannel4OutputLevelSecondary()}checkChannel4VolumeEnable(){this.channel4canPlay=this.memory[65313]>7,this.checkChannel4Enable(),this.cacheChannel4OutputLevelSecondary()}cacheChannel4OutputLevelSecondary(){this.channel4Enabled&&s.enabledChannels[3]?(this.channel4currentSampleLeftSecondary=this.channel4currentSampleLeft,this.channel4currentSampleRightSecondary=this.channel4currentSampleRight):(this.channel4currentSampleLeftSecondary=0,this.channel4currentSampleRightSecondary=0),this.cacheMixerOutputLevel()}cacheMixerOutputLevel(){const t=this.channel1currentSampleLeftTrimary+this.channel2currentSampleLeftTrimary+this.channel3currentSampleLeftSecondary+this.channel4currentSampleLeftSecondary,e=this.channel1currentSampleRightTrimary+this.channel2currentSampleRightTrimary+this.channel3currentSampleRightSecondary+this.channel4currentSampleRightSecondary;this.mixerOutputCache=t*this.VinLeftChannelMasterVolume<<16|e*this.VinRightChannelMasterVolume}connectDevice(t){this.resamplerFirstPassFactor=Math.max(Math.min(Math.floor(this.cpu.clocksPerSecond/44100),Math.floor(136.53125)),1),this.downSampleInputDivider=1/(240*this.resamplerFirstPassFactor);const e=this.cpu.clocksPerSecond/this.resamplerFirstPassFactor,i=Math.max(this.cpu.baseCyclesPerIteration*s.maxAudioBufferSpanAmountOverXInterpreterIterations/this.resamplerFirstPassFactor,8192)<<1;t.setSampleRate(e),t.setMaxBufferSize(i),t.initializeAudio(),this.device=t}setVolume(t){this.device&&this.device.setVolume(t)}adjustUnderrun(){if(!s.soundOn)return;let t=this.device.remainingBuffer();"number"==typeof t&&(t=this.bufferContainAmount-Math.max(t,0))>0&&this.recalculateIterationClockLimitForAudio((t>>1)*this.resamplerFirstPassFactor)}recalculateIterationClockLimitForAudio(t){this.cpu.cyclesTotal+=Math.min(t>>2<<2,this.cpu.cyclesTotalBase<<1)}outputAudio(){this.fillBuffer(),this.bufferPosition===this.bufferLength&&(this.device.writeAudio(this.buffer),this.bufferPosition=0),this.downsampleInput=0}fillBuffer(){this.buffer[this.bufferPosition++]=(this.downsampleInput>>>16)*this.downSampleInputDivider-1,this.buffer[this.bufferPosition++]=(65535&this.downsampleInput)*this.downSampleInputDivider-1}initBuffer(){this.audioIndex=0,this.bufferPosition=0,this.downsampleInput=0,this.bufferContainAmount=Math.max(this.cpu.baseCyclesPerIteration*s.minAudioBufferSpanAmountOverXInterpreterIterations/this.resamplerFirstPassFactor,4096)<<1,this.bufferLength=this.cpu.baseCyclesPerIteration/this.resamplerFirstPassFactor<<1,this.buffer=m(this.bufferLength,0,"float32")}generateWhiteNoise(){this.LSFR7Table=this.generateLSFR7Table(),this.LSFR15Table=this.generateLSFR15Table(),this.noiseSampleTable=this.LSFR15Table}generateLSFR7Table(){const t=m(2048,0,"int8");let e=127;for(let i=0;i<128;++i){const r=1-(1&e);t[128|i]=r,t[256|i]=2*r,t[384|i]=3*r,t[512|i]=4*r,t[640|i]=5*r,t[768|i]=6*r,t[896|i]=7*r,t[1024|i]=8*r,t[1152|i]=9*r,t[1280|i]=10*r,t[1408|i]=11*r,t[1536|i]=12*r,t[1664|i]=13*r,t[1792|i]=14*r,t[1920|i]=15*r;const s=e>>1;e=s|(1&(s^e))<<6}return t}generateLSFR15Table(){const t=m(524288,0,"int8");let e=32767;for(let i=0;i<32768;++i){const r=1-(1&e);t[32768|i]=r,t[65536|i]=2*r,t[98304|i]=3*r,t[131072|i]=4*r,t[163840|i]=5*r,t[196608|i]=6*r,t[229376|i]=7*r,t[262144|i]=8*r,t[294912|i]=9*r,t[327680|i]=10*r,t[360448|i]=11*r,t[393216|i]=12*r,t[425984|i]=13*r,t[458752|i]=14*r,t[491520|i]=15*r;const s=e>>1;e=s|(1&(s^e))<<14}return t}}class q{constructor({canvas:t,context:e,offscreenCanvas:i,offscreenContext:r,gameboy:s,width:h,height:a}){if(this.canvas=t,this.context=e,this.offscreenCanvas=i,this.offscreenContext=r,this.gameboy=s,this.offscreenWidth=160,this.offscreenHeight=144,this.offscreenRGBCount=this.offscreenWidth*this.offscreenHeight*3,this.offscreenRGBACount=this.offscreenWidth*this.offscreenHeight*4,this.width=h||this.offscreenWidth,this.height=a||this.offscreenHeight,this.swizzledFrame=null,this.canvasBuffer=null,this.resizePathClear=!0,"undefined"!=typeof document&&(this.canvas||(this.canvas=document.createElement("canvas")),this.offscreenCanvas||(this.offscreenCanvas=document.createElement("canvas"))),this.canvas&&(this.canvas.height=this.height,this.canvas.width=this.width,this.context||(this.context=this.canvas.getContext("2d"))),this.offscreenCanvas&&(this.offscreenCanvas.height=this.offscreenHeight,this.offscreenCanvas.width=this.offscreenWidth,this.offscreenContext||(this.offscreenContext=this.offscreenCanvas.getContext("2d"))),!this.context)throw new Error("please provide a canvas context in the lcd options");if(!this.offscreenContext)throw new Error("please provide a canvas offscreen context in the lcd options")}init(){this.offscreenContext.msImageSmoothingEnabled=!1,this.offscreenContext.mozImageSmoothingEnabled=!1,this.offscreenContext.webkitImageSmoothingEnabled=!1,this.offscreenContext.imageSmoothingEnabled=!1,this.context.msImageSmoothingEnabled=!1,this.context.mozImageSmoothingEnabled=!1,this.context.webkitImageSmoothingEnabled=!1,this.context.imageSmoothingEnabled=!1,this.canvasBuffer=this.offscreenContext.createImageData(this.offscreenWidth,this.offscreenHeight);let t=this.offscreenRGBACount;for(;t>0;)t-=4,this.canvasBuffer.data[t]=248,this.canvasBuffer.data[t+1]=248,this.canvasBuffer.data[t+2]=248,this.canvasBuffer.data[t+3]=255;this.graphicsBlit(),this.swizzledFrame||(this.swizzledFrame=m(this.offscreenRGBCount,255,"uint8")),this.drewFrame=!0,this.requestDraw()}graphicsBlit(){this.offscreenWidth===this.width&&this.offscreenHeight===this.height?this.context.putImageData(this.canvasBuffer,0,0):(this.offscreenContext.putImageData(this.canvasBuffer,0,0),this.context.drawImage(this.offscreenCanvas,0,0,this.width,this.height))}requestDraw(){this.drewFrame&&this.dispatchDraw()}dispatchDraw(){this.offscreenRGBACount>0&&92160===this.offscreenRGBACount&&this.processDraw(this.swizzledFrame)}resizeFrameBuffer(){this.resizePathClear&&(this.resizePathClear=!1,this.resizer.resize(this.swizzledFrame))}processDraw(t){const e=this.canvasBuffer.data;let i=0,r=0;for(;r>16&255,e[r++]=t[i]>>8&255,e[r++]=255&t[i],++i}DisplayShowOff(){0===this.drewBlank&&(this.clearFrameBuffer(),this.drewFrame=!0),this.drewBlank=2}clearFrameBuffer(){const t=this.swizzledFrame;let e=0;if(this.gameboy.cartridge.useGBCMode||this.colorizedGBPalettes)for(;e127,this.registerB=this.registerB<<1&255|(this.FCarry?1:0),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){this.FCarry=this.registerC>127,this.registerC=this.registerC<<1&255|(this.FCarry?1:0),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){this.FCarry=this.registerD>127,this.registerD=this.registerD<<1&255|(this.FCarry?1:0),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){this.FCarry=this.registerE>127,this.registerE=this.registerE<<1&255|(this.FCarry?1:0),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){this.FCarry=this.registersHL>32767,this.registersHL=this.registersHL<<1&65024|(this.FCarry?256:0)|255&this.registersHL,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){this.FCarry=128==(128&this.registersHL),this.registersHL=65280&this.registersHL|this.registersHL<<1&255|(this.FCarry?1:0),this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL);this.FCarry=t>127,t=t<<1&255|(this.FCarry?1:0),this.memoryWriter[this.registersHL](this.registersHL,t),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===t},function(){this.FCarry=this.registerA>127,this.registerA=this.registerA<<1&255|(this.FCarry?1:0),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){this.FCarry=1==(1&this.registerB),this.registerB=(this.FCarry?128:0)|this.registerB>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){this.FCarry=1==(1&this.registerC),this.registerC=(this.FCarry?128:0)|this.registerC>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){this.FCarry=1==(1&this.registerD),this.registerD=(this.FCarry?128:0)|this.registerD>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){this.FCarry=1==(1&this.registerE),this.registerE=(this.FCarry?128:0)|this.registerE>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){this.FCarry=256==(256&this.registersHL),this.registersHL=(this.FCarry?32768:0)|this.registersHL>>1&65280|255&this.registersHL,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){this.FCarry=1==(1&this.registersHL),this.registersHL=65280&this.registersHL|(this.FCarry?128:0)|(255&this.registersHL)>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL);this.FCarry=1==(1&t),t=(this.FCarry?128:0)|t>>1,this.memoryWriter[this.registersHL](this.registersHL,t),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===t},function(){this.FCarry=1==(1&this.registerA),this.registerA=(this.FCarry?128:0)|this.registerA>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){var t=this.registerB>127;this.registerB=this.registerB<<1&255|(this.FCarry?1:0),this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){var t=this.registerC>127;this.registerC=this.registerC<<1&255|(this.FCarry?1:0),this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){var t=this.registerD>127;this.registerD=this.registerD<<1&255|(this.FCarry?1:0),this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){var t=this.registerE>127;this.registerE=this.registerE<<1&255|(this.FCarry?1:0),this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){var t=this.registersHL>32767;this.registersHL=this.registersHL<<1&65024|(this.FCarry?256:0)|255&this.registersHL,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){var t=128==(128&this.registersHL);this.registersHL=65280&this.registersHL|this.registersHL<<1&255|(this.FCarry?1:0),this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL),e=t>127;t=t<<1&255|(this.FCarry?1:0),this.FCarry=e,this.memoryWriter[this.registersHL](this.registersHL,t),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===t},function(){var t=this.registerA>127;this.registerA=this.registerA<<1&255|(this.FCarry?1:0),this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){var t=1==(1&this.registerB);this.registerB=(this.FCarry?128:0)|this.registerB>>1,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){var t=1==(1&this.registerC);this.registerC=(this.FCarry?128:0)|this.registerC>>1,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){var t=1==(1&this.registerD);this.registerD=(this.FCarry?128:0)|this.registerD>>1,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){var t=1==(1&this.registerE);this.registerE=(this.FCarry?128:0)|this.registerE>>1,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){var t=256==(256&this.registersHL);this.registersHL=(this.FCarry?32768:0)|this.registersHL>>1&65280|255&this.registersHL,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){var t=1==(1&this.registersHL);this.registersHL=65280&this.registersHL|(this.FCarry?128:0)|(255&this.registersHL)>>1,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL),e=1==(1&t);t=(this.FCarry?128:0)|t>>1,this.FCarry=e,this.memoryWriter[this.registersHL](this.registersHL,t),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===t},function(){var t=1==(1&this.registerA);this.registerA=(this.FCarry?128:0)|this.registerA>>1,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){this.FCarry=this.registerB>127,this.registerB=this.registerB<<1&255,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){this.FCarry=this.registerC>127,this.registerC=this.registerC<<1&255,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){this.FCarry=this.registerD>127,this.registerD=this.registerD<<1&255,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){this.FCarry=this.registerE>127,this.registerE=this.registerE<<1&255,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){this.FCarry=this.registersHL>32767,this.registersHL=this.registersHL<<1&65024|255&this.registersHL,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){this.FCarry=128==(128&this.registersHL),this.registersHL=65280&this.registersHL|this.registersHL<<1&255,this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL);this.FCarry=t>127,t=t<<1&255,this.memoryWriter[this.registersHL](this.registersHL,t),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===t},function(){this.FCarry=this.registerA>127,this.registerA=this.registerA<<1&255,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){this.FCarry=1==(1&this.registerB),this.registerB=128&this.registerB|this.registerB>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){this.FCarry=1==(1&this.registerC),this.registerC=128&this.registerC|this.registerC>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){this.FCarry=1==(1&this.registerD),this.registerD=128&this.registerD|this.registerD>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){this.FCarry=1==(1&this.registerE),this.registerE=128&this.registerE|this.registerE>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){this.FCarry=256==(256&this.registersHL),this.registersHL=this.registersHL>>1&65280|33023&this.registersHL,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){this.FCarry=1==(1&this.registersHL),this.registersHL=65408&this.registersHL|(255&this.registersHL)>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL);this.FCarry=1==(1&t),t=128&t|t>>1,this.memoryWriter[this.registersHL](this.registersHL,t),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===t},function(){this.FCarry=1==(1&this.registerA),this.registerA=128&this.registerA|this.registerA>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){this.registerB=(15&this.registerB)<<4|this.registerB>>4,this.FZero=0===this.registerB,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.registerC=(15&this.registerC)<<4|this.registerC>>4,this.FZero=0===this.registerC,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.registerD=(15&this.registerD)<<4|this.registerD>>4,this.FZero=0===this.registerD,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.registerE=(15&this.registerE)<<4|this.registerE>>4,this.FZero=0===this.registerE,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.registersHL=(3840&this.registersHL)<<4|(61440&this.registersHL)>>4|255&this.registersHL,this.FZero=this.registersHL<256,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.registersHL=65280&this.registersHL|(15&this.registersHL)<<4|(240&this.registersHL)>>4,this.FZero=0==(255&this.registersHL),this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){var t=this.memoryReader[this.registersHL](this.registersHL);t=(15&t)<<4|t>>4,this.memoryWriter[this.registersHL](this.registersHL,t),this.FZero=0===t,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.registerA=(15&this.registerA)<<4|this.registerA>>4,this.FZero=0===this.registerA,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.FCarry=1==(1&this.registerB),this.registerB>>=1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){this.FCarry=1==(1&this.registerC),this.registerC>>=1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){this.FCarry=1==(1&this.registerD),this.registerD>>=1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){this.FCarry=1==(1&this.registerE),this.registerE>>=1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){this.FCarry=256==(256&this.registersHL),this.registersHL=this.registersHL>>1&65280|255&this.registersHL,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){this.FCarry=1==(1&this.registersHL),this.registersHL=65280&this.registersHL|(255&this.registersHL)>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL);this.FCarry=1==(1&t),this.memoryWriter[this.registersHL](this.registersHL,t>>1),this.FHalfCarry=this.FSubtract=!1,this.FZero=t<2},function(){this.FCarry=1==(1&this.registerA),this.registerA>>=1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(256&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(512&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1024&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2048&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4096&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8192&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16384&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32768&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.registerA)},function(){this.registerB&=254},function(){this.registerC&=254},function(){this.registerD&=254},function(){this.registerE&=254},function(){this.registersHL&=65279},function(){this.registersHL&=65534},function(){this.memoryWriter[this.registersHL](this.registersHL,254&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=254},function(){this.registerB&=253},function(){this.registerC&=253},function(){this.registerD&=253},function(){this.registerE&=253},function(){this.registersHL&=65023},function(){this.registersHL&=65533},function(){this.memoryWriter[this.registersHL](this.registersHL,253&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=253},function(){this.registerB&=251},function(){this.registerC&=251},function(){this.registerD&=251},function(){this.registerE&=251},function(){this.registersHL&=64511},function(){this.registersHL&=65531},function(){this.memoryWriter[this.registersHL](this.registersHL,251&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=251},function(){this.registerB&=247},function(){this.registerC&=247},function(){this.registerD&=247},function(){this.registerE&=247},function(){this.registersHL&=63487},function(){this.registersHL&=65527},function(){this.memoryWriter[this.registersHL](this.registersHL,247&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=247},function(){this.registerB&=239},function(){this.registerC&=239},function(){this.registerD&=239},function(){this.registerE&=239},function(){this.registersHL&=61439},function(){this.registersHL&=65519},function(){this.memoryWriter[this.registersHL](this.registersHL,239&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=239},function(){this.registerB&=223},function(){this.registerC&=223},function(){this.registerD&=223},function(){this.registerE&=223},function(){this.registersHL&=57343},function(){this.registersHL&=65503},function(){this.memoryWriter[this.registersHL](this.registersHL,223&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=223},function(){this.registerB&=191},function(){this.registerC&=191},function(){this.registerD&=191},function(){this.registerE&=191},function(){this.registersHL&=49151},function(){this.registersHL&=65471},function(){this.memoryWriter[this.registersHL](this.registersHL,191&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=191},function(){this.registerB&=127},function(){this.registerC&=127},function(){this.registerD&=127},function(){this.registerE&=127},function(){this.registersHL&=32767},function(){this.registersHL&=65407},function(){this.memoryWriter[this.registersHL](this.registersHL,127&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=127},function(){this.registerB|=1},function(){this.registerC|=1},function(){this.registerD|=1},function(){this.registerE|=1},function(){this.registersHL|=256},function(){this.registersHL|=1},function(){this.memoryWriter[this.registersHL](this.registersHL,1|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=1},function(){this.registerB|=2},function(){this.registerC|=2},function(){this.registerD|=2},function(){this.registerE|=2},function(){this.registersHL|=512},function(){this.registersHL|=2},function(){this.memoryWriter[this.registersHL](this.registersHL,2|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=2},function(){this.registerB|=4},function(){this.registerC|=4},function(){this.registerD|=4},function(){this.registerE|=4},function(){this.registersHL|=1024},function(){this.registersHL|=4},function(){this.memoryWriter[this.registersHL](this.registersHL,4|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=4},function(){this.registerB|=8},function(){this.registerC|=8},function(){this.registerD|=8},function(){this.registerE|=8},function(){this.registersHL|=2048},function(){this.registersHL|=8},function(){this.memoryWriter[this.registersHL](this.registersHL,8|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=8},function(){this.registerB|=16},function(){this.registerC|=16},function(){this.registerD|=16},function(){this.registerE|=16},function(){this.registersHL|=4096},function(){this.registersHL|=16},function(){this.memoryWriter[this.registersHL](this.registersHL,16|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=16},function(){this.registerB|=32},function(){this.registerC|=32},function(){this.registerD|=32},function(){this.registerE|=32},function(){this.registersHL|=8192},function(){this.registersHL|=32},function(){this.memoryWriter[this.registersHL](this.registersHL,32|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=32},function(){this.registerB|=64},function(){this.registerC|=64},function(){this.registerD|=64},function(){this.registerE|=64},function(){this.registersHL|=16384},function(){this.registersHL|=64},function(){this.memoryWriter[this.registersHL](this.registersHL,64|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=64},function(){this.registerB|=128},function(){this.registerC|=128},function(){this.registerD|=128},function(){this.registerE|=128},function(){this.registersHL|=32768},function(){this.registersHL|=128},function(){this.memoryWriter[this.registersHL](this.registersHL,128|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=128}],J=[8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8],z=[function(){},function(){this.registerC=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.registerB=this.memoryRead(this.programCounter+1&65535),this.programCounter=this.programCounter+2&65535},function(){this.memoryWrite(this.registerB<<8|this.registerC,this.registerA)},function(){var t=1+(this.registerB<<8|this.registerC);this.registerB=t>>8&255,this.registerC=255&t},function(){this.registerB=this.registerB+1&255,this.FZero=0===this.registerB,this.FHalfCarry=0==(15&this.registerB),this.FSubtract=!1},function(){this.registerB=this.registerB-1&255,this.FZero=0===this.registerB,this.FHalfCarry=15==(15&this.registerB),this.FSubtract=!0},function(){this.registerB=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535},function(){this.FCarry=this.registerA>127,this.registerA=this.registerA<<1&255|this.registerA>>7,this.FZero=this.FSubtract=this.FHalfCarry=!1},function(){var t=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+2&65535,this.memoryWrite(t,255&this.stackPointer),this.memoryWrite(t+1&65535,this.stackPointer>>8)},function(){var t=this.registersHL+(this.registerB<<8|this.registerC);this.FHalfCarry=(4095&this.registersHL)>(4095&t),this.FCarry=t>65535,this.registersHL=65535&t,this.FSubtract=!1},function(){this.registerA=this.memoryRead(this.registerB<<8|this.registerC)},function(){var t=(this.registerB<<8|this.registerC)-1&65535;this.registerB=t>>8,this.registerC=255&t},function(){this.registerC=this.registerC+1&255,this.FZero=0===this.registerC,this.FHalfCarry=0==(15&this.registerC),this.FSubtract=!1},function(){this.registerC=this.registerC-1&255,this.FZero=0===this.registerC,this.FHalfCarry=15==(15&this.registerC),this.FSubtract=!0},function(){this.registerC=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535},function(){this.registerA=this.registerA>>1|(1&this.registerA)<<7,this.FCarry=this.registerA>127,this.FZero=this.FSubtract=this.FHalfCarry=!1},function(){this.cartridge.useGBCMode&&1==(1&this.memory[65357])?(this.memory[65357]>127?(console.log("Going into single clock speed mode."),this.doubleSpeedShifter=0,this.memory[65357]&=127):(console.log("Going into double clock speed mode."),this.doubleSpeedShifter=1,this.memory[65357]|=128),this.memory[65357]&=254):this.handleSTOP()},function(){this.registerE=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.registerD=this.memoryRead(this.programCounter+1&65535),this.programCounter=this.programCounter+2&65535},function(){this.memoryWrite(this.registerD<<8|this.registerE,this.registerA)},function(){var t=1+(this.registerD<<8|this.registerE);this.registerD=t>>8&255,this.registerE=255&t},function(){this.registerD=this.registerD+1&255,this.FZero=0===this.registerD,this.FHalfCarry=0==(15&this.registerD),this.FSubtract=!1},function(){this.registerD=this.registerD-1&255,this.FZero=0===this.registerD,this.FHalfCarry=15==(15&this.registerD),this.FSubtract=!0},function(){this.registerD=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535},function(){var t=this.FCarry?1:0;this.FCarry=this.registerA>127,this.registerA=this.registerA<<1&255|t,this.FZero=this.FSubtract=this.FHalfCarry=!1},function(){this.programCounter=this.programCounter+(this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24)+1&65535},function(){var t=this.registersHL+(this.registerD<<8|this.registerE);this.FHalfCarry=(4095&this.registersHL)>(4095&t),this.FCarry=t>65535,this.registersHL=65535&t,this.FSubtract=!1},function(){this.registerA=this.memoryRead(this.registerD<<8|this.registerE)},function(){var t=(this.registerD<<8|this.registerE)-1&65535;this.registerD=t>>8,this.registerE=255&t},function(){this.registerE=this.registerE+1&255,this.FZero=0===this.registerE,this.FHalfCarry=0==(15&this.registerE),this.FSubtract=!1},function(){this.registerE=this.registerE-1&255,this.FZero=0===this.registerE,this.FHalfCarry=15==(15&this.registerE),this.FSubtract=!0},function(){this.registerE=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535},function(){var t=this.FCarry?128:0;this.FCarry=1==(1&this.registerA),this.registerA=this.registerA>>1|t,this.FZero=this.FSubtract=this.FHalfCarry=!1},function(){this.FZero?this.programCounter=this.programCounter+1&65535:(this.programCounter=this.programCounter+(this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24)+1&65535,this.CPUTicks+=4)},function(){this.registersHL=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+2&65535},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerA]),this.registersHL=this.registersHL+1&65535},function(){this.registersHL=this.registersHL+1&65535},function(){var t=1+(this.registersHL>>8)&255;this.FZero=0===t,this.FHalfCarry=0==(15&t),this.FSubtract=!1,this.registersHL=t<<8|255&this.registersHL},function(){var t=(this.registersHL>>8)-1&255;this.FZero=0===t,this.FHalfCarry=15==(15&t),this.FSubtract=!0,this.registersHL=t<<8|255&this.registersHL},function(){this.registersHL=this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<8|255&this.registersHL,this.programCounter=this.programCounter+1&65535},function(){this.FSubtract?this.FCarry&&this.FHalfCarry?(this.registerA=this.registerA+154&255,this.FHalfCarry=!1):this.FCarry?this.registerA=this.registerA+160&255:this.FHalfCarry&&(this.registerA=this.registerA+250&255,this.FHalfCarry=!1):((this.FCarry||this.registerA>153)&&(this.registerA=this.registerA+96&255,this.FCarry=!0),(this.FHalfCarry||(15&this.registerA)>9)&&(this.registerA=this.registerA+6&255,this.FHalfCarry=!1)),this.FZero=0===this.registerA},function(){this.FZero?(this.programCounter=this.programCounter+(this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24)+1&65535,this.CPUTicks+=4):this.programCounter=this.programCounter+1&65535},function(){this.FHalfCarry=(4095&this.registersHL)>2047,this.FCarry=this.registersHL>32767,this.registersHL=this.registersHL<<1&65535,this.FSubtract=!1},function(){this.registerA=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),this.registersHL=this.registersHL+1&65535},function(){this.registersHL=this.registersHL-1&65535},function(){var t=this.registersHL+1&255;this.FZero=0===t,this.FHalfCarry=0==(15&t),this.FSubtract=!1,this.registersHL=65280&this.registersHL|t},function(){var t=this.registersHL-1&255;this.FZero=0===t,this.FHalfCarry=15==(15&t),this.FSubtract=!0,this.registersHL=65280&this.registersHL|t},function(){this.registersHL=65280&this.registersHL|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535},function(){this.registerA^=255,this.FSubtract=this.FHalfCarry=!0},function(){this.FCarry?this.programCounter=this.programCounter+1&65535:(this.programCounter=this.programCounter+(this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24)+1&65535,this.CPUTicks+=4)},function(){this.stackPointer=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+2&65535},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerA]),this.registersHL=this.registersHL-1&65535},function(){this.stackPointer=this.stackPointer+1&65535},function(){var t=this.memoryReader[this.registersHL].apply(this,[this.registersHL])+1&255;this.FZero=0===t,this.FHalfCarry=0==(15&t),this.FSubtract=!1,this.memoryWriter[this.registersHL].apply(this,[this.registersHL,t])},function(){var t=this.memoryReader[this.registersHL].apply(this,[this.registersHL])-1&255;this.FZero=0===t,this.FHalfCarry=15==(15&t),this.FSubtract=!0,this.memoryWriter[this.registersHL].apply(this,[this.registersHL,t])},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.memoryReader[this.programCounter].apply(this,[this.programCounter])]),this.programCounter=this.programCounter+1&65535},function(){this.FCarry=!0,this.FSubtract=this.FHalfCarry=!1},function(){this.FCarry?(this.programCounter=this.programCounter+(this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24)+1&65535,this.CPUTicks+=4):this.programCounter=this.programCounter+1&65535},function(){var t=this.registersHL+this.stackPointer;this.FHalfCarry=(4095&this.registersHL)>(4095&t),this.FCarry=t>65535,this.registersHL=65535&t,this.FSubtract=!1},function(){this.registerA=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),this.registersHL=this.registersHL-1&65535},function(){this.stackPointer=this.stackPointer-1&65535},function(){this.registerA=this.registerA+1&255,this.FZero=0===this.registerA,this.FHalfCarry=0==(15&this.registerA),this.FSubtract=!1},function(){this.registerA=this.registerA-1&255,this.FZero=0===this.registerA,this.FHalfCarry=15==(15&this.registerA),this.FSubtract=!0},function(){this.registerA=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535},function(){this.FCarry=!this.FCarry,this.FSubtract=this.FHalfCarry=!1},function(){},function(){this.registerB=this.registerC},function(){this.registerB=this.registerD},function(){this.registerB=this.registerE},function(){this.registerB=this.registersHL>>8},function(){this.registerB=255&this.registersHL},function(){this.registerB=this.memoryReader[this.registersHL].apply(this,[this.registersHL])},function(){this.registerB=this.registerA},function(){this.registerC=this.registerB},function(){},function(){this.registerC=this.registerD},function(){this.registerC=this.registerE},function(){this.registerC=this.registersHL>>8},function(){this.registerC=255&this.registersHL},function(){this.registerC=this.memoryReader[this.registersHL].apply(this,[this.registersHL])},function(){this.registerC=this.registerA},function(){this.registerD=this.registerB},function(){this.registerD=this.registerC},function(){},function(){this.registerD=this.registerE},function(){this.registerD=this.registersHL>>8},function(){this.registerD=255&this.registersHL},function(){this.registerD=this.memoryReader[this.registersHL].apply(this,[this.registersHL])},function(){this.registerD=this.registerA},function(){this.registerE=this.registerB},function(){this.registerE=this.registerC},function(){this.registerE=this.registerD},function(){},function(){this.registerE=this.registersHL>>8},function(){this.registerE=255&this.registersHL},function(){this.registerE=this.memoryReader[this.registersHL].apply(this,[this.registersHL])},function(){this.registerE=this.registerA},function(){this.registersHL=this.registerB<<8|255&this.registersHL},function(){this.registersHL=this.registerC<<8|255&this.registersHL},function(){this.registersHL=this.registerD<<8|255&this.registersHL},function(){this.registersHL=this.registerE<<8|255&this.registersHL},function(){},function(){this.registersHL=257*(255&this.registersHL)},function(){this.registersHL=this.memoryReader[this.registersHL].apply(this,[this.registersHL])<<8|255&this.registersHL},function(){this.registersHL=this.registerA<<8|255&this.registersHL},function(){this.registersHL=65280&this.registersHL|this.registerB},function(){this.registersHL=65280&this.registersHL|this.registerC},function(){this.registersHL=65280&this.registersHL|this.registerD},function(){this.registersHL=65280&this.registersHL|this.registerE},function(){this.registersHL=65280&this.registersHL|this.registersHL>>8},function(){},function(){this.registersHL=65280&this.registersHL|this.memoryReader[this.registersHL].apply(this,[this.registersHL])},function(){this.registersHL=65280&this.registersHL|this.registerA},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerB])},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerC])},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerD])},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerE])},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registersHL>>8])},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,255&this.registersHL])},function(){(this.interruptsEnabled&this.interruptsRequested&31)>0?this.cartridge.useGBCMode||this.usedBootROM?this.CPUTicks+=4:this.skipPCIncrement=!0:this.calculateHALTPeriod()},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerA])},function(){this.registerA=this.registerB},function(){this.registerA=this.registerC},function(){this.registerA=this.registerD},function(){this.registerA=this.registerE},function(){this.registerA=this.registersHL>>8},function(){this.registerA=255&this.registersHL},function(){this.registerA=this.memoryReader[this.registersHL].apply(this,[this.registersHL])},function(){},function(){var t=this.registerA+this.registerB;this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerC;this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerD;this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerE;this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+(this.registersHL>>8);this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+(255&this.registersHL);this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.memoryReader[this.registersHL].apply(this,[this.registersHL]);this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){this.FHalfCarry=8==(8&this.registerA),this.FCarry=this.registerA>127,this.registerA=this.registerA<<1&255,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerB+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&this.registerB)+(this.FCarry?1:0)>15,this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerC+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&this.registerC)+(this.FCarry?1:0)>15,this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerD+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&this.registerD)+(this.FCarry?1:0)>15,this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerE+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&this.registerE)+(this.FCarry?1:0)>15,this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registersHL>>8,e=this.registerA+t+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&t)+(this.FCarry?1:0)>15,this.FCarry=e>255,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=255&this.registersHL,e=this.registerA+t+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&t)+(this.FCarry?1:0)>15,this.FCarry=e>255,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),e=this.registerA+t+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&t)+(this.FCarry?1:0)>15,this.FCarry=e>255,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA<<1|(this.FCarry?1:0);this.FHalfCarry=(this.registerA<<1&30|(this.FCarry?1:0))>15,this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA-this.registerB;this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.registerC;this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.registerD;this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.registerE;this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-(this.registersHL>>8);this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-(255&this.registersHL);this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.memoryReader[this.registersHL].apply(this,[this.registersHL]);this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){this.registerA=0,this.FHalfCarry=this.FCarry=!1,this.FZero=this.FSubtract=!0},function(){var t=this.registerA-this.registerB-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&this.registerB)-(this.FCarry?1:0)<0,this.FCarry=t<0,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!0},function(){var t=this.registerA-this.registerC-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&this.registerC)-(this.FCarry?1:0)<0,this.FCarry=t<0,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!0},function(){var t=this.registerA-this.registerD-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&this.registerD)-(this.FCarry?1:0)<0,this.FCarry=t<0,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!0},function(){var t=this.registerA-this.registerE-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&this.registerE)-(this.FCarry?1:0)<0,this.FCarry=t<0,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!0},function(){var t=this.registersHL>>8,e=this.registerA-t-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&t)-(this.FCarry?1:0)<0,this.FCarry=e<0,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!0},function(){var t=this.registerA-(255&this.registersHL)-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&this.registersHL)-(this.FCarry?1:0)<0,this.FCarry=t<0,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!0},function(){var t=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),e=this.registerA-t-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&t)-(this.FCarry?1:0)<0,this.FCarry=e<0,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!0},function(){this.FCarry?(this.FZero=!1,this.FSubtract=this.FHalfCarry=this.FCarry=!0,this.registerA=255):(this.FHalfCarry=this.FCarry=!1,this.FSubtract=this.FZero=!0,this.registerA=0)},function(){this.registerA&=this.registerB,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA&=this.registerC,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA&=this.registerD,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA&=this.registerE,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA&=this.registersHL>>8,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA&=this.registersHL,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA&=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA^=this.registerB,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA^=this.registerC,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA^=this.registerD,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA^=this.registerE,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA^=this.registersHL>>8,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA^=255&this.registersHL,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA^=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA=0,this.FZero=!0,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA|=this.registerB,this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.registerA|=this.registerC,this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.registerA|=this.registerD,this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.registerA|=this.registerE,this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.registerA|=this.registersHL>>8,this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.registerA|=255&this.registersHL,this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.registerA|=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){var t=this.registerA-this.registerB;this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.registerC;this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.registerD;this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.registerE;this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-(this.registersHL>>8);this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-(255&this.registersHL);this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.memoryReader[this.registersHL].apply(this,[this.registersHL]);this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){this.FHalfCarry=this.FCarry=!1,this.FZero=this.FSubtract=!0},function(){this.FZero||(this.programCounter=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535,this.CPUTicks+=12)},function(){this.registerC=this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.registerB=this.memoryRead(this.stackPointer+1&65535),this.stackPointer=this.stackPointer+2&65535},function(){this.FZero?this.programCounter=this.programCounter+2&65535:(this.programCounter=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.CPUTicks+=4)},function(){this.programCounter=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter])},function(){if(this.FZero)this.programCounter=this.programCounter+2&65535;else{var t=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+2&65535,this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=t,this.CPUTicks+=12}},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.registerB]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.registerC])},function(){var t=this.registerA+this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535,this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=0},function(){this.FZero&&(this.programCounter=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535,this.CPUTicks+=12)},function(){this.programCounter=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535},function(){this.FZero?(this.programCounter=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.CPUTicks+=4):this.programCounter=this.programCounter+2&65535},function(){const t=this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535,this.CPUTicks+=J[t],U[t].apply(this)},function(){if(this.FZero){var t=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+2&65535,this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=t,this.CPUTicks+=12}else this.programCounter=this.programCounter+2&65535},function(){var t=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+2&65535,this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=t},function(){var t=this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535;var e=this.registerA+t+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&t)+(this.FCarry?1:0)>15,this.FCarry=e>255,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!1},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=8},function(){this.FCarry||(this.programCounter=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535,this.CPUTicks+=12)},function(){this.registerE=this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.registerD=this.memoryRead(this.stackPointer+1&65535),this.stackPointer=this.stackPointer+2&65535},function(){this.FCarry?this.programCounter=this.programCounter+2&65535:(this.programCounter=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.CPUTicks+=4)},function(){console.error("Illegal op code 0xD3 called, pausing emulation.")},function(){if(this.FCarry)this.programCounter=this.programCounter+2&65535;else{var t=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+2&65535,this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=t,this.CPUTicks+=12}},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.registerD]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.registerE])},function(){var t=this.registerA-this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535,this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=16},function(){this.FCarry&&(this.programCounter=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535,this.CPUTicks+=12)},function(){this.programCounter=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535,this.IRQEnableDelay=2===this.IRQEnableDelay||118===this.memoryReader[this.programCounter].apply(this,[this.programCounter])?1:2},function(){this.FCarry?(this.programCounter=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.CPUTicks+=4):this.programCounter=this.programCounter+2&65535},function(){console.error("Illegal op code 0xDB called, pausing emulation.")},function(){if(this.FCarry){var t=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+2&65535,this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=t,this.CPUTicks+=12}else this.programCounter=this.programCounter+2&65535},function(){console.error("Illegal op code 0xDD called, pausing emulation.")},function(){var t=this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535;var e=this.registerA-t-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&t)-(this.FCarry?1:0)<0,this.FCarry=e<0,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!0},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=24},function(){this.memoryHighWrite(this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.registerA),this.programCounter=this.programCounter+1&65535},function(){this.registersHL=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535},function(){this.memoryHighWriter[this.registerC].apply(this,[this.registerC,this.registerA])},function(){console.log("Illegal op code 0xE3 called, pausing emulation.")},function(){console.log("Illegal op code 0xE4 called, pausing emulation.")},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.registersHL>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.registersHL])},function(){this.registerA&=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=32},function(){var t=this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24;this.programCounter=this.programCounter+1&65535;var e=this.stackPointer+t&65535;t=this.stackPointer^t^e,this.stackPointer=e,this.FCarry=256==(256&t),this.FHalfCarry=16==(16&t),this.FZero=this.FSubtract=!1},function(){this.programCounter=this.registersHL},function(){this.memoryWrite(this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.registerA),this.programCounter=this.programCounter+2&65535},function(){console.error("Illegal op code 0xEB called, pausing emulation.")},function(){console.error("Illegal op code 0xEC called, pausing emulation.")},function(){console.error("Illegal op code 0xED called, pausing emulation.")},function(){this.registerA^=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=40},function(){this.registerA=this.memoryHighRead(this.memoryReader[this.programCounter].apply(this,[this.programCounter])),this.programCounter=this.programCounter+1&65535},function(){var t=this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]);this.FZero=t>127,this.FSubtract=64==(64&t),this.FHalfCarry=32==(32&t),this.FCarry=16==(16&t),this.registerA=this.memoryRead(this.stackPointer+1&65535),this.stackPointer=this.stackPointer+2&65535},function(){this.registerA=this.memoryHighReader[this.registerC].apply(this,[this.registerC])},function(){this.IME=!1,this.IRQEnableDelay=0},function(){console.error("Illegal op code 0xF4 called, pausing emulation.")},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.registerA]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,(this.FZero?128:0)|(this.FSubtract?64:0)|(this.FHalfCarry?32:0)|(this.FCarry?16:0)])},function(){this.registerA|=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.FZero=0===this.registerA,this.programCounter=this.programCounter+1&65535,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=48},function(){var t=this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24;this.programCounter=this.programCounter+1&65535,this.registersHL=this.stackPointer+t&65535,t=this.stackPointer^t^this.registersHL,this.FCarry=256==(256&t),this.FHalfCarry=16==(16&t),this.FZero=this.FSubtract=!1},function(){this.stackPointer=this.registersHL},function(){this.registerA=this.memoryRead(this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter])),this.programCounter=this.programCounter+2&65535},function(){this.IRQEnableDelay=2===this.IRQEnableDelay||118===this.memoryReader[this.programCounter].apply(this,[this.programCounter])?1:2},()=>console.error("Illegal op code 0xFC called, pausing emulation."),()=>console.error("Illegal op code 0xFD called, pausing emulation."),function(){var t=this.registerA-this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535,this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=56}],X=[15,0,124,255,0,0,0,248,255,255,255,255,255,255,255,1,128,191,243,255,191,255,63,0,255,191,127,255,159,255,191,255,255,0,0,191,119,243,241,255,255,255,255,255,255,255,255,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255,145,128,0,0,0,0,0,252,0,0,0,0,255,126,255,254,255,255,255,255,255,255,62,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,192,255,193,0,254,255,255,255,248,255,0,0,0,143,0,0,255,255,255,255,255,255,255,255,206,237,102,102,204,13,0,11,3,115,0,131,0,12,0,13,0,8,17,31,136,137,0,14,220,204,110,230,221,221,217,153,187,187,103,99,110,14,236,204,221,220,153,159,187,185,51,62,69,236,82,250,8,183,7,93,1,253,192,255,8,252,0,229,11,248,194,206,244,249,15,127,69,109,61,254,70,151,51,94,8,239,241,255,134,131,36,116,18,252,0,159,180,183,6,213,208,122,0,158,4,95,65,47,29,119,54,117,129,170,112,58,152,209,113,2,77,1,193,255,13,0,211,5,249,0,11,0],Y=[!0,1,!0,!1,!0,!0,0,19,0,216,333,65534,256,!1,!0,!1,0,0,[],[],0,[],!1,1,-53248,0,0,0,!1,!1,!1,!1,!1,0,!1,!1,!0,0,128,!1,56,60,0,1024,0,0,0,0,(new Date).getTime(),0,[],!0,!0,8192,512,0,0,!1,0,0,!0,0,!1,0,1,0,!1,0,!1,8192,512,0,0,!1,0,0,!0,0,!1,0,4,0,!0,null,8,0,0,0,0,!1,0,0,!0,32767,!1,8,8,!1,!1,!1,!1,!1,!1,!1,!1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Z[2],0,Z[2],!1,!1,!1,!1,8192,0,2048,8,0,0,2048,0,144,0,0,!1,0,0,0,0,0,0,0,0,0,!1,!1,!1,!1,0,-61440,0,0,null,null,null,null,null,null,null,null,null,null,null,null,0,0,0,0,!1,0,0,!1,1,1];class Q{constructor(t){this.gameboy=t}init(){this.loadOld(Y.slice(0))}get(){const t=this.gameboy;return t.cartridge?d(t.memory.buffer.slice(0),t.VRAM.buffer.slice(0)):null}load(t){this.gameboy}loadOld(t){let e=0;const i=this.gameboy;i.inBootstrap=t[e++],i.registerA=t[e++],i.FZero=t[e++],i.FSubtract=t[e++],i.FHalfCarry=t[e++],i.FCarry=t[e++],i.registerB=t[e++],i.registerC=t[e++],i.registerD=t[e++],i.registerE=t[e++],i.registersHL=t[e++],i.stackPointer=t[e++],i.programCounter=t[e++],i.halt=t[e++],i.IME=t[e++],i.hdmaRunning=t[e++],i.CPUTicks=t[e++],i.doubleSpeedShifter=t[e++],i.memory=l(t[e++],"uint8"),i.VRAM=l(t[e++],"uint8"),i.currVRAMBank=t[e++],i.GBCMemory=l(t[e++],"uint8"),i.useGBCMode=t[e++],i.gbcRamBank=t[e++],i.gbcRamBankPosition=t[e++],i.ROMBank1Offset=t[e++],i.cartridge?i.cartridge.mbc.currentROMBank=t[e++]:e++,i.modeSTAT=t[e++],i.LYCMatchTriggerSTAT=t[e++],i.mode2TriggerSTAT=t[e++],i.mode1TriggerSTAT=t[e++],i.mode0TriggerSTAT=t[e++],i.LCDisOn=t[e++],i.gfxWindowCHRBankPosition=t[e++],i.gfxWindowDisplay=t[e++],i.gfxSpriteShow=t[e++],i.gfxSpriteNormalHeight=t[e++],i.gfxBackgroundCHRBankPosition=t[e++],i.gfxBackgroundBankOffset=t[e++],i.TIMAEnabled=t[e++],i.DIVTicks=t[e++],i.LCDTicks=t[e++],i.timerTicks=t[e++],i.TACClocker=t[e++],i.serialTimer=t[e++],i.serialShiftTimer=t[e++],i.serialShiftTimerAllocated=t[e++],i.IRQEnableDelay=t[e++],i.cartridge&&i.cartridge.hasRTC?i.cartridge.mbc3.rtc.lastTime=t[e++]:e++,i.drewBlank=t[e++],i.frameBuffer=l(t[e++],"int32"),i.bgEnabled=t[e++],i.BGPriorityEnabled=t[e++],i.audioController.channel1FrequencyTracker=t[e++],i.channel1FrequencyCounter=t[e++],i.channel1totalLength=t[e++],i.channel1envelopeVolume=t[e++],i.channel1envelopeType=t[e++],i.channel1envelopeSweeps=t[e++],i.channel1envelopeSweepsLast=t[e++],i.channel1consecutive=t[e++],i.channel1frequency=t[e++],i.channel1SweepFault=t[e++],i.channel1ShadowFrequency=t[e++],i.channel1timeSweep=t[e++],i.channel1lastTimeSweep=t[e++],i.channel1Swept=t[e++],i.channel1frequencySweepDivider=t[e++],i.channel1decreaseSweep=t[e++],i.channel2FrequencyTracker=t[e++],i.channel2FrequencyCounter=t[e++],i.channel2totalLength=t[e++],i.channel2envelopeVolume=t[e++],i.channel2envelopeType=t[e++],i.channel2envelopeSweeps=t[e++],i.channel2envelopeSweepsLast=t[e++],i.channel2consecutive=t[e++],i.channel2frequency=t[e++],i.channel3canPlay=t[e++],i.channel3totalLength=t[e++],i.channel3patternType=t[e++],i.channel3frequency=t[e++],i.channel3consecutive=t[e++],i.channel3PCM=l(t[e++],"int8"),i.audioController.channel4FrequencyPeriod=t[e++],i.audioController.channel4lastSampleLookup=t[e++],i.channel4totalLength=t[e++],i.channel4envelopeVolume=t[e++],i.channel4currentVolume=t[e++],i.channel4envelopeType=t[e++],i.channel4envelopeSweeps=t[e++],i.channel4envelopeSweepsLast=t[e++],i.channel4consecutive=t[e++],i.channel4BitRange=t[e++],i.soundMasterEnabled=t[e++],i.audioController.VinLeftChannelMasterVolume=t[e++],i.audioController.VinRightChannelMasterVolume=t[e++],i.leftChannel1=t[e++],i.leftChannel2=t[e++],i.leftChannel3=t[e++],i.leftChannel4=t[e++],i.rightChannel1=t[e++],i.rightChannel2=t[e++],i.rightChannel3=t[e++],i.rightChannel4=t[e++],i.channel1currentSampleLeft=t[e++],i.channel1currentSampleRight=t[e++],i.channel2currentSampleLeft=t[e++],i.channel2currentSampleRight=t[e++],i.channel3currentSampleLeft=t[e++],i.channel3currentSampleRight=t[e++],i.channel4currentSampleLeft=t[e++],i.channel4currentSampleRight=t[e++],i.channel1currentSampleLeftSecondary=t[e++],i.channel1currentSampleRightSecondary=t[e++],i.channel2currentSampleLeftSecondary=t[e++],i.channel2currentSampleRightSecondary=t[e++],i.channel3currentSampleLeftSecondary=t[e++],i.channel3currentSampleRightSecondary=t[e++],i.channel4currentSampleLeftSecondary=t[e++],i.channel4currentSampleRightSecondary=t[e++],i.channel1currentSampleLeftTrimary=t[e++],i.channel1currentSampleRightTrimary=t[e++],i.channel2currentSampleLeftTrimary=t[e++],i.channel2currentSampleRightTrimary=t[e++],i.audioController.mixerOutputCache=t[e++],i.audioController.channel1DutyTracker=t[e++],i.audioController.channel1CachedDuty=t[e++],i.audioController.channel2DutyTracker=t[e++],i.audioController.channel2CachedDuty=t[e++],i.audioController.channel1Enabled=t[e++],i.audioController.channel2Enabled=t[e++],i.audioController.channel3Enabled=t[e++],i.audioController.channel4Enabled=t[e++],i.audioController.sequencerClocks=t[e++],i.audioController.sequencePosition=t[e++],i.channel3Counter=t[e++],i.audioController.channel4Counter=t[e++],i.audioController.cachedChannel3Sample=t[e++],i.audioController.cachedChannel4Sample=t[e++],i.channel3FrequencyPeriod=t[e++],i.channel3lastSampleLookup=t[e++],i.actualScanLine=t[e++],i.lastUnrenderedLine=t[e++],i.queuedScanLines=t[e++],i.cartridge&&i.cartridge.hasRTC?(i.cartridge.mbc3.rtc.RTCisLatched=t[e++],i.cartridge.mbc3.rtc.latchedSeconds=t[e++],i.cartridge.mbc3.rtc.latchedMinutes=t[e++],i.cartridge.mbc3.rtc.latchedHours=t[e++],i.cartridge.mbc3.rtc.latchedLDays=t[e++],i.cartridge.mbc3.rtc.latchedHDays=t[e++],i.cartridge.mbc3.rtc.RTCSeconds=t[e++],i.cartridge.mbc3.rtc.RTCMinutes=t[e++],i.cartridge.mbc3.rtc.RTCHours=t[e++],i.cartridge.mbc3.rtc.RTCDays=t[e++],i.cartridge.mbc3.rtc.RTCDayOverFlow=t[e++],i.cartridge.mbc3.rtc.RTCHalt=t[e++]):e+=12,i.usedBootROM=t[e++],i.skipPCIncrement=t[e++],i.STATTracker=t[e++],i.gbcRamBankPositionECHO=t[e++],i.windowY=t[e++],i.windowX=t[e++],i.gbcOBJRawPalette=l(t[e++],"uint8"),i.gbcBGRawPalette=l(t[e++],"uint8"),i.gbOBJPalette=l(t[e++],"int32"),i.gbBGPalette=l(t[e++],"int32"),i.gbcOBJPalette=l(t[e++],"int32"),i.gbcBGPalette=l(t[e++],"int32"),i.gbBGColorizedPalette=l(t[e++],"int32"),i.gbOBJColorizedPalette=l(t[e++],"int32"),i.cachedBGPaletteConversion=l(t[e++],"int32"),i.cachedOBJPaletteConversion=l(t[e++],"int32"),i.BGCHRBank1=l(t[e++],"uint8"),i.BGCHRBank2=l(t[e++],"uint8"),i.haltPostClocks=t[e++],i.interruptsRequested=t[e++],i.interruptsEnabled=t[e++],i.checkIRQMatching(),i.remainingClocks=t[e++],i.colorizedGBPalettes=t[e++],i.backgroundY=t[e++],i.backgroundX=t[e++],i.CPUStopped=t[e++],i.audioController.audioClocksUntilNextEvent=t[e++],i.audioController.audioClocksUntilNextEventCounter=t[e]}}const j=0,_=16383,$=16384,K=32767,tt=32768,et=36863,it=38911,rt=40960,st=49151,ht=49152,at=53247,nt=53248,ot=57343,lt=65280,ct=65281,mt=65282,ut=65284,gt=65535;class dt{constructor(t){this.initialValue=15,this.value=255,this.gameboy=t}down(t){this.value&=255^1<>4:15)&(0==(16&t)?15&this.value:15)),this.gameboy.CPUStopped=!1}}class yt{constructor(t,e){this.readers=[],this.writers=[],this.data=m(65536,0,"uint8"),this.gameboy=t,this.data}write(t,e){return this.writers[t](t,e)}read(t){return this.readers[t](t)}jumpCompile(){this.gameboy.memoryReadJumpCompile(),this.gameboy.memoryWriteJumpCompile(),this.setReaders(j,_,t=>this.data[t]),this.setReaders($,K,t=>this.gameboy.cartridge.rom.getByte(this.gameboy.cartridge.mbc.currentROMBank+t)),this.setReaders(tt,it,this.gameboy.cartridge.useGBCMode?this.gameboy.VRAMDATAReadCGBCPU:this.gameboy.VRAMDATAReadDMGCPU),this.setReaders(rt,st,this.gameboy.cartridge.useGBCMode?this.gameboy.VRAMCHRReadCGBCPU:this.gameboy.VRAMCHRReadDMGCPU),this.gameboy.cartridge.mbc&&0===this.gameboy.cartridge.mbc.ramSize?this.setReaders(rt,st,this.gameboy.badMemoryRead):this.gameboy.cartridge.hasMBC7?this.setReaders(rt,st,this.gameboy.memoryReadMBC7):this.gameboy.cartridge.hasMBC3?this.setReaders(rt,st,this.gameboy.memoryReadMBC3):this.setReaders(rt,st,this.gameboy.memoryReadMBC),this.setReaders(ht,at,this.gameboy.memoryReadNormal),this.gameboy.cartridge.useGBCMode?this.setReaders(nt,ot,this.gameboy.memoryReadGBCMemory):this.setReaders(nt,ot,this.gameboy.memoryReadNormal)}setReaders(t,e,i){for(let r=t;r{"undefined"!=typeof window&&"vibrate"in window.navigator&&window.navigator.vibrate(200)})}loadCartridgeRomIntoMemory(){for(let t=0;t<16384;t++)this.memory[t]=this.cartridge.rom.getByte(t)}loadBootROMIntoMemory(){for(let t=0;t<256;t++)this.memory[t]=this.bootROM.getByte(t);if(this.bootROM.length>=256)for(let t=512;t<2304;t++)this.memory[t]=this.bootROM.getByte(t-256)}start(t){this.init(),this.connectCartridge(t),this.cartridge&&this.cartridge.mbc&&(this.cartridge.mbc.setupROM(),this.cartridge.mbc.on("ramWrite",()=>{this.events.emit("sramWrite")})),this.usedBootROM?(this.setupRAM(),this.initBootstrap()):(this.inBootstrap=!1,this.setupRAM(),this.initSkipBootstrap()),this.checkIRQMatching()}init(){this.stateManager.init(),this.initMemory(),this.lcdDevice.init(),this.initSound()}setupRAM(){this.cartridge.setupRAM(),this.cartridge.useGBCMode&&(this.VRAM=m(8192,0,"uint8"),this.GBCMemory=m(28672,0,"uint8")),this.jumpCompile(),this.initializeModeSpecificArrays()}initMemory(){this.memory=m(65536,0,"uint8"),this.audioController.setMemory(this.memory),this.frameBuffer=m(23040,16316664,"int32"),this.BGCHRBank1=m(2048,0,"uint8"),this.audioController.initMemory(),this.memoryNew=new yt(this,this.memory)}generateCacheArray(t){const e=[];let i=0;for(;i=0;){if(t>=48&&t<64)this.memoryWrite(65280|t,X[t]);else switch(t){case 0:case 1:case 2:case 5:case 7:case 15:case 255:this.memoryWrite(65280|t,X[t]);break;default:this.memory[65280|t]=X[t]}--t}this.cartridge.useGBCMode?(this.memory[65388]=254,this.memory[65396]=254):(this.memory[65352]=255,this.memory[65353]=255,this.memory[65388]=255,this.memory[65396]=255),console.log("Starting without the GBC boot ROM."),this.registerA=this.cartridge.useGBCMode?17:1,this.registerB=0,this.registerC=19,this.registerD=0,this.registerE=216,this.FZero=!0,this.FSubtract=!1,this.FHalfCarry=!0,this.FCarry=!0,this.registersHL=333,this.LCDCONTROL=this.LINECONTROL,this.IME=!1,this.IRQLineMatched=0,this.interruptsRequested=225,this.interruptsEnabled=0,this.hdmaRunning=!1,this.CPUTicks=12,this.STATTracker=0,this.modeSTAT=1,this.spriteCount=252,this.LYCMatchTriggerSTAT=!1,this.mode2TriggerSTAT=!1,this.mode1TriggerSTAT=!1,this.mode0TriggerSTAT=!1,this.LCDisOn=!0,this.audioController.channel1FrequencyTracker=8192,this.audioController.channel1DutyTracker=0,this.audioController.channel1CachedDuty=Z[2],this.audioController.channel1totalLength=0,this.audioController.channel1envelopeVolume=0,this.audioController.channel1envelopeType=!1,this.audioController.channel1envelopeSweeps=0,this.audioController.channel1envelopeSweepsLast=0,this.audioController.channel1consecutive=!0,this.audioController.channel1frequency=1985,this.audioController.channel1SweepFault=!0,this.audioController.channel1ShadowFrequency=1985,this.audioController.channel1timeSweep=1,this.audioController.channel1lastTimeSweep=0,this.audioController.channel1Swept=!1,this.audioController.channel1frequencySweepDivider=0,this.audioController.channel1decreaseSweep=!1,this.audioController.channel2FrequencyTracker=8192,this.audioController.channel2DutyTracker=0,this.audioController.channel2CachedDuty=Z[2],this.audioController.channel2totalLength=0,this.audioController.channel2envelopeVolume=0,this.audioController.channel2envelopeType=!1,this.audioController.channel2envelopeSweeps=0,this.audioController.channel2envelopeSweepsLast=0,this.audioController.channel2consecutive=!0,this.audioController.channel2frequency=0,this.audioController.channel3canPlay=!1,this.audioController.channel3totalLength=0,this.audioController.channel3patternType=4,this.audioController.channel3frequency=0,this.audioController.channel3consecutive=!0,this.audioController.channel3Counter=1048,this.audioController.channel4FrequencyPeriod=8,this.audioController.channel4totalLength=0,this.audioController.channel4envelopeVolume=0,this.audioController.channel4currentVolume=0,this.audioController.channel4envelopeType=!1,this.audioController.channel4envelopeSweeps=0,this.audioController.channel4envelopeSweepsLast=0,this.audioController.channel4consecutive=!0,this.audioController.channel4BitRange=32767,this.audioController.channel4VolumeShifter=15,this.audioController.channel1FrequencyCounter=512,this.audioController.channel2FrequencyCounter=512,this.audioController.channel3Counter=2048,this.audioController.channel3FrequencyPeriod=2048,this.audioController.channel3lastSampleLookup=0,this.audioController.channel4lastSampleLookup=0,this.audioController.VinLeftChannelMasterVolume=8,this.audioController.VinRightChannelMasterVolume=8,this.audioController.leftChannel1=!0,this.audioController.leftChannel2=!0,this.audioController.leftChannel3=!0,this.audioController.leftChannel4=!0,this.audioController.rightChannel1=!0,this.audioController.rightChannel2=!0,this.audioController.rightChannel3=!1,this.audioController.rightChannel4=!1,this.soundMasterEnabled=!0,this.DIVTicks=27044,this.LCDTicks=160,this.timerTicks=0,this.TIMAEnabled=!1,this.TACClocker=1024,this.serialTimer=0,this.serialShiftTimer=0,this.serialShiftTimerAllocated=0,this.IRQEnableDelay=0,this.actualScanLine=144,this.lastUnrenderedLine=0,this.gfxWindowDisplay=!1,this.gfxSpriteShow=!1,this.gfxSpriteNormalHeight=!0,this.bgEnabled=!0,this.hasBGPriority=!0,this.gfxWindowCHRBankPosition=0,this.gfxBackgroundCHRBankPosition=0,this.gfxBackgroundBankOffset=0,this.windowY=0,this.windowX=0,this.drewBlank=0,this.midScanlineOffset=-1,this.currentX=0}initBootstrap(){console.log("Starting selected boot ROM"),this.programCounter=0,this.stackPointer=0,this.IME=!1,this.LCDTicks=0,this.DIVTicks=0,this.registerA=0,this.registerB=0,this.registerC=0,this.registerD=0,this.registerE=0,this.FZero=this.FSubtract=this.FHalfCarry=this.FCarry=!1,this.registersHL=0,this.audioController.leftChannel1=!1,this.audioController.leftChannel2=!1,this.audioController.leftChannel3=!1,this.audioController.leftChannel4=!1,this.audioController.rightChannel1=!1,this.audioController.rightChannel2=!1,this.audioController.rightChannel3=!1,this.audioController.rightChannel4=!1,this.audioController.channel2frequency=this.audioController.channel1frequency=0,this.audioController.channel4consecutive=this.audioController.channel2consecutive=this.audioController.channel1consecutive=!1,this.audioController.VinLeftChannelMasterVolume=8,this.audioController.VinRightChannelMasterVolume=8,this.memory[lt]=this.joypad.initialValue}disableBootROM(){this.loadCartridgeRomIntoMemory(),this.usedGBCBootROM?this.cartridge.useGBCMode?this.recompileBootIOWriteHandling():this.adjustGBCtoGBMode():this.recompileBootIOWriteHandling()}setSpeed(t){this.cpu.setSpeed(t),this.initSound()}initSound(){this.audioController.connectDevice(this.audioDevice),this.audioController.setVolume(s.soundOn?s.soundVolume:0),this.audioController.initBuffer()}writeChannel3RAM(t,e){this.audioController.channel3canPlay&&this.audioController.runJIT(),this.memory[65328|t]=e,t<<=1,this.audioController.channel3PCM[t]=e>>4,this.audioController.channel3PCM[1|t]=15&e}run(){0==(2&this.stopEmulator)&&(1==(1&this.stopEmulator)?this.CPUStopped?(this.audioController.adjustUnderrun(),this.audioController.audioTicks+=this.cpu.cyclesTotal,this.audioController.runJIT(),this.stopEmulator|=1):(this.stopEmulator=0,this.audioController.adjustUnderrun(),this.cartridge.hasRTC&&this.cartridge.mbc.rtc.updateClock(),this.halt?(this.CPUTicks=0,this.calculateHALTPeriod(),this.halt?(this.updateCore(),this.iterationEndRoutine()):this.executeIteration()):this.executeIteration(),this.lcdDevice.requestDraw()):console.error("Iterator restarted a faulted core."))}executeIteration(){for(;0===this.stopEmulator;){switch(this.IRQEnableDelay){case 1:this.IME=!0,this.checkIRQMatching(),--this.IRQEnableDelay;break;case 2:--this.IRQEnableDelay}this.IRQLineMatched>0&&this.launchIRQ();const t=this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535,this.skipPCIncrement&&(this.programCounter=this.programCounter-1&65535,this.skipPCIncrement=!1),this.CPUTicks=O[t],z[t].apply(this);const e=this.CPUTicks>>this.doubleSpeedShifter;if(this.LCDTicks+=e,this.LCDCONTROL[this.actualScanLine](this),this.audioController.audioTicks+=e,this.cpu.ticks+=e,this.DIVTicks+=this.CPUTicks,this.TIMAEnabled)for(this.timerTicks+=this.CPUTicks;this.timerTicks>=this.TACClocker;)this.timerTicks-=this.TACClocker,256==++this.memory[65285]&&(this.memory[65285]=this.memory[65286],this.interruptsRequested|=4,this.checkIRQMatching());this.serialTimer>0&&(this.serialTimer-=this.CPUTicks,this.serialTimer<=0&&(this.interruptsRequested|=8,this.checkIRQMatching()),this.serialShiftTimer-=this.CPUTicks,this.serialShiftTimer<=0&&(this.serialShiftTimer=this.serialShiftTimerAllocated,this.memory[ct]=this.memory[ct]<<1&254|1)),this.cpu.ticks>=this.cpu.cyclesTotal&&this.iterationEndRoutine()}}iterationEndRoutine(){0==(1&this.stopEmulator)&&(this.audioController.runJIT(),this.memory[ut]=this.memory[ut]+(this.DIVTicks>>8)&255,this.DIVTicks&=255,this.stopEmulator|=1,this.cpu.ticks-=this.cpu.cyclesTotal,this.cpu.cyclesTotalCurrent+=this.cpu.cyclesTotalRoundoff,this.recalculateIterationClockLimit())}handleSTOP(){this.CPUStopped=!0,this.iterationEndRoutine(),this.cpu.ticks<0&&(this.audioController.audioTicks-=this.cpu.ticks,this.audioController.runJIT())}recalculateIterationClockLimit(){const t=this.cpu.cyclesTotalCurrent%4;this.cpu.cyclesTotal=this.cpu.cyclesTotalBase+this.cpu.cyclesTotalCurrent-t,this.cpu.cyclesTotalCurrent=t}scanLineMode2(){1!==this.STATTracker&&(this.mode2TriggerSTAT&&(this.interruptsRequested|=2,this.checkIRQMatching()),this.STATTracker=1,this.modeSTAT=2)}scanLineMode3(){3!==this.modeSTAT&&(0===this.STATTracker&&this.mode2TriggerSTAT&&(this.interruptsRequested|=2,this.checkIRQMatching()),this.STATTracker=1,this.modeSTAT=3)}scanLineMode0(){0!==this.modeSTAT&&(2!==this.STATTracker&&(0===this.STATTracker&&(this.mode2TriggerSTAT&&(this.interruptsRequested|=2,this.checkIRQMatching()),this.modeSTAT=3),this.incrementScanLineQueue(),this.updateSpriteCount(this.actualScanLine),this.STATTracker=2),this.LCDTicks>=this.spriteCount&&(this.hdmaRunning&&this.executeHDMA(),this.mode0TriggerSTAT&&(this.interruptsRequested|=2,this.checkIRQMatching()),this.STATTracker=3,this.modeSTAT=0))}clocksUntilLYCMatch(){return 0!==this.memory[65349]?this.memory[65349]>this.actualScanLine?456*(this.memory[65349]-this.actualScanLine):456*(154-this.actualScanLine+this.memory[65349]):456*(153===this.actualScanLine&&0===this.memory[65348]?154:153-this.actualScanLine)+8}clocksUntilMode0(){switch(this.modeSTAT){case 0:return 143===this.actualScanLine?(this.updateSpriteCount(0),this.spriteCount+5016):(this.updateSpriteCount(this.actualScanLine+1),this.spriteCount+456);case 2:case 3:return this.updateSpriteCount(this.actualScanLine),this.spriteCount;case 1:return this.updateSpriteCount(0),this.spriteCount+456*(154-this.actualScanLine)}}updateSpriteCount(t){if(this.spriteCount=252,this.cartridge.useGBCMode&&this.gfxSpriteShow)for(var e=t+16,i=0,r=this.gfxSpriteNormalHeight?8:16,s=65024;s<65184&&this.spriteCount<312;s+=4)(i=e-this.memory[s])>-1&&i>this.doubleSpeedShifter,this.LCDCONTROL[this.actualScanLine](this);var t=this.CPUTicks>>this.doubleSpeedShifter;if(this.audioController.audioTicks+=t,this.cpu.ticks+=t,this.DIVTicks+=this.CPUTicks,this.TIMAEnabled)for(this.timerTicks+=this.CPUTicks;this.timerTicks>=this.TACClocker;)this.timerTicks-=this.TACClocker,256==++this.memory[65285]&&(this.memory[65285]=this.memory[65286],this.interruptsRequested|=4,this.checkIRQMatching());this.serialTimer>0&&(this.serialTimer-=this.CPUTicks,this.serialTimer<=0&&(this.interruptsRequested|=8,this.checkIRQMatching()),this.serialShiftTimer-=this.CPUTicks,this.serialShiftTimer<=0&&(this.serialShiftTimer=this.serialShiftTimerAllocated,this.memory[ct]=this.memory[ct]<<1&254|1))}updateCoreFull(){this.updateCore(),this.cpu.ticks>=this.cpu.cyclesTotal&&this.iterationEndRoutine()}initializeLCDController(){for(var t=0;t<154;)t<143?this.LINECONTROL[t]=(()=>{this.LCDTicks<80?this.scanLineMode2():this.LCDTicks<252?this.scanLineMode3():this.LCDTicks<456?this.scanLineMode0():(this.LCDTicks-=456,3!=this.STATTracker&&(2!=this.STATTracker&&(0===this.STATTracker&&this.mode2TriggerSTAT&&(this.interruptsRequested|=2),this.incrementScanLineQueue()),this.hdmaRunning&&this.executeHDMA(),this.mode0TriggerSTAT&&(this.interruptsRequested|=2)),this.actualScanLine=++this.memory[65348],this.actualScanLine===this.memory[65349]?(this.memory[65345]|=4,this.LYCMatchTriggerSTAT&&(this.interruptsRequested|=2)):this.memory[65345]&=123,this.checkIRQMatching(),this.STATTracker=0,this.modeSTAT=2,this.LINECONTROL[this.actualScanLine].apply(this))}):143===t?this.LINECONTROL[143]=(()=>{this.LCDTicks<80?this.scanLineMode2():this.LCDTicks<252?this.scanLineMode3():this.LCDTicks<456?this.scanLineMode0():(this.LCDTicks-=456,3!=this.STATTracker&&(2!=this.STATTracker&&(0===this.STATTracker&&this.mode2TriggerSTAT&&(this.interruptsRequested|=2),this.incrementScanLineQueue()),this.hdmaRunning&&this.executeHDMA(),this.mode0TriggerSTAT&&(this.interruptsRequested|=2)),this.actualScanLine=this.memory[65348]=144,144===this.memory[65349]?(this.memory[65345]|=4,this.LYCMatchTriggerSTAT&&(this.interruptsRequested|=2)):this.memory[65345]&=123,this.STATTracker=0,this.modeSTAT=1,this.interruptsRequested|=this.mode1TriggerSTAT?3:1,this.checkIRQMatching(),0===this.drewBlank?(this.cpu.totalLinesPassed<144||144===this.cpu.totalLinesPassed&&this.midScanlineOffset>-1)&&(this.graphicsJITVBlank(),this.lcdDevice.prepareFrame()):--this.drewBlank,this.LINECONTROL[144].apply(this))}):t<153?this.LINECONTROL[t]=(()=>{this.LCDTicks>=456&&(this.LCDTicks-=456,this.actualScanLine=++this.memory[65348],this.actualScanLine===this.memory[65349]?(this.memory[65345]|=4,this.LYCMatchTriggerSTAT&&(this.interruptsRequested|=2,this.checkIRQMatching())):this.memory[65345]&=123,this.LINECONTROL[this.actualScanLine].apply(this))}):this.LINECONTROL[153]=(()=>{this.LCDTicks>=8&&(4!=this.STATTracker&&153===this.memory[65348]&&(this.memory[65348]=0,0===this.memory[65349]?(this.memory[65345]|=4,this.LYCMatchTriggerSTAT&&(this.interruptsRequested|=2,this.checkIRQMatching())):this.memory[65345]&=123,this.STATTracker=4),this.LCDTicks>=456&&(this.LCDTicks-=456,this.STATTracker=this.actualScanLine=0,this.LINECONTROL[0].apply(this)))}),++t}executeHDMA(){this.DMAWrite(1),this.halt?this.LCDTicks-this.spriteCount<(4>>this.doubleSpeedShifter|32)&&(this.CPUTicks=4+(32+this.spriteCount<>this.doubleSpeedShifter|32)):this.LCDTicks+=4>>this.doubleSpeedShifter|32,0===this.memory[65365]?(this.hdmaRunning=!1,this.memory[65365]=255):--this.memory[65365]}updateClock(){this.cartridge.mbc&&this.cartridge.mbc.rtc&&this.cartridge.mbc.rtc.updateClock()}renderScanLine(t){if(this.pixelStart=160*t,this.bgEnabled)this.pixelEnd=160,this.renderBGLayer(t),this.renderWindowLayer(t);else{const e=160*(t+1),i=this.cartridge.useGBCMode||this.colorizedGBPalettes?16316664:15728606;for(let r=160*t+this.currentX;r=82)){if(this.pixelEnd=this.LCDTicks-74,this.pixelEnd=Math.min(this.pixelEnd-this.midScanlineOffset-this.pixelEnd%8,160),this.bgEnabled)this.pixelStart=160*this.lastUnrenderedLine,this.renderBGLayer(this.lastUnrenderedLine),this.renderWindowLayer(this.lastUnrenderedLine);else for(var t=160*this.lastUnrenderedLine+this.pixelEnd,e=this.cartridge.useGBCMode||this.colorizedGBPalettes?16316664:15728606,i=160*this.lastUnrenderedLine+this.currentX;i0?this.BGCHRBank2:this.BGCHRBank1,this.tileCache=this.generateCacheArray(3968)):(this.gbOBJPalette=m(8,0,"int32"),this.gbBGPalette=m(4,0,"int32"),this.BGPalette=this.gbBGPalette,this.OBJPalette=this.gbOBJPalette,this.tileCache=this.generateCacheArray(1792),this.sortBuffer=m(256,0,"uint8"),this.OAMAddressCache=m(10,0,"int32")),this.renderPathBuild()}adjustGBCtoGBMode(){console.log("Stepping down from GBC mode."),this.VRAM=this.GBCMemory=this.BGCHRCurrentBank=this.BGCHRBank2=null,this.tileCache.length=1792,s.colorizeGBMode?(this.gbBGColorizedPalette=m(4,0,"int32"),this.gbOBJColorizedPalette=m(8,0,"int32"),this.cachedBGPaletteConversion=m(4,0,"int32"),this.cachedOBJPaletteConversion=m(8,0,"int32"),this.BGPalette=this.gbBGColorizedPalette,this.OBJPalette=this.gbOBJColorizedPalette,this.gbOBJPalette=this.gbBGPalette=null,this.getGBCColor()):(this.gbOBJPalette=m(8,0,"int32"),this.gbBGPalette=m(4,0,"int32"),this.BGPalette=this.gbBGPalette,this.OBJPalette=this.gbOBJPalette),this.sortBuffer=m(256,0,"uint8"),this.OAMAddressCache=m(10,0,"int32"),this.renderPathBuild(),this.jumpCompile()}renderPathBuild(){this.cartridge.useGBCMode?(this.priorityFlaggingPathRebuild(),this.renderSpriteLayer=this.renderSpriteGBCLayer):(this.renderBGLayer=this.renderBGGBLayer,this.renderWindowLayer=this.renderWindowGBLayer,this.renderSpriteLayer=this.renderSpriteGBLayer)}priorityFlaggingPathRebuild(){this.hasBGPriority?(this.renderBGLayer=this.BGGBCLayerRender,this.renderWindowLayer=this.WindowGBCLayerRender):(this.renderBGLayer=this.BGGBCLayerRenderNoPriorityFlagging,this.renderWindowLayer=this.WindowGBCLayerRenderNoPriorityFlagging)}initializeReferencesFromSaveState(){if(this.LCDCONTROL=this.LCDisOn?this.LINECONTROL:this.DISPLAYOFFCONTROL,this.cartridge.useGBCMode){this.BGCHRCurrentBank=this.currVRAMBank>0?this.BGCHRBank2:this.BGCHRBank1,this.tileCache=this.generateCacheArray(3968);for(let t=0;t<6144;t+=16)this.generateGBCTileBank1(t),this.generateGBCTileBank2(t)}else{this.colorizedGBPalettes?(this.BGPalette=this.gbBGColorizedPalette,this.OBJPalette=this.gbOBJColorizedPalette,this.updateGBBGPalette=this.updateGBColorizedBGPalette,this.updateGBOBJPalette=this.updateGBColorizedOBJPalette):(this.BGPalette=this.gbBGPalette,this.OBJPalette=this.gbOBJPalette),this.tileCache=this.generateCacheArray(1792);for(let t=32768;t<36864;t+=2)this.generateGBOAMTileLine(t);for(let t=36864;t<38912;t+=2)this.generateGBTileLine(t);this.sortBuffer=m(256,0,"uint8"),this.OAMAddressCache=m(10,0,"int32")}this.renderPathBuild()}adjustRGBTint(t){const e=31&t,i=t>>5&31,r=t>>10&31;return 13*e+2*i+r>>1<<16|3*i+r<<9|3*e+2*i+11*r>>1}getGBCColor(){for(let t=0;t<4;t++){const e=t<<1;this.cachedBGPaletteConversion[t]=this.adjustRGBTint(this.gbcBGRawPalette[1|e]<<8|this.gbcBGRawPalette[e]),this.cachedOBJPaletteConversion[t]=this.adjustRGBTint(this.gbcOBJRawPalette[1|e]<<8|this.gbcOBJRawPalette[e])}for(let t=4;t<8;t++){const e=t<<1;this.cachedOBJPaletteConversion[t]=this.adjustRGBTint(this.gbcOBJRawPalette[1|e]<<8|this.gbcOBJRawPalette[e])}this.updateGBBGPalette=this.updateGBColorizedBGPalette,this.updateGBOBJPalette=this.updateGBColorizedOBJPalette,this.updateGBBGPalette(this.memory[65351]),this.updateGBOBJPalette(0,this.memory[65352]),this.updateGBOBJPalette(1,this.memory[65353]),this.colorizedGBPalettes=!0}updateGBRegularBGPalette(t){this.gbBGPalette[0]=33554432|this.colors[3&t],this.gbBGPalette[1]=this.colors[t>>2&3],this.gbBGPalette[2]=this.colors[t>>4&3],this.gbBGPalette[3]=this.colors[t>>6]}updateGBColorizedBGPalette(t){this.gbBGColorizedPalette[0]=33554432|this.cachedBGPaletteConversion[3&t],this.gbBGColorizedPalette[1]=this.cachedBGPaletteConversion[t>>2&3],this.gbBGColorizedPalette[2]=this.cachedBGPaletteConversion[t>>4&3],this.gbBGColorizedPalette[3]=this.cachedBGPaletteConversion[t>>6]}updateGBRegularOBJPalette(t,e){this.gbOBJPalette[1|t]=this.colors[e>>2&3],this.gbOBJPalette[2|t]=this.colors[e>>4&3],this.gbOBJPalette[3|t]=this.colors[e>>6]}updateGBColorizedOBJPalette(t,e){this.gbOBJColorizedPalette[1|t]=this.cachedOBJPaletteConversion[t|e>>2&3],this.gbOBJColorizedPalette[2|t]=this.cachedOBJPaletteConversion[t|e>>4&3],this.gbOBJColorizedPalette[3|t]=this.cachedOBJPaletteConversion[t|e>>6]}updateGBCBGPalette(t,e){this.gbcBGRawPalette[t]!=e&&(this.midScanLineJIT(),this.gbcBGRawPalette[t]=e,0==(6&t)?(e=33554432|this.adjustRGBTint(this.gbcBGRawPalette[1|t]<<8|this.gbcBGRawPalette[62&t]),t>>=1,this.gbcBGPalette[t]=e,this.gbcBGPalette[32|t]=16777216|e):(e=this.adjustRGBTint(this.gbcBGRawPalette[1|t]<<8|this.gbcBGRawPalette[62&t]),t>>=1,this.gbcBGPalette[t]=e,this.gbcBGPalette[32|t]=16777216|e))}updateGBCOBJPalette(t,e){this.gbcOBJRawPalette[t]!==e&&(this.gbcOBJRawPalette[t]=e,(6&t)>0&&(this.midScanLineJIT(),this.gbcOBJPalette[t>>1]=16777216|this.adjustRGBTint(this.gbcOBJRawPalette[1|t]<<8|this.gbcOBJRawPalette[62&t])))}renderBGGBLayer(t){var e=this.backgroundY+t&255,i=(7&e)<<3,r=this.gfxBackgroundCHRBankPosition|(248&e)<<2,s=this.backgroundX+this.currentX&255,h=this.pixelStart+this.currentX,a=this.pixelStart+(this.gfxWindowDisplay&&t-this.windowY>=0?Math.min(Math.max(this.windowX,0)+this.currentX,this.pixelEnd):this.pixelEnd),n=r+(s>>3),o=this.BGCHRBank1[n];o>3;for(s+=m<<3,m+=n;n>3)+r;r=0?Math.min(Math.max(this.windowX,0)+this.currentX,this.pixelEnd):this.pixelEnd),n=r+(s>>3),o=this.BGCHRBank1[n];o>2,u=7&s;u<8&&h>3;for(s+=g<<3,g+=n;n>2,u=i,this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u]];if(h>2,u=i-1;h>3)+r;r>2,u=i,this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|c[u]];if(h>2,a-h){case 7:this.frameBuffer[h+6]=this.gbcBGPalette[m|c[6|i]];case 6:this.frameBuffer[h+5]=this.gbcBGPalette[m|c[5|i]];case 5:this.frameBuffer[h+4]=this.gbcBGPalette[m|c[4|i]];case 4:this.frameBuffer[h+3]=this.gbcBGPalette[m|c[3|i]];case 3:this.frameBuffer[h+2]=this.gbcBGPalette[m|c[2|i]];case 2:this.frameBuffer[h+1]=this.gbcBGPalette[m|c[1|i]];case 1:this.frameBuffer[h]=this.gbcBGPalette[m|c[i]]}}}BGGBCLayerRenderNoPriorityFlagging(t){var e=this.backgroundY+t&255,i=(7&e)<<3,r=this.gfxBackgroundCHRBankPosition|(248&e)<<2,s=this.backgroundX+this.currentX&255,h=this.pixelStart+this.currentX,a=this.pixelStart+(this.gfxWindowDisplay&&t-this.windowY>=0?Math.min(Math.max(this.windowX,0)+this.currentX,this.pixelEnd):this.pixelEnd),n=r+(s>>3),o=this.BGCHRBank1[n];o>3;for(s+=g<<3,g+=n;n>3)+r;r=0){var i=this.windowX>0?this.windowX+this.currentX:this.currentX,r=this.pixelStart+i,s=this.pixelStart+this.pixelEnd;if(r>3),n=this.BGCHRBank1[a];n>3);a=0){var i=this.windowX>0?this.windowX+this.currentX:this.currentX,r=this.pixelStart+i,s=this.pixelStart+this.pixelEnd;if(r>3),n=this.BGCHRBank1[a];n>2,m=i-this.windowX&7;for(i=Math.min(8,m+s-r);m>3);a>2,m=h,this.frameBuffer[r++]=this.gbcBGPalette[c|l[m++]],this.frameBuffer[r++]=this.gbcBGPalette[c|l[m++]],this.frameBuffer[r++]=this.gbcBGPalette[c|l[m++]],this.frameBuffer[r++]=this.gbcBGPalette[c|l[m++]],this.frameBuffer[r++]=this.gbcBGPalette[c|l[m++]],this.frameBuffer[r++]=this.gbcBGPalette[c|l[m++]],this.frameBuffer[r++]=this.gbcBGPalette[c|l[m++]],this.frameBuffer[r++]=this.gbcBGPalette[c|l[m]];if(r>2,s-r){case 7:this.frameBuffer[r+6]=this.gbcBGPalette[c|l[6|h]];case 6:this.frameBuffer[r+5]=this.gbcBGPalette[c|l[5|h]];case 5:this.frameBuffer[r+4]=this.gbcBGPalette[c|l[4|h]];case 4:this.frameBuffer[r+3]=this.gbcBGPalette[c|l[3|h]];case 3:this.frameBuffer[r+2]=this.gbcBGPalette[c|l[2|h]];case 2:this.frameBuffer[r+1]=this.gbcBGPalette[c|l[1|h]];case 1:this.frameBuffer[r]=this.gbcBGPalette[c|l[h]]}}}}}WindowGBCLayerRenderNoPriorityFlagging(t){if(this.gfxWindowDisplay){var e=t-this.windowY;if(e>=0){var i=this.windowX>0?this.windowX+this.currentX:this.currentX,r=this.pixelStart+i,s=this.pixelStart+this.pixelEnd;if(r>3),n=this.BGCHRBank1[a];n>3);a>2,l=this.tileCache[(96&n)<<4|this.memory[2|i]],g=h=this.memory[1|i],a=Math.min(168-g,8),s=g>7?0:8-g,u=this.pixelStart+(g>8?g-8:0);sh&&(this.frameBuffer[u]>=33554432?(c=l[r|s])>0&&(this.frameBuffer[u]=this.OBJPalette[o|c],this.sortBuffer[g]=h):this.frameBuffer[u]<16777216&&(c=l[r|s])>0&&n<128&&(this.frameBuffer[u]=this.OBJPalette[o|c],this.sortBuffer[g]=h));else for(let t=this.findLowestSpriteDrawable(e,15);m>2,l=(64&n)==(64&r)?this.tileCache[(96&n)<<4|254&this.memory[2|i]]:this.tileCache[(96&n)<<4|this.memory[2|i]|1],r&=63,g=h=this.memory[1|i],a=Math.min(168-g,8),s=g>7?0:8-g,u=this.pixelStart+(g>8?g-8:0);sh&&(this.frameBuffer[u]>=33554432?(c=l[r|s])>0&&(this.frameBuffer[u]=this.OBJPalette[o|c],this.sortBuffer[g]=h):this.frameBuffer[u]<16777216&&(c=l[r|s])>0&&n<128&&(this.frameBuffer[u]=this.OBJPalette[o|c],this.sortBuffer[g]=h))}}findLowestSpriteDrawable(t,e){for(var i=65024,r=0,s=0;i<65184&&r<10;)((s=t-this.memory[i])&e)===s&&(this.OAMAddressCache[r++]=i),i+=4;return r}renderSpriteGBCLayer(t){if(this.gfxSpriteShow){var e=65024,i=t+16,r=0,s=0,h=0,a=0,n=0,o=0,l=null,c=0,m=0,u=0;if(this.gfxSpriteNormalHeight){for(;e<65184&&u<10;e+=4)if((7&(r=i-this.memory[e]))===r){for(s=this.memory[1|e]-8,h=Math.min(160,s+8),o=(7&(n=this.memory[3|e]))<<2,l=this.tileCache[(8&n)<<8|(96&n)<<4|this.memory[2|e]],a=s>0?s:0,s-=r<<3,m=this.pixelStart+a;a=33554432?(c=l[a-s])>0&&(this.frameBuffer[m]=this.gbcOBJPalette[o|c]):this.frameBuffer[m]<16777216&&(c=l[a-s])>0&&n<128&&(this.frameBuffer[m]=this.gbcOBJPalette[o|c]);++u}}else for(;e<65184&&u<10;e+=4)if((15&(r=i-this.memory[e]))===r){for(s=this.memory[1|e]-8,h=Math.min(160,s+8),o=(7&(n=this.memory[3|e]))<<2,l=(64&n)==(64&r<<3)?this.tileCache[(8&n)<<8|(96&n)<<4|254&this.memory[2|e]]:this.tileCache[(8&n)<<8|(96&n)<<4|this.memory[2|e]|1],a=s>0?s:0,s-=(7&r)<<3,m=this.pixelStart+a;a=33554432?(c=l[a-s])>0&&(this.frameBuffer[m]=this.gbcOBJPalette[o|c]):this.frameBuffer[m]<16777216&&(c=l[a-s])>0&&n<128&&(this.frameBuffer[m]=this.gbcOBJPalette[o|c]);++u}}}generateGBTileLine(t){var e=this.memory[1|t]<<8|this.memory[40958&t],i=this.tileCache[(8176&t)>>4];i[7|(t=(14&t)<<2)]=(256&e)>>7|1&e,i[6|t]=(512&e)>>8|(2&e)>>1,i[5|t]=(1024&e)>>9|(4&e)>>2,i[4|t]=(2048&e)>>10|(8&e)>>3,i[3|t]=(4096&e)>>11|(16&e)>>4,i[2|t]=(8192&e)>>12|(32&e)>>5,i[1|t]=(16384&e)>>13|(64&e)>>6,i[t]=(32768&e)>>14|(128&e)>>7}generateGBCTileLineBank1(t){var e=this.memory[1|t]<<8|this.memory[40958&t];t&=8190;var i=this.tileCache[t>>4],r=this.tileCache[512|t>>4],s=this.tileCache[1024|t>>4],h=this.tileCache[1536|t>>4],a=56-(t=(14&t)<<2);h[a]=r[t]=s[7|a]=i[7|t]=(256&e)>>7|1&e,h[1|a]=r[1|t]=s[6|a]=i[6|t]=(512&e)>>8|(2&e)>>1,h[2|a]=r[2|t]=s[5|a]=i[5|t]=(1024&e)>>9|(4&e)>>2,h[3|a]=r[3|t]=s[4|a]=i[4|t]=(2048&e)>>10|(8&e)>>3,h[4|a]=r[4|t]=s[3|a]=i[3|t]=(4096&e)>>11|(16&e)>>4,h[5|a]=r[5|t]=s[2|a]=i[2|t]=(8192&e)>>12|(32&e)>>5,h[6|a]=r[6|t]=s[1|a]=i[1|t]=(16384&e)>>13|(64&e)>>6,h[7|a]=r[7|t]=s[a]=i[t]=(32768&e)>>14|(128&e)>>7}generateGBCTileBank1(t){var e=t>>4,i=this.tileCache[e],r=this.tileCache[512|e],s=this.tileCache[1024|e],h=this.tileCache[1536|e],a=0;t|=32768,e=0;var n=56;do{a=this.memory[1|t]<<8|this.memory[t],h[n]=r[e]=s[7|n]=i[7|e]=(256&a)>>7|1&a,h[1|n]=r[1|e]=s[6|n]=i[6|e]=(512&a)>>8|(2&a)>>1,h[2|n]=r[2|e]=s[5|n]=i[5|e]=(1024&a)>>9|(4&a)>>2,h[3|n]=r[3|e]=s[4|n]=i[4|e]=(2048&a)>>10|(8&a)>>3,h[4|n]=r[4|e]=s[3|n]=i[3|e]=(4096&a)>>11|(16&a)>>4,h[5|n]=r[5|e]=s[2|n]=i[2|e]=(8192&a)>>12|(32&a)>>5,h[6|n]=r[6|e]=s[1|n]=i[1|e]=(16384&a)>>13|(64&a)>>6,h[7|n]=r[7|e]=s[n]=i[e]=(32768&a)>>14|(128&a)>>7,e+=8,n-=8,t+=2}while(n>-1)}generateGBCTileLineBank2(t){var e=this.VRAM[1|t]<<8|this.VRAM[8190&t],i=this.tileCache[2048|t>>4],r=this.tileCache[2560|t>>4],s=this.tileCache[3072|t>>4],h=this.tileCache[3584|t>>4],a=56-(t=(14&t)<<2);h[a]=r[t]=s[7|a]=i[7|t]=(256&e)>>7|1&e,h[1|a]=r[1|t]=s[6|a]=i[6|t]=(512&e)>>8|(2&e)>>1,h[2|a]=r[2|t]=s[5|a]=i[5|t]=(1024&e)>>9|(4&e)>>2,h[3|a]=r[3|t]=s[4|a]=i[4|t]=(2048&e)>>10|(8&e)>>3,h[4|a]=r[4|t]=s[3|a]=i[3|t]=(4096&e)>>11|(16&e)>>4,h[5|a]=r[5|t]=s[2|a]=i[2|t]=(8192&e)>>12|(32&e)>>5,h[6|a]=r[6|t]=s[1|a]=i[1|t]=(16384&e)>>13|(64&e)>>6,h[7|a]=r[7|t]=s[a]=i[t]=(32768&e)>>14|(128&e)>>7}generateGBCTileBank2(t){var e=t>>4,i=this.tileCache[2048|e],r=this.tileCache[2560|e],s=this.tileCache[3072|e],h=this.tileCache[3584|e],a=0;e=0;var n=56;do{a=this.VRAM[1|t]<<8|this.VRAM[t],h[n]=r[e]=s[7|n]=i[7|e]=(256&a)>>7|1&a,h[1|n]=r[1|e]=s[6|n]=i[6|e]=(512&a)>>8|(2&a)>>1,h[2|n]=r[2|e]=s[5|n]=i[5|e]=(1024&a)>>9|(4&a)>>2,h[3|n]=r[3|e]=s[4|n]=i[4|e]=(2048&a)>>10|(8&a)>>3,h[4|n]=r[4|e]=s[3|n]=i[3|e]=(4096&a)>>11|(16&a)>>4,h[5|n]=r[5|e]=s[2|n]=i[2|e]=(8192&a)>>12|(32&a)>>5,h[6|n]=r[6|e]=s[1|n]=i[1|e]=(16384&a)>>13|(64&a)>>6,h[7|n]=r[7|e]=s[n]=i[e]=(32768&a)>>14|(128&a)>>7,e+=8,n-=8,t+=2}while(n>-1)}generateGBOAMTileLine(t){var e=this.memory[1|t]<<8|this.memory[40958&t];t&=8190;var i=this.tileCache[t>>4],r=this.tileCache[512|t>>4],s=this.tileCache[1024|t>>4],h=this.tileCache[1536|t>>4],a=56-(t=(14&t)<<2);h[a]=r[t]=s[7|a]=i[7|t]=(256&e)>>7|1&e,h[1|a]=r[1|t]=s[6|a]=i[6|t]=(512&e)>>8|(2&e)>>1,h[2|a]=r[2|t]=s[5|a]=i[5|t]=(1024&e)>>9|(4&e)>>2,h[3|a]=r[3|t]=s[4|a]=i[4|t]=(2048&e)>>10|(8&e)>>3,h[4|a]=r[4|t]=s[3|a]=i[3|t]=(4096&e)>>11|(16&e)>>4,h[5|a]=r[5|t]=s[2|a]=i[2|t]=(8192&e)>>12|(32&e)>>5,h[6|a]=r[6|t]=s[1|a]=i[1|t]=(16384&e)>>13|(64&e)>>6,h[7|a]=r[7|t]=s[a]=i[t]=(32768&e)>>14|(128&e)>>7}graphicsJIT(){this.LCDisOn&&(this.cpu.totalLinesPassed=0,this.graphicsJITScanlineGroup())}graphicsJITVBlank(){this.cpu.totalLinesPassed+=this.queuedScanLines,this.graphicsJITScanlineGroup()}graphicsJITScanlineGroup(){for(;this.queuedScanLines>0;)this.renderScanLine(this.lastUnrenderedLine),this.lastUnrenderedLine<143?++this.lastUnrenderedLine:this.lastUnrenderedLine=0,--this.queuedScanLines}incrementScanLineQueue(){this.queuedScanLines<144?++this.queuedScanLines:(this.currentX=0,this.midScanlineOffset=-1,this.lastUnrenderedLine<143?++this.lastUnrenderedLine:this.lastUnrenderedLine=0)}midScanLineJIT(){this.graphicsJIT(),this.renderMidScanLine()}launchIRQ(){var t=0,e=1;do{if((e&this.IRQLineMatched)===e)return this.IME=!1,this.interruptsRequested-=e,this.IRQLineMatched=0,this.CPUTicks=20,this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=64|t<<3,void this.updateCore();e=1<<++t}while(t<5)}checkIRQMatching(){this.IME&&(this.IRQLineMatched=this.interruptsEnabled&this.interruptsRequested&31)}calculateHALTPeriod(){if(this.halt)t=this.remainingClocks;else{this.halt=!0;var t=-1;if(this.LCDisOn&&(1==(1&this.interruptsEnabled)&&(t=456*((1===this.modeSTAT?298:144)-this.actualScanLine)-this.LCDTicks<=143?456*(154-this.actualScanLine):456)-this.LCDTicks<0&&8==(8&this.interruptsEnabled)&&(this.serialTimer<=t||-1===t)&&(t=this.serialTimer)}var e=this.cpu.cyclesTotal-this.cpu.ticks<=0?t<=e?(this.CPUTicks=Math.max(t,this.CPUTicks),this.updateCoreFull(),this.halt=!1,this.CPUTicks=0):(this.CPUTicks=Math.max(e,this.CPUTicks),this.remainingClocks=t-this.CPUTicks):this.CPUTicks+=e}memoryRead(t){return this.memoryReader[t].apply(this,[t])}memoryHighRead(t){return this.memoryHighReader[t].apply(this,[t])}memoryReadJumpCompile(){for(let t=0;t<=65535;t++)if(t<16384)this.memoryReader[t]=this.memoryReadNormal;else if(t<32768)this.memoryReader[t]=this.memoryReadROM;else if(t<38912)this.memoryReader[t]=this.cartridge.useGBCMode?this.VRAMDATAReadCGBCPU:this.VRAMDATAReadDMGCPU;else if(t<40960)this.memoryReader[t]=this.cartridge.useGBCMode?this.VRAMCHRReadCGBCPU:this.VRAMCHRReadDMGCPU;else if(t>=40960&&t<49152)this.cartridge.mbc&&0===this.cartridge.mbc.ramSize?this.memoryReader[t]=this.badMemoryRead:this.cartridge.hasMBC7?this.memoryReader[t]=this.memoryReadMBC7:this.cartridge.hasMBC3?this.memoryReader[t]=this.memoryReadMBC3:this.memoryReader[t]=this.memoryReadMBC;else if(t>=49152&&t<57344)!this.cartridge.useGBCMode||t<53248?this.memoryReader[t]=this.memoryReadNormal:this.memoryReader[t]=this.memoryReadGBCMemory;else if(t>=57344&&t<65024)!this.cartridge.useGBCMode||t<61440?this.memoryReader[t]=this.memoryReadECHONormal:this.memoryReader[t]=this.memoryReadECHOGBCMemory;else if(t<65184)this.memoryReader[t]=this.memoryReadOAM;else if(this.cartridge.useGBCMode&&t>=65184&&t<65280)this.memoryReader[t]=this.memoryReadNormal;else if(t>=65280)switch(t){case lt:this.memoryHighReader[0]=this.memoryReader[lt]=(t=>192|this.memory[lt]);break;case ct:this.memoryHighReader[1]=this.memoryReader[ct]=(t=>this.memory[mt]<128?this.memory[ct]:255);break;case mt:this.cartridge.useGBCMode?this.memoryHighReader[2]=this.memoryReader[mt]=(t=>(this.serialTimer<=0?124:252)|this.memory[mt]):this.memoryHighReader[2]=this.memoryReader[mt]=(t=>(this.serialTimer<=0?126:254)|this.memory[mt]);break;case 65283:this.memoryHighReader[3]=this.memoryReader[65283]=this.badMemoryRead;break;case ut:this.memoryHighReader[4]=this.memoryReader[ut]=(t=>(this.memory[ut]=this.memory[ut]+(this.DIVTicks>>8)&255,this.DIVTicks&=255,this.memory[ut]));break;case 65285:case 65286:this.memoryHighReader[255&t]=this.memoryHighReadNormal,this.memoryReader[t]=this.memoryReadNormal;break;case 65287:this.memoryHighReader[7]=this.memoryReader[65287]=(t=>248|this.memory[65287]);break;case 65288:case 65289:case 65290:case 65291:case 65292:case 65293:case 65294:this.memoryHighReader[255&t]=this.memoryReader[t]=this.badMemoryRead;break;case 65295:this.memoryHighReader[15]=this.memoryReader[65295]=(t=>224|this.interruptsRequested);break;case 65296:this.memoryHighReader[16]=this.memoryReader[65296]=(t=>128|this.memory[65296]);break;case 65297:this.memoryHighReader[17]=this.memoryReader[65297]=(t=>63|this.memory[65297]);break;case 65298:this.memoryHighReader[18]=this.memoryHighReadNormal,this.memoryReader[65298]=this.memoryReadNormal;break;case 65299:this.memoryHighReader[19]=this.memoryReader[65299]=this.badMemoryRead;break;case 65300:this.memoryHighReader[20]=this.memoryReader[65300]=(t=>191|this.memory[65300]);break;case 65301:this.memoryHighReader[21]=this.badMemoryRead,this.memoryReader[65301]=this.badMemoryRead;break;case 65302:this.memoryHighReader[22]=this.memoryReader[65302]=(t=>63|this.memory[65302]);break;case 65303:this.memoryHighReader[23]=this.memoryHighReadNormal,this.memoryReader[65303]=this.memoryReadNormal;break;case 65304:this.memoryHighReader[24]=this.memoryReader[65304]=this.badMemoryRead;break;case 65305:this.memoryHighReader[25]=this.memoryReader[65305]=(t=>191|this.memory[65305]);break;case 65306:this.memoryHighReader[26]=this.memoryReader[65306]=(t=>127|this.memory[65306]);break;case 65307:this.memoryHighReader[27]=this.memoryReader[65307]=this.badMemoryRead;break;case 65308:this.memoryHighReader[28]=this.memoryReader[65308]=(t=>159|this.memory[65308]);break;case 65309:this.memoryHighReader[29]=this.memoryReader[65309]=this.badMemoryRead;break;case 65310:this.memoryHighReader[30]=this.memoryReader[65310]=(t=>191|this.memory[65310]);break;case 65311:case 65312:this.memoryHighReader[255&t]=this.memoryReader[t]=this.badMemoryRead;break;case 65313:case 65314:this.memoryHighReader[255&t]=this.memoryHighReadNormal,this.memoryReader[t]=this.memoryReadNormal;break;case 65315:this.memoryHighReader[35]=this.memoryReader[65315]=(t=>191|this.memory[65315]);break;case 65316:case 65317:this.memoryHighReader[255&t]=this.memoryHighReadNormal,this.memoryReader[t]=this.memoryReadNormal;break;case 65318:this.memoryHighReader[38]=this.memoryReader[65318]=(t=>(this.audioController.runJIT(),112|this.memory[65318]));break;case 65319:case 65320:case 65321:case 65322:case 65323:case 65324:case 65325:case 65326:case 65327:this.memoryHighReader[255&t]=this.memoryReader[t]=this.badMemoryRead;break;case 65328:case 65329:case 65330:case 65331:case 65332:case 65333:case 65334:case 65335:case 65336:case 65337:case 65338:case 65339:case 65340:case 65341:case 65342:case 65343:this.memoryReader[t]=(t=>this.audioController.channel3canPlay?this.memory[65280|this.audioController.channel3lastSampleLookup>>1]:this.memory[t]),this.memoryHighReader[255&t]=(t=>this.audioController.channel3canPlay?this.memory[65280|this.audioController.channel3lastSampleLookup>>1]:this.memory[65280|t]);break;case 65344:this.memoryHighReader[64]=this.memoryHighReadNormal,this.memoryReader[65344]=this.memoryReadNormal;break;case 65345:this.memoryHighReader[65]=this.memoryReader[65345]=(t=>128|this.memory[65345]|this.modeSTAT);break;case 65346:this.memoryHighReader[66]=this.memoryReader[65346]=(t=>this.backgroundY);break;case 65347:this.memoryHighReader[67]=this.memoryReader[65347]=(t=>this.backgroundX);break;case 65348:this.memoryHighReader[68]=this.memoryReader[65348]=(t=>this.LCDisOn?this.memory[65348]:0);break;case 65349:case 65350:case 65351:case 65352:case 65353:this.memoryHighReader[255&t]=this.memoryHighReadNormal,this.memoryReader[t]=this.memoryReadNormal;break;case 65354:this.memoryHighReader[74]=this.memoryReader[65354]=(t=>this.windowY);break;case 65355:this.memoryHighReader[75]=this.memoryHighReadNormal,this.memoryReader[65355]=this.memoryReadNormal;break;case 65356:this.memoryHighReader[76]=this.memoryReader[65356]=this.badMemoryRead;break;case 65357:this.memoryHighReader[77]=this.memoryHighReadNormal,this.memoryReader[65357]=this.memoryReadNormal;break;case 65358:this.memoryHighReader[78]=this.memoryReader[65358]=this.badMemoryRead;break;case 65359:this.memoryHighReader[79]=this.memoryReader[65359]=(t=>this.currVRAMBank);break;case 65360:case 65361:case 65362:case 65363:case 65364:this.memoryHighReader[255&t]=this.memoryHighReadNormal,this.memoryReader[t]=this.memoryReadNormal;break;case 65365:this.cartridge.useGBCMode?this.memoryHighReader[85]=this.memoryReader[65365]=(t=>(!this.LCDisOn&&this.hdmaRunning&&(this.DMAWrite(1+(127&this.memory[65365])),this.memory[65365]=255,this.hdmaRunning=!1),this.memory[65365])):(this.memoryReader[65365]=this.memoryReadNormal,this.memoryHighReader[85]=this.memoryHighReadNormal);break;case 65366:this.cartridge.useGBCMode?this.memoryHighReader[86]=this.memoryReader[65366]=(t=>60|(this.memory[65366]>=192?2|193&this.memory[65366]:195&this.memory[65366])):(this.memoryReader[65366]=this.memoryReadNormal,this.memoryHighReader[86]=this.memoryHighReadNormal);break;case 65367:case 65368:case 65369:case 65370:case 65371:case 65372:case 65373:case 65374:case 65375:case 65376:case 65377:case 65378:case 65379:case 65380:case 65381:case 65382:case 65383:this.memoryHighReader[255&t]=this.memoryReader[t]=this.badMemoryRead;break;case 65384:case 65385:case 65386:case 65387:this.memoryHighReader[255&t]=this.memoryHighReadNormal,this.memoryReader[t]=this.memoryReadNormal;break;case 65388:this.cartridge.useGBCMode?this.memoryHighReader[108]=this.memoryReader[65388]=(t=>254|this.memory[65388]):this.memoryHighReader[108]=this.memoryReader[65388]=this.badMemoryRead;break;case 65389:case 65390:case 65391:this.memoryHighReader[255&t]=this.memoryReader[t]=this.badMemoryRead;break;case 65392:this.cartridge.useGBCMode?this.memoryHighReader[112]=this.memoryReader[65392]=(t=>64|this.memory[65392]):this.memoryHighReader[112]=this.memoryReader[65392]=this.badMemoryRead;break;case 65393:this.memoryHighReader[113]=this.memoryReader[65393]=this.badMemoryRead;break;case 65394:case 65395:this.memoryHighReader[255&t]=this.memoryReader[t]=this.memoryReadNormal;break;case 65396:this.cartridge.useGBCMode?this.memoryHighReader[116]=this.memoryReader[65396]=this.memoryReadNormal:this.memoryHighReader[116]=this.memoryReader[65396]=this.badMemoryRead;break;case 65397:this.memoryHighReader[117]=this.memoryReader[65397]=(t=>143|this.memory[65397]);break;case 65398:this.memoryHighReader[118]=this.memoryReader[65398]=(t=>(this.audioController.runJIT(),this.audioController.channel2envelopeVolume<<4|this.audioController.channel1envelopeVolume));break;case 65399:this.memoryHighReader[119]=this.memoryReader[65399]=(t=>(this.audioController.runJIT(),this.audioController.channel4envelopeVolume<<4|this.audioController.channel3envelopeVolume));break;case 65400:case 65401:case 65402:case 65403:case 65404:case 65405:case 65406:case 65407:this.memoryHighReader[255&t]=this.memoryReader[t]=this.badMemoryRead;break;case gt:this.memoryHighReader[255]=this.memoryReader[gt]=(t=>this.interruptsEnabled);break;default:this.memoryReader[t]=this.memoryReadNormal,this.memoryHighReader[255&t]=this.memoryHighReadNormal}else this.memoryReader[t]=this.badMemoryRead}memoryReadNormal(t){return this.memory[t]}memoryHighReadNormal(t){return this.memory[65280|t]}memoryReadROM(t){return this.cartridge.rom.getByte(this.cartridge.mbc.currentROMBank+t)}memoryReadMBC(t){return this.cartridge.mbc.readRAM(t)}memoryReadMBC7(t){return this.cartridge.mbc.readRAM(t)}memoryReadMBC3(t){return this.cartridge.mbc.readRAM(t)}memoryReadGBCMemory(t){return this.GBCMemory[t+this.gbcRamBankPosition]}memoryReadOAM(t){return this.modeSTAT>1?255:this.memory[t]}memoryReadECHOGBCMemory(t){return this.GBCMemory[t+this.gbcRamBankPositionECHO]}memoryReadECHONormal(t){return this.memory[t-8192]}badMemoryRead(t){return 255}VRAMDATAReadCGBCPU(t){return this.modeSTAT>2?255:0===this.currVRAMBank?this.memory[t]:this.VRAM[8191&t]}VRAMDATAReadDMGCPU(t){return this.modeSTAT>2?255:this.memory[t]}VRAMCHRReadCGBCPU(t){return this.modeSTAT>2?255:this.BGCHRCurrentBank[2047&t]}VRAMCHRReadDMGCPU(t){return this.modeSTAT>2?255:this.BGCHRBank1[2047&t]}memoryWrite(t,e){this.memoryWriter[t].apply(this,[t,e])}memoryHighWrite(t,e){this.memoryHighWriter[t].apply(this,[t,e])}memoryWriteJumpCompile(){for(var t=0;t<=65535;t++)t<=K?this.cartridge.hasMBC1?this.memoryWriter[t]=t<8192?this.MBCWriteEnable:t<16384?this.MBC1WriteROMBank:t<24576?this.MBC1WriteRAMBank:this.MBC1WriteType:this.cartridge.hasMBC2?this.memoryWriter[t]=t<4096?this.MBCWriteEnable:t>=8448&&t<8704?this.MBC2WriteROMBank:this.onIllegalWrite:this.cartridge.hasMBC3?this.memoryWriter[t]=t<8192?this.MBCWriteEnable:t<16384?this.MBC3WriteROMBank:t<24576?this.MBC3WriteRAMBank:this.MBC3WriteRTCLatch:this.cartridge.hasMBC5||this.cartridge.hasRUMBLE||this.cartridge.hasMBC7?this.memoryWriter[t]=t<8192?this.MBCWriteEnable:t<12288?this.MBC5WriteROMBankLow:t<16384?this.MBC5WriteROMBankHigh:t<24576?this.cartridge.hasRUMBLE?this.RUMBLEWriteRAMBank:this.MBC5WriteRAMBank:this.onIllegalWrite:this.cartridge.hasHuC3?this.memoryWriter[t]=t<8192?this.MBCWriteEnable:t<16384?this.MBC3WriteROMBank:t<24576?this.HuC3WriteRAMBank:this.onIllegalWrite:this.memoryWriter[t]=this.onIllegalWrite:t<=et?this.memoryWriter[t]=this.cartridge.useGBCMode?this.VRAMGBCDATAWrite:this.VRAMGBDATAWrite:t<38912?this.memoryWriter[t]=this.cartridge.useGBCMode?this.VRAMGBCDATAWrite:this.VRAMGBDATAUpperWrite:t<40960?this.memoryWriter[t]=this.cartridge.useGBCMode?this.VRAMGBCCHRMAPWrite:this.VRAMGBCHRMAPWrite:t<49152?this.cartridge.mbc&&0!==this.cartridge.mbc.ramSize?this.cartridge.hasMBC3?this.memoryWriter[t]=this.memoryWriteMBC3RAM:this.memoryWriter[t]=this.memoryWriteMBCRAM:this.memoryWriter[t]=this.onIllegalWrite:t<57344?this.cartridge.useGBCMode&&t>=53248?this.memoryWriter[t]=this.memoryWriteGBCRAM:this.memoryWriter[t]=this.memoryWriteNormal:t<65024?this.cartridge.useGBCMode&&t>=61440?this.memoryWriter[t]=this.memoryWriteECHOGBCRAM:this.memoryWriter[t]=this.memoryWriteECHONormal:t<=65184?this.memoryWriter[t]=this.memoryWriteOAMRAM:t<65280?this.cartridge.useGBCMode?this.memoryWriter[t]=this.memoryWriteNormal:this.memoryWriter[t]=this.onIllegalWrite:(this.memoryWriter[t]=this.memoryWriteNormal,this.memoryHighWriter[255&t]=this.memoryHighWriteNormal);this.registerWriteJumpCompile()}MBCWriteEnable(t,e){this.cartridge.mbc.writeEnable(t,e)}MBC1WriteROMBank(t,e){this.cartridge.mbc1.writeROMBank(t,e)}MBC1WriteRAMBank(t,e){this.cartridge.mbc1.writeRAMBank(t,e)}MBC1WriteType(t,e){this.cartridge.mbc1.writeType(t,e)}MBC2WriteROMBank(t,e){this.cartridge.mbc2.writeROMBank(t,e)}MBC3WriteROMBank(t,e){return this.cartridge.mbc3.writeROMBank(t,e)}MBC3WriteRAMBank(t,e){return this.cartridge.mbc3.writeRAMBank(t,e)}MBC3WriteRTCLatch(t,e){return this.cartridge.mbc3.rtc.writeLatch(t,e)}MBC5WriteROMBankLow(t,e){return this.cartridge.mbc5.writeROMBankLow(t,e)}MBC5WriteROMBankHigh(t,e){return this.cartridge.mbc5.writeROMBankHigh(t,e)}MBC5WriteRAMBank(t,e){return this.cartridge.mbc5.writeRAMBank(t,e)}RUMBLEWriteRAMBank(t,e){return this.cartridge.rumble.writeRAMBank(t,e)}HuC3WriteRAMBank(t,e){this.cartridge.mbc.currentMBCRAMBank=3&e,this.cartridge.mbc.currentRAMBankPosition=(this.cartridge.mbc.currentMBCRAMBank<<13)-40960}onIllegalWrite(t,e){}memoryWriteNormal(t,e){this.memory[t]=e}memoryHighWriteNormal(t,e){this.memory[65280|t]=e}memoryWriteMBCRAM(t,e){this.cartridge.mbc.writeRAM(t,e)}memoryWriteMBC3RAM(t,e){return this.cartridge.mbc.writeRAM(t,e)}memoryWriteGBCRAM(t,e){this.GBCMemory[t+this.gbcRamBankPosition]=e}memoryWriteOAMRAM(t,e){this.modeSTAT<2&&this.memory[t]!=e&&(this.graphicsJIT(),this.memory[t]=e)}memoryWriteECHOGBCRAM(t,e){this.GBCMemory[t+this.gbcRamBankPositionECHO]=e}memoryWriteECHONormal(t,e){this.memory[t-8192]=e}VRAMGBDATAWrite(t,e){this.modeSTAT<3&&this.memory[t]!=e&&(this.graphicsJIT(),this.memory[t]=e,this.generateGBOAMTileLine(t))}VRAMGBDATAUpperWrite(t,e){this.modeSTAT<3&&this.memory[t]!=e&&(this.graphicsJIT(),this.memory[t]=e,this.generateGBTileLine(t))}VRAMGBCDATAWrite(t,e){this.modeSTAT<3&&(0===this.currVRAMBank?this.memory[t]!=e&&(this.graphicsJIT(),this.memory[t]=e,this.generateGBCTileLineBank1(t)):(t&=8191,this.VRAM[t]!=e&&(this.graphicsJIT(),this.VRAM[t]=e,this.generateGBCTileLineBank2(t))))}VRAMGBCHRMAPWrite(t,e){this.modeSTAT<3&&(t&=2047,this.BGCHRBank1[t]!=e&&(this.graphicsJIT(),this.BGCHRBank1[t]=e))}VRAMGBCCHRMAPWrite(t,e){this.modeSTAT<3&&(t&=2047,this.BGCHRCurrentBank[t]!=e&&(this.graphicsJIT(),this.BGCHRCurrentBank[t]=e))}DMAWrite(t){this.halt||(this.CPUTicks+=4|t<<5<0);else{var h=this.VRAM;do{i<6144?(h[i]=r[e].apply(this,[e++]),h[1|i]=r[e].apply(this,[e++]),h[2|i]=r[e].apply(this,[e++]),h[3|i]=r[e].apply(this,[e++]),h[4|i]=r[e].apply(this,[e++]),h[5|i]=r[e].apply(this,[e++]),h[6|i]=r[e].apply(this,[e++]),h[7|i]=r[e].apply(this,[e++]),h[8|i]=r[e].apply(this,[e++]),h[9|i]=r[e].apply(this,[e++]),h[10|i]=r[e].apply(this,[e++]),h[11|i]=r[e].apply(this,[e++]),h[12|i]=r[e].apply(this,[e++]),h[13|i]=r[e].apply(this,[e++]),h[14|i]=r[e].apply(this,[e++]),h[15|i]=r[e].apply(this,[e++]),this.generateGBCTileBank2(i),i+=16):(i&=2032,this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),this.BGCHRBank2[i++]=r[e].apply(this,[e++]),i=i+6144&8176),e&=65520,--t}while(t>0)}s[65361]=e>>8,s[65362]=240&e,s[65363]=i>>8,s[65364]=240&i}registerWriteJumpCompile(){this.memoryHighWriter[0]=this.memoryWriter[lt]=((t,e)=>{this.memory[lt]=48&e|(0==(32&e)?this.joypad.value>>4:15)&(0==(16&e)?15&this.joypad.value:15)}),this.memoryHighWriter[1]=this.memoryWriter[ct]=((t,e)=>{this.memory[mt]<128&&(this.memory[ct]=e)}),this.memoryHighWriter[2]=this.memoryHighWriteNormal,this.memoryWriter[mt]=this.memoryWriteNormal,this.memoryHighWriter[3]=this.memoryWriter[65283]=this.onIllegalWrite,this.memoryHighWriter[4]=this.memoryWriter[ut]=((t,e)=>{this.DIVTicks&=255,this.memory[ut]=0}),this.memoryHighWriter[5]=this.memoryWriter[65285]=((t,e)=>{this.memory[65285]=e}),this.memoryHighWriter[6]=this.memoryWriter[65286]=((t,e)=>{this.memory[65286]=e}),this.memoryHighWriter[7]=this.memoryWriter[65287]=((t,e)=>{this.memory[65287]=7&e,this.TIMAEnabled=4==(4&e),this.TACClocker=Math.pow(4,0!=(3&e)?3&e:4)<<2}),this.memoryHighWriter[8]=this.memoryWriter[65288]=this.onIllegalWrite,this.memoryHighWriter[9]=this.memoryWriter[65289]=this.onIllegalWrite,this.memoryHighWriter[10]=this.memoryWriter[65290]=this.onIllegalWrite,this.memoryHighWriter[11]=this.memoryWriter[65291]=this.onIllegalWrite,this.memoryHighWriter[12]=this.memoryWriter[65292]=this.onIllegalWrite,this.memoryHighWriter[13]=this.memoryWriter[65293]=this.onIllegalWrite,this.memoryHighWriter[14]=this.memoryWriter[65294]=this.onIllegalWrite,this.memoryHighWriter[15]=this.memoryWriter[65295]=((t,e)=>{this.interruptsRequested=e,this.checkIRQMatching()}),this.memoryHighWriter[16]=this.memoryWriter[65296]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel1decreaseSweep&&0==(8&e)&&this.audioController.channel1Swept&&(this.audioController.channel1SweepFault=!0),this.audioController.channel1lastTimeSweep=(112&e)>>4,this.audioController.channel1frequencySweepDivider=7&e,this.audioController.channel1decreaseSweep=8==(8&e),this.memory[65296]=e,this.audioController.checkChannel1Enable())}),this.memoryHighWriter[17]=this.memoryWriter[65297]=((t,e)=>{!this.soundMasterEnabled&&this.cartridge.useGBCMode||(this.soundMasterEnabled?this.audioController.runJIT():e&=63,this.audioController.channel1CachedDuty=Z[e>>6],this.audioController.channel1totalLength=64-(63&e),this.memory[65297]=e,this.audioController.checkChannel1Enable())}),this.memoryHighWriter[18]=this.memoryWriter[65298]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel1Enabled&&0===this.audioController.channel1envelopeSweeps&&(8==(8&(this.memory[65298]^e))?(0==(8&this.memory[65298])&&(7==(7&this.memory[65298])?this.audioController.channel1envelopeVolume+=2:++this.audioController.channel1envelopeVolume),this.audioController.channel1envelopeVolume=16-this.audioController.channel1envelopeVolume&15):8==(15&this.memory[65298])&&(this.audioController.channel1envelopeVolume=1+this.audioController.channel1envelopeVolume&15),this.audioController.cacheChannel1OutputLevel()),this.audioController.channel1envelopeType=8==(8&e),this.memory[65298]=e,this.audioController.checkChannel1VolumeEnable())}),this.memoryHighWriter[19]=this.memoryWriter[65299]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel1frequency=1792&this.audioController.channel1frequency|e,this.audioController.channel1FrequencyTracker=2048-this.audioController.channel1frequency<<2)}),this.memoryHighWriter[20]=this.memoryWriter[65300]=((t,e)=>{if(this.soundMasterEnabled){if(this.audioController.runJIT(),this.audioController.channel1consecutive=0==(64&e),this.audioController.channel1frequency=(7&e)<<8|255&this.audioController.channel1frequency,this.audioController.channel1FrequencyTracker=2048-this.audioController.channel1frequency<<2,e>127){this.audioController.channel1timeSweep=this.audioController.channel1lastTimeSweep,this.audioController.channel1Swept=!1;var i=this.memory[65298];this.audioController.channel1envelopeVolume=i>>4,this.audioController.cacheChannel1OutputLevel(),this.audioController.channel1envelopeSweepsLast=(7&i)-1,0===this.audioController.channel1totalLength&&(this.audioController.channel1totalLength=64),this.audioController.channel1lastTimeSweep>0||this.audioController.channel1frequencySweepDivider>0?this.memory[65318]|=1:this.memory[65318]&=254,64==(64&e)&&(this.memory[65318]|=1),this.audioController.channel1ShadowFrequency=this.audioController.channel1frequency,this.audioController.channel1SweepFault=!1,this.audioController.performChannel1AudioSweepDummy()}this.audioController.checkChannel1Enable(),this.memory[65300]=e}}),this.memoryHighWriter[21]=this.memoryWriter[65301]=this.onIllegalWrite,this.memoryHighWriter[22]=this.memoryWriter[65302]=((t,e)=>{!this.soundMasterEnabled&&this.cartridge.useGBCMode||(this.soundMasterEnabled?this.audioController.runJIT():e&=63,this.audioController.channel2CachedDuty=Z[e>>6],this.audioController.channel2totalLength=64-(63&e),this.memory[65302]=e,this.audioController.checkChannel2Enable())}),this.memoryHighWriter[23]=this.memoryWriter[65303]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel2Enabled&&0===this.audioController.channel2envelopeSweeps&&(8==(8&(this.memory[65303]^e))?(0==(8&this.memory[65303])&&(7==(7&this.memory[65303])?this.audioController.channel2envelopeVolume+=2:++this.audioController.channel2envelopeVolume),this.audioController.channel2envelopeVolume=16-this.audioController.channel2envelopeVolume&15):8==(15&this.memory[65303])&&(this.audioController.channel2envelopeVolume=1+this.audioController.channel2envelopeVolume&15),this.audioController.cacheChannel2OutputLevel()),this.audioController.channel2envelopeType=8==(8&e),this.memory[65303]=e,this.audioController.checkChannel2VolumeEnable())}),this.memoryHighWriter[24]=this.memoryWriter[65304]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel2frequency=1792&this.audioController.channel2frequency|e,this.audioController.channel2FrequencyTracker=2048-this.audioController.channel2frequency<<2)}),this.memoryHighWriter[25]=this.memoryWriter[65305]=((t,e)=>{if(this.soundMasterEnabled){if(this.audioController.runJIT(),e>127){var i=this.memory[65303];this.audioController.channel2envelopeVolume=i>>4,this.audioController.cacheChannel2OutputLevel(),this.audioController.channel2envelopeSweepsLast=(7&i)-1,0===this.audioController.channel2totalLength&&(this.audioController.channel2totalLength=64),64==(64&e)&&(this.memory[65318]|=2)}this.audioController.channel2consecutive=0==(64&e),this.audioController.channel2frequency=(7&e)<<8|255&this.audioController.channel2frequency,this.audioController.channel2FrequencyTracker=2048-this.audioController.channel2frequency<<2,this.memory[65305]=e,this.audioController.checkChannel2Enable()}}),this.memoryHighWriter[26]=this.memoryWriter[65306]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),!this.audioController.channel3canPlay&&e>=128&&(this.audioController.channel3lastSampleLookup=0,this.audioController.cacheChannel3Update()),this.audioController.channel3canPlay=e>127,this.audioController.channel3canPlay&&this.memory[65306]>127&&!this.audioController.channel3consecutive&&(this.memory[65318]|=4),this.memory[65306]=e)}),this.memoryHighWriter[27]=this.memoryWriter[65307]=((t,e)=>{!this.soundMasterEnabled&&this.cartridge.useGBCMode||(this.soundMasterEnabled&&this.audioController.runJIT(),this.audioController.channel3totalLength=256-e,this.audioController.checkChannel3Enable())}),this.memoryHighWriter[28]=this.memoryWriter[65308]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),e&=96,this.memory[65308]=e,this.audioController.channel3patternType=0===e?4:(e>>5)-1)}),this.memoryHighWriter[29]=this.memoryWriter[65309]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel3frequency=1792&this.audioController.channel3frequency|e,this.audioController.channel3FrequencyPeriod=2048-this.audioController.channel3frequency<<1)}),this.memoryHighWriter[30]=this.memoryWriter[65310]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),e>127&&(0===this.audioController.channel3totalLength&&(this.audioController.channel3totalLength=256),this.audioController.channel3lastSampleLookup=0,64==(64&e)&&(this.memory[65318]|=4)),this.audioController.channel3consecutive=0==(64&e),this.audioController.channel3frequency=(7&e)<<8|255&this.audioController.channel3frequency,this.audioController.channel3FrequencyPeriod=2048-this.audioController.channel3frequency<<1,this.memory[65310]=e,this.audioController.checkChannel3Enable())}),this.memoryHighWriter[31]=this.memoryWriter[65311]=this.onIllegalWrite,this.memoryHighWriter[32]=this.memoryWriter[65312]=((t,e)=>{!this.soundMasterEnabled&&this.cartridge.useGBCMode||(this.soundMasterEnabled&&this.audioController.runJIT(),this.audioController.channel4totalLength=64-(63&e),this.audioController.checkChannel4Enable())}),this.memoryHighWriter[33]=this.memoryWriter[65313]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel4Enabled&&0===this.audioController.channel4envelopeSweeps&&(8==(8&(this.memory[65313]^e))?(0==(8&this.memory[65313])&&(7==(7&this.memory[65313])?this.audioController.channel4envelopeVolume+=2:++this.audioController.channel4envelopeVolume),this.audioController.channel4envelopeVolume=16-this.audioController.channel4envelopeVolume&15):8==(15&this.memory[65313])&&(this.audioController.channel4envelopeVolume=1+this.audioController.channel4envelopeVolume&15),this.audioController.channel4currentVolume=this.audioController.channel4envelopeVolume<{if(this.soundMasterEnabled){this.audioController.runJIT(),this.audioController.channel4FrequencyPeriod=Math.max((7&e)<<4,8)<<(e>>4);var i=8&e;(8===i&&32767===this.audioController.channel4BitRange||0===i&&127===this.audioController.channel4BitRange)&&(this.audioController.channel4lastSampleLookup=0,this.audioController.channel4BitRange=8===i?127:32767,this.audioController.channel4VolumeShifter=8===i?7:15,this.audioController.channel4currentVolume=this.audioController.channel4envelopeVolume<{if(this.soundMasterEnabled){if(this.audioController.runJIT(),this.memory[65315]=e,this.audioController.channel4consecutive=0==(64&e),e>127){var i=this.memory[65313];this.audioController.channel4envelopeVolume=i>>4,this.audioController.channel4currentVolume=this.audioController.channel4envelopeVolume<{this.soundMasterEnabled&&this.memory[65316]!=e&&(this.audioController.runJIT(),this.memory[65316]=e,this.audioController.VinLeftChannelMasterVolume=1+(e>>4&7),this.audioController.VinRightChannelMasterVolume=1+(7&e),this.audioController.cacheMixerOutputLevel())}),this.memoryHighWriter[37]=this.memoryWriter[65317]=((t,e)=>{this.soundMasterEnabled&&this.memory[65317]!=e&&(this.audioController.runJIT(),this.memory[65317]=e,this.audioController.rightChannel1=1==(1&e),this.audioController.rightChannel2=2==(2&e),this.audioController.rightChannel3=4==(4&e),this.audioController.rightChannel4=8==(8&e),this.audioController.leftChannel1=16==(16&e),this.audioController.leftChannel2=32==(32&e),this.audioController.leftChannel3=64==(64&e),this.audioController.leftChannel4=e>127,this.audioController.cacheChannel1OutputLevel(),this.audioController.cacheChannel2OutputLevel(),this.audioController.cacheChannel3OutputLevel(),this.audioController.cacheChannel4OutputLevel())}),this.memoryHighWriter[38]=this.memoryWriter[65318]=((t,e)=>{if(this.audioController.runJIT(),!this.soundMasterEnabled&&e>127)this.memory[65318]=128,this.soundMasterEnabled=!0,this.audioController.initStartState();else if(this.soundMasterEnabled&&e<128){this.memory[65318]=0,this.soundMasterEnabled=!1;for(var i=65296;i<65318;i++)this.memoryWriter[i].apply(this,[i,0])}}),this.memoryHighWriter[39]=this.memoryWriter[65319]=this.onIllegalWrite,this.memoryHighWriter[40]=this.memoryWriter[65320]=this.onIllegalWrite,this.memoryHighWriter[41]=this.memoryWriter[65321]=this.onIllegalWrite,this.memoryHighWriter[42]=this.memoryWriter[65322]=this.onIllegalWrite,this.memoryHighWriter[43]=this.memoryWriter[65323]=this.onIllegalWrite,this.memoryHighWriter[44]=this.memoryWriter[65324]=this.onIllegalWrite,this.memoryHighWriter[45]=this.memoryWriter[65325]=this.onIllegalWrite,this.memoryHighWriter[46]=this.memoryWriter[65326]=this.onIllegalWrite,this.memoryHighWriter[47]=this.memoryWriter[65327]=this.onIllegalWrite,this.memoryHighWriter[48]=this.memoryWriter[65328]=((t,e)=>{this.writeChannel3RAM(0,e)}),this.memoryHighWriter[49]=this.memoryWriter[65329]=((t,e)=>{this.writeChannel3RAM(1,e)}),this.memoryHighWriter[50]=this.memoryWriter[65330]=((t,e)=>{this.writeChannel3RAM(2,e)}),this.memoryHighWriter[51]=this.memoryWriter[65331]=((t,e)=>{this.writeChannel3RAM(3,e)}),this.memoryHighWriter[52]=this.memoryWriter[65332]=((t,e)=>{this.writeChannel3RAM(4,e)}),this.memoryHighWriter[53]=this.memoryWriter[65333]=((t,e)=>{this.writeChannel3RAM(5,e)}),this.memoryHighWriter[54]=this.memoryWriter[65334]=((t,e)=>{this.writeChannel3RAM(6,e)}),this.memoryHighWriter[55]=this.memoryWriter[65335]=((t,e)=>{this.writeChannel3RAM(7,e)}),this.memoryHighWriter[56]=this.memoryWriter[65336]=((t,e)=>{this.writeChannel3RAM(8,e)}),this.memoryHighWriter[57]=this.memoryWriter[65337]=((t,e)=>{this.writeChannel3RAM(9,e)}),this.memoryHighWriter[58]=this.memoryWriter[65338]=((t,e)=>{this.writeChannel3RAM(10,e)}),this.memoryHighWriter[59]=this.memoryWriter[65339]=((t,e)=>{this.writeChannel3RAM(11,e)}),this.memoryHighWriter[60]=this.memoryWriter[65340]=((t,e)=>{this.writeChannel3RAM(12,e)}),this.memoryHighWriter[61]=this.memoryWriter[65341]=((t,e)=>{this.writeChannel3RAM(13,e)}),this.memoryHighWriter[62]=this.memoryWriter[65342]=((t,e)=>{this.writeChannel3RAM(14,e)}),this.memoryHighWriter[63]=this.memoryWriter[65343]=((t,e)=>{this.writeChannel3RAM(15,e)}),this.memoryHighWriter[66]=this.memoryWriter[65346]=((t,e)=>{this.backgroundY!=e&&(this.midScanLineJIT(),this.backgroundY=e)}),this.memoryHighWriter[67]=this.memoryWriter[65347]=((t,e)=>{this.backgroundX!=e&&(this.midScanLineJIT(),this.backgroundX=e)}),this.memoryHighWriter[68]=this.memoryWriter[65348]=((t,e)=>{this.LCDisOn&&(this.modeSTAT=2,this.midScanlineOffset=-1,this.cpu.totalLinesPassed=this.currentX=this.queuedScanLines=this.lastUnrenderedLine=this.LCDTicks=this.STATTracker=this.actualScanLine=this.memory[65348]=0)}),this.memoryHighWriter[69]=this.memoryWriter[65349]=((t,e)=>{this.memory[65349]!=e&&(this.memory[65349]=e,this.LCDisOn&&this.matchLYC())}),this.memoryHighWriter[74]=this.memoryWriter[65354]=((t,e)=>{this.windowY!=e&&(this.midScanLineJIT(),this.windowY=e)}),this.memoryHighWriter[75]=this.memoryWriter[65355]=((t,e)=>{this.memory[65355]!=e&&(this.midScanLineJIT(),this.memory[65355]=e,this.windowX=e-7)}),this.memoryHighWriter[114]=this.memoryWriter[65394]=((t,e)=>{this.memory[65394]=e}),this.memoryHighWriter[115]=this.memoryWriter[65395]=((t,e)=>{this.memory[65395]=e}),this.memoryHighWriter[117]=this.memoryWriter[65397]=((t,e)=>{this.memory[65397]=e}),this.memoryHighWriter[118]=this.memoryWriter[65398]=this.onIllegalWrite,this.memoryHighWriter[119]=this.memoryWriter[65399]=this.onIllegalWrite,this.memoryHighWriter[255]=this.memoryWriter[gt]=((t,e)=>{this.interruptsEnabled=e,this.checkIRQMatching()}),this.recompileModelSpecificIOWriteHandling(),this.recompileBootIOWriteHandling()}recompileModelSpecificIOWriteHandling(){this.cartridge.useGBCMode?(this.memoryHighWriter[2]=this.memoryWriter[mt]=((t,e)=>{1==(1&e)?(this.memory[mt]=127&e,this.serialTimer=0==(2&e)?4096:128,this.serialShiftTimer=this.serialShiftTimerAllocated=0==(2&e)?512:16):(this.memory[mt]=e,this.serialShiftTimer=this.serialShiftTimerAllocated=this.serialTimer=0)}),this.memoryHighWriter[64]=this.memoryWriter[65344]=((t,e)=>{if(this.memory[65344]!=e){this.midScanLineJIT();var i=e>127;i!=this.LCDisOn&&(this.LCDisOn=i,this.memory[65345]&=120,this.midScanlineOffset=-1,this.cpu.totalLinesPassed=this.currentX=this.queuedScanLines=this.lastUnrenderedLine=this.STATTracker=this.LCDTicks=this.actualScanLine=this.memory[65348]=0,this.LCDisOn?(this.modeSTAT=2,this.matchLYC(),this.LCDCONTROL=this.LINECONTROL):(this.modeSTAT=0,this.LCDCONTROL=this.DISPLAYOFFCONTROL,this.lcdDevice.DisplayShowOff()),this.interruptsRequested&=253),this.gfxWindowCHRBankPosition=64==(64&e)?1024:0,this.gfxWindowDisplay=32==(32&e),this.gfxBackgroundBankOffset=16==(16&e)?0:128,this.gfxBackgroundCHRBankPosition=8==(8&e)?1024:0,this.gfxSpriteNormalHeight=0==(4&e),this.gfxSpriteShow=2==(2&e),this.hasBGPriority=1==(1&e),this.priorityFlaggingPathRebuild(),this.memory[65344]=e}}),this.memoryHighWriter[65]=this.memoryWriter[65345]=((t,e)=>{this.LYCMatchTriggerSTAT=64==(64&e),this.mode2TriggerSTAT=32==(32&e),this.mode1TriggerSTAT=16==(16&e),this.mode0TriggerSTAT=8==(8&e),this.memory[65345]=120&e}),this.memoryHighWriter[70]=this.memoryWriter[65350]=((t,e)=>{if(this.memory[65350]=e,e<224){e<<=8,t=65024;var i=this.modeSTAT;this.modeSTAT=0;var r=0;do{if((r=this.memoryReader[e].apply(this,[e++]))!=this.memory[t]){this.modeSTAT=i,this.graphicsJIT(),this.modeSTAT=0,this.memory[t++]=r;break}}while(++t<65184);if(t<65184)do{this.memory[t++]=this.memoryReader[e].apply(this,[e++]),this.memory[t++]=this.memoryReader[e].apply(this,[e++]),this.memory[t++]=this.memoryReader[e].apply(this,[e++]),this.memory[t++]=this.memoryReader[e].apply(this,[e++])}while(t<65184);this.modeSTAT=i}}),this.memoryHighWriter[77]=this.memoryWriter[65357]=((t,e)=>{this.memory[65357]=127&e|128&this.memory[65357]}),this.memoryHighWriter[79]=this.memoryWriter[65359]=((t,e)=>{this.currVRAMBank=1&e,this.currVRAMBank>0?this.BGCHRCurrentBank=this.BGCHRBank2:this.BGCHRCurrentBank=this.BGCHRBank1}),this.memoryHighWriter[81]=this.memoryWriter[65361]=((t,e)=>{this.hdmaRunning||(this.memory[65361]=e)}),this.memoryHighWriter[82]=this.memoryWriter[65362]=((t,e)=>{this.hdmaRunning||(this.memory[65362]=240&e)}),this.memoryHighWriter[83]=this.memoryWriter[65363]=((t,e)=>{this.hdmaRunning||(this.memory[65363]=31&e)}),this.memoryHighWriter[84]=this.memoryWriter[65364]=((t,e)=>{this.hdmaRunning||(this.memory[65364]=240&e)}),this.memoryHighWriter[85]=this.memoryWriter[65365]=((t,e)=>{this.hdmaRunning?0==(128&e)?(this.hdmaRunning=!1,this.memory[65365]|=128):this.memory[65365]=127&e:0==(128&e)?(this.DMAWrite(1+(127&e)),this.memory[65365]=255):(this.hdmaRunning=!0,this.memory[65365]=127&e)}),this.memoryHighWriter[104]=this.memoryWriter[65384]=((t,e)=>{this.memory[65385]=this.gbcBGRawPalette[63&e],this.memory[65384]=e}),this.memoryHighWriter[105]=this.memoryWriter[65385]=((t,e)=>{if(this.updateGBCBGPalette(63&this.memory[65384],e),this.memory[65384]>127){var i=this.memory[65384]+1&63;this.memory[65384]=128|i,this.memory[65385]=this.gbcBGRawPalette[i]}else this.memory[65385]=e}),this.memoryHighWriter[106]=this.memoryWriter[65386]=((t,e)=>{this.memory[65387]=this.gbcOBJRawPalette[63&e],this.memory[65386]=e}),this.memoryHighWriter[107]=this.memoryWriter[65387]=((t,e)=>{if(this.updateGBCOBJPalette(63&this.memory[65386],e),this.memory[65386]>127){var i=this.memory[65386]+1&63;this.memory[65386]=128|i,this.memory[65387]=this.gbcOBJRawPalette[i]}else this.memory[65387]=e}),this.memoryHighWriter[112]=this.memoryWriter[65392]=((t,e)=>{var i=this.memory[65361]<<8|this.memory[65362];(!this.hdmaRunning||i<53248||i>=57344)&&(this.gbcRamBank=Math.max(7&e,1),this.gbcRamBankPosition=(this.gbcRamBank-1<<12)-53248,this.gbcRamBankPositionECHO=this.gbcRamBankPosition-8192),this.memory[65392]=e}),this.memoryHighWriter[116]=this.memoryWriter[65396]=((t,e)=>{this.memory[65396]=e})):(this.memoryHighWriter[2]=this.memoryWriter[mt]=((t,e)=>{1==(1&e)?(this.memory[mt]=127&e,this.serialTimer=4096,this.serialShiftTimer=this.serialShiftTimerAllocated=512):(this.memory[mt]=e,this.serialShiftTimer=this.serialShiftTimerAllocated=this.serialTimer=0)}),this.memoryHighWriter[64]=this.memoryWriter[65344]=((t,e)=>{if(this.memory[65344]!=e){this.midScanLineJIT();var i=e>127;i!=this.LCDisOn&&(this.LCDisOn=i,this.memory[65345]&=120,this.midScanlineOffset=-1,this.cpu.totalLinesPassed=this.currentX=this.queuedScanLines=this.lastUnrenderedLine=this.STATTracker=this.LCDTicks=this.actualScanLine=this.memory[65348]=0,this.LCDisOn?(this.modeSTAT=2,this.matchLYC(),this.LCDCONTROL=this.LINECONTROL):(this.modeSTAT=0,this.LCDCONTROL=this.DISPLAYOFFCONTROL,this.lcdDevice.DisplayShowOff()),this.interruptsRequested&=253),this.gfxWindowCHRBankPosition=64==(64&e)?1024:0,this.gfxWindowDisplay=32==(32&e),this.gfxBackgroundBankOffset=16==(16&e)?0:128,this.gfxBackgroundCHRBankPosition=8==(8&e)?1024:0,this.gfxSpriteNormalHeight=0==(4&e),this.gfxSpriteShow=2==(2&e),this.bgEnabled=1==(1&e),this.memory[65344]=e}}),this.memoryHighWriter[65]=this.memoryWriter[65345]=((t,e)=>{this.LYCMatchTriggerSTAT=64==(64&e),this.mode2TriggerSTAT=32==(32&e),this.mode1TriggerSTAT=16==(16&e),this.mode0TriggerSTAT=8==(8&e),this.memory[65345]=120&e,(!this.usedBootROM||!this.usedGBCBootROM)&&this.LCDisOn&&this.modeSTAT<2&&(this.interruptsRequested|=2,this.checkIRQMatching())}),this.memoryHighWriter[70]=this.memoryWriter[65350]=((t,e)=>{if(this.memory[65350]=e,e>127&&e<224){e<<=8,t=65024;var i=this.modeSTAT;this.modeSTAT=0;var r=0;do{if((r=this.memoryReader[e].apply(this,[e++]))!=this.memory[t]){this.modeSTAT=i,this.graphicsJIT(),this.modeSTAT=0,this.memory[t++]=r;break}}while(++t<65184);if(t<65184)do{this.memory[t++]=this.memoryReader[e].apply(this,[e++]),this.memory[t++]=this.memoryReader[e].apply(this,[e++]),this.memory[t++]=this.memoryReader[e].apply(this,[e++]),this.memory[t++]=this.memoryReader[e].apply(this,[e++])}while(t<65184);this.modeSTAT=i}}),this.memoryHighWriter[71]=this.memoryWriter[65351]=((t,e)=>{this.memory[65351]!=e&&(this.midScanLineJIT(),this.updateGBBGPalette(e),this.memory[65351]=e)}),this.memoryHighWriter[72]=this.memoryWriter[65352]=((t,e)=>{this.memory[65352]!=e&&(this.midScanLineJIT(),this.updateGBOBJPalette(0,e),this.memory[65352]=e)}),this.memoryHighWriter[73]=this.memoryWriter[65353]=((t,e)=>{this.memory[65353]!=e&&(this.midScanLineJIT(),this.updateGBOBJPalette(4,e),this.memory[65353]=e)}),this.memoryHighWriter[77]=this.memoryWriter[65357]=((t,e)=>{this.memory[65357]=e}),this.memoryHighWriter[79]=this.memoryWriter[65359]=this.onIllegalWrite,this.memoryHighWriter[85]=this.memoryWriter[65365]=this.onIllegalWrite,this.memoryHighWriter[104]=this.memoryWriter[65384]=this.onIllegalWrite,this.memoryHighWriter[105]=this.memoryWriter[65385]=this.onIllegalWrite,this.memoryHighWriter[106]=this.memoryWriter[65386]=this.onIllegalWrite,this.memoryHighWriter[107]=this.memoryWriter[65387]=this.onIllegalWrite,this.memoryHighWriter[108]=this.memoryWriter[65388]=this.onIllegalWrite,this.memoryHighWriter[112]=this.memoryWriter[65392]=this.onIllegalWrite,this.memoryHighWriter[116]=this.memoryWriter[65396]=this.onIllegalWrite)}recompileBootIOWriteHandling(){this.inBootstrap?(this.memoryHighWriter[80]=this.memoryWriter[65360]=((t,e)=>{console.log("Boot ROM reads blocked: Bootstrap process has ended.",0),this.inBootstrap=!1,this.disableBootROM(),this.memory[65360]=e}),this.cartridge.useGBCMode&&(this.memoryHighWriter[108]=this.memoryWriter[65388]=((t,e)=>{this.inBootstrap&&this.cartridge.setGBCMode(e),this.memory[65388]=e}))):this.memoryHighWriter[80]=this.memoryWriter[65360]=this.onIllegalWrite}}var pt=function(t,e,i,r){return new(i||(i=Promise))(function(s,h){function a(t){try{o(r.next(t))}catch(t){h(t)}}function n(t){try{o(r.throw(t))}catch(t){h(t)}}function o(t){t.done?s(t.value):new i(function(e){e(t.value)}).then(a,n)}o((r=r.apply(t,e||[])).next())})};class Bt extends H.EventEmitter{constructor({audio:t,isPaused:e,lcd:i,isSoundEnabled:r,bootRom:h}={}){super(),this.buttons=["right","left","up","down","a","b","select","start"],"boolean"==typeof r&&(s.soundOn=r),e&&(this.isPaused=e),this.core=new Ct({audio:t,api:this,lcd:i,bootRom:h}),this.debouncedAutoSave=B(this.autoSave.bind(this),100),this.core.events.on("sramWrite",()=>{this.core.cartridge&&this.debouncedAutoSave()}),this.isOn=!1,this.actions=new G,this.registerActions(),"undefined"!=typeof document&&(this.storage=new F)}setBootRom(t){this.core.setBootRom(t)}isPaused(){return"undefined"!=typeof document&&document.hidden}setStorage(t){this.storage=t}registerActions(){this.buttons.forEach((t,e)=>{this.actions.register(t).on("down-"+t,()=>{this.core.joypad.down(e)}).on("up-"+t,()=>{this.core.joypad.up(e)})}),this.actions.register("speed").on("down-speed",t=>this.handleSpeed(t)).on("change-speed",t=>this.handleSpeed(t)).on("up-speed",()=>{this.setSpeed(1)})}handleSpeed(t){let e=2;t&&"number"==typeof t.value&&(e=2*t.value+1),this.setSpeed(e)}turnOn(){if(this.isOn)return;this.isOn=!0,this.core.start(this.cartridge),this.core.stopEmulator&=1;const t=e=>{this.isPaused()||((!this.lastRun||this.lastRunt(e),0)}}}i.d(e,"GameBoy",function(){return Bt}),i.d(e,"LocalStorage",function(){return F}),i.d(e,"util",function(){return r});e.default=Bt}])}); \ No newline at end of file +!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["jsgbc-core"]=e():t["jsgbc-core"]=e()}("undefined"!=typeof self?self:this,function(){return function(t){var e={};function i(r){if(e[r])return e[r].exports;var s=e[r]={i:r,l:!1,exports:{}};return t[r].call(s.exports,s,s.exports,i),s.l=!0,s.exports}return i.m=t,i.c=e,i.d=function(t,e,r){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)i.d(r,s,function(e){return t[e]}.bind(null,s));return r},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=3)}([function(t,e){t.exports=require("events")},function(t,e){t.exports=require("file-saver")},function(t,e){t.exports=require("jszip")},function(t,e,i){"use strict";i.r(e);var r={};i.r(r),i.d(r,"toTypedArray",function(){return c}),i.d(r,"fromTypedArray",function(){return l}),i.d(r,"getTypedArray",function(){return m}),i.d(r,"stringToArrayBuffer",function(){return u}),i.d(r,"fetchFileAsArrayBuffer",function(){return g}),i.d(r,"concatArrayBuffers",function(){return d}),i.d(r,"saveAs",function(){return y}),i.d(r,"readBlob",function(){return f}),i.d(r,"readCartridgeROM",function(){return C}),i.d(r,"hasExtension",function(){return p}),i.d(r,"debounce",function(){return B});var s={soundOn:!0,bootBootRomFirst:!0,gbHasPriority:!1,soundVolume:.7,colorizeGBMode:!0,runInterval:8,minAudioBufferSpanAmountOverXInterpreterIterations:10,maxAudioBufferSpanAmountOverXInterpreterIterations:20,alwaysAllowRWtoBanks:!1,forceGBBootRom:!1,enabledChannels:[!0,!0,!0,!0]},h=i(2),a=i.n(h),n=i(1),o=function(t,e,i,r){return new(i||(i=Promise))(function(s,h){function a(t){try{o(r.next(t))}catch(t){h(t)}}function n(t){try{o(r.throw(t))}catch(t){h(t)}}function o(t){t.done?s(t.value):new i(function(e){e(t.value)}).then(a,n)}o((r=r.apply(t,e||[])).next())})};function c(t,e){try{if(!(t&&t.length<1))return[];var i=t.length;let s;switch(e){case"uint8":s=new Uint8Array(i);break;case"int8":s=new Int8Array(i);break;case"int32":s=new Int32Array(i);break;case"float32":s=new Float32Array(i)}for(var r=0;r{if(t){const i=new FileReader;i.addEventListener("load",function(){2===this.readyState&&e(this.result)}),i.readAsArrayBuffer(t)}else i()})})}function C(t,e=""){return o(this,void 0,void 0,function*(){let i=yield f(t);if(p(e,"zip")){const t=yield a.a.loadAsync(i),e=Object.keys(t.files).filter(t=>p(t,"gbc")||p(t,"gb"));i=e.length>0?yield t.file(e[0]).async("arraybuffer"):null}return i})}function p(t,e){return t=t.toLowerCase(),e="."+e.toLowerCase(),t.lastIndexOf(e)===t.length-e.length}function B(t,e,i){var r,s,h,a,n;function o(){var c=Date.now()-a;c=0?r=setTimeout(o,e-c):(r=null,i||(n=t.apply(h,s),h=s=null))}null==e&&(e=100);var c=function(){h=this,s=arguments,a=Date.now();var c=i&&!r;return r||(r=setTimeout(o,e)),c&&(n=t.apply(h,s),h=s=null),n};return c.clear=function(){r&&(clearTimeout(r),r=null)},c.flush=function(){r&&(n=t.apply(h,s),h=s=null,clearTimeout(r),r=null)},c}var R=function(t,e,i,r){return new(i||(i=Promise))(function(s,h){function a(t){try{o(r.next(t))}catch(t){h(t)}}function n(t){try{o(r.throw(t))}catch(t){h(t)}}function o(t){t.done?s(t.value):new i(function(e){e(t.value)}).then(a,n)}o((r=r.apply(t,e||[])).next())})};class F{findState(t){return this.find("state-"+t)}findSRAM(t){return this.find("sram-"+t)}findRTC(t){return this.find("rtc-"+t)}setState(t,e){return R(this,void 0,void 0,function*(){return yield this.set("state-"+t,e)})}setSRAM(t,e){return R(this,void 0,void 0,function*(){return yield this.set("sram-"+t,e)})}setRTC(t,e){return R(this,void 0,void 0,function*(){return yield this.set("rtc-"+t,e)})}find(t){return function(t){if(!t||t.length<=0)return null;t=atob(t);const e=new Uint8Array(t.length);for(let i=0;i127,this.RTCHalt=64==(64&t),this.RTCDays=(1&t)<<8|255&this.RTCDays}writeHours(t){t<24?this.RTCHours=t:console.log("(Bank #"+this.mbc.currentMBCRAMBank+") RTC write out of range: "+t)}readSeconds(){return this.latchedSeconds}readMinutes(){return this.latchedMinutes}readHours(){return this.latchedHours}readDaysLow(){return this.latchedLDays}readDaysHigh(){return(this.RTCDayOverFlow?128:0)+(this.RTCHalt?64:0)+this.latchedHDays}writeLatch(t,e){0===e?this.RTCisLatched=!1:this.RTCisLatched||(this.RTCisLatched=!0,this.latchedSeconds=0|this.RTCSeconds,this.latchedMinutes=this.RTCMinutes,this.latchedHours=this.RTCHours,this.latchedLDays=255&this.RTCDays,this.latchedHDays=this.RTCDays>>8)}get(){const t=Math.round(this.lastTime/1e3),e=t>>0&65535,i=t>>16&65535;return new Uint32Array([this.RTCSeconds,this.RTCMinutes,this.RTCHours,this.RTCDays,this.RTCDayOverFlow,this.latchedSeconds,this.latchedMinutes,this.latchedHours,this.latchedLDays,this.latchedHDays,e,i])}load(t){const e=this.extract(t);this.RTCSeconds=e.seconds,this.RTCMinutes=e.minutes,this.RTCHours=e.hours,this.RTCDays=e.daysLow,this.RTCDayOverFlow=e.daysHigh,this.latchedSeconds=e.latchedSeconds,this.latchedMinutes=e.latchedMinutes,this.latchedHours=e.latchedHours,this.latchedLDays=e.latchedDaysLow,this.latchedHDays=e.latchedDaysHigh,this.lastTime=e.lastTime}cutBatteryFileArray(t){return new Uint32Array(t.slice(this.mbc.ramSize,this.mbc.ramSize+48))}extract(t){const e=t[0],i=t[1],r=t[2],s=t[3],h=t[4],a=t[5],n=t[6],o=t[7],c=t[8],l=t[9],m=t[10],u=t[11];let g=m;return m&&u&&(g=u<<16|m),{seconds:e,minutes:i,hours:r,daysLow:s,daysHigh:h,latchedSeconds:a,latchedMinutes:n,latchedHours:o,latchedDaysLow:c,latchedDaysHigh:l,lastTime:1e3*g}}saveState(){return[this.lastTime,this.RTCisLatched,this.latchedSeconds,this.latchedMinutes,this.latchedHours,this.latchedLDays,this.latchedHDays,this.RTCSeconds,this.RTCMinutes,this.RTCHours,this.RTCDays,this.RTCDayOverFlow,this.RTCHalt]}loadState(t){let e=0;this.lastTime=t[e++],this.RTCisLatched=t[e++],this.latchedSeconds=t[e++],this.latchedMinutes=t[e++],this.latchedHours=t[e++],this.latchedLDays=t[e++],this.latchedHDays=t[e++],this.RTCSeconds=t[e++],this.RTCMinutes=t[e++],this.RTCHours=t[e++],this.RTCDays=t[e++],this.RTCDayOverFlow=t[e++],this.RTCHalt=t[e]}updateClock(){const t=(new Date).getTime(),e=t-this.lastTime;if(this.lastTime=t,!this.RTCHalt)for(this.RTCSeconds+=e/1e3;this.RTCSeconds>=60;)this.RTCSeconds-=60,++this.RTCMinutes,this.RTCMinutes>=60&&(this.RTCMinutes-=60,++this.RTCHours,this.RTCHours>=24&&(this.RTCHours-=24,++this.RTCDays,this.RTCDays>=512&&(this.RTCDays-=512,this.RTCDayOverFlow=!0)))}}class M extends b{constructor(t){super(t),this.rtc=new A(this)}writeROMBank(t,e){this.ROMBank1Offset=127&e,this.setCurrentROMBank()}writeRAMBank(t,e){this.currentMBCRAMBank=e,e<4&&(this.currentRAMBankPosition=(this.currentMBCRAMBank<<13)-40960)}writeRAM(t,e){if(this.MBCRAMBanksEnabled||s.alwaysAllowRWtoBanks)switch(this.currentMBCRAMBank){case 0:case 1:case 2:case 3:this.emit("ramWrite"),this.RAM[t+this.currentRAMBankPosition]=e;break;case 8:this.rtc&&this.rtc.writeSeconds(e);break;case 9:this.rtc&&this.rtc.writeMinutes(e);break;case 10:this.rtc&&this.rtc.writeHours(e);break;case 11:this.rtc&&this.rtc.writeDaysLow(e);break;case 12:this.rtc&&this.rtc.writeDaysHigh(e);break;default:console.log("Invalid MBC3 bank address selected: "+this.currentMBCRAMBank)}}readRAM(t){if(this.MBCRAMBanksEnabled||s.alwaysAllowRWtoBanks)switch(this.currentMBCRAMBank){case 0:case 1:case 2:case 3:return this.RAM[t+this.currentRAMBankPosition];case 8:if(this.rtc)return this.rtc.readSeconds();break;case 9:if(this.rtc)return this.rtc.readMinutes();break;case 10:if(this.rtc)return this.rtc.readHours();break;case 11:if(this.rtc)return this.rtc.readDaysLow();break;case 12:if(this.rtc)return this.rtc.readDaysHigh()}return 255}}class T extends b{setCurrentROMBank(){this.currentROMBank=this.ROMBank1Offset%this.ROMBankEdge-1<<14}writeROMBankLow(t,e){this.ROMBank1Offset=256&this.ROMBank1Offset|e,this.setCurrentROMBank()}writeROMBankHigh(t,e){this.ROMBank1Offset=(1&e)<<8|255&this.ROMBank1Offset,this.setCurrentROMBank()}writeRAMBank(t,e){this.currentMBCRAMBank=15&e,this.currentRAMBankPosition=(this.currentMBCRAMBank<<13)-40960}}class P extends b{constructor(){super(...arguments),this.highX=127,this.lowX=127,this.highY=127,this.lowY=127}applyGyroEvent(t,e){t*=-100,t+=2047,this.highX=t>>8,this.lowX=255&t,e*=-100,e+=2047,this.highY=e>>8,this.lowY=255&e}read(t){if(this.MBCRAMBanksEnabled||s.alwaysAllowRWtoBanks)switch(t){case 40960:case 41056:case 41072:case 41088:return 0;case 41040:return this.highY;case 41024:return this.lowY;case 41008:return this.highX;case 40992:return this.lowX;default:return this.RAM[t+this.currentRAMBankPosition]}return 255}}class v extends T{writeRAMBank(t,e){8&e&&this.emit("rumble"),e&=7,super.writeRAMBank(t,e)}}const w="Game and Watch 50";class W{constructor(t){this.hasMBC1=!1,this.hasMBC2=!1,this.hasMBC3=!1,this.hasMBC5=!1,this.hasMBC7=!1,this.hasSRAM=!1,this.hasRUMBLE=!1,this.hasCamera=!1,this.hasTAMA5=!1,this.hasHuC3=!1,this.hasHuC1=!1,this.hasMMMO1=!1,this.hasRTC=!1,this.hasBattery=!1,this.rom=t instanceof S?t:new S(t)}connect(t){this.gameboy=t}interpret(){if(this.name=this.rom.getString(308,318),this.gameCode=this.rom.getString(319,322),this.colorCompatibilityByte=this.rom.getByte(323),this.type=this.rom.getByte(327),this.setTypeName(),this.name&&console.log("Game Title: "+this.name),this.gameCode&&console.log("Game Code: "+this.gameCode),this.colorCompatibilityByte&&console.log("Color Compatibility Byte: "+this.colorCompatibilityByte),this.type&&console.log("Cartridge Type: "+this.type),this.typeName&&console.log("Cartridge Type Name: "+this.typeName),this.romSizeType=this.rom.getByte(328),this.ramSizeType=this.rom.getByte(329),this.gameboy.usedBootROM)console.log("used boot rom"),this.useGBCMode=this.gameboy.usedGBCBootROM;else switch(this.colorCompatibilityByte){case 0:this.useGBCMode=!1;break;case 50:s.gbHasPriority||this.name+this.gameCode+this.colorCompatibilityByte!==w?this.useGBCMode=!1:(this.useGBCMode=!0,console.log("Created a boot exception for Game and Watch Gallery 2 (GBC ID byte is wrong on the cartridge)."));break;case 128:this.useGBCMode=!s.gbHasPriority;break;case 192:this.useGBCMode=!0;break;default:this.useGBCMode=!1,console.warn("Unknown GameBoy game type code #"+this.colorCompatibilityByte+", defaulting to GB mode (Old games don't have a type code).")}const t=this.rom.getByte(331),e=65280&this.rom.getByte(324)|255&this.rom.getByte(325);51!==t?(this.hasNewLicenseCode=!1,this.licenseCode=t):(this.hasNewLicenseCode=!0,this.licenseCode=e)}setGBCMode(t){this.useGBCMode=0==(1&t),this.name+this.gameCode+this.colorCompatibilityByte===w&&(this.useGBCMode=!0,console.log("Created a boot exception for Game and Watch Gallery 2 (GBC ID byte is wrong on the cartridge).")),console.log("Booted to GBC Mode: "+this.useGBCMode)}setTypeName(){switch(this.type){case 0:this.typeName="ROM";break;case 1:this.hasMBC1=!0,this.typeName="MBC1";break;case 2:this.hasMBC1=!0,this.hasSRAM=!0,this.typeName="MBC1 + SRAM";break;case 3:this.hasMBC1=!0,this.hasSRAM=!0,this.hasBattery=!0,this.typeName="MBC1 + SRAM + Battery";break;case 5:this.hasMBC2=!0,this.typeName="MBC2";break;case 6:this.hasMBC2=!0,this.hasBattery=!0,this.typeName="MBC2 + Battery";break;case 8:this.hasSRAM=!0,this.typeName="ROM + SRAM";break;case 9:this.hasSRAM=!0,this.hasBattery=!0,this.typeName="ROM + SRAM + Battery";break;case 11:this.hasMMMO1=!0,this.typeName="MMMO1";break;case 12:this.hasMMMO1=!0,this.hasSRAM=!0,this.typeName="MMMO1 + SRAM";break;case 13:this.hasMMMO1=!0,this.hasSRAM=!0,this.hasBattery=!0,this.typeName="MMMO1 + SRAM + Battery";break;case 15:this.hasMBC3=!0,this.hasRTC=!0,this.hasBattery=!0,this.typeName="MBC3 + RTC + Battery";break;case 16:this.hasMBC3=!0,this.hasRTC=!0,this.hasBattery=!0,this.hasSRAM=!0,this.typeName="MBC3 + RTC + Battery + SRAM";break;case 17:this.hasMBC3=!0,this.typeName="MBC3";break;case 18:this.hasMBC3=!0,this.hasSRAM=!0,this.typeName="MBC3 + SRAM";break;case 19:this.hasMBC3=!0,this.hasSRAM=!0,this.hasBattery=!0,this.typeName="MBC3 + SRAM + Battery";break;case 25:this.hasMBC5=!0,this.typeName="MBC5";break;case 26:this.hasMBC5=!0,this.hasSRAM=!0,this.typeName="MBC5 + SRAM";break;case 27:this.hasMBC5=!0,this.hasSRAM=!0,this.hasBattery=!0,this.typeName="MBC5 + SRAM + Battery";break;case 28:this.hasRUMBLE=!0,this.typeName="RUMBLE";break;case 29:this.hasRUMBLE=!0,this.hasSRAM=!0,this.typeName="RUMBLE + SRAM";break;case 30:this.hasRUMBLE=!0,this.hasSRAM=!0,this.hasBattery=!0,this.typeName="RUMBLE + SRAM + Battery";break;case 31:this.hasCamera=!0,this.typeName="GameBoy Camera";break;case 34:this.hasMBC7=!0,this.hasSRAM=!0,this.hasBattery=!0,this.typeName="MBC7 + SRAM + Battery";break;case 253:this.hasTAMA5=!0,this.typeName="TAMA5";break;case 254:this.hasHuC3=!0,this.typeName="HuC3";break;case 255:this.hasHuC1=!0,this.typeName="HuC1";break;default:throw new Error("Unknown Cartridge Type")}this.hasMBC1&&(this.mbc1=new k(this)),this.hasMBC2&&(this.mbc2=new L(this)),this.hasMBC3&&(this.mbc3=new M(this)),this.hasMBC5&&(this.mbc5=new T(this)),this.hasMBC7&&(this.mbc7=new P(this)),this.hasRUMBLE&&(this.mbc5=this.rumble=new v(this)),this.mbc=this.mbc1||this.mbc2||this.mbc3||this.mbc5||this.mbc7||this.rumble||null}setupRAM(){this.mbc&&this.mbc.setupRAM(),this.gameboy.api.loadSRAM(),this.gameboy.api.loadRTC()}}class G extends H.EventEmitter{constructor(){super(...arguments),this.map={}}register(t){return this.map[t]=!0,this}getAll(){return Object.keys(this.map)}is(t){return!!this.map[t]}down(t,e){this.emit("down-"+t,e)}change(t,e){this.emit("change-"+t,e)}up(t,e){this.emit("up-"+t,e)}}var O=[4,12,8,8,4,4,8,4,20,8,8,8,4,4,8,4,4,12,8,8,4,4,8,4,12,8,8,8,4,4,8,4,8,12,8,8,4,4,8,4,8,8,8,8,4,4,8,4,8,12,8,8,12,12,12,4,8,8,8,8,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,8,8,8,8,8,8,4,8,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,4,4,4,4,4,4,8,4,8,12,12,16,12,16,8,16,8,16,12,0,12,24,8,16,8,12,12,4,12,16,8,16,8,16,12,4,12,4,8,16,12,12,8,4,4,16,8,16,16,4,16,4,4,4,8,16,12,12,8,4,4,16,8,16,12,8,16,4,0,4,8,16];class E{constructor(t,e,i,r,s){this.fromSampleRate=t,this.toSampleRate=e,this.channels=0|i,this.outputBufferSize=r,this.noReturn=!!s,this.initialize()}initialize(){if(!(this.fromSampleRate>0&&this.toSampleRate>0&&this.channels>0))throw new Error("Invalid settings specified for the resampler.");this.fromSampleRate===this.toSampleRate?(this.resampler=this.bypassResampler,this.ratioWeight=1):(this.ratioWeight=this.fromSampleRate/this.toSampleRate,this.fromSampleRate 0) { \t\t\tvar weight = this.lastWeight; \t\t\tvar firstWeight = 0; \t\t\tvar secondWeight = 0; \t\t\tvar sourceOffset = 0; \t\t\tvar outputOffset = 0; \t\t\tvar outputBuffer = this.outputBuffer; \t\t\tfor (; weight < 1; weight += "+this.ratioWeight+") { \t\t\t\tsecondWeight = weight % 1; \t\t\t\tfirstWeight = 1 - secondWeight;";for(let e=0;e0?" + "+e:"")+"] * firstWeight) + (buffer[sourceOffset + "+(this.channels+e)+"] * secondWeight);";t+="weight += "+this.ratioWeight+"; \t\t\t\tsourceOffset = Math.floor(weight) * "+this.channels+"; \t\t\t}";for(let e=0;e 0) { \t\t\tvar weight = 0;";for(let e=0;e0?" + "+e:"")+"] * weight;";t+="currentPosition += weight; \t\t\t\t\t\tweight = 0; \t\t\t\t\t\tbreak; \t\t\t\t\t} \t\t\t\t} \t\t\t\tif (weight <= 0) {";for(let e=0;ethis.minBufferSize+this.channelsAllocated?t&-this.channelsAllocated:this.minBufferSize*this.channelsAllocated}writeAudio(t){let e=0;for(;ethis.processAudio(t)),this.audioNode.connect(this.context.destination),this.resetAudioBuffer(this.context.sampleRate))})}processAudio(t){const e=[];let i=0;for(;i0){const t=this.resampleControl.resampler(this.getBufferSamples()),e=this.resampleControl.outputBuffer;for(let i=0;i0;){for(e=Math.min(this.audioClocksUntilNextEventCounter,this.sequencerClocks,t),this.audioClocksUntilNextEventCounter-=e,this.sequencerClocks-=e,t-=e;e>0;){const t=Math.min(e,this.resamplerFirstPassFactor-this.audioIndex);e-=t,this.audioIndex+=t,this.downsampleInput+=this.mixerOutputCache*t,this.audioIndex===this.resamplerFirstPassFactor&&(this.audioIndex=0,this.outputAudio())}0===this.sequencerClocks&&(this.audioComputeSequencer(),this.sequencerClocks=8192),0===this.audioClocksUntilNextEventCounter&&this.computeChannels()}else for(;t>0;){const e=Math.min(t,this.resamplerFirstPassFactor-this.audioIndex);t-=e,this.audioIndex+=e,this.audioIndex===this.resamplerFirstPassFactor&&(this.audioIndex=0,this.outputAudio())}}generateFake(t){if(this.gameboy.soundMasterEnabled&&!this.gameboy.CPUStopped){let e=0;for(;t>0;)e=Math.min(this.audioClocksUntilNextEventCounter,this.sequencerClocks,t),this.audioClocksUntilNextEventCounter-=e,this.sequencerClocks-=e,t-=e,0===this.sequencerClocks&&(this.audioComputeSequencer(),this.sequencerClocks=8192),0===this.audioClocksUntilNextEventCounter&&this.computeChannels()}}runJIT(){s.soundOn?this.generate(this.audioTicks):this.generateFake(this.audioTicks),this.audioTicks=0}clockAudioEnvelope(){this.channel1envelopeSweepsLast>-1&&(this.channel1envelopeSweeps>0?--this.channel1envelopeSweeps:this.channel1envelopeType?this.channel1envelopeVolume<15?(++this.channel1envelopeVolume,this.channel1envelopeSweeps=this.channel1envelopeSweepsLast,this.cacheChannel1OutputLevel()):this.channel1envelopeSweepsLast=-1:this.channel1envelopeVolume>0?(--this.channel1envelopeVolume,this.channel1envelopeSweeps=this.channel1envelopeSweepsLast,this.cacheChannel1OutputLevel()):this.channel1envelopeSweepsLast=-1),this.channel2envelopeSweepsLast>-1&&(this.channel2envelopeSweeps>0?--this.channel2envelopeSweeps:this.channel2envelopeType?this.channel2envelopeVolume<15?(++this.channel2envelopeVolume,this.channel2envelopeSweeps=this.channel2envelopeSweepsLast,this.cacheChannel2OutputLevel()):this.channel2envelopeSweepsLast=-1:this.channel2envelopeVolume>0?(--this.channel2envelopeVolume,this.channel2envelopeSweeps=this.channel2envelopeSweepsLast,this.cacheChannel2OutputLevel()):this.channel2envelopeSweepsLast=-1),this.channel4envelopeSweepsLast>-1&&(this.channel4envelopeSweeps>0?--this.channel4envelopeSweeps:this.channel4envelopeType?this.channel4envelopeVolume<15?(this.channel4currentVolume=++this.channel4envelopeVolume<0?(this.channel4currentVolume=--this.channel4envelopeVolume<0&&!this.channel1decreaseSweep){const t=this.channel1ShadowFrequency+(this.channel1ShadowFrequency>>this.channel1frequencySweepDivider);t<=2047?t+(t>>this.channel1frequencySweepDivider)>2047&&(this.channel1SweepFault=!0,this.checkChannel1Enable(),this.memory[65318]&=254):(this.channel1SweepFault=!0,this.checkChannel1Enable(),this.memory[65318]&=254)}}audioComputeSequencer(){switch(this.sequencePosition++){case 0:this.clockAudioLength();break;case 2:this.clockAudioLength(),this.clockAudioSweep();break;case 4:this.clockAudioLength();break;case 6:this.clockAudioLength(),this.clockAudioSweep();break;case 7:this.clockAudioEnvelope(),this.sequencePosition=0}}clockAudioLength(){this.channel1totalLength>1?--this.channel1totalLength:1===this.channel1totalLength&&(this.channel1totalLength=0,this.checkChannel1Enable(),this.memory[65318]&=254),this.channel2totalLength>1?--this.channel2totalLength:1===this.channel2totalLength&&(this.channel2totalLength=0,this.checkChannel2Enable(),this.memory[65318]&=253),this.channel3totalLength>1?--this.channel3totalLength:1===this.channel3totalLength&&(this.channel3totalLength=0,this.checkChannel3Enable(),this.memory[65318]&=251),this.channel4totalLength>1?--this.channel4totalLength:1===this.channel4totalLength&&(this.channel4totalLength=0,this.checkChannel4Enable(),this.memory[65318]&=247)}clockAudioSweep(){!this.channel1SweepFault&&this.channel1timeSweep>0&&0==--this.channel1timeSweep&&this.runAudioSweep()}runAudioSweep(){this.channel1lastTimeSweep>0&&(this.channel1frequencySweepDivider>0?(this.channel1Swept=!0,this.channel1decreaseSweep?(this.channel1ShadowFrequency-=this.channel1ShadowFrequency>>this.channel1frequencySweepDivider,this.channel1frequency=2047&this.channel1ShadowFrequency,this.channel1FrequencyTracker=2048-this.channel1frequency<<2):(this.channel1ShadowFrequency+=this.channel1ShadowFrequency>>this.channel1frequencySweepDivider,this.channel1frequency=this.channel1ShadowFrequency,this.channel1ShadowFrequency<=2047?(this.channel1FrequencyTracker=2048-this.channel1frequency<<2,this.channel1ShadowFrequency+(this.channel1ShadowFrequency>>this.channel1frequencySweepDivider)>2047&&(this.channel1SweepFault=!0,this.checkChannel1Enable(),this.memory[65318]&=254)):(this.channel1frequency&=2047,this.channel1SweepFault=!0,this.checkChannel1Enable(),this.memory[65318]&=254)),this.channel1timeSweep=this.channel1lastTimeSweep):(this.channel1SweepFault=!0,this.checkChannel1Enable()))}computeChannels(){this.channel1FrequencyCounter-=this.audioClocksUntilNextEvent,this.channel2FrequencyCounter-=this.audioClocksUntilNextEvent,this.channel3Counter-=this.audioClocksUntilNextEvent,this.channel4Counter-=this.audioClocksUntilNextEvent,0===this.channel1FrequencyCounter&&(this.channel1FrequencyCounter=this.channel1FrequencyTracker,this.channel1DutyTracker=this.channel1DutyTracker+1&7,this.cacheChannel1OutputLevelTrimary()),0===this.channel2FrequencyCounter&&(this.channel2FrequencyCounter=this.channel2FrequencyTracker,this.channel2DutyTracker=this.channel2DutyTracker+1&7,this.cacheChannel2OutputLevelTrimary()),0===this.channel3Counter&&(this.channel3canPlay&&(this.channel3lastSampleLookup=this.channel3lastSampleLookup+1&31),this.channel3Counter=this.channel3FrequencyPeriod,this.cacheChannel3Update()),0===this.channel4Counter&&(this.channel4lastSampleLookup=this.channel4lastSampleLookup+1&this.channel4BitRange,this.channel4Counter=this.channel4FrequencyPeriod,this.cacheChannel4Update()),this.audioClocksUntilNextEventCounter=this.audioClocksUntilNextEvent=Math.min(this.channel1FrequencyCounter,this.channel2FrequencyCounter,this.channel3Counter,this.channel4Counter)}checkChannel1Enable(){this.channel1Enabled=(this.channel1consecutive||this.channel1totalLength>0)&&!this.channel1SweepFault&&this.channel1canPlay,this.cacheChannel1OutputLevelSecondary()}cacheChannel1OutputLevel(){this.channel1currentSampleLeft=this.leftChannel1?this.channel1envelopeVolume:0,this.channel1currentSampleRight=this.rightChannel1?this.channel1envelopeVolume:0,this.cacheChannel1OutputLevelSecondary()}checkChannel1VolumeEnable(){this.channel1canPlay=this.memory[65298]>7,this.checkChannel1Enable(),this.cacheChannel1OutputLevelSecondary()}cacheChannel1OutputLevelSecondary(){this.channel1Enabled?(this.channel1currentSampleLeftSecondary=this.channel1currentSampleLeft,this.channel1currentSampleRightSecondary=this.channel1currentSampleRight):(this.channel1currentSampleLeftSecondary=0,this.channel1currentSampleRightSecondary=0),this.cacheChannel1OutputLevelTrimary()}cacheChannel1OutputLevelTrimary(){this.channel1CachedDuty[this.channel1DutyTracker]&&s.enabledChannels[0]?(this.channel1currentSampleLeftTrimary=this.channel1currentSampleLeftSecondary,this.channel1currentSampleRightTrimary=this.channel1currentSampleRightSecondary):(this.channel1currentSampleLeftTrimary=0,this.channel1currentSampleRightTrimary=0),this.cacheMixerOutputLevel()}checkChannel2Enable(){this.channel2Enabled=(this.channel2consecutive||this.channel2totalLength>0)&&this.channel2canPlay,this.cacheChannel2OutputLevelSecondary()}cacheChannel2OutputLevel(){this.channel2currentSampleLeft=this.leftChannel2?this.channel2envelopeVolume:0,this.channel2currentSampleRight=this.rightChannel2?this.channel2envelopeVolume:0,this.cacheChannel2OutputLevelSecondary()}checkChannel2VolumeEnable(){this.channel2canPlay=this.memory[65303]>7,this.checkChannel2Enable(),this.cacheChannel2OutputLevelSecondary()}cacheChannel2OutputLevelSecondary(){this.channel2Enabled?(this.channel2currentSampleLeftSecondary=this.channel2currentSampleLeft,this.channel2currentSampleRightSecondary=this.channel2currentSampleRight):(this.channel2currentSampleLeftSecondary=0,this.channel2currentSampleRightSecondary=0),this.cacheChannel2OutputLevelTrimary()}cacheChannel2OutputLevelTrimary(){this.channel2CachedDuty[this.channel2DutyTracker]&&s.enabledChannels[1]?(this.channel2currentSampleLeftTrimary=this.channel2currentSampleLeftSecondary,this.channel2currentSampleRightTrimary=this.channel2currentSampleRightSecondary):(this.channel2currentSampleLeftTrimary=0,this.channel2currentSampleRightTrimary=0),this.cacheMixerOutputLevel()}cacheChannel3Update(){this.cachedChannel3Sample=this.channel3PCM[this.channel3lastSampleLookup]>>this.channel3patternType,this.cacheChannel3OutputLevel()}checkChannel3Enable(){this.channel3Enabled=this.channel3consecutive||this.channel3totalLength>0,this.channel3OutputLevelSecondaryCache()}cacheChannel3OutputLevel(){this.channel3currentSampleLeft=this.leftChannel3?this.cachedChannel3Sample:0,this.channel3currentSampleRight=this.rightChannel3?this.cachedChannel3Sample:0,this.channel3OutputLevelSecondaryCache()}channel3OutputLevelSecondaryCache(){this.channel3Enabled&&s.enabledChannels[2]?(this.channel3currentSampleLeftSecondary=this.channel3currentSampleLeft,this.channel3currentSampleRightSecondary=this.channel3currentSampleRight):(this.channel3currentSampleLeftSecondary=0,this.channel3currentSampleRightSecondary=0),this.cacheMixerOutputLevel()}checkChannel4Enable(){this.channel4Enabled=(this.channel4consecutive||this.channel4totalLength>0)&&this.channel4canPlay,this.cacheChannel4OutputLevelSecondary()}cacheChannel4Update(){this.cachedChannel4Sample=this.noiseSampleTable[this.channel4currentVolume|this.channel4lastSampleLookup],this.cacheChannel4OutputLevel()}cacheChannel4OutputLevel(){this.channel4currentSampleLeft=this.leftChannel4?this.cachedChannel4Sample:0,this.channel4currentSampleRight=this.rightChannel4?this.cachedChannel4Sample:0,this.cacheChannel4OutputLevelSecondary()}checkChannel4VolumeEnable(){this.channel4canPlay=this.memory[65313]>7,this.checkChannel4Enable(),this.cacheChannel4OutputLevelSecondary()}cacheChannel4OutputLevelSecondary(){this.channel4Enabled&&s.enabledChannels[3]?(this.channel4currentSampleLeftSecondary=this.channel4currentSampleLeft,this.channel4currentSampleRightSecondary=this.channel4currentSampleRight):(this.channel4currentSampleLeftSecondary=0,this.channel4currentSampleRightSecondary=0),this.cacheMixerOutputLevel()}cacheMixerOutputLevel(){const t=this.channel1currentSampleLeftTrimary+this.channel2currentSampleLeftTrimary+this.channel3currentSampleLeftSecondary+this.channel4currentSampleLeftSecondary,e=this.channel1currentSampleRightTrimary+this.channel2currentSampleRightTrimary+this.channel3currentSampleRightSecondary+this.channel4currentSampleRightSecondary;this.mixerOutputCache=t*this.VinLeftChannelMasterVolume<<16|e*this.VinRightChannelMasterVolume}connectDevice(t){this.resamplerFirstPassFactor=Math.max(Math.min(Math.floor(this.cpu.clocksPerSecond/44100),Math.floor(136.53125)),1),this.downSampleInputDivider=1/(240*this.resamplerFirstPassFactor);const e=this.cpu.clocksPerSecond/this.resamplerFirstPassFactor,i=Math.max(this.cpu.baseCyclesPerIteration*s.maxAudioBufferSpanAmountOverXInterpreterIterations/this.resamplerFirstPassFactor,8192)<<1;t.setSampleRate(e),t.setMaxBufferSize(i),t.initializeAudio(),this.device=t}setVolume(t){this.device&&this.device.setVolume(t)}adjustUnderrun(){if(!s.soundOn)return;let t=this.device.remainingBuffer();"number"==typeof t&&(t=this.bufferContainAmount-Math.max(t,0))>0&&this.recalculateIterationClockLimitForAudio((t>>1)*this.resamplerFirstPassFactor)}recalculateIterationClockLimitForAudio(t){this.cpu.cyclesTotal+=Math.min(t>>2<<2,this.cpu.cyclesTotalBase<<1)}outputAudio(){this.fillBuffer(),this.bufferPosition===this.bufferLength&&(this.device.writeAudio(this.buffer),this.bufferPosition=0),this.downsampleInput=0}fillBuffer(){this.buffer[this.bufferPosition++]=(this.downsampleInput>>>16)*this.downSampleInputDivider-1,this.buffer[this.bufferPosition++]=(65535&this.downsampleInput)*this.downSampleInputDivider-1}initBuffer(){this.audioIndex=0,this.bufferPosition=0,this.downsampleInput=0,this.bufferContainAmount=Math.max(this.cpu.baseCyclesPerIteration*s.minAudioBufferSpanAmountOverXInterpreterIterations/this.resamplerFirstPassFactor,4096)<<1,this.bufferLength=this.cpu.baseCyclesPerIteration/this.resamplerFirstPassFactor<<1,this.buffer=m(this.bufferLength,0,"float32")}generateWhiteNoise(){this.LSFR7Table=this.generateLSFR7Table(),this.LSFR15Table=this.generateLSFR15Table(),this.noiseSampleTable=this.LSFR15Table}generateLSFR7Table(){const t=m(2048,0,"int8");let e=127;for(let i=0;i<128;++i){const r=1-(1&e);t[128|i]=r,t[256|i]=2*r,t[384|i]=3*r,t[512|i]=4*r,t[640|i]=5*r,t[768|i]=6*r,t[896|i]=7*r,t[1024|i]=8*r,t[1152|i]=9*r,t[1280|i]=10*r,t[1408|i]=11*r,t[1536|i]=12*r,t[1664|i]=13*r,t[1792|i]=14*r,t[1920|i]=15*r;const s=e>>1;e=s|(1&(s^e))<<6}return t}generateLSFR15Table(){const t=m(524288,0,"int8");let e=32767;for(let i=0;i<32768;++i){const r=1-(1&e);t[32768|i]=r,t[65536|i]=2*r,t[98304|i]=3*r,t[131072|i]=4*r,t[163840|i]=5*r,t[196608|i]=6*r,t[229376|i]=7*r,t[262144|i]=8*r,t[294912|i]=9*r,t[327680|i]=10*r,t[360448|i]=11*r,t[393216|i]=12*r,t[425984|i]=13*r,t[458752|i]=14*r,t[491520|i]=15*r;const s=e>>1;e=s|(1&(s^e))<<14}return t}}class q{constructor({canvas:t,context:e,offscreenCanvas:i,offscreenContext:r,gameboy:s,width:h,height:a}){if(this.canvas=t,this.context=e,this.offscreenCanvas=i,this.offscreenContext=r,this.gameboy=s,this.offscreenWidth=160,this.offscreenHeight=144,this.offscreenRGBCount=this.offscreenWidth*this.offscreenHeight*3,this.offscreenRGBACount=this.offscreenWidth*this.offscreenHeight*4,this.width=h||this.offscreenWidth,this.height=a||this.offscreenHeight,this.swizzledFrame=null,this.canvasBuffer=null,this.resizePathClear=!0,"undefined"!=typeof document&&(this.canvas||(this.canvas=document.createElement("canvas")),this.offscreenCanvas||(this.offscreenCanvas=document.createElement("canvas"))),this.canvas&&(this.canvas.height=this.height,this.canvas.width=this.width,this.context||(this.context=this.canvas.getContext("2d"))),this.offscreenCanvas&&(this.offscreenCanvas.height=this.offscreenHeight,this.offscreenCanvas.width=this.offscreenWidth,this.offscreenContext||(this.offscreenContext=this.offscreenCanvas.getContext("2d"))),!this.context)throw new Error("please provide a canvas context in the lcd options");if(!this.offscreenContext)throw new Error("please provide a canvas offscreen context in the lcd options")}init(){this.offscreenContext.msImageSmoothingEnabled=!1,this.offscreenContext.mozImageSmoothingEnabled=!1,this.offscreenContext.webkitImageSmoothingEnabled=!1,this.offscreenContext.imageSmoothingEnabled=!1,this.context.msImageSmoothingEnabled=!1,this.context.mozImageSmoothingEnabled=!1,this.context.webkitImageSmoothingEnabled=!1,this.context.imageSmoothingEnabled=!1,this.canvasBuffer=this.offscreenContext.createImageData(this.offscreenWidth,this.offscreenHeight);let t=this.offscreenRGBACount;for(;t>0;)t-=4,this.canvasBuffer.data[t]=248,this.canvasBuffer.data[t+1]=248,this.canvasBuffer.data[t+2]=248,this.canvasBuffer.data[t+3]=255;this.graphicsBlit(),this.swizzledFrame||(this.swizzledFrame=m(this.offscreenRGBCount,255,"uint8")),this.drewFrame=!0,this.requestDraw()}graphicsBlit(){this.offscreenWidth===this.width&&this.offscreenHeight===this.height?this.context.putImageData(this.canvasBuffer,0,0):(this.offscreenContext.putImageData(this.canvasBuffer,0,0),this.context.drawImage(this.offscreenCanvas,0,0,this.width,this.height))}requestDraw(){this.drewFrame&&this.dispatchDraw()}dispatchDraw(){this.offscreenRGBACount>0&&92160===this.offscreenRGBACount&&this.processDraw(this.swizzledFrame)}resizeFrameBuffer(){this.resizePathClear&&(this.resizePathClear=!1,this.resizer.resize(this.swizzledFrame))}processDraw(t){const e=this.canvasBuffer.data;let i=0,r=0;for(;r>16&255,e[r++]=t[i]>>8&255,e[r++]=255&t[i],++i}DisplayShowOff(){0===this.drewBlank&&(this.clearFrameBuffer(),this.drewFrame=!0),this.drewBlank=2}clearFrameBuffer(){const t=this.swizzledFrame;let e=0;if(this.gameboy.cartridge.useGBCMode||this.colorizedGBPalettes)for(;e127,this.registerB=this.registerB<<1&255|(this.FCarry?1:0),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){this.FCarry=this.registerC>127,this.registerC=this.registerC<<1&255|(this.FCarry?1:0),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){this.FCarry=this.registerD>127,this.registerD=this.registerD<<1&255|(this.FCarry?1:0),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){this.FCarry=this.registerE>127,this.registerE=this.registerE<<1&255|(this.FCarry?1:0),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){this.FCarry=this.registersHL>32767,this.registersHL=this.registersHL<<1&65024|(this.FCarry?256:0)|255&this.registersHL,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){this.FCarry=128==(128&this.registersHL),this.registersHL=65280&this.registersHL|this.registersHL<<1&255|(this.FCarry?1:0),this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL);this.FCarry=t>127,t=t<<1&255|(this.FCarry?1:0),this.memoryWriter[this.registersHL](this.registersHL,t),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===t},function(){this.FCarry=this.registerA>127,this.registerA=this.registerA<<1&255|(this.FCarry?1:0),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){this.FCarry=1==(1&this.registerB),this.registerB=(this.FCarry?128:0)|this.registerB>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){this.FCarry=1==(1&this.registerC),this.registerC=(this.FCarry?128:0)|this.registerC>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){this.FCarry=1==(1&this.registerD),this.registerD=(this.FCarry?128:0)|this.registerD>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){this.FCarry=1==(1&this.registerE),this.registerE=(this.FCarry?128:0)|this.registerE>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){this.FCarry=256==(256&this.registersHL),this.registersHL=(this.FCarry?32768:0)|this.registersHL>>1&65280|255&this.registersHL,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){this.FCarry=1==(1&this.registersHL),this.registersHL=65280&this.registersHL|(this.FCarry?128:0)|(255&this.registersHL)>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL);this.FCarry=1==(1&t),t=(this.FCarry?128:0)|t>>1,this.memoryWriter[this.registersHL](this.registersHL,t),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===t},function(){this.FCarry=1==(1&this.registerA),this.registerA=(this.FCarry?128:0)|this.registerA>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){var t=this.registerB>127;this.registerB=this.registerB<<1&255|(this.FCarry?1:0),this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){var t=this.registerC>127;this.registerC=this.registerC<<1&255|(this.FCarry?1:0),this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){var t=this.registerD>127;this.registerD=this.registerD<<1&255|(this.FCarry?1:0),this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){var t=this.registerE>127;this.registerE=this.registerE<<1&255|(this.FCarry?1:0),this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){var t=this.registersHL>32767;this.registersHL=this.registersHL<<1&65024|(this.FCarry?256:0)|255&this.registersHL,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){var t=128==(128&this.registersHL);this.registersHL=65280&this.registersHL|this.registersHL<<1&255|(this.FCarry?1:0),this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL),e=t>127;t=t<<1&255|(this.FCarry?1:0),this.FCarry=e,this.memoryWriter[this.registersHL](this.registersHL,t),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===t},function(){var t=this.registerA>127;this.registerA=this.registerA<<1&255|(this.FCarry?1:0),this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){var t=1==(1&this.registerB);this.registerB=(this.FCarry?128:0)|this.registerB>>1,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){var t=1==(1&this.registerC);this.registerC=(this.FCarry?128:0)|this.registerC>>1,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){var t=1==(1&this.registerD);this.registerD=(this.FCarry?128:0)|this.registerD>>1,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){var t=1==(1&this.registerE);this.registerE=(this.FCarry?128:0)|this.registerE>>1,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){var t=256==(256&this.registersHL);this.registersHL=(this.FCarry?32768:0)|this.registersHL>>1&65280|255&this.registersHL,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){var t=1==(1&this.registersHL);this.registersHL=65280&this.registersHL|(this.FCarry?128:0)|(255&this.registersHL)>>1,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL),e=1==(1&t);t=(this.FCarry?128:0)|t>>1,this.FCarry=e,this.memoryWriter[this.registersHL](this.registersHL,t),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===t},function(){var t=1==(1&this.registerA);this.registerA=(this.FCarry?128:0)|this.registerA>>1,this.FCarry=t,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){this.FCarry=this.registerB>127,this.registerB=this.registerB<<1&255,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){this.FCarry=this.registerC>127,this.registerC=this.registerC<<1&255,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){this.FCarry=this.registerD>127,this.registerD=this.registerD<<1&255,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){this.FCarry=this.registerE>127,this.registerE=this.registerE<<1&255,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){this.FCarry=this.registersHL>32767,this.registersHL=this.registersHL<<1&65024|255&this.registersHL,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){this.FCarry=128==(128&this.registersHL),this.registersHL=65280&this.registersHL|this.registersHL<<1&255,this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL);this.FCarry=t>127,t=t<<1&255,this.memoryWriter[this.registersHL](this.registersHL,t),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===t},function(){this.FCarry=this.registerA>127,this.registerA=this.registerA<<1&255,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){this.FCarry=1==(1&this.registerB),this.registerB=128&this.registerB|this.registerB>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){this.FCarry=1==(1&this.registerC),this.registerC=128&this.registerC|this.registerC>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){this.FCarry=1==(1&this.registerD),this.registerD=128&this.registerD|this.registerD>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){this.FCarry=1==(1&this.registerE),this.registerE=128&this.registerE|this.registerE>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){this.FCarry=256==(256&this.registersHL),this.registersHL=this.registersHL>>1&65280|33023&this.registersHL,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){this.FCarry=1==(1&this.registersHL),this.registersHL=65408&this.registersHL|(255&this.registersHL)>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL);this.FCarry=1==(1&t),t=128&t|t>>1,this.memoryWriter[this.registersHL](this.registersHL,t),this.FHalfCarry=this.FSubtract=!1,this.FZero=0===t},function(){this.FCarry=1==(1&this.registerA),this.registerA=128&this.registerA|this.registerA>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){this.registerB=(15&this.registerB)<<4|this.registerB>>4,this.FZero=0===this.registerB,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.registerC=(15&this.registerC)<<4|this.registerC>>4,this.FZero=0===this.registerC,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.registerD=(15&this.registerD)<<4|this.registerD>>4,this.FZero=0===this.registerD,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.registerE=(15&this.registerE)<<4|this.registerE>>4,this.FZero=0===this.registerE,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.registersHL=(3840&this.registersHL)<<4|(61440&this.registersHL)>>4|255&this.registersHL,this.FZero=this.registersHL<256,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.registersHL=65280&this.registersHL|(15&this.registersHL)<<4|(240&this.registersHL)>>4,this.FZero=0==(255&this.registersHL),this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){var t=this.memoryReader[this.registersHL](this.registersHL);t=(15&t)<<4|t>>4,this.memoryWriter[this.registersHL](this.registersHL,t),this.FZero=0===t,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.registerA=(15&this.registerA)<<4|this.registerA>>4,this.FZero=0===this.registerA,this.FCarry=this.FHalfCarry=this.FSubtract=!1},function(){this.FCarry=1==(1&this.registerB),this.registerB>>=1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerB},function(){this.FCarry=1==(1&this.registerC),this.registerC>>=1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerC},function(){this.FCarry=1==(1&this.registerD),this.registerD>>=1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerD},function(){this.FCarry=1==(1&this.registerE),this.registerE>>=1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerE},function(){this.FCarry=256==(256&this.registersHL),this.registersHL=this.registersHL>>1&65280|255&this.registersHL,this.FHalfCarry=this.FSubtract=!1,this.FZero=this.registersHL<256},function(){this.FCarry=1==(1&this.registersHL),this.registersHL=65280&this.registersHL|(255&this.registersHL)>>1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0==(255&this.registersHL)},function(){var t=this.memoryReader[this.registersHL](this.registersHL);this.FCarry=1==(1&t),this.memoryWriter[this.registersHL](this.registersHL,t>>1),this.FHalfCarry=this.FSubtract=!1,this.FZero=t<2},function(){this.FCarry=1==(1&this.registerA),this.registerA>>=1,this.FHalfCarry=this.FSubtract=!1,this.FZero=0===this.registerA},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(256&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(512&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(1024&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(2048&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(4096&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(8192&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(16384&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(64&this.registerA)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.registerB)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.registerC)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.registerD)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.registerE)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(32768&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.registersHL)},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.memoryReader[this.registersHL](this.registersHL))},function(){this.FHalfCarry=!0,this.FSubtract=!1,this.FZero=0==(128&this.registerA)},function(){this.registerB&=254},function(){this.registerC&=254},function(){this.registerD&=254},function(){this.registerE&=254},function(){this.registersHL&=65279},function(){this.registersHL&=65534},function(){this.memoryWriter[this.registersHL](this.registersHL,254&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=254},function(){this.registerB&=253},function(){this.registerC&=253},function(){this.registerD&=253},function(){this.registerE&=253},function(){this.registersHL&=65023},function(){this.registersHL&=65533},function(){this.memoryWriter[this.registersHL](this.registersHL,253&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=253},function(){this.registerB&=251},function(){this.registerC&=251},function(){this.registerD&=251},function(){this.registerE&=251},function(){this.registersHL&=64511},function(){this.registersHL&=65531},function(){this.memoryWriter[this.registersHL](this.registersHL,251&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=251},function(){this.registerB&=247},function(){this.registerC&=247},function(){this.registerD&=247},function(){this.registerE&=247},function(){this.registersHL&=63487},function(){this.registersHL&=65527},function(){this.memoryWriter[this.registersHL](this.registersHL,247&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=247},function(){this.registerB&=239},function(){this.registerC&=239},function(){this.registerD&=239},function(){this.registerE&=239},function(){this.registersHL&=61439},function(){this.registersHL&=65519},function(){this.memoryWriter[this.registersHL](this.registersHL,239&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=239},function(){this.registerB&=223},function(){this.registerC&=223},function(){this.registerD&=223},function(){this.registerE&=223},function(){this.registersHL&=57343},function(){this.registersHL&=65503},function(){this.memoryWriter[this.registersHL](this.registersHL,223&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=223},function(){this.registerB&=191},function(){this.registerC&=191},function(){this.registerD&=191},function(){this.registerE&=191},function(){this.registersHL&=49151},function(){this.registersHL&=65471},function(){this.memoryWriter[this.registersHL](this.registersHL,191&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=191},function(){this.registerB&=127},function(){this.registerC&=127},function(){this.registerD&=127},function(){this.registerE&=127},function(){this.registersHL&=32767},function(){this.registersHL&=65407},function(){this.memoryWriter[this.registersHL](this.registersHL,127&this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA&=127},function(){this.registerB|=1},function(){this.registerC|=1},function(){this.registerD|=1},function(){this.registerE|=1},function(){this.registersHL|=256},function(){this.registersHL|=1},function(){this.memoryWriter[this.registersHL](this.registersHL,1|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=1},function(){this.registerB|=2},function(){this.registerC|=2},function(){this.registerD|=2},function(){this.registerE|=2},function(){this.registersHL|=512},function(){this.registersHL|=2},function(){this.memoryWriter[this.registersHL](this.registersHL,2|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=2},function(){this.registerB|=4},function(){this.registerC|=4},function(){this.registerD|=4},function(){this.registerE|=4},function(){this.registersHL|=1024},function(){this.registersHL|=4},function(){this.memoryWriter[this.registersHL](this.registersHL,4|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=4},function(){this.registerB|=8},function(){this.registerC|=8},function(){this.registerD|=8},function(){this.registerE|=8},function(){this.registersHL|=2048},function(){this.registersHL|=8},function(){this.memoryWriter[this.registersHL](this.registersHL,8|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=8},function(){this.registerB|=16},function(){this.registerC|=16},function(){this.registerD|=16},function(){this.registerE|=16},function(){this.registersHL|=4096},function(){this.registersHL|=16},function(){this.memoryWriter[this.registersHL](this.registersHL,16|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=16},function(){this.registerB|=32},function(){this.registerC|=32},function(){this.registerD|=32},function(){this.registerE|=32},function(){this.registersHL|=8192},function(){this.registersHL|=32},function(){this.memoryWriter[this.registersHL](this.registersHL,32|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=32},function(){this.registerB|=64},function(){this.registerC|=64},function(){this.registerD|=64},function(){this.registerE|=64},function(){this.registersHL|=16384},function(){this.registersHL|=64},function(){this.memoryWriter[this.registersHL](this.registersHL,64|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=64},function(){this.registerB|=128},function(){this.registerC|=128},function(){this.registerD|=128},function(){this.registerE|=128},function(){this.registersHL|=32768},function(){this.registersHL|=128},function(){this.memoryWriter[this.registersHL](this.registersHL,128|this.memoryReader[this.registersHL](this.registersHL))},function(){this.registerA|=128}],J=[8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,12,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8,8,8,8,8,8,8,16,8],z=[function(){},function(){this.registerC=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.registerB=this.memoryRead(this.programCounter+1&65535),this.programCounter=this.programCounter+2&65535},function(){this.memoryWrite(this.registerB<<8|this.registerC,this.registerA)},function(){var t=1+(this.registerB<<8|this.registerC);this.registerB=t>>8&255,this.registerC=255&t},function(){this.registerB=this.registerB+1&255,this.FZero=0===this.registerB,this.FHalfCarry=0==(15&this.registerB),this.FSubtract=!1},function(){this.registerB=this.registerB-1&255,this.FZero=0===this.registerB,this.FHalfCarry=15==(15&this.registerB),this.FSubtract=!0},function(){this.registerB=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535},function(){this.FCarry=this.registerA>127,this.registerA=this.registerA<<1&255|this.registerA>>7,this.FZero=this.FSubtract=this.FHalfCarry=!1},function(){var t=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+2&65535,this.memoryWrite(t,255&this.stackPointer),this.memoryWrite(t+1&65535,this.stackPointer>>8)},function(){var t=this.registersHL+(this.registerB<<8|this.registerC);this.FHalfCarry=(4095&this.registersHL)>(4095&t),this.FCarry=t>65535,this.registersHL=65535&t,this.FSubtract=!1},function(){this.registerA=this.memoryRead(this.registerB<<8|this.registerC)},function(){var t=(this.registerB<<8|this.registerC)-1&65535;this.registerB=t>>8,this.registerC=255&t},function(){this.registerC=this.registerC+1&255,this.FZero=0===this.registerC,this.FHalfCarry=0==(15&this.registerC),this.FSubtract=!1},function(){this.registerC=this.registerC-1&255,this.FZero=0===this.registerC,this.FHalfCarry=15==(15&this.registerC),this.FSubtract=!0},function(){this.registerC=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535},function(){this.registerA=this.registerA>>1|(1&this.registerA)<<7,this.FCarry=this.registerA>127,this.FZero=this.FSubtract=this.FHalfCarry=!1},function(){this.cartridge.useGBCMode&&1==(1&this.memory[65357])?(this.memory[65357]>127?(console.log("Going into single clock speed mode."),this.doubleSpeedShifter=0,this.memory[65357]&=127):(console.log("Going into double clock speed mode."),this.doubleSpeedShifter=1,this.memory[65357]|=128),this.memory[65357]&=254):this.handleSTOP()},function(){this.registerE=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.registerD=this.memoryRead(this.programCounter+1&65535),this.programCounter=this.programCounter+2&65535},function(){this.memoryWrite(this.registerD<<8|this.registerE,this.registerA)},function(){var t=1+(this.registerD<<8|this.registerE);this.registerD=t>>8&255,this.registerE=255&t},function(){this.registerD=this.registerD+1&255,this.FZero=0===this.registerD,this.FHalfCarry=0==(15&this.registerD),this.FSubtract=!1},function(){this.registerD=this.registerD-1&255,this.FZero=0===this.registerD,this.FHalfCarry=15==(15&this.registerD),this.FSubtract=!0},function(){this.registerD=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535},function(){var t=this.FCarry?1:0;this.FCarry=this.registerA>127,this.registerA=this.registerA<<1&255|t,this.FZero=this.FSubtract=this.FHalfCarry=!1},function(){this.programCounter=this.programCounter+(this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24)+1&65535},function(){var t=this.registersHL+(this.registerD<<8|this.registerE);this.FHalfCarry=(4095&this.registersHL)>(4095&t),this.FCarry=t>65535,this.registersHL=65535&t,this.FSubtract=!1},function(){this.registerA=this.memoryRead(this.registerD<<8|this.registerE)},function(){var t=(this.registerD<<8|this.registerE)-1&65535;this.registerD=t>>8,this.registerE=255&t},function(){this.registerE=this.registerE+1&255,this.FZero=0===this.registerE,this.FHalfCarry=0==(15&this.registerE),this.FSubtract=!1},function(){this.registerE=this.registerE-1&255,this.FZero=0===this.registerE,this.FHalfCarry=15==(15&this.registerE),this.FSubtract=!0},function(){this.registerE=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535},function(){var t=this.FCarry?128:0;this.FCarry=1==(1&this.registerA),this.registerA=this.registerA>>1|t,this.FZero=this.FSubtract=this.FHalfCarry=!1},function(){this.FZero?this.programCounter=this.programCounter+1&65535:(this.programCounter=this.programCounter+(this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24)+1&65535,this.CPUTicks+=4)},function(){this.registersHL=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+2&65535},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerA]),this.registersHL=this.registersHL+1&65535},function(){this.registersHL=this.registersHL+1&65535},function(){var t=1+(this.registersHL>>8)&255;this.FZero=0===t,this.FHalfCarry=0==(15&t),this.FSubtract=!1,this.registersHL=t<<8|255&this.registersHL},function(){var t=(this.registersHL>>8)-1&255;this.FZero=0===t,this.FHalfCarry=15==(15&t),this.FSubtract=!0,this.registersHL=t<<8|255&this.registersHL},function(){this.registersHL=this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<8|255&this.registersHL,this.programCounter=this.programCounter+1&65535},function(){this.FSubtract?this.FCarry&&this.FHalfCarry?(this.registerA=this.registerA+154&255,this.FHalfCarry=!1):this.FCarry?this.registerA=this.registerA+160&255:this.FHalfCarry&&(this.registerA=this.registerA+250&255,this.FHalfCarry=!1):((this.FCarry||this.registerA>153)&&(this.registerA=this.registerA+96&255,this.FCarry=!0),(this.FHalfCarry||(15&this.registerA)>9)&&(this.registerA=this.registerA+6&255,this.FHalfCarry=!1)),this.FZero=0===this.registerA},function(){this.FZero?(this.programCounter=this.programCounter+(this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24)+1&65535,this.CPUTicks+=4):this.programCounter=this.programCounter+1&65535},function(){this.FHalfCarry=(4095&this.registersHL)>2047,this.FCarry=this.registersHL>32767,this.registersHL=this.registersHL<<1&65535,this.FSubtract=!1},function(){this.registerA=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),this.registersHL=this.registersHL+1&65535},function(){this.registersHL=this.registersHL-1&65535},function(){var t=this.registersHL+1&255;this.FZero=0===t,this.FHalfCarry=0==(15&t),this.FSubtract=!1,this.registersHL=65280&this.registersHL|t},function(){var t=this.registersHL-1&255;this.FZero=0===t,this.FHalfCarry=15==(15&t),this.FSubtract=!0,this.registersHL=65280&this.registersHL|t},function(){this.registersHL=65280&this.registersHL|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535},function(){this.registerA^=255,this.FSubtract=this.FHalfCarry=!0},function(){this.FCarry?this.programCounter=this.programCounter+1&65535:(this.programCounter=this.programCounter+(this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24)+1&65535,this.CPUTicks+=4)},function(){this.stackPointer=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+2&65535},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerA]),this.registersHL=this.registersHL-1&65535},function(){this.stackPointer=this.stackPointer+1&65535},function(){var t=this.memoryReader[this.registersHL].apply(this,[this.registersHL])+1&255;this.FZero=0===t,this.FHalfCarry=0==(15&t),this.FSubtract=!1,this.memoryWriter[this.registersHL].apply(this,[this.registersHL,t])},function(){var t=this.memoryReader[this.registersHL].apply(this,[this.registersHL])-1&255;this.FZero=0===t,this.FHalfCarry=15==(15&t),this.FSubtract=!0,this.memoryWriter[this.registersHL].apply(this,[this.registersHL,t])},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.memoryReader[this.programCounter].apply(this,[this.programCounter])]),this.programCounter=this.programCounter+1&65535},function(){this.FCarry=!0,this.FSubtract=this.FHalfCarry=!1},function(){this.FCarry?(this.programCounter=this.programCounter+(this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24)+1&65535,this.CPUTicks+=4):this.programCounter=this.programCounter+1&65535},function(){var t=this.registersHL+this.stackPointer;this.FHalfCarry=(4095&this.registersHL)>(4095&t),this.FCarry=t>65535,this.registersHL=65535&t,this.FSubtract=!1},function(){this.registerA=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),this.registersHL=this.registersHL-1&65535},function(){this.stackPointer=this.stackPointer-1&65535},function(){this.registerA=this.registerA+1&255,this.FZero=0===this.registerA,this.FHalfCarry=0==(15&this.registerA),this.FSubtract=!1},function(){this.registerA=this.registerA-1&255,this.FZero=0===this.registerA,this.FHalfCarry=15==(15&this.registerA),this.FSubtract=!0},function(){this.registerA=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535},function(){this.FCarry=!this.FCarry,this.FSubtract=this.FHalfCarry=!1},function(){},function(){this.registerB=this.registerC},function(){this.registerB=this.registerD},function(){this.registerB=this.registerE},function(){this.registerB=this.registersHL>>8},function(){this.registerB=255&this.registersHL},function(){this.registerB=this.memoryReader[this.registersHL].apply(this,[this.registersHL])},function(){this.registerB=this.registerA},function(){this.registerC=this.registerB},function(){},function(){this.registerC=this.registerD},function(){this.registerC=this.registerE},function(){this.registerC=this.registersHL>>8},function(){this.registerC=255&this.registersHL},function(){this.registerC=this.memoryReader[this.registersHL].apply(this,[this.registersHL])},function(){this.registerC=this.registerA},function(){this.registerD=this.registerB},function(){this.registerD=this.registerC},function(){},function(){this.registerD=this.registerE},function(){this.registerD=this.registersHL>>8},function(){this.registerD=255&this.registersHL},function(){this.registerD=this.memoryReader[this.registersHL].apply(this,[this.registersHL])},function(){this.registerD=this.registerA},function(){this.registerE=this.registerB},function(){this.registerE=this.registerC},function(){this.registerE=this.registerD},function(){},function(){this.registerE=this.registersHL>>8},function(){this.registerE=255&this.registersHL},function(){this.registerE=this.memoryReader[this.registersHL].apply(this,[this.registersHL])},function(){this.registerE=this.registerA},function(){this.registersHL=this.registerB<<8|255&this.registersHL},function(){this.registersHL=this.registerC<<8|255&this.registersHL},function(){this.registersHL=this.registerD<<8|255&this.registersHL},function(){this.registersHL=this.registerE<<8|255&this.registersHL},function(){},function(){this.registersHL=257*(255&this.registersHL)},function(){this.registersHL=this.memoryReader[this.registersHL].apply(this,[this.registersHL])<<8|255&this.registersHL},function(){this.registersHL=this.registerA<<8|255&this.registersHL},function(){this.registersHL=65280&this.registersHL|this.registerB},function(){this.registersHL=65280&this.registersHL|this.registerC},function(){this.registersHL=65280&this.registersHL|this.registerD},function(){this.registersHL=65280&this.registersHL|this.registerE},function(){this.registersHL=65280&this.registersHL|this.registersHL>>8},function(){},function(){this.registersHL=65280&this.registersHL|this.memoryReader[this.registersHL].apply(this,[this.registersHL])},function(){this.registersHL=65280&this.registersHL|this.registerA},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerB])},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerC])},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerD])},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerE])},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registersHL>>8])},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,255&this.registersHL])},function(){(this.interruptsEnabled&this.interruptsRequested&31)>0?this.cartridge.useGBCMode||this.usedBootROM?this.CPUTicks+=4:this.skipPCIncrement=!0:this.calculateHALTPeriod()},function(){this.memoryWriter[this.registersHL].apply(this,[this.registersHL,this.registerA])},function(){this.registerA=this.registerB},function(){this.registerA=this.registerC},function(){this.registerA=this.registerD},function(){this.registerA=this.registerE},function(){this.registerA=this.registersHL>>8},function(){this.registerA=255&this.registersHL},function(){this.registerA=this.memoryReader[this.registersHL].apply(this,[this.registersHL])},function(){},function(){var t=this.registerA+this.registerB;this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerC;this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerD;this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerE;this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+(this.registersHL>>8);this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+(255&this.registersHL);this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.memoryReader[this.registersHL].apply(this,[this.registersHL]);this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){this.FHalfCarry=8==(8&this.registerA),this.FCarry=this.registerA>127,this.registerA=this.registerA<<1&255,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerB+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&this.registerB)+(this.FCarry?1:0)>15,this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerC+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&this.registerC)+(this.FCarry?1:0)>15,this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerD+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&this.registerD)+(this.FCarry?1:0)>15,this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA+this.registerE+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&this.registerE)+(this.FCarry?1:0)>15,this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registersHL>>8,e=this.registerA+t+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&t)+(this.FCarry?1:0)>15,this.FCarry=e>255,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=255&this.registersHL,e=this.registerA+t+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&t)+(this.FCarry?1:0)>15,this.FCarry=e>255,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),e=this.registerA+t+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&t)+(this.FCarry?1:0)>15,this.FCarry=e>255,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA<<1|(this.FCarry?1:0);this.FHalfCarry=(this.registerA<<1&30|(this.FCarry?1:0))>15,this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){var t=this.registerA-this.registerB;this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.registerC;this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.registerD;this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.registerE;this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-(this.registersHL>>8);this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-(255&this.registersHL);this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.memoryReader[this.registersHL].apply(this,[this.registersHL]);this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){this.registerA=0,this.FHalfCarry=this.FCarry=!1,this.FZero=this.FSubtract=!0},function(){var t=this.registerA-this.registerB-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&this.registerB)-(this.FCarry?1:0)<0,this.FCarry=t<0,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!0},function(){var t=this.registerA-this.registerC-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&this.registerC)-(this.FCarry?1:0)<0,this.FCarry=t<0,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!0},function(){var t=this.registerA-this.registerD-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&this.registerD)-(this.FCarry?1:0)<0,this.FCarry=t<0,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!0},function(){var t=this.registerA-this.registerE-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&this.registerE)-(this.FCarry?1:0)<0,this.FCarry=t<0,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!0},function(){var t=this.registersHL>>8,e=this.registerA-t-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&t)-(this.FCarry?1:0)<0,this.FCarry=e<0,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!0},function(){var t=this.registerA-(255&this.registersHL)-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&this.registersHL)-(this.FCarry?1:0)<0,this.FCarry=t<0,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!0},function(){var t=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),e=this.registerA-t-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&t)-(this.FCarry?1:0)<0,this.FCarry=e<0,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!0},function(){this.FCarry?(this.FZero=!1,this.FSubtract=this.FHalfCarry=this.FCarry=!0,this.registerA=255):(this.FHalfCarry=this.FCarry=!1,this.FSubtract=this.FZero=!0,this.registerA=0)},function(){this.registerA&=this.registerB,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA&=this.registerC,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA&=this.registerD,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA&=this.registerE,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA&=this.registersHL>>8,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA&=this.registersHL,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA&=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.registerA^=this.registerB,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA^=this.registerC,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA^=this.registerD,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA^=this.registerE,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA^=this.registersHL>>8,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA^=255&this.registersHL,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA^=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA=0,this.FZero=!0,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.registerA|=this.registerB,this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.registerA|=this.registerC,this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.registerA|=this.registerD,this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.registerA|=this.registerE,this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.registerA|=this.registersHL>>8,this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.registerA|=255&this.registersHL,this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.registerA|=this.memoryReader[this.registersHL].apply(this,[this.registersHL]),this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.FZero=0===this.registerA,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){var t=this.registerA-this.registerB;this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.registerC;this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.registerD;this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.registerE;this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-(this.registersHL>>8);this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-(255&this.registersHL);this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){var t=this.registerA-this.memoryReader[this.registersHL].apply(this,[this.registersHL]);this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){this.FHalfCarry=this.FCarry=!1,this.FZero=this.FSubtract=!0},function(){this.FZero||(this.programCounter=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535,this.CPUTicks+=12)},function(){this.registerC=this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.registerB=this.memoryRead(this.stackPointer+1&65535),this.stackPointer=this.stackPointer+2&65535},function(){this.FZero?this.programCounter=this.programCounter+2&65535:(this.programCounter=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.CPUTicks+=4)},function(){this.programCounter=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter])},function(){if(this.FZero)this.programCounter=this.programCounter+2&65535;else{var t=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+2&65535,this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=t,this.CPUTicks+=12}},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.registerB]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.registerC])},function(){var t=this.registerA+this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535,this.FHalfCarry=(15&t)<(15&this.registerA),this.FCarry=t>255,this.registerA=255&t,this.FZero=0===this.registerA,this.FSubtract=!1},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=0},function(){this.FZero&&(this.programCounter=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535,this.CPUTicks+=12)},function(){this.programCounter=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535},function(){this.FZero?(this.programCounter=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.CPUTicks+=4):this.programCounter=this.programCounter+2&65535},function(){const t=this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535,this.CPUTicks+=J[t],U[t].apply(this)},function(){if(this.FZero){var t=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+2&65535,this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=t,this.CPUTicks+=12}else this.programCounter=this.programCounter+2&65535},function(){var t=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+2&65535,this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=t},function(){var t=this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535;var e=this.registerA+t+(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)+(15&t)+(this.FCarry?1:0)>15,this.FCarry=e>255,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!1},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=8},function(){this.FCarry||(this.programCounter=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535,this.CPUTicks+=12)},function(){this.registerE=this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.registerD=this.memoryRead(this.stackPointer+1&65535),this.stackPointer=this.stackPointer+2&65535},function(){this.FCarry?this.programCounter=this.programCounter+2&65535:(this.programCounter=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.CPUTicks+=4)},function(){console.error("Illegal op code 0xD3 called, pausing emulation.")},function(){if(this.FCarry)this.programCounter=this.programCounter+2&65535;else{var t=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+2&65535,this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=t,this.CPUTicks+=12}},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.registerD]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.registerE])},function(){var t=this.registerA-this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535,this.FHalfCarry=(15&this.registerA)<(15&t),this.FCarry=t<0,this.registerA=255&t,this.FZero=0===t,this.FSubtract=!0},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=16},function(){this.FCarry&&(this.programCounter=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535,this.CPUTicks+=12)},function(){this.programCounter=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535,this.IRQEnableDelay=2===this.IRQEnableDelay||118===this.memoryReader[this.programCounter].apply(this,[this.programCounter])?1:2},function(){this.FCarry?(this.programCounter=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.CPUTicks+=4):this.programCounter=this.programCounter+2&65535},function(){console.error("Illegal op code 0xDB called, pausing emulation.")},function(){if(this.FCarry){var t=this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+2&65535,this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=t,this.CPUTicks+=12}else this.programCounter=this.programCounter+2&65535},function(){console.error("Illegal op code 0xDD called, pausing emulation.")},function(){var t=this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535;var e=this.registerA-t-(this.FCarry?1:0);this.FHalfCarry=(15&this.registerA)-(15&t)-(this.FCarry?1:0)<0,this.FCarry=e<0,this.registerA=255&e,this.FZero=0===this.registerA,this.FSubtract=!0},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=24},function(){this.memoryHighWrite(this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.registerA),this.programCounter=this.programCounter+1&65535},function(){this.registersHL=this.memoryRead(this.stackPointer+1&65535)<<8|this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]),this.stackPointer=this.stackPointer+2&65535},function(){this.memoryHighWriter[this.registerC].apply(this,[this.registerC,this.registerA])},function(){console.log("Illegal op code 0xE3 called, pausing emulation.")},function(){console.log("Illegal op code 0xE4 called, pausing emulation.")},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.registersHL>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.registersHL])},function(){this.registerA&=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535,this.FZero=0===this.registerA,this.FHalfCarry=!0,this.FSubtract=this.FCarry=!1},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=32},function(){var t=this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24;this.programCounter=this.programCounter+1&65535;var e=this.stackPointer+t&65535;t=this.stackPointer^t^e,this.stackPointer=e,this.FCarry=256==(256&t),this.FHalfCarry=16==(16&t),this.FZero=this.FSubtract=!1},function(){this.programCounter=this.registersHL},function(){this.memoryWrite(this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.registerA),this.programCounter=this.programCounter+2&65535},function(){console.error("Illegal op code 0xEB called, pausing emulation.")},function(){console.error("Illegal op code 0xEC called, pausing emulation.")},function(){console.error("Illegal op code 0xED called, pausing emulation.")},function(){this.registerA^=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.programCounter=this.programCounter+1&65535,this.FZero=0===this.registerA,this.FSubtract=this.FHalfCarry=this.FCarry=!1},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=40},function(){this.registerA=this.memoryHighRead(this.memoryReader[this.programCounter].apply(this,[this.programCounter])),this.programCounter=this.programCounter+1&65535},function(){var t=this.memoryReader[this.stackPointer].apply(this,[this.stackPointer]);this.FZero=t>127,this.FSubtract=64==(64&t),this.FHalfCarry=32==(32&t),this.FCarry=16==(16&t),this.registerA=this.memoryRead(this.stackPointer+1&65535),this.stackPointer=this.stackPointer+2&65535},function(){this.registerA=this.memoryHighReader[this.registerC].apply(this,[this.registerC])},function(){this.IME=!1,this.IRQEnableDelay=0},function(){console.error("Illegal op code 0xF4 called, pausing emulation.")},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.registerA]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,(this.FZero?128:0)|(this.FSubtract?64:0)|(this.FHalfCarry?32:0)|(this.FCarry?16:0)])},function(){this.registerA|=this.memoryReader[this.programCounter].apply(this,[this.programCounter]),this.FZero=0===this.registerA,this.programCounter=this.programCounter+1&65535,this.FSubtract=this.FCarry=this.FHalfCarry=!1},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=48},function(){var t=this.memoryReader[this.programCounter].apply(this,[this.programCounter])<<24>>24;this.programCounter=this.programCounter+1&65535,this.registersHL=this.stackPointer+t&65535,t=this.stackPointer^t^this.registersHL,this.FCarry=256==(256&t),this.FHalfCarry=16==(16&t),this.FZero=this.FSubtract=!1},function(){this.stackPointer=this.registersHL},function(){this.registerA=this.memoryRead(this.memoryRead(this.programCounter+1&65535)<<8|this.memoryReader[this.programCounter].apply(this,[this.programCounter])),this.programCounter=this.programCounter+2&65535},function(){this.IRQEnableDelay=2===this.IRQEnableDelay||118===this.memoryReader[this.programCounter].apply(this,[this.programCounter])?1:2},()=>console.error("Illegal op code 0xFC called, pausing emulation."),()=>console.error("Illegal op code 0xFD called, pausing emulation."),function(){var t=this.registerA-this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535,this.FHalfCarry=(15&t)>(15&this.registerA),this.FCarry=t<0,this.FZero=0===t,this.FSubtract=!0},function(){this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=56}],X=[15,0,124,255,0,0,0,248,255,255,255,255,255,255,255,1,128,191,243,255,191,255,63,0,255,191,127,255,159,255,191,255,255,0,0,191,119,243,241,255,255,255,255,255,255,255,255,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255,145,128,0,0,0,0,0,252,0,0,0,0,255,126,255,254,255,255,255,255,255,255,62,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,192,255,193,0,254,255,255,255,248,255,0,0,0,143,0,0,255,255,255,255,255,255,255,255,206,237,102,102,204,13,0,11,3,115,0,131,0,12,0,13,0,8,17,31,136,137,0,14,220,204,110,230,221,221,217,153,187,187,103,99,110,14,236,204,221,220,153,159,187,185,51,62,69,236,82,250,8,183,7,93,1,253,192,255,8,252,0,229,11,248,194,206,244,249,15,127,69,109,61,254,70,151,51,94,8,239,241,255,134,131,36,116,18,252,0,159,180,183,6,213,208,122,0,158,4,95,65,47,29,119,54,117,129,170,112,58,152,209,113,2,77,1,193,255,13,0,211,5,249,0,11,0],Y=[!0,1,!0,!1,!0,!0,0,19,0,216,333,65534,256,!1,!0,!1,0,0,[],[],0,[],!1,1,-53248,0,0,0,!1,!1,!1,!1,!1,0,!1,!1,!0,0,128,!1,56,60,0,1024,0,0,0,0,(new Date).getTime(),0,[],!0,!0,8192,512,0,0,!1,0,0,!0,0,!1,0,1,0,!1,0,!1,8192,512,0,0,!1,0,0,!0,0,!1,0,4,0,!0,null,8,0,0,0,0,!1,0,0,!0,32767,!1,8,8,!1,!1,!1,!1,!1,!1,!1,!1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Z[2],0,Z[2],!1,!1,!1,!1,8192,0,2048,8,0,0,2048,0,144,0,0,!1,0,0,0,0,0,0,0,0,0,!1,!1,!1,!1,0,-61440,0,0,null,null,null,null,null,null,null,null,null,null,null,null,0,0,0,0,!1,0,0,!1,1,1];class Q{constructor(t){this.gameboy=t}init(){this.loadOld(Y.slice(0))}get(){const t=this.gameboy;return t.cartridge?d(t.memory.buffer.slice(0),t.VRAM.buffer.slice(0)):null}load(t){this.gameboy}loadOld(t){let e=0;const i=this.gameboy;i.inBootstrap=t[e++],i.registerA=t[e++],i.FZero=t[e++],i.FSubtract=t[e++],i.FHalfCarry=t[e++],i.FCarry=t[e++],i.registerB=t[e++],i.registerC=t[e++],i.registerD=t[e++],i.registerE=t[e++],i.registersHL=t[e++],i.stackPointer=t[e++],i.programCounter=t[e++],i.halt=t[e++],i.IME=t[e++],i.hdmaRunning=t[e++],i.CPUTicks=t[e++],i.doubleSpeedShifter=t[e++],i.memory=c(t[e++],"uint8"),i.VRAM=c(t[e++],"uint8"),i.currVRAMBank=t[e++],i.GBCMemory=c(t[e++],"uint8"),i.useGBCMode=t[e++],i.gbcRamBank=t[e++],i.gbcRamBankPosition=t[e++],i.ROMBank1Offset=t[e++],i.cartridge?i.cartridge.mbc.currentROMBank=t[e++]:e++,i.modeSTAT=t[e++],i.LYCMatchTriggerSTAT=t[e++],i.mode2TriggerSTAT=t[e++],i.mode1TriggerSTAT=t[e++],i.mode0TriggerSTAT=t[e++],i.LCDisOn=t[e++],i.gfxWindowCHRBankPosition=t[e++],i.gfxWindowDisplay=t[e++],i.gfxSpriteShow=t[e++],i.gfxSpriteNormalHeight=t[e++],i.gfxBackgroundCHRBankPosition=t[e++],i.gfxBackgroundBankOffset=t[e++],i.TIMAEnabled=t[e++],i.DIVTicks=t[e++],i.LCDTicks=t[e++],i.timerTicks=t[e++],i.TACClocker=t[e++],i.serialTimer=t[e++],i.serialShiftTimer=t[e++],i.serialShiftTimerAllocated=t[e++],i.IRQEnableDelay=t[e++],i.cartridge&&i.cartridge.hasRTC?i.cartridge.mbc3.rtc.lastTime=t[e++]:e++,i.drewBlank=t[e++],i.frameBuffer=c(t[e++],"int32"),i.bgEnabled=t[e++],i.BGPriorityEnabled=t[e++],i.audioController.channel1FrequencyTracker=t[e++],i.channel1FrequencyCounter=t[e++],i.channel1totalLength=t[e++],i.channel1envelopeVolume=t[e++],i.channel1envelopeType=t[e++],i.channel1envelopeSweeps=t[e++],i.channel1envelopeSweepsLast=t[e++],i.channel1consecutive=t[e++],i.channel1frequency=t[e++],i.channel1SweepFault=t[e++],i.channel1ShadowFrequency=t[e++],i.channel1timeSweep=t[e++],i.channel1lastTimeSweep=t[e++],i.channel1Swept=t[e++],i.channel1frequencySweepDivider=t[e++],i.channel1decreaseSweep=t[e++],i.channel2FrequencyTracker=t[e++],i.channel2FrequencyCounter=t[e++],i.channel2totalLength=t[e++],i.channel2envelopeVolume=t[e++],i.channel2envelopeType=t[e++],i.channel2envelopeSweeps=t[e++],i.channel2envelopeSweepsLast=t[e++],i.channel2consecutive=t[e++],i.channel2frequency=t[e++],i.channel3canPlay=t[e++],i.channel3totalLength=t[e++],i.channel3patternType=t[e++],i.channel3frequency=t[e++],i.channel3consecutive=t[e++],i.channel3PCM=c(t[e++],"int8"),i.audioController.channel4FrequencyPeriod=t[e++],i.audioController.channel4lastSampleLookup=t[e++],i.channel4totalLength=t[e++],i.channel4envelopeVolume=t[e++],i.channel4currentVolume=t[e++],i.channel4envelopeType=t[e++],i.channel4envelopeSweeps=t[e++],i.channel4envelopeSweepsLast=t[e++],i.channel4consecutive=t[e++],i.channel4BitRange=t[e++],i.soundMasterEnabled=t[e++],i.audioController.VinLeftChannelMasterVolume=t[e++],i.audioController.VinRightChannelMasterVolume=t[e++],i.leftChannel1=t[e++],i.leftChannel2=t[e++],i.leftChannel3=t[e++],i.leftChannel4=t[e++],i.rightChannel1=t[e++],i.rightChannel2=t[e++],i.rightChannel3=t[e++],i.rightChannel4=t[e++],i.channel1currentSampleLeft=t[e++],i.channel1currentSampleRight=t[e++],i.channel2currentSampleLeft=t[e++],i.channel2currentSampleRight=t[e++],i.channel3currentSampleLeft=t[e++],i.channel3currentSampleRight=t[e++],i.channel4currentSampleLeft=t[e++],i.channel4currentSampleRight=t[e++],i.channel1currentSampleLeftSecondary=t[e++],i.channel1currentSampleRightSecondary=t[e++],i.channel2currentSampleLeftSecondary=t[e++],i.channel2currentSampleRightSecondary=t[e++],i.channel3currentSampleLeftSecondary=t[e++],i.channel3currentSampleRightSecondary=t[e++],i.channel4currentSampleLeftSecondary=t[e++],i.channel4currentSampleRightSecondary=t[e++],i.channel1currentSampleLeftTrimary=t[e++],i.channel1currentSampleRightTrimary=t[e++],i.channel2currentSampleLeftTrimary=t[e++],i.channel2currentSampleRightTrimary=t[e++],i.audioController.mixerOutputCache=t[e++],i.audioController.channel1DutyTracker=t[e++],i.audioController.channel1CachedDuty=t[e++],i.audioController.channel2DutyTracker=t[e++],i.audioController.channel2CachedDuty=t[e++],i.audioController.channel1Enabled=t[e++],i.audioController.channel2Enabled=t[e++],i.audioController.channel3Enabled=t[e++],i.audioController.channel4Enabled=t[e++],i.audioController.sequencerClocks=t[e++],i.audioController.sequencePosition=t[e++],i.channel3Counter=t[e++],i.audioController.channel4Counter=t[e++],i.audioController.cachedChannel3Sample=t[e++],i.audioController.cachedChannel4Sample=t[e++],i.channel3FrequencyPeriod=t[e++],i.channel3lastSampleLookup=t[e++],i.actualScanLine=t[e++],i.lastUnrenderedLine=t[e++],i.queuedScanLines=t[e++],i.cartridge&&i.cartridge.hasRTC?(i.cartridge.mbc3.rtc.RTCisLatched=t[e++],i.cartridge.mbc3.rtc.latchedSeconds=t[e++],i.cartridge.mbc3.rtc.latchedMinutes=t[e++],i.cartridge.mbc3.rtc.latchedHours=t[e++],i.cartridge.mbc3.rtc.latchedLDays=t[e++],i.cartridge.mbc3.rtc.latchedHDays=t[e++],i.cartridge.mbc3.rtc.RTCSeconds=t[e++],i.cartridge.mbc3.rtc.RTCMinutes=t[e++],i.cartridge.mbc3.rtc.RTCHours=t[e++],i.cartridge.mbc3.rtc.RTCDays=t[e++],i.cartridge.mbc3.rtc.RTCDayOverFlow=t[e++],i.cartridge.mbc3.rtc.RTCHalt=t[e++]):e+=12,i.usedBootROM=t[e++],i.skipPCIncrement=t[e++],i.STATTracker=t[e++],i.gbcRamBankPositionECHO=t[e++],i.windowY=t[e++],i.windowX=t[e++],i.gbcOBJRawPalette=c(t[e++],"uint8"),i.gbcBGRawPalette=c(t[e++],"uint8"),i.gbOBJPalette=c(t[e++],"int32"),i.gbBGPalette=c(t[e++],"int32"),i.gbcOBJPalette=c(t[e++],"int32"),i.gbcBGPalette=c(t[e++],"int32"),i.gbBGColorizedPalette=c(t[e++],"int32"),i.gbOBJColorizedPalette=c(t[e++],"int32"),i.cachedBGPaletteConversion=c(t[e++],"int32"),i.cachedOBJPaletteConversion=c(t[e++],"int32"),i.BGCHRBank1=c(t[e++],"uint8"),i.BGCHRBank2=c(t[e++],"uint8"),i.haltPostClocks=t[e++],i.interruptsRequested=t[e++],i.interruptsEnabled=t[e++],i.checkIRQMatching(),i.remainingClocks=t[e++],i.colorizedGBPalettes=t[e++],i.backgroundY=t[e++],i.backgroundX=t[e++],i.CPUStopped=t[e++],i.audioController.audioClocksUntilNextEvent=t[e++],i.audioController.audioClocksUntilNextEventCounter=t[e]}}const j=0,_=16383,$=16384,K=32767,tt=32768,et=36863,it=38911,rt=40960,st=49151,ht=49152,at=53247,nt=53248,ot=57343,ct=57344,lt=65023,mt=65024,ut=65183,gt=65184,dt=65279,yt=65280,ft=65281,Ct=65282,pt=65284,Bt=65535;class Rt{constructor(t){this.initialValue=15,this.value=255,this.gameboy=t}down(t){this.value&=255^1<>4:15)&(0==(16&t)?15&this.value:15)),this.gameboy.CPUStopped=!1}}class Ft{constructor(t,e){this.readers=[],this.writers=[],this.data=m(65536,0,"uint8"),this.gameboy=t,this.data=e}write(t,e){return this.writers[t](t,e)}read(t){const e=this.readers[t];if(!e)throw new Error("no_reader");return e(t)}hasReader(t){return!!this.readers[t]}jumpCompile(){this.setReaders(j,_,t=>this.data[t]),this.setReaders($,K,t=>this.gameboy.cartridge.rom.getByte(this.gameboy.cartridge.mbc.currentROMBank+t)),this.setReaders(tt,it,this.gameboy.cartridge.useGBCMode?this.gameboy.VRAMDATAReadCGBCPU:this.gameboy.VRAMDATAReadDMGCPU),this.setReaders(rt,st,this.gameboy.cartridge.useGBCMode?this.gameboy.VRAMCHRReadCGBCPU:this.gameboy.VRAMCHRReadDMGCPU),this.gameboy.cartridge.mbc&&0===this.gameboy.cartridge.mbc.ramSize?this.setReaders(rt,st,this.gameboy.badMemoryRead):this.gameboy.cartridge.hasMBC7?this.setReaders(rt,st,this.gameboy.memoryReadMBC7):this.gameboy.cartridge.hasMBC3?this.setReaders(rt,st,this.gameboy.memoryReadMBC3):this.setReaders(rt,st,this.gameboy.memoryReadMBC),this.setReaders(ht,at,this.gameboy.memoryReadNormal),this.gameboy.cartridge.useGBCMode?this.setReaders(nt,ot,this.gameboy.memoryReadGBCMemory):this.setReaders(nt,ot,this.gameboy.memoryReadNormal),this.setReaders(ct,61439,this.gameboy.memoryReadECHONormal),this.gameboy.cartridge.useGBCMode?this.setReaders(61440,lt,this.gameboy.memoryReadECHOGBCMemory):this.setReaders(61440,lt,this.gameboy.memoryReadECHONormal),this.setReaders(mt,ut,this.gameboy.memoryReadOAM),this.gameboy.cartridge.useGBCMode&&this.setReaders(gt,dt,this.gameboy.memoryReadNormal),this.gameboy.memoryReadJumpCompile(),this.gameboy.memoryWriteJumpCompile()}setReaders(t,e,i){for(let r=t;r<=e;r++)this.setReader(r,i)}setReader(t,e){this.readers[t]=e}setWriters(t,e,i){for(let r=t;r<=e;r++)this.setWriter(r,i)}setWriter(t,e){this.writers[t]=e}}class St{constructor(){this.speed=1,this.ticks=0,this.cyclesTotal=0,this.cyclesTotalBase=0,this.cyclesTotalCurrent=0,this.cyclesTotalRoundoff=0,this.baseCyclesPerIteration=0,this.totalLinesPassed=0,this.calculateTimings()}calculateTimings(){this.clocksPerSecond=4194304*this.speed,this.baseCyclesPerIteration=this.clocksPerSecond/1e3*s.runInterval,this.cyclesTotalRoundoff=this.baseCyclesPerIteration%4,this.cyclesTotalBase=this.cyclesTotal=this.baseCyclesPerIteration-this.cyclesTotalRoundoff|0,this.cyclesTotalCurrent=0}setSpeed(t){this.speed=t,this.calculateTimings()}}class Ht{constructor({audio:t={},api:e,lcd:i={},bootRom:r}){this.api=e,r&&this.setBootRom(r),this.events=new H.EventEmitter,i.gameboy=this,this.cpu=new St,this.audioDevice=new x({context:t.context,channels:2,volume:s.soundVolume}),this.audioController=new N({cpu:this.cpu,gameboy:this}),this.joypad=new Rt(this),this.lcdDevice=new q(i),this.lcdController=new V,this.stateManager=new Q(this),this.stateManager.init(),this.GBBOOTROM=[],this.GBCBOOTROM=[],this.memoryReadNormal=this.memoryReadNormal.bind(this),this.memoryWriteNormal=this.memoryWriteNormal.bind(this),this.memoryWriteGBCRAM=this.memoryWriteGBCRAM.bind(this),this.memoryWriteMBCRAM=this.memoryWriteMBCRAM.bind(this),this.memoryWriteMBC3RAM=this.memoryWriteMBC3RAM.bind(this),this.memoryReadGBCMemory=this.memoryReadGBCMemory.bind(this),this.memoryReadROM=this.memoryReadROM.bind(this),this.memoryHighWriteNormal=this.memoryHighWriteNormal.bind(this),this.memoryHighReadNormal=this.memoryHighReadNormal.bind(this),this.MBC5WriteRAMBank=this.MBC5WriteRAMBank.bind(this),this.MBCWriteEnable=this.MBCWriteEnable.bind(this),this.RUMBLEWriteRAMBank=this.RUMBLEWriteRAMBank.bind(this),this.onRUMBLE=this.onRUMBLE.bind(this),this.memoryReadMBC=this.memoryReadMBC.bind(this),this.memoryReadMBC3=this.memoryReadMBC3.bind(this),this.memoryReadMBC7=this.memoryReadMBC7.bind(this),this.renderBGGBLayer=this.renderBGGBLayer.bind(this),this.renderWindowGBLayer=this.renderWindowGBLayer.bind(this),this.renderSpriteGBLayer=this.renderSpriteGBLayer.bind(this),this.renderSpriteGBCLayer=this.renderSpriteGBCLayer.bind(this),this.stopEmulator=3,this.IRQLineMatched=0,this.memoryReader=[],this.memoryWriter=[],this.memoryHighReader=[],this.memoryHighWriter=[],this.spriteCount=252,this.LINECONTROL=[],this.DISPLAYOFFCONTROL=[function(){}],this.LCDCONTROL=null,this.initializeLCDController(),this.drewFrame=!1,this.midScanlineOffset=-1,this.pixelEnd=0,this.currentX=0,this.BGCHRCurrentBank=null,this.tileCache=null,this.colors=[15728606,11392916,5411443,1586242],this.OBJPalette=null,this.BGPalette=null,this.updateGBBGPalette=this.updateGBRegularBGPalette,this.updateGBOBJPalette=this.updateGBRegularOBJPalette,this.renderBGLayer=null,this.renderWindowLayer=null,this.renderSpriteLayer=null,this.pixelStart=0}setBootRom(t){this.bootROM=t instanceof S?t:new S(t)}loadState(t){this.stateManager.load(t),this.initializeReferencesFromSaveState(),this.jumpCompile(),this.lcdDevice.init(),this.initSound(),this.audioController.noiseSampleTable=32767===this.audioController.channel4BitRange?this.audioController.LSFR15Table:this.audioController.LSFR7Table,this.audioController.channel4VolumeShifter=32767===this.audioController.channel4BitRange?15:7}jumpCompile(){this.memoryNew.jumpCompile()}connectCartridge(t){this.cartridge&&this.cartridge.mbc&&this.cartridge.mbc.removeListener("rumble",this.onRUMBLE),t.connect(this),this.cartridge=t,this.cartridge&&this.cartridge.mbc&&this.cartridge.mbc.on("rumble",this.onRUMBLE),this.loadCartridgeRomIntoMemory(),this.bootROM&&this.loadBootROMIntoMemory(),this.cartridge.interpret()}onRUMBLE(){"undefined"!=typeof window&&"vibrate"in window.navigator&&window.navigator.vibrate(200)}loadCartridgeRomIntoMemory(){for(let t=0;t<16384;t++)this.memory[t]=this.cartridge.rom.getByte(t)}loadBootROMIntoMemory(){for(let t=0;t<256;t++)this.memory[t]=this.bootROM.getByte(t);if(this.bootROM.length>=256)for(let t=512;t<2304;t++)this.memory[t]=this.bootROM.getByte(t-256)}start(t){this.init(),this.connectCartridge(t),this.cartridge&&this.cartridge.mbc&&(this.cartridge.mbc.setupROM(),this.cartridge.mbc.on("ramWrite",()=>{this.events.emit("sramWrite")})),this.usedBootROM?(this.setupRAM(),this.initBootstrap()):(this.inBootstrap=!1,this.setupRAM(),this.initSkipBootstrap()),this.checkIRQMatching()}init(){this.stateManager.init(),this.initMemory(),this.lcdDevice.init(),this.initSound()}setupRAM(){this.cartridge.setupRAM(),this.cartridge.useGBCMode&&(this.VRAM=m(8192,0,"uint8"),this.GBCMemory=m(28672,0,"uint8")),this.jumpCompile(),this.initializeModeSpecificArrays()}initMemory(){this.memory=m(65536,0,"uint8"),this.audioController.setMemory(this.memory),this.frameBuffer=m(23040,16316664,"int32"),this.BGCHRBank1=m(2048,0,"uint8"),this.audioController.initMemory(),this.memoryNew=new Ft(this,this.memory)}generateCacheArray(t){const e=[];let i=0;for(;i=0;){if(t>=48&&t<64)this.memoryWrite(65280|t,X[t]);else switch(t){case 0:case 1:case 2:case 5:case 7:case 15:case 255:this.memoryWrite(65280|t,X[t]);break;default:this.memory[65280|t]=X[t]}--t}this.cartridge.useGBCMode?(this.memory[65388]=254,this.memory[65396]=254):(this.memory[65352]=255,this.memory[65353]=255,this.memory[65388]=255,this.memory[65396]=255),console.log("Starting without the GBC boot ROM."),this.registerA=this.cartridge.useGBCMode?17:1,this.registerB=0,this.registerC=19,this.registerD=0,this.registerE=216,this.FZero=!0,this.FSubtract=!1,this.FHalfCarry=!0,this.FCarry=!0,this.registersHL=333,this.LCDCONTROL=this.LINECONTROL,this.IME=!1,this.IRQLineMatched=0,this.interruptsRequested=225,this.interruptsEnabled=0,this.hdmaRunning=!1,this.CPUTicks=12,this.STATTracker=0,this.modeSTAT=1,this.spriteCount=252,this.LYCMatchTriggerSTAT=!1,this.mode2TriggerSTAT=!1,this.mode1TriggerSTAT=!1,this.mode0TriggerSTAT=!1,this.LCDisOn=!0,this.audioController.channel1FrequencyTracker=8192,this.audioController.channel1DutyTracker=0,this.audioController.channel1CachedDuty=Z[2],this.audioController.channel1totalLength=0,this.audioController.channel1envelopeVolume=0,this.audioController.channel1envelopeType=!1,this.audioController.channel1envelopeSweeps=0,this.audioController.channel1envelopeSweepsLast=0,this.audioController.channel1consecutive=!0,this.audioController.channel1frequency=1985,this.audioController.channel1SweepFault=!0,this.audioController.channel1ShadowFrequency=1985,this.audioController.channel1timeSweep=1,this.audioController.channel1lastTimeSweep=0,this.audioController.channel1Swept=!1,this.audioController.channel1frequencySweepDivider=0,this.audioController.channel1decreaseSweep=!1,this.audioController.channel2FrequencyTracker=8192,this.audioController.channel2DutyTracker=0,this.audioController.channel2CachedDuty=Z[2],this.audioController.channel2totalLength=0,this.audioController.channel2envelopeVolume=0,this.audioController.channel2envelopeType=!1,this.audioController.channel2envelopeSweeps=0,this.audioController.channel2envelopeSweepsLast=0,this.audioController.channel2consecutive=!0,this.audioController.channel2frequency=0,this.audioController.channel3canPlay=!1,this.audioController.channel3totalLength=0,this.audioController.channel3patternType=4,this.audioController.channel3frequency=0,this.audioController.channel3consecutive=!0,this.audioController.channel3Counter=1048,this.audioController.channel4FrequencyPeriod=8,this.audioController.channel4totalLength=0,this.audioController.channel4envelopeVolume=0,this.audioController.channel4currentVolume=0,this.audioController.channel4envelopeType=!1,this.audioController.channel4envelopeSweeps=0,this.audioController.channel4envelopeSweepsLast=0,this.audioController.channel4consecutive=!0,this.audioController.channel4BitRange=32767,this.audioController.channel4VolumeShifter=15,this.audioController.channel1FrequencyCounter=512,this.audioController.channel2FrequencyCounter=512,this.audioController.channel3Counter=2048,this.audioController.channel3FrequencyPeriod=2048,this.audioController.channel3lastSampleLookup=0,this.audioController.channel4lastSampleLookup=0,this.audioController.VinLeftChannelMasterVolume=8,this.audioController.VinRightChannelMasterVolume=8,this.audioController.leftChannel1=!0,this.audioController.leftChannel2=!0,this.audioController.leftChannel3=!0,this.audioController.leftChannel4=!0,this.audioController.rightChannel1=!0,this.audioController.rightChannel2=!0,this.audioController.rightChannel3=!1,this.audioController.rightChannel4=!1,this.soundMasterEnabled=!0,this.DIVTicks=27044,this.LCDTicks=160,this.timerTicks=0,this.TIMAEnabled=!1,this.TACClocker=1024,this.serialTimer=0,this.serialShiftTimer=0,this.serialShiftTimerAllocated=0,this.IRQEnableDelay=0,this.actualScanLine=144,this.lastUnrenderedLine=0,this.gfxWindowDisplay=!1,this.gfxSpriteShow=!1,this.gfxSpriteNormalHeight=!0,this.bgEnabled=!0,this.hasBGPriority=!0,this.gfxWindowCHRBankPosition=0,this.gfxBackgroundCHRBankPosition=0,this.gfxBackgroundBankOffset=0,this.windowY=0,this.windowX=0,this.drewBlank=0,this.midScanlineOffset=-1,this.currentX=0}initBootstrap(){console.log("Starting selected boot ROM"),this.programCounter=0,this.stackPointer=0,this.IME=!1,this.LCDTicks=0,this.DIVTicks=0,this.registerA=0,this.registerB=0,this.registerC=0,this.registerD=0,this.registerE=0,this.FZero=this.FSubtract=this.FHalfCarry=this.FCarry=!1,this.registersHL=0,this.audioController.leftChannel1=!1,this.audioController.leftChannel2=!1,this.audioController.leftChannel3=!1,this.audioController.leftChannel4=!1,this.audioController.rightChannel1=!1,this.audioController.rightChannel2=!1,this.audioController.rightChannel3=!1,this.audioController.rightChannel4=!1,this.audioController.channel2frequency=this.audioController.channel1frequency=0,this.audioController.channel4consecutive=this.audioController.channel2consecutive=this.audioController.channel1consecutive=!1,this.audioController.VinLeftChannelMasterVolume=8,this.audioController.VinRightChannelMasterVolume=8,this.memory[yt]=this.joypad.initialValue}disableBootROM(){this.loadCartridgeRomIntoMemory(),this.usedGBCBootROM?this.cartridge.useGBCMode?this.recompileBootIOWriteHandling():this.adjustGBCtoGBMode():this.recompileBootIOWriteHandling()}setSpeed(t){this.cpu.setSpeed(t),this.initSound()}initSound(){this.audioController.connectDevice(this.audioDevice),this.audioController.setVolume(s.soundOn?s.soundVolume:0),this.audioController.initBuffer()}writeChannel3RAM(t,e){this.audioController.channel3canPlay&&this.audioController.runJIT(),this.memory[65328|t]=e,t<<=1,this.audioController.channel3PCM[t]=e>>4,this.audioController.channel3PCM[1|t]=15&e}run(){0==(2&this.stopEmulator)&&(1==(1&this.stopEmulator)?this.CPUStopped?(this.audioController.adjustUnderrun(),this.audioController.audioTicks+=this.cpu.cyclesTotal,this.audioController.runJIT(),this.stopEmulator|=1):(this.stopEmulator=0,this.audioController.adjustUnderrun(),this.cartridge.hasRTC&&this.cartridge.mbc.rtc.updateClock(),this.halt?(this.CPUTicks=0,this.calculateHALTPeriod(),this.halt?(this.updateCore(),this.iterationEndRoutine()):this.executeIteration()):this.executeIteration(),this.lcdDevice.requestDraw()):console.error("Iterator restarted a faulted core."))}executeIteration(){for(;0===this.stopEmulator;){switch(this.IRQEnableDelay){case 1:this.IME=!0,this.checkIRQMatching(),--this.IRQEnableDelay;break;case 2:--this.IRQEnableDelay}this.IRQLineMatched>0&&this.launchIRQ();const t=this.memoryReader[this.programCounter].apply(this,[this.programCounter]);this.programCounter=this.programCounter+1&65535,this.skipPCIncrement&&(this.programCounter=this.programCounter-1&65535,this.skipPCIncrement=!1),this.CPUTicks=O[t],z[t].apply(this);const e=this.CPUTicks>>this.doubleSpeedShifter;if(this.LCDTicks+=e,this.LCDCONTROL[this.actualScanLine](this),this.audioController.audioTicks+=e,this.cpu.ticks+=e,this.DIVTicks+=this.CPUTicks,this.TIMAEnabled)for(this.timerTicks+=this.CPUTicks;this.timerTicks>=this.TACClocker;)this.timerTicks-=this.TACClocker,256==++this.memory[65285]&&(this.memory[65285]=this.memory[65286],this.interruptsRequested|=4,this.checkIRQMatching());this.serialTimer>0&&(this.serialTimer-=this.CPUTicks,this.serialTimer<=0&&(this.interruptsRequested|=8,this.checkIRQMatching()),this.serialShiftTimer-=this.CPUTicks,this.serialShiftTimer<=0&&(this.serialShiftTimer=this.serialShiftTimerAllocated,this.memory[ft]=this.memory[ft]<<1&254|1)),this.cpu.ticks>=this.cpu.cyclesTotal&&this.iterationEndRoutine()}}iterationEndRoutine(){0==(1&this.stopEmulator)&&(this.audioController.runJIT(),this.memory[pt]=this.memory[pt]+(this.DIVTicks>>8)&255,this.DIVTicks&=255,this.stopEmulator|=1,this.cpu.ticks-=this.cpu.cyclesTotal,this.cpu.cyclesTotalCurrent+=this.cpu.cyclesTotalRoundoff,this.recalculateIterationClockLimit())}handleSTOP(){this.CPUStopped=!0,this.iterationEndRoutine(),this.cpu.ticks<0&&(this.audioController.audioTicks-=this.cpu.ticks,this.audioController.runJIT())}recalculateIterationClockLimit(){const t=this.cpu.cyclesTotalCurrent%4;this.cpu.cyclesTotal=this.cpu.cyclesTotalBase+this.cpu.cyclesTotalCurrent-t,this.cpu.cyclesTotalCurrent=t}scanLineMode2(){1!==this.STATTracker&&(this.mode2TriggerSTAT&&(this.interruptsRequested|=2,this.checkIRQMatching()),this.STATTracker=1,this.modeSTAT=2)}scanLineMode3(){3!==this.modeSTAT&&(0===this.STATTracker&&this.mode2TriggerSTAT&&(this.interruptsRequested|=2,this.checkIRQMatching()),this.STATTracker=1,this.modeSTAT=3)}scanLineMode0(){0!==this.modeSTAT&&(2!==this.STATTracker&&(0===this.STATTracker&&(this.mode2TriggerSTAT&&(this.interruptsRequested|=2,this.checkIRQMatching()),this.modeSTAT=3),this.incrementScanLineQueue(),this.updateSpriteCount(this.actualScanLine),this.STATTracker=2),this.LCDTicks>=this.spriteCount&&(this.hdmaRunning&&this.executeHDMA(),this.mode0TriggerSTAT&&(this.interruptsRequested|=2,this.checkIRQMatching()),this.STATTracker=3,this.modeSTAT=0))}clocksUntilLYCMatch(){return 0!==this.memory[65349]?this.memory[65349]>this.actualScanLine?456*(this.memory[65349]-this.actualScanLine):456*(154-this.actualScanLine+this.memory[65349]):456*(153===this.actualScanLine&&0===this.memory[65348]?154:153-this.actualScanLine)+8}clocksUntilMode0(){switch(this.modeSTAT){case 0:return 143===this.actualScanLine?(this.updateSpriteCount(0),this.spriteCount+5016):(this.updateSpriteCount(this.actualScanLine+1),this.spriteCount+456);case 2:case 3:return this.updateSpriteCount(this.actualScanLine),this.spriteCount;case 1:return this.updateSpriteCount(0),this.spriteCount+456*(154-this.actualScanLine)}}updateSpriteCount(t){if(this.spriteCount=252,this.cartridge.useGBCMode&&this.gfxSpriteShow)for(var e=t+16,i=0,r=this.gfxSpriteNormalHeight?8:16,s=65024;s<65184&&this.spriteCount<312;s+=4)(i=e-this.memory[s])>-1&&i>this.doubleSpeedShifter,this.LCDCONTROL[this.actualScanLine](this);var t=this.CPUTicks>>this.doubleSpeedShifter;if(this.audioController.audioTicks+=t,this.cpu.ticks+=t,this.DIVTicks+=this.CPUTicks,this.TIMAEnabled)for(this.timerTicks+=this.CPUTicks;this.timerTicks>=this.TACClocker;)this.timerTicks-=this.TACClocker,256==++this.memory[65285]&&(this.memory[65285]=this.memory[65286],this.interruptsRequested|=4,this.checkIRQMatching());this.serialTimer>0&&(this.serialTimer-=this.CPUTicks,this.serialTimer<=0&&(this.interruptsRequested|=8,this.checkIRQMatching()),this.serialShiftTimer-=this.CPUTicks,this.serialShiftTimer<=0&&(this.serialShiftTimer=this.serialShiftTimerAllocated,this.memory[ft]=this.memory[ft]<<1&254|1))}updateCoreFull(){this.updateCore(),this.cpu.ticks>=this.cpu.cyclesTotal&&this.iterationEndRoutine()}initializeLCDController(){for(var t=0;t<154;)t<143?this.LINECONTROL[t]=(()=>{this.LCDTicks<80?this.scanLineMode2():this.LCDTicks<252?this.scanLineMode3():this.LCDTicks<456?this.scanLineMode0():(this.LCDTicks-=456,3!=this.STATTracker&&(2!=this.STATTracker&&(0===this.STATTracker&&this.mode2TriggerSTAT&&(this.interruptsRequested|=2),this.incrementScanLineQueue()),this.hdmaRunning&&this.executeHDMA(),this.mode0TriggerSTAT&&(this.interruptsRequested|=2)),this.actualScanLine=++this.memory[65348],this.actualScanLine===this.memory[65349]?(this.memory[65345]|=4,this.LYCMatchTriggerSTAT&&(this.interruptsRequested|=2)):this.memory[65345]&=123,this.checkIRQMatching(),this.STATTracker=0,this.modeSTAT=2,this.LINECONTROL[this.actualScanLine].apply(this))}):143===t?this.LINECONTROL[143]=(()=>{this.LCDTicks<80?this.scanLineMode2():this.LCDTicks<252?this.scanLineMode3():this.LCDTicks<456?this.scanLineMode0():(this.LCDTicks-=456,3!=this.STATTracker&&(2!=this.STATTracker&&(0===this.STATTracker&&this.mode2TriggerSTAT&&(this.interruptsRequested|=2),this.incrementScanLineQueue()),this.hdmaRunning&&this.executeHDMA(),this.mode0TriggerSTAT&&(this.interruptsRequested|=2)),this.actualScanLine=this.memory[65348]=144,144===this.memory[65349]?(this.memory[65345]|=4,this.LYCMatchTriggerSTAT&&(this.interruptsRequested|=2)):this.memory[65345]&=123,this.STATTracker=0,this.modeSTAT=1,this.interruptsRequested|=this.mode1TriggerSTAT?3:1,this.checkIRQMatching(),0===this.drewBlank?(this.cpu.totalLinesPassed<144||144===this.cpu.totalLinesPassed&&this.midScanlineOffset>-1)&&(this.graphicsJITVBlank(),this.lcdDevice.prepareFrame()):--this.drewBlank,this.LINECONTROL[144].apply(this))}):t<153?this.LINECONTROL[t]=(()=>{this.LCDTicks>=456&&(this.LCDTicks-=456,this.actualScanLine=++this.memory[65348],this.actualScanLine===this.memory[65349]?(this.memory[65345]|=4,this.LYCMatchTriggerSTAT&&(this.interruptsRequested|=2,this.checkIRQMatching())):this.memory[65345]&=123,this.LINECONTROL[this.actualScanLine].apply(this))}):this.LINECONTROL[153]=(()=>{this.LCDTicks>=8&&(4!=this.STATTracker&&153===this.memory[65348]&&(this.memory[65348]=0,0===this.memory[65349]?(this.memory[65345]|=4,this.LYCMatchTriggerSTAT&&(this.interruptsRequested|=2,this.checkIRQMatching())):this.memory[65345]&=123,this.STATTracker=4),this.LCDTicks>=456&&(this.LCDTicks-=456,this.STATTracker=this.actualScanLine=0,this.LINECONTROL[0].apply(this)))}),++t}executeHDMA(){this.DMAWrite(1),this.halt?this.LCDTicks-this.spriteCount<(4>>this.doubleSpeedShifter|32)&&(this.CPUTicks=4+(32+this.spriteCount<>this.doubleSpeedShifter|32)):this.LCDTicks+=4>>this.doubleSpeedShifter|32,0===this.memory[65365]?(this.hdmaRunning=!1,this.memory[65365]=255):--this.memory[65365]}updateClock(){this.cartridge.mbc&&this.cartridge.mbc.rtc&&this.cartridge.mbc.rtc.updateClock()}renderScanLine(t){if(this.pixelStart=160*t,this.bgEnabled)this.pixelEnd=160,this.renderBGLayer(t),this.renderWindowLayer(t);else{const e=160*(t+1),i=this.cartridge.useGBCMode||this.colorizedGBPalettes?16316664:15728606;for(let r=160*t+this.currentX;r=82)){if(this.pixelEnd=this.LCDTicks-74,this.pixelEnd=Math.min(this.pixelEnd-this.midScanlineOffset-this.pixelEnd%8,160),this.bgEnabled)this.pixelStart=160*this.lastUnrenderedLine,this.renderBGLayer(this.lastUnrenderedLine),this.renderWindowLayer(this.lastUnrenderedLine);else for(var t=160*this.lastUnrenderedLine+this.pixelEnd,e=this.cartridge.useGBCMode||this.colorizedGBPalettes?16316664:15728606,i=160*this.lastUnrenderedLine+this.currentX;i0?this.BGCHRBank2:this.BGCHRBank1,this.tileCache=this.generateCacheArray(3968)):(this.gbOBJPalette=m(8,0,"int32"),this.gbBGPalette=m(4,0,"int32"),this.BGPalette=this.gbBGPalette,this.OBJPalette=this.gbOBJPalette,this.tileCache=this.generateCacheArray(1792),this.sortBuffer=m(256,0,"uint8"),this.OAMAddressCache=m(10,0,"int32")),this.renderPathBuild()}adjustGBCtoGBMode(){console.log("Stepping down from GBC mode."),this.VRAM=this.GBCMemory=this.BGCHRCurrentBank=this.BGCHRBank2=null,this.tileCache.length=1792,s.colorizeGBMode?(this.gbBGColorizedPalette=m(4,0,"int32"),this.gbOBJColorizedPalette=m(8,0,"int32"),this.cachedBGPaletteConversion=m(4,0,"int32"),this.cachedOBJPaletteConversion=m(8,0,"int32"),this.BGPalette=this.gbBGColorizedPalette,this.OBJPalette=this.gbOBJColorizedPalette,this.gbOBJPalette=this.gbBGPalette=null,this.getGBCColor()):(this.gbOBJPalette=m(8,0,"int32"),this.gbBGPalette=m(4,0,"int32"),this.BGPalette=this.gbBGPalette,this.OBJPalette=this.gbOBJPalette),this.sortBuffer=m(256,0,"uint8"),this.OAMAddressCache=m(10,0,"int32"),this.renderPathBuild(),this.jumpCompile()}renderPathBuild(){this.cartridge.useGBCMode?(this.priorityFlaggingPathRebuild(),this.renderSpriteLayer=this.renderSpriteGBCLayer):(this.renderBGLayer=this.renderBGGBLayer,this.renderWindowLayer=this.renderWindowGBLayer,this.renderSpriteLayer=this.renderSpriteGBLayer)}priorityFlaggingPathRebuild(){this.hasBGPriority?(this.renderBGLayer=this.BGGBCLayerRender,this.renderWindowLayer=this.WindowGBCLayerRender):(this.renderBGLayer=this.BGGBCLayerRenderNoPriorityFlagging,this.renderWindowLayer=this.WindowGBCLayerRenderNoPriorityFlagging)}initializeReferencesFromSaveState(){if(this.LCDCONTROL=this.LCDisOn?this.LINECONTROL:this.DISPLAYOFFCONTROL,this.cartridge.useGBCMode){this.BGCHRCurrentBank=this.currVRAMBank>0?this.BGCHRBank2:this.BGCHRBank1,this.tileCache=this.generateCacheArray(3968);for(let t=0;t<6144;t+=16)this.generateGBCTileBank1(t),this.generateGBCTileBank2(t)}else{this.colorizedGBPalettes?(this.BGPalette=this.gbBGColorizedPalette,this.OBJPalette=this.gbOBJColorizedPalette,this.updateGBBGPalette=this.updateGBColorizedBGPalette,this.updateGBOBJPalette=this.updateGBColorizedOBJPalette):(this.BGPalette=this.gbBGPalette,this.OBJPalette=this.gbOBJPalette),this.tileCache=this.generateCacheArray(1792);for(let t=32768;t<36864;t+=2)this.generateGBOAMTileLine(t);for(let t=36864;t<38912;t+=2)this.generateGBTileLine(t);this.sortBuffer=m(256,0,"uint8"),this.OAMAddressCache=m(10,0,"int32")}this.renderPathBuild()}adjustRGBTint(t){const e=31&t,i=t>>5&31,r=t>>10&31;return 13*e+2*i+r>>1<<16|3*i+r<<9|3*e+2*i+11*r>>1}getGBCColor(){for(let t=0;t<4;t++){const e=t<<1;this.cachedBGPaletteConversion[t]=this.adjustRGBTint(this.gbcBGRawPalette[1|e]<<8|this.gbcBGRawPalette[e]),this.cachedOBJPaletteConversion[t]=this.adjustRGBTint(this.gbcOBJRawPalette[1|e]<<8|this.gbcOBJRawPalette[e])}for(let t=4;t<8;t++){const e=t<<1;this.cachedOBJPaletteConversion[t]=this.adjustRGBTint(this.gbcOBJRawPalette[1|e]<<8|this.gbcOBJRawPalette[e])}this.updateGBBGPalette=this.updateGBColorizedBGPalette,this.updateGBOBJPalette=this.updateGBColorizedOBJPalette,this.updateGBBGPalette(this.memory[65351]),this.updateGBOBJPalette(0,this.memory[65352]),this.updateGBOBJPalette(1,this.memory[65353]),this.colorizedGBPalettes=!0}updateGBRegularBGPalette(t){this.gbBGPalette[0]=33554432|this.colors[3&t],this.gbBGPalette[1]=this.colors[t>>2&3],this.gbBGPalette[2]=this.colors[t>>4&3],this.gbBGPalette[3]=this.colors[t>>6]}updateGBColorizedBGPalette(t){this.gbBGColorizedPalette[0]=33554432|this.cachedBGPaletteConversion[3&t],this.gbBGColorizedPalette[1]=this.cachedBGPaletteConversion[t>>2&3],this.gbBGColorizedPalette[2]=this.cachedBGPaletteConversion[t>>4&3],this.gbBGColorizedPalette[3]=this.cachedBGPaletteConversion[t>>6]}updateGBRegularOBJPalette(t,e){this.gbOBJPalette[1|t]=this.colors[e>>2&3],this.gbOBJPalette[2|t]=this.colors[e>>4&3],this.gbOBJPalette[3|t]=this.colors[e>>6]}updateGBColorizedOBJPalette(t,e){this.gbOBJColorizedPalette[1|t]=this.cachedOBJPaletteConversion[t|e>>2&3],this.gbOBJColorizedPalette[2|t]=this.cachedOBJPaletteConversion[t|e>>4&3],this.gbOBJColorizedPalette[3|t]=this.cachedOBJPaletteConversion[t|e>>6]}updateGBCBGPalette(t,e){this.gbcBGRawPalette[t]!=e&&(this.midScanLineJIT(),this.gbcBGRawPalette[t]=e,0==(6&t)?(e=33554432|this.adjustRGBTint(this.gbcBGRawPalette[1|t]<<8|this.gbcBGRawPalette[62&t]),t>>=1,this.gbcBGPalette[t]=e,this.gbcBGPalette[32|t]=16777216|e):(e=this.adjustRGBTint(this.gbcBGRawPalette[1|t]<<8|this.gbcBGRawPalette[62&t]),t>>=1,this.gbcBGPalette[t]=e,this.gbcBGPalette[32|t]=16777216|e))}updateGBCOBJPalette(t,e){this.gbcOBJRawPalette[t]!==e&&(this.gbcOBJRawPalette[t]=e,(6&t)>0&&(this.midScanLineJIT(),this.gbcOBJPalette[t>>1]=16777216|this.adjustRGBTint(this.gbcOBJRawPalette[1|t]<<8|this.gbcOBJRawPalette[62&t])))}renderBGGBLayer(t){var e=this.backgroundY+t&255,i=(7&e)<<3,r=this.gfxBackgroundCHRBankPosition|(248&e)<<2,s=this.backgroundX+this.currentX&255,h=this.pixelStart+this.currentX,a=this.pixelStart+(this.gfxWindowDisplay&&t-this.windowY>=0?Math.min(Math.max(this.windowX,0)+this.currentX,this.pixelEnd):this.pixelEnd),n=r+(s>>3),o=this.BGCHRBank1[n];o>3;for(s+=m<<3,m+=n;n>3)+r;r=0?Math.min(Math.max(this.windowX,0)+this.currentX,this.pixelEnd):this.pixelEnd),n=r+(s>>3),o=this.BGCHRBank1[n];o>2,u=7&s;u<8&&h>3;for(s+=g<<3,g+=n;n>2,u=i,this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u]];if(h>2,u=i-1;h>3)+r;r>2,u=i,this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u++]],this.frameBuffer[h++]=this.gbcBGPalette[m|l[u]];if(h>2,a-h){case 7:this.frameBuffer[h+6]=this.gbcBGPalette[m|l[6|i]];case 6:this.frameBuffer[h+5]=this.gbcBGPalette[m|l[5|i]];case 5:this.frameBuffer[h+4]=this.gbcBGPalette[m|l[4|i]];case 4:this.frameBuffer[h+3]=this.gbcBGPalette[m|l[3|i]];case 3:this.frameBuffer[h+2]=this.gbcBGPalette[m|l[2|i]];case 2:this.frameBuffer[h+1]=this.gbcBGPalette[m|l[1|i]];case 1:this.frameBuffer[h]=this.gbcBGPalette[m|l[i]]}}}BGGBCLayerRenderNoPriorityFlagging(t){var e=this.backgroundY+t&255,i=(7&e)<<3,r=this.gfxBackgroundCHRBankPosition|(248&e)<<2,s=this.backgroundX+this.currentX&255,h=this.pixelStart+this.currentX,a=this.pixelStart+(this.gfxWindowDisplay&&t-this.windowY>=0?Math.min(Math.max(this.windowX,0)+this.currentX,this.pixelEnd):this.pixelEnd),n=r+(s>>3),o=this.BGCHRBank1[n];o>3;for(s+=g<<3,g+=n;n>3)+r;r=0){var i=this.windowX>0?this.windowX+this.currentX:this.currentX,r=this.pixelStart+i,s=this.pixelStart+this.pixelEnd;if(r>3),n=this.BGCHRBank1[a];n>3);a=0){var i=this.windowX>0?this.windowX+this.currentX:this.currentX,r=this.pixelStart+i,s=this.pixelStart+this.pixelEnd;if(r>3),n=this.BGCHRBank1[a];n>2,m=i-this.windowX&7;for(i=Math.min(8,m+s-r);m>3);a>2,m=h,this.frameBuffer[r++]=this.gbcBGPalette[l|c[m++]],this.frameBuffer[r++]=this.gbcBGPalette[l|c[m++]],this.frameBuffer[r++]=this.gbcBGPalette[l|c[m++]],this.frameBuffer[r++]=this.gbcBGPalette[l|c[m++]],this.frameBuffer[r++]=this.gbcBGPalette[l|c[m++]],this.frameBuffer[r++]=this.gbcBGPalette[l|c[m++]],this.frameBuffer[r++]=this.gbcBGPalette[l|c[m++]],this.frameBuffer[r++]=this.gbcBGPalette[l|c[m]];if(r>2,s-r){case 7:this.frameBuffer[r+6]=this.gbcBGPalette[l|c[6|h]];case 6:this.frameBuffer[r+5]=this.gbcBGPalette[l|c[5|h]];case 5:this.frameBuffer[r+4]=this.gbcBGPalette[l|c[4|h]];case 4:this.frameBuffer[r+3]=this.gbcBGPalette[l|c[3|h]];case 3:this.frameBuffer[r+2]=this.gbcBGPalette[l|c[2|h]];case 2:this.frameBuffer[r+1]=this.gbcBGPalette[l|c[1|h]];case 1:this.frameBuffer[r]=this.gbcBGPalette[l|c[h]]}}}}}WindowGBCLayerRenderNoPriorityFlagging(t){if(this.gfxWindowDisplay){var e=t-this.windowY;if(e>=0){var i=this.windowX>0?this.windowX+this.currentX:this.currentX,r=this.pixelStart+i,s=this.pixelStart+this.pixelEnd;if(r>3),n=this.BGCHRBank1[a];n>3);a>2,c=this.tileCache[(96&n)<<4|this.memory[2|i]],g=h=this.memory[1|i],a=Math.min(168-g,8),s=g>7?0:8-g,u=this.pixelStart+(g>8?g-8:0);sh&&(this.frameBuffer[u]>=33554432?(l=c[r|s])>0&&(this.frameBuffer[u]=this.OBJPalette[o|l],this.sortBuffer[g]=h):this.frameBuffer[u]<16777216&&(l=c[r|s])>0&&n<128&&(this.frameBuffer[u]=this.OBJPalette[o|l],this.sortBuffer[g]=h));else for(let t=this.findLowestSpriteDrawable(e,15);m>2,c=(64&n)==(64&r)?this.tileCache[(96&n)<<4|254&this.memory[2|i]]:this.tileCache[(96&n)<<4|this.memory[2|i]|1],r&=63,g=h=this.memory[1|i],a=Math.min(168-g,8),s=g>7?0:8-g,u=this.pixelStart+(g>8?g-8:0);sh&&(this.frameBuffer[u]>=33554432?(l=c[r|s])>0&&(this.frameBuffer[u]=this.OBJPalette[o|l],this.sortBuffer[g]=h):this.frameBuffer[u]<16777216&&(l=c[r|s])>0&&n<128&&(this.frameBuffer[u]=this.OBJPalette[o|l],this.sortBuffer[g]=h))}}findLowestSpriteDrawable(t,e){for(var i=65024,r=0,s=0;i<65184&&r<10;)((s=t-this.memory[i])&e)===s&&(this.OAMAddressCache[r++]=i),i+=4;return r}renderSpriteGBCLayer(t){if(this.gfxSpriteShow){var e=65024,i=t+16,r=0,s=0,h=0,a=0,n=0,o=0,c=null,l=0,m=0,u=0;if(this.gfxSpriteNormalHeight){for(;e<65184&&u<10;e+=4)if((7&(r=i-this.memory[e]))===r){for(s=this.memory[1|e]-8,h=Math.min(160,s+8),o=(7&(n=this.memory[3|e]))<<2,c=this.tileCache[(8&n)<<8|(96&n)<<4|this.memory[2|e]],a=s>0?s:0,s-=r<<3,m=this.pixelStart+a;a=33554432?(l=c[a-s])>0&&(this.frameBuffer[m]=this.gbcOBJPalette[o|l]):this.frameBuffer[m]<16777216&&(l=c[a-s])>0&&n<128&&(this.frameBuffer[m]=this.gbcOBJPalette[o|l]);++u}}else for(;e<65184&&u<10;e+=4)if((15&(r=i-this.memory[e]))===r){for(s=this.memory[1|e]-8,h=Math.min(160,s+8),o=(7&(n=this.memory[3|e]))<<2,c=(64&n)==(64&r<<3)?this.tileCache[(8&n)<<8|(96&n)<<4|254&this.memory[2|e]]:this.tileCache[(8&n)<<8|(96&n)<<4|this.memory[2|e]|1],a=s>0?s:0,s-=(7&r)<<3,m=this.pixelStart+a;a=33554432?(l=c[a-s])>0&&(this.frameBuffer[m]=this.gbcOBJPalette[o|l]):this.frameBuffer[m]<16777216&&(l=c[a-s])>0&&n<128&&(this.frameBuffer[m]=this.gbcOBJPalette[o|l]);++u}}}generateGBTileLine(t){var e=this.memory[1|t]<<8|this.memory[40958&t],i=this.tileCache[(8176&t)>>4];i[7|(t=(14&t)<<2)]=(256&e)>>7|1&e,i[6|t]=(512&e)>>8|(2&e)>>1,i[5|t]=(1024&e)>>9|(4&e)>>2,i[4|t]=(2048&e)>>10|(8&e)>>3,i[3|t]=(4096&e)>>11|(16&e)>>4,i[2|t]=(8192&e)>>12|(32&e)>>5,i[1|t]=(16384&e)>>13|(64&e)>>6,i[t]=(32768&e)>>14|(128&e)>>7}generateGBCTileLineBank1(t){var e=this.memory[1|t]<<8|this.memory[40958&t];t&=8190;var i=this.tileCache[t>>4],r=this.tileCache[512|t>>4],s=this.tileCache[1024|t>>4],h=this.tileCache[1536|t>>4],a=56-(t=(14&t)<<2);h[a]=r[t]=s[7|a]=i[7|t]=(256&e)>>7|1&e,h[1|a]=r[1|t]=s[6|a]=i[6|t]=(512&e)>>8|(2&e)>>1,h[2|a]=r[2|t]=s[5|a]=i[5|t]=(1024&e)>>9|(4&e)>>2,h[3|a]=r[3|t]=s[4|a]=i[4|t]=(2048&e)>>10|(8&e)>>3,h[4|a]=r[4|t]=s[3|a]=i[3|t]=(4096&e)>>11|(16&e)>>4,h[5|a]=r[5|t]=s[2|a]=i[2|t]=(8192&e)>>12|(32&e)>>5,h[6|a]=r[6|t]=s[1|a]=i[1|t]=(16384&e)>>13|(64&e)>>6,h[7|a]=r[7|t]=s[a]=i[t]=(32768&e)>>14|(128&e)>>7}generateGBCTileBank1(t){var e=t>>4,i=this.tileCache[e],r=this.tileCache[512|e],s=this.tileCache[1024|e],h=this.tileCache[1536|e],a=0;t|=32768,e=0;var n=56;do{a=this.memory[1|t]<<8|this.memory[t],h[n]=r[e]=s[7|n]=i[7|e]=(256&a)>>7|1&a,h[1|n]=r[1|e]=s[6|n]=i[6|e]=(512&a)>>8|(2&a)>>1,h[2|n]=r[2|e]=s[5|n]=i[5|e]=(1024&a)>>9|(4&a)>>2,h[3|n]=r[3|e]=s[4|n]=i[4|e]=(2048&a)>>10|(8&a)>>3,h[4|n]=r[4|e]=s[3|n]=i[3|e]=(4096&a)>>11|(16&a)>>4,h[5|n]=r[5|e]=s[2|n]=i[2|e]=(8192&a)>>12|(32&a)>>5,h[6|n]=r[6|e]=s[1|n]=i[1|e]=(16384&a)>>13|(64&a)>>6,h[7|n]=r[7|e]=s[n]=i[e]=(32768&a)>>14|(128&a)>>7,e+=8,n-=8,t+=2}while(n>-1)}generateGBCTileLineBank2(t){var e=this.VRAM[1|t]<<8|this.VRAM[8190&t],i=this.tileCache[2048|t>>4],r=this.tileCache[2560|t>>4],s=this.tileCache[3072|t>>4],h=this.tileCache[3584|t>>4],a=56-(t=(14&t)<<2);h[a]=r[t]=s[7|a]=i[7|t]=(256&e)>>7|1&e,h[1|a]=r[1|t]=s[6|a]=i[6|t]=(512&e)>>8|(2&e)>>1,h[2|a]=r[2|t]=s[5|a]=i[5|t]=(1024&e)>>9|(4&e)>>2,h[3|a]=r[3|t]=s[4|a]=i[4|t]=(2048&e)>>10|(8&e)>>3,h[4|a]=r[4|t]=s[3|a]=i[3|t]=(4096&e)>>11|(16&e)>>4,h[5|a]=r[5|t]=s[2|a]=i[2|t]=(8192&e)>>12|(32&e)>>5,h[6|a]=r[6|t]=s[1|a]=i[1|t]=(16384&e)>>13|(64&e)>>6,h[7|a]=r[7|t]=s[a]=i[t]=(32768&e)>>14|(128&e)>>7}generateGBCTileBank2(t){var e=t>>4,i=this.tileCache[2048|e],r=this.tileCache[2560|e],s=this.tileCache[3072|e],h=this.tileCache[3584|e],a=0;e=0;var n=56;do{a=this.VRAM[1|t]<<8|this.VRAM[t],h[n]=r[e]=s[7|n]=i[7|e]=(256&a)>>7|1&a,h[1|n]=r[1|e]=s[6|n]=i[6|e]=(512&a)>>8|(2&a)>>1,h[2|n]=r[2|e]=s[5|n]=i[5|e]=(1024&a)>>9|(4&a)>>2,h[3|n]=r[3|e]=s[4|n]=i[4|e]=(2048&a)>>10|(8&a)>>3,h[4|n]=r[4|e]=s[3|n]=i[3|e]=(4096&a)>>11|(16&a)>>4,h[5|n]=r[5|e]=s[2|n]=i[2|e]=(8192&a)>>12|(32&a)>>5,h[6|n]=r[6|e]=s[1|n]=i[1|e]=(16384&a)>>13|(64&a)>>6,h[7|n]=r[7|e]=s[n]=i[e]=(32768&a)>>14|(128&a)>>7,e+=8,n-=8,t+=2}while(n>-1)}generateGBOAMTileLine(t){var e=this.memory[1|t]<<8|this.memory[40958&t];t&=8190;var i=this.tileCache[t>>4],r=this.tileCache[512|t>>4],s=this.tileCache[1024|t>>4],h=this.tileCache[1536|t>>4],a=56-(t=(14&t)<<2);h[a]=r[t]=s[7|a]=i[7|t]=(256&e)>>7|1&e,h[1|a]=r[1|t]=s[6|a]=i[6|t]=(512&e)>>8|(2&e)>>1,h[2|a]=r[2|t]=s[5|a]=i[5|t]=(1024&e)>>9|(4&e)>>2,h[3|a]=r[3|t]=s[4|a]=i[4|t]=(2048&e)>>10|(8&e)>>3,h[4|a]=r[4|t]=s[3|a]=i[3|t]=(4096&e)>>11|(16&e)>>4,h[5|a]=r[5|t]=s[2|a]=i[2|t]=(8192&e)>>12|(32&e)>>5,h[6|a]=r[6|t]=s[1|a]=i[1|t]=(16384&e)>>13|(64&e)>>6,h[7|a]=r[7|t]=s[a]=i[t]=(32768&e)>>14|(128&e)>>7}graphicsJIT(){this.LCDisOn&&(this.cpu.totalLinesPassed=0,this.graphicsJITScanlineGroup())}graphicsJITVBlank(){this.cpu.totalLinesPassed+=this.queuedScanLines,this.graphicsJITScanlineGroup()}graphicsJITScanlineGroup(){for(;this.queuedScanLines>0;)this.renderScanLine(this.lastUnrenderedLine),this.lastUnrenderedLine<143?++this.lastUnrenderedLine:this.lastUnrenderedLine=0,--this.queuedScanLines}incrementScanLineQueue(){this.queuedScanLines<144?++this.queuedScanLines:(this.currentX=0,this.midScanlineOffset=-1,this.lastUnrenderedLine<143?++this.lastUnrenderedLine:this.lastUnrenderedLine=0)}midScanLineJIT(){this.graphicsJIT(),this.renderMidScanLine()}launchIRQ(){var t=0,e=1;do{if((e&this.IRQLineMatched)===e)return this.IME=!1,this.interruptsRequested-=e,this.IRQLineMatched=0,this.CPUTicks=20,this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,this.programCounter>>8]),this.stackPointer=this.stackPointer-1&65535,this.memoryWriter[this.stackPointer].apply(this,[this.stackPointer,255&this.programCounter]),this.programCounter=64|t<<3,void this.updateCore();e=1<<++t}while(t<5)}checkIRQMatching(){this.IME&&(this.IRQLineMatched=this.interruptsEnabled&this.interruptsRequested&31)}calculateHALTPeriod(){if(this.halt)t=this.remainingClocks;else{this.halt=!0;var t=-1;if(this.LCDisOn&&(1==(1&this.interruptsEnabled)&&(t=456*((1===this.modeSTAT?298:144)-this.actualScanLine)-this.LCDTicks<=143?456*(154-this.actualScanLine):456)-this.LCDTicks<0&&8==(8&this.interruptsEnabled)&&(this.serialTimer<=t||-1===t)&&(t=this.serialTimer)}var e=this.cpu.cyclesTotal-this.cpu.ticks<=0?t<=e?(this.CPUTicks=Math.max(t,this.CPUTicks),this.updateCoreFull(),this.halt=!1,this.CPUTicks=0):(this.CPUTicks=Math.max(e,this.CPUTicks),this.remainingClocks=t-this.CPUTicks):this.CPUTicks+=e}memoryRead(t){return 65535=40960&&t<49152)this.cartridge.mbc&&0===this.cartridge.mbc.ramSize?this.memoryReader[t]=this.badMemoryRead:this.cartridge.hasMBC7?this.memoryReader[t]=this.memoryReadMBC7:this.cartridge.hasMBC3?this.memoryReader[t]=this.memoryReadMBC3:this.memoryReader[t]=this.memoryReadMBC;else if(t>=49152&&t<57344)!this.cartridge.useGBCMode||t<53248?this.memoryReader[t]=this.memoryReadNormal:this.memoryReader[t]=this.memoryReadGBCMemory;else if(t>=57344&&t<65024)!this.cartridge.useGBCMode||t<61440?this.memoryReader[t]=this.memoryReadECHONormal:this.memoryReader[t]=this.memoryReadECHOGBCMemory;else if(t<65184)this.memoryReader[t]=this.memoryReadOAM;else if(this.cartridge.useGBCMode&&t>=65184&&t<65280)this.memoryReader[t]=this.memoryReadNormal;else if(t>=65280)switch(t){case yt:this.memoryHighReader[0]=this.memoryReader[yt]=(t=>192|this.memory[yt]);break;case ft:this.memoryHighReader[1]=this.memoryReader[ft]=(t=>this.memory[Ct]<128?this.memory[ft]:255);break;case Ct:this.cartridge.useGBCMode?this.memoryHighReader[2]=this.memoryReader[Ct]=(t=>(this.serialTimer<=0?124:252)|this.memory[Ct]):this.memoryHighReader[2]=this.memoryReader[Ct]=(t=>(this.serialTimer<=0?126:254)|this.memory[Ct]);break;case 65283:this.memoryHighReader[3]=this.memoryReader[65283]=this.badMemoryRead;break;case pt:this.memoryHighReader[4]=this.memoryReader[pt]=(t=>(this.memory[pt]=this.memory[pt]+(this.DIVTicks>>8)&255,this.DIVTicks&=255,this.memory[pt]));break;case 65285:case 65286:this.memoryHighReader[255&t]=this.memoryHighReadNormal,this.memoryReader[t]=this.memoryReadNormal;break;case 65287:this.memoryHighReader[7]=this.memoryReader[65287]=(t=>248|this.memory[65287]);break;case 65288:case 65289:case 65290:case 65291:case 65292:case 65293:case 65294:this.memoryHighReader[255&t]=this.memoryReader[t]=this.badMemoryRead;break;case 65295:this.memoryHighReader[15]=this.memoryReader[65295]=(t=>224|this.interruptsRequested);break;case 65296:this.memoryHighReader[16]=this.memoryReader[65296]=(t=>128|this.memory[65296]);break;case 65297:this.memoryHighReader[17]=this.memoryReader[65297]=(t=>63|this.memory[65297]);break;case 65298:this.memoryHighReader[18]=this.memoryHighReadNormal,this.memoryReader[65298]=this.memoryReadNormal;break;case 65299:this.memoryHighReader[19]=this.memoryReader[65299]=this.badMemoryRead;break;case 65300:this.memoryHighReader[20]=this.memoryReader[65300]=(t=>191|this.memory[65300]);break;case 65301:this.memoryHighReader[21]=this.badMemoryRead,this.memoryReader[65301]=this.badMemoryRead;break;case 65302:this.memoryHighReader[22]=this.memoryReader[65302]=(t=>63|this.memory[65302]);break;case 65303:this.memoryHighReader[23]=this.memoryHighReadNormal,this.memoryReader[65303]=this.memoryReadNormal;break;case 65304:this.memoryHighReader[24]=this.memoryReader[65304]=this.badMemoryRead;break;case 65305:this.memoryHighReader[25]=this.memoryReader[65305]=(t=>191|this.memory[65305]);break;case 65306:this.memoryHighReader[26]=this.memoryReader[65306]=(t=>127|this.memory[65306]);break;case 65307:this.memoryHighReader[27]=this.memoryReader[65307]=this.badMemoryRead;break;case 65308:this.memoryHighReader[28]=this.memoryReader[65308]=(t=>159|this.memory[65308]);break;case 65309:this.memoryHighReader[29]=this.memoryReader[65309]=this.badMemoryRead;break;case 65310:this.memoryHighReader[30]=this.memoryReader[65310]=(t=>191|this.memory[65310]);break;case 65311:case 65312:this.memoryHighReader[255&t]=this.memoryReader[t]=this.badMemoryRead;break;case 65313:case 65314:this.memoryHighReader[255&t]=this.memoryHighReadNormal,this.memoryReader[t]=this.memoryReadNormal;break;case 65315:this.memoryHighReader[35]=this.memoryReader[65315]=(t=>191|this.memory[65315]);break;case 65316:case 65317:this.memoryHighReader[255&t]=this.memoryHighReadNormal,this.memoryReader[t]=this.memoryReadNormal;break;case 65318:this.memoryHighReader[38]=this.memoryReader[65318]=(t=>(this.audioController.runJIT(),112|this.memory[65318]));break;case 65319:case 65320:case 65321:case 65322:case 65323:case 65324:case 65325:case 65326:case 65327:this.memoryHighReader[255&t]=this.memoryReader[t]=this.badMemoryRead;break;case 65328:case 65329:case 65330:case 65331:case 65332:case 65333:case 65334:case 65335:case 65336:case 65337:case 65338:case 65339:case 65340:case 65341:case 65342:case 65343:this.memoryReader[t]=(t=>this.audioController.channel3canPlay?this.memory[65280|this.audioController.channel3lastSampleLookup>>1]:this.memory[t]),this.memoryHighReader[255&t]=(t=>this.audioController.channel3canPlay?this.memory[65280|this.audioController.channel3lastSampleLookup>>1]:this.memory[65280|t]);break;case 65344:this.memoryHighReader[64]=this.memoryHighReadNormal,this.memoryReader[65344]=this.memoryReadNormal;break;case 65345:this.memoryHighReader[65]=this.memoryReader[65345]=(t=>128|this.memory[65345]|this.modeSTAT);break;case 65346:this.memoryHighReader[66]=this.memoryReader[65346]=(t=>this.backgroundY);break;case 65347:this.memoryHighReader[67]=this.memoryReader[65347]=(t=>this.backgroundX);break;case 65348:this.memoryHighReader[68]=this.memoryReader[65348]=(t=>this.LCDisOn?this.memory[65348]:0);break;case 65349:case 65350:case 65351:case 65352:case 65353:this.memoryHighReader[255&t]=this.memoryHighReadNormal,this.memoryReader[t]=this.memoryReadNormal;break;case 65354:this.memoryHighReader[74]=this.memoryReader[65354]=(t=>this.windowY);break;case 65355:this.memoryHighReader[75]=this.memoryHighReadNormal,this.memoryReader[65355]=this.memoryReadNormal;break;case 65356:this.memoryHighReader[76]=this.memoryReader[65356]=this.badMemoryRead;break;case 65357:this.memoryHighReader[77]=this.memoryHighReadNormal,this.memoryReader[65357]=this.memoryReadNormal;break;case 65358:this.memoryHighReader[78]=this.memoryReader[65358]=this.badMemoryRead;break;case 65359:this.memoryHighReader[79]=this.memoryReader[65359]=(t=>this.currVRAMBank);break;case 65360:case 65361:case 65362:case 65363:case 65364:this.memoryHighReader[255&t]=this.memoryHighReadNormal,this.memoryReader[t]=this.memoryReadNormal;break;case 65365:this.cartridge.useGBCMode?this.memoryHighReader[85]=this.memoryReader[65365]=(t=>(!this.LCDisOn&&this.hdmaRunning&&(this.DMAWrite(1+(127&this.memory[65365])),this.memory[65365]=255,this.hdmaRunning=!1),this.memory[65365])):(this.memoryReader[65365]=this.memoryReadNormal,this.memoryHighReader[85]=this.memoryHighReadNormal);break;case 65366:this.cartridge.useGBCMode?this.memoryHighReader[86]=this.memoryReader[65366]=(t=>60|(this.memory[65366]>=192?2|193&this.memory[65366]:195&this.memory[65366])):(this.memoryReader[65366]=this.memoryReadNormal,this.memoryHighReader[86]=this.memoryHighReadNormal);break;case 65367:case 65368:case 65369:case 65370:case 65371:case 65372:case 65373:case 65374:case 65375:case 65376:case 65377:case 65378:case 65379:case 65380:case 65381:case 65382:case 65383:this.memoryHighReader[255&t]=this.memoryReader[t]=this.badMemoryRead;break;case 65384:case 65385:case 65386:case 65387:this.memoryHighReader[255&t]=this.memoryHighReadNormal,this.memoryReader[t]=this.memoryReadNormal;break;case 65388:this.cartridge.useGBCMode?this.memoryHighReader[108]=this.memoryReader[65388]=(t=>254|this.memory[65388]):this.memoryHighReader[108]=this.memoryReader[65388]=this.badMemoryRead;break;case 65389:case 65390:case 65391:this.memoryHighReader[255&t]=this.memoryReader[t]=this.badMemoryRead;break;case 65392:this.cartridge.useGBCMode?this.memoryHighReader[112]=this.memoryReader[65392]=(t=>64|this.memory[65392]):this.memoryHighReader[112]=this.memoryReader[65392]=this.badMemoryRead;break;case 65393:this.memoryHighReader[113]=this.memoryReader[65393]=this.badMemoryRead;break;case 65394:case 65395:this.memoryHighReader[255&t]=this.memoryReader[t]=this.memoryReadNormal;break;case 65396:this.cartridge.useGBCMode?this.memoryHighReader[116]=this.memoryReader[65396]=this.memoryReadNormal:this.memoryHighReader[116]=this.memoryReader[65396]=this.badMemoryRead;break;case 65397:this.memoryHighReader[117]=this.memoryReader[65397]=(t=>143|this.memory[65397]);break;case 65398:this.memoryHighReader[118]=this.memoryReader[65398]=(t=>(this.audioController.runJIT(),this.audioController.channel2envelopeVolume<<4|this.audioController.channel1envelopeVolume));break;case 65399:this.memoryHighReader[119]=this.memoryReader[65399]=(t=>(this.audioController.runJIT(),this.audioController.channel4envelopeVolume<<4|this.audioController.channel3envelopeVolume));break;case 65400:case 65401:case 65402:case 65403:case 65404:case 65405:case 65406:case 65407:this.memoryHighReader[255&t]=this.memoryReader[t]=this.badMemoryRead;break;case Bt:this.memoryHighReader[255]=this.memoryReader[Bt]=(t=>this.interruptsEnabled);break;default:this.memoryReader[t]=this.memoryReadNormal,this.memoryHighReader[255&t]=this.memoryHighReadNormal}else this.memoryReader[t]=this.badMemoryRead}memoryReadNormal(t){return this.memory[t]}memoryHighReadNormal(t){return this.memory[65280|t]}memoryReadROM(t){return this.cartridge.rom.getByte(this.cartridge.mbc.currentROMBank+t)}memoryReadMBC(t){return this.cartridge.mbc.readRAM(t)}memoryReadMBC7(t){return this.cartridge.mbc.readRAM(t)}memoryReadMBC3(t){return this.cartridge.mbc.readRAM(t)}memoryReadGBCMemory(t){return this.GBCMemory[t+this.gbcRamBankPosition]}memoryReadOAM(t){return this.modeSTAT>1?255:this.memory[t]}memoryReadECHOGBCMemory(t){return this.GBCMemory[t+this.gbcRamBankPositionECHO]}memoryReadECHONormal(t){return this.memory[t-8192]}badMemoryRead(t){return 255}VRAMDATAReadCGBCPU(t){return this.modeSTAT>2?255:0===this.currVRAMBank?this.memory[t]:this.VRAM[8191&t]}VRAMDATAReadDMGCPU(t){return this.modeSTAT>2?255:this.memory[t]}VRAMCHRReadCGBCPU(t){return this.modeSTAT>2?255:this.BGCHRCurrentBank[2047&t]}VRAMCHRReadDMGCPU(t){return this.modeSTAT>2?255:this.BGCHRBank1[2047&t]}memoryWrite(t,e){this.memoryWriter[t].apply(this,[t,e])}memoryHighWrite(t,e){this.memoryHighWriter[t].apply(this,[t,e])}memoryWriteJumpCompile(){for(var t=0;t<=65535;t++)t<=K?this.cartridge.hasMBC1?this.memoryWriter[t]=t<8192?this.MBCWriteEnable:t<16384?this.MBC1WriteROMBank:t<24576?this.MBC1WriteRAMBank:this.MBC1WriteType:this.cartridge.hasMBC2?this.memoryWriter[t]=t<4096?this.MBCWriteEnable:t>=8448&&t<8704?this.MBC2WriteROMBank:this.onIllegalWrite:this.cartridge.hasMBC3?this.memoryWriter[t]=t<8192?this.MBCWriteEnable:t<16384?this.MBC3WriteROMBank:t<24576?this.MBC3WriteRAMBank:this.MBC3WriteRTCLatch:this.cartridge.hasMBC5||this.cartridge.hasRUMBLE||this.cartridge.hasMBC7?this.memoryWriter[t]=t<8192?this.MBCWriteEnable:t<12288?this.MBC5WriteROMBankLow:t<16384?this.MBC5WriteROMBankHigh:t<24576?this.cartridge.hasRUMBLE?this.RUMBLEWriteRAMBank:this.MBC5WriteRAMBank:this.onIllegalWrite:this.cartridge.hasHuC3?this.memoryWriter[t]=t<8192?this.MBCWriteEnable:t<16384?this.MBC3WriteROMBank:t<24576?this.HuC3WriteRAMBank:this.onIllegalWrite:this.memoryWriter[t]=this.onIllegalWrite:t<=et?this.memoryWriter[t]=this.cartridge.useGBCMode?this.VRAMGBCDATAWrite:this.VRAMGBDATAWrite:t<38912?this.memoryWriter[t]=this.cartridge.useGBCMode?this.VRAMGBCDATAWrite:this.VRAMGBDATAUpperWrite:t<40960?this.memoryWriter[t]=this.cartridge.useGBCMode?this.VRAMGBCCHRMAPWrite:this.VRAMGBCHRMAPWrite:t<49152?this.cartridge.mbc&&0!==this.cartridge.mbc.ramSize?this.cartridge.hasMBC3?this.memoryWriter[t]=this.memoryWriteMBC3RAM:this.memoryWriter[t]=this.memoryWriteMBCRAM:this.memoryWriter[t]=this.onIllegalWrite:t<57344?this.cartridge.useGBCMode&&t>=53248?this.memoryWriter[t]=this.memoryWriteGBCRAM:this.memoryWriter[t]=this.memoryWriteNormal:t<65024?this.cartridge.useGBCMode&&t>=61440?this.memoryWriter[t]=this.memoryWriteECHOGBCRAM:this.memoryWriter[t]=this.memoryWriteECHONormal:t<=65184?this.memoryWriter[t]=this.memoryWriteOAMRAM:t<65280?this.cartridge.useGBCMode?this.memoryWriter[t]=this.memoryWriteNormal:this.memoryWriter[t]=this.onIllegalWrite:(this.memoryWriter[t]=this.memoryWriteNormal,this.memoryHighWriter[255&t]=this.memoryHighWriteNormal);this.registerWriteJumpCompile()}MBCWriteEnable(t,e){this.cartridge.mbc.writeEnable(t,e)}MBC1WriteROMBank(t,e){this.cartridge.mbc1.writeROMBank(t,e)}MBC1WriteRAMBank(t,e){this.cartridge.mbc1.writeRAMBank(t,e)}MBC1WriteType(t,e){this.cartridge.mbc1.writeType(t,e)}MBC2WriteROMBank(t,e){this.cartridge.mbc2.writeROMBank(t,e)}MBC3WriteROMBank(t,e){return this.cartridge.mbc3.writeROMBank(t,e)}MBC3WriteRAMBank(t,e){return this.cartridge.mbc3.writeRAMBank(t,e)}MBC3WriteRTCLatch(t,e){return this.cartridge.mbc3.rtc.writeLatch(t,e)}MBC5WriteROMBankLow(t,e){return this.cartridge.mbc5.writeROMBankLow(t,e)}MBC5WriteROMBankHigh(t,e){return this.cartridge.mbc5.writeROMBankHigh(t,e)}MBC5WriteRAMBank(t,e){return this.cartridge.mbc5.writeRAMBank(t,e)}RUMBLEWriteRAMBank(t,e){return this.cartridge.rumble.writeRAMBank(t,e)}HuC3WriteRAMBank(t,e){this.cartridge.mbc.currentMBCRAMBank=3&e,this.cartridge.mbc.currentRAMBankPosition=(this.cartridge.mbc.currentMBCRAMBank<<13)-40960}onIllegalWrite(t,e){}memoryWriteNormal(t,e){this.memory[t]=e}memoryHighWriteNormal(t,e){this.memory[65280|t]=e}memoryWriteMBCRAM(t,e){this.cartridge.mbc.writeRAM(t,e)}memoryWriteMBC3RAM(t,e){return this.cartridge.mbc.writeRAM(t,e)}memoryWriteGBCRAM(t,e){this.GBCMemory[t+this.gbcRamBankPosition]=e}memoryWriteOAMRAM(t,e){this.modeSTAT<2&&this.memory[t]!=e&&(this.graphicsJIT(),this.memory[t]=e)}memoryWriteECHOGBCRAM(t,e){this.GBCMemory[t+this.gbcRamBankPositionECHO]=e}memoryWriteECHONormal(t,e){this.memory[t-8192]=e}VRAMGBDATAWrite(t,e){this.modeSTAT<3&&this.memory[t]!=e&&(this.graphicsJIT(),this.memory[t]=e,this.generateGBOAMTileLine(t))}VRAMGBDATAUpperWrite(t,e){this.modeSTAT<3&&this.memory[t]!=e&&(this.graphicsJIT(),this.memory[t]=e,this.generateGBTileLine(t))}VRAMGBCDATAWrite(t,e){this.modeSTAT<3&&(0===this.currVRAMBank?this.memory[t]!=e&&(this.graphicsJIT(),this.memory[t]=e,this.generateGBCTileLineBank1(t)):(t&=8191,this.VRAM[t]!=e&&(this.graphicsJIT(),this.VRAM[t]=e,this.generateGBCTileLineBank2(t))))}VRAMGBCHRMAPWrite(t,e){this.modeSTAT<3&&(t&=2047,this.BGCHRBank1[t]!=e&&(this.graphicsJIT(),this.BGCHRBank1[t]=e))}VRAMGBCCHRMAPWrite(t,e){this.modeSTAT<3&&(t&=2047,this.BGCHRCurrentBank[t]!=e&&(this.graphicsJIT(),this.BGCHRCurrentBank[t]=e))}DMAWrite(t){this.halt||(this.CPUTicks+=4|t<<5<0);else{var s=this.VRAM;do{i<6144?(s[i]=this.memoryRead(e++),s[1|i]=this.memoryRead(e++),s[2|i]=this.memoryRead(e++),s[3|i]=this.memoryRead(e++),s[4|i]=this.memoryRead(e++),s[5|i]=this.memoryRead(e++),s[6|i]=this.memoryRead(e++),s[7|i]=this.memoryRead(e++),s[8|i]=this.memoryRead(e++),s[9|i]=this.memoryRead(e++),s[10|i]=this.memoryRead(e++),s[11|i]=this.memoryRead(e++),s[12|i]=this.memoryRead(e++),s[13|i]=this.memoryRead(e++),s[14|i]=this.memoryRead(e++),s[15|i]=this.memoryRead(e++),this.generateGBCTileBank2(i),i+=16):(i&=2032,this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),this.BGCHRBank2[i++]=this.memoryRead(e++),i=i+6144&8176),e&=65520,--t}while(t>0)}r[65361]=e>>8,r[65362]=240&e,r[65363]=i>>8,r[65364]=240&i}registerWriteJumpCompile(){this.memoryHighWriter[0]=this.memoryWriter[yt]=((t,e)=>{this.memory[yt]=48&e|(0==(32&e)?this.joypad.value>>4:15)&(0==(16&e)?15&this.joypad.value:15)}),this.memoryHighWriter[1]=this.memoryWriter[ft]=((t,e)=>{this.memory[Ct]<128&&(this.memory[ft]=e)}),this.memoryHighWriter[2]=this.memoryHighWriteNormal,this.memoryWriter[Ct]=this.memoryWriteNormal,this.memoryHighWriter[3]=this.memoryWriter[65283]=this.onIllegalWrite,this.memoryHighWriter[4]=this.memoryWriter[pt]=((t,e)=>{this.DIVTicks&=255,this.memory[pt]=0}),this.memoryHighWriter[5]=this.memoryWriter[65285]=((t,e)=>{this.memory[65285]=e}),this.memoryHighWriter[6]=this.memoryWriter[65286]=((t,e)=>{this.memory[65286]=e}),this.memoryHighWriter[7]=this.memoryWriter[65287]=((t,e)=>{this.memory[65287]=7&e,this.TIMAEnabled=4==(4&e),this.TACClocker=Math.pow(4,0!=(3&e)?3&e:4)<<2}),this.memoryHighWriter[8]=this.memoryWriter[65288]=this.onIllegalWrite,this.memoryHighWriter[9]=this.memoryWriter[65289]=this.onIllegalWrite,this.memoryHighWriter[10]=this.memoryWriter[65290]=this.onIllegalWrite,this.memoryHighWriter[11]=this.memoryWriter[65291]=this.onIllegalWrite,this.memoryHighWriter[12]=this.memoryWriter[65292]=this.onIllegalWrite,this.memoryHighWriter[13]=this.memoryWriter[65293]=this.onIllegalWrite,this.memoryHighWriter[14]=this.memoryWriter[65294]=this.onIllegalWrite,this.memoryHighWriter[15]=this.memoryWriter[65295]=((t,e)=>{this.interruptsRequested=e,this.checkIRQMatching()}),this.memoryHighWriter[16]=this.memoryWriter[65296]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel1decreaseSweep&&0==(8&e)&&this.audioController.channel1Swept&&(this.audioController.channel1SweepFault=!0),this.audioController.channel1lastTimeSweep=(112&e)>>4,this.audioController.channel1frequencySweepDivider=7&e,this.audioController.channel1decreaseSweep=8==(8&e),this.memory[65296]=e,this.audioController.checkChannel1Enable())}),this.memoryHighWriter[17]=this.memoryWriter[65297]=((t,e)=>{!this.soundMasterEnabled&&this.cartridge.useGBCMode||(this.soundMasterEnabled?this.audioController.runJIT():e&=63,this.audioController.channel1CachedDuty=Z[e>>6],this.audioController.channel1totalLength=64-(63&e),this.memory[65297]=e,this.audioController.checkChannel1Enable())}),this.memoryHighWriter[18]=this.memoryWriter[65298]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel1Enabled&&0===this.audioController.channel1envelopeSweeps&&(8==(8&(this.memory[65298]^e))?(0==(8&this.memory[65298])&&(7==(7&this.memory[65298])?this.audioController.channel1envelopeVolume+=2:++this.audioController.channel1envelopeVolume),this.audioController.channel1envelopeVolume=16-this.audioController.channel1envelopeVolume&15):8==(15&this.memory[65298])&&(this.audioController.channel1envelopeVolume=1+this.audioController.channel1envelopeVolume&15),this.audioController.cacheChannel1OutputLevel()),this.audioController.channel1envelopeType=8==(8&e),this.memory[65298]=e,this.audioController.checkChannel1VolumeEnable())}),this.memoryHighWriter[19]=this.memoryWriter[65299]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel1frequency=1792&this.audioController.channel1frequency|e,this.audioController.channel1FrequencyTracker=2048-this.audioController.channel1frequency<<2)}),this.memoryHighWriter[20]=this.memoryWriter[65300]=((t,e)=>{if(this.soundMasterEnabled){if(this.audioController.runJIT(),this.audioController.channel1consecutive=0==(64&e),this.audioController.channel1frequency=(7&e)<<8|255&this.audioController.channel1frequency,this.audioController.channel1FrequencyTracker=2048-this.audioController.channel1frequency<<2,e>127){this.audioController.channel1timeSweep=this.audioController.channel1lastTimeSweep,this.audioController.channel1Swept=!1;var i=this.memory[65298];this.audioController.channel1envelopeVolume=i>>4,this.audioController.cacheChannel1OutputLevel(),this.audioController.channel1envelopeSweepsLast=(7&i)-1,0===this.audioController.channel1totalLength&&(this.audioController.channel1totalLength=64),this.audioController.channel1lastTimeSweep>0||this.audioController.channel1frequencySweepDivider>0?this.memory[65318]|=1:this.memory[65318]&=254,64==(64&e)&&(this.memory[65318]|=1),this.audioController.channel1ShadowFrequency=this.audioController.channel1frequency,this.audioController.channel1SweepFault=!1,this.audioController.performChannel1AudioSweepDummy()}this.audioController.checkChannel1Enable(),this.memory[65300]=e}}),this.memoryHighWriter[21]=this.memoryWriter[65301]=this.onIllegalWrite,this.memoryHighWriter[22]=this.memoryWriter[65302]=((t,e)=>{!this.soundMasterEnabled&&this.cartridge.useGBCMode||(this.soundMasterEnabled?this.audioController.runJIT():e&=63,this.audioController.channel2CachedDuty=Z[e>>6],this.audioController.channel2totalLength=64-(63&e),this.memory[65302]=e,this.audioController.checkChannel2Enable())}),this.memoryHighWriter[23]=this.memoryWriter[65303]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel2Enabled&&0===this.audioController.channel2envelopeSweeps&&(8==(8&(this.memory[65303]^e))?(0==(8&this.memory[65303])&&(7==(7&this.memory[65303])?this.audioController.channel2envelopeVolume+=2:++this.audioController.channel2envelopeVolume),this.audioController.channel2envelopeVolume=16-this.audioController.channel2envelopeVolume&15):8==(15&this.memory[65303])&&(this.audioController.channel2envelopeVolume=1+this.audioController.channel2envelopeVolume&15),this.audioController.cacheChannel2OutputLevel()),this.audioController.channel2envelopeType=8==(8&e),this.memory[65303]=e,this.audioController.checkChannel2VolumeEnable())}),this.memoryHighWriter[24]=this.memoryWriter[65304]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel2frequency=1792&this.audioController.channel2frequency|e,this.audioController.channel2FrequencyTracker=2048-this.audioController.channel2frequency<<2)}),this.memoryHighWriter[25]=this.memoryWriter[65305]=((t,e)=>{if(this.soundMasterEnabled){if(this.audioController.runJIT(),e>127){var i=this.memory[65303];this.audioController.channel2envelopeVolume=i>>4,this.audioController.cacheChannel2OutputLevel(),this.audioController.channel2envelopeSweepsLast=(7&i)-1,0===this.audioController.channel2totalLength&&(this.audioController.channel2totalLength=64),64==(64&e)&&(this.memory[65318]|=2)}this.audioController.channel2consecutive=0==(64&e),this.audioController.channel2frequency=(7&e)<<8|255&this.audioController.channel2frequency,this.audioController.channel2FrequencyTracker=2048-this.audioController.channel2frequency<<2,this.memory[65305]=e,this.audioController.checkChannel2Enable()}}),this.memoryHighWriter[26]=this.memoryWriter[65306]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),!this.audioController.channel3canPlay&&e>=128&&(this.audioController.channel3lastSampleLookup=0,this.audioController.cacheChannel3Update()),this.audioController.channel3canPlay=e>127,this.audioController.channel3canPlay&&this.memory[65306]>127&&!this.audioController.channel3consecutive&&(this.memory[65318]|=4),this.memory[65306]=e)}),this.memoryHighWriter[27]=this.memoryWriter[65307]=((t,e)=>{!this.soundMasterEnabled&&this.cartridge.useGBCMode||(this.soundMasterEnabled&&this.audioController.runJIT(),this.audioController.channel3totalLength=256-e,this.audioController.checkChannel3Enable())}),this.memoryHighWriter[28]=this.memoryWriter[65308]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),e&=96,this.memory[65308]=e,this.audioController.channel3patternType=0===e?4:(e>>5)-1)}),this.memoryHighWriter[29]=this.memoryWriter[65309]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel3frequency=1792&this.audioController.channel3frequency|e,this.audioController.channel3FrequencyPeriod=2048-this.audioController.channel3frequency<<1)}),this.memoryHighWriter[30]=this.memoryWriter[65310]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),e>127&&(0===this.audioController.channel3totalLength&&(this.audioController.channel3totalLength=256),this.audioController.channel3lastSampleLookup=0,64==(64&e)&&(this.memory[65318]|=4)),this.audioController.channel3consecutive=0==(64&e),this.audioController.channel3frequency=(7&e)<<8|255&this.audioController.channel3frequency,this.audioController.channel3FrequencyPeriod=2048-this.audioController.channel3frequency<<1,this.memory[65310]=e,this.audioController.checkChannel3Enable())}),this.memoryHighWriter[31]=this.memoryWriter[65311]=this.onIllegalWrite,this.memoryHighWriter[32]=this.memoryWriter[65312]=((t,e)=>{!this.soundMasterEnabled&&this.cartridge.useGBCMode||(this.soundMasterEnabled&&this.audioController.runJIT(),this.audioController.channel4totalLength=64-(63&e),this.audioController.checkChannel4Enable())}),this.memoryHighWriter[33]=this.memoryWriter[65313]=((t,e)=>{this.soundMasterEnabled&&(this.audioController.runJIT(),this.audioController.channel4Enabled&&0===this.audioController.channel4envelopeSweeps&&(8==(8&(this.memory[65313]^e))?(0==(8&this.memory[65313])&&(7==(7&this.memory[65313])?this.audioController.channel4envelopeVolume+=2:++this.audioController.channel4envelopeVolume),this.audioController.channel4envelopeVolume=16-this.audioController.channel4envelopeVolume&15):8==(15&this.memory[65313])&&(this.audioController.channel4envelopeVolume=1+this.audioController.channel4envelopeVolume&15),this.audioController.channel4currentVolume=this.audioController.channel4envelopeVolume<{if(this.soundMasterEnabled){this.audioController.runJIT(),this.audioController.channel4FrequencyPeriod=Math.max((7&e)<<4,8)<<(e>>4);var i=8&e;(8===i&&32767===this.audioController.channel4BitRange||0===i&&127===this.audioController.channel4BitRange)&&(this.audioController.channel4lastSampleLookup=0,this.audioController.channel4BitRange=8===i?127:32767,this.audioController.channel4VolumeShifter=8===i?7:15,this.audioController.channel4currentVolume=this.audioController.channel4envelopeVolume<{if(this.soundMasterEnabled){if(this.audioController.runJIT(),this.memory[65315]=e,this.audioController.channel4consecutive=0==(64&e),e>127){var i=this.memory[65313];this.audioController.channel4envelopeVolume=i>>4,this.audioController.channel4currentVolume=this.audioController.channel4envelopeVolume<{this.soundMasterEnabled&&this.memory[65316]!=e&&(this.audioController.runJIT(),this.memory[65316]=e,this.audioController.VinLeftChannelMasterVolume=1+(e>>4&7),this.audioController.VinRightChannelMasterVolume=1+(7&e),this.audioController.cacheMixerOutputLevel())}),this.memoryHighWriter[37]=this.memoryWriter[65317]=((t,e)=>{this.soundMasterEnabled&&this.memory[65317]!=e&&(this.audioController.runJIT(),this.memory[65317]=e,this.audioController.rightChannel1=1==(1&e),this.audioController.rightChannel2=2==(2&e),this.audioController.rightChannel3=4==(4&e),this.audioController.rightChannel4=8==(8&e),this.audioController.leftChannel1=16==(16&e),this.audioController.leftChannel2=32==(32&e),this.audioController.leftChannel3=64==(64&e),this.audioController.leftChannel4=e>127,this.audioController.cacheChannel1OutputLevel(),this.audioController.cacheChannel2OutputLevel(),this.audioController.cacheChannel3OutputLevel(),this.audioController.cacheChannel4OutputLevel())}),this.memoryHighWriter[38]=this.memoryWriter[65318]=((t,e)=>{if(this.audioController.runJIT(),!this.soundMasterEnabled&&e>127)this.memory[65318]=128,this.soundMasterEnabled=!0,this.audioController.initStartState();else if(this.soundMasterEnabled&&e<128){this.memory[65318]=0,this.soundMasterEnabled=!1;for(var i=65296;i<65318;i++)this.memoryWriter[i].apply(this,[i,0])}}),this.memoryHighWriter[39]=this.memoryWriter[65319]=this.onIllegalWrite,this.memoryHighWriter[40]=this.memoryWriter[65320]=this.onIllegalWrite,this.memoryHighWriter[41]=this.memoryWriter[65321]=this.onIllegalWrite,this.memoryHighWriter[42]=this.memoryWriter[65322]=this.onIllegalWrite,this.memoryHighWriter[43]=this.memoryWriter[65323]=this.onIllegalWrite,this.memoryHighWriter[44]=this.memoryWriter[65324]=this.onIllegalWrite,this.memoryHighWriter[45]=this.memoryWriter[65325]=this.onIllegalWrite,this.memoryHighWriter[46]=this.memoryWriter[65326]=this.onIllegalWrite,this.memoryHighWriter[47]=this.memoryWriter[65327]=this.onIllegalWrite,this.memoryHighWriter[48]=this.memoryWriter[65328]=((t,e)=>{this.writeChannel3RAM(0,e)}),this.memoryHighWriter[49]=this.memoryWriter[65329]=((t,e)=>{this.writeChannel3RAM(1,e)}),this.memoryHighWriter[50]=this.memoryWriter[65330]=((t,e)=>{this.writeChannel3RAM(2,e)}),this.memoryHighWriter[51]=this.memoryWriter[65331]=((t,e)=>{this.writeChannel3RAM(3,e)}),this.memoryHighWriter[52]=this.memoryWriter[65332]=((t,e)=>{this.writeChannel3RAM(4,e)}),this.memoryHighWriter[53]=this.memoryWriter[65333]=((t,e)=>{this.writeChannel3RAM(5,e)}),this.memoryHighWriter[54]=this.memoryWriter[65334]=((t,e)=>{this.writeChannel3RAM(6,e)}),this.memoryHighWriter[55]=this.memoryWriter[65335]=((t,e)=>{this.writeChannel3RAM(7,e)}),this.memoryHighWriter[56]=this.memoryWriter[65336]=((t,e)=>{this.writeChannel3RAM(8,e)}),this.memoryHighWriter[57]=this.memoryWriter[65337]=((t,e)=>{this.writeChannel3RAM(9,e)}),this.memoryHighWriter[58]=this.memoryWriter[65338]=((t,e)=>{this.writeChannel3RAM(10,e)}),this.memoryHighWriter[59]=this.memoryWriter[65339]=((t,e)=>{this.writeChannel3RAM(11,e)}),this.memoryHighWriter[60]=this.memoryWriter[65340]=((t,e)=>{this.writeChannel3RAM(12,e)}),this.memoryHighWriter[61]=this.memoryWriter[65341]=((t,e)=>{this.writeChannel3RAM(13,e)}),this.memoryHighWriter[62]=this.memoryWriter[65342]=((t,e)=>{this.writeChannel3RAM(14,e)}),this.memoryHighWriter[63]=this.memoryWriter[65343]=((t,e)=>{this.writeChannel3RAM(15,e)}),this.memoryHighWriter[66]=this.memoryWriter[65346]=((t,e)=>{this.backgroundY!=e&&(this.midScanLineJIT(),this.backgroundY=e)}),this.memoryHighWriter[67]=this.memoryWriter[65347]=((t,e)=>{this.backgroundX!=e&&(this.midScanLineJIT(),this.backgroundX=e)}),this.memoryHighWriter[68]=this.memoryWriter[65348]=((t,e)=>{this.LCDisOn&&(this.modeSTAT=2,this.midScanlineOffset=-1,this.cpu.totalLinesPassed=this.currentX=this.queuedScanLines=this.lastUnrenderedLine=this.LCDTicks=this.STATTracker=this.actualScanLine=this.memory[65348]=0)}),this.memoryHighWriter[69]=this.memoryWriter[65349]=((t,e)=>{this.memory[65349]!=e&&(this.memory[65349]=e,this.LCDisOn&&this.matchLYC())}),this.memoryHighWriter[74]=this.memoryWriter[65354]=((t,e)=>{this.windowY!=e&&(this.midScanLineJIT(),this.windowY=e)}),this.memoryHighWriter[75]=this.memoryWriter[65355]=((t,e)=>{this.memory[65355]!=e&&(this.midScanLineJIT(),this.memory[65355]=e,this.windowX=e-7)}),this.memoryHighWriter[114]=this.memoryWriter[65394]=((t,e)=>{this.memory[65394]=e}),this.memoryHighWriter[115]=this.memoryWriter[65395]=((t,e)=>{this.memory[65395]=e}),this.memoryHighWriter[117]=this.memoryWriter[65397]=((t,e)=>{this.memory[65397]=e}),this.memoryHighWriter[118]=this.memoryWriter[65398]=this.onIllegalWrite,this.memoryHighWriter[119]=this.memoryWriter[65399]=this.onIllegalWrite,this.memoryHighWriter[255]=this.memoryWriter[Bt]=((t,e)=>{this.interruptsEnabled=e,this.checkIRQMatching()}),this.recompileModelSpecificIOWriteHandling(),this.recompileBootIOWriteHandling()}recompileModelSpecificIOWriteHandling(){this.cartridge.useGBCMode?(this.memoryHighWriter[2]=this.memoryWriter[Ct]=((t,e)=>{1==(1&e)?(this.memory[Ct]=127&e,this.serialTimer=0==(2&e)?4096:128,this.serialShiftTimer=this.serialShiftTimerAllocated=0==(2&e)?512:16):(this.memory[Ct]=e,this.serialShiftTimer=this.serialShiftTimerAllocated=this.serialTimer=0)}),this.memoryHighWriter[64]=this.memoryWriter[65344]=((t,e)=>{if(this.memory[65344]!=e){this.midScanLineJIT();var i=e>127;i!=this.LCDisOn&&(this.LCDisOn=i,this.memory[65345]&=120,this.midScanlineOffset=-1,this.cpu.totalLinesPassed=this.currentX=this.queuedScanLines=this.lastUnrenderedLine=this.STATTracker=this.LCDTicks=this.actualScanLine=this.memory[65348]=0,this.LCDisOn?(this.modeSTAT=2,this.matchLYC(),this.LCDCONTROL=this.LINECONTROL):(this.modeSTAT=0,this.LCDCONTROL=this.DISPLAYOFFCONTROL,this.lcdDevice.DisplayShowOff()),this.interruptsRequested&=253),this.gfxWindowCHRBankPosition=64==(64&e)?1024:0,this.gfxWindowDisplay=32==(32&e),this.gfxBackgroundBankOffset=16==(16&e)?0:128,this.gfxBackgroundCHRBankPosition=8==(8&e)?1024:0,this.gfxSpriteNormalHeight=0==(4&e),this.gfxSpriteShow=2==(2&e),this.hasBGPriority=1==(1&e),this.priorityFlaggingPathRebuild(),this.memory[65344]=e}}),this.memoryHighWriter[65]=this.memoryWriter[65345]=((t,e)=>{this.LYCMatchTriggerSTAT=64==(64&e),this.mode2TriggerSTAT=32==(32&e),this.mode1TriggerSTAT=16==(16&e),this.mode0TriggerSTAT=8==(8&e),this.memory[65345]=120&e}),this.memoryHighWriter[70]=this.memoryWriter[65350]=((t,e)=>{if(this.memory[65350]=e,e<224){e<<=8,t=65024;var i=this.modeSTAT;this.modeSTAT=0;var r=0;do{if((r=this.memoryReader[e].apply(this,[e++]))!=this.memory[t]){this.modeSTAT=i,this.graphicsJIT(),this.modeSTAT=0,this.memory[t++]=r;break}}while(++t<65184);if(t<65184)do{this.memory[t++]=this.memoryReader[e].apply(this,[e++]),this.memory[t++]=this.memoryReader[e].apply(this,[e++]),this.memory[t++]=this.memoryReader[e].apply(this,[e++]),this.memory[t++]=this.memoryReader[e].apply(this,[e++])}while(t<65184);this.modeSTAT=i}}),this.memoryHighWriter[77]=this.memoryWriter[65357]=((t,e)=>{this.memory[65357]=127&e|128&this.memory[65357]}),this.memoryHighWriter[79]=this.memoryWriter[65359]=((t,e)=>{this.currVRAMBank=1&e,this.currVRAMBank>0?this.BGCHRCurrentBank=this.BGCHRBank2:this.BGCHRCurrentBank=this.BGCHRBank1}),this.memoryHighWriter[81]=this.memoryWriter[65361]=((t,e)=>{this.hdmaRunning||(this.memory[65361]=e)}),this.memoryHighWriter[82]=this.memoryWriter[65362]=((t,e)=>{this.hdmaRunning||(this.memory[65362]=240&e)}),this.memoryHighWriter[83]=this.memoryWriter[65363]=((t,e)=>{this.hdmaRunning||(this.memory[65363]=31&e)}),this.memoryHighWriter[84]=this.memoryWriter[65364]=((t,e)=>{this.hdmaRunning||(this.memory[65364]=240&e)}),this.memoryHighWriter[85]=this.memoryWriter[65365]=((t,e)=>{this.hdmaRunning?0==(128&e)?(this.hdmaRunning=!1,this.memory[65365]|=128):this.memory[65365]=127&e:0==(128&e)?(this.DMAWrite(1+(127&e)),this.memory[65365]=255):(this.hdmaRunning=!0,this.memory[65365]=127&e)}),this.memoryHighWriter[104]=this.memoryWriter[65384]=((t,e)=>{this.memory[65385]=this.gbcBGRawPalette[63&e],this.memory[65384]=e}),this.memoryHighWriter[105]=this.memoryWriter[65385]=((t,e)=>{if(this.updateGBCBGPalette(63&this.memory[65384],e),this.memory[65384]>127){var i=this.memory[65384]+1&63;this.memory[65384]=128|i,this.memory[65385]=this.gbcBGRawPalette[i]}else this.memory[65385]=e}),this.memoryHighWriter[106]=this.memoryWriter[65386]=((t,e)=>{this.memory[65387]=this.gbcOBJRawPalette[63&e],this.memory[65386]=e}),this.memoryHighWriter[107]=this.memoryWriter[65387]=((t,e)=>{if(this.updateGBCOBJPalette(63&this.memory[65386],e),this.memory[65386]>127){var i=this.memory[65386]+1&63;this.memory[65386]=128|i,this.memory[65387]=this.gbcOBJRawPalette[i]}else this.memory[65387]=e}),this.memoryHighWriter[112]=this.memoryWriter[65392]=((t,e)=>{var i=this.memory[65361]<<8|this.memory[65362];(!this.hdmaRunning||i<53248||i>=57344)&&(this.gbcRamBank=Math.max(7&e,1),this.gbcRamBankPosition=(this.gbcRamBank-1<<12)-53248,this.gbcRamBankPositionECHO=this.gbcRamBankPosition-8192),this.memory[65392]=e}),this.memoryHighWriter[116]=this.memoryWriter[65396]=((t,e)=>{this.memory[65396]=e})):(this.memoryHighWriter[2]=this.memoryWriter[Ct]=((t,e)=>{1==(1&e)?(this.memory[Ct]=127&e,this.serialTimer=4096,this.serialShiftTimer=this.serialShiftTimerAllocated=512):(this.memory[Ct]=e,this.serialShiftTimer=this.serialShiftTimerAllocated=this.serialTimer=0)}),this.memoryHighWriter[64]=this.memoryWriter[65344]=((t,e)=>{if(this.memory[65344]!=e){this.midScanLineJIT();var i=e>127;i!=this.LCDisOn&&(this.LCDisOn=i,this.memory[65345]&=120,this.midScanlineOffset=-1,this.cpu.totalLinesPassed=this.currentX=this.queuedScanLines=this.lastUnrenderedLine=this.STATTracker=this.LCDTicks=this.actualScanLine=this.memory[65348]=0,this.LCDisOn?(this.modeSTAT=2,this.matchLYC(),this.LCDCONTROL=this.LINECONTROL):(this.modeSTAT=0,this.LCDCONTROL=this.DISPLAYOFFCONTROL,this.lcdDevice.DisplayShowOff()),this.interruptsRequested&=253),this.gfxWindowCHRBankPosition=64==(64&e)?1024:0,this.gfxWindowDisplay=32==(32&e),this.gfxBackgroundBankOffset=16==(16&e)?0:128,this.gfxBackgroundCHRBankPosition=8==(8&e)?1024:0,this.gfxSpriteNormalHeight=0==(4&e),this.gfxSpriteShow=2==(2&e),this.bgEnabled=1==(1&e),this.memory[65344]=e}}),this.memoryHighWriter[65]=this.memoryWriter[65345]=((t,e)=>{this.LYCMatchTriggerSTAT=64==(64&e),this.mode2TriggerSTAT=32==(32&e),this.mode1TriggerSTAT=16==(16&e),this.mode0TriggerSTAT=8==(8&e),this.memory[65345]=120&e,(!this.usedBootROM||!this.usedGBCBootROM)&&this.LCDisOn&&this.modeSTAT<2&&(this.interruptsRequested|=2,this.checkIRQMatching())}),this.memoryHighWriter[70]=this.memoryWriter[65350]=((t,e)=>{if(this.memory[65350]=e,e>127&&e<224){e<<=8,t=65024;var i=this.modeSTAT;this.modeSTAT=0;var r=0;do{if((r=this.memoryReader[e].apply(this,[e++]))!=this.memory[t]){this.modeSTAT=i,this.graphicsJIT(),this.modeSTAT=0,this.memory[t++]=r;break}}while(++t<65184);if(t<65184)do{this.memory[t++]=this.memoryReader[e].apply(this,[e++]),this.memory[t++]=this.memoryReader[e].apply(this,[e++]),this.memory[t++]=this.memoryReader[e].apply(this,[e++]),this.memory[t++]=this.memoryReader[e].apply(this,[e++])}while(t<65184);this.modeSTAT=i}}),this.memoryHighWriter[71]=this.memoryWriter[65351]=((t,e)=>{this.memory[65351]!=e&&(this.midScanLineJIT(),this.updateGBBGPalette(e),this.memory[65351]=e)}),this.memoryHighWriter[72]=this.memoryWriter[65352]=((t,e)=>{this.memory[65352]!=e&&(this.midScanLineJIT(),this.updateGBOBJPalette(0,e),this.memory[65352]=e)}),this.memoryHighWriter[73]=this.memoryWriter[65353]=((t,e)=>{this.memory[65353]!=e&&(this.midScanLineJIT(),this.updateGBOBJPalette(4,e),this.memory[65353]=e)}),this.memoryHighWriter[77]=this.memoryWriter[65357]=((t,e)=>{this.memory[65357]=e}),this.memoryHighWriter[79]=this.memoryWriter[65359]=this.onIllegalWrite,this.memoryHighWriter[85]=this.memoryWriter[65365]=this.onIllegalWrite,this.memoryHighWriter[104]=this.memoryWriter[65384]=this.onIllegalWrite,this.memoryHighWriter[105]=this.memoryWriter[65385]=this.onIllegalWrite,this.memoryHighWriter[106]=this.memoryWriter[65386]=this.onIllegalWrite,this.memoryHighWriter[107]=this.memoryWriter[65387]=this.onIllegalWrite,this.memoryHighWriter[108]=this.memoryWriter[65388]=this.onIllegalWrite,this.memoryHighWriter[112]=this.memoryWriter[65392]=this.onIllegalWrite,this.memoryHighWriter[116]=this.memoryWriter[65396]=this.onIllegalWrite)}recompileBootIOWriteHandling(){this.inBootstrap?(this.memoryHighWriter[80]=this.memoryWriter[65360]=((t,e)=>{console.log("Boot ROM reads blocked: Bootstrap process has ended.",0),this.inBootstrap=!1,this.disableBootROM(),this.memory[65360]=e}),this.cartridge.useGBCMode&&(this.memoryHighWriter[108]=this.memoryWriter[65388]=((t,e)=>{this.inBootstrap&&this.cartridge.setGBCMode(e),this.memory[65388]=e}))):this.memoryHighWriter[80]=this.memoryWriter[65360]=this.onIllegalWrite}}var bt=function(t,e,i,r){return new(i||(i=Promise))(function(s,h){function a(t){try{o(r.next(t))}catch(t){h(t)}}function n(t){try{o(r.throw(t))}catch(t){h(t)}}function o(t){t.done?s(t.value):new i(function(e){e(t.value)}).then(a,n)}o((r=r.apply(t,e||[])).next())})};class kt extends H.EventEmitter{constructor({audio:t,isPaused:e,lcd:i,isSoundEnabled:r,bootRom:h}={}){super(),this.buttons=["right","left","up","down","a","b","select","start"],"boolean"==typeof r&&(s.soundOn=r),e&&(this.isPaused=e),this.core=new Ht({audio:t,api:this,lcd:i,bootRom:h}),this.debouncedAutoSave=B(this.autoSave.bind(this),100),this.core.events.on("sramWrite",()=>{this.core.cartridge&&this.debouncedAutoSave()}),this.isOn=!1,this.actions=new G,this.registerActions(),"undefined"!=typeof document&&(this.storage=new F)}setBootRom(t){this.core.setBootRom(t)}isPaused(){return"undefined"!=typeof document&&document.hidden}setStorage(t){this.storage=t}registerActions(){this.buttons.forEach((t,e)=>{this.actions.register(t).on("down-"+t,()=>{this.core.joypad.down(e)}).on("up-"+t,()=>{this.core.joypad.up(e)})}),this.actions.register("speed").on("down-speed",t=>this.handleSpeed(t)).on("change-speed",t=>this.handleSpeed(t)).on("up-speed",()=>{this.setSpeed(1)})}handleSpeed(t){let e=2;t&&"number"==typeof t.value&&(e=2*t.value+1),this.setSpeed(e)}turnOn(){if(this.isOn)return;this.isOn=!0,this.core.start(this.cartridge),this.core.stopEmulator&=1;const t=e=>{this.isPaused()||((!this.lastRun||this.lastRunt(e),0)}}}i.d(e,"GameBoy",function(){return kt}),i.d(e,"LocalStorage",function(){return F}),i.d(e,"util",function(){return r});e.default=kt}])}); \ No newline at end of file diff --git a/dist/typings/core/GameBoyCore.d.ts b/dist/typings/core/GameBoyCore.d.ts index 9c49b27..bcd4453 100644 --- a/dist/typings/core/GameBoyCore.d.ts +++ b/dist/typings/core/GameBoyCore.d.ts @@ -210,6 +210,7 @@ export default class GameBoyCore { loadState(state: any): void; jumpCompile(): void; connectCartridge(cartridge: Cartridge): void; + onRUMBLE(): void; loadCartridgeRomIntoMemory(): void; loadBootROMIntoMemory(): void; start(cartridge: Cartridge): void; diff --git a/dist/typings/core/cartridge/RUMBLE.d.ts b/dist/typings/core/cartridge/RUMBLE.d.ts index 8daad72..0e383b6 100644 --- a/dist/typings/core/cartridge/RUMBLE.d.ts +++ b/dist/typings/core/cartridge/RUMBLE.d.ts @@ -1,4 +1,4 @@ import MBC5 from "./mbc5"; export default class RUMBLE extends MBC5 { - writeRAMBank(address: any, data: any): void; + writeRAMBank(address: number, data: number): void; } diff --git a/dist/typings/core/cartridge/mbc5.d.ts b/dist/typings/core/cartridge/mbc5.d.ts index 354f19b..baa517b 100644 --- a/dist/typings/core/cartridge/mbc5.d.ts +++ b/dist/typings/core/cartridge/mbc5.d.ts @@ -3,5 +3,5 @@ export default class MBC5 extends MBC { setCurrentROMBank(): void; writeROMBankLow(address: any, data: any): void; writeROMBankHigh(address: any, data: any): void; - writeRAMBank(address: any, data: any): void; + writeRAMBank(address: number, value: number): void; } diff --git a/dist/typings/core/memory/index.d.ts b/dist/typings/core/memory/index.d.ts index 0cd4087..54ae22a 100644 --- a/dist/typings/core/memory/index.d.ts +++ b/dist/typings/core/memory/index.d.ts @@ -10,6 +10,7 @@ export default class Memory { constructor(gameboy: GameBoyCore, data: any); write(address: number, value: number): void; read(address: number): number; + hasReader(address: number): boolean; jumpCompile(): void; setReaders(from: number, to: number, reader: ReaderFunction): void; setReader(address: number, reader: ReaderFunction): void; diff --git a/package.json b/package.json index b68fe54..762e906 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jsgbc", - "version": "0.5.5", + "version": "0.5.6", "description": "jsGBC Core Emulator", "main": "./dist/jsgbc-core.js", "types": "./dist/typings/index.d.ts", diff --git a/src/core/GameBoyCore.ts b/src/core/GameBoyCore.ts index ab3d31e..4664630 100644 --- a/src/core/GameBoyCore.ts +++ b/src/core/GameBoyCore.ts @@ -263,6 +263,7 @@ export default class GameBoyCore { this.MBC5WriteRAMBank = this.MBC5WriteRAMBank.bind(this); this.MBCWriteEnable = this.MBCWriteEnable.bind(this); this.RUMBLEWriteRAMBank = this.RUMBLEWriteRAMBank.bind(this); + this.onRUMBLE = this.onRUMBLE.bind(this); this.memoryReadMBC = this.memoryReadMBC.bind(this); this.memoryReadMBC3 = this.memoryReadMBC3.bind(this); this.memoryReadMBC7 = this.memoryReadMBC7.bind(this); @@ -331,20 +332,23 @@ export default class GameBoyCore { } connectCartridge(cartridge: Cartridge) { + if (this.cartridge && this.cartridge.mbc) this.cartridge.mbc.removeListener("rumble", this.onRUMBLE); + cartridge.connect(this); this.cartridge = cartridge; + if (this.cartridge && this.cartridge.mbc) this.cartridge.mbc.on("rumble", this.onRUMBLE); + this.loadCartridgeRomIntoMemory(); if (this.bootROM) this.loadBootROMIntoMemory(); this.cartridge.interpret(); + } - // TODO: cleanup - this.cartridge.mbc.on("rumble", () => { - if (typeof window !== "undefined" && "vibrate" in window.navigator) { - window.navigator.vibrate(200); - } - }); + onRUMBLE() { + if (typeof window !== "undefined" && "vibrate" in window.navigator) { + window.navigator.vibrate(200); + } } loadCartridgeRomIntoMemory() { @@ -412,7 +416,7 @@ export default class GameBoyCore { initMemory() { // Initialize the RAM: - this.memory = util.getTypedArray(0x10000, 0, "uint8"); // TODO: remove + this.memory = util.getTypedArray(0x10000, 0, "uint8"); // TODO: replace with Memory Class this.audioController.setMemory(this.memory); this.frameBuffer = util.getTypedArray(23040, 0xf8f8f8, "int32"); this.BGCHRBank1 = util.getTypedArray(0x800, 0, "uint8"); @@ -2379,6 +2383,12 @@ export default class GameBoyCore { //Memory Reading: memoryRead(address) { // Act as a wrapper for reading the returns from the compiled jumps to memory. + + if (0xFFFF < address) { + console.log(address, this.memoryReader[address]); + } + + if (this.memoryNew.hasReader(address)) return this.memoryNew.read(address); return this.memoryReader[address].apply(this, [address]); } @@ -2427,17 +2437,14 @@ export default class GameBoyCore { } else if (index >= 0xff00) { switch (index) { case MemoryLayout.JOYPAD_REG: - //JOYPAD: this.memoryHighReader[0] = this.memoryReader[MemoryLayout.JOYPAD_REG] = address => 0xc0 | this.memory[MemoryLayout.JOYPAD_REG]; // top nibble returns as set. break; case MemoryLayout.SB_REG: - // SB this.memoryHighReader[0x01] = this.memoryReader[MemoryLayout.SB_REG] = address => { return this.memory[MemoryLayout.SC_REG] < 0x80 ? this.memory[MemoryLayout.SB_REG] : 0xff; }; break; case MemoryLayout.SC_REG: - // SC if (this.cartridge.useGBCMode) { this.memoryHighReader[0x02] = this.memoryReader[MemoryLayout.SC_REG] = address => { return (this.serialTimer <= 0 ? 0x7c : 0xfc) | this.memory[MemoryLayout.SC_REG]; @@ -3166,56 +3173,54 @@ export default class GameBoyCore { //Clock the CPU for the DMA transfer (CPU is halted during the transfer): this.CPUTicks += 4 | tilesToTransfer << 5 << this.doubleSpeedShifter; } - //Source address of the transfer: + // Source address of the transfer: var source = this.memory[0xff51] << 8 | this.memory[0xff52]; - //Destination address in the VRAM memory range: + // Destination address in the VRAM memory range: var destination = this.memory[0xff53] << 8 | this.memory[0xff54]; - //Creating some references: - var memoryReader = this.memoryReader; - //JIT the graphics render queue: + // JIT the graphics render queue: this.graphicsJIT(); var memory = this.memory; - //Determining which bank we're working on so we can optimize: + // Determining which bank we're working on so we can optimize: if (this.currVRAMBank === 0) { - //DMA transfer for VRAM bank 0: + // DMA transfer for VRAM bank 0: do { if (destination < 0x1800) { - memory[0x8000 | destination] = memoryReader[source].apply(this, [source++]); - memory[0x8001 | destination] = memoryReader[source].apply(this, [source++]); - memory[0x8002 | destination] = memoryReader[source].apply(this, [source++]); - memory[0x8003 | destination] = memoryReader[source].apply(this, [source++]); - memory[0x8004 | destination] = memoryReader[source].apply(this, [source++]); - memory[0x8005 | destination] = memoryReader[source].apply(this, [source++]); - memory[0x8006 | destination] = memoryReader[source].apply(this, [source++]); - memory[0x8007 | destination] = memoryReader[source].apply(this, [source++]); - memory[0x8008 | destination] = memoryReader[source].apply(this, [source++]); - memory[0x8009 | destination] = memoryReader[source].apply(this, [source++]); - memory[0x800a | destination] = memoryReader[source].apply(this, [source++]); - memory[0x800b | destination] = memoryReader[source].apply(this, [source++]); - memory[0x800c | destination] = memoryReader[source].apply(this, [source++]); - memory[0x800d | destination] = memoryReader[source].apply(this, [source++]); - memory[0x800e | destination] = memoryReader[source].apply(this, [source++]); - memory[0x800f | destination] = memoryReader[source].apply(this, [source++]); + memory[0x8000 | destination] = this.memoryRead(source++); + memory[0x8001 | destination] = this.memoryRead(source++); + memory[0x8002 | destination] = this.memoryRead(source++); + memory[0x8003 | destination] = this.memoryRead(source++); + memory[0x8004 | destination] = this.memoryRead(source++); + memory[0x8005 | destination] = this.memoryRead(source++); + memory[0x8006 | destination] = this.memoryRead(source++); + memory[0x8007 | destination] = this.memoryRead(source++); + memory[0x8008 | destination] = this.memoryRead(source++); + memory[0x8009 | destination] = this.memoryRead(source++); + memory[0x800a | destination] = this.memoryRead(source++); + memory[0x800b | destination] = this.memoryRead(source++); + memory[0x800c | destination] = this.memoryRead(source++); + memory[0x800d | destination] = this.memoryRead(source++); + memory[0x800e | destination] = this.memoryRead(source++); + memory[0x800f | destination] = this.memoryRead(source++); this.generateGBCTileBank1(destination); destination += 0x10; } else { destination &= 0x7f0; - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank1[destination++] = memoryReader[source].apply(this, [source++]); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); + this.BGCHRBank1[destination++] = this.memoryRead(source++); destination = destination + 0x1800 & 0x1ff0; } source &= 0xfff0; @@ -3226,42 +3231,42 @@ export default class GameBoyCore { //DMA transfer for VRAM bank 1: do { if (destination < 0x1800) { - VRAM[destination] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0x1] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0x2] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0x3] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0x4] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0x5] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0x6] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0x7] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0x8] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0x9] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0xa] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0xb] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0xc] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0xd] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0xe] = memoryReader[source].apply(this, [source++]); - VRAM[destination | 0xf] = memoryReader[source].apply(this, [source++]); + VRAM[destination] = this.memoryRead(source++); + VRAM[destination | 0x1] = this.memoryRead(source++); + VRAM[destination | 0x2] = this.memoryRead(source++); + VRAM[destination | 0x3] = this.memoryRead(source++); + VRAM[destination | 0x4] = this.memoryRead(source++); + VRAM[destination | 0x5] = this.memoryRead(source++); + VRAM[destination | 0x6] = this.memoryRead(source++); + VRAM[destination | 0x7] = this.memoryRead(source++); + VRAM[destination | 0x8] = this.memoryRead(source++); + VRAM[destination | 0x9] = this.memoryRead(source++); + VRAM[destination | 0xa] = this.memoryRead(source++); + VRAM[destination | 0xb] = this.memoryRead(source++); + VRAM[destination | 0xc] = this.memoryRead(source++); + VRAM[destination | 0xd] = this.memoryRead(source++); + VRAM[destination | 0xe] = this.memoryRead(source++); + VRAM[destination | 0xf] = this.memoryRead(source++); this.generateGBCTileBank2(destination); destination += 0x10; } else { destination &= 0x7f0; - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); - this.BGCHRBank2[destination++] = memoryReader[source].apply(this, [source++]); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); + this.BGCHRBank2[destination++] = this.memoryRead(source++); destination = destination + 0x1800 & 0x1ff0; } source &= 0xfff0; diff --git a/src/core/cartridge/RUMBLE.ts b/src/core/cartridge/RUMBLE.ts index 014dc67..57d7df7 100644 --- a/src/core/cartridge/RUMBLE.ts +++ b/src/core/cartridge/RUMBLE.ts @@ -1,12 +1,11 @@ import MBC5 from "./mbc5"; export default class RUMBLE extends MBC5 { - writeRAMBank(address, data) { + writeRAMBank(address: number, data: number) { // MBC5 RAM bank switching - // Like MBC5, but bit 3 of the lower nibble is used for rumbling and bit 2 is ignored. - // console.log((data & 0x03).toString(2)); - this.currentMBCRAMBank = data & 0x03; - this.currentRAMBankPosition = (this.currentMBCRAMBank << 13) - 0xa000; if (data & 0x08) this.emit("rumble"); + data &= 0x7; + + super.writeRAMBank(address, data); } } diff --git a/src/core/cartridge/mbc5.ts b/src/core/cartridge/mbc5.ts index d6d86a2..9394f63 100644 --- a/src/core/cartridge/mbc5.ts +++ b/src/core/cartridge/mbc5.ts @@ -18,11 +18,9 @@ export default class MBC5 extends MBC { this.setCurrentROMBank(); } - writeRAMBank(address, data) { + writeRAMBank(address: number, value: number) { // MBC5 RAM bank switching - this.currentMBCRAMBank = data & 0xf; - this.currentRAMBankPosition = ( - this.currentMBCRAMBank << 13 - ) - 0xa000; + this.currentMBCRAMBank = value & 0xf; + this.currentRAMBankPosition = (this.currentMBCRAMBank << 13) - 0xa000; } } diff --git a/src/core/memory/Layout.ts b/src/core/memory/Layout.ts index b11621d..03b86a7 100644 --- a/src/core/memory/Layout.ts +++ b/src/core/memory/Layout.ts @@ -43,7 +43,8 @@ export const INTERNAL_RAM_BANK0_END = 0xCFFF; export const INTERNAL_RAM_SWITCH_BANK_START = 0xD000; export const INTERNAL_RAM_SWITCH_BANK_END = 0xDFFF; -export const ECHO_RAM_START = 0xE000; /* Mirrored Memory */ +// Mirrored Memory +export const ECHO_RAM_START = 0xE000; export const ECHO_RAM_END = 0xFDFF; /*--------------- HIGH MEM-------------- */ @@ -61,9 +62,9 @@ export const ZERO_PAGE_END = 0xFFFE; /* -------------------------------------*/ -//Convert between local IO memory address and global address -// export const GLOBAL_TO_IO_ADDR(A) A - 0xFF00 -// export const IO_TO_GLOBAL_ADDR(A) A + 0xFF00 +// Convert between local IO memory address and global address +// export const GLOBAL_TO_IO_ADDR(A) A - 0xFF00 +// export const IO_TO_GLOBAL_ADDR(A) A + 0xFF00 /* -------------IO ports/registers ------------------*/ export const JOYPAD_REG = 0xFF00; /* Register for reading joy pad info */ diff --git a/src/core/memory/index.ts b/src/core/memory/index.ts index 80f6201..d82b113 100644 --- a/src/core/memory/index.ts +++ b/src/core/memory/index.ts @@ -13,7 +13,7 @@ export default class Memory { constructor(gameboy: GameBoyCore, data) { this.gameboy = gameboy; - this.data; + this.data = data; } write(address: number, value: number) { @@ -21,13 +21,20 @@ export default class Memory { } read(address: number) { - return this.readers[address](address); + // console.log(address); + // debugger; + + const reader = this.readers[address]; + if (!reader) throw new Error("no_reader"); + + return reader(address); } - jumpCompile() { - this.gameboy.memoryReadJumpCompile(); // TODO: remove - this.gameboy.memoryWriteJumpCompile(); // TODO: remove + hasReader(address: number): boolean { + return !!this.readers[address]; + } + jumpCompile() { this.setReaders(MemoryLayout.INTERRUPT_VECTORS_START, MemoryLayout.CART_ROM_BANK0_END, (address: number) => this.data[address]); this.setReaders(MemoryLayout.CART_ROM_SWITCH_BANK_START, MemoryLayout.CART_ROM_SWITCH_BANK_END, (address: number) => this.gameboy.cartridge.rom.getByte(this.gameboy.cartridge.mbc.currentROMBank + address)); this.setReaders(MemoryLayout.TILE_SET_0_START, MemoryLayout.TILE_SET_1_END, this.gameboy.cartridge.useGBCMode ? this.gameboy.VRAMDATAReadCGBCPU : this.gameboy.VRAMDATAReadDMGCPU); @@ -49,10 +56,26 @@ export default class Memory { } else { this.setReaders(MemoryLayout.INTERNAL_RAM_SWITCH_BANK_START, MemoryLayout.INTERNAL_RAM_SWITCH_BANK_END, this.gameboy.memoryReadNormal); } + + this.setReaders(MemoryLayout.ECHO_RAM_START, 0xEFFF, this.gameboy.memoryReadECHONormal); + if (this.gameboy.cartridge.useGBCMode) { + this.setReaders(0xF000, MemoryLayout.ECHO_RAM_END, this.gameboy.memoryReadECHOGBCMemory); + } else { + this.setReaders(0xF000, MemoryLayout.ECHO_RAM_END, this.gameboy.memoryReadECHONormal); + } + + this.setReaders(MemoryLayout.SPRITE_ATTRIBUTE_TABLE_START, MemoryLayout.SPRITE_ATTRIBUTE_TABLE_END, this.gameboy.memoryReadOAM); + + if (this.gameboy.cartridge.useGBCMode) { + this.setReaders(MemoryLayout.UNUSABLE_MEM_START, MemoryLayout.UNUSABLE_MEM_END, this.gameboy.memoryReadNormal); + } + + this.gameboy.memoryReadJumpCompile(); // TODO: remove + this.gameboy.memoryWriteJumpCompile(); // TODO: remove } setReaders(from: number, to: number, reader: ReaderFunction) { - for (let index = from; index < to; index++) { + for (let index = from; index <= to; index++) { this.setReader(index, reader); } } @@ -62,7 +85,7 @@ export default class Memory { } setWriters(from: number, to: number, writer: WriterFunction) { - for (let index = from; index < to; index++) { + for (let index = from; index <= to; index++) { this.setWriter(index, writer); } }