v86 0.5.14 → 0.5.43

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.
@@ -1,729 +1,724 @@
1
- ;let module = {exports:{}}; var g=g||{};g.exportSymbol=function(a,b){"undefined"!==typeof module&&"undefined"!==typeof module.exports?module.exports[a]=b:"undefined"!==typeof window?window[a]=b:"function"===typeof importScripts&&(self[a]=b)};g.exportProperty=function(){};const ba=[[1,""],[2,"CPU"],[32768,"DISK"],[4,"FPU"],[8,"MEM"],[16,"DMA"],[32,"IO"],[64,"PS2"],[128,"PIC"],[256,"VGA"],[512,"PIT"],[1024,"MOUS"],[2048,"PCI"],[4096,"BIOS"],[8192,"FLOP"],[16384,"SERI"],[65536,"RTC"],[262144,"ACPI"],[524288,"APIC"],[1048576,"NET"],
2
- [2097152,"VIO"],[4194304,"9P"],[8388608,"SB16"],[16777216,"FETC"]];var ca=-15786961;function da(a,b){return(a||0===a?a+"":"").padEnd(b," ")}function ea(a,b){return(a||0===a?a+"":"").padStart(b,"0")}function k(a,b,c,d){m(0<=c);return new Proxy({},{get:function(e,f){e=new a(b.buffer,c,d);const h=e[f];if("function"===typeof h)return h.bind(e);m(/^\d+$/.test(f)||"buffer"===f||"length"===f||"BYTES_PER_ELEMENT"===f||"byteOffset"===f);return h},set:function(e,f,h){m(/^\d+$/.test(f));(new a(b.buffer,c,d))[f]=
3
- h;return!0}})}function z(a,b){a=a?a.toString(16):"";return"0x"+ea(a.toUpperCase(),b||1)}var fa;if("undefined"!==typeof crypto&&crypto.getRandomValues){const a=new Int32Array(1);fa=function(){crypto.getRandomValues(a);return a[0]}}else if("undefined"!==typeof require){const a=require("crypto");fa=function(){return a.randomBytes(4).readInt32LE(0)}}else"undefined"!==typeof process?import("node:crypto").then(a=>{fa=function(){return a.randomBytes(4).readInt32LE(0)}}):m(!1,"Unsupported platform: No cryptographic random values");
4
- var ha;if("function"===typeof Math.clz32)ha=function(a){m(0<a);return 31-Math.clz32(a)};else{for(var ia=new Int8Array(256),ka=0,la=-2;256>ka;ka++)ka&ka-1||la++,ia[ka]=la;ha=function(a){a>>>=0;m(0<a);var b=a>>>16;if(b){var c=b>>>8;return c?24+ia[c]:16+ia[b]}return(c=a>>>8)?8+ia[c]:ia[a]}}function na(a){m(0<=a);return 1>=a?1:1<<1+ha(a-1)}m(0===ha(1));m(1===ha(2));m(2===ha(7));m(3===ha(8));m(26===ha(123456789));m(1===na(0));m(1===na(1));m(2===na(2));m(8===na(7));m(8===na(8));m(134217728===na(123456789));
5
- function oa(a){var b=new Uint8Array(a),c,d;m(0===(a&a-1));this.length=0;this.push=function(e){this.length!==a&&this.length++;b[d]=e;d=d+1&a-1};this.shift=function(){if(this.length){var e=b[c];c=c+1&a-1;this.length--;return e}return-1};this.peek=function(){return this.length?b[c]:-1};this.clear=function(){this.length=d=c=0};this.clear()}function ra(a){this.size=a;this.data=new Float32Array(a);this.length=this.end=this.start=0;m(0===(a&a-1))}ra.prototype.push=function(a){this.length===this.size?this.start=
6
- this.start+1&this.size-1:this.length++;this.data[this.end]=a;this.end=this.end+1&this.size-1};ra.prototype.shift=function(){if(this.length){var a=this.data[this.start];this.start=this.start+1&this.size-1;this.length--;return a}};ra.prototype.shift_block=function(a){var b=new Float32Array(a);a>this.length&&(a=this.length);var c=this.start+a,d=this.data.subarray(this.start,c);b.set(d);c>=this.size&&(c-=this.size,b.set(this.data.subarray(0,c),d.length));this.start=c;this.length-=a;return b};ra.prototype.peek=
7
- function(){if(this.length)return this.data[this.start]};ra.prototype.clear=function(){this.length=this.end=this.start=0};function sa(a,b){Array.isArray(a)||(a=[a]);var c=new Blob(a);a=document.createElement("a");a.download=b;a.href=window.URL.createObjectURL(c);a.dataset.downloadurl=["application/octet-stream",a.download,a.href].join(":");document.createEvent?(b=document.createEvent("MouseEvent"),b.initMouseEvent("click",!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null),a.dispatchEvent(b)):a.click();window.URL.revokeObjectURL(a.href)}
8
- function ua(a){"number"===typeof a?this.view=new Uint8Array(a+7>>3):a instanceof ArrayBuffer?this.view=new Uint8Array(a):m(!1,"Bitmap: Invalid argument")}ua.prototype.set=function(a,b){const c=a>>3;a=1<<(a&7);this.view[c]=b?this.view[c]|a:this.view[c]&~a};ua.prototype.get=function(a){return this.view[a>>3]>>(a&7)&1};ua.prototype.get_buffer=function(){return this.view.buffer};var va;va="undefined"===typeof XMLHttpRequest?wa:xa;function xa(a,b,c){function d(){const n=c||0;setTimeout(()=>{va(a,b,n+1)},
9
- 1E3*([1,1,2,3,5,8,13,21][n]||34))}var e=new XMLHttpRequest;e.open(b.method||"get",a,!0);e.responseType=b.as_json?"json":"arraybuffer";if(b.headers)for(var f=Object.keys(b.headers),h=0;h<f.length;h++){var l=f[h];e.setRequestHeader(l,b.headers[l])}b.range&&(f=b.range.start,e.setRequestHeader("Range","bytes="+f+"-"+(f+b.range.length-1)),e.setRequestHeader("X-Accept-Encoding","identity"),e.onreadystatechange=function(){200===e.status&&(console.error("Server sent full file in response to ranged request, aborting",
10
- {filename:a}),e.abort())});e.onload=function(){if(4===e.readyState)if(200!==e.status&&206!==e.status)console.error("Loading the image "+a+" failed (status %d)",e.status),500<=e.status&&600>e.status&&d();else if(e.response){if(b.range){const n=e.getResponseHeader("Content-Encoding");n&&"identity"!==n&&console.error("Server sent Content-Encoding in response to ranged request",{filename:a,enc:n})}b.done&&b.done(e.response,e)}};e.onerror=function(n){console.error("Loading the image "+a+" failed",n);d()};
11
- b.progress&&(e.onprogress=function(n){b.progress(n)});e.send(null)}function wa(a,b){if(b.range)m(!b.as_json),import("node:fs").then(d=>d.open(a,"r",(e,f)=>{if(e)throw e;const h=b.range.length;var l=Buffer.allocUnsafe(h);d.read(f,l,0,h,b.range.start,(n,p)=>{if(n)throw n;m(p===h);b.done&&b.done(new Uint8Array(l));d.close(f,q=>{if(q)throw q;})})}));else{var c={encoding:b.as_json?"utf-8":null};import("node:fs").then(d=>d.readFile(a,c,function(e,f){e?console.log("Could not read file:",a,e):(e=f,e=b.as_json?
12
- JSON.parse(e):(new Uint8Array(e)).buffer,b.done(e))}))}}var D=function(){var a=ba.reduce(function(d,e){d[e[0]]=e[1];return d},{}),b="",c=0;return function(d,e){e=e||1;if(e&ca){d="["+da(a[e]||"",4)+"] "+d;if(d===b&&(c++,2048>c))return;e=new Date;e=ea(e.getHours(),2)+":"+ea(e.getMinutes(),2)+":"+ea(e.getSeconds(),2)+"+"+ea(e.getMilliseconds(),3)+" ";c&&(1===c?console.log(e+b):console.log("Previous message repeated "+c+" times"),c=0);console.log(e+d);b=d}}}();function m(a,b){if(!a){debugger;console.trace();
13
- if(b)throw"Assert failed: "+b;throw"Assert failed";}}function za(a,b){function c(A){A=A.toString(16);return"#"+"0".repeat(6-A.length)+A}function d(A){var x=256*pa,H=8*U,M=Xa?Xa.canvas:null;M&&M.width===x&&M.height===H||(M?(M.width=x,M.height=H):(M=new OffscreenCanvas(x,H),Xa=M.getContext("2d")),Eb=Xa.createImageData(x,H));const T=Eb.data;let S=0,V;H=Fb?function(aa){V=V||aa;T[S+3]=aa;T[S+7]=aa;S+=8}:function(aa){V=V||aa;T[S+3]=aa;S+=4};M=32-U;const ma=x*(U-1)*4;x=4*(pa-x*U);const ja=1020*pa;for(let aa=
14
- 0,ta=0;2048>aa;++aa,ta+=M,S+=x){const Ha=aa%256;aa&&!Ha&&(S+=ma);V=!1;for(let Oa=0;Oa<U;++Oa,++ta,S+=ja){const Pa=A[ta];for(let ya=128;0<ya;ya>>=1)H(Pa&ya?255:0);Gb&&H(Hb&&192<=Ha&&223>=Ha&&Pa&1?255:0)}$b[aa]=V?1:0}Xa.putImageData(Eb,0,0)}function e(A,x,H,M){if(x&&H){A.style.width="";A.style.height="";M&&(A.style.transform="");var T=A.getBoundingClientRect();M?A.style.transform=(1===x?"":" scaleX("+x+")")+(1===H?"":" scaleY("+H+")"):(0===x%1&&0===H%1?(h.style.imageRendering="crisp-edges",h.style.imageRendering=
15
- "pixelated",h.style["-ms-interpolation-mode"]="nearest-neighbor"):(h.style.imageRendering="",h.style["-ms-interpolation-mode"]=""),M=window.devicePixelRatio||1,0!==M%1&&(x/=M,H/=M));1!==x&&(A.style.width=T.width*x+"px");1!==H&&(A.style.height=T.height*H+"px")}}const f=a.container;this.screen_fill_buffer=b;console.assert(f,"options.container must be provided");this.FLAG_BLINKING=1;this.FLAG_FONT_PAGE_B=2;var h=f.getElementsByTagName("canvas")[0],l=h.getContext("2d",{alpha:!1}),n=f.getElementsByTagName("div")[0],
16
- p=document.createElement("div"),q,r,t=void 0!==a.scale?a.scale:1,v=void 0!==a.scale?a.scale:1,y=1,C,u,B,w,G,L,qa,Xa,Eb,$b=new Int8Array(2048),U,pa,Gb,Fb,Hb,Ib=0,Jb=0,tb,ac=0,ub,Kb,vb,Lb=[],kb=Lb,wb=0,Mb=!1;this.init=function(){const A=new Uint16Array([32,9786,9787,9829,9830,9827,9824,8226,9688,9675,9689,9794,9792,9834,9835,9788,9658,9668,8597,8252,182,167,9644,8616,8593,8595,8594,8592,8735,8596,9650,9660]),x=new Uint16Array([8962,199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,201,
17
- 230,198,244,246,242,251,249,255,214,220,162,163,165,8359,402,225,237,243,250,241,209,170,186,191,8976,172,189,188,161,171,187,9617,9618,9619,9474,9508,9569,9570,9558,9557,9571,9553,9559,9565,9564,9563,9488,9492,9524,9516,9500,9472,9532,9566,9567,9562,9556,9577,9574,9568,9552,9580,9575,9576,9572,9573,9561,9560,9554,9555,9579,9578,9496,9484,9608,9604,9612,9616,9600,945,223,915,960,931,963,181,964,934,920,937,948,8734,966,949,8745,8801,177,8805,8804,8992,8993,247,8776,176,8729,183,8730,8319,178,9632,
18
- 160]);for(var H=0,M;256>H;H++)M=126<H?x[H-127]:32>H?A[H]:H,Lb.push(String.fromCharCode(M));p.classList.add("cursor");p.style.position="absolute";p.style.backgroundColor="#ccc";p.style.width="7px";p.style.display="inline-block";this.set_mode(!1);this.set_size_text(80,25);2===u&&this.set_size_graphical(720,400,720,400);this.set_scale(t,v);this.timer()};this.make_screenshot=function(){const A=new Image;if(1===u||2===u)A.src=h.toDataURL("image/png");else{const x=[9,16],H=document.createElement("canvas");
19
- H.width=w*x[0];H.height=G*x[1];const M=H.getContext("2d");M.imageSmoothingEnabled=!1;M.font=window.getComputedStyle(n).font;M.textBaseline="top";for(let T=0;T<G;T++)for(let S=0;S<w;S++){const V=4*(T*w+S),ma=B[V+0],ja=B[V+3];M.fillStyle=c(B[V+2]);M.fillRect(S*x[0],T*x[1],x[0],x[1]);M.fillStyle=c(ja);M.fillText(kb[ma],S*x[0],T*x[1])}"none"!==p.style.display&&q<G&&r<w&&(M.fillStyle=p.style.backgroundColor,M.fillRect(r*x[0],q*x[1]+parseInt(p.style.marginTop,10),parseInt(p.style.width,10),parseInt(p.style.height,
20
- 10)));A.src=H.toDataURL("image/png")}return A};this.put_char=function(A,x,H,M,T,S){m(0<=A&&A<G);m(0<=x&&x<w);m(0<=H&&256>H);x=4*(A*w+x);B[x+0]=H;B[x+1]=M;B[x+2]=T;B[x+3]=S;C[A]=1};this.timer=function(){wb=requestAnimationFrame(()=>this.update_screen())};this.update_screen=function(){Mb||(0===u?this.update_text():1===u?this.update_graphical():this.update_graphical_text());this.timer()};this.update_text=function(){for(var A=0;A<G;A++)C[A]&&(this.text_update_row(A),C[A]=0)};this.update_graphical=function(){this.screen_fill_buffer()};
21
- this.update_graphical_text=function(){if(L){var A=performance.now();if(266<A-ac){tb=!tb;vb&&(C[q]=1);var x=4*w;for(let ma=0,ja=0;ma<G;++ma)if(C[ma])ja+=x;else for(var H=0;H<w;++H,ja+=4)if(B[ja+1]&1){C[ma]=1;ja+=x-4*H;break}ac=A}A=Xa.canvas;x=qa.canvas;H=4*w;const T=w*pa,S=U;let V=0;for(let ma=0,ja=0,aa=0;ma<G;++ma,ja+=U){if(!C[ma]){aa+=H;continue}++V;qa.clearRect(0,S,T,U);let ta,Ha,Oa,Pa;for(let ya=0;ya<T;ya+=pa,aa+=4){const bc=B[aa+0];var M=B[aa+1];const cc=B[aa+2],dc=B[aa+3],ec=M&2?Jb:Ib;M=(!(M&
22
- 1)||tb)&&$b[(ec<<8)+bc];Oa!==cc&&(void 0!==Oa&&(L.fillStyle=c(Oa),L.fillRect(Pa,ja,ya-Pa,U)),Oa=cc,Pa=ya);ta!==dc&&(void 0!==ta&&(qa.fillStyle=c(ta),qa.fillRect(Ha,0,ya-Ha,U)),ta=dc,Ha=ya);M&&qa.drawImage(A,bc*pa,ec*U,pa,U,ya,S,pa,U)}qa.fillStyle=c(ta);qa.fillRect(Ha,0,T-Ha,U);qa.globalCompositeOperation="destination-in";qa.drawImage(x,0,S,T,U,0,0,T,U);qa.globalCompositeOperation="source-over";L.fillStyle=c(Oa);L.fillRect(Pa,ja,T-Pa,U);L.drawImage(x,0,0,T,U,0,ja,T,U)}V&&(tb&&vb&&C[q]&&(L.fillStyle=
23
- c(B[4*(q*w+r)+3]),L.fillRect(r*pa,q*U+ub,pa,Kb-ub+1)),C.fill(0));V&&l.drawImage(L.canvas,0,0)}};this.destroy=function(){wb&&(cancelAnimationFrame(wb),wb=0)};this.pause=function(){Mb=!0;p.classList.remove("blinking-cursor")};this.continue=function(){Mb=!1;p.classList.add("blinking-cursor")};this.set_mode=function(A){u=A?1:a.use_graphical_text?2:0;0===u?(n.style.display="block",h.style.display="none"):(n.style.display="none",h.style.display="block",2===u&&C&&C.fill(1))};this.set_font_bitmap=function(A,
24
- x,H,M,T,S){const V=H?16:x?9:8;if(U!==A||pa!==V||Gb!==x||Fb!==H||Hb!==M||S)S=pa!==V||U!==A,U=A,pa=V,Gb=x,Fb=H,Hb=M,2===u&&(d(T),C.fill(1),S&&this.set_size_graphical_text())};this.set_font_page=function(A,x){if(Ib!==A||Jb!==x)Ib=A,Jb=x,C.fill(1)};this.clear_screen=function(){l.fillStyle="#000";l.fillRect(0,0,h.width,h.height)};this.set_size_graphical_text=function(){if(Xa){var A=pa*w,x=U*G,H=2*U;L&&L.canvas.width===A&&L.canvas.height===x&&qa.canvas.height===H||(L?(L.canvas.width=A,L.canvas.height=x,
25
- qa.canvas.width=A,qa.canvas.height=H):(L=(new OffscreenCanvas(A,x)).getContext("2d",{alpha:!1}),qa=(new OffscreenCanvas(A,H)).getContext("2d")),this.set_size_graphical(A,x,A,x),C.fill(1))}};this.set_size_text=function(A,x){if(A!==w||x!==G)if(C=new Int8Array(x),B=new Int32Array(A*x*4),w=A,G=x,0===u){for(;n.childNodes.length>x;)n.removeChild(n.firstChild);for(;n.childNodes.length<x;)n.appendChild(document.createElement("div"));for(A=0;A<x;A++)this.text_update_row(A);e(n,t,v,!0)}else 2===u&&this.set_size_graphical_text()};
26
- this.set_size_graphical=function(A,x){h.style.display="block";h.width=A;h.height=x;l.imageSmoothingEnabled=!1;y=640>=A&&2*A<window.innerWidth*window.devicePixelRatio&&2*x<window.innerHeight*window.devicePixelRatio?2:1;e(h,t*y,v*y,!1)};this.set_charmap=function(A){kb=A||Lb};this.set_scale=function(A,x){t=A;v=x;e(n,t,v,!0);e(h,t*y,v*y,!1)};this.update_cursor_scanline=function(A,x,H){if(A!==ub||x!==Kb||H!==vb)0===u?H?(p.style.display="inline",p.style.height=x-A+"px",p.style.marginTop=A+"px"):p.style.display=
27
- "none":2===u&&q<G&&(C[q]=1),ub=A,Kb=x,vb=H};this.update_cursor=function(A,x){if(A!==q||x!==r)A<G&&(C[A]=1),q<G&&(C[q]=1),q=A,r=x};this.text_update_row=function(A){var x=4*A*w,H;var M=n.childNodes[A];var T=document.createElement("div");for(var S=0;S<w;){var V=document.createElement("span");var ma=B[x+1]&1;var ja=B[x+2];var aa=B[x+3];ma&&V.classList.add("blink");V.style.backgroundColor=c(ja);V.style.color=c(aa);for(H="";S<w&&(B[x+1]&1)===ma&&B[x+2]===ja&&B[x+3]===aa;){var ta=B[x+0];H+=kb[ta];m(kb[ta]);
28
- S++;x+=4;if(A===q)if(S===r)break;else if(S===r+1){p.style.backgroundColor=V.style.color;T.appendChild(p);break}}V.textContent=H;T.appendChild(V)}M.parentNode.replaceChild(T,M)};this.update_buffer=function(A){for(const x of A)l.putImageData(x.image_data,x.screen_x-x.buffer_x,x.screen_y-x.buffer_y,x.buffer_x,x.buffer_y,x.buffer_width,x.buffer_height)};this.get_text_screen=function(){for(var A=[],x=0;x<G;x++)A.push(this.get_text_row(x));return A};this.get_text_row=function(A){let x="";for(let H=0;H<
29
- w;H++)x+=kb[B[4*(A*w+H)]];return x};this.init()}function Aa(a){m(a instanceof ArrayBuffer);this.buffer=a;this.byteLength=a.byteLength;this.onprogress=this.onload=void 0}Aa.prototype.load=function(){this.onload&&this.onload({buffer:this.buffer})};Aa.prototype.get=function(a,b,c){m(a+b<=this.byteLength);c(new Uint8Array(this.buffer,a,b))};Aa.prototype.set=function(a,b,c){m(a+b.byteLength<=this.byteLength);(new Uint8Array(this.buffer,a,b.byteLength)).set(b);c()};Aa.prototype.get_buffer=function(a){a(this.buffer)};
30
- Aa.prototype.get_state=function(){const a=[];a[0]=this.byteLength;a[1]=new Uint8Array(this.buffer);return a};Aa.prototype.set_state=function(a){this.byteLength=a[0];this.buffer=a[1].slice().buffer};function Ba(a,b,c){this.filename=a;this.byteLength=b;this.block_cache=new Map;this.block_cache_is_write=new Set;this.fixed_chunk_size=c;this.cache_reads=!!c;this.onprogress=this.onload=void 0}Ba.prototype.load=function(){void 0!==this.byteLength?this.onload&&this.onload(Object.create(null)):Ca(this.filename,
31
- (a,b)=>{if(a)throw Error("Cannot use: "+this.filename+". "+a);m(0<=b);this.byteLength=b;this.onload&&this.onload(Object.create(null))})};Ba.prototype.get_from_cache=function(a,b){var c=b/256;a/=256;for(var d=0;d<c;d++)if(!this.block_cache.get(a+d))return;if(1===c)return this.block_cache.get(a);b=new Uint8Array(b);for(d=0;d<c;d++)b.set(this.block_cache.get(a+d),256*d);return b};Ba.prototype.get=function(a,b,c){m(a+b<=this.byteLength);m(0===a%256);m(0===b%256);m(b);var d=this.get_from_cache(a,b);if(d)c(d);
32
- else{var e=a,f=b;this.fixed_chunk_size&&(e=a-a%this.fixed_chunk_size,f=Math.ceil((a-e+b)/this.fixed_chunk_size)*this.fixed_chunk_size);va(this.filename,{done:function(h){h=new Uint8Array(h);this.handle_read(e,f,h);e===a&&f===b?c(h):c(h.subarray(a-e,a-e+b))}.bind(this),range:{start:e,length:f}})}};Ba.prototype.set=function(a,b,c){var d=b.length;m(a+b.byteLength<=this.byteLength);m(0===a%256);m(0===d%256);m(d);a/=256;d/=256;for(var e=0;e<d;e++){var f=this.block_cache.get(a+e);if(void 0===f)f=b.slice(256*
33
- e,256*(e+1)),this.block_cache.set(a+e,f);else{const h=b.subarray(256*e,256*(e+1));m(f.byteLength===h.length);f.set(h)}this.block_cache_is_write.add(a+e)}c()};Ba.prototype.handle_read=function(a,b,c){a/=256;b/=256;for(var d=0;d<b;d++){const e=this.block_cache.get(a+d);e?c.set(e,256*d):this.cache_reads&&this.block_cache.set(a+d,c.slice(256*d,256*(d+1)))}};Ba.prototype.get_buffer=function(a){a()};Ba.prototype.get_state=function(){const a=[],b=[];for(const [c,d]of this.block_cache)m(isFinite(c)),this.block_cache_is_write.has(c)&&
34
- b.push([c,d]);a[0]=b;return a};Ba.prototype.set_state=function(a){a=a[0];this.block_cache.clear();this.block_cache_is_write.clear();for(const [b,c]of a)m(isFinite(b)),this.block_cache.set(b,c),this.block_cache_is_write.add(b)};function Da(a,b,c,d,e){const f=a.match(/\.[^\.]+(\.zst)?$/);this.extension=f?f[0]:"";this.basename=a.substring(0,a.length-this.extension.length);this.is_zstd=this.extension.endsWith(".zst");this.basename.endsWith("/")||(this.basename+="-");this.block_cache=new Map;this.block_cache_is_write=
35
- new Set;this.byteLength=b;this.fixed_chunk_size=c;this.partfile_alt_format=!!d;this.zstd_decompress=e;this.cache_reads=!!c;this.onprogress=this.onload=void 0}Da.prototype.load=function(){void 0===this.byteLength&&m(!1);this.onload&&this.onload(Object.create(null))};Da.prototype.get=function(a,b,c){m(a+b<=this.byteLength);m(0===a%256);m(0===b%256);m(b);var d=this.get_from_cache(a,b);if(d)c(d);else if(this.fixed_chunk_size){const f=Math.floor(a/this.fixed_chunk_size),h=a-f*this.fixed_chunk_size;m(0<=
36
- h);const l=Math.ceil((h+b)/this.fixed_chunk_size),n=new Uint8Array(l*this.fixed_chunk_size);let p=0;for(let q=0;q<l;q++){var e=(f+q)*this.fixed_chunk_size;d=this.partfile_alt_format?this.basename+(f+q+"").padStart(8,"0")+this.extension:this.basename+e+"-"+(e+this.fixed_chunk_size)+this.extension;(e=this.get_from_cache(e,this.fixed_chunk_size))?(n.set(e,q*this.fixed_chunk_size),p++,p===l&&c(n.subarray(h,h+b))):va(d,{done:async function(r){r=new Uint8Array(r);this.is_zstd&&(r=await this.zstd_decompress(this.fixed_chunk_size,
37
- r),r=new Uint8Array(r));n.set(r,q*this.fixed_chunk_size);this.handle_read((f+q)*this.fixed_chunk_size,this.fixed_chunk_size|0,r);p++;p===l&&c(n.subarray(h,h+b))}.bind(this)})}}else va(this.basename+a+"-"+(a+b)+this.extension,{done:function(f){m(f.byteLength===b);f=new Uint8Array(f);this.handle_read(a,b,f);c(f)}.bind(this)})};Da.prototype.get_from_cache=Ba.prototype.get_from_cache;Da.prototype.set=Ba.prototype.set;Da.prototype.handle_read=Ba.prototype.handle_read;Da.prototype.get_state=Ba.prototype.get_state;
38
- Da.prototype.set_state=Ba.prototype.set_state;function Ea(a){this.file=a;this.byteLength=a.size;1073741824<a.size&&console.warn("SyncFileBuffer: Allocating buffer of "+(a.size>>20)+" MB ...");this.buffer=new ArrayBuffer(a.size);this.onprogress=this.onload=void 0}Ea.prototype.load=function(){this.load_next(0)};Ea.prototype.load_next=function(a){var b=new FileReader;b.onload=function(d){d=new Uint8Array(d.target.result);(new Uint8Array(this.buffer,a)).set(d);this.load_next(a+4194304)}.bind(this);if(this.onprogress)this.onprogress({loaded:a,
39
- total:this.byteLength,lengthComputable:!0});if(a<this.byteLength){var c=this.file.slice(a,Math.min(a+4194304,this.byteLength));b.readAsArrayBuffer(c)}else this.file=void 0,this.onload&&this.onload({buffer:this.buffer})};Ea.prototype.get=Aa.prototype.get;Ea.prototype.set=Aa.prototype.set;Ea.prototype.get_buffer=Aa.prototype.get_buffer;Ea.prototype.get_state=Aa.prototype.get_state;Ea.prototype.set_state=Aa.prototype.set_state;function Fa(a){this.file=a;this.byteLength=a.size;this.block_cache=new Map;
40
- this.block_cache_is_write=new Set;this.onprogress=this.onload=void 0}Fa.prototype.load=function(){this.onload&&this.onload(Object.create(null))};Fa.prototype.get=function(a,b,c){m(0===a%256);m(0===b%256);m(b);var d=this.get_from_cache(a,b);d?c(d):(d=new FileReader,d.onload=function(e){e=new Uint8Array(e.target.result);this.handle_read(a,b,e);c(e)}.bind(this),d.readAsArrayBuffer(this.file.slice(a,a+b)))};Fa.prototype.get_from_cache=Ba.prototype.get_from_cache;Fa.prototype.set=Ba.prototype.set;Fa.prototype.handle_read=
41
- Ba.prototype.handle_read;Fa.prototype.get_state=Ba.prototype.get_state;Fa.prototype.set_state=Ba.prototype.set_state;Fa.prototype.get_buffer=function(a){a()};Fa.prototype.get_as_file=function(a){for(var b=[],c=Array.from(this.block_cache.keys()).sort(function(l,n){return l-n}),d=0,e=0;e<c.length;e++){var f=c[e],h=this.block_cache.get(f);f*=256;m(f>=d);f!==d&&(b.push(this.file.slice(d,f)),d=f);b.push(h);d+=h.length}d!==this.file.size&&b.push(this.file.slice(d));a=new File(b,a);m(a.size===this.file.size);
42
- return a};var Ca;Ca="undefined"===typeof XMLHttpRequest?function(a,b){import("node:fs").then(c=>c.stat(a,(d,e)=>{d?b(d):b(null,e.size)}))}:function(a,b){va(a,{done:(c,d)=>{c=d.getResponseHeader("Content-Range")||"";(d=c.match(/\/(\d+)\s*$/))?b(null,+d[1]):b("`Range: bytes=...` header not supported (Got `"+c+"`)")},headers:{Range:"bytes=0-0","X-Accept-Encoding":"identity"}})};function Ga(a,b){if(a.buffer instanceof ArrayBuffer)return new Aa(a.buffer);if("undefined"!==typeof File&&a.buffer instanceof
43
- File)return b=a.async,void 0===b&&(b=268435456<=a.buffer.size),b?new Fa(a.buffer):new Ea(a.buffer);if(a.url)return a.use_parts?new Da(a.url,a.size,a.fixed_chunk_size,!1,b):new Ba(a.url,a.size,a.fixed_chunk_size);D("Ignored file: url="+a.url+" buffer="+a.buffer)}function E(a){this.cpu=a;this.channel_page=new Uint8Array(8);this.channel_pagehi=new Uint8Array(8);this.channel_addr=new Uint16Array(8);this.channel_addr_init=new Uint16Array(8);this.channel_count=new Uint16Array(8);this.channel_count_init=
44
- new Uint16Array(8);this.channel_mask=new Uint8Array(8);this.channel_mode=new Uint8Array(8);this.unmask_listeners=[];this.lsb_msb_flipflop=0;a=a.io;a.register_write(0,this,this.port_addr_write.bind(this,0));a.register_write(2,this,this.port_addr_write.bind(this,1));a.register_write(4,this,this.port_addr_write.bind(this,2));a.register_write(6,this,this.port_addr_write.bind(this,3));a.register_write(1,this,this.port_count_write.bind(this,0));a.register_write(3,this,this.port_count_write.bind(this,1));
45
- a.register_write(5,this,this.port_count_write.bind(this,2));a.register_write(7,this,this.port_count_write.bind(this,3));a.register_read(0,this,this.port_addr_read.bind(this,0));a.register_read(2,this,this.port_addr_read.bind(this,1));a.register_read(4,this,this.port_addr_read.bind(this,2));a.register_read(6,this,this.port_addr_read.bind(this,3));a.register_read(1,this,this.port_count_read.bind(this,0));a.register_read(3,this,this.port_count_read.bind(this,1));a.register_read(5,this,this.port_count_read.bind(this,
46
- 2));a.register_read(7,this,this.port_count_read.bind(this,3));a.register_write(192,this,this.port_addr_write.bind(this,4));a.register_write(196,this,this.port_addr_write.bind(this,5));a.register_write(200,this,this.port_addr_write.bind(this,6));a.register_write(204,this,this.port_addr_write.bind(this,7));a.register_write(194,this,this.port_count_write.bind(this,4));a.register_write(198,this,this.port_count_write.bind(this,5));a.register_write(202,this,this.port_count_write.bind(this,6));a.register_write(206,
47
- this,this.port_count_write.bind(this,7));a.register_read(192,this,this.port_addr_read.bind(this,4));a.register_read(196,this,this.port_addr_read.bind(this,5));a.register_read(200,this,this.port_addr_read.bind(this,6));a.register_read(204,this,this.port_addr_read.bind(this,7));a.register_read(194,this,this.port_count_read.bind(this,4));a.register_read(198,this,this.port_count_read.bind(this,5));a.register_read(202,this,this.port_count_read.bind(this,6));a.register_read(206,this,this.port_count_read.bind(this,
48
- 7));a.register_write(135,this,this.port_page_write.bind(this,0));a.register_write(131,this,this.port_page_write.bind(this,1));a.register_write(129,this,this.port_page_write.bind(this,2));a.register_write(130,this,this.port_page_write.bind(this,3));a.register_write(143,this,this.port_page_write.bind(this,4));a.register_write(139,this,this.port_page_write.bind(this,5));a.register_write(137,this,this.port_page_write.bind(this,6));a.register_write(138,this,this.port_page_write.bind(this,7));a.register_read(135,
49
- this,this.port_page_read.bind(this,0));a.register_read(131,this,this.port_page_read.bind(this,1));a.register_read(129,this,this.port_page_read.bind(this,2));a.register_read(130,this,this.port_page_read.bind(this,3));a.register_read(143,this,this.port_page_read.bind(this,4));a.register_read(139,this,this.port_page_read.bind(this,5));a.register_read(137,this,this.port_page_read.bind(this,6));a.register_read(138,this,this.port_page_read.bind(this,7));a.register_write(1159,this,this.port_pagehi_write.bind(this,
50
- 0));a.register_write(1155,this,this.port_pagehi_write.bind(this,1));a.register_write(1153,this,this.port_pagehi_write.bind(this,2));a.register_write(1154,this,this.port_pagehi_write.bind(this,3));a.register_write(1163,this,this.port_pagehi_write.bind(this,5));a.register_write(1161,this,this.port_pagehi_write.bind(this,6));a.register_write(1162,this,this.port_pagehi_write.bind(this,7));a.register_read(1159,this,this.port_pagehi_read.bind(this,0));a.register_read(1155,this,this.port_pagehi_read.bind(this,
51
- 1));a.register_read(1153,this,this.port_pagehi_read.bind(this,2));a.register_read(1154,this,this.port_pagehi_read.bind(this,3));a.register_read(1163,this,this.port_pagehi_read.bind(this,5));a.register_read(1161,this,this.port_pagehi_read.bind(this,6));a.register_read(1162,this,this.port_pagehi_read.bind(this,7));a.register_write(10,this,this.port_singlemask_write.bind(this,0));a.register_write(212,this,this.port_singlemask_write.bind(this,4));a.register_write(15,this,this.port_multimask_write.bind(this,
52
- 0));a.register_write(222,this,this.port_multimask_write.bind(this,4));a.register_read(15,this,this.port_multimask_read.bind(this,0));a.register_read(222,this,this.port_multimask_read.bind(this,4));a.register_write(11,this,this.port_mode_write.bind(this,0));a.register_write(214,this,this.port_mode_write.bind(this,4));a.register_write(12,this,this.portC_write);a.register_write(216,this,this.portC_write)}E.prototype.get_state=function(){return[this.channel_page,this.channel_pagehi,this.channel_addr,
53
- this.channel_addr_init,this.channel_count,this.channel_count_init,this.channel_mask,this.channel_mode,this.lsb_msb_flipflop]};E.prototype.set_state=function(a){this.channel_page=a[0];this.channel_pagehi=a[1];this.channel_addr=a[2];this.channel_addr_init=a[3];this.channel_count=a[4];this.channel_count_init=a[5];this.channel_mask=a[6];this.channel_mode=a[7];this.lsb_msb_flipflop=a[8]};E.prototype.port_count_write=function(a,b){D("count write ["+a+"] = "+z(b),16);this.channel_count[a]=this.flipflop_get(this.channel_count[a],
54
- b,!1);this.channel_count_init[a]=this.flipflop_get(this.channel_count_init[a],b,!0)};E.prototype.port_count_read=function(a){D("count read ["+a+"] -> "+z(this.channel_count[a]),16);return this.flipflop_read(this.channel_count[a])};E.prototype.port_addr_write=function(a,b){D("addr write ["+a+"] = "+z(b),16);this.channel_addr[a]=this.flipflop_get(this.channel_addr[a],b,!1);this.channel_addr_init[a]=this.flipflop_get(this.channel_addr_init[a],b,!0)};E.prototype.port_addr_read=function(a){D("addr read ["+
55
- a+"] -> "+z(this.channel_addr[a]),16);return this.flipflop_read(this.channel_addr[a])};E.prototype.port_pagehi_write=function(a,b){D("pagehi write ["+a+"] = "+z(b),16);this.channel_pagehi[a]=b};E.prototype.port_pagehi_read=function(a){D("pagehi read ["+a+"]",16);return this.channel_pagehi[a]};E.prototype.port_page_write=function(a,b){D("page write ["+a+"] = "+z(b),16);this.channel_page[a]=b};E.prototype.port_page_read=function(a){D("page read ["+a+"]",16);return this.channel_page[a]};E.prototype.port_singlemask_write=
56
- function(a,b){a=(b&3)+a;b=b&4?1:0;D("singlechannel mask write ["+a+"] = "+b,16);this.update_mask(a,b)};E.prototype.port_multimask_write=function(a,b){D("multichannel mask write: "+z(b),16);for(var c=0;4>c;c++)this.update_mask(a+c,b&1<<c)};E.prototype.port_multimask_read=function(a){var b=0|this.channel_mask[a+0];b|=this.channel_mask[a+1]<<1;b|=this.channel_mask[a+2]<<2;b|=this.channel_mask[a+3]<<3;D("multichannel mask read: "+z(b),16);return b};E.prototype.port_mode_write=function(a,b){a=(b&3)+a;
57
- D("mode write ["+a+"] = "+z(b),16);this.channel_mode[a]=b};E.prototype.portC_write=function(){D("flipflop reset",16);this.lsb_msb_flipflop=0};E.prototype.on_unmask=function(a,b){this.unmask_listeners.push({fn:a,this_value:b})};E.prototype.update_mask=function(a,b){if(this.channel_mask[a]!==b&&(this.channel_mask[a]=b,!b))for(D("firing on_unmask("+a+")",16),b=0;b<this.unmask_listeners.length;b++)this.unmask_listeners[b].fn.call(this.unmask_listeners[b].this_value,a)};E.prototype.do_read=function(a,
58
- b,c,d,e){var f=this.count_get_8bit(d),h=this.address_get_8bit(d);D("DMA write channel "+d,16);D("to "+z(h)+" len "+z(f),16);c<f&&D("DMA should read more than provided: "+z(c)+" "+z(f),16);if(b+f>a.byteLength)D("DMA read outside of buffer",16),e(!0);else{var l=this.cpu;this.channel_addr[d]+=f;a.get(b,f,function(n){l.write_blob(n,h);e(!1)})}};E.prototype.do_write=function(a,b,c,d,e){var f=this.channel_count[d]+1&65535,h=5<=d?2:1,l=f*h,n=this.address_get_8bit(d),p=!1,q=!1,r=this.channel_mode[d]&16;D("DMA write channel "+
59
- d,16);D("to "+z(n)+" len "+z(l),16);c<l?(D("DMA should read more than provided",16),f=Math.floor(c/h),l=f*h,p=!0):c>l&&(D("DMA attempted to read more than provided",16),q=!0);b+l>a.byteLength?(D("DMA write outside of buffer",16),e(!0)):(this.channel_addr[d]+=f,this.channel_count[d]-=f,!p&&r&&(D("DMA autoinit",16),this.channel_addr[d]=this.channel_addr_init[d],this.channel_count[d]=this.channel_count_init[d]),a.set(b,this.cpu.mem8.subarray(n,n+l),()=>{q&&r?(D("DMA continuing from start",16),this.do_write(a,
60
- b+l,c-l,d,e)):e(!1)}))};E.prototype.address_get_8bit=function(a){var b=this.channel_addr[a];5<=a&&(b<<=1);b=b&65535|this.channel_page[a]<<16;return b|=this.channel_pagehi[a]<<24};E.prototype.count_get_8bit=function(a){var b=this.channel_count[a]+1;5<=a&&(b*=2);return b};E.prototype.flipflop_get=function(a,b,c){c||(this.lsb_msb_flipflop^=1);return this.lsb_msb_flipflop?a&-256|b:a&-65281|b<<8};E.prototype.flipflop_read=function(a){return(this.lsb_msb_flipflop^=1)?a&255:a>>8&255};function Ia(a){this.ports=
61
- [];this.cpu=a;for(var b=0;65536>b;b++)this.ports[b]=this.create_empty_entry();var c=a.memory_size[0];for(b=0;b<<17<c;b++)a.memory_map_read8[b]=a.memory_map_write8[b]=void 0,a.memory_map_read32[b]=a.memory_map_write32[b]=void 0;this.mmap_register(c,4294967296-c,function(d){D("Read from unmapped memory space, addr="+z(d>>>0,8),32);return 255},function(d,e){D("Write to unmapped memory space, addr="+z(d>>>0,8)+" value="+z(e,2),32)},function(d){D("Read from unmapped memory space, addr="+z(d>>>0,8),32);
62
- return-1},function(d,e){D("Write to unmapped memory space, addr="+z(d>>>0,8)+" value="+z(e>>>0,8),32)})}Ia.prototype.create_empty_entry=function(){return{read8:this.empty_port_read8,read16:this.empty_port_read16,read32:this.empty_port_read32,write8:this.empty_port_write,write16:this.empty_port_write,write32:this.empty_port_write,device:void 0}};Ia.prototype.empty_port_read8=function(){return 255};Ia.prototype.empty_port_read16=function(){return 65535};Ia.prototype.empty_port_read32=function(){return-1};
63
- Ia.prototype.empty_port_write=function(){};Ia.prototype.register_read=function(a,b,c,d,e){function f(h){m(!1,"Overlapped read"+h+" "+z(a,4)+" ("+b.name+")");return-1>>>32-h|0}m("number"===typeof a);m("object"===typeof b);m(!c||"function"===typeof c);m(!d||"function"===typeof d);m(!e||"function"===typeof e);m(c||d||e);c||(c=f.bind(this,8));d||(d=f.bind(this,16));e||(e=f.bind(this,32));c&&(this.ports[a].read8=c);d&&(this.ports[a].read16=d);e&&(this.ports[a].read32=e);this.ports[a].device=b};Ia.prototype.register_write=
64
- function(a,b,c,d,e){function f(h){m(!1,"Overlapped write"+h+" "+z(a)+" ("+b.name+")")}m("number"===typeof a);m("object"===typeof b);m(!c||"function"===typeof c);m(!d||"function"===typeof d);m(!e||"function"===typeof e);m(c||d||e);c||(c=f.bind(this,8));d||(d=f.bind(this,16));e||(e=f.bind(this,32));c&&(this.ports[a].write8=c);d&&(this.ports[a].write16=d);e&&(this.ports[a].write32=e);this.ports[a].device=b};Ia.prototype.register_read_consecutive=function(a,b,c,d,e,f){function h(){return c.call(this)|
65
- d.call(this)<<8}function l(){return e.call(this)|f.call(this)<<8}function n(){return c.call(this)|d.call(this)<<8|e.call(this)<<16|f.call(this)<<24}m(4===arguments.length||6===arguments.length);e&&f?(this.register_read(a,b,c,h,n),this.register_read(a+1,b,d),this.register_read(a+2,b,e,l),this.register_read(a+3,b,f)):(this.register_read(a,b,c,h),this.register_read(a+1,b,d))};Ia.prototype.register_write_consecutive=function(a,b,c,d,e,f){function h(p){c.call(this,p&255);d.call(this,p>>8&255)}function l(p){e.call(this,
66
- p&255);f.call(this,p>>8&255)}function n(p){c.call(this,p&255);d.call(this,p>>8&255);e.call(this,p>>16&255);f.call(this,p>>>24)}m(4===arguments.length||6===arguments.length);e&&f?(this.register_write(a,b,c,h,n),this.register_write(a+1,b,d),this.register_write(a+2,b,e,l),this.register_write(a+3,b,f)):(this.register_write(a,b,c,h),this.register_write(a+1,b,d))};Ia.prototype.mmap_read32_shim=function(a){var b=this.cpu.memory_map_read8[a>>>17];return b(a)|b(a+1)<<8|b(a+2)<<16|b(a+3)<<24};Ia.prototype.mmap_write32_shim=
67
- function(a,b){var c=this.cpu.memory_map_write8[a>>>17];c(a,b&255);c(a+1,b>>8&255);c(a+2,b>>16&255);c(a+3,b>>>24)};Ia.prototype.mmap_register=function(a,b,c,d,e,f){D("mmap_register addr="+z(a>>>0,8)+" size="+z(b,8),32);m(0===(a&131071));m(b&&0===(b&131071));e||(e=this.mmap_read32_shim.bind(this));f||(f=this.mmap_write32_shim.bind(this));for(a>>>=17;0<b;a++)this.cpu.memory_map_read8[a]=c,this.cpu.memory_map_write8[a]=d,this.cpu.memory_map_read32[a]=e,this.cpu.memory_map_write32[a]=f,b-=131072};Ia.prototype.port_write8=
68
- function(a,b){var c=this.ports[a];c.write8===this.empty_port_write&&D("write8 port #"+z(a,4)+" <- "+z(b,2)+this.get_port_description(a),32);return c.write8.call(c.device,b)};Ia.prototype.port_write16=function(a,b){var c=this.ports[a];c.write16===this.empty_port_write&&D("write16 port #"+z(a,4)+" <- "+z(b,4)+this.get_port_description(a),32);return c.write16.call(c.device,b)};Ia.prototype.port_write32=function(a,b){var c=this.ports[a];c.write32===this.empty_port_write&&D("write32 port #"+z(a,4)+" <- "+
69
- z(b>>>0,8)+this.get_port_description(a),32);return c.write32.call(c.device,b)};Ia.prototype.port_read8=function(a){var b=this.ports[a];b.read8===this.empty_port_read8&&D("read8 port #"+z(a,4)+this.get_port_description(a),32);b=b.read8.call(b.device,a);m("number"===typeof b);m(256>b&&0<=b,"8 bit port returned large value: "+z(a));return b};Ia.prototype.port_read16=function(a){var b=this.ports[a];b.read16===this.empty_port_read16&&D("read16 port #"+z(a,4)+this.get_port_description(a),32);b=b.read16.call(b.device,
70
- a);m("number"===typeof b);m(65536>b&&0<=b,"16 bit port returned large value: "+z(a));return b};Ia.prototype.port_read32=function(a){var b=this.ports[a];b.read32===this.empty_port_read32&&D("read32 port #"+z(a,4)+this.get_port_description(a),32);a=b.read32.call(b.device,a);m((a|0)===a);return a};var Ja={4:"PORT_DMA_ADDR_2",5:"PORT_DMA_CNT_2",10:"PORT_DMA1_MASK_REG",11:"PORT_DMA1_MODE_REG",12:"PORT_DMA1_CLEAR_FF_REG",13:"PORT_DMA1_MASTER_CLEAR",32:"PORT_PIC1_CMD",33:"PORT_PIC1_DATA",64:"PORT_PIT_COUNTER0",
71
- 65:"PORT_PIT_COUNTER1",66:"PORT_PIT_COUNTER2",67:"PORT_PIT_MODE",96:"PORT_PS2_DATA",97:"PORT_PS2_CTRLB",100:"PORT_PS2_STATUS",112:"PORT_CMOS_INDEX",113:"PORT_CMOS_DATA",128:"PORT_DIAG",129:"PORT_DMA_PAGE_2",146:"PORT_A20",160:"PORT_PIC2_CMD",161:"PORT_PIC2_DATA",178:"PORT_SMI_CMD",179:"PORT_SMI_STATUS",212:"PORT_DMA2_MASK_REG",214:"PORT_DMA2_MODE_REG",218:"PORT_DMA2_MASTER_CLEAR",240:"PORT_MATH_CLEAR",368:"PORT_ATA2_CMD_BASE",496:"PORT_ATA1_CMD_BASE",632:"PORT_LPT2",744:"PORT_SERIAL4",760:"PORT_SERIAL2",
72
- 884:"PORT_ATA2_CTRL_BASE",888:"PORT_LPT1",1E3:"PORT_SERIAL3",1008:"PORT_FD_BASE",1010:"PORT_FD_DOR",1012:"PORT_FD_STATUS",1013:"PORT_FD_DATA",1014:"PORT_HD_DATA",1015:"PORT_FD_DIR",1016:"PORT_SERIAL1",3320:"PORT_PCI_CMD",3321:"PORT_PCI_REBOOT",3324:"PORT_PCI_DATA",1026:"PORT_BIOS_DEBUG",1296:"PORT_QEMU_CFG_CTL",1297:"PORT_QEMU_CFG_DATA",45056:"PORT_ACPI_PM_BASE",45312:"PORT_SMB_BASE",35072:"PORT_BIOS_APM"};Ia.prototype.get_port_description=function(a){return Ja[a]?" ("+Ja[a]+")":""};var Ka={};function La(){this.listeners=
73
- {};this.pair=void 0}La.prototype.register=function(a,b,c){var d=this.listeners[a];void 0===d&&(d=this.listeners[a]=[]);d.push({fn:b,this_value:c})};La.prototype.unregister=function(a,b){var c=this.listeners[a];void 0!==c&&(this.listeners[a]=c.filter(function(d){return d.fn!==b}))};La.prototype.send=function(a,b){if(this.pair&&(a=this.pair.listeners[a],void 0!==a))for(var c=0;c<a.length;c++){var d=a[c];d.fn.call(d.this_value,b)}};La.prototype.send_async=function(a,b){m(1===arguments.length||2===arguments.length);
74
- setTimeout(this.send.bind(this,a,b),0)};Ka.create=function(){var a=new La,b=new La;a.pair=b;b.pair=a;return[a,b]};var Ma=new Uint8Array(256),Na=[],Qa=[],Ra=[],Sa=new Uint8Array(256),Ta=[];function F(a,b){this.cpu=a;this.bus=b;this.write_buffer=new oa(64);this.read_buffer=new oa(64);this.mixer_current_address=this.command_size=this.command=this.read_buffer_lastvalue=0;this.mixer_registers=new Uint8Array(256);this.mixer_reset();this.dummy_speaker_enabled=!1;this.test_register=0;this.dsp_signed=this.dsp_16bit=
75
- this.dsp_stereo=this.dsp_highspeed=!1;this.dac_buffers=[new ra(65536),new ra(65536)];this.dma=a.devices.dma;this.dma_channel=this.dma_irq=this.dma_bytes_block=this.dma_bytes_left=this.dma_bytes_count=this.dma_sample_count=0;this.dma_channel_8bit=1;this.dma_channel_16bit=5;this.dma_autoinit=!1;this.dma_buffer=new ArrayBuffer(65536);this.dma_buffer_int8=new Int8Array(this.dma_buffer);this.dma_buffer_uint8=new Uint8Array(this.dma_buffer);this.dma_buffer_int16=new Int16Array(this.dma_buffer);this.dma_buffer_uint16=
76
- new Uint16Array(this.dma_buffer);this.dma_syncbuffer=new Aa(this.dma_buffer);this.dma_paused=this.dma_waiting_transfer=!1;this.sampling_rate=22050;b.send("dac-tell-sampling-rate",this.sampling_rate);this.bytes_per_sample=1;this.e2_value=170;this.e2_count=0;this.asp_registers=new Uint8Array(256);this.mpu_read_buffer=new oa(64);this.fm_current_address1=this.fm_current_address0=this.mpu_read_buffer_lastvalue=0;this.fm_waveform_select_enable=!1;this.irq=5;this.irq_triggered=new Uint8Array(16);a.io.register_read_consecutive(544,
77
- this,this.port2x0_read,this.port2x1_read,this.port2x2_read,this.port2x3_read);a.io.register_read_consecutive(904,this,this.port2x0_read,this.port2x1_read);a.io.register_read_consecutive(548,this,this.port2x4_read,this.port2x5_read);a.io.register_read(550,this,this.port2x6_read);a.io.register_read(551,this,this.port2x7_read);a.io.register_read(552,this,this.port2x8_read);a.io.register_read(553,this,this.port2x9_read);a.io.register_read(554,this,this.port2xA_read);a.io.register_read(555,this,this.port2xB_read);
78
- a.io.register_read(556,this,this.port2xC_read);a.io.register_read(557,this,this.port2xD_read);a.io.register_read_consecutive(558,this,this.port2xE_read,this.port2xF_read);a.io.register_write_consecutive(544,this,this.port2x0_write,this.port2x1_write,this.port2x2_write,this.port2x3_write);a.io.register_write_consecutive(904,this,this.port2x0_write,this.port2x1_write);a.io.register_write_consecutive(548,this,this.port2x4_write,this.port2x5_write);a.io.register_write(550,this,this.port2x6_write);a.io.register_write(551,
79
- this,this.port2x7_write);a.io.register_write_consecutive(552,this,this.port2x8_write,this.port2x9_write);a.io.register_write(554,this,this.port2xA_write);a.io.register_write(555,this,this.port2xB_write);a.io.register_write(556,this,this.port2xC_write);a.io.register_write(557,this,this.port2xD_write);a.io.register_write(558,this,this.port2xE_write);a.io.register_write(559,this,this.port2xF_write);a.io.register_read_consecutive(816,this,this.port3x0_read,this.port3x1_read);a.io.register_write_consecutive(816,
80
- this,this.port3x0_write,this.port3x1_write);this.dma.on_unmask(this.dma_on_unmask,this);b.register("dac-request-data",function(){this.dac_handle_request()},this);b.register("speaker-has-initialized",function(){this.mixer_reset()},this);b.send("speaker-confirm-initialized");this.dsp_reset()}F.prototype.dsp_reset=function(){this.write_buffer.clear();this.read_buffer.clear();this.command_size=this.command=0;this.dummy_speaker_enabled=!1;this.test_register=0;this.dsp_signed=this.dsp_16bit=this.dsp_stereo=
81
- this.dsp_highspeed=!1;this.dac_buffers[0].clear();this.dac_buffers[1].clear();this.dma_channel=this.dma_irq=this.dma_bytes_block=this.dma_bytes_left=this.dma_bytes_count=this.dma_sample_count=0;this.dma_autoinit=!1;this.dma_buffer_uint8.fill(0);this.dma_paused=this.dma_waiting_transfer=!1;this.e2_value=170;this.e2_count=0;this.sampling_rate=22050;this.bytes_per_sample=1;this.lower_irq(1);this.irq_triggered.fill(0);this.asp_registers.fill(0);this.asp_registers[5]=1;this.asp_registers[9]=248};F.prototype.get_state=
82
- function(){var a=[];a[2]=this.read_buffer_lastvalue;a[3]=this.command;a[4]=this.command_size;a[5]=this.mixer_current_address;a[6]=this.mixer_registers;a[7]=this.dummy_speaker_enabled;a[8]=this.test_register;a[9]=this.dsp_highspeed;a[10]=this.dsp_stereo;a[11]=this.dsp_16bit;a[12]=this.dsp_signed;a[15]=this.dma_sample_count;a[16]=this.dma_bytes_count;a[17]=this.dma_bytes_left;a[18]=this.dma_bytes_block;a[19]=this.dma_irq;a[20]=this.dma_channel;a[21]=this.dma_channel_8bit;a[22]=this.dma_channel_16bit;
83
- a[23]=this.dma_autoinit;a[24]=this.dma_buffer_uint8;a[25]=this.dma_waiting_transfer;a[26]=this.dma_paused;a[27]=this.sampling_rate;a[28]=this.bytes_per_sample;a[29]=this.e2_value;a[30]=this.e2_count;a[31]=this.asp_registers;a[33]=this.mpu_read_buffer_last_value;a[34]=this.irq;a[35]=this.irq_triggered;return a};F.prototype.set_state=function(a){this.read_buffer_lastvalue=a[2];this.command=a[3];this.command_size=a[4];this.mixer_current_address=a[5];this.mixer_registers=a[6];this.mixer_full_update();
84
- this.dummy_speaker_enabled=a[7];this.test_register=a[8];this.dsp_highspeed=a[9];this.dsp_stereo=a[10];this.dsp_16bit=a[11];this.dsp_signed=a[12];this.dma_sample_count=a[15];this.dma_bytes_count=a[16];this.dma_bytes_left=a[17];this.dma_bytes_block=a[18];this.dma_irq=a[19];this.dma_channel=a[20];this.dma_channel_8bit=a[21];this.dma_channel_16bit=a[22];this.dma_autoinit=a[23];this.dma_buffer_uint8=a[24];this.dma_waiting_transfer=a[25];this.dma_paused=a[26];this.sampling_rate=a[27];this.bytes_per_sample=
85
- a[28];this.e2_value=a[29];this.e2_count=a[30];this.asp_registers=a[31];this.mpu_read_buffer_last_value=a[33];this.irq=a[34];this.irq_triggered=a[35];this.dma_buffer=this.dma_buffer_uint8.buffer;this.dma_buffer_int8=new Int8Array(this.dma_buffer);this.dma_buffer_int16=new Int16Array(this.dma_buffer);this.dma_buffer_uint16=new Uint16Array(this.dma_buffer);this.dma_syncbuffer=new Aa(this.dma_buffer);this.dma_paused?this.bus.send("dac-disable"):this.bus.send("dac-enable")};F.prototype.port2x0_read=function(){D("220 read: fm music status port (unimplemented)",
86
- 8388608);return 255};F.prototype.port2x1_read=function(){D("221 read: fm music data port (write only)",8388608);return 255};F.prototype.port2x2_read=function(){D("222 read: advanced fm music status port (unimplemented)",8388608);return 255};F.prototype.port2x3_read=function(){D("223 read: advanced music data port (write only)",8388608);return 255};F.prototype.port2x4_read=function(){D("224 read: mixer address port",8388608);return this.mixer_current_address};F.prototype.port2x5_read=function(){D("225 read: mixer data port",
87
- 8388608);return this.mixer_read(this.mixer_current_address)};F.prototype.port2x6_read=function(){D("226 read: (write only)",8388608);return 255};F.prototype.port2x7_read=function(){D("227 read: undocumented",8388608);return 255};F.prototype.port2x8_read=function(){D("228 read: fm music status port (unimplemented)",8388608);return 255};F.prototype.port2x9_read=function(){D("229 read: fm music data port (write only)",8388608);return 255};F.prototype.port2xA_read=function(){D("22A read: read data",8388608);
88
- this.read_buffer.length&&(this.read_buffer_lastvalue=this.read_buffer.shift());D(" <- "+this.read_buffer_lastvalue+" "+z(this.read_buffer_lastvalue)+" '"+String.fromCharCode(this.read_buffer_lastvalue)+"'",8388608);return this.read_buffer_lastvalue};F.prototype.port2xB_read=function(){D("22B read: undocumented",8388608);return 255};F.prototype.port2xC_read=function(){D("22C read: write-buffer status",8388608);return 127};F.prototype.port2xD_read=function(){D("22D read: undocumented",8388608);return 255};
89
- F.prototype.port2xE_read=function(){D("22E read: read-buffer status / irq 8bit ack.",8388608);this.irq_triggered[1]&&this.lower_irq(1);return(this.read_buffer.length&&!this.dsp_highspeed)<<7|127};F.prototype.port2xF_read=function(){D("22F read: irq 16bit ack",8388608);this.lower_irq(2);return 0};F.prototype.port2x0_write=function(a){D("220 write: (unimplemented) fm register 0 address = "+z(a),8388608);this.fm_current_address0=0};F.prototype.port2x1_write=function(a){D("221 write: (unimplemented) fm register 0 data = "+
90
- z(a),8388608);var b=Ta[this.fm_current_address0];b||(b=this.fm_default_write);b.call(this,a,0,this.fm_current_address0)};F.prototype.port2x2_write=function(a){D("222 write: (unimplemented) fm register 1 address = "+z(a),8388608);this.fm_current_address1=0};F.prototype.port2x3_write=function(a){D("223 write: (unimplemented) fm register 1 data ="+z(a),8388608);var b=Ta[this.fm_current_address1];b||(b=this.fm_default_write);b.call(this,a,1,this.fm_current_address1)};F.prototype.port2x4_write=function(a){D("224 write: mixer address = "+
91
- z(a),8388608);this.mixer_current_address=a};F.prototype.port2x5_write=function(a){D("225 write: mixer data = "+z(a),8388608);this.mixer_write(this.mixer_current_address,a)};F.prototype.port2x6_write=function(a){D("226 write: reset = "+z(a),8388608);this.dsp_highspeed?(D(" -> exit highspeed",8388608),this.dsp_highspeed=!1):a&&(D(" -> reset",8388608),this.dsp_reset());this.read_buffer.clear();this.read_buffer.push(170)};F.prototype.port2x7_write=function(){D("227 write: undocumented",8388608)};F.prototype.port2x8_write=
92
- function(){D("228 write: fm music register port (unimplemented)",8388608)};F.prototype.port2x9_write=function(){D("229 write: fm music data port (unimplemented)",8388608)};F.prototype.port2xA_write=function(){D("22A write: dsp read data port (read only)",8388608)};F.prototype.port2xB_write=function(){D("22B write: undocumented",8388608)};F.prototype.port2xC_write=function(a){D("22C write: write command/data",8388608);0===this.command?(D("22C write: command = "+z(a),8388608),this.command=a,this.write_buffer.clear(),
93
- this.command_size=Ma[a]):(D("22C write: data: "+z(a),8388608),this.write_buffer.push(a));this.write_buffer.length>=this.command_size&&this.command_do()};F.prototype.port2xD_write=function(){D("22D write: undocumented",8388608)};F.prototype.port2xE_write=function(){D("22E write: dsp read buffer status (read only)",8388608)};F.prototype.port2xF_write=function(){D("22F write: undocumented",8388608)};F.prototype.port3x0_read=function(){D("330 read: mpu data",8388608);this.mpu_read_buffer.length&&(this.mpu_read_buffer_lastvalue=
94
- this.mpu_read_buffer.shift());D(" <- "+z(this.mpu_read_buffer_lastvalue),8388608);return this.mpu_read_buffer_lastvalue};F.prototype.port3x0_write=function(a){D("330 write: mpu data (unimplemented) : "+z(a),8388608)};F.prototype.port3x1_read=function(){D("331 read: mpu status",8388608);return 0|128*!this.mpu_read_buffer.length};F.prototype.port3x1_write=function(a){D("331 write: mpu command: "+z(a),8388608);255===a&&(this.mpu_read_buffer.clear(),this.mpu_read_buffer.push(254))};F.prototype.command_do=
95
- function(){var a=Na[this.command];a||(a=this.dsp_default_handler);a.call(this);this.command_size=this.command=0;this.write_buffer.clear()};F.prototype.dsp_default_handler=function(){D("Unhandled command: "+z(this.command),8388608)};function I(a,b,c){c||(c=F.prototype.dsp_default_handler);for(var d=0;d<a.length;d++)Ma[a[d]]=b,Na[a[d]]=c}function Ua(a){for(var b=[],c=0;16>c;c++)b.push(a+c);return b}I([14],2,function(){this.asp_registers[this.write_buffer.shift()]=this.write_buffer.shift()});I([15],
96
- 1,function(){this.read_buffer.clear();this.read_buffer.push(this.asp_registers[this.write_buffer.shift()])});I([16],1,function(){var a=this.write_buffer.shift();a=Va(a/127.5+-1,-1,1);this.dac_buffers[0].push(a);this.dac_buffers[1].push(a);this.bus.send("dac-enable")});I([20,21],2,function(){this.dma_irq=1;this.dma_channel=this.dma_channel_8bit;this.dsp_highspeed=this.dsp_16bit=this.dsp_signed=this.dma_autoinit=!1;this.dma_transfer_size_set();this.dma_transfer_start()});I([22],2);I([23],2);I([28],
97
- 0,function(){this.dma_irq=1;this.dma_channel=this.dma_channel_8bit;this.dma_autoinit=!0;this.dsp_highspeed=this.dsp_16bit=this.dsp_signed=!1;this.dma_transfer_start()});I([31],0);I([32],0,function(){this.read_buffer.clear();this.read_buffer.push(127)});I([36],2);I([44],0);I([48],0);I([49],0);I([52],0);I([53],0);I([54],0);I([55],0);I([56],0);I([64],1,function(){this.sampling_rate_change(1E6/(256-this.write_buffer.shift())/this.get_channel_count())});I([65,66],2,function(){this.sampling_rate_change(this.write_buffer.shift()<<
98
- 8|this.write_buffer.shift())});I([72],2,function(){this.dma_transfer_size_set()});I([116],2);I([117],2);I([118],2);I([119],2);I([125],0);I([127],0);I([128],2);I([144],0,function(){this.dma_irq=1;this.dma_channel=this.dma_channel_8bit;this.dma_autoinit=!0;this.dsp_signed=!1;this.dsp_highspeed=!0;this.dsp_16bit=!1;this.dma_transfer_start()});I([145],0);I([152],0);I([153],0);I([160],0);I([168],0);I(Ua(176),3,function(){if(this.command&8)this.dsp_default_handler();else{var a=this.write_buffer.shift();
99
- this.dma_irq=2;this.dma_channel=this.dma_channel_16bit;this.dma_autoinit=!!(this.command&4);this.dsp_signed=!!(a&16);this.dsp_stereo=!!(a&32);this.dsp_16bit=!0;this.dma_transfer_size_set();this.dma_transfer_start()}});I(Ua(192),3,function(){if(this.command&8)this.dsp_default_handler();else{var a=this.write_buffer.shift();this.dma_irq=1;this.dma_channel=this.dma_channel_8bit;this.dma_autoinit=!!(this.command&4);this.dsp_signed=!!(a&16);this.dsp_stereo=!!(a&32);this.dsp_16bit=!1;this.dma_transfer_size_set();
100
- this.dma_transfer_start()}});I([208],0,function(){this.dma_paused=!0;this.bus.send("dac-disable")});I([209],0,function(){this.dummy_speaker_enabled=!0});I([211],0,function(){this.dummy_speaker_enabled=!1});I([212],0,function(){this.dma_paused=!1;this.bus.send("dac-enable")});I([213],0,function(){this.dma_paused=!0;this.bus.send("dac-disable")});I([214],0,function(){this.dma_paused=!1;this.bus.send("dac-enable")});I([216],0,function(){this.read_buffer.clear();this.read_buffer.push(255*this.dummy_speaker_enabled)});
101
- I([217,218],0,function(){this.dma_autoinit=!1});I([224],1,function(){this.read_buffer.clear();this.read_buffer.push(~this.write_buffer.shift())});I([225],0,function(){this.read_buffer.clear();this.read_buffer.push(4);this.read_buffer.push(5)});I([226],1);I([227],0,function(){this.read_buffer.clear();for(var a=0;44>a;a++)this.read_buffer.push("COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.".charCodeAt(a));this.read_buffer.push(0)});I([228],1,function(){this.test_register=this.write_buffer.shift()});
102
- I([232],0,function(){this.read_buffer.clear();this.read_buffer.push(this.test_register)});I([242,243],0,function(){this.raise_irq()});var Wa=new Uint8Array(256);Wa[14]=255;Wa[15]=7;Wa[55]=56;I([249],1,function(){var a=this.write_buffer.shift();D("dsp 0xf9: unknown function. input: "+a,8388608);this.read_buffer.clear();this.read_buffer.push(Wa[a])});F.prototype.mixer_read=function(a){var b=Qa[a];b?b=b.call(this):(b=this.mixer_registers[a],D("unhandled mixer register read. addr:"+z(a)+" data:"+z(b),
103
- 8388608));return b};F.prototype.mixer_write=function(a,b){var c=Ra[a];c?c.call(this,b):D("unhandled mixer register write. addr:"+z(a)+" data:"+z(b),8388608)};F.prototype.mixer_default_read=function(){D("mixer register read. addr:"+z(this.mixer_current_address),8388608);return this.mixer_registers[this.mixer_current_address]};F.prototype.mixer_default_write=function(a){D("mixer register write. addr:"+z(this.mixer_current_address)+" data:"+z(a),8388608);this.mixer_registers[this.mixer_current_address]=
104
- a};F.prototype.mixer_reset=function(){this.mixer_registers[4]=204;this.mixer_registers[34]=204;this.mixer_registers[38]=204;this.mixer_registers[40]=0;this.mixer_registers[46]=0;this.mixer_registers[10]=0;this.mixer_registers[48]=192;this.mixer_registers[49]=192;this.mixer_registers[50]=192;this.mixer_registers[51]=192;this.mixer_registers[52]=192;this.mixer_registers[53]=192;this.mixer_registers[54]=0;this.mixer_registers[55]=0;this.mixer_registers[56]=0;this.mixer_registers[57]=0;this.mixer_registers[59]=
105
- 0;this.mixer_registers[60]=31;this.mixer_registers[61]=21;this.mixer_registers[62]=11;this.mixer_registers[63]=0;this.mixer_registers[64]=0;this.mixer_registers[65]=0;this.mixer_registers[66]=0;this.mixer_registers[67]=0;this.mixer_registers[68]=128;this.mixer_registers[69]=128;this.mixer_registers[70]=128;this.mixer_registers[71]=128;this.mixer_full_update()};F.prototype.mixer_full_update=function(){for(var a=1;a<this.mixer_registers.length;a++)Sa[a]||this.mixer_write(a,this.mixer_registers[a])};
106
- function Ya(a,b){b||(b=F.prototype.mixer_default_read);Qa[a]=b}function Za(a,b){b||(b=F.prototype.mixer_default_write);Ra[a]=b}function $a(a,b,c){Sa[a]=1;Qa[a]=function(){return this.mixer_registers[b]&240|this.mixer_registers[c]>>>4};Ra[a]=function(d){this.mixer_registers[a]=d;var e=d<<4&240|this.mixer_registers[c]&15;this.mixer_write(b,d&240|this.mixer_registers[b]&15);this.mixer_write(c,e)}}function ab(a,b,c){Qa[a]=F.prototype.mixer_default_read;Ra[a]=function(d){this.mixer_registers[a]=d;this.bus.send("mixer-volume",
107
- [b,c,(d>>>2)-62])}}Ya(0,function(){this.mixer_reset();return 0});Za(0);$a(4,50,51);$a(34,48,49);$a(38,52,53);$a(40,54,55);$a(46,56,57);ab(48,0,0);ab(49,0,1);ab(50,2,0);ab(51,2,1);Ya(59);Za(59,function(a){this.mixer_registers[59]=a;this.bus.send("mixer-volume",[1,2,6*(a>>>6)-18])});Ya(65);Za(65,function(a){this.mixer_registers[65]=a;this.bus.send("mixer-gain-left",6*(a>>>6))});Ya(66);Za(66,function(a){this.mixer_registers[66]=a;this.bus.send("mixer-gain-right",6*(a>>>6))});Ya(68);Za(68,function(a){this.mixer_registers[68]=
108
- a;a>>>=3;this.bus.send("mixer-treble-left",a-(16>a?14:16))});Ya(69);Za(69,function(a){this.mixer_registers[69]=a;a>>>=3;this.bus.send("mixer-treble-right",a-(16>a?14:16))});Ya(70);Za(70,function(a){this.mixer_registers[70]=a;a>>>=3;this.bus.send("mixer-bass-right",a-(16>a?14:16))});Ya(71);Za(71,function(a){this.mixer_registers[71]=a;a>>>=3;this.bus.send("mixer-bass-right",a-(16>a?14:16))});Ya(128,function(){switch(this.irq){case 2:return 1;case 5:return 2;case 7:return 4;case 10:return 8;default:return 0}});
109
- Za(128,function(a){a&1&&(this.irq=2);a&2&&(this.irq=5);a&4&&(this.irq=7);a&8&&(this.irq=10)});Ya(129,function(){var a=0;switch(this.dma_channel_8bit){case 0:a|=1;break;case 1:a|=2;break;case 3:a|=8}switch(this.dma_channel_16bit){case 5:a|=32;break;case 6:a|=64;break;case 7:a|=128}return a});Za(129,function(a){a&1&&(this.dma_channel_8bit=0);a&2&&(this.dma_channel_8bit=1);a&8&&(this.dma_channel_8bit=3);a&32&&(this.dma_channel_16bit=5);a&64&&(this.dma_channel_16bit=6);a&128&&(this.dma_channel_16bit=
110
- 7)});Ya(130,function(){for(var a=32,b=0;16>b;b++)a|=b*this.irq_triggered[b];return a});F.prototype.fm_default_write=function(a,b,c){D("unhandled fm register write. addr:"+b+"|"+z(c)+" data:"+z(a),8388608)};function bb(a,b){b||(b=F.prototype.fm_default_write);for(var c=0;c<a.length;c++)Ta[a[c]]=b}function cb(a,b){for(var c=[];a<=b;a++)c.push(a);return c}var db=new Uint8Array(32);db[0]=0;db[1]=1;db[2]=2;db[3]=3;db[4]=4;db[5]=5;db[8]=6;db[9]=7;db[10]=8;db[11]=9;db[12]=10;db[13]=11;db[16]=12;db[17]=13;
111
- db[18]=14;db[19]=15;db[20]=16;db[21]=17;bb([1],function(a,b){this.fm_waveform_select_enable[b]=a&1;this.fm_update_waveforms()});bb([2]);bb([3]);bb([4],function(){});bb([5],function(a,b,c){0===b&&this.fm_default_write(a,b,c)});bb([8],function(){});bb(cb(32,53),function(){});bb(cb(64,85),function(){});bb(cb(96,117),function(){});bb(cb(128,149),function(){});bb(cb(160,168),function(){});bb(cb(176,184),function(){});bb([189],function(){});bb(cb(192,200),function(){});bb(cb(224,245),function(){});F.prototype.fm_update_waveforms=
112
- function(){};F.prototype.sampling_rate_change=function(a){this.sampling_rate=a;this.bus.send("dac-tell-sampling-rate",a)};F.prototype.get_channel_count=function(){return this.dsp_stereo?2:1};F.prototype.dma_transfer_size_set=function(){this.dma_sample_count=1+(this.write_buffer.shift()<<0)+(this.write_buffer.shift()<<8)};F.prototype.dma_transfer_start=function(){D("begin dma transfer",8388608);this.bytes_per_sample=1;this.dsp_16bit&&(this.bytes_per_sample*=2);this.dma_bytes_count=this.dma_sample_count*
113
- this.bytes_per_sample;this.dma_bytes_block=1024*this.bytes_per_sample;this.dma_bytes_block=Math.min(Math.max(this.dma_bytes_count>>2&-4,32),this.dma_bytes_block);this.dma_waiting_transfer=!0;this.dma.channel_mask[this.dma_channel]||this.dma_on_unmask(this.dma_channel)};F.prototype.dma_on_unmask=function(a){a===this.dma_channel&&this.dma_waiting_transfer&&(this.dma_waiting_transfer=!1,this.dma_bytes_left=this.dma_bytes_count,this.dma_paused=!1,this.bus.send("dac-enable"))};F.prototype.dma_transfer_next=
114
- function(){D("dma transfering next block",8388608);var a=Math.min(this.dma_bytes_left,this.dma_bytes_block),b=Math.floor(a/this.bytes_per_sample);this.dma.do_write(this.dma_syncbuffer,0,a,this.dma_channel,c=>{D("dma block transfer "+(c?"unsuccessful":"successful"),8388608);c||(this.dma_to_dac(b),this.dma_bytes_left-=a,this.dma_bytes_left||(this.raise_irq(this.dma_irq),this.dma_autoinit&&(this.dma_bytes_left=this.dma_bytes_count)))})};F.prototype.dma_to_dac=function(a){var b=this.dsp_16bit?32767.5:
115
- 127.5,c=this.dsp_signed?0:-1,d=this.dsp_stereo?1:2;var e=this.dsp_16bit?this.dsp_signed?this.dma_buffer_int16:this.dma_buffer_uint16:this.dsp_signed?this.dma_buffer_int8:this.dma_buffer_uint8;for(var f=0,h=0;h<a;h++)for(var l=Va(e[h]/b+c,-1,1),n=0;n<d;n++)this.dac_buffers[f].push(l),f^=1;this.dac_send()};F.prototype.dac_handle_request=function(){!this.dma_bytes_left||this.dma_paused?this.dac_send():this.dma_transfer_next()};F.prototype.dac_send=function(){if(this.dac_buffers[0].length){var a=this.dac_buffers[0].shift_block(this.dac_buffers[0].length),
116
- b=this.dac_buffers[1].shift_block(this.dac_buffers[1].length);this.bus.send("dac-send-data",[a,b],[a.buffer,b.buffer])}};F.prototype.raise_irq=function(a){D("raise irq",8388608);this.irq_triggered[a]=1;this.cpu.device_raise_irq(this.irq)};F.prototype.lower_irq=function(a){D("lower irq",8388608);this.irq_triggered[a]=0;this.cpu.device_lower_irq(this.irq)};function Va(a,b,c){return(a<b)*b+(a>c)*c+(b<=a&&a<=c)*a}function J(a,b){this.stopping=this.running=!1;this.idle=!0;this.tick_counter=0;this.worker=
117
- null;this.cpu=new K(a,b,()=>{this.idle&&this.next_tick(0)});this.bus=a;this.register_yield()}J.prototype.run=function(){this.stopping=!1;this.running||(this.running=!0,this.bus.send("emulator-started"));this.next_tick(0)};J.prototype.do_tick=function(){if(this.stopping||!this.running)this.stopping=this.running=!1,this.bus.send("emulator-stopped");else{this.idle=!1;var a=this.cpu.main_loop();this.next_tick(a)}};J.prototype.next_tick=function(a){const b=++this.tick_counter;this.idle=!0;this.yield(a,
118
- b)};J.prototype.yield_callback=function(a){a===this.tick_counter&&this.do_tick()};J.prototype.stop=function(){this.running&&(this.stopping=!0)};J.prototype.destroy=function(){this.unregister_yield()};J.prototype.restart=function(){this.cpu.reset_cpu();this.cpu.load_bios()};J.prototype.init=function(a){this.cpu.init(a,this.bus);this.bus.send("emulator-ready")};if("undefined"!==typeof process)J.prototype.yield=function(a,b){1>a?global.setImmediate(c=>this.yield_callback(c),b):setTimeout(c=>this.yield_callback(c),
119
- a,b)},J.prototype.register_yield=function(){},J.prototype.unregister_yield=function(){};else if("undefined"!==typeof Worker){function a(){let b;globalThis.onmessage=function(c){const d=c.data.t;b=b&&clearTimeout(b);1>d?postMessage(c.data.tick):b=setTimeout(()=>postMessage(c.data.tick),d)}}J.prototype.register_yield=function(){const b=URL.createObjectURL(new Blob(["("+a.toString()+")()"],{type:"text/javascript"}));this.worker=new Worker(b);this.worker.onmessage=c=>this.yield_callback(c.data);URL.revokeObjectURL(b)};
120
- J.prototype.yield=function(b,c){this.worker.postMessage({t:b,tick:c})};J.prototype.unregister_yield=function(){this.worker&&this.worker.terminate();this.worker=null}}else J.prototype.yield=function(a){setTimeout(()=>{this.do_tick()},a)},J.prototype.register_yield=function(){},J.prototype.unregister_yield=function(){};J.prototype.save_state=function(){return this.cpu.save_state()};J.prototype.restore_state=function(a){return this.cpu.restore_state(a)};if("object"===typeof performance&&performance.now)J.microtick=
121
- performance.now.bind(performance);else if("function"===typeof require){const {performance:a}=require("perf_hooks");J.microtick=a.now.bind(a)}else J.microtick="object"===typeof process&&process.hrtime?function(){var a=process.hrtime();return 1E3*a[0]+a[1]/1E6}:Date.now;const eb="Fixed (0);Lowest Prio (1);SMI (2);Reserved (3);NMI (4);INIT (5);Reserved (6);ExtINT (7)".split(";"),fb=["physical","logical"];function gb(a){this.cpu=a;this.timer_divider=this.apic_id=0;this.timer_divider_shift=1;this.timer_current_count=
122
- this.timer_initial_count=0;this.next_tick=J.microtick();this.lvt_error=this.lvt_int1=this.lvt_int0=this.lvt_perf_counter=this.lvt_thermal_sensor=this.lvt_timer=65536;this.icr1=this.icr0=this.tpr=0;this.irr=new Int32Array(8);this.isr=new Int32Array(8);this.tmr=new Int32Array(8);this.spurious_vector=254;this.destination_format=-1;this.read_error=this.error=this.local_destination=0;a.io.mmap_register(4276092928,1048576,b=>{D("Unsupported read8 from apic: "+z(b>>>0),524288);var c=b&3;return this.read32(b&
123
- -4)>>8*c&255},(b,c)=>{D("Unsupported write8 from apic: "+z(b)+" <- "+z(c),524288);D(Error().stack,void 0);m(!1)},b=>this.read32(b),(b,c)=>this.write32(b,c))}gb.prototype.read32=function(a){a=a-4276092928|0;switch(a){case 32:return D("APIC read id",524288),this.apic_id;case 48:return D("APIC read version",524288),327700;case 128:return this.tpr;case 208:return D("Read local destination",524288),this.local_destination;case 224:return D("Read destination format",524288),this.destination_format;case 240:return this.spurious_vector;
124
- case 256:case 272:case 288:case 304:case 320:case 336:case 352:case 368:return a=a-256>>4,D("Read isr "+a+": "+z(this.isr[a]>>>0,8),524288),this.isr[a];case 384:case 400:case 416:case 432:case 448:case 464:case 480:case 496:return a=a-384>>4,D("Read tmr "+a+": "+z(this.tmr[a]>>>0,8),524288),this.tmr[a];case 512:case 528:case 544:case 560:case 576:case 592:case 608:case 624:return a=a-512>>4,D("Read irr "+a+": "+z(this.irr[a]>>>0,8),524288),this.irr[a];case 640:return D("Read error: "+z(this.read_error>>>
125
- 0,8),524288),this.read_error;case 768:return this.icr0;case 784:return D("APIC read icr1",524288),this.icr1;case 800:return D("read timer lvt",524288),this.lvt_timer;case 816:return D("read lvt thermal sensor",524288),this.lvt_thermal_sensor;case 832:return D("read lvt perf counter",524288),this.lvt_perf_counter;case 848:return D("read lvt int0",524288),this.lvt_int0;case 864:return D("read lvt int1",524288),this.lvt_int1;case 880:return D("read lvt error",524288),this.lvt_error;case 992:return D("read timer divider",
126
- 524288),this.timer_divider;case 896:return D("read timer initial count",524288),this.timer_initial_count;case 912:return D("read timer current count: "+z(this.timer_current_count>>>0,8),524288),this.timer_current_count;default:return D("APIC read "+z(a),524288),m(!1),0}};gb.prototype.write32=function(a,b){a=a-4276092928|0;switch(a){case 32:D("APIC write id: "+z(b>>>8,8),524288);this.apic_id=b;break;case 48:D("APIC write version: "+z(b>>>0,8)+", ignored",524288);break;case 128:this.tpr=b&255;this.check_vector();
127
- break;case 176:b=this.highest_isr();-1!==b?(this.register_clear_bit(this.isr,b),this.register_get_bit(this.tmr,b)&&this.cpu.devices.ioapic.remote_eoi(b),this.check_vector()):D("Bad eoi: No isr set",524288);break;case 208:D("Set local destination: "+z(b>>>0,8),524288);this.local_destination=b&4278190080;break;case 224:D("Set destination format: "+z(b>>>0,8),524288);this.destination_format=b|16777215;break;case 240:D("Set spurious vector: "+z(b>>>0,8),524288);this.spurious_vector=b;break;case 640:D("Write error: "+
128
- z(b>>>0,8),524288);this.read_error=this.error;this.error=0;break;case 768:a=b&255;var c=b>>8&7,d=b>>11&1,e=b>>15&1,f=b>>18&3,h=this.icr1>>>24;D("APIC write icr0: "+z(b,8)+" vector="+z(a,2)+" destination_mode="+fb[d]+" delivery_mode="+eb[c]+" destination_shorthand="+["no","self","all with self","all without self"][f],524288);this.icr0=b&-4097;0===f?this.route(a,c,e,h,d):1===f?this.deliver(a,0,e):2===f?this.deliver(a,c,e):3!==f&&m(!1);break;case 784:D("APIC write icr1: "+z(b>>>0,8),524288);this.icr1=
129
- b;break;case 800:D("timer lvt: "+z(b>>>0,8),524288);this.lvt_timer=b;break;case 816:D("lvt thermal sensor: "+z(b>>>0,8),524288);this.lvt_thermal_sensor=b;break;case 832:D("lvt perf counter: "+z(b>>>0,8),524288);this.lvt_perf_counter=b;break;case 848:D("lvt int0: "+z(b>>>0,8),524288);this.lvt_int0=b;break;case 864:D("lvt int1: "+z(b>>>0,8),524288);this.lvt_int1=b;break;case 880:D("lvt error: "+z(b>>>0,8),524288);this.lvt_error=b;break;case 992:D("timer divider: "+z(b>>>0,8),524288);this.timer_divider=
130
- b;b=b&3|(b&8)>>1;this.timer_divider_shift=7===b?0:b+1;break;case 896:D("timer initial: "+z(b>>>0,8),524288);this.timer_initial_count=b>>>0;this.timer_current_count=b>>>0;this.next_tick=J.microtick();this.timer_active=!0;break;case 912:D("timer current: "+z(b>>>0,8),524288);m(!1,"read-only register");break;default:D("APIC write32 "+z(a)+" <- "+z(b>>>0,8),524288),m(!1)}};gb.prototype.timer=function(a){if(0===this.timer_current_count)return 100;const b=1E6/(1<<this.timer_divider_shift);a=(a-this.next_tick)*
131
- b>>>0;this.next_tick+=a/b;this.timer_current_count-=a;0>=this.timer_current_count&&(a=this.lvt_timer&393216,131072===a?(this.timer_current_count%=this.timer_initial_count,0>=this.timer_current_count&&(this.timer_current_count+=this.timer_initial_count),m(0!==this.timer_current_count),0===(this.lvt_timer&65536)&&this.deliver(this.lvt_timer&255,0,!1)):0===a&&(this.timer_current_count=0,D("APIC timer one shot end",524288),0===(this.lvt_timer&65536)&&this.deliver(this.lvt_timer&255,0,!1)));return Math.max(0,
132
- this.timer_current_count/b)};gb.prototype.route=function(a,b,c){this.deliver(a,b,c)};gb.prototype.deliver=function(a,b,c){5!==b&&4!==b&&((16>a||255===a)&&m(!1,"TODO: Invalid vector"),this.register_get_bit(this.irr,a)?D("Not delivered: irr already set, vector="+z(a,2),524288):(this.register_set_bit(this.irr,a),c?this.register_set_bit(this.tmr,a):this.register_clear_bit(this.tmr,a),this.check_vector()))};gb.prototype.highest_irr=function(){var a=this.register_get_highest_bit(this.irr);m(255!==a);m(16<=
133
- a||-1===a);return a};gb.prototype.highest_isr=function(){var a=this.register_get_highest_bit(this.isr);m(255!==a);m(16<=a||-1===a);return a};gb.prototype.check_vector=function(){var a=this.highest_irr();-1!==a&&(this.highest_isr()>=a||(a&240)<=(this.tpr&240)||this.cpu.handle_irqs())};gb.prototype.acknowledge_irq=function(){var a=this.highest_irr();if(-1===a||this.highest_isr()>=a||(a&240)<=(this.tpr&240))return-1;this.register_clear_bit(this.irr,a);this.register_set_bit(this.isr,a);this.check_vector();
134
- return a};gb.prototype.get_state=function(){var a=[];a[0]=this.apic_id;a[1]=this.timer_divider;a[2]=this.timer_divider_shift;a[3]=this.timer_initial_count;a[4]=this.timer_current_count;a[5]=this.next_tick;a[6]=this.lvt_timer;a[7]=this.lvt_perf_counter;a[8]=this.lvt_int0;a[9]=this.lvt_int1;a[10]=this.lvt_error;a[11]=this.tpr;a[12]=this.icr0;a[13]=this.icr1;a[14]=this.irr;a[15]=this.isr;a[16]=this.tmr;a[17]=this.spurious_vector;a[18]=this.destination_format;a[19]=this.local_destination;a[20]=this.error;
135
- a[21]=this.read_error;a[22]=this.lvt_thermal_sensor;return a};gb.prototype.set_state=function(a){this.apic_id=a[0];this.timer_divider=a[1];this.timer_divider_shift=a[2];this.timer_initial_count=a[3];this.timer_current_count=a[4];this.next_tick=a[5];this.lvt_timer=a[6];this.lvt_perf_counter=a[7];this.lvt_int0=a[8];this.lvt_int1=a[9];this.lvt_error=a[10];this.tpr=a[11];this.icr0=a[12];this.icr1=a[13];this.irr=a[14];this.isr=a[15];this.tmr=a[16];this.spurious_vector=a[17];this.destination_format=a[18];
136
- this.local_destination=a[19];this.error=a[20];this.read_error=a[21];this.lvt_thermal_sensor=a[22]||65536};gb.prototype.register_get_bit=function(a,b){m(0<=b&&256>b);return a[b>>5]>>(b&31)&1};gb.prototype.register_set_bit=function(a,b){m(0<=b&&256>b);a[b>>5]|=1<<(b&31)};gb.prototype.register_clear_bit=function(a,b){m(0<=b&&256>b);a[b>>5]&=~(1<<(b&31))};gb.prototype.register_get_highest_bit=function(a){for(var b=7;0<=b;b--){var c=a[b];if(c)return ha(c>>>0)|b<<5}return-1};function hb(a){this.cpu=a;this.ioredtbl_config=
137
- new Int32Array(24);this.ioredtbl_destination=new Int32Array(24);for(var b=0;b<this.ioredtbl_config.length;b++)this.ioredtbl_config[b]=65536;this.irq_value=this.irr=this.ioapic_id=this.ioregsel=0;m(!0);a.io.mmap_register(4273995776,131072,c=>{c=c-4273995776|0;if(16<=c&&20>c)return c-=16,D("ioapic read8 byte "+c+" "+z(this.ioregsel),524288),this.read(this.ioregsel)>>8*c&255;D("Unexpected IOAPIC register read: "+z(c>>>0),524288);m(!1);return 0},c=>{m(!1,"unsupported write8 from ioapic: "+z(c>>>0))},
138
- c=>{c=c-4273995776|0;if(0===c)return this.ioregsel;if(16===c)return this.read(this.ioregsel);D("Unexpected IOAPIC register read: "+z(c>>>0),524288);m(!1);return 0},(c,d)=>{c=c-4273995776|0;0===c?this.ioregsel=d:16===c?this.write(this.ioregsel,d):(D("Unexpected IOAPIC register write: "+z(c>>>0)+" <- "+z(d>>>0,8),524288),m(!1))})}hb.prototype.remote_eoi=function(a){for(var b=0;24>b;b++){var c=this.ioredtbl_config[b];(c&255)===a&&c&16384&&(D("Clear remote IRR for irq="+z(b),524288),this.ioredtbl_config[b]&=
139
- -16385,this.check_irq(b))}};hb.prototype.check_irq=function(a){var b=1<<a;if(0!==(this.irr&b)){var c=this.ioredtbl_config[a];if(0===(c&65536)){var d=c>>8&7,e=c>>11&1,f=c&255,h=this.ioredtbl_destination[a]>>>24,l=32768===(c&32768);if(0===(c&32768))this.irr&=~b;else if(this.ioredtbl_config[a]|=16384,c&16384){D("No route: level interrupt and remote IRR still set",524288);return}0===d||1===d?this.cpu.devices.apic.route(f,d,l,h,e):m(!1,"TODO");this.ioredtbl_config[a]&=-4097}}};hb.prototype.set_irq=function(a){if(24<=
140
- a)m(!1,"Bad irq: "+a,524288);else{var b=1<<a;0===(this.irq_value&b)&&(this.irq_value|=b,65536!==(this.ioredtbl_config[a]&98304)&&(this.irr|=b,this.check_irq(a)))}};hb.prototype.clear_irq=function(a){if(24<=a)m(!1,"Bad irq: "+a,524288);else{var b=1<<a;(this.irq_value&b)===b&&(this.irq_value&=~b,this.ioredtbl_config[a]&32768&&(this.irr&=~b))}};hb.prototype.read=function(a){if(0===a)return D("IOAPIC Read id",524288),this.ioapic_id<<24;if(1===a)return D("IOAPIC Read version",524288),1507345;if(2===a)return D("IOAPIC Read arbitration id",
141
- 524288),this.ioapic_id<<24;if(16<=a&&64>a){var b=a-16>>1;a&1?(a=this.ioredtbl_destination[b],D("IOAPIC Read destination irq="+z(b)+" -> "+z(a,8),524288)):(a=this.ioredtbl_config[b],D("IOAPIC Read config irq="+z(b)+" -> "+z(a,8),524288));return a}D("IOAPIC register read outside of range "+z(a),524288);m(!1);return 0};hb.prototype.write=function(a,b){if(0===a)this.ioapic_id=b>>>24&15;else if(1===a||2===a)D("Invalid write: "+a,524288);else if(16<=a&&64>a){var c=a-16>>1;if(a&1)this.ioredtbl_destination[c]=
142
- b&4278190080,D("Write destination "+z(b>>>0,8)+" irq="+z(c)+" dest="+z(b>>>24,2),524288);else{this.ioredtbl_config[c]=b&110591|this.ioredtbl_config[c]&-110592;a=b&255;var d=b>>8&7,e=b>>11&1,f=b>>15&1,h=b>>16&1;D("Write config "+z(b>>>0,8)+" irq="+z(c)+" vector="+z(a,2)+" deliverymode="+eb[d]+" destmode="+fb[e]+" is_level="+f+" disabled="+h,524288);this.check_irq(c)}}else D("IOAPIC register write outside of range "+z(a)+": "+z(b>>>0,8),524288),m(!1)};hb.prototype.get_state=function(){var a=[];a[0]=
143
- this.ioredtbl_config;a[1]=this.ioredtbl_destination;a[2]=this.ioregsel;a[3]=this.ioapic_id;a[4]=this.irr;a[5]=this.irq_value;return a};hb.prototype.set_state=function(a){this.ioredtbl_config=a[0];this.ioredtbl_destination=a[1];this.ioregsel=a[2];this.ioapic_id=a[3];this.irr=a[4];this.irq_value=a[5]};function ib(a){this.cpu=a;var b=a.io;a.devices.pci.register_device({pci_id:56,pci_space:[134,128,19,113,7,0,128,2,8,0,128,6,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
144
- 0,0,0,0,0,0,0,0,0,0,0,9,1,0,0],pci_bars:[],name:"acpi"});this.timer_imprecision_offset=this.timer_last_value=0;this.status=1;this.pm1_enable=this.pm1_status=0;this.last_timer=this.get_timer(J.microtick());this.gpe=new Uint8Array(4);b.register_read(45056,this,void 0,function(){D("ACPI pm1_status read",262144);return this.pm1_status});b.register_write(45056,this,void 0,function(c){D("ACPI pm1_status write: "+z(c,4),262144);this.pm1_status&=~c});b.register_read(45058,this,void 0,function(){D("ACPI pm1_enable read",
145
- 262144);return this.pm1_enable});b.register_write(45058,this,void 0,function(c){D("ACPI pm1_enable write: "+z(c),262144);this.pm1_enable=c});b.register_read(45060,this,void 0,function(){D("ACPI status read",262144);return this.status});b.register_write(45060,this,void 0,function(c){D("ACPI status write: "+z(c),262144);this.status=c});b.register_read(45064,this,void 0,void 0,function(){return this.get_timer(J.microtick())&16777215});b.register_read(45024,this,function(){D("Read gpe#0",262144);return this.gpe[0]});
146
- b.register_read(45025,this,function(){D("Read gpe#1",262144);return this.gpe[1]});b.register_read(45026,this,function(){D("Read gpe#2",262144);return this.gpe[2]});b.register_read(45027,this,function(){D("Read gpe#3",262144);return this.gpe[3]});b.register_write(45024,this,function(c){D("Write gpe#0: "+z(c),262144);this.gpe[0]=c});b.register_write(45025,this,function(c){D("Write gpe#1: "+z(c),262144);this.gpe[1]=c});b.register_write(45026,this,function(c){D("Write gpe#2: "+z(c),262144);this.gpe[2]=
147
- c});b.register_write(45027,this,function(c){D("Write gpe#3: "+z(c),262144);this.gpe[3]=c})}ib.prototype.timer=function(a){a=this.get_timer(a);var b=0!==((a^this.last_timer)&8388608);this.pm1_enable&1&&b?(D("ACPI raise irq",262144),this.pm1_status|=1,this.cpu.device_raise_irq(9)):this.cpu.device_lower_irq(9);this.last_timer=a;return 100};ib.prototype.get_timer=function(a){a=Math.round(3579.545*a);a===this.timer_last_value?3579.545>this.timer_imprecision_offset&&this.timer_imprecision_offset++:(m(a>
148
- this.timer_last_value),this.timer_last_value+this.timer_imprecision_offset<=a?(this.timer_imprecision_offset=0,this.timer_last_value=a):D("Warning: Overshot pmtimer, waiting; current="+a+" last="+this.timer_last_value+" offset="+this.timer_imprecision_offset,262144));return this.timer_last_value+this.timer_imprecision_offset};ib.prototype.get_state=function(){var a=[];a[0]=this.status;a[1]=this.pm1_status;a[2]=this.pm1_enable;a[3]=this.gpe;return a};ib.prototype.set_state=function(a){this.status=
149
- a[0];this.pm1_status=a[1];this.pm1_enable=a[2];this.gpe=a[3]};function jb(a,b){this.cpu=a;this.bus=b;this.counter_start_time=new Float64Array(3);this.counter_start_value=new Uint16Array(3);this.counter_next_low=new Uint8Array(4);this.counter_enabled=new Uint8Array(4);this.counter_mode=new Uint8Array(4);this.counter_read_mode=new Uint8Array(4);this.counter_latch=new Uint8Array(4);this.counter_latch_value=new Uint16Array(3);this.counter_reload=new Uint16Array(3);a.io.register_read(97,this,function(){var c=
150
- J.microtick(),d=66.66666666666667*c&1;c=this.did_rollover(2,c);return d<<4|c<<5});a.io.register_write(97,this,function(c){c&1?this.bus.send("pcspeaker-enable"):this.bus.send("pcspeaker-disable")});a.io.register_read(64,this,function(){return this.counter_read(0)});a.io.register_read(65,this,function(){return this.counter_read(1)});a.io.register_read(66,this,function(){return this.counter_read(2)});a.io.register_write(64,this,function(c){this.counter_write(0,c)});a.io.register_write(65,this,function(c){this.counter_write(1,
151
- c)});a.io.register_write(66,this,function(c){this.counter_write(2,c);this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]])});a.io.register_write(67,this,this.port43_write)}jb.prototype.get_state=function(){var a=[];a[0]=this.counter_next_low;a[1]=this.counter_enabled;a[2]=this.counter_mode;a[3]=this.counter_read_mode;a[4]=this.counter_latch;a[5]=this.counter_latch_value;a[6]=this.counter_reload;a[7]=this.counter_start_time;a[8]=this.counter_start_value;return a};jb.prototype.set_state=
152
- function(a){this.counter_next_low=a[0];this.counter_enabled=a[1];this.counter_mode=a[2];this.counter_read_mode=a[3];this.counter_latch=a[4];this.counter_latch_value=a[5];this.counter_reload=a[6];this.counter_start_time=a[7];this.counter_start_value=a[8]};jb.prototype.timer=function(a,b){var c=100;b||(this.counter_enabled[0]&&this.did_rollover(0,a)?(this.counter_start_value[0]=this.get_counter_value(0,a),this.counter_start_time[0]=a,D("pit interrupt. new value: "+this.counter_start_value[0],512),this.cpu.device_lower_irq(0),
153
- this.cpu.device_raise_irq(0),0===this.counter_mode[0]&&(this.counter_enabled[0]=0)):this.cpu.device_lower_irq(0),this.counter_enabled[0]&&(c=(this.counter_start_value[0]-Math.floor(1193.1816666*(a-this.counter_start_time[0])))/1193.1816666));return c};jb.prototype.get_counter_value=function(a,b){if(!this.counter_enabled[a])return 0;var c=b-this.counter_start_time[a],d=Math.floor(1193.1816666*c);b=this.counter_start_value[a]-d;D("diff="+c+" dticks="+d+" value="+b+" reload="+this.counter_reload[a],
154
- 512);c=this.counter_reload[a];b>=c?(D("Warning: Counter"+a+" value "+b+" is larger than reload "+c,512),b%=c):0>b&&(b=b%c+c);return b};jb.prototype.did_rollover=function(a,b){b-=this.counter_start_time[a];return 0>b?(D("Warning: PIT timer difference is negative, resetting (timer "+a+")"),!0):this.counter_start_value[a]<Math.floor(1193.1816666*b)};jb.prototype.counter_read=function(a){var b=this.counter_latch[a];if(b)return this.counter_latch[a]--,2===b?this.counter_latch_value[a]&255:this.counter_latch_value[a]>>
155
- 8;b=this.counter_next_low[a];3===this.counter_mode[a]&&(this.counter_next_low[a]^=1);a=this.get_counter_value(a,J.microtick());return b?a&255:a>>8};jb.prototype.counter_write=function(a,b){this.counter_reload[a]=this.counter_next_low[a]?this.counter_reload[a]&-256|b:this.counter_reload[a]&255|b<<8;3===this.counter_read_mode[a]&&this.counter_next_low[a]||(this.counter_reload[a]||(this.counter_reload[a]=65535),this.counter_start_value[a]=this.counter_reload[a],this.counter_enabled[a]=!0,this.counter_start_time[a]=
156
- J.microtick(),D("counter"+a+" reload="+z(this.counter_reload[a])+" tick="+(this.counter_reload[a]||65536)/1193.1816666+"ms",512));3===this.counter_read_mode[a]&&(this.counter_next_low[a]^=1)};jb.prototype.port43_write=function(a){var b=a>>1&7,c=a&1,d=a>>6&3;a=a>>4&3;1===d&&D("Unimplemented timer1",512);3===d?D("Unimplemented read back",512):0===a?(this.counter_latch[d]=2,b=this.get_counter_value(d,J.microtick()),D("latch: "+b,512),this.counter_latch_value[d]=b?b-1:0):(6<=b&&(b&=-5),D("Control: mode="+
157
- b+" ctr="+d+" read_mode="+a+" bcd="+c,512),this.counter_next_low[d]=1===a?0:1,0===d&&this.cpu.device_lower_irq(0),0!==b&&3!==b&&2!==b&&D("Unimplemented counter mode: "+z(b),512),this.counter_mode[d]=b,this.counter_read_mode[d]=a,2===d&&this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]]))};jb.prototype.dump=function(){const a=this.counter_reload[0];D("counter0 ticks every "+(a||65536)/1193.1816666+"ms (reload="+a+")")};function lb(a,b,c){this.bus=c;this.cpu=a;this.ints=
158
- 4;this.line_control=this.baud_rate=0;this.lsr=96;this.ier=this.fifo_control=0;this.iir=1;this.irq=this.scratch_register=this.modem_status=this.modem_control=0;this.input=[];this.current_line="";switch(b){case 1016:this.com=0;this.irq=4;break;case 760:this.com=1;this.irq=3;break;case 1E3:this.com=2;this.irq=4;break;case 744:this.irq=this.com=3;break;default:D("Invalid serial port: "+z(b),16384),this.com=0,this.irq=4}this.bus.register("serial"+this.com+"-input",function(d){this.data_received(d)},this);
159
- this.bus.register("serial"+this.com+"-modem-status-input",function(d){this.set_modem_status(d)},this);this.bus.register("serial"+this.com+"-carrier-detect-input",function(d){this.set_modem_status(d?this.modem_status|136:this.modem_status&-137)},this);this.bus.register("serial"+this.com+"-ring-indicator-input",function(d){this.set_modem_status(d?this.modem_status|68:this.modem_status&-69)},this);this.bus.register("serial"+this.com+"-data-set-ready-input",function(d){this.set_modem_status(d?this.modem_status|
160
- 34:this.modem_status&-35)},this);this.bus.register("serial"+this.com+"-clear-to-send-input",function(d){this.set_modem_status(d?this.modem_status|17:this.modem_status&-18)},this);a=a.io;a.register_write(b,this,function(d){this.write_data(d)},function(d){this.write_data(d&255);this.write_data(d>>8)});a.register_write(b|1,this,function(d){this.line_control&128?(this.baud_rate=this.baud_rate&255|d<<8,D("baud rate: "+z(this.baud_rate),16384)):(0===(this.ier&2)&&d&2&&this.ThrowInterrupt(2),this.ier=d&
161
- 15,D("interrupt enable: "+z(d),16384),this.CheckInterrupt())});a.register_read(b,this,function(){if(this.line_control&128)return this.baud_rate&255;let d=0;0===this.input.length?D("Read input empty",16384):(d=this.input.shift(),D("Read input: "+z(d),16384));0===this.input.length&&(this.lsr&=-2,this.ClearInterrupt(12),this.ClearInterrupt(4));return d});a.register_read(b|1,this,function(){return this.line_control&128?this.baud_rate>>8:this.ier&15});a.register_read(b|2,this,function(){var d=this.iir&
162
- 15;D("read interrupt identification: "+z(this.iir),16384);2===this.iir&&this.ClearInterrupt(2);this.fifo_control&1&&(d|=192);return d});a.register_write(b|2,this,function(d){D("fifo control: "+z(d),16384);this.fifo_control=d});a.register_read(b|3,this,function(){D("read line control: "+z(this.line_control),16384);return this.line_control});a.register_write(b|3,this,function(d){D("line control: "+z(d),16384);this.line_control=d});a.register_read(b|4,this,function(){return this.modem_control});a.register_write(b|
163
- 4,this,function(d){D("modem control: "+z(d),16384);this.modem_control=d});a.register_read(b|5,this,function(){D("read line status: "+z(this.lsr),16384);return this.lsr});a.register_write(b|5,this,function(){D("Factory test write",16384)});a.register_read(b|6,this,function(){D("read modem status: "+z(this.modem_status),16384);return this.modem_status&=240});a.register_write(b|6,this,function(d){D("write modem status: "+z(d),16384);this.set_modem_status(d)});a.register_read(b|7,this,function(){return this.scratch_register});
164
- a.register_write(b|7,this,function(d){this.scratch_register=d})}lb.prototype.get_state=function(){var a=[];a[0]=this.ints;a[1]=this.baud_rate;a[2]=this.line_control;a[3]=this.lsr;a[4]=this.fifo_control;a[5]=this.ier;a[6]=this.iir;a[7]=this.modem_control;a[8]=this.modem_status;a[9]=this.scratch_register;a[10]=this.irq;return a};lb.prototype.set_state=function(a){this.ints=a[0];this.baud_rate=a[1];this.line_control=a[2];this.lsr=a[3];this.fifo_control=a[4];this.ier=a[5];this.iir=a[6];this.modem_control=
165
- a[7];this.modem_status=a[8];this.scratch_register=a[9];this.irq=a[10]};lb.prototype.CheckInterrupt=function(){this.ints&4096&&this.ier&1?(this.iir=12,this.cpu.device_raise_irq(this.irq)):this.ints&16&&this.ier&1?(this.iir=4,this.cpu.device_raise_irq(this.irq)):this.ints&4&&this.ier&2?(this.iir=2,this.cpu.device_raise_irq(this.irq)):this.ints&1&&this.ier&8?(this.iir=0,this.cpu.device_raise_irq(this.irq)):(this.iir=1,this.cpu.device_lower_irq(this.irq))};lb.prototype.ThrowInterrupt=function(a){this.ints|=
166
- 1<<a;this.CheckInterrupt()};lb.prototype.ClearInterrupt=function(a){this.ints&=~(1<<a);this.CheckInterrupt()};lb.prototype.data_received=function(a){D("input: "+z(a),16384);this.input.push(a);this.lsr|=1;this.fifo_control&1?this.ThrowInterrupt(12):this.ThrowInterrupt(4)};lb.prototype.write_data=function(a){this.line_control&128?this.baud_rate=this.baud_rate&-256|a:(D("data: "+z(a),16384),this.ThrowInterrupt(2),this.bus.send("serial"+this.com+"-output-byte",a),a=String.fromCharCode(a),this.current_line+=
167
- a,"\n"===a&&(a=this.current_line.trimRight().replace(/[\x00-\x08\x0b-\x1f\x7f\x80-\xff]/g,""),D("SERIAL: "+a),this.current_line=""))};lb.prototype.set_modem_status=function(a){D("modem status: "+z(a),16384);const b=this.modem_status&15;let c=(this.modem_status^a)>>4;this.modem_status=a;this.modem_status=this.modem_status|c|b};function mb(a){this.pci_addr=new Uint8Array(4);this.pci_value=new Uint8Array(4);this.pci_response=new Uint8Array(4);this.pci_status=new Uint8Array(4);this.pci_addr32=new Int32Array(this.pci_addr.buffer);
168
- this.pci_value32=new Int32Array(this.pci_value.buffer);this.pci_response32=new Int32Array(this.pci_response.buffer);this.pci_status32=new Int32Array(this.pci_status.buffer);this.device_spaces=[];this.devices=[];this.cpu=a;for(var b=0;256>b;b++)this.device_spaces[b]=void 0,this.devices[b]=void 0;this.io=a.io;a.io.register_write(3324,this,function(c){this.pci_write8(this.pci_addr32[0],c)},function(c){this.pci_write16(this.pci_addr32[0],c)},function(c){this.pci_write32(this.pci_addr32[0],c)});a.io.register_write(3325,
169
- this,function(c){this.pci_write8(this.pci_addr32[0]+1|0,c)});a.io.register_write(3326,this,function(c){this.pci_write8(this.pci_addr32[0]+2|0,c)},function(c){this.pci_write16(this.pci_addr32[0]+2|0,c)});a.io.register_write(3327,this,function(c){this.pci_write8(this.pci_addr32[0]+3|0,c)});a.io.register_read_consecutive(3324,this,function(){return this.pci_response[0]},function(){return this.pci_response[1]},function(){return this.pci_response[2]},function(){return this.pci_response[3]});a.io.register_read_consecutive(3320,
170
- this,function(){return this.pci_status[0]},function(){return this.pci_status[1]},function(){return this.pci_status[2]},function(){return this.pci_status[3]});a.io.register_write_consecutive(3320,this,function(c){this.pci_addr[0]=c&252},function(c){2===(this.pci_addr[1]&6)&&6===(c&6)?(D("CPU reboot via PCI"),a.reboot_internal()):this.pci_addr[1]=c},function(c){this.pci_addr[2]=c},function(c){this.pci_addr[3]=c;this.pci_query()});this.register_device({pci_id:0,pci_space:[134,128,55,18,0,0,0,0,2,0,0,
171
- 6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0],pci_bars:[],name:"82441FX PMC"});this.isa_bridge={pci_id:8,pci_space:[134,128,0,112,7,0,0,2,0,0,1,6,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],pci_bars:[],name:"82371SB PIIX3 ISA"};this.isa_bridge_space=this.register_device(this.isa_bridge);this.isa_bridge_space8=new Uint8Array(this.isa_bridge_space.buffer)}
172
- mb.prototype.get_state=function(){for(var a=[],b=0;256>b;b++)a[b]=this.device_spaces[b];a[256]=this.pci_addr;a[257]=this.pci_value;a[258]=this.pci_response;a[259]=this.pci_status;return a};mb.prototype.set_state=function(a){for(var b=0;256>b;b++){var c=this.devices[b],d=a[b];if(c&&d){for(var e=0;e<c.pci_bars.length;e++){var f=d[4+e];if(f&1){var h=c.pci_bars[e];this.set_io_bars(h,h.original_bar&65534,f&65534)}}this.device_spaces[b].set(d)}else c&&D("Warning: While restoring PCI device: Device exists in current configuration but not in snapshot ("+
173
- c.name+")"),d&&D("Warning: While restoring PCI device: Device doesn't exist in current configuration but does in snapshot (device "+z(b,2)+")")}this.pci_addr.set(a[256]);this.pci_value.set(a[257]);this.pci_response.set(a[258]);this.pci_status.set(a[259])};mb.prototype.pci_query=function(){var a=this.pci_addr[2]<<8|this.pci_addr[1],b=this.pci_addr[0]&252,c=a>>3&31;var d="query enabled="+(this.pci_addr[3]>>7)+(" bdf="+z(a,4));d+=" dev="+z(c,2);d+=" addr="+z(b,2);c=this.device_spaces[a];void 0!==c?(this.pci_status32[0]=
174
- -2147483648,this.pci_response32[0]=b<c.byteLength?c[b>>2]:0,d+=" "+z(this.pci_addr32[0]>>>0,8)+" -> "+z(this.pci_response32[0]>>>0,8),b>=c.byteLength&&(d+=" (undef)"),d+=" ("+this.devices[a].name+")",D(d,2048)):(this.pci_response32[0]=-1,this.pci_status32[0]=0)};mb.prototype.pci_write8=function(a,b){var c=a>>8&65535;a&=255;var d=new Uint8Array(this.device_spaces[c].buffer),e=this.devices[c];d&&(m(!(16<=a&&44>a||48<=a&&52>a),"PCI: Expected 32-bit write, got 8-bit (addr: "+z(a)+")"),D("PCI write8 dev="+
175
- z(c>>3,2)+" ("+e.name+") addr="+z(a,4)+" value="+z(b,2),2048),d[a]=b)};mb.prototype.pci_write16=function(a,b){m(0===(a&1));var c=a>>8&65535;a&=255;var d=new Uint16Array(this.device_spaces[c].buffer),e=this.devices[c];d&&(16<=a&&44>a?D("Warning: PCI: Expected 32-bit write, got 16-bit (addr: "+z(a)+")"):(m(!(48<=a&&52>a),"PCI: Expected 32-bit write, got 16-bit (addr: "+z(a)+")"),D("PCI writ16 dev="+z(c>>3,2)+" ("+e.name+") addr="+z(a,4)+" value="+z(b,4),2048),d[a>>>1]=b))};mb.prototype.pci_write32=
176
- function(a,b){m(0===(a&3));var c=a>>8&65535;a&=255;var d=this.device_spaces[c],e=this.devices[c];if(d)if(16<=a&&40>a){var f=a-16>>2,h=e.pci_bars[f];D("BAR"+f+" exists="+(h?"y":"n")+" changed to "+z(b>>>0)+" dev="+z(c>>3,2)+" ("+e.name+") ",2048);h?(m(!(h.size&h.size-1),"bar size should be power of 2"),c=a>>2,e=d[c]&1,-1===(b|3|h.size-1)?(b=~(h.size-1)|e,0===e&&(d[c]=b)):0===e&&(f=h.original_bar,(b&-16)!==(f&-16)&&D("Warning: Changing memory bar not supported, ignored",2048),d[c]=f),1===e&&(m(1===
177
- e),e=d[c]&65534,f=b&65534,D("io bar changed from "+z(e>>>0,8)+" to "+z(f>>>0,8)+" size="+h.size,2048),this.set_io_bars(h,e,f),d[c]=b|1)):d[a>>2]=0;D("BAR effective value: "+z(d[a>>2]>>>0),2048)}else 48===a?(D("PCI write rom address dev="+z(c>>3,2)+" ("+e.name+") value="+z(b>>>0,8),2048),d[a>>2]=e.pci_rom_size?-1===(b|2047)?-e.pci_rom_size|0:e.pci_rom_address|0:0):4===a?D("PCI write dev="+z(c>>3,2)+" ("+e.name+") addr="+z(a,4)+" value="+z(b>>>0,8),2048):(D("PCI write dev="+z(c>>3,2)+" ("+e.name+") addr="+
178
- z(a,4)+" value="+z(b>>>0,8),2048),d[a>>>2]=b)};mb.prototype.register_device=function(a){m(void 0!==a.pci_id);m(void 0!==a.pci_space);m(void 0!==a.pci_bars);var b=a.pci_id;D("PCI register bdf="+z(b)+" ("+a.name+")",2048);m(!this.devices[b]);m(64<=a.pci_space.length);m(b<this.devices.length);var c=new Int32Array(64);c.set(new Int32Array((new Uint8Array(a.pci_space)).buffer));this.device_spaces[b]=c;this.devices[b]=a;b=c.slice(4,10);for(var d=0;d<a.pci_bars.length;d++){var e=a.pci_bars[d];if(e){var f=
179
- b[d],h=f&1;e.original_bar=f;e.entries=[];if(0!==h)for(m(1===h),f&=-2,h=0;h<e.size;h++)e.entries[h]=this.io.ports[f+h]}}return c};mb.prototype.set_io_bars=function(a,b,c){var d=a.size;D("Move io bars: from="+z(b)+" to="+z(c)+" count="+d,2048);for(var e=this.io.ports,f=0;f<d;f++){var h=e[b+f];4096<=b+f&&(e[b+f]=this.io.create_empty_entry());h.read8===this.io.empty_port_read8&&h.read16===this.io.empty_port_read16&&h.read32===this.io.empty_port_read32&&h.write8===this.io.empty_port_write&&h.write16===
180
- this.io.empty_port_write&&h.write32===this.io.empty_port_write&&D("Warning: Bad IO bar: Source not mapped, port="+z(b+f,4),2048);h=a.entries[f];var l=e[c+f];m(h&&l);4096<=c+f&&(e[c+f]=h);l.read8!==this.io.empty_port_read8&&l.read16!==this.io.empty_port_read16&&l.read32!==this.io.empty_port_read32&&l.write8!==this.io.empty_port_write&&l.write16!==this.io.empty_port_write&&l.write32!==this.io.empty_port_write||D("Warning: Bad IO bar: Target already mapped, port="+z(c+f,4),2048)}};mb.prototype.raise_irq=
181
- function(a){var b=this.device_spaces[a];m(b);this.cpu.device_raise_irq(this.isa_bridge_space8[96+((b[15]>>8&255)-1+((a>>3)-1&255)&3)])};mb.prototype.lower_irq=function(a){var b=this.device_spaces[a];m(b);this.cpu.device_lower_irq(this.isa_bridge_space8[96+((b[15]>>8&255)+(a>>3&255)-2&3)])};function nb(a,b,c){a[0]===b[0]&&a[1]===b[1]&&a[2]===b[2]&&a[3]===b[3]&&a[4]===b[4]&&a[5]===b[5]&&(D("Replace mac in eth destination field",1048576),a[0]=c[0],a[1]=c[1],a[2]=c[2],a[3]=c[3],a[4]=c[4],a[5]=c[5]);a[6]===
182
- b[0]&&a[7]===b[1]&&a[8]===b[2]&&a[9]===b[3]&&a[10]===b[4]&&a[11]===b[5]&&(D("Replace mac in eth source field",1048576),a[6]=c[0],a[7]=c[1],a[8]=c[2],a[9]=c[3],a[10]=c[4],a[11]=c[5]);var d=a[12]<<8|a[13];if(2048===d)if(a=a.subarray(14),d=a[0]>>4,4!==d)D("Expected ipv4.version==4 but got: "+d,1048576);else{if(m(5===(a[0]&15),"TODO: ihl!=5"),17===a[9]){a=a.subarray(20);d=a[0]<<8|a[1];var e=a[2]<<8|a[3];D("udp srcport="+d+" dstport="+e+" checksum="+z(a[6]<<8|a[7],4),1048576);if(67===d||67===e)if(d=a.subarray(8),
183
- e=d[236]<<24|d[237]<<16|d[238]<<8|d[239],1669485411!==e)D("dhcp packet didn't match magic: "+z(e,8));else for(d[28]===b[0]&&d[29]===b[1]&&d[30]===b[2]&&d[31]===b[3]&&d[32]===b[4]&&d[33]===b[5]&&(D("Replace mac in dhcp.chaddr",1048576),d[28]=c[0],d[29]=c[1],d[30]=c[2],d[31]=c[3],d[32]=c[4],d[33]=c[5],a[6]=a[7]=0),e=240;e<d.length;){const f=d[e++];if(255===f)break;const h=d[e++];61===f&&1===d[e+0]&&d[e+1]===b[0]&&d[e+2]===b[1]&&d[e+3]===b[2]&&d[e+4]===b[3]&&d[e+5]===b[4]&&d[e+6]===b[5]&&(D("Replace mac in dhcp.clientidentifier",
184
- 1048576),d[e+1]=c[0],d[e+2]=c[1],d[e+3]=c[2],d[e+4]=c[3],d[e+5]=c[4],d[e+6]=c[5],a[6]=a[7]=0);e+=h}}}else 2054===d&&(a=a.subarray(14),D("arp oper="+a[7]+" "+ob(a.subarray(8,14))+" "+ob(a.subarray(18,24)),1048576),a[8]===b[0]&&a[9]===b[1]&&a[10]===b[2]&&a[11]===b[3]&&a[12]===b[4]&&a[13]===b[5]&&(D("Replace mac in arp.sha",1048576),a[8]=c[0],a[9]=c[1],a[10]=c[2],a[11]=c[3],a[12]=c[4],a[13]=c[5]))}function ob(a){return[a[0].toString(16).padStart(2,"0"),a[1].toString(16).padStart(2,"0"),a[2].toString(16).padStart(2,
185
- "0"),a[3].toString(16).padStart(2,"0"),a[4].toString(16).padStart(2,"0"),a[5].toString(16).padStart(2,"0")].join(":")}function pb(a,b,c,d,e){this.cpu=a;this.pci=a.devices.pci;this.id=e||0;this.preserve_mac_from_state_image=c;this.mac_address_translation=d;this.bus=b;this.bus.register("net"+this.id+"-receive",function(f){this.receive(f)},this);this.port=768+256*this.id;this.name="ne2k";this.pci_space=[236,16,41,128,3,1,0,0,0,0,0,2,0,0,0,0,this.port&255|1,this.port>>8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
186
- 0,0,0,0,0,0,0,0,0,0,0,244,26,0,17,0,0,184,254,0,0,0,0,0,0,0,0,0,1,0,0];this.pci_id=(0===this.id?5:7+this.id)<<3;this.pci_bars=[{size:32}];this.imr=this.isr=0;this.cr=1;this.tpsr=this.tcnt=this.rcnt=this.dcfg=0;this.memory=new Uint8Array(32768);this.txcr=this.rxcr=0;this.tsr=1;this.mac=new Uint8Array([0,34,21,255*Math.random()|0,255*Math.random()|0,255*Math.random()|0]);this.bus.send("net"+this.id+"-mac",ob(this.mac));this.mar=Uint8Array.of(255,255,255,255,255,255,255,255);this.mac_address_in_state=
187
- null;for(b=0;6>b;b++)this.memory[b<<1]=this.memory[b<<1|1]=this.mac[b];this.memory[28]=this.memory[29]=87;this.memory[30]=this.memory[31]=87;D("Mac: "+ob(this.mac),1048576);this.rsar=0;this.pstart=64;this.pstop=128;this.boundary=this.curpg=76;b=a.io;b.register_read(this.port|0,this,function(){D("Read cmd",1048576);return this.cr});b.register_write(this.port|0,this,function(f){this.cr=f;D("Write command: "+z(f,2)+" newpg="+(this.cr>>6)+" txcr="+z(this.txcr,2),1048576);this.cr&1||(f&24&&0===this.rcnt&&
188
- this.do_interrupt(64),f&4&&(f=this.tpsr<<8,f=this.memory.subarray(f,f+this.tcnt),this.mac_address_in_state&&(f=new Uint8Array(f),nb(f,this.mac_address_in_state,this.mac)),this.bus.send("net"+this.id+"-send",f),this.bus.send("eth-transmit-end",[f.length]),this.cr&=-5,this.do_interrupt(2),D("Command: Transfer. length="+z(f.byteLength),1048576)))});b.register_read(this.port|13,this,function(){var f=this.get_page();if(1===f)return D("Read mar5",1048576),this.mar[5];D("Read counter0 pg="+f,1048576);return 0});
189
- b.register_read(this.port|14,this,function(){var f=this.get_page();if(1===f)return D("Read mar6",1048576),this.mar[6];D("Read8 counter1 pg="+f,1048576);return 0},function(){D("Read16 counter1 pg="+this.get_page(),1048576);return 0});b.register_read(this.port|15,this,function(){var f=this.get_page();if(1===f)return D("Read mar7",1048576),this.mar[7];D("Read counter2 pg="+f,1048576);return 0});b.register_read(this.port|31,this,function(){this.get_page();D("Read reset",1048576);this.do_interrupt(128);
190
- return 0});b.register_write(this.port|31,this,function(f){this.get_page();D("Write reset: "+z(f,2),1048576)});b.register_read(this.port|1,this,function(){var f=this.get_page();if(0===f)return this.pstart;if(1===f)return D("Read pg1/01 (mac[0])",1048576),this.mac[0];if(2===f)return this.pstart;D("Read pg"+f+"/01");m(!1);return 0});b.register_write(this.port|1,this,function(f){var h=this.get_page();0===h?(D("start page: "+z(f,2),1048576),this.pstart=f):1===h?(D("mac[0] = "+z(f),1048576),this.mac[0]=
191
- f):3===h?D("Unimplemented: Write pg3/01 (9346CR): "+z(f),1048576):(D("Write pg"+h+"/01: "+z(f),1048576),m(!1))});b.register_read(this.port|2,this,function(){var f=this.get_page();if(0===f)return this.pstop;if(1===f)return D("Read pg1/02 (mac[1])",1048576),this.mac[1];if(2===f)return this.pstop;D("Read pg"+f+"/02",1048576);m(!1);return 0});b.register_write(this.port|2,this,function(f){var h=this.get_page();0===h?(D("stop page: "+z(f,2),1048576),f>this.memory.length>>8&&(f=this.memory.length>>8,D("XXX: Adjusting stop page to "+
192
- z(f),1048576)),this.pstop=f):1===h?(D("mac[1] = "+z(f),1048576),this.mac[1]=f):(D("Write pg"+h+"/02: "+z(f),1048576),m(!1))});b.register_read(this.port|7,this,function(){var f=this.get_page();if(0===f)return D("Read isr: "+z(this.isr,2),1048576),this.isr;if(1===f)return D("Read curpg: "+z(this.curpg,2),1048576),this.curpg;m(!1);return 0});b.register_write(this.port|7,this,function(f){var h=this.get_page();0===h?(D("Write isr: "+z(f,2),1048576),this.isr&=~f,this.update_irq()):1===h?(D("Write curpg: "+
193
- z(f,2),1048576),this.curpg=f):m(!1)});b.register_write(this.port|13,this,function(f){var h=this.get_page();0===h?(this.txcr=f,D("Write tx config: "+z(f,2),1048576)):D("Unimplemented: Write pg"+h+"/0d "+z(f,2),1048576)});b.register_write(this.port|14,this,function(f){var h=this.get_page();0===h?(D("Write data configuration: "+z(f,2),1048576),this.dcfg=f):D("Unimplemented: Write pg"+h+"/0e "+z(f,2),1048576)});b.register_read(this.port|10,this,function(){var f=this.get_page();if(0===f)return D("Read pg0/0a",
194
- 1048576),80;if(1===f)return D("Read mar2",1048576),this.mar[2];m(!1,"TODO");return 0});b.register_write(this.port|10,this,function(f){var h=this.get_page();0===h?(D("Write remote byte count low: "+z(f,2),1048576),this.rcnt=this.rcnt&65280|f&255):D("Unimplemented: Write pg"+h+"/0a "+z(f,2),1048576)});b.register_read(this.port|11,this,function(){var f=this.get_page();if(0===f)return D("Read pg0/0b",1048576),67;if(1===f)return D("Read mar3",1048576),this.mar[3];m(!1,"TODO");return 0});b.register_write(this.port|
195
- 11,this,function(f){var h=this.get_page();0===h?(D("Write remote byte count high: "+z(f,2),1048576),this.rcnt=this.rcnt&255|f<<8&65280):D("Unimplemented: Write pg"+h+"/0b "+z(f,2),1048576)});b.register_read(this.port|8,this,function(){var f=this.get_page();if(0===f)return D("Read remote start address low",1048576),this.rsar&255;if(1===f)return D("Read mar0",1048576),this.mar[0];D("Unimplemented: Read pg"+f+"/08",1048576);m(!1);return 0});b.register_write(this.port|8,this,function(f){var h=this.get_page();
196
- 0===h?(D("Write remote start address low: "+z(f,2),1048576),this.rsar=this.rsar&65280|f&255):D("Unimplemented: Write pg"+h+"/08 "+z(f,2),1048576)});b.register_read(this.port|9,this,function(){var f=this.get_page();if(0===f)return D("Read remote start address high",1048576),this.rsar>>8&255;if(1===f)return D("Read mar1",1048576),this.mar[1];D("Unimplemented: Read pg"+f+"/09",1048576);m(!1);return 0});b.register_write(this.port|9,this,function(f){var h=this.get_page();0===h?(D("Write remote start address low: "+
197
- z(f,2),1048576),this.rsar=this.rsar&255|f<<8&65280):D("Unimplemented: Write pg"+h+"/09 "+z(f,2),1048576)});b.register_write(this.port|15,this,function(f){var h=this.get_page();0===h?(D("Write interrupt mask register: "+z(f,2)+" isr="+z(this.isr,2),1048576),this.imr=f,this.update_irq()):D("Unimplemented: Write pg"+h+"/0f "+z(f,2),1048576)});b.register_read(this.port|3,this,function(){var f=this.get_page();if(0===f)return D("Read boundary: "+z(this.boundary,2),1048576),this.boundary;if(1===f)return D("Read pg1/03 (mac[2])",
198
- 1048576),this.mac[2];3===f?D("Unimplemented: Read pg3/03 (CONFIG0)",1048576):(D("Read pg"+f+"/03",1048576),m(!1));return 0});b.register_write(this.port|3,this,function(f){var h=this.get_page();0===h?(D("Write boundary: "+z(f,2),1048576),this.boundary=f):1===h?(D("mac[2] = "+z(f),1048576),this.mac[2]=f):(D("Write pg"+h+"/03: "+z(f),1048576),m(!1))});b.register_read(this.port|4,this,function(){var f=this.get_page();if(0===f)return this.tsr;if(1===f)return D("Read pg1/04 (mac[3])",1048576),this.mac[3];
199
- D("Read pg"+f+"/04",1048576);m(!1);return 0});b.register_write(this.port|4,this,function(f){var h=this.get_page();0===h?(D("Write tpsr: "+z(f,2),1048576),this.tpsr=f):1===h?(D("mac[3] = "+z(f),1048576),this.mac[3]=f):(D("Write pg"+h+"/04: "+z(f),1048576),m(!1))});b.register_read(this.port|5,this,function(){var f=this.get_page();if(0===f)return D("Unimplemented: Read pg0/05 (NCR: Number of Collisions Register)",1048576),0;if(1===f)return D("Read pg1/05 (mac[4])",1048576),this.mac[4];3===f?D("Unimplemented: Read pg3/05 (CONFIG2)",
200
- 1048576):(D("Read pg"+f+"/05",1048576),m(!1));return 0});b.register_write(this.port|5,this,function(f){var h=this.get_page();0===h?(D("Write tcnt low: "+z(f,2),1048576),this.tcnt=this.tcnt&-256|f):1===h?(D("mac[4] = "+z(f),1048576),this.mac[4]=f):3===h?D("Unimplemented: Write pg3/05 (CONFIG2): "+z(f),1048576):(D("Write pg"+h+"/05: "+z(f),1048576),m(!1))});b.register_read(this.port|6,this,function(){var f=this.get_page();if(0===f)return m(!1,"TODO"),0;if(1===f)return D("Read pg1/06 (mac[5])",1048576),
201
- this.mac[5];3===f?D("Unimplemented: Read pg3/06 (CONFIG3)",1048576):(D("Read pg"+f+"/06",1048576),m(!1));return 0});b.register_write(this.port|6,this,function(f){var h=this.get_page();0===h?(D("Write tcnt high: "+z(f,2),1048576),this.tcnt=this.tcnt&255|f<<8):1===h?(D("mac[5] = "+z(f),1048576),this.mac[5]=f):3===h?D("Unimplemented: Write pg3/06 (CONFIG3): "+z(f),1048576):(D("Write pg"+h+"/06: "+z(f),1048576),m(!1))});b.register_read(this.port|12,this,function(){var f=this.get_page();if(0===f)return 9;
202
- if(1===f)return D("Read mar4",1048576),this.mar[4];D("Unimplemented: Read pg"+f+"/0c",1048576);m(!1);return 0});b.register_write(this.port|12,this,function(f){var h=this.get_page();0===h?(D("RX configuration reg write: "+z(f,2),1048576),this.rxcr=f):D("Unimplemented: Write pg"+h+"/0c: "+z(f),1048576)});b.register_read(this.port|16,this,this.data_port_read8,this.data_port_read16,this.data_port_read32);b.register_write(this.port|16,this,this.data_port_write16,this.data_port_write16,this.data_port_write32);
203
- a.devices.pci.register_device(this)}pb.prototype.get_state=function(){var a=[];a[0]=this.isr;a[1]=this.imr;a[2]=this.cr;a[3]=this.dcfg;a[4]=this.rcnt;a[5]=this.tcnt;a[6]=this.tpsr;a[7]=this.rsar;a[8]=this.pstart;a[9]=this.curpg;a[10]=this.boundary;a[11]=this.pstop;a[12]=this.rxcr;a[13]=this.txcr;a[14]=this.tsr;a[15]=this.mac;a[16]=this.memory;return a};pb.prototype.set_state=function(a){this.isr=a[0];this.imr=a[1];this.cr=a[2];this.dcfg=a[3];this.rcnt=a[4];this.tcnt=a[5];this.tpsr=a[6];this.rsar=
204
- a[7];this.pstart=a[8];this.curpg=a[9];this.boundary=a[10];this.pstop=a[11];this.rxcr=a[12];this.txcr=a[13];this.tsr=a[14];this.preserve_mac_from_state_image?(this.mac=a[15],this.memory=a[16]):this.mac_address_translation&&(this.mac_address_in_state=a[15],this.memory=a[16],D("Using mac address translation guest_os_mac="+ob(this.mac_address_in_state)+" real_mac="+ob(this.mac),1048576));this.bus.send("net"+this.id+"-mac",ob(this.mac))};pb.prototype.do_interrupt=function(a){D("Do interrupt "+z(a,2),1048576);
205
- this.isr|=a;this.update_irq()};pb.prototype.update_irq=function(){this.imr&this.isr?this.pci.raise_irq(this.pci_id):this.pci.lower_irq(this.pci_id)};pb.prototype.data_port_write=function(a){if(16>=this.rsar||16384<=this.rsar&&32768>this.rsar)this.memory[this.rsar]=a;this.rsar++;this.rcnt--;this.rsar>=this.pstop<<8&&(this.rsar+=this.pstart-this.pstop<<8);0===this.rcnt&&this.do_interrupt(64)};pb.prototype.data_port_write16=function(a){this.data_port_write(a);this.dcfg&1&&this.data_port_write(a>>8)};
206
- pb.prototype.data_port_write32=function(a){this.data_port_write(a);this.data_port_write(a>>8);this.data_port_write(a>>16);this.data_port_write(a>>24)};pb.prototype.data_port_read=function(){let a=0;32768>this.rsar&&(a=this.memory[this.rsar]);this.rsar++;this.rcnt--;this.rsar>=this.pstop<<8&&(this.rsar+=this.pstart-this.pstop<<8);0===this.rcnt&&this.do_interrupt(64);return a};pb.prototype.data_port_read8=function(){return this.data_port_read16()&255};pb.prototype.data_port_read16=function(){return this.dcfg&
207
- 1?this.data_port_read()|this.data_port_read()<<8:this.data_port_read()};pb.prototype.data_port_read32=function(){return this.data_port_read()|this.data_port_read()<<8|this.data_port_read()<<16|this.data_port_read()<<24};pb.prototype.receive=function(a){if(!(this.cr&1)&&(this.bus.send("eth-receive-end",[a.length]),this.rxcr&16||this.rxcr&4&&255===a[0]&&255===a[1]&&255===a[2]&&255===a[3]&&255===a[4]&&255===a[5]||!(this.rxcr&8&&1===(a[0]&1)||a[0]!==this.mac[0]||a[1]!==this.mac[1]||a[2]!==this.mac[2]||
208
- a[3]!==this.mac[3]||a[4]!==this.mac[4]||a[5]!==this.mac[5]))){this.mac_address_in_state&&(a=new Uint8Array(a),nb(a,this.mac,this.mac_address_in_state));var b=this.curpg<<8,c=Math.max(60,a.length)+4,d=b+4,e=this.curpg+1+(c>>8),f=b+c,h=1+(c>>8),l=this.boundary>this.curpg?this.boundary-this.curpg:this.pstop-this.curpg+this.boundary-this.pstart;l<h&&0!==this.boundary?D("Buffer full, dropping packet pstart="+z(this.pstart)+" pstop="+z(this.pstop)+" curpg="+z(this.curpg)+" needed="+z(h)+" boundary="+z(this.boundary)+
209
- " available="+z(l),1048576):(f>this.pstop<<8?(m(60<=a.length),f=(this.pstop<<8)-d,m(0<=f),this.memory.set(a.subarray(0,f),d),this.memory.set(a.subarray(f),this.pstart<<8),D("rcv cut="+z(f),1048576)):(this.memory.set(a,d),60>a.length&&this.memory.fill(0,d+a.length,d+60)),e>=this.pstop&&(e+=this.pstart-this.pstop),this.memory[b]=1,this.memory[b+1]=e,this.memory[b+2]=c,this.memory[b+3]=c>>8,this.curpg=e,D("rcv offset="+z(b)+" len="+z(c)+" next="+z(e),1048576),this.do_interrupt(1))}};pb.prototype.get_page=
210
- function(){return this.cr>>6&3};var N={Debug:function(a){D([].slice.apply(arguments).join(" "),4194304)},Abort:function(){throw Error("message.Abort()");}};var O={};const qb=new TextDecoder,rb=new TextEncoder;O.Marshall=function(a,b,c,d){for(var e,f=0,h=0;h<a.length;h++)switch(e=b[h],a[h]){case "w":c[d++]=e&255;c[d++]=e>>8&255;c[d++]=e>>16&255;c[d++]=e>>24&255;f+=4;break;case "d":c[d++]=e&255;c[d++]=e>>8&255;c[d++]=e>>16&255;c[d++]=e>>24&255;c[d++]=0;c[d++]=0;c[d++]=0;c[d++]=0;f+=8;break;case "h":c[d++]=
211
- e&255;c[d++]=e>>8;f+=2;break;case "b":c[d++]=e;f+=1;break;case "s":var l=d,n=0;c[d++]=0;c[d++]=0;f+=2;e=rb.encode(e);f+=e.byteLength;n+=e.byteLength;c.set(e,d);d+=e.byteLength;c[l+0]=n&255;c[l+1]=n>>8&255;break;case "Q":O.Marshall(["b","w","d"],[e.type,e.version,e.path],c,d);d+=13;f+=13;break;default:N.Debug("Marshall: Unknown type="+a[h])}return f};O.Unmarshall=function(a,b,c){let d=c.offset;for(var e=[],f=0;f<a.length;f++)switch(a[f]){case "w":var h=b[d++];h+=b[d++]<<8;h+=b[d++]<<16;h+=b[d++]<<
212
- 24>>>0;e.push(h);break;case "d":h=b[d++];h+=b[d++]<<8;h+=b[d++]<<16;h+=b[d++]<<24>>>0;d+=4;e.push(h);break;case "h":h=b[d++];e.push(h+(b[d++]<<8));break;case "b":e.push(b[d++]);break;case "s":h=b[d++];h+=b[d++]<<8;var l=b.slice(d,d+h);d+=h;e.push(qb.decode(l));break;case "Q":c.offset=d;h=O.Unmarshall(["b","w","d"],b,c);d=c.offset;e.push({type:h[0],version:h[1],path:h[2]});break;default:N.Debug("Error in Unmarshall: Unknown type="+a[f])}c.offset=d;return e};function sb(a,b){this.bus=b;this.rows=25;
213
- this.cols=80;this.ports=4;b=[{size_supported:16,notify_offset:0},{size_supported:16,notify_offset:1},{size_supported:16,notify_offset:2},{size_supported:16,notify_offset:3}];for(let c=1;c<this.ports;++c)b.push({size_supported:16,notify_offset:0}),b.push({size_supported:8,notify_offset:1});this.virtio=new xb(a,{name:"virtio-console",pci_id:96,device_id:4163,subsystem_device_id:3,common:{initial_port:47104,queues:b,features:[0,1,32],on_driver_ok:()=>{}},notification:{initial_port:47360,single_handler:!1,
214
- handlers:[()=>{},c=>{const d=this.virtio.queues[c],e=3<c?c-3>>1:0;for(;d.has_request();){const f=d.pop_request(),h=new Uint8Array(f.length_readable);f.get_next_blob(h);this.bus.send("virtio-console"+e+"-output-bytes",h);this.Ack(c,f)}},c=>{if(2!==c)m(!1,"VirtioConsole Notified for wrong queue: "+c+" (expected queue_id of 2)");else for(c=this.virtio.queues[c];c.count_requests()>c.size-2;)c.pop_request()},c=>{if(3!==c)m(!1,"VirtioConsole Notified for wrong queue: "+c+" (expected queue_id of 3)");else for(var d=
215
- this.virtio.queues[c];d.has_request();){var e=d.pop_request(),f=new Uint8Array(e.length_readable);e.get_next_blob(f);var h=O.Unmarshall(["w","h","h"],f,{offset:0});f=h[0];h=h[1];this.Ack(c,e);switch(h){case 0:for(e=0;e<this.ports;++e)this.SendEvent(e,1,0);break;case 3:this.Ack(c,e);this.SendEvent(f,4,1);this.SendName(f,"virtio-"+f);this.SendEvent(f,6,1);break;case 6:this.Ack(c,e);0===f&&this.SendWindowSize(f);break;default:m(!1," VirtioConsole received unknown event: "+h[1]);return}}}]},isr_status:{initial_port:46848},
216
- device_specific:{initial_port:46592,struct:[{bytes:2,name:"cols",read:()=>this.cols,write:()=>{}},{bytes:2,name:"rows",read:()=>this.rows,write:()=>{}},{bytes:4,name:"max_nr_ports",read:()=>this.ports,write:()=>{}},{bytes:4,name:"emerg_wr",read:()=>0,write:()=>{m(!1,"Emergency write!")}}]}});for(let c=0;c<this.ports;++c){const d=0===c?0:2*c+2;this.bus.register("virtio-console"+c+"-input-bytes",function(e){var f=this.virtio.queues[d];f.has_request()&&(f=f.pop_request(),this.Send(d,f,new Uint8Array(e)))},
217
- this);this.bus.register("virtio-console"+c+"-resize",function(e){0===c&&(this.cols=e[0],this.rows=e[1]);this.virtio.queues[2].is_configured()&&this.virtio.queues[2].has_request()&&this.SendWindowSize(c,e[0],e[1])},this)}}sb.prototype.SendWindowSize=function(a,b,c){c=c||this.rows;b=b||this.cols;const d=this.virtio.queues[2].pop_request(),e=new Uint8Array(12);O.Marshall(["w","h","h","h","h"],[a,5,0,c,b],e,0);this.Send(2,d,e)};sb.prototype.SendName=function(a,b){const c=this.virtio.queues[2].pop_request();
218
- b=(new TextEncoder).encode(b);const d=new Uint8Array(8+b.length+1);O.Marshall(["w","h","h"],[a,7,1],d,0);for(a=0;a<b.length;++a)d[a+8]=b[a];d[8+b.length]=0;this.Send(2,c,d)};sb.prototype.get_state=function(){const a=[];a[0]=this.virtio;a[1]=this.rows;a[2]=this.cols;a[3]=this.ports;return a};sb.prototype.set_state=function(a){this.virtio.set_state(a[0]);this.rows=a[1];this.cols=a[2];this.ports=a[3]};sb.prototype.reset=function(){this.virtio.reset()};sb.prototype.SendEvent=function(a,b,c){const d=this.virtio.queues[2].pop_request(),
219
- e=new Uint8Array(8);O.Marshall(["w","h","h"],[a,b,c],e,0);this.Send(2,d,e)};sb.prototype.Send=function(a,b,c){b.set_next_blob(c);this.virtio.queues[a].push_reply(b);this.virtio.queues[a].flush_replies()};sb.prototype.Ack=function(a,b){b.set_next_blob(new Uint8Array(0));this.virtio.queues[a].push_reply(b);this.virtio.queues[a].flush_replies()};function yb(a,b){this.cpu=a;this.bus=b;this.reset();this.bus.register("keyboard-code",function(c){this.kbd_send_code(c)},this);this.bus.register("mouse-click",
220
- function(c){this.mouse_send_click(c[0],c[1],c[2])},this);this.bus.register("mouse-delta",function(c){this.mouse_send_delta(c[0],c[1])},this);this.bus.register("mouse-wheel",function(c){this.wheel_movement-=c[0];this.wheel_movement-=2*c[1];this.wheel_movement=Math.min(7,Math.max(-8,this.wheel_movement));this.send_mouse_packet(0,0)},this);a.io.register_read(96,this,this.port60_read);a.io.register_read(100,this,this.port64_read);a.io.register_write(96,this,this.port60_write);a.io.register_write(100,
221
- this,this.port64_write)}yb.prototype.reset=function(){this.use_mouse=this.enable_mouse_stream=!1;this.have_mouse=!0;this.mouse_clicks=this.mouse_delta_y=this.mouse_delta_x=0;this.have_keyboard=!0;this.next_read_resolution=this.next_read_rate=this.next_handle_scan_code_set=this.next_read_led=this.next_read_sample=this.next_is_mouse_command=this.enable_keyboard_stream=!1;this.kbd_buffer=new oa(1024);this.last_port60_byte=0;this.sample_rate=100;this.mouse_id=this.mouse_detect_state=0;this.mouse_reset_workaround=
222
- !1;this.wheel_movement=0;this.resolution=4;this.scaling2=!1;this.last_mouse_packet=-1;this.mouse_buffer=new oa(1024);this.next_byte_is_aux=this.next_byte_is_ready=!1;this.command_register=5;this.controller_output_port=0;this.read_controller_output_port=this.read_command_register=this.read_output_register=!1};yb.prototype.get_state=function(){var a=[];a[0]=this.enable_mouse_stream;a[1]=this.use_mouse;a[2]=this.have_mouse;a[3]=this.mouse_delta_x;a[4]=this.mouse_delta_y;a[5]=this.mouse_clicks;a[6]=this.have_keyboard;
223
- a[7]=this.enable_keyboard_stream;a[8]=this.next_is_mouse_command;a[9]=this.next_read_sample;a[10]=this.next_read_led;a[11]=this.next_handle_scan_code_set;a[12]=this.next_read_rate;a[13]=this.next_read_resolution;a[15]=this.last_port60_byte;a[16]=this.sample_rate;a[17]=this.resolution;a[18]=this.scaling2;a[20]=this.command_register;a[21]=this.read_output_register;a[22]=this.read_command_register;a[23]=this.controller_output_port;a[24]=this.read_controller_output_port;a[25]=this.mouse_id;a[26]=this.mouse_detect_state;
224
- a[27]=this.mouse_reset_workaround;return a};yb.prototype.set_state=function(a){this.enable_mouse_stream=a[0];this.use_mouse=a[1];this.have_mouse=a[2];this.mouse_delta_x=a[3];this.mouse_delta_y=a[4];this.mouse_clicks=a[5];this.have_keyboard=a[6];this.enable_keyboard_stream=a[7];this.next_is_mouse_command=a[8];this.next_read_sample=a[9];this.next_read_led=a[10];this.next_handle_scan_code_set=a[11];this.next_read_rate=a[12];this.next_read_resolution=a[13];this.last_port60_byte=a[15];this.sample_rate=
225
- a[16];this.resolution=a[17];this.scaling2=a[18];this.command_register=a[20];this.read_output_register=a[21];this.read_command_register=a[22];this.controller_output_port=a[23];this.read_controller_output_port=a[24];this.mouse_id=a[25]||0;this.mouse_detect_state=a[26]||0;this.mouse_reset_workaround=a[27]||!1;this.next_byte_is_aux=this.next_byte_is_ready=!1;this.kbd_buffer.clear();this.mouse_buffer.clear();this.bus.send("mouse-enable",this.use_mouse)};yb.prototype.raise_irq=function(){this.next_byte_is_ready||
226
- (this.kbd_buffer.length?this.kbd_irq():this.mouse_buffer.length&&this.mouse_irq())};yb.prototype.mouse_irq=function(){this.next_byte_is_aux=this.next_byte_is_ready=!0;this.command_register&2&&(D("Mouse irq",64),this.cpu.device_lower_irq(12),this.cpu.device_raise_irq(12))};yb.prototype.kbd_irq=function(){this.next_byte_is_ready=!0;this.next_byte_is_aux=!1;this.command_register&1&&(D("Keyboard irq",64),this.cpu.device_lower_irq(1),this.cpu.device_raise_irq(1))};yb.prototype.kbd_send_code=function(a){this.enable_keyboard_stream&&
227
- (D("adding kbd code: "+z(a),64),this.kbd_buffer.push(a),this.raise_irq())};yb.prototype.mouse_send_delta=function(a,b){if(this.have_mouse&&this.use_mouse){var c=this.resolution*this.sample_rate/80;this.mouse_delta_x+=a*c;this.mouse_delta_y+=b*c;this.enable_mouse_stream&&(a=this.mouse_delta_x|0,b=this.mouse_delta_y|0,a||b)&&(Date.now(),this.mouse_delta_x-=a,this.mouse_delta_y-=b,this.send_mouse_packet(a,b))}};yb.prototype.mouse_send_click=function(a,b,c){this.have_mouse&&this.use_mouse&&(this.mouse_clicks=
228
- a|c<<1|b<<2,this.enable_mouse_stream&&this.send_mouse_packet(0,0))};yb.prototype.send_mouse_packet=function(a,b){var c=(0>b)<<5|(0>a)<<4|8|this.mouse_clicks;this.last_mouse_packet=Date.now();this.mouse_buffer.push(c);this.mouse_buffer.push(a);this.mouse_buffer.push(b);4===this.mouse_id?(this.mouse_buffer.push(0|this.wheel_movement&15),this.wheel_movement=0):3===this.mouse_id&&(this.mouse_buffer.push(this.wheel_movement&255),this.wheel_movement=0);this.raise_irq()};yb.prototype.apply_scaling2=function(a){var b=
229
- a>>31;switch(Math.abs(a)){case 0:case 1:case 3:return a;case 2:return b;case 4:return 6*b;case 5:return 9*b;default:return a<<1}};yb.prototype.port60_read=function(){this.next_byte_is_ready=!1;if(!this.kbd_buffer.length&&!this.mouse_buffer.length)return D("Port 60 read: Empty",64),this.last_port60_byte;this.next_byte_is_aux?(this.cpu.device_lower_irq(12),this.last_port60_byte=this.mouse_buffer.shift(),D("Port 60 read (mouse): "+z(this.last_port60_byte),64)):(this.cpu.device_lower_irq(1),this.last_port60_byte=
230
- this.kbd_buffer.shift(),D("Port 60 read (kbd) : "+z(this.last_port60_byte),64));(this.kbd_buffer.length||this.mouse_buffer.length)&&this.raise_irq();return this.last_port60_byte};yb.prototype.port64_read=function(){var a=16;this.next_byte_is_ready&&(a|=1);this.next_byte_is_aux&&(a|=32);D("port 64 read: "+z(a),64);return a};yb.prototype.port60_write=function(a){D("port 60 write: "+z(a),64);if(this.read_command_register)this.command_register=a,this.read_command_register=!1,D("Keyboard command register = "+
231
- z(this.command_register),64);else if(this.read_output_register)this.read_output_register=!1,this.mouse_buffer.clear(),this.mouse_buffer.push(a),this.mouse_irq();else if(this.next_read_sample){this.next_read_sample=!1;this.mouse_buffer.clear();this.mouse_buffer.push(250);this.sample_rate=a;switch(this.mouse_detect_state){case -1:60===a?(this.mouse_reset_workaround=!0,this.mouse_detect_state=0):(this.mouse_reset_workaround=!1,this.mouse_detect_state=200===a?1:0);break;case 0:200===a&&(this.mouse_detect_state=
232
- 1);break;case 1:this.mouse_detect_state=100===a?2:200===a?3:0;break;case 2:80===a&&(this.mouse_id=3);this.mouse_detect_state=-1;break;case 3:80===a&&(this.mouse_id=4),this.mouse_detect_state=-1}D("mouse sample rate: "+z(a)+", mouse id: "+z(this.mouse_id),64);this.sample_rate||(D("invalid sample rate, reset to 100",64),this.sample_rate=100);this.mouse_irq()}else if(this.next_read_resolution)this.next_read_resolution=!1,this.mouse_buffer.clear(),this.mouse_buffer.push(250),3<a?(this.resolution=4,D("invalid resolution, resetting to 4",
233
- 64)):(this.resolution=1<<a,D("resolution: "+this.resolution,64)),this.mouse_irq();else if(this.next_read_led)this.next_read_led=!1,this.kbd_buffer.push(250),this.kbd_irq();else if(this.next_handle_scan_code_set)this.next_handle_scan_code_set=!1,this.kbd_buffer.push(250),this.kbd_irq(),a||this.kbd_buffer.push(1);else if(this.next_read_rate)this.next_read_rate=!1,this.kbd_buffer.push(250),this.kbd_irq();else if(this.next_is_mouse_command){if(this.next_is_mouse_command=!1,D("Port 60 data register write: "+
234
- z(a),64),this.have_mouse){this.kbd_buffer.clear();this.mouse_buffer.clear();this.mouse_buffer.push(250);switch(a){case 230:D("Scaling 1:1",64);this.scaling2=!1;break;case 231:D("Scaling 2:1",64);this.scaling2=!0;break;case 232:this.next_read_resolution=!0;break;case 233:this.send_mouse_packet(0,0);break;case 235:D("unimplemented request single packet",64);this.send_mouse_packet(0,0);break;case 242:D("required id: "+z(this.mouse_id),64);this.mouse_buffer.push(this.mouse_id);this.mouse_clicks=this.mouse_delta_x=
235
- this.mouse_delta_y=0;this.raise_irq();break;case 243:this.next_read_sample=!0;break;case 244:this.use_mouse=this.enable_mouse_stream=!0;this.bus.send("mouse-enable",!0);this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0;break;case 245:this.enable_mouse_stream=!1;break;case 246:this.enable_mouse_stream=!1;this.sample_rate=100;this.scaling2=!1;this.resolution=4;break;case 255:D("Mouse reset",64);this.mouse_buffer.push(170);this.mouse_buffer.push(0);this.use_mouse=!0;this.bus.send("mouse-enable",
236
- !0);this.enable_mouse_stream=!1;this.sample_rate=100;this.scaling2=!1;this.resolution=4;this.mouse_reset_workaround||(this.mouse_id=0);this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0;break;default:D("Unimplemented mouse command: "+z(a),64)}this.mouse_irq()}}else if(this.read_controller_output_port)this.read_controller_output_port=!1,this.controller_output_port=a;else{D("Port 60 data register write: "+z(a),64);this.mouse_buffer.clear();this.kbd_buffer.clear();this.kbd_buffer.push(250);switch(a){case 237:this.next_read_led=
237
- !0;break;case 240:this.next_handle_scan_code_set=!0;break;case 242:this.kbd_buffer.push(171);this.kbd_buffer.push(131);break;case 243:this.next_read_rate=!0;break;case 244:D("kbd enable scanning",64);this.enable_keyboard_stream=!0;break;case 245:D("kbd disable scanning",64);this.enable_keyboard_stream=!1;break;case 246:break;case 255:this.kbd_buffer.clear();this.kbd_buffer.push(250);this.kbd_buffer.push(170);this.kbd_buffer.push(0);break;default:D("Unimplemented keyboard command: "+z(a),64)}this.kbd_irq()}};
238
- yb.prototype.port64_write=function(a){D("port 64 write: "+z(a),64);switch(a){case 32:this.kbd_buffer.clear();this.mouse_buffer.clear();this.kbd_buffer.push(this.command_register);this.kbd_irq();break;case 96:this.read_command_register=!0;break;case 209:this.read_controller_output_port=!0;break;case 211:this.read_output_register=!0;break;case 212:this.next_is_mouse_command=!0;break;case 167:D("Disable second port",64);this.command_register|=32;break;case 168:D("Enable second port",64);this.command_register&=
239
- -33;break;case 169:this.kbd_buffer.clear();this.mouse_buffer.clear();this.kbd_buffer.push(0);this.kbd_irq();break;case 170:this.kbd_buffer.clear();this.mouse_buffer.clear();this.kbd_buffer.push(85);this.kbd_irq();break;case 171:this.kbd_buffer.clear();this.mouse_buffer.clear();this.kbd_buffer.push(0);this.kbd_irq();break;case 173:D("Disable Keyboard",64);this.command_register|=16;break;case 174:D("Enable Keyboard",64);this.command_register&=-17;break;case 254:D("CPU reboot via PS2");this.cpu.reboot_internal();
240
- break;default:D("port 64: Unimplemented command byte: "+z(a),64)}};const zb=DataView.prototype,Ab={size:1,get:zb.getUint8,set:zb.setUint8},Bb={size:2,get:zb.getUint16,set:zb.setUint16},P={size:4,get:zb.getUint32,set:zb.setUint32},Db=Cb([{magic:P},{class:Ab},{data:Ab},{version0:Ab},{osabi:Ab},{abiversion:Ab},{pad0:function(a){return{size:a,get:()=>-1}}(7)},{type:Bb},{machine:Bb},{version1:P},{entry:P},{phoff:P},{shoff:P},{flags:P},{ehsize:Bb},{phentsize:Bb},{phnum:Bb},{shentsize:Bb},{shnum:Bb},{shstrndx:Bb}]);
241
- console.assert(52===Db.reduce((a,b)=>a+b.size,0));const Nb=Cb([{type:P},{offset:P},{vaddr:P},{paddr:P},{filesz:P},{memsz:P},{flags:P},{align:P}]);console.assert(32===Nb.reduce((a,b)=>a+b.size,0));const Ob=Cb([{name:P},{type:P},{flags:P},{addr:P},{offset:P},{size:P},{link:P},{info:P},{addralign:P},{entsize:P}]);console.assert(40===Ob.reduce((a,b)=>a+b.size,0));function Cb(a){return a.map(function(b){var c=Object.keys(b);console.assert(1===c.length);c=c[0];b=b[c];console.assert(0<b.size);return{name:c,
242
- type:b,size:b.size,get:b.get,set:b.set}})}function Pb(a,b){const c={};let d=0;for(const e of b)b=e.get.call(a,d,!0),console.assert(void 0===c[e.name]),c[e.name]=b,d+=e.size;return[c,d]}function Qb(a,b,c){const d=[];let e=0;for(var f=0;f<c;f++){const [h,l]=Pb(new DataView(a.buffer,a.byteOffset+e,void 0),b);d.push(h);e+=l}return[d,e]}function Rb(a){this.cpu=a;this.cmos_index=0;this.cmos_data=new Uint8Array(128);this.last_update=this.rtc_time=Date.now();this.next_interrupt_alarm=this.next_interrupt=
243
- 0;this.periodic_interrupt=!1;this.periodic_interrupt_time=.9765625;this.cmos_a=38;this.cmos_b=2;this.nmi_disabled=this.cmos_c=0;a.io.register_write(112,this,function(b){this.cmos_index=b&127;this.nmi_disabled=b>>7});a.io.register_write(113,this,this.cmos_port_write);a.io.register_read(113,this,this.cmos_port_read)}Rb.prototype.get_state=function(){var a=[];a[0]=this.cmos_index;a[1]=this.cmos_data;a[2]=this.rtc_time;a[3]=this.last_update;a[4]=this.next_interrupt;a[5]=this.next_interrupt_alarm;a[6]=
244
- this.periodic_interrupt;a[7]=this.periodic_interrupt_time;a[8]=this.cmos_a;a[9]=this.cmos_b;a[10]=this.cmos_c;a[11]=this.nmi_disabled;return a};Rb.prototype.set_state=function(a){this.cmos_index=a[0];this.cmos_data=a[1];this.rtc_time=a[2];this.last_update=a[3];this.next_interrupt=a[4];this.next_interrupt_alarm=a[5];this.periodic_interrupt=a[6];this.periodic_interrupt_time=a[7];this.cmos_a=a[8];this.cmos_b=a[9];this.cmos_c=a[10];this.nmi_disabled=a[11]};Rb.prototype.timer=function(a){a=Date.now();
245
- this.rtc_time+=a-this.last_update;this.last_update=a;this.periodic_interrupt&&this.next_interrupt<a?(this.cpu.device_raise_irq(8),this.cmos_c|=192,this.next_interrupt+=this.periodic_interrupt_time*Math.ceil((a-this.next_interrupt)/this.periodic_interrupt_time)):this.next_interrupt_alarm&&this.next_interrupt_alarm<a&&(this.cpu.device_raise_irq(8),this.cmos_c|=160,this.next_interrupt_alarm=0);let b=100;this.periodic_interrupt&&this.next_interrupt&&(b=Math.min(b,Math.max(0,this.next_interrupt-a)));this.next_interrupt_alarm&&
246
- (b=Math.min(b,Math.max(0,this.next_interrupt_alarm-a)));return b};Rb.prototype.bcd_pack=function(a){for(var b=0,c=0,d;a;)d=a%10,c|=d<<4*b,b++,a=(a-d)/10;return c};Rb.prototype.bcd_unpack=function(a){const b=a&15,c=a>>4&15;m(256>a);m(10>b);m(10>c);return b+10*c};Rb.prototype.encode_time=function(a){return this.cmos_b&4?a:this.bcd_pack(a)};Rb.prototype.decode_time=function(a){return this.cmos_b&4?a:this.bcd_unpack(a)};Rb.prototype.cmos_port_read=function(){var a=this.cmos_index;switch(a){case 0:return D("read second: "+
247
- z(this.encode_time((new Date(this.rtc_time)).getUTCSeconds())),65536),this.encode_time((new Date(this.rtc_time)).getUTCSeconds());case 2:return D("read minute: "+z(this.encode_time((new Date(this.rtc_time)).getUTCMinutes())),65536),this.encode_time((new Date(this.rtc_time)).getUTCMinutes());case 4:return D("read hour: "+z(this.encode_time((new Date(this.rtc_time)).getUTCHours())),65536),this.encode_time((new Date(this.rtc_time)).getUTCHours());case 6:return D("read day: "+z(this.encode_time((new Date(this.rtc_time)).getUTCDay()+
248
- 1)),65536),this.encode_time((new Date(this.rtc_time)).getUTCDay()+1);case 7:return D("read day of month: "+z(this.encode_time((new Date(this.rtc_time)).getUTCDate())),65536),this.encode_time((new Date(this.rtc_time)).getUTCDate());case 8:return D("read month: "+z(this.encode_time((new Date(this.rtc_time)).getUTCMonth()+1)),65536),this.encode_time((new Date(this.rtc_time)).getUTCMonth()+1);case 9:return D("read year: "+z(this.encode_time((new Date(this.rtc_time)).getUTCFullYear()%100)),65536),this.encode_time((new Date(this.rtc_time)).getUTCFullYear()%
249
- 100);case 10:return 999<=J.microtick()%1E3?this.cmos_a|128:this.cmos_a;case 11:return this.cmos_b;case 12:return this.cpu.device_lower_irq(8),D("cmos reg C read",65536),a=this.cmos_c,this.cmos_c&=-241,a;case 13:return 0;case 50:case 55:return D("read century: "+z(this.encode_time((new Date(this.rtc_time)).getUTCFullYear()/100|0)),65536),this.encode_time((new Date(this.rtc_time)).getUTCFullYear()/100|0);default:return D("cmos read from index "+z(a),65536),this.cmos_data[this.cmos_index]}};Rb.prototype.cmos_port_write=
250
- function(a){switch(this.cmos_index){case 10:this.cmos_a=a&127;this.periodic_interrupt_time=1E3/(32768>>(this.cmos_a&15)-1);D("Periodic interrupt, a="+z(this.cmos_a,2)+" t="+this.periodic_interrupt_time,65536);break;case 11:this.cmos_b=a;this.cmos_b&64&&(this.next_interrupt=Date.now());if(this.cmos_b&32){a=new Date;const b=this.decode_time(this.cmos_data[1]),c=this.decode_time(this.cmos_data[3]),d=this.decode_time(this.cmos_data[5]),e=new Date(Date.UTC(a.getUTCFullYear(),a.getUTCMonth(),a.getUTCDate(),
251
- d,c,b));D("RTC alarm scheduled for "+e+" hh:mm:ss="+d+":"+c+":"+b+" ms_from_now="+(e-a),65536);this.next_interrupt_alarm=+e}this.cmos_b&16&&D("Unimplemented: updated interrupt",65536);D("cmos b="+z(this.cmos_b,2),65536);break;case 1:case 3:case 5:this.cmos_write(this.cmos_index,a);break;default:D("cmos write index "+z(this.cmos_index)+": "+z(a),65536)}this.periodic_interrupt=64===(this.cmos_b&64)&&0<(this.cmos_a&15)};Rb.prototype.cmos_read=function(a){m(128>a);return this.cmos_data[a]};Rb.prototype.cmos_write=
252
- function(a,b){D("cmos "+z(a)+" <- "+z(b),65536);m(128>a);this.cmos_data[a]=b};function Q(a,b,c){this.io=a.io;this.cpu=a;this.dma=a.devices.dma;this.bytes_expecting=0;this.receiving_command=new Uint8Array(10);this.receiving_index=0;this.next_command=null;this.response_data=new Uint8Array(10);this.last_head=this.last_cylinder=this.drive=this.status_reg2=this.status_reg1=this.status_reg0=this.response_length=this.response_index=0;this.last_sector=1;this.dir=this.dor=0;this.fdb_image=this.fda_image=null;
253
- b?this.set_fda(b):(this.eject_fda(),this.cpu.devices.rtc.cmos_write(16,64));m(!c,"FDB not supported");this.io.register_read(1008,this,this.port3F0_read);this.io.register_read(1010,this,this.port3F2_read);this.io.register_read(1012,this,this.port3F4_read);this.io.register_read(1013,this,this.port3F5_read);this.io.register_read(1015,this,this.port3F7_read);this.io.register_write(1010,this,this.port3F2_write);this.io.register_write(1012,this,this.port3F4_write);this.io.register_write(1013,this,this.port3F5_write)}
254
- Q.prototype.eject_fda=function(){this.fda_image=null;this.number_of_cylinders=this.number_of_heads=this.sectors_per_track=0;this.dir=128};Q.prototype.set_fda=function(a){var b={[163840]:{type:1,tracks:40,sectors:8,heads:1},[184320]:{type:1,tracks:40,sectors:9,heads:1},[204800]:{type:1,tracks:40,sectors:10,heads:1},[327680]:{type:1,tracks:40,sectors:8,heads:2},[368640]:{type:1,tracks:40,sectors:9,heads:2},[409600]:{type:1,tracks:40,sectors:10,heads:2},[737280]:{type:3,tracks:80,sectors:9,heads:2},
255
- [1228800]:{type:2,tracks:80,sectors:15,heads:2},[1474560]:{type:4,tracks:80,sectors:18,heads:2},[1763328]:{type:5,tracks:82,sectors:21,heads:2},[2949120]:{type:5,tracks:80,sectors:36,heads:2},512:{type:1,tracks:1,sectors:1,heads:1}};let c=a.byteLength,d=b[c];d||(c=1474560<a.byteLength?2949120:1474560,d=b[c],m(a.buffer&&a.buffer instanceof ArrayBuffer),b=new Uint8Array(c),b.set(new Uint8Array(a.buffer)),a=new Aa(b.buffer),D("Warning: Unkown floppy size: "+a.byteLength+", assuming "+c));this.sectors_per_track=
256
- d.sectors;this.number_of_heads=d.heads;this.number_of_cylinders=d.tracks;this.fda_image=a;this.dir=128;this.cpu.devices.rtc.cmos_write(16,d.type<<4)};Q.prototype.get_state=function(){var a=[];a[0]=this.bytes_expecting;a[1]=this.receiving_command;a[2]=this.receiving_index;a[4]=this.response_data;a[5]=this.response_index;a[6]=this.response_length;a[8]=this.status_reg0;a[9]=this.status_reg1;a[10]=this.status_reg2;a[11]=this.drive;a[12]=this.last_cylinder;a[13]=this.last_head;a[14]=this.last_sector;a[15]=
257
- this.dor;a[16]=this.sectors_per_track;a[17]=this.number_of_heads;a[18]=this.number_of_cylinders;return a};Q.prototype.set_state=function(a){this.bytes_expecting=a[0];this.receiving_command=a[1];this.receiving_index=a[2];this.next_command=a[3];this.response_data=a[4];this.response_index=a[5];this.response_length=a[6];this.status_reg0=a[8];this.status_reg1=a[9];this.status_reg2=a[10];this.drive=a[11];this.last_cylinder=a[12];this.last_head=a[13];this.last_sector=a[14];this.dor=a[15];this.sectors_per_track=
258
- a[16];this.number_of_heads=a[17];this.number_of_cylinders=a[18]};Q.prototype.port3F0_read=function(){D("3F0 read",8192);return 0};Q.prototype.port3F4_read=function(){D("3F4 read",8192);var a=128;this.response_index<this.response_length&&(a|=80);0===(this.dor&8)&&(a|=32);return a};Q.prototype.port3F7_read=function(){D("3F7 read",8192);return this.dir};Q.prototype.port3F5_read=function(){if(this.response_index<this.response_length)return D("3F5 read: "+this.response_data[this.response_index],8192),
259
- this.cpu.device_lower_irq(6),this.response_data[this.response_index++];D("3F5 read, empty",8192);return 255};Q.prototype.port3F4_write=function(a){D("3F4/data rate write: "+z(a),8192);a&128&&(D("dsr reset",8192),this.status_reg0=192,this.cpu.device_raise_irq(6))};Q.prototype.port3F5_write=function(a){D("3F5 write "+z(a),8192);if(0<this.bytes_expecting){if(this.receiving_command[this.receiving_index++]=a,this.bytes_expecting--,0===this.bytes_expecting){a="3F5 command received: ";for(var b=0;b<this.receiving_index;b++)a+=
260
- z(this.receiving_command[b])+" ";D(a,8192);this.next_command.call(this,this.receiving_command)}}else{switch(a){case 3:this.next_command=this.fix_drive_data;this.bytes_expecting=2;break;case 19:this.next_command=this.configure;this.bytes_expecting=3;break;case 4:this.next_command=this.check_drive_status;this.bytes_expecting=1;break;case 5:case 69:case 197:this.next_command=function(c){this.do_sector(!0,c)};this.bytes_expecting=8;break;case 6:case 70:case 198:case 230:this.next_command=function(c){this.do_sector(!1,
261
- c)};this.bytes_expecting=8;break;case 7:this.next_command=this.calibrate;this.bytes_expecting=1;break;case 8:this.check_interrupt_status();break;case 74:this.next_command=this.read_sector_id;this.bytes_expecting=1;break;case 15:this.bytes_expecting=2;this.next_command=this.seek;break;case 14:case 16:D(14===a?"dump registers":"determine controller version",8192);this.status_reg0=128;this.response_data[0]=this.status_reg0;this.response_index=0;this.response_length=1;this.bytes_expecting=0;break;default:m(!1,
262
- "Unimplemented floppy command call "+z(a))}this.receiving_index=0}};Q.prototype.port3F2_read=function(){D("read 3F2: DOR",8192);return this.dor};Q.prototype.port3F2_write=function(a){4===(a&4)&&0===(this.dor&4)&&(this.status_reg0=192,this.cpu.device_raise_irq(6));D("start motors: "+z(a>>4),8192);D("enable dma/irq: "+!!(a&8),8192);D("reset fdc: "+!!(a&4),8192);D("drive select: "+(a&3),8192);0!==(a&3)&&D("guest: fdb not implemented",8192);D("DOR = "+z(a),8192);this.dor=a};Q.prototype.check_drive_status=
263
- function(){D("check drive status",8192);this.status_reg1=this.fda_image?0:5;this.response_index=0;this.response_length=1;this.response_data[0]=0};Q.prototype.seek=function(a){D("seek",8192);if(0!==(a[0]&3))D("seek on fdb",8192);else{var b=a[1];a=a[0]>>2&1;b!==this.last_cylinder&&(this.dir=0);this.status_reg1=this.fda_image?0:5;this.status_reg0=32;this.last_cylinder=b;this.last_head=a}this.raise_irq()};Q.prototype.calibrate=function(a){D("floppy calibrate",8192);this.seek([a[0],0])};Q.prototype.check_interrupt_status=
264
- function(){D("floppy check interrupt status",8192);this.response_index=0;this.response_length=2;this.response_data[0]=this.status_reg0;this.response_data[1]=this.last_cylinder};Q.prototype.do_sector=function(a,b){var c=b[2],d=b[1],e=b[3],f=128<<b[4],h=b[5]-b[3]+1,l=((c+this.number_of_heads*d)*this.sectors_per_track+e-1)*f;D("Floppy "+(a?"Write":"Read"),8192);D("from "+z(l)+" length "+z(h*f),8192);D(d+" / "+c+" / "+e,8192);b[4]||D("FDC: sector count is zero, use data length instead",8192);this.fda_image?
265
- (this.status_reg1=0,a?this.dma.do_write(this.fda_image,l,h*f,2,this.done.bind(this,b,d,c,e)):this.dma.do_read(this.fda_image,l,h*f,2,this.done.bind(this,b,d,c,e))):this.status_reg1=5};Q.prototype.done=function(a,b,c,d,e){e?D("XXX: Unhandled floppy error",8192):(d++,d>this.sectors_per_track&&(d=1,c++,c>=this.number_of_heads&&(c=0,b++)),b!==this.last_cylinder&&(this.dir=0),this.status_reg0=32,this.last_cylinder=b,this.last_head=c,this.last_sector=d,this.response_index=0,this.response_length=7,this.response_data[0]=
266
- c<<2|32,this.response_data[1]=0,this.response_data[2]=0,this.response_data[3]=b,this.response_data[4]=c,this.response_data[5]=d,this.response_data[6]=a[4],this.raise_irq())};Q.prototype.fix_drive_data=function(a){D("floppy fix drive data "+a.slice(0,this.bytes_expecting),8192)};Q.prototype.configure=function(a){D("floppy configure "+a.slice(0,this.bytes_expecting),8192)};Q.prototype.read_sector_id=function(a){D("floppy read sector id "+a,8192);this.response_index=0;this.response_length=7;this.response_data[0]=
267
- 0;this.response_data[1]=0;this.response_data[2]=0;this.response_data[3]=0;this.response_data[4]=0;this.response_data[5]=0;this.response_data[6]=0;this.raise_irq()};Q.prototype.raise_irq=function(){this.dor&8&&this.cpu.device_raise_irq(6)};function Sb(a,b,c,d,e,f){this.master=new R(this,a,b,d,e,0,f);this.slave=new R(this,a,c,!1,e,1,f);this.current_interface=this.master;this.cpu=a;0===e?(this.ata_port=496,this.irq=14,this.pci_id=240):1===e?(this.ata_port=368,this.irq=15,this.pci_id=248):m(!1,"IDE device with nr "+
268
- e+" ignored",32768);this.ata_port_high=this.ata_port|516;this.master_port=46080;this.pci_space=[134,128,16,112,5,0,160,2,0,128,1,1,0,0,0,0,this.ata_port&255|1,this.ata_port>>8,0,0,this.ata_port_high&255|1,this.ata_port_high>>8,0,0,0,0,0,0,0,0,0,0,this.master_port&255|1,this.master_port>>8,0,0,0,0,0,0,0,0,0,0,67,16,212,130,0,0,0,0,0,0,0,0,0,0,0,0,this.irq,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
269
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];this.pci_bars=[{size:8},{size:4},void 0,void 0,{size:16}];this.name="ide"+e;this.device_control=2;a.io.register_read(this.ata_port|7,this,function(){D("lower irq",32768);this.cpu.device_lower_irq(this.irq);return this.read_status()});a.io.register_read(this.ata_port_high|2,this,this.read_status);a.io.register_write(this.ata_port_high|2,this,this.write_control);a.io.register_read(this.ata_port|0,this,function(){return this.current_interface.read_data(1)},
270
- function(){return this.current_interface.read_data(2)},function(){return this.current_interface.read_data(4)});a.io.register_read(this.ata_port|1,this,function(){D("Read error: "+z(this.current_interface.error&255)+" slave="+(this.current_interface===this.slave),32768);return this.current_interface.error&255});a.io.register_read(this.ata_port|2,this,function(){D("Read bytecount: "+z(this.current_interface.bytecount&255),32768);return this.current_interface.bytecount&255});a.io.register_read(this.ata_port|
271
- 3,this,function(){D("Read sector: "+z(this.current_interface.sector&255),32768);return this.current_interface.sector&255});a.io.register_read(this.ata_port|4,this,function(){D("Read 1F4: "+z(this.current_interface.cylinder_low&255),32768);return this.current_interface.cylinder_low&255});a.io.register_read(this.ata_port|5,this,function(){D("Read 1F5: "+z(this.current_interface.cylinder_high&255),32768);return this.current_interface.cylinder_high&255});a.io.register_read(this.ata_port|6,this,function(){D("Read 1F6",
272
- 32768);return this.current_interface.drive_head&255});a.io.register_write(this.ata_port|0,this,function(h){this.current_interface.write_data_port8(h)},function(h){this.current_interface.write_data_port16(h)},function(h){this.current_interface.write_data_port32(h)});a.io.register_write(this.ata_port|1,this,function(h){D("1F1/lba_count: "+z(h),32768);this.master.lba_count=(this.master.lba_count<<8|h)&65535;this.slave.lba_count=(this.slave.lba_count<<8|h)&65535});a.io.register_write(this.ata_port|2,
273
- this,function(h){D("1F2/bytecount: "+z(h),32768);this.master.bytecount=(this.master.bytecount<<8|h)&65535;this.slave.bytecount=(this.slave.bytecount<<8|h)&65535});a.io.register_write(this.ata_port|3,this,function(h){D("1F3/sector: "+z(h),32768);this.master.sector=(this.master.sector<<8|h)&65535;this.slave.sector=(this.slave.sector<<8|h)&65535});a.io.register_write(this.ata_port|4,this,function(h){D("1F4/sector low: "+z(h),32768);this.master.cylinder_low=(this.master.cylinder_low<<8|h)&65535;this.slave.cylinder_low=
274
- (this.slave.cylinder_low<<8|h)&65535});a.io.register_write(this.ata_port|5,this,function(h){D("1F5/sector high: "+z(h),32768);this.master.cylinder_high=(this.master.cylinder_high<<8|h)&65535;this.slave.cylinder_high=(this.slave.cylinder_high<<8|h)&65535});a.io.register_write(this.ata_port|6,this,function(h){var l=h&16;D("1F6/drive: "+z(h,2),32768);l?(D("Slave",32768),this.current_interface=this.slave):this.current_interface=this.master;this.master.drive_head=h;this.slave.drive_head=h;this.master.is_lba=
275
- this.slave.is_lba=h>>6&1;this.master.head=this.slave.head=h&15});this.dma_command=this.dma_status=this.prdt_addr=0;a.io.register_write(this.ata_port|7,this,function(h){D("lower irq",32768);this.cpu.device_lower_irq(this.irq);this.current_interface.ata_command(h)});a.io.register_read(this.master_port|4,this,void 0,void 0,this.dma_read_addr);a.io.register_write(this.master_port|4,this,void 0,void 0,this.dma_set_addr);a.io.register_read(this.master_port,this,this.dma_read_command8,void 0,this.dma_read_command);
276
- a.io.register_write(this.master_port,this,this.dma_write_command8,void 0,this.dma_write_command);a.io.register_read(this.master_port|2,this,this.dma_read_status);a.io.register_write(this.master_port|2,this,this.dma_write_status);a.io.register_read(this.master_port|8,this,function(){D("DMA read 0x8",32768);return 0});a.io.register_read(this.master_port|10,this,function(){D("DMA read 0xA",32768);return 0});a.devices.pci.register_device(this);Object.seal(this)}Sb.prototype.read_status=function(){if(this.current_interface.buffer){var a=
277
- this.current_interface.status;D("ATA read status: "+z(a,2),32768);return a}return 0};Sb.prototype.write_control=function(a){D("set device control: "+z(a,2)+" interrupts "+(a&2?"disabled":"enabled"),32768);a&4&&(D("Reset via control port",32768),this.cpu.device_lower_irq(this.irq),this.master.device_reset(),this.slave.device_reset());this.device_control=a};Sb.prototype.dma_read_addr=function(){D("dma get address: "+z(this.prdt_addr,8),32768);return this.prdt_addr};Sb.prototype.dma_set_addr=function(a){D("dma set address: "+
278
- z(a,8),32768);this.prdt_addr=a};Sb.prototype.dma_read_status=function(){D("DMA read status: "+z(this.dma_status),32768);return this.dma_status};Sb.prototype.dma_write_status=function(a){D("DMA set status: "+z(a),32768);this.dma_status&=~(a&6)};Sb.prototype.dma_read_command=function(){return this.dma_read_command8()|this.dma_read_status()<<16};Sb.prototype.dma_read_command8=function(){D("DMA read command: "+z(this.dma_command),32768);return this.dma_command};Sb.prototype.dma_write_command=function(a){D("DMA write command: "+
279
- z(a),32768);this.dma_write_command8(a&255);this.dma_write_status(a>>16&255)};Sb.prototype.dma_write_command8=function(a){D("DMA write command8: "+z(a),32768);const b=this.dma_command;this.dma_command=a&9;if((b&1)!==(a&1))if(0===(a&1))this.dma_status&=-2;else switch(this.dma_status|=1,this.current_interface.current_command){case 37:case 200:this.current_interface.do_ata_read_sectors_dma();break;case 202:case 53:this.current_interface.do_ata_write_sectors_dma();break;case 160:this.current_interface.do_atapi_dma();
280
- break;default:D("Spurious dma command write, current command: "+z(this.current_interface.current_command),32768),m(!1)}};Sb.prototype.push_irq=function(){0===(this.device_control&2)&&(D("push irq",32768),this.dma_status|=4,this.cpu.device_raise_irq(this.irq))};Sb.prototype.get_state=function(){var a=[];a[0]=this.master;a[1]=this.slave;a[2]=this.ata_port;a[3]=this.irq;a[4]=this.pci_id;a[5]=this.ata_port_high;a[6]=this.master_port;a[7]=this.name;a[8]=this.device_control;a[9]=this.prdt_addr;a[10]=this.dma_status;
281
- a[11]=this.current_interface===this.master;a[12]=this.dma_command;return a};Sb.prototype.set_state=function(a){this.master.set_state(a[0]);this.slave.set_state(a[1]);this.ata_port=a[2];this.irq=a[3];this.pci_id=a[4];this.ata_port_high=a[5];this.master_port=a[6];this.name=a[7];this.device_control=a[8];this.prdt_addr=a[9];this.dma_status=a[10];this.current_interface=a[11]?this.master:this.slave;this.dma_command=a[12]};function R(a,b,c,d,e,f,h){this.device=a;this.bus=h;this.nr=e;this.cpu=b;this.buffer=
282
- c;this.sector_size=d?2048:512;this.is_atapi=d;this.cylinder_count=this.sectors_per_track=this.head_count=this.sector_count=0;this.buffer&&(this.sector_count=this.buffer.byteLength/this.sector_size,this.sector_count!==(this.sector_count|0)&&(D("Warning: Disk size not aligned with sector size",32768),this.sector_count=Math.ceil(this.sector_count)),d?(this.head_count=1,this.sectors_per_track=0):(this.head_count=16,this.sectors_per_track=63),this.cylinder_count=this.sector_count/this.head_count/this.sectors_per_track,
283
- this.cylinder_count!==(this.cylinder_count|0)&&(D("Warning: Rounding up cylinder count. Choose different head number",32768),this.cylinder_count=Math.floor(this.cylinder_count)),a=b.devices.rtc,a.cmos_write(57,a.cmos_read(57)|1<<4*this.nr),a.cmos_write(18,a.cmos_read(18)&15|240),a.cmos_write(27,this.cylinder_count&255),a.cmos_write(28,this.cylinder_count>>8&255),a.cmos_write(29,this.head_count&255),a.cmos_write(30,255),a.cmos_write(31,255),a.cmos_write(32,200),a.cmos_write(33,this.cylinder_count&
1
+ ;let module = {exports:{}}; const aa=[[1,""],[2,"CPU"],[32768,"DISK"],[4,"FPU"],[8,"MEM"],[16,"DMA"],[32,"IO"],[64,"PS2"],[128,"PIC"],[256,"VGA"],[512,"PIT"],[1024,"MOUS"],[2048,"PCI"],[4096,"BIOS"],[8192,"FLOP"],[16384,"SERI"],[65536,"RTC"],[262144,"ACPI"],[524288,"APIC"],[1048576,"NET"],[2097152,"VIO"],[4194304,"9P"],[8388608,"SB16"],[16777216,"FETC"]];function ba(a,b){return(a||0===a?a+"":"").padEnd(b," ")}function ca(a,b){return(a||0===a?a+"":"").padStart(b,"0")}function g(a,b,c,d){k(0<=c);return new Proxy({},{get:function(e,
2
+ f){e=new a(b.buffer,c,d);const h=e[f];if("function"===typeof h)return h.bind(e);k(/^\d+$/.test(f)||"buffer"===f||"length"===f||"BYTES_PER_ELEMENT"===f||"byteOffset"===f);return h},set:function(e,f,h){k(/^\d+$/.test(f));(new a(b.buffer,c,d))[f]=h;return!0}})}function m(a,b){a=a?a.toString(16):"";return"0x"+ca(a.toUpperCase(),b||1)}var da;if("undefined"!==typeof crypto&&crypto.getRandomValues){const a=new Int32Array(1);da=function(){crypto.getRandomValues(a);return a[0]}}else if("undefined"!==typeof require){const a=
3
+ require("crypto");da=function(){return a.randomBytes(4).readInt32LE(0)}}else"undefined"!==typeof process?import("node:crypto").then(a=>{da=function(){return a.randomBytes(4).readInt32LE(0)}}):k(!1,"Unsupported platform: No cryptographic random values");var ea;if("function"===typeof Math.clz32)ea=function(a){k(0<a);return 31-Math.clz32(a)};else{for(var fa=new Int8Array(256),ha=0,ja=-2;256>ha;ha++)ha&ha-1||ja++,fa[ha]=ja;ea=function(a){a>>>=0;k(0<a);var b=a>>>16;if(b){var c=b>>>8;return c?24+fa[c]:
4
+ 16+fa[b]}return(c=a>>>8)?8+fa[c]:fa[a]}}function ka(a){k(0<=a);return 1>=a?1:1<<1+ea(a-1)}k(0===ea(1));k(1===ea(2));k(2===ea(7));k(3===ea(8));k(26===ea(123456789));k(1===ka(0));k(1===ka(1));k(2===ka(2));k(8===ka(7));k(8===ka(8));k(134217728===ka(123456789));function ma(a){var b=new Uint8Array(a),c,d;k(0===(a&a-1));this.length=0;this.push=function(e){this.length!==a&&this.length++;b[d]=e;d=d+1&a-1};this.shift=function(){if(this.length){var e=b[c];c=c+1&a-1;this.length--;return e}return-1};this.peek=
5
+ function(){return this.length?b[c]:-1};this.clear=function(){this.length=d=c=0};this.clear()}function na(a){this.size=a;this.data=new Float32Array(a);this.length=this.end=this.start=0;k(0===(a&a-1))}na.prototype.push=function(a){this.length===this.size?this.start=this.start+1&this.size-1:this.length++;this.data[this.end]=a;this.end=this.end+1&this.size-1};na.prototype.shift=function(){if(this.length){var a=this.data[this.start];this.start=this.start+1&this.size-1;this.length--;return a}};na.prototype.shift_block=
6
+ function(a){var b=new Float32Array(a);a>this.length&&(a=this.length);var c=this.start+a,d=this.data.subarray(this.start,c);b.set(d);c>=this.size&&(c-=this.size,b.set(this.data.subarray(0,c),d.length));this.start=c;this.length-=a;return b};na.prototype.peek=function(){if(this.length)return this.data[this.start]};na.prototype.clear=function(){this.length=this.end=this.start=0};function qa(a,b){Array.isArray(a)||(a=[a]);var c=new Blob(a);a=document.createElement("a");a.download=b;a.href=window.URL.createObjectURL(c);
7
+ a.dataset.downloadurl=["application/octet-stream",a.download,a.href].join(":");document.createEvent?(b=document.createEvent("MouseEvent"),b.initMouseEvent("click",!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null),a.dispatchEvent(b)):a.click();window.URL.revokeObjectURL(a.href)}function ra(a){"number"===typeof a?this.view=new Uint8Array(a+7>>3):a instanceof ArrayBuffer?this.view=new Uint8Array(a):k(!1,"Bitmap: Invalid argument")}ra.prototype.set=function(a,b){const c=a>>3;a=1<<(a&7);this.view[c]=b?this.view[c]|
8
+ a:this.view[c]&~a};ra.prototype.get=function(a){return this.view[a>>3]>>(a&7)&1};ra.prototype.get_buffer=function(){return this.view.buffer};var sa,ua;if("undefined"===typeof XMLHttpRequest||"undefined"!==typeof process&&process.versions&&process.versions.node){let a;sa=async function(b,c){a||(a=await import("node:fs/promises"));if(c.range){k(!c.as_json);b=await a.open(b,"r");const e=c.range.length,f=Buffer.allocUnsafe(e);try{var d=await b.read({buffer:f,position:c.range.start});k(d.bytesRead===e)}finally{await b.close()}c.done&&
9
+ c.done(new Uint8Array(f))}else d=await a.readFile(b,{encoding:c.as_json?"utf-8":null}),d=c.as_json?JSON.parse(d):(new Uint8Array(d)).buffer,c.done(d)};ua=async function(b){a||(a=await import("node:fs/promises"));return(await a.stat(b)).size}}else sa=async function(a,b,c){function d(){const p=c||0;setTimeout(()=>{sa(a,b,p+1)},1E3*([1,1,2,3,5,8,13,21][p]||34))}var e=new XMLHttpRequest;e.open(b.method||"get",a,!0);e.responseType=b.as_json?"json":"arraybuffer";if(b.headers)for(var f=Object.keys(b.headers),
10
+ h=0;h<f.length;h++){var l=f[h];e.setRequestHeader(l,b.headers[l])}b.range&&(f=b.range.start,e.setRequestHeader("Range","bytes="+f+"-"+(f+b.range.length-1)),e.setRequestHeader("X-Accept-Encoding","identity"),e.onreadystatechange=function(){200===e.status&&(console.error("Server sent full file in response to ranged request, aborting",{filename:a}),e.abort())});e.onload=function(){if(4===e.readyState)if(200!==e.status&&206!==e.status)console.error("Loading the image "+a+" failed (status %d)",e.status),
11
+ 500<=e.status&&600>e.status&&d();else if(e.response){if(b.range){const p=e.getResponseHeader("Content-Encoding");p&&"identity"!==p&&console.error("Server sent Content-Encoding in response to ranged request",{filename:a,enc:p})}b.done&&b.done(e.response,e)}};e.onerror=function(p){console.error("Loading the image "+a+" failed",p);d()};b.progress&&(e.onprogress=function(p){b.progress(p)});e.send(null)},ua=async function(a){return new Promise((b,c)=>{sa(a,{done:(d,e)=>{d=e.getResponseHeader("Content-Range")||
12
+ "";(e=d.match(/\/(\d+)\s*$/))?b(+e[1]):c(Error("`Range: bytes=...` header not supported (Got `"+d+"`)"))},headers:{Range:"bytes=0-0","X-Accept-Encoding":"identity"}})})};var va=-15786961;const C=function(){const a=aa.reduce(function(d,e){d[e[0]]=e[1];return d},{});var b="",c=0;return function(d,e){e=e||1;if(e&va){d="["+ba(a[e]||"",4)+"] "+d;if(d===b&&(c++,2048>c))return;e=new Date;e=ca(e.getHours(),2)+":"+ca(e.getMinutes(),2)+":"+ca(e.getSeconds(),2)+"+"+ca(e.getMilliseconds(),3)+" ";c&&(1===c?console.log(e+
13
+ b):console.log("Previous message repeated "+c+" times"),c=0);console.log(e+d);b=d}}}();function k(a,b){if(!a){debugger;console.trace();if(b)throw"Assert failed: "+b;throw"Assert failed";}}function wa(a,b){function c(z){z=z.toString(16);return"#"+"0".repeat(6-z.length)+z}function d(z){var x=256*oa,G=8*S,L=Va?Va.canvas:null;L&&L.width===x&&L.height===G||(L?(L.width=x,L.height=G):(L=new OffscreenCanvas(x,G),Va=L.getContext("2d")),Eb=Va.createImageData(x,G));const R=Eb.data;let Q=0,T;G=Fb?function(Z){T=
14
+ T||Z;R[Q+3]=Z;R[Q+7]=Z;Q+=8}:function(Z){T=T||Z;R[Q+3]=Z;Q+=4};L=32-S;const la=x*(S-1)*4;x=4*(oa-x*S);const ia=1020*oa;for(let Z=0,ta=0;2048>Z;++Z,ta+=L,Q+=x){const Fa=Z%256;Z&&!Fa&&(Q+=la);T=!1;for(let Ma=0;Ma<S;++Ma,++ta,Q+=ia){const Na=z[ta];for(let xa=128;0<xa;xa>>=1)G(Na&xa?255:0);Gb&&G(Hb&&192<=Fa&&223>=Fa&&Na&1?255:0)}Zb[Z]=T?1:0}Va.putImageData(Eb,0,0)}function e(z,x,G,L){if(x&&G){z.style.width="";z.style.height="";L&&(z.style.transform="");var R=z.getBoundingClientRect();L?z.style.transform=
15
+ (1===x?"":" scaleX("+x+")")+(1===G?"":" scaleY("+G+")"):(0===x%1&&0===G%1?(h.style.imageRendering="crisp-edges",h.style.imageRendering="pixelated",h.style["-ms-interpolation-mode"]="nearest-neighbor"):(h.style.imageRendering="",h.style["-ms-interpolation-mode"]=""),L=window.devicePixelRatio||1,0!==L%1&&(x/=L,G/=L));1!==x&&(z.style.width=R.width*x+"px");1!==G&&(z.style.height=R.height*G+"px")}}const f=a.container;this.screen_fill_buffer=b;console.assert(f,"options.container must be provided");this.FLAG_BLINKING=
16
+ 1;this.FLAG_FONT_PAGE_B=2;var h=f.getElementsByTagName("canvas")[0],l=h.getContext("2d",{alpha:!1}),p=f.getElementsByTagName("div")[0],n=document.createElement("div"),q,r,t=void 0!==a.scale?a.scale:1,u=void 0!==a.scale?a.scale:1,y=1,A,v,B,w,F,J,pa,Va,Eb,Zb=new Int8Array(2048),S,oa,Gb,Fb,Hb,Ib=0,Jb=0,sb,$b=0,tb,Kb,ub,Lb=[],ib=Lb,vb=0,Mb=!1;this.init=function(){const z=new Uint16Array([32,9786,9787,9829,9830,9827,9824,8226,9688,9675,9689,9794,9792,9834,9835,9788,9658,9668,8597,8252,182,167,9644,8616,
17
+ 8593,8595,8594,8592,8735,8596,9650,9660]),x=new Uint16Array([8962,199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,201,230,198,244,246,242,251,249,255,214,220,162,163,165,8359,402,225,237,243,250,241,209,170,186,191,8976,172,189,188,161,171,187,9617,9618,9619,9474,9508,9569,9570,9558,9557,9571,9553,9559,9565,9564,9563,9488,9492,9524,9516,9500,9472,9532,9566,9567,9562,9556,9577,9574,9568,9552,9580,9575,9576,9572,9573,9561,9560,9554,9555,9579,9578,9496,9484,9608,9604,9612,9616,9600,945,
18
+ 223,915,960,931,963,181,964,934,920,937,948,8734,966,949,8745,8801,177,8805,8804,8992,8993,247,8776,176,8729,183,8730,8319,178,9632,160]);for(var G=0,L;256>G;G++)L=126<G?x[G-127]:32>G?z[G]:G,Lb.push(String.fromCharCode(L));n.classList.add("cursor");n.style.position="absolute";n.style.backgroundColor="#ccc";n.style.width="7px";n.style.display="inline-block";this.set_mode(!1);this.set_size_text(80,25);2===v&&this.set_size_graphical(720,400,720,400);this.set_scale(t,u);this.timer()};this.make_screenshot=
19
+ function(){const z=new Image;if(1===v||2===v)z.src=h.toDataURL("image/png");else{const x=[9,16],G=document.createElement("canvas");G.width=w*x[0];G.height=F*x[1];const L=G.getContext("2d");L.imageSmoothingEnabled=!1;L.font=window.getComputedStyle(p).font;L.textBaseline="top";for(let R=0;R<F;R++)for(let Q=0;Q<w;Q++){const T=4*(R*w+Q),la=B[T+0],ia=B[T+3];L.fillStyle=c(B[T+2]);L.fillRect(Q*x[0],R*x[1],x[0],x[1]);L.fillStyle=c(ia);L.fillText(ib[la],Q*x[0],R*x[1])}"none"!==n.style.display&&q<F&&r<w&&(L.fillStyle=
20
+ n.style.backgroundColor,L.fillRect(r*x[0],q*x[1]+parseInt(n.style.marginTop,10),parseInt(n.style.width,10),parseInt(n.style.height,10)));z.src=G.toDataURL("image/png")}return z};this.put_char=function(z,x,G,L,R,Q){k(0<=z&&z<F);k(0<=x&&x<w);k(0<=G&&256>G);x=4*(z*w+x);B[x+0]=G;B[x+1]=L;B[x+2]=R;B[x+3]=Q;A[z]=1};this.timer=function(){vb=requestAnimationFrame(()=>this.update_screen())};this.update_screen=function(){Mb||(0===v?this.update_text():1===v?this.update_graphical():this.update_graphical_text());
21
+ this.timer()};this.update_text=function(){for(var z=0;z<F;z++)A[z]&&(this.text_update_row(z),A[z]=0)};this.update_graphical=function(){this.screen_fill_buffer()};this.update_graphical_text=function(){if(J){var z=performance.now();if(266<z-$b){sb=!sb;ub&&(A[q]=1);var x=4*w;for(let la=0,ia=0;la<F;++la)if(A[la])ia+=x;else for(var G=0;G<w;++G,ia+=4)if(B[ia+1]&1){A[la]=1;ia+=x-4*G;break}$b=z}z=Va.canvas;x=pa.canvas;G=4*w;const R=w*oa,Q=S;let T=0;for(let la=0,ia=0,Z=0;la<F;++la,ia+=S){if(!A[la]){Z+=G;continue}++T;
22
+ pa.clearRect(0,Q,R,S);let ta,Fa,Ma,Na;for(let xa=0;xa<R;xa+=oa,Z+=4){const ac=B[Z+0];var L=B[Z+1];const bc=B[Z+2],cc=B[Z+3],dc=L&2?Jb:Ib;L=(!(L&1)||sb)&&Zb[(dc<<8)+ac];Ma!==bc&&(void 0!==Ma&&(J.fillStyle=c(Ma),J.fillRect(Na,ia,xa-Na,S)),Ma=bc,Na=xa);ta!==cc&&(void 0!==ta&&(pa.fillStyle=c(ta),pa.fillRect(Fa,0,xa-Fa,S)),ta=cc,Fa=xa);L&&pa.drawImage(z,ac*oa,dc*S,oa,S,xa,Q,oa,S)}pa.fillStyle=c(ta);pa.fillRect(Fa,0,R-Fa,S);pa.globalCompositeOperation="destination-in";pa.drawImage(x,0,Q,R,S,0,0,R,S);pa.globalCompositeOperation=
23
+ "source-over";J.fillStyle=c(Ma);J.fillRect(Na,ia,R-Na,S);J.drawImage(x,0,0,R,S,0,ia,R,S)}T&&(sb&&ub&&A[q]&&(J.fillStyle=c(B[4*(q*w+r)+3]),J.fillRect(r*oa,q*S+tb,oa,Kb-tb+1)),A.fill(0));T&&l.drawImage(J.canvas,0,0)}};this.destroy=function(){vb&&(cancelAnimationFrame(vb),vb=0)};this.pause=function(){Mb=!0;n.classList.remove("blinking-cursor")};this.continue=function(){Mb=!1;n.classList.add("blinking-cursor")};this.set_mode=function(z){v=z?1:a.use_graphical_text?2:0;0===v?(p.style.display="block",h.style.display=
24
+ "none"):(p.style.display="none",h.style.display="block",2===v&&A&&A.fill(1))};this.set_font_bitmap=function(z,x,G,L,R,Q){const T=G?16:x?9:8;if(S!==z||oa!==T||Gb!==x||Fb!==G||Hb!==L||Q)Q=oa!==T||S!==z,S=z,oa=T,Gb=x,Fb=G,Hb=L,2===v&&(d(R),A.fill(1),Q&&this.set_size_graphical_text())};this.set_font_page=function(z,x){if(Ib!==z||Jb!==x)Ib=z,Jb=x,A.fill(1)};this.clear_screen=function(){l.fillStyle="#000";l.fillRect(0,0,h.width,h.height)};this.set_size_graphical_text=function(){if(Va){var z=oa*w,x=S*F,
25
+ G=2*S;J&&J.canvas.width===z&&J.canvas.height===x&&pa.canvas.height===G||(J?(J.canvas.width=z,J.canvas.height=x,pa.canvas.width=z,pa.canvas.height=G):(J=(new OffscreenCanvas(z,x)).getContext("2d",{alpha:!1}),pa=(new OffscreenCanvas(z,G)).getContext("2d")),this.set_size_graphical(z,x,z,x),A.fill(1))}};this.set_size_text=function(z,x){if(z!==w||x!==F)if(A=new Int8Array(x),B=new Int32Array(z*x*4),w=z,F=x,0===v){for(;p.childNodes.length>x;)p.removeChild(p.firstChild);for(;p.childNodes.length<x;)p.appendChild(document.createElement("div"));
26
+ for(z=0;z<x;z++)this.text_update_row(z);e(p,t,u,!0)}else 2===v&&this.set_size_graphical_text()};this.set_size_graphical=function(z,x){h.style.display="block";h.width=z;h.height=x;l.imageSmoothingEnabled=!1;y=640>=z&&2*z<window.innerWidth*window.devicePixelRatio&&2*x<window.innerHeight*window.devicePixelRatio?2:1;e(h,t*y,u*y,!1)};this.set_charmap=function(z){ib=z||Lb};this.set_scale=function(z,x){t=z;u=x;e(p,t,u,!0);e(h,t*y,u*y,!1)};this.update_cursor_scanline=function(z,x,G){if(z!==tb||x!==Kb||G!==
27
+ ub)0===v?G?(n.style.display="inline",n.style.height=x-z+"px",n.style.marginTop=z+"px"):n.style.display="none":2===v&&q<F&&(A[q]=1),tb=z,Kb=x,ub=G};this.update_cursor=function(z,x){if(z!==q||x!==r)z<F&&(A[z]=1),q<F&&(A[q]=1),q=z,r=x};this.text_update_row=function(z){var x=4*z*w,G;var L=p.childNodes[z];var R=document.createElement("div");for(var Q=0;Q<w;){var T=document.createElement("span");var la=B[x+1]&1;var ia=B[x+2];var Z=B[x+3];la&&T.classList.add("blink");T.style.backgroundColor=c(ia);T.style.color=
28
+ c(Z);for(G="";Q<w&&(B[x+1]&1)===la&&B[x+2]===ia&&B[x+3]===Z;){var ta=B[x+0];G+=ib[ta];k(ib[ta]);Q++;x+=4;if(z===q)if(Q===r)break;else if(Q===r+1){n.style.backgroundColor=T.style.color;R.appendChild(n);break}}T.textContent=G;R.appendChild(T)}L.parentNode.replaceChild(R,L)};this.update_buffer=function(z){for(const x of z)l.putImageData(x.image_data,x.screen_x-x.buffer_x,x.screen_y-x.buffer_y,x.buffer_x,x.buffer_y,x.buffer_width,x.buffer_height)};this.get_text_screen=function(){for(var z=[],x=0;x<F;x++)z.push(this.get_text_row(x));
29
+ return z};this.get_text_row=function(z){let x="";for(let G=0;G<w;G++)x+=ib[B[4*(z*w+G)]];return x};this.init()}function ya(a){k(a instanceof ArrayBuffer);this.buffer=a;this.byteLength=a.byteLength;this.onprogress=this.onload=void 0}ya.prototype.load=function(){this.onload&&this.onload({buffer:this.buffer})};ya.prototype.get=function(a,b,c){k(a+b<=this.byteLength);c(new Uint8Array(this.buffer,a,b))};ya.prototype.set=function(a,b,c){k(a+b.byteLength<=this.byteLength);(new Uint8Array(this.buffer,a,b.byteLength)).set(b);
30
+ c()};ya.prototype.get_buffer=function(a){a(this.buffer)};ya.prototype.get_state=function(){const a=[];a[0]=this.byteLength;a[1]=new Uint8Array(this.buffer);return a};ya.prototype.set_state=function(a){this.byteLength=a[0];this.buffer=a[1].slice().buffer};function za(a,b,c){this.filename=a;this.byteLength=b;this.block_cache=new Map;this.block_cache_is_write=new Set;this.fixed_chunk_size=c;this.cache_reads=!!c;this.onprogress=this.onload=void 0}za.prototype.load=async function(){void 0===this.byteLength&&
31
+ (this.byteLength=await ua(this.filename));this.onload&&this.onload(Object.create(null))};za.prototype.get_from_cache=function(a,b){var c=b/256;a/=256;for(var d=0;d<c;d++)if(!this.block_cache.get(a+d))return;if(1===c)return this.block_cache.get(a);b=new Uint8Array(b);for(d=0;d<c;d++)b.set(this.block_cache.get(a+d),256*d);return b};za.prototype.get=function(a,b,c){k(a+b<=this.byteLength);k(0===a%256);k(0===b%256);k(b);var d=this.get_from_cache(a,b);if(d)c(d);else{var e=a,f=b;this.fixed_chunk_size&&
32
+ (e=a-a%this.fixed_chunk_size,f=Math.ceil((a-e+b)/this.fixed_chunk_size)*this.fixed_chunk_size);sa(this.filename,{done:function(h){h=new Uint8Array(h);this.handle_read(e,f,h);e===a&&f===b?c(h):c(h.subarray(a-e,a-e+b))}.bind(this),range:{start:e,length:f}})}};za.prototype.set=function(a,b,c){var d=b.length;k(a+b.byteLength<=this.byteLength);k(0===a%256);k(0===d%256);k(d);a/=256;d/=256;for(var e=0;e<d;e++){var f=this.block_cache.get(a+e);if(void 0===f)f=b.slice(256*e,256*(e+1)),this.block_cache.set(a+
33
+ e,f);else{const h=b.subarray(256*e,256*(e+1));k(f.byteLength===h.length);f.set(h)}this.block_cache_is_write.add(a+e)}c()};za.prototype.handle_read=function(a,b,c){a/=256;b/=256;for(var d=0;d<b;d++){const e=this.block_cache.get(a+d);e?c.set(e,256*d):this.cache_reads&&this.block_cache.set(a+d,c.slice(256*d,256*(d+1)))}};za.prototype.get_buffer=function(a){a()};za.prototype.get_state=function(){const a=[],b=[];for(const [c,d]of this.block_cache)k(isFinite(c)),this.block_cache_is_write.has(c)&&b.push([c,
34
+ d]);a[0]=b;return a};za.prototype.set_state=function(a){a=a[0];this.block_cache.clear();this.block_cache_is_write.clear();for(const [b,c]of a)k(isFinite(b)),this.block_cache.set(b,c),this.block_cache_is_write.add(b)};function Aa(a,b,c,d,e){const f=a.match(/\.[^\.]+(\.zst)?$/);this.extension=f?f[0]:"";this.basename=a.substring(0,a.length-this.extension.length);this.is_zstd=this.extension.endsWith(".zst");this.basename.endsWith("/")||(this.basename+="-");this.block_cache=new Map;this.block_cache_is_write=
35
+ new Set;this.byteLength=b;this.fixed_chunk_size=c;this.partfile_alt_format=!!d;this.zstd_decompress=e;this.cache_reads=!!c;this.onprogress=this.onload=void 0}Aa.prototype.load=function(){void 0===this.byteLength&&k(!1);this.onload&&this.onload(Object.create(null))};Aa.prototype.get=function(a,b,c){k(a+b<=this.byteLength);k(0===a%256);k(0===b%256);k(b);var d=this.get_from_cache(a,b);if(d)c(d);else if(this.fixed_chunk_size){const f=Math.floor(a/this.fixed_chunk_size),h=a-f*this.fixed_chunk_size;k(0<=
36
+ h);const l=Math.ceil((h+b)/this.fixed_chunk_size),p=new Uint8Array(l*this.fixed_chunk_size);let n=0;for(let q=0;q<l;q++){var e=(f+q)*this.fixed_chunk_size;d=this.partfile_alt_format?this.basename+(f+q+"").padStart(8,"0")+this.extension:this.basename+e+"-"+(e+this.fixed_chunk_size)+this.extension;(e=this.get_from_cache(e,this.fixed_chunk_size))?(p.set(e,q*this.fixed_chunk_size),n++,n===l&&c(p.subarray(h,h+b))):sa(d,{done:async function(r){r=new Uint8Array(r);this.is_zstd&&(r=await this.zstd_decompress(this.fixed_chunk_size,
37
+ r),r=new Uint8Array(r));p.set(r,q*this.fixed_chunk_size);this.handle_read((f+q)*this.fixed_chunk_size,this.fixed_chunk_size|0,r);n++;n===l&&c(p.subarray(h,h+b))}.bind(this)})}}else sa(this.basename+a+"-"+(a+b)+this.extension,{done:function(f){k(f.byteLength===b);f=new Uint8Array(f);this.handle_read(a,b,f);c(f)}.bind(this)})};Aa.prototype.get_from_cache=za.prototype.get_from_cache;Aa.prototype.set=za.prototype.set;Aa.prototype.handle_read=za.prototype.handle_read;Aa.prototype.get_state=za.prototype.get_state;
38
+ Aa.prototype.set_state=za.prototype.set_state;function Ba(a){this.file=a;this.byteLength=a.size;1073741824<a.size&&console.warn("SyncFileBuffer: Allocating buffer of "+(a.size>>20)+" MB ...");this.buffer=new ArrayBuffer(a.size);this.onprogress=this.onload=void 0}Ba.prototype.load=function(){this.load_next(0)};Ba.prototype.load_next=function(a){var b=new FileReader;b.onload=function(d){d=new Uint8Array(d.target.result);(new Uint8Array(this.buffer,a)).set(d);this.load_next(a+4194304)}.bind(this);if(this.onprogress)this.onprogress({loaded:a,
39
+ total:this.byteLength,lengthComputable:!0});if(a<this.byteLength){var c=this.file.slice(a,Math.min(a+4194304,this.byteLength));b.readAsArrayBuffer(c)}else this.file=void 0,this.onload&&this.onload({buffer:this.buffer})};Ba.prototype.get=ya.prototype.get;Ba.prototype.set=ya.prototype.set;Ba.prototype.get_buffer=ya.prototype.get_buffer;Ba.prototype.get_state=ya.prototype.get_state;Ba.prototype.set_state=ya.prototype.set_state;function Ca(a){this.file=a;this.byteLength=a.size;this.block_cache=new Map;
40
+ this.block_cache_is_write=new Set;this.onprogress=this.onload=void 0}Ca.prototype.load=function(){this.onload&&this.onload(Object.create(null))};Ca.prototype.get=function(a,b,c){k(0===a%256);k(0===b%256);k(b);var d=this.get_from_cache(a,b);d?c(d):(d=new FileReader,d.onload=function(e){e=new Uint8Array(e.target.result);this.handle_read(a,b,e);c(e)}.bind(this),d.readAsArrayBuffer(this.file.slice(a,a+b)))};Ca.prototype.get_from_cache=za.prototype.get_from_cache;Ca.prototype.set=za.prototype.set;Ca.prototype.handle_read=
41
+ za.prototype.handle_read;Ca.prototype.get_state=za.prototype.get_state;Ca.prototype.set_state=za.prototype.set_state;Ca.prototype.get_buffer=function(a){a()};Ca.prototype.get_as_file=function(a){for(var b=[],c=Array.from(this.block_cache.keys()).sort(function(l,p){return l-p}),d=0,e=0;e<c.length;e++){var f=c[e],h=this.block_cache.get(f);f*=256;k(f>=d);f!==d&&(b.push(this.file.slice(d,f)),d=f);b.push(h);d+=h.length}d!==this.file.size&&b.push(this.file.slice(d));a=new File(b,a);k(a.size===this.file.size);
42
+ return a};function Da(a,b){if(a.buffer instanceof ArrayBuffer)return new ya(a.buffer);if("undefined"!==typeof File&&a.buffer instanceof File)return b=a.async,void 0===b&&(b=268435456<=a.buffer.size),b?new Ca(a.buffer):new Ba(a.buffer);if(a.url)return a.use_parts?new Aa(a.url,a.size,a.fixed_chunk_size,!1,b):new za(a.url,a.size,a.fixed_chunk_size);C("Ignored file: url="+a.url+" buffer="+a.buffer)}function D(a){this.cpu=a;this.channel_page=new Uint8Array(8);this.channel_pagehi=new Uint8Array(8);this.channel_addr=
43
+ new Uint16Array(8);this.channel_addr_init=new Uint16Array(8);this.channel_count=new Uint16Array(8);this.channel_count_init=new Uint16Array(8);this.channel_mask=new Uint8Array(8);this.channel_mode=new Uint8Array(8);this.unmask_listeners=[];this.lsb_msb_flipflop=0;a=a.io;a.register_write(0,this,this.port_addr_write.bind(this,0));a.register_write(2,this,this.port_addr_write.bind(this,1));a.register_write(4,this,this.port_addr_write.bind(this,2));a.register_write(6,this,this.port_addr_write.bind(this,
44
+ 3));a.register_write(1,this,this.port_count_write.bind(this,0));a.register_write(3,this,this.port_count_write.bind(this,1));a.register_write(5,this,this.port_count_write.bind(this,2));a.register_write(7,this,this.port_count_write.bind(this,3));a.register_read(0,this,this.port_addr_read.bind(this,0));a.register_read(2,this,this.port_addr_read.bind(this,1));a.register_read(4,this,this.port_addr_read.bind(this,2));a.register_read(6,this,this.port_addr_read.bind(this,3));a.register_read(1,this,this.port_count_read.bind(this,
45
+ 0));a.register_read(3,this,this.port_count_read.bind(this,1));a.register_read(5,this,this.port_count_read.bind(this,2));a.register_read(7,this,this.port_count_read.bind(this,3));a.register_write(192,this,this.port_addr_write.bind(this,4));a.register_write(196,this,this.port_addr_write.bind(this,5));a.register_write(200,this,this.port_addr_write.bind(this,6));a.register_write(204,this,this.port_addr_write.bind(this,7));a.register_write(194,this,this.port_count_write.bind(this,4));a.register_write(198,
46
+ this,this.port_count_write.bind(this,5));a.register_write(202,this,this.port_count_write.bind(this,6));a.register_write(206,this,this.port_count_write.bind(this,7));a.register_read(192,this,this.port_addr_read.bind(this,4));a.register_read(196,this,this.port_addr_read.bind(this,5));a.register_read(200,this,this.port_addr_read.bind(this,6));a.register_read(204,this,this.port_addr_read.bind(this,7));a.register_read(194,this,this.port_count_read.bind(this,4));a.register_read(198,this,this.port_count_read.bind(this,
47
+ 5));a.register_read(202,this,this.port_count_read.bind(this,6));a.register_read(206,this,this.port_count_read.bind(this,7));a.register_write(135,this,this.port_page_write.bind(this,0));a.register_write(131,this,this.port_page_write.bind(this,1));a.register_write(129,this,this.port_page_write.bind(this,2));a.register_write(130,this,this.port_page_write.bind(this,3));a.register_write(143,this,this.port_page_write.bind(this,4));a.register_write(139,this,this.port_page_write.bind(this,5));a.register_write(137,
48
+ this,this.port_page_write.bind(this,6));a.register_write(138,this,this.port_page_write.bind(this,7));a.register_read(135,this,this.port_page_read.bind(this,0));a.register_read(131,this,this.port_page_read.bind(this,1));a.register_read(129,this,this.port_page_read.bind(this,2));a.register_read(130,this,this.port_page_read.bind(this,3));a.register_read(143,this,this.port_page_read.bind(this,4));a.register_read(139,this,this.port_page_read.bind(this,5));a.register_read(137,this,this.port_page_read.bind(this,
49
+ 6));a.register_read(138,this,this.port_page_read.bind(this,7));a.register_write(1159,this,this.port_pagehi_write.bind(this,0));a.register_write(1155,this,this.port_pagehi_write.bind(this,1));a.register_write(1153,this,this.port_pagehi_write.bind(this,2));a.register_write(1154,this,this.port_pagehi_write.bind(this,3));a.register_write(1163,this,this.port_pagehi_write.bind(this,5));a.register_write(1161,this,this.port_pagehi_write.bind(this,6));a.register_write(1162,this,this.port_pagehi_write.bind(this,
50
+ 7));a.register_read(1159,this,this.port_pagehi_read.bind(this,0));a.register_read(1155,this,this.port_pagehi_read.bind(this,1));a.register_read(1153,this,this.port_pagehi_read.bind(this,2));a.register_read(1154,this,this.port_pagehi_read.bind(this,3));a.register_read(1163,this,this.port_pagehi_read.bind(this,5));a.register_read(1161,this,this.port_pagehi_read.bind(this,6));a.register_read(1162,this,this.port_pagehi_read.bind(this,7));a.register_write(10,this,this.port_singlemask_write.bind(this,0));
51
+ a.register_write(212,this,this.port_singlemask_write.bind(this,4));a.register_write(15,this,this.port_multimask_write.bind(this,0));a.register_write(222,this,this.port_multimask_write.bind(this,4));a.register_read(15,this,this.port_multimask_read.bind(this,0));a.register_read(222,this,this.port_multimask_read.bind(this,4));a.register_write(11,this,this.port_mode_write.bind(this,0));a.register_write(214,this,this.port_mode_write.bind(this,4));a.register_write(12,this,this.portC_write);a.register_write(216,
52
+ this,this.portC_write)}D.prototype.get_state=function(){return[this.channel_page,this.channel_pagehi,this.channel_addr,this.channel_addr_init,this.channel_count,this.channel_count_init,this.channel_mask,this.channel_mode,this.lsb_msb_flipflop]};D.prototype.set_state=function(a){this.channel_page=a[0];this.channel_pagehi=a[1];this.channel_addr=a[2];this.channel_addr_init=a[3];this.channel_count=a[4];this.channel_count_init=a[5];this.channel_mask=a[6];this.channel_mode=a[7];this.lsb_msb_flipflop=a[8]};
53
+ D.prototype.port_count_write=function(a,b){C("count write ["+a+"] = "+m(b),16);this.channel_count[a]=this.flipflop_get(this.channel_count[a],b,!1);this.channel_count_init[a]=this.flipflop_get(this.channel_count_init[a],b,!0)};D.prototype.port_count_read=function(a){C("count read ["+a+"] -> "+m(this.channel_count[a]),16);return this.flipflop_read(this.channel_count[a])};D.prototype.port_addr_write=function(a,b){C("addr write ["+a+"] = "+m(b),16);this.channel_addr[a]=this.flipflop_get(this.channel_addr[a],
54
+ b,!1);this.channel_addr_init[a]=this.flipflop_get(this.channel_addr_init[a],b,!0)};D.prototype.port_addr_read=function(a){C("addr read ["+a+"] -> "+m(this.channel_addr[a]),16);return this.flipflop_read(this.channel_addr[a])};D.prototype.port_pagehi_write=function(a,b){C("pagehi write ["+a+"] = "+m(b),16);this.channel_pagehi[a]=b};D.prototype.port_pagehi_read=function(a){C("pagehi read ["+a+"]",16);return this.channel_pagehi[a]};D.prototype.port_page_write=function(a,b){C("page write ["+a+"] = "+m(b),
55
+ 16);this.channel_page[a]=b};D.prototype.port_page_read=function(a){C("page read ["+a+"]",16);return this.channel_page[a]};D.prototype.port_singlemask_write=function(a,b){a=(b&3)+a;b=b&4?1:0;C("singlechannel mask write ["+a+"] = "+b,16);this.update_mask(a,b)};D.prototype.port_multimask_write=function(a,b){C("multichannel mask write: "+m(b),16);for(var c=0;4>c;c++)this.update_mask(a+c,b&1<<c)};D.prototype.port_multimask_read=function(a){var b=0|this.channel_mask[a+0];b|=this.channel_mask[a+1]<<1;b|=
56
+ this.channel_mask[a+2]<<2;b|=this.channel_mask[a+3]<<3;C("multichannel mask read: "+m(b),16);return b};D.prototype.port_mode_write=function(a,b){a=(b&3)+a;C("mode write ["+a+"] = "+m(b),16);this.channel_mode[a]=b};D.prototype.portC_write=function(){C("flipflop reset",16);this.lsb_msb_flipflop=0};D.prototype.on_unmask=function(a,b){this.unmask_listeners.push({fn:a,this_value:b})};D.prototype.update_mask=function(a,b){if(this.channel_mask[a]!==b&&(this.channel_mask[a]=b,!b))for(C("firing on_unmask("+
57
+ a+")",16),b=0;b<this.unmask_listeners.length;b++)this.unmask_listeners[b].fn.call(this.unmask_listeners[b].this_value,a)};D.prototype.do_read=function(a,b,c,d,e){var f=this.count_get_8bit(d),h=this.address_get_8bit(d);C("DMA write channel "+d,16);C("to "+m(h)+" len "+m(f),16);c<f&&C("DMA should read more than provided: "+m(c)+" "+m(f),16);if(b+f>a.byteLength)C("DMA read outside of buffer",16),e(!0);else{var l=this.cpu;this.channel_addr[d]+=f;a.get(b,f,function(p){l.write_blob(p,h);e(!1)})}};D.prototype.do_write=
58
+ function(a,b,c,d,e){var f=this.channel_count[d]+1&65535,h=5<=d?2:1,l=f*h,p=this.address_get_8bit(d),n=!1,q=!1,r=this.channel_mode[d]&16;C("DMA write channel "+d,16);C("to "+m(p)+" len "+m(l),16);c<l?(C("DMA should read more than provided",16),f=Math.floor(c/h),l=f*h,n=!0):c>l&&(C("DMA attempted to read more than provided",16),q=!0);b+l>a.byteLength?(C("DMA write outside of buffer",16),e(!0)):(this.channel_addr[d]+=f,this.channel_count[d]-=f,!n&&r&&(C("DMA autoinit",16),this.channel_addr[d]=this.channel_addr_init[d],
59
+ this.channel_count[d]=this.channel_count_init[d]),a.set(b,this.cpu.mem8.subarray(p,p+l),()=>{q&&r?(C("DMA continuing from start",16),this.do_write(a,b+l,c-l,d,e)):e(!1)}))};D.prototype.address_get_8bit=function(a){var b=this.channel_addr[a];5<=a&&(b<<=1);b=b&65535|this.channel_page[a]<<16;return b|=this.channel_pagehi[a]<<24};D.prototype.count_get_8bit=function(a){var b=this.channel_count[a]+1;5<=a&&(b*=2);return b};D.prototype.flipflop_get=function(a,b,c){c||(this.lsb_msb_flipflop^=1);return this.lsb_msb_flipflop?
60
+ a&-256|b:a&-65281|b<<8};D.prototype.flipflop_read=function(a){return(this.lsb_msb_flipflop^=1)?a&255:a>>8&255};function Ea(a){this.ports=[];this.cpu=a;for(var b=0;65536>b;b++)this.ports[b]=this.create_empty_entry();var c=a.memory_size[0];for(b=0;b<<17<c;b++)a.memory_map_read8[b]=a.memory_map_write8[b]=void 0,a.memory_map_read32[b]=a.memory_map_write32[b]=void 0;this.mmap_register(c,4294967296-c,function(d){C("Read from unmapped memory space, addr="+m(d>>>0,8),32);return 255},function(d,e){C("Write to unmapped memory space, addr="+
61
+ m(d>>>0,8)+" value="+m(e,2),32)},function(d){C("Read from unmapped memory space, addr="+m(d>>>0,8),32);return-1},function(d,e){C("Write to unmapped memory space, addr="+m(d>>>0,8)+" value="+m(e>>>0,8),32)})}Ea.prototype.create_empty_entry=function(){return{read8:this.empty_port_read8,read16:this.empty_port_read16,read32:this.empty_port_read32,write8:this.empty_port_write,write16:this.empty_port_write,write32:this.empty_port_write,device:void 0}};Ea.prototype.empty_port_read8=function(){return 255};
62
+ Ea.prototype.empty_port_read16=function(){return 65535};Ea.prototype.empty_port_read32=function(){return-1};Ea.prototype.empty_port_write=function(){};Ea.prototype.register_read=function(a,b,c,d,e){function f(h){k(!1,"Overlapped read"+h+" "+m(a,4)+" ("+b.name+")");return-1>>>32-h|0}k("number"===typeof a);k("object"===typeof b);k(!c||"function"===typeof c);k(!d||"function"===typeof d);k(!e||"function"===typeof e);k(c||d||e);c||(c=f.bind(this,8));d||(d=f.bind(this,16));e||(e=f.bind(this,32));c&&(this.ports[a].read8=
63
+ c);d&&(this.ports[a].read16=d);e&&(this.ports[a].read32=e);this.ports[a].device=b};Ea.prototype.register_write=function(a,b,c,d,e){function f(h){k(!1,"Overlapped write"+h+" "+m(a)+" ("+b.name+")")}k("number"===typeof a);k("object"===typeof b);k(!c||"function"===typeof c);k(!d||"function"===typeof d);k(!e||"function"===typeof e);k(c||d||e);c||(c=f.bind(this,8));d||(d=f.bind(this,16));e||(e=f.bind(this,32));c&&(this.ports[a].write8=c);d&&(this.ports[a].write16=d);e&&(this.ports[a].write32=e);this.ports[a].device=
64
+ b};Ea.prototype.register_read_consecutive=function(a,b,c,d,e,f){function h(){return c.call(this)|d.call(this)<<8}function l(){return e.call(this)|f.call(this)<<8}function p(){return c.call(this)|d.call(this)<<8|e.call(this)<<16|f.call(this)<<24}k(4===arguments.length||6===arguments.length);e&&f?(this.register_read(a,b,c,h,p),this.register_read(a+1,b,d),this.register_read(a+2,b,e,l),this.register_read(a+3,b,f)):(this.register_read(a,b,c,h),this.register_read(a+1,b,d))};Ea.prototype.register_write_consecutive=
65
+ function(a,b,c,d,e,f){function h(n){c.call(this,n&255);d.call(this,n>>8&255)}function l(n){e.call(this,n&255);f.call(this,n>>8&255)}function p(n){c.call(this,n&255);d.call(this,n>>8&255);e.call(this,n>>16&255);f.call(this,n>>>24)}k(4===arguments.length||6===arguments.length);e&&f?(this.register_write(a,b,c,h,p),this.register_write(a+1,b,d),this.register_write(a+2,b,e,l),this.register_write(a+3,b,f)):(this.register_write(a,b,c,h),this.register_write(a+1,b,d))};Ea.prototype.mmap_read32_shim=function(a){var b=
66
+ this.cpu.memory_map_read8[a>>>17];return b(a)|b(a+1)<<8|b(a+2)<<16|b(a+3)<<24};Ea.prototype.mmap_write32_shim=function(a,b){var c=this.cpu.memory_map_write8[a>>>17];c(a,b&255);c(a+1,b>>8&255);c(a+2,b>>16&255);c(a+3,b>>>24)};Ea.prototype.mmap_register=function(a,b,c,d,e,f){C("mmap_register addr="+m(a>>>0,8)+" size="+m(b,8),32);k(0===(a&131071));k(b&&0===(b&131071));e||(e=this.mmap_read32_shim.bind(this));f||(f=this.mmap_write32_shim.bind(this));for(a>>>=17;0<b;a++)this.cpu.memory_map_read8[a]=c,this.cpu.memory_map_write8[a]=
67
+ d,this.cpu.memory_map_read32[a]=e,this.cpu.memory_map_write32[a]=f,b-=131072};Ea.prototype.port_write8=function(a,b){var c=this.ports[a];c.write8===this.empty_port_write&&C("write8 port #"+m(a,4)+" <- "+m(b,2)+this.get_port_description(a),32);return c.write8.call(c.device,b)};Ea.prototype.port_write16=function(a,b){var c=this.ports[a];c.write16===this.empty_port_write&&C("write16 port #"+m(a,4)+" <- "+m(b,4)+this.get_port_description(a),32);return c.write16.call(c.device,b)};Ea.prototype.port_write32=
68
+ function(a,b){var c=this.ports[a];c.write32===this.empty_port_write&&C("write32 port #"+m(a,4)+" <- "+m(b>>>0,8)+this.get_port_description(a),32);return c.write32.call(c.device,b)};Ea.prototype.port_read8=function(a){var b=this.ports[a];b.read8===this.empty_port_read8&&C("read8 port #"+m(a,4)+this.get_port_description(a),32);b=b.read8.call(b.device,a);k("number"===typeof b);k(256>b&&0<=b,"8 bit port returned large value: "+m(a));return b};Ea.prototype.port_read16=function(a){var b=this.ports[a];
69
+ b.read16===this.empty_port_read16&&C("read16 port #"+m(a,4)+this.get_port_description(a),32);b=b.read16.call(b.device,a);k("number"===typeof b);k(65536>b&&0<=b,"16 bit port returned large value: "+m(a));return b};Ea.prototype.port_read32=function(a){var b=this.ports[a];b.read32===this.empty_port_read32&&C("read32 port #"+m(a,4)+this.get_port_description(a),32);a=b.read32.call(b.device,a);k((a|0)===a);return a};var Ga={4:"PORT_DMA_ADDR_2",5:"PORT_DMA_CNT_2",10:"PORT_DMA1_MASK_REG",11:"PORT_DMA1_MODE_REG",
70
+ 12:"PORT_DMA1_CLEAR_FF_REG",13:"PORT_DMA1_MASTER_CLEAR",32:"PORT_PIC1_CMD",33:"PORT_PIC1_DATA",64:"PORT_PIT_COUNTER0",65:"PORT_PIT_COUNTER1",66:"PORT_PIT_COUNTER2",67:"PORT_PIT_MODE",96:"PORT_PS2_DATA",97:"PORT_PS2_CTRLB",100:"PORT_PS2_STATUS",112:"PORT_CMOS_INDEX",113:"PORT_CMOS_DATA",128:"PORT_DIAG",129:"PORT_DMA_PAGE_2",146:"PORT_A20",160:"PORT_PIC2_CMD",161:"PORT_PIC2_DATA",178:"PORT_SMI_CMD",179:"PORT_SMI_STATUS",212:"PORT_DMA2_MASK_REG",214:"PORT_DMA2_MODE_REG",218:"PORT_DMA2_MASTER_CLEAR",
71
+ 240:"PORT_MATH_CLEAR",368:"PORT_ATA2_CMD_BASE",496:"PORT_ATA1_CMD_BASE",632:"PORT_LPT2",744:"PORT_SERIAL4",760:"PORT_SERIAL2",884:"PORT_ATA2_CTRL_BASE",888:"PORT_LPT1",1E3:"PORT_SERIAL3",1008:"PORT_FD_BASE",1010:"PORT_FD_DOR",1012:"PORT_FD_STATUS",1013:"PORT_FD_DATA",1014:"PORT_HD_DATA",1015:"PORT_FD_DIR",1016:"PORT_SERIAL1",3320:"PORT_PCI_CMD",3321:"PORT_PCI_REBOOT",3324:"PORT_PCI_DATA",1026:"PORT_BIOS_DEBUG",1296:"PORT_QEMU_CFG_CTL",1297:"PORT_QEMU_CFG_DATA",45056:"PORT_ACPI_PM_BASE",45312:"PORT_SMB_BASE",
72
+ 35072:"PORT_BIOS_APM"};Ea.prototype.get_port_description=function(a){return Ga[a]?" ("+Ga[a]+")":""};var Ha={};function Ia(){this.listeners={};this.pair=void 0}Ia.prototype.register=function(a,b,c){var d=this.listeners[a];void 0===d&&(d=this.listeners[a]=[]);d.push({fn:b,this_value:c})};Ia.prototype.unregister=function(a,b){var c=this.listeners[a];void 0!==c&&(this.listeners[a]=c.filter(function(d){return d.fn!==b}))};Ia.prototype.send=function(a,b){if(this.pair&&(a=this.pair.listeners[a],void 0!==
73
+ a))for(var c=0;c<a.length;c++){var d=a[c];d.fn.call(d.this_value,b)}};Ia.prototype.send_async=function(a,b){k(1===arguments.length||2===arguments.length);setTimeout(this.send.bind(this,a,b),0)};Ha.create=function(){var a=new Ia,b=new Ia;a.pair=b;b.pair=a;return[a,b]};var Ja=new Uint8Array(256),Ka=[],La=[],Oa=[],Pa=new Uint8Array(256),Qa=[];function E(a,b){this.cpu=a;this.bus=b;this.write_buffer=new ma(64);this.read_buffer=new ma(64);this.mixer_current_address=this.command_size=this.command=this.read_buffer_lastvalue=
74
+ 0;this.mixer_registers=new Uint8Array(256);this.mixer_reset();this.dummy_speaker_enabled=!1;this.test_register=0;this.dsp_signed=this.dsp_16bit=this.dsp_stereo=this.dsp_highspeed=!1;this.dac_buffers=[new na(65536),new na(65536)];this.dma=a.devices.dma;this.dma_channel=this.dma_irq=this.dma_bytes_block=this.dma_bytes_left=this.dma_bytes_count=this.dma_sample_count=0;this.dma_channel_8bit=1;this.dma_channel_16bit=5;this.dma_autoinit=!1;this.dma_buffer=new ArrayBuffer(65536);this.dma_buffer_int8=new Int8Array(this.dma_buffer);
75
+ this.dma_buffer_uint8=new Uint8Array(this.dma_buffer);this.dma_buffer_int16=new Int16Array(this.dma_buffer);this.dma_buffer_uint16=new Uint16Array(this.dma_buffer);this.dma_syncbuffer=new ya(this.dma_buffer);this.dma_paused=this.dma_waiting_transfer=!1;this.sampling_rate=22050;b.send("dac-tell-sampling-rate",this.sampling_rate);this.bytes_per_sample=1;this.e2_value=170;this.e2_count=0;this.asp_registers=new Uint8Array(256);this.mpu_read_buffer=new ma(64);this.fm_current_address1=this.fm_current_address0=
76
+ this.mpu_read_buffer_lastvalue=0;this.fm_waveform_select_enable=!1;this.irq=5;this.irq_triggered=new Uint8Array(16);a.io.register_read_consecutive(544,this,this.port2x0_read,this.port2x1_read,this.port2x2_read,this.port2x3_read);a.io.register_read_consecutive(904,this,this.port2x0_read,this.port2x1_read);a.io.register_read_consecutive(548,this,this.port2x4_read,this.port2x5_read);a.io.register_read(550,this,this.port2x6_read);a.io.register_read(551,this,this.port2x7_read);a.io.register_read(552,this,
77
+ this.port2x8_read);a.io.register_read(553,this,this.port2x9_read);a.io.register_read(554,this,this.port2xA_read);a.io.register_read(555,this,this.port2xB_read);a.io.register_read(556,this,this.port2xC_read);a.io.register_read(557,this,this.port2xD_read);a.io.register_read_consecutive(558,this,this.port2xE_read,this.port2xF_read);a.io.register_write_consecutive(544,this,this.port2x0_write,this.port2x1_write,this.port2x2_write,this.port2x3_write);a.io.register_write_consecutive(904,this,this.port2x0_write,
78
+ this.port2x1_write);a.io.register_write_consecutive(548,this,this.port2x4_write,this.port2x5_write);a.io.register_write(550,this,this.port2x6_write);a.io.register_write(551,this,this.port2x7_write);a.io.register_write_consecutive(552,this,this.port2x8_write,this.port2x9_write);a.io.register_write(554,this,this.port2xA_write);a.io.register_write(555,this,this.port2xB_write);a.io.register_write(556,this,this.port2xC_write);a.io.register_write(557,this,this.port2xD_write);a.io.register_write(558,this,
79
+ this.port2xE_write);a.io.register_write(559,this,this.port2xF_write);a.io.register_read_consecutive(816,this,this.port3x0_read,this.port3x1_read);a.io.register_write_consecutive(816,this,this.port3x0_write,this.port3x1_write);this.dma.on_unmask(this.dma_on_unmask,this);b.register("dac-request-data",function(){this.dac_handle_request()},this);b.register("speaker-has-initialized",function(){this.mixer_reset()},this);b.send("speaker-confirm-initialized");this.dsp_reset()}E.prototype.dsp_reset=function(){this.write_buffer.clear();
80
+ this.read_buffer.clear();this.command_size=this.command=0;this.dummy_speaker_enabled=!1;this.test_register=0;this.dsp_signed=this.dsp_16bit=this.dsp_stereo=this.dsp_highspeed=!1;this.dac_buffers[0].clear();this.dac_buffers[1].clear();this.dma_channel=this.dma_irq=this.dma_bytes_block=this.dma_bytes_left=this.dma_bytes_count=this.dma_sample_count=0;this.dma_autoinit=!1;this.dma_buffer_uint8.fill(0);this.dma_paused=this.dma_waiting_transfer=!1;this.e2_value=170;this.e2_count=0;this.sampling_rate=22050;
81
+ this.bytes_per_sample=1;this.lower_irq(1);this.irq_triggered.fill(0);this.asp_registers.fill(0);this.asp_registers[5]=1;this.asp_registers[9]=248};E.prototype.get_state=function(){var a=[];a[2]=this.read_buffer_lastvalue;a[3]=this.command;a[4]=this.command_size;a[5]=this.mixer_current_address;a[6]=this.mixer_registers;a[7]=this.dummy_speaker_enabled;a[8]=this.test_register;a[9]=this.dsp_highspeed;a[10]=this.dsp_stereo;a[11]=this.dsp_16bit;a[12]=this.dsp_signed;a[15]=this.dma_sample_count;a[16]=this.dma_bytes_count;
82
+ a[17]=this.dma_bytes_left;a[18]=this.dma_bytes_block;a[19]=this.dma_irq;a[20]=this.dma_channel;a[21]=this.dma_channel_8bit;a[22]=this.dma_channel_16bit;a[23]=this.dma_autoinit;a[24]=this.dma_buffer_uint8;a[25]=this.dma_waiting_transfer;a[26]=this.dma_paused;a[27]=this.sampling_rate;a[28]=this.bytes_per_sample;a[29]=this.e2_value;a[30]=this.e2_count;a[31]=this.asp_registers;a[33]=this.mpu_read_buffer_last_value;a[34]=this.irq;a[35]=this.irq_triggered;return a};E.prototype.set_state=function(a){this.read_buffer_lastvalue=
83
+ a[2];this.command=a[3];this.command_size=a[4];this.mixer_current_address=a[5];this.mixer_registers=a[6];this.mixer_full_update();this.dummy_speaker_enabled=a[7];this.test_register=a[8];this.dsp_highspeed=a[9];this.dsp_stereo=a[10];this.dsp_16bit=a[11];this.dsp_signed=a[12];this.dma_sample_count=a[15];this.dma_bytes_count=a[16];this.dma_bytes_left=a[17];this.dma_bytes_block=a[18];this.dma_irq=a[19];this.dma_channel=a[20];this.dma_channel_8bit=a[21];this.dma_channel_16bit=a[22];this.dma_autoinit=a[23];
84
+ this.dma_buffer_uint8=a[24];this.dma_waiting_transfer=a[25];this.dma_paused=a[26];this.sampling_rate=a[27];this.bytes_per_sample=a[28];this.e2_value=a[29];this.e2_count=a[30];this.asp_registers=a[31];this.mpu_read_buffer_last_value=a[33];this.irq=a[34];this.irq_triggered=a[35];this.dma_buffer=this.dma_buffer_uint8.buffer;this.dma_buffer_int8=new Int8Array(this.dma_buffer);this.dma_buffer_int16=new Int16Array(this.dma_buffer);this.dma_buffer_uint16=new Uint16Array(this.dma_buffer);this.dma_syncbuffer=
85
+ new ya(this.dma_buffer);this.dma_paused?this.bus.send("dac-disable"):this.bus.send("dac-enable")};E.prototype.port2x0_read=function(){C("220 read: fm music status port (unimplemented)",8388608);return 255};E.prototype.port2x1_read=function(){C("221 read: fm music data port (write only)",8388608);return 255};E.prototype.port2x2_read=function(){C("222 read: advanced fm music status port (unimplemented)",8388608);return 255};E.prototype.port2x3_read=function(){C("223 read: advanced music data port (write only)",
86
+ 8388608);return 255};E.prototype.port2x4_read=function(){C("224 read: mixer address port",8388608);return this.mixer_current_address};E.prototype.port2x5_read=function(){C("225 read: mixer data port",8388608);return this.mixer_read(this.mixer_current_address)};E.prototype.port2x6_read=function(){C("226 read: (write only)",8388608);return 255};E.prototype.port2x7_read=function(){C("227 read: undocumented",8388608);return 255};E.prototype.port2x8_read=function(){C("228 read: fm music status port (unimplemented)",
87
+ 8388608);return 255};E.prototype.port2x9_read=function(){C("229 read: fm music data port (write only)",8388608);return 255};E.prototype.port2xA_read=function(){C("22A read: read data",8388608);this.read_buffer.length&&(this.read_buffer_lastvalue=this.read_buffer.shift());C(" <- "+this.read_buffer_lastvalue+" "+m(this.read_buffer_lastvalue)+" '"+String.fromCharCode(this.read_buffer_lastvalue)+"'",8388608);return this.read_buffer_lastvalue};E.prototype.port2xB_read=function(){C("22B read: undocumented",
88
+ 8388608);return 255};E.prototype.port2xC_read=function(){C("22C read: write-buffer status",8388608);return 127};E.prototype.port2xD_read=function(){C("22D read: undocumented",8388608);return 255};E.prototype.port2xE_read=function(){C("22E read: read-buffer status / irq 8bit ack.",8388608);this.irq_triggered[1]&&this.lower_irq(1);return(this.read_buffer.length&&!this.dsp_highspeed)<<7|127};E.prototype.port2xF_read=function(){C("22F read: irq 16bit ack",8388608);this.lower_irq(2);return 0};E.prototype.port2x0_write=
89
+ function(a){C("220 write: (unimplemented) fm register 0 address = "+m(a),8388608);this.fm_current_address0=0};E.prototype.port2x1_write=function(a){C("221 write: (unimplemented) fm register 0 data = "+m(a),8388608);var b=Qa[this.fm_current_address0];b||(b=this.fm_default_write);b.call(this,a,0,this.fm_current_address0)};E.prototype.port2x2_write=function(a){C("222 write: (unimplemented) fm register 1 address = "+m(a),8388608);this.fm_current_address1=0};E.prototype.port2x3_write=function(a){C("223 write: (unimplemented) fm register 1 data ="+
90
+ m(a),8388608);var b=Qa[this.fm_current_address1];b||(b=this.fm_default_write);b.call(this,a,1,this.fm_current_address1)};E.prototype.port2x4_write=function(a){C("224 write: mixer address = "+m(a),8388608);this.mixer_current_address=a};E.prototype.port2x5_write=function(a){C("225 write: mixer data = "+m(a),8388608);this.mixer_write(this.mixer_current_address,a)};E.prototype.port2x6_write=function(a){C("226 write: reset = "+m(a),8388608);this.dsp_highspeed?(C(" -> exit highspeed",8388608),this.dsp_highspeed=
91
+ !1):a&&(C(" -> reset",8388608),this.dsp_reset());this.read_buffer.clear();this.read_buffer.push(170)};E.prototype.port2x7_write=function(){C("227 write: undocumented",8388608)};E.prototype.port2x8_write=function(){C("228 write: fm music register port (unimplemented)",8388608)};E.prototype.port2x9_write=function(){C("229 write: fm music data port (unimplemented)",8388608)};E.prototype.port2xA_write=function(){C("22A write: dsp read data port (read only)",8388608)};E.prototype.port2xB_write=function(){C("22B write: undocumented",
92
+ 8388608)};E.prototype.port2xC_write=function(a){C("22C write: write command/data",8388608);0===this.command?(C("22C write: command = "+m(a),8388608),this.command=a,this.write_buffer.clear(),this.command_size=Ja[a]):(C("22C write: data: "+m(a),8388608),this.write_buffer.push(a));this.write_buffer.length>=this.command_size&&this.command_do()};E.prototype.port2xD_write=function(){C("22D write: undocumented",8388608)};E.prototype.port2xE_write=function(){C("22E write: dsp read buffer status (read only)",
93
+ 8388608)};E.prototype.port2xF_write=function(){C("22F write: undocumented",8388608)};E.prototype.port3x0_read=function(){C("330 read: mpu data",8388608);this.mpu_read_buffer.length&&(this.mpu_read_buffer_lastvalue=this.mpu_read_buffer.shift());C(" <- "+m(this.mpu_read_buffer_lastvalue),8388608);return this.mpu_read_buffer_lastvalue};E.prototype.port3x0_write=function(a){C("330 write: mpu data (unimplemented) : "+m(a),8388608)};E.prototype.port3x1_read=function(){C("331 read: mpu status",8388608);
94
+ return 0|128*!this.mpu_read_buffer.length};E.prototype.port3x1_write=function(a){C("331 write: mpu command: "+m(a),8388608);255===a&&(this.mpu_read_buffer.clear(),this.mpu_read_buffer.push(254))};E.prototype.command_do=function(){var a=Ka[this.command];a||(a=this.dsp_default_handler);a.call(this);this.command_size=this.command=0;this.write_buffer.clear()};E.prototype.dsp_default_handler=function(){C("Unhandled command: "+m(this.command),8388608)};function H(a,b,c){c||(c=E.prototype.dsp_default_handler);
95
+ for(var d=0;d<a.length;d++)Ja[a[d]]=b,Ka[a[d]]=c}function Ra(a){for(var b=[],c=0;16>c;c++)b.push(a+c);return b}H([14],2,function(){this.asp_registers[this.write_buffer.shift()]=this.write_buffer.shift()});H([15],1,function(){this.read_buffer.clear();this.read_buffer.push(this.asp_registers[this.write_buffer.shift()])});H([16],1,function(){var a=this.write_buffer.shift();a=Sa(a/127.5+-1,-1,1);this.dac_buffers[0].push(a);this.dac_buffers[1].push(a);this.bus.send("dac-enable")});H([20,21],2,function(){this.dma_irq=
96
+ 1;this.dma_channel=this.dma_channel_8bit;this.dsp_highspeed=this.dsp_16bit=this.dsp_signed=this.dma_autoinit=!1;this.dma_transfer_size_set();this.dma_transfer_start()});H([22],2);H([23],2);H([28],0,function(){this.dma_irq=1;this.dma_channel=this.dma_channel_8bit;this.dma_autoinit=!0;this.dsp_highspeed=this.dsp_16bit=this.dsp_signed=!1;this.dma_transfer_start()});H([31],0);H([32],0,function(){this.read_buffer.clear();this.read_buffer.push(127)});H([36],2);H([44],0);H([48],0);H([49],0);H([52],0);H([53],
97
+ 0);H([54],0);H([55],0);H([56],0);H([64],1,function(){this.sampling_rate_change(1E6/(256-this.write_buffer.shift())/this.get_channel_count())});H([65,66],2,function(){this.sampling_rate_change(this.write_buffer.shift()<<8|this.write_buffer.shift())});H([72],2,function(){this.dma_transfer_size_set()});H([116],2);H([117],2);H([118],2);H([119],2);H([125],0);H([127],0);H([128],2);H([144],0,function(){this.dma_irq=1;this.dma_channel=this.dma_channel_8bit;this.dma_autoinit=!0;this.dsp_signed=!1;this.dsp_highspeed=
98
+ !0;this.dsp_16bit=!1;this.dma_transfer_start()});H([145],0);H([152],0);H([153],0);H([160],0);H([168],0);H(Ra(176),3,function(){if(this.command&8)this.dsp_default_handler();else{var a=this.write_buffer.shift();this.dma_irq=2;this.dma_channel=this.dma_channel_16bit;this.dma_autoinit=!!(this.command&4);this.dsp_signed=!!(a&16);this.dsp_stereo=!!(a&32);this.dsp_16bit=!0;this.dma_transfer_size_set();this.dma_transfer_start()}});H(Ra(192),3,function(){if(this.command&8)this.dsp_default_handler();else{var a=
99
+ this.write_buffer.shift();this.dma_irq=1;this.dma_channel=this.dma_channel_8bit;this.dma_autoinit=!!(this.command&4);this.dsp_signed=!!(a&16);this.dsp_stereo=!!(a&32);this.dsp_16bit=!1;this.dma_transfer_size_set();this.dma_transfer_start()}});H([208],0,function(){this.dma_paused=!0;this.bus.send("dac-disable")});H([209],0,function(){this.dummy_speaker_enabled=!0});H([211],0,function(){this.dummy_speaker_enabled=!1});H([212],0,function(){this.dma_paused=!1;this.bus.send("dac-enable")});H([213],0,function(){this.dma_paused=
100
+ !0;this.bus.send("dac-disable")});H([214],0,function(){this.dma_paused=!1;this.bus.send("dac-enable")});H([216],0,function(){this.read_buffer.clear();this.read_buffer.push(255*this.dummy_speaker_enabled)});H([217,218],0,function(){this.dma_autoinit=!1});H([224],1,function(){this.read_buffer.clear();this.read_buffer.push(~this.write_buffer.shift())});H([225],0,function(){this.read_buffer.clear();this.read_buffer.push(4);this.read_buffer.push(5)});H([226],1);H([227],0,function(){this.read_buffer.clear();
101
+ for(var a=0;44>a;a++)this.read_buffer.push("COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.".charCodeAt(a));this.read_buffer.push(0)});H([228],1,function(){this.test_register=this.write_buffer.shift()});H([232],0,function(){this.read_buffer.clear();this.read_buffer.push(this.test_register)});H([242,243],0,function(){this.raise_irq()});var Ta=new Uint8Array(256);Ta[14]=255;Ta[15]=7;Ta[55]=56;H([249],1,function(){var a=this.write_buffer.shift();C("dsp 0xf9: unknown function. input: "+a,8388608);this.read_buffer.clear();
102
+ this.read_buffer.push(Ta[a])});E.prototype.mixer_read=function(a){var b=La[a];b?b=b.call(this):(b=this.mixer_registers[a],C("unhandled mixer register read. addr:"+m(a)+" data:"+m(b),8388608));return b};E.prototype.mixer_write=function(a,b){var c=Oa[a];c?c.call(this,b):C("unhandled mixer register write. addr:"+m(a)+" data:"+m(b),8388608)};E.prototype.mixer_default_read=function(){C("mixer register read. addr:"+m(this.mixer_current_address),8388608);return this.mixer_registers[this.mixer_current_address]};
103
+ E.prototype.mixer_default_write=function(a){C("mixer register write. addr:"+m(this.mixer_current_address)+" data:"+m(a),8388608);this.mixer_registers[this.mixer_current_address]=a};E.prototype.mixer_reset=function(){this.mixer_registers[4]=204;this.mixer_registers[34]=204;this.mixer_registers[38]=204;this.mixer_registers[40]=0;this.mixer_registers[46]=0;this.mixer_registers[10]=0;this.mixer_registers[48]=192;this.mixer_registers[49]=192;this.mixer_registers[50]=192;this.mixer_registers[51]=192;this.mixer_registers[52]=
104
+ 192;this.mixer_registers[53]=192;this.mixer_registers[54]=0;this.mixer_registers[55]=0;this.mixer_registers[56]=0;this.mixer_registers[57]=0;this.mixer_registers[59]=0;this.mixer_registers[60]=31;this.mixer_registers[61]=21;this.mixer_registers[62]=11;this.mixer_registers[63]=0;this.mixer_registers[64]=0;this.mixer_registers[65]=0;this.mixer_registers[66]=0;this.mixer_registers[67]=0;this.mixer_registers[68]=128;this.mixer_registers[69]=128;this.mixer_registers[70]=128;this.mixer_registers[71]=128;
105
+ this.mixer_full_update()};E.prototype.mixer_full_update=function(){for(var a=1;a<this.mixer_registers.length;a++)Pa[a]||this.mixer_write(a,this.mixer_registers[a])};function Ua(a,b){b||(b=E.prototype.mixer_default_read);La[a]=b}function Wa(a,b){b||(b=E.prototype.mixer_default_write);Oa[a]=b}function Xa(a,b,c){Pa[a]=1;La[a]=function(){return this.mixer_registers[b]&240|this.mixer_registers[c]>>>4};Oa[a]=function(d){this.mixer_registers[a]=d;var e=d<<4&240|this.mixer_registers[c]&15;this.mixer_write(b,
106
+ d&240|this.mixer_registers[b]&15);this.mixer_write(c,e)}}function Ya(a,b,c){La[a]=E.prototype.mixer_default_read;Oa[a]=function(d){this.mixer_registers[a]=d;this.bus.send("mixer-volume",[b,c,(d>>>2)-62])}}Ua(0,function(){this.mixer_reset();return 0});Wa(0);Xa(4,50,51);Xa(34,48,49);Xa(38,52,53);Xa(40,54,55);Xa(46,56,57);Ya(48,0,0);Ya(49,0,1);Ya(50,2,0);Ya(51,2,1);Ua(59);Wa(59,function(a){this.mixer_registers[59]=a;this.bus.send("mixer-volume",[1,2,6*(a>>>6)-18])});Ua(65);Wa(65,function(a){this.mixer_registers[65]=
107
+ a;this.bus.send("mixer-gain-left",6*(a>>>6))});Ua(66);Wa(66,function(a){this.mixer_registers[66]=a;this.bus.send("mixer-gain-right",6*(a>>>6))});Ua(68);Wa(68,function(a){this.mixer_registers[68]=a;a>>>=3;this.bus.send("mixer-treble-left",a-(16>a?14:16))});Ua(69);Wa(69,function(a){this.mixer_registers[69]=a;a>>>=3;this.bus.send("mixer-treble-right",a-(16>a?14:16))});Ua(70);Wa(70,function(a){this.mixer_registers[70]=a;a>>>=3;this.bus.send("mixer-bass-right",a-(16>a?14:16))});Ua(71);Wa(71,function(a){this.mixer_registers[71]=
108
+ a;a>>>=3;this.bus.send("mixer-bass-right",a-(16>a?14:16))});Ua(128,function(){switch(this.irq){case 2:return 1;case 5:return 2;case 7:return 4;case 10:return 8;default:return 0}});Wa(128,function(a){a&1&&(this.irq=2);a&2&&(this.irq=5);a&4&&(this.irq=7);a&8&&(this.irq=10)});Ua(129,function(){var a=0;switch(this.dma_channel_8bit){case 0:a|=1;break;case 1:a|=2;break;case 3:a|=8}switch(this.dma_channel_16bit){case 5:a|=32;break;case 6:a|=64;break;case 7:a|=128}return a});Wa(129,function(a){a&1&&(this.dma_channel_8bit=
109
+ 0);a&2&&(this.dma_channel_8bit=1);a&8&&(this.dma_channel_8bit=3);a&32&&(this.dma_channel_16bit=5);a&64&&(this.dma_channel_16bit=6);a&128&&(this.dma_channel_16bit=7)});Ua(130,function(){for(var a=32,b=0;16>b;b++)a|=b*this.irq_triggered[b];return a});E.prototype.fm_default_write=function(a,b,c){C("unhandled fm register write. addr:"+b+"|"+m(c)+" data:"+m(a),8388608)};function Za(a,b){b||(b=E.prototype.fm_default_write);for(var c=0;c<a.length;c++)Qa[a[c]]=b}function $a(a,b){for(var c=[];a<=b;a++)c.push(a);
110
+ return c}const ab=new Uint8Array(32);ab[0]=0;ab[1]=1;ab[2]=2;ab[3]=3;ab[4]=4;ab[5]=5;ab[8]=6;ab[9]=7;ab[10]=8;ab[11]=9;ab[12]=10;ab[13]=11;ab[16]=12;ab[17]=13;ab[18]=14;ab[19]=15;ab[20]=16;ab[21]=17;Za([1],function(a,b){this.fm_waveform_select_enable[b]=a&1;this.fm_update_waveforms()});Za([2]);Za([3]);Za([4],function(){});Za([5],function(a,b,c){0===b&&this.fm_default_write(a,b,c)});Za([8],function(){});Za($a(32,53),function(){});Za($a(64,85),function(){});Za($a(96,117),function(){});Za($a(128,149),
111
+ function(){});Za($a(160,168),function(){});Za($a(176,184),function(){});Za([189],function(){});Za($a(192,200),function(){});Za($a(224,245),function(){});E.prototype.fm_update_waveforms=function(){};E.prototype.sampling_rate_change=function(a){this.sampling_rate=a;this.bus.send("dac-tell-sampling-rate",a)};E.prototype.get_channel_count=function(){return this.dsp_stereo?2:1};E.prototype.dma_transfer_size_set=function(){this.dma_sample_count=1+(this.write_buffer.shift()<<0)+(this.write_buffer.shift()<<
112
+ 8)};E.prototype.dma_transfer_start=function(){C("begin dma transfer",8388608);this.bytes_per_sample=1;this.dsp_16bit&&(this.bytes_per_sample*=2);this.dma_bytes_count=this.dma_sample_count*this.bytes_per_sample;this.dma_bytes_block=1024*this.bytes_per_sample;this.dma_bytes_block=Math.min(Math.max(this.dma_bytes_count>>2&-4,32),this.dma_bytes_block);this.dma_waiting_transfer=!0;this.dma.channel_mask[this.dma_channel]||this.dma_on_unmask(this.dma_channel)};E.prototype.dma_on_unmask=function(a){a===this.dma_channel&&
113
+ this.dma_waiting_transfer&&(this.dma_waiting_transfer=!1,this.dma_bytes_left=this.dma_bytes_count,this.dma_paused=!1,this.bus.send("dac-enable"))};E.prototype.dma_transfer_next=function(){C("dma transfering next block",8388608);var a=Math.min(this.dma_bytes_left,this.dma_bytes_block),b=Math.floor(a/this.bytes_per_sample);this.dma.do_write(this.dma_syncbuffer,0,a,this.dma_channel,c=>{C("dma block transfer "+(c?"unsuccessful":"successful"),8388608);c||(this.dma_to_dac(b),this.dma_bytes_left-=a,this.dma_bytes_left||
114
+ (this.raise_irq(this.dma_irq),this.dma_autoinit&&(this.dma_bytes_left=this.dma_bytes_count)))})};E.prototype.dma_to_dac=function(a){var b=this.dsp_16bit?32767.5:127.5,c=this.dsp_signed?0:-1,d=this.dsp_stereo?1:2;var e=this.dsp_16bit?this.dsp_signed?this.dma_buffer_int16:this.dma_buffer_uint16:this.dsp_signed?this.dma_buffer_int8:this.dma_buffer_uint8;for(var f=0,h=0;h<a;h++)for(var l=Sa(e[h]/b+c,-1,1),p=0;p<d;p++)this.dac_buffers[f].push(l),f^=1;this.dac_send()};E.prototype.dac_handle_request=function(){!this.dma_bytes_left||
115
+ this.dma_paused?this.dac_send():this.dma_transfer_next()};E.prototype.dac_send=function(){if(this.dac_buffers[0].length){var a=this.dac_buffers[0].shift_block(this.dac_buffers[0].length),b=this.dac_buffers[1].shift_block(this.dac_buffers[1].length);this.bus.send("dac-send-data",[a,b],[a.buffer,b.buffer])}};E.prototype.raise_irq=function(a){C("raise irq",8388608);this.irq_triggered[a]=1;this.cpu.device_raise_irq(this.irq)};E.prototype.lower_irq=function(a){C("lower irq",8388608);this.irq_triggered[a]=
116
+ 0;this.cpu.device_lower_irq(this.irq)};function Sa(a,b,c){return(a<b)*b+(a>c)*c+(b<=a&&a<=c)*a}function bb(a){this.message=a}bb.prototype=Error();const cb={Uint8Array,Int8Array,Uint16Array,Int16Array,Uint32Array,Int32Array,Float32Array,Float64Array};function db(a,b){if("object"!==typeof a||null===a)return k("function"!==typeof a),a;if(Array.isArray(a))return a.map(f=>db(f,b));a.constructor===Object&&(console.log(a),k(a.constructor!==Object,"Expected non-object"));if(a.BYTES_PER_ELEMENT){var c=new Uint8Array(a.buffer,
117
+ a.byteOffset,a.length*a.BYTES_PER_ELEMENT);a=a.constructor.name.replace("bound ","");k(cb[a]);return{__state_type__:a,buffer_id:b.push(c)-1}}!a.get_state&&console.log("Object without get_state: ",a);c=a.get_state();a=[];for(var d=0;d<c.length;d++){var e=c[d];k("function"!==typeof e);a[d]=db(e,b)}return a}function eb(a,b){if("object"!==typeof a||null===a)return k("function"!==typeof a),a;if(Array.isArray(a)){for(var c=0;c<a.length;c++)a[c]=eb(a[c],b);return a}c=a.__state_type__;k(void 0!==c);const d=
118
+ cb[c];k(d,"Unkown type: "+c);return new d(b[a.buffer_id])}function fb(a,b){function c(u,y){const A=u.length;if(16>A)throw new bb("Invalid length: "+A);u=new Int32Array(u.buffer,u.byteOffset,4);if(-2039052682!==u[0])throw new bb("Invalid header: "+m(u[0]>>>0));if(6!==u[1])throw new bb("Version mismatch: dump="+u[1]+" we=6");if(y&&u[2]!==A)throw new bb("Length doesn't match header: real="+A+" header="+u[2]);return u[3]}function d(u){u=(new TextDecoder).decode(u);return JSON.parse(u)}b=new Uint8Array(b);
119
+ if(4247762216===(new Uint32Array(b.buffer,0,1))[0]){var e=a.zstd_create_ctx(b.length);(new Uint8Array(a.wasm_memory.buffer,a.zstd_get_src_ptr(e),b.length)).set(b);var f=a.zstd_read(e,16),h=new Uint8Array(a.wasm_memory.buffer,f,16),l=c(h,!1);a.zstd_read_free(f,16);f=a.zstd_read(e,l);h=new Uint8Array(a.wasm_memory.buffer,f,l);h=d(h);a.zstd_read_free(f,l);f=h.state;var p=h.buffer_infos;h=[];l=16+l;for(var n of p){p=(l+3&-4)-l;if(1048576<n.length){var q=a.zstd_read(e,p);a.zstd_read_free(q,p);q=new Uint8Array(n.length);
120
+ h.push(q.buffer);for(var r=0;r<n.length;){var t=n.length-r;k(0<=t);t=Math.min(t,1048576);const u=a.zstd_read(e,t);q.set(new Uint8Array(a.wasm_memory.buffer,u,t),r);a.zstd_read_free(u,t);r+=t}}else q=a.zstd_read(e,p+n.length),r=q+p,h.push(a.wasm_memory.buffer.slice(r,r+n.length)),a.zstd_read_free(q,p+n.length);l+=p+n.length}f=eb(f,h);a.set_state(f);a.zstd_free_ctx(e)}else{e=c(b,!0);if(0>e||e+12>=b.length)throw new bb("Invalid info block length: "+e);n=b.subarray(16,16+e);f=d(n);n=f.state;f=f.buffer_infos;
121
+ let u=16+e;u=u+3&-4;e=f.map(y=>{const A=u+y.offset;return b.buffer.slice(A,A+y.length)});n=eb(n,e);a.set_state(n)}}function gb(a,b,c,d,e){let f="";var h=[],l=b?"compiled":c?"jit exit":d?"unguarded register":e?"wasm size":"executed";for(let q=0;256>q;q++)for(let r=0;8>r;r++)for(const t of[!1,!0]){var p=a.wm.exports.get_opstats_buffer(b,c,d,e,q,!1,t,r);h.push({opcode:q,count:p,is_mem:t,fixed_g:r});p=a.wm.exports.get_opstats_buffer(b,c,d,e,q,!0,t,r);h.push({opcode:3840|q,count:p,is_mem:t,fixed_g:r})}a=
122
+ 0;b=new Set([38,46,54,62,100,101,102,103,240,242,243]);for(const {count:q,opcode:r}of h)b.has(r)||(a+=q);if(0===a)return"";c=new Uint32Array(256);b=new Uint32Array(256);for(const {opcode:q,count:r}of h)3840===(q&65280)?b[q&255]+=r:c[q&255]+=r;f=f+"------------------\nTotal: "+(a+"\n");const n=1E7<a?1E3:1;d=Math.max.apply(Math,h.map(({count:q})=>Math.round(q/n)));d=String(d).length;f+=`Instruction counts ${l} (in ${n}):\n`;for(e=0;256>e;e++)f+=e.toString(16).padStart(2,"0")+":"+ba(Math.round(c[e]/
123
+ n),d),f=15===e%16?f+"\n":f+" ";f=f+"\n"+`Instruction counts ${l} (0f, in ${n}):\n`;for(l=0;256>l;l++)f+=(l&255).toString(16).padStart(2,"0")+":"+ba(Math.round(b[l]/n),d),f=15===l%16?f+"\n":f+" ";f+="\n";h=h.filter(({count:q})=>q).sort(({count:q},{count:r})=>r-q);for(const {opcode:q,is_mem:r,fixed_g:t,count:u}of h.slice(0,200))h=q.toString(16)+"_"+t+(r?"_m":"_r"),f+=h+":"+(u/a*100).toFixed(2)+" ";return f+"\n"}function hb(a){this.cpu=a;this.cmos_index=0;this.cmos_data=new Uint8Array(128);this.last_update=
124
+ this.rtc_time=Date.now();this.next_interrupt_alarm=this.next_interrupt=0;this.periodic_interrupt=!1;this.periodic_interrupt_time=.9765625;this.cmos_a=38;this.cmos_b=2;this.nmi_disabled=this.cmos_c=0;a.io.register_write(112,this,function(b){this.cmos_index=b&127;this.nmi_disabled=b>>7});a.io.register_write(113,this,this.cmos_port_write);a.io.register_read(113,this,this.cmos_port_read)}hb.prototype.get_state=function(){var a=[];a[0]=this.cmos_index;a[1]=this.cmos_data;a[2]=this.rtc_time;a[3]=this.last_update;
125
+ a[4]=this.next_interrupt;a[5]=this.next_interrupt_alarm;a[6]=this.periodic_interrupt;a[7]=this.periodic_interrupt_time;a[8]=this.cmos_a;a[9]=this.cmos_b;a[10]=this.cmos_c;a[11]=this.nmi_disabled;return a};hb.prototype.set_state=function(a){this.cmos_index=a[0];this.cmos_data=a[1];this.rtc_time=a[2];this.last_update=a[3];this.next_interrupt=a[4];this.next_interrupt_alarm=a[5];this.periodic_interrupt=a[6];this.periodic_interrupt_time=a[7];this.cmos_a=a[8];this.cmos_b=a[9];this.cmos_c=a[10];this.nmi_disabled=
126
+ a[11]};hb.prototype.timer=function(a){a=Date.now();this.rtc_time+=a-this.last_update;this.last_update=a;this.periodic_interrupt&&this.next_interrupt<a?(this.cpu.device_raise_irq(8),this.cmos_c|=192,this.next_interrupt+=this.periodic_interrupt_time*Math.ceil((a-this.next_interrupt)/this.periodic_interrupt_time)):this.next_interrupt_alarm&&this.next_interrupt_alarm<a&&(this.cpu.device_raise_irq(8),this.cmos_c|=160,this.next_interrupt_alarm=0);let b=100;this.periodic_interrupt&&this.next_interrupt&&
127
+ (b=Math.min(b,Math.max(0,this.next_interrupt-a)));this.next_interrupt_alarm&&(b=Math.min(b,Math.max(0,this.next_interrupt_alarm-a)));return b};hb.prototype.bcd_pack=function(a){for(var b=0,c=0,d;a;)d=a%10,c|=d<<4*b,b++,a=(a-d)/10;return c};hb.prototype.bcd_unpack=function(a){const b=a&15,c=a>>4&15;k(256>a);k(10>b);k(10>c);return b+10*c};hb.prototype.encode_time=function(a){return this.cmos_b&4?a:this.bcd_pack(a)};hb.prototype.decode_time=function(a){return this.cmos_b&4?a:this.bcd_unpack(a)};hb.prototype.cmos_port_read=
128
+ function(){var a=this.cmos_index;switch(a){case 0:return C("read second: "+m(this.encode_time((new Date(this.rtc_time)).getUTCSeconds())),65536),this.encode_time((new Date(this.rtc_time)).getUTCSeconds());case 2:return C("read minute: "+m(this.encode_time((new Date(this.rtc_time)).getUTCMinutes())),65536),this.encode_time((new Date(this.rtc_time)).getUTCMinutes());case 4:return C("read hour: "+m(this.encode_time((new Date(this.rtc_time)).getUTCHours())),65536),this.encode_time((new Date(this.rtc_time)).getUTCHours());
129
+ case 6:return C("read day: "+m(this.encode_time((new Date(this.rtc_time)).getUTCDay()+1)),65536),this.encode_time((new Date(this.rtc_time)).getUTCDay()+1);case 7:return C("read day of month: "+m(this.encode_time((new Date(this.rtc_time)).getUTCDate())),65536),this.encode_time((new Date(this.rtc_time)).getUTCDate());case 8:return C("read month: "+m(this.encode_time((new Date(this.rtc_time)).getUTCMonth()+1)),65536),this.encode_time((new Date(this.rtc_time)).getUTCMonth()+1);case 9:return C("read year: "+
130
+ m(this.encode_time((new Date(this.rtc_time)).getUTCFullYear()%100)),65536),this.encode_time((new Date(this.rtc_time)).getUTCFullYear()%100);case 10:return 999<=I.microtick()%1E3?this.cmos_a|128:this.cmos_a;case 11:return this.cmos_b;case 12:return this.cpu.device_lower_irq(8),C("cmos reg C read",65536),a=this.cmos_c,this.cmos_c&=-241,a;case 13:return 0;case 50:case 55:return C("read century: "+m(this.encode_time((new Date(this.rtc_time)).getUTCFullYear()/100|0)),65536),this.encode_time((new Date(this.rtc_time)).getUTCFullYear()/
131
+ 100|0);default:return C("cmos read from index "+m(a),65536),this.cmos_data[this.cmos_index]}};hb.prototype.cmos_port_write=function(a){switch(this.cmos_index){case 10:this.cmos_a=a&127;this.periodic_interrupt_time=1E3/(32768>>(this.cmos_a&15)-1);C("Periodic interrupt, a="+m(this.cmos_a,2)+" t="+this.periodic_interrupt_time,65536);break;case 11:this.cmos_b=a;this.cmos_b&64&&(this.next_interrupt=Date.now());if(this.cmos_b&32){a=new Date;const b=this.decode_time(this.cmos_data[1]),c=this.decode_time(this.cmos_data[3]),
132
+ d=this.decode_time(this.cmos_data[5]),e=new Date(Date.UTC(a.getUTCFullYear(),a.getUTCMonth(),a.getUTCDate(),d,c,b));C("RTC alarm scheduled for "+e+" hh:mm:ss="+d+":"+c+":"+b+" ms_from_now="+(e-a),65536);this.next_interrupt_alarm=+e}this.cmos_b&16&&C("Unimplemented: updated interrupt",65536);C("cmos b="+m(this.cmos_b,2),65536);break;case 1:case 3:case 5:this.cmos_write(this.cmos_index,a);break;default:C("cmos write index "+m(this.cmos_index)+": "+m(a),65536)}this.periodic_interrupt=64===(this.cmos_b&
133
+ 64)&&0<(this.cmos_a&15)};hb.prototype.cmos_read=function(a){k(128>a);return this.cmos_data[a]};hb.prototype.cmos_write=function(a,b){C("cmos "+m(a)+" <- "+m(b),65536);k(128>a);this.cmos_data[a]=b};function jb(a,b){this.cpu=a;this.bus=b;this.counter_start_time=new Float64Array(3);this.counter_start_value=new Uint16Array(3);this.counter_next_low=new Uint8Array(4);this.counter_enabled=new Uint8Array(4);this.counter_mode=new Uint8Array(4);this.counter_read_mode=new Uint8Array(4);this.counter_latch=new Uint8Array(4);
134
+ this.counter_latch_value=new Uint16Array(3);this.counter_reload=new Uint16Array(3);a.io.register_read(97,this,function(){var c=I.microtick(),d=66.66666666666667*c&1;c=this.did_rollover(2,c);return d<<4|c<<5});a.io.register_write(97,this,function(c){c&1?this.bus.send("pcspeaker-enable"):this.bus.send("pcspeaker-disable")});a.io.register_read(64,this,function(){return this.counter_read(0)});a.io.register_read(65,this,function(){return this.counter_read(1)});a.io.register_read(66,this,function(){return this.counter_read(2)});
135
+ a.io.register_write(64,this,function(c){this.counter_write(0,c)});a.io.register_write(65,this,function(c){this.counter_write(1,c)});a.io.register_write(66,this,function(c){this.counter_write(2,c);this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]])});a.io.register_write(67,this,this.port43_write)}jb.prototype.get_state=function(){var a=[];a[0]=this.counter_next_low;a[1]=this.counter_enabled;a[2]=this.counter_mode;a[3]=this.counter_read_mode;a[4]=this.counter_latch;a[5]=
136
+ this.counter_latch_value;a[6]=this.counter_reload;a[7]=this.counter_start_time;a[8]=this.counter_start_value;return a};jb.prototype.set_state=function(a){this.counter_next_low=a[0];this.counter_enabled=a[1];this.counter_mode=a[2];this.counter_read_mode=a[3];this.counter_latch=a[4];this.counter_latch_value=a[5];this.counter_reload=a[6];this.counter_start_time=a[7];this.counter_start_value=a[8]};jb.prototype.timer=function(a,b){var c=100;b||(this.counter_enabled[0]&&this.did_rollover(0,a)?(this.counter_start_value[0]=
137
+ this.get_counter_value(0,a),this.counter_start_time[0]=a,C("pit interrupt. new value: "+this.counter_start_value[0],512),this.cpu.device_lower_irq(0),this.cpu.device_raise_irq(0),0===this.counter_mode[0]&&(this.counter_enabled[0]=0)):this.cpu.device_lower_irq(0),this.counter_enabled[0]&&(c=(this.counter_start_value[0]-Math.floor(1193.1816666*(a-this.counter_start_time[0])))/1193.1816666));return c};jb.prototype.get_counter_value=function(a,b){if(!this.counter_enabled[a])return 0;var c=b-this.counter_start_time[a],
138
+ d=Math.floor(1193.1816666*c);b=this.counter_start_value[a]-d;C("diff="+c+" dticks="+d+" value="+b+" reload="+this.counter_reload[a],512);c=this.counter_reload[a];b>=c?(C("Warning: Counter"+a+" value "+b+" is larger than reload "+c,512),b%=c):0>b&&(b=b%c+c);return b};jb.prototype.did_rollover=function(a,b){b-=this.counter_start_time[a];return 0>b?(C("Warning: PIT timer difference is negative, resetting (timer "+a+")"),!0):this.counter_start_value[a]<Math.floor(1193.1816666*b)};jb.prototype.counter_read=
139
+ function(a){var b=this.counter_latch[a];if(b)return this.counter_latch[a]--,2===b?this.counter_latch_value[a]&255:this.counter_latch_value[a]>>8;b=this.counter_next_low[a];3===this.counter_mode[a]&&(this.counter_next_low[a]^=1);a=this.get_counter_value(a,I.microtick());return b?a&255:a>>8};jb.prototype.counter_write=function(a,b){this.counter_reload[a]=this.counter_next_low[a]?this.counter_reload[a]&-256|b:this.counter_reload[a]&255|b<<8;3===this.counter_read_mode[a]&&this.counter_next_low[a]||(this.counter_reload[a]||
140
+ (this.counter_reload[a]=65535),this.counter_start_value[a]=this.counter_reload[a],this.counter_enabled[a]=!0,this.counter_start_time[a]=I.microtick(),C("counter"+a+" reload="+m(this.counter_reload[a])+" tick="+(this.counter_reload[a]||65536)/1193.1816666+"ms",512));3===this.counter_read_mode[a]&&(this.counter_next_low[a]^=1)};jb.prototype.port43_write=function(a){var b=a>>1&7,c=a&1,d=a>>6&3;a=a>>4&3;1===d&&C("Unimplemented timer1",512);3===d?C("Unimplemented read back",512):0===a?(this.counter_latch[d]=
141
+ 2,b=this.get_counter_value(d,I.microtick()),C("latch: "+b,512),this.counter_latch_value[d]=b?b-1:0):(6<=b&&(b&=-5),C("Control: mode="+b+" ctr="+d+" read_mode="+a+" bcd="+c,512),this.counter_next_low[d]=1===a?0:1,0===d&&this.cpu.device_lower_irq(0),0!==b&&3!==b&&2!==b&&C("Unimplemented counter mode: "+m(b),512),this.counter_mode[d]=b,this.counter_read_mode[d]=a,2===d&&this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]]))};jb.prototype.dump=function(){const a=this.counter_reload[0];
142
+ C("counter0 ticks every "+(a||65536)/1193.1816666+"ms (reload="+a+")")};function kb(a){if("undefined"!==typeof window)if(window.AudioContext||window.webkitAudioContext){var b=window.AudioWorklet?lb:mb;this.bus=a;this.audio_context=window.AudioContext?new AudioContext:new webkitAudioContext;this.mixer=new nb(a,this.audio_context);this.pcspeaker=new ob(a,this.audio_context,this.mixer);this.dac=new b(a,this.audio_context,this.mixer);this.pcspeaker.start();a.register("emulator-stopped",function(){this.audio_context.suspend()},
143
+ this);a.register("emulator-started",function(){this.audio_context.resume()},this);a.register("speaker-confirm-initialized",function(){a.send("speaker-has-initialized")},this);a.send("speaker-has-initialized")}else console.warn("Web browser doesn't support Web Audio API")}kb.prototype.destroy=function(){this.audio_context&&this.audio_context.close();this.audio_context=null;this.dac&&this.dac.node_processor&&this.dac.node_processor.port.close();this.dac=null};function nb(a,b){function c(d){return function(e){d.gain.setValueAtTime(e,
144
+ this.audio_context.currentTime)}}this.audio_context=b;this.sources=new Map;this.gain_right=this.gain_left=this.volume_right=this.volume_left=this.volume_both=1;this.node_treble_left=this.audio_context.createBiquadFilter();this.node_treble_right=this.audio_context.createBiquadFilter();this.node_treble_left.type="highshelf";this.node_treble_right.type="highshelf";this.node_treble_left.frequency.setValueAtTime(2E3,this.audio_context.currentTime);this.node_treble_right.frequency.setValueAtTime(2E3,this.audio_context.currentTime);
145
+ this.node_bass_left=this.audio_context.createBiquadFilter();this.node_bass_right=this.audio_context.createBiquadFilter();this.node_bass_left.type="lowshelf";this.node_bass_right.type="lowshelf";this.node_bass_left.frequency.setValueAtTime(200,this.audio_context.currentTime);this.node_bass_right.frequency.setValueAtTime(200,this.audio_context.currentTime);this.node_gain_left=this.audio_context.createGain();this.node_gain_right=this.audio_context.createGain();this.node_merger=this.audio_context.createChannelMerger(2);
146
+ this.input_left=this.node_treble_left;this.input_right=this.node_treble_right;this.node_treble_left.connect(this.node_bass_left);this.node_bass_left.connect(this.node_gain_left);this.node_gain_left.connect(this.node_merger,0,0);this.node_treble_right.connect(this.node_bass_right);this.node_bass_right.connect(this.node_gain_right);this.node_gain_right.connect(this.node_merger,0,1);this.node_merger.connect(this.audio_context.destination);a.register("mixer-connect",function(d){this.connect_source(d[0],
147
+ d[1])},this);a.register("mixer-disconnect",function(d){this.disconnect_source(d[0],d[1])},this);a.register("mixer-volume",function(d){var e=d[0],f=d[1];d=Math.pow(10,d[2]/20);var h=0===e?this:this.sources.get(e);void 0===h?k(!1,"Mixer set volume - cannot set volume for undefined source: "+e):h.set_volume(d,f)},this);a.register("mixer-gain-left",function(d){this.gain_left=Math.pow(10,d/20);this.update()},this);a.register("mixer-gain-right",function(d){this.gain_right=Math.pow(10,d/20);this.update()},
148
+ this);a.register("mixer-treble-left",c(this.node_treble_left),this);a.register("mixer-treble-right",c(this.node_treble_right),this);a.register("mixer-bass-left",c(this.node_bass_left),this);a.register("mixer-bass-right",c(this.node_bass_right),this)}nb.prototype.add_source=function(a,b){a=new pb(this.audio_context,a,this.input_left,this.input_right);k(!this.sources.has(b),"Mixer add source - overwritting source: "+b);this.sources.set(b,a);return a};nb.prototype.connect_source=function(a,b){var c=
149
+ this.sources.get(a);void 0===c?k(!1,"Mixer connect - cannot connect undefined source: "+a):c.connect(b)};nb.prototype.disconnect_source=function(a,b){var c=this.sources.get(a);void 0===c?k(!1,"Mixer disconnect - cannot disconnect undefined source: "+a):c.disconnect(b)};nb.prototype.set_volume=function(a,b){void 0===b&&(b=2);switch(b){case 0:this.volume_left=a;break;case 1:this.volume_right=a;break;case 2:this.volume_both=a;break;default:k(!1,"Mixer set master volume - unknown channel: "+b);return}this.update()};
150
+ nb.prototype.update=function(){var a=this.volume_both*this.volume_right*this.gain_right;this.node_gain_left.gain.setValueAtTime(this.volume_both*this.volume_left*this.gain_left,this.audio_context.currentTime);this.node_gain_right.gain.setValueAtTime(a,this.audio_context.currentTime)};function pb(a,b,c,d){this.audio_context=a;this.connected_right=this.connected_left=!0;this.volume_right=this.volume_left=this.volume_both=this.gain_hidden=1;this.node_splitter=a.createChannelSplitter(2);this.node_gain_left=
151
+ a.createGain();this.node_gain_right=a.createGain();b.connect(this.node_splitter);this.node_splitter.connect(this.node_gain_left,0);this.node_gain_left.connect(c);this.node_splitter.connect(this.node_gain_right,1);this.node_gain_right.connect(d)}pb.prototype.update=function(){var a=this.connected_right*this.gain_hidden*this.volume_both*this.volume_right;this.node_gain_left.gain.setValueAtTime(this.connected_left*this.gain_hidden*this.volume_both*this.volume_left,this.audio_context.currentTime);this.node_gain_right.gain.setValueAtTime(a,
152
+ this.audio_context.currentTime)};pb.prototype.connect=function(a){var b=!a||2===a;if(b||0===a)this.connected_left=!0;if(b||1===a)this.connected_right=!0;this.update()};pb.prototype.disconnect=function(a){var b=!a||2===a;if(b||0===a)this.connected_left=!1;if(b||1===a)this.connected_right=!1;this.update()};pb.prototype.set_volume=function(a,b){void 0===b&&(b=2);switch(b){case 0:this.volume_left=a;break;case 1:this.volume_right=a;break;case 2:this.volume_both=a;break;default:k(!1,"Mixer set volume - unknown channel: "+
153
+ b);return}this.update()};pb.prototype.set_gain_hidden=function(a){this.gain_hidden=a};function ob(a,b,c){this.node_oscillator=b.createOscillator();this.node_oscillator.type="square";this.node_oscillator.frequency.setValueAtTime(440,b.currentTime);this.mixer_connection=c.add_source(this.node_oscillator,1);this.mixer_connection.disconnect();a.register("pcspeaker-enable",function(){c.connect_source(1)},this);a.register("pcspeaker-disable",function(){c.disconnect_source(1)},this);a.register("pcspeaker-update",
154
+ function(d){var e=d[1],f=0;3===d[0]&&(f=Math.min(1193181.6665999999/e,this.node_oscillator.frequency.maxValue),f=Math.max(f,0));this.node_oscillator.frequency.setValueAtTime(f,b.currentTime)},this)}ob.prototype.start=function(){this.node_oscillator.start()};function lb(a,b,c){this.bus=a;this.audio_context=b;this.enabled=!1;this.sampling_rate=48E3;b=function(){function h(n){if(0===n)return 1;n*=Math.PI;return Math.sin(n)/n}function l(){var n=Reflect.construct(AudioWorkletProcessor,[],l);n.kernel_size=
155
+ 3;n.queue_data=Array(1024);n.queue_start=0;n.queue_end=0;n.queue_length=0;n.queue_size=n.queue_data.length;n.queued_samples=0;n.source_buffer_previous=p;n.source_buffer_current=p;n.source_samples_per_destination=1;n.source_block_start=0;n.source_time=0;n.source_offset=0;n.port.onmessage=q=>{switch(q.data.type){case "queue":n.queue_push(q.data.value);break;case "sampling-rate":n.source_samples_per_destination=q.data.value/sampleRate}};return n}var p=[new Float32Array(256),new Float32Array(256)];Reflect.setPrototypeOf(l.prototype,
156
+ AudioWorkletProcessor.prototype);Reflect.setPrototypeOf(l,AudioWorkletProcessor);l.prototype.process=l.prototype.process=function(n,q){for(n=0;n<q[0][0].length;n++){for(var r=0,t=0,u=this.source_offset+this.kernel_size,y=this.source_offset-this.kernel_size+1;y<=u;y++){var A=this.source_block_start+y;r+=this.get_sample(A,0)*this.kernel(this.source_time-y);t+=this.get_sample(A,1)*this.kernel(this.source_time-y)}if(isNaN(r)||isNaN(t))r=t=0,this.dbg_log("ERROR: NaN values! Ignoring for now.");q[0][0][n]=
157
+ r;q[0][1][n]=t;this.source_time+=this.source_samples_per_destination;this.source_offset=Math.floor(this.source_time)}q=this.source_offset;q+=this.kernel_size+2;this.source_time-=this.source_offset;this.source_block_start+=this.source_offset;this.source_offset=0;this.ensure_enough_data(q);return!0};l.prototype.kernel=function(n){return h(n)*h(n/this.kernel_size)};l.prototype.get_sample=function(n,q){return 0>n?(n+=this.source_buffer_previous[0].length,this.source_buffer_previous[q][n]):this.source_buffer_current[q][n]};
158
+ l.prototype.ensure_enough_data=function(n){var q=this.source_buffer_current[0].length;q-this.source_block_start<n&&(this.prepare_next_buffer(),this.source_block_start-=q)};l.prototype.prepare_next_buffer=function(){256>this.queued_samples&&this.queue_length&&this.dbg_log("Not enough samples - should not happen during midway of playback");this.source_buffer_previous=this.source_buffer_current;this.source_buffer_current=this.queue_shift();var n=this.source_buffer_current[0].length;if(256>n){for(var q=
159
+ this.queue_start,r=0;256>n&&r<this.queue_length;)n+=this.queue_data[q][0].length,q=q+1&this.queue_size-1,r++;n=Math.max(n,256);n=[new Float32Array(n),new Float32Array(n)];n[0].set(this.source_buffer_current[0]);n[1].set(this.source_buffer_current[1]);q=this.source_buffer_current[0].length;for(var t=0;t<r;t++){var u=this.queue_shift();n[0].set(u[0],q);n[1].set(u[1],q);q+=u[0].length}this.source_buffer_current=n}this.pump()};l.prototype.pump=function(){1024>this.queued_samples/this.source_samples_per_destination&&
160
+ this.port.postMessage({type:"pump"})};l.prototype.queue_push=function(n){this.queue_length<this.queue_size&&(this.queue_data[this.queue_end]=n,this.queue_end=this.queue_end+1&this.queue_size-1,this.queue_length++,this.queued_samples+=n[0].length,this.pump())};l.prototype.queue_shift=function(){if(!this.queue_length)return p;var n=this.queue_data[this.queue_start];this.queue_data[this.queue_start]=null;this.queue_start=this.queue_start+1&this.queue_size-1;this.queue_length--;this.queued_samples-=n[0].length;
161
+ return n};l.prototype.dbg_log=function(n){this.port.postMessage({type:"debug-log",value:n})};registerProcessor("dac-processor",l)}.toString();var d=b.indexOf("{")+1,e=b.lastIndexOf("}");b=b.substring(d,e);b=new Blob(["var DEBUG = true;\n"+b],{type:"application/javascript"});var f=URL.createObjectURL(b);this.node_processor=null;this.node_output=this.audio_context.createGain();this.audio_context.audioWorklet.addModule(f).then(()=>{URL.revokeObjectURL(f);this.node_processor=new AudioWorkletNode(this.audio_context,
162
+ "dac-processor",{numberOfInputs:0,numberOfOutputs:1,outputChannelCount:[2],parameterData:{},processorOptions:{}});this.node_processor.port.postMessage({type:"sampling-rate",value:this.sampling_rate});this.node_processor.port.onmessage=h=>{switch(h.data.type){case "pump":this.pump();break;case "debug-log":C("SpeakerWorkletDAC - Worklet: "+h.data.value)}};this.node_processor.connect(this.node_output)});this.mixer_connection=c.add_source(this.node_output,2);this.mixer_connection.set_gain_hidden(3);a.register("dac-send-data",
163
+ function(h){this.queue(h)},this);a.register("dac-enable",function(){this.enabled=!0},this);a.register("dac-disable",function(){this.enabled=!1},this);a.register("dac-tell-sampling-rate",function(h){k(0<h,"Sampling rate should be nonzero");this.sampling_rate=h;this.node_processor&&this.node_processor.port.postMessage({type:"sampling-rate",value:h})},this);this.debugger=new qb(this.audio_context,this.node_output)}lb.prototype.queue=function(a){this.node_processor&&(this.debugger.push_queued_data(a),
164
+ this.node_processor.port.postMessage({type:"queue",value:a},[a[0].buffer,a[1].buffer]))};lb.prototype.pump=function(){this.enabled&&this.bus.send("dac-request-data")};function mb(a,b,c){this.bus=a;this.audio_context=b;this.enabled=!1;this.sampling_rate=22050;this.buffered_time=0;this.rate_ratio=1;this.node_lowpass=this.audio_context.createBiquadFilter();this.node_lowpass.type="lowpass";this.node_output=this.node_lowpass;this.mixer_connection=c.add_source(this.node_output,2);this.mixer_connection.set_gain_hidden(3);
165
+ a.register("dac-send-data",function(d){this.queue(d)},this);a.register("dac-enable",function(){this.enabled=!0;this.pump()},this);a.register("dac-disable",function(){this.enabled=!1},this);a.register("dac-tell-sampling-rate",function(d){k(0<d,"Sampling rate should be nonzero");this.sampling_rate=d;this.rate_ratio=Math.ceil(8E3/d);this.node_lowpass.frequency.setValueAtTime(d/2,this.audio_context.currentTime)},this);this.debugger=new qb(this.audio_context,this.node_output)}mb.prototype.queue=function(a){this.debugger.push_queued_data(a);
166
+ var b=a[0].length,c=b/this.sampling_rate;if(1<this.rate_ratio){var d=this.audio_context.createBuffer(2,b*this.rate_ratio,this.sampling_rate*this.rate_ratio);for(var e=d.getChannelData(0),f=d.getChannelData(1),h=0,l=0;l<b;l++)for(var p=0;p<this.rate_ratio;p++,h++)e[h]=a[0][l],f[h]=a[1][l]}else d=this.audio_context.createBuffer(2,b,this.sampling_rate),d.copyToChannel?(d.copyToChannel(a[0],0),d.copyToChannel(a[1],1)):(d.getChannelData(0).set(a[0]),d.getChannelData(1).set(a[1]));a=this.audio_context.createBufferSource();
167
+ a.buffer=d;a.connect(this.node_lowpass);a.addEventListener("ended",this.pump.bind(this));d=this.audio_context.currentTime;if(this.buffered_time<d)for(C("Speaker DAC - Creating/Recreating reserve - shouldn't occur frequently during playback"),this.buffered_time=d,d=.2-c,b=0;b<=d;)b+=c,this.buffered_time+=c,setTimeout(()=>this.pump(),1E3*b);a.start(this.buffered_time);this.buffered_time+=c;setTimeout(()=>this.pump(),0)};mb.prototype.pump=function(){this.enabled&&(.2<this.buffered_time-this.audio_context.currentTime||
168
+ this.bus.send("dac-request-data"))};function qb(a,b){this.audio_context=a;this.node_source=b;this.node_processor=null;this.node_gain=this.audio_context.createGain();this.node_gain.gain.setValueAtTime(0,this.audio_context.currentTime);this.node_gain.connect(this.audio_context.destination);this.is_active=!1;this.queued_history=[];this.output_history=[];this.queued=[[],[]];this.output=[[],[]]}qb.prototype.start=function(a){this.is_active=!0;this.queued=[[],[]];this.output=[[],[]];this.queued_history.push(this.queued);
169
+ this.output_history.push(this.output);this.node_processor=this.audio_context.createScriptProcessor(1024,2,2);this.node_processor.onaudioprocess=b=>{this.output[0].push(b.inputBuffer.getChannelData(0).slice());this.output[1].push(b.inputBuffer.getChannelData(1).slice())};this.node_source.connect(this.node_processor);this.node_processor.connect(this.node_gain);setTimeout(()=>{this.stop()},a)};qb.prototype.stop=function(){this.is_active=!1;this.node_source.disconnect(this.node_processor);this.node_processor.disconnect();
170
+ this.node_processor=null};qb.prototype.push_queued_data=function(a){this.is_active&&(this.queued[0].push(a[0].slice()),this.queued[1].push(a[1].slice()))};qb.prototype.download_txt=function(a,b){a=this.output_history[a][b].map(c=>c.join(" ")).join(" ");qa(a,"dacdata.txt")};qb.prototype.download_csv=function(a){a=this.output_history[a];for(var b=[],c=0;c<a[0].length;c++)for(var d=0;d<a[0][c].length;d++)b.push(`${a[0][c][d]},${a[1][c][d]}`);qa(b.join("\n"),"dacdata.csv")};function rb(a,b,c){this.bus=
171
+ b;this.socket=void 0;this.id=c||0;this.send_queue=[];this.url=a;this.reconnect_interval=1E4;this.last_connect_attempt=Date.now()-this.reconnect_interval;this.send_queue_limit=64;this.destroyed=!1;this.bus.register("net"+this.id+"-send",function(d){this.send(d)},this)}rb.prototype.handle_message=function(a){this.bus&&this.bus.send("net"+this.id+"-receive",new Uint8Array(a.data))};rb.prototype.handle_close=function(){this.destroyed||(this.connect(),setTimeout(this.connect.bind(this),this.reconnect_interval))};
172
+ rb.prototype.handle_open=function(){for(var a=0;a<this.send_queue.length;a++)this.send(this.send_queue[a]);this.send_queue=[]};rb.prototype.handle_error=function(){};rb.prototype.destroy=function(){this.destroyed=!0;this.socket&&this.socket.close()};rb.prototype.connect=function(){if("undefined"!==typeof WebSocket){if(this.socket){var a=this.socket.readyState;if(0===a||1===a)return}a=Date.now();if(!(this.last_connect_attempt+this.reconnect_interval>a)){this.last_connect_attempt=Date.now();try{this.socket=
173
+ new WebSocket(this.url)}catch(b){console.error(b);return}this.socket.binaryType="arraybuffer";this.socket.onopen=this.handle_open.bind(this);this.socket.onmessage=this.handle_message.bind(this);this.socket.onclose=this.handle_close.bind(this);this.socket.onerror=this.handle_error.bind(this)}}};rb.prototype.send=function(a){this.socket&&1===this.socket.readyState?this.socket.send(a):(this.send_queue.push(a),this.send_queue.length>2*this.send_queue_limit&&(this.send_queue=this.send_queue.slice(-this.send_queue_limit)),
174
+ this.connect())};rb.prototype.change_proxy=function(a){this.url=a;this.socket&&(this.socket.onclose=function(){},this.socket.onerror=function(){},this.socket.close(),this.socket=void 0)};const wb=(new Date("1970-01-01T00:00:00Z")).getTime(),xb=(new Date("1900-01-01T00:00:00Z")).getTime(),yb=wb-xb,zb=Math.pow(2,32),Ab=[118,56,54];function Bb(a){return[0,1,2,3,4,5].map(b=>a[b].toString(16)).map(b=>1===b.length?"0"+b:b).join(":")}function Cb(a){return a[0]<<24|a[1]<<16|a[2]<<8|a[3]}class Db{constructor(a,
175
+ b){a=Math.min(a,16);this.maximum_capacity=b?Math.max(b,a):0;this.length=this.head=this.tail=0;this.buffer=new Uint8Array(a)}write(a){const b=a.length;var c=this.length+b;let d=this.buffer.length;if(d<c){for(k(0<d);d<c;)d*=2;if(this.maximum_capacity&&d>this.maximum_capacity)throw Error("stream capacity overflow in GrowableRingbuffer.write(), package dropped");c=new Uint8Array(d);this.peek(c);this.tail=0;this.head=this.length;this.buffer=c}c=this.buffer;const e=this.head+b;if(e>d){const f=d-this.head;
176
+ c.set(a.subarray(0,f),this.head);c.set(a.subarray(f))}else c.set(a,this.head);this.head=e%d;this.length+=b}peek(a){const b=Math.min(this.length,a.length);if(b){const e=this.buffer;var c=e.length,d=this.tail+b;d>c?(d%=c,c-=this.tail,a.set(e.subarray(this.tail)),a.set(e.subarray(0,d),c)):a.set(e.subarray(this.tail,d))}return b}remove(a){a>this.length&&(a=this.length);a&&(this.tail=(this.tail+a)%this.buffer.length,this.length-=a);return a}}function Nb(){const a=new Uint8Array(1518),b=a.buffer,c=a.byteOffset;
177
+ return{eth_frame:a,eth_frame_view:new DataView(b),eth_payload_view:new DataView(b,c+14,1500),ipv4_payload_view:new DataView(b,c+34,1480),udp_payload_view:new DataView(b,c+42,1472),text_encoder:new TextEncoder}}function Ob(a,b,c,d){d.eth_frame.set(b,c.byteOffset+a);return b.length}function Pb(a,b,c,d){const e=c.byteOffset+(a&-2);d=d.eth_frame;for(c=c.byteOffset;c<e;c+=2)b+=d[c]<<8|d[c+1];for(a&1&&(b+=d[e]<<8);b>>>16;)b=(b&65535)+(b>>>16);return~b&65535}function Qb(a,b){k(b.eth);a.eth_frame.fill(0);
178
+ var c=a.eth_frame,d=c.subarray,e=a.eth_frame_view;Ob(0,b.eth.dest,e,a);Ob(6,b.eth.src,e,a);e.setUint16(12,b.eth.ethertype);e=14;if(b.arp){var f=a.eth_payload_view;f.setUint16(0,b.arp.htype);f.setUint16(2,b.arp.ptype);f.setUint8(4,b.arp.sha.length);f.setUint8(5,b.arp.spa.length);f.setUint16(6,b.arp.oper);Ob(8,b.arp.sha,f,a);Ob(14,b.arp.spa,f,a);Ob(18,b.arp.tha,f,a);Ob(24,b.arp.tpa,f,a);e+=28}else if(b.ipv4){f=a.eth_payload_view;var h=20;if(b.icmp){var l=a.ipv4_payload_view;l.setUint8(0,b.icmp.type);
179
+ l.setUint8(1,b.icmp.code);l.setUint16(2,0);var p=4+Ob(4,b.icmp.data,l,a);l.setUint16(2,Pb(p,0,l,a));h+=p}else if(b.udp){l=a.ipv4_payload_view;var n=8;if(b.dhcp){p=n;var q=a.udp_payload_view;q.setUint8(0,b.dhcp.op);q.setUint8(1,b.dhcp.htype);q.setUint8(2,b.dhcp.hlen);q.setUint8(3,b.dhcp.hops);q.setUint32(4,b.dhcp.xid);q.setUint16(8,b.dhcp.secs);q.setUint16(10,b.dhcp.flags);q.setUint32(12,b.dhcp.ciaddr);q.setUint32(16,b.dhcp.yiaddr);q.setUint32(20,b.dhcp.siaddr);q.setUint32(24,b.dhcp.giaddr);Ob(28,
180
+ b.dhcp.chaddr,q,a);q.setUint32(236,1669485411);n=240;for(var r of b.dhcp.options)n+=Ob(n,r,q,a);p+=n}else if(b.dns){r=n;n=a.udp_payload_view;n.setUint16(0,b.dns.id);n.setUint16(2,b.dns.flags);n.setUint16(4,b.dns.questions.length);n.setUint16(6,b.dns.answers.length);let y=12;for(var t=0;t<b.dns.questions.length;++t){var u=b.dns.questions[t];for(q of u.name){const A=a.text_encoder.encodeInto(q,a.eth_frame.subarray(n.byteOffset+(y+1))).written;n.setUint8(y,A);y+=1+A}n.setUint16(y,u.type);y+=2;n.setUint16(y,
181
+ u.class);y+=2}for(t=0;t<b.dns.answers.length;++t){q=b.dns.answers[t];for(p of q.name)u=a.text_encoder.encodeInto(p,a.eth_frame.subarray(n.byteOffset+(y+1))).written,n.setUint8(y,u),y+=1+u;n.setUint16(y,q.type);y+=2;n.setUint16(y,q.class);y+=2;n.setUint32(y,q.ttl);y+=4;n.setUint16(y,q.data.length);y+=2;y+=Ob(y,q.data,n,a)}p=r+y}else b.ntp?(p=n,q=a.udp_payload_view,q.setUint8(0,b.ntp.flags),q.setUint8(1,b.ntp.stratum),q.setUint8(2,b.ntp.poll),q.setUint8(3,b.ntp.precision),q.setUint32(4,b.ntp.root_delay),
182
+ q.setUint32(8,b.ntp.root_disp),q.setUint32(12,b.ntp.ref_id),q.setUint32(16,b.ntp.ref_ts_i),q.setUint32(20,b.ntp.ref_ts_f),q.setUint32(24,b.ntp.ori_ts_i),q.setUint32(28,b.ntp.ori_ts_f),q.setUint32(32,b.ntp.rec_ts_i),q.setUint32(36,b.ntp.rec_ts_f),q.setUint32(40,b.ntp.trans_ts_i),q.setUint32(44,b.ntp.trans_ts_f),p+=48):p=n+Ob(0,b.udp.data,a.udp_payload_view,a);n=p;l.setUint16(0,b.udp.sport);l.setUint16(2,b.udp.dport);l.setUint16(4,n);l.setUint16(6,0);l.setUint16(6,Pb(n,(b.ipv4.src[0]<<8|b.ipv4.src[1])+
183
+ (b.ipv4.src[2]<<8|b.ipv4.src[3])+(b.ipv4.dest[0]<<8|b.ipv4.dest[1])+(b.ipv4.dest[2]<<8|b.ipv4.dest[3])+17+n,l,a));h+=n}else b.tcp&&(l=a.ipv4_payload_view,p=0,q=b.tcp,q.fin&&(p|=1),q.syn&&(p|=2),q.rst&&(p|=4),q.psh&&(p|=8),q.ack&&(p|=16),q.urg&&(p|=32),q.ece&&(p|=64),q.cwr&&(p|=128),l.setUint16(0,q.sport),l.setUint16(2,q.dport),l.setUint32(4,q.seq),l.setUint32(8,q.ackn),l.setUint8(12,80),l.setUint8(13,p),l.setUint16(14,q.winsize),l.setUint16(16,0),l.setUint16(18,q.urgent||0),p=20,b.tcp_data&&(p+=Ob(20,
184
+ b.tcp_data,l,a)),l.setUint16(16,Pb(p,(b.ipv4.src[0]<<8|b.ipv4.src[1])+(b.ipv4.src[2]<<8|b.ipv4.src[3])+(b.ipv4.dest[0]<<8|b.ipv4.dest[1])+(b.ipv4.dest[2]<<8|b.ipv4.dest[3])+6+p,l,a)),h+=p);f.setUint8(0,69);f.setUint8(1,b.ipv4.tos||0);f.setUint16(2,h);f.setUint16(4,b.ipv4.id||0);f.setUint8(6,64);f.setUint8(8,b.ipv4.ttl||32);f.setUint8(9,b.ipv4.proto);f.setUint16(10,0);Ob(12,b.ipv4.src,f,a);Ob(16,b.ipv4.dest,f,a);f.setUint16(10,Pb(20,0,f,a));e+=h}return d.call(c,0,e)}function Rb(a,b){fetch(`https://${b.doh_server||
185
+ "cloudflare-dns.com"}/dns-query`,{method:"POST",headers:[["content-type","application/dns-message"]],body:a.udp.data}).then(async c=>{c={eth:{ethertype:2048,src:b.router_mac,dest:a.eth.src},ipv4:{proto:17,src:b.router_ip,dest:a.ipv4.src},udp:{sport:53,dport:a.udp.sport,data:new Uint8Array(await c.arrayBuffer())}};b.receive(Qb(b.eth_encoder_buf,c))});return!0}function Sb(a,b){let c={};c.eth={ethertype:2048,src:b.router_mac,dest:a.eth.src};c.ipv4={proto:17,src:b.router_ip,dest:b.vm_ip};c.udp={sport:67,
186
+ dport:68};c.dhcp={htype:1,hlen:6,hops:0,xid:a.dhcp.xid,secs:0,flags:0,ciaddr:0,yiaddr:Cb(b.vm_ip),siaddr:Cb(b.router_ip),giaddr:Cb(b.router_ip),chaddr:a.dhcp.chaddr};let d=[],e=a.dhcp.options.find(function(f){return 53===f[0]});e&&3===e[2]&&(a.dhcp.op=3);1===a.dhcp.op&&(c.dhcp.op=2,d.push(new Uint8Array([53,1,2])));3===a.dhcp.op&&(c.dhcp.op=2,d.push(new Uint8Array([53,1,5])),d.push(new Uint8Array([51,4,8,0,0,0])));a=[b.router_ip[0],b.router_ip[1],b.router_ip[2],b.router_ip[3]];d.push(new Uint8Array([1,
187
+ 4,255,255,255,0]));b.masquerade&&(d.push(new Uint8Array([3,4].concat(a))),d.push(new Uint8Array([6,4].concat(a))));d.push(new Uint8Array([54,4].concat(a)));d.push(new Uint8Array([60,3].concat(Ab)));d.push(new Uint8Array([255,0]));c.dhcp.options=d;b.receive(Qb(b.eth_encoder_buf,c))}function Tb(a,b){let c={};var d=(new DataView(a.buffer,a.byteOffset,a.byteLength)).getUint16(12),e={ethertype:d,dest:a.subarray(0,6),dest_s:Bb(a.subarray(0,6)),src:a.subarray(6,12),src_s:Bb(a.subarray(6,12))};c.eth=e;a=
188
+ a.subarray(14,a.length);if(2048===d){var f=new DataView(a.buffer,a.byteOffset,a.byteLength),h=a[0]>>4&15;e=a[0]&15;var l=f.getUint8(1),p=f.getUint16(2);let n=f.getUint8(8);d=f.getUint8(9);f=f.getUint16(10);h={version:h,ihl:e,tos:l,len:p,ttl:n,proto:d,ip_checksum:f,src:a.subarray(12,16),dest:a.subarray(16,20)};Math.max(p,46)!==a.length&&C(`ipv4 Length mismatch: ${p} != ${a.length}`,16777216);c.ipv4=h;e=a.subarray(4*e,p);if(1===d)a=new DataView(e.buffer,e.byteOffset,e.byteLength),a={type:a.getUint8(0),
189
+ code:a.getUint8(1),checksum:a.getUint16(2),data:e.subarray(4)},c.icmp=a;else if(6===d)d=new DataView(e.buffer,e.byteOffset,e.byteLength),a={sport:d.getUint16(0),dport:d.getUint16(2),seq:d.getUint32(4),ackn:d.getUint32(8),doff:d.getUint8(12)>>4,winsize:d.getUint16(14),checksum:d.getUint16(16),urgent:d.getUint16(18)},d=d.getUint8(13),a.fin=!!(d&1),a.syn=!!(d&2),a.rst=!!(d&4),a.psh=!!(d&8),a.ack=!!(d&16),a.urg=!!(d&32),a.ece=!!(d&64),a.cwr=!!(d&128),c.tcp=a,c.tcp_data=e.subarray(4*a.doff);else if(17===
190
+ d){a=new DataView(e.buffer,e.byteOffset,e.byteLength);a={sport:a.getUint16(0),dport:a.getUint16(2),len:a.getUint16(4),checksum:a.getUint16(6),data:e.subarray(8),data_s:(new TextDecoder).decode(e.subarray(8))};if(67===a.dport||67===a.sport){e=e.subarray(8);d=new DataView(e.buffer,e.byteOffset,e.byteLength);e.subarray(44,236);d={op:d.getUint8(0),htype:d.getUint8(1),hlen:d.getUint8(2),hops:d.getUint8(3),xid:d.getUint32(4),secs:d.getUint16(8),flags:d.getUint16(10),ciaddr:d.getUint32(12),yiaddr:d.getUint32(16),
191
+ siaddr:d.getUint32(20),giaddr:d.getUint32(24),chaddr:e.subarray(28,44),magic:d.getUint32(236),options:[]};e=e.subarray(240);for(p=0;p<e.length;++p)h=p,0!==e[p]&&(++p,l=e[p],p+=l,d.options.push(e.subarray(h,h+l+2)));c.dhcp=d;c.dhcp_options=d.options}else 53===a.dport||53===a.sport?Ub(e.subarray(8),c):123===a.dport&&(d=e.subarray(8),d=new DataView(d.buffer,d.byteOffset,d.byteLength),c.ntp={flags:d.getUint8(0),stratum:d.getUint8(1),poll:d.getUint8(2),precision:d.getUint8(3),root_delay:d.getUint32(4),
192
+ root_disp:d.getUint32(8),ref_id:d.getUint32(12),ref_ts_i:d.getUint32(16),ref_ts_f:d.getUint32(20),ori_ts_i:d.getUint32(24),ori_ts_f:d.getUint32(28),rec_ts_i:d.getUint32(32),rec_ts_f:d.getUint32(36),trans_ts_i:d.getUint32(40),trans_ts_f:d.getUint32(44)});c.udp=a}}else 2054===d?(d=new DataView(a.buffer,a.byteOffset,a.byteLength),a={htype:d.getUint16(0),ptype:d.getUint16(2),oper:d.getUint16(6),sha:a.subarray(8,14),spa:a.subarray(14,18),tha:a.subarray(18,24),tpa:a.subarray(24,28)},c.arp=a):34525===d?
193
+ C("Unimplemented: ipv6"):C("Unknown ethertype: "+m(d),16777216);if(c.ipv4)if(c.tcp)a:{a=`${c.ipv4.src.join(".")}:${c.tcp.sport}:${c.ipv4.dest.join(".")}:${c.tcp.dport}`;if(c.tcp.syn&&(b.tcp_conn[a]&&C("SYN to already opened port",16777216),b.on_tcp_connection(c,a)))break a;if(b.tcp_conn[a])b.tcp_conn[a].process(c);else{C(`I dont know about ${a}, so resetting`,16777216);a=c.tcp.ackn;if(c.tcp.fin||c.tcp.syn)a+=1;d={};d.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src};d.ipv4={proto:6,src:c.ipv4.dest,
194
+ dest:c.ipv4.src};d.tcp={sport:c.tcp.dport,dport:c.tcp.sport,seq:a,ackn:c.tcp.seq+(c.tcp.syn?1:0),winsize:c.tcp.winsize,rst:!0,ack:c.tcp.syn};b.receive(Qb(b.eth_encoder_buf,d))}}else if(c.udp)if(c.dns)if("static"===b.dns_method){a={};a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src};a.ipv4={proto:17,src:b.router_ip,dest:c.ipv4.src};a.udp={sport:53,dport:c.udp.sport};d=[];for(e=0;e<c.dns.questions.length;++e)switch(p=c.dns.questions[e],p.type){case 1:d.push({name:p.name,type:p.type,class:p.class,
195
+ ttl:600,data:[192,168,87,1]})}a.dns={id:c.dns.id,flags:33152,questions:c.dns.questions,answers:d};b.receive(Qb(b.eth_encoder_buf,a))}else Rb(c,b);else c.dhcp?Sb(c,b):c.ntp?(a=Date.now()+yb,d=a%1E3/1E3*zb,e={},e.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},e.ipv4={proto:17,src:c.ipv4.dest,dest:c.ipv4.src},e.udp={sport:123,dport:c.udp.sport},e.ntp=Object.assign({},c.ntp),e.ntp.flags=36,e.ntp.poll=10,e.ntp.ori_ts_i=c.ntp.trans_ts_i,e.ntp.ori_ts_f=c.ntp.trans_ts_f,e.ntp.rec_ts_i=a/1E3,e.ntp.rec_ts_f=
196
+ d,e.ntp.trans_ts_i=a/1E3,e.ntp.trans_ts_f=d,e.ntp.stratum=2,b.receive(Qb(b.eth_encoder_buf,e))):8===c.udp.dport&&(a={},a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},a.ipv4={proto:17,src:c.ipv4.dest,dest:c.ipv4.src},a.udp={sport:c.udp.dport,dport:c.udp.sport,data:(new TextEncoder).encode(c.udp.data_s)},b.receive(Qb(b.eth_encoder_buf,a)));else c.icmp&&8===c.icmp.type&&(a={},a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},a.ipv4={proto:1,src:c.ipv4.dest,dest:c.ipv4.src},a.icmp={type:0,
197
+ code:c.icmp.code,data:c.icmp.data},b.receive(Qb(b.eth_encoder_buf,a)));else c.arp&&1===c.arp.oper&&2048===c.arp.ptype&&(a=Cb(c.arp.tpa)&4294967040,d=Cb(b.router_ip)&4294967040,!b.masquerade&&a!==d||a===d&&99<c.arp.tpa[3]||(a={},a.eth={ethertype:2054,src:b.router_mac,dest:c.eth.src},a.arp={htype:1,ptype:2048,oper:2,sha:b.router_mac,spa:c.arp.tpa,tha:c.eth.src,tpa:c.arp.spa},b.receive(Qb(b.eth_encoder_buf,a))))}function Ub(a,b){function c(){let n=[],q;do q=d.getUint8(l),n.push((new TextDecoder).decode(a.subarray(l+
198
+ 1,l+1+q))),l+=q+1;while(0<q);return n}let d=new DataView(a.buffer,a.byteOffset,a.byteLength),e={id:d.getUint16(0),flags:d.getUint16(2),questions:[],answers:[]};var f=d.getUint16(4);let h=d.getUint16(6);d.getUint16(8);d.getUint16(10);let l=12;for(var p=0;p<f;p++)e.questions.push({name:c(),type:d.getInt16(l),class:d.getInt16(l+2)}),l+=4;for(f=0;f<h;f++){p={name:c(),type:d.getInt16(l),class:d.getUint16(l+2),ttl:d.getUint32(l+4)};l+=8;let n=d.getUint16(l);l+=2;p.data=a.subarray(l,l+n);l+=n;e.answers.push(p)}b.dns=
199
+ e}function Vb(a,b){var c=b.vm_ip.join(".");const d=b.router_ip.join("."),e=16383*Math.random()|0;let f,h,l=0;do f=49152+(e+l)%16383,h=`${c}:${a}:${d}:${f}`;while(16383>++l&&b.tcp_conn[h]);if(b.tcp_conn[h])throw Error("pool of dynamic TCP port numbers exhausted, connection aborted");c=new Wb;c.tuple=h;c.hsrc=b.router_mac;c.psrc=b.router_ip;c.sport=f;c.hdest=b.vm_mac;c.dport=a;c.pdest=b.vm_ip;c.net=b;b.tcp_conn[h]=c;c.connect();return c}function Xb(a,b){return new Promise(c=>{let d=Vb(a,b);d.state=
200
+ "syn-probe";d.on("probe",c)})}function Wb(){this.state="closed";this.net=null;this.send_buffer=new Db(2048,0);this.send_chunk_buf=new Uint8Array(1460);this.delayed_send_fin=this.in_active_close=!1;this.delayed_state=void 0;this.events_handlers={}}Wb.prototype.on=function(a,b){this.events_handlers[a]=b};Wb.prototype.emit=function(a,...b){this.events_handlers[a]&&this.events_handlers[a].apply(this,b)};Wb.prototype.ipv4_reply=function(){let a={};a.eth={ethertype:2048,src:this.hsrc,dest:this.hdest};a.ipv4=
201
+ {proto:6,src:this.psrc,dest:this.pdest};a.tcp={sport:this.sport,dport:this.dport,winsize:this.winsize,ackn:this.ack,seq:this.seq,ack:!0};return a};Wb.prototype.packet_reply=function(a,b){a={sport:a.tcp.dport,dport:a.tcp.sport,winsize:a.tcp.winsize,ackn:this.ack,seq:this.seq};if(b)for(const c in b)a[c]=b[c];b=this.ipv4_reply();b.tcp=a;return b};Wb.prototype.connect=function(){this.seq=1338;this.ack=1;this.start_seq=0;this.winsize=64240;this.state="syn-sent";let a=this.ipv4_reply();a.ipv4.id=2345;a.tcp=
202
+ {sport:this.sport,dport:this.dport,seq:1337,ackn:0,winsize:0,syn:!0};this.net.receive(Qb(this.net.eth_encoder_buf,a))};Wb.prototype.accept=function(a){this.seq=1338;this.ack=a.tcp.seq+1;this.start_seq=a.tcp.seq;this.hsrc=this.net.router_mac;this.psrc=a.ipv4.dest;this.sport=a.tcp.dport;this.hdest=a.eth.src;this.dport=a.tcp.sport;this.pdest=a.ipv4.src;this.winsize=a.tcp.winsize;let b=this.ipv4_reply();b.tcp={sport:this.sport,dport:this.dport,seq:1337,ackn:this.ack,winsize:a.tcp.winsize,syn:!0,ack:!0};
203
+ this.state="established";this.net.receive(Qb(this.net.eth_encoder_buf,b))};Wb.prototype.process=function(a){if("closed"===this.state)a=this.packet_reply(a,{rst:!0}),this.net.receive(Qb(this.net.eth_encoder_buf,a));else if(a.tcp.rst){if("syn-probe"===this.state)this.emit("probe",!1);else this.on_close();this.release()}else if(a.tcp.syn){if("syn-sent"===this.state&&a.tcp.ack){this.ack=a.tcp.seq+1;this.start_seq=a.tcp.seq;this.last_received_ackn=a.tcp.ackn;var b=this.ipv4_reply();this.net.receive(Qb(this.net.eth_encoder_buf,
204
+ b));this.state="established";this.emit("connect")}else"syn-probe"===this.state&&a.tcp.ack?(this.emit("probe",!0),b=this.packet_reply(a,{rst:!0}),this.net.receive(Qb(this.net.eth_encoder_buf,b)),this.release()):C(`TCP[${this.tuple}]: WARNING: unexpected SYN packet dropped`,16777216);a.tcp_data.length&&C(`TCP[${this.tuple}]: WARNING: ${a.tcp_data.length} bytes of unexpected SYN packet payload dropped`,16777216)}else{if(a.tcp.ack)if("syn-received"===this.state)this.state="established";else if("fin-wait-1"===
205
+ this.state)a.tcp.fin||(this.state="fin-wait-2");else if("closing"===this.state||"last-ack"===this.state){this.release();return}if(void 0===this.last_received_ackn)this.last_received_ackn=a.tcp.ackn;else if(b=a.tcp.ackn-this.last_received_ackn,0<b){if(this.last_received_ackn=a.tcp.ackn,this.send_buffer.remove(b),this.seq+=b,this.pending=!1,this.delayed_send_fin&&!this.send_buffer.length){this.delayed_send_fin=!1;this.state=this.delayed_state;a=this.ipv4_reply();a.tcp.fin=!0;this.net.receive(Qb(this.net.eth_encoder_buf,
206
+ a));return}}else if(0>b){C(`TCP[${this.tuple}]: ERROR: ack underflow (pkt=${a.tcp.ackn} last=${this.last_received_ackn}), resetting`,16777216);a=this.packet_reply(a,{rst:!0});this.net.receive(Qb(this.net.eth_encoder_buf,a));this.on_close();this.release();return}a.tcp.fin?(this.ack!==a.tcp.seq&&C(`TCP[${this.tuple}]: WARNING: closing connection in state "${this.state}" with invalid seq (${this.ack} != ${a.tcp.seq})`,16777216),++this.ack,b=this.packet_reply(a,{}),"established"===this.state?(b.tcp.ack=
207
+ !0,this.state="close-wait",this.on_shutdown()):"fin-wait-1"===this.state?(a.tcp.ack?this.release():this.state="closing",b.tcp.ack=!0):"fin-wait-2"===this.state?(this.release(),b.tcp.ack=!0):(this.release(),this.on_close(),b.tcp.rst=!0),this.net.receive(Qb(this.net.eth_encoder_buf,b))):this.ack!==a.tcp.seq?(this.ack!==a.tcp.seq+1&&C(`Packet seq was wrong ex: ${this.ack} ~${this.ack-this.start_seq} `+`pk: ${a.tcp.seq} ~${this.start_seq-a.tcp.seq} `+`(${this.ack-a.tcp.seq}) = ${this.name}`,16777216),
208
+ a=this.packet_reply(a,{ack:!0}),this.net.receive(Qb(this.net.eth_encoder_buf,a))):a.tcp.ack&&0<a.tcp_data.length&&(this.ack+=a.tcp_data.length,b=this.ipv4_reply(),this.net.receive(Qb(this.net.eth_encoder_buf,b)),this.emit("data",a.tcp_data));this.pump()}};Wb.prototype.write=function(a){this.in_active_close||this.send_buffer.write(a);this.pump()};Wb.prototype.writev=function(a){if(!this.in_active_close)for(const b of a)this.send_buffer.write(b);this.pump()};Wb.prototype.close=function(){if(!this.in_active_close){this.in_active_close=
209
+ !0;if("established"===this.state||"syn-received"===this.state)var a="fin-wait-1";else if("close-wait"===this.state)a="last-ack";else{"syn-sent"!==this.state&&C(`TCP[${this.tuple}]: active close in unexpected state "${this.state}"`,16777216);this.release();return}this.send_buffer.length||this.pending?(this.delayed_send_fin=!0,this.delayed_state=a):(this.state=a,a=this.ipv4_reply(),a.tcp.fin=!0,this.net.receive(Qb(this.net.eth_encoder_buf,a)))}this.pump()};Wb.prototype.on_shutdown=function(){this.emit("shutdown")};
210
+ Wb.prototype.on_close=function(){this.emit("close")};Wb.prototype.release=function(){this.net.tcp_conn[this.tuple]&&(this.state="closed",delete this.net.tcp_conn[this.tuple])};Wb.prototype.pump=function(){if(this.send_buffer.length&&!this.pending){const a=this.send_chunk_buf,b=this.send_buffer.peek(a),c=this.ipv4_reply();c.tcp.psh=!0;c.tcp_data=a.subarray(0,b);this.net.receive(Qb(this.net.eth_encoder_buf,c));this.pending=!0}};function Yb(a,b){b=b||{};this.bus=a;this.id=b.id||0;this.router_mac=new Uint8Array((b.router_mac||
211
+ "52:54:0:1:2:3").split(":").map(function(c){return parseInt(c,16)}));this.router_ip=new Uint8Array((b.router_ip||"192.168.86.1").split(".").map(function(c){return parseInt(c,10)}));this.vm_ip=new Uint8Array((b.vm_ip||"192.168.86.100").split(".").map(function(c){return parseInt(c,10)}));this.masquerade=void 0===b.masquerade||!!b.masquerade;this.vm_mac=new Uint8Array(6);this.dns_method=b.dns_method||"static";this.doh_server=b.doh_server;this.tcp_conn={};this.eth_encoder_buf=Nb();this.fetch=(...c)=>
212
+ fetch(...c);this.cors_proxy=b.cors_proxy;this.bus.register("net"+this.id+"-mac",function(c){this.vm_mac=new Uint8Array(c.split(":").map(function(d){return parseInt(d,16)}))},this);this.bus.register("net"+this.id+"-send",function(c){this.send(c)},this)}Yb.prototype.destroy=function(){};Yb.prototype.on_tcp_connection=function(a,b){if(80===a.tcp.dport){let c=new Wb;c.state="syn-received";c.net=this;c.on("data",ec);c.tuple=b;c.accept(a);this.tcp_conn[b]=c;return!0}return!1};Yb.prototype.connect=function(a){return Vb(a,
213
+ this)};Yb.prototype.tcp_probe=function(a){return Xb(a,this)};async function ec(a){this.read=this.read||"";if((this.read+=(new TextDecoder).decode(a))&&-1!==this.read.indexOf("\r\n\r\n")){var b=this.read.indexOf("\r\n\r\n");a=this.read.substring(0,b).split(/\r\n/);b=this.read.substring(b+4);this.read="";let c=a[0].split(" "),d;d=/^https?:/.test(c[1])?new URL(c[1]):new URL("http://host"+c[1]);"undefined"!==typeof window&&"http:"===d.protocol&&"https:"===window.location.protocol&&(d.protocol="https:");
214
+ let e=new Headers;for(let p=1;p<a.length;++p){const n=this.net.parse_http_header(a[p]);if(!n){console.warn('The request contains an invalid header: "%s"',a[p]);this.write((new TextEncoder).encode("HTTP/1.1 400 Bad Request\r\nContent-Length: 0"));return}"host"===n.key.toLowerCase()?d.host=n.value:e.append(n.key,n.value)}C("HTTP Dispatch: "+d.href,16777216);this.name=d.href;a={method:c[0],headers:e};-1!==["put","post"].indexOf(a.method.toLowerCase())&&(a.body=b);const f=this.net.cors_proxy?this.net.cors_proxy+
215
+ encodeURIComponent(d.href):d.href,h=new TextEncoder;let l=!1;this.net.fetch(f,a).then(p=>{const n=[`HTTP/1.1 ${p.status} ${p.statusText}`,`x-was-fetch-redirected: ${!!p.redirected}`,`x-fetch-resp-url: ${p.url}`,"Connection: closed"];for(const [q,r]of p.headers.entries())["content-encoding","connection","content-length","transfer-encoding"].includes(q.toLowerCase())||n.push(`${q}: ${r}`);this.write(h.encode(n.join("\r\n")+"\r\n\r\n"));l=!0;if(p.body&&p.body.getReader){const q=p.body.getReader(),r=
216
+ ({value:t,done:u})=>{t&&this.write(t);if(u)this.close();else return q.read().then(r)};q.read().then(r)}else p.arrayBuffer().then(q=>{this.write(new Uint8Array(q));this.close()})}).catch(p=>{console.warn("Fetch Failed: "+f+"\n"+p);l||(p=h.encode(`Fetch ${f} failed:\n\n${p.stack||p.message}`),this.writev([h.encode(["HTTP/1.1 502 Fetch Error","Content-Type: text/plain",`Content-Length: ${p.length}`,"Connection: closed"].join("\r\n")+"\r\n\r\n"),p]));this.close()})}}Yb.prototype.fetch=async function(a,
217
+ b){this.cors_proxy&&(a=this.cors_proxy+encodeURIComponent(a));try{const c=await fetch(a,b),d=await c.arrayBuffer();return[c,d]}catch(c){return console.warn("Fetch Failed: "+a+"\n"+c),b=new Headers,b.set("Content-Type","text/plain"),[{status:502,statusText:"Fetch Error",headers:b},(new TextEncoder).encode(`Fetch ${a} failed:\n\n${c.stack}`).buffer]}};Yb.prototype.parse_http_header=function(a){var b=a.match(/^([^:]*):(.*)$/);if(b)if(a=b[1],b=b[2].trim(),0===a.length)C("Header key is empty, raw header",
218
+ 16777216);else if(0===b.length)C("Header value is empty",16777216);else if(/^[\w-]+$/.test(a)){if(/^[\x20-\x7E]+$/.test(b))return{key:a,value:b};C("Header value contains forbidden characters",16777216)}else C("Header key contains forbidden characters",16777216);else C("Unable to parse HTTP header",16777216)};Yb.prototype.send=function(a){Tb(a,this)};Yb.prototype.receive=function(a){this.bus.send("net"+this.id+"-receive",new Uint8Array(a))};function fc(a,b,c){this.register_ws(a);this.last_stream=1;
219
+ this.connections={0:{congestion:0}};this.congested_buffer=[];c=c||{};this.bus=b;this.id=c.id||0;this.router_mac=new Uint8Array((c.router_mac||"52:54:0:1:2:3").split(":").map(function(d){return parseInt(d,16)}));this.router_ip=new Uint8Array((c.router_ip||"192.168.86.1").split(".").map(function(d){return parseInt(d,10)}));this.vm_ip=new Uint8Array((c.vm_ip||"192.168.86.100").split(".").map(function(d){return parseInt(d,10)}));this.masquerade=void 0===c.masquerade||!!c.masquerade;this.vm_mac=new Uint8Array(6);
220
+ this.dns_method=c.dns_method||"doh";this.doh_server=c.doh_server;this.tcp_conn={};this.eth_encoder_buf=Nb();this.bus.register("net"+this.id+"-mac",function(d){this.vm_mac=new Uint8Array(d.split(":").map(function(e){return parseInt(e,16)}))},this);this.bus.register("net"+this.id+"-send",function(d){this.send(d)},this)}fc.prototype.register_ws=function(a){this.wispws=new WebSocket(a.replace("wisp://","ws://").replace("wisps://","wss://"));this.wispws.binaryType="arraybuffer";this.wispws.onmessage=b=>
221
+ {this.process_incoming_wisp_frame(new Uint8Array(b.data))};this.wispws.onclose=()=>{setTimeout(()=>{this.register_ws(a)},1E4)}};fc.prototype.send_packet=function(a,b,c){this.connections[c]&&(0<this.connections[c].congestion?("DATA"===b&&this.connections[c].congestion--,this.wispws.send(a)):(this.connections[c].congested=!0,this.congested_buffer.push({data:a,type:b})))};fc.prototype.process_incoming_wisp_frame=function(a){const b=new DataView(a.buffer),c=b.getUint32(1,!0);switch(a[0]){case 1:C("Server sent client-only packet CONNECT",
222
+ 1048576);break;case 2:if(this.connections[c])this.connections[c].data_callback(a.slice(5));else throw Error("Got a DATA packet but stream not registered. ID: "+c);break;case 3:this.connections[c]&&(this.connections[c].congestion=b.getUint32(5,!0));if(this.connections[c].congested){for(const d of this.congested_buffer)this.send_packet(d.data,d.type,c);this.connections[c].congested=!1}break;case 4:this.connections[c]&&this.connections[c].close_callback(b.getUint8(5));delete this.connections[c];break;
223
+ case 5:C("got a wisp V2 upgrade request, ignoring",1048576);break;default:C("Wisp server returned unknown packet: "+a[0],1048576)}};fc.prototype.send_wisp_frame=function(a){let b,c;switch(a.type){case "CONNECT":const d=(new TextEncoder).encode(a.hostname);b=new Uint8Array(8+d.length);c=new DataView(b.buffer);c.setUint8(0,1);c.setUint32(1,a.stream_id,!0);c.setUint8(5,1);c.setUint16(6,a.port,!0);b.set(d,8);this.connections[a.stream_id]={data_callback:a.data_callback,close_callback:a.close_callback,
224
+ congestion:this.connections[0].congestion};break;case "DATA":b=new Uint8Array(5+a.data.length);c=new DataView(b.buffer);c.setUint8(0,2);c.setUint32(1,a.stream_id,!0);b.set(a.data,5);break;case "CLOSE":b=new Uint8Array(6);c=new DataView(b.buffer);c.setUint8(0,4);c.setUint32(1,a.stream_id,!0);c.setUint8(5,a.reason);break;default:C("Client tried to send unknown packet: "+a.type,1048576)}this.send_packet(b,a.type,a.stream_id)};fc.prototype.destroy=function(){this.wispws&&(this.wispws.onmessage=null,this.wispws.onclose=
225
+ null,this.wispws.close(),this.wispws=null)};fc.prototype.on_tcp_connection=function(a,b){let c=new Wb;c.state="syn-received";c.net=this;c.tuple=b;c.stream_id=this.last_stream++;this.tcp_conn[b]=c;c.on("data",d=>{0!==d.length&&this.send_wisp_frame({type:"DATA",stream_id:c.stream_id,data:d})});c.on_close=()=>{this.send_wisp_frame({type:"CLOSE",stream_id:c.stream_id,reason:2})};c.on_shutdown=c.on_close;this.send_wisp_frame({type:"CONNECT",stream_id:c.stream_id,hostname:a.ipv4.dest.join("."),port:a.tcp.dport,
226
+ data_callback:d=>{c.write(d)},close_callback:()=>{c.close()}});c.accept(a);return!0};fc.prototype.send=function(a){Tb(a,this)};fc.prototype.receive=function(a){this.bus.send("net"+this.id+"-receive",new Uint8Array(a))};const gc="undefined"!==typeof window&&0<=window.navigator.platform.toString().toLowerCase().search("win");function hc(a){function b(w){return w.shiftKey&&w.ctrlKey&&(73===w.keyCode||74===w.keyCode||75===w.keyCode)||!u.emu_enabled?!1:w.target?w.target.classList.contains("phone_keyboard")||
227
+ "INPUT"!==w.target.nodeName&&"TEXTAREA"!==w.target.nodeName:!0}function c(w){!w.altKey&&n[56]&&l(56,!1);return f(w,!1)}function d(w){!w.altKey&&n[56]&&l(56,!1);return f(w,!0)}function e(){for(var w=Object.keys(n),F,J=0;J<w.length;J++)F=+w[J],n[F]&&l(F,!1);n={}}function f(w,F){if(u.bus&&b(w)){w.preventDefault&&w.preventDefault();if(gc&&(q&&(clearTimeout(t),w.getModifierState&&w.getModifierState("AltGraph")&&r===F&&"ControlLeft"===q.code&&"AltRight"===w.code||h(q,r),q=null),"ControlLeft"===w.code))return q=
228
+ w,r=F,t=setTimeout(()=>{h(q,r);q=null},10),!1;h(w,F);return!1}}function h(w,F){a:{if(void 0!==w.code){var J=B[w.code];if(void 0!==J)break a}J=y[w.keyCode]}J?l(J,F,w.repeat):console.log("Missing char in map: keyCode="+(w.keyCode||-1).toString(16)+" code="+w.code)}function l(w,F,J){if(F)n[w]&&!J&&l(w,!1);else if(!n[w])return;(n[w]=F)||(w|=128);255<w?(p(w>>8),p(w&255)):p(w)}function p(w){u.bus.send("keyboard-code",w)}var n={},q=null,r=!1,t=0,u=this;this.emu_enabled=!0;const y=new Uint16Array([0,0,0,
229
+ 0,0,0,0,0,14,15,0,0,0,28,0,0,42,29,56,0,58,0,0,0,0,0,0,1,0,0,0,0,57,57417,57425,57423,57415,57419,57416,57421,80,0,0,0,0,82,83,0,11,2,3,4,5,6,7,8,9,10,0,39,0,13,0,0,0,30,48,46,32,18,33,34,35,23,36,37,38,50,49,24,25,16,19,31,20,22,47,17,45,21,44,57435,57436,57437,0,0,82,79,80,81,75,76,77,71,72,73,0,0,0,0,0,0,59,60,61,62,63,64,65,66,67,68,87,88,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0,0,39,13,51,12,52,53,41,0,0,0,0,0,
230
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,43,27,40,0,57435,57400,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),A={8:8,10:13,32:32,39:222,44:188,45:189,46:190,47:191,48:48,49:49,50:50,51:51,52:52,53:53,54:54,55:55,56:56,57:57,59:186,61:187,91:219,92:220,93:221,96:192,97:65,98:66,99:67,100:68,101:69,102:70,103:71,104:72,105:73,106:74,107:75,108:76,109:77,110:78,111:79,112:80,113:81,114:82,115:83,116:84,117:85,118:86,119:87,120:88,121:89,122:90},v={33:49,34:222,35:51,36:52,37:53,38:55,40:57,41:48,42:56,43:187,58:186,
231
+ 60:188,62:190,63:191,64:50,65:65,66:66,67:67,68:68,69:69,70:70,71:71,72:72,73:73,74:74,75:75,76:76,77:77,78:78,79:79,80:80,81:81,82:82,83:83,84:84,85:85,86:86,87:87,88:88,89:89,90:90,94:54,95:189,123:219,124:220,125:221,126:192};var B={Escape:1,Digit1:2,Digit2:3,Digit3:4,Digit4:5,Digit5:6,Digit6:7,Digit7:8,Digit8:9,Digit9:10,Digit0:11,Minus:12,Equal:13,Backspace:14,Tab:15,KeyQ:16,KeyW:17,KeyE:18,KeyR:19,KeyT:20,KeyY:21,KeyU:22,KeyI:23,KeyO:24,KeyP:25,BracketLeft:26,BracketRight:27,Enter:28,ControlLeft:29,
232
+ KeyA:30,KeyS:31,KeyD:32,KeyF:33,KeyG:34,KeyH:35,KeyJ:36,KeyK:37,KeyL:38,Semicolon:39,Quote:40,Backquote:41,ShiftLeft:42,Backslash:43,KeyZ:44,KeyX:45,KeyC:46,KeyV:47,KeyB:48,KeyN:49,KeyM:50,Comma:51,Period:52,Slash:53,IntlRo:53,ShiftRight:54,NumpadMultiply:55,AltLeft:56,Space:57,CapsLock:58,F1:59,F2:60,F3:61,F4:62,F5:63,F6:64,F7:65,F8:66,F9:67,F10:68,NumLock:69,ScrollLock:70,Numpad7:71,Numpad8:72,Numpad9:73,NumpadSubtract:74,Numpad4:75,Numpad5:76,Numpad6:77,NumpadAdd:78,Numpad1:79,Numpad2:80,Numpad3:81,
233
+ Numpad0:82,NumpadDecimal:83,IntlBackslash:86,F11:87,F12:88,NumpadEnter:57372,ControlRight:57373,NumpadDivide:57397,AltRight:57400,Home:57415,ArrowUp:57416,PageUp:57417,ArrowLeft:57419,ArrowRight:57421,End:57423,ArrowDown:57424,PageDown:57425,Insert:57426,Delete:57427,OSLeft:57435,OSRight:57436,ContextMenu:57437};this.bus=a;this.destroy=function(){"undefined"!==typeof window&&(window.removeEventListener("keyup",c,!1),window.removeEventListener("keydown",d,!1),window.removeEventListener("blur",e,!1))};
234
+ this.init=function(){"undefined"!==typeof window&&(this.destroy(),window.addEventListener("keyup",c,!1),window.addEventListener("keydown",d,!1),window.addEventListener("blur",e,!1))};this.init();this.simulate_press=function(w){w={keyCode:w};f(w,!0);f(w,!1)};this.simulate_char=function(w){var F=w.charCodeAt(0);F in A?this.simulate_press(A[F]):F in v?(p(42),this.simulate_press(v[F]),p(170)):console.log("ascii -> keyCode not found: ",F,w)}}function ic(a,b){function c(v){if(!A.enabled||!A.emu_enabled)return!1;
235
+ var B=b||document.body,w;if(!(w=document.pointerLockElement))a:{for(v=v.target;v.parentNode;){if(v===B){w=!0;break a}v=v.parentNode}w=!1}return w}function d(v){c(v)&&(v=v.changedTouches)&&v.length&&(v=v[v.length-1],u=v.clientX,y=v.clientY)}function e(){if(q||t||r)A.bus.send("mouse-click",[!1,!1,!1]),q=t=r=!1}function f(v){if(A.bus&&c(v)&&A.is_running){var B=0,w=0,F=v.changedTouches;F?F.length&&(F=F[F.length-1],B=F.clientX-u,w=F.clientY-y,u=F.clientX,y=F.clientY,v.preventDefault()):"number"===typeof v.movementX?
236
+ (B=v.movementX,w=v.movementY):"number"===typeof v.webkitMovementX?(B=v.webkitMovementX,w=v.webkitMovementY):"number"===typeof v.mozMovementX?(B=v.mozMovementX,w=v.mozMovementY):(B=v.clientX-u,w=v.clientY-y,u=v.clientX,y=v.clientY);A.bus.send("mouse-delta",[.15*B,-(.15*w)]);b&&A.bus.send("mouse-absolute",[v.pageX-b.offsetLeft,v.pageY-b.offsetTop,b.offsetWidth,b.offsetHeight])}}function h(v){c(v)&&p(v,!0)}function l(v){c(v)&&p(v,!1)}function p(v,B){A.bus&&(1===v.which?q=B:2===v.which?t=B:3===v.which?
237
+ r=B:C("Unknown event.which: "+v.which),A.bus.send("mouse-click",[q,t,r]),v.preventDefault())}function n(v){if(c(v)){var B=v.wheelDelta||-v.detail;0>B?B=-1:0<B&&(B=1);A.bus.send("mouse-wheel",[B,0]);v.preventDefault()}}var q=!1,r=!1,t=!1,u=0,y=0,A=this;this.enabled=!1;this.emu_enabled=!0;this.bus=a;this.bus.register("mouse-enable",function(v){this.enabled=v},this);this.is_running=!1;this.bus.register("emulator-stopped",function(){this.is_running=!1},this);this.bus.register("emulator-started",function(){this.is_running=
238
+ !0},this);this.destroy=function(){"undefined"!==typeof window&&(window.removeEventListener("touchstart",d,!1),window.removeEventListener("touchend",e,!1),window.removeEventListener("touchmove",f,!1),window.removeEventListener("mousemove",f,!1),window.removeEventListener("mousedown",h,!1),window.removeEventListener("mouseup",l,!1),window.removeEventListener("wheel",n,{passive:!1}))};this.init=function(){"undefined"!==typeof window&&(this.destroy(),window.addEventListener("touchstart",d,!1),window.addEventListener("touchend",
239
+ e,!1),window.addEventListener("touchmove",f,!1),window.addEventListener("mousemove",f,!1),window.addEventListener("mousedown",h,!1),window.addEventListener("mouseup",l,!1),window.addEventListener("wheel",n,{passive:!1}))};this.init()}function jc(){var a,b=0,c=0;this.put_char=function(d,e,f){k(0<=d&&d<c);k(0<=e&&e<b);a[d*b+e]=f};this.destroy=function(){};this.pause=function(){};this.continue=function(){};this.set_mode=function(){};this.set_font_bitmap=function(){};this.set_font_page=function(){};this.clear_screen=
240
+ function(){};this.set_size_text=function(d,e){if(d!==b||e!==c)a=new Uint8Array(d*e),b=d,c=e};this.set_size_graphical=function(){};this.set_scale=function(){};this.update_cursor_scanline=function(){};this.update_cursor=function(){};this.update_buffer=function(){};this.get_text_screen=function(){for(var d=[],e=0;e<c;e++)d.push(this.get_text_row(e));return d};this.get_text_row=function(d){d*=b;return String.fromCharCode.apply(String,a.subarray(d,d+b))};this.set_size_text(80,25)}function kc(a,b){function c(l){h.bus&&
241
+ h.enabled&&(h.send_char(l.which),l.preventDefault())}function d(l){var p=l.which;8===p?(h.send_char(127),l.preventDefault()):9===p&&(h.send_char(9),l.preventDefault())}function e(l){if(h.enabled){for(var p=l.clipboardData.getData("text/plain"),n=0;n<p.length;n++)h.send_char(p.charCodeAt(n));l.preventDefault()}}function f(l){l.target!==a&&a.blur()}var h=this;this.enabled=!0;this.bus=b;this.text="";this.text_new_line=!1;this.last_update=0;this.bus.register("serial0-output-byte",function(l){l=String.fromCharCode(l);
242
+ this.show_char(l)},this);this.destroy=function(){a.removeEventListener("keypress",c,!1);a.removeEventListener("keydown",d,!1);a.removeEventListener("paste",e,!1);window.removeEventListener("mousedown",f,!1)};this.init=function(){this.destroy();a.style.display="block";a.addEventListener("keypress",c,!1);a.addEventListener("keydown",d,!1);a.addEventListener("paste",e,!1);window.addEventListener("mousedown",f,!1)};this.init();this.show_char=function(l){"\b"===l?(this.text=this.text.slice(0,-1),this.update()):
243
+ "\r"!==l&&(this.text+=l,"\n"===l&&(this.text_new_line=!0),this.update())};this.update=function(){var l=Date.now(),p=l-this.last_update;16>p?void 0===this.update_timer&&(this.update_timer=setTimeout(()=>{this.update_timer=void 0;var n=Date.now();k(15<=n-this.last_update);this.last_update=n;this.render()},16-p)):(void 0!==this.update_timer&&(clearTimeout(this.update_timer),this.update_timer=void 0),this.last_update=l,this.render())};this.render=function(){a.value=this.text;this.text_new_line&&(this.text_new_line=
244
+ !1,a.scrollTop=1E9)};this.send_char=function(l){h.bus&&h.bus.send("serial0-input",l)}}function lc(a,b){this.element=a;if(window.Terminal){var c=this.term=new window.Terminal({logLevel:"off"});c.write("This is the serial console. Whatever you type or paste here will be sent to COM1");var d=c.onData(function(e){for(let f=0;f<e.length;f++)b.send("serial0-input",e.charCodeAt(f))});b.register("serial0-output-byte",function(e){c.write(Uint8Array.of(e))},this);this.destroy=function(){d.dispose();c.dispose()}}}
245
+ lc.prototype.show=function(){this.term&&this.term.open(this.element)};function mc(a,b){b=b.id||0;this.bus=a;this.bus_send_msgid=`net${b}-send`;this.bus_recv_msgid=`net${b}-receive`;this.channel=new BroadcastChannel(`v86-inbrowser-${b}`);this.is_open=!0;this.nic_to_hub_fn=c=>{this.channel.postMessage(c)};this.bus.register(this.bus_send_msgid,this.nic_to_hub_fn,this);this.hub_to_nic_fn=c=>{this.bus.send(this.bus_recv_msgid,c.data)};this.channel.addEventListener("message",this.hub_to_nic_fn)}mc.prototype.destroy=
246
+ function(){this.is_open&&(this.bus.unregister(this.bus_send_msgid,this.nic_to_hub_fn),this.channel.removeEventListener("message",this.hub_to_nic_fn),this.channel.close(),this.is_open=!1)};function nc(){this.filedata=new Map}nc.prototype.read=async function(a,b,c){k(a,"MemoryFileStorage read: sha256sum should be a non-empty string");return(a=this.filedata.get(a))?a.subarray(b,b+c):null};nc.prototype.cache=async function(a,b){k(a,"MemoryFileStorage cache: sha256sum should be a non-empty string");this.filedata.set(a,
247
+ b)};nc.prototype.uncache=function(a){this.filedata.delete(a)};function oc(a,b){k(b,"ServerMemoryFileStorage: baseurl should not be empty");b.endsWith("/")||(b+="/");this.storage=a;this.baseurl=b}oc.prototype.load_from_server=function(a){return new Promise(b=>{sa(this.baseurl+a,{done:async c=>{c=new Uint8Array(c);await this.cache(a,c);b(c)}})})};oc.prototype.read=async function(a,b,c){const d=await this.storage.read(a,b,c);return d?d:(await this.load_from_server(a)).subarray(b,b+c)};oc.prototype.cache=
248
+ async function(a,b){return await this.storage.cache(a,b)};oc.prototype.uncache=function(a){this.storage.uncache(a)};const pc=new TextDecoder,qc=new TextEncoder;function K(a,b,c,d){for(var e,f=0,h=0;h<a.length;h++)switch(e=b[h],a[h]){case "w":c[d++]=e&255;c[d++]=e>>8&255;c[d++]=e>>16&255;c[d++]=e>>24&255;f+=4;break;case "d":c[d++]=e&255;c[d++]=e>>8&255;c[d++]=e>>16&255;c[d++]=e>>24&255;c[d++]=0;c[d++]=0;c[d++]=0;c[d++]=0;f+=8;break;case "h":c[d++]=e&255;c[d++]=e>>8;f+=2;break;case "b":c[d++]=e;f+=
249
+ 1;break;case "s":var l=d,p=0;c[d++]=0;c[d++]=0;f+=2;e=qc.encode(e);f+=e.byteLength;p+=e.byteLength;c.set(e,d);d+=e.byteLength;c[l+0]=p&255;c[l+1]=p>>8&255;break;case "Q":K(["b","w","d"],[e.type,e.version,e.path],c,d);d+=13;f+=13;break;default:C("Marshall: Unknown type="+a[h])}return f}function M(a,b,c){let d=c.offset;for(var e=[],f=0;f<a.length;f++)switch(a[f]){case "w":var h=b[d++];h+=b[d++]<<8;h+=b[d++]<<16;h+=b[d++]<<24>>>0;e.push(h);break;case "d":h=b[d++];h+=b[d++]<<8;h+=b[d++]<<16;h+=b[d++]<<
250
+ 24>>>0;d+=4;e.push(h);break;case "h":h=b[d++];e.push(h+(b[d++]<<8));break;case "b":e.push(b[d++]);break;case "s":h=b[d++];h+=b[d++]<<8;var l=b.slice(d,d+h);d+=h;e.push(pc.decode(l));break;case "Q":c.offset=d;h=M(["b","w","d"],b,c);d=c.offset;e.push({type:h[0],version:h[1],path:h[2]});break;default:C("Error in Unmarshall: Unknown type="+a[f])}c.offset=d;return e}const rc=new TextEncoder;function N(a,b){this.inodes=[];this.events=[];this.storage=a;this.qidcounter=b||{last_qidnumber:0};this.inodedata=
251
+ {};this.total_size=274877906944;this.used_size=0;this.mounts=[];this.CreateDirectory("",-1)}N.prototype.get_state=function(){let a=[];a[0]=this.inodes;a[1]=this.qidcounter.last_qidnumber;a[2]=[];for(const [b,c]of Object.entries(this.inodedata))0===(this.inodes[b].mode&16384)&&a[2].push([b,c]);a[3]=this.total_size;a[4]=this.used_size;return a=a.concat(this.mounts)};N.prototype.set_state=function(a){this.inodes=a[0].map(b=>{const c=new sc(0);c.set_state(b);return c});this.qidcounter.last_qidnumber=
252
+ a[1];this.inodedata={};for(let [b,c]of a[2])c.buffer.byteLength!==c.byteLength&&(c=c.slice()),this.inodedata[b]=c;this.total_size=a[3];this.used_size=a[4];this.mounts=a.slice(5)};N.prototype.AddEvent=function(a,b){var c=this.inodes[a];0===c.status||2===c.status?b():this.is_forwarder(c)?this.follow_fs(c).AddEvent(c.foreign_id,b):this.events.push({id:a,OnEvent:b})};N.prototype.HandleEvent=function(a){var b=this.inodes[a];this.is_forwarder(b)&&this.follow_fs(b).HandleEvent(b.foreign_id);b=[];for(var c=
253
+ 0;c<this.events.length;c++)this.events[c].id===a?this.events[c].OnEvent():b.push(this.events[c]);this.events=b};N.prototype.load_from_json=function(a){k(a,"Invalid fs passed to load_from_json");if(3!==a.version)throw"The filesystem JSON format has changed. Please update your fs2json (https://github.com/copy/fs2json) and recreate the filesystem JSON.";var b=a.fsroot;this.used_size=a.size;for(a=0;a<b.length;a++)this.LoadRecursive(b[a],0)};N.prototype.LoadRecursive=function(a,b){var c=this.CreateInode();
254
+ const d=a[0];c.size=a[1];c.mtime=a[2];c.ctime=c.mtime;c.atime=c.mtime;c.mode=a[3];c.uid=a[4];c.gid=a[5];var e=c.mode&61440;16384===e?(this.PushInode(c,b,d),this.LoadDir(this.inodes.length-1,a[6])):32768===e?(c.status=2,c.sha256sum=a[6],k(c.sha256sum),this.PushInode(c,b,d)):40960===e?(c.symlink=a[6],this.PushInode(c,b,d)):49152!==e&&C("Unexpected ifmt: "+m(e)+" ("+d+")",4194304)};N.prototype.LoadDir=function(a,b){for(var c=0;c<b.length;c++)this.LoadRecursive(b[c],a)};N.prototype.should_be_linked=function(a){return!this.is_forwarder(a)||
255
+ 0===a.foreign_id};N.prototype.link_under_dir=function(a,b,c){const d=this.inodes[b],e=this.inodes[a];k(!this.is_forwarder(e),"Filesystem: Shouldn't link under fowarder parents");k(this.IsDirectory(a),"Filesystem: Can't link under non-directories");k(this.should_be_linked(d),"Filesystem: Can't link across filesystems apart from their root");k(0<=d.nlinks,"Filesystem: Found negative nlinks value of "+d.nlinks);k(!e.direntries.has(c),"Filesystem: Name '"+c+"' is already taken");e.direntries.set(c,b);
256
+ d.nlinks++;this.IsDirectory(b)&&(k(!d.direntries.has(".."),"Filesystem: Cannot link a directory twice"),d.direntries.has(".")||d.nlinks++,d.direntries.set(".",b),d.direntries.set("..",a),e.nlinks++)};N.prototype.unlink_from_dir=function(a,b){const c=this.Search(a,b),d=this.inodes[c],e=this.inodes[a];k(!this.is_forwarder(e),"Filesystem: Can't unlink from forwarders");k(this.IsDirectory(a),"Filesystem: Can't unlink from non-directories");e.direntries.delete(b)?(d.nlinks--,this.IsDirectory(c)&&(k(d.direntries.get("..")===
257
+ a,"Filesystem: Found directory with bad parent id"),d.direntries.delete(".."),e.nlinks--),k(0<=d.nlinks,"Filesystem: Found negative nlinks value of "+d.nlinks)):k(!1,"Filesystem: Can't unlink non-existent file: "+b)};N.prototype.PushInode=function(a,b,c){-1!==b?(this.inodes.push(a),a.fid=this.inodes.length-1,this.link_under_dir(b,a.fid,c)):0===this.inodes.length?(this.inodes.push(a),a.direntries.set(".",0),a.direntries.set("..",0),a.nlinks=2):k(!1,"Error in Filesystem: Pushed inode with name = "+
258
+ c+" has no parent")};function sc(a){this.direntries=new Map;this.minor=this.major=this.mtime=this.atime=this.ctime=this.fid=this.gid=this.uid=this.size=this.status=0;this.symlink="";this.mode=493;this.qid={type:0,version:0,path:a};this.caps=void 0;this.nlinks=0;this.sha256sum="";this.locks=[];this.foreign_id=this.mount_id=-1}sc.prototype.get_state=function(){const a=[];a[0]=this.mode;a[1]=16384===(this.mode&61440)?[...this.direntries]:32768===(this.mode&61440)?this.sha256sum:40960===(this.mode&61440)?
259
+ this.symlink:49152===(this.mode&61440)?[this.minor,this.major]:null;a[2]=this.locks;a[3]=this.status;a[4]=this.size;a[5]=this.uid;a[6]=this.gid;a[7]=this.fid;a[8]=this.ctime;a[9]=this.atime;a[10]=this.mtime;a[11]=this.qid.version;a[12]=this.qid.path;a[13]=this.nlinks;return a};sc.prototype.set_state=function(a){this.mode=a[0];if(16384===(this.mode&61440)){this.direntries=new Map;for(const [b,c]of a[1])this.direntries.set(b,c)}else 32768===(this.mode&61440)?this.sha256sum=a[1]:40960===(this.mode&61440)?
260
+ this.symlink=a[1]:49152===(this.mode&61440)&&([this.minor,this.major]=a[1]);this.locks=[];for(const b of a[2]){const c=new tc;c.set_state(b);this.locks.push(c)}this.status=a[3];this.size=a[4];this.uid=a[5];this.gid=a[6];this.fid=a[7];this.ctime=a[8];this.atime=a[9];this.mtime=a[10];this.qid.type=(this.mode&61440)>>8;this.qid.version=a[11];this.qid.path=a[12];this.nlinks=a[13]};N.prototype.divert=function(a,b){const c=this.Search(a,b),d=this.inodes[c],e=new sc(-1);k(d,"Filesystem divert: name ("+b+
261
+ ") not found");k(this.IsDirectory(c)||1>=d.nlinks,"Filesystem: can't divert hardlinked file '"+b+"' with nlinks="+d.nlinks);Object.assign(e,d);const f=this.inodes.length;this.inodes.push(e);e.fid=f;this.is_forwarder(d)&&this.mounts[d.mount_id].backtrack.set(d.foreign_id,f);this.should_be_linked(d)&&(this.unlink_from_dir(a,b),this.link_under_dir(a,f,b));if(this.IsDirectory(c)&&!this.is_forwarder(d))for(const [h,l]of e.direntries)"."!==h&&".."!==h&&this.IsDirectory(l)&&this.inodes[l].direntries.set("..",
262
+ f);this.inodedata[f]=this.inodedata[c];delete this.inodedata[c];d.direntries=new Map;d.nlinks=0;return f};N.prototype.copy_inode=function(a,b){Object.assign(b,a,{fid:b.fid,direntries:b.direntries,nlinks:b.nlinks})};N.prototype.CreateInode=function(){const a=Math.round(Date.now()/1E3),b=new sc(++this.qidcounter.last_qidnumber);b.atime=b.ctime=b.mtime=a;return b};N.prototype.CreateDirectory=function(a,b){var c=this.inodes[b];if(0<=b&&this.is_forwarder(c))return b=c.foreign_id,a=this.follow_fs(c).CreateDirectory(a,
263
+ b),this.create_forwarder(c.mount_id,a);c=this.CreateInode();c.mode=16895;0<=b&&(c.uid=this.inodes[b].uid,c.gid=this.inodes[b].gid,c.mode=this.inodes[b].mode&511|16384);c.qid.type=64;this.PushInode(c,b,a);this.NotifyListeners(this.inodes.length-1,"newdir");return this.inodes.length-1};N.prototype.CreateFile=function(a,b){var c=this.inodes[b];if(this.is_forwarder(c))return b=c.foreign_id,a=this.follow_fs(c).CreateFile(a,b),this.create_forwarder(c.mount_id,a);c=this.CreateInode();c.uid=this.inodes[b].uid;
264
+ c.gid=this.inodes[b].gid;c.qid.type=128;c.mode=this.inodes[b].mode&438|32768;this.PushInode(c,b,a);this.NotifyListeners(this.inodes.length-1,"newfile");return this.inodes.length-1};N.prototype.CreateNode=function(a,b,c,d){var e=this.inodes[b];if(this.is_forwarder(e))return b=e.foreign_id,a=this.follow_fs(e).CreateNode(a,b,c,d),this.create_forwarder(e.mount_id,a);e=this.CreateInode();e.major=c;e.minor=d;e.uid=this.inodes[b].uid;e.gid=this.inodes[b].gid;e.qid.type=192;e.mode=this.inodes[b].mode&438;
265
+ this.PushInode(e,b,a);return this.inodes.length-1};N.prototype.CreateSymlink=function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=d.foreign_id,a=this.follow_fs(d).CreateSymlink(a,b,c),this.create_forwarder(d.mount_id,a);d=this.CreateInode();d.uid=this.inodes[b].uid;d.gid=this.inodes[b].gid;d.qid.type=160;d.symlink=c;d.mode=40960;this.PushInode(d,b,a);return this.inodes.length-1};N.prototype.CreateTextFile=async function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=
266
+ d.foreign_id,c=await this.follow_fs(d).CreateTextFile(a,b,c),this.create_forwarder(d.mount_id,c);d=this.CreateFile(a,b);b=this.inodes[d];a=new Uint8Array(c.length);b.size=c.length;for(b=0;b<c.length;b++)a[b]=c.charCodeAt(b);await this.set_data(d,a);return d};N.prototype.CreateBinaryFile=async function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=d.foreign_id,c=await this.follow_fs(d).CreateBinaryFile(a,b,c),this.create_forwarder(d.mount_id,c);d=this.CreateFile(a,b);a=this.inodes[d];
267
+ b=new Uint8Array(c.length);b.set(c);await this.set_data(d,b);a.size=c.length;return d};N.prototype.OpenInode=function(a,b){var c=this.inodes[a];if(this.is_forwarder(c))return this.follow_fs(c).OpenInode(c.foreign_id,b);16384===(c.mode&61440)&&this.FillDirectory(a);return!0};N.prototype.CloseInode=async function(a){var b=this.inodes[a];if(this.is_forwarder(b))return await this.follow_fs(b).CloseInode(b.foreign_id);2===b.status&&this.storage.uncache(b.sha256sum);4===b.status&&(b.status=-1,await this.DeleteData(a))};
268
+ N.prototype.Rename=async function(a,b,c,d){if(a===c&&b===d)return 0;var e=this.Search(a,b);if(-1===e)return-2;var f=this.GetFullPath(a)+"/"+b;if(-1!==this.Search(c,d)){var h=this.Unlink(c,d);if(0>h)return h}var l=this.inodes[e],p=this.inodes[a];h=this.inodes[c];if(this.is_forwarder(p)||this.is_forwarder(h))if(this.is_forwarder(p)&&p.mount_id===h.mount_id){if(a=await this.follow_fs(p).Rename(p.foreign_id,b,h.foreign_id,d),0>a)return a}else{if(this.is_a_root(e))return C("XXX: Attempted to move mountpoint ("+
269
+ b+") - skipped",4194304),-1;if(!this.IsDirectory(e)&&1<this.GetInode(e).nlinks)return C("XXX: Attempted to move hardlinked file ("+b+") across filesystems - skipped",4194304),-1;p=this.divert(a,b);const n=this.GetInode(e),q=await this.Read(p,0,n.size);this.is_forwarder(h)?(c=this.follow_fs(h),d=this.IsDirectory(p)?c.CreateDirectory(d,h.foreign_id):c.CreateFile(d,h.foreign_id),c=c.GetInode(d),this.copy_inode(n,c),this.set_forwarder(e,h.mount_id,d)):(this.delete_forwarder(l),this.copy_inode(n,l),this.link_under_dir(c,
270
+ e,d));await this.ChangeSize(e,n.size);q&&q.length&&await this.Write(e,0,q.length,q);if(this.IsDirectory(e))for(const r of this.GetChildren(p))if(h=await this.Rename(p,r,e,r),0>h)return h;await this.DeleteData(p);a=this.Unlink(a,b);if(0>a)return a}else this.unlink_from_dir(a,b),this.link_under_dir(c,e,d),l.qid.version++;this.NotifyListeners(e,"rename",{oldpath:f});return 0};N.prototype.Write=async function(a,b,c,d){this.NotifyListeners(a,"write");var e=this.inodes[a];if(this.is_forwarder(e))a=e.foreign_id,
271
+ await this.follow_fs(e).Write(a,b,c,d);else{var f=await this.get_buffer(a);!f||f.length<b+c?(await this.ChangeSize(a,Math.floor(3*(b+c)/2)),e.size=b+c,f=await this.get_buffer(a)):e.size<b+c&&(e.size=b+c);d&&f.set(d.subarray(0,c),b);await this.set_data(a,f)}};N.prototype.Read=async function(a,b,c){const d=this.inodes[a];return this.is_forwarder(d)?(a=d.foreign_id,await this.follow_fs(d).Read(a,b,c)):await this.get_data(a,b,c)};N.prototype.Search=function(a,b){a=this.inodes[a];if(this.is_forwarder(a)){const c=
272
+ a.foreign_id;b=this.follow_fs(a).Search(c,b);return-1===b?-1:this.get_forwarder(a.mount_id,b)}b=a.direntries.get(b);return void 0===b?-1:b};N.prototype.CountUsedInodes=function(){let a=this.inodes.length;for(const {fs:b,backtrack:c}of this.mounts)a+=b.CountUsedInodes(),a-=c.size;return a};N.prototype.CountFreeInodes=function(){let a=1048576;for(const {fs:b}of this.mounts)a+=b.CountFreeInodes();return a};N.prototype.GetTotalSize=function(){let a=this.used_size;for(const {fs:b}of this.mounts)a+=b.GetTotalSize();
273
+ return a};N.prototype.GetSpace=function(){let a=this.total_size;for(const {fs:b}of this.mounts)a+=b.GetSpace();return this.total_size};N.prototype.GetDirectoryName=function(a){const b=this.inodes[this.GetParent(a)];if(this.is_forwarder(b))return this.follow_fs(b).GetDirectoryName(this.inodes[a].foreign_id);if(!b)return"";for(const [c,d]of b.direntries)if(d===a)return c;k(!1,"Filesystem: Found directory inode whose parent doesn't link to it");return""};N.prototype.GetFullPath=function(a){k(this.IsDirectory(a),
274
+ "Filesystem: Cannot get full path of non-directory inode");for(var b="";0!==a;)b="/"+this.GetDirectoryName(a)+b,a=this.GetParent(a);return b.substring(1)};N.prototype.Link=function(a,b,c){if(this.IsDirectory(b))return-1;const d=this.inodes[a],e=this.inodes[b];if(this.is_forwarder(d))return this.is_forwarder(e)&&e.mount_id===d.mount_id?this.follow_fs(d).Link(d.foreign_id,e.foreign_id,c):(C("XXX: Attempted to hardlink a file into a child filesystem - skipped",4194304),-1);if(this.is_forwarder(e))return C("XXX: Attempted to hardlink file across filesystems - skipped",
275
+ 4194304),-1;this.link_under_dir(a,b,c);return 0};N.prototype.Unlink=function(a,b){if("."===b||".."===b)return-1;const c=this.Search(a,b),d=this.inodes[c],e=this.inodes[a];if(this.is_forwarder(e))return k(this.is_forwarder(d),"Children of forwarders should be forwarders"),a=e.foreign_id,this.follow_fs(e).Unlink(a,b);if(this.IsDirectory(c)&&!this.IsEmpty(c))return-39;this.unlink_from_dir(a,b);0===d.nlinks&&(d.status=4,this.NotifyListeners(c,"delete"));return 0};N.prototype.DeleteData=async function(a){const b=
276
+ this.inodes[a];this.is_forwarder(b)?await this.follow_fs(b).DeleteData(b.foreign_id):(b.size=0,delete this.inodedata[a])};N.prototype.get_buffer=async function(a){const b=this.inodes[a];k(b,`Filesystem get_buffer: idx ${a} does not point to an inode`);return this.inodedata[a]?this.inodedata[a]:2===b.status?(k(b.sha256sum,"Filesystem get_data: found inode on server without sha256sum"),await this.storage.read(b.sha256sum,0,b.size)):null};N.prototype.get_data=async function(a,b,c){const d=this.inodes[a];
277
+ k(d,`Filesystem get_data: idx ${a} does not point to an inode`);return this.inodedata[a]?this.inodedata[a].subarray(b,b+c):2===d.status?(k(d.sha256sum,"Filesystem get_data: found inode on server without sha256sum"),await this.storage.read(d.sha256sum,b,c)):null};N.prototype.set_data=async function(a,b){this.inodedata[a]=b;2===this.inodes[a].status&&(this.inodes[a].status=0,this.storage.uncache(this.inodes[a].sha256sum))};N.prototype.GetInode=function(a){k(!isNaN(a),"Filesystem GetInode: NaN idx");
278
+ k(0<=a&&a<this.inodes.length,"Filesystem GetInode: out of range idx:"+a);a=this.inodes[a];return this.is_forwarder(a)?this.follow_fs(a).GetInode(a.foreign_id):a};N.prototype.ChangeSize=async function(a,b){var c=this.GetInode(a),d=await this.get_data(a,0,c.size);if(b!==c.size){var e=new Uint8Array(b);c.size=b;d&&e.set(d.subarray(0,Math.min(d.length,c.size)),0);await this.set_data(a,e)}};N.prototype.SearchPath=function(a){a=a.replace("//","/");a=a.split("/");0<a.length&&0===a[a.length-1].length&&a.pop();
279
+ 0<a.length&&0===a[0].length&&a.shift();const b=a.length;var c=-1,d=0;let e=null;for(var f=0;f<b;f++)if(c=d,d=this.Search(c,a[f]),!e&&this.is_forwarder(this.inodes[c])&&(e="/"+a.slice(f).join("/")),-1===d)return f<b-1?{id:-1,parentid:-1,name:a[f],forward_path:e}:{id:-1,parentid:c,name:a[f],forward_path:e};return{id:d,parentid:c,name:a[f],forward_path:e}};N.prototype.GetRecursiveList=function(a,b){if(this.is_forwarder(this.inodes[a])){const c=this.follow_fs(this.inodes[a]),d=this.inodes[a].mount_id,
280
+ e=b.length;c.GetRecursiveList(this.inodes[a].foreign_id,b);for(a=e;a<b.length;a++)b[a].parentid=this.get_forwarder(d,b[a].parentid)}else for(const [c,d]of this.inodes[a].direntries)"."!==c&&".."!==c&&(b.push({parentid:a,name:c}),this.IsDirectory(d)&&this.GetRecursiveList(d,b))};N.prototype.RecursiveDelete=function(a){var b=[];a=this.SearchPath(a);if(-1!==a.id)for(this.GetRecursiveList(a.id,b),a=b.length-1;0<=a;a--){const c=this.Unlink(b[a].parentid,b[a].name);k(0===c,"Filesystem RecursiveDelete failed at parent="+
281
+ b[a].parentid+", name='"+b[a].name+"' with error code: "+-c)}};N.prototype.DeleteNode=function(a){var b=this.SearchPath(a);-1!==b.id&&(32768===(this.inodes[b.id].mode&61440)?(a=this.Unlink(b.parentid,b.name),k(0===a,"Filesystem DeleteNode failed with error code: "+-a)):16384===(this.inodes[b.id].mode&61440)&&(this.RecursiveDelete(a),a=this.Unlink(b.parentid,b.name),k(0===a,"Filesystem DeleteNode failed with error code: "+-a)))};N.prototype.NotifyListeners=function(){};N.prototype.Check=function(){for(var a=
282
+ 1;a<this.inodes.length;a++)if(-1!==this.inodes[a].status){var b=this.GetInode(a);0>b.nlinks&&C("Error in filesystem: negative nlinks="+b.nlinks+" at id ="+a,4194304);if(this.IsDirectory(a)){b=this.GetInode(a);this.IsDirectory(a)&&0>this.GetParent(a)&&C("Error in filesystem: negative parent id "+a,4194304);for(const [c,d]of b.direntries){0===c.length&&C("Error in filesystem: inode with no name and id "+d,4194304);for(const e of c)32>e&&C("Error in filesystem: Unallowed char in filename",4194304)}}}};
283
+ N.prototype.FillDirectory=function(a){var b=this.inodes[a];if(this.is_forwarder(b))this.follow_fs(b).FillDirectory(b.foreign_id);else{var c=0;for(const d of b.direntries.keys())c+=24+rc.encode(d).length;a=this.inodedata[a]=new Uint8Array(c);b.size=c;c=0;for(const [d,e]of b.direntries)b=this.GetInode(e),c+=K(["Q","d","b","s"],[b.qid,c+13+8+1+2+rc.encode(d).length,b.mode>>12,d],a,c)}};N.prototype.RoundToDirentry=function(a,b){const c=this.inodedata[a];k(c,`FS directory data for dirid=${a} should be generated`);
284
+ k(c.length,"FS directory should have at least an entry");if(b>=c.length)return c.length;for(a=0;;){const d=M(["Q","d"],c,{offset:a})[1];if(d>b)break;a=d}return a};N.prototype.IsDirectory=function(a){a=this.inodes[a];return this.is_forwarder(a)?this.follow_fs(a).IsDirectory(a.foreign_id):16384===(a.mode&61440)};N.prototype.IsEmpty=function(a){a=this.inodes[a];if(this.is_forwarder(a))return this.follow_fs(a).IsDirectory(a.foreign_id);for(const b of a.direntries.keys())if("."!==b&&".."!==b)return!1;
285
+ return!0};N.prototype.GetChildren=function(a){k(this.IsDirectory(a),"Filesystem: cannot get children of non-directory inode");a=this.inodes[a];if(this.is_forwarder(a))return this.follow_fs(a).GetChildren(a.foreign_id);const b=[];for(const c of a.direntries.keys())"."!==c&&".."!==c&&b.push(c);return b};N.prototype.GetParent=function(a){k(this.IsDirectory(a),"Filesystem: cannot get parent of non-directory inode");a=this.inodes[a];if(this.should_be_linked(a))return a.direntries.get("..");const b=this.follow_fs(a).GetParent(a.foreign_id);
286
+ k(-1!==b,"Filesystem: should not have invalid parent ids");return this.get_forwarder(a.mount_id,b)};N.prototype.PrepareCAPs=function(a){a=this.GetInode(a);if(a.caps)return a.caps.length;a.caps=new Uint8Array(20);a.caps[0]=0;a.caps[1]=0;a.caps[2]=0;a.caps[3]=2;a.caps[4]=255;a.caps[5]=255;a.caps[6]=255;a.caps[7]=255;a.caps[8]=255;a.caps[9]=255;a.caps[10]=255;a.caps[11]=255;a.caps[12]=63;a.caps[13]=0;a.caps[14]=0;a.caps[15]=0;a.caps[16]=63;a.caps[17]=0;a.caps[18]=0;a.caps[19]=0;return a.caps.length};
287
+ function uc(a){this.fs=a;this.backtrack=new Map}uc.prototype.get_state=function(){const a=[];a[0]=this.fs;a[1]=[...this.backtrack];return a};uc.prototype.set_state=function(a){this.fs=a[0];this.backtrack=new Map(a[1])};N.prototype.set_forwarder=function(a,b,c){const d=this.inodes[a];k(0===d.nlinks,"Filesystem: attempted to convert an inode into forwarder before unlinking the inode");this.is_forwarder(d)&&this.mounts[d.mount_id].backtrack.delete(d.foreign_id);d.status=5;d.mount_id=b;d.foreign_id=c;
288
+ this.mounts[b].backtrack.set(c,a)};N.prototype.create_forwarder=function(a,b){const c=this.CreateInode(),d=this.inodes.length;this.inodes.push(c);c.fid=d;this.set_forwarder(d,a,b);return d};N.prototype.is_forwarder=function(a){return 5===a.status};N.prototype.is_a_root=function(a){return 0===this.GetInode(a).fid};N.prototype.get_forwarder=function(a,b){var c=this.mounts[a];k(0<=b,"Filesystem get_forwarder: invalid foreign_id: "+b);k(c,"Filesystem get_forwarder: invalid mount number: "+a);c=c.backtrack.get(b);
289
+ return void 0===c?this.create_forwarder(a,b):c};N.prototype.delete_forwarder=function(a){k(this.is_forwarder(a),"Filesystem delete_forwarder: expected forwarder");a.status=-1;this.mounts[a.mount_id].backtrack.delete(a.foreign_id)};N.prototype.follow_fs=function(a){const b=this.mounts[a.mount_id];k(this.is_forwarder(a),"Filesystem follow_fs: inode should be a forwarding inode");k(b,"Filesystem follow_fs: inode<id="+a.fid+"> should point to valid mounted FS");return b.fs};N.prototype.Mount=function(a,
290
+ b){k(b.qidcounter===this.qidcounter,"Cannot mount filesystem whose qid numbers aren't synchronised with current filesystem.");var c=this.SearchPath(a);if(-1===c.parentid)return C("Mount failed: parent for path not found: "+a,4194304),-2;if(-1!==c.id)return C("Mount failed: file already exists at path: "+a,4194304),-17;if(c.forward_path)return a=this.inodes[c.parentid],c=this.follow_fs(a).Mount(c.forward_path,b),0>c?c:this.get_forwarder(a.mount_id,c);a=this.mounts.length;this.mounts.push(new uc(b));
291
+ b=this.create_forwarder(a,0);this.link_under_dir(c.parentid,b,c.name);return b};function tc(){this.type=2;this.start=0;this.length=Infinity;this.proc_id=-1;this.client_id=""}tc.prototype.get_state=function(){const a=[];a[0]=this.type;a[1]=this.start;a[2]=Infinity===this.length?0:this.length;a[3]=this.proc_id;a[4]=this.client_id;return a};tc.prototype.set_state=function(a){this.type=a[0];this.start=a[1];this.length=0===a[2]?Infinity:a[2];this.proc_id=a[3];this.client_id=a[4]};tc.prototype.clone=function(){const a=
292
+ new tc;a.set_state(this.get_state());return a};tc.prototype.conflicts_with=function(a){return this.proc_id===a.proc_id&&this.client_id===a.client_id||2===this.type||2===a.type||1!==this.type&&1!==a.type||this.start+this.length<=a.start||a.start+a.length<=this.start?!1:!0};tc.prototype.is_alike=function(a){return a.proc_id===this.proc_id&&a.client_id===this.client_id&&a.type===this.type};tc.prototype.may_merge_after=function(a){return this.is_alike(a)&&a.start+a.length===this.start};N.prototype.DescribeLock=
293
+ function(a,b,c,d,e){k(0===a||1===a||2===a,"Filesystem: Invalid lock type: "+a);k(0<=b,"Filesystem: Invalid negative lock starting offset: "+b);k(0<c,"Filesystem: Invalid non-positive lock length: "+c);const f=new tc;f.type=a;f.start=b;f.length=c;f.proc_id=d;f.client_id=e;return f};N.prototype.GetLock=function(a,b){a=this.inodes[a];if(this.is_forwarder(a)){var c=a.foreign_id;return this.follow_fs(a).GetLock(c,b)}for(c of a.locks)if(b.conflicts_with(c))return c.clone();return null};N.prototype.Lock=
294
+ function(a,b,c){const d=this.inodes[a];if(this.is_forwarder(d))return a=d.foreign_id,this.follow_fs(d).Lock(a,b,c);b=b.clone();if(2!==b.type&&this.GetLock(a,b))return 1;for(c=0;c<d.locks.length;c++){a=d.locks[c];k(0<a.length,"Filesystem: Found non-positive lock region length: "+a.length);k(0===a.type||1===a.type,"Filesystem: Found invalid lock type: "+a.type);k(!d.locks[c-1]||d.locks[c-1].start<=a.start,"Filesystem: Locks should be sorted by starting offset");if(a.start+a.length<=b.start)continue;
295
+ if(b.start+b.length<=a.start)break;if(a.proc_id!==b.proc_id||a.client_id!==b.client_id){k(!a.conflicts_with(b),"Filesytem: Found conflicting lock region, despite already checked for conflicts");continue}var e=b.start+b.length;const f=b.start-a.start,h=a.start+a.length-e;if(0<f&&0<h&&a.type===b.type)return 0;0<f&&(a.length=f);if(0>=f&&0<h)a.start=e,a.length=h;else if(0<h){for(;c<d.locks.length&&d.locks[c].start<e;)c++;d.locks.splice(c,0,this.DescribeLock(a.type,e,h,a.proc_id,a.client_id))}else 0>=
296
+ f&&(d.locks.splice(c,1),c--)}if(2!==b.type){c=b;a=!1;for(e=0;e<d.locks.length&&!(c.may_merge_after(d.locks[e])&&(d.locks[e].length+=b.length,c=d.locks[e],a=!0),b.start<=d.locks[e].start);e++);a||(d.locks.splice(e,0,c),e++);for(;e<d.locks.length;e++)if(d.locks[e].is_alike(c)){d.locks[e].may_merge_after(c)&&(c.length+=d.locks[e].length,d.locks.splice(e,1));break}}return 0};N.prototype.read_dir=function(a){a=this.SearchPath(a);if(-1!==a.id)return a=this.GetInode(a.id),Array.from(a.direntries.keys()).filter(b=>
297
+ "."!==b&&".."!==b)};N.prototype.read_file=function(a){a=this.SearchPath(a);if(-1===a.id)return Promise.resolve(null);const b=this.GetInode(a.id);return this.Read(a.id,0,b.size)};function O(a){"number"===typeof a.log_level&&(va=a.log_level);this.cpu_is_running=!1;this.cpu_exception_hook=function(){};var b=Ha.create();this.bus=b[0];this.emulator_bus=b[1];var c,d;const e=new WebAssembly.Table({element:"anyfunc",initial:1924});b={cpu_exception_hook:h=>this.cpu_exception_hook(h),run_hardware_timers:function(h,
298
+ l){return c.run_hardware_timers(h,l)},cpu_event_halt:()=>{this.emulator_bus.send("cpu-event-halt")},abort:function(){k(!1)},microtick:I.microtick,get_rand_int:function(){return da()},apic_acknowledge_irq:function(){return c.devices.apic.acknowledge_irq()},stop_idling:function(){return c.stop_idling()},io_port_read8:function(h){return c.io.port_read8(h)},io_port_read16:function(h){return c.io.port_read16(h)},io_port_read32:function(h){return c.io.port_read32(h)},io_port_write8:function(h,l){c.io.port_write8(h,
299
+ l)},io_port_write16:function(h,l){c.io.port_write16(h,l)},io_port_write32:function(h,l){c.io.port_write32(h,l)},mmap_read8:function(h){return c.mmap_read8(h)},mmap_read16:function(h){return c.mmap_read16(h)},mmap_read32:function(h){return c.mmap_read32(h)},mmap_write8:function(h,l){c.mmap_write8(h,l)},mmap_write16:function(h,l){c.mmap_write16(h,l)},mmap_write32:function(h,l){c.mmap_write32(h,l)},mmap_write64:function(h,l,p){c.mmap_write64(h,l,p)},mmap_write128:function(h,l,p,n,q){c.mmap_write128(h,
300
+ l,p,n,q)},log_from_wasm:function(h,l){h=String.fromCharCode(...(new Uint8Array(d.buffer,h>>>0,l>>>0)));C(h,2)},console_log_from_wasm:function(h,l){h=String.fromCharCode(...(new Uint8Array(d.buffer,h>>>0,l>>>0)));console.error(h)},dbg_trace_from_wasm:function(){C(Error().stack,2)},codegen_finalize:(h,l,p,n,q)=>{c.codegen_finalize(h,l,p,n,q)},jit_clear_func:h=>c.jit_clear_func(h),jit_clear_all_funcs:()=>c.jit_clear_all_funcs(),__indirect_function_table:e};let f=a.wasm_fn;f||(f=h=>new Promise(l=>{let p=
301
+ "v86-debug.wasm",n="v86-fallback.wasm";a.wasm_path?(p=a.wasm_path,n=p.replace("v86.wasm","v86-fallback.wasm")):"undefined"===typeof window&&"string"===typeof __dirname?(p=__dirname+"/"+p,n=__dirname+"/"+n):(p="build/"+p,n="build/"+n);sa(p,{done:async q=>{try{const {instance:r}=await WebAssembly.instantiate(q,h);this.wasm_source=q;l(r.exports)}catch(r){sa(n,{done:async t=>{const {instance:u}=await WebAssembly.instantiate(t,h);this.wasm_source=t;l(u.exports)}})}},progress:q=>{this.emulator_bus.send("download-progress",
302
+ {file_index:0,file_count:1,file_name:p,lengthComputable:q.lengthComputable,total:q.total,loaded:q.loaded})}})}));f({env:b}).then(h=>{d=h.memory;h.rust_init();h=this.v86=new I(this.emulator_bus,{exports:h,wasm_table:e});c=h.cpu;this.continue_init(h,a)});this.zstd_worker=null;this.zstd_worker_request_id=0}O.prototype.continue_init=async function(a,b){function c(t,u){switch(t){case "hda":e.hda=this.disk_images.hda=u;break;case "hdb":e.hdb=this.disk_images.hdb=u;break;case "cdrom":e.cdrom=this.disk_images.cdrom=
303
+ u;break;case "fda":e.fda=this.disk_images.fda=u;break;case "fdb":e.fdb=this.disk_images.fdb=u;break;case "multiboot":e.multiboot=this.disk_images.multiboot=u.buffer;break;case "bzimage":e.bzimage=this.disk_images.bzimage=u.buffer;break;case "initrd":e.initrd=this.disk_images.initrd=u.buffer;break;case "bios":e.bios=u.buffer;break;case "vga_bios":e.vga_bios=u.buffer;break;case "initial_state":e.initial_state=u.buffer;break;case "fs9p_json":e.fs9p_json=u;break;default:k(!1,t)}}async function d(){if(e.fs9p&&
304
+ e.fs9p_json)if(e.initial_state)C("Filesystem basefs ignored: Overridden by state image");else{if(e.fs9p.load_from_json(e.fs9p_json),b.bzimage_initrd_from_filesystem){const {bzimage_path:t,initrd_path:u}=this.get_bzimage_initrd_from_filesystem(e.fs9p);C("Found bzimage: "+t+" and initrd: "+u);const [y,A]=await Promise.all([e.fs9p.read_file(u),e.fs9p.read_file(t)]);c.call(this,"initrd",new ya(y.buffer));c.call(this,"bzimage",new ya(A.buffer))}}else k(!b.bzimage_initrd_from_filesystem||e.initial_state,
305
+ "bzimage_initrd_from_filesystem: Requires a filesystem");this.serial_adapter&&this.serial_adapter.show&&this.serial_adapter.show();this.v86.init(e);e.initial_state&&(a.restore_state(e.initial_state),e.initial_state=void 0);b.autostart&&this.v86.run();this.emulator_bus.send("emulator-loaded")}this.bus.register("emulator-stopped",function(){this.cpu_is_running=!1;this.screen_adapter.pause()},this);this.bus.register("emulator-started",function(){this.cpu_is_running=!0;this.screen_adapter.continue()},
306
+ this);var e={};this.disk_images={fda:void 0,fdb:void 0,hda:void 0,hdb:void 0,cdrom:void 0};var f=b.boot_order?b.boot_order:b.fda?801:b.hda?786:291;e.acpi=b.acpi;e.disable_jit=b.disable_jit;e.load_devices=!0;e.memory_size=b.memory_size||67108864;e.vga_memory_size=b.vga_memory_size||8388608;e.boot_order=f;e.fastboot=b.fastboot||!1;e.fda=void 0;e.fdb=void 0;e.uart1=b.uart1;e.uart2=b.uart2;e.uart3=b.uart3;e.cmdline=b.cmdline;e.preserve_mac_from_state_image=b.preserve_mac_from_state_image;e.mac_address_translation=
307
+ b.mac_address_translation;e.cpuid_level=b.cpuid_level;e.virtio_balloon=b.virtio_balloon;e.virtio_console=b.virtio_console;e.virtio_net=b.virtio_net;e.screen_options=b.screen_options;if(f=b.network_relay_url||b.net_device&&b.net_device.relay_url)"fetch"===f?this.network_adapter=new Yb(this.bus,b.net_device):"inbrowser"===f?this.network_adapter=new mc(this.bus,b.net_device):f.startsWith("wisp://")||f.startsWith("wisps://")?this.network_adapter=new fc(f,this.bus,b.net_device):this.network_adapter=new rb(f,
308
+ this.bus);e.net_device=b.net_device||{type:"ne2k"};f=b.screen||{};b.screen_container&&(f.container=b.screen_container);b.disable_keyboard||(this.keyboard_adapter=new hc(this.bus));b.disable_mouse||(this.mouse_adapter=new ic(this.bus,f.container));this.screen_adapter=f.container?new wa(f,()=>this.v86.cpu.devices.vga&&this.v86.cpu.devices.vga.screen_fill_buffer()):new jc;e.screen=this.screen_adapter;e.screen_options=f;b.serial_container&&(this.serial_adapter=new kc(b.serial_container,this.bus));b.serial_container_xtermjs&&
309
+ (this.serial_adapter=new lc(b.serial_container_xtermjs,this.bus));b.disable_speaker||(this.speaker_adapter=new kb(this.bus));var h=[];f=(t,u)=>{if(u)if(u.get&&u.set&&u.load)h.push({name:t,loadable:u});else{if("bios"===t||"vga_bios"===t||"initial_state"===t||"multiboot"===t||"bzimage"===t||"initrd"===t)u.async=!1;if("fda"===t||"fdb"===t)u.async=!1;u.url&&!u.async?h.push({name:t,url:u.url,size:u.size}):h.push({name:t,loadable:Da(u,this.zstd_decompress_worker.bind(this))})}};b.state&&console.warn("Warning: Unknown option 'state'. Did you mean 'initial_state'?");
310
+ f("bios",b.bios);f("vga_bios",b.vga_bios);f("cdrom",b.cdrom);f("hda",b.hda);f("hdb",b.hdb);f("fda",b.fda);f("fdb",b.fdb);f("initial_state",b.initial_state);f("multiboot",b.multiboot);f("bzimage",b.bzimage);f("initrd",b.initrd);if(b.filesystem){f=b.filesystem.basefs;var l=b.filesystem.baseurl;let t=new nc;l&&(t=new oc(t,l));e.fs9p=this.fs9p=new N(t);if(f){k(l,"Filesystem: baseurl must be specified");if("object"===typeof f){var p=f.size;f=f.url}k("string"===typeof f);h.push({name:"fs9p_json",url:f,
311
+ size:p,as_json:!0})}}var n=this,q=h.length,r=function(t){if(t===q)setTimeout(d.bind(this),0);else{var u=h[t];u.loadable?(u.loadable.onload=function(){c.call(this,u.name,u.loadable);r(t+1)}.bind(this),u.loadable.load()):sa(u.url,{done:function(y){u.url.endsWith(".zst")&&"initial_state"!==u.name&&(k(u.size,"A size must be provided for compressed images"),y=this.zstd_decompress(u.size,new Uint8Array(y)));c.call(this,u.name,u.as_json?y:new ya(y));r(t+1)}.bind(this),progress:function(y){200===y.target.status?
312
+ n.emulator_bus.send("download-progress",{file_index:t,file_count:q,file_name:u.url,lengthComputable:y.lengthComputable,total:y.total||u.size,loaded:y.loaded}):n.emulator_bus.send("download-error",{file_index:t,file_count:q,file_name:u.url,request:y.target})},as_json:u.as_json})}}.bind(this);r(0)};O.prototype.zstd_decompress=function(a,b){const c=this.v86.cpu;k(!this.zstd_context);this.zstd_context=c.zstd_create_ctx(b.length);(new Uint8Array(c.wasm_memory.buffer)).set(b,c.zstd_get_src_ptr(this.zstd_context));
313
+ b=c.zstd_read(this.zstd_context,a);const d=c.wasm_memory.buffer.slice(b,b+a);c.zstd_read_free(b,a);c.zstd_free_ctx(this.zstd_context);this.zstd_context=null;return d};O.prototype.zstd_decompress_worker=async function(a,b){if(!this.zstd_worker){const c=URL.createObjectURL(new Blob(["("+function(){let d;globalThis.onmessage=function(e){if(d){var {src:f,decompressed_size:h,id:l}=e.data;e=d.exports;var p=e.zstd_create_ctx(f.length);(new Uint8Array(e.memory.buffer)).set(f,e.zstd_get_src_ptr(p));var n=
314
+ e.zstd_read(p,h),q=e.memory.buffer.slice(n,n+h);e.zstd_read_free(n,h);e.zstd_free_ctx(p);postMessage({result:q,id:l},[q])}else p=Object.fromEntries("cpu_exception_hook run_hardware_timers cpu_event_halt microtick get_rand_int apic_acknowledge_irq stop_idling io_port_read8 io_port_read16 io_port_read32 io_port_write8 io_port_write16 io_port_write32 mmap_read8 mmap_read16 mmap_read32 mmap_write8 mmap_write16 mmap_write32 mmap_write64 mmap_write128 codegen_finalize jit_clear_func jit_clear_all_funcs".split(" ").map(r=>
315
+ [r,()=>console.error("zstd worker unexpectedly called "+r)])),p.__indirect_function_table=new WebAssembly.Table({element:"anyfunc",initial:1024}),p.abort=()=>{throw Error("zstd worker aborted");},p.log_from_wasm=p.console_log_from_wasm=(r,t)=>{console.log(String.fromCharCode(...(new Uint8Array(d.exports.memory.buffer,r,t))))},p.dbg_trace_from_wasm=()=>console.trace(),d=new WebAssembly.Instance(new WebAssembly.Module(e.data),{env:p})}}.toString()+")()"],{type:"text/javascript"}));this.zstd_worker=
316
+ new Worker(c);URL.revokeObjectURL(c);this.zstd_worker.postMessage(this.wasm_source,[this.wasm_source])}return new Promise(c=>{const d=this.zstd_worker_request_id++,e=async f=>{f.data.id===d&&(this.zstd_worker.removeEventListener("message",e),k(a===f.data.result.byteLength),c(f.data.result))};this.zstd_worker.addEventListener("message",e);this.zstd_worker.postMessage({src:b,decompressed_size:a,id:d},[b.buffer])})};O.prototype.get_bzimage_initrd_from_filesystem=function(a){const b=(a.read_dir("/")||
317
+ []).map(e=>"/"+e);a=(a.read_dir("/boot/")||[]).map(e=>"/boot/"+e);let c,d;for(const e of[].concat(b,a)){const f=/old/i.test(e)||/fallback/i.test(e),h=/vmlinuz/i.test(e)||/bzimage/i.test(e),l=/initrd/i.test(e)||/initramfs/i.test(e);!h||d&&f||(d=e);!l||c&&f||(c=e)}c&&d||(console.log("Failed to find bzimage or initrd in filesystem. Files:"),console.log(b.join(" ")),console.log(a.join(" ")));return{initrd_path:c,bzimage_path:d}};O.prototype.run=async function(){this.v86.run()};O.prototype.stop=async function(){this.cpu_is_running&&
318
+ await new Promise(a=>{const b=()=>{this.remove_listener("emulator-stopped",b);a()};this.add_listener("emulator-stopped",b);this.v86.stop()})};O.prototype.destroy=async function(){await this.stop();this.v86.destroy();this.keyboard_adapter&&this.keyboard_adapter.destroy();this.network_adapter&&this.network_adapter.destroy();this.mouse_adapter&&this.mouse_adapter.destroy();this.screen_adapter&&this.screen_adapter.destroy();this.serial_adapter&&this.serial_adapter.destroy();this.speaker_adapter&&this.speaker_adapter.destroy()};
319
+ O.prototype.restart=function(){this.v86.restart()};O.prototype.add_listener=function(a,b){this.bus.register(a,b,this)};O.prototype.remove_listener=function(a,b){this.bus.unregister(a,b)};O.prototype.restore_state=async function(a){k(1===arguments.length);this.v86.restore_state(a)};O.prototype.save_state=async function(){k(0===arguments.length);return this.v86.save_state()};O.prototype.get_instruction_counter=function(){return this.v86?this.v86.cpu.instruction_counter[0]>>>0:0};O.prototype.is_running=
320
+ function(){return this.cpu_is_running};O.prototype.set_fda=async function(a){if(a.url&&!a.async)sa(a.url,{done:b=>{this.v86.cpu.devices.fdc.set_fda(new ya(b))}});else{const b=Da(a,this.zstd_decompress_worker.bind(this));b.onload=()=>{this.v86.cpu.devices.fdc.set_fda(b)};await b.load()}};O.prototype.eject_fda=function(){this.v86.cpu.devices.fdc.eject_fda()};O.prototype.keyboard_send_scancodes=function(a){for(var b=0;b<a.length;b++)this.bus.send("keyboard-code",a[b])};O.prototype.keyboard_send_keys=
321
+ function(a){for(var b=0;b<a.length;b++)this.keyboard_adapter.simulate_press(a[b])};O.prototype.keyboard_send_text=function(a){for(var b=0;b<a.length;b++)this.keyboard_adapter.simulate_char(a[b])};O.prototype.screen_make_screenshot=function(){return this.screen_adapter?this.screen_adapter.make_screenshot():null};O.prototype.screen_set_scale=function(a,b){this.screen_adapter&&this.screen_adapter.set_scale(a,b)};O.prototype.screen_go_fullscreen=function(){if(this.screen_adapter){var a=document.getElementById("screen_container");
322
+ if(a){var b=a.requestFullScreen||a.webkitRequestFullscreen||a.mozRequestFullScreen||a.msRequestFullScreen;b&&(b.call(a),(a=document.getElementsByClassName("phone_keyboard")[0])&&a.focus());try{navigator.keyboard.lock()}catch(c){}this.lock_mouse()}}};O.prototype.lock_mouse=function(){var a=document.body,b=a.requestPointerLock||a.mozRequestPointerLock||a.webkitRequestPointerLock;b&&b.call(a)};O.prototype.mouse_set_status=function(a){this.mouse_adapter&&(this.mouse_adapter.emu_enabled=a)};O.prototype.keyboard_set_status=
323
+ function(a){this.keyboard_adapter&&(this.keyboard_adapter.emu_enabled=a)};O.prototype.serial0_send=function(a){for(var b=0;b<a.length;b++)this.bus.send("serial0-input",a.charCodeAt(b))};O.prototype.serial_send_bytes=function(a,b){for(var c=0;c<b.length;c++)this.bus.send("serial"+a+"-input",b[c])};O.prototype.serial_set_modem_status=function(a,b){this.bus.send("serial"+a+"-modem-status-input",b)};O.prototype.serial_set_carrier_detect=function(a,b){this.bus.send("serial"+a+"-carrier-detect-input",b)};
324
+ O.prototype.serial_set_ring_indicator=function(a,b){this.bus.send("serial"+a+"-ring-indicator-input",b)};O.prototype.serial_set_data_set_ready=function(a,b){this.bus.send("serial"+a+"-data-set-ready-input",b)};O.prototype.serial_set_clear_to_send=function(a,b){this.bus.send("serial"+a+"-clear-to-send-input",b)};O.prototype.mount_fs=async function(a,b,c){var d=new nc;b&&(d=new oc(d,b));d=new N(d,this.fs9p.qidcounter);b&&(k("object"===typeof c,"Filesystem: basefs must be a JSON object"),d.load_from_json(c));
325
+ a=this.fs9p.Mount(a,d);if(-2===a)throw new vc;if(-17===a)throw new wc;if(0>a)throw k(!1,"Unexpected error code: "+-a),Error("Failed to mount. Error number: "+-a);};O.prototype.create_file=async function(a,b){k(2===arguments.length);var c=this.fs9p;if(c){var d=a.split("/");d=d[d.length-1];var e=c.SearchPath(a).parentid;if(""!==d&&-1!==e)await c.CreateBinaryFile(d,e,b);else return Promise.reject(new vc)}};O.prototype.read_file=async function(a){k(1===arguments.length);var b=this.fs9p;if(b)return(b=
326
+ await b.read_file(a))?b:Promise.reject(new vc)};O.prototype.automatically=function(a){const b=c=>{const d=c[0];if(d){var e=c.slice(1);d.sleep?setTimeout(()=>b(e),1E3*d.sleep):d.vga_text?this.wait_until_vga_screen_contains(d.vga_text).then(()=>b(e)):d.keyboard_send?(Array.isArray(d.keyboard_send)?this.keyboard_send_scancodes(d.keyboard_send):(k("string"===typeof d.keyboard_send),this.keyboard_send_text(d.keyboard_send)),b(e)):d.call?(d.call(),b(e)):k(!1,d)}};b(a)};O.prototype.wait_until_vga_screen_contains=
327
+ function(a){return new Promise(b=>{function c(h){return"string"===typeof a?h.includes(a):a.test(h)}function d(h){[h]=h;e.add(h)}for(const h of this.screen_adapter.get_text_screen())if(c(h)){b(!0);return}const e=new Set,f=()=>{for(const h of e){const l=this.screen_adapter.get_text_row(h);if(c(l)){this.remove_listener("screen-put-char",d);b();return}}e.clear();setTimeout(f,100)};f();this.add_listener("screen-put-char",d)})};O.prototype.read_memory=function(a,b){return this.v86.cpu.read_blob(a,b)};O.prototype.write_memory=
328
+ function(a,b){this.v86.cpu.write_blob(a,b)};O.prototype.set_serial_container_xtermjs=function(a){this.serial_adapter&&this.serial_adapter.destroy&&this.serial_adapter.destroy();this.serial_adapter=new lc(a,this.bus);this.serial_adapter.show()};O.prototype.get_instruction_stats=function(){var a=this.v86.cpu;var b="";var c="COMPILE COMPILE_SKIPPED_NO_NEW_ENTRY_POINTS COMPILE_WRONG_ADDRESS_SPACE COMPILE_CUT_OFF_AT_END_OF_PAGE COMPILE_WITH_LOOP_SAFETY COMPILE_PAGE COMPILE_PAGE/COMPILE COMPILE_BASIC_BLOCK COMPILE_DUPLICATED_BASIC_BLOCK COMPILE_WASM_BLOCK COMPILE_WASM_LOOP COMPILE_DISPATCHER COMPILE_ENTRY_POINT COMPILE_WASM_TOTAL_BYTES COMPILE_WASM_TOTAL_BYTES/COMPILE_PAGE RUN_INTERPRETED RUN_INTERPRETED_NEW_PAGE RUN_INTERPRETED_PAGE_HAS_CODE RUN_INTERPRETED_PAGE_HAS_ENTRY_AFTER_PAGE_WALK RUN_INTERPRETED_NEAR_END_OF_PAGE RUN_INTERPRETED_DIFFERENT_STATE RUN_INTERPRETED_DIFFERENT_STATE_CPL3 RUN_INTERPRETED_DIFFERENT_STATE_FLAT RUN_INTERPRETED_DIFFERENT_STATE_IS32 RUN_INTERPRETED_DIFFERENT_STATE_SS32 RUN_INTERPRETED_MISSED_COMPILED_ENTRY_RUN_INTERPRETED RUN_INTERPRETED_STEPS RUN_FROM_CACHE RUN_FROM_CACHE_STEPS RUN_FROM_CACHE_STEPS/RUN_FROM_CACHE RUN_FROM_CACHE_STEPS/RUN_INTERPRETED_STEPS DIRECT_EXIT INDIRECT_JUMP INDIRECT_JUMP_NO_ENTRY NORMAL_PAGE_CHANGE NORMAL_FALLTHRU NORMAL_FALLTHRU_WITH_TARGET_BLOCK NORMAL_BRANCH NORMAL_BRANCH_WITH_TARGET_BLOCK CONDITIONAL_JUMP CONDITIONAL_JUMP_PAGE_CHANGE CONDITIONAL_JUMP_EXIT CONDITIONAL_JUMP_FALLTHRU CONDITIONAL_JUMP_FALLTHRU_WITH_TARGET_BLOCK CONDITIONAL_JUMP_BRANCH CONDITIONAL_JUMP_BRANCH_WITH_TARGET_BLOCK DISPATCHER_SMALL DISPATCHER_LARGE LOOP LOOP_SAFETY CONDITION_OPTIMISED CONDITION_UNOPTIMISED CONDITION_UNOPTIMISED_PF CONDITION_UNOPTIMISED_UNHANDLED_L CONDITION_UNOPTIMISED_UNHANDLED_LE FAILED_PAGE_CHANGE SAFE_READ_FAST SAFE_READ_SLOW_PAGE_CROSSED SAFE_READ_SLOW_NOT_VALID SAFE_READ_SLOW_NOT_USER SAFE_READ_SLOW_IN_MAPPED_RANGE SAFE_WRITE_FAST SAFE_WRITE_SLOW_PAGE_CROSSED SAFE_WRITE_SLOW_NOT_VALID SAFE_WRITE_SLOW_NOT_USER SAFE_WRITE_SLOW_IN_MAPPED_RANGE SAFE_WRITE_SLOW_READ_ONLY SAFE_WRITE_SLOW_HAS_CODE SAFE_READ_WRITE_FAST SAFE_READ_WRITE_SLOW_PAGE_CROSSED SAFE_READ_WRITE_SLOW_NOT_VALID SAFE_READ_WRITE_SLOW_NOT_USER SAFE_READ_WRITE_SLOW_IN_MAPPED_RANGE SAFE_READ_WRITE_SLOW_READ_ONLY SAFE_READ_WRITE_SLOW_HAS_CODE PAGE_FAULT TLB_MISS MAIN_LOOP MAIN_LOOP_IDLE DO_MANY_CYCLES CYCLE_INTERNAL INVALIDATE_ALL_MODULES_NO_FREE_WASM_INDICES INVALIDATE_MODULE_WRITTEN_WHILE_COMPILED INVALIDATE_MODULE_UNUSED_AFTER_OVERWRITE INVALIDATE_MODULE_DIRTY_PAGE INVALIDATE_PAGE_HAD_CODE INVALIDATE_PAGE_HAD_ENTRY_POINTS DIRTY_PAGE_DID_NOT_HAVE_CODE RUN_FROM_CACHE_EXIT_SAME_PAGE RUN_FROM_CACHE_EXIT_NEAR_END_OF_PAGE RUN_FROM_CACHE_EXIT_DIFFERENT_PAGE CLEAR_TLB FULL_CLEAR_TLB TLB_FULL TLB_GLOBAL_FULL MODRM_SIMPLE_REG MODRM_SIMPLE_REG_WITH_OFFSET MODRM_SIMPLE_CONST_OFFSET MODRM_COMPLEX SEG_OFFSET_OPTIMISED SEG_OFFSET_NOT_OPTIMISED SEG_OFFSET_NOT_OPTIMISED_ES SEG_OFFSET_NOT_OPTIMISED_FS SEG_OFFSET_NOT_OPTIMISED_GS SEG_OFFSET_NOT_OPTIMISED_NOT_FLAT".split(" "),
329
+ d=0;const e={};for(let h=0;h<c.length;h++){const l=c[h];var f=void 0;if(l.includes("/")){d++;const [p,n]=l.split("/");f=e[p]/e[n]}else f=e[l]=a.wm.exports.profiler_stat_get(h-d),f=1E8<=f?Math.round(f/1E6)+"m":1E5<=f?Math.round(f/1E3)+"k":f;b+=l+"="+f+"\n"}b+="\n";c=a.wm.exports.get_valid_tlb_entries_count();d=a.wm.exports.get_valid_global_tlb_entries_count();b=b+("TLB_ENTRIES="+c+" ("+d+" global, "+(c-d)+" non-global)\nWASM_TABLE_FREE=")+(a.wm.exports.jit_get_wasm_table_index_free_list_count()+"\n");
330
+ b+="JIT_CACHE_SIZE="+a.wm.exports.jit_get_cache_size()+"\n";b+="FLAT_SEGMENTS="+a.wm.exports.has_flat_segmentation()+"\n";b+="wasm memory size: "+(a.wasm_memory.buffer.byteLength>>20)+"m\n";b=b+"Config:\nJIT_DISABLED="+(a.wm.exports.get_jit_config(0)+"\n");b+="MAX_PAGES="+a.wm.exports.get_jit_config(1)+"\n";b+="JIT_USE_LOOP_SAFETY="+!!a.wm.exports.get_jit_config(2)+"\n";b+="MAX_EXTRA_BASIC_BLOCKS="+a.wm.exports.get_jit_config(3)+"\n";a=[gb(a,!1,!1,!1,!1),gb(a,!0,!1,!1,!1),gb(a,!1,!0,!1,!1),gb(a,!1,
331
+ !1,!0,!1),gb(a,!1,!1,!1,!0)].join("\n\n");return b+a};function wc(a){this.message=a||"File already exists"}wc.prototype=Error.prototype;function vc(a){this.message=a||"File not found"}vc.prototype=Error.prototype;"undefined"!==typeof module&&"undefined"!==typeof module.exports?module.exports.V86=O:"undefined"!==typeof window?window.V86=O:"function"===typeof importScripts&&(self.V86=O);function I(a,b){this.stopping=this.running=!1;this.idle=!0;this.tick_counter=0;this.worker=null;this.cpu=new P(a,
332
+ b,()=>{this.idle&&this.next_tick(0)});this.bus=a;this.register_yield()}I.prototype.run=function(){this.stopping=!1;this.running||(this.running=!0,this.bus.send("emulator-started"));this.next_tick(0)};I.prototype.do_tick=function(){if(this.stopping||!this.running)this.stopping=this.running=!1,this.bus.send("emulator-stopped");else{this.idle=!1;var a=this.cpu.main_loop();this.next_tick(a)}};I.prototype.next_tick=function(a){const b=++this.tick_counter;this.idle=!0;this.yield(a,b)};I.prototype.yield_callback=
333
+ function(a){a===this.tick_counter&&this.do_tick()};I.prototype.stop=function(){this.running&&(this.stopping=!0)};I.prototype.destroy=function(){this.unregister_yield()};I.prototype.restart=function(){this.cpu.reset_cpu();this.cpu.load_bios()};I.prototype.init=function(a){this.cpu.init(a,this.bus);this.bus.send("emulator-ready")};if("undefined"!==typeof process)I.prototype.yield=function(a,b){1>a?global.setImmediate(c=>this.yield_callback(c),b):setTimeout(c=>this.yield_callback(c),a,b)},I.prototype.register_yield=
334
+ function(){},I.prototype.unregister_yield=function(){};else if("undefined"!==typeof Worker){function a(){let b;globalThis.onmessage=function(c){const d=c.data.t;b=b&&clearTimeout(b);1>d?postMessage(c.data.tick):b=setTimeout(()=>postMessage(c.data.tick),d)}}I.prototype.register_yield=function(){const b=URL.createObjectURL(new Blob(["("+a.toString()+")()"],{type:"text/javascript"}));this.worker=new Worker(b);this.worker.onmessage=c=>this.yield_callback(c.data);URL.revokeObjectURL(b)};I.prototype.yield=
335
+ function(b,c){this.worker.postMessage({t:b,tick:c})};I.prototype.unregister_yield=function(){this.worker&&this.worker.terminate();this.worker=null}}else I.prototype.yield=function(a){setTimeout(()=>{this.do_tick()},a)},I.prototype.register_yield=function(){},I.prototype.unregister_yield=function(){};I.prototype.save_state=function(){for(var a=[],b=db(this.cpu,a),c=[],d=0,e=0;e<a.length;e++){var f=a[e].byteLength;c[e]={offset:d,length:f};d+=f;d=d+3&-4}b=JSON.stringify({buffer_infos:c,state:b});b=(new TextEncoder).encode(b);
336
+ e=16+b.length;e=e+3&-4;var h=e+d;d=new ArrayBuffer(h);var l=new Int32Array(d,0,4);(new Uint8Array(d,16,b.length)).set(b);f=new Uint8Array(d,e);l[0]=-2039052682;l[1]=6;l[2]=h;l[3]=b.length;for(e=0;e<a.length;e++)h=a[e],k(h.constructor===Uint8Array),f.set(h,c[e].offset);C("State: json size "+(b.byteLength>>10)+"k");C("State: Total buffers size "+(f.byteLength>>10)+"k");return d};I.prototype.restore_state=function(a){return fb(this.cpu,a)};if("object"===typeof performance&&performance.now)I.microtick=
337
+ performance.now.bind(performance);else if("function"===typeof require){const {performance:a}=require("perf_hooks");I.microtick=a.now.bind(a)}else I.microtick="object"===typeof process&&process.hrtime?function(){var a=process.hrtime();return 1E3*a[0]+a[1]/1E6}:Date.now;const xc="Fixed (0);Lowest Prio (1);SMI (2);Reserved (3);NMI (4);INIT (5);Reserved (6);ExtINT (7)".split(";"),yc=["physical","logical"];function zc(a){this.cpu=a;this.timer_divider=this.apic_id=0;this.timer_divider_shift=1;this.timer_current_count=
338
+ this.timer_initial_count=0;this.next_tick=I.microtick();this.lvt_error=this.lvt_int1=this.lvt_int0=this.lvt_perf_counter=this.lvt_thermal_sensor=this.lvt_timer=65536;this.icr1=this.icr0=this.tpr=0;this.irr=new Int32Array(8);this.isr=new Int32Array(8);this.tmr=new Int32Array(8);this.spurious_vector=254;this.destination_format=-1;this.read_error=this.error=this.local_destination=0;a.io.mmap_register(4276092928,1048576,b=>{C("Unsupported read8 from apic: "+m(b>>>0),524288);var c=b&3;return this.read32(b&
339
+ -4)>>8*c&255},(b,c)=>{C("Unsupported write8 from apic: "+m(b)+" <- "+m(c),524288);C(Error().stack,void 0);k(!1)},b=>this.read32(b),(b,c)=>this.write32(b,c))}zc.prototype.read32=function(a){a=a-4276092928|0;switch(a){case 32:return C("APIC read id",524288),this.apic_id;case 48:return C("APIC read version",524288),327700;case 128:return this.tpr;case 208:return C("Read local destination",524288),this.local_destination;case 224:return C("Read destination format",524288),this.destination_format;case 240:return this.spurious_vector;
340
+ case 256:case 272:case 288:case 304:case 320:case 336:case 352:case 368:return a=a-256>>4,C("Read isr "+a+": "+m(this.isr[a]>>>0,8),524288),this.isr[a];case 384:case 400:case 416:case 432:case 448:case 464:case 480:case 496:return a=a-384>>4,C("Read tmr "+a+": "+m(this.tmr[a]>>>0,8),524288),this.tmr[a];case 512:case 528:case 544:case 560:case 576:case 592:case 608:case 624:return a=a-512>>4,C("Read irr "+a+": "+m(this.irr[a]>>>0,8),524288),this.irr[a];case 640:return C("Read error: "+m(this.read_error>>>
341
+ 0,8),524288),this.read_error;case 768:return this.icr0;case 784:return C("APIC read icr1",524288),this.icr1;case 800:return C("read timer lvt",524288),this.lvt_timer;case 816:return C("read lvt thermal sensor",524288),this.lvt_thermal_sensor;case 832:return C("read lvt perf counter",524288),this.lvt_perf_counter;case 848:return C("read lvt int0",524288),this.lvt_int0;case 864:return C("read lvt int1",524288),this.lvt_int1;case 880:return C("read lvt error",524288),this.lvt_error;case 992:return C("read timer divider",
342
+ 524288),this.timer_divider;case 896:return C("read timer initial count",524288),this.timer_initial_count;case 912:return C("read timer current count: "+m(this.timer_current_count>>>0,8),524288),this.timer_current_count;default:return C("APIC read "+m(a),524288),k(!1),0}};zc.prototype.write32=function(a,b){a=a-4276092928|0;switch(a){case 32:C("APIC write id: "+m(b>>>8,8),524288);this.apic_id=b;break;case 48:C("APIC write version: "+m(b>>>0,8)+", ignored",524288);break;case 128:this.tpr=b&255;this.check_vector();
343
+ break;case 176:b=this.highest_isr();-1!==b?(this.register_clear_bit(this.isr,b),this.register_get_bit(this.tmr,b)&&this.cpu.devices.ioapic.remote_eoi(b),this.check_vector()):C("Bad eoi: No isr set",524288);break;case 208:C("Set local destination: "+m(b>>>0,8),524288);this.local_destination=b&4278190080;break;case 224:C("Set destination format: "+m(b>>>0,8),524288);this.destination_format=b|16777215;break;case 240:C("Set spurious vector: "+m(b>>>0,8),524288);this.spurious_vector=b;break;case 640:C("Write error: "+
344
+ m(b>>>0,8),524288);this.read_error=this.error;this.error=0;break;case 768:a=b&255;var c=b>>8&7,d=b>>11&1,e=b>>15&1,f=b>>18&3,h=this.icr1>>>24;C("APIC write icr0: "+m(b,8)+" vector="+m(a,2)+" destination_mode="+yc[d]+" delivery_mode="+xc[c]+" destination_shorthand="+["no","self","all with self","all without self"][f],524288);this.icr0=b&-4097;0===f?this.route(a,c,e,h,d):1===f?this.deliver(a,0,e):2===f?this.deliver(a,c,e):3!==f&&k(!1);break;case 784:C("APIC write icr1: "+m(b>>>0,8),524288);this.icr1=
345
+ b;break;case 800:C("timer lvt: "+m(b>>>0,8),524288);this.lvt_timer=b;break;case 816:C("lvt thermal sensor: "+m(b>>>0,8),524288);this.lvt_thermal_sensor=b;break;case 832:C("lvt perf counter: "+m(b>>>0,8),524288);this.lvt_perf_counter=b;break;case 848:C("lvt int0: "+m(b>>>0,8),524288);this.lvt_int0=b;break;case 864:C("lvt int1: "+m(b>>>0,8),524288);this.lvt_int1=b;break;case 880:C("lvt error: "+m(b>>>0,8),524288);this.lvt_error=b;break;case 992:C("timer divider: "+m(b>>>0,8),524288);this.timer_divider=
346
+ b;b=b&3|(b&8)>>1;this.timer_divider_shift=7===b?0:b+1;break;case 896:C("timer initial: "+m(b>>>0,8),524288);this.timer_initial_count=b>>>0;this.timer_current_count=b>>>0;this.next_tick=I.microtick();this.timer_active=!0;break;case 912:C("timer current: "+m(b>>>0,8),524288);k(!1,"read-only register");break;default:C("APIC write32 "+m(a)+" <- "+m(b>>>0,8),524288),k(!1)}};zc.prototype.timer=function(a){if(0===this.timer_current_count)return 100;const b=1E6/(1<<this.timer_divider_shift);a=(a-this.next_tick)*
347
+ b>>>0;this.next_tick+=a/b;this.timer_current_count-=a;0>=this.timer_current_count&&(a=this.lvt_timer&393216,131072===a?(this.timer_current_count%=this.timer_initial_count,0>=this.timer_current_count&&(this.timer_current_count+=this.timer_initial_count),k(0!==this.timer_current_count),0===(this.lvt_timer&65536)&&this.deliver(this.lvt_timer&255,0,!1)):0===a&&(this.timer_current_count=0,C("APIC timer one shot end",524288),0===(this.lvt_timer&65536)&&this.deliver(this.lvt_timer&255,0,!1)));return Math.max(0,
348
+ this.timer_current_count/b)};zc.prototype.route=function(a,b,c){this.deliver(a,b,c)};zc.prototype.deliver=function(a,b,c){5!==b&&4!==b&&((16>a||255===a)&&k(!1,"TODO: Invalid vector"),this.register_get_bit(this.irr,a)?C("Not delivered: irr already set, vector="+m(a,2),524288):(this.register_set_bit(this.irr,a),c?this.register_set_bit(this.tmr,a):this.register_clear_bit(this.tmr,a),this.check_vector()))};zc.prototype.highest_irr=function(){var a=this.register_get_highest_bit(this.irr);k(255!==a);k(16<=
349
+ a||-1===a);return a};zc.prototype.highest_isr=function(){var a=this.register_get_highest_bit(this.isr);k(255!==a);k(16<=a||-1===a);return a};zc.prototype.check_vector=function(){var a=this.highest_irr();-1!==a&&(this.highest_isr()>=a||(a&240)<=(this.tpr&240)||this.cpu.handle_irqs())};zc.prototype.acknowledge_irq=function(){var a=this.highest_irr();if(-1===a||this.highest_isr()>=a||(a&240)<=(this.tpr&240))return-1;this.register_clear_bit(this.irr,a);this.register_set_bit(this.isr,a);this.check_vector();
350
+ return a};zc.prototype.get_state=function(){var a=[];a[0]=this.apic_id;a[1]=this.timer_divider;a[2]=this.timer_divider_shift;a[3]=this.timer_initial_count;a[4]=this.timer_current_count;a[5]=this.next_tick;a[6]=this.lvt_timer;a[7]=this.lvt_perf_counter;a[8]=this.lvt_int0;a[9]=this.lvt_int1;a[10]=this.lvt_error;a[11]=this.tpr;a[12]=this.icr0;a[13]=this.icr1;a[14]=this.irr;a[15]=this.isr;a[16]=this.tmr;a[17]=this.spurious_vector;a[18]=this.destination_format;a[19]=this.local_destination;a[20]=this.error;
351
+ a[21]=this.read_error;a[22]=this.lvt_thermal_sensor;return a};zc.prototype.set_state=function(a){this.apic_id=a[0];this.timer_divider=a[1];this.timer_divider_shift=a[2];this.timer_initial_count=a[3];this.timer_current_count=a[4];this.next_tick=a[5];this.lvt_timer=a[6];this.lvt_perf_counter=a[7];this.lvt_int0=a[8];this.lvt_int1=a[9];this.lvt_error=a[10];this.tpr=a[11];this.icr0=a[12];this.icr1=a[13];this.irr=a[14];this.isr=a[15];this.tmr=a[16];this.spurious_vector=a[17];this.destination_format=a[18];
352
+ this.local_destination=a[19];this.error=a[20];this.read_error=a[21];this.lvt_thermal_sensor=a[22]||65536};zc.prototype.register_get_bit=function(a,b){k(0<=b&&256>b);return a[b>>5]>>(b&31)&1};zc.prototype.register_set_bit=function(a,b){k(0<=b&&256>b);a[b>>5]|=1<<(b&31)};zc.prototype.register_clear_bit=function(a,b){k(0<=b&&256>b);a[b>>5]&=~(1<<(b&31))};zc.prototype.register_get_highest_bit=function(a){for(var b=7;0<=b;b--){var c=a[b];if(c)return ea(c>>>0)|b<<5}return-1};function Ac(a){this.cpu=a;this.ioredtbl_config=
353
+ new Int32Array(24);this.ioredtbl_destination=new Int32Array(24);for(var b=0;b<this.ioredtbl_config.length;b++)this.ioredtbl_config[b]=65536;this.irq_value=this.irr=this.ioapic_id=this.ioregsel=0;k(!0);a.io.mmap_register(4273995776,131072,c=>{c=c-4273995776|0;if(16<=c&&20>c)return c-=16,C("ioapic read8 byte "+c+" "+m(this.ioregsel),524288),this.read(this.ioregsel)>>8*c&255;C("Unexpected IOAPIC register read: "+m(c>>>0),524288);k(!1);return 0},c=>{k(!1,"unsupported write8 from ioapic: "+m(c>>>0))},
354
+ c=>{c=c-4273995776|0;if(0===c)return this.ioregsel;if(16===c)return this.read(this.ioregsel);C("Unexpected IOAPIC register read: "+m(c>>>0),524288);k(!1);return 0},(c,d)=>{c=c-4273995776|0;0===c?this.ioregsel=d:16===c?this.write(this.ioregsel,d):(C("Unexpected IOAPIC register write: "+m(c>>>0)+" <- "+m(d>>>0,8),524288),k(!1))})}Ac.prototype.remote_eoi=function(a){for(var b=0;24>b;b++){var c=this.ioredtbl_config[b];(c&255)===a&&c&16384&&(C("Clear remote IRR for irq="+m(b),524288),this.ioredtbl_config[b]&=
355
+ -16385,this.check_irq(b))}};Ac.prototype.check_irq=function(a){var b=1<<a;if(0!==(this.irr&b)){var c=this.ioredtbl_config[a];if(0===(c&65536)){var d=c>>8&7,e=c>>11&1,f=c&255,h=this.ioredtbl_destination[a]>>>24,l=32768===(c&32768);if(0===(c&32768))this.irr&=~b;else if(this.ioredtbl_config[a]|=16384,c&16384){C("No route: level interrupt and remote IRR still set",524288);return}0===d||1===d?this.cpu.devices.apic.route(f,d,l,h,e):k(!1,"TODO");this.ioredtbl_config[a]&=-4097}}};Ac.prototype.set_irq=function(a){if(24<=
356
+ a)k(!1,"Bad irq: "+a,524288);else{var b=1<<a;0===(this.irq_value&b)&&(this.irq_value|=b,65536!==(this.ioredtbl_config[a]&98304)&&(this.irr|=b,this.check_irq(a)))}};Ac.prototype.clear_irq=function(a){if(24<=a)k(!1,"Bad irq: "+a,524288);else{var b=1<<a;(this.irq_value&b)===b&&(this.irq_value&=~b,this.ioredtbl_config[a]&32768&&(this.irr&=~b))}};Ac.prototype.read=function(a){if(0===a)return C("IOAPIC Read id",524288),this.ioapic_id<<24;if(1===a)return C("IOAPIC Read version",524288),1507345;if(2===a)return C("IOAPIC Read arbitration id",
357
+ 524288),this.ioapic_id<<24;if(16<=a&&64>a){var b=a-16>>1;a&1?(a=this.ioredtbl_destination[b],C("IOAPIC Read destination irq="+m(b)+" -> "+m(a,8),524288)):(a=this.ioredtbl_config[b],C("IOAPIC Read config irq="+m(b)+" -> "+m(a,8),524288));return a}C("IOAPIC register read outside of range "+m(a),524288);k(!1);return 0};Ac.prototype.write=function(a,b){if(0===a)this.ioapic_id=b>>>24&15;else if(1===a||2===a)C("Invalid write: "+a,524288);else if(16<=a&&64>a){var c=a-16>>1;if(a&1)this.ioredtbl_destination[c]=
358
+ b&4278190080,C("Write destination "+m(b>>>0,8)+" irq="+m(c)+" dest="+m(b>>>24,2),524288);else{this.ioredtbl_config[c]=b&110591|this.ioredtbl_config[c]&-110592;a=b&255;var d=b>>8&7,e=b>>11&1,f=b>>15&1,h=b>>16&1;C("Write config "+m(b>>>0,8)+" irq="+m(c)+" vector="+m(a,2)+" deliverymode="+xc[d]+" destmode="+yc[e]+" is_level="+f+" disabled="+h,524288);this.check_irq(c)}}else C("IOAPIC register write outside of range "+m(a)+": "+m(b>>>0,8),524288),k(!1)};Ac.prototype.get_state=function(){var a=[];a[0]=
359
+ this.ioredtbl_config;a[1]=this.ioredtbl_destination;a[2]=this.ioregsel;a[3]=this.ioapic_id;a[4]=this.irr;a[5]=this.irq_value;return a};Ac.prototype.set_state=function(a){this.ioredtbl_config=a[0];this.ioredtbl_destination=a[1];this.ioregsel=a[2];this.ioapic_id=a[3];this.irr=a[4];this.irq_value=a[5]};function Bc(a){this.cpu=a;var b=a.io;a.devices.pci.register_device({pci_id:56,pci_space:[134,128,19,113,7,0,128,2,8,0,128,6,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
360
+ 0,0,0,0,0,0,0,0,0,0,0,9,1,0,0],pci_bars:[],name:"acpi"});this.timer_imprecision_offset=this.timer_last_value=0;this.status=1;this.pm1_enable=this.pm1_status=0;this.last_timer=this.get_timer(I.microtick());this.gpe=new Uint8Array(4);b.register_read(45056,this,void 0,function(){C("ACPI pm1_status read",262144);return this.pm1_status});b.register_write(45056,this,void 0,function(c){C("ACPI pm1_status write: "+m(c,4),262144);this.pm1_status&=~c});b.register_read(45058,this,void 0,function(){C("ACPI pm1_enable read",
361
+ 262144);return this.pm1_enable});b.register_write(45058,this,void 0,function(c){C("ACPI pm1_enable write: "+m(c),262144);this.pm1_enable=c});b.register_read(45060,this,void 0,function(){C("ACPI status read",262144);return this.status});b.register_write(45060,this,void 0,function(c){C("ACPI status write: "+m(c),262144);this.status=c});b.register_read(45064,this,void 0,void 0,function(){return this.get_timer(I.microtick())&16777215});b.register_read(45024,this,function(){C("Read gpe#0",262144);return this.gpe[0]});
362
+ b.register_read(45025,this,function(){C("Read gpe#1",262144);return this.gpe[1]});b.register_read(45026,this,function(){C("Read gpe#2",262144);return this.gpe[2]});b.register_read(45027,this,function(){C("Read gpe#3",262144);return this.gpe[3]});b.register_write(45024,this,function(c){C("Write gpe#0: "+m(c),262144);this.gpe[0]=c});b.register_write(45025,this,function(c){C("Write gpe#1: "+m(c),262144);this.gpe[1]=c});b.register_write(45026,this,function(c){C("Write gpe#2: "+m(c),262144);this.gpe[2]=
363
+ c});b.register_write(45027,this,function(c){C("Write gpe#3: "+m(c),262144);this.gpe[3]=c})}Bc.prototype.timer=function(a){a=this.get_timer(a);var b=0!==((a^this.last_timer)&8388608);this.pm1_enable&1&&b?(C("ACPI raise irq",262144),this.pm1_status|=1,this.cpu.device_raise_irq(9)):this.cpu.device_lower_irq(9);this.last_timer=a;return 100};Bc.prototype.get_timer=function(a){a=Math.round(3579.545*a);a===this.timer_last_value?3579.545>this.timer_imprecision_offset&&this.timer_imprecision_offset++:(k(a>
364
+ this.timer_last_value),this.timer_last_value+this.timer_imprecision_offset<=a?(this.timer_imprecision_offset=0,this.timer_last_value=a):C("Warning: Overshot pmtimer, waiting; current="+a+" last="+this.timer_last_value+" offset="+this.timer_imprecision_offset,262144));return this.timer_last_value+this.timer_imprecision_offset};Bc.prototype.get_state=function(){var a=[];a[0]=this.status;a[1]=this.pm1_status;a[2]=this.pm1_enable;a[3]=this.gpe;return a};Bc.prototype.set_state=function(a){this.status=
365
+ a[0];this.pm1_status=a[1];this.pm1_enable=a[2];this.gpe=a[3]};function Cc(a,b,c){this.bus=c;this.cpu=a;this.ints=4;this.line_control=this.baud_rate=0;this.lsr=96;this.ier=this.fifo_control=0;this.iir=1;this.irq=this.scratch_register=this.modem_status=this.modem_control=0;this.input=[];this.current_line="";switch(b){case 1016:this.com=0;this.irq=4;break;case 760:this.com=1;this.irq=3;break;case 1E3:this.com=2;this.irq=4;break;case 744:this.irq=this.com=3;break;default:C("Invalid serial port: "+m(b),
366
+ 16384),this.com=0,this.irq=4}this.bus.register("serial"+this.com+"-input",function(d){this.data_received(d)},this);this.bus.register("serial"+this.com+"-modem-status-input",function(d){this.set_modem_status(d)},this);this.bus.register("serial"+this.com+"-carrier-detect-input",function(d){this.set_modem_status(d?this.modem_status|136:this.modem_status&-137)},this);this.bus.register("serial"+this.com+"-ring-indicator-input",function(d){this.set_modem_status(d?this.modem_status|68:this.modem_status&
367
+ -69)},this);this.bus.register("serial"+this.com+"-data-set-ready-input",function(d){this.set_modem_status(d?this.modem_status|34:this.modem_status&-35)},this);this.bus.register("serial"+this.com+"-clear-to-send-input",function(d){this.set_modem_status(d?this.modem_status|17:this.modem_status&-18)},this);a=a.io;a.register_write(b,this,function(d){this.write_data(d)},function(d){this.write_data(d&255);this.write_data(d>>8)});a.register_write(b|1,this,function(d){this.line_control&128?(this.baud_rate=
368
+ this.baud_rate&255|d<<8,C("baud rate: "+m(this.baud_rate),16384)):(0===(this.ier&2)&&d&2&&this.ThrowInterrupt(2),this.ier=d&15,C("interrupt enable: "+m(d),16384),this.CheckInterrupt())});a.register_read(b,this,function(){if(this.line_control&128)return this.baud_rate&255;let d=0;0===this.input.length?C("Read input empty",16384):(d=this.input.shift(),C("Read input: "+m(d),16384));0===this.input.length&&(this.lsr&=-2,this.ClearInterrupt(12),this.ClearInterrupt(4));return d});a.register_read(b|1,this,
369
+ function(){return this.line_control&128?this.baud_rate>>8:this.ier&15});a.register_read(b|2,this,function(){var d=this.iir&15;C("read interrupt identification: "+m(this.iir),16384);2===this.iir&&this.ClearInterrupt(2);this.fifo_control&1&&(d|=192);return d});a.register_write(b|2,this,function(d){C("fifo control: "+m(d),16384);this.fifo_control=d});a.register_read(b|3,this,function(){C("read line control: "+m(this.line_control),16384);return this.line_control});a.register_write(b|3,this,function(d){C("line control: "+
370
+ m(d),16384);this.line_control=d});a.register_read(b|4,this,function(){return this.modem_control});a.register_write(b|4,this,function(d){C("modem control: "+m(d),16384);this.modem_control=d});a.register_read(b|5,this,function(){C("read line status: "+m(this.lsr),16384);return this.lsr});a.register_write(b|5,this,function(){C("Factory test write",16384)});a.register_read(b|6,this,function(){C("read modem status: "+m(this.modem_status),16384);return this.modem_status&=240});a.register_write(b|6,this,
371
+ function(d){C("write modem status: "+m(d),16384);this.set_modem_status(d)});a.register_read(b|7,this,function(){return this.scratch_register});a.register_write(b|7,this,function(d){this.scratch_register=d})}Cc.prototype.get_state=function(){var a=[];a[0]=this.ints;a[1]=this.baud_rate;a[2]=this.line_control;a[3]=this.lsr;a[4]=this.fifo_control;a[5]=this.ier;a[6]=this.iir;a[7]=this.modem_control;a[8]=this.modem_status;a[9]=this.scratch_register;a[10]=this.irq;return a};Cc.prototype.set_state=function(a){this.ints=
372
+ a[0];this.baud_rate=a[1];this.line_control=a[2];this.lsr=a[3];this.fifo_control=a[4];this.ier=a[5];this.iir=a[6];this.modem_control=a[7];this.modem_status=a[8];this.scratch_register=a[9];this.irq=a[10]};Cc.prototype.CheckInterrupt=function(){this.ints&4096&&this.ier&1?(this.iir=12,this.cpu.device_raise_irq(this.irq)):this.ints&16&&this.ier&1?(this.iir=4,this.cpu.device_raise_irq(this.irq)):this.ints&4&&this.ier&2?(this.iir=2,this.cpu.device_raise_irq(this.irq)):this.ints&1&&this.ier&8?(this.iir=0,
373
+ this.cpu.device_raise_irq(this.irq)):(this.iir=1,this.cpu.device_lower_irq(this.irq))};Cc.prototype.ThrowInterrupt=function(a){this.ints|=1<<a;this.CheckInterrupt()};Cc.prototype.ClearInterrupt=function(a){this.ints&=~(1<<a);this.CheckInterrupt()};Cc.prototype.data_received=function(a){C("input: "+m(a),16384);this.input.push(a);this.lsr|=1;this.fifo_control&1?this.ThrowInterrupt(12):this.ThrowInterrupt(4)};Cc.prototype.write_data=function(a){this.line_control&128?this.baud_rate=this.baud_rate&-256|
374
+ a:(C("data: "+m(a),16384),this.ThrowInterrupt(2),this.bus.send("serial"+this.com+"-output-byte",a),a=String.fromCharCode(a),this.current_line+=a,"\n"===a&&(a=this.current_line.trimRight().replace(/[\x00-\x08\x0b-\x1f\x7f\x80-\xff]/g,""),C("SERIAL: "+a),this.current_line=""))};Cc.prototype.set_modem_status=function(a){C("modem status: "+m(a),16384);const b=this.modem_status&15;let c=(this.modem_status^a)>>4;this.modem_status=a;this.modem_status=this.modem_status|c|b};function Dc(a){this.pci_addr=new Uint8Array(4);
375
+ this.pci_value=new Uint8Array(4);this.pci_response=new Uint8Array(4);this.pci_status=new Uint8Array(4);this.pci_addr32=new Int32Array(this.pci_addr.buffer);this.pci_value32=new Int32Array(this.pci_value.buffer);this.pci_response32=new Int32Array(this.pci_response.buffer);this.pci_status32=new Int32Array(this.pci_status.buffer);this.device_spaces=[];this.devices=[];this.cpu=a;for(var b=0;256>b;b++)this.device_spaces[b]=void 0,this.devices[b]=void 0;this.io=a.io;a.io.register_write(3324,this,function(c){this.pci_write8(this.pci_addr32[0],
376
+ c)},function(c){this.pci_write16(this.pci_addr32[0],c)},function(c){this.pci_write32(this.pci_addr32[0],c)});a.io.register_write(3325,this,function(c){this.pci_write8(this.pci_addr32[0]+1|0,c)});a.io.register_write(3326,this,function(c){this.pci_write8(this.pci_addr32[0]+2|0,c)},function(c){this.pci_write16(this.pci_addr32[0]+2|0,c)});a.io.register_write(3327,this,function(c){this.pci_write8(this.pci_addr32[0]+3|0,c)});a.io.register_read_consecutive(3324,this,function(){return this.pci_response[0]},
377
+ function(){return this.pci_response[1]},function(){return this.pci_response[2]},function(){return this.pci_response[3]});a.io.register_read_consecutive(3320,this,function(){return this.pci_status[0]},function(){return this.pci_status[1]},function(){return this.pci_status[2]},function(){return this.pci_status[3]});a.io.register_write_consecutive(3320,this,function(c){this.pci_addr[0]=c&252},function(c){2===(this.pci_addr[1]&6)&&6===(c&6)?(C("CPU reboot via PCI"),a.reboot_internal()):this.pci_addr[1]=
378
+ c},function(c){this.pci_addr[2]=c},function(c){this.pci_addr[3]=c;this.pci_query()});this.register_device({pci_id:0,pci_space:[134,128,55,18,0,0,0,0,2,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0],pci_bars:[],name:"82441FX PMC"});this.isa_bridge={pci_id:8,pci_space:[134,128,0,112,7,0,0,2,0,0,1,6,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
379
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],pci_bars:[],name:"82371SB PIIX3 ISA"};this.isa_bridge_space=this.register_device(this.isa_bridge);this.isa_bridge_space8=new Uint8Array(this.isa_bridge_space.buffer)}Dc.prototype.get_state=function(){for(var a=[],b=0;256>b;b++)a[b]=this.device_spaces[b];a[256]=this.pci_addr;a[257]=this.pci_value;a[258]=this.pci_response;a[259]=this.pci_status;return a};Dc.prototype.set_state=function(a){for(var b=0;256>b;b++){var c=this.devices[b],d=a[b];if(c&&d){for(var e=0;e<c.pci_bars.length;e++){var f=
380
+ d[4+e];if(f&1){var h=c.pci_bars[e];this.set_io_bars(h,h.original_bar&65534,f&65534)}}this.device_spaces[b].set(d)}else c&&C("Warning: While restoring PCI device: Device exists in current configuration but not in snapshot ("+c.name+")"),d&&C("Warning: While restoring PCI device: Device doesn't exist in current configuration but does in snapshot (device "+m(b,2)+")")}this.pci_addr.set(a[256]);this.pci_value.set(a[257]);this.pci_response.set(a[258]);this.pci_status.set(a[259])};Dc.prototype.pci_query=
381
+ function(){var a=this.pci_addr[2]<<8|this.pci_addr[1],b=this.pci_addr[0]&252,c=a>>3&31;var d="query enabled="+(this.pci_addr[3]>>7)+(" bdf="+m(a,4));d+=" dev="+m(c,2);d+=" addr="+m(b,2);c=this.device_spaces[a];void 0!==c?(this.pci_status32[0]=-2147483648,this.pci_response32[0]=b<c.byteLength?c[b>>2]:0,d+=" "+m(this.pci_addr32[0]>>>0,8)+" -> "+m(this.pci_response32[0]>>>0,8),b>=c.byteLength&&(d+=" (undef)"),d+=" ("+this.devices[a].name+")",C(d,2048)):(this.pci_response32[0]=-1,this.pci_status32[0]=
382
+ 0)};Dc.prototype.pci_write8=function(a,b){var c=a>>8&65535;a&=255;var d=new Uint8Array(this.device_spaces[c].buffer),e=this.devices[c];d&&(k(!(16<=a&&44>a||48<=a&&52>a),"PCI: Expected 32-bit write, got 8-bit (addr: "+m(a)+")"),C("PCI write8 dev="+m(c>>3,2)+" ("+e.name+") addr="+m(a,4)+" value="+m(b,2),2048),d[a]=b)};Dc.prototype.pci_write16=function(a,b){k(0===(a&1));var c=a>>8&65535;a&=255;var d=new Uint16Array(this.device_spaces[c].buffer),e=this.devices[c];d&&(16<=a&&44>a?C("Warning: PCI: Expected 32-bit write, got 16-bit (addr: "+
383
+ m(a)+")"):(k(!(48<=a&&52>a),"PCI: Expected 32-bit write, got 16-bit (addr: "+m(a)+")"),C("PCI writ16 dev="+m(c>>3,2)+" ("+e.name+") addr="+m(a,4)+" value="+m(b,4),2048),d[a>>>1]=b))};Dc.prototype.pci_write32=function(a,b){k(0===(a&3));var c=a>>8&65535;a&=255;var d=this.device_spaces[c],e=this.devices[c];if(d)if(16<=a&&40>a){var f=a-16>>2,h=e.pci_bars[f];C("BAR"+f+" exists="+(h?"y":"n")+" changed to "+m(b>>>0)+" dev="+m(c>>3,2)+" ("+e.name+") ",2048);h?(k(!(h.size&h.size-1),"bar size should be power of 2"),
384
+ c=a>>2,e=d[c]&1,-1===(b|3|h.size-1)?(b=~(h.size-1)|e,0===e&&(d[c]=b)):0===e&&(f=h.original_bar,(b&-16)!==(f&-16)&&C("Warning: Changing memory bar not supported, ignored",2048),d[c]=f),1===e&&(k(1===e),e=d[c]&65534,f=b&65534,C("io bar changed from "+m(e>>>0,8)+" to "+m(f>>>0,8)+" size="+h.size,2048),this.set_io_bars(h,e,f),d[c]=b|1)):d[a>>2]=0;C("BAR effective value: "+m(d[a>>2]>>>0),2048)}else 48===a?(C("PCI write rom address dev="+m(c>>3,2)+" ("+e.name+") value="+m(b>>>0,8),2048),d[a>>2]=e.pci_rom_size?
385
+ -1===(b|2047)?-e.pci_rom_size|0:e.pci_rom_address|0:0):4===a?C("PCI write dev="+m(c>>3,2)+" ("+e.name+") addr="+m(a,4)+" value="+m(b>>>0,8),2048):(C("PCI write dev="+m(c>>3,2)+" ("+e.name+") addr="+m(a,4)+" value="+m(b>>>0,8),2048),d[a>>>2]=b)};Dc.prototype.register_device=function(a){k(void 0!==a.pci_id);k(void 0!==a.pci_space);k(void 0!==a.pci_bars);var b=a.pci_id;C("PCI register bdf="+m(b)+" ("+a.name+")",2048);k(!this.devices[b]);k(64<=a.pci_space.length);k(b<this.devices.length);var c=new Int32Array(64);
386
+ c.set(new Int32Array((new Uint8Array(a.pci_space)).buffer));this.device_spaces[b]=c;this.devices[b]=a;b=c.slice(4,10);for(var d=0;d<a.pci_bars.length;d++){var e=a.pci_bars[d];if(e){var f=b[d],h=f&1;e.original_bar=f;e.entries=[];if(0!==h)for(k(1===h),f&=-2,h=0;h<e.size;h++)e.entries[h]=this.io.ports[f+h]}}return c};Dc.prototype.set_io_bars=function(a,b,c){var d=a.size;C("Move io bars: from="+m(b)+" to="+m(c)+" count="+d,2048);for(var e=this.io.ports,f=0;f<d;f++){var h=e[b+f];4096<=b+f&&(e[b+f]=this.io.create_empty_entry());
387
+ h.read8===this.io.empty_port_read8&&h.read16===this.io.empty_port_read16&&h.read32===this.io.empty_port_read32&&h.write8===this.io.empty_port_write&&h.write16===this.io.empty_port_write&&h.write32===this.io.empty_port_write&&C("Warning: Bad IO bar: Source not mapped, port="+m(b+f,4),2048);h=a.entries[f];var l=e[c+f];k(h&&l);4096<=c+f&&(e[c+f]=h);l.read8!==this.io.empty_port_read8&&l.read16!==this.io.empty_port_read16&&l.read32!==this.io.empty_port_read32&&l.write8!==this.io.empty_port_write&&l.write16!==
388
+ this.io.empty_port_write&&l.write32!==this.io.empty_port_write||C("Warning: Bad IO bar: Target already mapped, port="+m(c+f,4),2048)}};Dc.prototype.raise_irq=function(a){var b=this.device_spaces[a];k(b);this.cpu.device_raise_irq(this.isa_bridge_space8[96+((b[15]>>8&255)-1+((a>>3)-1&255)&3)])};Dc.prototype.lower_irq=function(a){var b=this.device_spaces[a];k(b);this.cpu.device_lower_irq(this.isa_bridge_space8[96+((b[15]>>8&255)+(a>>3&255)-2&3)])};function Ec(a,b,c){a[0]===b[0]&&a[1]===b[1]&&a[2]===
389
+ b[2]&&a[3]===b[3]&&a[4]===b[4]&&a[5]===b[5]&&(C("Replace mac in eth destination field",1048576),a[0]=c[0],a[1]=c[1],a[2]=c[2],a[3]=c[3],a[4]=c[4],a[5]=c[5]);a[6]===b[0]&&a[7]===b[1]&&a[8]===b[2]&&a[9]===b[3]&&a[10]===b[4]&&a[11]===b[5]&&(C("Replace mac in eth source field",1048576),a[6]=c[0],a[7]=c[1],a[8]=c[2],a[9]=c[3],a[10]=c[4],a[11]=c[5]);var d=a[12]<<8|a[13];if(2048===d)if(a=a.subarray(14),d=a[0]>>4,4!==d)C("Expected ipv4.version==4 but got: "+d,1048576);else{if(k(5===(a[0]&15),"TODO: ihl!=5"),
390
+ 17===a[9]){a=a.subarray(20);d=a[0]<<8|a[1];var e=a[2]<<8|a[3];C("udp srcport="+d+" dstport="+e+" checksum="+m(a[6]<<8|a[7],4),1048576);if(67===d||67===e)if(d=a.subarray(8),e=d[236]<<24|d[237]<<16|d[238]<<8|d[239],1669485411!==e)C("dhcp packet didn't match magic: "+m(e,8));else for(d[28]===b[0]&&d[29]===b[1]&&d[30]===b[2]&&d[31]===b[3]&&d[32]===b[4]&&d[33]===b[5]&&(C("Replace mac in dhcp.chaddr",1048576),d[28]=c[0],d[29]=c[1],d[30]=c[2],d[31]=c[3],d[32]=c[4],d[33]=c[5],a[6]=a[7]=0),e=240;e<d.length;){const f=
391
+ d[e++];if(255===f)break;const h=d[e++];61===f&&1===d[e+0]&&d[e+1]===b[0]&&d[e+2]===b[1]&&d[e+3]===b[2]&&d[e+4]===b[3]&&d[e+5]===b[4]&&d[e+6]===b[5]&&(C("Replace mac in dhcp.clientidentifier",1048576),d[e+1]=c[0],d[e+2]=c[1],d[e+3]=c[2],d[e+4]=c[3],d[e+5]=c[4],d[e+6]=c[5],a[6]=a[7]=0);e+=h}}}else 2054===d&&(a=a.subarray(14),C("arp oper="+a[7]+" "+Fc(a.subarray(8,14))+" "+Fc(a.subarray(18,24)),1048576),a[8]===b[0]&&a[9]===b[1]&&a[10]===b[2]&&a[11]===b[3]&&a[12]===b[4]&&a[13]===b[5]&&(C("Replace mac in arp.sha",
392
+ 1048576),a[8]=c[0],a[9]=c[1],a[10]=c[2],a[11]=c[3],a[12]=c[4],a[13]=c[5]))}function Fc(a){return[a[0].toString(16).padStart(2,"0"),a[1].toString(16).padStart(2,"0"),a[2].toString(16).padStart(2,"0"),a[3].toString(16).padStart(2,"0"),a[4].toString(16).padStart(2,"0"),a[5].toString(16).padStart(2,"0")].join(":")}function Gc(a,b,c,d,e){this.cpu=a;this.pci=a.devices.pci;this.id=e||0;this.preserve_mac_from_state_image=c;this.mac_address_translation=d;this.bus=b;this.bus.register("net"+this.id+"-receive",
393
+ function(f){this.receive(f)},this);this.port=768+256*this.id;this.name="ne2k";this.pci_space=[236,16,41,128,3,1,0,0,0,0,0,2,0,0,0,0,this.port&255|1,this.port>>8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,0,17,0,0,184,254,0,0,0,0,0,0,0,0,0,1,0,0];this.pci_id=(0===this.id?5:7+this.id)<<3;this.pci_bars=[{size:32}];this.imr=this.isr=0;this.cr=1;this.tpsr=this.tcnt=this.rcnt=this.dcfg=0;this.memory=new Uint8Array(32768);this.txcr=this.rxcr=0;this.tsr=1;this.mac=new Uint8Array([0,34,21,
394
+ 255*Math.random()|0,255*Math.random()|0,255*Math.random()|0]);this.bus.send("net"+this.id+"-mac",Fc(this.mac));this.mar=Uint8Array.of(255,255,255,255,255,255,255,255);this.mac_address_in_state=null;for(b=0;6>b;b++)this.memory[b<<1]=this.memory[b<<1|1]=this.mac[b];this.memory[28]=this.memory[29]=87;this.memory[30]=this.memory[31]=87;C("Mac: "+Fc(this.mac),1048576);this.rsar=0;this.pstart=64;this.pstop=128;this.boundary=this.curpg=76;b=a.io;b.register_read(this.port|0,this,function(){C("Read cmd",1048576);
395
+ return this.cr});b.register_write(this.port|0,this,function(f){this.cr=f;C("Write command: "+m(f,2)+" newpg="+(this.cr>>6)+" txcr="+m(this.txcr,2),1048576);this.cr&1||(f&24&&0===this.rcnt&&this.do_interrupt(64),f&4&&(f=this.tpsr<<8,f=this.memory.subarray(f,f+this.tcnt),this.mac_address_in_state&&(f=new Uint8Array(f),Ec(f,this.mac_address_in_state,this.mac)),this.bus.send("net"+this.id+"-send",f),this.bus.send("eth-transmit-end",[f.length]),this.cr&=-5,this.do_interrupt(2),C("Command: Transfer. length="+
396
+ m(f.byteLength),1048576)))});b.register_read(this.port|13,this,function(){var f=this.get_page();if(1===f)return C("Read mar5",1048576),this.mar[5];C("Read counter0 pg="+f,1048576);return 0});b.register_read(this.port|14,this,function(){var f=this.get_page();if(1===f)return C("Read mar6",1048576),this.mar[6];C("Read8 counter1 pg="+f,1048576);return 0},function(){C("Read16 counter1 pg="+this.get_page(),1048576);return 0});b.register_read(this.port|15,this,function(){var f=this.get_page();if(1===f)return C("Read mar7",
397
+ 1048576),this.mar[7];C("Read counter2 pg="+f,1048576);return 0});b.register_read(this.port|31,this,function(){this.get_page();C("Read reset",1048576);this.do_interrupt(128);return 0});b.register_write(this.port|31,this,function(f){this.get_page();C("Write reset: "+m(f,2),1048576)});b.register_read(this.port|1,this,function(){var f=this.get_page();if(0===f)return this.pstart;if(1===f)return C("Read pg1/01 (mac[0])",1048576),this.mac[0];if(2===f)return this.pstart;C("Read pg"+f+"/01");k(!1);return 0});
398
+ b.register_write(this.port|1,this,function(f){var h=this.get_page();0===h?(C("start page: "+m(f,2),1048576),this.pstart=f):1===h?(C("mac[0] = "+m(f),1048576),this.mac[0]=f):3===h?C("Unimplemented: Write pg3/01 (9346CR): "+m(f),1048576):(C("Write pg"+h+"/01: "+m(f),1048576),k(!1))});b.register_read(this.port|2,this,function(){var f=this.get_page();if(0===f)return this.pstop;if(1===f)return C("Read pg1/02 (mac[1])",1048576),this.mac[1];if(2===f)return this.pstop;C("Read pg"+f+"/02",1048576);k(!1);return 0});
399
+ b.register_write(this.port|2,this,function(f){var h=this.get_page();0===h?(C("stop page: "+m(f,2),1048576),f>this.memory.length>>8&&(f=this.memory.length>>8,C("XXX: Adjusting stop page to "+m(f),1048576)),this.pstop=f):1===h?(C("mac[1] = "+m(f),1048576),this.mac[1]=f):(C("Write pg"+h+"/02: "+m(f),1048576),k(!1))});b.register_read(this.port|7,this,function(){var f=this.get_page();if(0===f)return C("Read isr: "+m(this.isr,2),1048576),this.isr;if(1===f)return C("Read curpg: "+m(this.curpg,2),1048576),
400
+ this.curpg;k(!1);return 0});b.register_write(this.port|7,this,function(f){var h=this.get_page();0===h?(C("Write isr: "+m(f,2),1048576),this.isr&=~f,this.update_irq()):1===h?(C("Write curpg: "+m(f,2),1048576),this.curpg=f):k(!1)});b.register_write(this.port|13,this,function(f){var h=this.get_page();0===h?(this.txcr=f,C("Write tx config: "+m(f,2),1048576)):C("Unimplemented: Write pg"+h+"/0d "+m(f,2),1048576)});b.register_write(this.port|14,this,function(f){var h=this.get_page();0===h?(C("Write data configuration: "+
401
+ m(f,2),1048576),this.dcfg=f):C("Unimplemented: Write pg"+h+"/0e "+m(f,2),1048576)});b.register_read(this.port|10,this,function(){var f=this.get_page();if(0===f)return C("Read pg0/0a",1048576),80;if(1===f)return C("Read mar2",1048576),this.mar[2];k(!1,"TODO");return 0});b.register_write(this.port|10,this,function(f){var h=this.get_page();0===h?(C("Write remote byte count low: "+m(f,2),1048576),this.rcnt=this.rcnt&65280|f&255):C("Unimplemented: Write pg"+h+"/0a "+m(f,2),1048576)});b.register_read(this.port|
402
+ 11,this,function(){var f=this.get_page();if(0===f)return C("Read pg0/0b",1048576),67;if(1===f)return C("Read mar3",1048576),this.mar[3];k(!1,"TODO");return 0});b.register_write(this.port|11,this,function(f){var h=this.get_page();0===h?(C("Write remote byte count high: "+m(f,2),1048576),this.rcnt=this.rcnt&255|f<<8&65280):C("Unimplemented: Write pg"+h+"/0b "+m(f,2),1048576)});b.register_read(this.port|8,this,function(){var f=this.get_page();if(0===f)return C("Read remote start address low",1048576),
403
+ this.rsar&255;if(1===f)return C("Read mar0",1048576),this.mar[0];C("Unimplemented: Read pg"+f+"/08",1048576);k(!1);return 0});b.register_write(this.port|8,this,function(f){var h=this.get_page();0===h?(C("Write remote start address low: "+m(f,2),1048576),this.rsar=this.rsar&65280|f&255):C("Unimplemented: Write pg"+h+"/08 "+m(f,2),1048576)});b.register_read(this.port|9,this,function(){var f=this.get_page();if(0===f)return C("Read remote start address high",1048576),this.rsar>>8&255;if(1===f)return C("Read mar1",
404
+ 1048576),this.mar[1];C("Unimplemented: Read pg"+f+"/09",1048576);k(!1);return 0});b.register_write(this.port|9,this,function(f){var h=this.get_page();0===h?(C("Write remote start address low: "+m(f,2),1048576),this.rsar=this.rsar&255|f<<8&65280):C("Unimplemented: Write pg"+h+"/09 "+m(f,2),1048576)});b.register_write(this.port|15,this,function(f){var h=this.get_page();0===h?(C("Write interrupt mask register: "+m(f,2)+" isr="+m(this.isr,2),1048576),this.imr=f,this.update_irq()):C("Unimplemented: Write pg"+
405
+ h+"/0f "+m(f,2),1048576)});b.register_read(this.port|3,this,function(){var f=this.get_page();if(0===f)return C("Read boundary: "+m(this.boundary,2),1048576),this.boundary;if(1===f)return C("Read pg1/03 (mac[2])",1048576),this.mac[2];3===f?C("Unimplemented: Read pg3/03 (CONFIG0)",1048576):(C("Read pg"+f+"/03",1048576),k(!1));return 0});b.register_write(this.port|3,this,function(f){var h=this.get_page();0===h?(C("Write boundary: "+m(f,2),1048576),this.boundary=f):1===h?(C("mac[2] = "+m(f),1048576),
406
+ this.mac[2]=f):(C("Write pg"+h+"/03: "+m(f),1048576),k(!1))});b.register_read(this.port|4,this,function(){var f=this.get_page();if(0===f)return this.tsr;if(1===f)return C("Read pg1/04 (mac[3])",1048576),this.mac[3];C("Read pg"+f+"/04",1048576);k(!1);return 0});b.register_write(this.port|4,this,function(f){var h=this.get_page();0===h?(C("Write tpsr: "+m(f,2),1048576),this.tpsr=f):1===h?(C("mac[3] = "+m(f),1048576),this.mac[3]=f):(C("Write pg"+h+"/04: "+m(f),1048576),k(!1))});b.register_read(this.port|
407
+ 5,this,function(){var f=this.get_page();if(0===f)return C("Unimplemented: Read pg0/05 (NCR: Number of Collisions Register)",1048576),0;if(1===f)return C("Read pg1/05 (mac[4])",1048576),this.mac[4];3===f?C("Unimplemented: Read pg3/05 (CONFIG2)",1048576):(C("Read pg"+f+"/05",1048576),k(!1));return 0});b.register_write(this.port|5,this,function(f){var h=this.get_page();0===h?(C("Write tcnt low: "+m(f,2),1048576),this.tcnt=this.tcnt&-256|f):1===h?(C("mac[4] = "+m(f),1048576),this.mac[4]=f):3===h?C("Unimplemented: Write pg3/05 (CONFIG2): "+
408
+ m(f),1048576):(C("Write pg"+h+"/05: "+m(f),1048576),k(!1))});b.register_read(this.port|6,this,function(){var f=this.get_page();if(0===f)return k(!1,"TODO"),0;if(1===f)return C("Read pg1/06 (mac[5])",1048576),this.mac[5];3===f?C("Unimplemented: Read pg3/06 (CONFIG3)",1048576):(C("Read pg"+f+"/06",1048576),k(!1));return 0});b.register_write(this.port|6,this,function(f){var h=this.get_page();0===h?(C("Write tcnt high: "+m(f,2),1048576),this.tcnt=this.tcnt&255|f<<8):1===h?(C("mac[5] = "+m(f),1048576),
409
+ this.mac[5]=f):3===h?C("Unimplemented: Write pg3/06 (CONFIG3): "+m(f),1048576):(C("Write pg"+h+"/06: "+m(f),1048576),k(!1))});b.register_read(this.port|12,this,function(){var f=this.get_page();if(0===f)return 9;if(1===f)return C("Read mar4",1048576),this.mar[4];C("Unimplemented: Read pg"+f+"/0c",1048576);k(!1);return 0});b.register_write(this.port|12,this,function(f){var h=this.get_page();0===h?(C("RX configuration reg write: "+m(f,2),1048576),this.rxcr=f):C("Unimplemented: Write pg"+h+"/0c: "+m(f),
410
+ 1048576)});b.register_read(this.port|16,this,this.data_port_read8,this.data_port_read16,this.data_port_read32);b.register_write(this.port|16,this,this.data_port_write16,this.data_port_write16,this.data_port_write32);a.devices.pci.register_device(this)}Gc.prototype.get_state=function(){var a=[];a[0]=this.isr;a[1]=this.imr;a[2]=this.cr;a[3]=this.dcfg;a[4]=this.rcnt;a[5]=this.tcnt;a[6]=this.tpsr;a[7]=this.rsar;a[8]=this.pstart;a[9]=this.curpg;a[10]=this.boundary;a[11]=this.pstop;a[12]=this.rxcr;a[13]=
411
+ this.txcr;a[14]=this.tsr;a[15]=this.mac;a[16]=this.memory;return a};Gc.prototype.set_state=function(a){this.isr=a[0];this.imr=a[1];this.cr=a[2];this.dcfg=a[3];this.rcnt=a[4];this.tcnt=a[5];this.tpsr=a[6];this.rsar=a[7];this.pstart=a[8];this.curpg=a[9];this.boundary=a[10];this.pstop=a[11];this.rxcr=a[12];this.txcr=a[13];this.tsr=a[14];this.preserve_mac_from_state_image?(this.mac=a[15],this.memory=a[16]):this.mac_address_translation&&(this.mac_address_in_state=a[15],this.memory=a[16],C("Using mac address translation guest_os_mac="+
412
+ Fc(this.mac_address_in_state)+" real_mac="+Fc(this.mac),1048576));this.bus.send("net"+this.id+"-mac",Fc(this.mac))};Gc.prototype.do_interrupt=function(a){C("Do interrupt "+m(a,2),1048576);this.isr|=a;this.update_irq()};Gc.prototype.update_irq=function(){this.imr&this.isr?this.pci.raise_irq(this.pci_id):this.pci.lower_irq(this.pci_id)};Gc.prototype.data_port_write=function(a){if(16>=this.rsar||16384<=this.rsar&&32768>this.rsar)this.memory[this.rsar]=a;this.rsar++;this.rcnt--;this.rsar>=this.pstop<<
413
+ 8&&(this.rsar+=this.pstart-this.pstop<<8);0===this.rcnt&&this.do_interrupt(64)};Gc.prototype.data_port_write16=function(a){this.data_port_write(a);this.dcfg&1&&this.data_port_write(a>>8)};Gc.prototype.data_port_write32=function(a){this.data_port_write(a);this.data_port_write(a>>8);this.data_port_write(a>>16);this.data_port_write(a>>24)};Gc.prototype.data_port_read=function(){let a=0;32768>this.rsar&&(a=this.memory[this.rsar]);this.rsar++;this.rcnt--;this.rsar>=this.pstop<<8&&(this.rsar+=this.pstart-
414
+ this.pstop<<8);0===this.rcnt&&this.do_interrupt(64);return a};Gc.prototype.data_port_read8=function(){return this.data_port_read16()&255};Gc.prototype.data_port_read16=function(){return this.dcfg&1?this.data_port_read()|this.data_port_read()<<8:this.data_port_read()};Gc.prototype.data_port_read32=function(){return this.data_port_read()|this.data_port_read()<<8|this.data_port_read()<<16|this.data_port_read()<<24};Gc.prototype.receive=function(a){if(!(this.cr&1)&&(this.bus.send("eth-receive-end",[a.length]),
415
+ this.rxcr&16||this.rxcr&4&&255===a[0]&&255===a[1]&&255===a[2]&&255===a[3]&&255===a[4]&&255===a[5]||!(this.rxcr&8&&1===(a[0]&1)||a[0]!==this.mac[0]||a[1]!==this.mac[1]||a[2]!==this.mac[2]||a[3]!==this.mac[3]||a[4]!==this.mac[4]||a[5]!==this.mac[5]))){this.mac_address_in_state&&(a=new Uint8Array(a),Ec(a,this.mac,this.mac_address_in_state));var b=this.curpg<<8,c=Math.max(60,a.length)+4,d=b+4,e=this.curpg+1+(c>>8),f=b+c,h=1+(c>>8),l=this.boundary>this.curpg?this.boundary-this.curpg:this.pstop-this.curpg+
416
+ this.boundary-this.pstart;l<h&&0!==this.boundary?C("Buffer full, dropping packet pstart="+m(this.pstart)+" pstop="+m(this.pstop)+" curpg="+m(this.curpg)+" needed="+m(h)+" boundary="+m(this.boundary)+" available="+m(l),1048576):(f>this.pstop<<8?(k(60<=a.length),f=(this.pstop<<8)-d,k(0<=f),this.memory.set(a.subarray(0,f),d),this.memory.set(a.subarray(f),this.pstart<<8),C("rcv cut="+m(f),1048576)):(this.memory.set(a,d),60>a.length&&this.memory.fill(0,d+a.length,d+60)),e>=this.pstop&&(e+=this.pstart-
417
+ this.pstop),this.memory[b]=1,this.memory[b+1]=e,this.memory[b+2]=c,this.memory[b+3]=c>>8,this.curpg=e,C("rcv offset="+m(b)+" len="+m(c)+" next="+m(e),1048576),this.do_interrupt(1))}};Gc.prototype.get_page=function(){return this.cr>>6&3};function Hc(a,b){this.bus=b;this.rows=25;this.cols=80;this.ports=4;b=[{size_supported:16,notify_offset:0},{size_supported:16,notify_offset:1},{size_supported:16,notify_offset:2},{size_supported:16,notify_offset:3}];for(let c=1;c<this.ports;++c)b.push({size_supported:16,
418
+ notify_offset:0}),b.push({size_supported:8,notify_offset:1});this.virtio=new Ic(a,{name:"virtio-console",pci_id:96,device_id:4163,subsystem_device_id:3,common:{initial_port:47104,queues:b,features:[0,1,32],on_driver_ok:()=>{}},notification:{initial_port:47360,single_handler:!1,handlers:[()=>{},c=>{const d=this.virtio.queues[c],e=3<c?c-3>>1:0;for(;d.has_request();){const f=d.pop_request(),h=new Uint8Array(f.length_readable);f.get_next_blob(h);this.bus.send("virtio-console"+e+"-output-bytes",h);this.Ack(c,
419
+ f)}},c=>{if(2!==c)k(!1,"VirtioConsole Notified for wrong queue: "+c+" (expected queue_id of 2)");else for(c=this.virtio.queues[c];c.count_requests()>c.size-2;)c.pop_request()},c=>{if(3!==c)k(!1,"VirtioConsole Notified for wrong queue: "+c+" (expected queue_id of 3)");else for(var d=this.virtio.queues[c];d.has_request();){var e=d.pop_request(),f=new Uint8Array(e.length_readable);e.get_next_blob(f);var h=M(["w","h","h"],f,{offset:0});f=h[0];h=h[1];this.Ack(c,e);switch(h){case 0:for(e=0;e<this.ports;++e)this.SendEvent(e,
420
+ 1,0);break;case 3:this.Ack(c,e);this.SendEvent(f,4,1);this.SendName(f,"virtio-"+f);this.SendEvent(f,6,1);break;case 6:this.Ack(c,e);0===f&&this.SendWindowSize(f);break;default:k(!1," VirtioConsole received unknown event: "+h[1]);return}}}]},isr_status:{initial_port:46848},device_specific:{initial_port:46592,struct:[{bytes:2,name:"cols",read:()=>this.cols,write:()=>{}},{bytes:2,name:"rows",read:()=>this.rows,write:()=>{}},{bytes:4,name:"max_nr_ports",read:()=>this.ports,write:()=>{}},{bytes:4,name:"emerg_wr",
421
+ read:()=>0,write:()=>{k(!1,"Emergency write!")}}]}});for(let c=0;c<this.ports;++c){const d=0===c?0:2*c+2;this.bus.register("virtio-console"+c+"-input-bytes",function(e){var f=this.virtio.queues[d];f.has_request()&&(f=f.pop_request(),this.Send(d,f,new Uint8Array(e)))},this);this.bus.register("virtio-console"+c+"-resize",function(e){0===c&&(this.cols=e[0],this.rows=e[1]);this.virtio.queues[2].is_configured()&&this.virtio.queues[2].has_request()&&this.SendWindowSize(c,e[0],e[1])},this)}}Hc.prototype.SendWindowSize=
422
+ function(a,b,c){c=c||this.rows;b=b||this.cols;const d=this.virtio.queues[2].pop_request(),e=new Uint8Array(12);K(["w","h","h","h","h"],[a,5,0,c,b],e,0);this.Send(2,d,e)};Hc.prototype.SendName=function(a,b){const c=this.virtio.queues[2].pop_request();b=(new TextEncoder).encode(b);const d=new Uint8Array(8+b.length+1);K(["w","h","h"],[a,7,1],d,0);for(a=0;a<b.length;++a)d[a+8]=b[a];d[8+b.length]=0;this.Send(2,c,d)};Hc.prototype.get_state=function(){const a=[];a[0]=this.virtio;a[1]=this.rows;a[2]=this.cols;
423
+ a[3]=this.ports;return a};Hc.prototype.set_state=function(a){this.virtio.set_state(a[0]);this.rows=a[1];this.cols=a[2];this.ports=a[3]};Hc.prototype.reset=function(){this.virtio.reset()};Hc.prototype.SendEvent=function(a,b,c){const d=this.virtio.queues[2].pop_request(),e=new Uint8Array(8);K(["w","h","h"],[a,b,c],e,0);this.Send(2,d,e)};Hc.prototype.Send=function(a,b,c){b.set_next_blob(c);this.virtio.queues[a].push_reply(b);this.virtio.queues[a].flush_replies()};Hc.prototype.Ack=function(a,b){b.set_next_blob(new Uint8Array(0));
424
+ this.virtio.queues[a].push_reply(b);this.virtio.queues[a].flush_replies()};function Jc(a,b){this.cpu=a;this.bus=b;this.reset();this.bus.register("keyboard-code",function(c){this.kbd_send_code(c)},this);this.bus.register("mouse-click",function(c){this.mouse_send_click(c[0],c[1],c[2])},this);this.bus.register("mouse-delta",function(c){this.mouse_send_delta(c[0],c[1])},this);this.bus.register("mouse-wheel",function(c){this.wheel_movement-=c[0];this.wheel_movement-=2*c[1];this.wheel_movement=Math.min(7,
425
+ Math.max(-8,this.wheel_movement));this.send_mouse_packet(0,0)},this);a.io.register_read(96,this,this.port60_read);a.io.register_read(100,this,this.port64_read);a.io.register_write(96,this,this.port60_write);a.io.register_write(100,this,this.port64_write)}Jc.prototype.reset=function(){this.use_mouse=this.enable_mouse_stream=!1;this.have_mouse=!0;this.mouse_clicks=this.mouse_delta_y=this.mouse_delta_x=0;this.have_keyboard=!0;this.next_read_resolution=this.next_read_rate=this.next_handle_scan_code_set=
426
+ this.next_read_led=this.next_read_sample=this.next_is_mouse_command=this.enable_keyboard_stream=!1;this.kbd_buffer=new ma(1024);this.last_port60_byte=0;this.sample_rate=100;this.mouse_id=this.mouse_detect_state=0;this.mouse_reset_workaround=!1;this.wheel_movement=0;this.resolution=4;this.scaling2=!1;this.last_mouse_packet=-1;this.mouse_buffer=new ma(1024);this.next_byte_is_aux=this.next_byte_is_ready=!1;this.command_register=5;this.controller_output_port=0;this.read_controller_output_port=this.read_command_register=
427
+ this.read_output_register=!1};Jc.prototype.get_state=function(){var a=[];a[0]=this.enable_mouse_stream;a[1]=this.use_mouse;a[2]=this.have_mouse;a[3]=this.mouse_delta_x;a[4]=this.mouse_delta_y;a[5]=this.mouse_clicks;a[6]=this.have_keyboard;a[7]=this.enable_keyboard_stream;a[8]=this.next_is_mouse_command;a[9]=this.next_read_sample;a[10]=this.next_read_led;a[11]=this.next_handle_scan_code_set;a[12]=this.next_read_rate;a[13]=this.next_read_resolution;a[15]=this.last_port60_byte;a[16]=this.sample_rate;
428
+ a[17]=this.resolution;a[18]=this.scaling2;a[20]=this.command_register;a[21]=this.read_output_register;a[22]=this.read_command_register;a[23]=this.controller_output_port;a[24]=this.read_controller_output_port;a[25]=this.mouse_id;a[26]=this.mouse_detect_state;a[27]=this.mouse_reset_workaround;return a};Jc.prototype.set_state=function(a){this.enable_mouse_stream=a[0];this.use_mouse=a[1];this.have_mouse=a[2];this.mouse_delta_x=a[3];this.mouse_delta_y=a[4];this.mouse_clicks=a[5];this.have_keyboard=a[6];
429
+ this.enable_keyboard_stream=a[7];this.next_is_mouse_command=a[8];this.next_read_sample=a[9];this.next_read_led=a[10];this.next_handle_scan_code_set=a[11];this.next_read_rate=a[12];this.next_read_resolution=a[13];this.last_port60_byte=a[15];this.sample_rate=a[16];this.resolution=a[17];this.scaling2=a[18];this.command_register=a[20];this.read_output_register=a[21];this.read_command_register=a[22];this.controller_output_port=a[23];this.read_controller_output_port=a[24];this.mouse_id=a[25]||0;this.mouse_detect_state=
430
+ a[26]||0;this.mouse_reset_workaround=a[27]||!1;this.next_byte_is_aux=this.next_byte_is_ready=!1;this.kbd_buffer.clear();this.mouse_buffer.clear();this.bus.send("mouse-enable",this.use_mouse)};Jc.prototype.raise_irq=function(){this.next_byte_is_ready||(this.kbd_buffer.length?this.kbd_irq():this.mouse_buffer.length&&this.mouse_irq())};Jc.prototype.mouse_irq=function(){this.next_byte_is_aux=this.next_byte_is_ready=!0;this.command_register&2&&(C("Mouse irq",64),this.cpu.device_lower_irq(12),this.cpu.device_raise_irq(12))};
431
+ Jc.prototype.kbd_irq=function(){this.next_byte_is_ready=!0;this.next_byte_is_aux=!1;this.command_register&1&&(C("Keyboard irq",64),this.cpu.device_lower_irq(1),this.cpu.device_raise_irq(1))};Jc.prototype.kbd_send_code=function(a){this.enable_keyboard_stream&&(C("adding kbd code: "+m(a),64),this.kbd_buffer.push(a),this.raise_irq())};Jc.prototype.mouse_send_delta=function(a,b){if(this.have_mouse&&this.use_mouse){var c=this.resolution*this.sample_rate/80;this.mouse_delta_x+=a*c;this.mouse_delta_y+=b*
432
+ c;this.enable_mouse_stream&&(a=this.mouse_delta_x|0,b=this.mouse_delta_y|0,a||b)&&(Date.now(),this.mouse_delta_x-=a,this.mouse_delta_y-=b,this.send_mouse_packet(a,b))}};Jc.prototype.mouse_send_click=function(a,b,c){this.have_mouse&&this.use_mouse&&(this.mouse_clicks=a|c<<1|b<<2,this.enable_mouse_stream&&this.send_mouse_packet(0,0))};Jc.prototype.send_mouse_packet=function(a,b){var c=(0>b)<<5|(0>a)<<4|8|this.mouse_clicks;this.last_mouse_packet=Date.now();this.mouse_buffer.push(c);this.mouse_buffer.push(a);
433
+ this.mouse_buffer.push(b);4===this.mouse_id?(this.mouse_buffer.push(0|this.wheel_movement&15),this.wheel_movement=0):3===this.mouse_id&&(this.mouse_buffer.push(this.wheel_movement&255),this.wheel_movement=0);this.raise_irq()};Jc.prototype.apply_scaling2=function(a){var b=a>>31;switch(Math.abs(a)){case 0:case 1:case 3:return a;case 2:return b;case 4:return 6*b;case 5:return 9*b;default:return a<<1}};Jc.prototype.port60_read=function(){this.next_byte_is_ready=!1;if(!this.kbd_buffer.length&&!this.mouse_buffer.length)return C("Port 60 read: Empty",
434
+ 64),this.last_port60_byte;this.next_byte_is_aux?(this.cpu.device_lower_irq(12),this.last_port60_byte=this.mouse_buffer.shift(),C("Port 60 read (mouse): "+m(this.last_port60_byte),64)):(this.cpu.device_lower_irq(1),this.last_port60_byte=this.kbd_buffer.shift(),C("Port 60 read (kbd) : "+m(this.last_port60_byte),64));(this.kbd_buffer.length||this.mouse_buffer.length)&&this.raise_irq();return this.last_port60_byte};Jc.prototype.port64_read=function(){var a=16;this.next_byte_is_ready&&(a|=1);this.next_byte_is_aux&&
435
+ (a|=32);C("port 64 read: "+m(a),64);return a};Jc.prototype.port60_write=function(a){C("port 60 write: "+m(a),64);if(this.read_command_register)this.command_register=a,this.read_command_register=!1,C("Keyboard command register = "+m(this.command_register),64);else if(this.read_output_register)this.read_output_register=!1,this.mouse_buffer.clear(),this.mouse_buffer.push(a),this.mouse_irq();else if(this.next_read_sample){this.next_read_sample=!1;this.mouse_buffer.clear();this.mouse_buffer.push(250);
436
+ this.sample_rate=a;switch(this.mouse_detect_state){case -1:60===a?(this.mouse_reset_workaround=!0,this.mouse_detect_state=0):(this.mouse_reset_workaround=!1,this.mouse_detect_state=200===a?1:0);break;case 0:200===a&&(this.mouse_detect_state=1);break;case 1:this.mouse_detect_state=100===a?2:200===a?3:0;break;case 2:80===a&&(this.mouse_id=3);this.mouse_detect_state=-1;break;case 3:80===a&&(this.mouse_id=4),this.mouse_detect_state=-1}C("mouse sample rate: "+m(a)+", mouse id: "+m(this.mouse_id),64);this.sample_rate||
437
+ (C("invalid sample rate, reset to 100",64),this.sample_rate=100);this.mouse_irq()}else if(this.next_read_resolution)this.next_read_resolution=!1,this.mouse_buffer.clear(),this.mouse_buffer.push(250),3<a?(this.resolution=4,C("invalid resolution, resetting to 4",64)):(this.resolution=1<<a,C("resolution: "+this.resolution,64)),this.mouse_irq();else if(this.next_read_led)this.next_read_led=!1,this.kbd_buffer.push(250),this.kbd_irq();else if(this.next_handle_scan_code_set)this.next_handle_scan_code_set=
438
+ !1,this.kbd_buffer.push(250),this.kbd_irq(),a||this.kbd_buffer.push(1);else if(this.next_read_rate)this.next_read_rate=!1,this.kbd_buffer.push(250),this.kbd_irq();else if(this.next_is_mouse_command){if(this.next_is_mouse_command=!1,C("Port 60 data register write: "+m(a),64),this.have_mouse){this.kbd_buffer.clear();this.mouse_buffer.clear();this.mouse_buffer.push(250);switch(a){case 230:C("Scaling 1:1",64);this.scaling2=!1;break;case 231:C("Scaling 2:1",64);this.scaling2=!0;break;case 232:this.next_read_resolution=
439
+ !0;break;case 233:this.send_mouse_packet(0,0);break;case 235:C("unimplemented request single packet",64);this.send_mouse_packet(0,0);break;case 242:C("required id: "+m(this.mouse_id),64);this.mouse_buffer.push(this.mouse_id);this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0;this.raise_irq();break;case 243:this.next_read_sample=!0;break;case 244:this.use_mouse=this.enable_mouse_stream=!0;this.bus.send("mouse-enable",!0);this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0;break;case 245:this.enable_mouse_stream=
440
+ !1;break;case 246:this.enable_mouse_stream=!1;this.sample_rate=100;this.scaling2=!1;this.resolution=4;break;case 255:C("Mouse reset",64);this.mouse_buffer.push(170);this.mouse_buffer.push(0);this.use_mouse=!0;this.bus.send("mouse-enable",!0);this.enable_mouse_stream=!1;this.sample_rate=100;this.scaling2=!1;this.resolution=4;this.mouse_reset_workaround||(this.mouse_id=0);this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0;break;default:C("Unimplemented mouse command: "+m(a),64)}this.mouse_irq()}}else if(this.read_controller_output_port)this.read_controller_output_port=
441
+ !1,this.controller_output_port=a;else{C("Port 60 data register write: "+m(a),64);this.mouse_buffer.clear();this.kbd_buffer.clear();this.kbd_buffer.push(250);switch(a){case 237:this.next_read_led=!0;break;case 240:this.next_handle_scan_code_set=!0;break;case 242:this.kbd_buffer.push(171);this.kbd_buffer.push(131);break;case 243:this.next_read_rate=!0;break;case 244:C("kbd enable scanning",64);this.enable_keyboard_stream=!0;break;case 245:C("kbd disable scanning",64);this.enable_keyboard_stream=!1;
442
+ break;case 246:break;case 255:this.kbd_buffer.clear();this.kbd_buffer.push(250);this.kbd_buffer.push(170);this.kbd_buffer.push(0);break;default:C("Unimplemented keyboard command: "+m(a),64)}this.kbd_irq()}};Jc.prototype.port64_write=function(a){C("port 64 write: "+m(a),64);switch(a){case 32:this.kbd_buffer.clear();this.mouse_buffer.clear();this.kbd_buffer.push(this.command_register);this.kbd_irq();break;case 96:this.read_command_register=!0;break;case 209:this.read_controller_output_port=!0;break;
443
+ case 211:this.read_output_register=!0;break;case 212:this.next_is_mouse_command=!0;break;case 167:C("Disable second port",64);this.command_register|=32;break;case 168:C("Enable second port",64);this.command_register&=-33;break;case 169:this.kbd_buffer.clear();this.mouse_buffer.clear();this.kbd_buffer.push(0);this.kbd_irq();break;case 170:this.kbd_buffer.clear();this.mouse_buffer.clear();this.kbd_buffer.push(85);this.kbd_irq();break;case 171:this.kbd_buffer.clear();this.mouse_buffer.clear();this.kbd_buffer.push(0);
444
+ this.kbd_irq();break;case 173:C("Disable Keyboard",64);this.command_register|=16;break;case 174:C("Enable Keyboard",64);this.command_register&=-17;break;case 254:C("CPU reboot via PS2");this.cpu.reboot_internal();break;default:C("port 64: Unimplemented command byte: "+m(a),64)}};const Kc=DataView.prototype,Lc={size:1,get:Kc.getUint8,set:Kc.setUint8},Mc={size:2,get:Kc.getUint16,set:Kc.setUint16},U={size:4,get:Kc.getUint32,set:Kc.setUint32},Oc=Nc([{magic:U},{class:Lc},{data:Lc},{version0:Lc},{osabi:Lc},
445
+ {abiversion:Lc},{pad0:function(a){return{size:a,get:()=>-1}}(7)},{type:Mc},{machine:Mc},{version1:U},{entry:U},{phoff:U},{shoff:U},{flags:U},{ehsize:Mc},{phentsize:Mc},{phnum:Mc},{shentsize:Mc},{shnum:Mc},{shstrndx:Mc}]);console.assert(52===Oc.reduce((a,b)=>a+b.size,0));const Pc=Nc([{type:U},{offset:U},{vaddr:U},{paddr:U},{filesz:U},{memsz:U},{flags:U},{align:U}]);console.assert(32===Pc.reduce((a,b)=>a+b.size,0));const Qc=Nc([{name:U},{type:U},{flags:U},{addr:U},{offset:U},{size:U},{link:U},{info:U},
446
+ {addralign:U},{entsize:U}]);console.assert(40===Qc.reduce((a,b)=>a+b.size,0));function Nc(a){return a.map(function(b){var c=Object.keys(b);console.assert(1===c.length);c=c[0];b=b[c];console.assert(0<b.size);return{name:c,type:b,size:b.size,get:b.get,set:b.set}})}function Rc(a,b){const c={};let d=0;for(const e of b)b=e.get.call(a,d,!0),console.assert(void 0===c[e.name]),c[e.name]=b,d+=e.size;return[c,d]}function Sc(a,b,c){const d=[];let e=0;for(var f=0;f<c;f++){const [h,l]=Rc(new DataView(a.buffer,
447
+ a.byteOffset+e,void 0),b);d.push(h);e+=l}return[d,e]}function V(a,b,c){this.io=a.io;this.cpu=a;this.dma=a.devices.dma;this.bytes_expecting=0;this.receiving_command=new Uint8Array(10);this.receiving_index=0;this.next_command=null;this.response_data=new Uint8Array(10);this.last_head=this.last_cylinder=this.drive=this.status_reg2=this.status_reg1=this.status_reg0=this.response_length=this.response_index=0;this.last_sector=1;this.dir=this.dor=0;this.fdb_image=this.fda_image=null;b?this.set_fda(b):(this.eject_fda(),
448
+ this.cpu.devices.rtc.cmos_write(16,64));k(!c,"FDB not supported");this.io.register_read(1008,this,this.port3F0_read);this.io.register_read(1010,this,this.port3F2_read);this.io.register_read(1012,this,this.port3F4_read);this.io.register_read(1013,this,this.port3F5_read);this.io.register_read(1015,this,this.port3F7_read);this.io.register_write(1010,this,this.port3F2_write);this.io.register_write(1012,this,this.port3F4_write);this.io.register_write(1013,this,this.port3F5_write)}V.prototype.eject_fda=
449
+ function(){this.fda_image=null;this.number_of_cylinders=this.number_of_heads=this.sectors_per_track=0;this.dir=128};V.prototype.set_fda=function(a){var b={[163840]:{type:1,tracks:40,sectors:8,heads:1},[184320]:{type:1,tracks:40,sectors:9,heads:1},[204800]:{type:1,tracks:40,sectors:10,heads:1},[327680]:{type:1,tracks:40,sectors:8,heads:2},[368640]:{type:1,tracks:40,sectors:9,heads:2},[409600]:{type:1,tracks:40,sectors:10,heads:2},[737280]:{type:3,tracks:80,sectors:9,heads:2},[1228800]:{type:2,tracks:80,
450
+ sectors:15,heads:2},[1474560]:{type:4,tracks:80,sectors:18,heads:2},[1763328]:{type:5,tracks:82,sectors:21,heads:2},[2949120]:{type:5,tracks:80,sectors:36,heads:2},512:{type:1,tracks:1,sectors:1,heads:1}};let c=a.byteLength,d=b[c];d||(c=1474560<a.byteLength?2949120:1474560,d=b[c],k(a.buffer&&a.buffer instanceof ArrayBuffer),b=new Uint8Array(c),b.set(new Uint8Array(a.buffer)),a=new ya(b.buffer),C("Warning: Unkown floppy size: "+a.byteLength+", assuming "+c));this.sectors_per_track=d.sectors;this.number_of_heads=
451
+ d.heads;this.number_of_cylinders=d.tracks;this.fda_image=a;this.dir=128;this.cpu.devices.rtc.cmos_write(16,d.type<<4)};V.prototype.get_state=function(){var a=[];a[0]=this.bytes_expecting;a[1]=this.receiving_command;a[2]=this.receiving_index;a[4]=this.response_data;a[5]=this.response_index;a[6]=this.response_length;a[8]=this.status_reg0;a[9]=this.status_reg1;a[10]=this.status_reg2;a[11]=this.drive;a[12]=this.last_cylinder;a[13]=this.last_head;a[14]=this.last_sector;a[15]=this.dor;a[16]=this.sectors_per_track;
452
+ a[17]=this.number_of_heads;a[18]=this.number_of_cylinders;return a};V.prototype.set_state=function(a){this.bytes_expecting=a[0];this.receiving_command=a[1];this.receiving_index=a[2];this.next_command=a[3];this.response_data=a[4];this.response_index=a[5];this.response_length=a[6];this.status_reg0=a[8];this.status_reg1=a[9];this.status_reg2=a[10];this.drive=a[11];this.last_cylinder=a[12];this.last_head=a[13];this.last_sector=a[14];this.dor=a[15];this.sectors_per_track=a[16];this.number_of_heads=a[17];
453
+ this.number_of_cylinders=a[18]};V.prototype.port3F0_read=function(){C("3F0 read",8192);return 0};V.prototype.port3F4_read=function(){C("3F4 read",8192);var a=128;this.response_index<this.response_length&&(a|=80);0===(this.dor&8)&&(a|=32);return a};V.prototype.port3F7_read=function(){C("3F7 read",8192);return this.dir};V.prototype.port3F5_read=function(){if(this.response_index<this.response_length)return C("3F5 read: "+this.response_data[this.response_index],8192),this.cpu.device_lower_irq(6),this.response_data[this.response_index++];
454
+ C("3F5 read, empty",8192);return 255};V.prototype.port3F4_write=function(a){C("3F4/data rate write: "+m(a),8192);a&128&&(C("dsr reset",8192),this.status_reg0=192,this.cpu.device_raise_irq(6))};V.prototype.port3F5_write=function(a){C("3F5 write "+m(a),8192);if(0<this.bytes_expecting){if(this.receiving_command[this.receiving_index++]=a,this.bytes_expecting--,0===this.bytes_expecting){a="3F5 command received: ";for(var b=0;b<this.receiving_index;b++)a+=m(this.receiving_command[b])+" ";C(a,8192);this.next_command.call(this,
455
+ this.receiving_command)}}else{switch(a){case 3:this.next_command=this.fix_drive_data;this.bytes_expecting=2;break;case 19:this.next_command=this.configure;this.bytes_expecting=3;break;case 4:this.next_command=this.check_drive_status;this.bytes_expecting=1;break;case 5:case 69:case 197:this.next_command=function(c){this.do_sector(!0,c)};this.bytes_expecting=8;break;case 6:case 70:case 198:case 230:this.next_command=function(c){this.do_sector(!1,c)};this.bytes_expecting=8;break;case 7:this.next_command=
456
+ this.calibrate;this.bytes_expecting=1;break;case 8:this.check_interrupt_status();break;case 74:this.next_command=this.read_sector_id;this.bytes_expecting=1;break;case 15:this.bytes_expecting=2;this.next_command=this.seek;break;case 14:case 16:C(14===a?"dump registers":"determine controller version",8192);this.status_reg0=128;this.response_data[0]=this.status_reg0;this.response_index=0;this.response_length=1;this.bytes_expecting=0;break;default:k(!1,"Unimplemented floppy command call "+m(a))}this.receiving_index=
457
+ 0}};V.prototype.port3F2_read=function(){C("read 3F2: DOR",8192);return this.dor};V.prototype.port3F2_write=function(a){4===(a&4)&&0===(this.dor&4)&&(this.status_reg0=192,this.cpu.device_raise_irq(6));C("start motors: "+m(a>>4),8192);C("enable dma/irq: "+!!(a&8),8192);C("reset fdc: "+!!(a&4),8192);C("drive select: "+(a&3),8192);0!==(a&3)&&C("guest: fdb not implemented",8192);C("DOR = "+m(a),8192);this.dor=a};V.prototype.check_drive_status=function(){C("check drive status",8192);this.status_reg1=this.fda_image?
458
+ 0:5;this.response_index=0;this.response_length=1;this.response_data[0]=0};V.prototype.seek=function(a){C("seek",8192);if(0!==(a[0]&3))C("seek on fdb",8192);else{var b=a[1];a=a[0]>>2&1;b!==this.last_cylinder&&(this.dir=0);this.status_reg1=this.fda_image?0:5;this.status_reg0=32;this.last_cylinder=b;this.last_head=a}this.raise_irq()};V.prototype.calibrate=function(a){C("floppy calibrate",8192);this.seek([a[0],0])};V.prototype.check_interrupt_status=function(){C("floppy check interrupt status",8192);
459
+ this.response_index=0;this.response_length=2;this.response_data[0]=this.status_reg0;this.response_data[1]=this.last_cylinder};V.prototype.do_sector=function(a,b){var c=b[2],d=b[1],e=b[3],f=128<<b[4],h=b[5]-b[3]+1,l=((c+this.number_of_heads*d)*this.sectors_per_track+e-1)*f;C("Floppy "+(a?"Write":"Read"),8192);C("from "+m(l)+" length "+m(h*f),8192);C(d+" / "+c+" / "+e,8192);b[4]||C("FDC: sector count is zero, use data length instead",8192);this.fda_image?(this.status_reg1=0,a?this.dma.do_write(this.fda_image,
460
+ l,h*f,2,this.done.bind(this,b,d,c,e)):this.dma.do_read(this.fda_image,l,h*f,2,this.done.bind(this,b,d,c,e))):this.status_reg1=5};V.prototype.done=function(a,b,c,d,e){e?C("XXX: Unhandled floppy error",8192):(d++,d>this.sectors_per_track&&(d=1,c++,c>=this.number_of_heads&&(c=0,b++)),b!==this.last_cylinder&&(this.dir=0),this.status_reg0=32,this.last_cylinder=b,this.last_head=c,this.last_sector=d,this.response_index=0,this.response_length=7,this.response_data[0]=c<<2|32,this.response_data[1]=0,this.response_data[2]=
461
+ 0,this.response_data[3]=b,this.response_data[4]=c,this.response_data[5]=d,this.response_data[6]=a[4],this.raise_irq())};V.prototype.fix_drive_data=function(a){C("floppy fix drive data "+a.slice(0,this.bytes_expecting),8192)};V.prototype.configure=function(a){C("floppy configure "+a.slice(0,this.bytes_expecting),8192)};V.prototype.read_sector_id=function(a){C("floppy read sector id "+a,8192);this.response_index=0;this.response_length=7;this.response_data[0]=0;this.response_data[1]=0;this.response_data[2]=
462
+ 0;this.response_data[3]=0;this.response_data[4]=0;this.response_data[5]=0;this.response_data[6]=0;this.raise_irq()};V.prototype.raise_irq=function(){this.dor&8&&this.cpu.device_raise_irq(6)};function Tc(a,b,c,d,e,f){this.master=new W(this,a,b,d,e,0,f);this.slave=new W(this,a,c,!1,e,1,f);this.current_interface=this.master;this.cpu=a;0===e?(this.ata_port=496,this.irq=14,this.pci_id=240):1===e?(this.ata_port=368,this.irq=15,this.pci_id=248):k(!1,"IDE device with nr "+e+" ignored",32768);this.ata_port_high=
463
+ this.ata_port|516;this.master_port=46080;this.pci_space=[134,128,16,112,5,0,160,2,0,128,1,1,0,0,0,0,this.ata_port&255|1,this.ata_port>>8,0,0,this.ata_port_high&255|1,this.ata_port_high>>8,0,0,0,0,0,0,0,0,0,0,this.master_port&255|1,this.master_port>>8,0,0,0,0,0,0,0,0,0,0,67,16,212,130,0,0,0,0,0,0,0,0,0,0,0,0,this.irq,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
464
+ 0,0,0,0,0,0,0];this.pci_bars=[{size:8},{size:4},void 0,void 0,{size:16}];this.name="ide"+e;this.device_control=2;a.io.register_read(this.ata_port|7,this,function(){C("lower irq",32768);this.cpu.device_lower_irq(this.irq);return this.read_status()});a.io.register_read(this.ata_port_high|2,this,this.read_status);a.io.register_write(this.ata_port_high|2,this,this.write_control);a.io.register_read(this.ata_port|0,this,function(){return this.current_interface.read_data(1)},function(){return this.current_interface.read_data(2)},
465
+ function(){return this.current_interface.read_data(4)});a.io.register_read(this.ata_port|1,this,function(){C("Read error: "+m(this.current_interface.error&255)+" slave="+(this.current_interface===this.slave),32768);return this.current_interface.error&255});a.io.register_read(this.ata_port|2,this,function(){C("Read bytecount: "+m(this.current_interface.bytecount&255),32768);return this.current_interface.bytecount&255});a.io.register_read(this.ata_port|3,this,function(){C("Read sector: "+m(this.current_interface.sector&
466
+ 255),32768);return this.current_interface.sector&255});a.io.register_read(this.ata_port|4,this,function(){C("Read 1F4: "+m(this.current_interface.cylinder_low&255),32768);return this.current_interface.cylinder_low&255});a.io.register_read(this.ata_port|5,this,function(){C("Read 1F5: "+m(this.current_interface.cylinder_high&255),32768);return this.current_interface.cylinder_high&255});a.io.register_read(this.ata_port|6,this,function(){C("Read 1F6",32768);return this.current_interface.drive_head&255});
467
+ a.io.register_write(this.ata_port|0,this,function(h){this.current_interface.write_data_port8(h)},function(h){this.current_interface.write_data_port16(h)},function(h){this.current_interface.write_data_port32(h)});a.io.register_write(this.ata_port|1,this,function(h){C("1F1/lba_count: "+m(h),32768);this.master.lba_count=(this.master.lba_count<<8|h)&65535;this.slave.lba_count=(this.slave.lba_count<<8|h)&65535});a.io.register_write(this.ata_port|2,this,function(h){C("1F2/bytecount: "+m(h),32768);this.master.bytecount=
468
+ (this.master.bytecount<<8|h)&65535;this.slave.bytecount=(this.slave.bytecount<<8|h)&65535});a.io.register_write(this.ata_port|3,this,function(h){C("1F3/sector: "+m(h),32768);this.master.sector=(this.master.sector<<8|h)&65535;this.slave.sector=(this.slave.sector<<8|h)&65535});a.io.register_write(this.ata_port|4,this,function(h){C("1F4/sector low: "+m(h),32768);this.master.cylinder_low=(this.master.cylinder_low<<8|h)&65535;this.slave.cylinder_low=(this.slave.cylinder_low<<8|h)&65535});a.io.register_write(this.ata_port|
469
+ 5,this,function(h){C("1F5/sector high: "+m(h),32768);this.master.cylinder_high=(this.master.cylinder_high<<8|h)&65535;this.slave.cylinder_high=(this.slave.cylinder_high<<8|h)&65535});a.io.register_write(this.ata_port|6,this,function(h){var l=h&16;C("1F6/drive: "+m(h,2),32768);l?(C("Slave",32768),this.current_interface=this.slave):this.current_interface=this.master;this.master.drive_head=h;this.slave.drive_head=h;this.master.is_lba=this.slave.is_lba=h>>6&1;this.master.head=this.slave.head=h&15});this.dma_command=
470
+ this.dma_status=this.prdt_addr=0;a.io.register_write(this.ata_port|7,this,function(h){C("lower irq",32768);this.cpu.device_lower_irq(this.irq);this.current_interface.ata_command(h)});a.io.register_read(this.master_port|4,this,void 0,void 0,this.dma_read_addr);a.io.register_write(this.master_port|4,this,void 0,void 0,this.dma_set_addr);a.io.register_read(this.master_port,this,this.dma_read_command8,void 0,this.dma_read_command);a.io.register_write(this.master_port,this,this.dma_write_command8,void 0,
471
+ this.dma_write_command);a.io.register_read(this.master_port|2,this,this.dma_read_status);a.io.register_write(this.master_port|2,this,this.dma_write_status);a.io.register_read(this.master_port|8,this,function(){C("DMA read 0x8",32768);return 0});a.io.register_read(this.master_port|10,this,function(){C("DMA read 0xA",32768);return 0});a.devices.pci.register_device(this);Object.seal(this)}Tc.prototype.read_status=function(){if(this.current_interface.buffer){var a=this.current_interface.status;C("ATA read status: "+
472
+ m(a,2),32768);return a}return 0};Tc.prototype.write_control=function(a){C("set device control: "+m(a,2)+" interrupts "+(a&2?"disabled":"enabled"),32768);a&4&&(C("Reset via control port",32768),this.cpu.device_lower_irq(this.irq),this.master.device_reset(),this.slave.device_reset());this.device_control=a};Tc.prototype.dma_read_addr=function(){C("dma get address: "+m(this.prdt_addr,8),32768);return this.prdt_addr};Tc.prototype.dma_set_addr=function(a){C("dma set address: "+m(a,8),32768);this.prdt_addr=
473
+ a};Tc.prototype.dma_read_status=function(){C("DMA read status: "+m(this.dma_status),32768);return this.dma_status};Tc.prototype.dma_write_status=function(a){C("DMA set status: "+m(a),32768);this.dma_status&=~(a&6)};Tc.prototype.dma_read_command=function(){return this.dma_read_command8()|this.dma_read_status()<<16};Tc.prototype.dma_read_command8=function(){C("DMA read command: "+m(this.dma_command),32768);return this.dma_command};Tc.prototype.dma_write_command=function(a){C("DMA write command: "+m(a),
474
+ 32768);this.dma_write_command8(a&255);this.dma_write_status(a>>16&255)};Tc.prototype.dma_write_command8=function(a){C("DMA write command8: "+m(a),32768);const b=this.dma_command;this.dma_command=a&9;if((b&1)!==(a&1))if(0===(a&1))this.dma_status&=-2;else switch(this.dma_status|=1,this.current_interface.current_command){case 37:case 200:this.current_interface.do_ata_read_sectors_dma();break;case 202:case 53:this.current_interface.do_ata_write_sectors_dma();break;case 160:this.current_interface.do_atapi_dma();
475
+ break;default:C("Spurious dma command write, current command: "+m(this.current_interface.current_command),32768),k(!1)}};Tc.prototype.push_irq=function(){0===(this.device_control&2)&&(C("push irq",32768),this.dma_status|=4,this.cpu.device_raise_irq(this.irq))};Tc.prototype.get_state=function(){var a=[];a[0]=this.master;a[1]=this.slave;a[2]=this.ata_port;a[3]=this.irq;a[4]=this.pci_id;a[5]=this.ata_port_high;a[6]=this.master_port;a[7]=this.name;a[8]=this.device_control;a[9]=this.prdt_addr;a[10]=this.dma_status;
476
+ a[11]=this.current_interface===this.master;a[12]=this.dma_command;return a};Tc.prototype.set_state=function(a){this.master.set_state(a[0]);this.slave.set_state(a[1]);this.ata_port=a[2];this.irq=a[3];this.pci_id=a[4];this.ata_port_high=a[5];this.master_port=a[6];this.name=a[7];this.device_control=a[8];this.prdt_addr=a[9];this.dma_status=a[10];this.current_interface=a[11]?this.master:this.slave;this.dma_command=a[12]};function W(a,b,c,d,e,f,h){this.device=a;this.bus=h;this.nr=e;this.cpu=b;this.buffer=
477
+ c;this.sector_size=d?2048:512;this.is_atapi=d;this.cylinder_count=this.sectors_per_track=this.head_count=this.sector_count=0;this.buffer&&(this.sector_count=this.buffer.byteLength/this.sector_size,this.sector_count!==(this.sector_count|0)&&(C("Warning: Disk size not aligned with sector size",32768),this.sector_count=Math.ceil(this.sector_count)),d?(this.head_count=1,this.sectors_per_track=0):(this.head_count=16,this.sectors_per_track=63),this.cylinder_count=this.sector_count/this.head_count/this.sectors_per_track,
478
+ this.cylinder_count!==(this.cylinder_count|0)&&(C("Warning: Rounding up cylinder count. Choose different head number",32768),this.cylinder_count=Math.floor(this.cylinder_count)),a=b.devices.rtc,a.cmos_write(57,a.cmos_read(57)|1<<4*this.nr),a.cmos_write(18,a.cmos_read(18)&15|240),a.cmos_write(27,this.cylinder_count&255),a.cmos_write(28,this.cylinder_count>>8&255),a.cmos_write(29,this.head_count&255),a.cmos_write(30,255),a.cmos_write(31,255),a.cmos_write(32,200),a.cmos_write(33,this.cylinder_count&
284
479
  255),a.cmos_write(34,this.cylinder_count>>8&255),a.cmos_write(35,this.sectors_per_track&255));this.buffer=c;this.drive_head=this.head=this.cylinder_high=this.cylinder_low=this.lba_count=this.sector=this.bytecount=this.is_lba=0;this.status=80;this.sectors_per_drq=128;this.data_pointer=this.error=0;this.data=new Uint8Array(65536);this.data16=new Uint16Array(this.data.buffer);this.data32=new Int32Array(this.data.buffer);this.data_end=this.data_length=0;this.current_atapi_command=this.current_command=
285
- -1;this.last_io_id=this.write_dest=0;this.in_progress_io_ids=new Set;this.cancelled_io_ids=new Set;Object.seal(this)}R.prototype.device_reset=function(){this.is_atapi?(this.status=0,this.sector=this.error=this.bytecount=1,this.cylinder_low=20,this.cylinder_high=235):(this.status=81,this.sector=this.error=this.bytecount=1,this.cylinder_high=this.cylinder_low=0);this.cancel_io_operations()};R.prototype.push_irq=function(){this.device.push_irq()};R.prototype.ata_command=function(a){D("ATA Command: "+
286
- z(a)+" slave="+(this.drive_head>>4&1),32768);if(this.buffer)switch(this.current_command=a,this.error=0,a){case 8:D("ATA device reset",32768);this.data_length=this.data_end=this.data_pointer=0;this.device_reset();this.push_irq();break;case 16:this.status=80;this.cylinder_low=0;this.push_irq();break;case 248:this.status=80;a=this.sector_count-1;this.sector=a&255;this.cylinder_low=a>>8&255;this.cylinder_high=a>>16&255;this.drive_head=this.drive_head&240|a>>24&15;this.push_irq();break;case 39:this.status=
480
+ -1;this.last_io_id=this.write_dest=0;this.in_progress_io_ids=new Set;this.cancelled_io_ids=new Set;Object.seal(this)}W.prototype.device_reset=function(){this.is_atapi?(this.status=0,this.sector=this.error=this.bytecount=1,this.cylinder_low=20,this.cylinder_high=235):(this.status=81,this.sector=this.error=this.bytecount=1,this.cylinder_high=this.cylinder_low=0);this.cancel_io_operations()};W.prototype.push_irq=function(){this.device.push_irq()};W.prototype.ata_command=function(a){C("ATA Command: "+
481
+ m(a)+" slave="+(this.drive_head>>4&1),32768);if(this.buffer)switch(this.current_command=a,this.error=0,a){case 8:C("ATA device reset",32768);this.data_length=this.data_end=this.data_pointer=0;this.device_reset();this.push_irq();break;case 16:this.status=80;this.cylinder_low=0;this.push_irq();break;case 248:this.status=80;a=this.sector_count-1;this.sector=a&255;this.cylinder_low=a>>8&255;this.cylinder_high=a>>16&255;this.drive_head=this.drive_head&240|a>>24&15;this.push_irq();break;case 39:this.status=
287
482
  80;a=this.sector_count-1;this.sector=a&255;this.cylinder_low=a>>8&255;this.cylinder_high=a>>16&255;this.sector|=a>>24<<8&65280;this.push_irq();break;case 32:case 36:case 41:case 196:this.ata_read_sectors(a);break;case 48:case 52:case 57:case 197:this.ata_write_sectors(a);break;case 144:this.push_irq();this.error=257;this.status=80;break;case 145:this.status=80;this.push_irq();break;case 160:this.is_atapi&&(this.status=88,this.data_allocate(12),this.data_end=12,this.bytecount=1,this.push_irq());break;
288
- case 161:D("ATA identify packet device",32768);this.is_atapi?(this.create_identify_packet(),this.status=88,this.cylinder_low=20,this.cylinder_high=235):this.status=65;this.push_irq();break;case 198:D("Logical sectors per DRQ Block: "+z(this.bytecount&255),32768);this.sectors_per_drq=this.bytecount&255;this.status=80;this.push_irq();break;case 37:case 200:this.ata_read_sectors_dma(a);break;case 53:case 202:this.ata_write_sectors_dma(a);break;case 64:D("read verify sectors",32768);this.status=80;this.push_irq();
289
- break;case 218:D("Unimplemented: get media status",32768);this.status=65;this.error=4;this.push_irq();break;case 224:D("ATA standby immediate",32768);this.status=80;this.push_irq();break;case 225:D("ATA idle immediate",32768);this.status=80;this.push_irq();break;case 231:D("ATA flush cache",32768);this.status=80;this.push_irq();break;case 236:D("ATA identify device",32768);if(this.is_atapi){this.status=65;this.error=4;this.push_irq();break}this.create_identify_packet();this.status=88;this.push_irq();
290
- break;case 234:D("flush cache ext",32768);this.status=80;this.push_irq();break;case 239:D("set features: "+z(this.bytecount&255),32768);this.status=80;this.push_irq();break;case 222:this.status=80;this.push_irq();break;case 245:D("security freeze lock",32768);this.status=80;this.push_irq();break;case 249:D("Unimplemented: set max address",32768);this.status=65;this.error=4;break;default:m(!1,"New ATA cmd on 1F7: "+z(a),32768),this.status=65,this.error=4}else D("abort: No buffer",32768),this.error=
291
- 4,this.status=65,this.push_irq()};R.prototype.atapi_handle=function(){D("ATAPI Command: "+z(this.data[0])+" slave="+(this.drive_head>>4&1),32768);this.data_pointer=0;this.current_atapi_command=this.data[0];switch(this.current_atapi_command){case 0:D("test unit ready",32768);this.data_allocate(0);this.data_end=this.data_length;this.status=80;break;case 3:this.data_allocate(this.data[4]);this.data_end=this.data_length;this.status=88;this.data[0]=240;this.data[2]=5;this.data[7]=8;break;case 18:var a=
292
- this.data[4];this.status=88;D("inquiry: "+z(this.data[1],2)+" length="+a,32768);this.data.set([5,128,1,49,31,0,0,0,83,79,78,89,32,32,32,32,67,68,45,82,79,77,32,67,68,85,45,49,48,48,48,32,49,46,49,97]);this.data_end=this.data_length=Math.min(36,a);break;case 26:this.data_allocate(this.data[4]);this.data_end=this.data_length;this.status=88;break;case 30:this.data_allocate(0);this.data_end=this.data_length;this.status=80;break;case 37:a=this.sector_count-1;this.data_set(new Uint8Array([a>>24&255,a>>
293
- 16&255,a>>8&255,a&255,0,0,this.sector_size>>8&255,this.sector_size&255]));this.data_end=this.data_length;this.status=88;break;case 40:this.lba_count&1?this.atapi_read_dma(this.data):this.atapi_read(this.data);break;case 66:a=this.data[8];this.data_allocate(Math.min(8,a));this.data_end=this.data_length;D("read q subcode: length="+a,32768);this.status=88;break;case 67:a=this.data[8]|this.data[7]<<8;var b=this.data[9]>>6;this.data_allocate(a);this.data_end=this.data_length;D("read toc: "+z(b,2)+" length="+
294
- a+" "+(this.data[1]&2)+" "+z(this.data[6]),32768);0===b?(a=this.sector_count,this.data.set(new Uint8Array([0,18,1,1,0,20,1,0,0,0,0,0,0,22,170,0,a>>24,a>>16&255,a>>8&255,a&255]))):1===b?this.data.set(new Uint8Array([0,10,1,1,0,0,0,0,0,0,0,0])):m(!1,"Unimplemented format: "+b);this.status=88;break;case 70:a=this.data[8]|this.data[7]<<8;a=Math.min(a,32);this.data_allocate(a);this.data_end=this.data_length;this.data[0]=a-4>>24&255;this.data[1]=a-4>>16&255;this.data[2]=a-4>>8&255;this.data[3]=a-4&255;
295
- this.data[6]=8;this.data[10]=3;this.status=88;break;case 81:this.data_allocate(0);this.data_end=this.data_length;this.status=80;break;case 82:D("Unimplemented ATAPI command: "+z(this.data[0]),32768);this.status=81;this.data_length=0;this.error=80;break;case 90:a=this.data[8]|this.data[7]<<8;b=this.data[2];D("mode sense: "+z(b)+" length="+a,32768);42===b&&this.data_allocate(Math.min(30,a));this.data_end=this.data_length;this.status=88;break;case 189:this.data_allocate(this.data[9]|this.data[8]<<8);
296
- this.data_end=this.data_length;this.data[5]=1;this.status=88;break;case 74:this.status=81;this.data_length=0;this.error=80;D("Unimplemented ATAPI command: "+z(this.data[0]),32768);break;case 190:D("Unimplemented ATAPI command: "+z(this.data[0]),32768);this.data_allocate(0);this.data_end=this.data_length;this.status=80;break;default:this.status=81,this.data_length=0,this.error=80,D("Unimplemented ATAPI command: "+z(this.data[0]),32768),m(!1)}this.bytecount=this.bytecount&-8|2;0===(this.status&128)&&
297
- this.push_irq();0===(this.status&128)&&0===this.data_length&&(this.bytecount|=1,this.status&=-9)};R.prototype.do_write=function(){this.status=80;m(this.data_length<=this.data.length);var a=this.data.subarray(0,this.data_length);m(0===this.data_length%512);this.ata_advance(this.current_command,this.data_length/512);this.push_irq();this.buffer.set(this.write_dest,a,function(){});this.report_write(this.data_length)};R.prototype.atapi_read=function(a){var b=a[2]<<24|a[3]<<16|a[4]<<8|a[5],c=a[7]<<8|a[8];
298
- a=a[1];var d=c*this.sector_size,e=b*this.sector_size;D("CD read lba="+z(b)+" lbacount="+z(c)+" bytecount="+z(d)+" flags="+z(a),32768);this.data_length=0;var f=this.cylinder_high<<8&65280|this.cylinder_low&255;D(z(this.cylinder_high,2)+" "+z(this.cylinder_low,2),32768);this.cylinder_low=this.cylinder_high=0;65535===f&&f--;f>d&&(f=d);e>=this.buffer.byteLength?(m(!1,"CD read: Outside of disk end="+z(e+d)+" size="+z(this.buffer.byteLength),32768),this.status=255,this.push_irq()):0===d?(this.status=80,
299
- this.data_pointer=0):(d=Math.min(d,this.buffer.byteLength-e),this.status=208,this.report_read_start(),this.read_buffer(e,d,h=>{D("cd read: data arrived",32768);this.data_set(h);this.status=88;this.bytecount=this.bytecount&-8|2;this.push_irq();this.data_end=f&=-4;this.data_end>this.data_length&&(this.data_end=this.data_length);this.cylinder_low=this.data_end&255;this.cylinder_high=this.data_end>>8&255;this.report_read_end(d)}))};R.prototype.atapi_read_dma=function(a){var b=a[2]<<24|a[3]<<16|a[4]<<
300
- 8|a[5],c=a[7]<<8|a[8];a=a[1];var d=c*this.sector_size,e=b*this.sector_size;D("CD read DMA lba="+z(b)+" lbacount="+z(c)+" bytecount="+z(d)+" flags="+z(a),32768);e>=this.buffer.byteLength?(m(!1,"CD read: Outside of disk end="+z(e+d)+" size="+z(this.buffer.byteLength),32768),this.status=255,this.push_irq()):(this.status=208,this.report_read_start(),this.read_buffer(e,d,f=>{D("atapi_read_dma: Data arrived");this.report_read_end(d);this.status=88;this.bytecount=this.bytecount&-8|2;this.data_set(f);this.do_atapi_dma()}))};
301
- R.prototype.do_atapi_dma=function(){if(0===(this.device.dma_status&1))D("do_atapi_dma: Status not set",32768);else if(0===(this.status&8))D("do_atapi_dma: DRQ not set",32768);else{D("atapi dma transfer len="+this.data_length,32768);var a=this.device.prdt_addr,b=0,c=this.data;do{var d=this.cpu.read32s(a),e=this.cpu.read16(a+4),f=this.cpu.read8(a+7)&128;e||(e=65536);D("dma read dest="+z(d)+" count="+z(e)+" datalen="+z(this.data_length),32768);this.cpu.write_blob(c.subarray(b,Math.min(b+e,this.data_length)),
302
- d);b+=e;a+=8;if(b>=this.data_length&&!f){D("leave early end="+ +f+" offset="+z(b)+" data_length="+z(this.data_length)+" cmd="+z(this.current_command),32768);break}}while(!f);D("end offset="+b,32768);this.status=80;this.device.dma_status&=-2;this.bytecount=this.bytecount&-8|3;this.push_irq()}};R.prototype.read_data=function(a){if(this.data_pointer<this.data_end){m(this.data_pointer+a-1<this.data_end);m(0===this.data_pointer%a,z(this.data_pointer)+" "+a);var b=1===a?this.data[this.data_pointer]:2===
303
- a?this.data16[this.data_pointer>>>1]:this.data32[this.data_pointer>>>2];this.data_pointer+=a;0===(this.data_pointer&(0===(this.data_end&4095)?4095:255))&&D("Read 1F0: "+z(this.data[this.data_pointer],2)+" cur="+z(this.data_pointer)+" cnt="+z(this.data_length),32768);this.data_pointer>=this.data_end&&this.read_end();return b}D("Read 1F0: empty",32768);this.data_pointer+=a;return 0};R.prototype.read_end=function(){D("read_end cmd="+z(this.current_command)+" data_pointer="+z(this.data_pointer)+" end="+
304
- z(this.data_end)+" length="+z(this.data_length),32768);if(160===this.current_command)if(this.data_end===this.data_length)this.status=80,this.bytecount=this.bytecount&-8|3,this.push_irq();else{this.status=88;this.bytecount=this.bytecount&-8|2;this.push_irq();var a=this.cylinder_high<<8&65280|this.cylinder_low&255;this.data_end+a>this.data_length?(this.cylinder_low=this.data_length-this.data_end&255,this.cylinder_high=this.data_length-this.data_end>>8&255,this.data_end=this.data_length):this.data_end+=
305
- a;D("data_end="+z(this.data_end),32768)}else this.error=0,this.data_pointer>=this.data_length?this.status=80:(196===this.current_command||41===this.current_command?(a=Math.min(this.sectors_per_drq,(this.data_length-this.data_end)/512),m(0===a%1)):(m(32===this.current_command||36===this.current_command),a=1),this.ata_advance(this.current_command,a),this.data_end+=512*a,this.status=88,this.push_irq())};R.prototype.write_data_port=function(a,b){m(0===this.data_pointer%b);this.data_pointer>=this.data_end?
306
- D("Redundant write to data port: "+z(a)+" count="+z(this.data_end)+" cur="+z(this.data_pointer),32768):((0===(this.data_pointer+b&(0===(this.data_end&4095)?4095:255))||20>this.data_end)&&D("Data port: "+z(a>>>0)+" count="+z(this.data_end)+" cur="+z(this.data_pointer),32768),1===b?this.data[this.data_pointer++]=a:2===b?(this.data16[this.data_pointer>>>1]=a,this.data_pointer+=2):(this.data32[this.data_pointer>>>2]=a,this.data_pointer+=4),m(this.data_pointer<=this.data_end),this.data_pointer===this.data_end&&
307
- this.write_end())};R.prototype.write_data_port8=function(a){this.write_data_port(a,1)};R.prototype.write_data_port16=function(a){this.write_data_port(a,2)};R.prototype.write_data_port32=function(a){this.write_data_port(a,4)};R.prototype.write_end=function(){160===this.current_command?this.atapi_handle():(D("write_end data_pointer="+z(this.data_pointer)+" data_length="+z(this.data_length),32768),this.data_pointer>=this.data_length?this.do_write():(m(48===this.current_command||52===this.current_command||
308
- 197===this.current_command,"Unexpected command: "+z(this.current_command)),this.status=88,this.data_end+=512,this.push_irq()))};R.prototype.ata_advance=function(a,b){D("Advance sectors="+b+" old_bytecount="+this.bytecount,32768);this.bytecount-=b;36===a||41===a||52===a||57===a||37===a||53===a?(a=b+this.get_lba48(),this.sector=a&255|a>>16&65280,this.cylinder_low=a>>8&255,this.cylinder_high=a>>16&255):this.is_lba?(a=b+this.get_lba28(),this.sector=a&255,this.cylinder_low=a>>8&255,this.cylinder_high=
309
- a>>16&255,this.head=this.head&-16|a&15):(a=b+this.get_chs(),b=a/(this.head_count*this.sectors_per_track)|0,this.cylinder_low=b&255,this.cylinder_high=b>>8&255,this.head=(a/this.sectors_per_track|0)%this.head_count&15,this.sector=a%this.sectors_per_track+1&255,m(a===this.get_chs()))};R.prototype.ata_read_sectors=function(a){var b=36===a||41===a,c=this.get_count(b);b=this.get_lba(b);var d=32===a||36===a,e=c*this.sector_size,f=b*this.sector_size;D("ATA read cmd="+z(a)+" mode="+(this.is_lba?"lba":"chs")+
310
- " lba="+z(b)+" lbacount="+z(c)+" bytecount="+z(e),32768);f+e>this.buffer.byteLength?(m(!1,"ATA read: Outside of disk",32768),this.status=255,this.push_irq()):(this.status=192,this.report_read_start(),this.read_buffer(f,e,h=>{D("ata_read: Data arrived",32768);this.data_set(h);this.status=88;this.data_end=d?512:Math.min(e,512*this.sectors_per_drq);this.ata_advance(a,d?1:Math.min(c,this.sectors_per_track));this.push_irq();this.report_read_end(e)}))};R.prototype.ata_read_sectors_dma=function(a){var b=
311
- 37===a;a=this.get_count(b);b=this.get_lba(b);var c=a*this.sector_size,d=b*this.sector_size;D("ATA DMA read lba="+z(b)+" lbacount="+z(a)+" bytecount="+z(c),32768);d+c>this.buffer.byteLength?(m(!1,"ATA read: Outside of disk",32768),this.status=255,this.push_irq()):(this.status=88,this.device.dma_status|=1)};R.prototype.do_ata_read_sectors_dma=function(){var a=37===this.current_command,b=this.get_count(a);a=this.get_lba(a);var c=b*this.sector_size,d=a*this.sector_size;m(a<this.buffer.byteLength);this.report_read_start();
312
- var e=this.device.prdt_addr;this.read_buffer(d,c,f=>{D("do_ata_read_sectors_dma: Data arrived",32768);var h=this.device.prdt_addr,l=0;m(e===h);do{var n=this.cpu.read32s(h),p=this.cpu.read16(h+4),q=this.cpu.read8(h+7)&128;p||(p=65536,D("dma: prd count was 0",32768));D("dma read transfer dest="+z(n)+" prd_count="+z(p),32768);this.cpu.write_blob(f.subarray(l,l+p),n);l+=p;h+=8}while(!q);m(l===c);this.ata_advance(this.current_command,b);this.status=80;this.device.dma_status&=-2;this.current_command=-1;
313
- this.push_irq();this.report_read_end(c)})};R.prototype.ata_write_sectors=function(a){var b=52===a||57===a,c=this.get_count(b);b=this.get_lba(b);a=48===a||52===a;var d=c*this.sector_size,e=b*this.sector_size;D("ATA write lba="+z(b)+" mode="+(this.is_lba?"lba":"chs")+" lbacount="+z(c)+" bytecount="+z(d),32768);e+d>this.buffer.byteLength?(m(!1,"ATA write: Outside of disk",32768),this.status=255,this.push_irq()):(this.status=88,this.data_allocate_noclear(d),this.data_end=a?512:Math.min(d,512*this.sectors_per_drq),
314
- this.write_dest=e)};R.prototype.ata_write_sectors_dma=function(a){var b=53===a;a=this.get_count(b);b=this.get_lba(b);var c=a*this.sector_size,d=b*this.sector_size;D("ATA DMA write lba="+z(b)+" lbacount="+z(a)+" bytecount="+z(c),32768);d+c>this.buffer.byteLength?(m(!1,"ATA DMA write: Outside of disk",32768),this.status=255,this.push_irq()):(this.status=88,this.device.dma_status|=1)};R.prototype.do_ata_write_sectors_dma=function(){var a=53===this.current_command,b=this.get_count(a),c=this.get_lba(a);
315
- a=b*this.sector_size;c*=this.sector_size;var d=this.device.prdt_addr,e=0;D("prdt addr: "+z(d,8),32768);const f=new Uint8Array(a);do{var h=this.cpu.read32s(d),l=this.cpu.read16(d+4),n=this.cpu.read8(d+7)&128;l||(l=65536,D("dma: prd count was 0",32768));D("dma write transfer dest="+z(h)+" prd_count="+z(l),32768);h=this.cpu.mem8.subarray(h,h+l);m(h.length===l);f.set(h,e);e+=l;d+=8}while(!n);m(e===f.length);this.buffer.set(c,f,()=>{D("dma write completed",32768);this.ata_advance(this.current_command,
316
- b);this.status=80;this.push_irq();this.device.dma_status&=-2;this.current_command=-1});this.report_write(a)};R.prototype.get_chs=function(){var a=this.cylinder_low&255|this.cylinder_high<<8&65280,b=this.head,c=this.sector&255;D("get_chs: c="+a+" h="+b+" s="+c,32768);return(a*this.head_count+b)*this.sectors_per_track+c-1};R.prototype.get_lba28=function(){return this.sector&255|this.cylinder_low<<8&65280|this.cylinder_high<<16&16711680|(this.head&15)<<24};R.prototype.get_lba48=function(){return(this.sector&
317
- 255|this.cylinder_low<<8&65280|this.cylinder_high<<16&16711680|this.sector>>8<<24&4278190080)>>>0};R.prototype.get_lba=function(a){return a?this.get_lba48():this.is_lba?this.get_lba28():this.get_chs()};R.prototype.get_count=function(a){a?(a=this.bytecount,0===a&&(a=65536)):(a=this.bytecount&255,0===a&&(a=256));return a};R.prototype.create_identify_packet=function(){if(this.drive_head&16)this.data_allocate(0);else{for(var a=0;512>a;a++)this.data[a]=0;a=Math.min(16383,this.cylinder_count);this.data_set([64,
483
+ case 161:C("ATA identify packet device",32768);this.is_atapi?(this.create_identify_packet(),this.status=88,this.cylinder_low=20,this.cylinder_high=235):this.status=65;this.push_irq();break;case 198:C("Logical sectors per DRQ Block: "+m(this.bytecount&255),32768);this.sectors_per_drq=this.bytecount&255;this.status=80;this.push_irq();break;case 37:case 200:this.ata_read_sectors_dma(a);break;case 53:case 202:this.ata_write_sectors_dma(a);break;case 64:C("read verify sectors",32768);this.status=80;this.push_irq();
484
+ break;case 218:C("Unimplemented: get media status",32768);this.status=65;this.error=4;this.push_irq();break;case 224:C("ATA standby immediate",32768);this.status=80;this.push_irq();break;case 225:C("ATA idle immediate",32768);this.status=80;this.push_irq();break;case 231:C("ATA flush cache",32768);this.status=80;this.push_irq();break;case 236:C("ATA identify device",32768);if(this.is_atapi){this.status=65;this.error=4;this.push_irq();break}this.create_identify_packet();this.status=88;this.push_irq();
485
+ break;case 234:C("flush cache ext",32768);this.status=80;this.push_irq();break;case 239:C("set features: "+m(this.bytecount&255),32768);this.status=80;this.push_irq();break;case 222:this.status=80;this.push_irq();break;case 245:C("security freeze lock",32768);this.status=80;this.push_irq();break;case 249:C("Unimplemented: set max address",32768);this.status=65;this.error=4;break;default:k(!1,"New ATA cmd on 1F7: "+m(a),32768),this.status=65,this.error=4}else C("abort: No buffer",32768),this.error=
486
+ 4,this.status=65,this.push_irq()};W.prototype.atapi_handle=function(){C("ATAPI Command: "+m(this.data[0])+" slave="+(this.drive_head>>4&1),32768);this.data_pointer=0;this.current_atapi_command=this.data[0];switch(this.current_atapi_command){case 0:C("test unit ready",32768);this.data_allocate(0);this.data_end=this.data_length;this.status=80;break;case 3:this.data_allocate(this.data[4]);this.data_end=this.data_length;this.status=88;this.data[0]=240;this.data[2]=5;this.data[7]=8;break;case 18:var a=
487
+ this.data[4];this.status=88;C("inquiry: "+m(this.data[1],2)+" length="+a,32768);this.data.set([5,128,1,49,31,0,0,0,83,79,78,89,32,32,32,32,67,68,45,82,79,77,32,67,68,85,45,49,48,48,48,32,49,46,49,97]);this.data_end=this.data_length=Math.min(36,a);break;case 26:this.data_allocate(this.data[4]);this.data_end=this.data_length;this.status=88;break;case 30:this.data_allocate(0);this.data_end=this.data_length;this.status=80;break;case 37:a=this.sector_count-1;this.data_set(new Uint8Array([a>>24&255,a>>
488
+ 16&255,a>>8&255,a&255,0,0,this.sector_size>>8&255,this.sector_size&255]));this.data_end=this.data_length;this.status=88;break;case 40:this.lba_count&1?this.atapi_read_dma(this.data):this.atapi_read(this.data);break;case 66:a=this.data[8];this.data_allocate(Math.min(8,a));this.data_end=this.data_length;C("read q subcode: length="+a,32768);this.status=88;break;case 67:a=this.data[8]|this.data[7]<<8;var b=this.data[9]>>6;this.data_allocate(a);this.data_end=this.data_length;C("read toc: "+m(b,2)+" length="+
489
+ a+" "+(this.data[1]&2)+" "+m(this.data[6]),32768);0===b?(a=this.sector_count,this.data.set(new Uint8Array([0,18,1,1,0,20,1,0,0,0,0,0,0,22,170,0,a>>24,a>>16&255,a>>8&255,a&255]))):1===b?this.data.set(new Uint8Array([0,10,1,1,0,0,0,0,0,0,0,0])):k(!1,"Unimplemented format: "+b);this.status=88;break;case 70:a=this.data[8]|this.data[7]<<8;a=Math.min(a,32);this.data_allocate(a);this.data_end=this.data_length;this.data[0]=a-4>>24&255;this.data[1]=a-4>>16&255;this.data[2]=a-4>>8&255;this.data[3]=a-4&255;
490
+ this.data[6]=8;this.data[10]=3;this.status=88;break;case 81:this.data_allocate(0);this.data_end=this.data_length;this.status=80;break;case 82:C("Unimplemented ATAPI command: "+m(this.data[0]),32768);this.status=81;this.data_length=0;this.error=80;break;case 90:a=this.data[8]|this.data[7]<<8;b=this.data[2];C("mode sense: "+m(b)+" length="+a,32768);42===b&&this.data_allocate(Math.min(30,a));this.data_end=this.data_length;this.status=88;break;case 189:this.data_allocate(this.data[9]|this.data[8]<<8);
491
+ this.data_end=this.data_length;this.data[5]=1;this.status=88;break;case 74:this.status=81;this.data_length=0;this.error=80;C("Unimplemented ATAPI command: "+m(this.data[0]),32768);break;case 190:C("Unimplemented ATAPI command: "+m(this.data[0]),32768);this.data_allocate(0);this.data_end=this.data_length;this.status=80;break;default:this.status=81,this.data_length=0,this.error=80,C("Unimplemented ATAPI command: "+m(this.data[0]),32768),k(!1)}this.bytecount=this.bytecount&-8|2;0===(this.status&128)&&
492
+ this.push_irq();0===(this.status&128)&&0===this.data_length&&(this.bytecount|=1,this.status&=-9)};W.prototype.do_write=function(){this.status=80;k(this.data_length<=this.data.length);var a=this.data.subarray(0,this.data_length);k(0===this.data_length%512);this.ata_advance(this.current_command,this.data_length/512);this.push_irq();this.buffer.set(this.write_dest,a,function(){});this.report_write(this.data_length)};W.prototype.atapi_read=function(a){var b=a[2]<<24|a[3]<<16|a[4]<<8|a[5],c=a[7]<<8|a[8];
493
+ a=a[1];var d=c*this.sector_size,e=b*this.sector_size;C("CD read lba="+m(b)+" lbacount="+m(c)+" bytecount="+m(d)+" flags="+m(a),32768);this.data_length=0;var f=this.cylinder_high<<8&65280|this.cylinder_low&255;C(m(this.cylinder_high,2)+" "+m(this.cylinder_low,2),32768);this.cylinder_low=this.cylinder_high=0;65535===f&&f--;f>d&&(f=d);e>=this.buffer.byteLength?(k(!1,"CD read: Outside of disk end="+m(e+d)+" size="+m(this.buffer.byteLength),32768),this.status=255,this.push_irq()):0===d?(this.status=80,
494
+ this.data_pointer=0):(d=Math.min(d,this.buffer.byteLength-e),this.status=208,this.report_read_start(),this.read_buffer(e,d,h=>{C("cd read: data arrived",32768);this.data_set(h);this.status=88;this.bytecount=this.bytecount&-8|2;this.push_irq();this.data_end=f&=-4;this.data_end>this.data_length&&(this.data_end=this.data_length);this.cylinder_low=this.data_end&255;this.cylinder_high=this.data_end>>8&255;this.report_read_end(d)}))};W.prototype.atapi_read_dma=function(a){var b=a[2]<<24|a[3]<<16|a[4]<<
495
+ 8|a[5],c=a[7]<<8|a[8];a=a[1];var d=c*this.sector_size,e=b*this.sector_size;C("CD read DMA lba="+m(b)+" lbacount="+m(c)+" bytecount="+m(d)+" flags="+m(a),32768);e>=this.buffer.byteLength?(k(!1,"CD read: Outside of disk end="+m(e+d)+" size="+m(this.buffer.byteLength),32768),this.status=255,this.push_irq()):(this.status=208,this.report_read_start(),this.read_buffer(e,d,f=>{C("atapi_read_dma: Data arrived");this.report_read_end(d);this.status=88;this.bytecount=this.bytecount&-8|2;this.data_set(f);this.do_atapi_dma()}))};
496
+ W.prototype.do_atapi_dma=function(){if(0===(this.device.dma_status&1))C("do_atapi_dma: Status not set",32768);else if(0===(this.status&8))C("do_atapi_dma: DRQ not set",32768);else{C("atapi dma transfer len="+this.data_length,32768);var a=this.device.prdt_addr,b=0,c=this.data;do{var d=this.cpu.read32s(a),e=this.cpu.read16(a+4),f=this.cpu.read8(a+7)&128;e||(e=65536);C("dma read dest="+m(d)+" count="+m(e)+" datalen="+m(this.data_length),32768);this.cpu.write_blob(c.subarray(b,Math.min(b+e,this.data_length)),
497
+ d);b+=e;a+=8;if(b>=this.data_length&&!f){C("leave early end="+ +f+" offset="+m(b)+" data_length="+m(this.data_length)+" cmd="+m(this.current_command),32768);break}}while(!f);C("end offset="+b,32768);this.status=80;this.device.dma_status&=-2;this.bytecount=this.bytecount&-8|3;this.push_irq()}};W.prototype.read_data=function(a){if(this.data_pointer<this.data_end){k(this.data_pointer+a-1<this.data_end);k(0===this.data_pointer%a,m(this.data_pointer)+" "+a);var b=1===a?this.data[this.data_pointer]:2===
498
+ a?this.data16[this.data_pointer>>>1]:this.data32[this.data_pointer>>>2];this.data_pointer+=a;0===(this.data_pointer&(0===(this.data_end&4095)?4095:255))&&C("Read 1F0: "+m(this.data[this.data_pointer],2)+" cur="+m(this.data_pointer)+" cnt="+m(this.data_length),32768);this.data_pointer>=this.data_end&&this.read_end();return b}C("Read 1F0: empty",32768);this.data_pointer+=a;return 0};W.prototype.read_end=function(){C("read_end cmd="+m(this.current_command)+" data_pointer="+m(this.data_pointer)+" end="+
499
+ m(this.data_end)+" length="+m(this.data_length),32768);if(160===this.current_command)if(this.data_end===this.data_length)this.status=80,this.bytecount=this.bytecount&-8|3,this.push_irq();else{this.status=88;this.bytecount=this.bytecount&-8|2;this.push_irq();var a=this.cylinder_high<<8&65280|this.cylinder_low&255;this.data_end+a>this.data_length?(this.cylinder_low=this.data_length-this.data_end&255,this.cylinder_high=this.data_length-this.data_end>>8&255,this.data_end=this.data_length):this.data_end+=
500
+ a;C("data_end="+m(this.data_end),32768)}else this.error=0,this.data_pointer>=this.data_length?this.status=80:(196===this.current_command||41===this.current_command?(a=Math.min(this.sectors_per_drq,(this.data_length-this.data_end)/512),k(0===a%1)):(k(32===this.current_command||36===this.current_command),a=1),this.ata_advance(this.current_command,a),this.data_end+=512*a,this.status=88,this.push_irq())};W.prototype.write_data_port=function(a,b){k(0===this.data_pointer%b);this.data_pointer>=this.data_end?
501
+ C("Redundant write to data port: "+m(a)+" count="+m(this.data_end)+" cur="+m(this.data_pointer),32768):((0===(this.data_pointer+b&(0===(this.data_end&4095)?4095:255))||20>this.data_end)&&C("Data port: "+m(a>>>0)+" count="+m(this.data_end)+" cur="+m(this.data_pointer),32768),1===b?this.data[this.data_pointer++]=a:2===b?(this.data16[this.data_pointer>>>1]=a,this.data_pointer+=2):(this.data32[this.data_pointer>>>2]=a,this.data_pointer+=4),k(this.data_pointer<=this.data_end),this.data_pointer===this.data_end&&
502
+ this.write_end())};W.prototype.write_data_port8=function(a){this.write_data_port(a,1)};W.prototype.write_data_port16=function(a){this.write_data_port(a,2)};W.prototype.write_data_port32=function(a){this.write_data_port(a,4)};W.prototype.write_end=function(){160===this.current_command?this.atapi_handle():(C("write_end data_pointer="+m(this.data_pointer)+" data_length="+m(this.data_length),32768),this.data_pointer>=this.data_length?this.do_write():(k(48===this.current_command||52===this.current_command||
503
+ 197===this.current_command,"Unexpected command: "+m(this.current_command)),this.status=88,this.data_end+=512,this.push_irq()))};W.prototype.ata_advance=function(a,b){C("Advance sectors="+b+" old_bytecount="+this.bytecount,32768);this.bytecount-=b;36===a||41===a||52===a||57===a||37===a||53===a?(a=b+this.get_lba48(),this.sector=a&255|a>>16&65280,this.cylinder_low=a>>8&255,this.cylinder_high=a>>16&255):this.is_lba?(a=b+this.get_lba28(),this.sector=a&255,this.cylinder_low=a>>8&255,this.cylinder_high=
504
+ a>>16&255,this.head=this.head&-16|a&15):(a=b+this.get_chs(),b=a/(this.head_count*this.sectors_per_track)|0,this.cylinder_low=b&255,this.cylinder_high=b>>8&255,this.head=(a/this.sectors_per_track|0)%this.head_count&15,this.sector=a%this.sectors_per_track+1&255,k(a===this.get_chs()))};W.prototype.ata_read_sectors=function(a){var b=36===a||41===a,c=this.get_count(b);b=this.get_lba(b);var d=32===a||36===a,e=c*this.sector_size,f=b*this.sector_size;C("ATA read cmd="+m(a)+" mode="+(this.is_lba?"lba":"chs")+
505
+ " lba="+m(b)+" lbacount="+m(c)+" bytecount="+m(e),32768);f+e>this.buffer.byteLength?(k(!1,"ATA read: Outside of disk",32768),this.status=255,this.push_irq()):(this.status=192,this.report_read_start(),this.read_buffer(f,e,h=>{C("ata_read: Data arrived",32768);this.data_set(h);this.status=88;this.data_end=d?512:Math.min(e,512*this.sectors_per_drq);this.ata_advance(a,d?1:Math.min(c,this.sectors_per_track));this.push_irq();this.report_read_end(e)}))};W.prototype.ata_read_sectors_dma=function(a){var b=
506
+ 37===a;a=this.get_count(b);b=this.get_lba(b);var c=a*this.sector_size,d=b*this.sector_size;C("ATA DMA read lba="+m(b)+" lbacount="+m(a)+" bytecount="+m(c),32768);d+c>this.buffer.byteLength?(k(!1,"ATA read: Outside of disk",32768),this.status=255,this.push_irq()):(this.status=88,this.device.dma_status|=1)};W.prototype.do_ata_read_sectors_dma=function(){var a=37===this.current_command,b=this.get_count(a);a=this.get_lba(a);var c=b*this.sector_size,d=a*this.sector_size;k(a<this.buffer.byteLength);this.report_read_start();
507
+ var e=this.device.prdt_addr;this.read_buffer(d,c,f=>{C("do_ata_read_sectors_dma: Data arrived",32768);var h=this.device.prdt_addr,l=0;k(e===h);do{var p=this.cpu.read32s(h),n=this.cpu.read16(h+4),q=this.cpu.read8(h+7)&128;n||(n=65536,C("dma: prd count was 0",32768));C("dma read transfer dest="+m(p)+" prd_count="+m(n),32768);this.cpu.write_blob(f.subarray(l,l+n),p);l+=n;h+=8}while(!q);k(l===c);this.ata_advance(this.current_command,b);this.status=80;this.device.dma_status&=-2;this.current_command=-1;
508
+ this.push_irq();this.report_read_end(c)})};W.prototype.ata_write_sectors=function(a){var b=52===a||57===a,c=this.get_count(b);b=this.get_lba(b);a=48===a||52===a;var d=c*this.sector_size,e=b*this.sector_size;C("ATA write lba="+m(b)+" mode="+(this.is_lba?"lba":"chs")+" lbacount="+m(c)+" bytecount="+m(d),32768);e+d>this.buffer.byteLength?(k(!1,"ATA write: Outside of disk",32768),this.status=255,this.push_irq()):(this.status=88,this.data_allocate_noclear(d),this.data_end=a?512:Math.min(d,512*this.sectors_per_drq),
509
+ this.write_dest=e)};W.prototype.ata_write_sectors_dma=function(a){var b=53===a;a=this.get_count(b);b=this.get_lba(b);var c=a*this.sector_size,d=b*this.sector_size;C("ATA DMA write lba="+m(b)+" lbacount="+m(a)+" bytecount="+m(c),32768);d+c>this.buffer.byteLength?(k(!1,"ATA DMA write: Outside of disk",32768),this.status=255,this.push_irq()):(this.status=88,this.device.dma_status|=1)};W.prototype.do_ata_write_sectors_dma=function(){var a=53===this.current_command,b=this.get_count(a),c=this.get_lba(a);
510
+ a=b*this.sector_size;c*=this.sector_size;var d=this.device.prdt_addr,e=0;C("prdt addr: "+m(d,8),32768);const f=new Uint8Array(a);do{var h=this.cpu.read32s(d),l=this.cpu.read16(d+4),p=this.cpu.read8(d+7)&128;l||(l=65536,C("dma: prd count was 0",32768));C("dma write transfer dest="+m(h)+" prd_count="+m(l),32768);h=this.cpu.mem8.subarray(h,h+l);k(h.length===l);f.set(h,e);e+=l;d+=8}while(!p);k(e===f.length);this.buffer.set(c,f,()=>{C("dma write completed",32768);this.ata_advance(this.current_command,
511
+ b);this.status=80;this.push_irq();this.device.dma_status&=-2;this.current_command=-1});this.report_write(a)};W.prototype.get_chs=function(){var a=this.cylinder_low&255|this.cylinder_high<<8&65280,b=this.head,c=this.sector&255;C("get_chs: c="+a+" h="+b+" s="+c,32768);return(a*this.head_count+b)*this.sectors_per_track+c-1};W.prototype.get_lba28=function(){return this.sector&255|this.cylinder_low<<8&65280|this.cylinder_high<<16&16711680|(this.head&15)<<24};W.prototype.get_lba48=function(){return(this.sector&
512
+ 255|this.cylinder_low<<8&65280|this.cylinder_high<<16&16711680|this.sector>>8<<24&4278190080)>>>0};W.prototype.get_lba=function(a){return a?this.get_lba48():this.is_lba?this.get_lba28():this.get_chs()};W.prototype.get_count=function(a){a?(a=this.bytecount,0===a&&(a=65536)):(a=this.bytecount&255,0===a&&(a=256));return a};W.prototype.create_identify_packet=function(){if(this.drive_head&16)this.data_allocate(0);else{for(var a=0;512>a;a++)this.data[a]=0;a=Math.min(16383,this.cylinder_count);this.data_set([64,
318
513
  this.is_atapi?133:0,a,a>>8,0,0,this.head_count,this.head_count>>8,this.sectors_per_track/512,this.sectors_per_track/512>>8,0,2,this.sectors_per_track,this.sectors_per_track>>8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,2,4,0,0,0,0,0,0,0,0,0,56,118,32,54,68,72,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,128,0,1,0,0,2,0,0,0,2,0,2,7,0,a,a>>8,this.head_count,this.head_count>>8,this.sectors_per_track,0,this.sector_count&255,this.sector_count>>
319
514
  8&255,this.sector_count>>16&255,this.sector_count>>24&255,0,0,this.sector_count&255,this.sector_count>>8&255,this.sector_count>>16&255,this.sector_count>>24&255,0,0,160===this.current_command?0:7,160===this.current_command?0:4,0,0,30,0,30,0,30,0,30,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,126,0,0,0,0,0,0,116,0,64,0,64,0,116,0,64,0,0,0,0,0,0,0,0,0,0,1,96,0,0,0,0,0,0,0,0,0,0,0,0,this.sector_count&255,this.sector_count>>8&255,this.sector_count>>16&255,this.sector_count>>24&255]);this.data_end=this.data_length=
320
- 512}};R.prototype.data_allocate=function(a){this.data_allocate_noclear(a);for(var b=0;b<a+3>>2;b++)this.data32[b]=0};R.prototype.data_allocate_noclear=function(a){this.data.length<a&&(this.data=new Uint8Array(a+3&-4),this.data16=new Uint16Array(this.data.buffer),this.data32=new Int32Array(this.data.buffer));this.data_length=a;this.data_pointer=0};R.prototype.data_set=function(a){this.data_allocate_noclear(a.length);this.data.set(a)};R.prototype.report_read_start=function(){this.bus.send("ide-read-start")};
321
- R.prototype.report_read_end=function(a){this.bus.send("ide-read-end",[this.nr,a,a/this.sector_size|0])};R.prototype.report_write=function(a){this.bus.send("ide-write-end",[this.nr,a,a/this.sector_size|0])};R.prototype.read_buffer=function(a,b,c){const d=this.last_io_id++;this.in_progress_io_ids.add(d);this.buffer.get(a,b,e=>{if(this.cancelled_io_ids.delete(d))m(!this.in_progress_io_ids.has(d));else{var f=this.in_progress_io_ids.delete(d);m(f);c(e)}})};R.prototype.cancel_io_operations=function(){for(const a of this.in_progress_io_ids)this.cancelled_io_ids.add(a);
322
- this.in_progress_io_ids.clear()};R.prototype.get_state=function(){var a=[];a[0]=this.bytecount;a[1]=this.cylinder_count;a[2]=this.cylinder_high;a[3]=this.cylinder_low;a[4]=this.data_pointer;a[5]=0;a[6]=0;a[7]=0;a[8]=0;a[9]=this.drive_head;a[10]=this.error;a[11]=this.head;a[12]=this.head_count;a[13]=this.is_atapi;a[14]=this.is_lba;a[15]=this.lba_count;a[16]=this.data;a[17]=this.data_length;a[18]=this.sector;a[19]=this.sector_count;a[20]=this.sector_size;a[21]=this.sectors_per_drq;a[22]=this.sectors_per_track;
323
- a[23]=this.status;a[24]=this.write_dest;a[25]=this.current_command;a[26]=this.data_end;a[27]=this.current_atapi_command;a[28]=this.buffer;return a};R.prototype.set_state=function(a){this.bytecount=a[0];this.cylinder_count=a[1];this.cylinder_high=a[2];this.cylinder_low=a[3];this.data_pointer=a[4];this.drive_head=a[9];this.error=a[10];this.head=a[11];this.head_count=a[12];this.is_atapi=a[13];this.is_lba=a[14];this.lba_count=a[15];this.data=a[16];this.data_length=a[17];this.sector=a[18];this.sector_count=
324
- a[19];this.sector_size=a[20];this.sectors_per_drq=a[21];this.sectors_per_track=a[22];this.status=a[23];this.write_dest=a[24];this.current_command=a[25];this.data_end=a[26];this.current_atapi_command=a[27];this.data16=new Uint16Array(this.data.buffer);this.data32=new Int32Array(this.data.buffer);this.buffer&&this.buffer.set_state(a[28])};function Tb(a,b,c){this.bus=b;this.id=a.devices.net?1:0;this.status=this.pairs=1;this.preserve_mac_from_state_image=c;this.mac=new Uint8Array([0,34,21,255*Math.random()|
325
- 0,255*Math.random()|0,255*Math.random()|0]);this.bus.send("net"+this.id+"-mac",ob(this.mac));b=[];for(c=0;c<this.pairs;++c)b.push({size_supported:1024,notify_offset:0}),b.push({size_supported:1024,notify_offset:1});b.push({size_supported:16,notify_offset:2});this.virtio=new xb(a,{name:"virtio-net",pci_id:80,device_id:4161,subsystem_device_id:1,common:{initial_port:51200,queues:b,features:[5,16,22,3,17,23,32],on_driver_ok:()=>{}},notification:{initial_port:51456,single_handler:!1,handlers:[()=>{},
326
- d=>{const e=this.virtio.queues[d];for(;e.has_request();){const f=e.pop_request(),h=new Uint8Array(f.length_readable);f.get_next_blob(h);this.bus.send("net"+this.id+"-send",h.subarray(12));this.bus.send("eth-transmit-end",[h.length-12]);this.virtio.queues[d].push_reply(f)}this.virtio.queues[d].flush_replies()},d=>{if(d!==2*this.pairs)m(!1,"VirtioNet Notified for wrong queue: "+d+" (expected queue_id of 3)");else for(var e=this.virtio.queues[d];e.has_request();){const l=e.pop_request();var f=new Uint8Array(l.length_readable);
327
- l.get_next_blob(f);var h=O.Unmarshall(["b","b"],f,{offset:0});const n=h[0];h=h[1];switch(n<<8|h){case 1024:f=O.Unmarshall(["h"],f,{offset:2});m(1===f[0]);this.Send(d,l,new Uint8Array([0]));break;case 257:this.mac=f.subarray(2,8);this.Send(d,l,new Uint8Array([0]));this.bus.send("net"+this.id+"-mac",ob(this.mac));break;default:m(!1," VirtioNet received unknown command: "+n+":"+h);this.Send(d,l,new Uint8Array([1]));return}}}]},isr_status:{initial_port:50944},device_specific:{initial_port:50688,struct:[0,
328
- 1,2,3,4,5].map((d,e)=>({bytes:1,name:"mac_"+e,read:()=>this.mac[e],write:()=>{}})).concat([{bytes:2,name:"status",read:()=>this.status,write:()=>{}},{bytes:2,name:"max_pairs",read:()=>this.pairs,write:()=>{}},{bytes:2,name:"mtu",read:()=>1500,write:()=>{}}])}});this.bus.register("net"+this.id+"-receive",d=>{this.bus.send("eth-receive-end",[d.length]);const e=new Uint8Array(12+d.byteLength);(new DataView(e.buffer,e.byteOffset,e.byteLength)).setInt16(10,1);e.set(d,12);d=this.virtio.queues[0];d.has_request()?
329
- (d=d.pop_request(),d.set_next_blob(e),this.virtio.queues[0].push_reply(d),this.virtio.queues[0].flush_replies()):console.log("No buffer to write into!")},this)}Tb.prototype.get_state=function(){const a=[];a[0]=this.virtio;a[1]=this.id;a[2]=this.mac;return a};Tb.prototype.set_state=function(a){this.virtio.set_state(a[0]);this.id=a[1];this.preserve_mac_from_state_image&&(this.mac=a[2],this.bus.send("net"+this.id+"-mac",ob(this.mac)))};Tb.prototype.reset=function(){this.virtio.reset()};Tb.prototype.Send=
330
- function(a,b,c){b.set_next_blob(c);this.virtio.queues[a].push_reply(b);this.virtio.queues[a].flush_replies()};Tb.prototype.Ack=function(a,b){this.virtio.queues[a].push_reply(b);this.virtio.queues[a].flush_replies()};function Ub(){var a,b=0,c=0;this.put_char=function(d,e,f){m(0<=d&&d<c);m(0<=e&&e<b);a[d*b+e]=f};this.destroy=function(){};this.pause=function(){};this.continue=function(){};this.set_mode=function(){};this.set_font_bitmap=function(){};this.set_font_page=function(){};this.clear_screen=function(){};
331
- this.set_size_text=function(d,e){if(d!==b||e!==c)a=new Uint8Array(d*e),b=d,c=e};this.set_size_graphical=function(){};this.set_scale=function(){};this.update_cursor_scanline=function(){};this.update_cursor=function(){};this.update_buffer=function(){};this.get_text_screen=function(){for(var d=[],e=0;e<c;e++)d.push(this.get_text_row(e));return d};this.get_text_row=function(d){d*=b;return String.fromCharCode.apply(String,a.subarray(d,d+b))};this.set_size_text(80,25)}const Vb=Uint32Array.from([655360,
332
- 655360,720896,753664]),Wb=Uint32Array.from([131072,65536,32768,32768]);function W(a,b,c,d){this.cpu=a;this.bus=b;this.screen=c;this.vga_memory_size=d;this.cursor_address=0;this.cursor_scanline_start=14;this.cursor_scanline_end=15;this.max_cols=80;this.max_rows=25;this.virtual_height=this.virtual_width=this.screen_height=this.screen_width=0;this.layers=[];this.start_address_latched=this.start_address=0;this.crtc=new Uint8Array(25);this.line_compare=this.offset_register=this.preset_row_scan=this.underline_location_register=
333
- this.vertical_blank_start=this.vertical_display_enable_end=this.horizontal_blank_start=this.horizontal_display_enable_end=this.crtc_mode=0;this.graphical_mode=!1;this.vga256_palette=new Int32Array(256);this.latch_dword=0;this.svga_version=45253;this.svga_height=this.svga_width=0;this.svga_enabled=!1;this.svga_bpp=32;this.svga_offset_y=this.svga_offset_x=this.svga_offset=this.svga_bank_offset=0;this.vga_memory_size=void 0===this.vga_memory_size||262144>this.vga_memory_size?262144:268435456<this.vga_memory_size?
334
- 268435456:na(this.vga_memory_size);D("effective vga memory size: "+this.vga_memory_size,256);this.pci_space=[52,18,17,17,3,1,0,0,0,0,0,3,0,0,0,0,8,14680064,57344,224,0,0,0,0,0,0,191,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,0,17,0,0,190,254,0,0,0,0,0,0,0,0,0,0,0,0];this.pci_id=144;this.pci_bars=[{size:this.vga_memory_size}];this.pci_rom_size=65536;this.pci_rom_address=4272947200;this.name="vga";this.dac_state=this.dac_color_index_read=this.dac_color_index_write=this.index_crtc=0;this.dac_mask=255;
335
- this.dac_map=new Uint8Array(16);this.attribute_controller_index=-1;this.palette_source=32;this.color_select=this.horizontal_panning=this.color_plane_enable=this.attribute_mode=0;this.sequencer_index=-1;this.plane_write_bm=15;this.clocking_mode=this.sequencer_memory_mode=0;this.graphics_index=-1;this.planar_rotate_reg=this.planar_mode=this.plane_read=this.character_map_select=0;this.planar_bitmap=255;this.max_scan_line=this.color_dont_care=this.color_compare=this.miscellaneous_graphics_register=this.planar_setreset_enable=
336
- this.planar_setreset=0;this.port_3DA_value=this.miscellaneous_output_register=255;this.font_page_ab_enabled=!1;b=a.io;b.register_write(960,this,this.port3C0_write);b.register_read(960,this,this.port3C0_read,this.port3C0_read16);b.register_read(961,this,this.port3C1_read);b.register_write(962,this,this.port3C2_write);b.register_write_consecutive(964,this,this.port3C4_write,this.port3C5_write);b.register_read(964,this,this.port3C4_read);b.register_read(965,this,this.port3C5_read);b.register_write_consecutive(974,
337
- this,this.port3CE_write,this.port3CF_write);b.register_read(974,this,this.port3CE_read);b.register_read(975,this,this.port3CF_read);b.register_read(966,this,this.port3C6_read);b.register_write(966,this,this.port3C6_write);b.register_write(967,this,this.port3C7_write);b.register_read(967,this,this.port3C7_read);b.register_write(968,this,this.port3C8_write);b.register_read(968,this,this.port3C8_read);b.register_write(969,this,this.port3C9_write);b.register_read(969,this,this.port3C9_read);b.register_read(972,
338
- this,this.port3CC_read);b.register_write(980,this,this.port3D4_write,this.port3D4_write16);b.register_write(981,this,this.port3D5_write,this.port3D5_write16);b.register_read(980,this,this.port3D4_read);b.register_read(981,this,this.port3D5_read,this.port3D5_read16);b.register_write(948,this,this.port3D4_write,this.port3D4_write16);b.register_write(949,this,this.port3D5_write,this.port3D5_write16);b.register_read(948,this,this.port3D4_read);b.register_read(949,this,this.port3D5_read,this.port3D5_read16);
339
- b.register_read(970,this,function(){D("3CA read",256);return 0});b.register_read(986,this,this.port3DA_read);b.register_read(954,this,this.port3DA_read);this.dispi_index=-1;this.dispi_enable_value=0;b.register_write(462,this,void 0,this.port1CE_write);b.register_write(463,this,void 0,this.port1CF_write);b.register_read(463,this,void 0,this.port1CF_read);c=a.svga_allocate_memory(this.vga_memory_size)>>>0;this.svga_memory=k(Uint8Array,a.wasm_memory,c,this.vga_memory_size);this.diff_addr_min=this.vga_memory_size;
340
- this.diff_addr_max=0;this.diff_plot_min=this.vga_memory_size;this.diff_plot_max=0;this.image_data=null;this.vga_memory=new Uint8Array(262144);this.plane0=new Uint8Array(this.vga_memory.buffer,0,65536);this.plane1=new Uint8Array(this.vga_memory.buffer,65536,65536);this.plane2=new Uint8Array(this.vga_memory.buffer,131072,65536);this.plane3=new Uint8Array(this.vga_memory.buffer,196608,65536);this.pixel_buffer=new Uint8Array(524288);b.mmap_register(655360,131072,e=>this.vga_memory_read(e),(e,f)=>this.vga_memory_write(e,
341
- f));a.devices.pci.register_device(this)}W.prototype.get_state=function(){var a=[];a[0]=this.vga_memory_size;a[1]=this.cursor_address;a[2]=this.cursor_scanline_start;a[3]=this.cursor_scanline_end;a[4]=this.max_cols;a[5]=this.max_rows;a[6]=this.vga_memory;a[7]=this.dac_state;a[8]=this.start_address;a[9]=this.graphical_mode;a[10]=this.vga256_palette;a[11]=this.latch_dword;a[12]=this.color_compare;a[13]=this.color_dont_care;a[14]=this.miscellaneous_graphics_register;a[15]=this.svga_width;a[16]=this.svga_height;
342
- a[17]=this.crtc_mode;a[18]=this.svga_enabled;a[19]=this.svga_bpp;a[20]=this.svga_bank_offset;a[21]=this.svga_offset;a[22]=this.index_crtc;a[23]=this.dac_color_index_write;a[24]=this.dac_color_index_read;a[25]=this.dac_map;a[26]=this.sequencer_index;a[27]=this.plane_write_bm;a[28]=this.sequencer_memory_mode;a[29]=this.graphics_index;a[30]=this.plane_read;a[31]=this.planar_mode;a[32]=this.planar_rotate_reg;a[33]=this.planar_bitmap;a[34]=this.max_scan_line;a[35]=this.miscellaneous_output_register;a[36]=
343
- this.port_3DA_value;a[37]=this.dispi_index;a[38]=this.dispi_enable_value;a[39]=this.svga_memory;a[41]=this.attribute_controller_index;a[42]=this.offset_register;a[43]=this.planar_setreset;a[44]=this.planar_setreset_enable;a[45]=this.start_address_latched;a[46]=this.crtc;a[47]=this.horizontal_display_enable_end;a[48]=this.horizontal_blank_start;a[49]=this.vertical_display_enable_end;a[50]=this.vertical_blank_start;a[51]=this.underline_location_register;a[52]=this.preset_row_scan;a[53]=this.offset_register;
344
- a[54]=this.palette_source;a[55]=this.attribute_mode;a[56]=this.color_plane_enable;a[57]=this.horizontal_panning;a[58]=this.color_select;a[59]=this.clocking_mode;a[60]=this.line_compare;a[61]=this.pixel_buffer;a[62]=this.dac_mask;a[63]=this.character_map_select;a[64]=this.font_page_ab_enabled;return a};W.prototype.set_state=function(a){this.vga_memory_size=a[0];this.cursor_address=a[1];this.cursor_scanline_start=a[2];this.cursor_scanline_end=a[3];this.max_cols=a[4];this.max_rows=a[5];a[6]&&this.vga_memory.set(a[6]);
345
- this.dac_state=a[7];this.start_address=a[8];this.graphical_mode=a[9];this.vga256_palette=a[10];this.latch_dword=a[11];this.color_compare=a[12];this.color_dont_care=a[13];this.miscellaneous_graphics_register=a[14];this.svga_width=a[15];this.svga_height=a[16];this.crtc_mode=a[17];this.svga_enabled=a[18];this.svga_bpp=a[19];this.svga_bank_offset=a[20];this.svga_offset=a[21];this.index_crtc=a[22];this.dac_color_index_write=a[23];this.dac_color_index_read=a[24];this.dac_map=a[25];this.sequencer_index=
346
- a[26];this.plane_write_bm=a[27];this.sequencer_memory_mode=a[28];this.graphics_index=a[29];this.plane_read=a[30];this.planar_mode=a[31];this.planar_rotate_reg=a[32];this.planar_bitmap=a[33];this.max_scan_line=a[34];this.miscellaneous_output_register=a[35];this.port_3DA_value=a[36];this.dispi_index=a[37];this.dispi_enable_value=a[38];this.svga_memory.set(a[39]);this.attribute_controller_index=a[41];this.offset_register=a[42];this.planar_setreset=a[43];this.planar_setreset_enable=a[44];this.start_address_latched=
347
- a[45];this.crtc.set(a[46]);this.horizontal_display_enable_end=a[47];this.horizontal_blank_start=a[48];this.vertical_display_enable_end=a[49];this.vertical_blank_start=a[50];this.underline_location_register=a[51];this.preset_row_scan=a[52];this.offset_register=a[53];this.palette_source=a[54];this.attribute_mode=a[55];this.color_plane_enable=a[56];this.horizontal_panning=a[57];this.color_select=a[58];this.clocking_mode=a[59];this.line_compare=a[60];a[61]&&this.pixel_buffer.set(a[61]);this.dac_mask=
348
- void 0===a[62]?255:a[62];this.character_map_select=void 0===a[63]?0:a[63];this.font_page_ab_enabled=void 0===a[64]?0:a[64];this.screen.set_mode(this.graphical_mode);this.graphical_mode?(this.screen_height=this.screen_width=0,this.svga_enabled?(this.set_size_graphical(this.svga_width,this.svga_height,this.svga_width,this.svga_height,this.svga_bpp),this.update_layers()):(this.update_vga_size(),this.update_layers(),this.complete_replot())):(this.set_font_bitmap(!0),this.set_size_text(this.max_cols,this.max_rows),
349
- this.set_font_page(),this.update_cursor_scanline(),this.update_cursor());this.complete_redraw()};W.prototype.vga_memory_read=function(a){if(this.svga_enabled)return this.cpu.read8((a-655360|this.svga_bank_offset)+3758096384|0);var b=this.miscellaneous_graphics_register>>2&3;a-=Vb[b];if(0>a||a>=Wb[b])return D("vga read outside memory space: addr:"+z(a),256),0;this.latch_dword=this.plane0[a];this.latch_dword|=this.plane1[a]<<8;this.latch_dword|=this.plane2[a]<<16;this.latch_dword|=this.plane3[a]<<24;
350
- if(this.planar_mode&8)return b=255,this.color_dont_care&1&&(b&=this.plane0[a]^~(this.color_compare&1?255:0)),this.color_dont_care&2&&(b&=this.plane1[a]^~(this.color_compare&2?255:0)),this.color_dont_care&4&&(b&=this.plane2[a]^~(this.color_compare&4?255:0)),this.color_dont_care&8&&(b&=this.plane3[a]^~(this.color_compare&8?255:0)),b;b=this.plane_read;this.graphical_mode?this.sequencer_memory_mode&8?(b=a&3,a&=-4):this.planar_mode&16&&(b=a&1,a&=-2):b&=3;return this.vga_memory[b<<16|a]};W.prototype.vga_memory_write=
351
- function(a,b){if(this.svga_enabled)this.cpu.write8((a-655360|this.svga_bank_offset)+3758096384|0,b);else{var c=this.miscellaneous_graphics_register>>2&3;a-=Vb[c];0>a||a>=Wb[c]?D("vga write outside memory space: addr:"+z(a)+", value:"+z(b),256):this.graphical_mode?this.vga_memory_write_graphical(a,b):this.plane_write_bm&3?this.vga_memory_write_text_mode(a,b):this.plane_write_bm&4&&(this.plane2[a]=b)}};W.prototype.vga_memory_write_graphical=function(a,b){var c=this.planar_mode&3,d=this.apply_feed(this.planar_bitmap),
352
- e=this.apply_expand(this.planar_setreset),f=this.apply_expand(this.planar_setreset_enable);switch(c){case 0:b=this.apply_rotate(b);var h=this.apply_feed(b);h=this.apply_setreset(h,f);h=this.apply_logical(h,this.latch_dword);h=this.apply_bitmask(h,d);break;case 1:h=this.latch_dword;break;case 2:h=this.apply_expand(b);h=this.apply_logical(h,this.latch_dword);h=this.apply_bitmask(h,d);break;case 3:b=this.apply_rotate(b),d&=this.apply_feed(b),h=this.apply_bitmask(e,d)}b=15;switch(this.sequencer_memory_mode&
353
- 12){case 0:b=5<<(a&1);a&=-2;break;case 8:case 12:b=1<<(a&3),a&=-4}b&=this.plane_write_bm;b&1&&(this.plane0[a]=h>>0&255);b&2&&(this.plane1[a]=h>>8&255);b&4&&(this.plane2[a]=h>>16&255);b&8&&(this.plane3[a]=h>>24&255);a=this.vga_addr_to_pixel(a);this.partial_replot(a,a+7)};W.prototype.apply_feed=function(a){return a|a<<8|a<<16|a<<24};W.prototype.apply_expand=function(a){return(a&1?255:0)|(a&2?255:0)<<8|(a&4?255:0)<<16|(a&8?255:0)<<24};W.prototype.apply_rotate=function(a){return(a|a<<8)>>>(this.planar_rotate_reg&
354
- 7)&255};W.prototype.apply_setreset=function(a,b){var c=this.apply_expand(this.planar_setreset);return(a|b&c)&(~b|c)};W.prototype.apply_logical=function(a,b){switch(this.planar_rotate_reg&24){case 8:return a&b;case 16:return a|b;case 24:return a^b}return a};W.prototype.apply_bitmask=function(a,b){return b&a|~b&this.latch_dword};W.prototype.text_mode_redraw=function(){const a=this.scan_line_to_screen_row(this.line_compare),b=Math.max(0,2*(2*this.offset_register-this.max_cols)),c=this.attribute_mode&
355
- 8,d=this.font_page_ab_enabled?7:15,e=c?7:15,f=this.screen.FLAG_BLINKING,h=this.screen.FLAG_FONT_PAGE_B;let l=this.start_address<<1;for(let n=0;n<this.max_rows;n++){n===a&&(l=0);for(let p=0;p<this.max_cols;p++){const q=this.vga_memory[l],r=this.vga_memory[l|1],t=(c&&r&128?f:0)|(!this.font_page_ab_enabled||r&8?0:h);this.bus.send("screen-put-char",[n,p,q]);this.screen.put_char(n,p,q,t,this.vga256_palette[this.dac_mask&this.dac_map[r>>4&e]],this.vga256_palette[this.dac_mask&this.dac_map[r&d]]);l+=2}l+=
356
- b}};W.prototype.vga_memory_write_text_mode=function(a,b){this.vga_memory[a]=b;var c=Math.max(this.max_cols,2*this.offset_register);let d;if(a>>1>=this.start_address){var e=(a>>1)-this.start_address;d=e/c|0;c=e%c}else e=a>>1,d=(e/c|0)+this.scan_line_to_screen_row(this.line_compare),c=e%c;m(0<=d&&0<=c);if(!(c>=this.max_cols||d>=this.max_rows)){a&1?(e=b,b=this.vga_memory[a&-2]):e=this.vga_memory[a|1];var f=this.attribute_mode&8;a=(f&&e&128?this.screen.FLAG_BLINKING:0)|(!this.font_page_ab_enabled||e&
357
- 8?0:this.screen.FLAG_FONT_PAGE_B);var h=this.font_page_ab_enabled?7:15;f=f?7:15;this.bus.send("screen-put-char",[d,c,b]);this.screen.put_char(d,c,b,a,this.vga256_palette[this.dac_mask&this.dac_map[e>>4&f]],this.vga256_palette[this.dac_mask&this.dac_map[e&h]])}};W.prototype.update_cursor=function(){var a=Math.max(this.max_cols,2*this.offset_register);let b;this.cursor_address>=this.start_address?(b=(this.cursor_address-this.start_address)/a|0,a=(this.cursor_address-this.start_address)%a):(b=(this.cursor_address/
358
- a|0)+this.scan_line_to_screen_row(this.line_compare),a=this.cursor_address%a);m(0<=b&&0<=a);this.screen.update_cursor(b,a)};W.prototype.complete_redraw=function(){D("complete redraw",256);this.graphical_mode?this.svga_enabled?this.cpu.svga_mark_dirty():(this.diff_addr_min=0,this.diff_addr_max=524288):this.text_mode_redraw()};W.prototype.complete_replot=function(){D("complete replot",256);this.graphical_mode&&!this.svga_enabled&&(this.diff_plot_min=0,this.diff_plot_max=524288,this.complete_redraw())};
359
- W.prototype.partial_redraw=function(a,b){a<this.diff_addr_min&&(this.diff_addr_min=a);b>this.diff_addr_max&&(this.diff_addr_max=b)};W.prototype.partial_replot=function(a,b){a<this.diff_plot_min&&(this.diff_plot_min=a);b>this.diff_plot_max&&(this.diff_plot_max=b);this.partial_redraw(a,b)};W.prototype.reset_diffs=function(){this.diff_addr_min=this.vga_memory_size;this.diff_addr_max=0;this.diff_plot_min=this.vga_memory_size;this.diff_plot_max=0};W.prototype.destroy=function(){};W.prototype.vga_bytes_per_line=
360
- function(){var a=this.offset_register<<2;this.underline_location_register&64?a<<=1:this.crtc_mode&64&&(a>>>=1);return a};W.prototype.vga_addr_shift_count=function(){var a=128+(~this.underline_location_register&this.crtc_mode&64);a-=this.underline_location_register&64;a-=this.attribute_mode&64;return a>>>6};W.prototype.vga_addr_to_pixel=function(a){var b=this.vga_addr_shift_count();if(~this.crtc_mode&3){var c=a-this.start_address;c&=this.crtc_mode<<13|-24577;c<<=b;var d=c/this.virtual_width|0;c%=this.virtual_width;
361
- switch(this.crtc_mode&3){case 2:d=d<<1|a>>13&1;break;case 1:d=d<<1|a>>14&1;break;case 0:d=d<<2|a>>13&3}return d*this.virtual_width+c+(this.start_address<<b)}return a<<b};W.prototype.scan_line_to_screen_row=function(a){this.max_scan_line&128&&(a>>>=1);a=Math.ceil(a/(1+(this.max_scan_line&31)));this.crtc_mode&1||(a<<=1);this.crtc_mode&2||(a<<=1);return a};W.prototype.set_size_text=function(a,b){m(!this.graphical_mode);this.max_cols=a;this.max_rows=b;this.screen.set_size_text(a,b);this.bus.send("screen-set-size",
362
- [a,b,0])};W.prototype.set_size_graphical=function(a,b,c,d,e){m(this.graphical_mode);c=Math.max(c,1);d=Math.max(d,1);if(this.screen_width!==a||this.screen_height!==b||this.virtual_width!==c||this.virtual_height!==d){this.screen_width=a;this.screen_height=b;this.virtual_width=c;this.virtual_height=d;if("undefined"!==typeof ImageData){const f=c*d,h=this.cpu.svga_allocate_dest_buffer(f)>>>0;this.dest_buffet_offset=h;this.image_data=new ImageData(new Uint8ClampedArray(this.cpu.wasm_memory.buffer,h,4*f),
363
- c,d);this.cpu.svga_mark_dirty()}this.screen.set_size_graphical(a,b,c,d);this.bus.send("screen-set-size",[a,b,e])}};W.prototype.update_vga_size=function(){if(!this.svga_enabled){var a=Math.min(1+this.horizontal_display_enable_end,this.horizontal_blank_start),b=Math.min(1+this.vertical_display_enable_end,this.vertical_blank_start);if(a&&b)if(this.graphical_mode){a<<=3;var c=this.offset_register<<4;this.attribute_mode&64&&(a>>>=1,c>>>=1);b=this.scan_line_to_screen_row(b);var d=Wb[0];const e=this.vga_bytes_per_line();
364
- this.set_size_graphical(a,b,c,e?Math.ceil(d/e):b,8);this.update_vertical_retrace();this.update_layers()}else this.max_scan_line&128&&(b>>>=1),c=b/(1+(this.max_scan_line&31))|0,a&&c&&this.set_size_text(a,c)}};W.prototype.update_layers=function(){this.graphical_mode||this.text_mode_redraw();if(this.svga_enabled)this.layers=[];else if(this.virtual_width&&this.screen_width)if(!this.palette_source||this.clocking_mode&32)this.layers=[],this.screen.clear_screen();else{var a=this.start_address_latched,b=
365
- this.horizontal_panning;this.attribute_mode&64&&(b>>>=1);var c=this.preset_row_scan>>5&3,d=this.vga_addr_to_pixel(a+c);a=d/this.virtual_width|0;var e=d%this.virtual_width+b;d=this.scan_line_to_screen_row(1+this.line_compare);d=Math.min(d,this.screen_height);var f=this.screen_height-d;this.layers=[];e=-e;for(var h=0;e<this.screen_width;e+=this.virtual_width,h++)this.layers.push({image_data:this.image_data,screen_x:e,screen_y:0,buffer_x:0,buffer_y:a+h,buffer_width:this.virtual_width,buffer_height:d});
366
- a=0;this.attribute_mode&32||(a=this.vga_addr_to_pixel(c)+b);e=-a;for(h=0;e<this.screen_width;e+=this.virtual_width,h++)this.layers.push({image_data:this.image_data,screen_x:e,screen_y:d,buffer_x:0,buffer_y:h,buffer_width:this.virtual_width,buffer_height:f})}};W.prototype.update_vertical_retrace=function(){this.port_3DA_value|=8;this.start_address_latched!==this.start_address&&(this.start_address_latched=this.start_address,this.update_layers())};W.prototype.update_cursor_scanline=function(){var a=
367
- this.max_scan_line&31;const b=Math.min(a,this.cursor_scanline_start&31);a=Math.min(a,this.cursor_scanline_end&31);this.screen.update_cursor_scanline(b,a,!(this.cursor_scanline_start&32)&&b<a)};W.prototype.port3C0_write=function(a){if(-1===this.attribute_controller_index)D("attribute controller index register: "+z(a),256),this.attribute_controller_index=a&31,D("attribute actual index: "+z(this.attribute_controller_index),256),this.palette_source!==(a&32)&&(this.palette_source=a&32,this.update_layers());
368
- else{if(16>this.attribute_controller_index)D("internal palette: "+z(this.attribute_controller_index)+" -> "+z(a),256),this.dac_map[this.attribute_controller_index]=a,this.attribute_mode&64||this.complete_redraw();else switch(this.attribute_controller_index){case 16:D("3C0 / attribute mode control: "+z(a),256);if(this.attribute_mode!==a){var b=this.attribute_mode;this.attribute_mode=a;const c=0!==(a&1);this.svga_enabled||this.graphical_mode===c||(this.graphical_mode=c,this.screen.set_mode(this.graphical_mode));
369
- (b^a)&64&&this.complete_replot();this.update_vga_size();this.complete_redraw();this.set_font_bitmap(!1)}break;case 18:D("3C0 / color plane enable: "+z(a),256);this.color_plane_enable!==a&&(this.color_plane_enable=a,this.complete_redraw());break;case 19:D("3C0 / horizontal panning: "+z(a),256);this.horizontal_panning!==a&&(this.horizontal_panning=a&15,this.update_layers());break;case 20:D("3C0 / color select: "+z(a),256);this.color_select!==a&&(this.color_select=a,this.complete_redraw());break;default:D("3C0 / attribute controller write "+
370
- z(this.attribute_controller_index)+": "+z(a),256)}this.attribute_controller_index=-1}};W.prototype.port3C0_read=function(){D("3C0 read",256);return(this.attribute_controller_index|this.palette_source)&255};W.prototype.port3C0_read16=function(){D("3C0 read16",256);return this.port3C0_read()|this.port3C1_read()<<8&65280};W.prototype.port3C1_read=function(){if(16>this.attribute_controller_index)return D("3C1 / internal palette read: "+z(this.attribute_controller_index)+" -> "+z(this.dac_map[this.attribute_controller_index]),
371
- 256),this.dac_map[this.attribute_controller_index]&255;switch(this.attribute_controller_index){case 16:return D("3C1 / attribute mode read: "+z(this.attribute_mode),256),this.attribute_mode;case 18:return D("3C1 / color plane enable read: "+z(this.color_plane_enable),256),this.color_plane_enable;case 19:return D("3C1 / horizontal panning read: "+z(this.horizontal_panning),256),this.horizontal_panning;case 20:return D("3C1 / color select read: "+z(this.color_select),256),this.color_select;default:D("3C1 / attribute controller read "+
372
- z(this.attribute_controller_index),256)}return 255};W.prototype.port3C2_write=function(a){D("3C2 / miscellaneous output register = "+z(a),256);this.miscellaneous_output_register=a};W.prototype.port3C4_write=function(a){this.sequencer_index=a};W.prototype.port3C4_read=function(){return this.sequencer_index};W.prototype.port3C5_write=function(a){switch(this.sequencer_index){case 1:D("clocking mode: "+z(a),256);var b=this.clocking_mode;this.clocking_mode=a;(b^a)&32&&this.update_layers();this.set_font_bitmap(!1);
373
- break;case 2:D("plane write mask: "+z(a),256);b=this.plane_write_bm;this.plane_write_bm=a;this.graphical_mode||!(b&4)||this.plane_write_bm&4||this.set_font_bitmap(!0);break;case 3:D("character map select: "+z(a),256);b=this.character_map_select;this.character_map_select=a;this.graphical_mode||b===a||this.set_font_page();break;case 4:D("sequencer memory mode: "+z(a),256);this.sequencer_memory_mode=a;break;default:D("3C5 / sequencer write "+z(this.sequencer_index)+": "+z(a),256)}};W.prototype.port3C5_read=
374
- function(){D("3C5 / sequencer read "+z(this.sequencer_index),256);switch(this.sequencer_index){case 1:return this.clocking_mode;case 2:return this.plane_write_bm;case 3:return this.character_map_select;case 4:return this.sequencer_memory_mode;case 6:return 18}return 0};W.prototype.port3C6_write=function(a){this.dac_mask!==a&&(this.dac_mask=a,this.complete_redraw())};W.prototype.port3C6_read=function(){return this.dac_mask};W.prototype.port3C7_write=function(a){D("3C7 write: "+z(a),256);this.dac_color_index_read=
375
- 3*a;this.dac_state&=0};W.prototype.port3C7_read=function(){return this.dac_state};W.prototype.port3C8_write=function(a){this.dac_color_index_write=3*a;this.dac_state|=3};W.prototype.port3C8_read=function(){return this.dac_color_index_write/3&255};W.prototype.port3C9_write=function(a){var b=this.dac_color_index_write/3|0,c=this.dac_color_index_write%3,d=this.vga256_palette[b];if(0===(this.dispi_enable_value&32)){a&=63;const e=a&1;a=a<<2|e<<1|e}0===c?d=d&-16711681|a<<16:1===c?d=d&-65281|a<<8:(d=d&-256|
376
- a,D("dac set color, index="+z(b)+" value="+z(d),256));this.vga256_palette[b]!==d&&(this.vga256_palette[b]=d,this.complete_redraw());this.dac_color_index_write++};W.prototype.port3C9_read=function(){D("3C9 read",256);var a=this.vga256_palette[this.dac_color_index_read/3|0]>>8*(2-this.dac_color_index_read%3)&255;this.dac_color_index_read++;return this.dispi_enable_value&32?a:a>>2};W.prototype.port3CC_read=function(){D("3CC read",256);return this.miscellaneous_output_register};W.prototype.port3CE_write=
377
- function(a){this.graphics_index=a};W.prototype.port3CE_read=function(){return this.graphics_index};W.prototype.port3CF_write=function(a){switch(this.graphics_index){case 0:this.planar_setreset=a;D("plane set/reset: "+z(a),256);break;case 1:this.planar_setreset_enable=a;D("plane set/reset enable: "+z(a),256);break;case 2:this.color_compare=a;D("color compare: "+z(a),256);break;case 3:this.planar_rotate_reg=a;D("plane rotate: "+z(a),256);break;case 4:this.plane_read=a;D("plane read: "+z(a),256);break;
378
- case 5:var b=this.planar_mode;this.planar_mode=a;D("planar mode: "+z(a),256);(b^a)&96&&this.complete_replot();break;case 6:D("miscellaneous graphics register: "+z(a),256);this.miscellaneous_graphics_register!==a&&(this.miscellaneous_graphics_register=a,this.update_vga_size());break;case 7:this.color_dont_care=a;D("color don't care: "+z(a),256);break;case 8:this.planar_bitmap=a;D("planar bitmap: "+z(a),256);break;default:D("3CF / graphics write "+z(this.graphics_index)+": "+z(a),256)}};W.prototype.port3CF_read=
379
- function(){D("3CF / graphics read "+z(this.graphics_index),256);switch(this.graphics_index){case 0:return this.planar_setreset;case 1:return this.planar_setreset_enable;case 2:return this.color_compare;case 3:return this.planar_rotate_reg;case 4:return this.plane_read;case 5:return this.planar_mode;case 6:return this.miscellaneous_graphics_register;case 7:return this.color_dont_care;case 8:return this.planar_bitmap}return 0};W.prototype.port3D4_write=function(a){D("3D4 / crtc index: "+a,256);this.index_crtc=
380
- a};W.prototype.port3D4_write16=function(a){this.port3D4_write(a&255);this.port3D5_write(a>>8&255)};W.prototype.port3D4_read=function(){D("3D4 read / crtc index: "+this.index_crtc,256);return this.index_crtc};W.prototype.port3D5_write=function(a){switch(this.index_crtc){case 1:D("3D5 / hdisp enable end write: "+z(a),256);this.horizontal_display_enable_end!==a&&(this.horizontal_display_enable_end=a,this.update_vga_size());break;case 2:this.horizontal_blank_start!==a&&(this.horizontal_blank_start=a,
381
- this.update_vga_size());break;case 7:D("3D5 / overflow register write: "+z(a),256);var b=this.vertical_display_enable_end;this.vertical_display_enable_end&=255;this.vertical_display_enable_end=this.vertical_display_enable_end|a<<3&512|a<<7&256;b!==this.vertical_display_enable_end&&this.update_vga_size();this.line_compare=this.line_compare&767|a<<4&256;b=this.vertical_blank_start;this.vertical_blank_start=this.vertical_blank_start&767|a<<5&256;b!==this.vertical_blank_start&&this.update_vga_size();
382
- this.update_layers();break;case 8:D("3D5 / preset row scan write: "+z(a),256);this.preset_row_scan=a;this.update_layers();break;case 9:D("3D5 / max scan line write: "+z(a),256);var c=this.max_scan_line;this.max_scan_line=a;this.line_compare=this.line_compare&511|a<<3&512;b=this.vertical_blank_start;this.vertical_blank_start=this.vertical_blank_start&511|a<<4&512;((c^this.max_scan_line)&159||b!==this.vertical_blank_start)&&this.update_vga_size();this.update_cursor_scanline();this.update_layers();this.set_font_bitmap(!1);
383
- break;case 10:D("3D5 / cursor scanline start write: "+z(a),256);this.cursor_scanline_start=a;this.update_cursor_scanline();break;case 11:D("3D5 / cursor scanline end write: "+z(a),256);this.cursor_scanline_end=a;this.update_cursor_scanline();break;case 12:(this.start_address>>8&255)!==a&&(this.start_address=this.start_address&255|a<<8,this.update_layers(),~this.crtc_mode&3&&this.complete_replot());D("3D5 / start addr hi write: "+z(a)+" -> "+z(this.start_address,4),256);break;case 13:(this.start_address&
384
- 255)!==a&&(this.start_address=this.start_address&65280|a,this.update_layers(),~this.crtc_mode&3&&this.complete_replot());D("3D5 / start addr lo write: "+z(a)+" -> "+z(this.start_address,4),256);break;case 14:D("3D5 / cursor address hi write: "+z(a),256);this.cursor_address=this.cursor_address&255|a<<8;this.update_cursor();break;case 15:D("3D5 / cursor address lo write: "+z(a),256);this.cursor_address=this.cursor_address&65280|a;this.update_cursor();break;case 18:D("3D5 / vdisp enable end write: "+
385
- z(a),256);(this.vertical_display_enable_end&255)!==a&&(this.vertical_display_enable_end=this.vertical_display_enable_end&768|a,this.update_vga_size());break;case 19:D("3D5 / offset register write: "+z(a),256);this.offset_register!==a&&(this.offset_register=a,this.update_vga_size(),~this.crtc_mode&3&&this.complete_replot());break;case 20:D("3D5 / underline location write: "+z(a),256);this.underline_location_register!==a&&(b=this.underline_location_register,this.underline_location_register=a,this.update_vga_size(),
386
- (b^a)&64&&this.complete_replot());break;case 21:D("3D5 / vertical blank start write: "+z(a),256);(this.vertical_blank_start&255)!==a&&(this.vertical_blank_start=this.vertical_blank_start&768|a,this.update_vga_size());break;case 23:D("3D5 / crtc mode write: "+z(a),256);this.crtc_mode!==a&&(b=this.crtc_mode,this.crtc_mode=a,this.update_vga_size(),(b^a)&67&&this.complete_replot());break;case 24:D("3D5 / line compare write: "+z(a),256);this.line_compare=this.line_compare&768|a;this.update_layers();break;
387
- default:this.index_crtc<this.crtc.length&&(this.crtc[this.index_crtc]=a),D("3D5 / CRTC write "+z(this.index_crtc)+": "+z(a),256)}};W.prototype.port3D5_write16=function(a){D("16-bit write to 3D5: "+z(a,4),256);this.port3D5_write(a&255)};W.prototype.port3D5_read=function(){D("3D5 read "+z(this.index_crtc),256);switch(this.index_crtc){case 1:return this.horizontal_display_enable_end;case 2:return this.horizontal_blank_start;case 7:return this.vertical_display_enable_end>>7&2|this.vertical_blank_start>>
388
- 5&8|this.line_compare>>4&16|this.vertical_display_enable_end>>3&64;case 8:return this.preset_row_scan;case 9:return this.max_scan_line;case 10:return this.cursor_scanline_start;case 11:return this.cursor_scanline_end;case 12:return this.start_address&255;case 13:return this.start_address>>8;case 14:return this.cursor_address>>8;case 15:return this.cursor_address&255;case 18:return this.vertical_display_enable_end&255;case 19:return this.offset_register;case 20:return this.underline_location_register;
389
- case 21:return this.vertical_blank_start&255;case 23:return this.crtc_mode;case 24:return this.line_compare&255}return this.index_crtc<this.crtc.length?this.crtc[this.index_crtc]:0};W.prototype.port3D5_read16=function(){D("Warning: 16-bit read from 3D5",256);return this.port3D5_read()};W.prototype.port3DA_read=function(){D("3DA read - status 1 and clear attr index",256);var a=this.port_3DA_value;this.graphical_mode?(this.port_3DA_value^=1,this.port_3DA_value&=1):(this.port_3DA_value&1&&(this.port_3DA_value^=
390
- 8),this.port_3DA_value^=1);this.attribute_controller_index=-1;return a};W.prototype.port1CE_write=function(a){this.dispi_index=a};W.prototype.port1CF_write=function(a){D("1CF / dispi write "+z(this.dispi_index)+": "+z(a),256);const b=this.svga_enabled;switch(this.dispi_index){case 0:45248<=a&&45253>=a?this.svga_version=a:D("Invalid version value: "+z(a),256);break;case 1:this.svga_width=a;2560<this.svga_width&&(D("svga_width reduced from "+this.svga_width+" to 2560",256),this.svga_width=2560);break;
391
- case 2:this.svga_height=a;1600<this.svga_height&&(D("svga_height reduced from "+this.svga_height+" to 1600",256),this.svga_height=1600);break;case 3:this.svga_bpp=a;break;case 4:(this.svga_enabled=1===(a&1))&&0===(a&128)&&this.svga_memory.fill(0);this.dispi_enable_value=a;break;case 5:D("SVGA bank offset: "+z(a<<16),256);this.svga_bank_offset=a<<16;break;case 8:D("SVGA X offset: "+z(a),256);this.svga_offset_x!==a&&(this.svga_offset_x=a,this.svga_offset=this.svga_offset_y*this.svga_width+this.svga_offset_x,
392
- this.complete_redraw());break;case 9:D("SVGA Y offset: "+z(a*this.svga_width)+" y="+z(a),256);this.svga_offset_y!==a&&(this.svga_offset_y=a,this.svga_offset=this.svga_offset_y*this.svga_width+this.svga_offset_x,this.complete_redraw());break;default:D("Unimplemented dispi write index: "+z(this.dispi_index),256)}!this.svga_enabled||this.svga_width&&this.svga_height||(D("SVGA: disabled because of invalid width/height: "+this.svga_width+"x"+this.svga_height,256),this.svga_enabled=!1);m(4!==this.svga_bpp,
393
- "unimplemented svga bpp: 4");m(4===this.svga_bpp||8===this.svga_bpp||15===this.svga_bpp||16===this.svga_bpp||24===this.svga_bpp||32===this.svga_bpp,"unexpected svga bpp: "+this.svga_bpp);this.svga_enabled?D("SVGA: enabled, "+this.svga_width+"x"+this.svga_height+"x"+this.svga_bpp,256):D("SVGA: disabled");this.svga_enabled&&!b&&(this.svga_offset_y=this.svga_offset_x=this.svga_offset=0,this.graphical_mode=!0,this.screen.set_mode(this.graphical_mode),this.set_size_graphical(this.svga_width,this.svga_height,
394
- this.svga_width,this.svga_height,this.svga_bpp));this.svga_enabled||(this.svga_bank_offset=0);this.update_layers()};W.prototype.port1CF_read=function(){D("1CF / dispi read "+z(this.dispi_index),256);return this.svga_register_read(this.dispi_index)};W.prototype.svga_register_read=function(a){switch(a){case 0:return this.svga_version;case 1:return this.dispi_enable_value&2?2560:this.svga_width;case 2:return this.dispi_enable_value&2?1600:this.svga_height;case 3:return this.dispi_enable_value&2?32:this.svga_bpp;
395
- case 4:return this.dispi_enable_value;case 5:return this.svga_bank_offset>>>16;case 6:return this.screen_width?this.screen_width:1;case 8:return this.svga_offset_x;case 9:return this.svga_offset_y;case 10:return this.vga_memory_size/65536|0;default:D("Unimplemented dispi read index: "+z(this.dispi_index),256)}return 255};W.prototype.vga_replot=function(){for(var a=this.diff_plot_min&-16,b=Math.min(this.diff_plot_max|15,524287),c=this.vga_addr_shift_count(),d=~this.crtc_mode&3,e=this.planar_mode&96,
396
- f=this.attribute_mode&64;a<=b;){var h=a>>>c;if(d){var l=a/this.virtual_width|0,n=a-this.virtual_width*l;switch(d){case 1:h=(l&1)<<13;l>>>=1;break;case 2:h=(l&1)<<14;l>>>=1;break;case 3:h=(l&3)<<13,l>>>=2}h|=(l*this.virtual_width+n>>>c)+this.start_address}l=this.plane0[h];n=this.plane1[h];var p=this.plane2[h],q=this.plane3[h];h=new Uint8Array(8);switch(e){case 0:l<<=0;n<<=1;p<<=2;q<<=3;for(var r=7;0<=r;r--)h[7-r]=l>>r&1|n>>r&2|p>>r&4|q>>r&8;break;case 32:h[0]=l>>6&3|p>>4&12;h[1]=l>>4&3|p>>2&12;h[2]=
397
- l>>2&3|p>>0&12;h[3]=l>>0&3|p<<2&12;h[4]=n>>6&3|q>>4&12;h[5]=n>>4&3|q>>2&12;h[6]=n>>2&3|q>>0&12;h[7]=n>>0&3|q<<2&12;break;case 64:case 96:h[0]=l>>4&15,h[1]=l>>0&15,h[2]=n>>4&15,h[3]=n>>0&15,h[4]=p>>4&15,h[5]=p>>0&15,h[6]=q>>4&15,h[7]=q>>0&15}if(f)for(l=r=0;4>r;r++,a++,l+=2)this.pixel_buffer[a]=h[l]<<4|h[l+1];else for(r=0;8>r;r++,a++)this.pixel_buffer[a]=h[r]}};W.prototype.vga_redraw=function(){var a=this.diff_addr_min,b=Math.min(this.diff_addr_max,524287);const c=new Int32Array(this.cpu.wasm_memory.buffer,
398
- this.dest_buffet_offset,this.virtual_width*this.virtual_height);var d=255,e=0;this.attribute_mode&128&&(d&=207,e|=this.color_select<<4&48);if(this.attribute_mode&64)for(;a<=b;a++){var f=this.pixel_buffer[a]&d|e;f=this.vga256_palette[f];c[a]=f&65280|f<<16|f>>16|4278190080}else for(d&=63,e|=this.color_select<<4&192;a<=b;a++)f=this.dac_map[this.pixel_buffer[a]&this.color_plane_enable]&d|e,f=this.vga256_palette[f],c[a]=f&65280|f<<16|f>>16|4278190080};W.prototype.screen_fill_buffer=function(){if(this.graphical_mode){if(0===
399
- this.image_data.data.byteLength){var a=new Uint8ClampedArray(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,4*this.virtual_width*this.virtual_height);this.image_data=new ImageData(a,this.virtual_width,this.virtual_height);this.update_layers()}if(this.svga_enabled){a=0;let d=this.svga_height;if(8===this.svga_bpp){const e=new Int32Array(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,this.screen_width*this.screen_height),f=new Uint8Array(this.cpu.wasm_memory.buffer,this.svga_memory.byteOffset,
400
- this.vga_memory_size);for(var b=0;b<e.length;b++){var c=this.vga256_palette[f[b]];e[b]=c&65280|c<<16|c>>16|4278190080}}else this.cpu.svga_fill_pixel_buffer(this.svga_bpp,this.svga_offset),b=15===this.svga_bpp?2:this.svga_bpp/8,a=((this.cpu.svga_dirty_bitmap_min_offset[0]/b|0)-this.svga_offset)/this.svga_width|0,d=(((this.cpu.svga_dirty_bitmap_max_offset[0]/b|0)-this.svga_offset)/this.svga_width|0)+1;a<d&&(a=Math.max(a,0),d=Math.min(d,this.svga_height),this.screen.update_buffer([{image_data:this.image_data,
401
- screen_x:0,screen_y:a,buffer_x:0,buffer_y:a,buffer_width:this.svga_width,buffer_height:d-a}]))}else this.vga_replot(),this.vga_redraw(),this.screen.update_buffer(this.layers);this.reset_diffs()}this.update_vertical_retrace()};W.prototype.set_font_bitmap=function(a){const b=this.max_scan_line&31;if(b&&!this.graphical_mode){const c=!!(this.clocking_mode&8);this.screen.set_font_bitmap(b+1,!c&&!(this.clocking_mode&1),c,!!(this.attribute_mode&4),this.plane2,a)}};W.prototype.set_font_page=function(){const a=
402
- [0,2,4,6,1,3,5,7],b=(this.character_map_select&12)>>2|(this.character_map_select&32)>>3,c=this.character_map_select&3|(this.character_map_select&16)>>2;this.font_page_ab_enabled=b!==c;this.screen.set_font_page(a[b],a[c]);this.complete_redraw()};const Xb="SWAP_IN SWAP_OUT MAJFLT MINFLT MEMFREE MEMTOT AVAIL CACHES HTLB_PGALLOC HTLB_PGFAIL".split(" ");function Yb(a,b){this.bus=b;this.zeroed=this.fp_cmd=this.actual=this.num_pages=0;this.virtio=new xb(a,{name:"virtio-balloon",pci_id:88,device_id:4165,
403
- subsystem_device_id:5,common:{initial_port:55296,queues:[{size_supported:32,notify_offset:0},{size_supported:32,notify_offset:0},{size_supported:2,notify_offset:1},{size_supported:64,notify_offset:2}],features:[1,3,32],on_driver_ok:()=>{D("Balloon setup",2048)}},notification:{initial_port:55552,single_handler:!1,handlers:[c=>{const d=this.virtio.queues[c];for(;d.has_request();){var e=d.pop_request();const f=new Uint8Array(e.length_readable);e.get_next_blob(f);this.virtio.queues[c].push_reply(e);e=
404
- f.byteLength/4;this.actual+=0===c?e:-e}this.virtio.queues[c].flush_replies()},c=>{var d=this.virtio.queues[c];if(d.has_request()){d=d.pop_request();const e=new Uint8Array(d.length_readable);d.get_next_blob(e);let f={};for(let h=0;h<d.length_readable;h+=10){let [l,n]=O.Unmarshall(["h","d"],e,{offset:h});f[Xb[l]]=n}this.virtio.queues[c].push_reply(d);this.stats_cb&&this.stats_cb(f)}},c=>{const d=this.virtio.queues[c];for(;d.has_request();){const f=d.pop_request();if(0<f.length_readable){var e=new Uint8Array(f.length_readable);
405
- f.get_next_blob(e);[e]=O.Unmarshall(["w"],e,{offset:0});0===e&&(this.free_cb&&this.free_cb(this.zeroed),1<this.fp_cmd&&(this.fp_cmd=1),this.virtio.notify_config_changes())}if(0<f.length_writable)for(new Uint8Array(0),e=0;e<f.write_buffers.length;++e){let h=f.write_buffers[e];this.zeroed+=h.len;this.virtio.cpu.zero_memory(h.addr_low,h.len)}this.virtio.queues[c].push_reply(f)}this.virtio.queues[c].flush_replies()}]},isr_status:{initial_port:55040},device_specific:{initial_port:54784,struct:[{bytes:4,
406
- name:"num_pages",read:()=>this.num_pages,write:()=>{}},{bytes:4,name:"actual",read:()=>this.actual,write:()=>{}},{bytes:4,name:"free_page_hint_cmd_id",read:()=>this.fp_cmd,write:()=>{}}]}})}Yb.prototype.Inflate=function(a){this.num_pages+=a;this.virtio.notify_config_changes()};Yb.prototype.Deflate=function(a){this.num_pages-=a;this.virtio.notify_config_changes()};Yb.prototype.Cleanup=function(a){this.fp_cmd=2;this.free_cb=a;this.zeroed=0;this.virtio.notify_config_changes()};Yb.prototype.get_state=
407
- function(){const a=[];a[0]=this.virtio;a[1]=this.num_pages;a[2]=this.actual;return a};Yb.prototype.set_state=function(a){this.virtio.set_state(a[0]);this.num_pages=a[1];this.actual=a[2]};Yb.prototype.GetStats=function(a){this.stats_cb=a;for(a=this.virtio.queues[2];a.has_request();){const b=a.pop_request();this.virtio.queues[2].push_reply(b)}this.virtio.queues[2].flush_replies()};Yb.prototype.Reset=function(){};function Zb(a,b,c,d){D("Trying to load kernel of size "+b.byteLength);var e=new Uint8Array(b);
408
- const f=new Uint16Array(b);var h=new Uint32Array(b),l=e[497]||4,n=f[255];if(43605!==n)D("Bad checksum1: "+z(n));else if(n=f[257]|f[258]<<16,1400005704!==n)D("Bad checksum2: "+z(n));else{n=f[259];m(514<=n);var p=e[529];m(p&1);var q=f[283],r=h[139],t=h[140],v=e[564],y=e[565],C=518<=n?h[142]:255,u=h[146],B=h[147],w=h[150],G=h[151],L=h[152];D("kernel boot protocol version: "+z(n));D("flags="+z(p)+" xflags="+z(q));D("code32_start="+z(h[133]));D("initrd_addr_max="+z(r));D("kernel_alignment="+z(t));D("relocatable="+
409
- v);D("min_alignment="+z(y));D("cmdline max="+z(C));D("payload offset="+z(u)+" size="+z(B));D("pref_address="+z(G)+":"+z(w));D("init_size="+z(L));e[528]=255;e[529]=p&-97|128;f[274]=56832;f[253]=65535;D("heap_end_ptr="+z(56832));d+="\x00";m(d.length<C);D("cmd_line_ptr="+z(581632));h[138]=581632;for(e=0;e<d.length;e++)a[581632+e]=d.charCodeAt(e);l=512*(l+1);D("prot_mode_kernel_start="+z(l));d=new Uint8Array(b,0,l);b=new Uint8Array(b,l);e=l=0;c&&(l=67108864,e=c.byteLength,m(1048576+b.length<l),a.set(new Uint8Array(c),
410
- l));h[134]=l;h[135]=e;m(655360>524288+d.length);a.set(d,524288);a.set(b,1048576);a=new Uint8Array(512);(new Uint16Array(a.buffer))[0]=43605;a[2]=1;c=3;a[c++]=250;a[c++]=184;a[c++]=32768;a[c++]=128;a[c++]=142;a[c++]=192;a[c++]=142;a[c++]=216;a[c++]=142;a[c++]=224;a[c++]=142;a[c++]=232;a[c++]=142;a[c++]=208;a[c++]=188;a[c++]=57344;a[c++]=224;a[c++]=234;a[c++]=0;a[c++]=0;a[c++]=32800;a[c++]=128;m(512>c);h=a[c]=0;for(b=0;b<a.length;b++)h+=a[b];a[c]=-h;return{name:"genroms/kernel.bin",data:a}}}function K(a,
411
- b,c){this.stop_idling=c;this.wm=b;this.wasm_patch();this.create_jit_imports();this.wasm_memory=b=this.wm.exports.memory;this.memory_size=k(Uint32Array,b,812,1);this.mem8=new Uint8Array(0);this.mem32s=new Int32Array(this.mem8.buffer);this.segment_is_null=k(Uint8Array,b,724,8);this.segment_offsets=k(Int32Array,b,736,8);this.segment_limits=k(Uint32Array,b,768,8);this.segment_access_bytes=k(Uint8Array,b,512,8);this.protected_mode=k(Int32Array,b,800,1);this.idtr_size=k(Int32Array,b,564,1);this.idtr_offset=
412
- k(Int32Array,b,568,1);this.gdtr_size=k(Int32Array,b,572,1);this.gdtr_offset=k(Int32Array,b,576,1);this.tss_size_32=k(Int32Array,b,1128,1);this.page_fault=k(Uint32Array,b,540,8);this.cr=k(Int32Array,b,580,8);this.cpl=k(Uint8Array,b,612,1);this.is_32=k(Int32Array,b,804,1);this.stack_size_32=k(Int32Array,b,808,1);this.in_hlt=k(Uint8Array,b,616,1);this.last_virt_eip=k(Int32Array,b,620,1);this.eip_phys=k(Int32Array,b,624,1);this.sysenter_cs=k(Int32Array,b,636,1);this.sysenter_esp=k(Int32Array,b,640,1);
413
- this.sysenter_eip=k(Int32Array,b,644,1);this.prefixes=k(Int32Array,b,648,1);this.flags=k(Int32Array,b,120,1);this.flags_changed=k(Int32Array,b,100,1);this.last_op_size=k(Int32Array,b,96,1);this.last_op1=k(Int32Array,b,104,1);this.last_result=k(Int32Array,b,112,1);this.current_tsc=k(Uint32Array,b,960,2);this.devices={};this.instruction_pointer=k(Int32Array,b,556,1);this.previous_ip=k(Int32Array,b,560,1);this.apic_enabled=k(Uint8Array,b,548,1);this.acpi_enabled=k(Uint8Array,b,552,1);this.memory_map_read8=
414
- [];this.memory_map_write8=[];this.memory_map_read32=[];this.memory_map_write32=[];this.bios={main:null,vga:null};this.instruction_counter=k(Uint32Array,b,664,1);this.reg32=k(Int32Array,b,64,8);this.fpu_st=k(Int32Array,b,1152,32);this.fpu_stack_empty=k(Uint8Array,b,816,1);this.fpu_stack_empty[0]=255;this.fpu_stack_ptr=k(Uint8Array,b,1032,1);this.fpu_stack_ptr[0]=0;this.fpu_control_word=k(Uint16Array,b,1036,1);this.fpu_control_word[0]=895;this.fpu_status_word=k(Uint16Array,b,1040,1);this.fpu_status_word[0]=
415
- 0;this.fpu_ip=k(Int32Array,b,1048,1);this.fpu_ip[0]=0;this.fpu_ip_selector=k(Int32Array,b,1052,1);this.fpu_ip_selector[0]=0;this.fpu_opcode=k(Int32Array,b,1044,1);this.fpu_opcode[0]=0;this.fpu_dp=k(Int32Array,b,1056,1);this.fpu_dp[0]=0;this.fpu_dp_selector=k(Int32Array,b,1060,1);this.fpu_dp_selector[0]=0;this.reg_xmm32s=k(Int32Array,b,832,32);this.mxcsr=k(Int32Array,b,824,1);this.sreg=k(Uint16Array,b,668,8);this.dreg=k(Int32Array,b,684,8);this.reg_pdpte=k(Int32Array,b,968,8);this.svga_dirty_bitmap_min_offset=
416
- k(Uint32Array,b,716,1);this.svga_dirty_bitmap_max_offset=k(Uint32Array,b,720,1);this.fw_value=[];this.fw_pointer=0;this.option_roms=[];this.io=void 0;this.bus=a;this.set_tsc(0,0);this.debug_init();this.seen_code={};this.seen_code_uncompiled={}}K.prototype.clear_opstats=function(){(new Uint8Array(this.wasm_memory.buffer,32768,131072)).fill(0);this.wm.exports.profiler_init()};K.prototype.create_jit_imports=function(){const a=Object.create(null);a.m=this.wm.exports.memory;for(const b of Object.keys(this.wm.exports))b.startsWith("_")||
417
- b.startsWith("zstd")||b.endsWith("_js")||(a[b]=this.wm.exports[b]);this.jit_imports=a};K.prototype.wasm_patch=function(){const a=c=>this.wm.exports[c],b=c=>{const d=a(c);console.assert(d,"Missing import: "+c);return d};this.reset_cpu=b("reset_cpu");this.getiopl=b("getiopl");this.get_eflags=b("get_eflags");this.handle_irqs=b("handle_irqs");this.main_loop=b("main_loop");this.set_jit_config=b("set_jit_config");this.read8=b("read8");this.read16=b("read16");this.read32s=b("read32s");this.write8=b("write8");
418
- this.write16=b("write16");this.write32=b("write32");this.in_mapped_range=b("in_mapped_range");this.fpu_load_tag_word=b("fpu_load_tag_word");this.fpu_load_status_word=b("fpu_load_status_word");this.fpu_get_sti_f64=b("fpu_get_sti_f64");this.translate_address_system_read=b("translate_address_system_read_js");this.get_seg_cs=b("get_seg_cs");this.get_real_eip=b("get_real_eip");this.clear_tlb=b("clear_tlb");this.full_clear_tlb=b("full_clear_tlb");this.update_state_flags=b("update_state_flags");this.set_tsc=
419
- b("set_tsc");this.store_current_tsc=b("store_current_tsc");this.set_cpuid_level=b("set_cpuid_level");this.pic_set_irq=b("pic_set_irq");this.pic_clear_irq=b("pic_clear_irq");this.jit_force_generate_unsafe=a("jit_force_generate_unsafe");this.jit_clear_cache=b("jit_clear_cache_js");this.jit_dirty_cache=b("jit_dirty_cache");this.codegen_finalize_finished=b("codegen_finalize_finished");this.allocate_memory=b("allocate_memory");this.zero_memory=b("zero_memory");this.is_memory_zeroed=b("is_memory_zeroed");
420
- this.svga_allocate_memory=b("svga_allocate_memory");this.svga_allocate_dest_buffer=b("svga_allocate_dest_buffer");this.svga_fill_pixel_buffer=b("svga_fill_pixel_buffer");this.svga_mark_dirty=b("svga_mark_dirty");this.get_pic_addr_master=b("get_pic_addr_master");this.get_pic_addr_slave=b("get_pic_addr_slave");this.zstd_create_ctx=b("zstd_create_ctx");this.zstd_get_src_ptr=b("zstd_get_src_ptr");this.zstd_free_ctx=b("zstd_free_ctx");this.zstd_read=b("zstd_read");this.zstd_read_free=b("zstd_read_free");
421
- this.port20_read=b("port20_read");this.port21_read=b("port21_read");this.portA0_read=b("portA0_read");this.portA1_read=b("portA1_read");this.port20_write=b("port20_write");this.port21_write=b("port21_write");this.portA0_write=b("portA0_write");this.portA1_write=b("portA1_write");this.port4D0_read=b("port4D0_read");this.port4D1_read=b("port4D1_read");this.port4D0_write=b("port4D0_write");this.port4D1_write=b("port4D1_write")};K.prototype.jit_force_generate=function(a){this.jit_force_generate_unsafe?
422
- this.jit_force_generate_unsafe(a):m(!1,"Not supported in this wasm build: jit_force_generate_unsafe")};K.prototype.jit_clear_func=function(a){m(0<=a&&900>a);this.wm.wasm_table.set(a+1024,null)};K.prototype.jit_clear_all_funcs=function(){const a=this.wm.wasm_table;for(let b=0;900>b;b++)a.set(1024+b,null)};K.prototype.get_state=function(){var a=[];a[0]=this.memory_size[0];a[1]=new Uint8Array([...this.segment_is_null,...this.segment_access_bytes]);a[2]=this.segment_offsets;a[3]=this.segment_limits;a[4]=
423
- this.protected_mode[0];a[5]=this.idtr_offset[0];a[6]=this.idtr_size[0];a[7]=this.gdtr_offset[0];a[8]=this.gdtr_size[0];a[9]=this.page_fault[0];a[10]=this.cr;a[11]=this.cpl[0];a[13]=this.is_32[0];a[16]=this.stack_size_32[0];a[17]=this.in_hlt[0];a[18]=this.last_virt_eip[0];a[19]=this.eip_phys[0];a[22]=this.sysenter_cs[0];a[23]=this.sysenter_eip[0];a[24]=this.sysenter_esp[0];a[25]=this.prefixes[0];a[26]=this.flags[0];a[27]=this.flags_changed[0];a[28]=this.last_op1[0];a[30]=this.last_op_size[0];a[37]=
424
- this.instruction_pointer[0];a[38]=this.previous_ip[0];a[39]=this.reg32;a[40]=this.sreg;a[41]=this.dreg;a[42]=this.reg_pdpte;this.store_current_tsc();a[43]=this.current_tsc;a[45]=this.devices.virtio_9p;a[46]=this.devices.apic;a[47]=this.devices.rtc;a[48]=this.devices.pci;a[49]=this.devices.dma;a[50]=this.devices.acpi;a[52]=this.devices.vga;a[53]=this.devices.ps2;a[54]=this.devices.uart0;a[55]=this.devices.fdc;a[56]=this.devices.cdrom;a[57]=this.devices.hda;a[58]=this.devices.pit;a[59]=this.devices.net;
425
- a[60]=this.get_state_pic();a[61]=this.devices.sb16;a[62]=this.fw_value;a[63]=this.devices.ioapic;a[64]=this.tss_size_32[0];a[66]=this.reg_xmm32s;a[67]=this.fpu_st;a[68]=this.fpu_stack_empty[0];a[69]=this.fpu_stack_ptr[0];a[70]=this.fpu_control_word[0];a[71]=this.fpu_ip[0];a[72]=this.fpu_ip_selector[0];a[73]=this.fpu_dp[0];a[74]=this.fpu_dp_selector[0];a[75]=this.fpu_opcode[0];const {packed_memory:b,bitmap:c}=this.pack_memory();a[77]=b;a[78]=new Uint8Array(c.get_buffer());a[79]=this.devices.uart1;
426
- a[80]=this.devices.uart2;a[81]=this.devices.uart3;a[82]=this.devices.virtio_console;a[83]=this.devices.virtio_net;a[84]=this.devices.virtio_balloon;return a};K.prototype.get_state_pic=function(){const a=new Uint8Array(this.wasm_memory.buffer,this.get_pic_addr_master(),13),b=new Uint8Array(this.wasm_memory.buffer,this.get_pic_addr_slave(),13),c=[],d=[];c[0]=a[0];c[1]=a[1];c[2]=a[2];c[3]=a[3];c[4]=a[4];c[5]=d;c[6]=a[6];c[7]=a[7];c[8]=a[8];c[9]=a[9];c[10]=a[10];c[11]=a[11];c[12]=a[12];d[0]=b[0];d[1]=
427
- b[1];d[2]=b[2];d[3]=b[3];d[4]=b[4];d[5]=null;d[6]=b[6];d[7]=b[7];d[8]=b[8];d[9]=b[9];d[10]=b[10];d[11]=b[11];d[12]=b[12];return c};K.prototype.set_state=function(a){this.memory_size[0]=a[0];this.mem8.length!==this.memory_size[0]&&console.warn("Note: Memory size mismatch. we="+this.mem8.length+" state="+this.memory_size[0]);8===a[1].length?(this.segment_is_null.set(a[1]),this.segment_access_bytes.fill(242),this.segment_access_bytes[1]=250):16===a[1].length?(this.segment_is_null.set(a[1].subarray(0,
428
- 8)),this.segment_access_bytes.set(a[1].subarray(8,16))):m("Unexpected cpu segment state length:"+a[1].length);this.segment_offsets.set(a[2]);this.segment_limits.set(a[3]);this.protected_mode[0]=a[4];this.idtr_offset[0]=a[5];this.idtr_size[0]=a[6];this.gdtr_offset[0]=a[7];this.gdtr_size[0]=a[8];this.page_fault[0]=a[9];this.cr.set(a[10]);this.cpl[0]=a[11];this.is_32[0]=a[13];this.stack_size_32[0]=a[16];this.in_hlt[0]=a[17];this.last_virt_eip[0]=a[18];this.eip_phys[0]=a[19];this.sysenter_cs[0]=a[22];
429
- this.sysenter_eip[0]=a[23];this.sysenter_esp[0]=a[24];this.prefixes[0]=a[25];this.flags[0]=a[26];this.flags_changed[0]=a[27];this.last_op1[0]=a[28];this.last_op_size[0]=a[30];this.instruction_pointer[0]=a[37];this.previous_ip[0]=a[38];this.reg32.set(a[39]);this.sreg.set(a[40]);this.dreg.set(a[41]);a[42]&&this.reg_pdpte.set(a[42]);this.set_tsc(a[43][0],a[43][1]);this.devices.virtio_9p&&this.devices.virtio_9p.set_state(a[45]);this.devices.apic&&this.devices.apic.set_state(a[46]);this.devices.rtc&&this.devices.rtc.set_state(a[47]);
430
- this.devices.pci&&this.devices.pci.set_state(a[48]);this.devices.dma&&this.devices.dma.set_state(a[49]);this.devices.acpi&&this.devices.acpi.set_state(a[50]);this.devices.vga&&this.devices.vga.set_state(a[52]);this.devices.ps2&&this.devices.ps2.set_state(a[53]);this.devices.uart0&&this.devices.uart0.set_state(a[54]);this.devices.fdc&&this.devices.fdc.set_state(a[55]);this.devices.cdrom&&this.devices.cdrom.set_state(a[56]);this.devices.hda&&this.devices.hda.set_state(a[57]);this.devices.pit&&this.devices.pit.set_state(a[58]);
431
- this.devices.net&&this.devices.net.set_state(a[59]);this.set_state_pic(a[60]);this.devices.sb16&&this.devices.sb16.set_state(a[61]);this.devices.uart1&&this.devices.uart1.set_state(a[79]);this.devices.uart2&&this.devices.uart2.set_state(a[80]);this.devices.uart3&&this.devices.uart3.set_state(a[81]);this.devices.virtio_console&&this.devices.virtio_console.set_state(a[82]);this.devices.virtio_net&&this.devices.virtio_net.set_state(a[83]);this.devices.virtio_balloon&&this.devices.virtio_balloon.set_state(a[84]);
432
- this.fw_value=a[62];this.devices.ioapic&&this.devices.ioapic.set_state(a[63]);this.tss_size_32[0]=a[64];this.reg_xmm32s.set(a[66]);this.fpu_st.set(a[67]);this.fpu_stack_empty[0]=a[68];this.fpu_stack_ptr[0]=a[69];this.fpu_control_word[0]=a[70];this.fpu_ip[0]=a[71];this.fpu_ip_selector[0]=a[72];this.fpu_dp[0]=a[73];this.fpu_dp_selector[0]=a[74];this.fpu_opcode[0]=a[75];const b=new ua(a[78].buffer);this.unpack_memory(b,a[77]);this.update_state_flags();this.full_clear_tlb();this.jit_clear_cache()};K.prototype.set_state_pic=
433
- function(a){const b=new Uint8Array(this.wasm_memory.buffer,this.get_pic_addr_master(),13),c=new Uint8Array(this.wasm_memory.buffer,this.get_pic_addr_slave(),13);b[0]=a[0];b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];const d=a[5];b[6]=a[6];b[7]=a[7];b[8]=a[8];b[9]=a[9];b[10]=a[10];b[11]=a[11];b[12]=a[12];c[0]=d[0];c[1]=d[1];c[2]=d[2];c[3]=d[3];c[4]=d[4];c[6]=d[6];c[7]=d[7];c[8]=d[8];c[9]=d[9];c[10]=d[10];c[11]=d[11];c[12]=d[12]};K.prototype.pack_memory=function(){m(0===(this.mem8.length&4095));var a=this.mem8.length>>
434
- 12,b=[];for(var c=0;c<a;c++)this.is_memory_zeroed(c<<12,4096)||b.push(c);a=new ua(a);c=new Uint8Array(b.length<<12);for(const [d,e]of b.entries())a.set(e,1),b=e<<12,b=this.mem8.subarray(b,b+4096),c.set(b,d<<12);return{bitmap:a,packed_memory:c}};K.prototype.unpack_memory=function(a,b){this.zero_memory(0,this.memory_size[0]);const c=this.memory_size[0]>>12;let d=0;for(let f=0;f<c;f++)if(a.get(f)){var e=d<<12;e=b.subarray(e,e+4096);this.mem8.set(e,f<<12);d++}};K.prototype.reboot_internal=function(){this.reset_cpu();
435
- this.fw_value=[];this.devices.virtio_9p&&this.devices.virtio_9p.reset();this.devices.virtio_console&&this.devices.virtio_console.reset();this.devices.virtio_net&&this.devices.virtio_net.reset();this.devices.ps2&&this.devices.ps2.reset();this.load_bios()};K.prototype.reset_memory=function(){this.mem8.fill(0)};K.prototype.create_memory=function(a,b){a<b?(a=b,D("Rounding memory size up to "+a,2)):0>(a|0)&&(a=Math.pow(2,31)-131072,D("Rounding memory size down to "+a,2));a=(a-1|131071)+1|0;m(0<(a|0));
436
- m(0===(a&131071));console.assert(0===this.memory_size[0],"Expected uninitialised memory");this.memory_size[0]=a;b=this.allocate_memory(a);this.mem8=k(Uint8Array,this.wasm_memory,b,a);this.mem32s=k(Uint32Array,this.wasm_memory,b,a>>2)};K.prototype.init=function(a,b){this.create_memory(a.memory_size||67108864,a.initrd?67108864:1048576);a.disable_jit&&this.set_jit_config(0,1);a.cpuid_level&&this.set_cpuid_level(a.cpuid_level);this.acpi_enabled[0]=+a.acpi;this.reset_cpu();var c=new Ia(this);this.io=c;
437
- this.bios.main=a.bios;this.bios.vga=a.vga_bios;this.load_bios();if(a.bzimage){const e=Zb(this.mem8,a.bzimage,a.initrd,a.cmdline||"");e&&this.option_roms.push(e)}c.register_read(179,this,function(){D("port 0xB3 read");return 0});var d=0;c.register_read(146,this,function(){return d});c.register_write(146,this,function(e){d=e});c.register_read(1297,this,function(){if(this.fw_pointer<this.fw_value.length)return this.fw_value[this.fw_pointer++];m(!1,"config port: Read past value");return 0});c.register_write(1296,
438
- this,void 0,function(e){function f(n){return new Uint8Array(Int32Array.of(n).buffer)}function h(n){return n>>8|n<<8&65280}function l(n){return n<<24|n<<8&16711680|n>>8&65280|n>>>24}D("bios config port, index="+z(e));this.fw_pointer=0;if(0===e)this.fw_value=f(1431127377);else if(1===e)this.fw_value=f(0);else if(3===e)this.fw_value=f(this.memory_size[0]);else if(5===e)this.fw_value=f(1);else if(15===e)this.fw_value=f(1);else if(13===e)this.fw_value=new Uint8Array(16);else if(25===e){e=new Int32Array(4+
439
- 64*this.option_roms.length);const n=new Uint8Array(e.buffer);e[0]=l(this.option_roms.length);for(let p=0;p<this.option_roms.length;p++){const {name:q,data:r}=this.option_roms[p],t=4+64*p;m(65536>49152+p);e[t+0>>2]=l(r.length);e[t+4>>2]=h(49152+p);m(56>q.length);for(let v=0;v<q.length;v++)n[t+8+v]=q.charCodeAt(v)}this.fw_value=n}else 32768<=e&&49152>e?this.fw_value=f(0):49152<=e&&e-49152<this.option_roms.length?this.fw_value=this.option_roms[e-49152].data:(D("Warning: Unimplemented fw index: "+z(e)),
440
- this.fw_value=f(0))});c.register_write(128,this,function(){});c.register_read(128,this,function(){return 255});c.register_write(233,this,function(){});c.register_read(32,this,this.port20_read);c.register_read(33,this,this.port21_read);c.register_read(160,this,this.portA0_read);c.register_read(161,this,this.portA1_read);c.register_write(32,this,this.port20_write);c.register_write(33,this,this.port21_write);c.register_write(160,this,this.portA0_write);c.register_write(161,this,this.portA1_write);c.register_read(1232,
441
- this,this.port4D0_read);c.register_read(1233,this,this.port4D1_read);c.register_write(1232,this,this.port4D0_write);c.register_write(1233,this,this.port4D1_write);this.devices={};a.load_devices&&(this.devices.pci=new mb(this),this.acpi_enabled[0]&&(this.devices.ioapic=new hb(this),this.devices.apic=new gb(this),this.devices.acpi=new ib(this)),this.devices.rtc=new Rb(this),this.fill_cmos(this.devices.rtc,a),this.devices.dma=new E(this),this.devices.vga=new W(this,b,a.screen,a.vga_memory_size||8388608),
442
- this.devices.ps2=new yb(this,b),this.devices.uart0=new lb(this,1016,b),a.uart1&&(this.devices.uart1=new lb(this,760,b)),a.uart2&&(this.devices.uart2=new lb(this,1E3,b)),a.uart3&&(this.devices.uart3=new lb(this,744,b)),this.devices.fdc=new Q(this,a.fda,a.fdb),c=0,a.hda&&(this.devices.hda=new Sb(this,a.hda,a.hdb,!1,c++,b)),a.cdrom&&(this.devices.cdrom=new Sb(this,a.cdrom,void 0,!0,c++,b)),this.devices.pit=new jb(this,b),"ne2k"===a.net_device.type?this.devices.net=new pb(this,b,a.preserve_mac_from_state_image,
443
- a.mac_address_translation):"virtio"===a.net_device.type&&(this.devices.virtio_net=new Tb(this,b,a.preserve_mac_from_state_image)),a.fs9p&&(this.devices.virtio_9p=new fc(a.fs9p,this,b)),a.virtio_console&&(this.devices.virtio_console=new sb(this,b)),a.virtio_balloon&&(this.devices.virtio_balloon=new Yb(this,b)),this.devices.sb16=new F(this,b));a.multiboot&&(D("loading multiboot",2),a=this.load_multiboot_option_rom(a.multiboot,a.initrd,a.cmdline))&&(this.bios.main?(D("adding option rom for multiboot",
444
- 2),this.option_roms.push(a)):(D("loaded multiboot without bios",2),this.reg32[0]=this.io.port_read32(244)));this.debug.init()};K.prototype.load_multiboot=function(a){this.bios.main&&m(!1,"load_multiboot not supported with BIOS");this.load_multiboot_option_rom(a,void 0,"")&&(D("loaded multiboot",2),this.reg32[0]=this.io.port_read32(244))};K.prototype.load_multiboot_option_rom=function(a,b,c){D("Trying multiboot from buffer of size "+a.byteLength,2);if(8192>a.byteLength){var d=new Int32Array(2048);
445
- (new Uint8Array(d.buffer)).set(new Uint8Array(a))}else d=new Int32Array(a,0,2048);for(var e=0;8192>e;e+=4){if(464367618===d[e>>2]){var f=d[e+4>>2];if(464367618+f+d[e+8>>2]|0){D("Multiboot checksum check failed",2);continue}}else continue;D("Multiboot magic found, flags: "+z(f>>>0,8),2);m(0===(f&-65540),"TODO");var h=this;this.io.register_read(244,this,function(){return 0},function(){return 0},function(){var q=31860,r=0;if(c){r|=4;h.write32(31760,q);c+="\x00";var t=(new TextEncoder).encode(c);h.write_blob(t,
446
- q);q+=t.length}if(f&2){r|=64;t=0;h.write32(31788,0);h.write32(31792,q);var v=0;let w=!1;for(let G=0;4294967296>G;G+=131072)w&&void 0!==h.memory_map_read8[G>>>17]?(h.write32(q,20),h.write32(q+4,v),h.write32(q+8,0),h.write32(q+12,G-v),h.write32(q+16,0),h.write32(q+20,1),q+=24,t+=24,w=!1):w||void 0!==h.memory_map_read8[G>>>17]||(v=G,w=!0);m(!w,"top of 4GB shouldn't have memory");h.write32(31788,t)}h.write32(31744,r);t=r=0;if(f&65536){D("Multiboot specifies its own address table",2);var y=d[e+12>>2];
447
- var C=d[e+16>>2];var u=d[e+20>>2];var B=d[e+24>>2];r=d[e+28>>2];D("header="+z(y,8)+" load="+z(C,8)+" load_end="+z(u,8)+" bss_end="+z(B,8)+" entry="+z(r,8));m(C<=y);y=e-(y-C);0===u?t=void 0:(m(u>=C),t=u-C);y=new Uint8Array(a,y,t);h.write_blob(y,C);r|=0;t=Math.max(u,B)}else if(1179403647===d[0]){D("Multiboot image is in elf format",2);v=new DataView(a);const [w,G]=Pb(v,Db);console.assert(52===G);for(y of Object.keys(w))D(y+": 0x"+(w[y].toString(16)>>>0));console.assert(1179403647===w.magic,"Bad magic");
448
- console.assert(1===w.class,"Unimplemented: 64 bit elf");console.assert(1===w.data,"Unimplemented: big endian");console.assert(1===w.version0,"Bad version0");console.assert(2===w.type,"Unimplemented type");console.assert(1===w.version1,"Bad version1");console.assert(52===w.ehsize,"Bad header size");console.assert(32===w.phentsize,"Bad program header size");console.assert(40===w.shentsize,"Bad section header size");[r]=Qb(new DataView(v.buffer,v.byteOffset+w.phoff,w.phentsize*w.phnum),Nb,w.phnum);[y]=
449
- Qb(new DataView(v.buffer,v.byteOffset+w.shoff,w.shentsize*w.shnum),Ob,w.shnum);if(ca){console.log("%d program headers:",r.length);for(B of r)console.log("type=%s offset=%s vaddr=%s paddr=%s filesz=%s memsz=%s flags=%s align=%s",B.type.toString(16),B.offset.toString(16),B.vaddr.toString(16),B.paddr.toString(16),B.filesz.toString(16),B.memsz.toString(16),B.flags.toString(16),B.align.toString(16));console.log("%d section headers:",y.length);for(C of y)console.log("name=%s type=%s flags=%s addr=%s offset=%s size=%s link=%s info=%s addralign=%s entsize=%s",
450
- C.name.toString(16),C.type.toString(16),C.flags.toString(16),C.addr.toString(16),C.offset.toString(16),C.size.toString(16),C.link.toString(16),C.info.toString(16),C.addralign.toString(16),C.entsize.toString(16))}B=w;C=r;r=B.entry;for(u of C)0!==u.type&&(1===u.type?(m(u.filesz<=u.memsz),u.paddr+u.memsz<h.memory_size[0]?(u.filesz&&(C=new Uint8Array(a,u.offset,u.filesz),h.write_blob(C,u.paddr)),t=Math.max(t,u.paddr+u.memsz),D("prg load "+u.paddr+" to "+(u.paddr+u.memsz),2),r===B.entry&&u.vaddr<=r&&u.vaddr+
451
- u.memsz>r&&(r=r-u.vaddr+u.paddr)):D("Warning: Skipped loading section, paddr="+z(u.paddr)+" memsz="+u.memsz,2)):2===u.type||3===u.type||4===u.type||6===u.type||7===u.type||1685382480===u.type||1685382481===u.type||1685382482===u.type||1685382483===u.type?D("skip load type "+u.type+" "+u.paddr+" to "+(u.paddr+u.memsz),2):m(!1,"unimplemented elf section type: "+z(u.type)))}else m(!1,"Not a bootable multiboot format");b&&(h.write32(31764,1),h.write32(31768,q),u=t,0!==(u&4095)&&(u=(u&-4096)+4096),D("ramdisk address "+
452
- u),B=u+b.byteLength,h.write32(q,u),h.write32(q+4,B),h.write32(q+8,0),h.write32(q+12,0),m(B<h.memory_size[0]),h.write_blob(new Uint8Array(b),u));h.reg32[3]=31744;h.cr[0]=1;h.protected_mode[0]=1;h.flags[0]=2;h.is_32[0]=1;h.stack_size_32[0]=1;for(q=0;6>q;q++)h.segment_is_null[q]=0,h.segment_offsets[q]=0,h.segment_limits[q]=4294967295,h.sreg[q]=45058;h.instruction_pointer[0]=h.get_seg_cs()+r|0;h.update_state_flags();D("Starting multiboot kernel at:",2);h.debug.dump_state();h.debug.dump_regs();return 732803074});
453
- this.io.register_write_consecutive(244,this,function(q){console.log("Test exited with code "+z(q,2));throw"HALT";},function(){},function(){},function(){});for(let q=0;15>=q;q++){function r(t){D("kvm-unit-test: Set irq "+z(q)+" to "+z(t,2));t?this.device_raise_irq(q):this.device_lower_irq(q)}this.io.register_write(8192+q,this,r,r,r)}const n=new Uint8Array(512);(new Uint16Array(n.buffer))[0]=43605;n[2]=1;var l=3;n[l++]=102;n[l++]=229;n[l++]=244;m(512>l);let p=n[l]=0;for(let q=0;q<n.length;q++)p+=n[q];
454
- n[l]=-p;return{name:"genroms/multiboot.bin",data:n}}D("Multiboot header not found",2)};K.prototype.fill_cmos=function(a,b){var c=b.boot_order||291;a.cmos_write(56,1|c>>4&240);a.cmos_write(61,c&255);a.cmos_write(21,128);a.cmos_write(22,2);c=0;1048576<=this.memory_size[0]&&(c=this.memory_size[0]-1048576>>10,c=Math.min(c,65535));a.cmos_write(23,c&255);a.cmos_write(24,c>>8&255);a.cmos_write(48,c&255);a.cmos_write(49,c>>8&255);c=0;16777216<=this.memory_size[0]&&(c=this.memory_size[0]-16777216>>16,c=Math.min(c,
455
- 65535));a.cmos_write(52,c&255);a.cmos_write(53,c>>8&255);a.cmos_write(91,0);a.cmos_write(92,0);a.cmos_write(93,0);a.cmos_write(20,47);a.cmos_write(95,0);b.fastboot&&a.cmos_write(63,1)};K.prototype.load_bios=function(){var a=this.bios.main,b=this.bios.vga;if(a){m(a instanceof ArrayBuffer);var c=new Uint8Array(a);this.write_blob(c,1048576-a.byteLength);if(b){m(b instanceof ArrayBuffer);var d=new Uint8Array(b);this.write_blob(d,786432);this.io.mmap_register(4272947200,1048576,function(e){e=e-4272947200|
456
- 0;return e<d.length?d[e]:0},function(){m(!1,"Unexpected write to VGA rom")})}else D("Warning: No VGA BIOS");this.io.mmap_register(4293918720,1048576,function(e){return this.mem8[e&1048575]}.bind(this),function(e,f){this.mem8[e&1048575]=f}.bind(this))}else D("Warning: No BIOS")};K.prototype.codegen_finalize=function(a,b,c,d,e){d>>>=0;e>>>=0;m(0<=a&&900>a);const f=new Uint8Array(this.wasm_memory.buffer,d,e);this.seen_code[b]=(this.seen_code[b]||0)+1;this.test_hook_did_generate_wasm&&this.test_hook_did_generate_wasm(f);
457
- WebAssembly.instantiate(f,{e:this.jit_imports}).then(h=>{this.wm.wasm_table.set(a+1024,h.instance.exports.f);this.codegen_finalize_finished(a,b,c);this.test_hook_did_finalize_wasm&&this.test_hook_did_finalize_wasm(f)}).catch(h=>{console.log(h);debugger;throw h;})};K.prototype.log_uncompiled_code=function(){};K.prototype.dump_function_code=function(){};K.prototype.run_hardware_timers=function(a,b){const c=this.devices.pit.timer(b,!1),d=this.devices.rtc.timer(b,!1);let e=100,f=100;a&&(e=this.devices.acpi.timer(b),
458
- f=this.devices.apic.timer(b));return Math.min(c,d,e,f)};K.prototype.device_raise_irq=function(a){m(1===arguments.length);this.pic_set_irq(a);this.devices.ioapic&&this.devices.ioapic.set_irq(a)};K.prototype.device_lower_irq=function(a){this.pic_clear_irq(a);this.devices.ioapic&&this.devices.ioapic.clear_irq(a)};function xb(a,b){this.cpu=a;this.pci=a.devices.pci;this.device_id=b.device_id;this.pci_space=[244,26,b.device_id&255,b.device_id>>8,7,5,16,0,1,0,2,0,0,0,0,0,1,168,0,0,0,16,191,254,0,0,0,0,0,
459
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,b.subsystem_device_id&255,b.subsystem_device_id>>8,0,0,0,0,64,0,0,0,0,0,0,0,0,1,0,0];this.pci_space=this.pci_space.concat(Array(256-this.pci_space.length).fill(0));this.pci_id=b.pci_id;this.pci_bars=[];this.name=b.name;this.driver_feature_select=this.device_feature_select=0;this.device_feature=new Uint32Array(4);this.driver_feature=new Uint32Array(4);for(var c of b.common.features)m(0<=c,"VirtIO device<"+this.name+"> feature bit numbers must be non-negative"),
460
- m(128>c,"VirtIO device<"+this.name+"> feature bit numbers assumed less than 128 in implementation"),this.device_feature[c>>>5]|=1<<(c&31),this.driver_feature[c>>>5]|=1<<(c&31);m(b.common.features.includes(32),"VirtIO device<"+this.name+"> only non-transitional devices are supported");this.features_ok=!0;this.device_status=0;this.config_has_changed=!1;this.config_generation=0;this.queues=[];for(var d of b.common.queues)this.queues.push(new X(a,this,d));this.queue_select=0;this.queue_selected=this.queues[0];
461
- this.isr_status=0;c=new Set;for(var e of this.queues.map(f=>f.notify_offset))d=b.notification.single_handler?0:e,c.add(d),m(b.notification.handlers[d],"VirtIO device<"+this.name+"> every queue's notifier must exist");for(const [f,h]of b.notification.handlers.entries())m(!h||c.has(f),"VirtIO device<"+this.name+"> no defined notify handler should be unused");e=[];e.push(this.create_common_capability(b.common));e.push(this.create_notification_capability(b.notification));e.push(this.create_isr_capability(b.isr_status));
462
- b.device_specific&&e.push(this.create_device_specific_capability(b.device_specific));this.init_capabilities(e);a.devices.pci.register_device(this);this.reset()}xb.prototype.create_common_capability=function(a){return{type:1,bar:0,port:a.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array(0),struct:[{bytes:4,name:"device_feature_select",read:()=>this.device_feature_select,write:b=>{this.device_feature_select=b}},{bytes:4,name:"device_feature",read:()=>this.device_feature[this.device_feature_select]||
463
- 0,write:()=>{}},{bytes:4,name:"driver_feature_select",read:()=>this.driver_feature_select,write:b=>{this.driver_feature_select=b}},{bytes:4,name:"driver_feature",read:()=>this.driver_feature[this.driver_feature_select]||0,write:b=>{const c=this.device_feature[this.driver_feature_select];this.driver_feature_select<this.driver_feature.length&&(this.driver_feature[this.driver_feature_select]=b&c);this.features_ok=this.features_ok&&!(b&~c)}},{bytes:2,name:"msix_config",read:()=>{D("No msi-x capability supported.",
464
- 2097152);return 65535},write:()=>{D("No msi-x capability supported.",2097152)}},{bytes:2,name:"num_queues",read:()=>this.queues.length,write:()=>{}},{bytes:1,name:"device_status",read:()=>this.device_status,write:b=>{0===b?(D("Reset device<"+this.name+">",2097152),this.reset()):b&128?D("Warning: Device<"+this.name+"> status failed",2097152):D("Device<"+this.name+"> status: "+(b&1?"ACKNOWLEDGE ":"")+(b&2?"DRIVER ":"")+(b&4?"DRIVER_OK":"")+(b&8?"FEATURES_OK ":"")+(b&64?"DEVICE_NEEDS_RESET":""),2097152);
465
- b&~this.device_status&4&&this.device_status&64&&this.notify_config_changes();this.features_ok||(b&8&&D("Removing FEATURES_OK",2097152),b&=-9);this.device_status=b;if(b&~this.device_status&4)a.on_driver_ok()}},{bytes:1,name:"config_generation",read:()=>this.config_generation,write:()=>{}},{bytes:2,name:"queue_select",read:()=>this.queue_select,write:b=>{this.queue_select=b;this.queue_selected=this.queue_select<this.queues.length?this.queues[this.queue_select]:null}},{bytes:2,name:"queue_size",read:()=>
466
- this.queue_selected?this.queue_selected.size:0,write:b=>{this.queue_selected&&(b&b-1&&(D("Warning: dev<"+this.name+"> Given queue size was not a power of 2. Rounding up to next power of 2.",2097152),b=1<<ha(b-1)+1),b>this.queue_selected.size_supported&&(D("Warning: dev<"+this.name+"> Trying to set queue size greater than supported. Clamping to supported size.",2097152),b=this.queue_selected.size_supported),this.queue_selected.set_size(b))}},{bytes:2,name:"queue_msix_vector",read:()=>{D("No msi-x capability supported.",
467
- 2097152);return 65535},write:()=>{D("No msi-x capability supported.",2097152)}},{bytes:2,name:"queue_enable",read:()=>this.queue_selected?this.queue_selected.enabled|0:0,write:b=>{this.queue_selected&&(1===b?this.queue_selected.is_configured()?this.queue_selected.enable():D("Driver bug: tried enabling unconfigured queue",2097152):0===b&&D("Driver bug: tried writing 0 to queue_enable",2097152))}},{bytes:2,name:"queue_notify_off",read:()=>this.queue_selected?this.queue_selected.notify_offset:0,write:()=>
468
- {}},{bytes:4,name:"queue_desc (low dword)",read:()=>this.queue_selected?this.queue_selected.desc_addr:0,write:b=>{this.queue_selected&&(this.queue_selected.desc_addr=b)}},{bytes:4,name:"queue_desc (high dword)",read:()=>0,write:b=>{0!==b&&D("Warning: High dword of 64 bit queue_desc ignored:"+b,2097152)}},{bytes:4,name:"queue_avail (low dword)",read:()=>this.queue_selected?this.queue_selected.avail_addr:0,write:b=>{this.queue_selected&&(this.queue_selected.avail_addr=b)}},{bytes:4,name:"queue_avail (high dword)",
469
- read:()=>0,write:b=>{0!==b&&D("Warning: High dword of 64 bit queue_avail ignored:"+b,2097152)}},{bytes:4,name:"queue_used (low dword)",read:()=>this.queue_selected?this.queue_selected.used_addr:0,write:b=>{this.queue_selected&&(this.queue_selected.used_addr=b)}},{bytes:4,name:"queue_used (high dword)",read:()=>0,write:b=>{0!==b&&D("Warning: High dword of 64 bit queue_used ignored:"+b,2097152)}}]}};xb.prototype.create_notification_capability=function(a){const b=[];let c;a.single_handler?(m(1===a.handlers.length,
470
- "VirtIO device<"+this.name+"> too many notify handlers specified: expected single handler"),c=0):c=2;for(const [d,e]of a.handlers.entries())b.push({bytes:2,name:"notify"+d,read:()=>65535,write:e||(()=>{})});return{type:2,bar:1,port:a.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array([c&255,c>>8&255,c>>16&255,c>>24]),struct:b}};xb.prototype.create_isr_capability=function(a){return{type:3,bar:2,port:a.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array(0),struct:[{bytes:1,name:"isr_status",
471
- read:()=>{const b=this.isr_status;this.lower_irq();return b},write:()=>{}}]}};xb.prototype.create_device_specific_capability=function(a){m(~a.offset&3,"VirtIO device<"+this.name+"> device specific cap offset must be 4-byte aligned");return{type:4,bar:3,port:a.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array(0),struct:a.struct}};xb.prototype.init_capabilities=function(a){let b=this.pci_space[52]=64;var c=b;for(const e of a){a=16+e.extra.length;c=b;b=c+a;m(256>=b,"VirtIO device<"+this.name+"> can't fit all capabilities into 256byte configspace");
472
- m(0<=e.bar&&6>e.bar,"VirtIO device<"+this.name+"> capability invalid bar number");var d=e.struct.reduce((f,h)=>f+h.bytes,0);d+=e.offset;d=16>d?16:1<<ha(d-1)+1;m(0===(e.port&d-1),"VirtIO device<"+this.name+"> capability port should be aligned to pci bar size");this.pci_bars[e.bar]={size:d};this.pci_space[c]=9;this.pci_space[c+1]=b;this.pci_space[c+2]=a;this.pci_space[c+3]=e.type;this.pci_space[c+4]=e.bar;this.pci_space[c+5]=0;this.pci_space[c+6]=0;this.pci_space[c+7]=0;this.pci_space[c+8]=e.offset&
473
- 255;this.pci_space[c+9]=e.offset>>>8&255;this.pci_space[c+10]=e.offset>>>16&255;this.pci_space[c+11]=e.offset>>>24;this.pci_space[c+12]=d&255;this.pci_space[c+13]=d>>>8&255;this.pci_space[c+14]=d>>>16&255;this.pci_space[c+15]=d>>>24;for(const [f,h]of e.extra.entries())this.pci_space[c+16+f]=h;c=16+4*e.bar;this.pci_space[c]=e.port&254|!e.use_mmio;this.pci_space[c+1]=e.port>>>8&255;this.pci_space[c+2]=e.port>>>16&255;this.pci_space[c+3]=e.port>>>24&255;c=e.port+e.offset;for(const f of e.struct){let h=
474
- f.read;a=f.write;h=()=>{const l=f.read();D("Device<"+this.name+"> cap["+e.type+"] read["+f.name+"] => "+z(l,8*f.bytes),2097152);return l};a=l=>{D("Device<"+this.name+"> cap["+e.type+"] write["+f.name+"] <= "+z(l,8*f.bytes),2097152);f.write(l)};if(e.use_mmio)m(!1,"VirtIO device <"+this.name+"> mmio capability not implemented.");else{d=function(n){D("Warning: 8-bit read from 16-bit virtio port",2097152);return h(n&-2)>>((n&1)<<3)&255};const l=function(n){D("Warning: 8-bit read from 32-bit virtio port",
475
- 2097152);return h(n&-4)>>((n&3)<<3)&255};switch(f.bytes){case 4:this.cpu.io.register_read(c,this,l,void 0,h);this.cpu.io.register_read(c+1,this,l);this.cpu.io.register_read(c+2,this,l);this.cpu.io.register_read(c+3,this,l);this.cpu.io.register_write(c,this,void 0,void 0,a);break;case 2:this.cpu.io.register_read(c,this,d,h);this.cpu.io.register_read(c+1,this,d);this.cpu.io.register_write(c,this,void 0,a);break;case 1:this.cpu.io.register_read(c,this,h);this.cpu.io.register_write(c,this,a);break;default:m(!1,
476
- "VirtIO device <"+this.name+"> invalid capability field width of "+f.bytes+" bytes")}}c+=f.bytes}}m(256>=b+20,"VirtIO device<"+this.name+"> can't fit all capabilities into 256byte configspace");this.pci_space[b]=9;this.pci_space[b+1]=0;this.pci_space[b+2]=20;this.pci_space[b+3]=5;this.pci_space[b+4]=0;this.pci_space[b+5]=0;this.pci_space[b+6]=0;this.pci_space[b+7]=0;this.pci_space[b+8]=0;this.pci_space[b+9]=0;this.pci_space[b+10]=0;this.pci_space[b+11]=0;this.pci_space[b+12]=0;this.pci_space[b+13]=
477
- 0;this.pci_space[b+14]=0;this.pci_space[b+15]=0;this.pci_space[b+16]=0;this.pci_space[b+17]=0;this.pci_space[b+18]=0;this.pci_space[b+19]=0};xb.prototype.get_state=function(){let a=[];a[0]=this.device_feature_select;a[1]=this.driver_feature_select;a[2]=this.device_feature;a[3]=this.driver_feature;a[4]=this.features_ok;a[5]=this.device_status;a[6]=this.config_has_changed;a[7]=this.config_generation;a[8]=this.isr_status;a[9]=this.queue_select;return a=a.concat(this.queues)};xb.prototype.set_state=function(a){this.device_feature_select=
478
- a[0];this.driver_feature_select=a[1];this.device_feature=a[2];this.driver_feature=a[3];this.features_ok=a[4];this.device_status=a[5];this.config_has_changed=a[6];this.config_generation=a[7];this.isr_status=a[8];this.queue_select=a[9];let b=0;for(const c of a.slice(10))this.queues[b].set_state(c),b++;this.queue_selected=this.queues[this.queue_select]||null};xb.prototype.reset=function(){this.driver_feature_select=this.device_feature_select=0;this.driver_feature.set(this.device_feature);this.features_ok=
479
- !0;this.queue_select=this.device_status=0;this.queue_selected=this.queues[0];for(const a of this.queues)a.reset();this.config_has_changed=!1;this.config_generation=0;this.lower_irq()};xb.prototype.notify_config_changes=function(){this.config_has_changed=!0;this.device_status&4?this.raise_irq(2):m(!1,"VirtIO device<"+this.name+"> attempted to notify driver before DRIVER_OK")};xb.prototype.update_config_generation=function(){this.config_has_changed&&(this.config_generation++,this.config_generation&=
480
- 255,this.config_has_changed=!1)};xb.prototype.is_feature_negotiated=function(a){return 0<(this.driver_feature[a>>>5]&1<<(a&31))};xb.prototype.needs_reset=function(){D("Device<"+this.name+"> experienced error - requires reset",2097152);this.device_status|=64;this.device_status&4&&this.notify_config_changes()};xb.prototype.raise_irq=function(a){D("Raise irq "+z(a),2097152);this.isr_status|=a;this.pci.raise_irq(this.pci_id)};xb.prototype.lower_irq=function(){D("Lower irq ",2097152);this.isr_status=0;
481
- this.pci.lower_irq(this.pci_id)};function X(a,b,c){this.cpu=a;this.virtio=b;this.size_supported=this.size=c.size_supported;this.mask=this.size-1;this.enabled=!1;this.notify_offset=c.notify_offset;this.num_staged_replies=this.used_addr=this.avail_last_idx=this.avail_addr=this.desc_addr=0;this.reset()}X.prototype.get_state=function(){const a=[];a[0]=this.size;a[1]=this.size_supported;a[2]=this.enabled;a[3]=this.notify_offset;a[4]=this.desc_addr;a[5]=this.avail_addr;a[6]=this.avail_last_idx;a[7]=this.used_addr;
482
- a[8]=this.num_staged_replies;a[9]=1;return a};X.prototype.set_state=function(a){this.size=a[0];this.size_supported=a[1];this.enabled=a[2];this.notify_offset=a[3];this.desc_addr=a[4];this.avail_addr=a[5];this.avail_last_idx=a[6];this.used_addr=a[7];this.num_staged_replies=a[8];this.mask=this.size-1;this.fix_wrapping=1!==a[9]};X.prototype.reset=function(){this.enabled=!1;this.num_staged_replies=this.used_addr=this.avail_last_idx=this.avail_addr=this.desc_addr=0;this.set_size(this.size_supported)};X.prototype.is_configured=
483
- function(){return this.desc_addr&&this.avail_addr&&this.used_addr};X.prototype.enable=function(){m(this.is_configured(),"VirtQueue must be configured before enabled");this.enabled=!0};X.prototype.set_size=function(a){m(0===(a&a-1),"VirtQueue size must be power of 2 or zero");m(a<=this.size_supported,"VirtQueue size must be within supported size");this.size=a;this.mask=a-1};X.prototype.count_requests=function(){m(this.avail_addr,"VirtQueue addresses must be configured before use");this.fix_wrapping&&
484
- (this.fix_wrapping=!1,this.avail_last_idx=(this.avail_get_idx()&~this.mask)+(this.avail_last_idx&this.mask));return this.avail_get_idx()-this.avail_last_idx&65535};X.prototype.has_request=function(){m(this.avail_addr,"VirtQueue addresses must be configured before use");return 0!==this.count_requests()};X.prototype.pop_request=function(){m(this.avail_addr,"VirtQueue addresses must be configured before use");m(this.has_request(),"VirtQueue must not pop nonexistent request");var a=this.avail_get_entry(this.avail_last_idx);
485
- D("Pop request: avail_last_idx="+this.avail_last_idx+" desc_idx="+a,2097152);a=new gc(this,a);this.avail_last_idx=this.avail_last_idx+1&65535;return a};X.prototype.push_reply=function(a){m(this.used_addr,"VirtQueue addresses must be configured before use");m(this.num_staged_replies<this.size,"VirtQueue replies must not exceed queue size");const b=this.used_get_idx()+this.num_staged_replies&this.mask;D("Push reply: used_idx="+b+" desc_idx="+a.head_idx,2097152);this.used_set_entry(b,a.head_idx,a.length_written);
486
- this.num_staged_replies++};X.prototype.flush_replies=function(){m(this.used_addr,"VirtQueue addresses must be configured before use");if(0===this.num_staged_replies)D("flush_replies: Nothing to flush",2097152);else{D("Flushing "+this.num_staged_replies+" replies",2097152);var a=this.used_get_idx()+this.num_staged_replies&65535;this.used_set_idx(a);this.num_staged_replies=0;this.virtio.is_feature_negotiated(29)?(this.avail_get_used_event(),this.virtio.raise_irq(1)):~this.avail_get_flags()&1&&this.virtio.raise_irq(1)}};
487
- X.prototype.notify_me_after=function(a){m(0<=a,"Must skip a non-negative number of requests");a=this.avail_get_idx()+a&65535;this.used_set_avail_event(a)};X.prototype.get_descriptor=function(a,b){return{addr_low:this.cpu.read32s(a+16*b),addr_high:this.cpu.read32s(a+16*b+4),len:this.cpu.read32s(a+16*b+8),flags:this.cpu.read16(a+16*b+12),next:this.cpu.read16(a+16*b+14)}};X.prototype.avail_get_flags=function(){return this.cpu.read16(this.avail_addr)};X.prototype.avail_get_idx=function(){return this.cpu.read16(this.avail_addr+
488
- 2)};X.prototype.avail_get_entry=function(a){return this.cpu.read16(this.avail_addr+4+2*(a&this.mask))};X.prototype.avail_get_used_event=function(){return this.cpu.read16(this.avail_addr+4+2*this.size)};X.prototype.used_get_flags=function(){return this.cpu.read16(this.used_addr)};X.prototype.used_set_flags=function(a){this.cpu.write16(this.used_addr,a)};X.prototype.used_get_idx=function(){return this.cpu.read16(this.used_addr+2)};X.prototype.used_set_idx=function(a){this.cpu.write16(this.used_addr+
489
- 2,a)};X.prototype.used_set_entry=function(a,b,c){this.cpu.write32(this.used_addr+4+8*a,b);this.cpu.write32(this.used_addr+8+8*a,c)};X.prototype.used_set_avail_event=function(a){this.cpu.write16(this.used_addr+4+8*this.size,a)};function gc(a,b){this.cpu=a.cpu;this.virtio=a.virtio;this.head_idx=b;this.read_buffers=[];this.length_readable=this.read_buffer_offset=this.read_buffer_idx=0;this.write_buffers=[];this.length_writable=this.length_written=this.write_buffer_offset=this.write_buffer_idx=0;let c=
490
- a.desc_addr,d=0,e=a.size,f=!1;const h=this.virtio.is_feature_negotiated(28);D("<<< Descriptor chain start",2097152);do{const l=a.get_descriptor(c,b);D("descriptor: idx="+b+" addr="+z(l.addr_high,8)+":"+z(l.addr_low,8)+" len="+z(l.len,8)+" flags="+z(l.flags,4)+" next="+z(l.next,4),2097152);if(h&&l.flags&4)l.flags&1&&D("Driver bug: has set VIRTQ_DESC_F_NEXT flag in an indirect table descriptor",2097152),c=l.addr_low,d=b=0,e=l.len/16,D("start indirect",2097152);else{if(l.flags&2)f=!0,this.write_buffers.push(l),
491
- this.length_writable+=l.len;else{if(f){D("Driver bug: readonly buffer after writeonly buffer within chain",2097152);break}this.read_buffers.push(l);this.length_readable+=l.len}d++;if(d>e){D("Driver bug: descriptor chain cycle detected",2097152);break}if(l.flags&1)b=l.next;else break}}while(1);D("Descriptor chain end >>>",2097152)}gc.prototype.get_next_blob=function(a){let b=0,c=a.length;for(;c;){if(this.read_buffer_idx===this.read_buffers.length){D("Device<"+this.virtio.name+"> Read more than device-readable buffers has",
492
- 2097152);break}var d=this.read_buffers[this.read_buffer_idx];const e=d.addr_low+this.read_buffer_offset;d=d.len-this.read_buffer_offset;d>c?(d=c,this.read_buffer_offset+=c):(this.read_buffer_idx++,this.read_buffer_offset=0);a.set(this.cpu.read_blob(e,d),b);b+=d;c-=d}return b};gc.prototype.set_next_blob=function(a){let b=0,c=a.length;for(;c;){if(this.write_buffer_idx===this.write_buffers.length){D("Device<"+this.virtio.name+"> Write more than device-writable capacity",2097152);break}var d=this.write_buffers[this.write_buffer_idx];
493
- const e=d.addr_low+this.write_buffer_offset;d=d.len-this.write_buffer_offset;d>c?(d=c,this.write_buffer_offset+=c):(this.write_buffer_idx++,this.write_buffer_offset=0);this.cpu.write_blob(a.subarray(b,b+d),e);b+=d;c-=d}this.length_written+=b;return b};function hc(){this.filedata=new Map}hc.prototype.read=async function(a,b,c){m(a,"MemoryFileStorage read: sha256sum should be a non-empty string");return(a=this.filedata.get(a))?a.subarray(b,b+c):null};hc.prototype.cache=async function(a,b){m(a,"MemoryFileStorage cache: sha256sum should be a non-empty string");
494
- this.filedata.set(a,b)};hc.prototype.uncache=function(a){this.filedata.delete(a)};function ic(a,b){m(b,"ServerMemoryFileStorage: baseurl should not be empty");b.endsWith("/")||(b+="/");this.storage=a;this.baseurl=b}ic.prototype.load_from_server=function(a){return new Promise(b=>{va(this.baseurl+a,{done:async c=>{c=new Uint8Array(c);await this.cache(a,c);b(c)}})})};ic.prototype.read=async function(a,b,c){const d=await this.storage.read(a,b,c);return d?d:(await this.load_from_server(a)).subarray(b,
495
- b+c)};ic.prototype.cache=async function(a,b){return await this.storage.cache(a,b)};ic.prototype.uncache=function(a){this.storage.uncache(a)};function Y(a,b){this.inodes=[];this.events=[];this.storage=a;this.qidcounter=b||{last_qidnumber:0};this.inodedata={};this.total_size=274877906944;this.used_size=0;this.mounts=[];this.CreateDirectory("",-1)}Y.prototype.get_state=function(){let a=[];a[0]=this.inodes;a[1]=this.qidcounter.last_qidnumber;a[2]=[];for(const [b,c]of Object.entries(this.inodedata))0===
496
- (this.inodes[b].mode&16384)&&a[2].push([b,c]);a[3]=this.total_size;a[4]=this.used_size;return a=a.concat(this.mounts)};Y.prototype.set_state=function(a){this.inodes=a[0].map(b=>{const c=new jc(0);c.set_state(b);return c});this.qidcounter.last_qidnumber=a[1];this.inodedata={};for(let [b,c]of a[2])c.buffer.byteLength!==c.byteLength&&(c=c.slice()),this.inodedata[b]=c;this.total_size=a[3];this.used_size=a[4];this.mounts=a.slice(5)};Y.prototype.AddEvent=function(a,b){var c=this.inodes[a];0===c.status||
497
- 2===c.status?b():this.is_forwarder(c)?this.follow_fs(c).AddEvent(c.foreign_id,b):this.events.push({id:a,OnEvent:b})};Y.prototype.HandleEvent=function(a){var b=this.inodes[a];this.is_forwarder(b)&&this.follow_fs(b).HandleEvent(b.foreign_id);b=[];for(var c=0;c<this.events.length;c++)this.events[c].id===a?this.events[c].OnEvent():b.push(this.events[c]);this.events=b};Y.prototype.load_from_json=function(a){m(a,"Invalid fs passed to load_from_json");if(3!==a.version)throw"The filesystem JSON format has changed. Please update your fs2json (https://github.com/copy/fs2json) and recreate the filesystem JSON.";
498
- var b=a.fsroot;this.used_size=a.size;for(a=0;a<b.length;a++)this.LoadRecursive(b[a],0)};Y.prototype.LoadRecursive=function(a,b){var c=this.CreateInode();const d=a[0];c.size=a[1];c.mtime=a[2];c.ctime=c.mtime;c.atime=c.mtime;c.mode=a[3];c.uid=a[4];c.gid=a[5];var e=c.mode&61440;16384===e?(this.PushInode(c,b,d),this.LoadDir(this.inodes.length-1,a[6])):32768===e?(c.status=2,c.sha256sum=a[6],m(c.sha256sum),this.PushInode(c,b,d)):40960===e?(c.symlink=a[6],this.PushInode(c,b,d)):49152!==e&&D("Unexpected ifmt: "+
499
- z(e)+" ("+d+")")};Y.prototype.LoadDir=function(a,b){for(var c=0;c<b.length;c++)this.LoadRecursive(b[c],a)};Y.prototype.should_be_linked=function(a){return!this.is_forwarder(a)||0===a.foreign_id};Y.prototype.link_under_dir=function(a,b,c){const d=this.inodes[b],e=this.inodes[a];m(!this.is_forwarder(e),"Filesystem: Shouldn't link under fowarder parents");m(this.IsDirectory(a),"Filesystem: Can't link under non-directories");m(this.should_be_linked(d),"Filesystem: Can't link across filesystems apart from their root");
500
- m(0<=d.nlinks,"Filesystem: Found negative nlinks value of "+d.nlinks);m(!e.direntries.has(c),"Filesystem: Name '"+c+"' is already taken");e.direntries.set(c,b);d.nlinks++;this.IsDirectory(b)&&(m(!d.direntries.has(".."),"Filesystem: Cannot link a directory twice"),d.direntries.has(".")||d.nlinks++,d.direntries.set(".",b),d.direntries.set("..",a),e.nlinks++)};Y.prototype.unlink_from_dir=function(a,b){const c=this.Search(a,b),d=this.inodes[c],e=this.inodes[a];m(!this.is_forwarder(e),"Filesystem: Can't unlink from forwarders");
501
- m(this.IsDirectory(a),"Filesystem: Can't unlink from non-directories");e.direntries.delete(b)?(d.nlinks--,this.IsDirectory(c)&&(m(d.direntries.get("..")===a,"Filesystem: Found directory with bad parent id"),d.direntries.delete(".."),e.nlinks--),m(0<=d.nlinks,"Filesystem: Found negative nlinks value of "+d.nlinks)):m(!1,"Filesystem: Can't unlink non-existent file: "+b)};Y.prototype.PushInode=function(a,b,c){-1!==b?(this.inodes.push(a),a.fid=this.inodes.length-1,this.link_under_dir(b,a.fid,c)):0===
502
- this.inodes.length?(this.inodes.push(a),a.direntries.set(".",0),a.direntries.set("..",0),a.nlinks=2):(N.Debug("Error in Filesystem: Pushed inode with name = "+c+" has no parent"),N.Abort())};function jc(a){this.direntries=new Map;this.minor=this.major=this.mtime=this.atime=this.ctime=this.fid=this.gid=this.uid=this.size=this.status=0;this.symlink="";this.mode=493;this.qid={type:0,version:0,path:a};this.caps=void 0;this.nlinks=0;this.sha256sum="";this.locks=[];this.foreign_id=this.mount_id=-1}jc.prototype.get_state=
503
- function(){const a=[];a[0]=this.mode;a[1]=16384===(this.mode&61440)?[...this.direntries]:32768===(this.mode&61440)?this.sha256sum:40960===(this.mode&61440)?this.symlink:49152===(this.mode&61440)?[this.minor,this.major]:null;a[2]=this.locks;a[3]=this.status;a[4]=this.size;a[5]=this.uid;a[6]=this.gid;a[7]=this.fid;a[8]=this.ctime;a[9]=this.atime;a[10]=this.mtime;a[11]=this.qid.version;a[12]=this.qid.path;a[13]=this.nlinks;return a};jc.prototype.set_state=function(a){this.mode=a[0];if(16384===(this.mode&
504
- 61440)){this.direntries=new Map;for(const [b,c]of a[1])this.direntries.set(b,c)}else 32768===(this.mode&61440)?this.sha256sum=a[1]:40960===(this.mode&61440)?this.symlink=a[1]:49152===(this.mode&61440)&&([this.minor,this.major]=a[1]);this.locks=[];for(const b of a[2]){const c=new kc;c.set_state(b);this.locks.push(c)}this.status=a[3];this.size=a[4];this.uid=a[5];this.gid=a[6];this.fid=a[7];this.ctime=a[8];this.atime=a[9];this.mtime=a[10];this.qid.type=(this.mode&61440)>>8;this.qid.version=a[11];this.qid.path=
505
- a[12];this.nlinks=a[13]};Y.prototype.divert=function(a,b){const c=this.Search(a,b),d=this.inodes[c],e=new jc(-1);m(d,"Filesystem divert: name ("+b+") not found");m(this.IsDirectory(c)||1>=d.nlinks,"Filesystem: can't divert hardlinked file '"+b+"' with nlinks="+d.nlinks);Object.assign(e,d);const f=this.inodes.length;this.inodes.push(e);e.fid=f;this.is_forwarder(d)&&this.mounts[d.mount_id].backtrack.set(d.foreign_id,f);this.should_be_linked(d)&&(this.unlink_from_dir(a,b),this.link_under_dir(a,f,b));
506
- if(this.IsDirectory(c)&&!this.is_forwarder(d))for(const [h,l]of e.direntries)"."!==h&&".."!==h&&this.IsDirectory(l)&&this.inodes[l].direntries.set("..",f);this.inodedata[f]=this.inodedata[c];delete this.inodedata[c];d.direntries=new Map;d.nlinks=0;return f};Y.prototype.copy_inode=function(a,b){Object.assign(b,a,{fid:b.fid,direntries:b.direntries,nlinks:b.nlinks})};Y.prototype.CreateInode=function(){const a=Math.round(Date.now()/1E3),b=new jc(++this.qidcounter.last_qidnumber);b.atime=b.ctime=b.mtime=
507
- a;return b};Y.prototype.CreateDirectory=function(a,b){var c=this.inodes[b];if(0<=b&&this.is_forwarder(c))return b=c.foreign_id,a=this.follow_fs(c).CreateDirectory(a,b),this.create_forwarder(c.mount_id,a);c=this.CreateInode();c.mode=16895;0<=b&&(c.uid=this.inodes[b].uid,c.gid=this.inodes[b].gid,c.mode=this.inodes[b].mode&511|16384);c.qid.type=64;this.PushInode(c,b,a);this.NotifyListeners(this.inodes.length-1,"newdir");return this.inodes.length-1};Y.prototype.CreateFile=function(a,b){var c=this.inodes[b];
508
- if(this.is_forwarder(c))return b=c.foreign_id,a=this.follow_fs(c).CreateFile(a,b),this.create_forwarder(c.mount_id,a);c=this.CreateInode();c.uid=this.inodes[b].uid;c.gid=this.inodes[b].gid;c.qid.type=128;c.mode=this.inodes[b].mode&438|32768;this.PushInode(c,b,a);this.NotifyListeners(this.inodes.length-1,"newfile");return this.inodes.length-1};Y.prototype.CreateNode=function(a,b,c,d){var e=this.inodes[b];if(this.is_forwarder(e))return b=e.foreign_id,a=this.follow_fs(e).CreateNode(a,b,c,d),this.create_forwarder(e.mount_id,
509
- a);e=this.CreateInode();e.major=c;e.minor=d;e.uid=this.inodes[b].uid;e.gid=this.inodes[b].gid;e.qid.type=192;e.mode=this.inodes[b].mode&438;this.PushInode(e,b,a);return this.inodes.length-1};Y.prototype.CreateSymlink=function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=d.foreign_id,a=this.follow_fs(d).CreateSymlink(a,b,c),this.create_forwarder(d.mount_id,a);d=this.CreateInode();d.uid=this.inodes[b].uid;d.gid=this.inodes[b].gid;d.qid.type=160;d.symlink=c;d.mode=40960;this.PushInode(d,
510
- b,a);return this.inodes.length-1};Y.prototype.CreateTextFile=async function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=d.foreign_id,c=await this.follow_fs(d).CreateTextFile(a,b,c),this.create_forwarder(d.mount_id,c);d=this.CreateFile(a,b);b=this.inodes[d];a=new Uint8Array(c.length);b.size=c.length;for(b=0;b<c.length;b++)a[b]=c.charCodeAt(b);await this.set_data(d,a);return d};Y.prototype.CreateBinaryFile=async function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=
511
- d.foreign_id,c=await this.follow_fs(d).CreateBinaryFile(a,b,c),this.create_forwarder(d.mount_id,c);d=this.CreateFile(a,b);a=this.inodes[d];b=new Uint8Array(c.length);b.set(c);await this.set_data(d,b);a.size=c.length;return d};Y.prototype.OpenInode=function(a,b){var c=this.inodes[a];if(this.is_forwarder(c))return this.follow_fs(c).OpenInode(c.foreign_id,b);16384===(c.mode&61440)&&this.FillDirectory(a);return!0};Y.prototype.CloseInode=async function(a){var b=this.inodes[a];if(this.is_forwarder(b))return await this.follow_fs(b).CloseInode(b.foreign_id);
512
- 2===b.status&&this.storage.uncache(b.sha256sum);4===b.status&&(b.status=-1,await this.DeleteData(a))};Y.prototype.Rename=async function(a,b,c,d){if(a===c&&b===d)return 0;var e=this.Search(a,b);if(-1===e)return-2;var f=this.GetFullPath(a)+"/"+b;if(-1!==this.Search(c,d)){var h=this.Unlink(c,d);if(0>h)return h}var l=this.inodes[e],n=this.inodes[a];h=this.inodes[c];if(this.is_forwarder(n)||this.is_forwarder(h))if(this.is_forwarder(n)&&n.mount_id===h.mount_id){if(a=await this.follow_fs(n).Rename(n.foreign_id,
513
- b,h.foreign_id,d),0>a)return a}else{if(this.is_a_root(e))return D("XXX: Attempted to move mountpoint ("+b+") - skipped",4194304),-1;if(!this.IsDirectory(e)&&1<this.GetInode(e).nlinks)return D("XXX: Attempted to move hardlinked file ("+b+") across filesystems - skipped",4194304),-1;n=this.divert(a,b);const p=this.GetInode(e),q=await this.Read(n,0,p.size);this.is_forwarder(h)?(c=this.follow_fs(h),d=this.IsDirectory(n)?c.CreateDirectory(d,h.foreign_id):c.CreateFile(d,h.foreign_id),c=c.GetInode(d),this.copy_inode(p,
514
- c),this.set_forwarder(e,h.mount_id,d)):(this.delete_forwarder(l),this.copy_inode(p,l),this.link_under_dir(c,e,d));await this.ChangeSize(e,p.size);q&&q.length&&await this.Write(e,0,q.length,q);if(this.IsDirectory(e))for(const r of this.GetChildren(n))if(h=await this.Rename(n,r,e,r),0>h)return h;await this.DeleteData(n);a=this.Unlink(a,b);if(0>a)return a}else this.unlink_from_dir(a,b),this.link_under_dir(c,e,d),l.qid.version++;this.NotifyListeners(e,"rename",{oldpath:f});return 0};Y.prototype.Write=
515
- async function(a,b,c,d){this.NotifyListeners(a,"write");var e=this.inodes[a];if(this.is_forwarder(e))a=e.foreign_id,await this.follow_fs(e).Write(a,b,c,d);else{var f=await this.get_buffer(a);!f||f.length<b+c?(await this.ChangeSize(a,Math.floor(3*(b+c)/2)),e.size=b+c,f=await this.get_buffer(a)):e.size<b+c&&(e.size=b+c);d&&f.set(d.subarray(0,c),b);await this.set_data(a,f)}};Y.prototype.Read=async function(a,b,c){const d=this.inodes[a];return this.is_forwarder(d)?(a=d.foreign_id,await this.follow_fs(d).Read(a,
516
- b,c)):await this.get_data(a,b,c)};Y.prototype.Search=function(a,b){a=this.inodes[a];if(this.is_forwarder(a)){const c=a.foreign_id;b=this.follow_fs(a).Search(c,b);return-1===b?-1:this.get_forwarder(a.mount_id,b)}b=a.direntries.get(b);return void 0===b?-1:b};Y.prototype.CountUsedInodes=function(){let a=this.inodes.length;for(const {fs:b,backtrack:c}of this.mounts)a+=b.CountUsedInodes(),a-=c.size;return a};Y.prototype.CountFreeInodes=function(){let a=1048576;for(const {fs:b}of this.mounts)a+=b.CountFreeInodes();
517
- return a};Y.prototype.GetTotalSize=function(){let a=this.used_size;for(const {fs:b}of this.mounts)a+=b.GetTotalSize();return a};Y.prototype.GetSpace=function(){let a=this.total_size;for(const {fs:b}of this.mounts)a+=b.GetSpace();return this.total_size};Y.prototype.GetDirectoryName=function(a){const b=this.inodes[this.GetParent(a)];if(this.is_forwarder(b))return this.follow_fs(b).GetDirectoryName(this.inodes[a].foreign_id);if(!b)return"";for(const [c,d]of b.direntries)if(d===a)return c;m(!1,"Filesystem: Found directory inode whose parent doesn't link to it");
518
- return""};Y.prototype.GetFullPath=function(a){m(this.IsDirectory(a),"Filesystem: Cannot get full path of non-directory inode");for(var b="";0!==a;)b="/"+this.GetDirectoryName(a)+b,a=this.GetParent(a);return b.substring(1)};Y.prototype.Link=function(a,b,c){if(this.IsDirectory(b))return-1;const d=this.inodes[a],e=this.inodes[b];if(this.is_forwarder(d))return this.is_forwarder(e)&&e.mount_id===d.mount_id?this.follow_fs(d).Link(d.foreign_id,e.foreign_id,c):(D("XXX: Attempted to hardlink a file into a child filesystem - skipped",
519
- 4194304),-1);if(this.is_forwarder(e))return D("XXX: Attempted to hardlink file across filesystems - skipped",4194304),-1;this.link_under_dir(a,b,c);return 0};Y.prototype.Unlink=function(a,b){if("."===b||".."===b)return-1;const c=this.Search(a,b),d=this.inodes[c],e=this.inodes[a];if(this.is_forwarder(e))return m(this.is_forwarder(d),"Children of forwarders should be forwarders"),a=e.foreign_id,this.follow_fs(e).Unlink(a,b);if(this.IsDirectory(c)&&!this.IsEmpty(c))return-39;this.unlink_from_dir(a,b);
520
- 0===d.nlinks&&(d.status=4,this.NotifyListeners(c,"delete"));return 0};Y.prototype.DeleteData=async function(a){const b=this.inodes[a];this.is_forwarder(b)?await this.follow_fs(b).DeleteData(b.foreign_id):(b.size=0,delete this.inodedata[a])};Y.prototype.get_buffer=async function(a){const b=this.inodes[a];m(b,`Filesystem get_buffer: idx ${a} does not point to an inode`);return this.inodedata[a]?this.inodedata[a]:2===b.status?(m(b.sha256sum,"Filesystem get_data: found inode on server without sha256sum"),
521
- await this.storage.read(b.sha256sum,0,b.size)):null};Y.prototype.get_data=async function(a,b,c){const d=this.inodes[a];m(d,`Filesystem get_data: idx ${a} does not point to an inode`);return this.inodedata[a]?this.inodedata[a].subarray(b,b+c):2===d.status?(m(d.sha256sum,"Filesystem get_data: found inode on server without sha256sum"),await this.storage.read(d.sha256sum,b,c)):null};Y.prototype.set_data=async function(a,b){this.inodedata[a]=b;2===this.inodes[a].status&&(this.inodes[a].status=0,this.storage.uncache(this.inodes[a].sha256sum))};
522
- Y.prototype.GetInode=function(a){m(!isNaN(a),"Filesystem GetInode: NaN idx");m(0<=a&&a<this.inodes.length,"Filesystem GetInode: out of range idx:"+a);a=this.inodes[a];return this.is_forwarder(a)?this.follow_fs(a).GetInode(a.foreign_id):a};Y.prototype.ChangeSize=async function(a,b){var c=this.GetInode(a),d=await this.get_data(a,0,c.size);if(b!==c.size){var e=new Uint8Array(b);c.size=b;d&&e.set(d.subarray(0,Math.min(d.length,c.size)),0);await this.set_data(a,e)}};Y.prototype.SearchPath=function(a){a=
523
- a.replace("//","/");a=a.split("/");0<a.length&&0===a[a.length-1].length&&a.pop();0<a.length&&0===a[0].length&&a.shift();const b=a.length;var c=-1,d=0;let e=null;for(var f=0;f<b;f++)if(c=d,d=this.Search(c,a[f]),!e&&this.is_forwarder(this.inodes[c])&&(e="/"+a.slice(f).join("/")),-1===d)return f<b-1?{id:-1,parentid:-1,name:a[f],forward_path:e}:{id:-1,parentid:c,name:a[f],forward_path:e};return{id:d,parentid:c,name:a[f],forward_path:e}};Y.prototype.GetRecursiveList=function(a,b){if(this.is_forwarder(this.inodes[a])){const c=
524
- this.follow_fs(this.inodes[a]),d=this.inodes[a].mount_id,e=b.length;c.GetRecursiveList(this.inodes[a].foreign_id,b);for(a=e;a<b.length;a++)b[a].parentid=this.get_forwarder(d,b[a].parentid)}else for(const [c,d]of this.inodes[a].direntries)"."!==c&&".."!==c&&(b.push({parentid:a,name:c}),this.IsDirectory(d)&&this.GetRecursiveList(d,b))};Y.prototype.RecursiveDelete=function(a){var b=[];a=this.SearchPath(a);if(-1!==a.id)for(this.GetRecursiveList(a.id,b),a=b.length-1;0<=a;a--){const c=this.Unlink(b[a].parentid,
525
- b[a].name);m(0===c,"Filesystem RecursiveDelete failed at parent="+b[a].parentid+", name='"+b[a].name+"' with error code: "+-c)}};Y.prototype.DeleteNode=function(a){var b=this.SearchPath(a);-1!==b.id&&(32768===(this.inodes[b.id].mode&61440)?(a=this.Unlink(b.parentid,b.name),m(0===a,"Filesystem DeleteNode failed with error code: "+-a)):16384===(this.inodes[b.id].mode&61440)&&(this.RecursiveDelete(a),a=this.Unlink(b.parentid,b.name),m(0===a,"Filesystem DeleteNode failed with error code: "+-a)))};Y.prototype.NotifyListeners=
526
- function(){};Y.prototype.Check=function(){for(var a=1;a<this.inodes.length;a++)if(-1!==this.inodes[a].status){var b=this.GetInode(a);0>b.nlinks&&N.Debug("Error in filesystem: negative nlinks="+b.nlinks+" at id ="+a);if(this.IsDirectory(a)){b=this.GetInode(a);this.IsDirectory(a)&&0>this.GetParent(a)&&N.Debug("Error in filesystem: negative parent id "+a);for(const [c,d]of b.direntries){0===c.length&&N.Debug("Error in filesystem: inode with no name and id "+d);for(const e of c)32>e&&N.Debug("Error in filesystem: Unallowed char in filename")}}}};
527
- Y.prototype.FillDirectory=function(a){var b=this.inodes[a];if(this.is_forwarder(b))this.follow_fs(b).FillDirectory(b.foreign_id);else{var c=0;for(const d of b.direntries.keys())c+=24+rb.encode(d).length;a=this.inodedata[a]=new Uint8Array(c);b.size=c;c=0;for(const [d,e]of b.direntries)b=this.GetInode(e),c+=O.Marshall(["Q","d","b","s"],[b.qid,c+13+8+1+2+rb.encode(d).length,b.mode>>12,d],a,c)}};Y.prototype.RoundToDirentry=function(a,b){const c=this.inodedata[a];m(c,`FS directory data for dirid=${a} should be generated`);
528
- m(c.length,"FS directory should have at least an entry");if(b>=c.length)return c.length;for(a=0;;){const d=O.Unmarshall(["Q","d"],c,{offset:a})[1];if(d>b)break;a=d}return a};Y.prototype.IsDirectory=function(a){a=this.inodes[a];return this.is_forwarder(a)?this.follow_fs(a).IsDirectory(a.foreign_id):16384===(a.mode&61440)};Y.prototype.IsEmpty=function(a){a=this.inodes[a];if(this.is_forwarder(a))return this.follow_fs(a).IsDirectory(a.foreign_id);for(const b of a.direntries.keys())if("."!==b&&".."!==
529
- b)return!1;return!0};Y.prototype.GetChildren=function(a){m(this.IsDirectory(a),"Filesystem: cannot get children of non-directory inode");a=this.inodes[a];if(this.is_forwarder(a))return this.follow_fs(a).GetChildren(a.foreign_id);const b=[];for(const c of a.direntries.keys())"."!==c&&".."!==c&&b.push(c);return b};Y.prototype.GetParent=function(a){m(this.IsDirectory(a),"Filesystem: cannot get parent of non-directory inode");a=this.inodes[a];if(this.should_be_linked(a))return a.direntries.get("..");
530
- const b=this.follow_fs(a).GetParent(a.foreign_id);m(-1!==b,"Filesystem: should not have invalid parent ids");return this.get_forwarder(a.mount_id,b)};Y.prototype.PrepareCAPs=function(a){a=this.GetInode(a);if(a.caps)return a.caps.length;a.caps=new Uint8Array(20);a.caps[0]=0;a.caps[1]=0;a.caps[2]=0;a.caps[3]=2;a.caps[4]=255;a.caps[5]=255;a.caps[6]=255;a.caps[7]=255;a.caps[8]=255;a.caps[9]=255;a.caps[10]=255;a.caps[11]=255;a.caps[12]=63;a.caps[13]=0;a.caps[14]=0;a.caps[15]=0;a.caps[16]=63;a.caps[17]=
531
- 0;a.caps[18]=0;a.caps[19]=0;return a.caps.length};function lc(a){this.fs=a;this.backtrack=new Map}lc.prototype.get_state=function(){const a=[];a[0]=this.fs;a[1]=[...this.backtrack];return a};lc.prototype.set_state=function(a){this.fs=a[0];this.backtrack=new Map(a[1])};Y.prototype.set_forwarder=function(a,b,c){const d=this.inodes[a];m(0===d.nlinks,"Filesystem: attempted to convert an inode into forwarder before unlinking the inode");this.is_forwarder(d)&&this.mounts[d.mount_id].backtrack.delete(d.foreign_id);
532
- d.status=5;d.mount_id=b;d.foreign_id=c;this.mounts[b].backtrack.set(c,a)};Y.prototype.create_forwarder=function(a,b){const c=this.CreateInode(),d=this.inodes.length;this.inodes.push(c);c.fid=d;this.set_forwarder(d,a,b);return d};Y.prototype.is_forwarder=function(a){return 5===a.status};Y.prototype.is_a_root=function(a){return 0===this.GetInode(a).fid};Y.prototype.get_forwarder=function(a,b){var c=this.mounts[a];m(0<=b,"Filesystem get_forwarder: invalid foreign_id: "+b);m(c,"Filesystem get_forwarder: invalid mount number: "+
533
- a);c=c.backtrack.get(b);return void 0===c?this.create_forwarder(a,b):c};Y.prototype.delete_forwarder=function(a){m(this.is_forwarder(a),"Filesystem delete_forwarder: expected forwarder");a.status=-1;this.mounts[a.mount_id].backtrack.delete(a.foreign_id)};Y.prototype.follow_fs=function(a){const b=this.mounts[a.mount_id];m(this.is_forwarder(a),"Filesystem follow_fs: inode should be a forwarding inode");m(b,"Filesystem follow_fs: inode<id="+a.fid+"> should point to valid mounted FS");return b.fs};Y.prototype.Mount=
534
- function(a,b){m(b.qidcounter===this.qidcounter,"Cannot mount filesystem whose qid numbers aren't synchronised with current filesystem.");var c=this.SearchPath(a);if(-1===c.parentid)return D("Mount failed: parent for path not found: "+a,4194304),-2;if(-1!==c.id)return D("Mount failed: file already exists at path: "+a,4194304),-17;if(c.forward_path)return a=this.inodes[c.parentid],c=this.follow_fs(a).Mount(c.forward_path,b),0>c?c:this.get_forwarder(a.mount_id,c);a=this.mounts.length;this.mounts.push(new lc(b));
535
- b=this.create_forwarder(a,0);this.link_under_dir(c.parentid,b,c.name);return b};function kc(){this.type=2;this.start=0;this.length=Infinity;this.proc_id=-1;this.client_id=""}kc.prototype.get_state=function(){const a=[];a[0]=this.type;a[1]=this.start;a[2]=Infinity===this.length?0:this.length;a[3]=this.proc_id;a[4]=this.client_id;return a};kc.prototype.set_state=function(a){this.type=a[0];this.start=a[1];this.length=0===a[2]?Infinity:a[2];this.proc_id=a[3];this.client_id=a[4]};kc.prototype.clone=function(){const a=
536
- new kc;a.set_state(this.get_state());return a};kc.prototype.conflicts_with=function(a){return this.proc_id===a.proc_id&&this.client_id===a.client_id||2===this.type||2===a.type||1!==this.type&&1!==a.type||this.start+this.length<=a.start||a.start+a.length<=this.start?!1:!0};kc.prototype.is_alike=function(a){return a.proc_id===this.proc_id&&a.client_id===this.client_id&&a.type===this.type};kc.prototype.may_merge_after=function(a){return this.is_alike(a)&&a.start+a.length===this.start};Y.prototype.DescribeLock=
537
- function(a,b,c,d,e){m(0===a||1===a||2===a,"Filesystem: Invalid lock type: "+a);m(0<=b,"Filesystem: Invalid negative lock starting offset: "+b);m(0<c,"Filesystem: Invalid non-positive lock length: "+c);const f=new kc;f.type=a;f.start=b;f.length=c;f.proc_id=d;f.client_id=e;return f};Y.prototype.GetLock=function(a,b){a=this.inodes[a];if(this.is_forwarder(a)){var c=a.foreign_id;return this.follow_fs(a).GetLock(c,b)}for(c of a.locks)if(b.conflicts_with(c))return c.clone();return null};Y.prototype.Lock=
538
- function(a,b,c){const d=this.inodes[a];if(this.is_forwarder(d))return a=d.foreign_id,this.follow_fs(d).Lock(a,b,c);b=b.clone();if(2!==b.type&&this.GetLock(a,b))return 1;for(c=0;c<d.locks.length;c++){a=d.locks[c];m(0<a.length,"Filesystem: Found non-positive lock region length: "+a.length);m(0===a.type||1===a.type,"Filesystem: Found invalid lock type: "+a.type);m(!d.locks[c-1]||d.locks[c-1].start<=a.start,"Filesystem: Locks should be sorted by starting offset");if(a.start+a.length<=b.start)continue;
539
- if(b.start+b.length<=a.start)break;if(a.proc_id!==b.proc_id||a.client_id!==b.client_id){m(!a.conflicts_with(b),"Filesytem: Found conflicting lock region, despite already checked for conflicts");continue}var e=b.start+b.length;const f=b.start-a.start,h=a.start+a.length-e;if(0<f&&0<h&&a.type===b.type)return 0;0<f&&(a.length=f);if(0>=f&&0<h)a.start=e,a.length=h;else if(0<h){for(;c<d.locks.length&&d.locks[c].start<e;)c++;d.locks.splice(c,0,this.DescribeLock(a.type,e,h,a.proc_id,a.client_id))}else 0>=
540
- f&&(d.locks.splice(c,1),c--)}if(2!==b.type){c=b;a=!1;for(e=0;e<d.locks.length&&!(c.may_merge_after(d.locks[e])&&(d.locks[e].length+=b.length,c=d.locks[e],a=!0),b.start<=d.locks[e].start);e++);a||(d.locks.splice(e,0,c),e++);for(;e<d.locks.length;e++)if(d.locks[e].is_alike(c)){d.locks[e].may_merge_after(c)&&(c.length+=d.locks[e].length,d.locks.splice(e,1));break}}return 0};Y.prototype.read_dir=function(a){a=this.SearchPath(a);if(-1!==a.id)return a=this.GetInode(a.id),Array.from(a.direntries.keys()).filter(b=>
541
- "."!==b&&".."!==b)};Y.prototype.read_file=function(a){a=this.SearchPath(a);if(-1===a.id)return Promise.resolve(null);const b=this.GetInode(a.id);return this.Read(a.id,0,b.size)};const mc=["shared","exclusive","unlock"];function fc(a,b,c){this.fs=a;this.bus=c;this.configspace_tagname=[104,111,115,116,57,112];this.configspace_taglen=this.configspace_tagname.length;this.VERSION="9P2000.L";this.msize=this.BLOCKSIZE=8192;this.replybuffer=new Uint8Array(2*this.msize);this.replybuffersize=0;this.fids=[];
542
- this.virtio=new xb(b,{name:"virtio-9p",pci_id:48,device_id:4169,subsystem_device_id:9,common:{initial_port:43008,queues:[{size_supported:32,notify_offset:0}],features:[0,32,29,28],on_driver_ok:()=>{}},notification:{initial_port:43264,single_handler:!1,handlers:[d=>{if(0!==d)m(!1,"Virtio9P Notified for non-existent queue: "+d+" (expected queue_id of 0)");else{for(;this.virtqueue.has_request();)d=this.virtqueue.pop_request(),this.ReceiveRequest(d);this.virtqueue.notify_me_after(0)}}]},isr_status:{initial_port:42752},
543
- device_specific:{initial_port:42496,struct:[{bytes:2,name:"mount tag length",read:()=>this.configspace_taglen,write:()=>{}}].concat(Array.from(Array(254).keys()).map(d=>({bytes:1,name:"mount tag name "+d,read:()=>this.configspace_tagname[d]||0,write:()=>{}})))}});this.virtqueue=this.virtio.queues[0]}fc.prototype.get_state=function(){var a=[];a[0]=this.configspace_tagname;a[1]=this.configspace_taglen;a[2]=this.virtio;a[3]=this.VERSION;a[4]=this.BLOCKSIZE;a[5]=this.msize;a[6]=this.replybuffer;a[7]=
544
- this.replybuffersize;a[8]=this.fids.map(function(b){return[b.inodeid,b.type,b.uid,b.dbg_name]});a[9]=this.fs;return a};fc.prototype.set_state=function(a){this.configspace_tagname=a[0];this.configspace_taglen=a[1];this.virtio.set_state(a[2]);this.virtqueue=this.virtio.queues[0];this.VERSION=a[3];this.BLOCKSIZE=a[4];this.msize=a[5];this.replybuffer=a[6];this.replybuffersize=a[7];this.fids=a[8].map(function(b){return{inodeid:b[0],type:b[1],uid:b[2],dbg_name:b[3]}});this.fs.set_state(a[9])};fc.prototype.Createfid=
545
- function(a,b,c,d){return{inodeid:a,type:b,uid:c,dbg_name:d}};fc.prototype.update_dbg_name=function(a,b){for(const c of this.fids)c.inodeid===a&&(c.dbg_name=b)};fc.prototype.reset=function(){this.fids=[];this.virtio.reset()};fc.prototype.BuildReply=function(a,b,c){m(0<=c,"9P: Negative payload size");O.Marshall(["w","b","h"],[c+7,a+1,b],this.replybuffer,0);c+7>=this.replybuffer.length&&N.Debug("Error in 9p: payloadsize exceeds maximum length");this.replybuffersize=c+7};fc.prototype.SendError=function(a,
546
- b,c){b=O.Marshall(["w"],[c],this.replybuffer,7);this.BuildReply(6,a,b)};fc.prototype.SendReply=function(a){m(0<=this.replybuffersize,"9P: Negative replybuffersize");a.set_next_blob(this.replybuffer.subarray(0,this.replybuffersize));this.virtqueue.push_reply(a);this.virtqueue.flush_replies()};fc.prototype.ReceiveRequest=async function(a){var b=new Uint8Array(a.length_readable);a.get_next_blob(b);var c={offset:0},d=O.Unmarshall(["w","b","h"],b,c),e=d[0],f=d[1],h=d[2];switch(f){case 8:e=this.fs.GetTotalSize();
547
- b=this.fs.GetSpace();d=[16914839];d[1]=this.BLOCKSIZE;d[2]=Math.floor(b/d[1]);d[3]=d[2]-Math.floor(e/d[1]);d[4]=d[2]-Math.floor(e/d[1]);d[5]=this.fs.CountUsedInodes();d[6]=this.fs.CountFreeInodes();d[7]=0;d[8]=256;e=O.Marshall("wwddddddw".split(""),d,this.replybuffer,7);this.BuildReply(f,h,e);this.SendReply(a);break;case 112:case 12:d=O.Unmarshall(["w","w"],b,c);var l=d[0];c=d[1];N.Debug("[open] fid="+l+", mode="+c);b=this.fids[l].inodeid;var n=this.fs.GetInode(b);N.Debug("file open "+this.fids[l].dbg_name);
548
- d=this.fs.OpenInode(b,c);this.fs.AddEvent(this.fids[l].inodeid,function(){N.Debug("file opened "+this.fids[l].dbg_name+" tag:"+h);var t=[];t[0]=n.qid;t[1]=this.msize-24;O.Marshall(["Q","w"],t,this.replybuffer,7);this.BuildReply(f,h,17);this.SendReply(a)}.bind(this));break;case 70:d=O.Unmarshall(["w","w","s"],b,c);b=d[0];l=d[1];e=d[2];N.Debug("[link] dfid="+b+", name="+e);d=this.fs.Link(this.fids[b].inodeid,this.fids[l].inodeid,e);if(0>d){e="";-1===d?e="Operation not permitted":(e="Unknown error: "+
549
- -d,m(!1,"[link]: Unexpected error code: "+-d));this.SendError(h,e,-d);this.SendReply(a);break}this.BuildReply(f,h,0);this.SendReply(a);break;case 16:d=O.Unmarshall(["w","s","s","w"],b,c);l=d[0];e=d[1];b=d[2];d=d[3];N.Debug("[symlink] fid="+l+", name="+e+", symgt="+b+", gid="+d);b=this.fs.CreateSymlink(e,this.fids[l].inodeid,b);n=this.fs.GetInode(b);n.uid=this.fids[l].uid;n.gid=d;O.Marshall(["Q"],[n.qid],this.replybuffer,7);this.BuildReply(f,h,13);this.SendReply(a);break;case 18:d=O.Unmarshall("wswwww".split(""),
550
- b,c);l=d[0];e=d[1];c=d[2];b=d[3];var p=d[4];d=d[5];N.Debug("[mknod] fid="+l+", name="+e+", major="+b+", minor="+p);b=this.fs.CreateNode(e,this.fids[l].inodeid,b,p);n=this.fs.GetInode(b);n.mode=c;n.uid=this.fids[l].uid;n.gid=d;O.Marshall(["Q"],[n.qid],this.replybuffer,7);this.BuildReply(f,h,13);this.SendReply(a);break;case 22:d=O.Unmarshall(["w"],b,c);l=d[0];n=this.fs.GetInode(this.fids[l].inodeid);N.Debug("[readlink] fid="+l+" name="+this.fids[l].dbg_name+" target="+n.symlink);e=O.Marshall(["s"],
551
- [n.symlink],this.replybuffer,7);this.BuildReply(f,h,e);this.SendReply(a);break;case 72:d=O.Unmarshall(["w","s","w","w"],b,c);l=d[0];e=d[1];c=d[2];d=d[3];N.Debug("[mkdir] fid="+l+", name="+e+", mode="+c+", gid="+d);b=this.fs.CreateDirectory(e,this.fids[l].inodeid);n=this.fs.GetInode(b);n.mode=c|16384;n.uid=this.fids[l].uid;n.gid=d;O.Marshall(["Q"],[n.qid],this.replybuffer,7);this.BuildReply(f,h,13);this.SendReply(a);break;case 14:d=O.Unmarshall(["w","s","w","w","w"],b,c);l=d[0];e=d[1];b=d[2];c=d[3];
552
- d=d[4];this.bus.send("9p-create",[e,this.fids[l].inodeid]);N.Debug("[create] fid="+l+", name="+e+", flags="+b+", mode="+c+", gid="+d);b=this.fs.CreateFile(e,this.fids[l].inodeid);this.fids[l].inodeid=b;this.fids[l].type=1;this.fids[l].dbg_name=e;n=this.fs.GetInode(b);n.uid=this.fids[l].uid;n.gid=d;n.mode=c|32768;O.Marshall(["Q","w"],[n.qid,this.msize-24],this.replybuffer,7);this.BuildReply(f,h,17);this.SendReply(a);break;case 52:d=O.Unmarshall("wbwddws".split(""),b,c);l=d[0];b=d[2];e=0===d[4]?Infinity:
553
- d[4];e=this.fs.DescribeLock(d[1],d[3],e,d[5],d[6]);N.Debug("[lock] fid="+l+", type="+mc[e.type]+", start="+e.start+", length="+e.length+", proc_id="+e.proc_id);d=this.fs.Lock(this.fids[l].inodeid,e,b);O.Marshall(["b"],[d],this.replybuffer,7);this.BuildReply(f,h,1);this.SendReply(a);break;case 54:d=O.Unmarshall("wbddws".split(""),b,c);l=d[0];e=0===d[3]?Infinity:d[3];e=this.fs.DescribeLock(d[1],d[2],e,d[4],d[5]);N.Debug("[getlock] fid="+l+", type="+mc[e.type]+", start="+e.start+", length="+e.length+
554
- ", proc_id="+e.proc_id);d=this.fs.GetLock(this.fids[l].inodeid,e);d||(d=e,d.type=2);e=O.Marshall(["b","d","d","w","s"],[d.type,d.start,Infinity===d.length?0:d.length,d.proc_id,d.client_id],this.replybuffer,7);this.BuildReply(f,h,e);this.SendReply(a);break;case 24:d=O.Unmarshall(["w","d"],b,c);l=d[0];n=this.fs.GetInode(this.fids[l].inodeid);N.Debug("[getattr]: fid="+l+" name="+this.fids[l].dbg_name+" request mask="+d[1]);if(!n||4===n.status){N.Debug("getattr: unlinked");this.SendError(h,"No such file or directory",
555
- 2);this.SendReply(a);break}d[0]=d[1];d[1]=n.qid;d[2]=n.mode;d[3]=n.uid;d[4]=n.gid;d[5]=n.nlinks;d[6]=n.major<<8|n.minor;d[7]=n.size;d[8]=this.BLOCKSIZE;d[9]=Math.floor(n.size/512+1);d[10]=n.atime;d[11]=0;d[12]=n.mtime;d[13]=0;d[14]=n.ctime;d[15]=0;d[16]=0;d[17]=0;d[18]=0;d[19]=0;O.Marshall("dQwwwddddddddddddddd".split(""),d,this.replybuffer,7);this.BuildReply(f,h,153);this.SendReply(a);break;case 26:d=O.Unmarshall("wwwwwddddd".split(""),b,c);l=d[0];n=this.fs.GetInode(this.fids[l].inodeid);N.Debug("[setattr]: fid="+
556
- l+" request mask="+d[1]+" name="+this.fids[l].dbg_name);d[1]&1&&(n.mode=d[2]);d[1]&2&&(n.uid=d[3]);d[1]&4&&(n.gid=d[4]);d[1]&16&&(n.atime=Math.floor((new Date).getTime()/1E3));d[1]&32&&(n.mtime=Math.floor((new Date).getTime()/1E3));d[1]&64&&(n.ctime=Math.floor((new Date).getTime()/1E3));d[1]&128&&(n.atime=d[6]);d[1]&256&&(n.mtime=d[8]);d[1]&8&&await this.fs.ChangeSize(this.fids[l].inodeid,d[5]);this.BuildReply(f,h,0);this.SendReply(a);break;case 50:d=O.Unmarshall(["w","d"],b,c);l=d[0];this.BuildReply(f,
557
- h,0);this.SendReply(a);break;case 40:case 116:d=O.Unmarshall(["w","d","w"],b,c);l=d[0];e=d[1];p=d[2];n=this.fs.GetInode(this.fids[l].inodeid);40===f&&N.Debug("[treaddir]: fid="+l+" offset="+e+" count="+p);116===f&&N.Debug("[read]: fid="+l+" ("+this.fids[l].dbg_name+") offset="+e+" count="+p+" fidtype="+this.fids[l].type);if(!n||4===n.status){N.Debug("read/treaddir: unlinked");this.SendError(h,"No such file or directory",2);this.SendReply(a);break}if(2===this.fids[l].type)for(n.caps.length<e+p&&(p=
558
- n.caps.length-e),d=0;d<p;d++)this.replybuffer[11+d]=n.caps[e+d];else this.fs.OpenInode(this.fids[l].inodeid,void 0),d=this.fids[l].inodeid,p=Math.min(p,this.replybuffer.length-11),n.size<e+p?p=n.size-e:40===f&&(p=this.fs.RoundToDirentry(d,e+p)-e),e>n.size&&(p=0),this.bus.send("9p-read-start",[this.fids[l].dbg_name]),d=await this.fs.Read(d,e,p),this.bus.send("9p-read-end",[this.fids[l].dbg_name,p]),d&&this.replybuffer.set(d,11);O.Marshall(["w"],[p],this.replybuffer,7);this.BuildReply(f,h,4+p);this.SendReply(a);
559
- break;case 118:d=O.Unmarshall(["w","d","w"],b,c);l=d[0];e=d[1];p=d[2];d=this.fids[l].dbg_name;N.Debug("[write]: fid="+l+" ("+d+") offset="+e+" count="+p+" fidtype="+this.fids[l].type);if(2===this.fids[l].type){this.SendError(h,"Setxattr not supported",95);this.SendReply(a);break}else await this.fs.Write(this.fids[l].inodeid,e,p,b.subarray(c.offset));this.bus.send("9p-write-end",[d,p]);O.Marshall(["w"],[p],this.replybuffer,7);this.BuildReply(f,h,4);this.SendReply(a);break;case 74:d=O.Unmarshall(["w",
560
- "s","w","s"],b,c);e=d[0];b=d[1];c=d[2];d=d[3];N.Debug("[renameat]: oldname="+b+" newname="+d);d=await this.fs.Rename(this.fids[e].inodeid,b,this.fids[c].inodeid,d);if(0>d){e="";-2===d?e="No such file or directory":-1===d?e="Operation not permitted":-39===d?e="Directory not empty":(e="Unknown error: "+-d,m(!1,"[renameat]: Unexpected error code: "+-d));this.SendError(h,e,-d);this.SendReply(a);break}this.BuildReply(f,h,0);this.SendReply(a);break;case 76:d=O.Unmarshall(["w","s","w"],b,c);c=d[0];e=d[1];
561
- b=d[2];N.Debug("[unlink]: dirfd="+c+" name="+e+" flags="+b);l=this.fs.Search(this.fids[c].inodeid,e);if(-1===l){this.SendError(h,"No such file or directory",2);this.SendReply(a);break}d=this.fs.Unlink(this.fids[c].inodeid,e);if(0>d){e="";-39===d?e="Directory not empty":-1===d?e="Operation not permitted":(e="Unknown error: "+-d,m(!1,"[unlink]: Unexpected error code: "+-d));this.SendError(h,e,-d);this.SendReply(a);break}this.BuildReply(f,h,0);this.SendReply(a);break;case 100:d=O.Unmarshall(["w","s"],
562
- b,c);N.Debug("[version]: msize="+d[0]+" version="+d[1]);this.msize!==d[0]&&(this.msize=d[0],this.replybuffer=new Uint8Array(Math.min(16777216,2*this.msize)));e=O.Marshall(["w","s"],[this.msize,this.VERSION],this.replybuffer,7);this.BuildReply(f,h,e);this.SendReply(a);break;case 104:d=O.Unmarshall(["w","w","s","s","w"],b,c);l=d[0];e=d[4];N.Debug("[attach]: fid="+l+" afid="+z(d[1])+" uname="+d[2]+" aname="+d[3]);this.fids[l]=this.Createfid(0,1,e,"");n=this.fs.GetInode(this.fids[l].inodeid);O.Marshall(["Q"],
563
- [n.qid],this.replybuffer,7);this.BuildReply(f,h,13);this.SendReply(a);this.bus.send("9p-attach");break;case 108:d=O.Unmarshall(["h"],b,c);N.Debug("[flush] "+h);this.BuildReply(f,h,0);this.SendReply(a);break;case 110:d=O.Unmarshall(["w","w","h"],b,c);l=d[0];p=d[1];var q=d[2];N.Debug("[walk]: fid="+d[0]+" nwfid="+d[1]+" nwname="+q);if(0===q){this.fids[p]=this.Createfid(this.fids[l].inodeid,1,this.fids[l].uid,this.fids[l].dbg_name);O.Marshall(["h"],[0],this.replybuffer,7);this.BuildReply(f,h,2);this.SendReply(a);
564
- break}e=[];for(d=0;d<q;d++)e.push("s");c=O.Unmarshall(e,b,c);b=this.fids[l].inodeid;e=9;var r=0;N.Debug("walk in dir "+this.fids[l].dbg_name+" to: "+c.toString());for(d=0;d<q;d++){b=this.fs.Search(b,c[d]);if(-1===b){N.Debug("Could not find: "+c[d]);break}e+=O.Marshall(["Q"],[this.fs.GetInode(b).qid],this.replybuffer,e);r++;this.fids[p]=this.Createfid(b,1,this.fids[l].uid,c[d])}O.Marshall(["h"],[r],this.replybuffer,7);this.BuildReply(f,h,e-7);this.SendReply(a);break;case 120:d=O.Unmarshall(["w"],b,
565
- c);N.Debug("[clunk]: fid="+d[0]);this.fids[d[0]]&&0<=this.fids[d[0]].inodeid&&(await this.fs.CloseInode(this.fids[d[0]].inodeid),this.fids[d[0]].inodeid=-1,this.fids[d[0]].type=-1);this.BuildReply(f,h,0);this.SendReply(a);break;case 32:d=O.Unmarshall(["w","s","d","w"],b,c);l=d[0];e=d[1];c=d[2];b=d[3];N.Debug("[txattrcreate]: fid="+l+" name="+e+" attr_size="+c+" flags="+b);this.fids[l].type=2;this.BuildReply(f,h,0);this.SendReply(a);break;case 30:d=O.Unmarshall(["w","w","s"],b,c);l=d[0];e=d[2];N.Debug("[xattrwalk]: fid="+
566
- d[0]+" newfid="+d[1]+" name="+d[2]);this.SendError(h,"Setxattr not supported",95);this.SendReply(a);break;default:N.Debug("Error in Virtio9p: Unknown id "+f+" received"),N.Abort()}};K.prototype.mmap_read8=function(a){a=this.memory_map_read8[a>>>17](a);m(0<=a&&255>=a);return a};K.prototype.mmap_write8=function(a,b){m(0<=b&&255>=b);this.memory_map_write8[a>>>17](a,b)};K.prototype.mmap_read16=function(a){var b=this.memory_map_read8[a>>>17];a=b(a)|b(a+1|0)<<8;m(0<=a&&65535>=a);return a};K.prototype.mmap_write16=
567
- function(a,b){var c=this.memory_map_write8[a>>>17];m(0<=b&&65535>=b);c(a,b&255);c(a+1|0,b>>8)};K.prototype.mmap_read32=function(a){return this.memory_map_read32[a>>>17](a)};K.prototype.mmap_write32=function(a,b){this.memory_map_write32[a>>>17](a,b)};K.prototype.mmap_write64=function(a,b,c){var d=a>>>17;m(d===a+7>>>17);d=this.memory_map_write32[d];d(a,b);d(a+4,c)};K.prototype.mmap_write128=function(a,b,c,d,e){var f=a>>>17;m(f===a+12>>>17);f=this.memory_map_write32[f];f(a,b);f(a+4,c);f(a+8,d);f(a+12,
568
- e)};K.prototype.write_blob=function(a,b){m(a&&0<=a.length);a.length&&(m(!this.in_mapped_range(b)),m(!this.in_mapped_range(b+a.length-1)),this.jit_dirty_cache(b,b+a.length),this.mem8.set(a,b))};K.prototype.read_blob=function(a,b){b&&(m(!this.in_mapped_range(a)),m(!this.in_mapped_range(a+b-1)));return this.mem8.subarray(a,a+b)};function nc(a){this.message=a}nc.prototype=Error();const oc={Uint8Array,Int8Array,Uint16Array,Int16Array,Uint32Array,Int32Array,Float32Array,Float64Array};function pc(a,b){if("object"!==
569
- typeof a||null===a)return m("function"!==typeof a),a;if(Array.isArray(a))return a.map(f=>pc(f,b));a.constructor===Object&&(console.log(a),m(a.constructor!==Object,"Expected non-object"));if(a.BYTES_PER_ELEMENT){var c=new Uint8Array(a.buffer,a.byteOffset,a.length*a.BYTES_PER_ELEMENT);a=a.constructor.name.replace("bound ","");m(oc[a]);return{__state_type__:a,buffer_id:b.push(c)-1}}!a.get_state&&console.log("Object without get_state: ",a);c=a.get_state();a=[];for(var d=0;d<c.length;d++){var e=c[d];m("function"!==
570
- typeof e);a[d]=pc(e,b)}return a}function qc(a,b){if("object"!==typeof a||null===a)return m("function"!==typeof a),a;if(Array.isArray(a)){for(var c=0;c<a.length;c++)a[c]=qc(a[c],b);return a}c=a.__state_type__;m(void 0!==c);const d=oc[c];m(d,"Unkown type: "+c);return new d(b[a.buffer_id])}K.prototype.save_state=function(){for(var a=[],b=pc(this,a),c=[],d=0,e=0;e<a.length;e++){var f=a[e].byteLength;c[e]={offset:d,length:f};d+=f;d=d+3&-4}b=JSON.stringify({buffer_infos:c,state:b});b=(new TextEncoder).encode(b);
571
- e=16+b.length;e=e+3&-4;var h=e+d;d=new ArrayBuffer(h);var l=new Int32Array(d,0,4);(new Uint8Array(d,16,b.length)).set(b);f=new Uint8Array(d,e);l[0]=-2039052682;l[1]=6;l[2]=h;l[3]=b.length;for(e=0;e<a.length;e++)h=a[e],m(h.constructor===Uint8Array),f.set(h,c[e].offset);D("State: json size "+(b.byteLength>>10)+"k");D("State: Total buffers size "+(f.byteLength>>10)+"k");return d};K.prototype.restore_state=function(a){function b(t,v){const y=t.length;if(16>y)throw new nc("Invalid length: "+y);t=new Int32Array(t.buffer,
572
- t.byteOffset,4);if(-2039052682!==t[0])throw new nc("Invalid header: "+z(t[0]>>>0));if(6!==t[1])throw new nc("Version mismatch: dump="+t[1]+" we=6");if(v&&t[2]!==y)throw new nc("Length doesn't match header: real="+y+" header="+t[2]);return t[3]}function c(t){t=(new TextDecoder).decode(t);return JSON.parse(t)}a=new Uint8Array(a);if(4247762216===(new Uint32Array(a.buffer,0,1))[0]){var d=this.zstd_create_ctx(a.length);(new Uint8Array(this.wasm_memory.buffer,this.zstd_get_src_ptr(d),a.length)).set(a);
573
- var e=this.zstd_read(d,16),f=new Uint8Array(this.wasm_memory.buffer,e,16),h=b(f,!1);this.zstd_read_free(e,16);e=this.zstd_read(d,h);f=new Uint8Array(this.wasm_memory.buffer,e,h);f=c(f);this.zstd_read_free(e,h);e=f.state;var l=f.buffer_infos;f=[];h=16+h;for(var n of l){l=(h+3&-4)-h;if(1048576<n.length){var p=this.zstd_read(d,l);this.zstd_read_free(p,l);p=new Uint8Array(n.length);f.push(p.buffer);for(var q=0;q<n.length;){var r=n.length-q;m(0<=r);r=Math.min(r,1048576);const t=this.zstd_read(d,r);p.set(new Uint8Array(this.wasm_memory.buffer,
574
- t,r),q);this.zstd_read_free(t,r);q+=r}}else p=this.zstd_read(d,l+n.length),q=p+l,f.push(this.wasm_memory.buffer.slice(q,q+n.length)),this.zstd_read_free(p,l+n.length);h+=l+n.length}e=qc(e,f);this.set_state(e);this.zstd_free_ctx(d)}else{d=b(a,!0);if(0>d||d+12>=a.length)throw new nc("Invalid info block length: "+d);n=a.subarray(16,16+d);e=c(n);n=e.state;e=e.buffer_infos;let t=16+d;t=t+3&-4;d=e.map(v=>{const y=t+v.offset;return a.buffer.slice(y,y+v.length)});n=qc(n,d);this.set_state(n)}};K.prototype.debug_init=
575
- function(){function a(p){for(var q=e.protected_mode[0]?"prot":"real",r=e.get_eflags(),t=e.getiopl(),v=e.cpl[0],y=z(e.sreg[1],4)+":"+z(e.get_real_eip()>>>0,8),C=z(e.sreg[2],4)+":"+z(e.reg32[0]>>>0,8),u=e.is_32[0]?"32":"16",B=e.flags[0]&512?1:0,w={[1]:"c",[4]:"p",[16]:"a",[64]:"z",[128]:"s",[256]:"t",[512]:"i",[1024]:"d",[2048]:"o"},G="",L=0;16>L;L++)w[1<<L]&&(G=r&1<<L?G+w[1<<L]:G+" ");return"mode="+q+"/"+u+" paging="+ +(0!==(e.cr[0]&-2147483648))+" pae="+ +(0!==(e.cr[4]&32))+" iopl="+t+" cpl="+v+" if="+
576
- B+" cs:eip="+y+" cs_off="+z(e.get_seg_cs()>>>0,8)+" flgs="+z(e.get_eflags()>>>0,6)+" ("+G+") ss:esp="+C+" ssize="+ +e.stack_size_32[0]+(p?" in "+p:"")}function b(){for(var p={eax:0,ecx:1,edx:2,ebx:3,esp:4,ebp:5,esi:6,edi:7},q="eax ecx edx ebx esp ebp esi edi".split(" "),r="",t="",v=0;4>v;v++)r+=q[v]+"="+z(e.reg32[p[q[v]]]>>>0,8)+" ",t+=q[v+4]+"="+z(e.reg32[p[q[v+4]]]>>>0,8)+" ";r+=" ds="+z(e.sreg[3],4)+" es="+z(e.sreg[0],4)+" fs="+z(e.sreg[4],4);t+=" gs="+z(e.sreg[5],4)+" cs="+z(e.sreg[1],4)+" ss="+
577
- z(e.sreg[2],4);return[r,t]}function c(p,q,r){if(!(p&1))return!1;var t=128===(p&128);return{size:t,global:256===(p&256),accessed:32===(p&32),dirty:64===(p&64),cache_disable:16===(p&16),user:4===(p&4),read_write:2===(p&2),address:(t&&!r?p&(q?4292870144:4290772992):p&4294963200)>>>0}}function d(p,q,r){for(var t=q?512:1024,v=q?8:4,y=q?21:22,C=0;C<t;C++){var u=e.read32s(p+C*v),B=c(u,q,!0);if(B)if(u="",u+=B.size?"S ":" ",u+=B.accessed?"A ":" ",u+=B.cache_disable?"Cd ":" ",u+=B.user?"U ":" ",u+=B.read_write?
578
- "Rw ":" ",B.size)D("=== "+z(r+(C<<y)>>>0,8)+" -> "+z(B.address>>>0,8)+" | "+u);else{D("=== "+z(r+(C<<y)>>>0,8)+" | "+u);for(var w=0;w<t;w++){var G=B.address+w*v;u=e.read32s(G);var L=c(u,q,!1);L&&(u="",u+=L.cache_disable?"Cd ":" ",u+=L.user?"U ":" ",u+=L.read_write?"Rw ":" ",u+=L.global?"G ":" ",u+=L.accessed?"A ":" ",u+=L.dirty?"Di ":" ",D("# "+z(r+(C<<y|w<<12)>>>0,8)+" -> "+z(L.address,8)+" | "+u+" (at "+z(G,8)+")"))}}}}var e=this,f={};this.debug=f;f.init=function(){function p(r){10===
579
- r?(D(q,4096),q=""):q+=String.fromCharCode(r)}if(e.io){var q="";e.io.register_write(1026,this,p);e.io.register_write(1280,this,p)}};f.get_regs_short=b;f.dump_regs=function(){var p=b();D(p[0],2);D(p[1],2)};f.get_state=a;f.dump_state=function(p){D(a(p),2)};f.dump_stack=function(p,q){var r=e.reg32[4];D("========= STACK ==========");if(q>=p||void 0===q)p=5,q=-5;for(;p>q;p--){var t=" ";p||(t="=> ");t+=z(p,2)+" | ";D(t+z(r+4*p,8)+" | "+z(e.read32s(r+4*p)>>>0))}};f.dump_page_structures=function(){if(e.cr[4]&
580
- 32){D("PAE enabled");for(var p=0;4>p;p++){var q=e.read32s(e.cr[3]+8*p);q&1&&d(q&4294963200,!0,p<<30)}}else D("PAE disabled"),d(e.cr[3],!1,0)};f.dump_gdt_ldt=function(){function p(q,r){for(var t=0;t<r;t+=8,q+=8){var v=e.read16(q+2)|e.read8(q+4)<<16|e.read8(q+7)<<24,y=e.read16(q)|(e.read8(q+6)&15)<<16,C=e.read8(q+5),u=e.read8(q+6)>>4,B="",w=C>>5&3;B=C&128?B+" P ":B+"NP ";C&16?(B=u&4?B+"32b ":B+"16b ",C&8?(B+="X ",C&4&&(B+="C ")):B+="R ",B+="RW "):B+="sys: "+z(C&15);u&8&&(y=y<<12|4095);D(z(t&-8,4)+" "+
581
- z(v>>>0,8)+" ("+z(y>>>0,8)+" bytes) "+B+"; dpl = "+w+", a = "+C.toString(2)+", f = "+u.toString(2))}}D("gdt: (len = "+z(e.gdtr_size[0])+")");p(e.translate_address_system_read(e.gdtr_offset[0]),e.gdtr_size[0]);D("\nldt: (len = "+z(e.segment_limits[7])+")");p(e.translate_address_system_read(e.segment_offsets[7]),e.segment_limits[7])};f.dump_idt=function(){for(var p=0;p<e.idtr_size[0];p+=8){var q=e.translate_address_system_read(e.idtr_offset[0]+p),r=e.read16(q)|e.read16(q+6)<<16,t=e.read16(q+2);q=e.read8(q+
582
- 5);var v=q>>5&3;var y=5===(q&31)?"task gate ":14===(q&31)?"intr gate ":15===(q&31)?"trap gate ":"invalid ";y=q&128?y+" P":y+"NP";D(z(p>>3,4)+" "+z(r>>>0,8)+", "+z(t,4)+"; "+y+"; dpl = "+v+", t = "+q.toString(2))}};f.get_memory_dump=function(p,q){void 0===p?(p=0,q=e.memory_size[0]):void 0===q&&(q=p,p=0);return e.mem8.slice(p,p+q).buffer};f.memory_hex_dump=function(p,q){q=q||64;for(var r,t,v=0;v<q>>4;v++){r=z(p+(v<<4),5)+" ";for(var y=0;16>y;y++)t=e.read8(p+(v<<4)+y),r+=z(t,2)+" ";r+=" ";for(y=
583
- 0;16>y;y++)t=e.read8(p+(v<<4)+y),r+=33>t||126<t?".":String.fromCharCode(t);D(r)}};f.used_memory_dump=function(){for(var p=e.memory_size[0]/128/16|0,q,r=0;16>r;r++){q=z(128*r*p,8)+" | ";for(var t=0;128>t;t++)q+=0<e.mem32s[(128*r+t)*p]?"X":" ";D(q)}};f.debug_interrupt=function(){};let h,l;f.dump_code=function(p,q,r){if(!l){if(void 0===h&&(h="function"===typeof require?require("./capstone-x86.min.js"):window.cs,void 0===h)){D("Warning: Missing capstone library, disassembly not available");return}l=[new h.Capstone(h.ARCH_X86,
584
- h.MODE_16),new h.Capstone(h.ARCH_X86,h.MODE_32)]}try{l[p].disasm(q,r).forEach(function(t){D(z(t.address>>>0)+": "+da(t.bytes.map(v=>z(v,2).slice(-2)).join(" "),20)+" "+t.mnemonic+" "+t.op_str)}),D("")}catch(t){D("Could not disassemble: "+Array.from(q).map(v=>z(v,2)).join(" "))}};let n;f.dump_wasm=function(p){if(void 0===n&&(n="function"===typeof require?require("./libwabt.cjs"):new window.WabtModule,void 0===n)){D("Warning: Missing libwabt, wasm dump not available");return}p=p.slice();try{var q=n.readWasm(p,
585
- {readDebugNames:!1});q.generateNames();q.applyNames();const v=q.toText({foldExprs:!0,inlineExport:!0});D(v)}catch(v){var r=new Blob([p]),t=document.createElement("a");t.download="failed.wasm";t.href=window.URL.createObjectURL(r);t.dataset.downloadurl=["application/octet-stream",t.download,t.href].join(":");t.click();window.URL.revokeObjectURL(t.src);console.log(v.toString())}finally{q&&q.destroy()}}};const rc="undefined"!==typeof window&&0<=window.navigator.platform.toString().toLowerCase().search("win");
586
- function sc(a){function b(w){return w.shiftKey&&w.ctrlKey&&(73===w.keyCode||74===w.keyCode||75===w.keyCode)||!v.emu_enabled?!1:w.target?w.target.classList.contains("phone_keyboard")||"INPUT"!==w.target.nodeName&&"TEXTAREA"!==w.target.nodeName:!0}function c(w){!w.altKey&&p[56]&&l(56,!1);return f(w,!1)}function d(w){!w.altKey&&p[56]&&l(56,!1);return f(w,!0)}function e(){for(var w=Object.keys(p),G,L=0;L<w.length;L++)G=+w[L],p[G]&&l(G,!1);p={}}function f(w,G){if(v.bus&&b(w)){w.preventDefault&&w.preventDefault();
587
- if(rc&&(q&&(clearTimeout(t),w.getModifierState&&w.getModifierState("AltGraph")&&r===G&&"ControlLeft"===q.code&&"AltRight"===w.code||h(q,r),q=null),"ControlLeft"===w.code))return q=w,r=G,t=setTimeout(()=>{h(q,r);q=null},10),!1;h(w,G);return!1}}function h(w,G){a:{if(void 0!==w.code){var L=B[w.code];if(void 0!==L)break a}L=y[w.keyCode]}L?l(L,G,w.repeat):console.log("Missing char in map: keyCode="+(w.keyCode||-1).toString(16)+" code="+w.code)}function l(w,G,L){if(G)p[w]&&!L&&l(w,!1);else if(!p[w])return;
588
- (p[w]=G)||(w|=128);255<w?(n(w>>8),n(w&255)):n(w)}function n(w){v.bus.send("keyboard-code",w)}var p={},q=null,r=!1,t=0,v=this;this.emu_enabled=!0;var y=new Uint16Array([0,0,0,0,0,0,0,0,14,15,0,0,0,28,0,0,42,29,56,0,58,0,0,0,0,0,0,1,0,0,0,0,57,57417,57425,57423,57415,57419,57416,57421,80,0,0,0,0,82,83,0,11,2,3,4,5,6,7,8,9,10,0,39,0,13,0,0,0,30,48,46,32,18,33,34,35,23,36,37,38,50,49,24,25,16,19,31,20,22,47,17,45,21,44,57435,57436,57437,0,0,82,79,80,81,75,76,77,71,72,73,0,0,0,0,0,0,59,60,61,62,63,64,
589
- 65,66,67,68,87,88,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0,0,39,13,51,12,52,53,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,43,27,40,0,57435,57400,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),C={8:8,10:13,32:32,39:222,44:188,45:189,46:190,47:191,48:48,49:49,50:50,51:51,52:52,53:53,54:54,55:55,56:56,57:57,59:186,61:187,91:219,92:220,93:221,96:192,97:65,98:66,99:67,100:68,101:69,102:70,103:71,104:72,105:73,106:74,107:75,
590
- 108:76,109:77,110:78,111:79,112:80,113:81,114:82,115:83,116:84,117:85,118:86,119:87,120:88,121:89,122:90},u={33:49,34:222,35:51,36:52,37:53,38:55,40:57,41:48,42:56,43:187,58:186,60:188,62:190,63:191,64:50,65:65,66:66,67:67,68:68,69:69,70:70,71:71,72:72,73:73,74:74,75:75,76:76,77:77,78:78,79:79,80:80,81:81,82:82,83:83,84:84,85:85,86:86,87:87,88:88,89:89,90:90,94:54,95:189,123:219,124:220,125:221,126:192},B={Escape:1,Digit1:2,Digit2:3,Digit3:4,Digit4:5,Digit5:6,Digit6:7,Digit7:8,Digit8:9,Digit9:10,
591
- Digit0:11,Minus:12,Equal:13,Backspace:14,Tab:15,KeyQ:16,KeyW:17,KeyE:18,KeyR:19,KeyT:20,KeyY:21,KeyU:22,KeyI:23,KeyO:24,KeyP:25,BracketLeft:26,BracketRight:27,Enter:28,ControlLeft:29,KeyA:30,KeyS:31,KeyD:32,KeyF:33,KeyG:34,KeyH:35,KeyJ:36,KeyK:37,KeyL:38,Semicolon:39,Quote:40,Backquote:41,ShiftLeft:42,Backslash:43,KeyZ:44,KeyX:45,KeyC:46,KeyV:47,KeyB:48,KeyN:49,KeyM:50,Comma:51,Period:52,Slash:53,IntlRo:53,ShiftRight:54,NumpadMultiply:55,AltLeft:56,Space:57,CapsLock:58,F1:59,F2:60,F3:61,F4:62,F5:63,
592
- F6:64,F7:65,F8:66,F9:67,F10:68,NumLock:69,ScrollLock:70,Numpad7:71,Numpad8:72,Numpad9:73,NumpadSubtract:74,Numpad4:75,Numpad5:76,Numpad6:77,NumpadAdd:78,Numpad1:79,Numpad2:80,Numpad3:81,Numpad0:82,NumpadDecimal:83,IntlBackslash:86,F11:87,F12:88,NumpadEnter:57372,ControlRight:57373,NumpadDivide:57397,AltRight:57400,Home:57415,ArrowUp:57416,PageUp:57417,ArrowLeft:57419,ArrowRight:57421,End:57423,ArrowDown:57424,PageDown:57425,Insert:57426,Delete:57427,OSLeft:57435,OSRight:57436,ContextMenu:57437};this.bus=
593
- a;this.destroy=function(){"undefined"!==typeof window&&(window.removeEventListener("keyup",c,!1),window.removeEventListener("keydown",d,!1),window.removeEventListener("blur",e,!1))};this.init=function(){"undefined"!==typeof window&&(this.destroy(),window.addEventListener("keyup",c,!1),window.addEventListener("keydown",d,!1),window.addEventListener("blur",e,!1))};this.init();this.simulate_press=function(w){w={keyCode:w};f(w,!0);f(w,!1)};this.simulate_char=function(w){var G=w.charCodeAt(0);G in C?this.simulate_press(C[G]):
594
- G in u?(n(42),this.simulate_press(u[G]),n(170)):console.log("ascii -> keyCode not found: ",G,w)}}function tc(a,b){function c(u){if(!C.enabled||!C.emu_enabled)return!1;var B=b||document.body,w;if(!(w=document.pointerLockElement))a:{for(u=u.target;u.parentNode;){if(u===B){w=!0;break a}u=u.parentNode}w=!1}return w}function d(u){c(u)&&(u=u.changedTouches)&&u.length&&(u=u[u.length-1],v=u.clientX,y=u.clientY)}function e(){if(q||t||r)C.bus.send("mouse-click",[!1,!1,!1]),q=t=r=!1}function f(u){if(C.bus&&
595
- c(u)&&C.is_running){var B=0,w=0,G=u.changedTouches;G?G.length&&(G=G[G.length-1],B=G.clientX-v,w=G.clientY-y,v=G.clientX,y=G.clientY,u.preventDefault()):"number"===typeof u.movementX?(B=u.movementX,w=u.movementY):"number"===typeof u.webkitMovementX?(B=u.webkitMovementX,w=u.webkitMovementY):"number"===typeof u.mozMovementX?(B=u.mozMovementX,w=u.mozMovementY):(B=u.clientX-v,w=u.clientY-y,v=u.clientX,y=u.clientY);C.bus.send("mouse-delta",[.15*B,-(.15*w)]);b&&C.bus.send("mouse-absolute",[u.pageX-b.offsetLeft,
596
- u.pageY-b.offsetTop,b.offsetWidth,b.offsetHeight])}}function h(u){c(u)&&n(u,!0)}function l(u){c(u)&&n(u,!1)}function n(u,B){C.bus&&(1===u.which?q=B:2===u.which?t=B:3===u.which?r=B:D("Unknown event.which: "+u.which),C.bus.send("mouse-click",[q,t,r]),u.preventDefault())}function p(u){if(c(u)){var B=u.wheelDelta||-u.detail;0>B?B=-1:0<B&&(B=1);C.bus.send("mouse-wheel",[B,0]);u.preventDefault()}}var q=!1,r=!1,t=!1,v=0,y=0,C=this;this.enabled=!1;this.emu_enabled=!0;this.bus=a;this.bus.register("mouse-enable",
597
- function(u){this.enabled=u},this);this.is_running=!1;this.bus.register("emulator-stopped",function(){this.is_running=!1},this);this.bus.register("emulator-started",function(){this.is_running=!0},this);this.destroy=function(){"undefined"!==typeof window&&(window.removeEventListener("touchstart",d,!1),window.removeEventListener("touchend",e,!1),window.removeEventListener("touchmove",f,!1),window.removeEventListener("mousemove",f,!1),window.removeEventListener("mousedown",h,!1),window.removeEventListener("mouseup",
598
- l,!1),window.removeEventListener("wheel",p,{passive:!1}))};this.init=function(){"undefined"!==typeof window&&(this.destroy(),window.addEventListener("touchstart",d,!1),window.addEventListener("touchend",e,!1),window.addEventListener("touchmove",f,!1),window.addEventListener("mousemove",f,!1),window.addEventListener("mousedown",h,!1),window.addEventListener("mouseup",l,!1),window.addEventListener("wheel",p,{passive:!1}))};this.init()}function uc(a){if("undefined"!==typeof window)if(window.AudioContext||
599
- window.webkitAudioContext){var b=window.AudioWorklet?vc:wc;this.bus=a;this.audio_context=window.AudioContext?new AudioContext:new webkitAudioContext;this.mixer=new xc(a,this.audio_context);this.pcspeaker=new yc(a,this.audio_context,this.mixer);this.dac=new b(a,this.audio_context,this.mixer);this.pcspeaker.start();a.register("emulator-stopped",function(){this.audio_context.suspend()},this);a.register("emulator-started",function(){this.audio_context.resume()},this);a.register("speaker-confirm-initialized",
600
- function(){a.send("speaker-has-initialized")},this);a.send("speaker-has-initialized")}else console.warn("Web browser doesn't support Web Audio API")}uc.prototype.destroy=function(){this.audio_context&&this.audio_context.close();this.audio_context=null;this.dac&&this.dac.node_processor&&this.dac.node_processor.port.close();this.dac=null};function xc(a,b){function c(d){return function(e){d.gain.setValueAtTime(e,this.audio_context.currentTime)}}this.audio_context=b;this.sources=new Map;this.gain_right=
601
- this.gain_left=this.volume_right=this.volume_left=this.volume_both=1;this.node_treble_left=this.audio_context.createBiquadFilter();this.node_treble_right=this.audio_context.createBiquadFilter();this.node_treble_left.type="highshelf";this.node_treble_right.type="highshelf";this.node_treble_left.frequency.setValueAtTime(2E3,this.audio_context.currentTime);this.node_treble_right.frequency.setValueAtTime(2E3,this.audio_context.currentTime);this.node_bass_left=this.audio_context.createBiquadFilter();this.node_bass_right=
602
- this.audio_context.createBiquadFilter();this.node_bass_left.type="lowshelf";this.node_bass_right.type="lowshelf";this.node_bass_left.frequency.setValueAtTime(200,this.audio_context.currentTime);this.node_bass_right.frequency.setValueAtTime(200,this.audio_context.currentTime);this.node_gain_left=this.audio_context.createGain();this.node_gain_right=this.audio_context.createGain();this.node_merger=this.audio_context.createChannelMerger(2);this.input_left=this.node_treble_left;this.input_right=this.node_treble_right;
603
- this.node_treble_left.connect(this.node_bass_left);this.node_bass_left.connect(this.node_gain_left);this.node_gain_left.connect(this.node_merger,0,0);this.node_treble_right.connect(this.node_bass_right);this.node_bass_right.connect(this.node_gain_right);this.node_gain_right.connect(this.node_merger,0,1);this.node_merger.connect(this.audio_context.destination);a.register("mixer-connect",function(d){this.connect_source(d[0],d[1])},this);a.register("mixer-disconnect",function(d){this.disconnect_source(d[0],
604
- d[1])},this);a.register("mixer-volume",function(d){var e=d[0],f=d[1];d=Math.pow(10,d[2]/20);var h=0===e?this:this.sources.get(e);void 0===h?m(!1,"Mixer set volume - cannot set volume for undefined source: "+e):h.set_volume(d,f)},this);a.register("mixer-gain-left",function(d){this.gain_left=Math.pow(10,d/20);this.update()},this);a.register("mixer-gain-right",function(d){this.gain_right=Math.pow(10,d/20);this.update()},this);a.register("mixer-treble-left",c(this.node_treble_left),this);a.register("mixer-treble-right",
605
- c(this.node_treble_right),this);a.register("mixer-bass-left",c(this.node_bass_left),this);a.register("mixer-bass-right",c(this.node_bass_right),this)}xc.prototype.add_source=function(a,b){a=new zc(this.audio_context,a,this.input_left,this.input_right);m(!this.sources.has(b),"Mixer add source - overwritting source: "+b);this.sources.set(b,a);return a};xc.prototype.connect_source=function(a,b){var c=this.sources.get(a);void 0===c?m(!1,"Mixer connect - cannot connect undefined source: "+a):c.connect(b)};
606
- xc.prototype.disconnect_source=function(a,b){var c=this.sources.get(a);void 0===c?m(!1,"Mixer disconnect - cannot disconnect undefined source: "+a):c.disconnect(b)};xc.prototype.set_volume=function(a,b){void 0===b&&(b=2);switch(b){case 0:this.volume_left=a;break;case 1:this.volume_right=a;break;case 2:this.volume_both=a;break;default:m(!1,"Mixer set master volume - unknown channel: "+b);return}this.update()};xc.prototype.update=function(){var a=this.volume_both*this.volume_right*this.gain_right;this.node_gain_left.gain.setValueAtTime(this.volume_both*
607
- this.volume_left*this.gain_left,this.audio_context.currentTime);this.node_gain_right.gain.setValueAtTime(a,this.audio_context.currentTime)};function zc(a,b,c,d){this.audio_context=a;this.connected_right=this.connected_left=!0;this.volume_right=this.volume_left=this.volume_both=this.gain_hidden=1;this.node_splitter=a.createChannelSplitter(2);this.node_gain_left=a.createGain();this.node_gain_right=a.createGain();b.connect(this.node_splitter);this.node_splitter.connect(this.node_gain_left,0);this.node_gain_left.connect(c);
608
- this.node_splitter.connect(this.node_gain_right,1);this.node_gain_right.connect(d)}zc.prototype.update=function(){var a=this.connected_right*this.gain_hidden*this.volume_both*this.volume_right;this.node_gain_left.gain.setValueAtTime(this.connected_left*this.gain_hidden*this.volume_both*this.volume_left,this.audio_context.currentTime);this.node_gain_right.gain.setValueAtTime(a,this.audio_context.currentTime)};zc.prototype.connect=function(a){var b=!a||2===a;if(b||0===a)this.connected_left=!0;if(b||
609
- 1===a)this.connected_right=!0;this.update()};zc.prototype.disconnect=function(a){var b=!a||2===a;if(b||0===a)this.connected_left=!1;if(b||1===a)this.connected_right=!1;this.update()};zc.prototype.set_volume=function(a,b){void 0===b&&(b=2);switch(b){case 0:this.volume_left=a;break;case 1:this.volume_right=a;break;case 2:this.volume_both=a;break;default:m(!1,"Mixer set volume - unknown channel: "+b);return}this.update()};zc.prototype.set_gain_hidden=function(a){this.gain_hidden=a};function yc(a,b,c){this.node_oscillator=
610
- b.createOscillator();this.node_oscillator.type="square";this.node_oscillator.frequency.setValueAtTime(440,b.currentTime);this.mixer_connection=c.add_source(this.node_oscillator,1);this.mixer_connection.disconnect();a.register("pcspeaker-enable",function(){c.connect_source(1)},this);a.register("pcspeaker-disable",function(){c.disconnect_source(1)},this);a.register("pcspeaker-update",function(d){var e=d[1],f=0;3===d[0]&&(f=Math.min(1193181.6665999999/e,this.node_oscillator.frequency.maxValue),f=Math.max(f,
611
- 0));this.node_oscillator.frequency.setValueAtTime(f,b.currentTime)},this)}yc.prototype.start=function(){this.node_oscillator.start()};function vc(a,b,c){this.bus=a;this.audio_context=b;this.enabled=!1;this.sampling_rate=48E3;b=function(){function h(p){if(0===p)return 1;p*=Math.PI;return Math.sin(p)/p}function l(){var p=Reflect.construct(AudioWorkletProcessor,[],l);p.kernel_size=3;p.queue_data=Array(1024);p.queue_start=0;p.queue_end=0;p.queue_length=0;p.queue_size=p.queue_data.length;p.queued_samples=
612
- 0;p.source_buffer_previous=n;p.source_buffer_current=n;p.source_samples_per_destination=1;p.source_block_start=0;p.source_time=0;p.source_offset=0;p.port.onmessage=q=>{switch(q.data.type){case "queue":p.queue_push(q.data.value);break;case "sampling-rate":p.source_samples_per_destination=q.data.value/sampleRate}};return p}var n=[new Float32Array(256),new Float32Array(256)];Reflect.setPrototypeOf(l.prototype,AudioWorkletProcessor.prototype);Reflect.setPrototypeOf(l,AudioWorkletProcessor);l.prototype.process=
613
- l.prototype.process=function(p,q){for(p=0;p<q[0][0].length;p++){for(var r=0,t=0,v=this.source_offset+this.kernel_size,y=this.source_offset-this.kernel_size+1;y<=v;y++){var C=this.source_block_start+y;r+=this.get_sample(C,0)*this.kernel(this.source_time-y);t+=this.get_sample(C,1)*this.kernel(this.source_time-y)}if(isNaN(r)||isNaN(t))r=t=0,this.dbg_log("ERROR: NaN values! Ignoring for now.");q[0][0][p]=r;q[0][1][p]=t;this.source_time+=this.source_samples_per_destination;this.source_offset=Math.floor(this.source_time)}q=
614
- this.source_offset;q+=this.kernel_size+2;this.source_time-=this.source_offset;this.source_block_start+=this.source_offset;this.source_offset=0;this.ensure_enough_data(q);return!0};l.prototype.kernel=function(p){return h(p)*h(p/this.kernel_size)};l.prototype.get_sample=function(p,q){return 0>p?(p+=this.source_buffer_previous[0].length,this.source_buffer_previous[q][p]):this.source_buffer_current[q][p]};l.prototype.ensure_enough_data=function(p){var q=this.source_buffer_current[0].length;q-this.source_block_start<
615
- p&&(this.prepare_next_buffer(),this.source_block_start-=q)};l.prototype.prepare_next_buffer=function(){256>this.queued_samples&&this.queue_length&&this.dbg_log("Not enough samples - should not happen during midway of playback");this.source_buffer_previous=this.source_buffer_current;this.source_buffer_current=this.queue_shift();var p=this.source_buffer_current[0].length;if(256>p){for(var q=this.queue_start,r=0;256>p&&r<this.queue_length;)p+=this.queue_data[q][0].length,q=q+1&this.queue_size-1,r++;
616
- p=Math.max(p,256);p=[new Float32Array(p),new Float32Array(p)];p[0].set(this.source_buffer_current[0]);p[1].set(this.source_buffer_current[1]);q=this.source_buffer_current[0].length;for(var t=0;t<r;t++){var v=this.queue_shift();p[0].set(v[0],q);p[1].set(v[1],q);q+=v[0].length}this.source_buffer_current=p}this.pump()};l.prototype.pump=function(){1024>this.queued_samples/this.source_samples_per_destination&&this.port.postMessage({type:"pump"})};l.prototype.queue_push=function(p){this.queue_length<this.queue_size&&
617
- (this.queue_data[this.queue_end]=p,this.queue_end=this.queue_end+1&this.queue_size-1,this.queue_length++,this.queued_samples+=p[0].length,this.pump())};l.prototype.queue_shift=function(){if(!this.queue_length)return n;var p=this.queue_data[this.queue_start];this.queue_data[this.queue_start]=null;this.queue_start=this.queue_start+1&this.queue_size-1;this.queue_length--;this.queued_samples-=p[0].length;return p};l.prototype.dbg_log=function(p){this.port.postMessage({type:"debug-log",value:p})};registerProcessor("dac-processor",
618
- l)}.toString();var d=b.indexOf("{")+1,e=b.lastIndexOf("}");b=b.substring(d,e);b=new Blob(["var DEBUG = true;\n"+b],{type:"application/javascript"});var f=URL.createObjectURL(b);this.node_processor=null;this.node_output=this.audio_context.createGain();this.audio_context.audioWorklet.addModule(f).then(()=>{URL.revokeObjectURL(f);this.node_processor=new AudioWorkletNode(this.audio_context,"dac-processor",{numberOfInputs:0,numberOfOutputs:1,outputChannelCount:[2],parameterData:{},processorOptions:{}});
619
- this.node_processor.port.postMessage({type:"sampling-rate",value:this.sampling_rate});this.node_processor.port.onmessage=h=>{switch(h.data.type){case "pump":this.pump();break;case "debug-log":D("SpeakerWorkletDAC - Worklet: "+h.data.value)}};this.node_processor.connect(this.node_output)});this.mixer_connection=c.add_source(this.node_output,2);this.mixer_connection.set_gain_hidden(3);a.register("dac-send-data",function(h){this.queue(h)},this);a.register("dac-enable",function(){this.enabled=!0},this);
620
- a.register("dac-disable",function(){this.enabled=!1},this);a.register("dac-tell-sampling-rate",function(h){m(0<h,"Sampling rate should be nonzero");this.sampling_rate=h;this.node_processor&&this.node_processor.port.postMessage({type:"sampling-rate",value:h})},this);this.debugger=new Ac(this.audio_context,this.node_output)}vc.prototype.queue=function(a){this.node_processor&&(this.debugger.push_queued_data(a),this.node_processor.port.postMessage({type:"queue",value:a},[a[0].buffer,a[1].buffer]))};vc.prototype.pump=
621
- function(){this.enabled&&this.bus.send("dac-request-data")};function wc(a,b,c){this.bus=a;this.audio_context=b;this.enabled=!1;this.sampling_rate=22050;this.buffered_time=0;this.rate_ratio=1;this.node_lowpass=this.audio_context.createBiquadFilter();this.node_lowpass.type="lowpass";this.node_output=this.node_lowpass;this.mixer_connection=c.add_source(this.node_output,2);this.mixer_connection.set_gain_hidden(3);a.register("dac-send-data",function(d){this.queue(d)},this);a.register("dac-enable",function(){this.enabled=
622
- !0;this.pump()},this);a.register("dac-disable",function(){this.enabled=!1},this);a.register("dac-tell-sampling-rate",function(d){m(0<d,"Sampling rate should be nonzero");this.sampling_rate=d;this.rate_ratio=Math.ceil(8E3/d);this.node_lowpass.frequency.setValueAtTime(d/2,this.audio_context.currentTime)},this);this.debugger=new Ac(this.audio_context,this.node_output)}wc.prototype.queue=function(a){this.debugger.push_queued_data(a);var b=a[0].length,c=b/this.sampling_rate;if(1<this.rate_ratio){var d=
623
- this.audio_context.createBuffer(2,b*this.rate_ratio,this.sampling_rate*this.rate_ratio);for(var e=d.getChannelData(0),f=d.getChannelData(1),h=0,l=0;l<b;l++)for(var n=0;n<this.rate_ratio;n++,h++)e[h]=a[0][l],f[h]=a[1][l]}else d=this.audio_context.createBuffer(2,b,this.sampling_rate),d.copyToChannel?(d.copyToChannel(a[0],0),d.copyToChannel(a[1],1)):(d.getChannelData(0).set(a[0]),d.getChannelData(1).set(a[1]));a=this.audio_context.createBufferSource();a.buffer=d;a.connect(this.node_lowpass);a.addEventListener("ended",
624
- this.pump.bind(this));d=this.audio_context.currentTime;if(this.buffered_time<d)for(D("Speaker DAC - Creating/Recreating reserve - shouldn't occur frequently during playback"),this.buffered_time=d,d=.2-c,b=0;b<=d;)b+=c,this.buffered_time+=c,setTimeout(()=>this.pump(),1E3*b);a.start(this.buffered_time);this.buffered_time+=c;setTimeout(()=>this.pump(),0)};wc.prototype.pump=function(){this.enabled&&(.2<this.buffered_time-this.audio_context.currentTime||this.bus.send("dac-request-data"))};function Ac(a,
625
- b){this.audio_context=a;this.node_source=b;this.node_processor=null;this.node_gain=this.audio_context.createGain();this.node_gain.gain.setValueAtTime(0,this.audio_context.currentTime);this.node_gain.connect(this.audio_context.destination);this.is_active=!1;this.queued_history=[];this.output_history=[];this.queued=[[],[]];this.output=[[],[]]}Ac.prototype.start=function(a){this.is_active=!0;this.queued=[[],[]];this.output=[[],[]];this.queued_history.push(this.queued);this.output_history.push(this.output);
626
- this.node_processor=this.audio_context.createScriptProcessor(1024,2,2);this.node_processor.onaudioprocess=b=>{this.output[0].push(b.inputBuffer.getChannelData(0).slice());this.output[1].push(b.inputBuffer.getChannelData(1).slice())};this.node_source.connect(this.node_processor);this.node_processor.connect(this.node_gain);setTimeout(()=>{this.stop()},a)};Ac.prototype.stop=function(){this.is_active=!1;this.node_source.disconnect(this.node_processor);this.node_processor.disconnect();this.node_processor=
627
- null};Ac.prototype.push_queued_data=function(a){this.is_active&&(this.queued[0].push(a[0].slice()),this.queued[1].push(a[1].slice()))};Ac.prototype.download_txt=function(a,b){a=this.output_history[a][b].map(c=>c.join(" ")).join(" ");sa(a,"dacdata.txt")};Ac.prototype.download_csv=function(a){a=this.output_history[a];for(var b=[],c=0;c<a[0].length;c++)for(var d=0;d<a[0][c].length;d++)b.push(`${a[0][c][d]},${a[1][c][d]}`);sa(b.join("\n"),"dacdata.csv")};function Bc(a,b){function c(l){h.bus&&h.enabled&&
628
- (h.send_char(l.which),l.preventDefault())}function d(l){var n=l.which;8===n?(h.send_char(127),l.preventDefault()):9===n&&(h.send_char(9),l.preventDefault())}function e(l){if(h.enabled){for(var n=l.clipboardData.getData("text/plain"),p=0;p<n.length;p++)h.send_char(n.charCodeAt(p));l.preventDefault()}}function f(l){l.target!==a&&a.blur()}var h=this;this.enabled=!0;this.bus=b;this.text="";this.text_new_line=!1;this.last_update=0;this.bus.register("serial0-output-byte",function(l){l=String.fromCharCode(l);
629
- this.show_char(l)},this);this.destroy=function(){a.removeEventListener("keypress",c,!1);a.removeEventListener("keydown",d,!1);a.removeEventListener("paste",e,!1);window.removeEventListener("mousedown",f,!1)};this.init=function(){this.destroy();a.style.display="block";a.addEventListener("keypress",c,!1);a.addEventListener("keydown",d,!1);a.addEventListener("paste",e,!1);window.addEventListener("mousedown",f,!1)};this.init();this.show_char=function(l){"\b"===l?(this.text=this.text.slice(0,-1),this.update()):
630
- "\r"!==l&&(this.text+=l,"\n"===l&&(this.text_new_line=!0),this.update())};this.update=function(){var l=Date.now(),n=l-this.last_update;16>n?void 0===this.update_timer&&(this.update_timer=setTimeout(()=>{this.update_timer=void 0;var p=Date.now();m(15<=p-this.last_update);this.last_update=p;this.render()},16-n)):(void 0!==this.update_timer&&(clearTimeout(this.update_timer),this.update_timer=void 0),this.last_update=l,this.render())};this.render=function(){a.value=this.text;this.text_new_line&&(this.text_new_line=
631
- !1,a.scrollTop=1E9)};this.send_char=function(l){h.bus&&h.bus.send("serial0-input",l)}}function Cc(a,b){this.element=a;if(window.Terminal){var c=this.term=new window.Terminal({logLevel:"off"});c.write("This is the serial console. Whatever you type or paste here will be sent to COM1");var d=c.onData(function(e){for(let f=0;f<e.length;f++)b.send("serial0-input",e.charCodeAt(f))});b.register("serial0-output-byte",function(e){c.write(Uint8Array.of(e))},this);this.destroy=function(){d.dispose();c.dispose()}}}
632
- Cc.prototype.show=function(){this.term&&this.term.open(this.element)};function Dc(a,b,c){this.bus=b;this.socket=void 0;this.id=c||0;this.send_queue=[];this.url=a;this.reconnect_interval=1E4;this.last_connect_attempt=Date.now()-this.reconnect_interval;this.send_queue_limit=64;this.destroyed=!1;this.bus.register("net"+this.id+"-send",function(d){this.send(d)},this)}Dc.prototype.handle_message=function(a){this.bus&&this.bus.send("net"+this.id+"-receive",new Uint8Array(a.data))};Dc.prototype.handle_close=
633
- function(){this.destroyed||(this.connect(),setTimeout(this.connect.bind(this),this.reconnect_interval))};Dc.prototype.handle_open=function(){for(var a=0;a<this.send_queue.length;a++)this.send(this.send_queue[a]);this.send_queue=[]};Dc.prototype.handle_error=function(){};Dc.prototype.destroy=function(){this.destroyed=!0;this.socket&&this.socket.close()};Dc.prototype.connect=function(){if("undefined"!==typeof WebSocket){if(this.socket){var a=this.socket.readyState;if(0===a||1===a)return}a=Date.now();
634
- if(!(this.last_connect_attempt+this.reconnect_interval>a)){this.last_connect_attempt=Date.now();try{this.socket=new WebSocket(this.url)}catch(b){console.error(b);return}this.socket.binaryType="arraybuffer";this.socket.onopen=this.handle_open.bind(this);this.socket.onmessage=this.handle_message.bind(this);this.socket.onclose=this.handle_close.bind(this);this.socket.onerror=this.handle_error.bind(this)}}};Dc.prototype.send=function(a){this.socket&&1===this.socket.readyState?this.socket.send(a):(this.send_queue.push(a),
635
- this.send_queue.length>2*this.send_queue_limit&&(this.send_queue=this.send_queue.slice(-this.send_queue_limit)),this.connect())};Dc.prototype.change_proxy=function(a){this.url=a;this.socket&&(this.socket.onclose=function(){},this.socket.onerror=function(){},this.socket.close(),this.socket=void 0)};const Ec={stats_to_string:function(a){return Ec.print_misc_stats(a)+Ec.print_instruction_counts(a)},print_misc_stats:function(a){let b="";var c="COMPILE COMPILE_SKIPPED_NO_NEW_ENTRY_POINTS COMPILE_WRONG_ADDRESS_SPACE COMPILE_CUT_OFF_AT_END_OF_PAGE COMPILE_WITH_LOOP_SAFETY COMPILE_PAGE COMPILE_PAGE/COMPILE COMPILE_BASIC_BLOCK COMPILE_DUPLICATED_BASIC_BLOCK COMPILE_WASM_BLOCK COMPILE_WASM_LOOP COMPILE_DISPATCHER COMPILE_ENTRY_POINT COMPILE_WASM_TOTAL_BYTES COMPILE_WASM_TOTAL_BYTES/COMPILE_PAGE RUN_INTERPRETED RUN_INTERPRETED_NEW_PAGE RUN_INTERPRETED_PAGE_HAS_CODE RUN_INTERPRETED_PAGE_HAS_ENTRY_AFTER_PAGE_WALK RUN_INTERPRETED_NEAR_END_OF_PAGE RUN_INTERPRETED_DIFFERENT_STATE RUN_INTERPRETED_DIFFERENT_STATE_CPL3 RUN_INTERPRETED_DIFFERENT_STATE_FLAT RUN_INTERPRETED_DIFFERENT_STATE_IS32 RUN_INTERPRETED_DIFFERENT_STATE_SS32 RUN_INTERPRETED_MISSED_COMPILED_ENTRY_RUN_INTERPRETED RUN_INTERPRETED_STEPS RUN_FROM_CACHE RUN_FROM_CACHE_STEPS RUN_FROM_CACHE_STEPS/RUN_FROM_CACHE RUN_FROM_CACHE_STEPS/RUN_INTERPRETED_STEPS DIRECT_EXIT INDIRECT_JUMP INDIRECT_JUMP_NO_ENTRY NORMAL_PAGE_CHANGE NORMAL_FALLTHRU NORMAL_FALLTHRU_WITH_TARGET_BLOCK NORMAL_BRANCH NORMAL_BRANCH_WITH_TARGET_BLOCK CONDITIONAL_JUMP CONDITIONAL_JUMP_PAGE_CHANGE CONDITIONAL_JUMP_EXIT CONDITIONAL_JUMP_FALLTHRU CONDITIONAL_JUMP_FALLTHRU_WITH_TARGET_BLOCK CONDITIONAL_JUMP_BRANCH CONDITIONAL_JUMP_BRANCH_WITH_TARGET_BLOCK DISPATCHER_SMALL DISPATCHER_LARGE LOOP LOOP_SAFETY CONDITION_OPTIMISED CONDITION_UNOPTIMISED CONDITION_UNOPTIMISED_PF CONDITION_UNOPTIMISED_UNHANDLED_L CONDITION_UNOPTIMISED_UNHANDLED_LE FAILED_PAGE_CHANGE SAFE_READ_FAST SAFE_READ_SLOW_PAGE_CROSSED SAFE_READ_SLOW_NOT_VALID SAFE_READ_SLOW_NOT_USER SAFE_READ_SLOW_IN_MAPPED_RANGE SAFE_WRITE_FAST SAFE_WRITE_SLOW_PAGE_CROSSED SAFE_WRITE_SLOW_NOT_VALID SAFE_WRITE_SLOW_NOT_USER SAFE_WRITE_SLOW_IN_MAPPED_RANGE SAFE_WRITE_SLOW_READ_ONLY SAFE_WRITE_SLOW_HAS_CODE SAFE_READ_WRITE_FAST SAFE_READ_WRITE_SLOW_PAGE_CROSSED SAFE_READ_WRITE_SLOW_NOT_VALID SAFE_READ_WRITE_SLOW_NOT_USER SAFE_READ_WRITE_SLOW_IN_MAPPED_RANGE SAFE_READ_WRITE_SLOW_READ_ONLY SAFE_READ_WRITE_SLOW_HAS_CODE PAGE_FAULT TLB_MISS MAIN_LOOP MAIN_LOOP_IDLE DO_MANY_CYCLES CYCLE_INTERNAL INVALIDATE_ALL_MODULES_NO_FREE_WASM_INDICES INVALIDATE_MODULE_WRITTEN_WHILE_COMPILED INVALIDATE_MODULE_UNUSED_AFTER_OVERWRITE INVALIDATE_MODULE_DIRTY_PAGE INVALIDATE_PAGE_HAD_CODE INVALIDATE_PAGE_HAD_ENTRY_POINTS DIRTY_PAGE_DID_NOT_HAVE_CODE RUN_FROM_CACHE_EXIT_SAME_PAGE RUN_FROM_CACHE_EXIT_NEAR_END_OF_PAGE RUN_FROM_CACHE_EXIT_DIFFERENT_PAGE CLEAR_TLB FULL_CLEAR_TLB TLB_FULL TLB_GLOBAL_FULL MODRM_SIMPLE_REG MODRM_SIMPLE_REG_WITH_OFFSET MODRM_SIMPLE_CONST_OFFSET MODRM_COMPLEX SEG_OFFSET_OPTIMISED SEG_OFFSET_NOT_OPTIMISED SEG_OFFSET_NOT_OPTIMISED_ES SEG_OFFSET_NOT_OPTIMISED_FS SEG_OFFSET_NOT_OPTIMISED_GS SEG_OFFSET_NOT_OPTIMISED_NOT_FLAT".split(" "),
636
- d=0;const e={};for(let h=0;h<c.length;h++){const l=c[h];var f=void 0;if(l.includes("/")){d++;const [n,p]=l.split("/");f=e[n]/e[p]}else f=e[l]=a.wm.exports.profiler_stat_get(h-d),f=1E8<=f?Math.round(f/1E6)+"m":1E5<=f?Math.round(f/1E3)+"k":f;b+=l+"="+f+"\n"}b+="\n";c=a.wm.exports.get_valid_tlb_entries_count();d=a.wm.exports.get_valid_global_tlb_entries_count();b=b+("TLB_ENTRIES="+c+" ("+d+" global, "+(c-d)+" non-global)\nWASM_TABLE_FREE=")+(a.wm.exports.jit_get_wasm_table_index_free_list_count()+"\n");
637
- b+="JIT_CACHE_SIZE="+a.wm.exports.jit_get_cache_size()+"\n";b+="FLAT_SEGMENTS="+a.wm.exports.has_flat_segmentation()+"\n";b+="wasm memory size: "+(a.wasm_memory.buffer.byteLength>>20)+"m\n";b=b+"Config:\nJIT_DISABLED="+(a.wm.exports.get_jit_config(0)+"\n");b+="MAX_PAGES="+a.wm.exports.get_jit_config(1)+"\n";b+="JIT_USE_LOOP_SAFETY="+!!a.wm.exports.get_jit_config(2)+"\n";return b+="MAX_EXTRA_BASIC_BLOCKS="+a.wm.exports.get_jit_config(3)+"\n"},print_instruction_counts:function(a){return[Ec.print_instruction_counts_offset(a,
638
- !1,!1,!1,!1),Ec.print_instruction_counts_offset(a,!0,!1,!1,!1),Ec.print_instruction_counts_offset(a,!1,!0,!1,!1),Ec.print_instruction_counts_offset(a,!1,!1,!0,!1),Ec.print_instruction_counts_offset(a,!1,!1,!1,!0)].join("\n\n")},print_instruction_counts_offset:function(a,b,c,d,e){let f="";var h=[],l=b?"compiled":c?"jit exit":d?"unguarded register":e?"wasm size":"executed";for(let q=0;256>q;q++)for(let r=0;8>r;r++)for(const t of[!1,!0]){var n=a.wm.exports.get_opstats_buffer(b,c,d,e,q,!1,t,r);h.push({opcode:q,
639
- count:n,is_mem:t,fixed_g:r});n=a.wm.exports.get_opstats_buffer(b,c,d,e,q,!0,t,r);h.push({opcode:3840|q,count:n,is_mem:t,fixed_g:r})}a=0;b=new Set([38,46,54,62,100,101,102,103,240,242,243]);for(const {count:q,opcode:r}of h)b.has(r)||(a+=q);if(0===a)return"";c=new Uint32Array(256);b=new Uint32Array(256);for(const {opcode:q,count:r}of h)3840===(q&65280)?b[q&255]+=r:c[q&255]+=r;f=f+"------------------\nTotal: "+(a+"\n");const p=1E7<a?1E3:1;d=Math.max.apply(Math,h.map(({count:q})=>Math.round(q/p)));d=
640
- String(d).length;f+=`Instruction counts ${l} (in ${p}):\n`;for(e=0;256>e;e++)f+=e.toString(16).padStart(2,"0")+":"+da(Math.round(c[e]/p),d),f=15===e%16?f+"\n":f+" ";f=f+"\n"+`Instruction counts ${l} (0f, in ${p}):\n`;for(l=0;256>l;l++)f+=(l&255).toString(16).padStart(2,"0")+":"+da(Math.round(b[l]/p),d),f=15===l%16?f+"\n":f+" ";f+="\n";h=h.filter(({count:q})=>q).sort(({count:q},{count:r})=>r-q);for(const {opcode:q,is_mem:r,fixed_g:t,count:v}of h.slice(0,200))h=q.toString(16)+"_"+t+(r?"_m":"_r"),f+=
641
- h+":"+(v/a*100).toFixed(2)+" ";return f+"\n"}};g.exportSymbol("print_stats$$module$src$browser$print_stats",Ec);const Fc=(new Date("1970-01-01T00:00:00Z")).getTime(),Gc=(new Date("1900-01-01T00:00:00Z")).getTime(),Hc=Fc-Gc,Ic=Math.pow(2,32),Jc=[118,56,54];function Kc(a){return[0,1,2,3,4,5].map(b=>a[b].toString(16)).map(b=>1===b.length?"0"+b:b).join(":")}function Lc(a){return a[0]<<24|a[1]<<16|a[2]<<8|a[3]}class Mc{constructor(a,b){a=Math.min(a,16);this.maximum_capacity=b?Math.max(b,a):0;this.length=
642
- this.head=this.tail=0;this.buffer=new Uint8Array(a)}write(a){const b=a.length;var c=this.length+b;let d=this.buffer.length;if(d<c){for(;d<c;)d*=2;if(this.maximum_capacity&&d>this.maximum_capacity)throw Error("stream capacity overflow in GrowableRingbuffer.write(), package dropped");c=new Uint8Array(d);this.peek(c);this.tail=0;this.head=this.length;this.buffer=c}c=this.buffer;const e=this.head+b;if(e>d){const f=d-this.head;c.set(a.subarray(0,f),this.head);c.set(a.subarray(f))}else c.set(a,this.head);
643
- this.head=e%d;this.length+=b}peek(a){const b=Math.min(this.length,a.length);if(b){const e=this.buffer;var c=e.length,d=this.tail+b;d>c?(d%=c,c-=this.tail,a.set(e.subarray(this.tail)),a.set(e.subarray(0,d),c)):a.set(e.subarray(this.tail,d))}return b}remove(a){a>this.length&&(a=this.length);a&&(this.tail=(this.tail+a)%this.buffer.length,this.length-=a);return a}}function Nc(){const a=new Uint8Array(1518),b=a.buffer,c=a.byteOffset;return{eth_frame:a,eth_frame_view:new DataView(b),eth_payload_view:new DataView(b,
644
- c+14,1500),ipv4_payload_view:new DataView(b,c+34,1480),udp_payload_view:new DataView(b,c+42,1472),text_encoder:new TextEncoder}}function Oc(a,b,c,d){d.eth_frame.set(b,c.byteOffset+a);return b.length}function Pc(a,b,c,d){const e=c.byteOffset+(a&-2);d=d.eth_frame;for(c=c.byteOffset;c<e;c+=2)b+=d[c]<<8|d[c+1];for(a&1&&(b+=d[e]<<8);b>>16;)b=(b&65535)+(b>>16);return~b&65535}function Qc(a,b){m(b.eth);a.eth_frame.fill(0);var c=a.eth_frame,d=c.subarray,e=a.eth_frame_view;Oc(0,b.eth.dest,e,a);Oc(6,b.eth.src,
645
- e,a);e.setUint16(12,b.eth.ethertype);e=14;if(b.arp){var f=a.eth_payload_view;f.setUint16(0,b.arp.htype);f.setUint16(2,b.arp.ptype);f.setUint8(4,b.arp.sha.length);f.setUint8(5,b.arp.spa.length);f.setUint16(6,b.arp.oper);Oc(8,b.arp.sha,f,a);Oc(14,b.arp.spa,f,a);Oc(18,b.arp.tha,f,a);Oc(24,b.arp.tpa,f,a);e+=28}else if(b.ipv4){f=a.eth_payload_view;var h=20;if(b.icmp){var l=a.ipv4_payload_view;l.setUint8(0,b.icmp.type);l.setUint8(1,b.icmp.code);l.setUint16(2,0);var n=4+Oc(4,b.icmp.data,l,a);l.setUint16(2,
646
- Pc(n,0,l,a));h+=n}else if(b.udp){l=a.ipv4_payload_view;var p=8;if(b.dhcp){n=p;var q=a.udp_payload_view;q.setUint8(0,b.dhcp.op);q.setUint8(1,b.dhcp.htype);q.setUint8(2,b.dhcp.hlen);q.setUint8(3,b.dhcp.hops);q.setUint32(4,b.dhcp.xid);q.setUint16(8,b.dhcp.secs);q.setUint16(10,b.dhcp.flags);q.setUint32(12,b.dhcp.ciaddr);q.setUint32(16,b.dhcp.yiaddr);q.setUint32(20,b.dhcp.siaddr);q.setUint32(24,b.dhcp.giaddr);Oc(28,b.dhcp.chaddr,q,a);q.setUint32(236,1669485411);p=240;for(var r of b.dhcp.options)p+=Oc(p,
647
- r,q,a);n+=p}else if(b.dns){r=p;p=a.udp_payload_view;p.setUint16(0,b.dns.id);p.setUint16(2,b.dns.flags);p.setUint16(4,b.dns.questions.length);p.setUint16(6,b.dns.answers.length);let y=12;for(var t=0;t<b.dns.questions.length;++t){var v=b.dns.questions[t];for(q of v.name){const C=a.text_encoder.encodeInto(q,a.eth_frame.subarray(p.byteOffset+(y+1))).written;p.setUint8(y,C);y+=1+C}p.setUint16(y,v.type);y+=2;p.setUint16(y,v.class);y+=2}for(t=0;t<b.dns.answers.length;++t){q=b.dns.answers[t];for(n of q.name)v=
648
- a.text_encoder.encodeInto(n,a.eth_frame.subarray(p.byteOffset+(y+1))).written,p.setUint8(y,v),y+=1+v;p.setUint16(y,q.type);y+=2;p.setUint16(y,q.class);y+=2;p.setUint32(y,q.ttl);y+=4;p.setUint16(y,q.data.length);y+=2;y+=Oc(y,q.data,p,a)}n=r+y}else b.ntp?(n=p,q=a.udp_payload_view,q.setUint8(0,b.ntp.flags),q.setUint8(1,b.ntp.stratum),q.setUint8(2,b.ntp.poll),q.setUint8(3,b.ntp.precision),q.setUint32(4,b.ntp.root_delay),q.setUint32(8,b.ntp.root_disp),q.setUint32(12,b.ntp.ref_id),q.setUint32(16,b.ntp.ref_ts_i),
649
- q.setUint32(20,b.ntp.ref_ts_f),q.setUint32(24,b.ntp.ori_ts_i),q.setUint32(28,b.ntp.ori_ts_f),q.setUint32(32,b.ntp.rec_ts_i),q.setUint32(36,b.ntp.rec_ts_f),q.setUint32(40,b.ntp.trans_ts_i),q.setUint32(44,b.ntp.trans_ts_f),n+=48):n=p+Oc(0,b.udp.data,a.udp_payload_view,a);p=n;l.setUint16(0,b.udp.sport);l.setUint16(2,b.udp.dport);l.setUint16(4,p);l.setUint16(6,0);l.setUint16(6,Pc(p,(b.ipv4.src[0]<<8|b.ipv4.src[1])+(b.ipv4.src[2]<<8|b.ipv4.src[3])+(b.ipv4.dest[0]<<8|b.ipv4.dest[1])+(b.ipv4.dest[2]<<8|
650
- b.ipv4.dest[3])+17+p,l,a));h+=p}else b.tcp&&(l=a.ipv4_payload_view,n=0,q=b.tcp,q.fin&&(n|=1),q.syn&&(n|=2),q.rst&&(n|=4),q.psh&&(n|=8),q.ack&&(n|=16),q.urg&&(n|=32),q.ece&&(n|=64),q.cwr&&(n|=128),l.setUint16(0,q.sport),l.setUint16(2,q.dport),l.setUint32(4,q.seq),l.setUint32(8,q.ackn),l.setUint8(12,80),l.setUint8(13,n),l.setUint16(14,q.winsize),l.setUint16(16,0),l.setUint16(18,q.urgent||0),n=20,b.tcp_data&&(n+=Oc(20,b.tcp_data,l,a)),l.setUint16(16,Pc(n,(b.ipv4.src[0]<<8|b.ipv4.src[1])+(b.ipv4.src[2]<<
651
- 8|b.ipv4.src[3])+(b.ipv4.dest[0]<<8|b.ipv4.dest[1])+(b.ipv4.dest[2]<<8|b.ipv4.dest[3])+6+n,l,a)),h+=n);f.setUint8(0,69);f.setUint8(1,b.ipv4.tos||0);f.setUint16(2,h);f.setUint16(4,b.ipv4.id||0);f.setUint8(6,64);f.setUint8(8,b.ipv4.ttl||32);f.setUint8(9,b.ipv4.proto);f.setUint16(10,0);Oc(12,b.ipv4.src,f,a);Oc(16,b.ipv4.dest,f,a);f.setUint16(10,Pc(20,0,f,a));e+=h}return d.call(c,0,e)}function Rc(a,b){fetch(`https://${b.doh_server||"cloudflare-dns.com"}/dns-query`,{method:"POST",headers:[["content-type",
652
- "application/dns-message"]],body:a.udp.data}).then(async c=>{c={eth:{ethertype:2048,src:b.router_mac,dest:a.eth.src},ipv4:{proto:17,src:b.router_ip,dest:a.ipv4.src},udp:{sport:53,dport:a.udp.sport,data:new Uint8Array(await c.arrayBuffer())}};b.receive(Qc(b.eth_encoder_buf,c))});return!0}function Sc(a,b){let c={};c.eth={ethertype:2048,src:b.router_mac,dest:a.eth.src};c.ipv4={proto:17,src:b.router_ip,dest:b.vm_ip};c.udp={sport:67,dport:68};c.dhcp={htype:1,hlen:6,hops:0,xid:a.dhcp.xid,secs:0,flags:0,
653
- ciaddr:0,yiaddr:Lc(b.vm_ip),siaddr:Lc(b.router_ip),giaddr:Lc(b.router_ip),chaddr:a.dhcp.chaddr};let d=[],e=a.dhcp.options.find(function(f){return 53===f[0]});e&&3===e[2]&&(a.dhcp.op=3);1===a.dhcp.op&&(c.dhcp.op=2,d.push(new Uint8Array([53,1,2])));3===a.dhcp.op&&(c.dhcp.op=2,d.push(new Uint8Array([53,1,5])),d.push(new Uint8Array([51,4,8,0,0,0])));a=[b.router_ip[0],b.router_ip[1],b.router_ip[2],b.router_ip[3]];d.push(new Uint8Array([1,4,255,255,255,0]));b.masquerade&&(d.push(new Uint8Array([3,4].concat(a))),
654
- d.push(new Uint8Array([6,4].concat(a))));d.push(new Uint8Array([54,4].concat(a)));d.push(new Uint8Array([60,3].concat(Jc)));d.push(new Uint8Array([255,0]));c.dhcp.options=d;b.receive(Qc(b.eth_encoder_buf,c))}function Tc(a,b){let c={};var d=(new DataView(a.buffer,a.byteOffset,a.byteLength)).getUint16(12),e={ethertype:d,dest:a.subarray(0,6),dest_s:Kc(a.subarray(0,6)),src:a.subarray(6,12),src_s:Kc(a.subarray(6,12))};c.eth=e;a=a.subarray(14,a.length);if(2048===d){var f=new DataView(a.buffer,a.byteOffset,
655
- a.byteLength),h=a[0]>>4&15;e=a[0]&15;var l=f.getUint8(1),n=f.getUint16(2);let p=f.getUint8(8);d=f.getUint8(9);f=f.getUint16(10);h={version:h,ihl:e,tos:l,len:n,ttl:p,proto:d,ip_checksum:f,src:a.subarray(12,16),dest:a.subarray(16,20)};Math.max(n,46)!==a.length&&D(`ipv4 Length mismatch: ${n} != ${a.length}`,16777216);c.ipv4=h;e=a.subarray(4*e,n);if(1===d)a=new DataView(e.buffer,e.byteOffset,e.byteLength),a={type:a.getUint8(0),code:a.getUint8(1),checksum:a.getUint16(2),data:e.subarray(4)},c.icmp=a;else if(6===
656
- d)d=new DataView(e.buffer,e.byteOffset,e.byteLength),a={sport:d.getUint16(0),dport:d.getUint16(2),seq:d.getUint32(4),ackn:d.getUint32(8),doff:d.getUint8(12)>>4,winsize:d.getUint16(14),checksum:d.getUint16(16),urgent:d.getUint16(18)},d=d.getUint8(13),a.fin=!!(d&1),a.syn=!!(d&2),a.rst=!!(d&4),a.psh=!!(d&8),a.ack=!!(d&16),a.urg=!!(d&32),a.ece=!!(d&64),a.cwr=!!(d&128),c.tcp=a,c.tcp_data=e.subarray(4*a.doff);else if(17===d){a=new DataView(e.buffer,e.byteOffset,e.byteLength);a={sport:a.getUint16(0),dport:a.getUint16(2),
657
- len:a.getUint16(4),checksum:a.getUint16(6),data:e.subarray(8),data_s:(new TextDecoder).decode(e.subarray(8))};if(67===a.dport||67===a.sport){e=e.subarray(8);d=new DataView(e.buffer,e.byteOffset,e.byteLength);e.subarray(44,236);d={op:d.getUint8(0),htype:d.getUint8(1),hlen:d.getUint8(2),hops:d.getUint8(3),xid:d.getUint32(4),secs:d.getUint16(8),flags:d.getUint16(10),ciaddr:d.getUint32(12),yiaddr:d.getUint32(16),siaddr:d.getUint32(20),giaddr:d.getUint32(24),chaddr:e.subarray(28,44),magic:d.getUint32(236),
658
- options:[]};e=e.subarray(240);for(n=0;n<e.length;++n)h=n,0!==e[n]&&(++n,l=e[n],n+=l,d.options.push(e.subarray(h,h+l+2)));c.dhcp=d;c.dhcp_options=d.options}else 53===a.dport||53===a.sport?Uc(e.subarray(8),c):123===a.dport&&(d=e.subarray(8),d=new DataView(d.buffer,d.byteOffset,d.byteLength),c.ntp={flags:d.getUint8(0),stratum:d.getUint8(1),poll:d.getUint8(2),precision:d.getUint8(3),root_delay:d.getUint32(4),root_disp:d.getUint32(8),ref_id:d.getUint32(12),ref_ts_i:d.getUint32(16),ref_ts_f:d.getUint32(20),
659
- ori_ts_i:d.getUint32(24),ori_ts_f:d.getUint32(28),rec_ts_i:d.getUint32(32),rec_ts_f:d.getUint32(36),trans_ts_i:d.getUint32(40),trans_ts_f:d.getUint32(44)});c.udp=a}}else 2054===d?(d=new DataView(a.buffer,a.byteOffset,a.byteLength),a={htype:d.getUint16(0),ptype:d.getUint16(2),oper:d.getUint16(6),sha:a.subarray(8,14),spa:a.subarray(14,18),tha:a.subarray(18,24),tpa:a.subarray(24,28)},c.arp=a):34525===d?D("Unimplemented: ipv6"):D("Unknown ethertype: "+z(d),16777216);if(c.ipv4)if(c.tcp)a:{a=`${c.ipv4.src.join(".")}:${c.tcp.sport}:${c.ipv4.dest.join(".")}:${c.tcp.dport}`;
660
- if(c.tcp.syn&&(b.tcp_conn[a]&&D("SYN to already opened port",16777216),b.on_tcp_connection(c,a)))break a;if(b.tcp_conn[a])b.tcp_conn[a].process(c);else{D(`I dont know about ${a}, so resetting`,16777216);a=c.tcp.ackn;if(c.tcp.fin||c.tcp.syn)a+=1;d={};d.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src};d.ipv4={proto:6,src:c.ipv4.dest,dest:c.ipv4.src};d.tcp={sport:c.tcp.dport,dport:c.tcp.sport,seq:a,ackn:c.tcp.seq+(c.tcp.syn?1:0),winsize:c.tcp.winsize,rst:!0,ack:c.tcp.syn};b.receive(Qc(b.eth_encoder_buf,
661
- d))}}else if(c.udp)if(c.dns)if("static"===b.dns_method){a={};a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src};a.ipv4={proto:17,src:b.router_ip,dest:c.ipv4.src};a.udp={sport:53,dport:c.udp.sport};d=[];for(e=0;e<c.dns.questions.length;++e)switch(n=c.dns.questions[e],n.type){case 1:d.push({name:n.name,type:n.type,class:n.class,ttl:600,data:[192,168,87,1]})}a.dns={id:c.dns.id,flags:33152,questions:c.dns.questions,answers:d};b.receive(Qc(b.eth_encoder_buf,a))}else Rc(c,b);else c.dhcp?Sc(c,b):c.ntp?
662
- (a=Date.now()+Hc,d=a%1E3/1E3*Ic,e={},e.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},e.ipv4={proto:17,src:c.ipv4.dest,dest:c.ipv4.src},e.udp={sport:123,dport:c.udp.sport},e.ntp=Object.assign({},c.ntp),e.ntp.flags=36,e.ntp.poll=10,e.ntp.ori_ts_i=c.ntp.trans_ts_i,e.ntp.ori_ts_f=c.ntp.trans_ts_f,e.ntp.rec_ts_i=a/1E3,e.ntp.rec_ts_f=d,e.ntp.trans_ts_i=a/1E3,e.ntp.trans_ts_f=d,e.ntp.stratum=2,b.receive(Qc(b.eth_encoder_buf,e))):8===c.udp.dport&&(a={},a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},
663
- a.ipv4={proto:17,src:c.ipv4.dest,dest:c.ipv4.src},a.udp={sport:c.udp.dport,dport:c.udp.sport,data:(new TextEncoder).encode(c.udp.data_s)},b.receive(Qc(b.eth_encoder_buf,a)));else c.icmp&&8===c.icmp.type&&(a={},a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},a.ipv4={proto:1,src:c.ipv4.dest,dest:c.ipv4.src},a.icmp={type:0,code:c.icmp.code,data:c.icmp.data},b.receive(Qc(b.eth_encoder_buf,a)));else c.arp&&1===c.arp.oper&&2048===c.arp.ptype&&(a=Lc(c.arp.tpa)&4294967040,d=Lc(b.router_ip)&4294967040,
664
- !b.masquerade&&a!==d||a===d&&99<c.arp.tpa[3]||(a={},a.eth={ethertype:2054,src:b.router_mac,dest:c.eth.src},a.arp={htype:1,ptype:2048,oper:2,sha:b.router_mac,spa:c.arp.tpa,tha:c.eth.src,tpa:c.arp.spa},b.receive(Qc(b.eth_encoder_buf,a))))}function Uc(a,b){function c(){let p=[],q;do q=d.getUint8(l),p.push((new TextDecoder).decode(a.subarray(l+1,l+1+q))),l+=q+1;while(0<q);return p}let d=new DataView(a.buffer,a.byteOffset,a.byteLength),e={id:d.getUint16(0),flags:d.getUint16(2),questions:[],answers:[]};
665
- var f=d.getUint16(4);let h=d.getUint16(6);d.getUint16(8);d.getUint16(10);let l=12;for(var n=0;n<f;n++)e.questions.push({name:c(),type:d.getInt16(l),class:d.getInt16(l+2)}),l+=4;for(f=0;f<h;f++){n={name:c(),type:d.getInt16(l),class:d.getUint16(l+2),ttl:d.getUint32(l+4)};l+=8;let p=d.getUint16(l);l+=2;n.data=a.subarray(l,l+p);l+=p;e.answers.push(n)}b.dns=e}function Vc(a,b){var c=b.vm_ip.join(".");const d=b.router_ip.join("."),e=16383*Math.random()|0;let f,h,l=0;do f=49152+(e+l)%16383,h=`${c}:${a}:${d}:${f}`;
666
- while(16383>++l&&b.tcp_conn[h]);if(b.tcp_conn[h])throw Error("pool of dynamic TCP port numbers exhausted, connection aborted");c=new Wc;c.tuple=h;c.hsrc=b.router_mac;c.psrc=b.router_ip;c.sport=f;c.hdest=b.vm_mac;c.dport=a;c.pdest=b.vm_ip;c.net=b;b.tcp_conn[h]=c;c.connect();return c}function Xc(a,b){return new Promise(c=>{let d=Vc(a,b);d.state="syn-probe";d.on("probe",c)})}function Wc(){this.state="closed";this.net=null;this.send_buffer=new Mc(2048,0);this.send_chunk_buf=new Uint8Array(1460);this.delayed_send_fin=
667
- this.in_active_close=!1;this.delayed_state=void 0;this.events_handlers={}}Wc.prototype.on=function(a,b){this.events_handlers[a]=b};Wc.prototype.emit=function(a,...b){this.events_handlers[a]&&this.events_handlers[a].apply(this,b)};Wc.prototype.ipv4_reply=function(){let a={};a.eth={ethertype:2048,src:this.hsrc,dest:this.hdest};a.ipv4={proto:6,src:this.psrc,dest:this.pdest};a.tcp={sport:this.sport,dport:this.dport,winsize:this.winsize,ackn:this.ack,seq:this.seq,ack:!0};return a};Wc.prototype.packet_reply=
668
- function(a,b){a={sport:a.tcp.dport,dport:a.tcp.sport,winsize:a.tcp.winsize,ackn:this.ack,seq:this.seq};if(b)for(const c in b)a[c]=b[c];b=this.ipv4_reply();b.tcp=a;return b};Wc.prototype.connect=function(){this.seq=1338;this.ack=1;this.start_seq=0;this.winsize=64240;this.state="syn-sent";let a=this.ipv4_reply();a.ipv4.id=2345;a.tcp={sport:this.sport,dport:this.dport,seq:1337,ackn:0,winsize:0,syn:!0};this.net.receive(Qc(this.net.eth_encoder_buf,a))};Wc.prototype.accept=function(a){this.seq=1338;this.ack=
669
- a.tcp.seq+1;this.start_seq=a.tcp.seq;this.hsrc=this.net.router_mac;this.psrc=a.ipv4.dest;this.sport=a.tcp.dport;this.hdest=a.eth.src;this.dport=a.tcp.sport;this.pdest=a.ipv4.src;this.winsize=a.tcp.winsize;let b=this.ipv4_reply();b.tcp={sport:this.sport,dport:this.dport,seq:1337,ackn:this.ack,winsize:a.tcp.winsize,syn:!0,ack:!0};this.state="established";this.net.receive(Qc(this.net.eth_encoder_buf,b))};Wc.prototype.process=function(a){if("closed"===this.state)a=this.packet_reply(a,{rst:!0}),this.net.receive(Qc(this.net.eth_encoder_buf,
670
- a));else if(a.tcp.rst){if("syn-probe"===this.state)this.emit("probe",!1);else this.on_close();this.release()}else if(a.tcp.syn){if("syn-sent"===this.state&&a.tcp.ack){this.ack=a.tcp.seq+1;this.start_seq=a.tcp.seq;this.last_received_ackn=a.tcp.ackn;var b=this.ipv4_reply();this.net.receive(Qc(this.net.eth_encoder_buf,b));this.state="established";this.emit("connect")}else"syn-probe"===this.state&&a.tcp.ack?(this.emit("probe",!0),b=this.packet_reply(a,{rst:!0}),this.net.receive(Qc(this.net.eth_encoder_buf,
671
- b)),this.release()):D(`TCP[${this.tuple}]: WARNING: unexpected SYN packet dropped`,16777216);a.tcp_data.length&&D(`TCP[${this.tuple}]: WARNING: ${a.tcp_data.length} bytes of unexpected SYN packet payload dropped`,16777216)}else{if(a.tcp.ack)if("syn-received"===this.state)this.state="established";else if("fin-wait-1"===this.state)a.tcp.fin||(this.state="fin-wait-2");else if("closing"===this.state||"last-ack"===this.state){this.release();return}if(void 0===this.last_received_ackn)this.last_received_ackn=
672
- a.tcp.ackn;else if(b=a.tcp.ackn-this.last_received_ackn,0<b){if(this.last_received_ackn=a.tcp.ackn,this.send_buffer.remove(b),this.seq+=b,this.pending=!1,this.delayed_send_fin&&!this.send_buffer.length){this.delayed_send_fin=!1;this.state=this.delayed_state;a=this.ipv4_reply();a.tcp.fin=!0;this.net.receive(Qc(this.net.eth_encoder_buf,a));return}}else if(0>b){D(`TCP[${this.tuple}]: ERROR: ack underflow (pkt=${a.tcp.ackn} last=${this.last_received_ackn}), resetting`,16777216);a=this.packet_reply(a,
673
- {rst:!0});this.net.receive(Qc(this.net.eth_encoder_buf,a));this.on_close();this.release();return}a.tcp.fin?(this.ack!==a.tcp.seq&&D(`TCP[${this.tuple}]: WARNING: closing connection in state "${this.state}" with invalid seq (${this.ack} != ${a.tcp.seq})`,16777216),++this.ack,b=this.packet_reply(a,{}),"established"===this.state?(b.tcp.ack=!0,this.state="close-wait",this.on_shutdown()):"fin-wait-1"===this.state?(a.tcp.ack?this.release():this.state="closing",b.tcp.ack=!0):"fin-wait-2"===this.state?(this.release(),
674
- b.tcp.ack=!0):(this.release(),this.on_close(),b.tcp.rst=!0),this.net.receive(Qc(this.net.eth_encoder_buf,b))):this.ack!==a.tcp.seq?(this.ack!==a.tcp.seq+1&&D(`Packet seq was wrong ex: ${this.ack} ~${this.ack-this.start_seq} `+`pk: ${a.tcp.seq} ~${this.start_seq-a.tcp.seq} `+`(${this.ack-a.tcp.seq}) = ${this.name}`,16777216),a=this.packet_reply(a,{ack:!0}),this.net.receive(Qc(this.net.eth_encoder_buf,a))):a.tcp.ack&&0<a.tcp_data.length&&(this.ack+=a.tcp_data.length,b=this.ipv4_reply(),this.net.receive(Qc(this.net.eth_encoder_buf,
675
- b)),this.emit("data",a.tcp_data));this.pump()}};Wc.prototype.write=function(a){this.in_active_close||this.send_buffer.write(a);this.pump()};Wc.prototype.writev=function(a){if(!this.in_active_close)for(const b of a)this.send_buffer.write(b);this.pump()};Wc.prototype.close=function(){if(!this.in_active_close){this.in_active_close=!0;if("established"===this.state||"syn-received"===this.state)var a="fin-wait-1";else if("close-wait"===this.state)a="last-ack";else{"syn-sent"!==this.state&&D(`TCP[${this.tuple}]: active close in unexpected state "${this.state}"`,
676
- 16777216);this.release();return}this.send_buffer.length||this.pending?(this.delayed_send_fin=!0,this.delayed_state=a):(this.state=a,a=this.ipv4_reply(),a.tcp.fin=!0,this.net.receive(Qc(this.net.eth_encoder_buf,a)))}this.pump()};Wc.prototype.on_shutdown=function(){this.emit("shutdown")};Wc.prototype.on_close=function(){this.emit("close")};Wc.prototype.release=function(){this.net.tcp_conn[this.tuple]&&(this.state="closed",delete this.net.tcp_conn[this.tuple])};Wc.prototype.pump=function(){if(this.send_buffer.length&&
677
- !this.pending){const a=this.send_chunk_buf,b=this.send_buffer.peek(a),c=this.ipv4_reply();c.tcp.psh=!0;c.tcp_data=a.subarray(0,b);this.net.receive(Qc(this.net.eth_encoder_buf,c));this.pending=!0}};function Yc(a,b){b=b||{};this.bus=a;this.id=b.id||0;this.router_mac=new Uint8Array((b.router_mac||"52:54:0:1:2:3").split(":").map(function(c){return parseInt(c,16)}));this.router_ip=new Uint8Array((b.router_ip||"192.168.86.1").split(".").map(function(c){return parseInt(c,10)}));this.vm_ip=new Uint8Array((b.vm_ip||
678
- "192.168.86.100").split(".").map(function(c){return parseInt(c,10)}));this.masquerade=void 0===b.masquerade||!!b.masquerade;this.vm_mac=new Uint8Array(6);this.dns_method=b.dns_method||"static";this.doh_server=b.doh_server;this.tcp_conn={};this.eth_encoder_buf=Nc();this.fetch=(...c)=>fetch(...c);this.cors_proxy=b.cors_proxy;this.bus.register("net"+this.id+"-mac",function(c){this.vm_mac=new Uint8Array(c.split(":").map(function(d){return parseInt(d,16)}))},this);this.bus.register("net"+this.id+"-send",
679
- function(c){this.send(c)},this)}g.exportSymbol("FetchNetworkAdapter$$module$src$browser$fetch_network",Yc);Yc.prototype.destroy=function(){};Yc.prototype.on_tcp_connection=function(a,b){if(80===a.tcp.dport){let c=new Wc;c.state="syn-received";c.net=this;c.on("data",Zc);c.tuple=b;c.accept(a);this.tcp_conn[b]=c;return!0}return!1};Yc.prototype.connect=function(a){return Vc(a,this)};Yc.prototype.tcp_probe=function(a){return Xc(a,this)};async function Zc(a){this.read=this.read||"";if((this.read+=(new TextDecoder).decode(a))&&
680
- -1!==this.read.indexOf("\r\n\r\n")){var b=this.read.indexOf("\r\n\r\n");a=this.read.substring(0,b).split(/\r\n/);b=this.read.substring(b+4);this.read="";let c=a[0].split(" "),d;d=/^https?:/.test(c[1])?new URL(c[1]):new URL("http://host"+c[1]);"undefined"!==typeof window&&"http:"===d.protocol&&"https:"===window.location.protocol&&(d.protocol="https:");let e=new Headers;for(let n=1;n<a.length;++n){const p=this.net.parse_http_header(a[n]);if(!p){console.warn('The request contains an invalid header: "%s"',
681
- a[n]);this.write((new TextEncoder).encode("HTTP/1.1 400 Bad Request\r\nContent-Length: 0"));return}"host"===p.key.toLowerCase()?d.host=p.value:e.append(p.key,p.value)}D("HTTP Dispatch: "+d.href,16777216);this.name=d.href;a={method:c[0],headers:e};-1!==["put","post"].indexOf(a.method.toLowerCase())&&(a.body=b);const f=this.net.cors_proxy?this.net.cors_proxy+encodeURIComponent(d.href):d.href,h=new TextEncoder;let l=!1;this.net.fetch(f,a).then(n=>{const p=[`HTTP/1.1 ${n.status} ${n.statusText}`,`x-was-fetch-redirected: ${!!n.redirected}`,
682
- `x-fetch-resp-url: ${n.url}`,"Connection: closed"];for(const [q,r]of n.headers.entries())["content-encoding","connection","content-length","transfer-encoding"].includes(q.toLowerCase())||p.push(`${q}: ${r}`);this.write(h.encode(p.join("\r\n")+"\r\n\r\n"));l=!0;if(n.body&&n.body.getReader){const q=n.body.getReader(),r=({value:t,done:v})=>{t&&this.write(t);if(v)this.close();else return q.read().then(r)};q.read().then(r)}else n.arrayBuffer().then(q=>{this.write(new Uint8Array(q));this.close()})}).catch(n=>
683
- {console.warn("Fetch Failed: "+f+"\n"+n);l||(n=h.encode(`Fetch ${f} failed:\n\n${n.stack||n.message}`),this.writev([h.encode(["HTTP/1.1 502 Fetch Error","Content-Type: text/plain",`Content-Length: ${n.length}`,"Connection: closed"].join("\r\n")+"\r\n\r\n"),n]));this.close()})}}Yc.prototype.fetch=async function(a,b){this.cors_proxy&&(a=this.cors_proxy+encodeURIComponent(a));try{const c=await fetch(a,b),d=await c.arrayBuffer();return[c,d]}catch(c){return console.warn("Fetch Failed: "+a+"\n"+c),b=new Headers,
684
- b.set("Content-Type","text/plain"),[{status:502,statusText:"Fetch Error",headers:b},(new TextEncoder).encode(`Fetch ${a} failed:\n\n${c.stack}`).buffer]}};Yc.prototype.parse_http_header=function(a){var b=a.match(/^([^:]*):(.*)$/);if(b)if(a=b[1],b=b[2].trim(),0===a.length)D("Header key is empty, raw header",16777216);else if(0===b.length)D("Header value is empty",16777216);else if(/^[\w-]+$/.test(a)){if(/^[\x20-\x7E]+$/.test(b))return{key:a,value:b};D("Header value contains forbidden characters",16777216)}else D("Header key contains forbidden characters",
685
- 16777216);else D("Unable to parse HTTP header",16777216)};Yc.prototype.send=function(a){Tc(a,this)};Yc.prototype.receive=function(a){this.bus.send("net"+this.id+"-receive",new Uint8Array(a))};function $c(a,b,c){this.register_ws(a);this.last_stream=1;this.connections={0:{congestion:0}};this.congested_buffer=[];c=c||{};this.bus=b;this.id=c.id||0;this.router_mac=new Uint8Array((c.router_mac||"52:54:0:1:2:3").split(":").map(function(d){return parseInt(d,16)}));this.router_ip=new Uint8Array((c.router_ip||
686
- "192.168.86.1").split(".").map(function(d){return parseInt(d,10)}));this.vm_ip=new Uint8Array((c.vm_ip||"192.168.86.100").split(".").map(function(d){return parseInt(d,10)}));this.masquerade=void 0===c.masquerade||!!c.masquerade;this.vm_mac=new Uint8Array(6);this.dns_method=c.dns_method||"doh";this.doh_server=c.doh_server;this.tcp_conn={};this.eth_encoder_buf=Nc();this.bus.register("net"+this.id+"-mac",function(d){this.vm_mac=new Uint8Array(d.split(":").map(function(e){return parseInt(e,16)}))},this);
687
- this.bus.register("net"+this.id+"-send",function(d){this.send(d)},this)}$c.prototype.register_ws=function(a){this.wispws=new WebSocket(a.replace("wisp://","ws://").replace("wisps://","wss://"));this.wispws.binaryType="arraybuffer";this.wispws.onmessage=b=>{this.process_incoming_wisp_frame(new Uint8Array(b.data))};this.wispws.onclose=()=>{setTimeout(()=>{this.register_ws(a)},1E4)}};$c.prototype.send_packet=function(a,b,c){this.connections[c]&&(0<this.connections[c].congestion?("DATA"===b&&this.connections[c].congestion--,
688
- this.wispws.send(a)):(this.connections[c].congested=!0,this.congested_buffer.push({data:a,type:b})))};$c.prototype.process_incoming_wisp_frame=function(a){const b=new DataView(a.buffer),c=b.getUint32(1,!0);switch(a[0]){case 1:D("Server sent client-only packet CONNECT",1048576);break;case 2:if(this.connections[c])this.connections[c].data_callback(a.slice(5));else throw Error("Got a DATA packet but stream not registered. ID: "+c);break;case 3:this.connections[c]&&(this.connections[c].congestion=b.getUint32(5,
689
- !0));if(this.connections[c].congested){for(const d of this.congested_buffer)this.send_packet(d.data,d.type,c);this.connections[c].congested=!1}break;case 4:this.connections[c]&&this.connections[c].close_callback(b.getUint8(5));delete this.connections[c];break;case 5:D("got a wisp V2 upgrade request, ignoring",1048576);break;default:D("Wisp server returned unknown packet: "+a[0],1048576)}};$c.prototype.send_wisp_frame=function(a){let b,c;switch(a.type){case "CONNECT":const d=(new TextEncoder).encode(a.hostname);
690
- b=new Uint8Array(8+d.length);c=new DataView(b.buffer);c.setUint8(0,1);c.setUint32(1,a.stream_id,!0);c.setUint8(5,1);c.setUint16(6,a.port,!0);b.set(d,8);this.connections[a.stream_id]={data_callback:a.data_callback,close_callback:a.close_callback,congestion:this.connections[0].congestion};break;case "DATA":b=new Uint8Array(5+a.data.length);c=new DataView(b.buffer);c.setUint8(0,2);c.setUint32(1,a.stream_id,!0);b.set(a.data,5);break;case "CLOSE":b=new Uint8Array(6);c=new DataView(b.buffer);c.setUint8(0,
691
- 4);c.setUint32(1,a.stream_id,!0);c.setUint8(5,a.reason);break;default:D("Client tried to send unknown packet: "+a.type,1048576)}this.send_packet(b,a.type,a.stream_id)};$c.prototype.destroy=function(){this.wispws&&(this.wispws.onmessage=null,this.wispws.onclose=null,this.wispws.close(),this.wispws=null)};$c.prototype.on_tcp_connection=function(a,b){let c=new Wc;c.state="syn-received";c.net=this;c.tuple=b;c.stream_id=this.last_stream++;this.tcp_conn[b]=c;c.on("data",d=>{0!==d.length&&this.send_wisp_frame({type:"DATA",
692
- stream_id:c.stream_id,data:d})});c.on_close=()=>{this.send_wisp_frame({type:"CLOSE",stream_id:c.stream_id,reason:2})};c.on_shutdown=c.on_close;this.send_wisp_frame({type:"CONNECT",stream_id:c.stream_id,hostname:a.ipv4.dest.join("."),port:a.tcp.dport,data_callback:d=>{c.write(d)},close_callback:()=>{c.close()}});c.accept(a);return!0};$c.prototype.send=function(a){Tc(a,this)};$c.prototype.receive=function(a){this.bus.send("net"+this.id+"-receive",new Uint8Array(a))};function ad(a,b){b=b.id||0;this.bus=
693
- a;this.bus_send_msgid=`net${b}-send`;this.bus_recv_msgid=`net${b}-receive`;this.channel=new BroadcastChannel(`v86-inbrowser-${b}`);this.is_open=!0;this.nic_to_hub_fn=c=>{this.channel.postMessage(c)};this.bus.register(this.bus_send_msgid,this.nic_to_hub_fn,this);this.hub_to_nic_fn=c=>{this.bus.send(this.bus_recv_msgid,c.data)};this.channel.addEventListener("message",this.hub_to_nic_fn)}ad.prototype.destroy=function(){this.is_open&&(this.bus.unregister(this.bus_send_msgid,this.nic_to_hub_fn),this.channel.removeEventListener("message",
694
- this.hub_to_nic_fn),this.channel.close(),this.is_open=!1)};function Z(a){"number"===typeof a.log_level&&(ca=a.log_level);this.cpu_is_running=!1;this.cpu_exception_hook=function(){};var b=Ka.create();this.bus=b[0];this.emulator_bus=b[1];var c,d;const e=new WebAssembly.Table({element:"anyfunc",initial:1924});b={cpu_exception_hook:h=>this.cpu_exception_hook(h),run_hardware_timers:function(h,l){return c.run_hardware_timers(h,l)},cpu_event_halt:()=>{this.emulator_bus.send("cpu-event-halt")},abort:function(){m(!1)},
695
- microtick:J.microtick,get_rand_int:function(){return fa()},apic_acknowledge_irq:function(){return c.devices.apic.acknowledge_irq()},stop_idling:function(){return c.stop_idling()},io_port_read8:function(h){return c.io.port_read8(h)},io_port_read16:function(h){return c.io.port_read16(h)},io_port_read32:function(h){return c.io.port_read32(h)},io_port_write8:function(h,l){c.io.port_write8(h,l)},io_port_write16:function(h,l){c.io.port_write16(h,l)},io_port_write32:function(h,l){c.io.port_write32(h,l)},
696
- mmap_read8:function(h){return c.mmap_read8(h)},mmap_read16:function(h){return c.mmap_read16(h)},mmap_read32:function(h){return c.mmap_read32(h)},mmap_write8:function(h,l){c.mmap_write8(h,l)},mmap_write16:function(h,l){c.mmap_write16(h,l)},mmap_write32:function(h,l){c.mmap_write32(h,l)},mmap_write64:function(h,l,n){c.mmap_write64(h,l,n)},mmap_write128:function(h,l,n,p,q){c.mmap_write128(h,l,n,p,q)},log_from_wasm:function(h,l){h=String.fromCharCode(...(new Uint8Array(d.buffer,h>>>0,l>>>0)));D(h,2)},
697
- console_log_from_wasm:function(h,l){h=String.fromCharCode(...(new Uint8Array(d.buffer,h>>>0,l>>>0)));console.error(h)},dbg_trace_from_wasm:function(){D(Error().stack,2)},codegen_finalize:(h,l,n,p,q)=>{c.codegen_finalize(h,l,n,p,q)},jit_clear_func:h=>c.jit_clear_func(h),jit_clear_all_funcs:()=>c.jit_clear_all_funcs(),__indirect_function_table:e};let f=a.wasm_fn;f||(f=h=>new Promise(l=>{let n="v86-debug.wasm",p="v86-fallback.wasm";if(a.wasm_path){n=a.wasm_path;const q=n.lastIndexOf("/");p=(-1===q?"":
698
- n.substr(0,q))+"/"+p}else"undefined"===typeof window&&"string"===typeof __dirname?(n=__dirname+"/"+n,p=__dirname+"/"+p):(n="build/"+n,p="build/"+p);va(n,{done:async q=>{try{const {instance:r}=await WebAssembly.instantiate(q,h);this.wasm_source=q;l(r.exports)}catch(r){va(p,{done:async t=>{const {instance:v}=await WebAssembly.instantiate(t,h);this.wasm_source=t;l(v.exports)}})}},progress:q=>{this.emulator_bus.send("download-progress",{file_index:0,file_count:1,file_name:n,lengthComputable:q.lengthComputable,
699
- total:q.total,loaded:q.loaded})}})}));f({env:b}).then(h=>{d=h.memory;h.rust_init();h=this.v86=new J(this.emulator_bus,{exports:h,wasm_table:e});c=h.cpu;this.continue_init(h,a)});this.zstd_worker=null;this.zstd_worker_request_id=0}g.exportSymbol("V86$$module$src$browser$starter",Z);Z.prototype.continue_init=async function(a,b){function c(t,v){switch(t){case "hda":e.hda=this.disk_images.hda=v;break;case "hdb":e.hdb=this.disk_images.hdb=v;break;case "cdrom":e.cdrom=this.disk_images.cdrom=v;break;case "fda":e.fda=
700
- this.disk_images.fda=v;break;case "fdb":e.fdb=this.disk_images.fdb=v;break;case "multiboot":e.multiboot=this.disk_images.multiboot=v.buffer;break;case "bzimage":e.bzimage=this.disk_images.bzimage=v.buffer;break;case "initrd":e.initrd=this.disk_images.initrd=v.buffer;break;case "bios":e.bios=v.buffer;break;case "vga_bios":e.vga_bios=v.buffer;break;case "initial_state":e.initial_state=v.buffer;break;case "fs9p_json":e.fs9p_json=v;break;default:m(!1,t)}}async function d(){if(e.fs9p&&e.fs9p_json)if(e.initial_state)D("Filesystem basefs ignored: Overridden by state image");
701
- else{if(e.fs9p.load_from_json(e.fs9p_json),b.bzimage_initrd_from_filesystem){const {bzimage_path:t,initrd_path:v}=this.get_bzimage_initrd_from_filesystem(e.fs9p);D("Found bzimage: "+t+" and initrd: "+v);const [y,C]=await Promise.all([e.fs9p.read_file(v),e.fs9p.read_file(t)]);c.call(this,"initrd",new Aa(y.buffer));c.call(this,"bzimage",new Aa(C.buffer))}}else m(!b.bzimage_initrd_from_filesystem||e.initial_state,"bzimage_initrd_from_filesystem: Requires a filesystem");this.serial_adapter&&this.serial_adapter.show&&
702
- this.serial_adapter.show();this.v86.init(e);e.initial_state&&(a.restore_state(e.initial_state),e.initial_state=void 0);b.autostart&&this.v86.run();this.emulator_bus.send("emulator-loaded")}this.bus.register("emulator-stopped",function(){this.cpu_is_running=!1;this.screen_adapter.pause()},this);this.bus.register("emulator-started",function(){this.cpu_is_running=!0;this.screen_adapter.continue()},this);var e={};this.disk_images={fda:void 0,fdb:void 0,hda:void 0,hdb:void 0,cdrom:void 0};var f=b.boot_order?
703
- b.boot_order:b.fda?801:b.hda?786:291;e.acpi=b.acpi;e.disable_jit=b.disable_jit;e.load_devices=!0;e.memory_size=b.memory_size||67108864;e.vga_memory_size=b.vga_memory_size||8388608;e.boot_order=f;e.fastboot=b.fastboot||!1;e.fda=void 0;e.fdb=void 0;e.uart1=b.uart1;e.uart2=b.uart2;e.uart3=b.uart3;e.cmdline=b.cmdline;e.preserve_mac_from_state_image=b.preserve_mac_from_state_image;e.mac_address_translation=b.mac_address_translation;e.cpuid_level=b.cpuid_level;e.virtio_balloon=b.virtio_balloon;e.virtio_console=
704
- b.virtio_console;e.virtio_net=b.virtio_net;e.screen_options=b.screen_options;if(f=b.network_relay_url||b.net_device&&b.net_device.relay_url)"fetch"===f?this.network_adapter=new Yc(this.bus,b.net_device):"inbrowser"===f?this.network_adapter=new ad(this.bus,b.net_device):f.startsWith("wisp://")||f.startsWith("wisps://")?this.network_adapter=new $c(f,this.bus,b.net_device):this.network_adapter=new Dc(f,this.bus);e.net_device=b.net_device||{type:"ne2k"};f=b.screen||{};b.screen_container&&(f.container=
705
- b.screen_container);b.disable_keyboard||(this.keyboard_adapter=new sc(this.bus));b.disable_mouse||(this.mouse_adapter=new tc(this.bus,f.container));this.screen_adapter=f.container?new za(f,()=>this.v86.cpu.devices.vga&&this.v86.cpu.devices.vga.screen_fill_buffer()):new Ub;e.screen=this.screen_adapter;e.screen_options=f;b.serial_container&&(this.serial_adapter=new Bc(b.serial_container,this.bus));b.serial_container_xtermjs&&(this.serial_adapter=new Cc(b.serial_container_xtermjs,this.bus));b.disable_speaker||
706
- (this.speaker_adapter=new uc(this.bus));var h=[];f=(t,v)=>{if(v)if(v.get&&v.set&&v.load)h.push({name:t,loadable:v});else{if("bios"===t||"vga_bios"===t||"initial_state"===t||"multiboot"===t||"bzimage"===t||"initrd"===t)v.async=!1;if("fda"===t||"fdb"===t)v.async=!1;v.url&&!v.async?h.push({name:t,url:v.url,size:v.size}):h.push({name:t,loadable:Ga(v,this.zstd_decompress_worker.bind(this))})}};b.state&&console.warn("Warning: Unknown option 'state'. Did you mean 'initial_state'?");f("bios",b.bios);f("vga_bios",
707
- b.vga_bios);f("cdrom",b.cdrom);f("hda",b.hda);f("hdb",b.hdb);f("fda",b.fda);f("fdb",b.fdb);f("initial_state",b.initial_state);f("multiboot",b.multiboot);f("bzimage",b.bzimage);f("initrd",b.initrd);if(b.filesystem){f=b.filesystem.basefs;var l=b.filesystem.baseurl;let t=new hc;l&&(t=new ic(t,l));e.fs9p=this.fs9p=new Y(t);if(f){m(l,"Filesystem: baseurl must be specified");if("object"===typeof f){var n=f.size;f=f.url}m("string"===typeof f);h.push({name:"fs9p_json",url:f,size:n,as_json:!0})}}var p=this,
708
- q=h.length,r=function(t){if(t===q)setTimeout(d.bind(this),0);else{var v=h[t];v.loadable?(v.loadable.onload=function(){c.call(this,v.name,v.loadable);r(t+1)}.bind(this),v.loadable.load()):va(v.url,{done:function(y){v.url.endsWith(".zst")&&"initial_state"!==v.name&&(m(v.size,"A size must be provided for compressed images"),y=this.zstd_decompress(v.size,new Uint8Array(y)));c.call(this,v.name,v.as_json?y:new Aa(y));r(t+1)}.bind(this),progress:function(y){200===y.target.status?p.emulator_bus.send("download-progress",
709
- {file_index:t,file_count:q,file_name:v.url,lengthComputable:y.lengthComputable,total:y.total||v.size,loaded:y.loaded}):p.emulator_bus.send("download-error",{file_index:t,file_count:q,file_name:v.url,request:y.target})},as_json:v.as_json})}}.bind(this);r(0)};Z.prototype.zstd_decompress=function(a,b){const c=this.v86.cpu;m(!this.zstd_context);this.zstd_context=c.zstd_create_ctx(b.length);(new Uint8Array(c.wasm_memory.buffer)).set(b,c.zstd_get_src_ptr(this.zstd_context));b=c.zstd_read(this.zstd_context,
710
- a);const d=c.wasm_memory.buffer.slice(b,b+a);c.zstd_read_free(b,a);c.zstd_free_ctx(this.zstd_context);this.zstd_context=null;return d};Z.prototype.zstd_decompress_worker=async function(a,b){if(!this.zstd_worker){const c=URL.createObjectURL(new Blob(["("+function(){let d;globalThis.onmessage=function(e){if(d){var {src:f,decompressed_size:h,id:l}=e.data;e=d.exports;var n=e.zstd_create_ctx(f.length);(new Uint8Array(e.memory.buffer)).set(f,e.zstd_get_src_ptr(n));var p=e.zstd_read(n,h),q=e.memory.buffer.slice(p,
711
- p+h);e.zstd_read_free(p,h);e.zstd_free_ctx(n);postMessage({result:q,id:l},[q])}else n=Object.fromEntries("cpu_exception_hook run_hardware_timers cpu_event_halt microtick get_rand_int apic_acknowledge_irq stop_idling io_port_read8 io_port_read16 io_port_read32 io_port_write8 io_port_write16 io_port_write32 mmap_read8 mmap_read16 mmap_read32 mmap_write8 mmap_write16 mmap_write32 mmap_write64 mmap_write128 codegen_finalize jit_clear_func jit_clear_all_funcs".split(" ").map(r=>[r,()=>console.error("zstd worker unexpectedly called "+
712
- r)])),n.__indirect_function_table=new WebAssembly.Table({element:"anyfunc",initial:1024}),n.abort=()=>{throw Error("zstd worker aborted");},n.log_from_wasm=n.console_log_from_wasm=(r,t)=>{console.log(String.fromCharCode(...(new Uint8Array(d.exports.memory.buffer,r,t))))},n.dbg_trace_from_wasm=()=>console.trace(),d=new WebAssembly.Instance(new WebAssembly.Module(e.data),{env:n})}}.toString()+")()"],{type:"text/javascript"}));this.zstd_worker=new Worker(c);URL.revokeObjectURL(c);this.zstd_worker.postMessage(this.wasm_source,
713
- [this.wasm_source])}return new Promise(c=>{const d=this.zstd_worker_request_id++,e=async f=>{f.data.id===d&&(this.zstd_worker.removeEventListener("message",e),m(a===f.data.result.byteLength),c(f.data.result))};this.zstd_worker.addEventListener("message",e);this.zstd_worker.postMessage({src:b,decompressed_size:a,id:d},[b.buffer])})};Z.prototype.get_bzimage_initrd_from_filesystem=function(a){const b=(a.read_dir("/")||[]).map(e=>"/"+e);a=(a.read_dir("/boot/")||[]).map(e=>"/boot/"+e);let c,d;for(const e of[].concat(b,
714
- a)){const f=/old/i.test(e)||/fallback/i.test(e),h=/vmlinuz/i.test(e)||/bzimage/i.test(e),l=/initrd/i.test(e)||/initramfs/i.test(e);!h||d&&f||(d=e);!l||c&&f||(c=e)}c&&d||(console.log("Failed to find bzimage or initrd in filesystem. Files:"),console.log(b.join(" ")),console.log(a.join(" ")));return{initrd_path:c,bzimage_path:d}};Z.prototype.run=async function(){this.v86.run()};g.exportProperty(Z.prototype,"run",Z.prototype.run);Z.prototype.stop=async function(){this.cpu_is_running&&await new Promise(a=>
715
- {const b=()=>{this.remove_listener("emulator-stopped",b);a()};this.add_listener("emulator-stopped",b);this.v86.stop()})};g.exportProperty(Z.prototype,"stop",Z.prototype.stop);Z.prototype.destroy=async function(){await this.stop();this.v86.destroy();this.keyboard_adapter&&this.keyboard_adapter.destroy();this.network_adapter&&this.network_adapter.destroy();this.mouse_adapter&&this.mouse_adapter.destroy();this.screen_adapter&&this.screen_adapter.destroy();this.serial_adapter&&this.serial_adapter.destroy();
716
- this.speaker_adapter&&this.speaker_adapter.destroy()};g.exportProperty(Z.prototype,"destroy",Z.prototype.destroy);Z.prototype.restart=function(){this.v86.restart()};g.exportProperty(Z.prototype,"restart",Z.prototype.restart);Z.prototype.add_listener=function(a,b){this.bus.register(a,b,this)};g.exportProperty(Z.prototype,"add_listener",Z.prototype.add_listener);Z.prototype.remove_listener=function(a,b){this.bus.unregister(a,b)};g.exportProperty(Z.prototype,"remove_listener",Z.prototype.remove_listener);
717
- Z.prototype.restore_state=async function(a){m(1===arguments.length);this.v86.restore_state(a)};g.exportProperty(Z.prototype,"restore_state",Z.prototype.restore_state);Z.prototype.save_state=async function(){m(0===arguments.length);return this.v86.save_state()};g.exportProperty(Z.prototype,"save_state",Z.prototype.save_state);Z.prototype.get_instruction_counter=function(){return this.v86?this.v86.cpu.instruction_counter[0]>>>0:0};g.exportProperty(Z.prototype,"get_instruction_counter",Z.prototype.get_instruction_counter);
718
- Z.prototype.is_running=function(){return this.cpu_is_running};g.exportProperty(Z.prototype,"is_running",Z.prototype.is_running);Z.prototype.set_fda=async function(a){if(a.url&&!a.async)va(a.url,{done:b=>{this.v86.cpu.devices.fdc.set_fda(new Aa(b))}});else{const b=Ga(a,this.zstd_decompress_worker.bind(this));b.onload=()=>{this.v86.cpu.devices.fdc.set_fda(b)};await b.load()}};g.exportProperty(Z.prototype,"set_fda",Z.prototype.set_fda);Z.prototype.eject_fda=function(){this.v86.cpu.devices.fdc.eject_fda()};
719
- g.exportProperty(Z.prototype,"eject_fda",Z.prototype.eject_fda);Z.prototype.keyboard_send_scancodes=function(a){for(var b=0;b<a.length;b++)this.bus.send("keyboard-code",a[b])};g.exportProperty(Z.prototype,"keyboard_send_scancodes",Z.prototype.keyboard_send_scancodes);Z.prototype.keyboard_send_keys=function(a){for(var b=0;b<a.length;b++)this.keyboard_adapter.simulate_press(a[b])};g.exportProperty(Z.prototype,"keyboard_send_keys",Z.prototype.keyboard_send_keys);Z.prototype.keyboard_send_text=function(a){for(var b=
720
- 0;b<a.length;b++)this.keyboard_adapter.simulate_char(a[b])};g.exportProperty(Z.prototype,"keyboard_send_text",Z.prototype.keyboard_send_text);Z.prototype.screen_make_screenshot=function(){return this.screen_adapter?this.screen_adapter.make_screenshot():null};g.exportProperty(Z.prototype,"screen_make_screenshot",Z.prototype.screen_make_screenshot);Z.prototype.screen_set_scale=function(a,b){this.screen_adapter&&this.screen_adapter.set_scale(a,b)};g.exportProperty(Z.prototype,"screen_set_scale",Z.prototype.screen_set_scale);
721
- Z.prototype.screen_go_fullscreen=function(){if(this.screen_adapter){var a=document.getElementById("screen_container");if(a){var b=a.requestFullScreen||a.webkitRequestFullscreen||a.mozRequestFullScreen||a.msRequestFullScreen;b&&(b.call(a),(a=document.getElementsByClassName("phone_keyboard")[0])&&a.focus());try{navigator.keyboard.lock()}catch(c){}this.lock_mouse()}}};g.exportProperty(Z.prototype,"screen_go_fullscreen",Z.prototype.screen_go_fullscreen);Z.prototype.lock_mouse=function(){var a=document.body,
722
- b=a.requestPointerLock||a.mozRequestPointerLock||a.webkitRequestPointerLock;b&&b.call(a)};g.exportProperty(Z.prototype,"lock_mouse",Z.prototype.lock_mouse);Z.prototype.mouse_set_status=function(a){this.mouse_adapter&&(this.mouse_adapter.emu_enabled=a)};Z.prototype.keyboard_set_status=function(a){this.keyboard_adapter&&(this.keyboard_adapter.emu_enabled=a)};g.exportProperty(Z.prototype,"keyboard_set_status",Z.prototype.keyboard_set_status);Z.prototype.serial0_send=function(a){for(var b=0;b<a.length;b++)this.bus.send("serial0-input",
723
- a.charCodeAt(b))};g.exportProperty(Z.prototype,"serial0_send",Z.prototype.serial0_send);Z.prototype.serial_send_bytes=function(a,b){for(var c=0;c<b.length;c++)this.bus.send("serial"+a+"-input",b[c])};g.exportProperty(Z.prototype,"serial_send_bytes",Z.prototype.serial_send_bytes);Z.prototype.serial_set_modem_status=function(a,b){this.bus.send("serial"+a+"-modem-status-input",b)};Z.prototype.serial_set_carrier_detect=function(a,b){this.bus.send("serial"+a+"-carrier-detect-input",b)};Z.prototype.serial_set_ring_indicator=
724
- function(a,b){this.bus.send("serial"+a+"-ring-indicator-input",b)};Z.prototype.serial_set_data_set_ready=function(a,b){this.bus.send("serial"+a+"-data-set-ready-input",b)};Z.prototype.serial_set_clear_to_send=function(a,b){this.bus.send("serial"+a+"-clear-to-send-input",b)};Z.prototype.mount_fs=async function(a,b,c){var d=new hc;b&&(d=new ic(d,b));d=new Y(d,this.fs9p.qidcounter);b&&(m("object"===typeof c,"Filesystem: basefs must be a JSON object"),d.load_from_json(c));a=this.fs9p.Mount(a,d);if(-2===
725
- a)throw new bd;if(-17===a)throw new cd;if(0>a)throw m(!1,"Unexpected error code: "+-a),Error("Failed to mount. Error number: "+-a);};g.exportProperty(Z.prototype,"mount_fs",Z.prototype.mount_fs);Z.prototype.create_file=async function(a,b){m(2===arguments.length);var c=this.fs9p;if(c){var d=a.split("/");d=d[d.length-1];var e=c.SearchPath(a).parentid;if(""!==d&&-1!==e)await c.CreateBinaryFile(d,e,b);else return Promise.reject(new bd)}};g.exportProperty(Z.prototype,"create_file",Z.prototype.create_file);
726
- Z.prototype.read_file=async function(a){m(1===arguments.length);var b=this.fs9p;if(b)return(b=await b.read_file(a))?b:Promise.reject(new bd)};g.exportProperty(Z.prototype,"read_file",Z.prototype.read_file);Z.prototype.automatically=function(a){const b=c=>{const d=c[0];if(d){var e=c.slice(1);d.sleep?setTimeout(()=>b(e),1E3*d.sleep):d.vga_text?this.wait_until_vga_screen_contains(d.vga_text).then(()=>b(e)):d.keyboard_send?(Array.isArray(d.keyboard_send)?this.keyboard_send_scancodes(d.keyboard_send):
727
- (m("string"===typeof d.keyboard_send),this.keyboard_send_text(d.keyboard_send)),b(e)):d.call?(d.call(),b(e)):m(!1,d)}};b(a)};Z.prototype.wait_until_vga_screen_contains=function(a){return new Promise(b=>{function c(h){return"string"===typeof a?h.includes(a):a.test(h)}function d(h){[h]=h;e.add(h)}for(const h of this.screen_adapter.get_text_screen())if(c(h)){b(!0);return}const e=new Set,f=()=>{for(const h of e){const l=this.screen_adapter.get_text_row(h);if(c(l)){this.remove_listener("screen-put-char",
728
- d);b();return}}e.clear();setTimeout(f,100)};f();this.add_listener("screen-put-char",d)})};Z.prototype.read_memory=function(a,b){return this.v86.cpu.read_blob(a,b)};Z.prototype.write_memory=function(a,b){this.v86.cpu.write_blob(a,b)};Z.prototype.set_serial_container_xtermjs=function(a){this.serial_adapter&&this.serial_adapter.destroy&&this.serial_adapter.destroy();this.serial_adapter=new Cc(a,this.bus);this.serial_adapter.show()};function cd(a){this.message=a||"File already exists"}cd.prototype=Error.prototype;
729
- function bd(a){this.message=a||"File not found"}bd.prototype=Error.prototype;"undefined"!==typeof module&&"undefined"!==typeof module.exports&&(module.exports.V86=Z,module.exports.print_stats=Ec);export{};; export default module.exports.V86; export let {V86, CPU} = module.exports;
515
+ 512}};W.prototype.data_allocate=function(a){this.data_allocate_noclear(a);for(var b=0;b<a+3>>2;b++)this.data32[b]=0};W.prototype.data_allocate_noclear=function(a){this.data.length<a&&(this.data=new Uint8Array(a+3&-4),this.data16=new Uint16Array(this.data.buffer),this.data32=new Int32Array(this.data.buffer));this.data_length=a;this.data_pointer=0};W.prototype.data_set=function(a){this.data_allocate_noclear(a.length);this.data.set(a)};W.prototype.report_read_start=function(){this.bus.send("ide-read-start")};
516
+ W.prototype.report_read_end=function(a){this.bus.send("ide-read-end",[this.nr,a,a/this.sector_size|0])};W.prototype.report_write=function(a){this.bus.send("ide-write-end",[this.nr,a,a/this.sector_size|0])};W.prototype.read_buffer=function(a,b,c){const d=this.last_io_id++;this.in_progress_io_ids.add(d);this.buffer.get(a,b,e=>{if(this.cancelled_io_ids.delete(d))k(!this.in_progress_io_ids.has(d));else{var f=this.in_progress_io_ids.delete(d);k(f);c(e)}})};W.prototype.cancel_io_operations=function(){for(const a of this.in_progress_io_ids)this.cancelled_io_ids.add(a);
517
+ this.in_progress_io_ids.clear()};W.prototype.get_state=function(){var a=[];a[0]=this.bytecount;a[1]=this.cylinder_count;a[2]=this.cylinder_high;a[3]=this.cylinder_low;a[4]=this.data_pointer;a[5]=0;a[6]=0;a[7]=0;a[8]=0;a[9]=this.drive_head;a[10]=this.error;a[11]=this.head;a[12]=this.head_count;a[13]=this.is_atapi;a[14]=this.is_lba;a[15]=this.lba_count;a[16]=this.data;a[17]=this.data_length;a[18]=this.sector;a[19]=this.sector_count;a[20]=this.sector_size;a[21]=this.sectors_per_drq;a[22]=this.sectors_per_track;
518
+ a[23]=this.status;a[24]=this.write_dest;a[25]=this.current_command;a[26]=this.data_end;a[27]=this.current_atapi_command;a[28]=this.buffer;return a};W.prototype.set_state=function(a){this.bytecount=a[0];this.cylinder_count=a[1];this.cylinder_high=a[2];this.cylinder_low=a[3];this.data_pointer=a[4];this.drive_head=a[9];this.error=a[10];this.head=a[11];this.head_count=a[12];this.is_atapi=a[13];this.is_lba=a[14];this.lba_count=a[15];this.data=a[16];this.data_length=a[17];this.sector=a[18];this.sector_count=
519
+ a[19];this.sector_size=a[20];this.sectors_per_drq=a[21];this.sectors_per_track=a[22];this.status=a[23];this.write_dest=a[24];this.current_command=a[25];this.data_end=a[26];this.current_atapi_command=a[27];this.data16=new Uint16Array(this.data.buffer);this.data32=new Int32Array(this.data.buffer);this.buffer&&this.buffer.set_state(a[28])};function Uc(a,b,c){this.bus=b;this.id=a.devices.net?1:0;this.status=this.pairs=1;this.preserve_mac_from_state_image=c;this.mac=new Uint8Array([0,34,21,255*Math.random()|
520
+ 0,255*Math.random()|0,255*Math.random()|0]);this.bus.send("net"+this.id+"-mac",Fc(this.mac));b=[];for(c=0;c<this.pairs;++c)b.push({size_supported:1024,notify_offset:0}),b.push({size_supported:1024,notify_offset:1});b.push({size_supported:16,notify_offset:2});this.virtio=new Ic(a,{name:"virtio-net",pci_id:80,device_id:4161,subsystem_device_id:1,common:{initial_port:51200,queues:b,features:[5,16,22,3,17,23,32],on_driver_ok:()=>{}},notification:{initial_port:51456,single_handler:!1,handlers:[()=>{},
521
+ d=>{const e=this.virtio.queues[d];for(;e.has_request();){const f=e.pop_request(),h=new Uint8Array(f.length_readable);f.get_next_blob(h);this.bus.send("net"+this.id+"-send",h.subarray(12));this.bus.send("eth-transmit-end",[h.length-12]);this.virtio.queues[d].push_reply(f)}this.virtio.queues[d].flush_replies()},d=>{if(d!==2*this.pairs)k(!1,"VirtioNet Notified for wrong queue: "+d+" (expected queue_id of 3)");else for(var e=this.virtio.queues[d];e.has_request();){const l=e.pop_request();var f=new Uint8Array(l.length_readable);
522
+ l.get_next_blob(f);var h=M(["b","b"],f,{offset:0});const p=h[0];h=h[1];switch(p<<8|h){case 1024:f=M(["h"],f,{offset:2});k(1===f[0]);this.Send(d,l,new Uint8Array([0]));break;case 257:this.mac=f.subarray(2,8);this.Send(d,l,new Uint8Array([0]));this.bus.send("net"+this.id+"-mac",Fc(this.mac));break;default:k(!1," VirtioNet received unknown command: "+p+":"+h);this.Send(d,l,new Uint8Array([1]));return}}}]},isr_status:{initial_port:50944},device_specific:{initial_port:50688,struct:[0,1,2,3,4,5].map((d,
523
+ e)=>({bytes:1,name:"mac_"+e,read:()=>this.mac[e],write:()=>{}})).concat([{bytes:2,name:"status",read:()=>this.status,write:()=>{}},{bytes:2,name:"max_pairs",read:()=>this.pairs,write:()=>{}},{bytes:2,name:"mtu",read:()=>1500,write:()=>{}}])}});this.bus.register("net"+this.id+"-receive",d=>{this.bus.send("eth-receive-end",[d.length]);const e=new Uint8Array(12+d.byteLength);(new DataView(e.buffer,e.byteOffset,e.byteLength)).setInt16(10,1);e.set(d,12);d=this.virtio.queues[0];d.has_request()?(d=d.pop_request(),
524
+ d.set_next_blob(e),this.virtio.queues[0].push_reply(d),this.virtio.queues[0].flush_replies()):console.log("No buffer to write into!")},this)}Uc.prototype.get_state=function(){const a=[];a[0]=this.virtio;a[1]=this.id;a[2]=this.mac;return a};Uc.prototype.set_state=function(a){this.virtio.set_state(a[0]);this.id=a[1];this.preserve_mac_from_state_image&&(this.mac=a[2],this.bus.send("net"+this.id+"-mac",Fc(this.mac)))};Uc.prototype.reset=function(){this.virtio.reset()};Uc.prototype.Send=function(a,b,c){b.set_next_blob(c);
525
+ this.virtio.queues[a].push_reply(b);this.virtio.queues[a].flush_replies()};Uc.prototype.Ack=function(a,b){this.virtio.queues[a].push_reply(b);this.virtio.queues[a].flush_replies()};const Vc=Uint32Array.from([655360,655360,720896,753664]),Wc=Uint32Array.from([131072,65536,32768,32768]);function X(a,b,c,d){this.cpu=a;this.bus=b;this.screen=c;this.vga_memory_size=d;this.cursor_address=0;this.cursor_scanline_start=14;this.cursor_scanline_end=15;this.max_cols=80;this.max_rows=25;this.virtual_height=this.virtual_width=
526
+ this.screen_height=this.screen_width=0;this.layers=[];this.start_address_latched=this.start_address=0;this.crtc=new Uint8Array(25);this.line_compare=this.offset_register=this.preset_row_scan=this.underline_location_register=this.vertical_blank_start=this.vertical_display_enable_end=this.horizontal_blank_start=this.horizontal_display_enable_end=this.crtc_mode=0;this.graphical_mode=!1;this.vga256_palette=new Int32Array(256);this.latch_dword=0;this.svga_version=45253;this.svga_height=this.svga_width=
527
+ 0;this.svga_enabled=!1;this.svga_bpp=32;this.svga_offset_y=this.svga_offset_x=this.svga_offset=this.svga_bank_offset=0;this.vga_memory_size=void 0===this.vga_memory_size||262144>this.vga_memory_size?262144:268435456<this.vga_memory_size?268435456:ka(this.vga_memory_size);C("effective vga memory size: "+this.vga_memory_size,256);this.pci_space=[52,18,17,17,3,1,0,0,0,0,0,3,0,0,0,0,8,14680064,57344,224,0,0,0,0,0,0,191,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,0,17,0,0,190,254,0,0,0,0,0,0,0,0,0,0,0,
528
+ 0];this.pci_id=144;this.pci_bars=[{size:this.vga_memory_size}];this.pci_rom_size=65536;this.pci_rom_address=4272947200;this.name="vga";this.dac_state=this.dac_color_index_read=this.dac_color_index_write=this.index_crtc=0;this.dac_mask=255;this.dac_map=new Uint8Array(16);this.attribute_controller_index=-1;this.palette_source=32;this.color_select=this.horizontal_panning=this.color_plane_enable=this.attribute_mode=0;this.sequencer_index=-1;this.plane_write_bm=15;this.clocking_mode=this.sequencer_memory_mode=
529
+ 0;this.graphics_index=-1;this.planar_rotate_reg=this.planar_mode=this.plane_read=this.character_map_select=0;this.planar_bitmap=255;this.max_scan_line=this.color_dont_care=this.color_compare=this.miscellaneous_graphics_register=this.planar_setreset_enable=this.planar_setreset=0;this.port_3DA_value=this.miscellaneous_output_register=255;this.font_page_ab_enabled=!1;b=a.io;b.register_write(960,this,this.port3C0_write);b.register_read(960,this,this.port3C0_read,this.port3C0_read16);b.register_read(961,
530
+ this,this.port3C1_read);b.register_write(962,this,this.port3C2_write);b.register_write_consecutive(964,this,this.port3C4_write,this.port3C5_write);b.register_read(964,this,this.port3C4_read);b.register_read(965,this,this.port3C5_read);b.register_write_consecutive(974,this,this.port3CE_write,this.port3CF_write);b.register_read(974,this,this.port3CE_read);b.register_read(975,this,this.port3CF_read);b.register_read(966,this,this.port3C6_read);b.register_write(966,this,this.port3C6_write);b.register_write(967,
531
+ this,this.port3C7_write);b.register_read(967,this,this.port3C7_read);b.register_write(968,this,this.port3C8_write);b.register_read(968,this,this.port3C8_read);b.register_write(969,this,this.port3C9_write);b.register_read(969,this,this.port3C9_read);b.register_read(972,this,this.port3CC_read);b.register_write(980,this,this.port3D4_write,this.port3D4_write16);b.register_write(981,this,this.port3D5_write,this.port3D5_write16);b.register_read(980,this,this.port3D4_read);b.register_read(981,this,this.port3D5_read,
532
+ this.port3D5_read16);b.register_write(948,this,this.port3D4_write,this.port3D4_write16);b.register_write(949,this,this.port3D5_write,this.port3D5_write16);b.register_read(948,this,this.port3D4_read);b.register_read(949,this,this.port3D5_read,this.port3D5_read16);b.register_read(970,this,function(){C("3CA read",256);return 0});b.register_read(986,this,this.port3DA_read);b.register_read(954,this,this.port3DA_read);this.dispi_index=-1;this.dispi_enable_value=0;b.register_write(462,this,void 0,this.port1CE_write);
533
+ b.register_write(463,this,void 0,this.port1CF_write);b.register_read(463,this,void 0,this.port1CF_read);c=a.svga_allocate_memory(this.vga_memory_size)>>>0;this.svga_memory=g(Uint8Array,a.wasm_memory,c,this.vga_memory_size);this.diff_addr_min=this.vga_memory_size;this.diff_addr_max=0;this.diff_plot_min=this.vga_memory_size;this.diff_plot_max=0;this.image_data=null;this.vga_memory=new Uint8Array(262144);this.plane0=new Uint8Array(this.vga_memory.buffer,0,65536);this.plane1=new Uint8Array(this.vga_memory.buffer,
534
+ 65536,65536);this.plane2=new Uint8Array(this.vga_memory.buffer,131072,65536);this.plane3=new Uint8Array(this.vga_memory.buffer,196608,65536);this.pixel_buffer=new Uint8Array(524288);b.mmap_register(655360,131072,e=>this.vga_memory_read(e),(e,f)=>this.vga_memory_write(e,f));a.devices.pci.register_device(this)}X.prototype.get_state=function(){var a=[];a[0]=this.vga_memory_size;a[1]=this.cursor_address;a[2]=this.cursor_scanline_start;a[3]=this.cursor_scanline_end;a[4]=this.max_cols;a[5]=this.max_rows;
535
+ a[6]=this.vga_memory;a[7]=this.dac_state;a[8]=this.start_address;a[9]=this.graphical_mode;a[10]=this.vga256_palette;a[11]=this.latch_dword;a[12]=this.color_compare;a[13]=this.color_dont_care;a[14]=this.miscellaneous_graphics_register;a[15]=this.svga_width;a[16]=this.svga_height;a[17]=this.crtc_mode;a[18]=this.svga_enabled;a[19]=this.svga_bpp;a[20]=this.svga_bank_offset;a[21]=this.svga_offset;a[22]=this.index_crtc;a[23]=this.dac_color_index_write;a[24]=this.dac_color_index_read;a[25]=this.dac_map;
536
+ a[26]=this.sequencer_index;a[27]=this.plane_write_bm;a[28]=this.sequencer_memory_mode;a[29]=this.graphics_index;a[30]=this.plane_read;a[31]=this.planar_mode;a[32]=this.planar_rotate_reg;a[33]=this.planar_bitmap;a[34]=this.max_scan_line;a[35]=this.miscellaneous_output_register;a[36]=this.port_3DA_value;a[37]=this.dispi_index;a[38]=this.dispi_enable_value;a[39]=this.svga_memory;a[41]=this.attribute_controller_index;a[42]=this.offset_register;a[43]=this.planar_setreset;a[44]=this.planar_setreset_enable;
537
+ a[45]=this.start_address_latched;a[46]=this.crtc;a[47]=this.horizontal_display_enable_end;a[48]=this.horizontal_blank_start;a[49]=this.vertical_display_enable_end;a[50]=this.vertical_blank_start;a[51]=this.underline_location_register;a[52]=this.preset_row_scan;a[53]=this.offset_register;a[54]=this.palette_source;a[55]=this.attribute_mode;a[56]=this.color_plane_enable;a[57]=this.horizontal_panning;a[58]=this.color_select;a[59]=this.clocking_mode;a[60]=this.line_compare;a[61]=this.pixel_buffer;a[62]=
538
+ this.dac_mask;a[63]=this.character_map_select;a[64]=this.font_page_ab_enabled;return a};X.prototype.set_state=function(a){this.vga_memory_size=a[0];this.cursor_address=a[1];this.cursor_scanline_start=a[2];this.cursor_scanline_end=a[3];this.max_cols=a[4];this.max_rows=a[5];a[6]&&this.vga_memory.set(a[6]);this.dac_state=a[7];this.start_address=a[8];this.graphical_mode=a[9];this.vga256_palette=a[10];this.latch_dword=a[11];this.color_compare=a[12];this.color_dont_care=a[13];this.miscellaneous_graphics_register=
539
+ a[14];this.svga_width=a[15];this.svga_height=a[16];this.crtc_mode=a[17];this.svga_enabled=a[18];this.svga_bpp=a[19];this.svga_bank_offset=a[20];this.svga_offset=a[21];this.index_crtc=a[22];this.dac_color_index_write=a[23];this.dac_color_index_read=a[24];this.dac_map=a[25];this.sequencer_index=a[26];this.plane_write_bm=a[27];this.sequencer_memory_mode=a[28];this.graphics_index=a[29];this.plane_read=a[30];this.planar_mode=a[31];this.planar_rotate_reg=a[32];this.planar_bitmap=a[33];this.max_scan_line=
540
+ a[34];this.miscellaneous_output_register=a[35];this.port_3DA_value=a[36];this.dispi_index=a[37];this.dispi_enable_value=a[38];this.svga_memory.set(a[39]);this.attribute_controller_index=a[41];this.offset_register=a[42];this.planar_setreset=a[43];this.planar_setreset_enable=a[44];this.start_address_latched=a[45];this.crtc.set(a[46]);this.horizontal_display_enable_end=a[47];this.horizontal_blank_start=a[48];this.vertical_display_enable_end=a[49];this.vertical_blank_start=a[50];this.underline_location_register=
541
+ a[51];this.preset_row_scan=a[52];this.offset_register=a[53];this.palette_source=a[54];this.attribute_mode=a[55];this.color_plane_enable=a[56];this.horizontal_panning=a[57];this.color_select=a[58];this.clocking_mode=a[59];this.line_compare=a[60];a[61]&&this.pixel_buffer.set(a[61]);this.dac_mask=void 0===a[62]?255:a[62];this.character_map_select=void 0===a[63]?0:a[63];this.font_page_ab_enabled=void 0===a[64]?0:a[64];this.screen.set_mode(this.graphical_mode);this.graphical_mode?(this.screen_height=this.screen_width=
542
+ 0,this.svga_enabled?(this.set_size_graphical(this.svga_width,this.svga_height,this.svga_width,this.svga_height,this.svga_bpp),this.update_layers()):(this.update_vga_size(),this.update_layers(),this.complete_replot())):(this.set_font_bitmap(!0),this.set_size_text(this.max_cols,this.max_rows),this.set_font_page(),this.update_cursor_scanline(),this.update_cursor());this.complete_redraw()};X.prototype.vga_memory_read=function(a){if(this.svga_enabled)return this.cpu.read8((a-655360|this.svga_bank_offset)+
543
+ 3758096384|0);var b=this.miscellaneous_graphics_register>>2&3;a-=Vc[b];if(0>a||a>=Wc[b])return C("vga read outside memory space: addr:"+m(a),256),0;this.latch_dword=this.plane0[a];this.latch_dword|=this.plane1[a]<<8;this.latch_dword|=this.plane2[a]<<16;this.latch_dword|=this.plane3[a]<<24;if(this.planar_mode&8)return b=255,this.color_dont_care&1&&(b&=this.plane0[a]^~(this.color_compare&1?255:0)),this.color_dont_care&2&&(b&=this.plane1[a]^~(this.color_compare&2?255:0)),this.color_dont_care&4&&(b&=
544
+ this.plane2[a]^~(this.color_compare&4?255:0)),this.color_dont_care&8&&(b&=this.plane3[a]^~(this.color_compare&8?255:0)),b;b=this.plane_read;this.graphical_mode?this.sequencer_memory_mode&8?(b=a&3,a&=-4):this.planar_mode&16&&(b=a&1,a&=-2):b&=3;return this.vga_memory[b<<16|a]};X.prototype.vga_memory_write=function(a,b){if(this.svga_enabled)this.cpu.write8((a-655360|this.svga_bank_offset)+3758096384|0,b);else{var c=this.miscellaneous_graphics_register>>2&3;a-=Vc[c];0>a||a>=Wc[c]?C("vga write outside memory space: addr:"+
545
+ m(a)+", value:"+m(b),256):this.graphical_mode?this.vga_memory_write_graphical(a,b):this.plane_write_bm&3?this.vga_memory_write_text_mode(a,b):this.plane_write_bm&4&&(this.plane2[a]=b)}};X.prototype.vga_memory_write_graphical=function(a,b){var c=this.planar_mode&3,d=this.apply_feed(this.planar_bitmap),e=this.apply_expand(this.planar_setreset),f=this.apply_expand(this.planar_setreset_enable);switch(c){case 0:b=this.apply_rotate(b);var h=this.apply_feed(b);h=this.apply_setreset(h,f);h=this.apply_logical(h,
546
+ this.latch_dword);h=this.apply_bitmask(h,d);break;case 1:h=this.latch_dword;break;case 2:h=this.apply_expand(b);h=this.apply_logical(h,this.latch_dword);h=this.apply_bitmask(h,d);break;case 3:b=this.apply_rotate(b),d&=this.apply_feed(b),h=this.apply_bitmask(e,d)}b=15;switch(this.sequencer_memory_mode&12){case 0:b=5<<(a&1);a&=-2;break;case 8:case 12:b=1<<(a&3),a&=-4}b&=this.plane_write_bm;b&1&&(this.plane0[a]=h>>0&255);b&2&&(this.plane1[a]=h>>8&255);b&4&&(this.plane2[a]=h>>16&255);b&8&&(this.plane3[a]=
547
+ h>>24&255);a=this.vga_addr_to_pixel(a);this.partial_replot(a,a+7)};X.prototype.apply_feed=function(a){return a|a<<8|a<<16|a<<24};X.prototype.apply_expand=function(a){return(a&1?255:0)|(a&2?255:0)<<8|(a&4?255:0)<<16|(a&8?255:0)<<24};X.prototype.apply_rotate=function(a){return(a|a<<8)>>>(this.planar_rotate_reg&7)&255};X.prototype.apply_setreset=function(a,b){var c=this.apply_expand(this.planar_setreset);return(a|b&c)&(~b|c)};X.prototype.apply_logical=function(a,b){switch(this.planar_rotate_reg&24){case 8:return a&
548
+ b;case 16:return a|b;case 24:return a^b}return a};X.prototype.apply_bitmask=function(a,b){return b&a|~b&this.latch_dword};X.prototype.text_mode_redraw=function(){const a=this.scan_line_to_screen_row(this.line_compare),b=Math.max(0,2*(2*this.offset_register-this.max_cols)),c=this.attribute_mode&8,d=this.font_page_ab_enabled?7:15,e=c?7:15,f=this.screen.FLAG_BLINKING,h=this.screen.FLAG_FONT_PAGE_B;let l=this.start_address<<1;for(let p=0;p<this.max_rows;p++){p===a&&(l=0);for(let n=0;n<this.max_cols;n++){const q=
549
+ this.vga_memory[l],r=this.vga_memory[l|1],t=(c&&r&128?f:0)|(!this.font_page_ab_enabled||r&8?0:h);this.bus.send("screen-put-char",[p,n,q]);this.screen.put_char(p,n,q,t,this.vga256_palette[this.dac_mask&this.dac_map[r>>4&e]],this.vga256_palette[this.dac_mask&this.dac_map[r&d]]);l+=2}l+=b}};X.prototype.vga_memory_write_text_mode=function(a,b){this.vga_memory[a]=b;var c=Math.max(this.max_cols,2*this.offset_register);let d;if(a>>1>=this.start_address){var e=(a>>1)-this.start_address;d=e/c|0;c=e%c}else e=
550
+ a>>1,d=(e/c|0)+this.scan_line_to_screen_row(this.line_compare),c=e%c;k(0<=d&&0<=c);if(!(c>=this.max_cols||d>=this.max_rows)){a&1?(e=b,b=this.vga_memory[a&-2]):e=this.vga_memory[a|1];var f=this.attribute_mode&8;a=(f&&e&128?this.screen.FLAG_BLINKING:0)|(!this.font_page_ab_enabled||e&8?0:this.screen.FLAG_FONT_PAGE_B);var h=this.font_page_ab_enabled?7:15;f=f?7:15;this.bus.send("screen-put-char",[d,c,b]);this.screen.put_char(d,c,b,a,this.vga256_palette[this.dac_mask&this.dac_map[e>>4&f]],this.vga256_palette[this.dac_mask&
551
+ this.dac_map[e&h]])}};X.prototype.update_cursor=function(){var a=Math.max(this.max_cols,2*this.offset_register);let b;this.cursor_address>=this.start_address?(b=(this.cursor_address-this.start_address)/a|0,a=(this.cursor_address-this.start_address)%a):(b=(this.cursor_address/a|0)+this.scan_line_to_screen_row(this.line_compare),a=this.cursor_address%a);k(0<=b&&0<=a);this.screen.update_cursor(b,a)};X.prototype.complete_redraw=function(){C("complete redraw",256);this.graphical_mode?this.svga_enabled?
552
+ this.cpu.svga_mark_dirty():(this.diff_addr_min=0,this.diff_addr_max=524288):this.text_mode_redraw()};X.prototype.complete_replot=function(){C("complete replot",256);this.graphical_mode&&!this.svga_enabled&&(this.diff_plot_min=0,this.diff_plot_max=524288,this.complete_redraw())};X.prototype.partial_redraw=function(a,b){a<this.diff_addr_min&&(this.diff_addr_min=a);b>this.diff_addr_max&&(this.diff_addr_max=b)};X.prototype.partial_replot=function(a,b){a<this.diff_plot_min&&(this.diff_plot_min=a);b>this.diff_plot_max&&
553
+ (this.diff_plot_max=b);this.partial_redraw(a,b)};X.prototype.reset_diffs=function(){this.diff_addr_min=this.vga_memory_size;this.diff_addr_max=0;this.diff_plot_min=this.vga_memory_size;this.diff_plot_max=0};X.prototype.destroy=function(){};X.prototype.vga_bytes_per_line=function(){var a=this.offset_register<<2;this.underline_location_register&64?a<<=1:this.crtc_mode&64&&(a>>>=1);return a};X.prototype.vga_addr_shift_count=function(){var a=128+(~this.underline_location_register&this.crtc_mode&64);a-=
554
+ this.underline_location_register&64;a-=this.attribute_mode&64;return a>>>6};X.prototype.vga_addr_to_pixel=function(a){var b=this.vga_addr_shift_count();if(~this.crtc_mode&3){var c=a-this.start_address;c&=this.crtc_mode<<13|-24577;c<<=b;var d=c/this.virtual_width|0;c%=this.virtual_width;switch(this.crtc_mode&3){case 2:d=d<<1|a>>13&1;break;case 1:d=d<<1|a>>14&1;break;case 0:d=d<<2|a>>13&3}return d*this.virtual_width+c+(this.start_address<<b)}return a<<b};X.prototype.scan_line_to_screen_row=function(a){this.max_scan_line&
555
+ 128&&(a>>>=1);a=Math.ceil(a/(1+(this.max_scan_line&31)));this.crtc_mode&1||(a<<=1);this.crtc_mode&2||(a<<=1);return a};X.prototype.set_size_text=function(a,b){k(!this.graphical_mode);this.max_cols=a;this.max_rows=b;this.screen.set_size_text(a,b);this.bus.send("screen-set-size",[a,b,0])};X.prototype.set_size_graphical=function(a,b,c,d,e){k(this.graphical_mode);c=Math.max(c,1);d=Math.max(d,1);if(this.screen_width!==a||this.screen_height!==b||this.virtual_width!==c||this.virtual_height!==d){this.screen_width=
556
+ a;this.screen_height=b;this.virtual_width=c;this.virtual_height=d;if("undefined"!==typeof ImageData){const f=c*d,h=this.cpu.svga_allocate_dest_buffer(f)>>>0;this.dest_buffet_offset=h;this.image_data=new ImageData(new Uint8ClampedArray(this.cpu.wasm_memory.buffer,h,4*f),c,d);this.cpu.svga_mark_dirty()}this.screen.set_size_graphical(a,b,c,d);this.bus.send("screen-set-size",[a,b,e])}};X.prototype.update_vga_size=function(){if(!this.svga_enabled){var a=Math.min(1+this.horizontal_display_enable_end,this.horizontal_blank_start),
557
+ b=Math.min(1+this.vertical_display_enable_end,this.vertical_blank_start);if(a&&b)if(this.graphical_mode){a<<=3;var c=this.offset_register<<4;this.attribute_mode&64&&(a>>>=1,c>>>=1);b=this.scan_line_to_screen_row(b);var d=Wc[0];const e=this.vga_bytes_per_line();this.set_size_graphical(a,b,c,e?Math.ceil(d/e):b,8);this.update_vertical_retrace();this.update_layers()}else this.max_scan_line&128&&(b>>>=1),c=b/(1+(this.max_scan_line&31))|0,a&&c&&this.set_size_text(a,c)}};X.prototype.update_layers=function(){this.graphical_mode||
558
+ this.text_mode_redraw();if(this.svga_enabled)this.layers=[];else if(this.virtual_width&&this.screen_width)if(!this.palette_source||this.clocking_mode&32)this.layers=[],this.screen.clear_screen();else{var a=this.start_address_latched,b=this.horizontal_panning;this.attribute_mode&64&&(b>>>=1);var c=this.preset_row_scan>>5&3,d=this.vga_addr_to_pixel(a+c);a=d/this.virtual_width|0;var e=d%this.virtual_width+b;d=this.scan_line_to_screen_row(1+this.line_compare);d=Math.min(d,this.screen_height);var f=this.screen_height-
559
+ d;this.layers=[];e=-e;for(var h=0;e<this.screen_width;e+=this.virtual_width,h++)this.layers.push({image_data:this.image_data,screen_x:e,screen_y:0,buffer_x:0,buffer_y:a+h,buffer_width:this.virtual_width,buffer_height:d});a=0;this.attribute_mode&32||(a=this.vga_addr_to_pixel(c)+b);e=-a;for(h=0;e<this.screen_width;e+=this.virtual_width,h++)this.layers.push({image_data:this.image_data,screen_x:e,screen_y:d,buffer_x:0,buffer_y:h,buffer_width:this.virtual_width,buffer_height:f})}};X.prototype.update_vertical_retrace=
560
+ function(){this.port_3DA_value|=8;this.start_address_latched!==this.start_address&&(this.start_address_latched=this.start_address,this.update_layers())};X.prototype.update_cursor_scanline=function(){var a=this.max_scan_line&31;const b=Math.min(a,this.cursor_scanline_start&31);a=Math.min(a,this.cursor_scanline_end&31);this.screen.update_cursor_scanline(b,a,!(this.cursor_scanline_start&32)&&b<a)};X.prototype.port3C0_write=function(a){if(-1===this.attribute_controller_index)C("attribute controller index register: "+
561
+ m(a),256),this.attribute_controller_index=a&31,C("attribute actual index: "+m(this.attribute_controller_index),256),this.palette_source!==(a&32)&&(this.palette_source=a&32,this.update_layers());else{if(16>this.attribute_controller_index)C("internal palette: "+m(this.attribute_controller_index)+" -> "+m(a),256),this.dac_map[this.attribute_controller_index]=a,this.attribute_mode&64||this.complete_redraw();else switch(this.attribute_controller_index){case 16:C("3C0 / attribute mode control: "+m(a),256);
562
+ if(this.attribute_mode!==a){var b=this.attribute_mode;this.attribute_mode=a;const c=0!==(a&1);this.svga_enabled||this.graphical_mode===c||(this.graphical_mode=c,this.screen.set_mode(this.graphical_mode));(b^a)&64&&this.complete_replot();this.update_vga_size();this.complete_redraw();this.set_font_bitmap(!1)}break;case 18:C("3C0 / color plane enable: "+m(a),256);this.color_plane_enable!==a&&(this.color_plane_enable=a,this.complete_redraw());break;case 19:C("3C0 / horizontal panning: "+m(a),256);this.horizontal_panning!==
563
+ a&&(this.horizontal_panning=a&15,this.update_layers());break;case 20:C("3C0 / color select: "+m(a),256);this.color_select!==a&&(this.color_select=a,this.complete_redraw());break;default:C("3C0 / attribute controller write "+m(this.attribute_controller_index)+": "+m(a),256)}this.attribute_controller_index=-1}};X.prototype.port3C0_read=function(){C("3C0 read",256);return(this.attribute_controller_index|this.palette_source)&255};X.prototype.port3C0_read16=function(){C("3C0 read16",256);return this.port3C0_read()|
564
+ this.port3C1_read()<<8&65280};X.prototype.port3C1_read=function(){if(16>this.attribute_controller_index)return C("3C1 / internal palette read: "+m(this.attribute_controller_index)+" -> "+m(this.dac_map[this.attribute_controller_index]),256),this.dac_map[this.attribute_controller_index]&255;switch(this.attribute_controller_index){case 16:return C("3C1 / attribute mode read: "+m(this.attribute_mode),256),this.attribute_mode;case 18:return C("3C1 / color plane enable read: "+m(this.color_plane_enable),
565
+ 256),this.color_plane_enable;case 19:return C("3C1 / horizontal panning read: "+m(this.horizontal_panning),256),this.horizontal_panning;case 20:return C("3C1 / color select read: "+m(this.color_select),256),this.color_select;default:C("3C1 / attribute controller read "+m(this.attribute_controller_index),256)}return 255};X.prototype.port3C2_write=function(a){C("3C2 / miscellaneous output register = "+m(a),256);this.miscellaneous_output_register=a};X.prototype.port3C4_write=function(a){this.sequencer_index=
566
+ a};X.prototype.port3C4_read=function(){return this.sequencer_index};X.prototype.port3C5_write=function(a){switch(this.sequencer_index){case 1:C("clocking mode: "+m(a),256);var b=this.clocking_mode;this.clocking_mode=a;(b^a)&32&&this.update_layers();this.set_font_bitmap(!1);break;case 2:C("plane write mask: "+m(a),256);b=this.plane_write_bm;this.plane_write_bm=a;this.graphical_mode||!(b&4)||this.plane_write_bm&4||this.set_font_bitmap(!0);break;case 3:C("character map select: "+m(a),256);b=this.character_map_select;
567
+ this.character_map_select=a;this.graphical_mode||b===a||this.set_font_page();break;case 4:C("sequencer memory mode: "+m(a),256);this.sequencer_memory_mode=a;break;default:C("3C5 / sequencer write "+m(this.sequencer_index)+": "+m(a),256)}};X.prototype.port3C5_read=function(){C("3C5 / sequencer read "+m(this.sequencer_index),256);switch(this.sequencer_index){case 1:return this.clocking_mode;case 2:return this.plane_write_bm;case 3:return this.character_map_select;case 4:return this.sequencer_memory_mode;
568
+ case 6:return 18}return 0};X.prototype.port3C6_write=function(a){this.dac_mask!==a&&(this.dac_mask=a,this.complete_redraw())};X.prototype.port3C6_read=function(){return this.dac_mask};X.prototype.port3C7_write=function(a){C("3C7 write: "+m(a),256);this.dac_color_index_read=3*a;this.dac_state&=0};X.prototype.port3C7_read=function(){return this.dac_state};X.prototype.port3C8_write=function(a){this.dac_color_index_write=3*a;this.dac_state|=3};X.prototype.port3C8_read=function(){return this.dac_color_index_write/
569
+ 3&255};X.prototype.port3C9_write=function(a){var b=this.dac_color_index_write/3|0,c=this.dac_color_index_write%3,d=this.vga256_palette[b];if(0===(this.dispi_enable_value&32)){a&=63;const e=a&1;a=a<<2|e<<1|e}0===c?d=d&-16711681|a<<16:1===c?d=d&-65281|a<<8:(d=d&-256|a,C("dac set color, index="+m(b)+" value="+m(d),256));this.vga256_palette[b]!==d&&(this.vga256_palette[b]=d,this.complete_redraw());this.dac_color_index_write++};X.prototype.port3C9_read=function(){C("3C9 read",256);var a=this.vga256_palette[this.dac_color_index_read/
570
+ 3|0]>>8*(2-this.dac_color_index_read%3)&255;this.dac_color_index_read++;return this.dispi_enable_value&32?a:a>>2};X.prototype.port3CC_read=function(){C("3CC read",256);return this.miscellaneous_output_register};X.prototype.port3CE_write=function(a){this.graphics_index=a};X.prototype.port3CE_read=function(){return this.graphics_index};X.prototype.port3CF_write=function(a){switch(this.graphics_index){case 0:this.planar_setreset=a;C("plane set/reset: "+m(a),256);break;case 1:this.planar_setreset_enable=
571
+ a;C("plane set/reset enable: "+m(a),256);break;case 2:this.color_compare=a;C("color compare: "+m(a),256);break;case 3:this.planar_rotate_reg=a;C("plane rotate: "+m(a),256);break;case 4:this.plane_read=a;C("plane read: "+m(a),256);break;case 5:var b=this.planar_mode;this.planar_mode=a;C("planar mode: "+m(a),256);(b^a)&96&&this.complete_replot();break;case 6:C("miscellaneous graphics register: "+m(a),256);this.miscellaneous_graphics_register!==a&&(this.miscellaneous_graphics_register=a,this.update_vga_size());
572
+ break;case 7:this.color_dont_care=a;C("color don't care: "+m(a),256);break;case 8:this.planar_bitmap=a;C("planar bitmap: "+m(a),256);break;default:C("3CF / graphics write "+m(this.graphics_index)+": "+m(a),256)}};X.prototype.port3CF_read=function(){C("3CF / graphics read "+m(this.graphics_index),256);switch(this.graphics_index){case 0:return this.planar_setreset;case 1:return this.planar_setreset_enable;case 2:return this.color_compare;case 3:return this.planar_rotate_reg;case 4:return this.plane_read;
573
+ case 5:return this.planar_mode;case 6:return this.miscellaneous_graphics_register;case 7:return this.color_dont_care;case 8:return this.planar_bitmap}return 0};X.prototype.port3D4_write=function(a){C("3D4 / crtc index: "+a,256);this.index_crtc=a};X.prototype.port3D4_write16=function(a){this.port3D4_write(a&255);this.port3D5_write(a>>8&255)};X.prototype.port3D4_read=function(){C("3D4 read / crtc index: "+this.index_crtc,256);return this.index_crtc};X.prototype.port3D5_write=function(a){switch(this.index_crtc){case 1:C("3D5 / hdisp enable end write: "+
574
+ m(a),256);this.horizontal_display_enable_end!==a&&(this.horizontal_display_enable_end=a,this.update_vga_size());break;case 2:this.horizontal_blank_start!==a&&(this.horizontal_blank_start=a,this.update_vga_size());break;case 7:C("3D5 / overflow register write: "+m(a),256);var b=this.vertical_display_enable_end;this.vertical_display_enable_end&=255;this.vertical_display_enable_end=this.vertical_display_enable_end|a<<3&512|a<<7&256;b!==this.vertical_display_enable_end&&this.update_vga_size();this.line_compare=
575
+ this.line_compare&767|a<<4&256;b=this.vertical_blank_start;this.vertical_blank_start=this.vertical_blank_start&767|a<<5&256;b!==this.vertical_blank_start&&this.update_vga_size();this.update_layers();break;case 8:C("3D5 / preset row scan write: "+m(a),256);this.preset_row_scan=a;this.update_layers();break;case 9:C("3D5 / max scan line write: "+m(a),256);var c=this.max_scan_line;this.max_scan_line=a;this.line_compare=this.line_compare&511|a<<3&512;b=this.vertical_blank_start;this.vertical_blank_start=
576
+ this.vertical_blank_start&511|a<<4&512;((c^this.max_scan_line)&159||b!==this.vertical_blank_start)&&this.update_vga_size();this.update_cursor_scanline();this.update_layers();this.set_font_bitmap(!1);break;case 10:C("3D5 / cursor scanline start write: "+m(a),256);this.cursor_scanline_start=a;this.update_cursor_scanline();break;case 11:C("3D5 / cursor scanline end write: "+m(a),256);this.cursor_scanline_end=a;this.update_cursor_scanline();break;case 12:(this.start_address>>8&255)!==a&&(this.start_address=
577
+ this.start_address&255|a<<8,this.update_layers(),~this.crtc_mode&3&&this.complete_replot());C("3D5 / start addr hi write: "+m(a)+" -> "+m(this.start_address,4),256);break;case 13:(this.start_address&255)!==a&&(this.start_address=this.start_address&65280|a,this.update_layers(),~this.crtc_mode&3&&this.complete_replot());C("3D5 / start addr lo write: "+m(a)+" -> "+m(this.start_address,4),256);break;case 14:C("3D5 / cursor address hi write: "+m(a),256);this.cursor_address=this.cursor_address&255|a<<8;
578
+ this.update_cursor();break;case 15:C("3D5 / cursor address lo write: "+m(a),256);this.cursor_address=this.cursor_address&65280|a;this.update_cursor();break;case 18:C("3D5 / vdisp enable end write: "+m(a),256);(this.vertical_display_enable_end&255)!==a&&(this.vertical_display_enable_end=this.vertical_display_enable_end&768|a,this.update_vga_size());break;case 19:C("3D5 / offset register write: "+m(a),256);this.offset_register!==a&&(this.offset_register=a,this.update_vga_size(),~this.crtc_mode&3&&this.complete_replot());
579
+ break;case 20:C("3D5 / underline location write: "+m(a),256);this.underline_location_register!==a&&(b=this.underline_location_register,this.underline_location_register=a,this.update_vga_size(),(b^a)&64&&this.complete_replot());break;case 21:C("3D5 / vertical blank start write: "+m(a),256);(this.vertical_blank_start&255)!==a&&(this.vertical_blank_start=this.vertical_blank_start&768|a,this.update_vga_size());break;case 23:C("3D5 / crtc mode write: "+m(a),256);this.crtc_mode!==a&&(b=this.crtc_mode,this.crtc_mode=
580
+ a,this.update_vga_size(),(b^a)&67&&this.complete_replot());break;case 24:C("3D5 / line compare write: "+m(a),256);this.line_compare=this.line_compare&768|a;this.update_layers();break;default:this.index_crtc<this.crtc.length&&(this.crtc[this.index_crtc]=a),C("3D5 / CRTC write "+m(this.index_crtc)+": "+m(a),256)}};X.prototype.port3D5_write16=function(a){C("16-bit write to 3D5: "+m(a,4),256);this.port3D5_write(a&255)};X.prototype.port3D5_read=function(){C("3D5 read "+m(this.index_crtc),256);switch(this.index_crtc){case 1:return this.horizontal_display_enable_end;
581
+ case 2:return this.horizontal_blank_start;case 7:return this.vertical_display_enable_end>>7&2|this.vertical_blank_start>>5&8|this.line_compare>>4&16|this.vertical_display_enable_end>>3&64;case 8:return this.preset_row_scan;case 9:return this.max_scan_line;case 10:return this.cursor_scanline_start;case 11:return this.cursor_scanline_end;case 12:return this.start_address&255;case 13:return this.start_address>>8;case 14:return this.cursor_address>>8;case 15:return this.cursor_address&255;case 18:return this.vertical_display_enable_end&
582
+ 255;case 19:return this.offset_register;case 20:return this.underline_location_register;case 21:return this.vertical_blank_start&255;case 23:return this.crtc_mode;case 24:return this.line_compare&255}return this.index_crtc<this.crtc.length?this.crtc[this.index_crtc]:0};X.prototype.port3D5_read16=function(){C("Warning: 16-bit read from 3D5",256);return this.port3D5_read()};X.prototype.port3DA_read=function(){C("3DA read - status 1 and clear attr index",256);var a=this.port_3DA_value;this.graphical_mode?
583
+ (this.port_3DA_value^=1,this.port_3DA_value&=1):(this.port_3DA_value&1&&(this.port_3DA_value^=8),this.port_3DA_value^=1);this.attribute_controller_index=-1;return a};X.prototype.port1CE_write=function(a){this.dispi_index=a};X.prototype.port1CF_write=function(a){C("1CF / dispi write "+m(this.dispi_index)+": "+m(a),256);const b=this.svga_enabled;switch(this.dispi_index){case 0:45248<=a&&45253>=a?this.svga_version=a:C("Invalid version value: "+m(a),256);break;case 1:this.svga_width=a;2560<this.svga_width&&
584
+ (C("svga_width reduced from "+this.svga_width+" to 2560",256),this.svga_width=2560);break;case 2:this.svga_height=a;1600<this.svga_height&&(C("svga_height reduced from "+this.svga_height+" to 1600",256),this.svga_height=1600);break;case 3:this.svga_bpp=a;break;case 4:(this.svga_enabled=1===(a&1))&&0===(a&128)&&this.svga_memory.fill(0);this.dispi_enable_value=a;break;case 5:C("SVGA bank offset: "+m(a<<16),256);this.svga_bank_offset=a<<16;break;case 8:C("SVGA X offset: "+m(a),256);this.svga_offset_x!==
585
+ a&&(this.svga_offset_x=a,this.svga_offset=this.svga_offset_y*this.svga_width+this.svga_offset_x,this.complete_redraw());break;case 9:C("SVGA Y offset: "+m(a*this.svga_width)+" y="+m(a),256);this.svga_offset_y!==a&&(this.svga_offset_y=a,this.svga_offset=this.svga_offset_y*this.svga_width+this.svga_offset_x,this.complete_redraw());break;default:C("Unimplemented dispi write index: "+m(this.dispi_index),256)}!this.svga_enabled||this.svga_width&&this.svga_height||(C("SVGA: disabled because of invalid width/height: "+
586
+ this.svga_width+"x"+this.svga_height,256),this.svga_enabled=!1);k(4!==this.svga_bpp,"unimplemented svga bpp: 4");k(4===this.svga_bpp||8===this.svga_bpp||15===this.svga_bpp||16===this.svga_bpp||24===this.svga_bpp||32===this.svga_bpp,"unexpected svga bpp: "+this.svga_bpp);this.svga_enabled?C("SVGA: enabled, "+this.svga_width+"x"+this.svga_height+"x"+this.svga_bpp,256):C("SVGA: disabled");this.svga_enabled&&!b&&(this.svga_offset_y=this.svga_offset_x=this.svga_offset=0,this.graphical_mode=!0,this.screen.set_mode(this.graphical_mode),
587
+ this.set_size_graphical(this.svga_width,this.svga_height,this.svga_width,this.svga_height,this.svga_bpp));this.svga_enabled||(this.svga_bank_offset=0);this.update_layers()};X.prototype.port1CF_read=function(){C("1CF / dispi read "+m(this.dispi_index),256);return this.svga_register_read(this.dispi_index)};X.prototype.svga_register_read=function(a){switch(a){case 0:return this.svga_version;case 1:return this.dispi_enable_value&2?2560:this.svga_width;case 2:return this.dispi_enable_value&2?1600:this.svga_height;
588
+ case 3:return this.dispi_enable_value&2?32:this.svga_bpp;case 4:return this.dispi_enable_value;case 5:return this.svga_bank_offset>>>16;case 6:return this.screen_width?this.screen_width:1;case 8:return this.svga_offset_x;case 9:return this.svga_offset_y;case 10:return this.vga_memory_size/65536|0;default:C("Unimplemented dispi read index: "+m(this.dispi_index),256)}return 255};X.prototype.vga_replot=function(){for(var a=this.diff_plot_min&-16,b=Math.min(this.diff_plot_max|15,524287),c=this.vga_addr_shift_count(),
589
+ d=~this.crtc_mode&3,e=this.planar_mode&96,f=this.attribute_mode&64;a<=b;){var h=a>>>c;if(d){var l=a/this.virtual_width|0,p=a-this.virtual_width*l;switch(d){case 1:h=(l&1)<<13;l>>>=1;break;case 2:h=(l&1)<<14;l>>>=1;break;case 3:h=(l&3)<<13,l>>>=2}h|=(l*this.virtual_width+p>>>c)+this.start_address}l=this.plane0[h];p=this.plane1[h];var n=this.plane2[h],q=this.plane3[h];h=new Uint8Array(8);switch(e){case 0:l<<=0;p<<=1;n<<=2;q<<=3;for(var r=7;0<=r;r--)h[7-r]=l>>r&1|p>>r&2|n>>r&4|q>>r&8;break;case 32:h[0]=
590
+ l>>6&3|n>>4&12;h[1]=l>>4&3|n>>2&12;h[2]=l>>2&3|n>>0&12;h[3]=l>>0&3|n<<2&12;h[4]=p>>6&3|q>>4&12;h[5]=p>>4&3|q>>2&12;h[6]=p>>2&3|q>>0&12;h[7]=p>>0&3|q<<2&12;break;case 64:case 96:h[0]=l>>4&15,h[1]=l>>0&15,h[2]=p>>4&15,h[3]=p>>0&15,h[4]=n>>4&15,h[5]=n>>0&15,h[6]=q>>4&15,h[7]=q>>0&15}if(f)for(l=r=0;4>r;r++,a++,l+=2)this.pixel_buffer[a]=h[l]<<4|h[l+1];else for(r=0;8>r;r++,a++)this.pixel_buffer[a]=h[r]}};X.prototype.vga_redraw=function(){var a=this.diff_addr_min,b=Math.min(this.diff_addr_max,524287);const c=
591
+ new Int32Array(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,this.virtual_width*this.virtual_height);var d=255,e=0;this.attribute_mode&128&&(d&=207,e|=this.color_select<<4&48);if(this.attribute_mode&64)for(;a<=b;a++){var f=this.pixel_buffer[a]&d|e;f=this.vga256_palette[f];c[a]=f&65280|f<<16|f>>16|4278190080}else for(d&=63,e|=this.color_select<<4&192;a<=b;a++)f=this.dac_map[this.pixel_buffer[a]&this.color_plane_enable]&d|e,f=this.vga256_palette[f],c[a]=f&65280|f<<16|f>>16|4278190080};X.prototype.screen_fill_buffer=
592
+ function(){if(this.graphical_mode){if(0===this.image_data.data.byteLength){var a=new Uint8ClampedArray(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,4*this.virtual_width*this.virtual_height);this.image_data=new ImageData(a,this.virtual_width,this.virtual_height);this.update_layers()}if(this.svga_enabled){a=0;let d=this.svga_height;if(8===this.svga_bpp){const e=new Int32Array(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,this.screen_width*this.screen_height),f=new Uint8Array(this.cpu.wasm_memory.buffer,
593
+ this.svga_memory.byteOffset,this.vga_memory_size);for(var b=0;b<e.length;b++){var c=this.vga256_palette[f[b]];e[b]=c&65280|c<<16|c>>16|4278190080}}else this.cpu.svga_fill_pixel_buffer(this.svga_bpp,this.svga_offset),b=15===this.svga_bpp?2:this.svga_bpp/8,a=((this.cpu.svga_dirty_bitmap_min_offset[0]/b|0)-this.svga_offset)/this.svga_width|0,d=(((this.cpu.svga_dirty_bitmap_max_offset[0]/b|0)-this.svga_offset)/this.svga_width|0)+1;a<d&&(a=Math.max(a,0),d=Math.min(d,this.svga_height),this.screen.update_buffer([{image_data:this.image_data,
594
+ screen_x:0,screen_y:a,buffer_x:0,buffer_y:a,buffer_width:this.svga_width,buffer_height:d-a}]))}else this.vga_replot(),this.vga_redraw(),this.screen.update_buffer(this.layers);this.reset_diffs()}this.update_vertical_retrace()};X.prototype.set_font_bitmap=function(a){const b=this.max_scan_line&31;if(b&&!this.graphical_mode){const c=!!(this.clocking_mode&8);this.screen.set_font_bitmap(b+1,!c&&!(this.clocking_mode&1),c,!!(this.attribute_mode&4),this.plane2,a)}};X.prototype.set_font_page=function(){const a=
595
+ [0,2,4,6,1,3,5,7],b=(this.character_map_select&12)>>2|(this.character_map_select&32)>>3,c=this.character_map_select&3|(this.character_map_select&16)>>2;this.font_page_ab_enabled=b!==c;this.screen.set_font_page(a[b],a[c]);this.complete_redraw()};const Xc="SWAP_IN SWAP_OUT MAJFLT MINFLT MEMFREE MEMTOT AVAIL CACHES HTLB_PGALLOC HTLB_PGFAIL".split(" ");function Yc(a,b){this.bus=b;this.zeroed=this.fp_cmd=this.actual=this.num_pages=0;this.virtio=new Ic(a,{name:"virtio-balloon",pci_id:88,device_id:4165,
596
+ subsystem_device_id:5,common:{initial_port:55296,queues:[{size_supported:32,notify_offset:0},{size_supported:32,notify_offset:0},{size_supported:2,notify_offset:1},{size_supported:64,notify_offset:2}],features:[1,3,32],on_driver_ok:()=>{C("Balloon setup",2048)}},notification:{initial_port:55552,single_handler:!1,handlers:[c=>{const d=this.virtio.queues[c];for(;d.has_request();){var e=d.pop_request();const f=new Uint8Array(e.length_readable);e.get_next_blob(f);this.virtio.queues[c].push_reply(e);e=
597
+ f.byteLength/4;this.actual+=0===c?e:-e}this.virtio.queues[c].flush_replies()},c=>{var d=this.virtio.queues[c];if(d.has_request()){d=d.pop_request();const e=new Uint8Array(d.length_readable);d.get_next_blob(e);let f={};for(let h=0;h<d.length_readable;h+=10){let [l,p]=M(["h","d"],e,{offset:h});f[Xc[l]]=p}this.virtio.queues[c].push_reply(d);this.stats_cb&&this.stats_cb(f)}},c=>{const d=this.virtio.queues[c];for(;d.has_request();){const f=d.pop_request();if(0<f.length_readable){var e=new Uint8Array(f.length_readable);
598
+ f.get_next_blob(e);[e]=M(["w"],e,{offset:0});0===e&&(this.free_cb&&this.free_cb(this.zeroed),1<this.fp_cmd&&(this.fp_cmd=1),this.virtio.notify_config_changes())}if(0<f.length_writable)for(new Uint8Array(0),e=0;e<f.write_buffers.length;++e){let h=f.write_buffers[e];this.zeroed+=h.len;this.virtio.cpu.zero_memory(h.addr_low,h.len)}this.virtio.queues[c].push_reply(f)}this.virtio.queues[c].flush_replies()}]},isr_status:{initial_port:55040},device_specific:{initial_port:54784,struct:[{bytes:4,name:"num_pages",
599
+ read:()=>this.num_pages,write:()=>{}},{bytes:4,name:"actual",read:()=>this.actual,write:()=>{}},{bytes:4,name:"free_page_hint_cmd_id",read:()=>this.fp_cmd,write:()=>{}}]}})}Yc.prototype.Inflate=function(a){this.num_pages+=a;this.virtio.notify_config_changes()};Yc.prototype.Deflate=function(a){this.num_pages-=a;this.virtio.notify_config_changes()};Yc.prototype.Cleanup=function(a){this.fp_cmd=2;this.free_cb=a;this.zeroed=0;this.virtio.notify_config_changes()};Yc.prototype.get_state=function(){const a=
600
+ [];a[0]=this.virtio;a[1]=this.num_pages;a[2]=this.actual;return a};Yc.prototype.set_state=function(a){this.virtio.set_state(a[0]);this.num_pages=a[1];this.actual=a[2]};Yc.prototype.GetStats=function(a){this.stats_cb=a;for(a=this.virtio.queues[2];a.has_request();){const b=a.pop_request();this.virtio.queues[2].push_reply(b)}this.virtio.queues[2].flush_replies()};Yc.prototype.Reset=function(){};function Zc(a,b,c,d){C("Trying to load kernel of size "+b.byteLength);var e=new Uint8Array(b);const f=new Uint16Array(b);
601
+ var h=new Uint32Array(b),l=e[497]||4,p=f[255];if(43605!==p)C("Bad checksum1: "+m(p));else if(p=f[257]|f[258]<<16,1400005704!==p)C("Bad checksum2: "+m(p));else{p=f[259];k(514<=p);var n=e[529];k(n&1);var q=f[283],r=h[139],t=h[140],u=e[564],y=e[565],A=518<=p?h[142]:255,v=h[146],B=h[147],w=h[150],F=h[151],J=h[152];C("kernel boot protocol version: "+m(p));C("flags="+m(n)+" xflags="+m(q));C("code32_start="+m(h[133]));C("initrd_addr_max="+m(r));C("kernel_alignment="+m(t));C("relocatable="+u);C("min_alignment="+
602
+ m(y));C("cmdline max="+m(A));C("payload offset="+m(v)+" size="+m(B));C("pref_address="+m(F)+":"+m(w));C("init_size="+m(J));e[528]=255;e[529]=n&-97|128;f[274]=56832;f[253]=65535;C("heap_end_ptr="+m(56832));d+="\x00";k(d.length<A);C("cmd_line_ptr="+m(581632));h[138]=581632;for(e=0;e<d.length;e++)a[581632+e]=d.charCodeAt(e);l=512*(l+1);C("prot_mode_kernel_start="+m(l));d=new Uint8Array(b,0,l);b=new Uint8Array(b,l);e=l=0;c&&(l=67108864,e=c.byteLength,k(1048576+b.length<l),a.set(new Uint8Array(c),l));
603
+ h[134]=l;h[135]=e;k(655360>524288+d.length);a.set(d,524288);a.set(b,1048576);a=new Uint8Array(512);(new Uint16Array(a.buffer))[0]=43605;a[2]=1;c=3;a[c++]=250;a[c++]=184;a[c++]=32768;a[c++]=128;a[c++]=142;a[c++]=192;a[c++]=142;a[c++]=216;a[c++]=142;a[c++]=224;a[c++]=142;a[c++]=232;a[c++]=142;a[c++]=208;a[c++]=188;a[c++]=57344;a[c++]=224;a[c++]=234;a[c++]=0;a[c++]=0;a[c++]=32800;a[c++]=128;k(512>c);h=a[c]=0;for(b=0;b<a.length;b++)h+=a[b];a[c]=-h;return{name:"genroms/kernel.bin",data:a}}}function P(a,
604
+ b,c){this.stop_idling=c;this.wm=b;this.wasm_patch();this.create_jit_imports();this.wasm_memory=b=this.wm.exports.memory;this.memory_size=g(Uint32Array,b,812,1);this.mem8=new Uint8Array(0);this.mem32s=new Int32Array(this.mem8.buffer);this.segment_is_null=g(Uint8Array,b,724,8);this.segment_offsets=g(Int32Array,b,736,8);this.segment_limits=g(Uint32Array,b,768,8);this.segment_access_bytes=g(Uint8Array,b,512,8);this.protected_mode=g(Int32Array,b,800,1);this.idtr_size=g(Int32Array,b,564,1);this.idtr_offset=
605
+ g(Int32Array,b,568,1);this.gdtr_size=g(Int32Array,b,572,1);this.gdtr_offset=g(Int32Array,b,576,1);this.tss_size_32=g(Int32Array,b,1128,1);this.page_fault=g(Uint32Array,b,540,8);this.cr=g(Int32Array,b,580,8);this.cpl=g(Uint8Array,b,612,1);this.is_32=g(Int32Array,b,804,1);this.stack_size_32=g(Int32Array,b,808,1);this.in_hlt=g(Uint8Array,b,616,1);this.last_virt_eip=g(Int32Array,b,620,1);this.eip_phys=g(Int32Array,b,624,1);this.sysenter_cs=g(Int32Array,b,636,1);this.sysenter_esp=g(Int32Array,b,640,1);
606
+ this.sysenter_eip=g(Int32Array,b,644,1);this.prefixes=g(Int32Array,b,648,1);this.flags=g(Int32Array,b,120,1);this.flags_changed=g(Int32Array,b,100,1);this.last_op_size=g(Int32Array,b,96,1);this.last_op1=g(Int32Array,b,104,1);this.last_result=g(Int32Array,b,112,1);this.current_tsc=g(Uint32Array,b,960,2);this.devices={};this.instruction_pointer=g(Int32Array,b,556,1);this.previous_ip=g(Int32Array,b,560,1);this.apic_enabled=g(Uint8Array,b,548,1);this.acpi_enabled=g(Uint8Array,b,552,1);this.memory_map_read8=
607
+ [];this.memory_map_write8=[];this.memory_map_read32=[];this.memory_map_write32=[];this.bios={main:null,vga:null};this.instruction_counter=g(Uint32Array,b,664,1);this.reg32=g(Int32Array,b,64,8);this.fpu_st=g(Int32Array,b,1152,32);this.fpu_stack_empty=g(Uint8Array,b,816,1);this.fpu_stack_empty[0]=255;this.fpu_stack_ptr=g(Uint8Array,b,1032,1);this.fpu_stack_ptr[0]=0;this.fpu_control_word=g(Uint16Array,b,1036,1);this.fpu_control_word[0]=895;this.fpu_status_word=g(Uint16Array,b,1040,1);this.fpu_status_word[0]=
608
+ 0;this.fpu_ip=g(Int32Array,b,1048,1);this.fpu_ip[0]=0;this.fpu_ip_selector=g(Int32Array,b,1052,1);this.fpu_ip_selector[0]=0;this.fpu_opcode=g(Int32Array,b,1044,1);this.fpu_opcode[0]=0;this.fpu_dp=g(Int32Array,b,1056,1);this.fpu_dp[0]=0;this.fpu_dp_selector=g(Int32Array,b,1060,1);this.fpu_dp_selector[0]=0;this.reg_xmm32s=g(Int32Array,b,832,32);this.mxcsr=g(Int32Array,b,824,1);this.sreg=g(Uint16Array,b,668,8);this.dreg=g(Int32Array,b,684,8);this.reg_pdpte=g(Int32Array,b,968,8);this.svga_dirty_bitmap_min_offset=
609
+ g(Uint32Array,b,716,1);this.svga_dirty_bitmap_max_offset=g(Uint32Array,b,720,1);this.fw_value=[];this.fw_pointer=0;this.option_roms=[];this.io=void 0;this.bus=a;this.set_tsc(0,0);this.debug_init();this.seen_code={};this.seen_code_uncompiled={}}P.prototype.mmap_read8=function(a){a=this.memory_map_read8[a>>>17](a);k(0<=a&&255>=a);return a};P.prototype.mmap_write8=function(a,b){k(0<=b&&255>=b);this.memory_map_write8[a>>>17](a,b)};P.prototype.mmap_read16=function(a){var b=this.memory_map_read8[a>>>17];
610
+ a=b(a)|b(a+1|0)<<8;k(0<=a&&65535>=a);return a};P.prototype.mmap_write16=function(a,b){var c=this.memory_map_write8[a>>>17];k(0<=b&&65535>=b);c(a,b&255);c(a+1|0,b>>8)};P.prototype.mmap_read32=function(a){return this.memory_map_read32[a>>>17](a)};P.prototype.mmap_write32=function(a,b){this.memory_map_write32[a>>>17](a,b)};P.prototype.mmap_write64=function(a,b,c){var d=a>>>17;k(d===a+7>>>17);d=this.memory_map_write32[d];d(a,b);d(a+4,c)};P.prototype.mmap_write128=function(a,b,c,d,e){var f=a>>>17;k(f===
611
+ a+12>>>17);f=this.memory_map_write32[f];f(a,b);f(a+4,c);f(a+8,d);f(a+12,e)};P.prototype.write_blob=function(a,b){k(a&&0<=a.length);a.length&&(k(!this.in_mapped_range(b)),k(!this.in_mapped_range(b+a.length-1)),this.jit_dirty_cache(b,b+a.length),this.mem8.set(a,b))};P.prototype.read_blob=function(a,b){b&&(k(!this.in_mapped_range(a)),k(!this.in_mapped_range(a+b-1)));return this.mem8.subarray(a,a+b)};P.prototype.clear_opstats=function(){(new Uint8Array(this.wasm_memory.buffer,32768,131072)).fill(0);this.wm.exports.profiler_init()};
612
+ P.prototype.create_jit_imports=function(){const a=Object.create(null);a.m=this.wm.exports.memory;for(const b of Object.keys(this.wm.exports))b.startsWith("_")||b.startsWith("zstd")||b.endsWith("_js")||(a[b]=this.wm.exports[b]);this.jit_imports=a};P.prototype.wasm_patch=function(){const a=c=>this.wm.exports[c],b=c=>{const d=a(c);console.assert(d,"Missing import: "+c);return d};this.reset_cpu=b("reset_cpu");this.getiopl=b("getiopl");this.get_eflags=b("get_eflags");this.handle_irqs=b("handle_irqs");
613
+ this.main_loop=b("main_loop");this.set_jit_config=b("set_jit_config");this.read8=b("read8");this.read16=b("read16");this.read32s=b("read32s");this.write8=b("write8");this.write16=b("write16");this.write32=b("write32");this.in_mapped_range=b("in_mapped_range");this.fpu_load_tag_word=b("fpu_load_tag_word");this.fpu_load_status_word=b("fpu_load_status_word");this.fpu_get_sti_f64=b("fpu_get_sti_f64");this.translate_address_system_read=b("translate_address_system_read_js");this.get_seg_cs=b("get_seg_cs");
614
+ this.get_real_eip=b("get_real_eip");this.clear_tlb=b("clear_tlb");this.full_clear_tlb=b("full_clear_tlb");this.update_state_flags=b("update_state_flags");this.set_tsc=b("set_tsc");this.store_current_tsc=b("store_current_tsc");this.set_cpuid_level=b("set_cpuid_level");this.pic_set_irq=b("pic_set_irq");this.pic_clear_irq=b("pic_clear_irq");this.jit_force_generate_unsafe=a("jit_force_generate_unsafe");this.jit_clear_cache=b("jit_clear_cache_js");this.jit_dirty_cache=b("jit_dirty_cache");this.codegen_finalize_finished=
615
+ b("codegen_finalize_finished");this.allocate_memory=b("allocate_memory");this.zero_memory=b("zero_memory");this.is_memory_zeroed=b("is_memory_zeroed");this.svga_allocate_memory=b("svga_allocate_memory");this.svga_allocate_dest_buffer=b("svga_allocate_dest_buffer");this.svga_fill_pixel_buffer=b("svga_fill_pixel_buffer");this.svga_mark_dirty=b("svga_mark_dirty");this.get_pic_addr_master=b("get_pic_addr_master");this.get_pic_addr_slave=b("get_pic_addr_slave");this.zstd_create_ctx=b("zstd_create_ctx");
616
+ this.zstd_get_src_ptr=b("zstd_get_src_ptr");this.zstd_free_ctx=b("zstd_free_ctx");this.zstd_read=b("zstd_read");this.zstd_read_free=b("zstd_read_free");this.port20_read=b("port20_read");this.port21_read=b("port21_read");this.portA0_read=b("portA0_read");this.portA1_read=b("portA1_read");this.port20_write=b("port20_write");this.port21_write=b("port21_write");this.portA0_write=b("portA0_write");this.portA1_write=b("portA1_write");this.port4D0_read=b("port4D0_read");this.port4D1_read=b("port4D1_read");
617
+ this.port4D0_write=b("port4D0_write");this.port4D1_write=b("port4D1_write")};P.prototype.jit_force_generate=function(a){this.jit_force_generate_unsafe?this.jit_force_generate_unsafe(a):k(!1,"Not supported in this wasm build: jit_force_generate_unsafe")};P.prototype.jit_clear_func=function(a){k(0<=a&&900>a);this.wm.wasm_table.set(a+1024,null)};P.prototype.jit_clear_all_funcs=function(){const a=this.wm.wasm_table;for(let b=0;900>b;b++)a.set(1024+b,null)};P.prototype.get_state=function(){var a=[];a[0]=
618
+ this.memory_size[0];a[1]=new Uint8Array([...this.segment_is_null,...this.segment_access_bytes]);a[2]=this.segment_offsets;a[3]=this.segment_limits;a[4]=this.protected_mode[0];a[5]=this.idtr_offset[0];a[6]=this.idtr_size[0];a[7]=this.gdtr_offset[0];a[8]=this.gdtr_size[0];a[9]=this.page_fault[0];a[10]=this.cr;a[11]=this.cpl[0];a[13]=this.is_32[0];a[16]=this.stack_size_32[0];a[17]=this.in_hlt[0];a[18]=this.last_virt_eip[0];a[19]=this.eip_phys[0];a[22]=this.sysenter_cs[0];a[23]=this.sysenter_eip[0];a[24]=
619
+ this.sysenter_esp[0];a[25]=this.prefixes[0];a[26]=this.flags[0];a[27]=this.flags_changed[0];a[28]=this.last_op1[0];a[30]=this.last_op_size[0];a[37]=this.instruction_pointer[0];a[38]=this.previous_ip[0];a[39]=this.reg32;a[40]=this.sreg;a[41]=this.dreg;a[42]=this.reg_pdpte;this.store_current_tsc();a[43]=this.current_tsc;a[45]=this.devices.virtio_9p;a[46]=this.devices.apic;a[47]=this.devices.rtc;a[48]=this.devices.pci;a[49]=this.devices.dma;a[50]=this.devices.acpi;a[52]=this.devices.vga;a[53]=this.devices.ps2;
620
+ a[54]=this.devices.uart0;a[55]=this.devices.fdc;a[56]=this.devices.cdrom;a[57]=this.devices.hda;a[58]=this.devices.pit;a[59]=this.devices.net;a[60]=this.get_state_pic();a[61]=this.devices.sb16;a[62]=this.fw_value;a[63]=this.devices.ioapic;a[64]=this.tss_size_32[0];a[66]=this.reg_xmm32s;a[67]=this.fpu_st;a[68]=this.fpu_stack_empty[0];a[69]=this.fpu_stack_ptr[0];a[70]=this.fpu_control_word[0];a[71]=this.fpu_ip[0];a[72]=this.fpu_ip_selector[0];a[73]=this.fpu_dp[0];a[74]=this.fpu_dp_selector[0];a[75]=
621
+ this.fpu_opcode[0];const {packed_memory:b,bitmap:c}=this.pack_memory();a[77]=b;a[78]=new Uint8Array(c.get_buffer());a[79]=this.devices.uart1;a[80]=this.devices.uart2;a[81]=this.devices.uart3;a[82]=this.devices.virtio_console;a[83]=this.devices.virtio_net;a[84]=this.devices.virtio_balloon;return a};P.prototype.get_state_pic=function(){const a=new Uint8Array(this.wasm_memory.buffer,this.get_pic_addr_master(),13),b=new Uint8Array(this.wasm_memory.buffer,this.get_pic_addr_slave(),13),c=[],d=[];c[0]=a[0];
622
+ c[1]=a[1];c[2]=a[2];c[3]=a[3];c[4]=a[4];c[5]=d;c[6]=a[6];c[7]=a[7];c[8]=a[8];c[9]=a[9];c[10]=a[10];c[11]=a[11];c[12]=a[12];d[0]=b[0];d[1]=b[1];d[2]=b[2];d[3]=b[3];d[4]=b[4];d[5]=null;d[6]=b[6];d[7]=b[7];d[8]=b[8];d[9]=b[9];d[10]=b[10];d[11]=b[11];d[12]=b[12];return c};P.prototype.set_state=function(a){this.memory_size[0]=a[0];this.mem8.length!==this.memory_size[0]&&console.warn("Note: Memory size mismatch. we="+this.mem8.length+" state="+this.memory_size[0]);8===a[1].length?(this.segment_is_null.set(a[1]),
623
+ this.segment_access_bytes.fill(242),this.segment_access_bytes[1]=250):16===a[1].length?(this.segment_is_null.set(a[1].subarray(0,8)),this.segment_access_bytes.set(a[1].subarray(8,16))):k("Unexpected cpu segment state length:"+a[1].length);this.segment_offsets.set(a[2]);this.segment_limits.set(a[3]);this.protected_mode[0]=a[4];this.idtr_offset[0]=a[5];this.idtr_size[0]=a[6];this.gdtr_offset[0]=a[7];this.gdtr_size[0]=a[8];this.page_fault[0]=a[9];this.cr.set(a[10]);this.cpl[0]=a[11];this.is_32[0]=a[13];
624
+ this.stack_size_32[0]=a[16];this.in_hlt[0]=a[17];this.last_virt_eip[0]=a[18];this.eip_phys[0]=a[19];this.sysenter_cs[0]=a[22];this.sysenter_eip[0]=a[23];this.sysenter_esp[0]=a[24];this.prefixes[0]=a[25];this.flags[0]=a[26];this.flags_changed[0]=a[27];this.last_op1[0]=a[28];this.last_op_size[0]=a[30];this.instruction_pointer[0]=a[37];this.previous_ip[0]=a[38];this.reg32.set(a[39]);this.sreg.set(a[40]);this.dreg.set(a[41]);a[42]&&this.reg_pdpte.set(a[42]);this.set_tsc(a[43][0],a[43][1]);this.devices.virtio_9p&&
625
+ this.devices.virtio_9p.set_state(a[45]);this.devices.apic&&this.devices.apic.set_state(a[46]);this.devices.rtc&&this.devices.rtc.set_state(a[47]);this.devices.pci&&this.devices.pci.set_state(a[48]);this.devices.dma&&this.devices.dma.set_state(a[49]);this.devices.acpi&&this.devices.acpi.set_state(a[50]);this.devices.vga&&this.devices.vga.set_state(a[52]);this.devices.ps2&&this.devices.ps2.set_state(a[53]);this.devices.uart0&&this.devices.uart0.set_state(a[54]);this.devices.fdc&&this.devices.fdc.set_state(a[55]);
626
+ this.devices.cdrom&&this.devices.cdrom.set_state(a[56]);this.devices.hda&&this.devices.hda.set_state(a[57]);this.devices.pit&&this.devices.pit.set_state(a[58]);this.devices.net&&this.devices.net.set_state(a[59]);this.set_state_pic(a[60]);this.devices.sb16&&this.devices.sb16.set_state(a[61]);this.devices.uart1&&this.devices.uart1.set_state(a[79]);this.devices.uart2&&this.devices.uart2.set_state(a[80]);this.devices.uart3&&this.devices.uart3.set_state(a[81]);this.devices.virtio_console&&this.devices.virtio_console.set_state(a[82]);
627
+ this.devices.virtio_net&&this.devices.virtio_net.set_state(a[83]);this.devices.virtio_balloon&&this.devices.virtio_balloon.set_state(a[84]);this.fw_value=a[62];this.devices.ioapic&&this.devices.ioapic.set_state(a[63]);this.tss_size_32[0]=a[64];this.reg_xmm32s.set(a[66]);this.fpu_st.set(a[67]);this.fpu_stack_empty[0]=a[68];this.fpu_stack_ptr[0]=a[69];this.fpu_control_word[0]=a[70];this.fpu_ip[0]=a[71];this.fpu_ip_selector[0]=a[72];this.fpu_dp[0]=a[73];this.fpu_dp_selector[0]=a[74];this.fpu_opcode[0]=
628
+ a[75];const b=new ra(a[78].buffer);this.unpack_memory(b,a[77]);this.update_state_flags();this.full_clear_tlb();this.jit_clear_cache()};P.prototype.set_state_pic=function(a){const b=new Uint8Array(this.wasm_memory.buffer,this.get_pic_addr_master(),13),c=new Uint8Array(this.wasm_memory.buffer,this.get_pic_addr_slave(),13);b[0]=a[0];b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];const d=a[5];b[6]=a[6];b[7]=a[7];b[8]=a[8];b[9]=a[9];b[10]=a[10];b[11]=a[11];b[12]=a[12];c[0]=d[0];c[1]=d[1];c[2]=d[2];c[3]=d[3];
629
+ c[4]=d[4];c[6]=d[6];c[7]=d[7];c[8]=d[8];c[9]=d[9];c[10]=d[10];c[11]=d[11];c[12]=d[12]};P.prototype.pack_memory=function(){k(0===(this.mem8.length&4095));var a=this.mem8.length>>12,b=[];for(var c=0;c<a;c++)this.is_memory_zeroed(c<<12,4096)||b.push(c);a=new ra(a);c=new Uint8Array(b.length<<12);for(const [d,e]of b.entries())a.set(e,1),b=e<<12,b=this.mem8.subarray(b,b+4096),c.set(b,d<<12);return{bitmap:a,packed_memory:c}};P.prototype.unpack_memory=function(a,b){this.zero_memory(0,this.memory_size[0]);
630
+ const c=this.memory_size[0]>>12;let d=0;for(let f=0;f<c;f++)if(a.get(f)){var e=d<<12;e=b.subarray(e,e+4096);this.mem8.set(e,f<<12);d++}};P.prototype.reboot_internal=function(){this.reset_cpu();this.fw_value=[];this.devices.virtio_9p&&this.devices.virtio_9p.reset();this.devices.virtio_console&&this.devices.virtio_console.reset();this.devices.virtio_net&&this.devices.virtio_net.reset();this.devices.ps2&&this.devices.ps2.reset();this.load_bios()};P.prototype.reset_memory=function(){this.mem8.fill(0)};
631
+ P.prototype.create_memory=function(a,b){a<b?(a=b,C("Rounding memory size up to "+a,2)):0>(a|0)&&(a=Math.pow(2,31)-131072,C("Rounding memory size down to "+a,2));a=(a-1|131071)+1|0;k(0<(a|0));k(0===(a&131071));console.assert(0===this.memory_size[0],"Expected uninitialised memory");this.memory_size[0]=a;b=this.allocate_memory(a);this.mem8=g(Uint8Array,this.wasm_memory,b,a);this.mem32s=g(Uint32Array,this.wasm_memory,b,a>>2)};P.prototype.init=function(a,b){this.create_memory(a.memory_size||67108864,a.initrd?
632
+ 67108864:1048576);a.disable_jit&&this.set_jit_config(0,1);a.cpuid_level&&this.set_cpuid_level(a.cpuid_level);this.acpi_enabled[0]=+a.acpi;this.reset_cpu();var c=new Ea(this);this.io=c;this.bios.main=a.bios;this.bios.vga=a.vga_bios;this.load_bios();if(a.bzimage){const e=Zc(this.mem8,a.bzimage,a.initrd,a.cmdline||"");e&&this.option_roms.push(e)}c.register_read(179,this,function(){C("port 0xB3 read");return 0});var d=0;c.register_read(146,this,function(){return d});c.register_write(146,this,function(e){d=
633
+ e});c.register_read(1297,this,function(){if(this.fw_pointer<this.fw_value.length)return this.fw_value[this.fw_pointer++];k(!1,"config port: Read past value");return 0});c.register_write(1296,this,void 0,function(e){function f(p){return new Uint8Array(Int32Array.of(p).buffer)}function h(p){return p>>8|p<<8&65280}function l(p){return p<<24|p<<8&16711680|p>>8&65280|p>>>24}C("bios config port, index="+m(e));this.fw_pointer=0;if(0===e)this.fw_value=f(1431127377);else if(1===e)this.fw_value=f(0);else if(3===
634
+ e)this.fw_value=f(this.memory_size[0]);else if(5===e)this.fw_value=f(1);else if(15===e)this.fw_value=f(1);else if(13===e)this.fw_value=new Uint8Array(16);else if(25===e){e=new Int32Array(4+64*this.option_roms.length);const p=new Uint8Array(e.buffer);e[0]=l(this.option_roms.length);for(let n=0;n<this.option_roms.length;n++){const {name:q,data:r}=this.option_roms[n],t=4+64*n;k(65536>49152+n);e[t+0>>2]=l(r.length);e[t+4>>2]=h(49152+n);k(56>q.length);for(let u=0;u<q.length;u++)p[t+8+u]=q.charCodeAt(u)}this.fw_value=
635
+ p}else 32768<=e&&49152>e?this.fw_value=f(0):49152<=e&&e-49152<this.option_roms.length?this.fw_value=this.option_roms[e-49152].data:(C("Warning: Unimplemented fw index: "+m(e)),this.fw_value=f(0))});c.register_write(128,this,function(){});c.register_read(128,this,function(){return 255});c.register_write(233,this,function(){});c.register_read(32,this,this.port20_read);c.register_read(33,this,this.port21_read);c.register_read(160,this,this.portA0_read);c.register_read(161,this,this.portA1_read);c.register_write(32,
636
+ this,this.port20_write);c.register_write(33,this,this.port21_write);c.register_write(160,this,this.portA0_write);c.register_write(161,this,this.portA1_write);c.register_read(1232,this,this.port4D0_read);c.register_read(1233,this,this.port4D1_read);c.register_write(1232,this,this.port4D0_write);c.register_write(1233,this,this.port4D1_write);this.devices={};a.load_devices&&(this.devices.pci=new Dc(this),this.acpi_enabled[0]&&(this.devices.ioapic=new Ac(this),this.devices.apic=new zc(this),this.devices.acpi=
637
+ new Bc(this)),this.devices.rtc=new hb(this),this.fill_cmos(this.devices.rtc,a),this.devices.dma=new D(this),this.devices.vga=new X(this,b,a.screen,a.vga_memory_size||8388608),this.devices.ps2=new Jc(this,b),this.devices.uart0=new Cc(this,1016,b),a.uart1&&(this.devices.uart1=new Cc(this,760,b)),a.uart2&&(this.devices.uart2=new Cc(this,1E3,b)),a.uart3&&(this.devices.uart3=new Cc(this,744,b)),this.devices.fdc=new V(this,a.fda,a.fdb),c=0,a.hda&&(this.devices.hda=new Tc(this,a.hda,a.hdb,!1,c++,b)),a.cdrom&&
638
+ (this.devices.cdrom=new Tc(this,a.cdrom,void 0,!0,c++,b)),this.devices.pit=new jb(this,b),"ne2k"===a.net_device.type?this.devices.net=new Gc(this,b,a.preserve_mac_from_state_image,a.mac_address_translation):"virtio"===a.net_device.type&&(this.devices.virtio_net=new Uc(this,b,a.preserve_mac_from_state_image)),a.fs9p&&(this.devices.virtio_9p=new $c(a.fs9p,this,b)),a.virtio_console&&(this.devices.virtio_console=new Hc(this,b)),a.virtio_balloon&&(this.devices.virtio_balloon=new Yc(this,b)),this.devices.sb16=
639
+ new E(this,b));a.multiboot&&(C("loading multiboot",2),a=this.load_multiboot_option_rom(a.multiboot,a.initrd,a.cmdline))&&(this.bios.main?(C("adding option rom for multiboot",2),this.option_roms.push(a)):(C("loaded multiboot without bios",2),this.reg32[0]=this.io.port_read32(244)));this.debug.init()};P.prototype.load_multiboot=function(a){this.bios.main&&k(!1,"load_multiboot not supported with BIOS");this.load_multiboot_option_rom(a,void 0,"")&&(C("loaded multiboot",2),this.reg32[0]=this.io.port_read32(244))};
640
+ P.prototype.load_multiboot_option_rom=function(a,b,c){C("Trying multiboot from buffer of size "+a.byteLength,2);if(8192>a.byteLength){var d=new Int32Array(2048);(new Uint8Array(d.buffer)).set(new Uint8Array(a))}else d=new Int32Array(a,0,2048);for(var e=0;8192>e;e+=4){if(464367618===d[e>>2]){var f=d[e+4>>2];if(464367618+f+d[e+8>>2]|0){C("Multiboot checksum check failed",2);continue}}else continue;C("Multiboot magic found, flags: "+m(f>>>0,8),2);k(0===(f&-65540),"TODO");var h=this;this.io.register_read(244,
641
+ this,function(){return 0},function(){return 0},function(){var q=31860,r=0;if(c){r|=4;h.write32(31760,q);c+="\x00";var t=(new TextEncoder).encode(c);h.write_blob(t,q);q+=t.length}if(f&2){r|=64;t=0;h.write32(31788,0);h.write32(31792,q);var u=0;let w=!1;for(let F=0;4294967296>F;F+=131072)w&&void 0!==h.memory_map_read8[F>>>17]?(h.write32(q,20),h.write32(q+4,u),h.write32(q+8,0),h.write32(q+12,F-u),h.write32(q+16,0),h.write32(q+20,1),q+=24,t+=24,w=!1):w||void 0!==h.memory_map_read8[F>>>17]||(u=F,w=!0);
642
+ k(!w,"top of 4GB shouldn't have memory");h.write32(31788,t)}h.write32(31744,r);t=r=0;if(f&65536){C("Multiboot specifies its own address table",2);var y=d[e+12>>2];var A=d[e+16>>2];var v=d[e+20>>2];var B=d[e+24>>2];r=d[e+28>>2];C("header="+m(y,8)+" load="+m(A,8)+" load_end="+m(v,8)+" bss_end="+m(B,8)+" entry="+m(r,8));k(A<=y);y=e-(y-A);0===v?t=void 0:(k(v>=A),t=v-A);y=new Uint8Array(a,y,t);h.write_blob(y,A);r|=0;t=Math.max(v,B)}else if(1179403647===d[0]){C("Multiboot image is in elf format",2);u=new DataView(a);
643
+ const [w,F]=Rc(u,Oc);console.assert(52===F);for(y of Object.keys(w))C(y+": 0x"+(w[y].toString(16)>>>0));console.assert(1179403647===w.magic,"Bad magic");console.assert(1===w.class,"Unimplemented: 64 bit elf");console.assert(1===w.data,"Unimplemented: big endian");console.assert(1===w.version0,"Bad version0");console.assert(2===w.type,"Unimplemented type");console.assert(1===w.version1,"Bad version1");console.assert(52===w.ehsize,"Bad header size");console.assert(32===w.phentsize,"Bad program header size");
644
+ console.assert(40===w.shentsize,"Bad section header size");[r]=Sc(new DataView(u.buffer,u.byteOffset+w.phoff,w.phentsize*w.phnum),Pc,w.phnum);[y]=Sc(new DataView(u.buffer,u.byteOffset+w.shoff,w.shentsize*w.shnum),Qc,w.shnum);if(va){console.log("%d program headers:",r.length);for(B of r)console.log("type=%s offset=%s vaddr=%s paddr=%s filesz=%s memsz=%s flags=%s align=%s",B.type.toString(16),B.offset.toString(16),B.vaddr.toString(16),B.paddr.toString(16),B.filesz.toString(16),B.memsz.toString(16),
645
+ B.flags.toString(16),B.align.toString(16));console.log("%d section headers:",y.length);for(A of y)console.log("name=%s type=%s flags=%s addr=%s offset=%s size=%s link=%s info=%s addralign=%s entsize=%s",A.name.toString(16),A.type.toString(16),A.flags.toString(16),A.addr.toString(16),A.offset.toString(16),A.size.toString(16),A.link.toString(16),A.info.toString(16),A.addralign.toString(16),A.entsize.toString(16))}B=w;A=r;r=B.entry;for(v of A)0!==v.type&&(1===v.type?(k(v.filesz<=v.memsz),v.paddr+v.memsz<
646
+ h.memory_size[0]?(v.filesz&&(A=new Uint8Array(a,v.offset,v.filesz),h.write_blob(A,v.paddr)),t=Math.max(t,v.paddr+v.memsz),C("prg load "+v.paddr+" to "+(v.paddr+v.memsz),2),r===B.entry&&v.vaddr<=r&&v.vaddr+v.memsz>r&&(r=r-v.vaddr+v.paddr)):C("Warning: Skipped loading section, paddr="+m(v.paddr)+" memsz="+v.memsz,2)):2===v.type||3===v.type||4===v.type||6===v.type||7===v.type||1685382480===v.type||1685382481===v.type||1685382482===v.type||1685382483===v.type?C("skip load type "+v.type+" "+v.paddr+" to "+
647
+ (v.paddr+v.memsz),2):k(!1,"unimplemented elf section type: "+m(v.type)))}else k(!1,"Not a bootable multiboot format");b&&(h.write32(31764,1),h.write32(31768,q),v=t,0!==(v&4095)&&(v=(v&-4096)+4096),C("ramdisk address "+v),B=v+b.byteLength,h.write32(q,v),h.write32(q+4,B),h.write32(q+8,0),h.write32(q+12,0),k(B<h.memory_size[0]),h.write_blob(new Uint8Array(b),v));h.reg32[3]=31744;h.cr[0]=1;h.protected_mode[0]=1;h.flags[0]=2;h.is_32[0]=1;h.stack_size_32[0]=1;for(q=0;6>q;q++)h.segment_is_null[q]=0,h.segment_offsets[q]=
648
+ 0,h.segment_limits[q]=4294967295,h.sreg[q]=45058;h.instruction_pointer[0]=h.get_seg_cs()+r|0;h.update_state_flags();C("Starting multiboot kernel at:",2);h.debug.dump_state();h.debug.dump_regs();return 732803074});this.io.register_write_consecutive(244,this,function(q){console.log("Test exited with code "+m(q,2));throw"HALT";},function(){},function(){},function(){});for(let q=0;15>=q;q++){function r(t){C("kvm-unit-test: Set irq "+m(q)+" to "+m(t,2));t?this.device_raise_irq(q):this.device_lower_irq(q)}
649
+ this.io.register_write(8192+q,this,r,r,r)}const p=new Uint8Array(512);(new Uint16Array(p.buffer))[0]=43605;p[2]=1;var l=3;p[l++]=102;p[l++]=229;p[l++]=244;k(512>l);let n=p[l]=0;for(let q=0;q<p.length;q++)n+=p[q];p[l]=-n;return{name:"genroms/multiboot.bin",data:p}}C("Multiboot header not found",2)};P.prototype.fill_cmos=function(a,b){var c=b.boot_order||291;a.cmos_write(56,1|c>>4&240);a.cmos_write(61,c&255);a.cmos_write(21,128);a.cmos_write(22,2);c=0;1048576<=this.memory_size[0]&&(c=this.memory_size[0]-
650
+ 1048576>>10,c=Math.min(c,65535));a.cmos_write(23,c&255);a.cmos_write(24,c>>8&255);a.cmos_write(48,c&255);a.cmos_write(49,c>>8&255);c=0;16777216<=this.memory_size[0]&&(c=this.memory_size[0]-16777216>>16,c=Math.min(c,65535));a.cmos_write(52,c&255);a.cmos_write(53,c>>8&255);a.cmos_write(91,0);a.cmos_write(92,0);a.cmos_write(93,0);a.cmos_write(20,47);a.cmos_write(95,0);b.fastboot&&a.cmos_write(63,1)};P.prototype.load_bios=function(){var a=this.bios.main,b=this.bios.vga;if(a){k(a instanceof ArrayBuffer);
651
+ var c=new Uint8Array(a);this.write_blob(c,1048576-a.byteLength);if(b){k(b instanceof ArrayBuffer);var d=new Uint8Array(b);this.write_blob(d,786432);this.io.mmap_register(4272947200,1048576,function(e){e=e-4272947200|0;return e<d.length?d[e]:0},function(){k(!1,"Unexpected write to VGA rom")})}else C("Warning: No VGA BIOS");this.io.mmap_register(4293918720,1048576,function(e){return this.mem8[e&1048575]}.bind(this),function(e,f){this.mem8[e&1048575]=f}.bind(this))}else C("Warning: No BIOS")};P.prototype.codegen_finalize=
652
+ function(a,b,c,d,e){d>>>=0;e>>>=0;k(0<=a&&900>a);const f=new Uint8Array(this.wasm_memory.buffer,d,e);this.seen_code[b]=(this.seen_code[b]||0)+1;this.test_hook_did_generate_wasm&&this.test_hook_did_generate_wasm(f);WebAssembly.instantiate(f,{e:this.jit_imports}).then(h=>{this.wm.wasm_table.set(a+1024,h.instance.exports.f);this.codegen_finalize_finished(a,b,c);this.test_hook_did_finalize_wasm&&this.test_hook_did_finalize_wasm(f)}).catch(h=>{console.log(h);debugger;throw h;})};P.prototype.log_uncompiled_code=
653
+ function(){};P.prototype.dump_function_code=function(){};P.prototype.run_hardware_timers=function(a,b){const c=this.devices.pit.timer(b,!1),d=this.devices.rtc.timer(b,!1);let e=100,f=100;a&&(e=this.devices.acpi.timer(b),f=this.devices.apic.timer(b));return Math.min(c,d,e,f)};P.prototype.device_raise_irq=function(a){k(1===arguments.length);this.pic_set_irq(a);this.devices.ioapic&&this.devices.ioapic.set_irq(a)};P.prototype.device_lower_irq=function(a){this.pic_clear_irq(a);this.devices.ioapic&&this.devices.ioapic.clear_irq(a)};
654
+ P.prototype.debug_init=function(){function a(n){for(var q=e.protected_mode[0]?"prot":"real",r=e.get_eflags(),t=e.getiopl(),u=e.cpl[0],y=m(e.sreg[1],4)+":"+m(e.get_real_eip()>>>0,8),A=m(e.sreg[2],4)+":"+m(e.reg32[0]>>>0,8),v=e.is_32[0]?"32":"16",B=e.flags[0]&512?1:0,w={[1]:"c",[4]:"p",[16]:"a",[64]:"z",[128]:"s",[256]:"t",[512]:"i",[1024]:"d",[2048]:"o"},F="",J=0;16>J;J++)w[1<<J]&&(F=r&1<<J?F+w[1<<J]:F+" ");return"mode="+q+"/"+v+" paging="+ +(0!==(e.cr[0]&-2147483648))+" pae="+ +(0!==(e.cr[4]&32))+
655
+ " iopl="+t+" cpl="+u+" if="+B+" cs:eip="+y+" cs_off="+m(e.get_seg_cs()>>>0,8)+" flgs="+m(e.get_eflags()>>>0,6)+" ("+F+") ss:esp="+A+" ssize="+ +e.stack_size_32[0]+(n?" in "+n:"")}function b(){for(var n={eax:0,ecx:1,edx:2,ebx:3,esp:4,ebp:5,esi:6,edi:7},q="eax ecx edx ebx esp ebp esi edi".split(" "),r="",t="",u=0;4>u;u++)r+=q[u]+"="+m(e.reg32[n[q[u]]]>>>0,8)+" ",t+=q[u+4]+"="+m(e.reg32[n[q[u+4]]]>>>0,8)+" ";r+=" ds="+m(e.sreg[3],4)+" es="+m(e.sreg[0],4)+" fs="+m(e.sreg[4],4);t+=" gs="+m(e.sreg[5],
656
+ 4)+" cs="+m(e.sreg[1],4)+" ss="+m(e.sreg[2],4);return[r,t]}function c(n,q,r){if(!(n&1))return!1;var t=128===(n&128);return{size:t,global:256===(n&256),accessed:32===(n&32),dirty:64===(n&64),cache_disable:16===(n&16),user:4===(n&4),read_write:2===(n&2),address:(t&&!r?n&(q?4292870144:4290772992):n&4294963200)>>>0}}function d(n,q,r){for(var t=q?512:1024,u=q?8:4,y=q?21:22,A=0;A<t;A++){var v=e.read32s(n+A*u),B=c(v,q,!0);if(B)if(v="",v+=B.size?"S ":" ",v+=B.accessed?"A ":" ",v+=B.cache_disable?"Cd ":
657
+ " ",v+=B.user?"U ":" ",v+=B.read_write?"Rw ":" ",B.size)C("=== "+m(r+(A<<y)>>>0,8)+" -> "+m(B.address>>>0,8)+" | "+v);else{C("=== "+m(r+(A<<y)>>>0,8)+" | "+v);for(var w=0;w<t;w++){var F=B.address+w*u;v=e.read32s(F);var J=c(v,q,!1);J&&(v="",v+=J.cache_disable?"Cd ":" ",v+=J.user?"U ":" ",v+=J.read_write?"Rw ":" ",v+=J.global?"G ":" ",v+=J.accessed?"A ":" ",v+=J.dirty?"Di ":" ",C("# "+m(r+(A<<y|w<<12)>>>0,8)+" -> "+m(J.address,8)+" | "+v+" (at "+m(F,8)+")"))}}}}var e=this,f={};this.debug=
658
+ f;f.init=function(){function n(r){10===r?(C(q,4096),q=""):q+=String.fromCharCode(r)}if(e.io){var q="";e.io.register_write(1026,this,n);e.io.register_write(1280,this,n)}};f.get_regs_short=b;f.dump_regs=function(){var n=b();C(n[0],2);C(n[1],2)};f.get_state=a;f.dump_state=function(n){C(a(n),2)};f.dump_stack=function(n,q){var r=e.reg32[4];C("========= STACK ==========");if(q>=n||void 0===q)n=5,q=-5;for(;n>q;n--){var t=" ";n||(t="=> ");t+=m(n,2)+" | ";C(t+m(r+4*n,8)+" | "+m(e.read32s(r+4*n)>>>0))}};
659
+ f.dump_page_structures=function(){if(e.cr[4]&32){C("PAE enabled");for(var n=0;4>n;n++){var q=e.read32s(e.cr[3]+8*n);q&1&&d(q&4294963200,!0,n<<30)}}else C("PAE disabled"),d(e.cr[3],!1,0)};f.dump_gdt_ldt=function(){function n(q,r){for(var t=0;t<r;t+=8,q+=8){var u=e.read16(q+2)|e.read8(q+4)<<16|e.read8(q+7)<<24,y=e.read16(q)|(e.read8(q+6)&15)<<16,A=e.read8(q+5),v=e.read8(q+6)>>4,B="",w=A>>5&3;B=A&128?B+" P ":B+"NP ";A&16?(B=v&4?B+"32b ":B+"16b ",A&8?(B+="X ",A&4&&(B+="C ")):B+="R ",B+="RW "):B+="sys: "+
660
+ m(A&15);v&8&&(y=y<<12|4095);C(m(t&-8,4)+" "+m(u>>>0,8)+" ("+m(y>>>0,8)+" bytes) "+B+"; dpl = "+w+", a = "+A.toString(2)+", f = "+v.toString(2))}}C("gdt: (len = "+m(e.gdtr_size[0])+")");n(e.translate_address_system_read(e.gdtr_offset[0]),e.gdtr_size[0]);C("\nldt: (len = "+m(e.segment_limits[7])+")");n(e.translate_address_system_read(e.segment_offsets[7]),e.segment_limits[7])};f.dump_idt=function(){for(var n=0;n<e.idtr_size[0];n+=8){var q=e.translate_address_system_read(e.idtr_offset[0]+n),r=e.read16(q)|
661
+ e.read16(q+6)<<16,t=e.read16(q+2);q=e.read8(q+5);var u=q>>5&3;var y=5===(q&31)?"task gate ":14===(q&31)?"intr gate ":15===(q&31)?"trap gate ":"invalid ";y=q&128?y+" P":y+"NP";C(m(n>>3,4)+" "+m(r>>>0,8)+", "+m(t,4)+"; "+y+"; dpl = "+u+", t = "+q.toString(2))}};f.get_memory_dump=function(n,q){void 0===n?(n=0,q=e.memory_size[0]):void 0===q&&(q=n,n=0);return e.mem8.slice(n,n+q).buffer};f.memory_hex_dump=function(n,q){q=q||64;for(var r,t,u=0;u<q>>4;u++){r=m(n+(u<<4),5)+" ";for(var y=0;16>y;y++)t=
662
+ e.read8(n+(u<<4)+y),r+=m(t,2)+" ";r+=" ";for(y=0;16>y;y++)t=e.read8(n+(u<<4)+y),r+=33>t||126<t?".":String.fromCharCode(t);C(r)}};f.used_memory_dump=function(){for(var n=e.memory_size[0]/128/16|0,q,r=0;16>r;r++){q=m(128*r*n,8)+" | ";for(var t=0;128>t;t++)q+=0<e.mem32s[(128*r+t)*n]?"X":" ";C(q)}};f.debug_interrupt=function(){};let h,l;f.dump_code=function(n,q,r){if(!l){if(void 0===h&&(h="function"===typeof require?require("./capstone-x86.min.js"):window.cs,void 0===h)){C("Warning: Missing capstone library, disassembly not available");
663
+ return}l=[new h.Capstone(h.ARCH_X86,h.MODE_16),new h.Capstone(h.ARCH_X86,h.MODE_32)]}try{l[n].disasm(q,r).forEach(function(t){C(m(t.address>>>0)+": "+ba(t.bytes.map(u=>m(u,2).slice(-2)).join(" "),20)+" "+t.mnemonic+" "+t.op_str)}),C("")}catch(t){C("Could not disassemble: "+Array.from(q).map(u=>m(u,2)).join(" "))}};let p;f.dump_wasm=function(n){if(void 0===p&&(p="function"===typeof require?require("./libwabt.cjs"):new window.WabtModule,void 0===p)){C("Warning: Missing libwabt, wasm dump not available");
664
+ return}n=n.slice();try{var q=p.readWasm(n,{readDebugNames:!1});q.generateNames();q.applyNames();const u=q.toText({foldExprs:!0,inlineExport:!0});C(u)}catch(u){var r=new Blob([n]),t=document.createElement("a");t.download="failed.wasm";t.href=window.URL.createObjectURL(r);t.dataset.downloadurl=["application/octet-stream",t.download,t.href].join(":");t.click();window.URL.revokeObjectURL(t.src);console.log(u.toString())}finally{q&&q.destroy()}}};function Ic(a,b){this.cpu=a;this.pci=a.devices.pci;this.device_id=
665
+ b.device_id;this.pci_space=[244,26,b.device_id&255,b.device_id>>8,7,5,16,0,1,0,2,0,0,0,0,0,1,168,0,0,0,16,191,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,b.subsystem_device_id&255,b.subsystem_device_id>>8,0,0,0,0,64,0,0,0,0,0,0,0,0,1,0,0];this.pci_space=this.pci_space.concat(Array(256-this.pci_space.length).fill(0));this.pci_id=b.pci_id;this.pci_bars=[];this.name=b.name;this.driver_feature_select=this.device_feature_select=0;this.device_feature=new Uint32Array(4);this.driver_feature=new Uint32Array(4);
666
+ for(var c of b.common.features)k(0<=c,"VirtIO device<"+this.name+"> feature bit numbers must be non-negative"),k(128>c,"VirtIO device<"+this.name+"> feature bit numbers assumed less than 128 in implementation"),this.device_feature[c>>>5]|=1<<(c&31),this.driver_feature[c>>>5]|=1<<(c&31);k(b.common.features.includes(32),"VirtIO device<"+this.name+"> only non-transitional devices are supported");this.features_ok=!0;this.device_status=0;this.config_has_changed=!1;this.config_generation=0;this.queues=
667
+ [];for(var d of b.common.queues)this.queues.push(new Y(a,this,d));this.queue_select=0;this.queue_selected=this.queues[0];this.isr_status=0;c=new Set;for(var e of this.queues.map(f=>f.notify_offset))d=b.notification.single_handler?0:e,c.add(d),k(b.notification.handlers[d],"VirtIO device<"+this.name+"> every queue's notifier must exist");for(const [f,h]of b.notification.handlers.entries())k(!h||c.has(f),"VirtIO device<"+this.name+"> no defined notify handler should be unused");e=[];e.push(this.create_common_capability(b.common));
668
+ e.push(this.create_notification_capability(b.notification));e.push(this.create_isr_capability(b.isr_status));b.device_specific&&e.push(this.create_device_specific_capability(b.device_specific));this.init_capabilities(e);a.devices.pci.register_device(this);this.reset()}Ic.prototype.create_common_capability=function(a){return{type:1,bar:0,port:a.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array(0),struct:[{bytes:4,name:"device_feature_select",read:()=>this.device_feature_select,write:b=>{this.device_feature_select=
669
+ b}},{bytes:4,name:"device_feature",read:()=>this.device_feature[this.device_feature_select]||0,write:()=>{}},{bytes:4,name:"driver_feature_select",read:()=>this.driver_feature_select,write:b=>{this.driver_feature_select=b}},{bytes:4,name:"driver_feature",read:()=>this.driver_feature[this.driver_feature_select]||0,write:b=>{const c=this.device_feature[this.driver_feature_select];this.driver_feature_select<this.driver_feature.length&&(this.driver_feature[this.driver_feature_select]=b&c);this.features_ok=
670
+ this.features_ok&&!(b&~c)}},{bytes:2,name:"msix_config",read:()=>{C("No msi-x capability supported.",2097152);return 65535},write:()=>{C("No msi-x capability supported.",2097152)}},{bytes:2,name:"num_queues",read:()=>this.queues.length,write:()=>{}},{bytes:1,name:"device_status",read:()=>this.device_status,write:b=>{0===b?(C("Reset device<"+this.name+">",2097152),this.reset()):b&128?C("Warning: Device<"+this.name+"> status failed",2097152):C("Device<"+this.name+"> status: "+(b&1?"ACKNOWLEDGE ":"")+
671
+ (b&2?"DRIVER ":"")+(b&4?"DRIVER_OK":"")+(b&8?"FEATURES_OK ":"")+(b&64?"DEVICE_NEEDS_RESET":""),2097152);b&~this.device_status&4&&this.device_status&64&&this.notify_config_changes();this.features_ok||(b&8&&C("Removing FEATURES_OK",2097152),b&=-9);this.device_status=b;if(b&~this.device_status&4)a.on_driver_ok()}},{bytes:1,name:"config_generation",read:()=>this.config_generation,write:()=>{}},{bytes:2,name:"queue_select",read:()=>this.queue_select,write:b=>{this.queue_select=b;this.queue_selected=this.queue_select<
672
+ this.queues.length?this.queues[this.queue_select]:null}},{bytes:2,name:"queue_size",read:()=>this.queue_selected?this.queue_selected.size:0,write:b=>{this.queue_selected&&(b&b-1&&(C("Warning: dev<"+this.name+"> Given queue size was not a power of 2. Rounding up to next power of 2.",2097152),b=1<<ea(b-1)+1),b>this.queue_selected.size_supported&&(C("Warning: dev<"+this.name+"> Trying to set queue size greater than supported. Clamping to supported size.",2097152),b=this.queue_selected.size_supported),
673
+ this.queue_selected.set_size(b))}},{bytes:2,name:"queue_msix_vector",read:()=>{C("No msi-x capability supported.",2097152);return 65535},write:()=>{C("No msi-x capability supported.",2097152)}},{bytes:2,name:"queue_enable",read:()=>this.queue_selected?this.queue_selected.enabled|0:0,write:b=>{this.queue_selected&&(1===b?this.queue_selected.is_configured()?this.queue_selected.enable():C("Driver bug: tried enabling unconfigured queue",2097152):0===b&&C("Driver bug: tried writing 0 to queue_enable",
674
+ 2097152))}},{bytes:2,name:"queue_notify_off",read:()=>this.queue_selected?this.queue_selected.notify_offset:0,write:()=>{}},{bytes:4,name:"queue_desc (low dword)",read:()=>this.queue_selected?this.queue_selected.desc_addr:0,write:b=>{this.queue_selected&&(this.queue_selected.desc_addr=b)}},{bytes:4,name:"queue_desc (high dword)",read:()=>0,write:b=>{0!==b&&C("Warning: High dword of 64 bit queue_desc ignored:"+b,2097152)}},{bytes:4,name:"queue_avail (low dword)",read:()=>this.queue_selected?this.queue_selected.avail_addr:
675
+ 0,write:b=>{this.queue_selected&&(this.queue_selected.avail_addr=b)}},{bytes:4,name:"queue_avail (high dword)",read:()=>0,write:b=>{0!==b&&C("Warning: High dword of 64 bit queue_avail ignored:"+b,2097152)}},{bytes:4,name:"queue_used (low dword)",read:()=>this.queue_selected?this.queue_selected.used_addr:0,write:b=>{this.queue_selected&&(this.queue_selected.used_addr=b)}},{bytes:4,name:"queue_used (high dword)",read:()=>0,write:b=>{0!==b&&C("Warning: High dword of 64 bit queue_used ignored:"+b,2097152)}}]}};
676
+ Ic.prototype.create_notification_capability=function(a){const b=[];let c;a.single_handler?(k(1===a.handlers.length,"VirtIO device<"+this.name+"> too many notify handlers specified: expected single handler"),c=0):c=2;for(const [d,e]of a.handlers.entries())b.push({bytes:2,name:"notify"+d,read:()=>65535,write:e||(()=>{})});return{type:2,bar:1,port:a.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array([c&255,c>>8&255,c>>16&255,c>>24]),struct:b}};Ic.prototype.create_isr_capability=function(a){return{type:3,
677
+ bar:2,port:a.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array(0),struct:[{bytes:1,name:"isr_status",read:()=>{const b=this.isr_status;this.lower_irq();return b},write:()=>{}}]}};Ic.prototype.create_device_specific_capability=function(a){k(~a.offset&3,"VirtIO device<"+this.name+"> device specific cap offset must be 4-byte aligned");return{type:4,bar:3,port:a.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array(0),struct:a.struct}};Ic.prototype.init_capabilities=function(a){let b=this.pci_space[52]=
678
+ 64;var c=b;for(const e of a){a=16+e.extra.length;c=b;b=c+a;k(256>=b,"VirtIO device<"+this.name+"> can't fit all capabilities into 256byte configspace");k(0<=e.bar&&6>e.bar,"VirtIO device<"+this.name+"> capability invalid bar number");var d=e.struct.reduce((f,h)=>f+h.bytes,0);d+=e.offset;d=16>d?16:1<<ea(d-1)+1;k(0===(e.port&d-1),"VirtIO device<"+this.name+"> capability port should be aligned to pci bar size");this.pci_bars[e.bar]={size:d};this.pci_space[c]=9;this.pci_space[c+1]=b;this.pci_space[c+
679
+ 2]=a;this.pci_space[c+3]=e.type;this.pci_space[c+4]=e.bar;this.pci_space[c+5]=0;this.pci_space[c+6]=0;this.pci_space[c+7]=0;this.pci_space[c+8]=e.offset&255;this.pci_space[c+9]=e.offset>>>8&255;this.pci_space[c+10]=e.offset>>>16&255;this.pci_space[c+11]=e.offset>>>24;this.pci_space[c+12]=d&255;this.pci_space[c+13]=d>>>8&255;this.pci_space[c+14]=d>>>16&255;this.pci_space[c+15]=d>>>24;for(const [f,h]of e.extra.entries())this.pci_space[c+16+f]=h;c=16+4*e.bar;this.pci_space[c]=e.port&254|!e.use_mmio;
680
+ this.pci_space[c+1]=e.port>>>8&255;this.pci_space[c+2]=e.port>>>16&255;this.pci_space[c+3]=e.port>>>24&255;c=e.port+e.offset;for(const f of e.struct){let h=f.read;a=f.write;h=()=>{const l=f.read();C("Device<"+this.name+"> cap["+e.type+"] read["+f.name+"] => "+m(l,8*f.bytes),2097152);return l};a=l=>{C("Device<"+this.name+"> cap["+e.type+"] write["+f.name+"] <= "+m(l,8*f.bytes),2097152);f.write(l)};if(e.use_mmio)k(!1,"VirtIO device <"+this.name+"> mmio capability not implemented.");else{d=function(p){C("Warning: 8-bit read from 16-bit virtio port",
681
+ 2097152);return h(p&-2)>>((p&1)<<3)&255};const l=function(p){C("Warning: 8-bit read from 32-bit virtio port",2097152);return h(p&-4)>>((p&3)<<3)&255};switch(f.bytes){case 4:this.cpu.io.register_read(c,this,l,void 0,h);this.cpu.io.register_read(c+1,this,l);this.cpu.io.register_read(c+2,this,l);this.cpu.io.register_read(c+3,this,l);this.cpu.io.register_write(c,this,void 0,void 0,a);break;case 2:this.cpu.io.register_read(c,this,d,h);this.cpu.io.register_read(c+1,this,d);this.cpu.io.register_write(c,
682
+ this,void 0,a);break;case 1:this.cpu.io.register_read(c,this,h);this.cpu.io.register_write(c,this,a);break;default:k(!1,"VirtIO device <"+this.name+"> invalid capability field width of "+f.bytes+" bytes")}}c+=f.bytes}}k(256>=b+20,"VirtIO device<"+this.name+"> can't fit all capabilities into 256byte configspace");this.pci_space[b]=9;this.pci_space[b+1]=0;this.pci_space[b+2]=20;this.pci_space[b+3]=5;this.pci_space[b+4]=0;this.pci_space[b+5]=0;this.pci_space[b+6]=0;this.pci_space[b+7]=0;this.pci_space[b+
683
+ 8]=0;this.pci_space[b+9]=0;this.pci_space[b+10]=0;this.pci_space[b+11]=0;this.pci_space[b+12]=0;this.pci_space[b+13]=0;this.pci_space[b+14]=0;this.pci_space[b+15]=0;this.pci_space[b+16]=0;this.pci_space[b+17]=0;this.pci_space[b+18]=0;this.pci_space[b+19]=0};Ic.prototype.get_state=function(){let a=[];a[0]=this.device_feature_select;a[1]=this.driver_feature_select;a[2]=this.device_feature;a[3]=this.driver_feature;a[4]=this.features_ok;a[5]=this.device_status;a[6]=this.config_has_changed;a[7]=this.config_generation;
684
+ a[8]=this.isr_status;a[9]=this.queue_select;return a=a.concat(this.queues)};Ic.prototype.set_state=function(a){this.device_feature_select=a[0];this.driver_feature_select=a[1];this.device_feature=a[2];this.driver_feature=a[3];this.features_ok=a[4];this.device_status=a[5];this.config_has_changed=a[6];this.config_generation=a[7];this.isr_status=a[8];this.queue_select=a[9];let b=0;for(const c of a.slice(10))this.queues[b].set_state(c),b++;this.queue_selected=this.queues[this.queue_select]||null};Ic.prototype.reset=
685
+ function(){this.driver_feature_select=this.device_feature_select=0;this.driver_feature.set(this.device_feature);this.features_ok=!0;this.queue_select=this.device_status=0;this.queue_selected=this.queues[0];for(const a of this.queues)a.reset();this.config_has_changed=!1;this.config_generation=0;this.lower_irq()};Ic.prototype.notify_config_changes=function(){this.config_has_changed=!0;this.device_status&4?this.raise_irq(2):k(!1,"VirtIO device<"+this.name+"> attempted to notify driver before DRIVER_OK")};
686
+ Ic.prototype.update_config_generation=function(){this.config_has_changed&&(this.config_generation++,this.config_generation&=255,this.config_has_changed=!1)};Ic.prototype.is_feature_negotiated=function(a){return 0<(this.driver_feature[a>>>5]&1<<(a&31))};Ic.prototype.needs_reset=function(){C("Device<"+this.name+"> experienced error - requires reset",2097152);this.device_status|=64;this.device_status&4&&this.notify_config_changes()};Ic.prototype.raise_irq=function(a){C("Raise irq "+m(a),2097152);this.isr_status|=
687
+ a;this.pci.raise_irq(this.pci_id)};Ic.prototype.lower_irq=function(){C("Lower irq ",2097152);this.isr_status=0;this.pci.lower_irq(this.pci_id)};function Y(a,b,c){this.cpu=a;this.virtio=b;this.size_supported=this.size=c.size_supported;this.mask=this.size-1;this.enabled=!1;this.notify_offset=c.notify_offset;this.num_staged_replies=this.used_addr=this.avail_last_idx=this.avail_addr=this.desc_addr=0;this.reset()}Y.prototype.get_state=function(){const a=[];a[0]=this.size;a[1]=this.size_supported;a[2]=
688
+ this.enabled;a[3]=this.notify_offset;a[4]=this.desc_addr;a[5]=this.avail_addr;a[6]=this.avail_last_idx;a[7]=this.used_addr;a[8]=this.num_staged_replies;a[9]=1;return a};Y.prototype.set_state=function(a){this.size=a[0];this.size_supported=a[1];this.enabled=a[2];this.notify_offset=a[3];this.desc_addr=a[4];this.avail_addr=a[5];this.avail_last_idx=a[6];this.used_addr=a[7];this.num_staged_replies=a[8];this.mask=this.size-1;this.fix_wrapping=1!==a[9]};Y.prototype.reset=function(){this.enabled=!1;this.num_staged_replies=
689
+ this.used_addr=this.avail_last_idx=this.avail_addr=this.desc_addr=0;this.set_size(this.size_supported)};Y.prototype.is_configured=function(){return this.desc_addr&&this.avail_addr&&this.used_addr};Y.prototype.enable=function(){k(this.is_configured(),"VirtQueue must be configured before enabled");this.enabled=!0};Y.prototype.set_size=function(a){k(0===(a&a-1),"VirtQueue size must be power of 2 or zero");k(a<=this.size_supported,"VirtQueue size must be within supported size");this.size=a;this.mask=
690
+ a-1};Y.prototype.count_requests=function(){k(this.avail_addr,"VirtQueue addresses must be configured before use");this.fix_wrapping&&(this.fix_wrapping=!1,this.avail_last_idx=(this.avail_get_idx()&~this.mask)+(this.avail_last_idx&this.mask));return this.avail_get_idx()-this.avail_last_idx&65535};Y.prototype.has_request=function(){k(this.avail_addr,"VirtQueue addresses must be configured before use");return 0!==this.count_requests()};Y.prototype.pop_request=function(){k(this.avail_addr,"VirtQueue addresses must be configured before use");
691
+ k(this.has_request(),"VirtQueue must not pop nonexistent request");var a=this.avail_get_entry(this.avail_last_idx);C("Pop request: avail_last_idx="+this.avail_last_idx+" desc_idx="+a,2097152);a=new ad(this,a);this.avail_last_idx=this.avail_last_idx+1&65535;return a};Y.prototype.push_reply=function(a){k(this.used_addr,"VirtQueue addresses must be configured before use");k(this.num_staged_replies<this.size,"VirtQueue replies must not exceed queue size");const b=this.used_get_idx()+this.num_staged_replies&
692
+ this.mask;C("Push reply: used_idx="+b+" desc_idx="+a.head_idx,2097152);this.used_set_entry(b,a.head_idx,a.length_written);this.num_staged_replies++};Y.prototype.flush_replies=function(){k(this.used_addr,"VirtQueue addresses must be configured before use");if(0===this.num_staged_replies)C("flush_replies: Nothing to flush",2097152);else{C("Flushing "+this.num_staged_replies+" replies",2097152);var a=this.used_get_idx()+this.num_staged_replies&65535;this.used_set_idx(a);this.num_staged_replies=0;this.virtio.is_feature_negotiated(29)?
693
+ (this.avail_get_used_event(),this.virtio.raise_irq(1)):~this.avail_get_flags()&1&&this.virtio.raise_irq(1)}};Y.prototype.notify_me_after=function(a){k(0<=a,"Must skip a non-negative number of requests");a=this.avail_get_idx()+a&65535;this.used_set_avail_event(a)};Y.prototype.get_descriptor=function(a,b){return{addr_low:this.cpu.read32s(a+16*b),addr_high:this.cpu.read32s(a+16*b+4),len:this.cpu.read32s(a+16*b+8),flags:this.cpu.read16(a+16*b+12),next:this.cpu.read16(a+16*b+14)}};Y.prototype.avail_get_flags=
694
+ function(){return this.cpu.read16(this.avail_addr)};Y.prototype.avail_get_idx=function(){return this.cpu.read16(this.avail_addr+2)};Y.prototype.avail_get_entry=function(a){return this.cpu.read16(this.avail_addr+4+2*(a&this.mask))};Y.prototype.avail_get_used_event=function(){return this.cpu.read16(this.avail_addr+4+2*this.size)};Y.prototype.used_get_flags=function(){return this.cpu.read16(this.used_addr)};Y.prototype.used_set_flags=function(a){this.cpu.write16(this.used_addr,a)};Y.prototype.used_get_idx=
695
+ function(){return this.cpu.read16(this.used_addr+2)};Y.prototype.used_set_idx=function(a){this.cpu.write16(this.used_addr+2,a)};Y.prototype.used_set_entry=function(a,b,c){this.cpu.write32(this.used_addr+4+8*a,b);this.cpu.write32(this.used_addr+8+8*a,c)};Y.prototype.used_set_avail_event=function(a){this.cpu.write16(this.used_addr+4+8*this.size,a)};function ad(a,b){this.cpu=a.cpu;this.virtio=a.virtio;this.head_idx=b;this.read_buffers=[];this.length_readable=this.read_buffer_offset=this.read_buffer_idx=
696
+ 0;this.write_buffers=[];this.length_writable=this.length_written=this.write_buffer_offset=this.write_buffer_idx=0;let c=a.desc_addr,d=0,e=a.size,f=!1;const h=this.virtio.is_feature_negotiated(28);C("<<< Descriptor chain start",2097152);do{const l=a.get_descriptor(c,b);C("descriptor: idx="+b+" addr="+m(l.addr_high,8)+":"+m(l.addr_low,8)+" len="+m(l.len,8)+" flags="+m(l.flags,4)+" next="+m(l.next,4),2097152);if(h&&l.flags&4)l.flags&1&&C("Driver bug: has set VIRTQ_DESC_F_NEXT flag in an indirect table descriptor",
697
+ 2097152),c=l.addr_low,d=b=0,e=l.len/16,C("start indirect",2097152);else{if(l.flags&2)f=!0,this.write_buffers.push(l),this.length_writable+=l.len;else{if(f){C("Driver bug: readonly buffer after writeonly buffer within chain",2097152);break}this.read_buffers.push(l);this.length_readable+=l.len}d++;if(d>e){C("Driver bug: descriptor chain cycle detected",2097152);break}if(l.flags&1)b=l.next;else break}}while(1);C("Descriptor chain end >>>",2097152)}ad.prototype.get_next_blob=function(a){let b=0,c=a.length;
698
+ for(;c;){if(this.read_buffer_idx===this.read_buffers.length){C("Device<"+this.virtio.name+"> Read more than device-readable buffers has",2097152);break}var d=this.read_buffers[this.read_buffer_idx];const e=d.addr_low+this.read_buffer_offset;d=d.len-this.read_buffer_offset;d>c?(d=c,this.read_buffer_offset+=c):(this.read_buffer_idx++,this.read_buffer_offset=0);a.set(this.cpu.read_blob(e,d),b);b+=d;c-=d}return b};ad.prototype.set_next_blob=function(a){let b=0,c=a.length;for(;c;){if(this.write_buffer_idx===
699
+ this.write_buffers.length){C("Device<"+this.virtio.name+"> Write more than device-writable capacity",2097152);break}var d=this.write_buffers[this.write_buffer_idx];const e=d.addr_low+this.write_buffer_offset;d=d.len-this.write_buffer_offset;d>c?(d=c,this.write_buffer_offset+=c):(this.write_buffer_idx++,this.write_buffer_offset=0);this.cpu.write_blob(a.subarray(b,b+d),e);b+=d;c-=d}this.length_written+=b;return b};const bd=["shared","exclusive","unlock"];function $c(a,b,c){this.fs=a;this.bus=c;this.configspace_tagname=
700
+ [104,111,115,116,57,112];this.configspace_taglen=this.configspace_tagname.length;this.VERSION="9P2000.L";this.msize=this.BLOCKSIZE=8192;this.replybuffer=new Uint8Array(2*this.msize);this.replybuffersize=0;this.fids=[];this.virtio=new Ic(b,{name:"virtio-9p",pci_id:48,device_id:4169,subsystem_device_id:9,common:{initial_port:43008,queues:[{size_supported:32,notify_offset:0}],features:[0,32,29,28],on_driver_ok:()=>{}},notification:{initial_port:43264,single_handler:!1,handlers:[d=>{if(0!==d)k(!1,"Virtio9P Notified for non-existent queue: "+
701
+ d+" (expected queue_id of 0)");else{for(;this.virtqueue.has_request();)d=this.virtqueue.pop_request(),this.ReceiveRequest(d);this.virtqueue.notify_me_after(0)}}]},isr_status:{initial_port:42752},device_specific:{initial_port:42496,struct:[{bytes:2,name:"mount tag length",read:()=>this.configspace_taglen,write:()=>{}}].concat(Array.from(Array(254).keys()).map(d=>({bytes:1,name:"mount tag name "+d,read:()=>this.configspace_tagname[d]||0,write:()=>{}})))}});this.virtqueue=this.virtio.queues[0]}$c.prototype.get_state=
702
+ function(){var a=[];a[0]=this.configspace_tagname;a[1]=this.configspace_taglen;a[2]=this.virtio;a[3]=this.VERSION;a[4]=this.BLOCKSIZE;a[5]=this.msize;a[6]=this.replybuffer;a[7]=this.replybuffersize;a[8]=this.fids.map(function(b){return[b.inodeid,b.type,b.uid,b.dbg_name]});a[9]=this.fs;return a};$c.prototype.set_state=function(a){this.configspace_tagname=a[0];this.configspace_taglen=a[1];this.virtio.set_state(a[2]);this.virtqueue=this.virtio.queues[0];this.VERSION=a[3];this.BLOCKSIZE=a[4];this.msize=
703
+ a[5];this.replybuffer=a[6];this.replybuffersize=a[7];this.fids=a[8].map(function(b){return{inodeid:b[0],type:b[1],uid:b[2],dbg_name:b[3]}});this.fs.set_state(a[9])};$c.prototype.Createfid=function(a,b,c,d){return{inodeid:a,type:b,uid:c,dbg_name:d}};$c.prototype.update_dbg_name=function(a,b){for(const c of this.fids)c.inodeid===a&&(c.dbg_name=b)};$c.prototype.reset=function(){this.fids=[];this.virtio.reset()};$c.prototype.BuildReply=function(a,b,c){k(0<=c,"9P: Negative payload size");K(["w","b","h"],
704
+ [c+7,a+1,b],this.replybuffer,0);c+7>=this.replybuffer.length&&C("Error in 9p: payloadsize exceeds maximum length",4194304);this.replybuffersize=c+7};$c.prototype.SendError=function(a,b,c){b=K(["w"],[c],this.replybuffer,7);this.BuildReply(6,a,b)};$c.prototype.SendReply=function(a){k(0<=this.replybuffersize,"9P: Negative replybuffersize");a.set_next_blob(this.replybuffer.subarray(0,this.replybuffersize));this.virtqueue.push_reply(a);this.virtqueue.flush_replies()};$c.prototype.ReceiveRequest=async function(a){var b=
705
+ new Uint8Array(a.length_readable);a.get_next_blob(b);var c={offset:0},d=M(["w","b","h"],b,c),e=d[0],f=d[1],h=d[2];switch(f){case 8:e=this.fs.GetTotalSize();b=this.fs.GetSpace();d=[16914839];d[1]=this.BLOCKSIZE;d[2]=Math.floor(b/d[1]);d[3]=d[2]-Math.floor(e/d[1]);d[4]=d[2]-Math.floor(e/d[1]);d[5]=this.fs.CountUsedInodes();d[6]=this.fs.CountFreeInodes();d[7]=0;d[8]=256;e=K("wwddddddw".split(""),d,this.replybuffer,7);this.BuildReply(f,h,e);this.SendReply(a);break;case 112:case 12:d=M(["w","w"],b,c);
706
+ var l=d[0];c=d[1];C("[open] fid="+l+", mode="+c,4194304);b=this.fids[l].inodeid;var p=this.fs.GetInode(b);C("file open "+this.fids[l].dbg_name,4194304);d=this.fs.OpenInode(b,c);this.fs.AddEvent(this.fids[l].inodeid,function(){C("file opened "+this.fids[l].dbg_name+" tag:"+h,4194304);var t=[];t[0]=p.qid;t[1]=this.msize-24;K(["Q","w"],t,this.replybuffer,7);this.BuildReply(f,h,17);this.SendReply(a)}.bind(this));break;case 70:d=M(["w","w","s"],b,c);b=d[0];l=d[1];e=d[2];C("[link] dfid="+b+", name="+e,
707
+ 4194304);d=this.fs.Link(this.fids[b].inodeid,this.fids[l].inodeid,e);if(0>d){e="";-1===d?e="Operation not permitted":(e="Unknown error: "+-d,k(!1,"[link]: Unexpected error code: "+-d));this.SendError(h,e,-d);this.SendReply(a);break}this.BuildReply(f,h,0);this.SendReply(a);break;case 16:d=M(["w","s","s","w"],b,c);l=d[0];e=d[1];b=d[2];d=d[3];C("[symlink] fid="+l+", name="+e+", symgt="+b+", gid="+d,4194304);b=this.fs.CreateSymlink(e,this.fids[l].inodeid,b);p=this.fs.GetInode(b);p.uid=this.fids[l].uid;
708
+ p.gid=d;K(["Q"],[p.qid],this.replybuffer,7);this.BuildReply(f,h,13);this.SendReply(a);break;case 18:d=M("wswwww".split(""),b,c);l=d[0];e=d[1];c=d[2];b=d[3];var n=d[4];d=d[5];C("[mknod] fid="+l+", name="+e+", major="+b+", minor="+n,4194304);b=this.fs.CreateNode(e,this.fids[l].inodeid,b,n);p=this.fs.GetInode(b);p.mode=c;p.uid=this.fids[l].uid;p.gid=d;K(["Q"],[p.qid],this.replybuffer,7);this.BuildReply(f,h,13);this.SendReply(a);break;case 22:d=M(["w"],b,c);l=d[0];p=this.fs.GetInode(this.fids[l].inodeid);
709
+ C("[readlink] fid="+l+" name="+this.fids[l].dbg_name+" target="+p.symlink,4194304);e=K(["s"],[p.symlink],this.replybuffer,7);this.BuildReply(f,h,e);this.SendReply(a);break;case 72:d=M(["w","s","w","w"],b,c);l=d[0];e=d[1];c=d[2];d=d[3];C("[mkdir] fid="+l+", name="+e+", mode="+c+", gid="+d,4194304);b=this.fs.CreateDirectory(e,this.fids[l].inodeid);p=this.fs.GetInode(b);p.mode=c|16384;p.uid=this.fids[l].uid;p.gid=d;K(["Q"],[p.qid],this.replybuffer,7);this.BuildReply(f,h,13);this.SendReply(a);break;case 14:d=
710
+ M(["w","s","w","w","w"],b,c);l=d[0];e=d[1];b=d[2];c=d[3];d=d[4];this.bus.send("9p-create",[e,this.fids[l].inodeid]);C("[create] fid="+l+", name="+e+", flags="+b+", mode="+c+", gid="+d,4194304);b=this.fs.CreateFile(e,this.fids[l].inodeid);this.fids[l].inodeid=b;this.fids[l].type=1;this.fids[l].dbg_name=e;p=this.fs.GetInode(b);p.uid=this.fids[l].uid;p.gid=d;p.mode=c|32768;K(["Q","w"],[p.qid,this.msize-24],this.replybuffer,7);this.BuildReply(f,h,17);this.SendReply(a);break;case 52:d=M("wbwddws".split(""),
711
+ b,c);l=d[0];b=d[2];e=0===d[4]?Infinity:d[4];e=this.fs.DescribeLock(d[1],d[3],e,d[5],d[6]);C("[lock] fid="+l+", type="+bd[e.type]+", start="+e.start+", length="+e.length+", proc_id="+e.proc_id);d=this.fs.Lock(this.fids[l].inodeid,e,b);K(["b"],[d],this.replybuffer,7);this.BuildReply(f,h,1);this.SendReply(a);break;case 54:d=M("wbddws".split(""),b,c);l=d[0];e=0===d[3]?Infinity:d[3];e=this.fs.DescribeLock(d[1],d[2],e,d[4],d[5]);C("[getlock] fid="+l+", type="+bd[e.type]+", start="+e.start+", length="+e.length+
712
+ ", proc_id="+e.proc_id);d=this.fs.GetLock(this.fids[l].inodeid,e);d||(d=e,d.type=2);e=K(["b","d","d","w","s"],[d.type,d.start,Infinity===d.length?0:d.length,d.proc_id,d.client_id],this.replybuffer,7);this.BuildReply(f,h,e);this.SendReply(a);break;case 24:d=M(["w","d"],b,c);l=d[0];p=this.fs.GetInode(this.fids[l].inodeid);C("[getattr]: fid="+l+" name="+this.fids[l].dbg_name+" request mask="+d[1],4194304);if(!p||4===p.status){C("getattr: unlinked",4194304);this.SendError(h,"No such file or directory",
713
+ 2);this.SendReply(a);break}d[0]=d[1];d[1]=p.qid;d[2]=p.mode;d[3]=p.uid;d[4]=p.gid;d[5]=p.nlinks;d[6]=p.major<<8|p.minor;d[7]=p.size;d[8]=this.BLOCKSIZE;d[9]=Math.floor(p.size/512+1);d[10]=p.atime;d[11]=0;d[12]=p.mtime;d[13]=0;d[14]=p.ctime;d[15]=0;d[16]=0;d[17]=0;d[18]=0;d[19]=0;K("dQwwwddddddddddddddd".split(""),d,this.replybuffer,7);this.BuildReply(f,h,153);this.SendReply(a);break;case 26:d=M("wwwwwddddd".split(""),b,c);l=d[0];p=this.fs.GetInode(this.fids[l].inodeid);C("[setattr]: fid="+l+" request mask="+
714
+ d[1]+" name="+this.fids[l].dbg_name,4194304);d[1]&1&&(p.mode=d[2]);d[1]&2&&(p.uid=d[3]);d[1]&4&&(p.gid=d[4]);d[1]&16&&(p.atime=Math.floor((new Date).getTime()/1E3));d[1]&32&&(p.mtime=Math.floor((new Date).getTime()/1E3));d[1]&64&&(p.ctime=Math.floor((new Date).getTime()/1E3));d[1]&128&&(p.atime=d[6]);d[1]&256&&(p.mtime=d[8]);d[1]&8&&await this.fs.ChangeSize(this.fids[l].inodeid,d[5]);this.BuildReply(f,h,0);this.SendReply(a);break;case 50:d=M(["w","d"],b,c);l=d[0];this.BuildReply(f,h,0);this.SendReply(a);
715
+ break;case 40:case 116:d=M(["w","d","w"],b,c);l=d[0];e=d[1];n=d[2];p=this.fs.GetInode(this.fids[l].inodeid);40===f&&C("[treaddir]: fid="+l+" offset="+e+" count="+n,4194304);116===f&&C("[read]: fid="+l+" ("+this.fids[l].dbg_name+") offset="+e+" count="+n+" fidtype="+this.fids[l].type,4194304);if(!p||4===p.status){C("read/treaddir: unlinked",4194304);this.SendError(h,"No such file or directory",2);this.SendReply(a);break}if(2===this.fids[l].type)for(p.caps.length<e+n&&(n=p.caps.length-e),d=0;d<n;d++)this.replybuffer[11+
716
+ d]=p.caps[e+d];else this.fs.OpenInode(this.fids[l].inodeid,void 0),d=this.fids[l].inodeid,n=Math.min(n,this.replybuffer.length-11),p.size<e+n?n=p.size-e:40===f&&(n=this.fs.RoundToDirentry(d,e+n)-e),e>p.size&&(n=0),this.bus.send("9p-read-start",[this.fids[l].dbg_name]),d=await this.fs.Read(d,e,n),this.bus.send("9p-read-end",[this.fids[l].dbg_name,n]),d&&this.replybuffer.set(d,11);K(["w"],[n],this.replybuffer,7);this.BuildReply(f,h,4+n);this.SendReply(a);break;case 118:d=M(["w","d","w"],b,c);l=d[0];
717
+ e=d[1];n=d[2];d=this.fids[l].dbg_name;C("[write]: fid="+l+" ("+d+") offset="+e+" count="+n+" fidtype="+this.fids[l].type,4194304);if(2===this.fids[l].type){this.SendError(h,"Setxattr not supported",95);this.SendReply(a);break}else await this.fs.Write(this.fids[l].inodeid,e,n,b.subarray(c.offset));this.bus.send("9p-write-end",[d,n]);K(["w"],[n],this.replybuffer,7);this.BuildReply(f,h,4);this.SendReply(a);break;case 74:d=M(["w","s","w","s"],b,c);e=d[0];b=d[1];c=d[2];d=d[3];C("[renameat]: oldname="+
718
+ b+" newname="+d,4194304);d=await this.fs.Rename(this.fids[e].inodeid,b,this.fids[c].inodeid,d);if(0>d){e="";-2===d?e="No such file or directory":-1===d?e="Operation not permitted":-39===d?e="Directory not empty":(e="Unknown error: "+-d,k(!1,"[renameat]: Unexpected error code: "+-d));this.SendError(h,e,-d);this.SendReply(a);break}this.BuildReply(f,h,0);this.SendReply(a);break;case 76:d=M(["w","s","w"],b,c);c=d[0];e=d[1];b=d[2];C("[unlink]: dirfd="+c+" name="+e+" flags="+b,4194304);l=this.fs.Search(this.fids[c].inodeid,
719
+ e);if(-1===l){this.SendError(h,"No such file or directory",2);this.SendReply(a);break}d=this.fs.Unlink(this.fids[c].inodeid,e);if(0>d){e="";-39===d?e="Directory not empty":-1===d?e="Operation not permitted":(e="Unknown error: "+-d,k(!1,"[unlink]: Unexpected error code: "+-d));this.SendError(h,e,-d);this.SendReply(a);break}this.BuildReply(f,h,0);this.SendReply(a);break;case 100:d=M(["w","s"],b,c);C("[version]: msize="+d[0]+" version="+d[1],4194304);this.msize!==d[0]&&(this.msize=d[0],this.replybuffer=
720
+ new Uint8Array(Math.min(16777216,2*this.msize)));e=K(["w","s"],[this.msize,this.VERSION],this.replybuffer,7);this.BuildReply(f,h,e);this.SendReply(a);break;case 104:d=M(["w","w","s","s","w"],b,c);l=d[0];e=d[4];C("[attach]: fid="+l+" afid="+m(d[1])+" uname="+d[2]+" aname="+d[3],4194304);this.fids[l]=this.Createfid(0,1,e,"");p=this.fs.GetInode(this.fids[l].inodeid);K(["Q"],[p.qid],this.replybuffer,7);this.BuildReply(f,h,13);this.SendReply(a);this.bus.send("9p-attach");break;case 108:d=M(["h"],b,c);
721
+ C("[flush] "+h,4194304);this.BuildReply(f,h,0);this.SendReply(a);break;case 110:d=M(["w","w","h"],b,c);l=d[0];n=d[1];var q=d[2];C("[walk]: fid="+d[0]+" nwfid="+d[1]+" nwname="+q,4194304);if(0===q){this.fids[n]=this.Createfid(this.fids[l].inodeid,1,this.fids[l].uid,this.fids[l].dbg_name);K(["h"],[0],this.replybuffer,7);this.BuildReply(f,h,2);this.SendReply(a);break}e=[];for(d=0;d<q;d++)e.push("s");c=M(e,b,c);b=this.fids[l].inodeid;e=9;var r=0;C("walk in dir "+this.fids[l].dbg_name+" to: "+c.toString(),
722
+ 4194304);for(d=0;d<q;d++){b=this.fs.Search(b,c[d]);if(-1===b){C("Could not find: "+c[d],4194304);break}e+=K(["Q"],[this.fs.GetInode(b).qid],this.replybuffer,e);r++;this.fids[n]=this.Createfid(b,1,this.fids[l].uid,c[d])}K(["h"],[r],this.replybuffer,7);this.BuildReply(f,h,e-7);this.SendReply(a);break;case 120:d=M(["w"],b,c);C("[clunk]: fid="+d[0],4194304);this.fids[d[0]]&&0<=this.fids[d[0]].inodeid&&(await this.fs.CloseInode(this.fids[d[0]].inodeid),this.fids[d[0]].inodeid=-1,this.fids[d[0]].type=-1);
723
+ this.BuildReply(f,h,0);this.SendReply(a);break;case 32:d=M(["w","s","d","w"],b,c);l=d[0];e=d[1];c=d[2];b=d[3];C("[txattrcreate]: fid="+l+" name="+e+" attr_size="+c+" flags="+b,4194304);this.fids[l].type=2;this.BuildReply(f,h,0);this.SendReply(a);break;case 30:d=M(["w","w","s"],b,c);l=d[0];e=d[2];C("[xattrwalk]: fid="+d[0]+" newfid="+d[1]+" name="+d[2],4194304);this.SendError(h,"Setxattr not supported",95);this.SendReply(a);break;default:C("Error in Virtio9p: Unknown id "+f+" received",4194304),k(!1)}};
724
+ export{};; export default module.exports.V86; export let {V86, CPU} = module.exports;