import{aast0}from"./chunk-ZE6LZV4I.js";import{Basa0,Iase0,qas$,tasx0,yasU}from"./chunk-ULPRULB6.js";import{JasO,Oasb,gasq,iasj,lasW,masX,sasV,uasl}from"./chunk-RXF2JDJ3.js";import"./chunk-WAARL7ET.js";varz=classextends${constructor(){super();this.cpuFrequency=1022727;this.sampleRate=this.cpuFrequency;this.cpuCyclesPerLine=65;this.cpuCyclesPerFrame=this.cpuCyclesPerLine*262;this.canvasWidth=280;this.numVisibleScanlines=192;this.numTotalScanlines=262;this.defaultROMSize=77824;this.LOAD_BASE=0;this.HDR_SIZE=0;this.ram=newUint8Array(77824);this.cpu=newx0;this.grdirty=newArray(49152>>7);this.grparams={dirty:this.grdirty,grswitch:F,mem:this.ram};this.kbdlatch=0;this.soundstate=0;this.auxRAMselected=!1;this.auxRAMbank=1;this.writeinhibit=!0;this.bank2rdoffset=0;this.bank2wroffset=0;this.slots=newArray(8);this.fakeDrive={readROM:x=>{vara=this.cpu.getPC();if(a>=50688&&a<50944)switch(x){case0:return0;case1:returnthis.LOAD_BASE&255;case2:returnthis.LOAD_BASE>>8&255;default:return0}elseswitch(x){case0:returnthis.rom&&this.loadRAMWithProgram(),76;case1:return32;case3:return0;case5:return3;case7:return60;default:return0}},readConst:x=>0,read:x=>this.floatbus(),write:(x,a)=>{}};this.loadBIOS(newW().decode(X(atob(u0)))),this.connectCPUMemoryBus(this),this.bios[54170-(65536-this.bios.length)]=96}saveState(){return{c:this.cpu.saveState(),ram:this.ram.slice(),kbdlatch:this.kbdlatch,soundstate:this.soundstate,grswitch:this.grparams.grswitch,auxRAMselected:this.auxRAMselected,auxRAMbank:this.auxRAMbank,writeinhibit:this.writeinhibit,slots:this.slots.map(x=>x&&x.saveState&&x.saveState()),inputs:null}}loadState(x){this.cpu.loadState(x.c),this.ram.set(x.ram),this.kbdlatch=x.kbdlatch,this.soundstate=x.soundstate,this.grparams.grswitch=x.grswitch,this.auxRAMselected=x.auxRAMselected,this.auxRAMbank=x.auxRAMbank,this.writeinhibit=x.writeinhibit,this.setupLanguageCardConstants();for(vara=0;a<this.slots.length;a++)this.slots[a]&&this.slots[a].loadState&&this.slots[a].loadState(x.slots[a]);this.ap2disp.invalidate()}saveControlsState(){return{inputs:null,kbdlatch:this.kbdlatch}}loadControlsState(x){this.kbdlatch=x.kbdlatch}loadBIOS(x,a){x.length!=12288&&(console.log(`apple2 loadBIOS !!!WARNING!!!: BIOS wants length 0x3000, but BIOS '${a}' has length 0x${x.length.toString(16)}`),console.log("will load BIOS to end of memory anyway...")),this.bios=Uint8Array.from(x)}loadROM(x){if(x.length==16*35*256){vara=news0(this,x);this.slots[6]=a,this.reset()}else{if(super.loadROM(x),j(this.rom.slice(0,4),[0,5,22,0]))this.LOAD_BASE=this.rom[57]|this.rom[56]<<8,this.HDR_SIZE=58;else{lete=this.rom[0]|this.rom[1]<<8,m=this.rom[2]|this.rom[3]<<8,t=e<49152&&e+m<77824&&(e==2051||(e&255)==0);m==x.length-4&&t?(this.LOAD_BASE=e,this.HDR_SIZE=4):(this.LOAD_BASE=2051,this.HDR_SIZE=0)}this.slots[6]=this.fakeDrive}}loadRAMWithProgram(){console.log(`Loading program into Apple ][ RAM at $${this.LOAD_BASE.toString(16)}`);letx=this.rom.slice(this.HDR_SIZE,this.HDR_SIZE+this.ram.length-this.LOAD_BASE);this.ram.set(x,this.LOAD_BASE),this.HDR_SIZE==58&&(this.ram[48896]=76,this.ram[49007]=1)}reset(){this.auxRAMselected=!1,this.auxRAMbank=1,this.writeinhibit=!0,this.ram.fill(0,768,1024),super.reset(),this.skipboot()}skipboot(){for(varx=0;x<2e6&&(this.cpu.advanceClock(),this.cpu.getPC()>>8!=198);x++);for(varx=0;x<2e6&&(this.cpu.advanceClock(),!(this.cpu.getPC()>>8<198));x++);}readConst(x){if(x<49152)returnthis.ram[x];if(x>=53248)returnthis.auxRAMselected?x>=57344?this.ram[x]:this.ram[x+this.bank2rdoffset]:this.bios[x-(65536-this.bios.length)];if(x>=49408&&x<51200){vara=x>>8&7;return(this.slots[a]&&this.slots[a].readConst(x&255))|0}elsereturn0}read(x){if(x&=65535,x<49152||x>=53248)returnthis.readConst(x);if(x<49408){this.probe.logIORead(x,0);vara=x>>4&15;switch(a){case0:returnthis.kbdlatch;case1:this.kbdlatch&=127;break;case3:this.soundstate=this.soundstate^1;break;case5:(x&15)<8&&((x&1)!=0?this.grparams.grswitch|=1<<(x>>1&7):this.grparams.grswitch&=~(1<<(x>>1&7)));break;case6:switch(x&7){case1:case2:case3:retu