@moonbase-machines/c64 0.6.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/moonbase-c64.js +2 -2
- package/dist/moonbase-c64.js.map +3 -3
- package/package.json +3 -3
- package/start.js +2 -2
package/dist/moonbase-c64.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/*! @moonbase-machines/c64 0.
|
|
1
|
+
/*! @moonbase-machines/c64 0.8.0: MIT, (c) 2026 Mark Wharton (its LICENSE) */
|
|
2
2
|
var Ce=Object.defineProperty;var X=(x,t)=>{for(var e in t)Ce(x,e,{get:t[e],enumerable:!0})};var zt={};X(zt,{compile:()=>Ht,evaluator:()=>Y0,instantiate:()=>bt});var jt={};X(jt,{TOKENS:()=>C0,crunch:()=>_0,dataItems:()=>lt,direct:()=>ut,expression:()=>S0,lines:()=>X0,list:()=>v0,program:()=>G0,store:()=>dt});var Yt={};X(Yt,{BasicError:()=>j,ERRORS:()=>p0,fail:()=>M,fout:()=>K0,int16:()=>Q,nextRandom:()=>M0,scanNumber:()=>$0,seedRandom:()=>U0,val:()=>m0});var Pt={};X(Pt,{ARG:()=>Ie,ARGOV:()=>Le,ARISGN:()=>Ne,BITS:()=>$e,CONSTANTS:()=>N0,FAC:()=>Se,FACOV:()=>Fe,FloatError:()=>b0,Floats:()=>l0,RESHO:()=>Ue,pack:()=>W0,setFac:()=>q0,unpack:()=>nt});var Se=97,Ie=105,Fe=112,Le=86,Ne=111,$e=104,Ue=38,Me=x=>x.flatMap(t=>[t>>>24&255,t>>>16&255,t>>>8&255,t&255]),N0={47548:[129,0,0,0,0],48913:[128,0,0,0,0],47865:[132,32,0,0,0],47553:[3,127,94,86,203,121,128,19,155,11,100,128,118,56,147,22,130,56,170,59,32],47574:[128,53,4,243,52],47579:[129,53,4,243,52],47584:[128,128,0,0,0],47589:[128,49,114,23,248],49087:[129,56,170,59,41],49092:[7,113,52,88,62,86,116,22,126,179,27,119,47,238,227,133,122,29,132,28,42,124,99,89,88,10,126,117,253,231,198,128,49,114,24,16,129,0,0,0,0],48918:Me([-1e8,1e7,-1e6,1e5,-1e4,1e3,-100,10,-1,-216e4,216e3,-36e3,3600,-600,60]),48563:[155,62,188,31,253],48568:[158,110,107,39,253],48573:[158,110,107,40,0],58080:[129,73,15,218,162],58085:[131,73,15,218,162],58090:[127,0,0,0,0],58095:[5,132,230,26,45,27,134,40,7,251,248,135,153,104,137,1,135,35,53,223,225,134,165,93,231,40,131,73,15,218,162]};function W0(x){if(x===0)return[0,0,0,0,0];let t=Math.abs(x),e=Math.floor(Math.log2(t))+1,s=t/2**e;s>=1?(s/=2,e++):s<.5&&(s*=2,e--);let f=Math.round(s*2**32);return f>=2**32&&(f/=2,e++),[e+128,f>>>24&127|(x<0?128:0),f>>>16&255,f>>>8&255,f&255]}function q0(x,t){let e=W0(t);if(x.write(112,0),!e[0]){for(let s=97;s<=102;s++)x.write(s,0);return}x.write(97,e[0]),x.write(98,e[1]|128),x.write(99,e[2]),x.write(100,e[3]),x.write(101,e[4]),x.write(102,t<0?255:0)}function nt(x){if(!x[0])return 0;let t=(x[1]|128)*2**24+x[2]*65536+x[3]*256+x[4];return(x[1]&128?-1:1)*t/2**32*2**(x[0]-128)}var l0=class{constructor(t){this.mem=t}r(t){return this.mem.read(t&65535)}w(t,e){this.mem.write(t&65535,e&255)}mant(t){return((this.r(t)*256+this.r(t+1))*256+this.r(t+2))*256+this.r(t+3)}setMant(t,e){this.w(t,e/2**24),this.w(t+1,e/65536),this.w(t+2,e/256),this.w(t+3,e)}fac(){let t=this.r(97);return t?(this.r(102)&128?-1:1)*(this.mant(98)+this.r(112)/256)/2**32*2**(t-128):0}conupk(t){this.w(34,t),this.w(35,t>>8);let e=[0,1,2,3,4].map(s=>this.r(t+s));return this.w(109,e[4]),this.w(108,e[3]),this.w(107,e[2]),this.w(110,e[1]),this.w(111,e[1]^this.r(102)),this.w(106,e[1]|128),this.w(105,e[0]),{a:this.r(97),y:0}}movfm(t){this.w(34,t),this.w(35,t>>8);let e=[0,1,2,3,4].map(s=>this.r(t+s));return this.w(101,e[4]),this.w(100,e[3]),this.w(99,e[2]),this.w(102,e[1]),this.w(98,e[1]|128),this.w(97,e[0]),this.w(112,0),{a:e[0],y:0}}movmf(t){return this.round(),this.w(34,t),this.w(35,t>>8),this.w(t+4,this.r(101)),this.w(t+3,this.r(100)),this.w(t+2,this.r(99)),this.w(t+1,(this.r(102)|127)&this.r(98)),this.w(t,this.r(97)),this.w(112,0),{a:this.r(97),y:0}}movfa(){this.w(102,this.r(110));for(let t=5;t>=1;t--)this.w(96+t,this.r(104+t));return this.w(112,0),{a:this.r(105),x:0}}movaf(){return this.round(),this.movef()}movef(){for(let t=6;t>=1;t--)this.w(104+t,this.r(96+t));return this.w(112,0),{a:this.r(97),x:0}}round(){if(!this.r(97))return;let t=this.r(112);if(this.w(112,t<<1),!(t&128))return;let e=this.mant(98)+1;if(e<2**32){this.setMant(98,e);return}this.setMant(98,0),this.carryIn(1)}carryIn(t){let e=this.r(97)+1;if(e>255)throw R0();this.w(97,e);let s=this.mant(98),f=this.r(112);this.setMant(98,Math.floor(s/2)+(t?2**31:0)),this.w(112,f>>1|(s&1)<<7)}sign(){return this.r(97)?this.r(102)&128?255:1:0}fcomp(t){return this.compare(t).a}compare(t){this.w(36,t),this.w(37,t>>8);let e=this.r(t);if(!e)return{a:this.sign(),y:1};let s=this.r(t+1),f=this.r(102),n=(r,h)=>{let p=f&128?1:0;return{a:(r?p^1:p)?255:1,y:h}};if((s^f)&128)return{a:f&128?255:1,y:1};if(e!==this.r(97))return n(e>this.r(97),1);for(let[r,h]of[[1,98],[2,99],[3,100]]){let p=r===1?s|128:this.r(t+r),d=this.r(h);if(p!==d)return n(p>d,r)}let o=this.r(112)>127?1:0,c=this.r(t+4)-this.r(101)-o;return(c&255)===0?{a:0,y:4}:n(c>=0,4)}fadd(t){return this.conupk(t),this.faddt()}fsub(t){return this.conupk(t),this.fsubt()}fsubt(){return this.w(102,this.r(102)^255),this.w(111,this.r(102)^this.r(110)),this.faddt()}faddt(){if(!this.r(97))return this.movfa();this.w(86,this.r(112));let t=this.r(105);if(!t)return{a:0};let e=t-this.r(97)&255,s=105,f=t>=this.r(97)?1:0,n=0;if(e!==0&&(f?(this.w(97,t),this.w(102,this.r(110)),e=~e+1&255,this.w(86,0),s=97):this.w(112,0),n=e-249&128?this.shift(s,e,0):this.bitShift(s,e,this.r(112),!1)),this.r(111)&128){let r=s===105?97:105,h=this.r(86)-n,p=h&255,d=h<0?1:0,l=this.mant(r+1)-this.mant(s+1)-d;return this.w(112,p),this.setMant(98,(l+2**32)%2**32),l<0&&this.negate(),this.normal()}let o=n+this.r(86)+(e===0?1:0),c=this.mant(98)+this.mant(106)+(o>255?1:0);return this.w(112,o),this.setMant(98,c%2**32),c>=2**32&&this.carryIn(1),{a:this.r(97)}}negate(){this.w(102,this.r(102)^255),this.complement()}complement(){let t=(~this.r(112)&255)+1,e=2**32-1-this.mant(98)+(t>255?1:0);this.w(112,t),this.setMant(98,e%2**32)}normal(){let t=0;for(;!this.r(98);)if(this.w(98,this.r(99)),this.w(99,this.r(100)),this.w(100,this.r(101)),this.w(101,this.r(112)),this.w(112,0),t+=8,t===32)return this.zero();for(;!(this.r(98)&128);){let e=this.r(112),s=this.mant(98);this.w(112,e<<1),this.setMant(98,(s*2+(e>>7))%2**32),t++}return t>=this.r(97)?this.zero():(this.w(97,this.r(97)-t),{a:this.r(97)})}zero(){return this.w(97,0),this.w(102,0),{a:0}}shift(t,e,s=0){for(;;){let f=e+8+s;if(e=f&255,s=f>255?1:0,e&128||!e){this.byteShift(t);continue}let n=e-8-(1-s);return e=n&255,s=n>=0?1:0,s?this.r(112):this.bitShift(t,e,this.r(112),!0)}}byteShift(t){this.w(112,this.r(t+4)),this.w(t+4,this.r(t+3)),this.w(t+3,this.r(t+2)),this.w(t+2,this.r(t+1)),this.w(t+1,this.r(104))}bitShift(t,e,s,f){for(;e!==0;e=e+1&255){let n=this.r(t+1),o=this.mant(t+1),c=f?n&128:0;this.setMant(t+1,Math.floor(o/2)+c*2**24),s=s>>1|(o&1)<<7,f=!0}return s}fmult(t){return this.conupk(t),this.fmultt()}fmultt(){if(!this.r(97))return{a:0};let t=this.muldiv();if(t.zero)return{a:0};for(let s of[38,39,40,41])this.w(s,0);let e=t.c;for(let s of[112,101,100,99,98])e=this.mltply(this.r(s),e,s===98);return this.w(98,this.r(38)),this.w(99,this.r(39)),this.w(100,this.r(40)),this.w(101,this.r(41)),this.normal()}mltply(t,e,s){if(!t&&!s)return this.byteShift(37),this.shift(37,0,e),0;for(let f=t>>1|128,n=t&1;;){let o=this.mant(38),c=0;n&&(o+=this.mant(106),c=o>=2**32?1:0,o%=2**32);let r=this.r(112);if(this.w(112,r>>1|(o&1)<<7),this.setMant(38,Math.floor(o/2)+c*2**31),n=f&1,f>>=1,!f)return 1}}muldiv(t=this.r(105)){if(!t)return this.zero(),{zero:!0};let e=t+this.r(97),s=e&255;if(e<256&&s<128)return this.zero(),{zero:!0};if(e>=256&&s&128)throw R0();let f=s+128&255,n=e<256?1:0;return this.w(97,f),this.w(102,f?this.r(111):0),{zero:!1,c:n}}fdiv(t){return this.conupk(t),this.fdivt()}fdivt(){if(!this.r(97))throw new b0(20);if(this.round(),this.w(97,0-this.r(97)&255),this.muldiv().zero)return{a:0};let t=this.r(97)+1;if(t>255)throw R0();this.w(97,t);let e=this.mant(106),s=0,f=0,n=this.mant(98);for(let o=0;o<34;o++){let c=s||e>=n?1:0;c&&(e=(e-n+2**32)%2**32),f=f*2+c,s=e>=2**31?1:0,e=e*2%2**32}return this.setMant(106,e),this.w(112,f%4<<6),this.setMant(98,Math.floor(f/4)),this.normal()}qint(){if(!this.r(97)){for(let e of[98,99,100,101])this.w(e,0);return{a:0,y:0}}let t=this.r(97)-160&255;if(this.r(102)&128&&(this.w(104,255),this.complement()),t-249&255&128)this.shift(97,t,0);else{let e=this.mant(98);this.setMant(98,Math.floor(e/2)+(this.r(102)&128?2**31:0)),this.bitShift(97,t+1&255,0,!0)}return this.w(104,0),{y:0}}int(){if(this.r(97)>=160)return{a:this.r(97)};this.qint(),this.w(112,0);let t=this.r(102);return this.w(102,0),this.float32(t)}float32(t){return this.w(97,160),this.w(7,this.r(101)),t&128&&this.negate(),this.normal()}};Object.assign(l0.prototype,{mul10(){this.movaf();let x=this.r(97);if(!x)return{a:0};if(x+2>255)throw R0();return this.addDoubled(x+2)},addDoubled(x){let t=this.r(105)-x&255;this.w(111,0),this.w(97,x),this.w(102,this.r(110)),this.w(86,0);let e=this.bitShift(97,t,this.r(112),!1)+this.r(86),s=this.mant(98)+this.mant(106)+(e>255?1:0);this.w(112,e),this.setMant(98,s%2**32),s>=2**32&&this.carryIn(1);let f=this.r(97)+1;if(f>255)throw R0();return this.w(97,f),{a:f}},div10(){return this.movaf(),this.divInto(47865,0)},divInto(x,t){return this.w(111,t),this.movfm(x),this.fdivt()},byte(x){this.w(98,x),this.w(99,0);for(let t of[100,101,112,102])this.w(t,0);return this.w(97,136),x&128&&this.negate(),this.normal()},addDigit(x){return this.movaf(),this.byte(x),this.w(111,this.r(110)^this.r(102)),this.faddt()},poly2(x){return this.w(113,x),this.w(114,x>>8),this.poly()},poly(){this.movmf(92);let x=this.r(113)|this.r(114)<<8;this.w(103,this.r(x)),x++,this.w(113,x),this.w(114,x>>8);for(let t=x;;t=92)if(this.fmult(t),x=(this.r(113)|this.r(114)<<8)+5,this.w(113,x),this.w(114,x>>8),this.fadd(x),this.w(103,this.r(103)-1),!this.r(103))return{a:this.r(97)}},poly1(x){return this.w(113,x),this.w(114,x>>8),this.movmf(87),this.fmult(87),this.poly(),this.fmult(87)},log(){if(this.sign()!==1)throw new b0(14);let x=this.r(97)-128&255;return this.w(97,128),this.fadd(47574),this.fdiv(47579),this.fsub(47548),this.poly1(47553),this.fadd(47584),this.addDigit(x),this.fmult(47589)},exp(x=0){let t=this.r(97);if(this.fmult(49087),t&&this.r(97))x=0;else if(t){let o=129+t;x=o<256&&(o&255)<128?0:1}let e=this.r(112)+80+x;if(e>255){let o=this.mant(98)+1;this.setMant(98,o%2**32),o>=2**32&&this.carryIn(1)}this.w(86,e),this.movef();let s=()=>{if(!(this.r(102)&128))throw R0();return this.zero()};if(this.r(97)>=136)return s();this.int();let f=this.r(7)+129&255;if(!f)return s();for(let o=0;o<6;o++){let c=this.r(105+o);this.w(105+o,this.r(97+o)),this.w(97+o,c)}this.w(112,this.r(86)),this.fsubt(),this.negop(),this.poly2(49092),this.w(111,0);let n=f-1&255;return n?(this.muldiv(n),{a:0}):this.zero()},fpwrt(x,t){if(x)return this.exp(t);if(!this.r(105))return this.w(97,0),this.w(102,0),{a:0};this.movmf(78);let e=this.r(110),s=0;if(e&128){this.int();let{a:f,y:n}=this.compare(78);f?(e=f,s=n&1):(e=n,s=this.r(7)&1)}this.w(102,e);for(let f=5;f>=1;f--)this.w(96+f,this.r(104+f));return this.w(112,0),this.log(),this.fmult(78),this.exp(0),s&&this.r(97)&&this.w(102,this.r(102)^255),{a:this.r(97)}},sin(){this.movaf(),this.divInto(58085,this.r(110)),this.movaf(),this.int(),this.w(111,0),this.fsubt(),this.fsub(58090);let x=this.r(102);return x&128?(this.fadd(48913),this.r(102)&128||(this.w(18,this.r(18)^255),this.negop())):this.negop(),this.fadd(58090),x&128&&this.negop(),this.poly1(58095)},cos(){return this.fadd(58080),this.sin()},fout(){let x=(d,l)=>this.w(255+d,l),t=1;if(x(t,this.r(102)&128?45:32),this.w(102,this.r(255+t)),this.w(113,t),t++,!this.r(97))return x(t,48),x(t+1,0),{a:0,y:1};let e=0;this.r(97)<=128&&(this.fmult(48573),e=247),this.w(93,e);let s="FOUT4";for(;;)if(s==="FOUT4"){let d=this.fcomp(48568);if(d===0){s="BIGGES";break}s=d===1?"FOUT9":"FOUT3"}else if(s==="FOUT3"){if(this.fcomp(48563)===1){s="FOUT5";break}this.mul10(),this.w(93,this.r(93)-1&255),s=this.r(93)?"FOUT3":"FOUT9"}else if(this.div10(),this.w(93,this.r(93)+1&255),this.r(93))s="FOUT4";else{s="FOUT5";break}s==="FOUT5"&&this.fadd(48913),this.qint();let f=1,n;for(e=this.r(93)+10&255,e&128||e>=11?n=e-2:(f=e-1&255,n=0),this.w(94,n&255),this.w(93,f),(f===0||f&128)&&(t=this.r(113),t++,x(t,46),f&&(t++,x(t,48)),this.w(113,t)),t=0,f=128;;){let d=0;for(let l of[3,2,1,0]){let y=this.r(98+l)+this.r(48918+t+l)+d;this.w(98+l,y&255),d=y>>8}if(f=f+1&255,!(d?f&128:!(f&128))){if(e=f,d){let l=(e^255)+10+1;e=l&255,d=l>>8}if(e=e+47+d&255,t+=4,this.w(71,t),t=this.r(113)+1,f=e,x(t,e&127),this.w(93,this.r(93)-1&255),this.r(93)||(t++,x(t,46)),this.w(113,t),t=this.r(71),f=(f^255)&128,t===36||t===60)break}}t=this.r(113);let o;do o=this.r(255+t),t--;while(o===48);o!==46&&t++;let c=this.r(94);if(!c)return x(t+1,0),{a:0,y:1};let r=43;c&128&&(c=0-c&255,r=45),x(t+2,r),x(t+1,69);let h=47,p=1;do{h++;let d=c-10-(1-p);p=d>=0?1:0,c=d&255}while(p);return x(t+4,c+58&255),x(t+3,h),x(t+5,0),{a:0,y:1}},negop(){return this.r(97)&&this.w(102,this.r(102)^255),{a:this.r(102)}}});var b0=class extends Error{constructor(t){super("BASIC error "+t),this.x=t}},R0=()=>new b0(15);var j=class extends Error{},p0=["TOO MANY FILES","FILE OPEN","FILE NOT OPEN","FILE NOT FOUND","DEVICE NOT PRESENT","NOT INPUT FILE","NOT OUTPUT FILE","MISSING FILE NAME","ILLEGAL DEVICE NUMBER","NEXT WITHOUT FOR","SYNTAX","RETURN WITHOUT GOSUB","OUT OF DATA","ILLEGAL QUANTITY","OVERFLOW","OUT OF MEMORY","UNDEF'D STATEMENT","BAD SUBSCRIPT","REDIM'D ARRAY","DIVISION BY ZERO","ILLEGAL DIRECT","TYPE MISMATCH","STRING TOO LONG","FILE DATA","FORMULA TOO COMPLEX","CAN'T CONTINUE","UNDEF'D FUNCTION","VERIFY","LOAD","BREAK"],M=x=>{throw new j("?"+x+" ERROR")},o0=new Uint8Array(65536),De=new l0({read:x=>o0[x],write:(x,t)=>{o0[x]=t}});for(let[x,t]of Object.entries(N0))o0.set(t,+x);function K0(x){let t=W0(x);if(!t[0]||Math.abs(x)<2**-128)return" 0";t[0]>255&&M("OVERFLOW"),o0[97]=t[0],o0[98]=t[1]|128,o0.set(t.slice(2),99),o0[102]=x<0?255:0,o0[112]=0,De.fout();let e="";for(let s=256;o0[s];s++)e+=String.fromCharCode(o0[s]);return e}function m0(x){let t=x.replace(/ /g,""),e=/^[+-]?(\d*\.?\d*)(E[+-]?\d+)?/.exec(t),s=e&&e[1]&&e[1]!=="."?Number(e[0].replace(/E$/,"")):0;return Number.isFinite(s)?s:0}function $0(x,t){let e="",s=()=>{for(;x[t]===" ";)t++},f=()=>{for(;x[t]>="0"&&x[t]<="9";)e+=x[t++],s()};return s(),(x[t]==="-"||x[t]==="+")&&(e+=x[t++],s()),f(),x[t]==="."&&(e+=x[t++],s(),f()),x[t]==="E"&&(e+=x[t++],s(),(x[t]==="-"||x[t]==="+")&&(e+=x[t++],s()),f()),{text:e,end:t}}var Q=x=>{let t=Math.floor(x);return(t<-32768||t>32767)&&M("ILLEGAL QUANTITY"),t},U0=x=>Math.imul(x,2654435761)>>>0,M0=x=>(Math.imul(x,1664525)+1013904223>>>0&4294967294)>>>0;var ot=x=>Object.assign(new j("?SYNTAX ERROR"),{detail:x}),C0="END FOR NEXT DATA INPUT# INPUT DIM READ LET GOTO RUN IF RESTORE GOSUB RETURN REM STOP ON WAIT LOAD SAVE VERIFY DEF POKE PRINT# PRINT CONT LIST CLR CMD SYS OPEN CLOSE GET NEW TAB( TO FN SPC( THEN NOT STEP + - * / ^ AND OR > = < SGN INT ABS USR FRE POS SQR RND LOG EXP COS SIN TAN ATN PEEK LEN STR$ VAL ASC CHR$ LEFT$ RIGHT$ MID$ GO".split(" "),H=Object.fromEntries(C0.map((x,t)=>[x,128+t])),ve=new Set(["SGN","INT","ABS","USR","FRE","POS","SQR","RND","LOG","EXP","COS","SIN","TAN","ATN","PEEK","LEN","STR$","VAL","ASC","CHR$","LEFT$","RIGHT$","MID$"]),rt={[H.OR]:["or",1],[H.AND]:["and",2],[H[">"]]:[">",4],[H["="]]:["=",4],[H["<"]]:["<",4],[H["+"]]:["+",5],[H["-"]]:["-",5],[H["*"]]:["*",6],[H["/"]]:["/",6],[H["^"]]:["^",8]};function ct(x,t){let e={at:t,peek(){for(;x[e.at]===32;)e.at++;return x[e.at]},next(){let s=e.peek();return e.at++,s},end(){let s=e.peek();return s===0||s===58},expect(s){if(e.next()!==s)throw ot(`$${(e.at-1).toString(16)}: expected $${s.toString(16)}`)}};return e}var ht=x=>x>=65&&x<=90,w0=x=>x>=48&&x<=57;function T0(x){let t=String.fromCharCode(x.next());for(;ht(Q0(x))||w0(Q0(x));){let e=String.fromCharCode(x.next());t.length<2&&(t+=e)}return(Q0(x)===36||Q0(x)===37)&&(t+=String.fromCharCode(x.next())),t}var Q0=x=>x.peek();function n0(x,t){let e=x.peek();if(e===34){x.at++;let s=x.at;for(;t[x.at]&&t[x.at]!==34;)x.at++;let f=String.fromCharCode(...t.subarray(s,x.at));return t[x.at]===34&&x.at++,{k:"str",s:f,at:s}}if(w0(e)||e===46){let s="",f=()=>{s+=String.fromCharCode(x.next())};for(;w0(x.peek())||x.peek()===46;)f();if(x.peek()===69)for(f(),(x.peek()===H["-"]||x.peek()===H["+"])&&(x.at++,s+=t[x.at-1]===H["-"]?"-":"+");w0(x.peek());)f();return{k:"num",v:Number(s)}}if(e===40){x.at++;let s=r0(x,t,0);return x.expect(41),s}if(e===H["-"])return x.at++,{k:"neg",a:r0(x,t,7)};if(e===H["+"])return x.at++,r0(x,t,7);if(e===H.NOT)return x.at++,{k:"not",a:r0(x,t,3)};if(e===H.FN){x.at++;let s=T0(x);x.expect(40);let f=r0(x,t,0);return x.expect(41),{k:"fn-call",n:s,a:f}}if(e>=128&&ve.has(C0[e-128])){x.at++,x.expect(40);let s=[r0(x,t,0)];for(;x.peek()===44;)x.at++,s.push(r0(x,t,0));return x.expect(41),{k:"fn",n:C0[e-128].toLowerCase(),args:s}}if(ht(e)){let s=T0(x);if(s==="TI"||s==="ST"||s==="TI$")return{k:"special",n:s.toLowerCase()};if(x.peek()===40){x.at++;let f=[r0(x,t,0)];for(;x.peek()===44;)x.at++,f.push(r0(x,t,0));return x.expect(41),{k:"elem",n:s,i:f}}return{k:"var",n:s}}throw ot(`$${x.at.toString(16)}: not an expression ($${e.toString(16)})`)}function r0(x,t,e){let s=n0(x,t);for(;;){let f=x.peek(),n=rt[f];if(!n||n[1]<e)return s;x.at++;let o=n[0];n[1]===4&&rt[x.peek()]?.[1]===4&&(o={"<>":"<>","><":"<>","<=":"<=","=<":"<=",">=":">=","=>":">="}[o+rt[x.peek()][0]],x.at++),s={k:"op",o,a:s,b:r0(x,t,n[1]+1)}}}function lt(x,t){let e=[];for(let s of G0(x,t)){let f=s.bytes;for(let n=0,o=!1;f[n];n++)if(f[n]===34&&(o=!o),!o){if(f[n]===Bt)break;if(f[n]===at){for(n++;;n++){for(;f[n]===32;)n++;let c="",r=!1;if(f[n]===34){for(r=!0,n++;f[n]&&f[n]!==34;n++)c+=String.fromCharCode(f[n]);for(f[n]===34&&n++;f[n]&&f[n]!==44&&f[n]!==58;)n++}else for(;f[n]&&f[n]!==44&&f[n]!==58;n++)c+=String.fromCharCode(f[n]);if(e.push({text:c,quoted:r,line:s.n}),f[n]!==44)break}if(!f[n])break}}}return e}function ut(x,t,e){let s=X0(x,t),f=new Map(s.filter(n=>n.label!==void 0).map(n=>[n.line,n.label]));return s.push({at:0,k:"end",args:[]},{label:"direct",line:65535}),Vt(ct(x,e),x,s,n=>f.has(n)?f.get(n):{missing:n},null),s.push({at:0,k:"end",args:[]}),s}function S0(x,t){let e=ct(x,t);return{e:r0(e,x,0),end:e.at}}function X0(x,t){let e=[],s=new Map,f=[];for(let o=t;x[o]|x[o+1]<<8;o=x.indexOf(0,o+4)+1)s.set(x[o+2]|x[o+3]<<8,o),f.push(o);let n=o=>s.has(o)?s.get(o):{missing:o};for(let[o,c]of f.entries()){e.push({label:c,line:x[c+2]|x[c+3]<<8});let r=f[o+1]??null,h=ct(x,c+4);Vt(h,x,e,n,r)}return e}function Vt(x,t,e,s,f){for(;!x.end()||x.peek()===58;){if(x.peek()===58){x.at++;continue}let n=x.at;try{Ge(x,t,e,s,f,n)}catch(o){if(!(o instanceof j))throw o;for(e.push({at:n,k:"fail",what:/^\?(.*) ERROR$/.exec(o.message)[1],args:[]});t[x.at];)x.at++}}}function Ge(x,t,e,s,f,n){{let o=x.next(),c=o>=128?C0[o-128]:null,r=d=>e.push({at:n,...d}),h=()=>{let d=S0(t,x.at);return x.at=d.end,d.e},p=()=>{if(x.end()){r({k:"newline",args:[]});return}for(;!x.end();){let d=x.peek();if(d===H["TAB("]||d===H["SPC("]){x.at++;let l=h();x.expect(41),r({k:d===H["TAB("]?"tab":"spc",args:[l]})}else if(d===59){x.at++;continue}else if(d===44){x.at++,r({k:"print-comma",args:[]});continue}else r({k:"print",args:[h()]});if(x.end()){r({k:"newline",args:[]});break}if(x.peek()===59&&(x.at++,x.end()))break}};if(!c||c==="LET"){c?x.peek():x.at--;let d=n0(x,t);x.expect(H["="]),r({k:"let",t:d,e:h()})}else if(c==="IF"){let d=h();if(x.peek()===H.GOTO){x.at++,r({k:"if-goto",e:d,to:s(u0(x))});return}if(x.expect(H.THEN),w0(x.peek())){r({k:"if-goto",e:d,to:s(u0(x))});return}r({k:"unless",e:d,to:f??-1})}else if(c==="GOTO"||c==="GOSUB")r({k:c.toLowerCase(),to:s(u0(x))});else if(c==="GO")x.expect(H.TO),r({k:"goto",to:s(u0(x))});else{if(o>=163)throw ot(`$${n.toString(16)}: ${c} begins no statement`);if(c==="DATA"){let d=!1;for(;t[x.at]&&(d||t[x.at]!==58);)t[x.at]===34&&(d=!d),x.at++}else if(c==="REM")for(;t[x.at];)x.at++;else if(c==="RESTORE")r({k:"restore",args:[]});else if(c==="DIM")for(;;){let d=n0(x,t);if(d.k!=="elem"&&M("SYNTAX"),r({k:"dim",n:d.n,sizes:d.i}),x.peek()!==44)break;x.at++}else if(c==="DEF"){x.expect(H.FN);let d=T0(x);x.expect(40);let l=T0(x);x.expect(41),x.expect(H["="]),r({k:"def",n:d,p:l,e:h()})}else if(c==="INPUT"){let d=null;x.peek()===34&&(d=n0(x,t).s,x.next()!==59&&M("SYNTAX"));let l=[];for(;;){let y=n0(x,t);if(y.k!=="var"&&y.k!=="elem"&&M("SYNTAX"),l.push(y),x.peek()!==44)break;x.at++}r({k:"input-keys",prompt:d,targets:l})}else if(c==="READ")for(;;){let d=n0(x,t);if(d.k!=="var"&&d.k!=="elem"&&M("SYNTAX"),r({k:d.n.endsWith("$")?"read$":"read",t:d}),x.peek()!==44)break;x.at++}else if(c==="ON"){let d=h(),l=x.next();l!==H.GOTO&&l!==H.GOSUB&&M("SYNTAX");let y=[s(u0(x))];for(;x.peek()===44;)x.at++,y.push(s(u0(x)));r({k:"on",e:d,to:y,gosub:l===H.GOSUB})}else if(c==="RETURN"||c==="CLR"||c==="END"||c==="STOP"||c==="NEW"||c==="CONT")r({k:c.toLowerCase(),args:[]});else if(c==="RUN")r(w0(x.peek())?{k:"run",to:s(u0(x)),args:[]}:{k:"run",args:[]});else if(c==="LIST"){let d=u0(x),l=d;x.peek()===H["-"]&&(x.at++,l=u0(x)),x.end()||M("SYNTAX"),r({k:"list",args:[{k:"num",v:d},{k:"num",v:l||65535}]})}else if(c==="POKE"){let d=h();x.expect(44),r({k:"poke",a:d,v:h()})}else if(c==="SYS"){let d=h(),l=x.at;for(;t[x.at]&&t[x.at]!==58;)x.at++;r({k:"sys",e:d,text:l,end:x.at,words:""})}else if(c==="LOAD"){let d=[h()];for(;x.peek()===44;)x.at++,d.push(h());r({k:"load",args:d})}else if(c==="GET"&&x.peek()===35){x.at++;let d=h();x.expect(44);let l=[n0(x,t)];for(;x.peek()===44;)x.at++,l.push(n0(x,t));r({k:"get#",args:[d],targets:l})}else if(c==="GET")r({k:"get",t:n0(x,t)});else if(c==="INPUT#"){let d=h();x.expect(44);let l=[n0(x,t)];for(;x.peek()===44;)x.at++,l.push(n0(x,t));r({k:"input#",args:[d]}),l.forEach((y,E)=>r({k:E?"input-item":"input",t:y,last:E===l.length-1}))}else if(c==="WAIT"){let d=[h()];x.expect(44),d.push(h()),x.peek()===44&&(x.at++,d.push(h())),r({k:"wait",args:d})}else if(c==="SAVE"||c==="VERIFY"){let d=x.end()?[]:[h()];for(;x.peek()===44;)x.at++,d.push(h());r({k:c.toLowerCase(),args:d})}else if(c==="FOR"){let d=T0(x);x.expect(H["="]);let l=h();x.expect(H.TO);let y=h(),E=x.peek()===H.STEP?(x.at++,h()):void 0;r({k:"let",t:{k:"var",n:d},e:l}),e.push({at:n,k:"for",v:d,to:y,...E?{step:E}:{}})}else if(c==="NEXT"){if(!ht(x.peek())){r({k:"next",v:null,args:[]});return}for(;r({k:"next-var",v:T0(x),args:[]}),x.peek()===44;)x.at++}else if(c==="PRINT")p();else if(c==="CMD"||c==="PRINT#")r({k:"print#",args:[h()]}),x.peek()===44?x.at++:x.end()||M("SYNTAX"),p(),c==="PRINT#"&&r({k:"clrchn",args:[]});else if(c==="OPEN"){let d=x.end()?[]:[h()];for(;x.peek()===44;)x.at++,d.push(h());r({k:"open",args:d})}else if(c==="CLOSE")r({k:"close",args:[h()]});else throw new Error(`$${n.toString(16)}: ${c} is not handled`)}}}var u0=x=>{let t=0;for(;w0(x.peek());)t=t*10+x.next()-48;return t},D0=[...C0.flatMap(x=>[...x].map((t,e,s)=>t.charCodeAt(0)|(e===s.length-1?128:0))),0],Pe=H.PRINT,at=H.DATA,Bt=H.REM;function _0(x){let t=[...x,0],e=[],s=0,f=0,n=c=>(e.push(c),s++,c===0?"done":(c===58?f=0:c===at&&(f=at-58),c===Bt?"rem":null)),o=c=>{for(;;){let r=t[s];if(r===0||r===c)return n(r);e.push(r),s++}};for(;;){let c=t[s],r=null;if(c>=128){if(c!==255){s++;continue}r=n(c)}else if(c===32)r=n(c);else if(c===34)e.push(c),s++,r=o(34);else if(f&64)r=n(c);else if(c===63)r=n(Pe);else if(c>=48&&c<60)r=n(c);else{let h=0,p=0,d=null;for(;;){let l=s;for(;;){let y=t[l]-D0[h]&255;if(y===0){l++,h++;continue}y===128&&(d={token:128|p,end:l});break}if(d)break;p++;do h++;while(!(D0[h-1]&128));if(D0[h]===0)break}d?(s=d.end,r=n(d.token)):r=n(c)}if(r==="done"||r==="rem"&&o(0)==="done")return e}}function v0(x){let t=[],e=!1;for(let s of x){if(s===0)break;if(s<128||s===255||e){s===34&&(e=!e),t.push(s);continue}let f=s-127,n=255;for(;--f;)do n=n+1&255;while(!(D0[n]&128));for(;;){n=n+1&255;let o=D0[n];if(o&128){t.push(o&127);break}if(t.push(o),!o)break}}return t}function G0(x,t){let e=[];for(let s=t;x[s]|x[s+1]<<8;){let f=x.indexOf(0,s+4);e.push({n:x[s+2]|x[s+3]<<8,bytes:[...x.subarray(s+4,f+1)]}),s=f+1}return e}function dt(x,t,e,s){let f=G0(x,t).filter(o=>o.n!==e);s.length>1&&f.push({n:e,bytes:s}),f.sort((o,c)=>o.n-c.n);let n=t;for(let o of f){let c=n+4+o.bytes.length;x.set([c&255,c>>8,o.n&255,o.n>>8,...o.bytes],n),n=c}return x[n]=0,x[n+1]=0,n+2}var g0=(x,t=4)=>x.toString(16).toUpperCase().padStart(t,"0"),t0=x=>JSON.stringify(x),x0=x=>x.op!==void 0?`, b.here(0x${x.op.toString(16)})`:"";function B(x){switch(x.k){case"num":return String(x.v);case"str":return t0(x.s);case"special":return{ti:"b.ti()",ti$:"b.tis()",st:"b.st()",pi:String(Math.PI)}[x.n];case"var":return`b.get(${t0(x.n)})`;case"elem":return`b.elem(${t0(x.n)}, [${x.i.map(B).join(", ")}]${x0(x)})`;case"neg":return`-b.num(${B(x.a)})`;case"not":return`b.not(${B(x.a)})`;case"op":{let t=B(x.a),e=B(x.b);switch(x.o){case"+":return`b.add(${t}, ${e}${x0(x)})`;case"-":return`(b.num(${t}) - b.num(${e}))`;case"*":return`(b.num(${t}) * b.num(${e}))`;case"/":return`b.div(${t}, ${e}${x0(x)})`;case"^":return`b.pow(${t}, ${e}${x0(x)})`;case"and":return`b.and(${t}, ${e}${x0(x)})`;case"or":return`b.or(${t}, ${e}${x0(x)})`;default:return`b.cmp(${t}, ${e}, ${t0(x.o)}${x0(x)})`}}case"fn-call":return`b.fn(${t0(x.n)}, ${B(x.a)})`;case"fn":{let t=x.args.map(B);return`b.${{str$:"str",chr$:"chr",left$:"left",right$:"right",mid$:"mid"}[x.n]??x.n}(${t.join(", ")}${t.length<3&&x.n==="mid$"?", undefined":""}${x0(x)})`}}throw new Error("no expression "+x.k)}var P0=(x,t,e)=>x.k==="elem"?`b.setElem(${t0(x.n)}, [${x.i.map(B).join(", ")}], ${t}${e?x0(e):""})`:`b.set(${t0(x.n)}, ${t}${e?x0(e):""})`,J0=x=>x.n.endsWith("$");function Ht(x,{name:t,source:e,mem:s,cost:f=0,render:n}){let o={},c=[];for(let l of x)l.label!==void 0?o["L"+g0(typeof l.label=="number"?l.label:parseInt(l.label.slice(1),16))]=c.length:c.push(l);let r=l=>{if(l?.missing!==void 0)return null;let y=o["L"+g0(l)];if(y===void 0)throw new Error("no label for $"+g0(l));return y},h=new Map,p=c.map((l,y)=>{let E=O=>O?.missing!==void 0?"b.undef();":`e.pc = ${r(O)};`,u;switch(l.k){case"let":u=P0(l.t,B(l.e),l);break;case"dim":u=`b.dim(${t0(l.n)}, [${l.sizes.map(B).join(", ")}]);`;break;case"poke":u=`b.poke(${B(l.a)}, ${B(l.v)}${x0(l)});`;break;case"for":u=`b.for(${t0(l.v)}, ${B(l.to)}, ${l.step?B(l.step):1}, ${y+1});`;break;case"next":case"next-var":u=`{ const back = b.next(${l.v?t0(l.v):"null"}); if (back !== null) e.pc = back; }`;break;case"goto":u=E(l.to);break;case"gosub":u=`b.gosub(${y+1}); ${E(l.to)}`;break;case"return":u="e.pc = b.ret();";break;case"on":u=`{ const n = b.on(${B(l.e)}${x0(l)}); if (n >= 1 && n <= ${l.to.length}) { const to = [${l.to.map(r).join(", ")}][n - 1]; if (to === null) b.undef(); ${l.gosub?`b.gosub(${y+1}); `:""}e.pc = to; } }`;break;case"unless":u=l.to<0?`if (!b.num(${B(l.e)})) e.pc = ${c.length};`:`if (!b.num(${B(l.e)})) ${E(l.to)}`;break;case"if-goto":u=`if (b.num(${B(l.e)})) ${E(l.to)}`;break;case"if-return":u=`if (b.num(${B(l.e)})) e.pc = b.ret();`;break;case"print":u=`b.print(${B(l.args[0])});`;break;case"print,":u=`b.print(${B(l.args[0])}); b.comma();`;break;case"print-line":u=`b.print(${B(l.args[0])}); b.newline();`;break;case"print-line#":u=`b.print(${B(l.args[0])}); b.newline(); b.clrchn();`;break;case"print#":u=`b.output(${B(l.args[0])});`;break;case"print#-line":u=`b.output(${B(l.args[0])}); b.newline(); b.clrchn();`;break;case"clrchn":u="b.clrchn();";break;case"input#":u=`b.input(${B(l.args[0])});`;break;case"get#":u=`b.input(${B(l.args[0])}); ${l.targets.map(O=>P0(O,J0(O)?"b.getFrom()":"b.val(b.getFrom())")).join("; ")}; b.clrchn();`;break;case"input":case"input-item":u=`{ const v = b.item(${J0(l.t)}, ${l.k==="input"}); if (v !== undefined) ${P0(l.t,"v")}; }`+(l.last?" b.inputDone();":"");break;case"input-start":u="";break;case"open":u=`b.open([${l.args.map(B).join(", ")}]);`;break;case"close":u=`b.close(${B(l.args[0])});`;break;case"wait":u=`if (!b.wait(${l.args.map(B).join(", ")})) { e.pc = ${y}; if (++e.idle > (e.routines.polls ?? 200) && !e.clock) e.polled = true; }`;break;case"print-comma":u="b.comma();";break;case"newline":u="b.newline();";break;case"tab":u=`b.tab(${B(l.args[0])}${x0(l)});`;break;case"spc":u=`b.spc(${B(l.args[0])}${x0(l)});`;break;case"get":u=`{ const k = b.key(); if (k === null) { e.pc = ${y}; return; } ${P0(l.t,J0(l.t)?"k":"b.val(k)")} }`;break;case"read":case"read$":u=P0(l.t,`b.read(${J0(l.t)})`);break;case"clr":u="b.clr();";break;case"restore":u="b.restore();";break;case"def":u=`b.def(${t0(l.n)}, ${t0(l.p)}, b => ${B(l.e)});`;break;case"end":case"stop":u="e.ended = true;";break;case"ti$=":u=`b.setTis(${B(l.args[0])});`;break;case"run":u=`b.run(${y+1}); ${l.to!==void 0?E(l.to):"e.pc = 0;"}`;break;case"fail":u=`b.failWith(${t0(l.what)});`;break;case"run-main":u=`e.pc = b.resume ?? ${c.length?r(l.to):0}; b.resume = undefined;`;break;case"nop":case"flag+40":case"flag-40":case"flag+80":case"flag-80":u="";break;case"sys":{if(u=`return b.sys(${B(l.e)}, 0x${g0(l.text)}, s[${y}]);`,s)for(let O=l.text;O<l.end;){for(;s[O]===32||s[O]===44;)O++;if(O>=l.end)break;let I;try{I=S0(s,O)}catch{break}h.set(O,[I.end,B(I.e),I.e.k==="str"?I.e.at:null]),O=I.end}break}default:u=`return b.statement(${t0(l.k)}, [${(l.args??[]).map(B).join(", ")}]${l.t?", "+t0(l.t.n):""}, s[${y}]);`}return(l.op!==void 0?`b.here(0x${l.op.toString(16)}); `:"")+u}),d=[];d.push(`// ${t}, compiled from ${e} by @moonbase-machines/c64/basic-compile.js: compiled again, never edited`),d.push("export default {"),d.push(` labels: ${JSON.stringify(o)},`),d.push(" data: [],"),d.push(" steps: [");for(let[l,y]of c.entries())d.push(` [0x${g0(y.at)}, ${l}, ${t0(n(y))}],`);d.push(" ],"),d.push(" run: {");for(let[l,y]of p.entries())d.push(` ${l}: (e, s) => { const b = e.basic;${f?` b.cost(${f});`:""} ${y} },`);d.push(" },"),d.push(" params: {");for(let[l,[y,E,u]]of[...h].sort((O,I)=>O[0]-I[0]))d.push(` 0x${g0(l)}: [0x${g0(y)}, b => ${E}${u!==null?`, 0x${g0(u)}`:""}],`);return d.push(" },"),d.push("};"),d.join(`
|
|
3
3
|
`)+`
|
|
4
4
|
`}function bt(x,t){let e=Ht(x,t);return new Function("return "+e.slice(e.indexOf("export default ")+15).replace(/;\s*$/,""))()}var Y0=x=>new Function("b","return "+B(x));var qt={};X(qt,{listing:()=>Be});var Wt={};X(Wt,{CONTROLS:()=>pt,bytes:()=>Ve,text:()=>mt});var pt={WHT:5,"DISABLE SHIFT":8,"ENABLE SHIFT":9,RETURN:13,LOWER:14,DOWN:17,"RVS ON":18,HOME:19,DEL:20,RED:28,RIGHT:29,GRN:30,BLU:31,ORNG:129,F1:133,F3:134,F5:135,F7:136,F2:137,F4:138,F6:139,F8:140,UPPER:142,BLK:144,UP:145,"RVS OFF":146,CLR:147,INST:148,BRN:149,LRED:150,GRY1:151,GRY2:152,LGRN:153,LBLU:154,GRY3:155,PUR:156,LEFT:157,YEL:158,CYN:159,"SHIFT SPACE":160,PI:255},Ye=Object.fromEntries(Object.entries(pt).map(([x,t])=>[t,x]));function Ve(x,{lower:t=!1}={}){let e=[];for(let s=0;s<x.length;s++){let f=x[s];if(f==="{"){let o=x.indexOf("}",s),c=x.slice(s+1,o);if(o<0)throw new Error("a { with no }: "+x);let r=/^\$([0-9A-F]{2})$/i.exec(c)?parseInt(c.slice(1),16):pt[c.toUpperCase()];if(r===void 0)throw new Error(`no control character {${c}}`);e.push(r),s=o;continue}let n=f.charCodeAt(0);f==="\xA3"?e.push(92):f==="\u2191"?e.push(94):f==="\u2190"?e.push(95):f==="\u03C0"?e.push(255):n>=97&&n<=122?e.push(n-32):t&&n>=65&&n<=90?e.push(n+128):e.push(n&255)}return e}function mt(x,{lower:t=!1}={}){let e="";for(let s of x)t&&s>=65&&s<=90?e+=String.fromCharCode(s+32):t&&s>=193&&s<=218?e+=String.fromCharCode(s-128):s===13?e+="\r":s>=32&&s<91||s===93?e+=String.fromCharCode(s):s===92?e+="\xA3":s===94?e+="\u2191":s===95?e+="\u2190":s===91?e+="[":e+=`{${Ye[s]??"$"+s.toString(16).toUpperCase().padStart(2,"0")}}`;return e}function Be(x,{lower:t=!1}={}){let e=[];for(let s=0;s+4<=x.length&&x[s]|x[s+1]<<8;){let f=x[s+2]|x[s+3]<<8,n=s+4;for(;n<x.length&&x[n];)n++;e.push(f+" "+mt(v0(x.subarray(s+4,n)),{lower:t})),s=n+1}return e.join(`
|
|
5
5
|
`)+`
|
|
6
6
|
`}var Xt={};X(Xt,{evaluateInMemory:()=>yt,install:()=>kt,printError:()=>Z0});var Kt={};X(Kt,{Basic:()=>k0});var k0=class{constructor(t,{data:e=0,quiet:s=!1}={}){this.e=t,this.data=this.dataStart=e,this.quiet=s,this.v=Object.create(null),this.arrays=Object.create(null),this.stack=[],this.channel=0}here(t){this.at=t}get(t){return this.v[t]??(t.endsWith("$")?"":0)}set(t,e){if(t.endsWith("$")){typeof e!="string"&&M("TYPE MISMATCH"),e.length>255&&M("STRING TOO LONG"),this.v[t]=e;return}typeof e!="number"&&M("TYPE MISMATCH"),this.v[t]=t.endsWith("%")?Q(e):e}clr(){this.v=Object.create(null),this.arrays=Object.create(null),this.stack=[],this.fns=null}run(t){let e=this.e.mem;e[152]=0,e[153]=0,e[154]=3,this.clr(),this.data=this.dataStart,this.datum=0,this.resume=t}dim(t,e){this.arrays[t]&&M("REDIM'D ARRAY");let s=e.map(n=>Q(n)+1),f=s.reduce((n,o)=>n*o,1);this.arrays[t]={dims:s,data:new Array(f).fill(t.endsWith("$")?"":0)}}place(t,e){let s=this.arrays[t]??(this.dim(t,e.map(()=>10)),this.arrays[t]);e.length!==s.dims.length&&M("BAD SUBSCRIPT");let f=0;for(let n=0;n<e.length;n++){let o=Q(e[n]);(o<0||o>=s.dims[n])&&M("BAD SUBSCRIPT"),f=f*s.dims[n]+o}return[s,f]}elem(t,e){let[s,f]=this.place(t,e);return s.data[f]}setElem(t,e,s){let[f,n]=this.place(t,e);t.endsWith("$")!==(typeof s=="string")&&M("TYPE MISMATCH"),f.data[n]=t.endsWith("%")?Q(s):s}add(t,e){return typeof t!=typeof e&&M("TYPE MISMATCH"),typeof t=="string"&&t.length+e.length>255&&M("STRING TOO LONG"),t+e}num(t){return typeof t!="number"&&M("TYPE MISMATCH"),t}cmp(t,e,s){typeof t!=typeof e&&M("TYPE MISMATCH");let f=t<e?-1:t>e?1:0;return{"=":f===0,"<>":f!==0,"<":f<0,">":f>0,"<=":f<=0,">=":f>=0}[s]?-1:0}and(t,e){return Q(this.num(t))&Q(this.num(e))}or(t,e){return Q(this.num(t))|Q(this.num(e))}not(t){return~Q(this.num(t))}div(t,e){return this.num(e)===0&&M("DIVISION BY ZERO"),this.num(t)/e}pow(t,e){return t=this.num(t),e=this.num(e),t<0&&e!==Math.floor(e)&&M("ILLEGAL QUANTITY"),t**e}int(t){return Math.floor(this.num(t))}abs(t){return Math.abs(this.num(t))}sqr(t){return this.num(t)<0&&M("ILLEGAL QUANTITY"),Math.sqrt(t)}log(t){return this.num(t)<=0&&M("ILLEGAL QUANTITY"),Math.log(t)}exp(t){return Math.exp(this.num(t))}sin(t){return Math.sin(this.num(t))}cos(t){return Math.cos(this.num(t))}tan(t){return Math.tan(this.num(t))}atn(t){return Math.atan(this.num(t))}len(t){return typeof t!="string"&&M("TYPE MISMATCH"),t.length}val(t){return typeof t!="string"&&M("TYPE MISMATCH"),m0(t)}rnd(){return this.random()}fre(){return 0}usr(){M("UNDEF'D FUNCTION")}sgn(t){return Math.sign(this.num(t))}chr(t){let e=Q(t);return(e<0||e>255)&&M("ILLEGAL QUANTITY"),String.fromCharCode(e)}asc(t){return t.length||M("ILLEGAL QUANTITY"),t.charCodeAt(0)}str(t){return K0(this.num(t))}left(t,e){return e=Q(e),(e<0||e>255)&&M("ILLEGAL QUANTITY"),t.slice(0,e)}right(t,e){return e=Q(e),(e<0||e>255)&&M("ILLEGAL QUANTITY"),e?t.slice(-e):""}mid(t,e,s=255){return e=Q(e),s=Q(s),(e<1||e>255||s<0||s>255)&&M("ILLEGAL QUANTITY"),t.substr(e-1,s)}peek(t){return t=Math.floor(this.num(t)),(t<0||t>65535)&&M("ILLEGAL QUANTITY"),this.e.read(t)}random(){let t=this.e;return t.randomState=M0(t.randomState??U0(t.title.seed??17)),t.randomState/2**32}ti(){let t=this.e,e=t.mem;return!t.clock&&!this.quiet&&(t.polledSince?(t.polledSince=!1,t.reads=0):(t.reads=(t.reads??0)+1)>=(t.routines.reads??1)&&(t.reads=0,t.routines.poll(t))),e[160]*65536+e[161]*256+e[162]}tis(){let t=Math.floor(this.ti()/60);return[Math.floor(t/3600)%24,Math.floor(t/60)%60,t%60].map(e=>String(e).padStart(2,"0")).join("")}setTis(t){/^\d{6}$/.test(t)||M("ILLEGAL QUANTITY");let e=((+t.slice(0,2)*60+ +t.slice(2,4))*60+ +t.slice(4))*60,s=this.e;s.a=e&255,s.x=e>>8&255,s.y=e>>16&255,this.call("settim")}st(){return this.e.mem[144]}poke(t,e){t=Math.floor(this.num(t)),e=Math.floor(this.num(e)),(t<0||t>65535||e<0||e>255)&&M("ILLEGAL QUANTITY"),this.e.write(t,e)}cost(t){let e=this.e;e.cycles+=t,e.delay+=t/1022.727}sys(t,e,s){return this.e.routines.sys(this.e,Math.floor(this.num(t)),e,s)}undef(){M("UNDEF'D STATEMENT")}readText(t){let e=this.items[this.datum??0];if(e||M("OUT OF DATA"),this.datum=(this.datum??0)+1,t)return e.text;let s=e.text.replace(/ /g,"");if(e.quoted||!/^[+-]?(\d+\.?\d*|\.\d*)?(E[+-]?\d*)?$/.test(s)){let f=new j("?SYNTAX ERROR");throw f.basicLine=e.line,f}return m0(s)}restore(){this.datum=0,this.data=this.dataStart}def(t,e,s){this.e.mem[58]===255&&M("ILLEGAL DIRECT"),(this.fns??=Object.create(null))[t]={p:e,f:s}}fn(t,e){let s=this.fns?.[t];s||M("UNDEF'D FUNCTION");let f=this.v[s.p],n=this.num(e);this.set(s.p,n);try{return s.f(this)}finally{f===void 0?delete this.v[s.p]:this.v[s.p]=f}}failWith(t){M(t)}statement(t,e,s,f){let n=this.e.routines["basic-"+t];if(!n)throw new Error(t.toUpperCase()+" is not handled");return n(this.e,e,s,f)}pos(){return this.e.mem[211]%40}for(t,e,s,f){let n=this.stack.findLastIndex(o=>o.for===t);n>=0&&(this.stack.length=n),this.stack.push({for:t,to:this.num(e),step:this.num(s),next:f})}next(t){let e=t?this.stack.findLastIndex(n=>n.for===t):this.stack.findLastIndex(n=>n.for!==void 0);(e<0||t===null&&this.stack.slice(e+1).some(n=>n.gosub!==void 0))&&M("NEXT WITHOUT FOR"),this.stack.length=e+1;let s=this.stack[e],f=this.get(s.for)+s.step;return this.set(s.for,f),(s.step>=0?f<=s.to:f>=s.to)?s.next:(this.stack.length=e,null)}gosub(t){this.stack.push({gosub:t})}ret(){let t=this.stack.findLastIndex(s=>s.gosub!==void 0);t<0&&M("RETURN WITHOUT GOSUB");let e=this.stack[t].gosub;return this.stack.length=t,e}on(t){let e=Math.floor(this.num(t));return(e<0||e>255)&&M("ILLEGAL QUANTITY"),e}read(t){if(this.items)return this.readText(t);let e=this.e.mem,s=e[this.data];s&128&&M("OUT OF DATA");let f=String.fromCharCode(...e.subarray(this.data+1,this.data+1+s));return this.data+=1+s,t?f:m0(f)}out(t){let e=this.e;e.a=t,this.call("chrout")}call(t){let e=this.e;this.quiet&&e.inside++;try{e.routines[t](e,[],null)}finally{this.quiet&&e.inside--}}print(t){if(typeof t=="number"){for(let e of K0(t))this.out(e.charCodeAt(0));this.out(this.e.mem[19]?32:29)}else for(let e=0;e<t.length;e++)this.out(t.charCodeAt(e))}newline(){this.out(13),this.e.mem[19]&128&&this.out(10)}open([t,e=1,s,f=""]){let n=this.e.mem;n[184]=Q(t)&255,n[186]=Q(e)&255,n[185]=s===void 0?n[186]<3?255:0:Q(s)&255;for(let o=0;o<f.length;o++)n[512+o]=f.charCodeAt(o);f.length?(Object.assign(this.e,{a:f.length,x:0,y:2}),this.call("setnam")):n[183]=0,this.call("open"),this.e.c&&M(["","TOO MANY FILES","FILE OPEN","FILE NOT OPEN"][this.e.a]??"DEVICE NOT PRESENT")}close(t){this.e.a=Q(t)&255,this.call("close")}output(t){this.e.mem[19]=Q(t)&255,this.e.x=this.e.mem[19],this.call("chkout")}input(t){this.e.mem[19]=Q(t)&255,this.e.x=this.e.mem[19],this.call("chkin"),this.line=null}clrchn(){this.call("clrchn"),this.e.mem[19]=0}wait(t,e,s=0){let f=this.e,n=f.mem;if(!n[198]&&f.keys.length){let o=f.keys.shift();o>=0&&(n[631]=o,n[198]=1)}return((this.peek(t)^Q(s))&Q(e))!==0}readLine(t){for(;;){let e="";for(;this.call("chrin"),this.e.a!==13;)e+=String.fromCharCode(this.e.a),e.length>88&&M("STRING TOO LONG");if(this.e.mem[144]&2)return this.givenUp=!0,"";if(e.length||!t)return e}}item(t,e){if(e&&(this.givenUp=!1),(e||this.line===null||this.linePos>=this.line.length)&&(this.line=this.readLine(e),this.linePos=0),this.givenUp)return;let s=this.line,f=this.linePos;for(;s[f]===" ";)f++;let n;if(t&&s[f]==='"'){let o=s.indexOf('"',f+1);for(n=s.slice(f+1,o<0?s.length:o),f=o<0?s.length:o+1;s[f]===" ";)f++}else if(t){let o=f;for(;o<s.length&&s[o]!==","&&s[o]!==":";)o++;n=s.slice(f,o),f=o}else{let o=$0(s,f);n=o.text,f=o.end}return f<s.length&&s[f]!==","&&s[f]!==":"&&M("FILE DATA"),s[f]===","&&f++,this.linePos=f,t?n:m0(n)}inputDone(){if(this.e.mem[19]){this.call("clrchn"),this.e.mem[19]=0;return}throw new Error("INPUT from the keyboard is not handled")}getFrom(){return this.call("getin"),this.e.a?String.fromCharCode(this.e.a):""}comma(){let e=10-this.e.mem[211]%40%10;for(;e--;)this.out(this.e.mem[19]?32:29)}spc(t){for(t=Q(t),(t<0||t>255)&&M("ILLEGAL QUANTITY");t-- >0;)this.out(this.e.mem[19]?32:29)}tab(t){t=Q(t),(t<0||t>255)&&M("ILLEGAL QUANTITY");let e=t-this.e.mem[211]%40;for(;e-- >0;)this.out(29)}key(){if(this.got===void 0&&(this.e.mem[19]=0,this.call("getin"),this.got=this.e.a,this.e.polled&&!this.quiet))return null;let t=this.got;return this.got=void 0,this.quiet||this.call("clrchn"),t?String.fromCharCode(t):""}};var Qt=x=>x.toString(16).toUpperCase().padStart(4,"0"),gt=97,je=[48185,48332,48216,784,45949,45982,49009,57495,47594,49133,57956,57963,58036,58126,47117,46972,46181,47021,46987,46828,46848,46892,46903];function kt({cpu:x,read:t,write:e,evaluate:s,chrout:f=n=>x.hooks.get(65490)(n)}){let n=()=>t(122)|t(123)<<8,o=i=>{e(122,i&255),e(123,i>>8&255)},c=()=>{throw new j("?SYNTAX ERROR")},r=i=>{let a=n();for(;t(a)===32;)a++;o(a);let b=t(a);i.a=b,i.z=b===0||b===58?1:0,i.c=b>=48&&b<=57?0:1,i.n=b>>7},h=i=>{r(i),i.a!==44&&c(),o(n()+1),r(i)},p=0,d=(i,a)=>{r(i);let b=n(),{value:k,end:T,place:P}=s(b);if(typeof k=="string"){if(a)throw new j("?TYPE MISMATCH ERROR");if(P===void 0)throw new Error("a string parameter not in the text, at $"+Qt(b)+": not handled");e(25,k.length),e(26,P&255),e(27,P>>8),e(100,25),e(101,0),e(13,255)}else p=k,e(13,0),q0({read:t,write:e},k);o(T),r(i)},l=(i,a)=>{if(p!==Math.floor(p)||p<i||p>a)throw new j("?ILLEGAL QUANTITY ERROR");return p},y=i=>{d(i,!0),i.x=l(0,255)},E=i=>{x.a=i,f(x)},u={121:r,115:i=>{o(n()+1),r(i)},44797:h,44426:i=>d(i,!0),44446:i=>d(i,!1),47095:i=>{let a=l(0,65535);e(20,a&255),e(21,a>>8),i.y=a&255,i.a=a>>8},47006:y,57856:i=>{h(i),y(i)},57862:i=>{if(r(i),i.z)return"return"},57870:i=>{h(i),i.z&&c()},43806:i=>{for(let a=i.a|i.y<<8,b=0;t(a+b)&&b<256;b++)E(t(a+b))},58692:()=>E(147),59903:i=>{let a=(t(648)<<8)+i.x*40;for(let b=0;b<40;b++)e(a+b,32),e(55296+i.x*40+b,t(646))}};u[45460]=i=>{let a=t(11)<<1,b=a>>8;a&=255,a+=5+b,b=a>>8,a&=255,a+=t(95)+b,b=a>>8,a&=255;let k=t(96);b&&(k=k+1&255),e(88,a),e(89,k),i.a=a,i.y=k,i.c=b;let T=b?k:a;i.z=T?0:1,i.n=T>>7};let O=(i,a)=>{let b=t(52);return a!==b?a<b:i<t(51)},I=(i,a,b)=>{let k=t(52),T=b!==k?b-k:a-t(51);i.c=T>=0?1:0,i.z=T&255?0:1,i.n=T>>7&1};u[41992]=i=>{let{a,y:b}=i;if(O(a,b))return I(i,a,b);let k=[];for(let T=87;T<=96;T++)k.push(t(T));if(u[46374](i),k.forEach((T,P)=>e(87+P,T)),Object.assign(i,{a,y:b}),O(a,b))return I(i,a,b);throw new j("?OUT OF MEMORY ERROR")},u[45900]=i=>{e(34,i.y);let a=t(95)|t(96)<<8,b=t(a+i.y)|t(a+(i.y-1&255))<<8;e(40,b&255),e(41,b>>8);let k=t(113)|t(114)<<8,T=k*b;if(T>65535)throw new j("?OUT OF MEMORY ERROR");e(113,0),e(114,0),e(93,0),Object.assign(i,{x:T&255,y:T>>8,a:T>>8,c:0,z:1,n:0})},u[45738]=i=>{let a=t(88)|t(89)<<8,b=i.a+t(89)+i.c;if(b>255)throw new j("?OUT OF MEMORY ERROR");e(89,b);let k=i.x+t(88);if(k>255&&(k&=255,b=b+1&255,!b))throw new j("?OUT OF MEMORY ERROR");let T={a:i.a,x:i.x};Object.assign(i,{a:k,y:b}),u[41992](i),e(49,k),e(50,b);let P=k|b<<8;for(let e0=a;e0<P;e0++)e(e0,0);e(89,a>>8),e(114,0);let z=t(95)|t(96)<<8,J=P-z;if(e(z+2,J&255),e(z+3,J>>8),!t(12))throw new Error("an array made by its first use ($B2AA, DIM's flag clear): not handled");let f0=t(12);Object.assign(i,{a:f0,x:T.x,y:3,c:1,z:f0?0:1,n:f0>>7})},u[57648]=i=>(i.push(225),i.push(70),Object.assign(i,{a:t(780),x:t(781),y:t(782)}),i.p=t(783),i.pc=t(20)|t(21)<<8,!1),u[57671]=i=>{e(780,i.a),e(781,i.x),e(782,i.y),e(783,i.p)};let A=new l0({read:t,write:e}),D=(i,a,b)=>{let k=b+2;i.push(k>>8),i.push(k&255);try{return x.hooks.get(a)(i)}finally{i.pull(),i.pull()}},V=i=>{try{return i()}catch(a){throw a instanceof b0?new j("?"+p0[a.x-1]+" ERROR"):a}};u[44600]=i=>{let a=i.a,b=i.pull(),k=i.pull();return e(34,b+1&255),e(35,k),i.y=a,i.push(a),u[44611](i)},u[44611]=i=>{A.round();for(let a of[101,100,99,98,97])i.push(t(a));return i.a=t(97),i.z=i.a?0:1,i.n=i.a>>7,i.pc=t(34)|t(35)<<8,!1};let N=i=>i.a|i.y<<8,K=(i,a)=>{i.z=a&255?0:1,i.n=a>>7&1},G=(i,a=()=>({}))=>b=>{let k=V(()=>i(b));Object.assign(b,a(b,k)??{}),K(b,b.a)},U=(i,a)=>({a:a?.a??t(97)});Object.assign(u,{48034:G(i=>A.movfm(N(i)),(i,a)=>({a:a.a,y:0})),48084:G(i=>A.movmf(i.x|i.y<<8),(i,a)=>({a:a.a,y:0})),48080:G(i=>(i.x=t(73),i.y=t(74),A.movmf(i.x|i.y<<8)),(i,a)=>({a:a.a,y:0})),48071:G(i=>(i.x=92,A.movmf(92)),(i,a)=>({a:a.a,y:0})),48074:G(i=>(i.x=87,A.movmf(87)),(i,a)=>({a:a.a,y:0})),48124:G(()=>A.movfa(),()=>({a:t(105),x:0})),48140:G(()=>A.movaf(),()=>({a:t(97),x:0})),48143:G(()=>A.movef(),()=>({a:t(97),x:0})),48155:G(()=>A.round(),()=>({a:t(97)})),48171:G(()=>({a:A.sign()}),(i,a)=>({a:a.a,c:t(102)>>7})),48219:G(i=>A.compare(N(i)),(i,a)=>a),48221:G(i=>A.compare(t(36)|i.y<<8),(i,a)=>a),47207:G(i=>A.fadd(N(i)),U),47210:G(()=>A.faddt(),U),47184:G(i=>A.fsub(N(i)),U),47187:G(()=>A.fsubt(),U),47656:G(i=>A.fmult(N(i)),U),47659:G(()=>A.fmultt(),U),47887:G(i=>A.fdiv(N(i)),U),47890:G(()=>A.fdivt(),U),48332:G(()=>A.int(),U),48283:G(()=>A.qint(),()=>({y:0})),48347:G(i=>A.float32(i.a),U),47594:G(()=>A.log(),U),49133:G(i=>A.exp(i.c),U),49019:G(i=>A.fpwrt(i.z,i.c),U),49009:G(i=>(A.movaf(),A.movfm(48913),A.fpwrt(0,i.c)),U),57963:G(()=>A.sin(),U),57956:G(()=>A.cos(),U)});let R=null;u[57495]=i=>{R=M0(R??U0(17)),q0({read:t,write:e},R/2**32),i.a=t(97),K(i,i.a)},u[48900]=i=>{e(255+i.y,i.a),e(256+i.y,0),Object.assign(i,{a:0,y:1,z:0,n:0})},u[48371]=i=>{for(let P=10;P>=0;P--)e(93+P,0);let a=()=>u[115](i),b=()=>!i.c;b()||(i.a===45?(e(103,255),a()):i.a===43&&a());let k;for(;;){if(b()){let P=i.a;t(95)&128&&e(93,t(93)+1),V(()=>{A.mul10(),A.addDigit(P-48)}),a();continue}if(i.a===46){if(e(95,t(95)>>1|128),t(95)&64){k=t(94);break}a();continue}if(i.a===69){for(a(),i.c&&(i.a===171||i.a===45)?(e(96,t(96)>>1|128),a()):i.c&&(i.a===170||i.a===43)&&a();b();){let P=t(94);if(P>=10){if(!(t(96)&128))throw new j("?OVERFLOW ERROR");e(94,100)}else e(94,P*10+i.a-48);a()}k=t(96)&128?0-t(94)&255:t(94);break}k=t(94);break}let T=k-t(93)&255;if(e(94,T),T&&T<128)for(;T;T=T-1&255)V(()=>A.mul10()),e(94,T-1);else if(T)for(;T;T=T+1&255)V(()=>A.div10()),e(94,T+1&255);t(103)&128&&A.negop(),i.a=t(t(103)&128?102:103),K(i,i.a)};let C=i=>{D(i,65502,44932),e(100,i.x),e(99,i.y),e(101,i.a),e(98,0),i.y=0};u[44923]=i=>{let a=i.c;C(i),e(gt,160),e(112,0),e(102,0),a||A.negate();let b=A.normal();Object.assign(i,{a:b.a,y:0}),K(i,i.a)},u[47164]=i=>(e(74,i.x),i.y=0,i.pc=47168,!1),u[47168]=i=>{let a=(t((t(20)|t(21)<<8)+i.y)^t(74))&t(73);if(!a)return"wait";i.a=a,K(i,a)},u[46758]=i=>{let a=t(100)|t(101)<<8;e(34,a&255),e(35,a>>8);let b=u[46811]({a:a&255,y:a>>8}),k=t(a),T=t(a+1),P=t(a+2);if(b&&P===t(52)&&T===t(51)){let z=t(51)+k;e(51,z&255),z>255&&e(52,t(52)+1)}e(34,T),e(35,P),Object.assign(i,{a:k,x:T,y:P}),K(i,k)},u[46811]=i=>i.y!==t(24)||i.a!==t(23)?(i.z=0,!1):(e(22,i.a),e(23,i.a-3&255),i.y=0,i.z=1,!0),u[45013]=i=>{let a=je[i.a-104>>1];if(a===void 0||i.a&1)throw new Error("a function at $"+Qt(40938+i.a)+" ($AFD5): not handled");return e(85,a&255),e(86,a>>8),i.y=i.a,i.push(175),i.push(226),i.pc=a,!1},u[45027]=i=>{let a=t(13);if(a&128)throw new j("?TYPE MISMATCH ERROR");i.n=1&a>>7,i.v=1&a>>6,i.z=i.a&a?0:1},u[47024]=i=>{if(!i.a){e(gt,0),e(102,0),i.a=0,i.z=1,i.n=0;return}let a=t(34)|t(35)<<8,b=a+i.a;e(113,t(122)),e(114,t(123)),o(a),e(36,b&255),e(37,b>>8&255);let k=t(b);e(b,0),u[121](i),u[48371](i),e(b,k),Object.assign(i,{a:k,x:t(113),y:t(114)}),K(i,i.y),o(i.x|i.y<<8)},u[43488]=i=>{if(u[46758](i),i.a!==6)throw new j("?ILLEGAL QUANTITY ERROR");e(gt,0),e(102,0);let a=b=>{if(i.a=t((t(34)|t(35)<<8)+b),i.a===32?u[115](i):i.c=i.a>=48?0:1,i.c)throw new j("?ILLEGAL QUANTITY ERROR");V(()=>A.addDigit(i.a-48&255))};for(let b=0;b<6;b+=2){e(113,b),a(b),V(()=>A.mul10()),e(113,b+1),a(b+1);let{a:k}=A.movaf();k&&V(()=>A.addDoubled(k+1))}return V(()=>A.mul10()),A.qint(),Object.assign(i,{x:t(100),y:t(99),a:t(101)}),x.hooks.get(65499)(i)};let L=i=>{let a=128,b=i.y,k=0;for(;;){do{let e0=0;k=0;for(let A0=3;A0>=0;A0--)e0=t(98+A0)+t(48918+b+A0)+k,e(98+A0,e0),k=e0>>8;a=a+1&255}while(k?a&128:!(a&128));let J=a;if(k){let e0=(J^255)+10+1;J=e0&255,k=e0>>8}J=J+47+k&255,b=b+4&255,e(71,b);let f0=t(113)+1&255;if(e(255+f0,J&127),e(93,t(93)-1),t(93)||(f0=f0+1&255,e(255+f0,46)),e(113,f0),a=(J^255)&128,b===36||b===60)break}let T=t(113),P;do P=t(255+T),T=T-1&255;while(P===48);P!==46&&(T=T+1&255);let z=t(94);if(z){let J=43;z&128&&(z=0-z&255,J=45),e(257+T,J),e(256+T,69),e(259+T,48+z%10),e(258+T,48+Math.floor(z/10)),e(260+T,0)}else e(256+T,0);Object.assign(i,{a:0,y:1,z:0,n:0})};u[48744]=L,u[46324]=i=>{let a=i.a;for(e(15,t(15)>>1);;){let b=S(51)-a;if(b>=S(49)&&b>=0){$(51,b),$(53,b),Object.assign(i,{a,x:b&255,y:b>>8});return}if(t(15)&128)throw new j("?OUT OF MEMORY ERROR");u[46374](i),e(15,128)}},u[46205]=i=>{u[46324](i),e(98,i.x),e(99,i.y),e(97,i.a)},u[46197]=i=>(e(80,t(100)),e(81,t(101)),u[46205](i)),u[46728]=i=>{e(34,i.x),e(35,i.y);let a=i.x|i.y<<8,b=S(53),k=i.a;for(let T=k-1;T>=0;T--)e(b+T,t(a+T));$(53,b+k)},u[46282]=i=>{let a=t(22);if(a===34)throw new j("?FORMULA TOO COMPLEX ERROR");e(a,t(97)),e(a+1,t(98)),e(a+2,t(99)),e(100,a),e(101,0),e(112,0),e(13,255),e(23,a),e(22,a+3),Object.assign(i,{x:a+3,y:255})},u[46215]=i=>{e(7,34),e(8,34);let a=i.a|i.y<<8;e(111,i.a),e(112,i.y),e(98,i.a),e(99,i.y);let b=0,k=0;for(;;b++){let P=t(a+b);if(!P)break;if(P===34){k=1;break}}e(97,b);let T=a+b+k;return e(113,T&255),e(114,T>>8&255),(i.y===0||i.y===2)&&(i.a=b,u[46197](i),Object.assign(i,{a:b,x:a&255,y:a>>8}),u[46728](i)),u[46282](i)},u[46191]=i=>(Object.assign(i,{a:255,y:0}),u[46215](i)),u[44872]=i=>(C(i),e(94,0),e(113,255),e(93,6),i.y=36,L(i),u[46191](i)),u[45640]=()=>{throw new j("?ILLEGAL QUANTITY ERROR")},u[46755]=i=>{if(!(t(13)&128))throw new j("?TYPE MISMATCH ERROR");return u[46758](i)},u[57946]=i=>(u[46755](i),i.x=t(34),i.y=t(35),x.hooks.get(65469)(i));let S=i=>t(i)|t(i+1)<<8,$=(i,a)=>{e(i,a&255),e(i+1,a>>8&255)},Y=i=>{let a=S(34);if(t(a+i)){let b=t(a+i+1),k=t(a+i+2),T=k<t(52)||k===t(52)&&b<t(51),P=k>t(96)||k===t(96)&&b>=t(95);T&&P&&(e(95,b),e(96,k),$(78,a),e(85,t(83)))}$(34,a+t(83))},W=()=>{let i=S(90)-S(95)&65535,a=i&255,b=(i>>8)+1&255;e(34,a);let k=T=>e(S(88)+T,t(S(90)+T));a&&($(90,S(90)-a),$(88,S(88)-a));for(let T=!0;;T=!1){if(!T||a){do a=a-1&255,a&&k(a);while(a);k(0)}if(e(91,t(91)-1&255),e(89,t(89)-1&255),b=b-1&255,!b)break}};u[46374]=i=>{let a=S(55);for(;;){for($(51,a),$(78,0),$(95,S(49)),$(34,25);t(34)!==t(22);)Y(0);for(e(83,7),$(34,S(45));S(34)!==S(47);){let k=S(34);!(t(k)&128)&&t(k+1)&128?Y(2):$(34,k+7)}for($(88,S(47)),e(83,3);S(88)!==S(49);){let k=S(88);if($(34,k),$(88,k+S(k+2)+(k>=S(49)?1:0)),t(k)&128||!(t(k+1)&128))continue;let T=(t(k+4)<<1&255)+5+(t(k+4)>>7);for($(34,k+(T&255)+(T>>8));S(34)!==S(88);)Y(0)}if(!S(78)){Object.assign(i,{a:0,x:t(35),y:0,z:1,n:0});return}let b=(t(85)&4)>>1;e(85,b),$(90,S(95)+t(S(78)+b)),$(88,S(51)),W(),e(S(78)+b+1,t(88)),e(89,t(89)+1&255),e(S(78)+b+2,t(89)),a=S(88)}};for(let[i,a]of Object.entries(u))x.hooks.set(+i,a)}function Z0({cpu:x,write:t},e,s){let f=/^\?(.*) ERROR$/.exec(e)?.[1];if(!p0.includes(f))return!1;let n=c=>{x.a=c,x.hooks.get(65490)(x)},o=c=>{for(let r of c)n(r.charCodeAt(0))};return x.hooks.get(65484)(x),t(19,0),n(13),n(63),o(f),o(" ERROR"),s!==void 0&&s>>8!==255&&(o(" IN "),o(String(s))),o(`\r
|
|
7
7
|
READY.\r
|
|
8
|
-
`),t(157,128),!0}function yt({cpu:x,read:t,write:e,ram:s}){let f=new l0({read:t,write:e}),n=new Map,o=r=>{let h=r.endsWith("%")?"%":r.endsWith("$")?"$":"",p=h?r.slice(0,-1):r,d=p.charCodeAt(0)|(h==="%"?128:0),l=(p.charCodeAt(1)||0)|(h?128:0),y=t(47)|t(48)<<8;for(let E=t(45)|t(46)<<8;E<y;E+=7){if(t(E)!==d||t(E+1)!==l)continue;let u=[2,3,4,5,6].map(O=>t(E+O));if(h==="%")return(u[0]<<8|u[1])<<16>>16;if(h==="$"){let O="",I=u[1]|u[2]<<8;for(let A=0;A<u[0];A++)O+=String.fromCharCode(t(I+A));return O}return nt(u)}throw new Error(`the variable ${r} not in BASIC's table: its making by PTRGET not handled`)},c=Object.assign(Object.create(k0.prototype),{get:o,rnd:()=>(x.hooks.get(57495)(x),f.fac())});return r=>{let{e:h,end:p}=S0(s,r),d=n.get(r);return d||n.set(r,d=Y0(h)),{value:d(c),end:p,place:h.k==="str"?h.at:void 0}}}var te={};X(te,{keyboard:()=>ze,pace:()=>qe,painter:()=>We});var _t={};X(_t,{CURSOR:()=>tt,JOYSTICK:()=>F0,MATRIX:()=>I0,MODIFIERS:()=>c0,SHIFTED:()=>At});var I0={Backspace:[0,0],Delete:[0,0],Enter:[0,1],F7:[0,3],F1:[0,4],F3:[0,5],F5:[0,6],3:[1,0],w:[1,1],a:[1,2],4:[1,3],z:[1,4],s:[1,5],e:[1,6],5:[2,0],r:[2,1],d:[2,2],6:[2,3],c:[2,4],f:[2,5],t:[2,6],x:[2,7],7:[3,0],y:[3,1],g:[3,2],8:[3,3],b:[3,4],h:[3,5],u:[3,6],v:[3,7],9:[4,0],i:[4,1],j:[4,2],0:[4,3],m:[4,4],k:[4,5],o:[4,6],n:[4,7],"+":[5,0],p:[5,1],l:[5,2],"-":[5,3],".":[5,4],":":[5,5],"@":[5,6],",":[5,7],"\xA3":[6,0],"*":[6,1],";":[6,2],Home:[6,3],"=":[6,5],"^":[6,6],"/":[6,7],1:[7,0],_:[7,1],2:[7,3]," ":[7,4],q:[7,6],Escape:[7,7]},At={"!":"1",'"':"2","#":"3",$:"4","%":"5","&":"6","'":"7","(":"8",")":"9","?":"/","<":",",">":".","[":":","]":";",F2:"F1",F4:"F3",F6:"F5",F8:"F7"},c0={Control:[7,5],Tab:[7,2]},F0={ArrowUp:1,ArrowDown:2,ArrowLeft:4,ArrowRight:8,Control:16,Shift:16,Alt:16},tt={ArrowDown:[0,7,0],ArrowRight:[0,2,0],ArrowUp:[0,7,1],ArrowLeft:[0,2,1]};var Zt={};X(Zt,{HEIGHT:()=>E0,LEFT:()=>d0,PALETTE:()=>V0,TOP:()=>wt,VIC:()=>O0,WIDTH:()=>_});var V0=[[0,0,0],[255,255,255],[136,0,0],[170,255,238],[204,68,204],[0,204,85],[0,0,170],[238,238,119],[221,136,85],[102,68,0],[255,119,119],[51,51,51],[119,119,119],[170,255,102],[0,136,255],[187,187,187]],_=384,E0=272,d0=32,wt=35,Jt=16,He=287,O0=class{constructor(t){this.m=t,this.r=new Uint8Array(64),this.r[17]=27,this.r[22]=200,this.r[24]=20,this.frame=new Uint8Array(_*E0),this.rasterIrq=!1,this.spriteHits=0,this.spriteBackground=0,this.inside=[0,_-1],this.priority=new Uint8Array(_)}read(t){if(t===17)return this.r[17]&127|this.m.line>>1&128;if(t===18)return this.m.line&255;if(t===25)return this.r[25]|112;if(t===30){let e=this.spriteHits;return this.spriteHits=0,e}if(t===31){let e=this.spriteBackground;return this.spriteBackground=0,e}return t>=32&&t<47?this.r[t]|240:this.r[t]}write(t,e){if(t===25){this.r[25]&=~(e&15);return}this.r[t]=e}irq(){let t=this.m.line,e=this.r[18]|(this.r[17]&128)<<1;return t===e&&!this.rasterIrq&&(this.rasterIrq=!0,this.r[25]|=1),t!==e&&(this.rasterIrq=!1),this.r[25]&this.r[26]&15?(this.r[25]|=128,!0):!1}bank(){return(~this.m.cia2.pra&3)<<14}fetch(t){let e=this.bank(),s=e|t&16383;return(s&28672)===4096&&!(e&16384)?this.m.chargen[s&4095]:this.m.ram[s]}line(t){if(t<Jt||t>He)return;let e=t-Jt,s=this.frame,f=e*_,n=this.r,o=n[32]&15,c=n[33]&15,r=n[17]&16,h=n[17]&8,p=n[22]&8,d=h?51:55,l=h?250:246,y=p?24:31,E=p?343:334,u=this.priority;u.fill(0);for(let L=0;L<_;L++)s[f+L]=o;if(!r||t<d||t>l){this.sprites(t,f,!0);return}let O=n[17]&7,I=n[22]&7,A=n[17]&32,D=n[17]&64,V=n[22]&16,N=(n[24]>>4&15)<<10,K=(n[24]>>1&7)<<11,G=(n[24]&8)<<10,U=t-48-O;if(U<0||U>=200){this.sprites(t,f,!1);return}let R=U>>3,C=U&7;for(let L=0;L<40;L++){let S=R*40+L,$=this.fetch(N+S),Y=this.m.color[S]&15,W,i;A?(W=this.fetch(G+S*8+C),i=V?[c,$>>4,$&15,Y]:[$&15,$>>4]):(W=this.fetch(K+((D?$&63:$)<<3)+C),D?i=[n[33+($>>6)]&15,Y]:V&&Y&8?i=[c,n[34]&15,n[35]&15,Y&7]:i=[c,Y]);let a=i.length===4&&!0,b=d0+I+L*8;if(a&&(A||V&&Y&8))for(let k=0;k<4;k++){let T=W>>6-k*2&3,P=i[T],z=b+k*2;z>=0&&z+1<_&&(s[f+z]=P,s[f+z+1]=P,T>=2&&(u[z]=1,u[z+1]=1))}else for(let k=0;k<8;k++){let T=W>>7-k&1,P=b+k;P>=0&&P<_&&(s[f+P]=T?i[1]:i[0],T&&(u[P]=1))}}for(let L=0;L<d0+y-24;L++)s[f+L]=o;for(let L=d0+E-24+1;L<_;L++)s[f+L]=o;this.inside=[d0+y-24,d0+E-24],this.sprites(t,f,!1)}sprites(t,e,s){let f=this.r,n=f[21];if(!n)return;let o=(f[24]>>4&15)<<10,c=this.frame,r=this.priority,h=new Uint8Array(_);for(let p=7;p>=0;p--){if(!(n&1<<p))continue;let d=f[p*2]|(f[16]>>p&1)<<8,l=f[p*2+1],y=f[23]&1<<p,E=f[29]&1<<p,u=y?42:21,O=t-l-1;if(O<0||O>=u)continue;let I=y?O>>1:O,A=this.fetch(o+1016+p),D=A<<6,V=f[28]&1<<p,N=f[27]&1<<p,K=f[39+p]&15,G=d0+d-24;for(let U=0;U<3;U++){let R=this.fetch(D+I*3+U);if(V)for(let C=0;C<4;C++){let L=R>>6-C*2&3;if(!L)continue;let S=L===1?f[37]&15:L===2?K:f[38]&15,$=(U*8+C*2)*(E?2:1);for(let Y=0;Y<(E?4:2);Y++)this.spritePixel(e,G+$+Y,S,p,N,h,s)}else for(let C=0;C<8;C++){if(!(R>>7-C&1))continue;let L=(U*8+C)*(E?2:1);for(let S=0;S<(E?2:1);S++)this.spritePixel(e,G+L+S,K,p,N,h,s)}}}}spritePixel(t,e,s,f,n,o,c){e<0||e>=_||(o[e]&&(this.spriteHits|=1<<f|1<<o[e]-1),o[e]=f+1,!(this.priority[e]&&(this.spriteBackground|=1<<f,n))&&(c||e<this.inside[0]||e>this.inside[1]||(this.frame[t+e]=s)))}};var et=.4;function ze(x,{ignore:t=()=>!1,onKey:e=()=>{},log:s=!1}={}){let f=()=>!((x.vic?.r[24]??21)&2),n=new Map,o=[],c=new Map,r=new Set,h=0,p=(u,O)=>{let I=h-(c.get(u)??-9);if(I>=2){x.key(u[0],u[1],!1),O&&x.key(1,7,!1);return}o.push([u[0],u[1],2-I]),O&&o.push([1,7,2-I])};window.addEventListener("keydown",u=>{if(t(u)||(e(u),u.metaKey||u.repeat))return;if(c0[u.key]&&(x.key(...c0[u.key],!0),u.preventDefault()),F0[u.key]){x.joystick|=F0[u.key];let K=tt[u.key];K&&(K[2]&&x.key(1,7,!0),x.key(K[0],K[1],!0)),u.preventDefault();return}let O=/^Key([A-Z])$/.exec(u.code)?.[1]?.toLowerCase(),I=O?null:At[u.key],A=O?I0[O]:I?I0[I]:I0[u.key]||I0[u.key.toLowerCase()];if(!A){c0[u.key]&&u.preventDefault();return}(O?f()?u.altKey:u.shiftKey:I||u.key.length===1&&u.key!==u.key.toLowerCase()&&!/[0-9]/.test(u.key)||u.shiftKey&&/^F[1357]$/.test(u.key))&&x.key(1,7,!0);let[D,V]=c0.Control,N=u.ctrlKey&&!(x.keys[D]&1<<V)?c0.Control:null;N&&(x.key(...N,!0),c.set(N,h)),x.key(A[0],A[1],!0),n.set(u.code,A),c.set(A,h),N&&r.add(u.code),s&&console.log("key",JSON.stringify(u.key),"-> column",A[0],"row",A[1]),u.preventDefault()}),window.addEventListener("keyup",u=>{if(c0[u.key]&&x.key(...c0[u.key],!1),F0[u.key]){x.joystick&=~F0[u.key];let I=tt[u.key];I&&(x.key(I[0],I[1],!1),I[2]&&x.key(1,7,!1));return}let O=n.get(u.code);O?(p(O,!0),r.delete(u.code)&&p(c0.Control,!1),n.delete(u.code)):x.key(1,7,!1)});let d=()=>{x.keys.fill(0),x.joystick=0,n.clear(),o.length=0,c.clear(),r.clear(),l=0,y=!1},l=0,y=!1,E=()=>{if(typeof navigator>"u"||typeof navigator.getGamepads!="function")return;let u=0,O=!1;for(let I of navigator.getGamepads()){if(!I||!I.connected)continue;let A=N=>!!I.buttons[N]?.pressed,D=I.axes[0]??0,V=I.axes[1]??0;(A(12)||V<-et)&&(u|=1),(A(13)||V>et)&&(u|=2),(A(14)||D<-et)&&(u|=4),(A(15)||D>et)&&(u|=8),(A(0)||A(1))&&(u|=16),A(9)&&(O=!0)}x.joystick=x.joystick&~l|u,l=u,O!==y&&(x.key(7,7,O),y=O)};return window.addEventListener("blur",d),document.addEventListener("visibilitychange",()=>{document.hidden&&d()}),{press(u,O,I=3){x.key(u,O,!0),o.push([u,O,I])},frame(){h++,E();for(let u=o.length-1;u>=0;u--)--o[u][2]<=0&&(x.key(o[u][0],o[u][1],!1),o.splice(u,1))}}}function We(x){let t=x.getContext("2d");x.width=_*2,x.height=E0*2;let e=new Uint8ClampedArray(_*2*E0*2*4),s=new ImageData(e,_*2,E0*2);return f=>{for(let n=0;n<E0;n++)for(let o=0;o<_;o++){let[c,r,h]=V0[f[n*_+o]];for(let p=0;p<2;p++){let d=((n*2+p)*_*2+o*2)*4;for(let l=0;l<2;l++)e[d++]=c,e[d++]=r,e[d++]=h,e[d++]=255}}t.putImageData(s,0,0)}}function qe(x,t,e=()=>{}){let s=0,f=performance.now();function n(){let o=Math.min(2,Math.floor((performance.now()-f)/20)-s);for(let c=0;c<o;c++)x.runFrame(),s++,e(s);t(s),requestAnimationFrame(n)}n()}var xe={};X(xe,{SECTORS:()=>B0,add:()=>Rt,make:()=>Ke,petsciiName:()=>Et,read:()=>Xe});var B0=[...Array(17).fill(21),...Array(7).fill(19),...Array(6).fill(18),...Array(5).fill(17)],ee={DEL:0,SEQ:1,PRG:2,USR:3,REL:4},h0=(x,t)=>(B0.slice(0,x-1).reduce((e,s)=>e+s,0)+t)*256,Ot=x=>{let t=new Uint8Array(16).fill(160);return[...String(x).toUpperCase()].slice(0,16).forEach((e,s)=>{t[s]=e.charCodeAt(0)&255}),t};function Ke({name:x="MOONBASE",id:t="MB",files:e=[]}={}){let s=new Uint8Array(174848),f=B0.map(d=>new Array(d).fill(!1)),n=(d,l)=>(f[d-1][l]=!0,h0(d,l));n(18,0);let o=[];for(let d=1;d<=17;d++)18-d>=1&&o.push(18-d),18+d<=35&&o.push(18+d);let c=()=>{for(let d of o){let l=B0[d-1],y=f[d-1].lastTaken??-10;for(let E=0;E<l;E++){let u=(y+10+E)%l;if(!f[d-1][u])return f[d-1][u]=!0,f[d-1].lastTaken=u,[d,u]}}throw new Error("the disk is full")},r=e.map(d=>{let l=d.data instanceof Uint8Array?d.data:Uint8Array.from(d.data),y=Math.max(1,Math.ceil(l.length/254)),E=Array.from({length:y},c);return E.forEach(([u,O],I)=>{let A=n(u,O),D=l.subarray(I*254,(I+1)*254);I+1<y?(s[A]=E[I+1][0],s[A+1]=E[I+1][1]):(s[A]=0,s[A+1]=D.length+1),s.set(D,A+2)}),{name:Ot(d.name),type:128|(ee[d.type??"PRG"]??2),first:E[0],blocks:y}}),h=[];for(let d=0,l=1;d<Math.max(1,Math.ceil(r.length/8));d++,l=(l+3)%19){for(;f[17][l];)l=(l+1)%19;if(h.length>=18)throw new Error("the directory is full: 144 files");n(18,l),h.push(l)}h.forEach((d,l)=>{let y=h0(18,d);l+1<h.length?(s[y]=18,s[y+1]=h[l+1]):(s[y]=0,s[y+1]=255),r.slice(l*8,l*8+8).forEach((E,u)=>{let O=y+u*32;s[O+2]=E.type,s[O+3]=E.first[0],s[O+4]=E.first[1],s.set(E.name,O+5),s[O+30]=E.blocks&255,s[O+31]=E.blocks>>8})});let p=h0(18,0);s[p]=18,s[p+1]=h[0],s[p+2]=65;for(let d=1;d<=35;d++){let l=f[d-1].map(E=>!E),y=p+4*d;s[y]=l.filter(Boolean).length;for(let E=0;E<l.length;E++)l[E]&&(s[y+1+(E>>3)]|=1<<(E&7))}return s.set(Ot(x),p+144),s[p+160]=s[p+161]=160,s[p+162]=String(t).charCodeAt(0)||48,s[p+163]=String(t).charCodeAt(1)||48,s[p+164]=160,s[p+165]=50,s[p+166]=65,s.fill(160,p+167,p+171),s}function Rt(x,t){let e=Uint8Array.from(x),s=h0(18,0),f=(p,d)=>!!(e[s+4*p+1+(d>>3)]&1<<(d&7)),n=(p,d,l)=>{let y=s+4*p+1+(d>>3),E=f(p,d);l?e[y]|=1<<(d&7):e[y]&=~(1<<(d&7)),e[s+4*p]+=l&&!E?1:!l&&E?-1:0},o=[];for(let p=1;p<=17;p++)18-p>=1&&o.push(18-p),18+p<=35&&o.push(18+p);let c={},r=()=>{for(let p of o){let d=B0[p-1];for(let l=0;l<d;l++){let y=((c[p]??-10)+10+l)%d;if(f(p,y))return n(p,y,!1),c[p]=y,[p,y]}}throw new Error("the disk is full")},h=()=>{let p=[];for(let[d,l]=[e[s],e[s+1]],y=0;d&&y<18;y++){let E=h0(d,l);for(let u=0;u<8;u++)p.push(E+u*32);[d,l]=[e[E],e[E+1]]}return p};for(let p of t){let d=Ot(p.name),l=p.data instanceof Uint8Array?p.data:Uint8Array.from(p.data);for(let O of h())if(!(!(e[O+2]&7)||!d.every((I,A)=>e[O+5+A]===I))){for(let[I,A]=[e[O+3],e[O+4]],D=0;I&&D<683;D++){let V=h0(I,A);n(I,A,!0),[I,A]=[e[V],e[V+1]]}e[O+2]=0}let y=Math.max(1,Math.ceil(l.length/254)),E=Array.from({length:y},r);E.forEach(([O,I],A)=>{let D=h0(O,I),V=l.subarray(A*254,(A+1)*254);e.fill(0,D,D+256),A+1<y?(e[D]=E[A+1][0],e[D+1]=E[A+1][1]):(e[D]=0,e[D+1]=V.length+1),e.set(V,D+2)});let u=h().find(O=>!(e[O+2]&7)&&!e[O+2]);if(u===void 0){let O=h(),I=O[O.length-1]-224,A=1;for(;A<19&&!f(18,A);)A++;if(A>=19)throw new Error("the directory is full");n(18,A,!1);let D=h0(18,A);e.fill(0,D,D+256),e[D+1]=255,e[I]=18,e[I+1]=A,u=D}e.fill(0,u+2,u+32),e[u+2]=128|(ee[p.type??"PRG"]??2),e[u+3]=E[0][0],e[u+4]=E[0][1],e.set(d,u+5),e[u+30]=y&255,e[u+31]=y>>8}return e}var Qe=["DEL","SEQ","PRG","USR","REL"],Et=x=>String.fromCharCode(...[...x].filter(t=>t!==160).map(t=>t>=193&&t<=218?t-128:t)).trim();function Xe(x){if(x.length!==174848&&x.length!==175531)throw new Error("a .d64 is 174,848 bytes, and this one is "+x.length);let t=(c,r)=>x.subarray(h0(c,r),h0(c,r)+256),e=(c,r)=>{let h=[];for(;c;){let d=t(c,r);[c,r]=[d[0],d[1]],h.push(d.subarray(2,c?256:r+1))}let p=new Uint8Array(h.reduce((d,l)=>d+l.length,0));return h.reduce((d,l)=>(p.set(l,d),d+l.length),0),p},s=t(18,0),f=Et(s.subarray(144,160)),n=String.fromCharCode(s[162],s[163]),o=[];for(let[c,r]=[s[0],s[1]];c;){let h=t(c,r);for(let p=0;p<8;p++){let d=h.subarray(p*32,(p+1)*32);if(!d[2])continue;let l=Qe[d[2]&7]||"?",y=e(d[3],d[4]),E={name:Et(d.subarray(5,21)),type:l,locked:!!(d[2]&64),closed:!!(d[2]&128),blocks:d[30]|d[31]<<8,body:y};l==="PRG"&&y.length>=2&&(E.address=y[0]|y[1]<<8,E.body=y.subarray(2),E.raw=y),o.push(E)}[c,r]=[h[0],h[1]]}return{name:f,id:n,files:o}}var ie={};X(ie,{Drive:()=>j0});var Tt=[...Array(17).fill(21),...Array(7).fill(19),...Array(6).fill(18),...Array(5).fill(17)],se=["DEL","SEQ","PRG","USR","REL"],y0="00, OK,00,00\r",_e="62,FILE NOT FOUND,00,00\r",Je="31,SYNTAX ERROR,00,00\r",s0=x=>Uint8Array.from(x,t=>t.charCodeAt(0)),Ct=x=>x>=193&&x<=218?x-128:x,j0=class x{constructor(t){this.image=t,this.mem=new Uint8Array(2048),this.saved=new Map,this.keep=null,this.channels=new Map,this.listening=!1,this.talking=!1,this.channel=0,this.opening=null,this.status=s0(y0),this.reply=null,this.pos=0,this.channels.set(15,{command:[]})}sector(t,e,s=this.image){let f=Tt.slice(0,t-1).reduce((n,o)=>n+o,0)+e;return s.subarray(f*256,(f+1)*256)}chain(t,e){let s=[],f=0;for(;t&&f++<800;){let c=this.sector(t,e);[t,e]=[c[0],c[1]],s.push(c.subarray(2,t?256:e+1))}let n=new Uint8Array(s.reduce((c,r)=>c+r.length,0)),o=0;for(let c of s)n.set(c,o),o+=c.length;return n}directory(){let t=this.sector(18,0),e=[];for(let[s,f]=[t[0],t[1]],n=0;s&&n++<40;){let o=this.sector(s,f);for(let c=0;c<8;c++){let r=o.subarray(c*32,(c+1)*32);r[2]&&e.push({name:Array.from(r.subarray(5,21)).filter(h=>h!==160).map(Ct),type:se[r[2]&7]||"?",track:r[3],sector:r[4]})}[s,f]=[o[0],o[1]]}return e}loadable(){return this.directory().filter(t=>t.type!=="DEL")}static matches(t,e){for(let s=0;s<t.length;s++){if(t[s]===42)return!0;if(s>=e.length||t[s]!==63&&Ct(t[s])!==e[s])return!1}return t.length===e.length}static strip(t){let e=Array.from(t).map(Ct);return e[0]===64&&(e=e.slice(1)),e[1]===58&&(e=e.slice(2)),e}find(t){t=x.strip(t);for(let[e,s]of this.saved)if(x.matches(t,Array.from(s0(e))))return{raw:s,saved:!0};for(let e of this.loadable())if(x.matches(t,e.name))return{...e,raw:this.chain(e.track,e.sector)}}listing(){let t=this.saved.size?Rt(this.image,[...this.saved].map(([c,r])=>({name:c,data:r}))):this.image,e=(c,r)=>this.sector(c,r,t),s=e(18,0),f=[1,4],n=(c,r)=>f.push(1,1,c&255,c>>8,...r,0);n(0,[18,34,...s.subarray(144,160),34,32,s[162],s[163],32,s[165],s[166]]);for(let[c,r]=[s[0],s[1]],h=0;c&&h++<40;){let p=e(c,r);for(let d=0;d<8;d++){let l=p.subarray(d*32,(d+1)*32);if(!l[2])continue;let y=l[30]|l[31]<<8,E=Array.from(l.subarray(5,21)),u=E.indexOf(160),O=u<0?E:E.slice(0,u),I=y<10?3:y<100?2:1;n(y,[...Array(I).fill(32),34,...O,34,...Array(16-O.length).fill(32),l[2]&128?32:42,...s0(se[l[2]&7]||"???"),l[2]&64?60:32])}[c,r]=[p[0],p[1]]}let o=0;for(let c=1;c<=35;c++)c!==18&&(o+=s[4*c]);return n(o,[...s0("BLOCKS FREE."),...Array(13).fill(32)]),f.push(0,0),Uint8Array.from(f)}remember(t){t&&t.track&&(this.mem[126]=t.track,this.mem[622]=0,this.mem[623]=t.sector)}load(t){t=x.strip(t);let e;return t[0]===36?(this.status=s0(y0),this.listing()):(t.length===1&&t[0]===42?e=this.mem[126]?{raw:this.chain(this.mem[126],this.mem[623])}:{...this.loadable()[0],raw:null}:e=this.find(t),e&&!e.raw&&(e.raw=this.chain(e.track,e.sector)),this.remember(e),this.status=s0(e?y0:_e),e&&e.raw)}save(t,e){this.store(String.fromCharCode(...x.strip(t)),e),this.status=s0(y0)}store(t,e){this.saved.set(t,e),this.keep?.(t,e)}listen(t){return this.listening=t===8,this.listening}talk(t){return this.talking=t===8,this.talking}second(t){if(!this.listening)return;let e=t&15,s=t&240;s===240?this.opening={ch:e,name:[]}:s===224?this.close(e):this.channel=e}tksa(t){this.talking&&(this.channel=t&15,this.pos=0)}ciout(t){if(!this.listening)return;if(this.opening){this.opening.name.push(t);return}let e=this.channels.get(this.channel);e&&(e.command?e.command.push(t):e.write?e.write.push(t):e.buffer&&(e.buffer[e.pos]=t,e.pos=e.pos+1&255))}unlsn(){if(this.opening)this.open(this.opening.ch,this.opening.name),this.opening=null;else if(this.channel===15){let t=this.channels.get(15);t.command.length&&(this.execute(Uint8Array.from(t.command)),t.command=[])}this.listening=!1}untlk(){this.talking=!1}acptr(){let t=this.channels.get(this.channel);if(this.channel===15){let e=this.reply||this.status;if(this.pos>=e.length)return this.reply=null,this.pos=0,null;let s=e[this.pos++],f=this.pos>=e.length;return f&&(this.reply?this.reply=null:this.status=s0(y0),this.pos=0),{byte:s,eoi:f}}if(!t)return null;if(t.buffer){let e=t.buffer[t.pos];return t.pos=t.pos+1&255,{byte:e,eoi:t.pos===0}}return!t.data||t.pos>=t.data.length?null:{byte:t.data[t.pos++],eoi:t.pos>=t.data.length}}open(t,e){if(t===15){e.length&&this.execute(Uint8Array.from(e));return}if(e[0]===35){this.channels.set(t,{buffer:new Uint8Array(256),pos:0}),this.status=s0(y0);return}let s=[],f=[];for(let c of e)c===44?(s.push(f),f=[]):f.push(c);if(s.push(f),(t===1||t!==0&&s.slice(1).some(c=>c[0]===87)?"W":"R")==="W"){this.channels.set(t,{write:[],name:String.fromCharCode(...x.strip(s[0]))}),this.status=s0(y0);return}let o=this.load(s[0]);this.channels.set(t,{data:o??new Uint8Array(0),pos:0})}close(t){let e=this.channels.get(t);e&&e.write&&this.store(e.name,Uint8Array.from(e.write)),t!==15&&this.channels.delete(t)}execute(t){this.mem.set(t.subarray(0,42),512);let e=String.fromCharCode(...t);if(this.status=s0(y0),this.reply=null,e.startsWith("M-W")){let f=t[3]|t[4]<<8,n=t[5];for(let o=0;o<n&&6+o<t.length;o++)this.mem[f+o&2047]=t[6+o];return}if(e.startsWith("M-R")){let f=t[3]|t[4]<<8,n=t.length>5?t[5]||256:1;this.reply=this.mem.slice(f&2047,(f&2047)+n),this.pos=0;return}if(e.startsWith("M-E")){let f=t.subarray(5);for(let n=0;n+8<=f.length;n++)if(f[n]===162&&f[n+2]===160&&f[n+4]===134&&f[n+5]===6&&f[n+6]===132&&f[n+7]===7){let o=f[n+1],c=f[n+3];o>=1&&o<=35&&c<Tt[o-1]&&this.mem.set(this.sector(o,c),768);return}return}let s=/^(U1|UA|B-R|B-P)[: ]?\s*(.*)$/.exec(e.replace(/\r$/,""));if(s){let f=s[2].split(/[\s,]+/).filter(Boolean).map(Number),n=this.channels.get(f[0]);if(!n||!n.buffer){this.status=s0("70,NO CHANNEL,00,00\r");return}if(s[1]==="B-P"){n.pos=f[1]&255;return}let[,,o,c]=f;o>=1&&o<=35&&c<Tt[o-1]?(n.buffer.set(this.sector(o,c)),n.pos=0):this.status=s0("66,ILLEGAL TRACK OR SECTOR,"+String(o).padStart(2,"0")+","+String(c).padStart(2,"0")+"\r");return}/^I/.test(e)||(this.status=s0(Je))}};var ne={};X(ne,{editor:()=>St});var fe=[144,5,28,159,156,30,31,158,129,149,150,151,152,153,154,155];function St({read:x,write:t,keyRow:e=()=>255}){let s=(i,a)=>t(i,a&255),f=i=>i*40&255,n=i=>{s(209,f(i)),s(210,x(217+i)&3|x(648))},o=()=>{s(243,x(209)),s(244,x(210)&3|216)},c=()=>x(209)|x(210)<<8,r=()=>x(243)|x(244)<<8;function h(i){n(i),o();for(let a=39;a>=0;a--)t(r()+a,x(646)),t(c()+a,32)}function p(){let i=x(214),a=x(211);for(;!(x(217+i)&128)&&(a=a+40&255,s(211,a),i--,!(i<0)););n(i&255);let b=39;for(i++;!(x(217+i)&128)&&(b+=40,i++,!(i>127)););s(213,b),o()}function d(){let i=x(648)|128,a=0;for(let b=0;b<26;b++)s(217+b,i),a+=40,a>255&&(a&=255,i++);s(243,255);for(let b=24;b>=0;b--)h(b);l()}function l(){s(211,0),s(214,0),p()}function y(){let i=[172,173,174,175].map(x);for(;;){s(214,x(214)-1),s(201,x(201)-1),s(677,x(677)-1);let a;for(a=0;a<24;a++)n(a),E(f(a+1),x(218+a));n(a),h(24);for(let b=0;b<24;b++)s(217+b,x(217+b)&127|x(218+b)&128);if(s(241,x(241)|128),x(217)&128)break}s(214,x(214)+1),s(677,x(677)+1),e(127)===251&&s(198,0),[172,173,174,175].forEach((a,b)=>s(a,i[b]))}function E(i,a){let b=i|(a&3|x(648))<<8,k=i|(a&3|216)<<8;s(172,i),s(173,b>>8),o(),s(174,i),s(175,k>>8);for(let T=39;T>=0;T--)t(c()+T,x(b+T)),t(r()+T,x(k+T))}function u(i=x(214)+1){let a=i;for(;!(x(217+a)&128);)a++;if(s(677,a),a>24)return y(),a=x(677)-1,s(214,x(214)-1),O(a);let b=[172,173,174,175].map(x);for(let k=24;k>x(677);k--)n(k),E(f(k-1),x(216+k));n(Math.max(x(677),0)),h(x(677));for(let k=23;k>=x(677)&&k>0;k--)s(218+k,x(218+k)&127|x(217+k)&128);O(x(677)),[172,173,174,175].forEach((k,T)=>s(k,b[T]))}function O(i){s(217+i,x(217+i)&127),s(217+i+1,x(217+i+1)|128),s(213,x(213)+40);let a=i;for(;!(x(217+a)&128)&&a>0;)a--;n(a)}function I(){s(201,x(201)>>1);let i=x(214);do i++,i===25&&(y(),i=x(214));while(!(x(217+i)&128));s(214,i),p()}function A(){s(216,0),s(199,0),s(212,0),s(211,0),I()}function D(){for(let i of[39,79])if(x(211)===i){x(214)!==25&&s(214,x(214)+1);return}}function V(){for(let i of[0,40])if(x(211)===i){s(214,x(214)-1);return}}function N(i,a){s(205,2),o(),t(c()+x(211),i),t(r()+x(211),a)}function K(){let i=x(214);if(D(),s(211,x(211)+1),x(213)>=x(211))return;if(x(213)===79){s(214,x(214)-1),I(),s(211,0);return}if(x(658))return u(i+1);let a=x(214);a>=25&&(y(),s(214,x(214)-1),a=x(214)),O(a)}function G(i){x(199)&&(i|=128),x(216)&&s(216,x(216)-1),N(i,x(646)),K()}function U(){x(216)&&s(212,x(212)>>1)}function R(){let i=x(214);return i?(s(214,i-1),p(),s(211,x(213)),!0):(s(211,0),!1)}function C(i){if(s(215,i),s(208,0),i&128)return L(i);if(i===13)return A(),U();if(i>=32){let b=i<96?i&63:i&223;return i===34&&s(212,x(212)^1),G(b),U()}if(x(216))return G(i|128),U();if(i===20){if(x(211)===0){if(!R())return U()}else{V(),s(211,x(211)-1),o();for(let b=x(211);b+1!==x(213)+1&&b<x(213);b++)t(c()+b,x(c()+b+1)),t(r()+b,x(r()+b+1))}return t(c()+x(213),32),t(r()+x(213),x(646)),U()}if(x(212))return G(i|128),U();if(i===18&&s(199,18),i===19&&l(),i===29){let b=x(211)+1;return D(),s(211,b),b-1>=x(213)&&(s(214,x(214)-1),I(),s(211,0)),U()}if(i===17){let b=x(211)+40;if(s(214,x(214)+1),b>x(213)){s(214,x(214)-1);do b-=40,b>=0&&s(211,b);while(b>0&&b>=40);I()}else s(211,b);return U()}let a=fe.indexOf(i);return a>=0&&s(646,a),i===14&&t(53272,x(53272)|2),i===8&&s(657,128),i===9&&s(657,0),U()}function L(i){if(i&=127,i===127&&(i=94),i>=32)return G(i|64),U();if(i===13)return A(),U();if(x(212)||i!==20&&x(216))return G(i|64|128),U();if(i===20){let b=x(213);if(!(x(c()+b)===32&&b!==x(211))){if(b===79)return U();u()}for(b=x(213),o();b>x(211);b--)t(c()+b,x(c()+b-1)),t(r()+b,x(r()+b-1));return t(c()+b,32),t(r()+b,x(646)),s(216,x(216)+1),U()}if(i===17){let b=x(214);if(b){s(214,b-1);let k=x(211)-40;k>=0?s(211,k):p()}return U()}if(i===18&&s(199,0),i===29)return x(211)===0?R():(V(),s(211,x(211)-1)),U();if(i===19)return d(),U();let a=fe.indexOf(i|128);return a>=0&&s(646,a),i===14&&t(53272,x(53272)&-3),U()}function S(){s(202,x(211)),s(201,x(214))}function $(){let i=x(631);for(let a=0;a<x(198)-1;a++)s(631+a,x(632+a));return s(198,x(198)-1),i}function Y(){if(!x(208))for(;;){let k=x(198);if(s(204,k),s(658,k),!k)return null;x(207)&&(s(207,0),s(205,2),o(),t(c()+x(211),x(206)),t(r()+x(211),x(647)));let T=$();if(T===131){s(198,9),[76,79,65,68,13,82,85,78,13].forEach((z,J)=>s(631+J,z));continue}if(T!==13){C(T);continue}let P=x(213);for(s(208,P);x(c()+P)===32&&--P;);if(s(200,P+1),s(658,0),s(211,0),s(212,0),!(x(201)&128)){let z=x(214);if(z!==x(201)){for(;!(x(217+z)&128)&&z;)z--;n(z)}if(z===x(201)&&(s(211,x(202)),x(202)>=x(200)))return W()}break}let i=x(211),a=x(c()+i),b=a&63;return a&64&&(b|=128),!(a&128&&x(212))&&!(a&32)&&(b|=64),s(211,i+1),b===34&&s(212,x(212)^1),i===x(200)?W():(s(215,b),b===222?255:b)}function W(){return s(208,0),(x(153)===3||x(154)!==3)&&C(13),s(215,13),13}return{chrout:C,clear:d,home:l,setLine:p,clearLine:h,scroll:y,linePointer:n,colourPointer:o,begin:S,input:Y}}var re={};X(re,{glyphHash:()=>Ze});function Ze(x){let t=2166136261;for(let e=0;e<8;e++)t^=x[e],t=Math.imul(t,16777619)>>>0;return t}var ae={};X(ae,{GLYPH_HASHES:()=>tx});var tx=Uint32Array.of(2788871881,3484888503,3246991681,3624609685,453335123,1933125101,3077548787,2485223133,3877308795,3432987389,2981056711,1872599573,1643867547,1179332908,2247513867,2517044099,3094798189,3185123891,1385555207,852155623,1305655403,3768945513,3104403535,172795524,151750749,958039455,520025719,998705349,604089761,3595948113,2979418607,3944112791,2615243109,3434190861,1980558771,1675726591,43263885,2750147941,1233223601,4068015871,2558456709,1388364817,2582067888,3201647787,1929597349,2477030683,1661155445,767913286,4126389003,3192125595,2919328883,1388120033,3069637630,4281928141,1433572807,2917230913,4042988033,23194427,1680998453,2219162189,1896240069,206115429,3517826675,3008015341,3070282263,574191955,1008328549,3070282263,3197812635,4268266599,3339900715,267615717,4025240901,2468127309,1645230771,4202881437,1240450747,2802247017,3429822949,139144843,2788675629,4100334877,4246080071,4226633506,507226597,62691803,2473932793,2356380797,502307177,1567189477,100162740,3999692135,2749038821,1008328549,3586085438,4127708279,2615243109,2582733285,3025216001,3892497018,1289914376,1491276389,2998968857,1176470573,486888467,3544142820,1176470573,3226581351,1478355617,506322047,922277773,1280307259,2734711375,1279432831,886906767,1781439333,1491276389,4115828709,2118204077,621133707,690878844,34897702,3404710277,1615110309,2483765857,161469789,3762642597,2625755105,3568540537,3700270475,1401725133,4229510277,223371831,1692511041,1693599947,3531295949,4097784435,3527471925,2598774295,1127039233,4072743631,3279545972,2228721123,3008149239,3556571053,3598981159,288175123,2624662271,3292225379,2720560613,531943631,549462116,3212481081,2640094591,4041105511,3145394397,3294299081,3803506729,1114461399,2807665807,1823345245,3940892309,2400498843,2943953607,2583855265,1600820257,261986117,821172103,3363790221,4102991185,1527536824,666862203,4085848461,617022555,59323069,4105358430,3702878111,892881135,3840200543,4070938477,3628736310,2403520025,3873452447,3432863805,1617024269,1957258959,1636585133,835202373,3350047897,1478118129,2039306899,3533524333,3810128655,3685453475,1401108333,3810128655,3747807783,2841555295,3443588247,3604414269,3922001261,502616453,464003347,1564178357,639042183,3798018505,3228952373,3155195831,1959305101,3892720933,3466729791,4177298282,314954333,2583995099,2265111153,3440879813,3669217341,3527045533,1332417832,4144417359,3486903389,1401108333,3644333762,3356726655,1823345245,1084481853,2227149249,905105778,3450774288,1791622109,4143158105,990627269,2459051247,2790306112,990627269,616548367,1761986145,3863348887,1739784821,3881622407,3829722775,3096044247,1062015511,527219373,1791622109,4073289565,657495013,2887613495,2129402996,2208511534,2020044237,827865245,3992838977,2738655045,626497277,565138177,2788871881,1732001465,3394772477,2638126053,867869333,757064797,3427580885,676599287,3863560071,2872764249,1926724151,2926798603,1987369905,3977488583,1472896809,3531648339,4104394829,2455026901,2648202479,1637458909,3045183589,2874071771,711513647,1934610232,1987004669,2552993337,851174649,998705349,604089761,3595948113,2979418607,3944112791,2615243109,3434190861,1980558771,1675726591,43263885,2750147941,1233223601,4068015871,2558456709,1388364817,2582067888,3201647787,1929597349,2477030683,1661155445,767913286,4126389003,3192125595,2919328883,1388120033,3069637630,4281928141,1433572807,2917230913,4042988033,23194427,1680998453,2219162189,1896240069,206115429,3517826675,3008015341,3070282263,3484888503,3246991681,3624609685,453335123,1933125101,3077548787,2485223133,3877308795,3432987389,2981056711,1872599573,1643867547,1179332908,2247513867,2517044099,3094798189,3185123891,1385555207,852155623,1305655403,3768945513,3104403535,172795524,151750749,958039455,520025719,3999692135,2749038821,1008328549,4143158105,2678619321,2615243109,2582733285,3025216001,3892497018,1289914376,1491276389,2998968857,1176470573,486888467,1057150249,1176470573,3226581351,1478355617,506322047,922277773,1280307259,2734711375,1279432831,886906767,1781439333,1491276389,4115828709,2118204077,621133707,690878844,34897702,4015435567,1615110309,2483765857,161469789,3762642597,2625755105,3568540537,1901009401,2874680809,779276517,4022933181,4029064749,2040122065,3519314459,2878917951,1253589729,2183500875,2097480327,9711401,3148018267,4273879737,3218028591,2398275789,2613323033,3363024487,1320020493,3203018197,2674282647,2085422203,2232165276,1116765149,731252793,3981948569,3145394397,3294299081,3803506729,1114461399,2807665807,1823345245,3940892309,2400498843,2943953607,2583855265,1600820257,261986117,821172103,3363790221,4102991185,1527536824,666862203,4085848461,617022555,59323069,4105358430,3702878111,892881135,3840200543,4070938477,3628736310,2403520025,3873452447,3432863805,1617024269,1957258959,1636585133,835202373,3350047897,1478118129,2039306899,3533524333,3810128655,3700270475,1401725133,4229510277,223371831,1692511041,1693599947,3531295949,4097784435,3527471925,2598774295,1127039233,4072743631,3279545972,2228721123,3008149239,3556571053,3598981159,288175123,2624662271,3292225379,2720560613,531943631,549462116,3212481081,2640094591,4041105511,4144417359,3486903389,1401108333,2998968857,3570429753,1823345245,1084481853,2227149249,905105778,3450774288,1791622109,4143158105,990627269,2459051247,1076661217,990627269,616548367,1761986145,3863348887,1739784821,3881622407,3829722775,3096044247,1062015511,527219373,1791622109,4073289565,657495013,2887613495,2129402996,2208511534,2263771151,827865245,3992838977,2738655045,626497277,565138177);var he={};X(he,{KEY_TABLES:()=>It,VECTORS:()=>st,VIC:()=>xt,install:()=>Nt,kernalByte:()=>Lt,matrixOf:()=>ix,screenCode:()=>Ft});var xt=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,155,55,0,0,0,8,0,20,15,0,0,0,0,0,0,14,6,1,2,3,4,0,1,2,3,4,5,6,7,76],st=[59953,65126,65095,62282,62097,61966,62032,62259,61783,61898,63213,61758,62255,65126,62629,62957];function Ft(x){return x>=32&&x<64?x:x>=64&&x<96?x-64:x>=96&&x<128?x-32:x>=160&&x<192?x-64:x>=192&&x<255?x-128:x===255?94:32}var oe=[20,13,29,136,133,134,135,17,51,87,65,52,90,83,69,1,53,82,68,54,67,70,84,88,55,89,71,56,66,72,85,86,57,73,74,48,77,75,79,78,43,80,76,45,46,58,64,44,92,42,59,19,1,61,94,47,49,95,4,50,32,2,81,3],ex=[148,141,157,140,137,138,139,145,35,215,193,36,218,211,197,1,37,210,196,38,195,198,212,216,39,217,199,40,194,200,213,214,41,201,202,48,205,203,207,206,219,208,204,221,62,91,186,60,169,192,93,147,1,61,222,63,33,95,4,34,160,2,209,131],xx=[148,141,157,140,137,138,139,145,150,179,176,151,173,174,177,1,152,178,172,153,188,187,163,189,154,183,165,155,191,180,184,190,41,162,181,48,167,161,185,170,166,175,182,220,62,91,164,60,168,223,93,147,1,61,222,63,129,95,4,149,160,2,171,131],sx=[255,255,255,255,255,255,255,255,28,23,1,159,26,19,5,255,156,18,4,30,3,6,20,24,31,25,7,158,2,8,21,22,18,9,10,146,13,11,15,14,255,16,12,255,255,27,0,255,28,255,29,255,255,31,30,255,144,6,255,5,255,255,17,255],ce=[oe,ex,xx,sx],L0=[60289,60354,60419,60536],It=[60281,60601];function Lt(x){if(x>=60281&&x<60289)return L0[x-60281>>1]>>(x&1?0:8)&255;let t=L0.findIndex(f=>x>=f&&x<=f+64),e=x-L0[t],s=t<0?void 0:e===64?255:ce[t][e];if(s===void 0&&x>=It[0]&&x<It[1])throw new Error(`the KERNAL's byte at $${x.toString(16).toUpperCase()}: not handled`);return s}function ix(x){let t={157:2,145:7};if(t[x]!==void 0)return{code:t[x],shift:1};let e=oe.indexOf(x);return e<0||[1,2,3,4].includes(x)?null:{code:e,shift:0}}function Nt({cpu:x,read:t,write:e,drive:s,loaded:f=()=>{},call:n,held:o=()=>null,log:c=()=>{}}){let r=R=>t(R),h=(R,C)=>e(R,C&255),p=St({read:t,write:e,keyRow:R=>{e(56320,R);let C=t(56321);return e(56320,127),C}}),d=R=>p.chrout(R),l=()=>p.clear(),y=!1,E=R=>h(144,R),u=()=>r(152),O=R=>{for(let C=0;C<u();C++)if(r(601+C)===R)return C;return-1},I=()=>{let R=r(184),C=r(186),L=r(185);if(O(R)>=0){x.a=2,x.c=1;return}if(u()>=10){x.a=1,x.c=1;return}let S=u();if(h(601+S,R),h(611+S,C),h(621+S,L|96),h(152,S+1),C>=4&&(E(0),r(183))){if(!s.listen(C)){E(128),x.a=5,x.c=1;return}s.second(L|96|240);let $=r(187)|r(188)<<8;for(let Y=0;Y<r(183);Y++)s.ciout(t($+Y));s.unlsn()}x.c=0},A=()=>{let R=O(x.a);if(R<0){x.c=0;return}let C=r(611+R),L=r(621+R);if(C>=4){let S=s.listen(C);E(S?0:128),S&&(s.second(L&239|224),s.unlsn())}for(let S=R;S<u()-1;S++)h(601+S,r(602+S)),h(611+S,r(612+S)),h(621+S,r(622+S));h(152,u()-1),x.c=0},D=R=>{let C=O(x.x);if(C<0){x.a=3,x.c=1;return}let L=r(611+C),S=r(621+C);if(L>=4){let $=R?s.listen(L):s.talk(L);if(E($?0:128),!$){x.a=5,x.c=1;return}R?s.second(S):s.tksa(S)}h(R?154:153,L),x.c=0},V=()=>{r(154)>=4&&s.unlsn(),r(153)>=4&&s.untlk(),h(154,3),h(153,0)},N={65490:()=>{r(154)>=4?s.ciout(x.a):d(x.a),x.c=0},65472:I,65475:A,65478:()=>D(!1),65481:()=>D(!0),65511:()=>{V(),h(152,0)},65487:()=>{if(r(153)===0){y||(p.begin(),y=!0);let C=p.input();if(C===null)return"wait";y=!1,x.a=C,x.c=0;return}if(r(153)!==8)throw new Error("CHRIN from device "+r(153)+": not handled");if(r(144)){x.a=13,x.c=0;return}let R=s.acptr();R?(x.a=R.byte,R.eoi&&E(r(144)|64)):(x.a=13,E(r(144)|66)),x.c=0},65520:()=>{x.c?(x.x=r(214),x.y=r(211)):(h(214,x.x),h(211,x.y),p.setLine())},65484:V,65424:()=>{h(157,x.a)},65505:()=>{x.z=0},65463:()=>{x.a=r(144),x.nz(x.a)},65466:()=>{h(184,x.a),h(186,x.x),h(185,x.y)},65469:()=>{h(183,x.a),h(187,x.x),h(188,x.y)},65493:()=>{let R=r(183),C=r(187)|r(188)<<8,L=Uint8Array.from({length:R},(a,b)=>r(C+b)),S=r(185),$=r(186)===8?s.load(L):null;if(!$||$.length<2||x.a!==0){x.a=4,x.c=1,E(66),c("LOAD",String.fromCharCode(...L),"not found");return}let Y=S===0?x.x|x.y<<8:$[0]|$[1]<<8,W=$.subarray(2);for(let a=0;a<W.length;a++)e(Y+a,W[a]);let i=Y+W.length;h(174,i),h(175,i>>8),E(0),x.x=i&255,x.y=i>>8,x.c=0,x.a=0,f(String.fromCharCode(...L),Y,i-1)},65496:()=>{let R=r(183),C=r(187)|r(188)<<8,L=Uint8Array.from({length:R},(W,i)=>r(C+i)),S=r(x.a)|r(x.a+1)<<8,$=x.x|x.y<<8,Y=new Uint8Array($-S+2);Y[0]=S&255,Y[1]=S>>8;for(let W=S;W<$;W++)Y[W-S+2]=t(W);s.save(L,Y),x.c=0,x.a=0},65457:()=>E(s.listen(x.a)?0:128),65460:()=>E(s.talk(x.a)?0:128),65427:()=>s.second(x.a),65430:()=>s.tksa(x.a),65448:()=>s.ciout(x.a),65454:()=>s.unlsn(),65451:()=>s.untlk(),65445:()=>{let R=s.acptr();R?(x.a=R.byte,R.eoi&&E(r(144)|64)):(x.a=13,E(r(144)|66)),x.c=0},65499:()=>{h(162,x.a),h(161,x.x),h(160,x.y)},65502:()=>{x.a=r(162),x.x=r(161),x.y=r(160)},65514:()=>{let R=(r(160)<<16|r(161)<<8|r(162))+1;R>=5184001&&(R=0),h(160,R>>16),h(161,R>>8),h(162,R),h(145,t(56321))},65371:()=>{xt.forEach((R,C)=>e(53248+C,R)),h(646,r(646)||14),h(648,r(648)||4),h(199,0),l()},64789:()=>st.forEach((R,C)=>{h(788+2*C,R),h(789+2*C,R>>8)})},K=()=>{let R=r(655)|r(656)<<8;if(R!==60232){n(R);return}let C=r(203),L=r(653),S;L===3?(U(L),S=Math.max(0,L0.indexOf(r(245)|r(246)<<8))):(S=L*2>=8?3:L,h(245,L0[S]),h(246,L0[S]>>8));let $=C===64?255:ce[S][C],Y=()=>{h(197,C),h(654,L),$!==255&&r(198)<r(649)&&(h(631+r(198),$),h(198,r(198)+1))};if(C!==r(197)){h(652,16),Y();return}let W=$&127,i=r(650);if(!(i&128)){if(i&64)return;if(W===127){Y();return}if(![20,32,29,17].includes(W))return}r(652)&&(h(652,r(652)-1),r(652))||(h(651,r(651)-1),!r(651)&&(h(651,4),!r(198)&&Y()))},G=()=>{let R=o(),C=R?.shift??0;h(653,C),h(203,R?.code??64),R&&R.code!==null?K():(U(C),h(197,64),h(654,C))},U=R=>{R===3&&r(654)!==3&&!(r(657)&128)&&e(53272,t(53272)^2)};N[60125]=K,N[59953]=()=>{N[65514](),N[59956]()},N[59956]=()=>{if(!r(204)&&(h(205,r(205)-1),!r(205))){h(205,20);let R=r(211),C=(r(209)|r(210)<<8)+R,L=55296+((r(209)|r(210)<<8)&1023)+R,S=r(207)&1;h(207,r(207)>>1);let $=r(647),Y=t(C);S||(h(207,r(207)+1),h(206,Y),h(647,t(L)),$=r(646),Y=r(206)),e(C,Y^128),e(L,$)}r(1)&16?(h(192,0),h(1,r(1)|32)):r(192)||h(1,r(1)&31),G()},N[60027]=G;for(let R of[60030,60033])N[R]=()=>{};N[48201]=()=>{if(x.x!==144||!x.c)throw new Error("FLOATC with another exponent or sign: not handled");let R=r(98)<<24|r(99)<<16,C=x.x;if(!R)C=0;else for(;!(R&2147483648);)R=R<<1>>>0,C--;h(97,C),h(98,R>>>24),h(99,R>>>16),h(100,0),h(101,0),h(102,0),h(112,0),x.y=0},N[48607]=()=>{let R=r(97),C=(r(98)<<24|r(99)<<16|r(100)<<8|r(101))>>>0,L=160-R;if(R&&(L<0||L>32||C%2**L))throw new Error("FOUT of a number not whole: not handled");let S=R?C/2**L:0,$=r(102)&128?45:32,Y=String(S);e(255+x.y,$),h(102,$);for(let W=0;W<Y.length;W++)e(256+x.y+W,Y.charCodeAt(W));e(256+x.y+Y.length,0),x.a=0,x.y=1,x.x=0},N[62421]=()=>{if(r(185)&128||!r(183)){x.c=0;return}E(s.listen(r(186))?0:128),s.second(r(185)|240);let R=r(187)|r(188)<<8;for(let C=0;C<r(183);C++)s.ciout(r(R+C));s.unlsn(),x.c=r(144)&128?1:0},N[63042]=()=>{if(r(185)&128)return;let R=s.listen(r(186));E(R?0:128),R&&(s.second(r(185)&239|224),s.unlsn()),x.c=0},Object.assign(N,{61898:N[65490],62629:N[65493],62957:N[65496]});for(let[R,C]of Object.entries(N))x.hooks.set(+R,C)}var ke={};X(ke,{C64:()=>Dt});import{cpu as fx}from"@moonbase-machines/mos6502";var{CPU:le,CYCLES:Gx}=fx;var ue={};X(ue,{Printer:()=>H0,WIDTH:()=>$t,bus:()=>Ut,paper:()=>nx});var $t=80,H0=class{constructor({on:t=!0}={}){this.on=t,this.listening=!1,this.lower=!1,this.wide=!1,this.reverse=!1,this.lines=[],this.line=[],this.changed=()=>{}}listen(t){return this.listening=this.on&&t===4,this.listening}talk(){return!1}second(t){this.listening&&(this.lower=(t&15)===7)}tksa(){}ciout(t){if(this.listening){switch(t){case 13:this.feed();return;case 10:return;case 17:this.lower=!0;return;case 145:this.lower=!1;return;case 14:this.wide=!0;return;case 15:this.wide=!1;return;case 18:this.reverse=!0;return;case 146:this.reverse=!1;return;case 29:t=32;break}(t&127)<32||(this.line.length>=$t&&this.feed(),this.line.push({code:t,lower:this.lower,wide:this.wide,reverse:this.reverse}))}}feed(){this.lines.push(this.line),this.line=[],this.wide=!1,this.reverse=!1,this.changed()}unlsn(){this.listening=!1}untlk(){}acptr(){return null}tear(){this.lines=[],this.line=[],this.changed()}};function Ut(x,t){let e=[x,t];return{drive:x,printer:t,listen:s=>e.map(f=>f.listen(s)).some(Boolean),talk:s=>x.talk(s),second:s=>e.forEach(f=>f.second(s)),tksa:s=>x.tksa(s),ciout:s=>e.forEach(f=>f.ciout(s)),unlsn:()=>e.forEach(s=>s.unlsn()),untlk:()=>x.untlk(),acptr:()=>x.acptr(),load:s=>x.load(s),save:(s,f)=>x.save(s,f),find:s=>x.find(s)}}function nx(x,t,e,{scale:s=2}={}){let n=8*s,o=n*1.5,c=Math.max(12,t.lines.length+2);x.width=($t*8+48)*s/2*2,x.height=c*o+n;let r=x.getContext("2d"),h=x.width;r.fillStyle="#fbfaf3",r.fillRect(0,0,h,x.height);for(let l=0;l<c;l+=6)r.fillStyle="#e6f0e2",r.fillRect(24*s/2,l*o+n/2,h-24*s,o*3);r.fillStyle="#d9d6c8";for(let l=n/2;l<x.height;l+=n*2)for(let y of[24*s/4,h-24*s/4])r.beginPath(),r.arc(y,l,s*2.5,0,Math.PI*2),r.fill();let p=[...t.lines,t.line],d=s;p.forEach((l,y)=>{let E=24*s/2+n/2;for(let u of l){let O=Ft(u.code),I=(u.lower?2048:0)+O*8,A=u.wide?2:1;for(let D=0;D<8;D++){let V=e?e[I+D]:0;for(let N=0;N<8;N++)V>>7-N&1^(u.reverse?1:0)&&(r.fillStyle="#26241f",r.fillRect(E+N*d*A,y*o+n/2+D*d,d*A,d))}E+=n*A}})}var me={};X(me,{STANDS_IN:()=>ax,install:()=>Mt,standIn:()=>it});import{chargen as rx}from"@moonbase-machines/charset";var{chargen:de}=rx;var ax=["zoopak","goldfields","crayon64","crayon64-studio"],ox={65530:67,65531:254,65532:226,65533:252,65534:72,65535:255,44689:188,64433:141,65449:221};function be(x){let t=new Uint8Array(8192),e=new Uint8Array(8192);for(let s=x;s<x+8192;s++){let f=ox[s];if(f===void 0&&x===57344)try{f=Lt(s)}catch{f=void 0}for(let[n,o]of Object.entries(N0))s>=+n&&s<+n+o.length&&(f=o[s-n]);f!==void 0&&(t[s-x]=f,e[s-x]=1)}return Object.assign(t,{known:e,from:x})}function it(){return{kernal:be(57344),basic:be(40960),chargen:de(),standIn:!0}}var pe={15:1,52:1,61:2,58:4};function cx(x){let t=null,e=0;for(let s=0;s<8;s++)for(let f=0;f<8;f++){if(!(x.keys[s]&1<<f))continue;let n=s*8+f;pe[n]?e|=pe[n]:t===null&&(t=n)}return t===null&&!e?null:{code:t,shift:e}}function Mt(x){let t=x.cpu,e=g=>x.read(g),s=(g,m)=>x.write(g,m&255),f=g=>g>=57344&&x.port&2||g>=40960&&g<49152&&(x.port&3)===3||g===115||g===121,n=g=>e(g)|e(g+1)<<8,o=g=>{let m=t.pc,w=t.sp;t.push(m-1>>8),t.push(m-1&255),t.pc=g;for(let F=0;F<1e6&&!(t.pc===m&&t.sp===w);F++)t.step()},c=new Set(t.hooks.keys());Nt({cpu:t,read:e,write:s,drive:x.bus,call:o,held:()=>cx(x),log:x.log}),t.hooks.set(65508,()=>{if(e(153)>=4)return t.hooks.get(61783)(t);let g=e(198);if(!g){t.a=0,t.nz(0),t.c=0;return}t.a=e(631);for(let m=0;m<g-1;m++)s(631+m,e(632+m));s(198,g-1),t.nz(t.a),t.c=0}),t.hooks.set(65439,t.hooks.get(60027)),t.hooks.set(60039,t.hooks.get(60027)),t.hooks.set(65517,()=>{t.x=40,t.y=25}),t.hooks.set(65523,()=>{t.x=0,t.y=220});let r={65472:[794,62282],65475:[796,62097],65478:[798,61966],65481:[800,62032],65484:[802,62259],65487:[804,61783],65490:[806,61898],65505:[808,63213],65508:[810,61758],65511:[812,62255]};for(let[g,[m,w]]of Object.entries(r).map(([F,v])=>[+F,v]))t.hooks.set(w,t.hooks.get(g)),t.hooks.set(g,()=>(t.pc=n(m),!1));t.hooks.set(65493,()=>(s(195,t.x),s(196,t.y),t.pc=62622,!1)),t.hooks.set(65496,()=>(t.pc=62941,!1)),kt({cpu:t,read:e,write:s,chrout:t.hooks.get(61898),evaluate:g=>{let m=V?.params?.[g];if(!m)return h(g);let[w,F,v]=m;return{value:F(N),end:w,place:v}}});let h=yt({cpu:t,read:e,write:s,ram:x.ram}),p=[...t.hooks.keys()].filter(g=>!c.has(g)),d=()=>{t.y=t.pull(),t.x=t.pull(),t.a=t.pull(),t.p=t.pull(),t.pc=t.pull()|t.pull()<<8},l=(g,m)=>t.hooks.set(g,m);{let g=t.hooks.get(65371);t.hooks.set(65371,()=>{g(t);for(let w=0;w<1e5&&e(53266)!==0;w++);s(678,e(53273)&1);let m=e(678)?16421:17045;s(56324,m),s(56325,m>>8),s(56333,129),s(56334,e(56334)&128|17),s(56576,e(56576)|16)})}for(let g of[59953,59956,60027,60030]){let m=t.hooks.get(g);l(g,()=>(m(t),e(56333),d(),!1))}l(60033,()=>(d(),!1)),l(65352,()=>(t.push(t.a),t.push(t.x),t.push(t.y),t.pc=n(e(260+t.sp)&16?790:788),!1)),l(65091,()=>(t.i=1,t.pc=n(792),!1)),l(65095,()=>(e(56589),t.p=t.pull(),t.pc=t.pull()|t.pull()<<8,!1)),l(64738,()=>(I(),t.i=0,t.pc=42112,!1)),l(42112,()=>D()),l(57720,()=>(e(185)||(s(45,e(174)),s(46,e(175))),O("\rREADY.\r"),t.sp=255,t.pc=42112,!1)),l(43121,()=>(k(),t.pc=42926,!1)),l(42926,()=>A0()),l(42585,()=>(s(47,e(45)),s(48,e(46)),s(49,e(45)),s(50,e(46)),s(51,e(55)),s(52,e(56)),k(),C=null,!0)),l(42291,()=>{for(let g=e(43)|e(44)<<8,m=0;e(g+1)&&m<65536;m++){let w=g+4;for(;e(w);)w++;s(g,w+1&255),s(g+1,w+1>>8),g=w+1}return N&&b(),!0}),l(42039,()=>(t.pc=e(768)|e(769)<<8,!1)),l(58251,()=>(t.pc=t.x&128?42100:42042,!1)),l(42042,()=>(Z0(E,"?"+p0[t.x-1]+" ERROR",e(57)|e(58)<<8),t.sp=255,t.pc=42112,!1));let y=(g,m)=>{let w=t.sp,F=m+2,v=t.pc;t.push(F>>8),t.push(F&255),t.pc=g;for(let q=0;q<1e6&&!(t.pc===F+1&&t.sp===w);q++)t.step();t.pc=v},E={cpu:{get a(){return t.a},set a(g){t.a=g},hooks:{get:g=>()=>y(g,42055)}},write:s};l(42100,()=>(O("\rREADY.\r"),t.sp=255,t.pc=42112,!1));for(let g of[...p,42039,58251,42042,42100,60033,65352,65091,65095,64738,42112,57720,43121,42926,42585,42291]){let m=t.hooks.get(g);t.hooks.set(g,w=>{if(!f(w.pc))return!1;let F;try{F=m(w)}catch(v){let q=v instanceof j&&!U&&/^\?(.*) ERROR$/.exec(v.message)?.[1];if(!p0.includes(q))throw v;return w.x=p0.indexOf(q)+1,w.pc=42039,!1}return F==="return"?(w.pull(),w.pull(),!0):F})}let u=g=>{let{a:m,x:w,y:F}=t;t.a=g,t.hooks.get(61898)(t),Object.assign(t,{a:m,x:w,y:F})},O=g=>{for(let m of g)u(m.charCodeAt(0))};function I(){t.i=1,t.sp=255,x.port=55,x.portDir=47;for(let m=2;m<1024;m++)(m<256||m>=512)&&(x.ram[m]=0);s(56333,127),s(56589,127),s(56320,127),s(56322,255),s(56323,0),s(56334,8),s(56335,8),s(56590,8),s(56591,8),s(56576,7),s(56578,63),s(56324,37),s(56325,64),s(56333,129),s(56334,17),s(642,8),s(644,160),s(643,0),s(641,0),s(648,4),s(646,14),s(649,10),s(651,4),s(652,10),s(650,0),s(655,72),s(656,235),s(245,129),s(246,235),s(678,1),s(154,3),s(153,0),s(157,128),s(204,1),s(205,20);let g=(m,w)=>{s(m,w),s(m+1,w>>8)};g(43,2049),g(45,2051),g(47,2051),g(49,2051),g(51,40960),g(55,40960),s(84,76),s(784,76),g(785,45640),[58251,42115,42364,42778,42980,44678].forEach((m,w)=>g(768+2*w,m)),g(3,45482),g(5,45969),[230,122,208,2,230,123,173,96,234,201,58,176,10,201,32,240,239,56,233,48,56,233,208,96,128,79,199,82,88].forEach((m,w)=>s(115+w,m)),s(83,3),s(104,0),s(19,0),s(24,0),s(509,1),s(508,1),s(22,25),x.ram[2048]=0,x.ram[2049]=0,x.ram[2050]=0,st.forEach((m,w)=>{s(788+2*w,m),s(789+2*w,m>>8)}),xt.forEach((m,w)=>s(53248+w,m)),t.hooks.get(65371)(t),O("\rREADY.\r")}let A=[];function D(){for(;;){if(t.hooks.get(61783)(t)==="wait")return"wait";if(t.a===13)break;if(A.push(t.a),A.length>=89)return A=[],t.x=23,t.pc=42039,!1}let g=A;A=[],g.forEach((v,q)=>s(512+q,v)),s(512+g.length,0),u(13);let m=0;for(;g[m]===32;)m++;if(m===g.length)return"wait";if(s(58,255),g[m]>=48&&g[m]<=57){let v=0;for(;m<g.length&&(g[m]===32||g[m]>=48&&g[m]<=57);m++)if(g[m]!==32){if(v>=6400)return t.x=11,t.pc=42039,!1;v=v*10+g[m]-48}let q=dt(x.ram,e(43)|e(44)<<8,v,_0(g.slice(m)));for(let i0 of[45,47,49])s(i0,q),s(i0+1,q>>8);return s(51,e(55)),s(52,e(56)),N?.clr(),C=null,"wait"}let w=String.fromCharCode(...g.slice(m)).trim();if(w==="RUN")return t.pc=43121,!1;let F=/^LOAD"([^"]*)"(?:,(\d+))?(?:,(\d+))?$/.exec(w);if(F){let[,v,q="1",i0="0"]=F;for(let z0=0;z0<v.length;z0++)s(512+z0,v.charCodeAt(z0));if(s(183,v.length),s(187,0),s(188,2),s(186,+q),s(185,+i0),s(184,1),O("\rSEARCHING FOR "+v),t.a=0,t.x=1,t.y=8,t.sp=247,s(504,119),s(505,225),x.load(),t.c)return O("\r?FILE NOT FOUND ERROR"),t.sp=255,O("\rREADY.\r"),"wait";let{a:Z,x:a0,y:Te}=t;return O("\rLOADING"),Object.assign(t,{a:Z,x:a0,y:Te,c:0}),!0}return _0(g.slice(m)).forEach((v,q)=>s(512+q,v)),J()}let V=null,N=null,K=[],G=null,U=!1,R=[],C=null,L=null,S=-1,$=0,Y=65536,W=Symbol("the processor goes on"),i=g=>m=>{Object.assign(t,{a:m.a,x:m.x,y:m.y}),t.hooks.get(g)(t),Object.assign(m,{a:t.a,x:t.x,y:t.y,c:t.c,z:t.z,n:t.n,v:t.v})},a={get mem(){return x.ram},read:e,write:s,a:0,x:0,y:0,c:0,z:0,n:0,v:0,pc:0,ended:!1,idle:0,polled:!1,inside:0,keys:[],delay:0,cycles:0,title:{seed:17},routines:{chrout:i(61898),chrin:i(61783),getin:i(61758),clrchn:i(62259),chkin:i(61966),chkout:i(62032),open:i(62282),close:i(62097),setnam:i(65469),settim:i(65499),setlfs:i(65466),load:g=>{Object.assign(t,{a:g.a,x:g.x,y:g.y}),x.load(),Object.assign(g,{a:t.a,x:t.x,y:t.y,c:t.c})},sys:(g,m,w)=>(s(122,w),s(123,w>>8),s(20,m),s(21,m>>8),t.push(167),t.push(173),t.pc=57648,W),"basic-load":(g,[m,w=1,F=0])=>{for(let v=0;v<m.length;v++)s(512+v,m.charCodeAt(v));if(s(183,m.length),s(187,0),s(188,2),s(186,w),s(185,F),s(184,0),Object.assign(g,{a:0,x:1,y:8}),g.routines.load(g),g.c)throw new j("?FILE NOT FOUND ERROR");F?g.pc=0:b()}}};function b(g=X0(x.ram,e(43)|e(44)<<8)){V=bt(g,{name:"program",source:"BASIC's text in memory",mem:x.ram,cost:0,render:()=>""}),R=[];let m=65535,w=0;for(let F of g)F.label!==void 0?(m=F.line??65535,F.label===Y&&(w=R.length)):R.push(m);return K=g.filter(F=>F.label===void 0),N&&(N.items=lt(x.ram,e(43)|e(44)<<8)),a.pc=0,a.ended=!1,w}function k(){N=new k0(a,{quiet:!0}),a.basic=N,b()}let T=null;function P(g,m){if(R[m]===65535)throw new j("?ILLEGAL DIRECT ERROR");if(!T){if(T={k:m,n:0,text:null,at:0,typed:[]},g.prompt!==null)for(let F of g.prompt)u(F.charCodeAt(0));O("? ")}let w=T;for(;;){if(w.text===null){for(;;){if(t.hooks.get(61783)(t)==="wait")return a.pc=m,t.pc=42926,"wait";if(t.a===13)break;if(w.typed.push(t.a),w.typed.length>=89)throw T=null,new j("?STRING TOO LONG ERROR")}if(u(13),w.text=w.typed,w.typed=[],w.at=0,w.fresh=!0,!w.text.length&&w.n===0){T=null,a.ended=!0,a.pc=m+1;return}}let F=w.text,v=()=>{for(;F[w.at]===32;)w.at++};if(w.n===g.targets.length){w.at<F.length&&O("?EXTRA IGNORED\r"),T=null,a.pc=m+1;return}if(w.n>0&&!w.fresh&&(w.at>=F.length||F[w.at]===58)){O("?? "),w.text=null;continue}!w.fresh&&F[w.at]===44&&w.at++,w.fresh=!1,v();let q=g.targets[w.n],i0=q.n.endsWith("$"),Z;if(i0){let a0="";if(F[w.at]===34){for(w.at++;w.at<F.length&&F[w.at]!==34;)a0+=String.fromCharCode(F[w.at++]);F[w.at]===34&&w.at++,v()}else for(;w.at<F.length&&F[w.at]!==44&&F[w.at]!==58;)a0+=String.fromCharCode(F[w.at++]);Z=a0}else{let a0=$0(String.fromCharCode(...F),w.at);w.at=a0.end,Z=m0(a0.text)}if(w.at<F.length&&F[w.at]!==44&&F[w.at]!==58)return O("?REDO FROM START\r"),T=null,P(g,m);q.k==="elem"?N.setElem(q.n,q.i.map(a0=>Y0(a0)(N)),Z):N.set(q.n,Z),w.n++}}function z(g){O("\rBREAK"),g!==void 0&&g>>8!==255&&O(" IN "+g)}function J(){N||(N=new k0(a,{quiet:!0}),a.basic=N);let g=ut(x.ram,e(43)|e(44)<<8,512);return g[g.findIndex(m=>m.label==="direct")].label=Y,a.pc=b(g),t.pc=42926,!1}a.routines["basic-list"]=(g,[m,w])=>{for(let F of G0(x.ram,e(43)|e(44)<<8))if(!(F.n<m)){if(u(13),F.n>w)break;O(F.n+" "),v0(F.bytes).forEach(v=>u(v))}},a.routines["basic-new"]=g=>{let m=e(43)|e(44)<<8;s(m,0),s(m+1,0);for(let w of[45,47,49])s(w,m+2),s(w+1,m+2>>8);s(51,e(55)),s(52,e(56)),N?.clr(),C=null,g.ended=!0};let f0=(g,m)=>{if(m!==8)throw m===1?new Error("the tape is not handled"):new j("?DEVICE NOT PRESENT ERROR");if(!g.length)throw new j("?MISSING FILE NAME ERROR");return Uint8Array.from(g,w=>w.charCodeAt(0))},e0=()=>e(58)===255;a.routines["basic-save"]=(g,[m="",w=1])=>{let F=f0(m,w),v=e(43)|e(44)<<8,q=e(45)|e(46)<<8;e0()&&O("\rSAVING "+m);let i0=new Uint8Array(q-v+2);i0[0]=v&255,i0[1]=v>>8;for(let Z=v;Z<q;Z++)i0[Z-v+2]=e(Z);x.drive.save(F,i0)},a.routines["basic-verify"]=(g,[m="",w=1,F=0])=>{let v=f0(m,w);e0()&&O("\rSEARCHING FOR "+m);let q=x.drive.load(v);if(!q||q.length<2)throw new j("?FILE NOT FOUND ERROR");e0()&&O("\rVERIFYING");let i0=F?q[0]|q[1]<<8:e(43)|e(44)<<8;for(let Z=2;Z<q.length;Z++)if(e(i0+Z-2)!==q[Z])throw new j("?VERIFY ERROR");O("\rOK\r")},a.routines["basic-cont"]=g=>{if(C===null)throw new j("?CAN'T CONTINUE ERROR");g.pc=C};function A0(){if(G){let{k:g,t:m}=G;G=null;let w=t.a,F=w?String.fromCharCode(w):"";N.set(m.n,m.n.endsWith("$")?F:w&&Number(F)||0),a.pc=g+1}for(let g=0;g<1e5;g++){if(x.basicPace&&(x.frame!==S&&(S=x.frame,$=x.basicPace),$--<=0))return"wait";let m=a.pc;if(a.ended||m>=V.steps.length)return a.ended&&/^(end|stop)$/.test(K[m-1]?.k)&&R[m-1]!==65535&&(C=m),a.ended&&K[m-1]?.k==="stop"&&z(R[m-1]),O("\rREADY.\r"),t.sp=255,t.pc=42112,!1;let w=K[m];if(R[m]!==void 0&&(s(57,R[m]),s(58,R[m]>>8)),e(145)===127&&L!==m)return s(198,0),C=m,z(R[m]),O("\rREADY.\r"),t.sp=255,t.pc=42112,!1;if(L=null,w.k==="input-keys"){let v=P(w,m);if(v!==void 0)return v;continue}if(w.k==="get")return s(19,0),G={k:m,t:w.t},t.push(167),t.push(173),t.pc=65508,!1;a.pc=m+1;let F;try{U=!0,F=V.run[m](a,V.steps)}catch(v){if(!(v instanceof j)||!Z0(E,v.message,v.basicLine??R[m]??N.at))throw v;return t.sp=255,t.pc=42112,!1}finally{U=!1}if(F===W)return!1;if(w.k==="wait"&&a.pc===m)return L=m,t.pc=42926,"wait"}return"wait"}}var ge=312,hx=63,lx=15,Dt=class{constructor(t,e,{log:s=()=>{},printer:f=new H0({on:!1})}={}){this.game=t,this.log=s,this.ram=new Uint8Array(65536),this.kernal=e.kernal,this.basic=e.basic,this.chargen=e.chargen,this.color=new Uint8Array(1024),this.port=55,this.portDir=47,this.cia1=new ft,this.cia2=new ft,this.sid=new Uint8Array(32),this.sidWrites=[],this.vic=new O0(this),this.keys=new Uint8Array(8),this.joystick=0,this.drive=new j0(Uint8Array.from(atob(t.image),r=>r.charCodeAt(0))),this.printer=f,this.halted=!1,this.frame=0,this.line=0,this.cpu=new le(this,{nmos:!0,writeBack:!0});let n=(r,h)=>!e.standIn&&this.read16(r)===h;this.cpu.hooks.set(62622,()=>n(816,62629)?!1:this.load()),this.cpu.hooks.set(62941,()=>n(818,62957)?!1:this.save());let o=this.bus=Ut(this.drive,this.printer),c=this.ram;this.cpu.hooks.set(60684,r=>(o.listen(r.a)||(c[144]|=128),!0)),this.cpu.hooks.set(60681,r=>(o.talk(r.a)||(c[144]|=128),!0)),this.cpu.hooks.set(60857,r=>(o.second(r.a),!0)),this.cpu.hooks.set(60871,r=>(o.tksa(r.a),!0)),this.cpu.hooks.set(60893,r=>(o.ciout(r.a),r.c=0,!0)),this.cpu.hooks.set(60926,()=>(o.unlsn(),!0)),this.cpu.hooks.set(60911,()=>(o.untlk(),!0)),this.cpu.hooks.set(60947,r=>{let h=o.acptr();return h?(r.a=h.byte,h.eoi&&(c[144]|=64)):(r.a=13,c[144]|=66),r.c=0,!0}),e.standIn&&Mt(this),this.reset()}reset(){this.port=55,this.portDir=47,this.cpu.pc=this.read16(65532),this.cpu.sp=253,this.cpu.i=1}read16(t){return this.read(t)|this.read(t+1)<<8}read(t){if(t===0)return this.portDir;if(t===1)return this.port&this.portDir|~this.portDir&23&255|16;let e=this.port&7;return t>=57344?e&2?this.rom(this.kernal,t):this.ram[t]:t>=53248&&t<57344?e&3&&e&4?this.io(t):e&3?this.chargen[t-53248]:this.ram[t]:t>=40960&&t<49152?(e&3)===3?this.rom(this.basic,t):this.ram[t]:this.ram[t]}rom(t,e){let s=e-(t===this.kernal?57344:40960);if(t.known&&!t.known[s])throw new Error(`the ${t===this.kernal?"KERNAL":"BASIC"} ROM at $${e.toString(16).toUpperCase()}: not handled`);return t[s]}write(t,e){if(t===0){this.portDir=e;return}if(t===1){this.port=e;return}let s=this.port&7;if(t>=53248&&t<57344&&s&3&&s&4){this.ioWrite(t,e);return}this.ram[t]=e}io(t){return t<54272?this.vic.read(t&63):t<55296?(t&31)===27?Math.random()*256|0:(t&31)===28?0:this.sid[t&31]:t<56320?this.color[t-55296]|240:t<56576?this.cia1.read(t&15,this):t<56832?this.cia2.read(t&15,this):255}ioWrite(t,e){t<54272?this.vic.write(t&63,e):t<55296?(this.sid[t&31]=e,this.sidWrites.push([this.cpu.cycles,t&31,e])):t<56320?this.color[t-55296]=e&15:t<56576?this.cia1.write(t&15,e):t<56832&&this.cia2.write(t&15,e)}keyboardRows(t){let e=255;for(let s=0;s<8;s++)t&1<<s||(e&=~this.keys[s]);return e&255}load(){let t=this.cpu,e=this.ram.subarray(this.ram[187]|this.ram[188]<<8,(this.ram[187]|this.ram[188]<<8)+this.ram[183]),s=this.ram[186],f=this.ram[185],n=String.fromCharCode(...e);if(s!==8){t.a=5,t.c=1;return}let o=this.drive.load(e);if(!o||o.length<2||t.a!==0){this.log("LOAD "+JSON.stringify(n)+(o?" verify":" not found")),t.a=4,t.c=1,this.ram[144]=66;return}let c=f===0?t.x|t.y<<8:o[0]|o[1]<<8,r=o.subarray(2);for(let p=0;p<r.length;p++)this.write(c+p,r[p]);let h=c+r.length;this.ram[174]=h&255,this.ram[175]=h>>8,this.ram[144]=0,t.x=h&255,t.y=h>>8,t.c=0,t.a=0,this.log("LOAD",JSON.stringify(n),"$"+c.toString(16),r.length+" bytes")}save(){let t=this.cpu,e=this.pstringAt(this.ram[187]|this.ram[188]<<8,this.ram[183]),s=this.ram[t.a]|this.ram[t.a+1]<<8,f=t.x|t.y<<8,n=new Uint8Array(f-s+2);n[0]=s&255,n[1]=s>>8;for(let o=s;o<f;o++)n[o-s+2]=this.read(o);this.drive.save(this.ram.subarray(this.ram[187]|this.ram[188]<<8,(this.ram[187]|this.ram[188]<<8)+this.ram[183]),n),this.log("SAVE",JSON.stringify(e),n.length+" bytes"),t.c=0,t.a=0}pstringAt(t,e){let s="";for(let f=0;f<e;f++)s+=String.fromCharCode(this.ram[t+f]);return s}key(t,e,s){s?this.keys[t]|=1<<e:this.keys[t]&=~(1<<e)}type(t){let e=Math.min(10,t.length);for(let s=0;s<e;s++)this.ram[631+s]=t.charCodeAt(s);return this.ram[198]=e,t.slice(e)}runLine(){let t=this.cpu.cycles+hx,e=this.cpu.cycles+lx,s=!1;for(;this.cpu.cycles<t&&!this.halted;){!s&&this.cpu.cycles>=e&&(this.vic.line(this.line),s=!0);let f=this.cpu.cycles,n=this.cpu.pc;(this.port&2)===0&&n>=57344&&this.cpu.hooks.has(n),this.cpu.step();let o=this.cpu.cycles-f;this.cia1.tick(o)&&this.cia1.icr&128&&this.cpu.irq(),this.cia2.tick(o);let c=(this.cia2.icr&128)!==0;c&&!this.nmiLine&&this.cpu.nmi(),this.nmiLine=c}s||this.vic.line(this.line),this.vic.irq()&&this.cpu.irq(),this.line=(this.line+1)%ge,this.line===0&&this.frame++}runFrame(){for(let t=0;t<ge;t++)this.runLine()}},ft=class{constructor(){this.ta=65535,this.taLatch=65535,this.tb=65535,this.tbLatch=65535,this.cra=0,this.crb=0,this.icr=0,this.mask=0,this.pra=255,this.prb=255,this.ddra=0,this.ddrb=0,this.tod=0}read(t,e){switch(t){case 0:return e&&this===e.cia1?(this.pra|~this.ddra)&~e.joystick&255:(this.pra|~this.ddra)&255;case 1:return e&&this===e.cia1?e.keyboardRows(this.pra|~this.ddra)&(this.prb|~this.ddrb):(this.prb|~this.ddrb)&255;case 2:return this.ddra;case 3:return this.ddrb;case 4:return this.ta&255;case 5:return this.ta>>8;case 6:return this.tb&255;case 7:return this.tb>>8;case 8:return 0;case 9:return 0;case 10:return 0;case 11:return 0;case 13:{let s=this.icr;return this.icr=0,s}case 14:return this.cra;case 15:return this.crb}return 0}write(t,e){switch(t){case 0:this.pra=e;break;case 1:this.prb=e;break;case 2:this.ddra=e;break;case 3:this.ddrb=e;break;case 4:this.taLatch=this.taLatch&65280|e;break;case 5:this.taLatch=this.taLatch&255|e<<8,this.cra&1||(this.ta=this.taLatch);break;case 6:this.tbLatch=this.tbLatch&65280|e;break;case 7:this.tbLatch=this.tbLatch&255|e<<8,this.crb&1||(this.tb=this.tbLatch);break;case 13:e&128?this.mask|=e&31:this.mask&=~e;break;case 14:this.cra=e,e&16&&(this.ta=this.taLatch);break;case 15:this.crb=e,e&16&&(this.tb=this.tbLatch);break}}tick(t){let e=!1;if(this.cra&1)for(this.ta-=t;this.ta<0;)this.ta+=this.taLatch+1,this.icr|=1,this.cra&8&&(this.cra&=-2,this.ta=this.taLatch),(this.crb&96)===64&&this.crb&1&&--this.tb<0&&(this.tb=this.tbLatch,this.icr|=2);if(this.crb&1&&(this.crb&96)===0)for(this.tb-=t;this.tb<0;)this.tb+=this.tbLatch+1,this.icr|=2,this.crb&8&&(this.crb&=-2,this.tb=this.tbLatch);return this.icr&this.mask&31&&(this.icr|=128,e=!0),e}};var ye={};X(ye,{paint:()=>vt});function vt(x,t,e,s,f){let n={ram:x,chargen:e??new Uint8Array(4096),color:t.subarray(2048,3072),cia2:{pra:t[3328]},line:0};s??=new O0(n),s.m=n,s.r.set(t.subarray(0,64));let o=f?.size?[...f].sort((r,h)=>r[0]-h[0]):[],c=(r,h)=>{r===256?n.cia2.pra=h:s.r[r]=h};if(o.length)for(let[r,h]of o.at(-1)[1])c(r,h);for(let r=0,h=0;r<312;r++){for(;h<o.length&&o[h][0]<r;){for(let[p,d]of o[h][1])c(p,d);h++}n.line=r,s.line(r)}return s.frame}var Ae={};X(Ae,{chip:()=>ux,rows:()=>dx});function ux({registers:x={},color:t=[],bank:e=0}={}){let s=new Uint8Array(4096);for(let[f,n]of Object.entries(x))s[+f]=n;return s.set(t.slice(0,1024),2048),s[3328]=3-(e>>14),s}function dx({mem:x,chip:t,chargen:e,crop:s=[0,0,320,200],scale:f=1}){let n=vt(x,t,e),[o,c,r,h]=s,p=[];for(let d=0;d<h;d++){let l=[];for(let y=0;y<r;y++){let E=V0[n[(wt+c+d)*_+d0+o+y]];for(let u=0;u<f;u++)l.push(E)}for(let y=0;y<f;y++)p.push(l)}return p}var we={};X(we,{keepDrive:()=>bx});function bx(x,t){for(let e of t.list())x.saved.set(e.name,t.read(e.name));x.keep=(e,s)=>t.write(e,s)}var Ee={};X(Ee,{SID:()=>Gt});var px=[2,8,16,24,38,56,68,80,100,250,500,800,1e3,3e3,5e3,8e3],Oe=[6,24,48,72,114,168,204,240,300,750,1500,2400,3e3,9e3,15e3,24e3],Gt=class{constructor(t){this.rate=t,this.r=new Uint8Array(32),this.v=[0,1,2].map(()=>({phase:0,env:0,stage:"off",noise:8388600,noiseOut:0}))}write(t,e){let s=this.r[t];if(this.r[t]=e,t%7===4&&t<21){let f=this.v[t/7|0];e&1&&!(s&1)?f.stage="attack":!(e&1)&&s&1&&(f.stage="release")}}render(t,e){let s=new Float32Array(t),f=0;for(let n=0;n<t;n++){for(;f<e.length&&e[f][0]<=n;)this.write(e[f][1],e[f][2]),f++;let o=0;for(let c=0;c<3;c++){let r=c*7,h=this.v[c],p=this.r,d=p[r]|p[r+1]<<8,l=p[r+4],y=px[p[r+5]>>4]/1e3*this.rate,E=Oe[p[r+5]&15]/1e3*this.rate,u=(p[r+6]>>4)/15,O=Oe[p[r+6]&15]/1e3*this.rate;if(h.stage==="attack"?(h.env+=1/Math.max(1,y),h.env>=1&&(h.env=1,h.stage="decay")):h.stage==="decay"?(h.env-=(1-u)/Math.max(1,E)*3,h.env<=u&&(h.env=u,h.stage="sustain")):h.stage==="release"&&(h.env-=1/Math.max(1,O)*3,h.env<=0&&(h.env=0,h.stage="off")),h.env<=0&&h.stage!=="attack")continue;let I=d*985248/16777216/this.rate,A=h.phase;h.phase=(h.phase+I)%1;let D=0;if(l&16&&(D+=h.phase<.5?h.phase*4-1:3-h.phase*4),l&32&&(D+=h.phase*2-1),l&64){let V=(p[r+2]|(p[r+3]&15)<<8)/4096;D+=h.phase<V?1:-1}if(l&128){if(h.phase<A){let V=(h.noise>>22^h.noise>>17)&1;h.noise=(h.noise<<1|V)&8388607,h.noiseOut=((h.noise>>22&1)<<7|(h.noise>>20&1)<<6|(h.noise>>16&1)<<5|(h.noise>>13&1)<<4|(h.noise>>11&1)<<3|(h.noise>>7&1)<<2|(h.noise>>4&1)<<1|h.noise>>2&1)/128-1}D+=h.noiseOut}o+=D*h.env}s[n]=o*(this.r[24]&15)/15*.2}return s}};var Re={};X(Re,{ROM_CHOICES:()=>mx,c64Roms:()=>gx,typed:()=>kx});var mx=[null,"commodore","moonbase-petscii"];async function gx(x,t,e){if(x!=="commodore"&&x!=="moonbase-petscii")return it();try{return x==="moonbase-petscii"?{...it(),chargen:await e("chargen")}:{kernal:await e("kernal"),basic:await e("basic"),chargen:await e("chargen")}}catch{return null}}function kx(x,t){let e=x.drive.find(Uint8Array.from(t.start,s=>s.charCodeAt(0)))?.raw;return'LOAD"'+t.start+'",8,1\r'+(e&&(e[0]|e[1]<<8)===2049?"RUN\r":"")}export{Kt as basic,zt as basicCompile,jt as basicLang,qt as basicList,Xt as basicRom,te as browser,xe as d64,ie as drive,ne as editor,Pt as float,re as glyphHash,ae as glyphHashes,he as kernal,_t as keys,ke as machine,Yt as numbers,ye as paint,Wt as petscii,Ae as picture,ue as printer,me as rom,we as saves,Ee as sid,Re as start,Zt as vic};
|
|
8
|
+
`),t(157,128),!0}function yt({cpu:x,read:t,write:e,ram:s}){let f=new l0({read:t,write:e}),n=new Map,o=r=>{let h=r.endsWith("%")?"%":r.endsWith("$")?"$":"",p=h?r.slice(0,-1):r,d=p.charCodeAt(0)|(h==="%"?128:0),l=(p.charCodeAt(1)||0)|(h?128:0),y=t(47)|t(48)<<8;for(let E=t(45)|t(46)<<8;E<y;E+=7){if(t(E)!==d||t(E+1)!==l)continue;let u=[2,3,4,5,6].map(O=>t(E+O));if(h==="%")return(u[0]<<8|u[1])<<16>>16;if(h==="$"){let O="",I=u[1]|u[2]<<8;for(let A=0;A<u[0];A++)O+=String.fromCharCode(t(I+A));return O}return nt(u)}throw new Error(`the variable ${r} not in BASIC's table: its making by PTRGET not handled`)},c=Object.assign(Object.create(k0.prototype),{get:o,rnd:()=>(x.hooks.get(57495)(x),f.fac())});return r=>{let{e:h,end:p}=S0(s,r),d=n.get(r);return d||n.set(r,d=Y0(h)),{value:d(c),end:p,place:h.k==="str"?h.at:void 0}}}var te={};X(te,{keyboard:()=>ze,pace:()=>qe,painter:()=>We});var _t={};X(_t,{CURSOR:()=>tt,JOYSTICK:()=>F0,MATRIX:()=>I0,MODIFIERS:()=>c0,SHIFTED:()=>At});var I0={Backspace:[0,0],Delete:[0,0],Enter:[0,1],F7:[0,3],F1:[0,4],F3:[0,5],F5:[0,6],3:[1,0],w:[1,1],a:[1,2],4:[1,3],z:[1,4],s:[1,5],e:[1,6],5:[2,0],r:[2,1],d:[2,2],6:[2,3],c:[2,4],f:[2,5],t:[2,6],x:[2,7],7:[3,0],y:[3,1],g:[3,2],8:[3,3],b:[3,4],h:[3,5],u:[3,6],v:[3,7],9:[4,0],i:[4,1],j:[4,2],0:[4,3],m:[4,4],k:[4,5],o:[4,6],n:[4,7],"+":[5,0],p:[5,1],l:[5,2],"-":[5,3],".":[5,4],":":[5,5],"@":[5,6],",":[5,7],"\xA3":[6,0],"*":[6,1],";":[6,2],Home:[6,3],"=":[6,5],"^":[6,6],"/":[6,7],1:[7,0],_:[7,1],2:[7,3]," ":[7,4],q:[7,6],Escape:[7,7]},At={"!":"1",'"':"2","#":"3",$:"4","%":"5","&":"6","'":"7","(":"8",")":"9","?":"/","<":",",">":".","[":":","]":";",F2:"F1",F4:"F3",F6:"F5",F8:"F7"},c0={Control:[7,5],Tab:[7,2]},F0={ArrowUp:1,ArrowDown:2,ArrowLeft:4,ArrowRight:8,Control:16,Shift:16,Alt:16},tt={ArrowDown:[0,7,0],ArrowRight:[0,2,0],ArrowUp:[0,7,1],ArrowLeft:[0,2,1]};var Zt={};X(Zt,{HEIGHT:()=>E0,LEFT:()=>d0,PALETTE:()=>V0,TOP:()=>wt,VIC:()=>O0,WIDTH:()=>_});var V0=[[0,0,0],[255,255,255],[136,0,0],[170,255,238],[204,68,204],[0,204,85],[0,0,170],[238,238,119],[221,136,85],[102,68,0],[255,119,119],[51,51,51],[119,119,119],[170,255,102],[0,136,255],[187,187,187]],_=384,E0=272,d0=32,wt=35,Jt=16,He=287,O0=class{constructor(t){this.m=t,this.r=new Uint8Array(64),this.r[17]=27,this.r[22]=200,this.r[24]=20,this.frame=new Uint8Array(_*E0),this.rasterIrq=!1,this.spriteHits=0,this.spriteBackground=0,this.inside=[0,_-1],this.priority=new Uint8Array(_)}read(t){if(t===17)return this.r[17]&127|this.m.line>>1&128;if(t===18)return this.m.line&255;if(t===25)return this.r[25]|112;if(t===30){let e=this.spriteHits;return this.spriteHits=0,e}if(t===31){let e=this.spriteBackground;return this.spriteBackground=0,e}return t>=32&&t<47?this.r[t]|240:this.r[t]}write(t,e){if(t===25){this.r[25]&=~(e&15);return}this.r[t]=e}irq(){let t=this.m.line,e=this.r[18]|(this.r[17]&128)<<1;return t===e&&!this.rasterIrq&&(this.rasterIrq=!0,this.r[25]|=1),t!==e&&(this.rasterIrq=!1),this.r[25]&this.r[26]&15?(this.r[25]|=128,!0):!1}bank(){return(~this.m.cia2.pra&3)<<14}fetch(t){let e=this.bank(),s=e|t&16383;return(s&28672)===4096&&!(e&16384)?this.m.chargen[s&4095]:this.m.ram[s]}line(t){if(t<Jt||t>He)return;let e=t-Jt,s=this.frame,f=e*_,n=this.r,o=n[32]&15,c=n[33]&15,r=n[17]&16,h=n[17]&8,p=n[22]&8,d=h?51:55,l=h?250:246,y=p?24:31,E=p?343:334,u=this.priority;u.fill(0);for(let L=0;L<_;L++)s[f+L]=o;if(!r||t<d||t>l){this.sprites(t,f,!0);return}let O=n[17]&7,I=n[22]&7,A=n[17]&32,D=n[17]&64,V=n[22]&16,N=(n[24]>>4&15)<<10,K=(n[24]>>1&7)<<11,G=(n[24]&8)<<10,U=t-48-O;if(U<0||U>=200){this.sprites(t,f,!1);return}let R=U>>3,C=U&7;for(let L=0;L<40;L++){let S=R*40+L,$=this.fetch(N+S),Y=this.m.color[S]&15,W,i;A?(W=this.fetch(G+S*8+C),i=V?[c,$>>4,$&15,Y]:[$&15,$>>4]):(W=this.fetch(K+((D?$&63:$)<<3)+C),D?i=[n[33+($>>6)]&15,Y]:V&&Y&8?i=[c,n[34]&15,n[35]&15,Y&7]:i=[c,Y]);let a=i.length===4&&!0,b=d0+I+L*8;if(a&&(A||V&&Y&8))for(let k=0;k<4;k++){let T=W>>6-k*2&3,P=i[T],z=b+k*2;z>=0&&z+1<_&&(s[f+z]=P,s[f+z+1]=P,T>=2&&(u[z]=1,u[z+1]=1))}else for(let k=0;k<8;k++){let T=W>>7-k&1,P=b+k;P>=0&&P<_&&(s[f+P]=T?i[1]:i[0],T&&(u[P]=1))}}for(let L=0;L<d0+y-24;L++)s[f+L]=o;for(let L=d0+E-24+1;L<_;L++)s[f+L]=o;this.inside=[d0+y-24,d0+E-24],this.sprites(t,f,!1)}sprites(t,e,s){let f=this.r,n=f[21];if(!n)return;let o=(f[24]>>4&15)<<10,c=this.frame,r=this.priority,h=new Uint8Array(_);for(let p=7;p>=0;p--){if(!(n&1<<p))continue;let d=f[p*2]|(f[16]>>p&1)<<8,l=f[p*2+1],y=f[23]&1<<p,E=f[29]&1<<p,u=y?42:21,O=t-l-1;if(O<0||O>=u)continue;let I=y?O>>1:O,A=this.fetch(o+1016+p),D=A<<6,V=f[28]&1<<p,N=f[27]&1<<p,K=f[39+p]&15,G=d0+d-24;for(let U=0;U<3;U++){let R=this.fetch(D+I*3+U);if(V)for(let C=0;C<4;C++){let L=R>>6-C*2&3;if(!L)continue;let S=L===1?f[37]&15:L===2?K:f[38]&15,$=(U*8+C*2)*(E?2:1);for(let Y=0;Y<(E?4:2);Y++)this.spritePixel(e,G+$+Y,S,p,N,h,s)}else for(let C=0;C<8;C++){if(!(R>>7-C&1))continue;let L=(U*8+C)*(E?2:1);for(let S=0;S<(E?2:1);S++)this.spritePixel(e,G+L+S,K,p,N,h,s)}}}}spritePixel(t,e,s,f,n,o,c){e<0||e>=_||(o[e]&&(this.spriteHits|=1<<f|1<<o[e]-1),o[e]=f+1,!(this.priority[e]&&(this.spriteBackground|=1<<f,n))&&(c||e<this.inside[0]||e>this.inside[1]||(this.frame[t+e]=s)))}};var et=.4;function ze(x,{ignore:t=()=>!1,onKey:e=()=>{},log:s=!1}={}){let f=()=>!((x.vic?.r[24]??21)&2),n=new Map,o=[],c=new Map,r=new Set,h=0,p=(u,O)=>{let I=h-(c.get(u)??-9);if(I>=2){x.key(u[0],u[1],!1),O&&x.key(1,7,!1);return}o.push([u[0],u[1],2-I]),O&&o.push([1,7,2-I])};window.addEventListener("keydown",u=>{if(t(u)||(e(u),u.metaKey||u.repeat))return;if(c0[u.key]&&(x.key(...c0[u.key],!0),u.preventDefault()),F0[u.key]){x.joystick|=F0[u.key];let K=tt[u.key];K&&(K[2]&&x.key(1,7,!0),x.key(K[0],K[1],!0)),u.preventDefault();return}let O=/^Key([A-Z])$/.exec(u.code)?.[1]?.toLowerCase(),I=O?null:At[u.key],A=O?I0[O]:I?I0[I]:I0[u.key]||I0[u.key.toLowerCase()];if(!A){c0[u.key]&&u.preventDefault();return}(O?f()?u.altKey:u.shiftKey:I||u.key.length===1&&u.key!==u.key.toLowerCase()&&!/[0-9]/.test(u.key)||u.shiftKey&&/^F[1357]$/.test(u.key))&&x.key(1,7,!0);let[D,V]=c0.Control,N=u.ctrlKey&&!(x.keys[D]&1<<V)?c0.Control:null;N&&(x.key(...N,!0),c.set(N,h)),x.key(A[0],A[1],!0),n.set(u.code,A),c.set(A,h),N&&r.add(u.code),s&&console.log("key",JSON.stringify(u.key),"-> column",A[0],"row",A[1]),u.preventDefault()}),window.addEventListener("keyup",u=>{if(c0[u.key]&&x.key(...c0[u.key],!1),F0[u.key]){x.joystick&=~F0[u.key];let I=tt[u.key];I&&(x.key(I[0],I[1],!1),I[2]&&x.key(1,7,!1));return}let O=n.get(u.code);O?(p(O,!0),r.delete(u.code)&&p(c0.Control,!1),n.delete(u.code)):x.key(1,7,!1)});let d=()=>{x.keys.fill(0),x.joystick=0,n.clear(),o.length=0,c.clear(),r.clear(),l=0,y=!1},l=0,y=!1,E=()=>{if(typeof navigator>"u"||typeof navigator.getGamepads!="function")return;let u=0,O=!1;for(let I of navigator.getGamepads()){if(!I||!I.connected)continue;let A=N=>!!I.buttons[N]?.pressed,D=I.axes[0]??0,V=I.axes[1]??0;(A(12)||V<-et)&&(u|=1),(A(13)||V>et)&&(u|=2),(A(14)||D<-et)&&(u|=4),(A(15)||D>et)&&(u|=8),(A(0)||A(1))&&(u|=16),A(9)&&(O=!0)}x.joystick=x.joystick&~l|u,l=u,O!==y&&(x.key(7,7,O),y=O)};return window.addEventListener("blur",d),document.addEventListener("visibilitychange",()=>{document.hidden&&d()}),{press(u,O,I=3){x.key(u,O,!0),o.push([u,O,I])},frame(){h++,E();for(let u=o.length-1;u>=0;u--)--o[u][2]<=0&&(x.key(o[u][0],o[u][1],!1),o.splice(u,1))}}}function We(x){let t=x.getContext("2d");x.width=_*2,x.height=E0*2;let e=new Uint8ClampedArray(_*2*E0*2*4),s=new ImageData(e,_*2,E0*2);return f=>{for(let n=0;n<E0;n++)for(let o=0;o<_;o++){let[c,r,h]=V0[f[n*_+o]];for(let p=0;p<2;p++){let d=((n*2+p)*_*2+o*2)*4;for(let l=0;l<2;l++)e[d++]=c,e[d++]=r,e[d++]=h,e[d++]=255}}t.putImageData(s,0,0)}}function qe(x,t,e=()=>{}){let s=0,f=performance.now();function n(){let o=Math.min(2,Math.floor((performance.now()-f)/20)-s);for(let c=0;c<o;c++)x.runFrame(),s++,e(s);t(s),requestAnimationFrame(n)}n()}var xe={};X(xe,{SECTORS:()=>B0,add:()=>Rt,make:()=>Ke,petsciiName:()=>Et,read:()=>Xe});var B0=[...Array(17).fill(21),...Array(7).fill(19),...Array(6).fill(18),...Array(5).fill(17)],ee={DEL:0,SEQ:1,PRG:2,USR:3,REL:4},h0=(x,t)=>(B0.slice(0,x-1).reduce((e,s)=>e+s,0)+t)*256,Ot=x=>{let t=new Uint8Array(16).fill(160);return[...String(x).toUpperCase()].slice(0,16).forEach((e,s)=>{t[s]=e.charCodeAt(0)&255}),t};function Ke({name:x="MOONBASE",id:t="MB",files:e=[]}={}){let s=new Uint8Array(174848),f=B0.map(d=>new Array(d).fill(!1)),n=(d,l)=>(f[d-1][l]=!0,h0(d,l));n(18,0);let o=[];for(let d=1;d<=17;d++)18-d>=1&&o.push(18-d),18+d<=35&&o.push(18+d);let c=()=>{for(let d of o){let l=B0[d-1],y=f[d-1].lastTaken??-10;for(let E=0;E<l;E++){let u=(y+10+E)%l;if(!f[d-1][u])return f[d-1][u]=!0,f[d-1].lastTaken=u,[d,u]}}throw new Error("the disk is full")},r=e.map(d=>{let l=d.data instanceof Uint8Array?d.data:Uint8Array.from(d.data),y=Math.max(1,Math.ceil(l.length/254)),E=Array.from({length:y},c);return E.forEach(([u,O],I)=>{let A=n(u,O),D=l.subarray(I*254,(I+1)*254);I+1<y?(s[A]=E[I+1][0],s[A+1]=E[I+1][1]):(s[A]=0,s[A+1]=D.length+1),s.set(D,A+2)}),{name:Ot(d.name),type:128|(ee[d.type??"PRG"]??2),first:E[0],blocks:y}}),h=[];for(let d=0,l=1;d<Math.max(1,Math.ceil(r.length/8));d++,l=(l+3)%19){for(;f[17][l];)l=(l+1)%19;if(h.length>=18)throw new Error("the directory is full: 144 files");n(18,l),h.push(l)}h.forEach((d,l)=>{let y=h0(18,d);l+1<h.length?(s[y]=18,s[y+1]=h[l+1]):(s[y]=0,s[y+1]=255),r.slice(l*8,l*8+8).forEach((E,u)=>{let O=y+u*32;s[O+2]=E.type,s[O+3]=E.first[0],s[O+4]=E.first[1],s.set(E.name,O+5),s[O+30]=E.blocks&255,s[O+31]=E.blocks>>8})});let p=h0(18,0);s[p]=18,s[p+1]=h[0],s[p+2]=65;for(let d=1;d<=35;d++){let l=f[d-1].map(E=>!E),y=p+4*d;s[y]=l.filter(Boolean).length;for(let E=0;E<l.length;E++)l[E]&&(s[y+1+(E>>3)]|=1<<(E&7))}return s.set(Ot(x),p+144),s[p+160]=s[p+161]=160,s[p+162]=String(t).charCodeAt(0)||48,s[p+163]=String(t).charCodeAt(1)||48,s[p+164]=160,s[p+165]=50,s[p+166]=65,s.fill(160,p+167,p+171),s}function Rt(x,t){let e=Uint8Array.from(x),s=h0(18,0),f=(p,d)=>!!(e[s+4*p+1+(d>>3)]&1<<(d&7)),n=(p,d,l)=>{let y=s+4*p+1+(d>>3),E=f(p,d);l?e[y]|=1<<(d&7):e[y]&=~(1<<(d&7)),e[s+4*p]+=l&&!E?1:!l&&E?-1:0},o=[];for(let p=1;p<=17;p++)18-p>=1&&o.push(18-p),18+p<=35&&o.push(18+p);let c={},r=()=>{for(let p of o){let d=B0[p-1];for(let l=0;l<d;l++){let y=((c[p]??-10)+10+l)%d;if(f(p,y))return n(p,y,!1),c[p]=y,[p,y]}}throw new Error("the disk is full")},h=()=>{let p=[];for(let[d,l]=[e[s],e[s+1]],y=0;d&&y<18;y++){let E=h0(d,l);for(let u=0;u<8;u++)p.push(E+u*32);[d,l]=[e[E],e[E+1]]}return p};for(let p of t){let d=Ot(p.name),l=p.data instanceof Uint8Array?p.data:Uint8Array.from(p.data);for(let O of h())if(!(!(e[O+2]&7)||!d.every((I,A)=>e[O+5+A]===I))){for(let[I,A]=[e[O+3],e[O+4]],D=0;I&&D<683;D++){let V=h0(I,A);n(I,A,!0),[I,A]=[e[V],e[V+1]]}e[O+2]=0}let y=Math.max(1,Math.ceil(l.length/254)),E=Array.from({length:y},r);E.forEach(([O,I],A)=>{let D=h0(O,I),V=l.subarray(A*254,(A+1)*254);e.fill(0,D,D+256),A+1<y?(e[D]=E[A+1][0],e[D+1]=E[A+1][1]):(e[D]=0,e[D+1]=V.length+1),e.set(V,D+2)});let u=h().find(O=>!(e[O+2]&7)&&!e[O+2]);if(u===void 0){let O=h(),I=O[O.length-1]-224,A=1;for(;A<19&&!f(18,A);)A++;if(A>=19)throw new Error("the directory is full");n(18,A,!1);let D=h0(18,A);e.fill(0,D,D+256),e[D+1]=255,e[I]=18,e[I+1]=A,u=D}e.fill(0,u+2,u+32),e[u+2]=128|(ee[p.type??"PRG"]??2),e[u+3]=E[0][0],e[u+4]=E[0][1],e.set(d,u+5),e[u+30]=y&255,e[u+31]=y>>8}return e}var Qe=["DEL","SEQ","PRG","USR","REL"],Et=x=>String.fromCharCode(...[...x].filter(t=>t!==160).map(t=>t>=193&&t<=218?t-128:t)).trim();function Xe(x){if(x.length!==174848&&x.length!==175531)throw new Error("a .d64 is 174,848 bytes, and this one is "+x.length);let t=(c,r)=>x.subarray(h0(c,r),h0(c,r)+256),e=(c,r)=>{let h=[];for(;c;){let d=t(c,r);[c,r]=[d[0],d[1]],h.push(d.subarray(2,c?256:r+1))}let p=new Uint8Array(h.reduce((d,l)=>d+l.length,0));return h.reduce((d,l)=>(p.set(l,d),d+l.length),0),p},s=t(18,0),f=Et(s.subarray(144,160)),n=String.fromCharCode(s[162],s[163]),o=[];for(let[c,r]=[s[0],s[1]];c;){let h=t(c,r);for(let p=0;p<8;p++){let d=h.subarray(p*32,(p+1)*32);if(!d[2])continue;let l=Qe[d[2]&7]||"?",y=e(d[3],d[4]),E={name:Et(d.subarray(5,21)),type:l,locked:!!(d[2]&64),closed:!!(d[2]&128),blocks:d[30]|d[31]<<8,body:y};l==="PRG"&&y.length>=2&&(E.address=y[0]|y[1]<<8,E.body=y.subarray(2),E.raw=y),o.push(E)}[c,r]=[h[0],h[1]]}return{name:f,id:n,files:o}}var ie={};X(ie,{Drive:()=>j0});var Tt=[...Array(17).fill(21),...Array(7).fill(19),...Array(6).fill(18),...Array(5).fill(17)],se=["DEL","SEQ","PRG","USR","REL"],y0="00, OK,00,00\r",_e="62,FILE NOT FOUND,00,00\r",Je="31,SYNTAX ERROR,00,00\r",s0=x=>Uint8Array.from(x,t=>t.charCodeAt(0)),Ct=x=>x>=193&&x<=218?x-128:x,j0=class x{constructor(t){this.image=t,this.mem=new Uint8Array(2048),this.saved=new Map,this.keep=null,this.channels=new Map,this.listening=!1,this.talking=!1,this.channel=0,this.opening=null,this.status=s0(y0),this.reply=null,this.pos=0,this.channels.set(15,{command:[]})}sector(t,e,s=this.image){let f=Tt.slice(0,t-1).reduce((n,o)=>n+o,0)+e;return s.subarray(f*256,(f+1)*256)}chain(t,e){let s=[],f=0;for(;t&&f++<800;){let c=this.sector(t,e);[t,e]=[c[0],c[1]],s.push(c.subarray(2,t?256:e+1))}let n=new Uint8Array(s.reduce((c,r)=>c+r.length,0)),o=0;for(let c of s)n.set(c,o),o+=c.length;return n}directory(){let t=this.sector(18,0),e=[];for(let[s,f]=[t[0],t[1]],n=0;s&&n++<40;){let o=this.sector(s,f);for(let c=0;c<8;c++){let r=o.subarray(c*32,(c+1)*32);r[2]&&e.push({name:Array.from(r.subarray(5,21)).filter(h=>h!==160).map(Ct),type:se[r[2]&7]||"?",track:r[3],sector:r[4]})}[s,f]=[o[0],o[1]]}return e}loadable(){return this.directory().filter(t=>t.type!=="DEL")}static matches(t,e){for(let s=0;s<t.length;s++){if(t[s]===42)return!0;if(s>=e.length||t[s]!==63&&Ct(t[s])!==e[s])return!1}return t.length===e.length}static strip(t){let e=Array.from(t).map(Ct);return e[0]===64&&(e=e.slice(1)),e[1]===58&&(e=e.slice(2)),e}find(t){t=x.strip(t);for(let[e,s]of this.saved)if(x.matches(t,Array.from(s0(e))))return{raw:s,saved:!0};for(let e of this.loadable())if(x.matches(t,e.name))return{...e,raw:this.chain(e.track,e.sector)}}listing(){let t=this.saved.size?Rt(this.image,[...this.saved].map(([c,r])=>({name:c,data:r}))):this.image,e=(c,r)=>this.sector(c,r,t),s=e(18,0),f=[1,4],n=(c,r)=>f.push(1,1,c&255,c>>8,...r,0);n(0,[18,34,...s.subarray(144,160),34,32,s[162],s[163],32,s[165],s[166]]);for(let[c,r]=[s[0],s[1]],h=0;c&&h++<40;){let p=e(c,r);for(let d=0;d<8;d++){let l=p.subarray(d*32,(d+1)*32);if(!l[2])continue;let y=l[30]|l[31]<<8,E=Array.from(l.subarray(5,21)),u=E.indexOf(160),O=u<0?E:E.slice(0,u),I=y<10?3:y<100?2:1;n(y,[...Array(I).fill(32),34,...O,34,...Array(16-O.length).fill(32),l[2]&128?32:42,...s0(se[l[2]&7]||"???"),l[2]&64?60:32])}[c,r]=[p[0],p[1]]}let o=0;for(let c=1;c<=35;c++)c!==18&&(o+=s[4*c]);return n(o,[...s0("BLOCKS FREE."),...Array(13).fill(32)]),f.push(0,0),Uint8Array.from(f)}remember(t){t&&t.track&&(this.mem[126]=t.track,this.mem[622]=0,this.mem[623]=t.sector)}load(t){t=x.strip(t);let e;return t[0]===36?(this.status=s0(y0),this.listing()):(t.length===1&&t[0]===42?e=this.mem[126]?{raw:this.chain(this.mem[126],this.mem[623])}:{...this.loadable()[0],raw:null}:e=this.find(t),e&&!e.raw&&(e.raw=this.chain(e.track,e.sector)),this.remember(e),this.status=s0(e?y0:_e),e&&e.raw)}save(t,e){this.store(String.fromCharCode(...x.strip(t)),e),this.status=s0(y0)}store(t,e){this.saved.set(t,e),this.keep?.(t,e)}listen(t){return this.listening=t===8,this.listening}talk(t){return this.talking=t===8,this.talking}second(t){if(!this.listening)return;let e=t&15,s=t&240;s===240?this.opening={ch:e,name:[]}:s===224?this.close(e):this.channel=e}tksa(t){this.talking&&(this.channel=t&15,this.pos=0)}ciout(t){if(!this.listening)return;if(this.opening){this.opening.name.push(t);return}let e=this.channels.get(this.channel);e&&(e.command?e.command.push(t):e.write?e.write.push(t):e.buffer&&(e.buffer[e.pos]=t,e.pos=e.pos+1&255))}unlsn(){if(this.opening)this.open(this.opening.ch,this.opening.name),this.opening=null;else if(this.channel===15){let t=this.channels.get(15);t.command.length&&(this.execute(Uint8Array.from(t.command)),t.command=[])}this.listening=!1}untlk(){this.talking=!1}acptr(){let t=this.channels.get(this.channel);if(this.channel===15){let e=this.reply||this.status;if(this.pos>=e.length)return this.reply=null,this.pos=0,null;let s=e[this.pos++],f=this.pos>=e.length;return f&&(this.reply?this.reply=null:this.status=s0(y0),this.pos=0),{byte:s,eoi:f}}if(!t)return null;if(t.buffer){let e=t.buffer[t.pos];return t.pos=t.pos+1&255,{byte:e,eoi:t.pos===0}}return!t.data||t.pos>=t.data.length?null:{byte:t.data[t.pos++],eoi:t.pos>=t.data.length}}open(t,e){if(t===15){e.length&&this.execute(Uint8Array.from(e));return}if(e[0]===35){this.channels.set(t,{buffer:new Uint8Array(256),pos:0}),this.status=s0(y0);return}let s=[],f=[];for(let c of e)c===44?(s.push(f),f=[]):f.push(c);if(s.push(f),(t===1||t!==0&&s.slice(1).some(c=>c[0]===87)?"W":"R")==="W"){this.channels.set(t,{write:[],name:String.fromCharCode(...x.strip(s[0]))}),this.status=s0(y0);return}let o=this.load(s[0]);this.channels.set(t,{data:o??new Uint8Array(0),pos:0})}close(t){let e=this.channels.get(t);e&&e.write&&this.store(e.name,Uint8Array.from(e.write)),t!==15&&this.channels.delete(t)}execute(t){this.mem.set(t.subarray(0,42),512);let e=String.fromCharCode(...t);if(this.status=s0(y0),this.reply=null,e.startsWith("M-W")){let f=t[3]|t[4]<<8,n=t[5];for(let o=0;o<n&&6+o<t.length;o++)this.mem[f+o&2047]=t[6+o];return}if(e.startsWith("M-R")){let f=t[3]|t[4]<<8,n=t.length>5?t[5]||256:1;this.reply=this.mem.slice(f&2047,(f&2047)+n),this.pos=0;return}if(e.startsWith("M-E")){let f=t.subarray(5);for(let n=0;n+8<=f.length;n++)if(f[n]===162&&f[n+2]===160&&f[n+4]===134&&f[n+5]===6&&f[n+6]===132&&f[n+7]===7){let o=f[n+1],c=f[n+3];o>=1&&o<=35&&c<Tt[o-1]&&this.mem.set(this.sector(o,c),768);return}return}let s=/^(U1|UA|B-R|B-P)[: ]?\s*(.*)$/.exec(e.replace(/\r$/,""));if(s){let f=s[2].split(/[\s,]+/).filter(Boolean).map(Number),n=this.channels.get(f[0]);if(!n||!n.buffer){this.status=s0("70,NO CHANNEL,00,00\r");return}if(s[1]==="B-P"){n.pos=f[1]&255;return}let[,,o,c]=f;o>=1&&o<=35&&c<Tt[o-1]?(n.buffer.set(this.sector(o,c)),n.pos=0):this.status=s0("66,ILLEGAL TRACK OR SECTOR,"+String(o).padStart(2,"0")+","+String(c).padStart(2,"0")+"\r");return}/^I/.test(e)||(this.status=s0(Je))}};var ne={};X(ne,{editor:()=>St});var fe=[144,5,28,159,156,30,31,158,129,149,150,151,152,153,154,155];function St({read:x,write:t,keyRow:e=()=>255}){let s=(i,a)=>t(i,a&255),f=i=>i*40&255,n=i=>{s(209,f(i)),s(210,x(217+i)&3|x(648))},o=()=>{s(243,x(209)),s(244,x(210)&3|216)},c=()=>x(209)|x(210)<<8,r=()=>x(243)|x(244)<<8;function h(i){n(i),o();for(let a=39;a>=0;a--)t(r()+a,x(646)),t(c()+a,32)}function p(){let i=x(214),a=x(211);for(;!(x(217+i)&128)&&(a=a+40&255,s(211,a),i--,!(i<0)););n(i&255);let b=39;for(i++;!(x(217+i)&128)&&(b+=40,i++,!(i>127)););s(213,b),o()}function d(){let i=x(648)|128,a=0;for(let b=0;b<26;b++)s(217+b,i),a+=40,a>255&&(a&=255,i++);s(243,255);for(let b=24;b>=0;b--)h(b);l()}function l(){s(211,0),s(214,0),p()}function y(){let i=[172,173,174,175].map(x);for(;;){s(214,x(214)-1),s(201,x(201)-1),s(677,x(677)-1);let a;for(a=0;a<24;a++)n(a),E(f(a+1),x(218+a));n(a),h(24);for(let b=0;b<24;b++)s(217+b,x(217+b)&127|x(218+b)&128);if(s(241,x(241)|128),x(217)&128)break}s(214,x(214)+1),s(677,x(677)+1),e(127)===251&&s(198,0),[172,173,174,175].forEach((a,b)=>s(a,i[b]))}function E(i,a){let b=i|(a&3|x(648))<<8,k=i|(a&3|216)<<8;s(172,i),s(173,b>>8),o(),s(174,i),s(175,k>>8);for(let T=39;T>=0;T--)t(c()+T,x(b+T)),t(r()+T,x(k+T))}function u(i=x(214)+1){let a=i;for(;!(x(217+a)&128);)a++;if(s(677,a),a>24)return y(),a=x(677)-1,s(214,x(214)-1),O(a);let b=[172,173,174,175].map(x);for(let k=24;k>x(677);k--)n(k),E(f(k-1),x(216+k));n(Math.max(x(677),0)),h(x(677));for(let k=23;k>=x(677)&&k>0;k--)s(218+k,x(218+k)&127|x(217+k)&128);O(x(677)),[172,173,174,175].forEach((k,T)=>s(k,b[T]))}function O(i){s(217+i,x(217+i)&127),s(217+i+1,x(217+i+1)|128),s(213,x(213)+40);let a=i;for(;!(x(217+a)&128)&&a>0;)a--;n(a)}function I(){s(201,x(201)>>1);let i=x(214);do i++,i===25&&(y(),i=x(214));while(!(x(217+i)&128));s(214,i),p()}function A(){s(216,0),s(199,0),s(212,0),s(211,0),I()}function D(){for(let i of[39,79])if(x(211)===i){x(214)!==25&&s(214,x(214)+1);return}}function V(){for(let i of[0,40])if(x(211)===i){s(214,x(214)-1);return}}function N(i,a){s(205,2),o(),t(c()+x(211),i),t(r()+x(211),a)}function K(){let i=x(214);if(D(),s(211,x(211)+1),x(213)>=x(211))return;if(x(213)===79){s(214,x(214)-1),I(),s(211,0);return}if(x(658))return u(i+1);let a=x(214);a>=25&&(y(),s(214,x(214)-1),a=x(214)),O(a)}function G(i){x(199)&&(i|=128),x(216)&&s(216,x(216)-1),N(i,x(646)),K()}function U(){x(216)&&s(212,x(212)>>1)}function R(){let i=x(214);return i?(s(214,i-1),p(),s(211,x(213)),!0):(s(211,0),!1)}function C(i){if(s(215,i),s(208,0),i&128)return L(i);if(i===13)return A(),U();if(i>=32){let b=i<96?i&63:i&223;return i===34&&s(212,x(212)^1),G(b),U()}if(x(216))return G(i|128),U();if(i===20){if(x(211)===0){if(!R())return U()}else{V(),s(211,x(211)-1),o();for(let b=x(211);b+1!==x(213)+1&&b<x(213);b++)t(c()+b,x(c()+b+1)),t(r()+b,x(r()+b+1))}return t(c()+x(213),32),t(r()+x(213),x(646)),U()}if(x(212))return G(i|128),U();if(i===18&&s(199,18),i===19&&l(),i===29){let b=x(211)+1;return D(),s(211,b),b-1>=x(213)&&(s(214,x(214)-1),I(),s(211,0)),U()}if(i===17){let b=x(211)+40;if(s(214,x(214)+1),b>x(213)){s(214,x(214)-1);do b-=40,b>=0&&s(211,b);while(b>0&&b>=40);I()}else s(211,b);return U()}let a=fe.indexOf(i);return a>=0&&s(646,a),i===14&&t(53272,x(53272)|2),i===8&&s(657,128),i===9&&s(657,0),U()}function L(i){if(i&=127,i===127&&(i=94),i>=32)return G(i|64),U();if(i===13)return A(),U();if(x(212)||i!==20&&x(216))return G(i|64|128),U();if(i===20){let b=x(213);if(!(x(c()+b)===32&&b!==x(211))){if(b===79)return U();u()}for(b=x(213),o();b>x(211);b--)t(c()+b,x(c()+b-1)),t(r()+b,x(r()+b-1));return t(c()+b,32),t(r()+b,x(646)),s(216,x(216)+1),U()}if(i===17){let b=x(214);if(b){s(214,b-1);let k=x(211)-40;k>=0?s(211,k):p()}return U()}if(i===18&&s(199,0),i===29)return x(211)===0?R():(V(),s(211,x(211)-1)),U();if(i===19)return d(),U();let a=fe.indexOf(i|128);return a>=0&&s(646,a),i===14&&t(53272,x(53272)&-3),U()}function S(){s(202,x(211)),s(201,x(214))}function $(){let i=x(631);for(let a=0;a<x(198)-1;a++)s(631+a,x(632+a));return s(198,x(198)-1),i}function Y(){if(!x(208))for(;;){let k=x(198);if(s(204,k),s(658,k),!k)return null;x(207)&&(s(207,0),s(205,2),o(),t(c()+x(211),x(206)),t(r()+x(211),x(647)));let T=$();if(T===131){s(198,9),[76,79,65,68,13,82,85,78,13].forEach((z,J)=>s(631+J,z));continue}if(T!==13){C(T);continue}let P=x(213);for(s(208,P);x(c()+P)===32&&--P;);if(s(200,P+1),s(658,0),s(211,0),s(212,0),!(x(201)&128)){let z=x(214);if(z!==x(201)){for(;!(x(217+z)&128)&&z;)z--;n(z)}if(z===x(201)&&(s(211,x(202)),x(202)>=x(200)))return W()}break}let i=x(211),a=x(c()+i),b=a&63;return a&64&&(b|=128),!(a&128&&x(212))&&!(a&32)&&(b|=64),s(211,i+1),b===34&&s(212,x(212)^1),i===x(200)?W():(s(215,b),b===222?255:b)}function W(){return s(208,0),(x(153)===3||x(154)!==3)&&C(13),s(215,13),13}return{chrout:C,clear:d,home:l,setLine:p,clearLine:h,scroll:y,linePointer:n,colourPointer:o,begin:S,input:Y}}var re={};X(re,{glyphHash:()=>Ze});function Ze(x){let t=2166136261;for(let e=0;e<8;e++)t^=x[e],t=Math.imul(t,16777619)>>>0;return t}var ae={};X(ae,{GLYPH_HASHES:()=>tx});var tx=Uint32Array.of(2788871881,3484888503,3246991681,3624609685,453335123,1933125101,3077548787,2485223133,3877308795,3432987389,2981056711,1872599573,1643867547,1179332908,2247513867,2517044099,3094798189,3185123891,1385555207,852155623,1305655403,3768945513,3104403535,172795524,151750749,958039455,520025719,998705349,604089761,3595948113,2979418607,3944112791,2615243109,3434190861,1980558771,1675726591,43263885,2750147941,1233223601,4068015871,2558456709,1388364817,2582067888,3201647787,1929597349,2477030683,1661155445,767913286,4126389003,3192125595,2919328883,1388120033,3069637630,4281928141,1433572807,2917230913,4042988033,23194427,1680998453,2219162189,1896240069,206115429,3517826675,3008015341,3070282263,574191955,1008328549,3070282263,3197812635,4268266599,3339900715,267615717,4025240901,2468127309,1645230771,4202881437,1240450747,2802247017,3429822949,139144843,2788675629,4100334877,4246080071,4226633506,507226597,62691803,2473932793,2356380797,502307177,1567189477,100162740,3999692135,2749038821,1008328549,3586085438,4127708279,2615243109,2582733285,3025216001,3892497018,1289914376,1491276389,2998968857,1176470573,486888467,3544142820,1176470573,3226581351,1478355617,506322047,922277773,1280307259,2734711375,1279432831,886906767,1781439333,1491276389,4115828709,2118204077,621133707,690878844,34897702,3404710277,1615110309,2483765857,161469789,3762642597,2625755105,3568540537,3700270475,1401725133,4229510277,223371831,1692511041,1693599947,3531295949,4097784435,3527471925,2598774295,1127039233,4072743631,3279545972,2228721123,3008149239,3556571053,3598981159,288175123,2624662271,3292225379,2720560613,531943631,549462116,3212481081,2640094591,4041105511,3145394397,3294299081,3803506729,1114461399,2807665807,1823345245,3940892309,2400498843,2943953607,2583855265,1600820257,261986117,821172103,3363790221,4102991185,1527536824,666862203,4085848461,617022555,59323069,4105358430,3702878111,892881135,3840200543,4070938477,3628736310,2403520025,3873452447,3432863805,1617024269,1957258959,1636585133,835202373,3350047897,1478118129,2039306899,3533524333,3810128655,3685453475,1401108333,3810128655,3747807783,2841555295,3443588247,3604414269,3922001261,502616453,464003347,1564178357,639042183,3798018505,3228952373,3155195831,1959305101,3892720933,3466729791,4177298282,314954333,2583995099,2265111153,3440879813,3669217341,3527045533,1332417832,4144417359,3486903389,1401108333,3644333762,3356726655,1823345245,1084481853,2227149249,905105778,3450774288,1791622109,4143158105,990627269,2459051247,2790306112,990627269,616548367,1761986145,3863348887,1739784821,3881622407,3829722775,3096044247,1062015511,527219373,1791622109,4073289565,657495013,2887613495,2129402996,2208511534,2020044237,827865245,3992838977,2738655045,626497277,565138177,2788871881,1732001465,3394772477,2638126053,867869333,757064797,3427580885,676599287,3863560071,2872764249,1926724151,2926798603,1987369905,3977488583,1472896809,3531648339,4104394829,2455026901,2648202479,1637458909,3045183589,2874071771,711513647,1934610232,1987004669,2552993337,851174649,998705349,604089761,3595948113,2979418607,3944112791,2615243109,3434190861,1980558771,1675726591,43263885,2750147941,1233223601,4068015871,2558456709,1388364817,2582067888,3201647787,1929597349,2477030683,1661155445,767913286,4126389003,3192125595,2919328883,1388120033,3069637630,4281928141,1433572807,2917230913,4042988033,23194427,1680998453,2219162189,1896240069,206115429,3517826675,3008015341,3070282263,3484888503,3246991681,3624609685,453335123,1933125101,3077548787,2485223133,3877308795,3432987389,2981056711,1872599573,1643867547,1179332908,2247513867,2517044099,3094798189,3185123891,1385555207,852155623,1305655403,3768945513,3104403535,172795524,151750749,958039455,520025719,3999692135,2749038821,1008328549,4143158105,2678619321,2615243109,2582733285,3025216001,3892497018,1289914376,1491276389,2998968857,1176470573,486888467,1057150249,1176470573,3226581351,1478355617,506322047,922277773,1280307259,2734711375,1279432831,886906767,1781439333,1491276389,4115828709,2118204077,621133707,690878844,34897702,4015435567,1615110309,2483765857,161469789,3762642597,2625755105,3568540537,1901009401,2874680809,779276517,4022933181,4029064749,2040122065,3519314459,2878917951,1253589729,2183500875,2097480327,9711401,3148018267,4273879737,3218028591,2398275789,2613323033,3363024487,1320020493,3203018197,2674282647,2085422203,2232165276,1116765149,731252793,3981948569,3145394397,3294299081,3803506729,1114461399,2807665807,1823345245,3940892309,2400498843,2943953607,2583855265,1600820257,261986117,821172103,3363790221,4102991185,1527536824,666862203,4085848461,617022555,59323069,4105358430,3702878111,892881135,3840200543,4070938477,3628736310,2403520025,3873452447,3432863805,1617024269,1957258959,1636585133,835202373,3350047897,1478118129,2039306899,3533524333,3810128655,3700270475,1401725133,4229510277,223371831,1692511041,1693599947,3531295949,4097784435,3527471925,2598774295,1127039233,4072743631,3279545972,2228721123,3008149239,3556571053,3598981159,288175123,2624662271,3292225379,2720560613,531943631,549462116,3212481081,2640094591,4041105511,4144417359,3486903389,1401108333,2998968857,3570429753,1823345245,1084481853,2227149249,905105778,3450774288,1791622109,4143158105,990627269,2459051247,1076661217,990627269,616548367,1761986145,3863348887,1739784821,3881622407,3829722775,3096044247,1062015511,527219373,1791622109,4073289565,657495013,2887613495,2129402996,2208511534,2263771151,827865245,3992838977,2738655045,626497277,565138177);var he={};X(he,{KEY_TABLES:()=>It,VECTORS:()=>st,VIC:()=>xt,install:()=>Nt,kernalByte:()=>Lt,matrixOf:()=>ix,screenCode:()=>Ft});var xt=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,155,55,0,0,0,8,0,20,15,0,0,0,0,0,0,14,6,1,2,3,4,0,1,2,3,4,5,6,7,76],st=[59953,65126,65095,62282,62097,61966,62032,62259,61783,61898,63213,61758,62255,65126,62629,62957];function Ft(x){return x>=32&&x<64?x:x>=64&&x<96?x-64:x>=96&&x<128?x-32:x>=160&&x<192?x-64:x>=192&&x<255?x-128:x===255?94:32}var oe=[20,13,29,136,133,134,135,17,51,87,65,52,90,83,69,1,53,82,68,54,67,70,84,88,55,89,71,56,66,72,85,86,57,73,74,48,77,75,79,78,43,80,76,45,46,58,64,44,92,42,59,19,1,61,94,47,49,95,4,50,32,2,81,3],ex=[148,141,157,140,137,138,139,145,35,215,193,36,218,211,197,1,37,210,196,38,195,198,212,216,39,217,199,40,194,200,213,214,41,201,202,48,205,203,207,206,219,208,204,221,62,91,186,60,169,192,93,147,1,61,222,63,33,95,4,34,160,2,209,131],xx=[148,141,157,140,137,138,139,145,150,179,176,151,173,174,177,1,152,178,172,153,188,187,163,189,154,183,165,155,191,180,184,190,41,162,181,48,167,161,185,170,166,175,182,220,62,91,164,60,168,223,93,147,1,61,222,63,129,95,4,149,160,2,171,131],sx=[255,255,255,255,255,255,255,255,28,23,1,159,26,19,5,255,156,18,4,30,3,6,20,24,31,25,7,158,2,8,21,22,18,9,10,146,13,11,15,14,255,16,12,255,255,27,0,255,28,255,29,255,255,31,30,255,144,6,255,5,255,255,17,255],ce=[oe,ex,xx,sx],L0=[60289,60354,60419,60536],It=[60281,60601];function Lt(x){if(x>=60281&&x<60289)return L0[x-60281>>1]>>(x&1?0:8)&255;let t=L0.findIndex(f=>x>=f&&x<=f+64),e=x-L0[t],s=t<0?void 0:e===64?255:ce[t][e];if(s===void 0&&x>=It[0]&&x<It[1])throw new Error(`the KERNAL's byte at $${x.toString(16).toUpperCase()}: not handled`);return s}function ix(x){let t={157:2,145:7};if(t[x]!==void 0)return{code:t[x],shift:1};let e=oe.indexOf(x);return e<0||[1,2,3,4].includes(x)?null:{code:e,shift:0}}function Nt({cpu:x,read:t,write:e,drive:s,loaded:f=()=>{},call:n,held:o=()=>null,log:c=()=>{}}){let r=R=>t(R),h=(R,C)=>e(R,C&255),p=St({read:t,write:e,keyRow:R=>{e(56320,R);let C=t(56321);return e(56320,127),C}}),d=R=>p.chrout(R),l=()=>p.clear(),y=!1,E=R=>h(144,R),u=()=>r(152),O=R=>{for(let C=0;C<u();C++)if(r(601+C)===R)return C;return-1},I=()=>{let R=r(184),C=r(186),L=r(185);if(O(R)>=0){x.a=2,x.c=1;return}if(u()>=10){x.a=1,x.c=1;return}let S=u();if(h(601+S,R),h(611+S,C),h(621+S,L|96),h(152,S+1),C>=4&&(E(0),r(183))){if(!s.listen(C)){E(128),x.a=5,x.c=1;return}s.second(L|96|240);let $=r(187)|r(188)<<8;for(let Y=0;Y<r(183);Y++)s.ciout(t($+Y));s.unlsn()}x.c=0},A=()=>{let R=O(x.a);if(R<0){x.c=0;return}let C=r(611+R),L=r(621+R);if(C>=4){let S=s.listen(C);E(S?0:128),S&&(s.second(L&239|224),s.unlsn())}for(let S=R;S<u()-1;S++)h(601+S,r(602+S)),h(611+S,r(612+S)),h(621+S,r(622+S));h(152,u()-1),x.c=0},D=R=>{let C=O(x.x);if(C<0){x.a=3,x.c=1;return}let L=r(611+C),S=r(621+C);if(L>=4){let $=R?s.listen(L):s.talk(L);if(E($?0:128),!$){x.a=5,x.c=1;return}R?s.second(S):s.tksa(S)}h(R?154:153,L),x.c=0},V=()=>{r(154)>=4&&s.unlsn(),r(153)>=4&&s.untlk(),h(154,3),h(153,0)},N={65490:()=>{r(154)>=4?s.ciout(x.a):d(x.a),x.c=0},65472:I,65475:A,65478:()=>D(!1),65481:()=>D(!0),65511:()=>{V(),h(152,0)},65487:()=>{if(r(153)===0){y||(p.begin(),y=!0);let C=p.input();if(C===null)return"wait";y=!1,x.a=C,x.c=0;return}if(r(153)!==8)throw new Error("CHRIN from device "+r(153)+": not handled");if(r(144)){x.a=13,x.c=0;return}let R=s.acptr();R?(x.a=R.byte,R.eoi&&E(r(144)|64)):(x.a=13,E(r(144)|66)),x.c=0},65520:()=>{x.c?(x.x=r(214),x.y=r(211)):(h(214,x.x),h(211,x.y),p.setLine())},65484:V,65424:()=>{h(157,x.a)},65505:()=>{x.z=0},65463:()=>{x.a=r(144),x.nz(x.a)},65466:()=>{h(184,x.a),h(186,x.x),h(185,x.y)},65469:()=>{h(183,x.a),h(187,x.x),h(188,x.y)},65493:()=>{let R=r(183),C=r(187)|r(188)<<8,L=Uint8Array.from({length:R},(a,b)=>r(C+b)),S=r(185),$=r(186)===8?s.load(L):null;if(!$||$.length<2||x.a!==0){x.a=4,x.c=1,E(66),c("LOAD",String.fromCharCode(...L),"not found");return}let Y=S===0?x.x|x.y<<8:$[0]|$[1]<<8,W=$.subarray(2);for(let a=0;a<W.length;a++)e(Y+a,W[a]);let i=Y+W.length;h(174,i),h(175,i>>8),E(0),x.x=i&255,x.y=i>>8,x.c=0,x.a=0,f(String.fromCharCode(...L),Y,i-1)},65496:()=>{let R=r(183),C=r(187)|r(188)<<8,L=Uint8Array.from({length:R},(W,i)=>r(C+i)),S=r(x.a)|r(x.a+1)<<8,$=x.x|x.y<<8,Y=new Uint8Array($-S+2);Y[0]=S&255,Y[1]=S>>8;for(let W=S;W<$;W++)Y[W-S+2]=t(W);s.save(L,Y),x.c=0,x.a=0},65457:()=>E(s.listen(x.a)?0:128),65460:()=>E(s.talk(x.a)?0:128),65427:()=>s.second(x.a),65430:()=>s.tksa(x.a),65448:()=>s.ciout(x.a),65454:()=>s.unlsn(),65451:()=>s.untlk(),65445:()=>{let R=s.acptr();R?(x.a=R.byte,R.eoi&&E(r(144)|64)):(x.a=13,E(r(144)|66)),x.c=0},65499:()=>{h(162,x.a),h(161,x.x),h(160,x.y)},65502:()=>{x.a=r(162),x.x=r(161),x.y=r(160)},65514:()=>{let R=(r(160)<<16|r(161)<<8|r(162))+1;R>=5184001&&(R=0),h(160,R>>16),h(161,R>>8),h(162,R),h(145,t(56321))},65371:()=>{xt.forEach((R,C)=>e(53248+C,R)),h(646,r(646)||14),h(648,r(648)||4),h(199,0),l()},64789:()=>st.forEach((R,C)=>{h(788+2*C,R),h(789+2*C,R>>8)})},K=()=>{let R=r(655)|r(656)<<8;if(R!==60232){n(R);return}let C=r(203),L=r(653),S;L===3?(U(L),S=Math.max(0,L0.indexOf(r(245)|r(246)<<8))):(S=L*2>=8?3:L,h(245,L0[S]),h(246,L0[S]>>8));let $=C===64?255:ce[S][C],Y=()=>{h(197,C),h(654,L),$!==255&&r(198)<r(649)&&(h(631+r(198),$),h(198,r(198)+1))};if(C!==r(197)){h(652,16),Y();return}let W=$&127,i=r(650);if(!(i&128)){if(i&64)return;if(W===127){Y();return}if(![20,32,29,17].includes(W))return}r(652)&&(h(652,r(652)-1),r(652))||(h(651,r(651)-1),!r(651)&&(h(651,4),!r(198)&&Y()))},G=()=>{let R=o(),C=R?.shift??0;h(653,C),h(203,R?.code??64),R&&R.code!==null?K():(U(C),h(197,64),h(654,C))},U=R=>{R===3&&r(654)!==3&&!(r(657)&128)&&e(53272,t(53272)^2)};N[60125]=K,N[59953]=()=>{N[65514](),N[59956]()},N[59956]=()=>{if(!r(204)&&(h(205,r(205)-1),!r(205))){h(205,20);let R=r(211),C=(r(209)|r(210)<<8)+R,L=55296+((r(209)|r(210)<<8)&1023)+R,S=r(207)&1;h(207,r(207)>>1);let $=r(647),Y=t(C);S||(h(207,r(207)+1),h(206,Y),h(647,t(L)),$=r(646),Y=r(206)),e(C,Y^128),e(L,$)}r(1)&16?(h(192,0),h(1,r(1)|32)):r(192)||h(1,r(1)&31),G()},N[60027]=G;for(let R of[60030,60033])N[R]=()=>{};N[48201]=()=>{if(x.x!==144||!x.c)throw new Error("FLOATC with another exponent or sign: not handled");let R=r(98)<<24|r(99)<<16,C=x.x;if(!R)C=0;else for(;!(R&2147483648);)R=R<<1>>>0,C--;h(97,C),h(98,R>>>24),h(99,R>>>16),h(100,0),h(101,0),h(102,0),h(112,0),x.y=0},N[48607]=()=>{let R=r(97),C=(r(98)<<24|r(99)<<16|r(100)<<8|r(101))>>>0,L=160-R;if(R&&(L<0||L>32||C%2**L))throw new Error("FOUT of a number not whole: not handled");let S=R?C/2**L:0,$=r(102)&128?45:32,Y=String(S);e(255+x.y,$),h(102,$);for(let W=0;W<Y.length;W++)e(256+x.y+W,Y.charCodeAt(W));e(256+x.y+Y.length,0),x.a=0,x.y=1,x.x=0},N[62421]=()=>{if(r(185)&128||!r(183)){x.c=0;return}E(s.listen(r(186))?0:128),s.second(r(185)|240);let R=r(187)|r(188)<<8;for(let C=0;C<r(183);C++)s.ciout(r(R+C));s.unlsn(),x.c=r(144)&128?1:0},N[63042]=()=>{if(r(185)&128)return;let R=s.listen(r(186));E(R?0:128),R&&(s.second(r(185)&239|224),s.unlsn()),x.c=0},Object.assign(N,{61898:N[65490],62629:N[65493],62957:N[65496]});for(let[R,C]of Object.entries(N))x.hooks.set(+R,C)}var ke={};X(ke,{C64:()=>Dt});import{cpu as fx}from"@moonbase-machines/mos6502";var{CPU:le,CYCLES:Gx}=fx;var ue={};X(ue,{Printer:()=>H0,WIDTH:()=>$t,bus:()=>Ut,paper:()=>nx});var $t=80,H0=class{constructor({on:t=!0}={}){this.on=t,this.listening=!1,this.lower=!1,this.wide=!1,this.reverse=!1,this.lines=[],this.line=[],this.changed=()=>{}}listen(t){return this.listening=this.on&&t===4,this.listening}talk(){return!1}second(t){this.listening&&(this.lower=(t&15)===7)}tksa(){}ciout(t){if(this.listening){switch(t){case 13:this.feed();return;case 10:return;case 17:this.lower=!0;return;case 145:this.lower=!1;return;case 14:this.wide=!0;return;case 15:this.wide=!1;return;case 18:this.reverse=!0;return;case 146:this.reverse=!1;return;case 29:t=32;break}(t&127)<32||(this.line.length>=$t&&this.feed(),this.line.push({code:t,lower:this.lower,wide:this.wide,reverse:this.reverse}))}}feed(){this.lines.push(this.line),this.line=[],this.wide=!1,this.reverse=!1,this.changed()}unlsn(){this.listening=!1}untlk(){}acptr(){return null}tear(){this.lines=[],this.line=[],this.changed()}};function Ut(x,t){let e=[x,t];return{drive:x,printer:t,listen:s=>e.map(f=>f.listen(s)).some(Boolean),talk:s=>x.talk(s),second:s=>e.forEach(f=>f.second(s)),tksa:s=>x.tksa(s),ciout:s=>e.forEach(f=>f.ciout(s)),unlsn:()=>e.forEach(s=>s.unlsn()),untlk:()=>x.untlk(),acptr:()=>x.acptr(),load:s=>x.load(s),save:(s,f)=>x.save(s,f),find:s=>x.find(s)}}function nx(x,t,e,{scale:s=2}={}){let n=8*s,o=n*1.5,c=Math.max(12,t.lines.length+2);x.width=($t*8+48)*s/2*2,x.height=c*o+n;let r=x.getContext("2d"),h=x.width;r.fillStyle="#fbfaf3",r.fillRect(0,0,h,x.height);for(let l=0;l<c;l+=6)r.fillStyle="#e6f0e2",r.fillRect(24*s/2,l*o+n/2,h-24*s,o*3);r.fillStyle="#d9d6c8";for(let l=n/2;l<x.height;l+=n*2)for(let y of[24*s/4,h-24*s/4])r.beginPath(),r.arc(y,l,s*2.5,0,Math.PI*2),r.fill();let p=[...t.lines,t.line],d=s;p.forEach((l,y)=>{let E=24*s/2+n/2;for(let u of l){let O=Ft(u.code),I=(u.lower?2048:0)+O*8,A=u.wide?2:1;for(let D=0;D<8;D++){let V=e?e[I+D]:0;for(let N=0;N<8;N++)V>>7-N&1^(u.reverse?1:0)&&(r.fillStyle="#26241f",r.fillRect(E+N*d*A,y*o+n/2+D*d,d*A,d))}E+=n*A}})}var me={};X(me,{STANDS_IN:()=>ax,install:()=>Mt,standIn:()=>it});import{chargen as rx}from"@moonbase-machines/charset";var{chargen:de}=rx;var ax=["zoopak","goldfields","crayon64","crayon64-studio"],ox={65530:67,65531:254,65532:226,65533:252,65534:72,65535:255,44689:188,64433:141,65449:221};function be(x){let t=new Uint8Array(8192),e=new Uint8Array(8192);for(let s=x;s<x+8192;s++){let f=ox[s];if(f===void 0&&x===57344)try{f=Lt(s)}catch{f=void 0}for(let[n,o]of Object.entries(N0))s>=+n&&s<+n+o.length&&(f=o[s-n]);f!==void 0&&(t[s-x]=f,e[s-x]=1)}return Object.assign(t,{known:e,from:x})}function it(){return{kernal:be(57344),basic:be(40960),chargen:de(),standIn:!0}}var pe={15:1,52:1,61:2,58:4};function cx(x){let t=null,e=0;for(let s=0;s<8;s++)for(let f=0;f<8;f++){if(!(x.keys[s]&1<<f))continue;let n=s*8+f;pe[n]?e|=pe[n]:t===null&&(t=n)}return t===null&&!e?null:{code:t,shift:e}}function Mt(x){let t=x.cpu,e=g=>x.read(g),s=(g,m)=>x.write(g,m&255),f=g=>g>=57344&&x.port&2||g>=40960&&g<49152&&(x.port&3)===3||g===115||g===121,n=g=>e(g)|e(g+1)<<8,o=g=>{let m=t.pc,w=t.sp;t.push(m-1>>8),t.push(m-1&255),t.pc=g;for(let F=0;F<1e6&&!(t.pc===m&&t.sp===w);F++)t.step()},c=new Set(t.hooks.keys());Nt({cpu:t,read:e,write:s,drive:x.bus,call:o,held:()=>cx(x),log:x.log}),t.hooks.set(65508,()=>{if(e(153)>=4)return t.hooks.get(61783)(t);let g=e(198);if(!g){t.a=0,t.nz(0),t.c=0;return}t.a=e(631);for(let m=0;m<g-1;m++)s(631+m,e(632+m));s(198,g-1),t.nz(t.a),t.c=0}),t.hooks.set(65439,t.hooks.get(60027)),t.hooks.set(60039,t.hooks.get(60027)),t.hooks.set(65517,()=>{t.x=40,t.y=25}),t.hooks.set(65523,()=>{t.x=0,t.y=220});let r={65472:[794,62282],65475:[796,62097],65478:[798,61966],65481:[800,62032],65484:[802,62259],65487:[804,61783],65490:[806,61898],65505:[808,63213],65508:[810,61758],65511:[812,62255]};for(let[g,[m,w]]of Object.entries(r).map(([F,v])=>[+F,v]))t.hooks.set(w,t.hooks.get(g)),t.hooks.set(g,()=>(t.pc=n(m),!1));t.hooks.set(65493,()=>(s(195,t.x),s(196,t.y),t.pc=62622,!1)),t.hooks.set(65496,()=>(t.pc=62941,!1)),kt({cpu:t,read:e,write:s,chrout:t.hooks.get(61898),evaluate:g=>{let m=V?.params?.[g];if(!m)return h(g);let[w,F,v]=m;return{value:F(N),end:w,place:v}}});let h=yt({cpu:t,read:e,write:s,ram:x.ram}),p=[...t.hooks.keys()].filter(g=>!c.has(g)),d=()=>{t.y=t.pull(),t.x=t.pull(),t.a=t.pull(),t.p=t.pull(),t.pc=t.pull()|t.pull()<<8},l=(g,m)=>t.hooks.set(g,m);{let g=t.hooks.get(65371);t.hooks.set(65371,()=>{g(t);for(let w=0;w<1e5&&e(53266)!==0;w++);s(678,e(53273)&1);let m=e(678)?16421:17045;s(56324,m),s(56325,m>>8),s(56333,129),s(56334,e(56334)&128|17),s(56576,e(56576)|16)})}for(let g of[59953,59956,60027,60030]){let m=t.hooks.get(g);l(g,()=>(m(t),e(56333),d(),!1))}l(60033,()=>(d(),!1)),l(65352,()=>(t.push(t.a),t.push(t.x),t.push(t.y),t.pc=n(e(260+t.sp)&16?790:788),!1)),l(65091,()=>(t.i=1,t.pc=n(792),!1)),l(65095,()=>(e(56589),t.p=t.pull(),t.pc=t.pull()|t.pull()<<8,!1)),l(64738,()=>(I(),t.i=0,t.pc=42112,!1)),l(42112,()=>D()),l(57720,()=>(e(185)||(s(45,e(174)),s(46,e(175))),O("\rREADY.\r"),t.sp=255,t.pc=42112,!1)),l(43121,()=>(k(),t.pc=42926,!1)),l(42926,()=>A0()),l(42585,()=>(s(47,e(45)),s(48,e(46)),s(49,e(45)),s(50,e(46)),s(51,e(55)),s(52,e(56)),k(),C=null,!0)),l(42291,()=>{for(let g=e(43)|e(44)<<8,m=0;e(g+1)&&m<65536;m++){let w=g+4;for(;e(w);)w++;s(g,w+1&255),s(g+1,w+1>>8),g=w+1}return N&&b(),!0}),l(42039,()=>(t.pc=e(768)|e(769)<<8,!1)),l(58251,()=>(t.pc=t.x&128?42100:42042,!1)),l(42042,()=>(Z0(E,"?"+p0[t.x-1]+" ERROR",e(57)|e(58)<<8),t.sp=255,t.pc=42112,!1));let y=(g,m)=>{let w=t.sp,F=m+2,v=t.pc;t.push(F>>8),t.push(F&255),t.pc=g;for(let q=0;q<1e6&&!(t.pc===F+1&&t.sp===w);q++)t.step();t.pc=v},E={cpu:{get a(){return t.a},set a(g){t.a=g},hooks:{get:g=>()=>y(g,42055)}},write:s};l(42100,()=>(O("\rREADY.\r"),t.sp=255,t.pc=42112,!1));for(let g of[...p,42039,58251,42042,42100,60033,65352,65091,65095,64738,42112,57720,43121,42926,42585,42291]){let m=t.hooks.get(g);t.hooks.set(g,w=>{if(!f(w.pc))return!1;let F;try{F=m(w)}catch(v){let q=v instanceof j&&!U&&/^\?(.*) ERROR$/.exec(v.message)?.[1];if(!p0.includes(q))throw v;return w.x=p0.indexOf(q)+1,w.pc=42039,!1}return F==="return"?(w.pull(),w.pull(),!0):F})}let u=g=>{let{a:m,x:w,y:F}=t;t.a=g,t.hooks.get(61898)(t),Object.assign(t,{a:m,x:w,y:F})},O=g=>{for(let m of g)u(m.charCodeAt(0))};function I(){t.i=1,t.sp=255,x.port=55,x.portDir=47;for(let m=2;m<1024;m++)(m<256||m>=512)&&(x.ram[m]=0);s(56333,127),s(56589,127),s(56320,127),s(56322,255),s(56323,0),s(56334,8),s(56335,8),s(56590,8),s(56591,8),s(56576,7),s(56578,63),s(56324,37),s(56325,64),s(56333,129),s(56334,17),s(642,8),s(644,160),s(643,0),s(641,0),s(648,4),s(646,14),s(649,10),s(651,4),s(652,10),s(650,0),s(655,72),s(656,235),s(245,129),s(246,235),s(678,1),s(154,3),s(153,0),s(157,128),s(204,1),s(205,20);let g=(m,w)=>{s(m,w),s(m+1,w>>8)};g(43,2049),g(45,2051),g(47,2051),g(49,2051),g(51,40960),g(55,40960),s(84,76),s(784,76),g(785,45640),[58251,42115,42364,42778,42980,44678].forEach((m,w)=>g(768+2*w,m)),g(3,45482),g(5,45969),[230,122,208,2,230,123,173,96,234,201,58,176,10,201,32,240,239,56,233,48,56,233,208,96,128,79,199,82,88].forEach((m,w)=>s(115+w,m)),s(83,3),s(104,0),s(19,0),s(24,0),s(509,1),s(508,1),s(22,25),x.ram[2048]=0,x.ram[2049]=0,x.ram[2050]=0,st.forEach((m,w)=>{s(788+2*w,m),s(789+2*w,m>>8)}),xt.forEach((m,w)=>s(53248+w,m)),t.hooks.get(65371)(t),O("\rREADY.\r")}let A=[];function D(){for(;;){if(t.hooks.get(61783)(t)==="wait")return"wait";if(t.a===13)break;if(A.push(t.a),A.length>=89)return A=[],t.x=23,t.pc=42039,!1}let g=A;A=[],g.forEach((v,q)=>s(512+q,v)),s(512+g.length,0),u(13);let m=0;for(;g[m]===32;)m++;if(m===g.length)return"wait";if(s(58,255),g[m]>=48&&g[m]<=57){let v=0;for(;m<g.length&&(g[m]===32||g[m]>=48&&g[m]<=57);m++)if(g[m]!==32){if(v>=6400)return t.x=11,t.pc=42039,!1;v=v*10+g[m]-48}let q=dt(x.ram,e(43)|e(44)<<8,v,_0(g.slice(m)));for(let i0 of[45,47,49])s(i0,q),s(i0+1,q>>8);return s(51,e(55)),s(52,e(56)),N?.clr(),C=null,"wait"}let w=String.fromCharCode(...g.slice(m)).trim();if(w==="RUN")return t.pc=43121,!1;let F=/^LOAD"([^"]*)"(?:,(\d+))?(?:,(\d+))?$/.exec(w);if(F){let[,v,q="1",i0="0"]=F;for(let z0=0;z0<v.length;z0++)s(512+z0,v.charCodeAt(z0));if(s(183,v.length),s(187,0),s(188,2),s(186,+q),s(185,+i0),s(184,1),O("\rSEARCHING FOR "+v),t.a=0,t.x=1,t.y=8,t.sp=247,s(504,119),s(505,225),x.load(),t.c)return O("\r?FILE NOT FOUND ERROR"),t.sp=255,O("\rREADY.\r"),"wait";let{a:Z,x:a0,y:Te}=t;return O("\rLOADING"),Object.assign(t,{a:Z,x:a0,y:Te,c:0}),!0}return _0(g.slice(m)).forEach((v,q)=>s(512+q,v)),J()}let V=null,N=null,K=[],G=null,U=!1,R=[],C=null,L=null,S=-1,$=0,Y=65536,W=Symbol("the processor goes on"),i=g=>m=>{Object.assign(t,{a:m.a,x:m.x,y:m.y}),t.hooks.get(g)(t),Object.assign(m,{a:t.a,x:t.x,y:t.y,c:t.c,z:t.z,n:t.n,v:t.v})},a={get mem(){return x.ram},read:e,write:s,a:0,x:0,y:0,c:0,z:0,n:0,v:0,pc:0,ended:!1,idle:0,polled:!1,inside:0,keys:[],delay:0,cycles:0,title:{seed:17},routines:{chrout:i(61898),chrin:i(61783),getin:i(61758),clrchn:i(62259),chkin:i(61966),chkout:i(62032),open:i(62282),close:i(62097),setnam:i(65469),settim:i(65499),setlfs:i(65466),load:g=>{Object.assign(t,{a:g.a,x:g.x,y:g.y}),x.load(),Object.assign(g,{a:t.a,x:t.x,y:t.y,c:t.c})},sys:(g,m,w)=>(s(122,w),s(123,w>>8),s(20,m),s(21,m>>8),t.push(167),t.push(173),t.pc=57648,W),"basic-load":(g,[m,w=1,F=0])=>{for(let v=0;v<m.length;v++)s(512+v,m.charCodeAt(v));if(s(183,m.length),s(187,0),s(188,2),s(186,w),s(185,F),s(184,0),Object.assign(g,{a:0,x:1,y:8}),g.routines.load(g),g.c)throw new j("?FILE NOT FOUND ERROR");F?g.pc=0:b()}}};function b(g=X0(x.ram,e(43)|e(44)<<8)){V=bt(g,{name:"program",source:"BASIC's text in memory",mem:x.ram,cost:0,render:()=>""}),R=[];let m=65535,w=0;for(let F of g)F.label!==void 0?(m=F.line??65535,F.label===Y&&(w=R.length)):R.push(m);return K=g.filter(F=>F.label===void 0),N&&(N.items=lt(x.ram,e(43)|e(44)<<8)),a.pc=0,a.ended=!1,w}function k(){N=new k0(a,{quiet:!0}),a.basic=N,b()}let T=null;function P(g,m){if(R[m]===65535)throw new j("?ILLEGAL DIRECT ERROR");if(!T){if(T={k:m,n:0,text:null,at:0,typed:[]},g.prompt!==null)for(let F of g.prompt)u(F.charCodeAt(0));O("? ")}let w=T;for(;;){if(w.text===null){for(;;){if(t.hooks.get(61783)(t)==="wait")return a.pc=m,t.pc=42926,"wait";if(t.a===13)break;if(w.typed.push(t.a),w.typed.length>=89)throw T=null,new j("?STRING TOO LONG ERROR")}if(u(13),w.text=w.typed,w.typed=[],w.at=0,w.fresh=!0,!w.text.length&&w.n===0){T=null,a.ended=!0,a.pc=m+1;return}}let F=w.text,v=()=>{for(;F[w.at]===32;)w.at++};if(w.n===g.targets.length){w.at<F.length&&O("?EXTRA IGNORED\r"),T=null,a.pc=m+1;return}if(w.n>0&&!w.fresh&&(w.at>=F.length||F[w.at]===58)){O("?? "),w.text=null;continue}!w.fresh&&F[w.at]===44&&w.at++,w.fresh=!1,v();let q=g.targets[w.n],i0=q.n.endsWith("$"),Z;if(i0){let a0="";if(F[w.at]===34){for(w.at++;w.at<F.length&&F[w.at]!==34;)a0+=String.fromCharCode(F[w.at++]);F[w.at]===34&&w.at++,v()}else for(;w.at<F.length&&F[w.at]!==44&&F[w.at]!==58;)a0+=String.fromCharCode(F[w.at++]);Z=a0}else{let a0=$0(String.fromCharCode(...F),w.at);w.at=a0.end,Z=m0(a0.text)}if(w.at<F.length&&F[w.at]!==44&&F[w.at]!==58)return O("?REDO FROM START\r"),T=null,P(g,m);q.k==="elem"?N.setElem(q.n,q.i.map(a0=>Y0(a0)(N)),Z):N.set(q.n,Z),w.n++}}function z(g){O("\rBREAK"),g!==void 0&&g>>8!==255&&O(" IN "+g)}function J(){N||(N=new k0(a,{quiet:!0}),a.basic=N);let g=ut(x.ram,e(43)|e(44)<<8,512);return g[g.findIndex(m=>m.label==="direct")].label=Y,a.pc=b(g),t.pc=42926,!1}a.routines["basic-list"]=(g,[m,w])=>{for(let F of G0(x.ram,e(43)|e(44)<<8))if(!(F.n<m)){if(u(13),F.n>w)break;O(F.n+" "),v0(F.bytes).forEach(v=>u(v))}},a.routines["basic-new"]=g=>{let m=e(43)|e(44)<<8;s(m,0),s(m+1,0);for(let w of[45,47,49])s(w,m+2),s(w+1,m+2>>8);s(51,e(55)),s(52,e(56)),N?.clr(),C=null,g.ended=!0};let f0=(g,m)=>{if(m!==8)throw m===1?new Error("the tape is not handled"):new j("?DEVICE NOT PRESENT ERROR");if(!g.length)throw new j("?MISSING FILE NAME ERROR");return Uint8Array.from(g,w=>w.charCodeAt(0))},e0=()=>e(58)===255;a.routines["basic-save"]=(g,[m="",w=1])=>{let F=f0(m,w),v=e(43)|e(44)<<8,q=e(45)|e(46)<<8;e0()&&O("\rSAVING "+m);let i0=new Uint8Array(q-v+2);i0[0]=v&255,i0[1]=v>>8;for(let Z=v;Z<q;Z++)i0[Z-v+2]=e(Z);x.drive.save(F,i0)},a.routines["basic-verify"]=(g,[m="",w=1,F=0])=>{let v=f0(m,w);e0()&&O("\rSEARCHING FOR "+m);let q=x.drive.load(v);if(!q||q.length<2)throw new j("?FILE NOT FOUND ERROR");e0()&&O("\rVERIFYING");let i0=F?q[0]|q[1]<<8:e(43)|e(44)<<8;for(let Z=2;Z<q.length;Z++)if(e(i0+Z-2)!==q[Z])throw new j("?VERIFY ERROR");O("\rOK\r")},a.routines["basic-cont"]=g=>{if(C===null)throw new j("?CAN'T CONTINUE ERROR");g.pc=C};function A0(){if(G){let{k:g,t:m}=G;G=null;let w=t.a,F=w?String.fromCharCode(w):"";N.set(m.n,m.n.endsWith("$")?F:w&&Number(F)||0),a.pc=g+1}for(let g=0;g<1e5;g++){if(x.basicPace&&(x.frame!==S&&(S=x.frame,$=x.basicPace),$--<=0))return"wait";let m=a.pc;if(a.ended||m>=V.steps.length)return a.ended&&/^(end|stop)$/.test(K[m-1]?.k)&&R[m-1]!==65535&&(C=m),a.ended&&K[m-1]?.k==="stop"&&z(R[m-1]),O("\rREADY.\r"),t.sp=255,t.pc=42112,!1;let w=K[m];if(R[m]!==void 0&&(s(57,R[m]),s(58,R[m]>>8)),e(145)===127&&L!==m)return s(198,0),C=m,z(R[m]),O("\rREADY.\r"),t.sp=255,t.pc=42112,!1;if(L=null,w.k==="input-keys"){let v=P(w,m);if(v!==void 0)return v;continue}if(w.k==="get")return s(19,0),G={k:m,t:w.t},t.push(167),t.push(173),t.pc=65508,!1;a.pc=m+1;let F;try{U=!0,F=V.run[m](a,V.steps)}catch(v){if(!(v instanceof j)||!Z0(E,v.message,v.basicLine??R[m]??N.at))throw v;return t.sp=255,t.pc=42112,!1}finally{U=!1}if(F===W)return!1;if(w.k==="wait"&&a.pc===m)return L=m,t.pc=42926,"wait"}return"wait"}}var ge=312,hx=63,lx=15,Dt=class{constructor(t,e,{log:s=()=>{},printer:f=new H0({on:!1})}={}){this.game=t,this.log=s,this.ram=new Uint8Array(65536),this.kernal=e.kernal,this.basic=e.basic,this.chargen=e.chargen,this.color=new Uint8Array(1024),this.port=55,this.portDir=47,this.cia1=new ft,this.cia2=new ft,this.sid=new Uint8Array(32),this.sidWrites=[],this.vic=new O0(this),this.keys=new Uint8Array(8),this.joystick=0,this.drive=new j0(Uint8Array.from(atob(t.image),r=>r.charCodeAt(0))),this.printer=f,this.halted=!1,this.frame=0,this.line=0,this.cpu=new le(this,{nmos:!0,writeBack:!0});let n=(r,h)=>!e.standIn&&this.read16(r)===h;this.cpu.hooks.set(62622,()=>n(816,62629)?!1:this.load()),this.cpu.hooks.set(62941,()=>n(818,62957)?!1:this.save());let o=this.bus=Ut(this.drive,this.printer),c=this.ram;this.cpu.hooks.set(60684,r=>(o.listen(r.a)||(c[144]|=128),!0)),this.cpu.hooks.set(60681,r=>(o.talk(r.a)||(c[144]|=128),!0)),this.cpu.hooks.set(60857,r=>(o.second(r.a),!0)),this.cpu.hooks.set(60871,r=>(o.tksa(r.a),!0)),this.cpu.hooks.set(60893,r=>(o.ciout(r.a),r.c=0,!0)),this.cpu.hooks.set(60926,()=>(o.unlsn(),!0)),this.cpu.hooks.set(60911,()=>(o.untlk(),!0)),this.cpu.hooks.set(60947,r=>{let h=o.acptr();return h?(r.a=h.byte,h.eoi&&(c[144]|=64)):(r.a=13,c[144]|=66),r.c=0,!0}),e.standIn&&Mt(this),this.reset()}reset(){this.port=55,this.portDir=47,this.cpu.pc=this.read16(65532),this.cpu.sp=253,this.cpu.i=1}read16(t){return this.read(t)|this.read(t+1)<<8}read(t){if(t===0)return this.portDir;if(t===1)return this.port&this.portDir|~this.portDir&23&255|16;let e=this.port&7;return t>=57344?e&2?this.rom(this.kernal,t):this.ram[t]:t>=53248&&t<57344?e&3&&e&4?this.io(t):e&3?this.chargen[t-53248]:this.ram[t]:t>=40960&&t<49152?(e&3)===3?this.rom(this.basic,t):this.ram[t]:this.ram[t]}rom(t,e){let s=e-(t===this.kernal?57344:40960);if(t.known&&!t.known[s])throw new Error(`the ${t===this.kernal?"KERNAL":"BASIC"} ROM at $${e.toString(16).toUpperCase()}: not handled`);return t[s]}write(t,e){if(t===0){this.portDir=e;return}if(t===1){this.port=e;return}let s=this.port&7;if(t>=53248&&t<57344&&s&3&&s&4){this.ioWrite(t,e);return}this.ram[t]=e}io(t){return t<54272?this.vic.read(t&63):t<55296?(t&31)===27?Math.random()*256|0:(t&31)===28?0:this.sid[t&31]:t<56320?this.color[t-55296]|240:t<56576?this.cia1.read(t&15,this):t<56832?this.cia2.read(t&15,this):255}ioWrite(t,e){t<54272?this.vic.write(t&63,e):t<55296?(this.sid[t&31]=e,this.sidWrites.push([this.cpu.cycles,t&31,e])):t<56320?this.color[t-55296]=e&15:t<56576?this.cia1.write(t&15,e):t<56832&&this.cia2.write(t&15,e)}keyboardRows(t){let e=255;for(let s=0;s<8;s++)t&1<<s||(e&=~this.keys[s]);return e&255}load(){let t=this.cpu,e=this.ram.subarray(this.ram[187]|this.ram[188]<<8,(this.ram[187]|this.ram[188]<<8)+this.ram[183]),s=this.ram[186],f=this.ram[185],n=String.fromCharCode(...e);if(s!==8){t.a=5,t.c=1;return}let o=this.drive.load(e);if(!o||o.length<2||t.a!==0){this.log("LOAD "+JSON.stringify(n)+(o?" verify":" not found")),t.a=4,t.c=1,this.ram[144]=66;return}let c=f===0?t.x|t.y<<8:o[0]|o[1]<<8,r=o.subarray(2);for(let p=0;p<r.length;p++)this.write(c+p,r[p]);let h=c+r.length;this.ram[174]=h&255,this.ram[175]=h>>8,this.ram[144]=0,t.x=h&255,t.y=h>>8,t.c=0,t.a=0,this.log("LOAD",JSON.stringify(n),"$"+c.toString(16),r.length+" bytes")}save(){let t=this.cpu,e=this.pstringAt(this.ram[187]|this.ram[188]<<8,this.ram[183]),s=this.ram[t.a]|this.ram[t.a+1]<<8,f=t.x|t.y<<8,n=new Uint8Array(f-s+2);n[0]=s&255,n[1]=s>>8;for(let o=s;o<f;o++)n[o-s+2]=this.read(o);this.drive.save(this.ram.subarray(this.ram[187]|this.ram[188]<<8,(this.ram[187]|this.ram[188]<<8)+this.ram[183]),n),this.log("SAVE",JSON.stringify(e),n.length+" bytes"),t.c=0,t.a=0}pstringAt(t,e){let s="";for(let f=0;f<e;f++)s+=String.fromCharCode(this.ram[t+f]);return s}key(t,e,s){s?this.keys[t]|=1<<e:this.keys[t]&=~(1<<e)}type(t){let e=Math.min(10,t.length);for(let s=0;s<e;s++)this.ram[631+s]=t.charCodeAt(s);return this.ram[198]=e,t.slice(e)}runLine(){let t=this.cpu.cycles+hx,e=this.cpu.cycles+lx,s=!1;for(;this.cpu.cycles<t&&!this.halted;){!s&&this.cpu.cycles>=e&&(this.vic.line(this.line),s=!0);let f=this.cpu.cycles,n=this.cpu.pc;(this.port&2)===0&&n>=57344&&this.cpu.hooks.has(n),this.cpu.step();let o=this.cpu.cycles-f;this.cia1.tick(o)&&this.cia1.icr&128&&this.cpu.irq(),this.cia2.tick(o);let c=(this.cia2.icr&128)!==0;c&&!this.nmiLine&&this.cpu.nmi(),this.nmiLine=c}s||this.vic.line(this.line),this.vic.irq()&&this.cpu.irq(),this.line=(this.line+1)%ge,this.line===0&&this.frame++}runFrame(){for(let t=0;t<ge;t++)this.runLine()}},ft=class{constructor(){this.ta=65535,this.taLatch=65535,this.tb=65535,this.tbLatch=65535,this.cra=0,this.crb=0,this.icr=0,this.mask=0,this.pra=255,this.prb=255,this.ddra=0,this.ddrb=0,this.tod=0}read(t,e){switch(t){case 0:return e&&this===e.cia1?(this.pra|~this.ddra)&~e.joystick&255:(this.pra|~this.ddra)&255;case 1:return e&&this===e.cia1?e.keyboardRows(this.pra|~this.ddra)&(this.prb|~this.ddrb):(this.prb|~this.ddrb)&255;case 2:return this.ddra;case 3:return this.ddrb;case 4:return this.ta&255;case 5:return this.ta>>8;case 6:return this.tb&255;case 7:return this.tb>>8;case 8:return 0;case 9:return 0;case 10:return 0;case 11:return 0;case 13:{let s=this.icr;return this.icr=0,s}case 14:return this.cra;case 15:return this.crb}return 0}write(t,e){switch(t){case 0:this.pra=e;break;case 1:this.prb=e;break;case 2:this.ddra=e;break;case 3:this.ddrb=e;break;case 4:this.taLatch=this.taLatch&65280|e;break;case 5:this.taLatch=this.taLatch&255|e<<8,this.cra&1||(this.ta=this.taLatch);break;case 6:this.tbLatch=this.tbLatch&65280|e;break;case 7:this.tbLatch=this.tbLatch&255|e<<8,this.crb&1||(this.tb=this.tbLatch);break;case 13:e&128?this.mask|=e&31:this.mask&=~e;break;case 14:this.cra=e,e&16&&(this.ta=this.taLatch);break;case 15:this.crb=e,e&16&&(this.tb=this.tbLatch);break}}tick(t){let e=!1;if(this.cra&1)for(this.ta-=t;this.ta<0;)this.ta+=this.taLatch+1,this.icr|=1,this.cra&8&&(this.cra&=-2,this.ta=this.taLatch),(this.crb&96)===64&&this.crb&1&&--this.tb<0&&(this.tb=this.tbLatch,this.icr|=2);if(this.crb&1&&(this.crb&96)===0)for(this.tb-=t;this.tb<0;)this.tb+=this.tbLatch+1,this.icr|=2,this.crb&8&&(this.crb&=-2,this.tb=this.tbLatch);return this.icr&this.mask&31&&(this.icr|=128,e=!0),e}};var ye={};X(ye,{paint:()=>vt});function vt(x,t,e,s,f){let n={ram:x,chargen:e??new Uint8Array(4096),color:t.subarray(2048,3072),cia2:{pra:t[3328]},line:0};s??=new O0(n),s.m=n,s.r.set(t.subarray(0,64));let o=f?.size?[...f].sort((r,h)=>r[0]-h[0]):[],c=(r,h)=>{r===256?n.cia2.pra=h:s.r[r]=h};if(o.length)for(let[r,h]of o.at(-1)[1])c(r,h);for(let r=0,h=0;r<312;r++){for(;h<o.length&&o[h][0]<r;){for(let[p,d]of o[h][1])c(p,d);h++}n.line=r,s.line(r)}return s.frame}var Ae={};X(Ae,{chip:()=>ux,rows:()=>dx});function ux({registers:x={},color:t=[],bank:e=0}={}){let s=new Uint8Array(4096);for(let[f,n]of Object.entries(x))s[+f]=n;return s.set(t.slice(0,1024),2048),s[3328]=3-(e>>14),s}function dx({mem:x,chip:t,chargen:e,crop:s=[0,0,320,200],scale:f=1}){let n=vt(x,t,e),[o,c,r,h]=s,p=[];for(let d=0;d<h;d++){let l=[];for(let y=0;y<r;y++){let E=V0[n[(wt+c+d)*_+d0+o+y]];for(let u=0;u<f;u++)l.push(E)}for(let y=0;y<f;y++)p.push(l)}return p}var we={};X(we,{keepDrive:()=>bx});function bx(x,t){for(let e of t.list())x.saved.set(e.name,t.read(e.name));x.keep=(e,s)=>t.write(e,s)}var Ee={};X(Ee,{SID:()=>Gt});var px=[2,8,16,24,38,56,68,80,100,250,500,800,1e3,3e3,5e3,8e3],Oe=[6,24,48,72,114,168,204,240,300,750,1500,2400,3e3,9e3,15e3,24e3],Gt=class{constructor(t){this.rate=t,this.r=new Uint8Array(32),this.v=[0,1,2].map(()=>({phase:0,env:0,stage:"off",noise:8388600,noiseOut:0}))}write(t,e){let s=this.r[t];if(this.r[t]=e,t%7===4&&t<21){let f=this.v[t/7|0];e&1&&!(s&1)?f.stage="attack":!(e&1)&&s&1&&(f.stage="release")}}render(t,e){let s=new Float32Array(t),f=0;for(let n=0;n<t;n++){for(;f<e.length&&e[f][0]<=n;)this.write(e[f][1],e[f][2]),f++;let o=0;for(let c=0;c<3;c++){let r=c*7,h=this.v[c],p=this.r,d=p[r]|p[r+1]<<8,l=p[r+4],y=px[p[r+5]>>4]/1e3*this.rate,E=Oe[p[r+5]&15]/1e3*this.rate,u=(p[r+6]>>4)/15,O=Oe[p[r+6]&15]/1e3*this.rate;if(h.stage==="attack"?(h.env+=1/Math.max(1,y),h.env>=1&&(h.env=1,h.stage="decay")):h.stage==="decay"?(h.env-=(1-u)/Math.max(1,E)*3,h.env<=u&&(h.env=u,h.stage="sustain")):h.stage==="release"&&(h.env-=1/Math.max(1,O)*3,h.env<=0&&(h.env=0,h.stage="off")),h.env<=0&&h.stage!=="attack")continue;let I=d*985248/16777216/this.rate,A=h.phase;h.phase=(h.phase+I)%1;let D=0;if(l&16&&(D+=h.phase<.5?h.phase*4-1:3-h.phase*4),l&32&&(D+=h.phase*2-1),l&64){let V=(p[r+2]|(p[r+3]&15)<<8)/4096;D+=h.phase<V?1:-1}if(l&128){if(h.phase<A){let V=(h.noise>>22^h.noise>>17)&1;h.noise=(h.noise<<1|V)&8388607,h.noiseOut=((h.noise>>22&1)<<7|(h.noise>>20&1)<<6|(h.noise>>16&1)<<5|(h.noise>>13&1)<<4|(h.noise>>11&1)<<3|(h.noise>>7&1)<<2|(h.noise>>4&1)<<1|h.noise>>2&1)/128-1}D+=h.noiseOut}o+=D*h.env}s[n]=o*(this.r[24]&15)/15*.2}return s}};var Re={};X(Re,{ROM_CHOICES:()=>mx,c64Roms:()=>gx,typed:()=>kx});var mx=[null,"commodore","moonbase-petscii"];async function gx(x,t){if(x!=="commodore"&&x!=="moonbase-petscii")return it();try{return x==="moonbase-petscii"?{...it(),chargen:await t("chargen")}:{kernal:await t("kernal"),basic:await t("basic"),chargen:await t("chargen")}}catch{return null}}function kx(x,t){let e=x.drive.find(Uint8Array.from(t.start,s=>s.charCodeAt(0)))?.raw;return'LOAD"'+t.start+'",8,1\r'+(e&&(e[0]|e[1]<<8)===2049?"RUN\r":"")}export{Kt as basic,zt as basicCompile,jt as basicLang,qt as basicList,Xt as basicRom,te as browser,xe as d64,ie as drive,ne as editor,Pt as float,re as glyphHash,ae as glyphHashes,he as kernal,_t as keys,ke as machine,Yt as numbers,ye as paint,Wt as petscii,Ae as picture,ue as printer,me as rom,we as saves,Ee as sid,Re as start,Zt as vic};
|
|
9
9
|
//# sourceMappingURL=moonbase-c64.js.map
|