mirror of
https://github.com/sehugg/8bitworkshop.git
synced 2026-04-24 11:16:38 +00:00
13 lines
42 KiB
JavaScript
13 lines
42 KiB
JavaScript
import{C as G,E as U,G as j,H as z,r as O,u as D}from"./chunk-236FAHT6.js";import{$ as M,J as B,V as o,W as V,Y as W,g as n}from"./chunk-QWAF5HSH.js";import"./chunk-KT7KMEQC.js";var x=class x{constructor(){this.A=0;this.F=0;this.B=0;this.C=0;this.D=0;this.E=0;this.H=0;this.L=0;this.SP=65534;this.PC=256;this.ime=!1;this.imeDelay=!1;this.halted=!1;this.interruptFlags=0;this.cyclesPerInsn=0;this.o=0;this.stable=!0}connectMemoryBus(t){this.bus=t}reset(){this.A=1,this.F=176,this.B=0,this.C=19,this.D=0,this.E=216,this.H=1,this.L=77,this.SP=65534,this.PC=256,this.ime=!1,this.imeDelay=!1,this.halted=!1,this.interruptFlags=0}interrupt(t){this.interruptFlags|=t}getPC(){return this.PC}getSP(){return this.SP}isStable(){return this.stable}saveState(){return{PC:this.PC,SP:this.SP,AF:this.A<<8|this.F,BC:this.B<<8|this.C,DE:this.D<<8|this.E,HL:this.H<<8|this.L,o:this.o,ime:this.ime?1:0,halted:this.halted,interrupt:this.interruptFlags}}loadState(t){this.PC=t.PC,this.SP=t.SP,this.A=t.AF>>8&255,this.F=t.AF&240,this.B=t.BC>>8&255,this.C=t.BC&255,this.D=t.DE>>8&255,this.E=t.DE&255,this.H=t.HL>>8&255,this.L=t.HL&255,this.o=t.o,this.ime=!!t.ime,this.halted=t.halted,this.interruptFlags=t.interrupt}getAF(){return this.A<<8|this.F&240}getBC(){return this.B<<8|this.C}getDE(){return this.D<<8|this.E}getHL(){return this.H<<8|this.L}setAF(t){this.A=t>>8&255,this.F=t&240}setBC(t){this.B=t>>8&255,this.C=t&255}setDE(t){this.D=t>>8&255,this.E=t&255}setHL(t){this.H=t>>8&255,this.L=t&255}read(t){return this.bus.read(t&65535)}write(t,e){this.bus.write(t&65535,e&255)}readWord(t){return this.read(t)|this.read(t+1)<<8}writeWord(t,e){this.write(t,e&255),this.write(t+1,e>>8&255)}fetchByte(){var t=this.read(this.PC);return this.PC=this.PC+1&65535,t}fetchWord(){var t=this.fetchByte(),e=this.fetchByte();return e<<8|t}fetchSignedByte(){var t=this.fetchByte();return t<128?t:t-256}pushWord(t){this.SP=this.SP-1&65535,this.write(this.SP,t>>8&255),this.SP=this.SP-1&65535,this.write(this.SP,t&255)}popWord(){var t=this.read(this.SP);this.SP=this.SP+1&65535;var e=this.read(this.SP);return this.SP=this.SP+1&65535,e<<8|t}getZ(){return!!(this.F&x.FLAG_Z)}getN(){return!!(this.F&x.FLAG_N)}getH(){return!!(this.F&x.FLAG_H)}getC(){return!!(this.F&x.FLAG_C)}setZ(t){t?this.F|=x.FLAG_Z:this.F&=~x.FLAG_Z}setN(t){t?this.F|=x.FLAG_N:this.F&=~x.FLAG_N}setH(t){t?this.F|=x.FLAG_H:this.F&=~x.FLAG_H}setC(t){t?this.F|=x.FLAG_C:this.F&=~x.FLAG_C}setFlags(t,e,s,h){this.F=(t?x.FLAG_Z:0)|(e?x.FLAG_N:0)|(s?x.FLAG_H:0)|(h?x.FLAG_C:0)}getReg(t){switch(t){case 0:return this.B;case 1:return this.C;case 2:return this.D;case 3:return this.E;case 4:return this.H;case 5:return this.L;case 6:return this.read(this.getHL());case 7:return this.A}return 0}setReg(t,e){switch(e&=255,t){case 0:this.B=e;break;case 1:this.C=e;break;case 2:this.D=e;break;case 3:this.E=e;break;case 4:this.H=e;break;case 5:this.L=e;break;case 6:this.write(this.getHL(),e);break;case 7:this.A=e;break}}getReg16(t){switch(t){case 0:return this.getBC();case 1:return this.getDE();case 2:return this.getHL();case 3:return this.SP}return 0}setReg16(t,e){switch(e&=65535,t){case 0:this.setBC(e);break;case 1:this.setDE(e);break;case 2:this.setHL(e);break;case 3:this.SP=e;break}}getReg16AF(t){switch(t){case 0:return this.getBC();case 1:return this.getDE();case 2:return this.getHL();case 3:return this.getAF()}return 0}setReg16AF(t,e){switch(e&=65535,t){case 0:this.setBC(e);break;case 1:this.setDE(e);break;case 2:this.setHL(e);break;case 3:this.setAF(e);break}}checkCondition(t){switch(t){case 0:return!this.getZ();case 1:return this.getZ();case 2:return!this.getC();case 3:return this.getC()}return!1}addA(t){var e=this.A+t;this.setFlags((e&255)===0,!1,(this.A&15)+(t&15)>15,e>255),this.A=e&255}adcA(t){var e=this.getC()?1:0,s=this.A+t+e;this.setFlags((s&255)===0,!1,(this.A&15)+(t&15)+e>15,s>255),this.A=s&255}subA(t){var e=this.A-t;this.setFlags((e&255)===0,!0,(this.A&15)<(t&15),e<0),this.A=e&255}sbcA(t){var e=this.getC()?1:0,s=this.A-t-e;this.setFlags((s&255)===0,!0,(this.A&15)<(t&15)+e,s<0),this.A=s&255}andA(t){this.A&=t,this.setFlags(this.A===0,!1,!0,!1)}xorA(t){this.A^=t,this.A&=255,this.setFlags(this.A===0,!1,!1,!1)}orA(t){this.A|=t,this.A&=255,this.setFlags(this.A===0,!1,!1,!1)}cpA(t){var e=this.A-t;this.setFlags((e&255)===0,!0,(this.A&15)<(t&15),e<0)}aluOp(t,e){switch(t){case 0:this.addA(e);break;case 1:this.adcA(e);break;case 2:this.subA(e);break;case 3:this.sbcA(e);break;case 4:this.andA(e);break;case 5:this.xorA(e);break;case 6:this.orA(e);break;case 7:this.cpA(e);break}}inc8(t){var e=t+1&255;return this.setZ(e===0),this.setN(!1),this.setH((t&15)===15),e}dec8(t){var e=t-1&255;return this.setZ(e===0),this.setN(!0),this.setH((t&15)===0),e}addHL(t){var e=this.getHL(),s=e+t;this.setN(!1),this.setH((e&4095)+(t&4095)>4095),this.setC(s>65535),this.setHL(s&65535)}daa(){var t=this.A;this.getN()?(this.getC()&&(t-=96),this.getH()&&(t-=6)):((this.getC()||t>153)&&(t+=96,this.setC(!0)),(this.getH()||(t&15)>9)&&(t+=6)),t&=255,this.setZ(t===0),this.setH(!1),this.A=t}cbOp(t){var e=t>>3&31,s=t&7,h=this.getReg(s),r=s===6?4:2,i;if(t<64){switch(e){case 0:i=(h<<1|h>>7)&255,this.setFlags(i===0,!1,!1,!!(h&128));break;case 1:i=(h>>1|h<<7)&255,this.setFlags(i===0,!1,!1,!!(h&1));break;case 2:i=(h<<1|(this.getC()?1:0))&255,this.setFlags(i===0,!1,!1,!!(h&128));break;case 3:i=(h>>1|(this.getC()?128:0))&255,this.setFlags(i===0,!1,!1,!!(h&1));break;case 4:i=h<<1&255,this.setFlags(i===0,!1,!1,!!(h&128));break;case 5:i=(h>>1|h&128)&255,this.setFlags(i===0,!1,!1,!!(h&1));break;case 6:i=(h&15)<<4|(h&240)>>4,this.setFlags(i===0,!1,!1,!1);break;case 7:i=h>>1&255,this.setFlags(i===0,!1,!1,!!(h&1));break;default:i=h}this.setReg(s,i),s===6&&(r=4)}else if(t<128){var a=t>>3&7;this.setZ(!(h&1<<a)),this.setN(!1),this.setH(!0),s===6&&(r=3)}else if(t<192){var a=t>>3&7;this.setReg(s,h&~(1<<a)),s===6&&(r=4)}else{var a=t>>3&7;this.setReg(s,h|1<<a),s===6&&(r=4)}return r}handleInterrupts(){if(this.interruptFlags===0||(this.halted&&(this.halted=!1),!this.ime))return 0;for(var t=0;t<5;t++)if(this.interruptFlags&1<<t)return this.ime=!1,this.interruptFlags&=~(1<<t),this.pushWord(this.PC),this.PC=64+t*8,5;return 0}advanceInsn(){this.imeDelay&&(this.ime=!0,this.imeDelay=!1);var t=this.handleInterrupts();if(t>0)return t;if(this.halted)return 1;this.stable=!0;var e=this.fetchByte();this.o=e,this.stable=!1;var s=this.executeOpcode(e);return this.stable=!0,s}executeOpcode(t){var e=t>>6&3,s=t>>3&7,h=t&7,r=s>>1&3,i=s&1;switch(e){case 0:return this.execX0(s,h,r,i,t);case 1:return h===6&&s===6?(this.halted=!0,1):(this.setReg(s,this.getReg(h)),s===6||h===6?2:1);case 2:return this.aluOp(s,this.getReg(h)),h===6?2:1;case 3:return this.execX3(s,h,r,i,t)}return 1}execX0(t,e,s,h,r){switch(e){case 0:switch(t){case 0:return 1;case 1:var i=this.fetchWord();return this.writeWord(i,this.SP),5;case 2:return this.PC=this.PC+1&65535,1;case 3:var a=this.fetchSignedByte();return this.PC=this.PC+a&65535,3;case 4:case 5:case 6:case 7:var a=this.fetchSignedByte();return this.checkCondition(t-4)?(this.PC=this.PC+a&65535,3):2}break;case 1:return h===0?(this.setReg16(s,this.fetchWord()),3):(this.addHL(this.getReg16(s)),2);case 2:if(h===0)switch(s){case 0:return this.write(this.getBC(),this.A),2;case 1:return this.write(this.getDE(),this.A),2;case 2:return this.write(this.getHL(),this.A),this.setHL(this.getHL()+1&65535),2;case 3:return this.write(this.getHL(),this.A),this.setHL(this.getHL()-1&65535),2}else switch(s){case 0:return this.A=this.read(this.getBC()),2;case 1:return this.A=this.read(this.getDE()),2;case 2:return this.A=this.read(this.getHL()),this.setHL(this.getHL()+1&65535),2;case 3:return this.A=this.read(this.getHL()),this.setHL(this.getHL()-1&65535),2}break;case 3:return h===0?(this.setReg16(s,this.getReg16(s)+1&65535),2):(this.setReg16(s,this.getReg16(s)-1&65535),2);case 4:return this.setReg(t,this.inc8(this.getReg(t))),t===6?3:1;case 5:return this.setReg(t,this.dec8(this.getReg(t))),t===6?3:1;case 6:return this.setReg(t,this.fetchByte()),t===6?3:2;case 7:switch(t){case 0:var d=this.A>>7&1;return this.A=(this.A<<1|d)&255,this.setFlags(!1,!1,!1,!!d),1;case 1:var c=this.A&1;return this.A=(this.A>>1|c<<7)&255,this.setFlags(!1,!1,!1,!!c),1;case 2:var d=this.A>>7&1;return this.A=(this.A<<1|(this.getC()?1:0))&255,this.setFlags(!1,!1,!1,!!d),1;case 3:var c=this.A&1;return this.A=(this.A>>1|(this.getC()?128:0))&255,this.setFlags(!1,!1,!1,!!c),1;case 4:return this.daa(),1;case 5:return this.A=~this.A&255,this.setN(!0),this.setH(!0),1;case 6:return this.setN(!1),this.setH(!1),this.setC(!0),1;case 7:return this.setN(!1),this.setH(!1),this.setC(!this.getC()),1}break}return 1}execX3(t,e,s,h,r){switch(e){case 0:switch(t){case 0:case 1:case 2:case 3:return this.checkCondition(t)?(this.PC=this.popWord(),5):2;case 4:var i=this.fetchByte();return this.write(65280+i,this.A),3;case 5:var a=this.fetchSignedByte(),d=this.SP+a&65535;return this.setFlags(!1,!1,(this.SP&15)+(a&15)>15,(this.SP&255)+(a&255)>255),this.SP=d,4;case 6:var i=this.fetchByte();return this.A=this.read(65280+i),3;case 7:var a=this.fetchSignedByte(),d=this.SP+a&65535;return this.setFlags(!1,!1,(this.SP&15)+(a&15)>15,(this.SP&255)+(a&255)>255),this.setHL(d),3}break;case 1:if(h===0)return this.setReg16AF(s,this.popWord()),3;switch(s){case 0:return this.PC=this.popWord(),4;case 1:return this.PC=this.popWord(),this.ime=!0,4;case 2:return this.PC=this.getHL(),1;case 3:return this.SP=this.getHL(),2}break;case 2:switch(t){case 0:case 1:case 2:case 3:var c=this.fetchWord();return this.checkCondition(t)?(this.PC=c,4):3;case 4:return this.write(65280+this.C,this.A),2;case 5:return this.write(this.fetchWord(),this.A),4;case 6:return this.A=this.read(65280+this.C),2;case 7:return this.A=this.read(this.fetchWord()),4}break;case 3:switch(t){case 0:return this.PC=this.fetchWord(),4;case 1:var u=this.fetchByte();return 2+this.cbOp(u);case 6:return this.ime=!1,this.imeDelay=!1,1;case 7:return this.imeDelay=!0,1;default:return 1}case 4:switch(t){case 0:case 1:case 2:case 3:var c=this.fetchWord();return this.checkCondition(t)?(this.pushWord(this.PC),this.PC=c,6):3;default:return 1}case 5:if(h===0)return this.pushWord(this.getReg16AF(s)),4;if(s===0){var c=this.fetchWord();return this.pushWord(this.PC),this.PC=c,6}return 1;case 6:return this.aluOp(t,this.fetchByte()),2;case 7:return this.pushWord(this.PC),this.PC=t*8,4}return 1}};x.FLAG_Z=128,x.FLAG_N=64,x.FLAG_H=32,x.FLAG_C=16;var S=x;var q=[4291876996,4287080524,4283591476,4281151778],J=W([[o.RIGHT,0,1],[o.LEFT,0,2],[o.UP,0,4],[o.DOWN,0,8],[o.A,0,16],[o.B,0,32],[o.GP_A,0,16],[o.GP_B,0,32],[o.SELECT,0,64],[o.START,0,128],[o.VK_ENTER,0,128],[o.VK_SHIFT,0,64]]),N=[[0,0,0,0,0,0,0,1],[1,0,0,0,0,0,0,1],[1,0,0,0,0,1,1,1],[0,1,1,1,1,1,1,0]],l=new Uint8Array(48);l[0]=128,l[1]=63,l[2]=0,l[3]=255,l[4]=191,l[5]=255,l[6]=63,l[7]=0,l[8]=255,l[9]=191,l[10]=127,l[11]=255,l[12]=159,l[13]=255,l[14]=191,l[15]=255,l[16]=255,l[17]=0,l[18]=0,l[19]=191,l[20]=0,l[21]=0,l[22]=112;var L=class{constructor(){this.enabled=!1;this.nr50=0;this.nr51=0;this.ch1_enabled=!1;this.ch1_dacEnabled=!1;this.ch1_duty=0;this.ch1_dutyPos=0;this.ch1_lengthCounter=0;this.ch1_lengthEnabled=!1;this.ch1_volume=0;this.ch1_envInitVol=0;this.ch1_envDir=0;this.ch1_envPeriod=0;this.ch1_envTimer=0;this.ch1_freq=0;this.ch1_freqTimer=0;this.ch1_sweepPeriod=0;this.ch1_sweepDir=0;this.ch1_sweepShift=0;this.ch1_sweepTimer=0;this.ch1_sweepEnabled=!1;this.ch1_sweepShadow=0;this.ch2_enabled=!1;this.ch2_dacEnabled=!1;this.ch2_duty=0;this.ch2_dutyPos=0;this.ch2_lengthCounter=0;this.ch2_lengthEnabled=!1;this.ch2_volume=0;this.ch2_envInitVol=0;this.ch2_envDir=0;this.ch2_envPeriod=0;this.ch2_envTimer=0;this.ch2_freq=0;this.ch2_freqTimer=0;this.ch3_enabled=!1;this.ch3_dacEnabled=!1;this.ch3_lengthCounter=0;this.ch3_lengthEnabled=!1;this.ch3_volume=0;this.ch3_freq=0;this.ch3_freqTimer=0;this.ch3_samplePos=0;this.ch3_sampleBuffer=0;this.waveRAM=new Uint8Array(16);this.ch4_enabled=!1;this.ch4_dacEnabled=!1;this.ch4_lengthCounter=0;this.ch4_lengthEnabled=!1;this.ch4_volume=0;this.ch4_envInitVol=0;this.ch4_envDir=0;this.ch4_envPeriod=0;this.ch4_envTimer=0;this.ch4_clockShift=0;this.ch4_widthMode=0;this.ch4_divisor=0;this.ch4_freqTimer=0;this.ch4_lfsr=32767;this.frameSeqTimer=0;this.frameSeqStep=0;this.regs=new Uint8Array(48)}reset(){this.enabled=!1,this.nr50=0,this.nr51=0,this.ch1_enabled=!1,this.ch1_dacEnabled=!1,this.ch1_duty=0,this.ch1_dutyPos=0,this.ch1_lengthCounter=0,this.ch1_lengthEnabled=!1,this.ch1_volume=0,this.ch1_envInitVol=0,this.ch1_envDir=0,this.ch1_envPeriod=0,this.ch1_envTimer=0,this.ch1_freq=0,this.ch1_freqTimer=0,this.ch1_sweepPeriod=0,this.ch1_sweepDir=0,this.ch1_sweepShift=0,this.ch1_sweepTimer=0,this.ch1_sweepEnabled=!1,this.ch1_sweepShadow=0,this.ch2_enabled=!1,this.ch2_dacEnabled=!1,this.ch2_duty=0,this.ch2_dutyPos=0,this.ch2_lengthCounter=0,this.ch2_lengthEnabled=!1,this.ch2_volume=0,this.ch2_envInitVol=0,this.ch2_envDir=0,this.ch2_envPeriod=0,this.ch2_envTimer=0,this.ch2_freq=0,this.ch2_freqTimer=0,this.ch3_enabled=!1,this.ch3_dacEnabled=!1,this.ch3_lengthCounter=0,this.ch3_lengthEnabled=!1,this.ch3_volume=0,this.ch3_freq=0,this.ch3_freqTimer=0,this.ch3_samplePos=0,this.ch3_sampleBuffer=0,this.waveRAM.fill(0),this.ch4_enabled=!1,this.ch4_dacEnabled=!1,this.ch4_lengthCounter=0,this.ch4_lengthEnabled=!1,this.ch4_volume=0,this.ch4_envInitVol=0,this.ch4_envDir=0,this.ch4_envPeriod=0,this.ch4_envTimer=0,this.ch4_clockShift=0,this.ch4_widthMode=0,this.ch4_divisor=0,this.ch4_freqTimer=0,this.ch4_lfsr=32767,this.frameSeqTimer=0,this.frameSeqStep=0,this.regs.fill(0)}readRegister(t){if(t>=48&&t<=63)return this.ch3_enabled?this.waveRAM[this.ch3_samplePos>>1]:this.waveRAM[t-48];if(t===38){var e=(this.enabled?128:0)|(this.ch1_enabled?1:0)|(this.ch2_enabled?2:0)|(this.ch3_enabled?4:0)|(this.ch4_enabled?8:0);return e|112}if(!this.enabled)return 255;var s=t-16;return this.regs[s]|l[s]}writeRegister(t,e){if(t>=48&&t<=63){this.waveRAM[t-48]=e;return}if(t===38){var s=this.enabled;if(this.enabled=!!(e&128),s&&!this.enabled){for(var h=16;h<=37;h++)this.writeRegister(h,0);this.ch1_enabled=!1,this.ch2_enabled=!1,this.ch3_enabled=!1,this.ch4_enabled=!1}!s&&this.enabled&&(this.frameSeqTimer=0,this.frameSeqStep=0);return}if(this.enabled){var r=t-16;switch(this.regs[r]=e,t){case 16:this.ch1_sweepPeriod=e>>4&7,this.ch1_sweepDir=e>>3&1,this.ch1_sweepShift=e&7;break;case 17:this.ch1_duty=e>>6&3,this.ch1_lengthCounter=64-(e&63);break;case 18:this.ch1_envInitVol=e>>4&15,this.ch1_envDir=e>>3&1,this.ch1_envPeriod=e&7,this.ch1_dacEnabled=(e&248)!==0,this.ch1_dacEnabled||(this.ch1_enabled=!1);break;case 19:this.ch1_freq=this.ch1_freq&1792|e;break;case 20:this.ch1_freq=this.ch1_freq&255|(e&7)<<8,this.ch1_lengthEnabled=!!(e&64),e&128&&this.triggerChannel1();break;case 22:this.ch2_duty=e>>6&3,this.ch2_lengthCounter=64-(e&63);break;case 23:this.ch2_envInitVol=e>>4&15,this.ch2_envDir=e>>3&1,this.ch2_envPeriod=e&7,this.ch2_dacEnabled=(e&248)!==0,this.ch2_dacEnabled||(this.ch2_enabled=!1);break;case 24:this.ch2_freq=this.ch2_freq&1792|e;break;case 25:this.ch2_freq=this.ch2_freq&255|(e&7)<<8,this.ch2_lengthEnabled=!!(e&64),e&128&&this.triggerChannel2();break;case 26:this.ch3_dacEnabled=!!(e&128),this.ch3_dacEnabled||(this.ch3_enabled=!1);break;case 27:this.ch3_lengthCounter=256-e;break;case 28:this.ch3_volume=e>>5&3;break;case 29:this.ch3_freq=this.ch3_freq&1792|e;break;case 30:this.ch3_freq=this.ch3_freq&255|(e&7)<<8,this.ch3_lengthEnabled=!!(e&64),e&128&&this.triggerChannel3();break;case 32:this.ch4_lengthCounter=64-(e&63);break;case 33:this.ch4_envInitVol=e>>4&15,this.ch4_envDir=e>>3&1,this.ch4_envPeriod=e&7,this.ch4_dacEnabled=(e&248)!==0,this.ch4_dacEnabled||(this.ch4_enabled=!1);break;case 34:this.ch4_clockShift=e>>4&15,this.ch4_widthMode=e>>3&1,this.ch4_divisor=e&7;break;case 35:this.ch4_lengthEnabled=!!(e&64),e&128&&this.triggerChannel4();break;case 36:this.nr50=e;break;case 37:this.nr51=e;break}}}triggerChannel1(){this.ch1_dacEnabled&&(this.ch1_enabled=!0),this.ch1_lengthCounter===0&&(this.ch1_lengthCounter=64),this.ch1_freqTimer=(2048-this.ch1_freq)*4,this.ch1_volume=this.ch1_envInitVol,this.ch1_envTimer=this.ch1_envPeriod||8,this.ch1_sweepShadow=this.ch1_freq,this.ch1_sweepTimer=this.ch1_sweepPeriod||8,this.ch1_sweepEnabled=this.ch1_sweepPeriod>0||this.ch1_sweepShift>0,this.ch1_sweepShift>0&&this.calcSweepFreq()}triggerChannel2(){this.ch2_dacEnabled&&(this.ch2_enabled=!0),this.ch2_lengthCounter===0&&(this.ch2_lengthCounter=64),this.ch2_freqTimer=(2048-this.ch2_freq)*4,this.ch2_volume=this.ch2_envInitVol,this.ch2_envTimer=this.ch2_envPeriod||8}triggerChannel3(){this.ch3_dacEnabled&&(this.ch3_enabled=!0),this.ch3_lengthCounter===0&&(this.ch3_lengthCounter=256),this.ch3_freqTimer=(2048-this.ch3_freq)*2,this.ch3_samplePos=0}triggerChannel4(){this.ch4_dacEnabled&&(this.ch4_enabled=!0),this.ch4_lengthCounter===0&&(this.ch4_lengthCounter=64);var t=this.ch4_divisor===0?8:this.ch4_divisor*16;this.ch4_freqTimer=t<<this.ch4_clockShift,this.ch4_volume=this.ch4_envInitVol,this.ch4_envTimer=this.ch4_envPeriod||8,this.ch4_lfsr=32767}calcSweepFreq(){var t=this.ch1_sweepShadow>>this.ch1_sweepShift;return this.ch1_sweepDir?t=this.ch1_sweepShadow-t:t=this.ch1_sweepShadow+t,t>2047&&(this.ch1_enabled=!1),t}clock(t){if(this.enabled)for(var e=0;e<t;e++){if(this.frameSeqTimer++,this.frameSeqTimer>=8192&&(this.frameSeqTimer=0,this.clockFrameSequencer()),this.ch1_enabled&&(this.ch1_freqTimer--,this.ch1_freqTimer<=0&&(this.ch1_freqTimer=(2048-this.ch1_freq)*4,this.ch1_dutyPos=this.ch1_dutyPos+1&7)),this.ch2_enabled&&(this.ch2_freqTimer--,this.ch2_freqTimer<=0&&(this.ch2_freqTimer=(2048-this.ch2_freq)*4,this.ch2_dutyPos=this.ch2_dutyPos+1&7)),this.ch3_enabled&&(this.ch3_freqTimer--,this.ch3_freqTimer<=0)){this.ch3_freqTimer=(2048-this.ch3_freq)*2,this.ch3_samplePos=this.ch3_samplePos+1&31;var s=this.waveRAM[this.ch3_samplePos>>1];this.ch3_sampleBuffer=this.ch3_samplePos&1?s&15:s>>4}if(this.ch4_enabled&&(this.ch4_freqTimer--,this.ch4_freqTimer<=0)){var h=this.ch4_divisor===0?8:this.ch4_divisor*16;this.ch4_freqTimer=h<<this.ch4_clockShift;var r=this.ch4_lfsr&1^this.ch4_lfsr>>1&1;this.ch4_lfsr=this.ch4_lfsr>>1|r<<14,this.ch4_widthMode&&(this.ch4_lfsr=this.ch4_lfsr&-65|r<<6)}}}clockFrameSequencer(){(this.frameSeqStep&1)===0&&this.clockLengthCounters(),(this.frameSeqStep===2||this.frameSeqStep===6)&&this.clockSweep(),this.frameSeqStep===7&&this.clockEnvelopes(),this.frameSeqStep=this.frameSeqStep+1&7}clockLengthCounters(){this.ch1_lengthEnabled&&this.ch1_lengthCounter>0&&(this.ch1_lengthCounter--,this.ch1_lengthCounter===0&&(this.ch1_enabled=!1)),this.ch2_lengthEnabled&&this.ch2_lengthCounter>0&&(this.ch2_lengthCounter--,this.ch2_lengthCounter===0&&(this.ch2_enabled=!1)),this.ch3_lengthEnabled&&this.ch3_lengthCounter>0&&(this.ch3_lengthCounter--,this.ch3_lengthCounter===0&&(this.ch3_enabled=!1)),this.ch4_lengthEnabled&&this.ch4_lengthCounter>0&&(this.ch4_lengthCounter--,this.ch4_lengthCounter===0&&(this.ch4_enabled=!1))}clockSweep(){if(this.ch1_sweepEnabled&&(this.ch1_sweepTimer--,this.ch1_sweepTimer<=0&&(this.ch1_sweepTimer=this.ch1_sweepPeriod||8,this.ch1_sweepPeriod>0))){var t=this.calcSweepFreq();t<=2047&&this.ch1_sweepShift>0&&(this.ch1_sweepShadow=t,this.ch1_freq=t,this.calcSweepFreq())}}clockEnvelopes(){this.ch1_envPeriod>0&&(this.ch1_envTimer--,this.ch1_envTimer<=0&&(this.ch1_envTimer=this.ch1_envPeriod||8,this.ch1_envDir&&this.ch1_volume<15?this.ch1_volume++:!this.ch1_envDir&&this.ch1_volume>0&&this.ch1_volume--)),this.ch2_envPeriod>0&&(this.ch2_envTimer--,this.ch2_envTimer<=0&&(this.ch2_envTimer=this.ch2_envPeriod||8,this.ch2_envDir&&this.ch2_volume<15?this.ch2_volume++:!this.ch2_envDir&&this.ch2_volume>0&&this.ch2_volume--)),this.ch4_envPeriod>0&&(this.ch4_envTimer--,this.ch4_envTimer<=0&&(this.ch4_envTimer=this.ch4_envPeriod||8,this.ch4_envDir&&this.ch4_volume<15?this.ch4_volume++:!this.ch4_envDir&&this.ch4_volume>0&&this.ch4_volume--))}getSample(){if(!this.enabled)return 0;var t=0,e=0,s=0,h=0;if(this.ch1_enabled&&this.ch1_dacEnabled&&(t=N[this.ch1_duty][this.ch1_dutyPos]?this.ch1_volume:0),this.ch2_enabled&&this.ch2_dacEnabled&&(e=N[this.ch2_duty][this.ch2_dutyPos]?this.ch2_volume:0),this.ch3_enabled&&this.ch3_dacEnabled){var r=this.ch3_sampleBuffer;switch(this.ch3_volume){case 0:r=0;break;case 1:break;case 2:r>>=1;break;case 3:r>>=2;break}s=r}this.ch4_enabled&&this.ch4_dacEnabled&&(h=this.ch4_lfsr&1?0:this.ch4_volume);var i=t/7.5-1,a=e/7.5-1,d=s/7.5-1,c=h/7.5-1,u=0,b=0;this.nr51&16&&(u+=i),this.nr51&32&&(u+=a),this.nr51&64&&(u+=d),this.nr51&128&&(u+=c),this.nr51&1&&(b+=i),this.nr51&2&&(b+=a),this.nr51&4&&(b+=d),this.nr51&8&&(b+=c);var F=(this.nr50>>4&7)+1,_=(this.nr50&7)+1;return u*=F/8,b*=_/8,(u+b)/8}saveState(){return{enabled:this.enabled,nr50:this.nr50,nr51:this.nr51,ch1_enabled:this.ch1_enabled,ch1_dacEnabled:this.ch1_dacEnabled,ch1_duty:this.ch1_duty,ch1_dutyPos:this.ch1_dutyPos,ch1_lengthCounter:this.ch1_lengthCounter,ch1_lengthEnabled:this.ch1_lengthEnabled,ch1_volume:this.ch1_volume,ch1_envInitVol:this.ch1_envInitVol,ch1_envDir:this.ch1_envDir,ch1_envPeriod:this.ch1_envPeriod,ch1_envTimer:this.ch1_envTimer,ch1_freq:this.ch1_freq,ch1_freqTimer:this.ch1_freqTimer,ch1_sweepPeriod:this.ch1_sweepPeriod,ch1_sweepDir:this.ch1_sweepDir,ch1_sweepShift:this.ch1_sweepShift,ch1_sweepTimer:this.ch1_sweepTimer,ch1_sweepEnabled:this.ch1_sweepEnabled,ch1_sweepShadow:this.ch1_sweepShadow,ch2_enabled:this.ch2_enabled,ch2_dacEnabled:this.ch2_dacEnabled,ch2_duty:this.ch2_duty,ch2_dutyPos:this.ch2_dutyPos,ch2_lengthCounter:this.ch2_lengthCounter,ch2_lengthEnabled:this.ch2_lengthEnabled,ch2_volume:this.ch2_volume,ch2_envInitVol:this.ch2_envInitVol,ch2_envDir:this.ch2_envDir,ch2_envPeriod:this.ch2_envPeriod,ch2_envTimer:this.ch2_envTimer,ch2_freq:this.ch2_freq,ch2_freqTimer:this.ch2_freqTimer,ch3_enabled:this.ch3_enabled,ch3_dacEnabled:this.ch3_dacEnabled,ch3_lengthCounter:this.ch3_lengthCounter,ch3_lengthEnabled:this.ch3_lengthEnabled,ch3_volume:this.ch3_volume,ch3_freq:this.ch3_freq,ch3_freqTimer:this.ch3_freqTimer,ch3_samplePos:this.ch3_samplePos,ch3_sampleBuffer:this.ch3_sampleBuffer,waveRAM:this.waveRAM.slice(0),ch4_enabled:this.ch4_enabled,ch4_dacEnabled:this.ch4_dacEnabled,ch4_lengthCounter:this.ch4_lengthCounter,ch4_lengthEnabled:this.ch4_lengthEnabled,ch4_volume:this.ch4_volume,ch4_envInitVol:this.ch4_envInitVol,ch4_envDir:this.ch4_envDir,ch4_envPeriod:this.ch4_envPeriod,ch4_envTimer:this.ch4_envTimer,ch4_clockShift:this.ch4_clockShift,ch4_widthMode:this.ch4_widthMode,ch4_divisor:this.ch4_divisor,ch4_freqTimer:this.ch4_freqTimer,ch4_lfsr:this.ch4_lfsr,frameSeqTimer:this.frameSeqTimer,frameSeqStep:this.frameSeqStep,regs:this.regs.slice(0)}}loadState(t){this.enabled=t.enabled,this.nr50=t.nr50,this.nr51=t.nr51,this.ch1_enabled=t.ch1_enabled,this.ch1_dacEnabled=t.ch1_dacEnabled,this.ch1_duty=t.ch1_duty,this.ch1_dutyPos=t.ch1_dutyPos,this.ch1_lengthCounter=t.ch1_lengthCounter,this.ch1_lengthEnabled=t.ch1_lengthEnabled,this.ch1_volume=t.ch1_volume,this.ch1_envInitVol=t.ch1_envInitVol,this.ch1_envDir=t.ch1_envDir,this.ch1_envPeriod=t.ch1_envPeriod,this.ch1_envTimer=t.ch1_envTimer,this.ch1_freq=t.ch1_freq,this.ch1_freqTimer=t.ch1_freqTimer,this.ch1_sweepPeriod=t.ch1_sweepPeriod,this.ch1_sweepDir=t.ch1_sweepDir,this.ch1_sweepShift=t.ch1_sweepShift,this.ch1_sweepTimer=t.ch1_sweepTimer,this.ch1_sweepEnabled=t.ch1_sweepEnabled,this.ch1_sweepShadow=t.ch1_sweepShadow,this.ch2_enabled=t.ch2_enabled,this.ch2_dacEnabled=t.ch2_dacEnabled,this.ch2_duty=t.ch2_duty,this.ch2_dutyPos=t.ch2_dutyPos,this.ch2_lengthCounter=t.ch2_lengthCounter,this.ch2_lengthEnabled=t.ch2_lengthEnabled,this.ch2_volume=t.ch2_volume,this.ch2_envInitVol=t.ch2_envInitVol,this.ch2_envDir=t.ch2_envDir,this.ch2_envPeriod=t.ch2_envPeriod,this.ch2_envTimer=t.ch2_envTimer,this.ch2_freq=t.ch2_freq,this.ch2_freqTimer=t.ch2_freqTimer,this.ch3_enabled=t.ch3_enabled,this.ch3_dacEnabled=t.ch3_dacEnabled,this.ch3_lengthCounter=t.ch3_lengthCounter,this.ch3_lengthEnabled=t.ch3_lengthEnabled,this.ch3_volume=t.ch3_volume,this.ch3_freq=t.ch3_freq,this.ch3_freqTimer=t.ch3_freqTimer,this.ch3_samplePos=t.ch3_samplePos,this.ch3_sampleBuffer=t.ch3_sampleBuffer,t.waveRAM&&this.waveRAM.set(t.waveRAM),this.ch4_enabled=t.ch4_enabled,this.ch4_dacEnabled=t.ch4_dacEnabled,this.ch4_lengthCounter=t.ch4_lengthCounter,this.ch4_lengthEnabled=t.ch4_lengthEnabled,this.ch4_volume=t.ch4_volume,this.ch4_envInitVol=t.ch4_envInitVol,this.ch4_envDir=t.ch4_envDir,this.ch4_envPeriod=t.ch4_envPeriod,this.ch4_envTimer=t.ch4_envTimer,this.ch4_clockShift=t.ch4_clockShift,this.ch4_widthMode=t.ch4_widthMode,this.ch4_divisor=t.ch4_divisor,this.ch4_freqTimer=t.ch4_freqTimer,this.ch4_lfsr=t.ch4_lfsr,this.frameSeqTimer=t.frameSeqTimer,this.frameSeqStep=t.frameSeqStep,t.regs&&this.regs.set(t.regs)}},P=class extends O{constructor(){super();this.cpuFrequency=4194304;this.canvasWidth=160;this.numVisibleScanlines=144;this.numTotalScanlines=154;this.cpuCyclesPerLine=456;this.sampleRate=9240*4;this.overscan=!1;this.defaultROMSize=32768;this.cpu=new S;this.ram=new Uint8Array(8192);this.vram=new Uint8Array(8192);this.oam=new Uint8Array(160);this.hram=new Uint8Array(128);this.extram=new Uint8Array(8192);this.joyp=207;this.sb=0;this.sc=0;this.divCounter=0;this.tima=0;this.tma=0;this.tac=0;this.iflag=0;this.ie=0;this.lcdc=145;this.stat=0;this.scy=0;this.scx=0;this.ly=0;this.lyc=0;this.dma=0;this.bgp=252;this.obp0=255;this.obp1=255;this.wy=0;this.wx=0;this.apu=new L;this.apuCycleAccum=0;this.ppuMode=0;this.ppuDot=0;this.windowLine=0;this.mbcType=0;this.romBank=1;this.ramBank=0;this.ramEnabled=!1;this.mbcMode=0;this.romBankMask=31;this.read=M([[0,16383,16383,e=>this.rom?this.rom[e]:255],[16384,32767,16383,e=>this.readBankedROM(e)],[32768,40959,8191,e=>this.vram[e]],[40960,49151,8191,e=>this.readExtRAM(e)],[49152,57343,8191,e=>this.ram[e]],[57344,65023,8191,e=>this.ram[e]],[65024,65183,255,e=>this.oam[e]],[65184,65279,255,e=>255],[65280,65407,127,e=>this.readIO(e)],[65408,65534,127,e=>this.hram[e]],[65535,65535,0,e=>this.ie]]);this.write=M([[0,8191,8191,(e,s)=>{this.writeMBC(e,s)}],[8192,16383,8191,(e,s)=>{this.writeMBC(8192+e,s)}],[16384,24575,8191,(e,s)=>{this.writeMBC(16384+e,s)}],[24576,32767,8191,(e,s)=>{this.writeMBC(24576+e,s)}],[32768,40959,8191,(e,s)=>{this.vram[e]=s}],[40960,49151,8191,(e,s)=>{this.writeExtRAM(e,s)}],[49152,57343,8191,(e,s)=>{this.ram[e]=s}],[57344,65023,8191,(e,s)=>{this.ram[e]=s}],[65024,65183,255,(e,s)=>{this.oam[e]=s}],[65184,65279,255,(e,s)=>{}],[65280,65407,127,(e,s)=>{this.writeIO(e,s)}],[65408,65534,127,(e,s)=>{this.hram[e]=s}],[65535,65535,0,(e,s)=>{this.ie=s}]]);this.timerSubCycles=0;this.handler=V(this.inputs,this.getKeyboardMap()),this.connectCPUMemoryBus(this)}getKeyboardMap(){return J}readBankedROM(e){if(!this.rom)return 255;var s=this.romBank;this.mbcType===0&&(s=1);var h=e+s*16384;return h<this.rom.length?this.rom[h]:255}writeMBC(e,s){if(this.mbcType!==0)if(e<8192)this.ramEnabled=(s&15)===10;else if(e<16384){var h=s&31;h===0&&(h=1),this.romBank=this.romBank&96|h,this.romBank&=this.romBankMask}else e<24576?this.mbcMode===0?(this.romBank=this.romBank&31|(s&3)<<5,this.romBank&=this.romBankMask):this.ramBank=s&3:this.mbcMode=s&1}readExtRAM(e){if(!this.ramEnabled&&this.mbcType>0)return 255;var s=e+this.ramBank*8192;return s<this.extram.length?this.extram[s]:255}writeExtRAM(e,s){if(!(!this.ramEnabled&&this.mbcType>0)){var h=e+this.ramBank*8192;h<this.extram.length&&(this.extram[h]=s)}}readIO(e){switch(e){case 0:return this.readJoypad();case 1:return this.sb;case 2:return this.sc;case 4:return this.divCounter>>8&255;case 5:return this.tima;case 6:return this.tma;case 7:return this.tac|248;case 15:return this.iflag|224;case 16:case 17:case 18:case 19:case 20:case 21:case 22:case 23:case 24:case 25:case 26:case 27:case 28:case 29:case 30:case 31:case 32:case 33:case 34:case 35:case 36:case 37:case 38:case 48:case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:case 58:case 59:case 60:case 61:case 62:case 63:return this.apu.readRegister(e);case 64:return this.lcdc;case 65:return this.stat|128;case 66:return this.scy;case 67:return this.scx;case 68:return this.ly;case 69:return this.lyc;case 70:return this.dma;case 71:return this.bgp;case 72:return this.obp0;case 73:return this.obp1;case 74:return this.wy;case 75:return this.wx;default:return 255}}writeIO(e,s){switch(e){case 0:this.joyp=s;break;case 1:this.sb=s;break;case 2:this.sc=s;break;case 4:this.divCounter=0;break;case 5:this.tima=s;break;case 6:this.tma=s;break;case 7:this.tac=s&7;break;case 15:this.iflag=s&31;break;case 16:case 17:case 18:case 19:case 20:case 21:case 22:case 23:case 24:case 25:case 26:case 27:case 28:case 29:case 30:case 31:case 32:case 33:case 34:case 35:case 36:case 37:case 38:case 48:case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:case 58:case 59:case 60:case 61:case 62:case 63:this.apu.writeRegister(e,s);break;case 64:this.lcdc=s,s&128||(this.ly=0,this.ppuMode=0,this.ppuDot=0,this.stat=this.stat&252);break;case 65:this.stat=this.stat&7|s&120;break;case 66:this.scy=s;break;case 67:this.scx=s;break;case 68:break;case 69:this.lyc=s;break;case 70:this.dmaTransfer(s);break;case 71:this.bgp=s;break;case 72:this.obp0=s;break;case 73:this.obp1=s;break;case 74:this.wy=s;break;case 75:this.wx=s;break}}readJoypad(){var e=this.joyp|15;return this.joyp&16||(e&=~(this.inputs[0]&15)),this.joyp&32||(e&=~(this.inputs[0]>>4&15)),e|192}dmaTransfer(e){this.dma=e;for(var s=e<<8,h=0;h<160;h++)this.oam[h]=this.read(s+h)}updateTimer(e){if(this.divCounter=this.divCounter+e*4&65535,!!(this.tac&4)){var s;switch(this.tac&3){case 0:s=1024;break;case 1:s=16;break;case 2:s=64;break;case 3:s=256;break;default:s=1024}for(var h=0;h<e;h++)this.tima++,this.tima>255&&(this.tima=this.tma,this.iflag|=4)}}syncInterrupts(){var e=this.iflag&this.ie&31;this.cpu.interruptFlags=e}syncInterruptsBack(){var e=this.iflag&this.ie&31&~this.cpu.interruptFlags;this.iflag&=~e}advanceCPU(){this.syncInterrupts();var e=this.cpu.interruptFlags,s=this.cpu,h=1;this.cpu.isStable()&&this.probe.logExecute(this.cpu.getPC(),this.cpu.getSP()),s.advanceInsn&&(h=s.advanceInsn(1));var r=h*4;if(this.probe.logClocks(r),this.cpu.interruptFlags!==e&&this.syncInterruptsBack(),this.updateTimerMCycles(h),this.updatePPUMode(r),this.apu.clock(r),this.audio){this.apuCycleAccum+=r;for(var i=114;this.apuCycleAccum>=i;)this.apuCycleAccum-=i,this.audio.feedSample(this.apu.getSample(),1)}return r}updatePPUMode(e){if(!(this.scanline>=144)){this.ppuDot+=e;var s=this.ppuMode;this.ppuDot<80?this.ppuMode=2:this.ppuDot<252?this.ppuMode=3:this.ppuMode=0,this.ppuMode!==s&&(this.stat=this.stat&252|this.ppuMode&3,this.ppuMode===0&&this.stat&8?this.iflag|=2:this.ppuMode===2&&this.stat&32&&(this.iflag|=2))}}updateTimerMCycles(e){var s=e*4;if(this.divCounter=this.divCounter+s&65535,!!(this.tac&4)){var h;switch(this.tac&3){case 0:h=1024;break;case 1:h=16;break;case 2:h=64;break;case 3:h=256;break;default:h=1024}for(this.timerSubCycles+=s;this.timerSubCycles>=h;)this.timerSubCycles-=h,this.tima=this.tima+1&255,this.tima===0&&(this.tima=this.tma,this.iflag|=4)}}startScanline(){this.ly=this.scanline,this.ppuDot=0,this.scanline<144?this.ppuMode=2:(this.ppuMode=1,this.scanline===144&&(this.iflag|=1,this.stat&16&&(this.iflag|=2))),this.ly===this.lyc?(this.stat|=4,this.stat&64&&(this.iflag|=2)):this.stat&=-5,this.stat=this.stat&252|this.ppuMode&3}drawScanline(){if(this.lcdc&128&&!(this.scanline>=144)){var e=this.scanline*160;if(this.lcdc&1)this.drawBGLine(e);else for(var s=0;s<160;s++)this.pixels[e+s]=q[0];this.lcdc&32&&this.scanline>=this.wy&&this.drawWindowLine(e),this.lcdc&2&&this.drawSpriteLine(e)}}getPaletteColor(e,s){var h=e>>s*2&3;return q[h]}drawBGLine(e){for(var s=this.lcdc&8?7168:6144,h=this.lcdc&16?0:2048,r=!(this.lcdc&16),i=this.scanline+this.scy&255,a=i>>3&31,d=i&7,c=0;c<160;c++){var u=c+this.scx&255,b=u>>3&31,F=7-(u&7),_=this.vram[s+a*32+b],m;r?m=4096+(_<<24>>24)*16:m=_*16;var p=this.vram[m+d*2],f=this.vram[m+d*2+1],g=(f>>F&1)<<1|p>>F&1;this.pixels[e+c]=this.getPaletteColor(this.bgp,g)}}drawWindowLine(e){var s=this.wx-7;if(!(s>=160)){for(var h=this.lcdc&64?7168:6144,r=this.lcdc&16?0:2048,i=!(this.lcdc&16),a=this.windowLine,d=a>>3&31,c=a&7,u=!1,b=Math.max(0,s);b<160;b++){var F=b-s,_=F>>3&31,m=7-(F&7),p=this.vram[h+d*32+_],f;i?f=4096+(p<<24>>24)*16:f=p*16;var g=this.vram[f+c*2],y=this.vram[f+c*2+1],C=(y>>m&1)<<1|g>>m&1;this.pixels[e+b]=this.getPaletteColor(this.bgp,C),u=!0}u&&this.windowLine++}}drawSpriteLine(e){for(var s=this.lcdc&4?16:8,h=0,r=[],i=0;i<40&&h<10;i++){var a=this.oam[i*4]-16,d=this.oam[i*4+1]-8;this.scanline>=a&&this.scanline<a+s&&(r.push({x:d,index:i}),h++)}r.sort((E,T)=>E.x!==T.x?E.x-T.x:E.index-T.index);for(var c=r.length-1;c>=0;c--){var u=r[c].index,b=this.oam[u*4]-16,F=this.oam[u*4+1]-8,_=this.oam[u*4+2],m=this.oam[u*4+3],p=m&16?this.obp1:this.obp0,f=!!(m&32),g=!!(m&64),y=!!(m&128);s===16&&(_&=254);var C=this.scanline-b;g&&(C=s-1-C);for(var R=_*16+C*2,K=this.vram[R],Y=this.vram[R+1],w=0;w<8;w++){var A=F+w;if(!(A<0||A>=160)){var H=f?w:7-w,I=(Y>>H&1)<<1|K>>H&1;if(I!==0){if(y){var $=this.pixels[e+A];if($!==q[this.bgp&3])continue}this.pixels[e+A]=this.getPaletteColor(p,I)}}}}}loadROM(e,s){if(e.length>327){var h=e[327];switch(h){case 0:this.mbcType=0;break;case 1:case 2:case 3:this.mbcType=1;break;default:this.mbcType=1;break}}this.rom=new Uint8Array(Math.max(e.length,32768)),this.rom.set(e);var r=Math.max(2,this.rom.length>>14);if(this.romBankMask=r-1,e.length>329)switch(e[329]){case 0:break;case 1:this.extram=new Uint8Array(2048);break;case 2:this.extram=new Uint8Array(8192);break;case 3:this.extram=new Uint8Array(32768);break;case 4:this.extram=new Uint8Array(131072);break;case 5:this.extram=new Uint8Array(65536);break}this.reset()}reset(){super.reset(),this.ram.fill(0),this.vram.fill(0),this.oam.fill(0),this.hram.fill(0),this.iflag=0,this.ie=0,this.lcdc=145,this.stat=0,this.scy=0,this.scx=0,this.ly=0,this.lyc=0,this.bgp=252,this.obp0=255,this.obp1=255,this.wy=0,this.wx=0,this.joyp=207,this.divCounter=0,this.tima=0,this.tma=0,this.tac=0,this.ppuMode=0,this.ppuDot=0,this.windowLine=0,this.romBank=1,this.ramBank=0,this.ramEnabled=!1,this.mbcMode=0,this.timerSubCycles=0,this.apuCycleAccum=0,this.apu.reset()}preFrame(){this.windowLine=0}readVRAMAddress(e){return this.vram[e&8191]}saveState(){var e=super.saveState();return e.vram=this.vram.slice(0),e.oam=this.oam.slice(0),e.hram=this.hram.slice(0),e.extram=this.extram.slice(0),e.io={joyp:this.joyp,sb:this.sb,sc:this.sc,divCounter:this.divCounter,tima:this.tima,tma:this.tma,tac:this.tac,iflag:this.iflag,ie:this.ie,lcdc:this.lcdc,stat:this.stat,scy:this.scy,scx:this.scx,ly:this.ly,lyc:this.lyc,dma:this.dma,bgp:this.bgp,obp0:this.obp0,obp1:this.obp1,wy:this.wy,wx:this.wx,ppuMode:this.ppuMode,ppuDot:this.ppuDot,windowLine:this.windowLine,romBank:this.romBank,ramBank:this.ramBank,ramEnabled:this.ramEnabled,mbcMode:this.mbcMode,timerSubCycles:this.timerSubCycles},e.apu=this.apu.saveState(),e.apuCycleAccum=this.apuCycleAccum,e}loadState(e){super.loadState(e),this.vram.set(e.vram),this.oam.set(e.oam),this.hram.set(e.hram),e.extram&&this.extram.set(e.extram);var s=e.io;this.joyp=s.joyp,this.sb=s.sb,this.sc=s.sc,this.divCounter=s.divCounter,this.tima=s.tima,this.tma=s.tma,this.tac=s.tac,this.iflag=s.iflag,this.ie=s.ie,this.lcdc=s.lcdc,this.stat=s.stat,this.scy=s.scy,this.scx=s.scx,this.ly=s.ly,this.lyc=s.lyc,this.dma=s.dma,this.bgp=s.bgp,this.obp0=s.obp0,this.obp1=s.obp1,this.wy=s.wy,this.wx=s.wx,this.ppuMode=s.ppuMode,this.ppuDot=s.ppuDot,this.windowLine=s.windowLine,this.romBank=s.romBank,this.ramBank=s.ramBank,this.ramEnabled=s.ramEnabled,this.mbcMode=s.mbcMode,this.timerSubCycles=s.timerSubCycles,e.apu&&this.apu.loadState(e.apu),this.apuCycleAccum=e.apuCycleAccum||0}getDebugCategories(){return["CPU","Stack","PPU"]}getDebugInfo(e,s){switch(e){case"PPU":return"LCDC "+n(this.lcdc,2)+" STAT "+n(this.stat,2)+`
|
|
LY `+n(this.ly,2)+" LYC "+n(this.lyc,2)+`
|
|
SCX `+n(this.scx,2)+" SCY "+n(this.scy,2)+`
|
|
WX `+n(this.wx,2)+" WY "+n(this.wy,2)+`
|
|
BGP `+n(this.bgp,2)+" OBP0 "+n(this.obp0,2)+`
|
|
OBP1 `+n(this.obp1,2)+" DMA "+n(this.dma,2)+`
|
|
IF `+n(this.iflag,2)+" IE "+n(this.ie,2)+`
|
|
DIV `+n(this.divCounter>>8&255,2)+" TIMA "+n(this.tima,2)+`
|
|
TMA `+n(this.tma,2)+" TAC "+n(this.tac,2)+`
|
|
Bank `+this.romBank+`
|
|
`}}};var Q=["nop","ld bc,xx","ld (bc),a","inc bc","inc b","dec b","ld b,x","rlca","ld (xx),sp","add hl,bc","ld a,(bc)","dec bc","inc c","dec c","ld c,x","rrca","stop x","ld de,xx","ld (de),a","inc de","inc d","dec d","ld d,x","rla","jr x","add hl,de","ld a,(de)","dec de","inc e","dec e","ld e,x","rra","jr nz,x","ld hl,xx","ld (hl+),a","inc hl","inc h","dec h","ld h,x","daa","jr z,x","add hl,hl","ld a,(hl+)","dec hl","inc l","dec l","ld l,x","cpl","jr nc,x","ld sp,xx","ld (hl-),a","inc sp","inc (hl)","dec (hl)","ld (hl),x","scf","jr c,x","add hl,sp","ld a,(hl-)","dec sp","inc a","dec a","ld a,x","ccf","ld b,b","ld b,c","ld b,d","ld b,e","ld b,h","ld b,l","ld b,(hl)","ld b,a","ld c,b","ld c,c","ld c,d","ld c,e","ld c,h","ld c,l","ld c,(hl)","ld c,a","ld d,b","ld d,c","ld d,d","ld d,e","ld d,h","ld d,l","ld d,(hl)","ld d,a","ld e,b","ld e,c","ld e,d","ld e,e","ld e,h","ld e,l","ld e,(hl)","ld e,a","ld h,b","ld h,c","ld h,d","ld h,e","ld h,h","ld h,l","ld h,(hl)","ld h,a","ld l,b","ld l,c","ld l,d","ld l,e","ld l,h","ld l,l","ld l,(hl)","ld l,a","ld (hl),b","ld (hl),c","ld (hl),d","ld (hl),e","ld (hl),h","ld (hl),l","halt","ld (hl),a","ld a,b","ld a,c","ld a,d","ld a,e","ld a,h","ld a,l","ld a,(hl)","ld a,a","add a,b","add a,c","add a,d","add a,e","add a,h","add a,l","add a,(hl)","add a,a","adc a,b","adc a,c","adc a,d","adc a,e","adc a,h","adc a,l","adc a,(hl)","adc a,a","sub b","sub c","sub d","sub e","sub h","sub l","sub (hl)","sub a","sbc a,b","sbc a,c","sbc a,d","sbc a,e","sbc a,h","sbc a,l","sbc a,(hl)","sbc a,a","and b","and c","and d","and e","and h","and l","and (hl)","and a","xor b","xor c","xor d","xor e","xor h","xor l","xor (hl)","xor a","or b","or c","or d","or e","or h","or l","or (hl)","or a","cp b","cp c","cp d","cp e","cp h","cp l","cp (hl)","cp a","ret nz","pop bc","jp nz,xx","jp xx","call nz,xx","push bc","add a,x","rst 00h","ret z","ret","jp z,xx","xxCBxx","call z,xx","call xx","adc a,x","rst 08h","ret nc","pop de","jp nc,xx","??","call nc,xx","push de","sub x","rst 10h","ret c","reti","jp c,xx","??","call c,xx","??","sbc a,x","rst 18h","ldh (x),a","pop hl","ld (c),a","??","??","push hl","and x","rst 20h","add sp,x","jp (hl)","ld (xx),a","??","??","??","xor x","rst 28h","ldh a,(x)","pop af","ld a,(c)","di","??","push af","or x","rst 30h","ld hl,sp+x","ld sp,hl","ld a,(xx)","ei","??","??","cp x","rst 38h"],X=["rlc b","rlc c","rlc d","rlc e","rlc h","rlc l","rlc (hl)","rlc a","rrc b","rrc c","rrc d","rrc e","rrc h","rrc l","rrc (hl)","rrc a","rl b","rl c","rl d","rl e","rl h","rl l","rl (hl)","rl a","rr b","rr c","rr d","rr e","rr h","rr l","rr (hl)","rr a","sla b","sla c","sla d","sla e","sla h","sla l","sla (hl)","sla a","sra b","sra c","sra d","sra e","sra h","sra l","sra (hl)","sra a","swap b","swap c","swap d","swap e","swap h","swap l","swap (hl)","swap a","srl b","srl c","srl d","srl e","srl h","srl l","srl (hl)","srl a","bit 0,b","bit 0,c","bit 0,d","bit 0,e","bit 0,h","bit 0,l","bit 0,(hl)","bit 0,a","bit 1,b","bit 1,c","bit 1,d","bit 1,e","bit 1,h","bit 1,l","bit 1,(hl)","bit 1,a","bit 2,b","bit 2,c","bit 2,d","bit 2,e","bit 2,h","bit 2,l","bit 2,(hl)","bit 2,a","bit 3,b","bit 3,c","bit 3,d","bit 3,e","bit 3,h","bit 3,l","bit 3,(hl)","bit 3,a","bit 4,b","bit 4,c","bit 4,d","bit 4,e","bit 4,h","bit 4,l","bit 4,(hl)","bit 4,a","bit 5,b","bit 5,c","bit 5,d","bit 5,e","bit 5,h","bit 5,l","bit 5,(hl)","bit 5,a","bit 6,b","bit 6,c","bit 6,d","bit 6,e","bit 6,h","bit 6,l","bit 6,(hl)","bit 6,a","bit 7,b","bit 7,c","bit 7,d","bit 7,e","bit 7,h","bit 7,l","bit 7,(hl)","bit 7,a","res 0,b","res 0,c","res 0,d","res 0,e","res 0,h","res 0,l","res 0,(hl)","res 0,a","res 1,b","res 1,c","res 1,d","res 1,e","res 1,h","res 1,l","res 1,(hl)","res 1,a","res 2,b","res 2,c","res 2,d","res 2,e","res 2,h","res 2,l","res 2,(hl)","res 2,a","res 3,b","res 3,c","res 3,d","res 3,e","res 3,h","res 3,l","res 3,(hl)","res 3,a","res 4,b","res 4,c","res 4,d","res 4,e","res 4,h","res 4,l","res 4,(hl)","res 4,a","res 5,b","res 5,c","res 5,d","res 5,e","res 5,h","res 5,l","res 5,(hl)","res 5,a","res 6,b","res 6,c","res 6,d","res 6,e","res 6,h","res 6,l","res 6,(hl)","res 6,a","res 7,b","res 7,c","res 7,d","res 7,e","res 7,h","res 7,l","res 7,(hl)","res 7,a","set 0,b","set 0,c","set 0,d","set 0,e","set 0,h","set 0,l","set 0,(hl)","set 0,a","set 1,b","set 1,c","set 1,d","set 1,e","set 1,h","set 1,l","set 1,(hl)","set 1,a","set 2,b","set 2,c","set 2,d","set 2,e","set 2,h","set 2,l","set 2,(hl)","set 2,a","set 3,b","set 3,c","set 3,d","set 3,e","set 3,h","set 3,l","set 3,(hl)","set 3,a","set 4,b","set 4,c","set 4,d","set 4,e","set 4,h","set 4,l","set 4,(hl)","set 4,a","set 5,b","set 5,c","set 5,d","set 5,e","set 5,h","set 5,l","set 5,(hl)","set 5,a","set 6,b","set 6,c","set 6,d","set 6,e","set 6,h","set 6,l","set 6,(hl)","set 6,a","set 7,b","set 7,c","set 7,d","set 7,e","set 7,h","set 7,l","set 7,(hl)","set 7,a"];function Z(v,t,e,s){var h,r=1,i=!1;if(t===203?(h=X[e],r=2):h=Q[t],(!h||!h.length||h==="??")&&(h="??"),/\bxx\b/.test(h))h=h.replace(/\bxx\b/,"$"+n(e+(s<<8),4)),r+=2,i=!0;else if(/\bx\b/.test(h)){var a=t===203?s:e;if(h.startsWith("jr")||h.startsWith("jr ")){var d=a<128?v+2+a:v+2-(256-a);d&=65535,h=h.replace(/\bx\b/,"$"+n(d,4)),i=!0}else h.startsWith("ldh")?h=h.replace(/\bx\b/,"$"+n(a,2)):h=h.replace(/\bx\b/,"$"+n(a,2));r+=1}return{line:h.toUpperCase(),nbytes:r,isaddr:i}}var ee=[{id:"hello.sgb",name:"Hello (ASM)"},{id:"main.wiz",name:"Snake Game (Wiz)"}],k=class extends z{constructor(){super(...arguments);this.getToolForFilename=U}newMachine(){return new P}getPresets(){return ee}getDefaultExtension(){return".c"}readAddress(e){return this.machine.read(e)}readVRAMAddress(e){return this.machine.readVRAMAddress(e)}getOriginPC(){return 256}getROMExtension(){return".gb"}getMemoryMap(){return{main:[{name:"ROM Bank 0",start:0,size:16384,type:"rom"},{name:"ROM Bank 1+",start:16384,size:16384,type:"rom"},{name:"Video RAM",start:32768,size:8192,type:"ram"},{name:"External RAM",start:40960,size:8192,type:"ram"},{name:"Work RAM",start:49152,size:8192,type:"ram"},{name:"OAM",start:65024,size:160,type:"ram"},{name:"I/O Registers",start:65280,size:128,type:"io"},{name:"High RAM",start:65408,size:127,type:"ram"}]}}getDebugCategories(){return D(this.machine)?this.machine.getDebugCategories():["CPU","Stack","PPU"]}getDebugInfo(e,s){switch(e){case"CPU":return G(s.c);case"Stack":{var h=s.c.SP-1&65535,r=h&65280,i=r+255;return h==0&&(h=65536),j(this,[],r,i,h,205)}default:return D(this.machine)&&this.machine.getDebugInfo(e,s)}}disassemble(e,s){return Z(e,s(e),s(e+1),s(e+2))}showHelp(){return"https://8bitworkshop.com/docs/platforms/gameboy/"}};B.gb=k;B.gameboy=k;
|
|
//# sourceMappingURL=gb-ZNUP323D.js.map
|