v86 0.5.12 → 0.5.13

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,7 +1,7 @@
1
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
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
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,ma=-2;256>ka;ka++)ka&ka-1||ma++,ia[ka]=ma;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));
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
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
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
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)}
@@ -10,21 +10,21 @@ function ua(a){"number"===typeof a?this.view=new Uint8Array(a+7>>3):a instanceof
10
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
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
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 la=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+=la);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=
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
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
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
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
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),la=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[la],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,
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
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 la=0,ja=0;la<G;++la)if(C[la])ja+=x;else for(var H=0;H<w;++H,ja+=4)if(B[ja+1]&1){C[la]=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 la=0,ja=0,aa=0;la<G;++la,ja+=U){if(!C[la]){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&
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
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
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
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
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
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 la=B[x+1]&1;var ja=B[x+2];var aa=B[x+3];la&&V.classList.add("blink");V.style.backgroundColor=c(ja);V.style.color=c(aa);for(H="";S<w&&(B[x+1]&1)===la&&B[x+2]===ja&&B[x+3]===aa;){var ta=B[x+0];H+=kb[ta];m(kb[ta]);
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
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
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
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,
@@ -216,67 +216,67 @@ this.virtio.queues[c];d.has_request();){var e=d.pop_request(),f=new Uint8Array(e
216
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
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
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.use_mouse=this.enable_mouse_stream=!1;this.have_mouse=!0;this.mouse_clicks=this.mouse_delta_y=this.mouse_delta_x=
220
- 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=!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=
221
- !1;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,Math.max(-8,this.wheel_movement));this.send_mouse_packet(0,0)},this);this.command_register=5;this.controller_output_port=
222
- 0;this.read_controller_output_port=this.read_command_register=this.read_output_register=!1;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)}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;a[7]=this.enable_keyboard_stream;
223
- 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;a[27]=this.mouse_reset_workaround;
224
- 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=a[16];this.resolution=a[17];this.scaling2=
225
- 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||(this.kbd_buffer.length?this.kbd_irq():
226
- 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&&(D("adding kbd code: "+
227
- 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=a|c<<1|b<<2,this.enable_mouse_stream&&
228
- 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=a>>31;switch(Math.abs(a)){case 0:case 1:case 3:return a;
229
- 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=this.kbd_buffer.shift(),D("Port 60 read (kbd) : "+
230
- 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 = "+z(this.command_register),64);else if(this.read_output_register)this.read_output_register=
231
- !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=1);break;case 1:this.mouse_detect_state=100===a?2:200===a?3:0;break;case 2:80===
232
- 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",64)):(this.resolution=1<<a,D("resolution: "+
233
- 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: "+z(a),64),this.have_mouse){this.kbd_buffer.clear();
234
- 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=this.mouse_delta_y=0;this.raise_irq();
235
- 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",!0);this.enable_mouse_stream=!1;this.sample_rate=
236
- 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=!0;break;case 240:this.next_handle_scan_code_set=
237
- !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()}};yb.prototype.port64_write=function(a){D("port 64 write: "+
238
- 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&=-33;break;case 169:this.kbd_buffer.clear();this.mouse_buffer.clear();
239
- 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();break;default:D("port 64: Unimplemented command byte: "+
240
- 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}]);console.assert(52===Db.reduce((a,b)=>a+b.size,
241
- 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,type:b,size:b.size,get:b.get,set:b.set}})}
242
- 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=0;this.periodic_interrupt=!1;this.periodic_interrupt_time=
243
- .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]=this.periodic_interrupt;a[7]=this.periodic_interrupt_time;
244
- 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();this.rtc_time+=a-this.last_update;this.last_update=a;this.periodic_interrupt&&
245
- 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&&(b=Math.min(b,Math.max(0,this.next_interrupt_alarm-a)));
246
- 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: "+z(this.encode_time((new Date(this.rtc_time)).getUTCSeconds())),
247
- 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()+1)),65536),this.encode_time((new Date(this.rtc_time)).getUTCDay()+
248
- 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()%100);case 10:return 999<=
249
- 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=function(a){switch(this.cmos_index){case 10:this.cmos_a=
250
- 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(),d,c,b));D("RTC alarm scheduled for "+e+" hh:mm:ss="+
251
- 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=function(a,b){D("cmos "+z(a)+" <- "+z(b),
252
- 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;b?this.set_fda(b):(this.eject_fda(),this.cpu.devices.rtc.cmos_write(16,
253
- 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)}Q.prototype.eject_fda=function(){this.fda_image=null;this.number_of_cylinders=
254
- 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},[1228800]:{type:2,tracks:80,sectors:15,heads:2},[1474560]:{type:4,tracks:80,sectors:18,
255
- 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=d.sectors;this.number_of_heads=d.heads;this.number_of_cylinders=d.tracks;this.fda_image=
256
- 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]=this.dor;a[16]=this.sectors_per_track;a[17]=this.number_of_heads;a[18]=this.number_of_cylinders;
257
- 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=a[16];this.number_of_heads=a[17];this.number_of_cylinders=a[18]};Q.prototype.port3F0_read=
258
- 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),this.cpu.device_lower_irq(6),this.response_data[this.response_index++];D("3F5 read, empty",
259
- 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+=z(this.receiving_command[b])+" ";D(a,8192);this.next_command.call(this,
260
- 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=
261
- 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,"Unimplemented floppy command call "+z(a))}this.receiving_index=
262
- 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=function(){D("check drive status",8192);this.status_reg1=this.fda_image?
263
- 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=function(){D("floppy check interrupt status",8192);
264
- 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?(this.status_reg1=0,a?this.dma.do_write(this.fda_image,
265
- 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]=c<<2|32,this.response_data[1]=0,this.response_data[2]=
266
- 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]=0;this.response_data[1]=0;this.response_data[2]=
267
- 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 "+e+" ignored",32768);this.ata_port_high=
268
- 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,
269
- 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)},function(){return this.current_interface.read_data(2)},
270
- 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|3,this,function(){D("Read sector: "+z(this.current_interface.sector&
271
- 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",32768);return this.current_interface.drive_head&255});
272
- 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,this,function(h){D("1F2/bytecount: "+z(h),32768);this.master.bytecount=
273
- (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=(this.slave.cylinder_low<<8|h)&65535});a.io.register_write(this.ata_port|
274
- 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=this.slave.is_lba=h>>6&1;this.master.head=this.slave.head=h&15});this.dma_command=
275
- 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);a.io.register_write(this.master_port,this,this.dma_write_command8,void 0,
276
- 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=this.current_interface.status;D("ATA read status: "+
277
- 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: "+z(a,8),32768);this.prdt_addr=
278
- 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: "+z(a),
279
- 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();
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
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
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
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,
@@ -432,64 +432,64 @@ this.devices.net&&this.devices.net.set_state(a[59]);this.set_state_pic(a[60]);th
432
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
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
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.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));m(0===(a&131071));console.assert(0===this.memory_size[0],
436
- "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;this.bios.main=a.bios;this.bios.vga=a.vga_bios;this.load_bios();
437
- 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,this,void 0,function(e){function f(n){return new Uint8Array(Int32Array.of(n).buffer)}
438
- 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+64*this.option_roms.length);const n=new Uint8Array(e.buffer);e[0]=l(this.option_roms.length);
439
- 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)),this.fw_value=f(0))});c.register_write(128,this,function(){});c.register_read(128,this,function(){return 255});
440
- 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,this,this.port4D0_read);c.register_read(1233,this,this.port4D1_read);c.register_write(1232,this,
441
- 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),this.devices.ps2=new yb(this,b),this.devices.uart0=new lb(this,1016,b),a.uart1&&(this.devices.uart1=
442
- 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,a.mac_address_translation):"virtio"===a.net_device.type&&(this.devices.virtio_net=new Tb(this,
443
- 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",2),this.option_roms.push(a)):(D("loaded multiboot without bios",2),this.reg32[0]=this.io.port_read32(244)));
444
- 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);(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===
445
- 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,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>
446
- 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];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="+
447
- 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");console.assert(1===w.class,"Unimplemented: 64 bit elf");console.assert(1===w.data,"Unimplemented: big endian");
448
- 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]=Qb(new DataView(v.buffer,v.byteOffset+w.shoff,w.shentsize*w.shnum),Ob,w.shnum);if(ca){console.log("%d program headers:",
449
- 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",C.name.toString(16),C.type.toString(16),C.flags.toString(16),C.addr.toString(16),
450
- 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+u.memsz>r&&(r=r-u.vaddr+u.paddr)):D("Warning: Skipped loading section, paddr="+
451
- 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 "+u),B=u+b.byteLength,h.write32(q,u),h.write32(q+4,B),h.write32(q+
452
- 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});this.io.register_write_consecutive(244,this,function(q){console.log("Test exited with code "+
453
- 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];n[l]=-p;return{name:"genroms/multiboot.bin",data:n}}D("Multiboot header not found",2)};K.prototype.fill_cmos=
454
- 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,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,
455
- 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|0;return e<d.length?d[e]:0},function(){m(!1,"Unexpected write to VGA rom")})}else D("Warning: No VGA BIOS");
456
- 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);WebAssembly.instantiate(f,{e:this.jit_imports}).then(h=>{this.wm.wasm_table.set(a+1024,
457
- 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),f=this.devices.apic.timer(b));return Math.min(c,d,e,f)};K.prototype.device_raise_irq=
458
- 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,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>>
459
- 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"),m(128>c,"VirtIO device<"+this.name+"> feature bit numbers assumed less than 128 in implementation"),
460
- 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];this.isr_status=0;c=new Set;for(var e of this.queues.map(f=>f.notify_offset))d=b.notification.single_handler?
461
- 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));b.device_specific&&e.push(this.create_device_specific_capability(b.device_specific));this.init_capabilities(e);
462
- 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]||0,write:()=>{}},{bytes:4,name:"driver_feature_select",read:()=>this.driver_feature_select,write:b=>{this.driver_feature_select=
463
- 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.",2097152);return 65535},write:()=>{D("No msi-x capability supported.",2097152)}},{bytes:2,name:"num_queues",read:()=>
464
- 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);b&~this.device_status&4&&this.device_status&64&&this.notify_config_changes();this.features_ok||(b&8&&D("Removing FEATURES_OK",
465
- 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:()=>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.",
466
- 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.",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:
467
- 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:()=>{}},{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=
468
- 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)",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?
469
- 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,"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,
470
- 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",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<"+
471
- 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");m(0<=e.bar&&6>e.bar,"VirtIO device<"+this.name+"> capability invalid bar number");var d=e.struct.reduce((f,h)=>f+
472
- 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&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>>>
473
- 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=f.read;a=f.write;h=()=>{const l=f.read();D("Device<"+this.name+"> cap["+e.type+"] read["+f.name+"] => "+z(l,8*
474
- 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",2097152);return h(n&-4)>>((n&3)<<3)&255};switch(f.bytes){case 4:this.cpu.io.register_read(c,this,l,void 0,
475
- 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,"VirtIO device <"+this.name+"> invalid capability field width of "+f.bytes+" bytes")}}c+=f.bytes}}m(256>=
476
- 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]=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+
477
- 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=a[0];this.driver_feature_select=a[1];this.device_feature=a[2];this.driver_feature=
478
- 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=!0;this.queue_select=this.device_status=0;this.queue_selected=this.queues[0];
479
- 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&=255,this.config_has_changed=!1)};xb.prototype.is_feature_negotiated=function(a){return 0<
480
- (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;this.pci.lower_irq(this.pci_id)};function X(a,b,c){this.cpu=a;this.virtio=b;this.size_supported=
481
- 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;a[8]=this.num_staged_replies;a[9]=1;return a};X.prototype.set_state=function(a){this.size=
482
- 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=function(){return this.desc_addr&&this.avail_addr&&this.used_addr};
483
- 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&&(this.fix_wrapping=!1,this.avail_last_idx=(this.avail_get_idx()&
484
- ~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);D("Pop request: avail_last_idx="+this.avail_last_idx+
485
- " 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);this.num_staged_replies++};X.prototype.flush_replies=
486
- 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)}};X.prototype.notify_me_after=function(a){m(0<=
487
- 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+2)};X.prototype.avail_get_entry=
488
- 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+2,a)};X.prototype.used_set_entry=
489
- 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=a.desc_addr,d=0,e=a.size,f=!1;const h=
490
- 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),this.length_writable+=l.len;
491
- 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",2097152);
492
- 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];
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
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
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
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===