8bitworkshop/gen/atari8-N4PRCBAP.js

11 lines
26 KiB
JavaScript

import{a as xt}from"./chunk-ZE6LZV4I.js";import{B as ft,I as dt,g as nt,k as ht,q as ot,t as ct,y as lt}from"./chunk-ULPRULB6.js";import{J as I,O as T,V as A,W as i,X as it,Z as rt,a as v,aa as C,ea as at,g as f,r as _}from"./chunk-RXF2JDJ3.js";import"./chunk-WAARL7ET.js";var Ut=[0,25,17,9],Yt=[0,25+64,17+80,9+96],E=0,Bt=1,j=2,z=3,Ht=4,Qt=5,jt=7,zt=9,L=10,Xt=11,Gt=12,qt=13,ut=14,Wt=15,K=15;var Zt=17-4,$t=110-4,Jt=105,X=[0,0,8,10,8,16,8,16,8,4,4,2,1,2,1,1],te=[0,0,2,2,2,2,4,4,8,4,4,4,4,2,2,2],ee=[0,0,0,0,0,1,0,1,0,0,2,1,0,0,0,0],mt=[0,0,1,1,2,2,2,2,8,4,4,2,2,2,2,1],V=class{constructor(t,e){this.regs=new Uint8Array(16);this.dma_enabled=!1;this.dliop=0;this.mode=0;this.jmp=!1;this.lms=!1;this.dlarg_lo=0;this.dlarg_hi=0;this.period=0;this.scanaddr=0;this.startaddr=0;this.pfbyte=0;this.ch=0;this.linesleft=0;this.yofs=0;this.isfirstline=!1;this.v=0;this.h=0;this.linebuf=new Uint8Array(48);this.dmaclock=0;this.dmaidx=0;this.output=0;this.dramrefresh=!1;this.in_vscroll=0;this.read=t,this.nmi=e}reset(){this.regs.fill(0),this.regs[ut]=0,this.regs[K]=127,this.regs[Gt]=0,this.regs[qt]=255,this.setReg(E,0),this.h=this.v=0,this.startaddr=this.scanaddr=0,this.dmaclock=0}saveState(){return _(0,{},this)}loadState(t){_(0,this,t),this.setReg(E,t.regs[E])}static stateToLongString(t){let e="";return e+="H: "+v(t.h,3)+" V: "+v(t.v,3)+`
`,e+="DLIOp: "+f(t.dliop,2)+" Lines: "+t.yofs+"/"+t.linesleft,e+=" DMA "+(t.dma_enabled?"ON ":"off"),t.dma_enabled&&(e+=" idx "+t.dmaidx+" clk "+f(t.dmaclock)),e+=`
`,e+="Addr: "+f(t.scanaddr,4)+`
`,e+=A(t.regs,0,16).replace("$00","Regs"),e}setReg(t,e){switch(t){case L:this.regs[L]=255;return;case Wt:this.regs[K]=31;return}this.regs[t]=e}readReg(t){switch(t){case K:return this.regs[t];case Xt:return this.v>>1;default:return 255}}processDLIEntry(){if(this.mode==0)this.linesleft=(this.dliop>>4&7)+1,this.dmaclock=0;else{this.linesleft=X[this.mode],this.period=te[this.mode],this.jmp?(this.regs[j]=this.dlarg_lo,this.regs[z]=this.dlarg_hi,this.mode=this.period=0,this.dliop&64&&(this.linesleft=1,this.dma_enabled=!1),this.dmaclock=0):this.lms&&(this.scanaddr=this.dlarg_lo+(this.dlarg_hi<<8)),this.startaddr=this.scanaddr;let t=this.regs[E]&3,e=this.dliop&16?(this.regs[Ht]&15)>>1:0;this.dliop&16&&t<3&&t++,this.left=Ut[t]+e,this.right=Yt[t]+e;let s=this.regs[Qt]&15;this.dliop&32^this.in_vscroll&&(this.in_vscroll?this.linesleft=s+1:(this.linesleft-=s,this.yofs+=s),this.linesleft&=15,this.in_vscroll^=32)}}nextLine(){this.linesleft>0&&(this.linesleft--,this.yofs++,this.isfirstline=!1,this.mode>=8&&this.linesleft&&(this.scanaddr=this.startaddr))}triggerNMI(t){this.regs[K]=t|31,this.regs[ut]&t&&this.nmi()}getDlistAddr(){return this.regs[j]+(this.regs[z]<<8)}nextInsn(){let t=this.getDlistAddr(),e=this.read(t);return t=t+1&1023|t&~1023,this.regs[j]=t&255,this.regs[z]=t>>8,e}nextScreen(){let t=this.read(this.scanaddr);return this.incScanAddr(),t}incScanAddr(){this.scanaddr=this.scanaddr+1&4095|this.scanaddr&~4095}dlDMAEnabled(){return this.regs[E]&32}isVisibleScanline(){return this.v>=8&&this.v<248}isPlayfieldDMAEnabled(){return this.dma_enabled&&!this.linesleft}isPlayerDMAEnabled(){return this.regs[E]&8}isMissileDMAEnabled(){return this.regs[E]&12}isWSYNC(){return this.regs[L]!=0}clockPulse(){let t=this.isWSYNC();if(!this.isVisibleScanline())this.doVBlank();else{switch(this.h){case 0:this.isMissileDMAEnabled()&&(this.doPlayerMissileDMA(3),t=!0);break;case 1:if(this.isPlayfieldDMAEnabled()){let e=this.nextInsn();this.jmp=(e&~64)==1,this.lms=(e&64)!=0&&(e&15)!=0,this.mode=e&15,this.dliop=e,this.yofs=0,this.isfirstline=!0,t=!0}break;case 2:case 3:case 4:case 5:this.isPlayerDMAEnabled()&&(this.doPlayerMissileDMA(this.h+2),t=!0);break;case 6:case 7:this.isPlayfieldDMAEnabled()&&this.isfirstline&&(this.jmp||this.lms)&&(this.h==6&&(this.dlarg_lo=this.nextInsn()),this.h==7&&(this.dlarg_hi=this.nextInsn()),t=!0);break;case 8:this.isfirstline&&this.processDLIEntry(),this.dliop&128&&this.linesleft==1&&this.triggerNMI(128);break;case 9:break;case 111:this.dma_enabled&&this.nextLine(),++this.v;break}if(this.output=0,this.mode>=2&&this.period){let e=this.h<=Jt;this.dmaclock=this.dmaclock<<1&511,this.dmaclock&1<<this.period&&(this.dmaclock|=1),this.h==this.left&&(this.dmaclock|=1,this.dmaidx=0),this.h==this.right&&(this.dmaclock&=~1,this.dmaidx++),this.dmaclock&1?(this.mode<8&&this.isfirstline&&(e?this.linebuf[this.dmaidx]=this.nextScreen():this.incScanAddr(),t=e),this.dmaidx++):this.dmaclock&8&&(this.ch=this.linebuf[this.dmaidx-4/this.period],e?this.readBitmapData():this.mode>=8&&this.incScanAddr(),t=e),this.output=this.h>=this.left+3&&this.h<=this.right+2?4:0}}return(this.h<Zt||this.h>$t)&&(this.output=2),this.incHorizCounter(),!t&&this.dramrefresh&&(this.read(0),this.dramrefresh=!1,t=!0),t}incHorizCounter(){switch(this.h){case 25:case 25+4*1:case 25+4*2:case 25+4*3:case 25+4*4:case 25+4*5:case 25+4*6:case 25+4*7:case 25+4*8:this.dramrefresh=!0;break;case 102:this.regs[L]=0;break;case 113:this.h=0;return}++this.h}doVBlank(){this.linesleft=this.mode=this.period=0,this.h==111&&this.v++,this.v==248&&this.h==0&&this.triggerNMI(64),this.v==262&&this.h==112&&(this.v=0),this.v==7&&this.h==113&&(this.dma_enabled=this.dlDMAEnabled()!=0),this.output=2,this.dmaclock=0}doPlayerMissileDMA(t){let e=this.regs[E]&16,s=this.regs[jt]<<8;e?(s&=63488,s|=t<<8,s|=this.v&255):(s&=64512,s|=t<<7,s|=this.v>>1),this.read(s)}readBitmapData(){let t=this.mode;if(t<8){let e=this.ch,s=this.yofs>>ee[this.mode],a=s&7,n=this.regs[zt];(t&14)==6?(e&=63,n&=254):(e&=127,n&=252);let h=(e<<3)+(n<<8);if((t&14)==2){let o=this.regs[Bt],l=t==3&&(e&96)==96;o&4?this.pfbyte=this.read(h+(a^7)):this.pfbyte=this.read(h+a),l&&s<2&&(this.pfbyte=0),!l&&s>7&&(this.pfbyte=0),this.ch&128&&(o&1&&(this.pfbyte=0),o&2&&(this.pfbyte^=255))}else this.pfbyte=this.read(h+a)}else this.pfbyte=this.nextScreen()}shiftout(){if(this.output==4)switch(this.mode){case 2:case 3:case 15:{let t=this.pfbyte>>7&1;return this.pfbyte<<=1,t?8:6}case 6:case 7:{let t=this.pfbyte>>7&1;return this.pfbyte<<=1,t?(this.ch>>6)+4:0}case 9:case 11:case 12:{let t=this.pfbyte>>7&1;return this.pfbyte<<=1,t?4:0}case 4:case 5:{let t=this.pfbyte>>6&3;return this.pfbyte<<=2,this.ch&128?[0,4,5,7][t]:[0,4,5,6][t]}case 8:case 10:case 13:case 14:{let t=this.pfbyte>>6&3;return this.pfbyte<<=2,[0,4,5,6][t]}}return this.output}};var se=0;var ie=8,re=12,gt=13,pt=17,y=18,g=22,ae=23,ne=24,he=25,S=26,w=27,oe=28,bt=29,ce=30,le=31,fe=0,de=4,xe=8,N=12,_t=16,G=31,yt=-9,F=[0,1,2,3,7,7,7,7,8,8,8,8,4,5,6,7,0,1,2,3,7,7,7,7,8,8,8,8,4,5,6,7,0,1,6,7,5,5,5,5,8,8,8,8,2,3,4,5,0,1,6,7,5,5,5,5,8,8,8,8,2,3,4,5,4,5,6,7,3,3,3,3,8,8,8,8,0,1,2,3,4,5,6,7,3,3,3,3,8,8,8,8,0,1,2,3,4,5,6,7,3,3,3,3,8,8,8,8,0,1,2,3,4,5,6,7,3,3,3,3,8,8,8,8,0,1,2,3,2,3,4,5,7,7,7,7,8,8,8,8,0,1,6,7,2,3,4,5,7,7,7,7,8,8,8,8,0,1,6,7,2,3,4,5,7,7,7,7,8,8,8,8,0,1,6,7,2,3,4,5,7,7,7,7,8,8,8,8,0,1,6,7,2,3,4,5,7,7,7,7,8,8,8,8,0,1,6,7,2,3,4,5,7,7,7,7,8,8,8,8,0,1,6,7,2,3,4,5,7,7,7,7,8,8,8,8,0,1,6,7,2,3,4,5,7,7,7,7,8,8,8,8,0,1,6,7],ue=[y+0,y+1,y+2,y+3,g+0,g+1,g+2,g+3,S,S,S,S,g+0,g+1,g+2,g+3],U=class{constructor(){this.regs=new Uint8Array(32);this.readregs=new Uint8Array(32);this.shiftregs=new Uint32Array(8);this.count=0;this.an=0;this.rgb=0;this.pmcol=0;this.gtiacol=0;this.gtiacol2=0;this.hbias=yt;this.pmDebugMask=-1}reset(){this.regs.fill(0),this.readregs.fill(0),this.readregs[20]=15,this.readregs.fill(15,21),this.count=0}saveState(){return _(0,{},this)}loadState(t){_(0,this,t)}setReg(t,e){switch(t){case y:case y+1:case y+2:case y+3:case g:case g+1:case g+2:case g+3:case S:e&=254;break;case ce:this.readregs.fill(0,0,16);return}this.regs[t]=e}readReg(t){switch(t){case G:return this.readregs[t]&~this.regs[le]}return this.readregs[t]}sync(){this.count=0}setBias(t){this.hbias=yt+t}updateGfx(t,e,s){switch(t){case 0:this.regs[bt]&1&&(this.regs[pt]=s);break;case 2:case 3:case 4:case 5:this.regs[bt]&2&&(!(e&1)||!(this.regs[oe]&1<<t+2))&&(this.regs[gt-2+t]=s);break}}getPlayfieldColor(){switch(this.regs[w]>>6){case 0:switch(this.an){case 0:return S;case 4:case 5:case 6:case 7:return g+this.an-4;case 8:return this.regs[ne]&240|this.regs[ae]&15|256}break;case 1:return this.regs[S]&240|this.gtiacol&15|256;case 2:return ue[this.gtiacol];case 3:return this.regs[S]&15|this.gtiacol<<4|256}return 256}anySpriteActive(){return this.shiftregs[0]||this.shiftregs[1]||this.shiftregs[2]||this.shiftregs[3]||this.shiftregs[4]||this.shiftregs[5]||this.shiftregs[6]||this.shiftregs[7]}processPlayerMissile(){if(!this.anySpriteActive()){this.evalTrigger(0),this.evalTrigger(1),this.evalTrigger(2),this.evalTrigger(3),this.evalTrigger(4),this.evalTrigger(5),this.evalTrigger(6),this.evalTrigger(7),this.pmcol=-1;return}if(this.an==2){this.shiftObject(0),this.shiftObject(1),this.shiftObject(2),this.shiftObject(3),this.shiftObject(4),this.shiftObject(5),this.shiftObject(6),this.shiftObject(7),this.pmcol=-1;return}let t=(this.regs[w]&15)<<4,e=F[(this.an&7)+8+t],s=this.an-4,a=-1,n=0;for(let h=0;h<4;h++)if(this.shiftObject(h)){s>=0&&(this.readregs[de+h]|=1<<s),n|=1<<h;let l=F[h+t];l<e&&(a=h,e=l)}for(let h=0;h<4;h++)if(this.shiftObject(h+4)){s>=0&&(this.readregs[fe+h]|=1<<s),this.readregs[xe+h]|=n;let l=this.regs[w]&16?F[t+15]:F[h+t];l<e&&(a=h+4,e=l)}n&1&&(this.readregs[N+0]|=n&~1),n&2&&(this.readregs[N+1]|=n&~2),n&4&&(this.readregs[N+2]|=n&~4),n&8&&(this.readregs[N+3]|=n&~8),this.pmcol=a>=0?this.getObjectColor(a):-1}shiftObject(t){let e=(this.shiftregs[t]&2147483648)!=0;return this.shiftregs[t]<<=1,this.evalTrigger(t),e}getObjectColor(t){return this.regs[w]&16&&t>=4?this.regs[he]:this.regs[y+(t&3)]}evalTrigger(t){this.regs[se+t]+this.hbias==this.count&&this.triggerObject(t)}triggerObject(t){let e,s;if(!!(this.pmDebugMask&1<<t)){if(t<4)e=this.regs[ie+t]&3,s=this.regs[gt+t];else{let a=t-4<<1;e=this.regs[re]>>a&3,s=(this.regs[pt]>>a&3)<<6}e&1?s=At(s):s<<=8,e==3?s=At(s):s<<=16,this.shiftregs[t]|=s}}clockPulse1(){this.processPlayerMissile(),this.clockPulse2(),this.count++}clockPulse2(){var t;if(this.pmcol>=0)t=this.pmcol;else{let e=this.getPlayfieldColor();t=e&256?e&255:this.regs[e]}this.rgb=Et[t],this.gtiacol2=this.gtiacol2<<1|this.an>>3}clockPulse4(){this.gtiacol=this.gtiacol2&15}static stateToLongString(t){let e="";return e+=`X: ${v(t.count,3)} ANTIC: ${f(t.an,1)} PM: ${f(t.pmcol,3)}
`,e+=`Write Registers:
`,e+=A(t.regs,0,32),e+=`Read Registers:
`,e+=A(t.readregs,0,32),e}};function At(r){return r=(r|r<<8)&16711935,r=(r|r<<4)&252645135,r=(r|r<<2)&858993459,r=(r|r<<1)&1431655765,r|r<<1}var Et=new Uint32Array(256);for(k=0;k<256;k++)Et[k]=at(k);var k;var c=0;var me=2;var St=4;var Ot=6;var p=8,ge=9,pe=10,be=11,_e=13,q=14,R=15;var ye=8,Ae=9,Ee=10,Se=13,P=14,Y=15;var Rt=128,It=64,Oe=32,Tt=16,Pt=8;var Re=1,Dt=28,Ie=114;var Mt=511,vt=131071,u=0,m=1,b=2,d=3,O=114,Te=15;var Pe=8;var W,B;function De(){W=new Uint8Array(511),B=new Uint8Array(16385);let r=511;for(let t=0;t<511;t++)r=(((r>>5^r)&1)<<8)+(r>>1),W[t]=r;r=131071;for(let t=0;t<16385;t++)r=(((r>>5^r)&255)<<9)+(r>>8),B[t]=r>>1}var H=class{constructor(t,e){this.irq=t;this.antic_xpos=e;this.regs=new Uint8Array(16);this.readregs=new Uint8Array(16);this.divnirq=new Uint32Array(4);this.divnmax=new Uint32Array(4);this.pot_inputs=new Uint8Array(8);this.basemult=0;this.pot_scanline=0;this.random_scanline_counter=0;this.kbcode=0;this.DELAYED_SERIN_IRQ=0;this.DELAYED_SEROUT_IRQ=0;this.DELAYED_XMTDONE_IRQ=0;this.init()}saveState(){return _(0,{},this)}loadState(t){_(0,this,t)}init(){this.readregs.fill(255),this.readregs[Y]=239,this.basemult=Dt,this.pot_inputs.fill(128),De()}read(t){let e=this.readregs[t];switch(t&=15,t){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:return e=this.pot_inputs[t],e<this.pot_scanline?e:this.pot_scanline;case ye:for(let s=0;s<8;s++)this.pot_inputs[s]<=this.pot_scanline&&(e&=~(1<<s));break;case Ae:return this.kbcode;case Y:e=Y+(this.CASSETTE_IOLineStatus()<<4);break;case Ee:if((this.regs[R]&3)!=0){let s=this.random_scanline_counter+this.antic_xpos();if(this.regs[p]&Rt)e=W[s%Mt];else{s%=vt;let a=s>>3;s&=7,e=(B[a]>>s)+(B[a+1]<<8-s)}}break}return e&255}write(t,e){switch(t&=15,this.regs[t]=e,t){case p:e&Re?this.basemult=Ie:this.basemult=Dt,this.update_counter(1<<u|1<<m|1<<b|1<<d);break;case c:this.update_counter(this.regs[p]&Tt?1<<m|1<<u:1<<u);break;case me:this.update_counter(1<<m);break;case St:this.update_counter(this.regs[p]&Pt?1<<d|1<<b:1<<b);break;case Ot:this.update_counter(1<<d);break;case q:this.readregs[P]|=~e&247,~this.readregs[P]&this.regs[q]&&this.generateIRQ(this.readregs[P]);break;case pe:this.readregs[Y]|=224;break;case be:this.regs[R]&4||(this.pot_scanline=0);break;case _e:(this.regs[R]&112)==32&&this.siocheck()&&this.SIO_PutByte(e),(this.regs[R]&8)==0?(this.DELAYED_SEROUT_IRQ=Pe,this.readregs[P]|=8,this.DELAYED_XMTDONE_IRQ=Te):(this.DELAYED_SEROUT_IRQ=312*50*10*(this.regs[St]+this.regs[Ot]*256)/895e3,this.DELAYED_SEROUT_IRQ>=3?(this.readregs[P]|=8,this.DELAYED_XMTDONE_IRQ=2*this.DELAYED_SEROUT_IRQ-2):(this.DELAYED_SEROUT_IRQ=0,this.DELAYED_XMTDONE_IRQ=0));break;case ge:this.divnirq[u]=this.divnmax[u],this.divnirq[m]=this.divnmax[m],this.divnirq[b]=this.divnmax[b],this.divnirq[d]=this.divnmax[d];break;case R:e&4&&(this.pot_scanline=228),(e&3)==0&&(this.DELAYED_SERIN_IRQ=0,this.DELAYED_SEROUT_IRQ=0,this.DELAYED_XMTDONE_IRQ=0);break}this.snd_update(t)}update_counter(t){t&1<<u&&(this.regs[p]&It?this.divnmax[u]=this.regs[c+u]+4:this.divnmax[u]=(this.regs[c+u]+1)*this.basemult,this.divnmax[u]<O&&(this.divnmax[u]=O)),t&1<<m&&(this.regs[p]&Tt?this.regs[p]&It?this.divnmax[m]=this.regs[c+m]*256+this.regs[c+u]+7:this.divnmax[m]=(this.regs[c+m]*256+this.regs[c+u]+1)*this.basemult:this.divnmax[m]=(this.regs[c+m]+1)*this.basemult,this.divnmax[m]<O&&(this.divnmax[m]=O)),t&1<<d&&(this.regs[p]&Pt?this.regs[p]&Oe?this.divnmax[d]=this.regs[c+d]*256+this.regs[c+b]+7:this.divnmax[d]=(this.regs[c+d]*256+this.regs[c+b]+1)*this.basemult:this.divnmax[d]=(this.regs[c+d]+1)*this.basemult,this.divnmax[d]<O&&(this.divnmax[d]=O))}snd_update(t){}advanceScanline(){(this.regs[R]&3)!=0&&(this.pot_scanline<228&&this.pot_scanline++,this.random_scanline_counter+=O,this.random_scanline_counter%=this.regs[p]&Rt?Mt:vt,this.DELAYED_SERIN_IRQ>0&&--this.DELAYED_SERIN_IRQ==0&&(this.readregs[Se]=this.SIO_GetByte(),this.generateIRQ(32)),this.DELAYED_SEROUT_IRQ>0&&--this.DELAYED_SEROUT_IRQ==0&&this.generateIRQ(16),this.DELAYED_XMTDONE_IRQ>0&&--this.DELAYED_XMTDONE_IRQ==0&&this.generateIRQ(8),this.advanceIRQTimer(u,1),this.advanceIRQTimer(m,2),this.advanceIRQTimer(d,4))}advanceIRQTimer(t,e){(this.divnirq[t]-=O)<0&&(this.divnirq[t]+=this.divnmax[t],this.generateIRQ(e))}generateIRQ(t){this.regs[q]&t&&(this.irq(),this.readregs[P]&=~t)}static stateToLongString(t){let e="";return e+=`Write Registers:
`,e+=A(t.regs,0,16),e+=`Read Registers:
`,e+=A(t.readregs,0,16),e}CASSETTE_IOLineStatus(){return 0}siocheck(){return((this.regs[c+b]==40||this.regs[c+b]==16||this.regs[c+b]==8||this.regs[c+b]==10)&&this.regs[c+d]==0||(this.regs[R]&120)==40)&&(this.regs[p]&40)==40}SIO_PutByte(t){console.log("SIO put byte",t)}SIO_GetByte(){return 0}};var Me=[i.VK_L,i.VK_J,i.VK_SEMICOLON,i.VK_F4,i.VK_F5,i.VK_K,i.VK_BACK_SLASH,i.VK_TILDE,i.VK_O,null,i.VK_P,i.VK_U,i.VK_ENTER,i.VK_I,i.VK_MINUS2,i.VK_EQUALS2,i.VK_V,i.VK_F7,i.VK_C,i.VK_F6,i.VK_F4,i.VK_B,i.VK_X,i.VK_Z,i.VK_4,null,i.VK_3,i.VK_6,i.VK_ESCAPE,i.VK_5,i.VK_2,i.VK_1,i.VK_COMMA,i.VK_SPACE,i.VK_PERIOD,i.VK_N,null,i.VK_M,i.VK_SLASH,null,i.VK_R,null,i.VK_E,i.VK_Y,i.VK_TAB,i.VK_T,i.VK_W,i.VK_Q,i.VK_9,null,i.VK_0,i.VK_7,i.VK_BACK_SPACE,i.VK_8,null,null,i.VK_F,i.VK_H,i.VK_D,null,i.VK_CAPS_LOCK,i.VK_G,i.VK_S,i.VK_A],ve=rt([[i.UP,0,1],[i.DOWN,0,2],[i.LEFT,0,4],[i.RIGHT,0,8],[{c:16,n:"Shift",plyr:0,button:0},2,1],[i.VK_F1,3,1],[i.VK_F2,3,2],[i.VK_F3,3,4]]),Q=class extends ot{constructor(){super();this.cpuFrequency=1789773;this.numTotalScanlines=262;this.cpuCyclesPerLine=114;this.canvasWidth=336;this.numVisibleScanlines=224;this.aspectRatio=this.canvasWidth/this.numVisibleScanlines*.857;this.firstVisibleScanline=16;this.firstVisibleClock=(44-6)*2;this.defaultROMSize=32768;this.overscan=!0;this.audioOversample=2;this.sampleRate=this.numTotalScanlines*60*this.audioOversample;this.run_address=-1;this.inputs=new Uint8Array(4);this.linergb=new Uint32Array(this.canvasWidth);this.lastdmabyte=0;this.keycode=0;this.cart_80=!1;this.cart_a0=!1;this.xexdata=null;this.keyboard_active=!0;this.d500=new Uint8Array(256);this.cpu=new ct,this.ram=new Uint8Array(65536),this.bios=new Uint8Array(10240),this.bus=this.newBus(),this.connectCPUMemoryBus(this.bus),this.antic=new V(this.readDMA.bind(this),this.antic_nmi.bind(this)),this.gtia=new U,this.irq_pokey=new H(this.pokey_irq.bind(this),()=>this.antic.h),this.audio_pokey=nt(1),this.audioadapter=new ht(this.audio_pokey.pokey1,this.audioOversample,this.sampleRate),this.handler=it(this.inputs,ve,this.getKeyboardFunction(),!0)}newBus(){return{read:C([[0,32767,65535,t=>this.ram[t]],[32768,40959,65535,t=>this.cart_80?this.rom[t-32768]:this.ram[t]],[40960,49151,65535,t=>this.cart_a0?this.rom[t-32768]:this.ram[t]],[53248,53503,31,t=>this.gtia.readReg(t)],[53760,54015,15,t=>this.readPokey(t)],[54016,54271,15,t=>this.readPIA(t)],[54272,54527,15,t=>this.antic.readReg(t)],[54528,54783,255,t=>this.d500[t]],[55296,65535,65535,t=>this.bios[t-55296]]]),write:C([[0,49146,65535,(t,e)=>{this.ram[t]=e}],[49147,49151,65535,(t,e)=>{this.ram[t]=e,this.initCartA()}],[53248,53503,31,(t,e)=>{this.gtia.setReg(t,e)}],[53760,54015,15,(t,e)=>{this.writePokey(t,e)}],[54272,54527,15,(t,e)=>{this.antic.setReg(t,e)}],[54528,54783,255,(t,e)=>{this.writeMapper(t,e)}]])}}loadBIOS(t){this.bios.set(t)}reset(){super.reset(),this.antic.reset(),this.gtia.reset(),this.keycode=0}read(t){return this.bus.read(t)}readDMA(t){let e=this.bus.read(t);return this.probe.logDMARead(t,e),this.lastdmabyte=e,e}readConst(t){return t<53248||t>=54528?this.bus.read(t):255}write(t,e){this.bus.write(t,e)}readPokey(t){switch(t&15){case 9:return this.keycode&255;case 15:return~this.keycode>>6&4|~this.keycode>>3&8|18;default:return this.irq_pokey.read(t)}}readPIA(t){if(t==0||t==1)return~this.inputs[t]}writePokey(t,e){this.audio_pokey.pokey1.setRegister(t,e),this.irq_pokey.write(t,e)}startScanline(){this.gtia.sync();for(let t=0;t<4;t++)this.gtia.readregs[_t+t]=~this.inputs[2]>>t&1;this.gtia.readregs[G]=~this.inputs[3]&7,this.audio&&this.audioadapter.generate(this.audio),this.irq_pokey.advanceScanline()}drawScanline(){let t=this.antic.v-this.firstVisibleScanline;t>=0&&t<this.numVisibleScanlines&&this.pixels.set(this.linergb,t*this.canvasWidth)}advanceCPU(){this.antic.clockPulse()?(this.antic.h<8&&this.gtia.updateGfx(this.antic.h-1,this.antic.v,this.lastdmabyte),this.antic.isWSYNC()&&this.probe.logWait(0),this.probe.logClocks(1)):super.advanceCPU();let t=this.antic.h*4-this.firstVisibleClock,e=()=>{this.gtia.clockPulse1(),this.linergb[t++]=this.gtia.rgb},s=()=>{this.gtia.clockPulse2(),this.linergb[t++]=this.gtia.rgb};this.gtia.clockPulse4(),this.antic.dliop&16&&this.antic.regs[4]&1?(t+=2,this.gtia.setBias(-1)):this.gtia.setBias(0);let a=mt[this.antic.mode],n=this.antic.h&1;return(a<8||n)&&(this.gtia.an=this.antic.shiftout()),e(),a==1&&(this.gtia.an=this.antic.shiftout()),s(),a<=2&&(this.gtia.an=this.antic.shiftout()),e(),a==1&&(this.gtia.an=this.antic.shiftout()),s(),1}loadState(t){this.loadControlsState(t),this.cpu.loadState(t.c),this.ram.set(t.ram),this.antic.loadState(t.antic),this.gtia.loadState(t.gtia),this.irq_pokey.loadState(t.pokey),this.lastdmabyte=t.lastdmabyte,this.cart_80=t.cart_80,this.cart_a0=t.cart_a0}saveState(){return{c:this.cpu.saveState(),ram:this.ram.slice(0),antic:this.antic.saveState(),gtia:this.gtia.saveState(),pokey:this.irq_pokey.saveState(),inputs:this.inputs.slice(0),lastdmabyte:this.lastdmabyte,keycode:this.keycode,cart_80:this.cart_80,cart_a0:this.cart_a0}}loadControlsState(t){this.inputs.set(t.inputs),this.keycode=t.keycode}saveControlsState(){return{inputs:this.inputs.slice(0),keycode:this.keycode}}getRasterY(){return this.antic.v}getRasterX(){return this.antic.h}getRasterCanvasPosition(){return{x:this.antic.h*4-this.firstVisibleClock,y:this.antic.v-this.firstVisibleScanline}}getDebugCategories(){return["CPU","Stack","ANTIC","GTIA","POKEY"]}getDebugInfo(t,e){switch(t){case"ANTIC":return V.stateToLongString(e.antic);case"GTIA":return U.stateToLongString(e.gtia);case"POKEY":return H.stateToLongString(e.pokey)}}getKeyboardFunction(){return(t,e,s,a)=>{if(!this.keyboard_active)return!1;if(a&(T.KeyDown|T.KeyUp)){var n=Me;if(e==i.VK_F9.c)return this.irq_pokey.generateIRQ(128),!0;for(var h=0;h<n.length;h++)if(n[h]&&n[h].c==e&&(this.keycode=h,a&T.Shift&&(this.keycode|=64),a&T.Ctrl&&(this.keycode|=128),a&T.KeyDown))return this.keycode|=256,this.irq_pokey.generateIRQ(64),!0}}}pokey_irq(){this.cpu.IRQ(),this.probe.logInterrupt(2)}antic_nmi(){this.cpu.NMI(),this.probe.logInterrupt(1)}loadROM(t,e){t[0]==255&&t[1]==255&&!(e==null?void 0:e.endsWith(".rom"))?this.xexdata=t:this.loadCartridge(t)}loadCartridge(t){if(t[0]==67&&t[1]==65&&t[2]==82&&t[3]==84&&(t=t.slice(16)),t.length!=4096&&t.length!=8192&&t.length!=16384&&t.length!=32768)throw new Error("Sorry, this platform can only load 4/8/16/32 KB cartridges at the moment.");let e=new Uint8Array(32768);for(let s=0;s<=e.length-t.length;s+=t.length)e.set(t,s);this.run_address=e[32766]+e[32767]*256,this.cart_a0=!0,this.cart_80=t.length==16384,super.loadROM(e)}writeMapper(t,e){t==255&&(e==128&&(this.cart_80=!1),e==160&&(this.cart_a0=!1))}loadXEX(t){let e=2,s=this.d500,a=0;for(var n=-1;e<t.length;){let o=t[e+0]+t[e+1]*256,l=t[e+2]+t[e+3]*256;console.log("XEX",f(e),f(o),f(l)),e+=4;for(let x=o;x<=l;x++)this.ram[x]=t[e++];if(o==736&&l==737&&(n=this.ram[736]+this.ram[737]*256,console.log("XEX run",f(n))),o==738&&l==739){var h=this.ram[738]+this.ram[739]*256;console.log("XEX init",f(h)),s[a++]=32,s[a++]=h&255,s[a++]=h>>8}if(e>t.length)throw new Error("Bad .XEX file format")}n>=0&&(s[a++]=169,s[a++]=160,s[a++]=141,s[a++]=255,s[a++]=213,s[a++]=76,s[a++]=n&255,s[a++]=n>>8,this.ram[10]=0,this.ram[11]=213,this.run_address=54528)}initCartA(){this.cpu.getPC()==61823&&this.xexdata&&this.loadXEX(this.xexdata)}setPaddleInput(t,e){this.irq_pokey.pot_inputs[t]=255-e}getDebugDisplayList(){let t=this.antic.getDlistAddr(),e=()=>{let n=this.read(t);return t=t+1&1023|t&~1023,n},s=[],a=0;for(let n=0;n<256&&a<240;n++){let h=t,o=e(),l=o&15,x="",D=!1,M;if(l==0)M=(o>>4&7)+1,x+=" blank="+M;else{M=X[l],x+=" mode="+f(l),x+=" lines="+M,D=(o&~64)==1;let st=(o&64)!=0&&(o&15)!=0;if(o&16&&(x+=" HSCROL"),o&32&&(x+=" VSCROL"),o&128&&(x+=" DLI"),D&&o&64?x+=" JVB":D?x+=" JMP":st&&(x+=" LMS"),D||st){let Nt=e(),Ft=e();x+=" $"+f(Ft)+""+f(Nt)}}if(s.push("$"+f(h)+" y="+a+" "+x),D)break;a+=M}return s}},Z=class extends Q{newBus(){return{read:C([[0,16383,65535,t=>this.ram[t]],[16384,49151,65535,t=>this.rom?this.rom[t-16384]:0],[49152,53247,31,t=>this.gtia.readReg(t)],[54272,54527,15,t=>this.antic.readReg(t)],[59392,61439,15,t=>this.readPokey(t)],[63488,65535,2047,t=>this.bios[t]]]),write:C([[0,16383,65535,(t,e)=>{this.ram[t]=e}],[49152,53247,31,(t,e)=>{this.gtia.setReg(t,e)}],[54272,54527,15,(t,e)=>{this.antic.setReg(t,e)}],[59392,61439,15,(t,e)=>{this.writePokey(t,e)}]])}}};var $=[{id:"hello.dasm",name:"Hello World (ASM)"},{id:"hellopm.dasm",name:"Hello Sprites (ASM)"},{id:"helloconio.c",name:"Text Mode (C)"},{id:"siegegame.c",name:"Siege Game (C)"},{id:"hellodlist.c",name:"Display List (C)"}],Ct=$.concat([{id:"testmusic.c",name:"POKEY Music (C)"},{id:"sieve.bas",name:"Benchmark (FastBasic)",category:"FastBasic"},{id:"pmtest.bas",name:"Sprites Test (FastBasic)"},{id:"dli.bas",name:"DLI Test (FastBasic)"},{id:"joyas.bas",name:"Match-3 Game (FastBasic)"}]),kt={main:[{name:"RAM",start:0,size:49152,type:"ram"},{name:"Left Cartridge ROM",start:40960,size:8192,type:"rom"},{name:"GTIA",start:53248,size:32,type:"io"},{name:"POKEY",start:53760,size:16,type:"io"},{name:"PIA",start:54016,size:4,type:"io"},{name:"ANTIC",start:54272,size:16,type:"io"},{name:"Cartridge Control Line",start:54784,size:256,type:"io"},{name:"ROM",start:55296,size:2048,type:"rom"},{name:"Character Set",start:57344,size:1024,type:"rom"},{name:"ROM",start:58368,size:7168,type:"rom"}]};function Lt(r){return r.endsWith(".bas")||r.endsWith(".fb")||r.endsWith(".fbi")?"fastbasic":lt(r)}var J=class extends dt{constructor(){super(...arguments);this.getToolForFilename=Lt;this.showHelp=wt;this.getROMExtension=Ce;this.biosPath="res/altirra/kernel.rom"}newMachine(){return new Q}getPresets(){return Ct}getDefaultExtension(){return".c"}readAddress(t){return this.machine.readConst(t)}getMemoryMap(){return kt}async start(){let t=await this.loadKernel();await super.start(),this.machine.loadBIOS(t)}async loadKernel(){var t=await fetch(this.biosPath);if(t.status==200||t.size){var e=await t.arrayBuffer();return new Uint8Array(e)}else throw new Error("could not load BIOS file")}getDebugTree(){let t=super.getDebugTree();return t.display_list=this.machine.getDebugDisplayList(),t}},Kt=class extends J{constructor(){super(...arguments);this.biosPath="res/altirra/superkernel.rom"}getPresets(){return $}newMachine(){return new Z}},tt=class extends xt{constructor(){super(...arguments);this.getToolForFilename=Lt;this.getOpcodeMetadata=ft;this.showHelp=wt}getPresets(){return $}getDefaultExtension(){return".asm"}},et=class extends tt{constructor(){super(...arguments);this.getMemoryMap=function(){return kt}}getPresets(){return Ct}loadROM(t,e){this.started?(this.loadROMFile(e),this.loadRegion(":cartleft:cart:rom",e)):this.startModule(this.mainElement,{jsfile:"mame8bitws.js",biosfile:"a800xl.zip",cfgfile:"a800xl.cfg",driver:"a800xl",width:336*2,height:225*2,romfn:"/emulator/cart.rom",romdata:new Uint8Array(e),romsize:8192,preInit:function(s){}})}start(){}},Vt=class extends tt{constructor(){super(...arguments);this.getMemoryMap=function(){return{main:[{name:"RAM",start:0,size:16384,type:"ram"},{name:"Cartridge ROM",start:16384,size:32768,type:"rom"},{name:"GTIA",start:49152,size:32,type:"io"},{name:"ANTIC",start:54272,size:16,type:"io"},{name:"POKEY",start:59392,size:16,type:"io"},{name:"ATARI Character Set",start:63488,size:1024,type:"rom"},{name:"ROM",start:64512,size:1024,type:"rom"}]}}}loadROM(t,e){this.started?(this.loadROMFile(e),this.loadRegion(":cartleft:cart:rom",e)):this.startModule(this.mainElement,{jsfile:"mame8bitws.js",biosfile:"a5200/5200.rom",cfgfile:"a5200.cfg",driver:"a5200",width:336*2,height:225*2,romfn:"/emulator/cart.rom",romdata:new Uint8Array(e),romsize:32768,preInit:function(s){}})}start(){}};function Ce(r){return r==null?".bin":r[0]==255&&r[1]==255?".xex":".rom"}function wt(){return"https://8bitworkshop.com/docs/platforms/atari8/"}I["atari8-800.xlmame"]=et;I["atari8-800xl.mame"]=et;I["atari8-5200.mame"]=Vt;I["atari8-800"]=J;I["atari8-5200"]=Kt;
//# sourceMappingURL=atari8-N4PRCBAP.js.map