Update v86 to 5d02960

This commit is contained in:
Felix Rieseberg
2022-10-04 10:09:26 +02:00
parent ef57e3a7fe
commit 19a1bbc002
3 changed files with 211 additions and 200 deletions

Binary file not shown.

View File

@@ -1,4 +1,4 @@
;(function(){var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.SIMPLE_FROUND_POLYFILL=!1;$jscomp.ISOLATE_POLYFILLS=!1;$jscomp.FORCE_POLYFILL_PROMISE=!1;$jscomp.FORCE_POLYFILL_PROMISE_WHEN_NO_UNHANDLED_REJECTION=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,b,c){if(a==Array.prototype||a==Object.prototype)return a;a[b]=c.value;return a};
;(function(){'use strict';var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.SIMPLE_FROUND_POLYFILL=!1;$jscomp.ISOLATE_POLYFILLS=!1;$jscomp.FORCE_POLYFILL_PROMISE=!1;$jscomp.FORCE_POLYFILL_PROMISE_WHEN_NO_UNHANDLED_REJECTION=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,b,c){if(a==Array.prototype||a==Object.prototype)return a;a[b]=c.value;return a};
$jscomp.getGlobal=function(a){a=["object"==typeof globalThis&&globalThis,a,"object"==typeof window&&window,"object"==typeof self&&self,"object"==typeof global&&global];for(var b=0;b<a.length;++b){var c=a[b];if(c&&c.Math==Math)return c}throw Error("Cannot find global object");};$jscomp.global=$jscomp.getGlobal(this);$jscomp.IS_SYMBOL_NATIVE="function"===typeof Symbol&&"symbol"===typeof Symbol("x");$jscomp.TRUST_ES6_POLYFILLS=!$jscomp.ISOLATE_POLYFILLS||$jscomp.IS_SYMBOL_NATIVE;$jscomp.polyfills={};
$jscomp.propertyToPolyfillSymbol={};$jscomp.POLYFILL_PREFIX="$jscp$";var $jscomp$lookupPolyfilledValue=function(a,b){var c=$jscomp.propertyToPolyfillSymbol[b];if(null==c)return a[b];c=a[c];return void 0!==c?c:a[b]};$jscomp.polyfill=function(a,b,c,d){b&&($jscomp.ISOLATE_POLYFILLS?$jscomp.polyfillIsolated(a,b,c,d):$jscomp.polyfillUnisolated(a,b,c,d))};
$jscomp.polyfillUnisolated=function(a,b,c,d){c=$jscomp.global;a=a.split(".");for(d=0;d<a.length-1;d++){var e=a[d];if(!(e in c))return;c=c[e]}a=a[a.length-1];d=c[a];b=b(d);b!=d&&null!=b&&$jscomp.defineProperty(c,a,{configurable:!0,writable:!0,value:b})};
@@ -7,16 +7,17 @@ $jscomp.global.Symbol(e):$jscomp.POLYFILL_PREFIX+c+"$"+e),$jscomp.defineProperty
var LOG_ALL=-1,LOG_NONE=0,LOG_OTHER=1,LOG_CPU=2,LOG_FPU=4,LOG_MEM=8,LOG_DMA=16,LOG_IO=32,LOG_PS2=64,LOG_PIC=128,LOG_VGA=256,LOG_PIT=512,LOG_MOUSE=1024,LOG_PCI=2048,LOG_BIOS=4096,LOG_FLOPPY=8192,LOG_SERIAL=16384,LOG_DISK=32768,LOG_RTC=65536,LOG_HPET=131072,LOG_ACPI=262144,LOG_APIC=524288,LOG_NET=1048576,LOG_VIRTIO=2097152,LOG_9P=4194304,LOG_SB16=8388608,LOG_NAMES=[[1,""],[LOG_CPU,"CPU"],[LOG_DISK,"DISK"],[LOG_FPU,"FPU"],[LOG_MEM,"MEM"],[LOG_DMA,"DMA"],[LOG_IO,"IO"],[LOG_PS2,"PS2"],[LOG_PIC,"PIC"],
[LOG_VGA,"VGA"],[LOG_PIT,"PIT"],[LOG_MOUSE,"MOUS"],[LOG_PCI,"PCI"],[LOG_BIOS,"BIOS"],[LOG_FLOPPY,"FLOP"],[LOG_SERIAL,"SERI"],[LOG_RTC,"RTC"],[LOG_HPET,"HPET"],[LOG_ACPI,"ACPI"],[LOG_APIC,"APIC"],[LOG_NET,"NET"],[LOG_VIRTIO,"VIO"],[LOG_9P,"9P"],[LOG_SB16,"SB16"]],FLAG_CARRY=1,FLAG_PARITY=4,FLAG_ADJUST=16,FLAG_ZERO=64,FLAG_SIGN=128,FLAG_TRAP=256,FLAG_INTERRUPT=512,FLAG_DIRECTION=1024,FLAG_OVERFLOW=2048,FLAG_IOPL=12288,FLAG_NT=16384,FLAG_RF=65536,FLAG_VM=131072,FLAG_AC=262144,FLAG_VIF=524288,FLAG_VIP=
1048576,FLAG_ID=2097152,FLAGS_DEFAULT=2,REG_EAX=0,REG_ECX=1,REG_EDX=2,REG_EBX=3,REG_ESP=4,REG_EBP=5,REG_ESI=6,REG_EDI=7,REG_ES=0,REG_CS=1,REG_SS=2,REG_DS=3,REG_FS=4,REG_GS=5,REG_LDTR=7,MMAP_BLOCK_BITS=17,MMAP_BLOCK_SIZE=1<<MMAP_BLOCK_BITS,CR0_PG=-2147483648,CR4_PAE=32,FW_CFG_SIGNATURE=0,FW_CFG_ID=1,FW_CFG_RAM_SIZE=3,FW_CFG_NB_CPUS=5,FW_CFG_MAX_CPUS=15,FW_CFG_NUMA=13,FW_CFG_FILE_DIR=25,FW_CFG_CUSTOM_START=32768,FW_CFG_FILE_START=49152,FW_CFG_SIGNATURE_QEMU=1431127377,WASM_TABLE_SIZE=900,WASM_TABLE_OFFSET=
1024,MIXER_CHANNEL_LEFT=0,MIXER_CHANNEL_RIGHT=1,MIXER_CHANNEL_BOTH=2,MIXER_SRC_MASTER=0,MIXER_SRC_PCSPEAKER=1,MIXER_SRC_DAC=2;function ScreenAdapter(a,b){function c(w){w=w.toString(16);return"#"+"0".repeat(6-w.length)+w}function d(w,y,C,D){w.style.width="";w.style.height="";D&&(w.style.transform="");var E=w.getBoundingClientRect();D?w.style.transform=(1===y?"":" scaleX("+y+")")+(1===C?"":" scaleY("+C+")"):(0===y%1&&0===C%1?(e.style.imageRendering="crisp-edges",e.style.imageRendering="pixelated",e.style["-ms-interpolation-mode"]="nearest-neighbor"):(e.style.imageRendering="",e.style["-ms-interpolation-mode"]=""),D=window.devicePixelRatio||
1,0!==D%1&&(y/=D,C/=D));1!==y&&(w.style.width=E.width*y+"px");1!==C&&(w.style.height=E.height*C+"px")}console.assert(a,"1st argument must be a DOM container");var e=a.getElementsByTagName("canvas")[0],g=e.getContext("2d",{alpha:!1}),f=a.getElementsByTagName("div")[0],k=document.createElement("div"),m,l,n=1,p=1,t=1,r,q=!1,v,u,x,z=!1,A=this;a=new Uint16Array([199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,201,230,198,244,246,242,251,249,255,214,220,162,163,165,8359,402,225,237,243,
250,241,209,170,186,191,8976,172,189,188,161,171,187,9617,9618,9619,9474,9508,9569,9570,9558,9557,9571,9553,9559,9565,9564,9563,9488,9492,9524,9516,9500,9472,9532,9566,9567,9562,9556,9577,9574,9568,9552,9580,9575,9576,9572,9573,9561,9560,9554,9555,9579,9578,9496,9484,9608,9604,9612,9616,9600,945,223,915,960,931,963,181,964,934,920,937,948,8734,966,949,8745,8801,177,8805,8804,8992,8993,247,8776,176,8729,183,8730,8319,178,9632,160]);for(var B=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]),I=[],J,F=0;256>F;F++)J=127<F?a[F-128]:32>F?B[F]:F,I[F]=String.fromCharCode(J);g.imageSmoothingEnabled=!1;k.style.position="absolute";k.style.backgroundColor="#ccc";k.style.width="7px";k.style.display="inline-block";f.style.display="block";e.style.display="none";this.bus=b;b.register("screen-set-mode",function(w){this.set_mode(w)},this);b.register("screen-fill-buffer-end",function(w){this.update_buffer(w)},
1024,MIXER_CHANNEL_LEFT=0,MIXER_CHANNEL_RIGHT=1,MIXER_CHANNEL_BOTH=2,MIXER_SRC_MASTER=0,MIXER_SRC_PCSPEAKER=1,MIXER_SRC_DAC=2;function ScreenAdapter(a,b){function c(w){w=w.toString(16);return"#"+"0".repeat(6-w.length)+w}function d(w,y,B,A){w.style.width="";w.style.height="";A&&(w.style.transform="");var E=w.getBoundingClientRect();A?w.style.transform=(1===y?"":" scaleX("+y+")")+(1===B?"":" scaleY("+B+")"):(0===y%1&&0===B%1?(e.style.imageRendering="crisp-edges",e.style.imageRendering="pixelated",e.style["-ms-interpolation-mode"]="nearest-neighbor"):(e.style.imageRendering="",e.style["-ms-interpolation-mode"]=""),A=window.devicePixelRatio||
1,0!==A%1&&(y/=A,B/=A));1!==y&&(w.style.width=E.width*y+"px");1!==B&&(w.style.height=E.height*B+"px")}console.assert(a,"1st argument must be a DOM container");var e=a.getElementsByTagName("canvas")[0],g=e.getContext("2d",{alpha:!1}),f=a.getElementsByTagName("div")[0],k=document.createElement("div"),m,l,n=1,q=1,t=1,r,p=!1,v,u,x,z=!1,C=this;a=new Uint16Array([199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,201,230,198,244,246,242,251,249,255,214,220,162,163,165,8359,402,225,237,243,
250,241,209,170,186,191,8976,172,189,188,161,171,187,9617,9618,9619,9474,9508,9569,9570,9558,9557,9571,9553,9559,9565,9564,9563,9488,9492,9524,9516,9500,9472,9532,9566,9567,9562,9556,9577,9574,9568,9552,9580,9575,9576,9572,9573,9561,9560,9554,9555,9579,9578,9496,9484,9608,9604,9612,9616,9600,945,223,915,960,931,963,181,964,934,920,937,948,8734,966,949,8745,8801,177,8805,8804,8992,8993,247,8776,176,8729,183,8730,8319,178,9632,160]);for(var D=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]),I=[],J,G=0;256>G;G++)J=127<G?a[G-128]:32>G?D[G]:G,I[G]=String.fromCharCode(J);g.imageSmoothingEnabled=!1;k.style.position="absolute";k.style.backgroundColor="#ccc";k.style.width="7px";k.style.display="inline-block";f.style.display="block";e.style.display="none";this.bus=b;b.register("screen-set-mode",function(w){this.set_mode(w)},this);b.register("screen-fill-buffer-end",function(w){this.update_buffer(w)},
this);b.register("screen-put-char",function(w){this.put_char(w[0],w[1],w[2],w[3],w[4])},this);b.register("screen-update-cursor",function(w){this.update_cursor(w[0],w[1])},this);b.register("screen-update-cursor-scanline",function(w){this.update_cursor_scanline(w[0],w[1])},this);b.register("screen-clear",function(){this.clear_screen()},this);b.register("screen-set-size-text",function(w){this.set_size_text(w[0],w[1])},this);b.register("screen-set-size-graphical",function(w){this.set_size_graphical(w[0],
w[1],w[2],w[3])},this);this.init=function(){this.set_size_text(80,25);this.timer()};this.make_screenshot=function(){try{const w=new Image;w.src=e.toDataURL("image/png");window.open("").document.write(w.outerHTML)}catch(w){}};this.put_char=function(w,y,C,D,E){w<x&&y<u&&(y=3*(w*u+y),dbg_assert(0<=C&&256>C),v[y]=C,v[y+1]=D,v[y+2]=E,r[w]=1)};this.timer=function(){z||requestAnimationFrame(q?N:O)};var O=function(){for(var w=0;w<x;w++)r[w]&&(A.text_update_row(w),r[w]=0);this.timer()}.bind(this),N=function(){this.bus.send("screen-fill-buffer");
this.timer()}.bind(this);this.destroy=function(){z=!0};this.set_mode=function(w){(q=w)?(f.style.display="none",e.style.display="block"):(f.style.display="block",e.style.display="none")};this.clear_screen=function(){g.fillStyle="#000";g.fillRect(0,0,e.width,e.height)};this.set_size_text=function(w,y){if(w!==u||y!==x){r=new Int8Array(y);v=new Int32Array(w*y*3);u=w;for(x=y;f.childNodes.length>y;)f.removeChild(f.firstChild);for(;f.childNodes.length<y;)f.appendChild(document.createElement("div"));for(w=
0;w<y;w++)this.text_update_row(w);d(f,n,p,!0)}};this.set_size_graphical=function(w,y,C,D){DEBUG_SCREEN_LAYERS&&(w=C,y=D);e.style.display="block";e.width=w;e.height=y;t=640>=w&&2*w<window.innerWidth&&2*w<window.innerHeight?2:1;d(e,n*t,p*t,!1)};this.set_scale=function(w,y){n=w;p=y;d(f,n,p,!0);d(e,n*t,p*t,!1)};this.set_scale(n,p);this.update_cursor_scanline=function(w,y){w&32?k.style.display="none":(k.style.display="inline",k.style.height=Math.min(15,y-w)+"px",k.style.marginTop=Math.min(15,w)+"px")};
this.update_cursor=function(w,y){if(w!==m||y!==l)r[w]=1,r[m]=1,m=w,l=y};this.text_update_row=function(w){var y=3*w*u,C;var D=f.childNodes[w];var E=document.createElement("div");for(var G=0;G<u;){var H=document.createElement("span");var K=v[y+1];var L=v[y+2];H.style.backgroundColor=c(K);H.style.color=c(L);for(C="";G<u&&v[y+1]===K&&v[y+2]===L;){var M=v[y];C+=I[M];dbg_assert(I[M]);G++;y+=3;if(w===m)if(G===l)break;else if(G===l+1){E.appendChild(k);break}}H.textContent=C;E.appendChild(H)}D.parentNode.replaceChild(E,
D)};this.update_buffer=function(w){DEBUG_SCREEN_LAYERS?(g.strokeStyle="#0F0",g.lineWidth=4,w.forEach(y=>{g.strokeRect(y.buffer_x,y.buffer_y,y.buffer_width,y.buffer_height)}),g.lineWidth=1):w.forEach(y=>{g.putImageData(y.image_data,y.screen_x-y.buffer_x,y.screen_y-y.buffer_y,y.buffer_x,y.buffer_y,y.buffer_width,y.buffer_height)})};this.init()};const VIRTIO_9P_F_MOUNT_TAG=0,VIRTIO_9P_MAX_TAGLEN=254;
w[1],w[2],w[3])},this);this.init=function(){this.set_size_text(80,25);this.timer()};this.make_screenshot=function(){const w=new Image;if(p)w.src=e.toDataURL("image/png");else{const y=[9,16],B=document.createElement("canvas");B.width=u*y[0];B.height=x*y[1];const A=B.getContext("2d");A.imageSmoothingEnabled=!1;A.font=window.getComputedStyle(f).font;A.textBaseline="top";for(let E=0;E<u;E++)for(let F=0;F<x;F++){const H=3*(F*u+E);A.fillStyle=c(v[H+1]);A.fillRect(E*y[0],F*y[1],y[0],y[1]);A.fillStyle=c(v[H+
2]);A.fillText(I[v[H]],E*y[0],F*y[1])}"none"!==k.style.display&&(A.fillStyle=k.style.backgroundColor,A.fillRect(l*y[0],m*y[1]+parseInt(k.style.marginTop,10)-1,parseInt(k.style.width,10),parseInt(k.style.height,10)));w.src=B.toDataURL("image/png")}try{window.open("").document.write(w.outerHTML)}catch(y){}};this.put_char=function(w,y,B,A,E){w<x&&y<u&&(y=3*(w*u+y),dbg_assert(0<=B&&256>B),v[y]=B,v[y+1]=A,v[y+2]=E,r[w]=1)};this.timer=function(){z||requestAnimationFrame(p?N:O)};var O=function(){for(var w=
0;w<x;w++)r[w]&&(C.text_update_row(w),r[w]=0);this.timer()}.bind(this),N=function(){this.bus.send("screen-fill-buffer");this.timer()}.bind(this);this.destroy=function(){z=!0};this.set_mode=function(w){(p=w)?(f.style.display="none",e.style.display="block"):(f.style.display="block",e.style.display="none")};this.clear_screen=function(){g.fillStyle="#000";g.fillRect(0,0,e.width,e.height)};this.set_size_text=function(w,y){if(w!==u||y!==x){r=new Int8Array(y);v=new Int32Array(w*y*3);u=w;for(x=y;f.childNodes.length>
y;)f.removeChild(f.firstChild);for(;f.childNodes.length<y;)f.appendChild(document.createElement("div"));for(w=0;w<y;w++)this.text_update_row(w);d(f,n,q,!0)}};this.set_size_graphical=function(w,y,B,A){DEBUG_SCREEN_LAYERS&&(w=B,y=A);e.style.display="block";e.width=w;e.height=y;t=640>=w&&2*w<window.innerWidth&&2*w<window.innerHeight?2:1;d(e,n*t,q*t,!1)};this.set_scale=function(w,y){n=w;q=y;d(f,n,q,!0);d(e,n*t,q*t,!1)};this.set_scale(n,q);this.update_cursor_scanline=function(w,y){w&32?k.style.display=
"none":(k.style.display="inline",k.style.height=Math.min(15,y-w)+"px",k.style.marginTop=Math.min(15,w)+"px")};this.update_cursor=function(w,y){if(w!==m||y!==l)r[w]=1,r[m]=1,m=w,l=y};this.text_update_row=function(w){var y=3*w*u,B;var A=f.childNodes[w];var E=document.createElement("div");for(var F=0;F<u;){var H=document.createElement("span");var K=v[y+1];var L=v[y+2];H.style.backgroundColor=c(K);H.style.color=c(L);for(B="";F<u&&v[y+1]===K&&v[y+2]===L;){var M=v[y];B+=I[M];dbg_assert(I[M]);F++;y+=3;if(w===
m)if(F===l)break;else if(F===l+1){E.appendChild(k);break}}H.textContent=B;E.appendChild(H)}A.parentNode.replaceChild(E,A)};this.update_buffer=function(w){DEBUG_SCREEN_LAYERS?(g.strokeStyle="#0F0",g.lineWidth=4,w.forEach(y=>{g.strokeRect(y.buffer_x,y.buffer_y,y.buffer_width,y.buffer_height)}),g.lineWidth=1):w.forEach(y=>{g.putImageData(y.image_data,y.screen_x-y.buffer_x,y.screen_y-y.buffer_y,y.buffer_x,y.buffer_y,y.buffer_width,y.buffer_height)})};this.init()};const VIRTIO_9P_F_MOUNT_TAG=0,VIRTIO_9P_MAX_TAGLEN=254;
var EPERM=1,ENOENT=2,EEXIST=17,EINVAL=22,EOPNOTSUPP=95,ENOTEMPTY=39,EPROTO=71,P9_SETATTR_MODE=1,P9_SETATTR_UID=2,P9_SETATTR_GID=4,P9_SETATTR_SIZE=8,P9_SETATTR_ATIME=16,P9_SETATTR_MTIME=32,P9_SETATTR_CTIME=64,P9_SETATTR_ATIME_SET=128,P9_SETATTR_MTIME_SET=256,P9_STAT_MODE_DIR=2147483648,P9_STAT_MODE_APPEND=1073741824,P9_STAT_MODE_EXCL=536870912,P9_STAT_MODE_MOUNT=268435456,P9_STAT_MODE_AUTH=134217728,P9_STAT_MODE_TMP=67108864,P9_STAT_MODE_SYMLINK=33554432,P9_STAT_MODE_LINK=16777216,P9_STAT_MODE_DEVICE=
8388608,P9_STAT_MODE_NAMED_PIPE=2097152,P9_STAT_MODE_SOCKET=1048576,P9_STAT_MODE_SETUID=524288,P9_STAT_MODE_SETGID=262144,P9_STAT_MODE_SETVTX=65536;const P9_LOCK_TYPE_RDLCK=0,P9_LOCK_TYPE_WRLCK=1,P9_LOCK_TYPE_UNLCK=2,P9_LOCK_TYPES=Object.freeze(["shared","exclusive","unlock"]),P9_LOCK_FLAGS_BLOCK=1,P9_LOCK_FLAGS_RECLAIM=2,P9_LOCK_SUCCESS=0,P9_LOCK_BLOCKED=1,P9_LOCK_ERROR=2,P9_LOCK_GRACE=3;var FID_NONE=-1,FID_INODE=1,FID_XATTR=2;
function Virtio9p(a,b,c){this.fs=a;this.bus=c;this.configspace_tagname=[104,111,115,116,57,112];this.configspace_taglen=this.configspace_tagname.length;this.VERSION="9P2000.L";this.msize=this.BLOCKSIZE=8192;this.replybuffer=new Uint8Array(2*this.msize);this.replybuffersize=0;this.fids=[];this.virtio=new VirtIO(b,{name:"virtio-9p",pci_id:48,device_id:4169,subsystem_device_id:9,common:{initial_port:43008,queues:[{size_supported:32,notify_offset:0}],features:[VIRTIO_9P_F_MOUNT_TAG,VIRTIO_F_VERSION_1,
@@ -43,11 +44,11 @@ if(!m||m.status===STATUS_UNLINKED){message.Debug("read/treaddir: unlinked");this
EOPNOTSUPP);this.SendReply(a);break}else await this.fs.Write(this.fids[k].inodeid,e,l,b.subarray(c.offset));this.bus.send("9p-write-end",[d,l]);marshall.Marshall(["w"],[l],this.replybuffer,7);this.BuildReply(g,f,4);this.SendReply(a);break;case 74:d=marshall.Unmarshall(["w","s","w","s"],b,c);c=d[0];l=d[1];e=d[2];b=d[3];message.Debug("[renameat]: oldname="+l+" newname="+b);d=await this.fs.Rename(this.fids[c].inodeid,l,this.fids[e].inodeid,b);if(0>d){e="";d===-ENOENT?e="No such file or directory":d===
-EPERM?e="Operation not permitted":d===-ENOTEMPTY?e="Directory not empty":(e="Unknown error: "+-d,dbg_assert(!1,"[renameat]: Unexpected error code: "+-d));this.SendError(f,e,-d);this.SendReply(a);break}TRACK_FILENAMES&&(d=this.fs.Search(this.fids[e].inodeid,b),this.update_dbg_name(d,b));this.BuildReply(g,f,0);this.SendReply(a);break;case 76:d=marshall.Unmarshall(["w","s","w"],b,c);c=d[0];e=d[1];b=d[2];message.Debug("[unlink]: dirfd="+c+" name="+e+" flags="+b);k=this.fs.Search(this.fids[c].inodeid,
e);if(-1==k){this.SendError(f,"No such file or directory",ENOENT);this.SendReply(a);break}d=this.fs.Unlink(this.fids[c].inodeid,e);if(0>d){e="";d===-ENOTEMPTY?e="Directory not empty":d===-EPERM?e="Operation not permitted":(e="Unknown error: "+-d,dbg_assert(!1,"[unlink]: Unexpected error code: "+-d));this.SendError(f,e,-d);this.SendReply(a);break}this.BuildReply(g,f,0);this.SendReply(a);break;case 100:d=marshall.Unmarshall(["w","s"],b,c);message.Debug("[version]: msize="+d[0]+" version="+d[1]);this.msize=
d[0];e=marshall.Marshall(["w","s"],[this.msize,this.VERSION],this.replybuffer,7);this.BuildReply(g,f,e);this.SendReply(a);break;case 104:d=marshall.Unmarshall(["w","w","s","s","w"],b,c);k=d[0];e=d[4];message.Debug("[attach]: fid="+k+" afid="+hex8(d[1])+" uname="+d[2]+" aname="+d[3]);this.fids[k]=this.Createfid(0,FID_INODE,e,"");m=this.fs.GetInode(this.fids[k].inodeid);marshall.Marshall(["Q"],[m.qid],this.replybuffer,7);this.BuildReply(g,f,13);this.SendReply(a);break;case 108:d=marshall.Unmarshall(["h"],
b,c);message.Debug("[flush] "+f);this.BuildReply(g,f,0);this.SendReply(a);break;case 110:d=marshall.Unmarshall(["w","w","h"],b,c);k=d[0];l=d[1];var n=d[2];message.Debug("[walk]: fid="+d[0]+" nwfid="+d[1]+" nwname="+n);if(0==n){this.fids[l]=this.Createfid(this.fids[k].inodeid,FID_INODE,this.fids[k].uid,this.fids[k].dbg_name);marshall.Marshall(["h"],[0],this.replybuffer,7);this.BuildReply(g,f,2);this.SendReply(a);break}e=[];for(d=0;d<n;d++)e.push("s");c=marshall.Unmarshall(e,b,c);b=this.fids[k].inodeid;
e=9;var p=0;message.Debug("walk in dir "+this.fids[k].dbg_name+" to: "+c.toString());for(d=0;d<n;d++){b=this.fs.Search(b,c[d]);if(-1==b){message.Debug("Could not find: "+c[d]);break}e+=marshall.Marshall(["Q"],[this.fs.GetInode(b).qid],this.replybuffer,e);p++;this.fids[l]=this.Createfid(b,FID_INODE,this.fids[k].uid,c[d])}marshall.Marshall(["h"],[p],this.replybuffer,7);this.BuildReply(g,f,e-7);this.SendReply(a);break;case 120:d=marshall.Unmarshall(["w"],b,c);message.Debug("[clunk]: fid="+d[0]);this.fids[d[0]]&&
0<=this.fids[d[0]].inodeid&&(await this.fs.CloseInode(this.fids[d[0]].inodeid),this.fids[d[0]].inodeid=-1,this.fids[d[0]].type=FID_NONE);this.BuildReply(g,f,0);this.SendReply(a);break;case 32:d=marshall.Unmarshall(["w","s","d","w"],b,c);k=d[0];e=d[1];c=d[2];b=d[3];message.Debug("[txattrcreate]: fid="+k+" name="+e+" attr_size="+c+" flags="+b);this.fids[k].type=FID_XATTR;this.BuildReply(g,f,0);this.SendReply(a);break;case 30:d=marshall.Unmarshall(["w","w","s"],b,c);k=d[0];e=d[2];message.Debug("[xattrwalk]: fid="+
d[0]+" newfid="+d[1]+" name="+d[2]);this.SendError(f,"Setxattr not supported",EOPNOTSUPP);this.SendReply(a);break;default:message.Debug("Error in Virtio9p: Unknown id "+g+" received"),message.Abort()}};var DEBUG=!1,LOG_TO_FILE=!1,LOG_ALL_IO=!1,DUMP_GENERATED_WASM=!1,DUMP_UNCOMPILED_ASSEMBLY=!1,TRACK_FILENAMES=!1,LOG_LEVEL=LOG_ALL&~LOG_PS2&~LOG_PIT&~LOG_VIRTIO&~LOG_9P&~LOG_PIC&~LOG_DMA&~LOG_SERIAL&~LOG_NET&~LOG_FLOPPY&~LOG_DISK&~LOG_VGA&~LOG_SB16,DEBUG_SCREEN_LAYERS=DEBUG&&!1,ENABLE_HPET=DEBUG&&!1,TIME_PER_FRAME=1,TSC_RATE=1E6,APIC_TIMER_FREQ=TSC_RATE;function IO(a){this.ports=[];this.cpu=a;for(var b=0;65536>b;b++)this.ports[b]=this.create_empty_entry();var c=a.memory_size[0];for(b=0;b<<MMAP_BLOCK_BITS<c;b++)a.memory_map_read8[b]=a.memory_map_write8[b]=void 0,a.memory_map_read32[b]=a.memory_map_write32[b]=void 0;this.mmap_register(c,4294967296-c,function(d){dbg_log("Read from unmapped memory space, addr="+h(d>>>0,8),LOG_IO);return 255},function(d,e){dbg_log("Write to unmapped memory space, addr="+h(d>>>0,8)+" value="+h(e,2),LOG_IO)},function(d){dbg_log("Read from unmapped memory space, addr="+
d[0];e=marshall.Marshall(["w","s"],[this.msize,this.VERSION],this.replybuffer,7);this.BuildReply(g,f,e);this.SendReply(a);break;case 104:d=marshall.Unmarshall(["w","w","s","s","w"],b,c);k=d[0];e=d[4];message.Debug("[attach]: fid="+k+" afid="+hex8(d[1])+" uname="+d[2]+" aname="+d[3]);this.fids[k]=this.Createfid(0,FID_INODE,e,"");m=this.fs.GetInode(this.fids[k].inodeid);marshall.Marshall(["Q"],[m.qid],this.replybuffer,7);this.BuildReply(g,f,13);this.SendReply(a);this.bus.send("9p-attach");break;case 108:d=
marshall.Unmarshall(["h"],b,c);message.Debug("[flush] "+f);this.BuildReply(g,f,0);this.SendReply(a);break;case 110:d=marshall.Unmarshall(["w","w","h"],b,c);k=d[0];l=d[1];var n=d[2];message.Debug("[walk]: fid="+d[0]+" nwfid="+d[1]+" nwname="+n);if(0==n){this.fids[l]=this.Createfid(this.fids[k].inodeid,FID_INODE,this.fids[k].uid,this.fids[k].dbg_name);marshall.Marshall(["h"],[0],this.replybuffer,7);this.BuildReply(g,f,2);this.SendReply(a);break}e=[];for(d=0;d<n;d++)e.push("s");c=marshall.Unmarshall(e,
b,c);b=this.fids[k].inodeid;e=9;var q=0;message.Debug("walk in dir "+this.fids[k].dbg_name+" to: "+c.toString());for(d=0;d<n;d++){b=this.fs.Search(b,c[d]);if(-1==b){message.Debug("Could not find: "+c[d]);break}e+=marshall.Marshall(["Q"],[this.fs.GetInode(b).qid],this.replybuffer,e);q++;this.fids[l]=this.Createfid(b,FID_INODE,this.fids[k].uid,c[d])}marshall.Marshall(["h"],[q],this.replybuffer,7);this.BuildReply(g,f,e-7);this.SendReply(a);break;case 120:d=marshall.Unmarshall(["w"],b,c);message.Debug("[clunk]: fid="+
d[0]);this.fids[d[0]]&&0<=this.fids[d[0]].inodeid&&(await this.fs.CloseInode(this.fids[d[0]].inodeid),this.fids[d[0]].inodeid=-1,this.fids[d[0]].type=FID_NONE);this.BuildReply(g,f,0);this.SendReply(a);break;case 32:d=marshall.Unmarshall(["w","s","d","w"],b,c);k=d[0];e=d[1];c=d[2];b=d[3];message.Debug("[txattrcreate]: fid="+k+" name="+e+" attr_size="+c+" flags="+b);this.fids[k].type=FID_XATTR;this.BuildReply(g,f,0);this.SendReply(a);break;case 30:d=marshall.Unmarshall(["w","w","s"],b,c);k=d[0];e=d[2];
message.Debug("[xattrwalk]: fid="+d[0]+" newfid="+d[1]+" name="+d[2]);this.SendError(f,"Setxattr not supported",EOPNOTSUPP);this.SendReply(a);break;default:message.Debug("Error in Virtio9p: Unknown id "+g+" received"),message.Abort()}};var DEBUG=!1,LOG_TO_FILE=!1,LOG_ALL_IO=!1,DUMP_GENERATED_WASM=!1,DUMP_UNCOMPILED_ASSEMBLY=!1,TRACK_FILENAMES=!1,LOG_LEVEL=LOG_ALL&~LOG_PS2&~LOG_PIT&~LOG_VIRTIO&~LOG_9P&~LOG_PIC&~LOG_DMA&~LOG_SERIAL&~LOG_NET&~LOG_FLOPPY&~LOG_DISK&~LOG_VGA&~LOG_SB16,DEBUG_SCREEN_LAYERS=DEBUG&&!1,ENABLE_HPET=DEBUG&&!1,TIME_PER_FRAME=1,TSC_RATE=1E6,APIC_TIMER_FREQ=TSC_RATE;function IO(a){this.ports=[];this.cpu=a;for(var b=0;65536>b;b++)this.ports[b]=this.create_empty_entry();var c=a.memory_size[0];for(b=0;b<<MMAP_BLOCK_BITS<c;b++)a.memory_map_read8[b]=a.memory_map_write8[b]=void 0,a.memory_map_read32[b]=a.memory_map_write32[b]=void 0;this.mmap_register(c,4294967296-c,function(d){dbg_log("Read from unmapped memory space, addr="+h(d>>>0,8),LOG_IO);return 255},function(d,e){dbg_log("Write to unmapped memory space, addr="+h(d>>>0,8)+" value="+h(e,2),LOG_IO)},function(d){dbg_log("Read from unmapped memory space, addr="+
h(d>>>0,8),LOG_IO);return-1},function(d,e){dbg_log("Write to unmapped memory space, addr="+h(d>>>0,8)+" value="+h(e>>>0,8),LOG_IO)})}IO.prototype.create_empty_entry=function(){return{read8:this.empty_port_read8,read16:this.empty_port_read16,read32:this.empty_port_read32,write8:this.empty_port_write,write16:this.empty_port_write,write32:this.empty_port_write,device:void 0}};IO.prototype.empty_port_read8=function(){return 255};IO.prototype.empty_port_read16=function(){return 65535};
IO.prototype.empty_port_read32=function(){return-1};IO.prototype.empty_port_write=function(a){};
IO.prototype.register_read=function(a,b,c,d,e){dbg_assert("number"===typeof a);dbg_assert("object"===typeof b);dbg_assert(!c||"function"===typeof c);dbg_assert(!d||"function"===typeof d);dbg_assert(!e||"function"===typeof e);dbg_assert(c||d||e);if(DEBUG){var g=function(f){dbg_assert(!1,"Overlapped read"+f+" "+h(a,4)+" ("+b.name+")");return-1>>>32-f|0};c||(c=g.bind(this,8));d||(d=g.bind(this,16));e||(e=g.bind(this,32))}c&&(this.ports[a].read8=c);d&&(this.ports[a].read16=d);e&&(this.ports[a].read32=
@@ -63,33 +64,50 @@ IO.prototype.port_read16=function(a){var b=this.ports[a];(b.read16===this.empty_
IO.prototype.port_read32=function(a){var b=this.ports[a];(b.read32===this.empty_port_read32||LOG_ALL_IO)&&dbg_log("read32 port #"+h(a,4)+this.get_port_description(a),LOG_IO);a=b.read32.call(b.device);dbg_assert((a|0)===a);return a};
var debug_port_list={4:"PORT_DMA_ADDR_2",5:"PORT_DMA_CNT_2",10:"PORT_DMA1_MASK_REG",11:"PORT_DMA1_MODE_REG",12:"PORT_DMA1_CLEAR_FF_REG",13:"PORT_DMA1_MASTER_CLEAR",32:"PORT_PIC1_CMD",33:"PORT_PIC1_DATA",64:"PORT_PIT_COUNTER0",65:"PORT_PIT_COUNTER1",66:"PORT_PIT_COUNTER2",67:"PORT_PIT_MODE",96:"PORT_PS2_DATA",97:"PORT_PS2_CTRLB",100:"PORT_PS2_STATUS",112:"PORT_CMOS_INDEX",113:"PORT_CMOS_DATA",128:"PORT_DIAG",129:"PORT_DMA_PAGE_2",146:"PORT_A20",160:"PORT_PIC2_CMD",161:"PORT_PIC2_DATA",178:"PORT_SMI_CMD",
179:"PORT_SMI_STATUS",212:"PORT_DMA2_MASK_REG",214:"PORT_DMA2_MODE_REG",218:"PORT_DMA2_MASTER_CLEAR",240:"PORT_MATH_CLEAR",368:"PORT_ATA2_CMD_BASE",496:"PORT_ATA1_CMD_BASE",632:"PORT_LPT2",744:"PORT_SERIAL4",760:"PORT_SERIAL2",884:"PORT_ATA2_CTRL_BASE",888:"PORT_LPT1",1E3:"PORT_SERIAL3",1008:"PORT_FD_BASE",1010:"PORT_FD_DOR",1012:"PORT_FD_STATUS",1013:"PORT_FD_DATA",1014:"PORT_HD_DATA",1015:"PORT_FD_DIR",1016:"PORT_SERIAL1",3320:"PORT_PCI_CMD",3321:"PORT_PCI_REBOOT",3324:"PORT_PCI_DATA",1026:"PORT_BIOS_DEBUG",
1296:"PORT_QEMU_CFG_CTL",1297:"PORT_QEMU_CFG_DATA",45056:"PORT_ACPI_PM_BASE",45312:"PORT_SMB_BASE",35072:"PORT_BIOS_APM"};IO.prototype.get_port_description=function(a){return debug_port_list[a]?" ("+debug_port_list[a]+")":""};function v86(a,b){this.stopped=this.running=!1;this.tick_counter=0;this.worker=null;this.cpu=new CPU(a,b,()=>{this.idle&&this.next_tick(0)});this.bus=a;a.register("cpu-init",this.init,this);a.register("cpu-run",this.run,this);a.register("cpu-stop",this.stop,this);a.register("cpu-restart",this.restart,this);this.register_yield()}v86.prototype.run=function(){this.stopped=!1;this.running||(this.running=!0,this.bus.send("emulator-started"));this.next_tick(0)};
v86.prototype.do_tick=function(){if(this.stopped||!this.running)this.stopped=this.running=!1,this.bus.send("emulator-stopped");else{this.idle=!1;var a=this.cpu.main_run();this.next_tick(a)}};v86.prototype.next_tick=function(a){const b=++this.tick_counter;this.idle=!0;this.yield(a,b)};v86.prototype.yield_callback=function(a){a===this.tick_counter&&this.do_tick()};v86.prototype.stop=function(){this.running&&(this.stopped=!0)};v86.prototype.destroy=function(){this.unregister_yield()};
1296:"PORT_QEMU_CFG_CTL",1297:"PORT_QEMU_CFG_DATA",45056:"PORT_ACPI_PM_BASE",45312:"PORT_SMB_BASE",35072:"PORT_BIOS_APM"};IO.prototype.get_port_description=function(a){return debug_port_list[a]?" ("+debug_port_list[a]+")":""};function v86(a,b){this.stopping=this.running=!1;this.tick_counter=0;this.worker=null;this.cpu=new CPU(a,b,()=>{this.idle&&this.next_tick(0)});this.bus=a;a.register("cpu-init",this.init,this);a.register("cpu-run",this.run,this);a.register("cpu-stop",this.stop,this);a.register("cpu-restart",this.restart,this);this.register_yield()}v86.prototype.run=function(){this.stopping=!1;this.running||(this.running=!0,this.bus.send("emulator-started"));this.next_tick(0)};
v86.prototype.do_tick=function(){if(this.stopping||!this.running)this.stopping=this.running=!1,this.bus.send("emulator-stopped");else{this.idle=!1;var a=this.cpu.main_run();this.next_tick(a)}};v86.prototype.next_tick=function(a){const b=++this.tick_counter;this.idle=!0;this.yield(a,b)};v86.prototype.yield_callback=function(a){a===this.tick_counter&&this.do_tick()};v86.prototype.stop=function(){this.running&&(this.stopping=!0)};v86.prototype.destroy=function(){this.unregister_yield()};
v86.prototype.restart=function(){this.cpu.reset_cpu();this.cpu.load_bios()};v86.prototype.init=function(a){this.cpu.init(a,this.bus);this.bus.send("emulator-ready")};
if("undefined"!==typeof process)v86.prototype.yield=function(a,b){1>a?global.setImmediate(c=>this.yield_callback(c),b):setTimeout(c=>this.yield_callback(c),a,b)},v86.prototype.register_yield=function(){},v86.prototype.unregister_yield=function(){};else if("undefined"!==typeof Worker){function a(){globalThis.onmessage=function(b){const c=b.data.t;1>c?postMessage(b.data.tick):setTimeout(()=>postMessage(b.data.tick),c)}}v86.prototype.register_yield=function(){const b=URL.createObjectURL(new Blob(["("+
a.toString()+")()"],{type:"text/javascript"}));this.worker=new Worker(b);this.worker.onmessage=c=>this.yield_callback(c.data);URL.revokeObjectURL(b)};v86.prototype.yield=function(b,c){this.worker.postMessage({t:b,tick:c})};v86.prototype.unregister_yield=function(){this.worker.terminate();this.worker=null}}else v86.prototype.yield=function(a){setTimeout(()=>{this.do_tick()},a)},v86.prototype.register_yield=function(){},v86.prototype.unregister_yield=function(){};v86.prototype.save_state=function(){return this.cpu.save_state()};
v86.prototype.restore_state=function(a){return this.cpu.restore_state(a)};if("object"===typeof performance&&performance.now)v86.microtick=performance.now.bind(performance);else if("function"===typeof require){const {performance:a}=require("perf_hooks");v86.microtick=a.now.bind(a)}else v86.microtick="object"===typeof process&&process.hrtime?function(){var a=process.hrtime();return 1E3*a[0]+a[1]/1E6}:Date.now;var goog=goog||{};goog.exportSymbol=function(){};goog.exportProperty=function(){};var v86util=v86util||{};v86util.pads=function(a,b){return(a||0===a?a+"":"").padEnd(b," ")};v86util.pad0=function(a,b){return(a||0===a?a+"":"").padStart(b,"0")};v86util.zeros=function(a){return Array(a).fill(0)};v86util.range=function(a){return Array.from(Array(a).keys())};
v86util.view=function(a,b,c,d){return new Proxy({},{get:function(e,g,f){e=new a(b.buffer,c,d);f=e[g];if("function"===typeof f)return f.bind(e);dbg_assert(/^\d+$/.test(g)||"buffer"===g||"length"===g||"BYTES_PER_ELEMENT"===g||"byteOffset"===g);return f},set:function(e,g,f,k){dbg_assert(/^\d+$/.test(g));(new a(b.buffer,c,d))[g]=f;return!0}})};function h(a,b){a=a?a.toString(16):"";return"0x"+v86util.pad0(a.toUpperCase(),b||1)}
if("undefined"!==typeof crypto&&crypto.getRandomValues){let a=new Int32Array(1);v86util.get_rand_int=function(){crypto.getRandomValues(a);return a[0]}}else if("undefined"!==typeof require){const a=require("crypto");v86util.get_rand_int=function(){return a.randomBytes(4).readInt32LE(0)}}else dbg_assert(!1,"Unsupported platform: No cryptographic random values");function SyncBuffer(a){dbg_assert(a instanceof ArrayBuffer);this.buffer=a;this.byteLength=a.byteLength;this.onprogress=this.onload=void 0}
SyncBuffer.prototype.load=function(){this.onload&&this.onload({buffer:this.buffer})};SyncBuffer.prototype.get=function(a,b,c){dbg_assert(a+b<=this.byteLength);c(new Uint8Array(this.buffer,a,b))};SyncBuffer.prototype.set=function(a,b,c){dbg_assert(a+b.byteLength<=this.byteLength);(new Uint8Array(this.buffer,a,b.byteLength)).set(b);c()};SyncBuffer.prototype.get_buffer=function(a){a(this.buffer)};
SyncBuffer.prototype.get_state=function(){const a=[];a[0]=this.byteLength;a[1]=new Uint8Array(this.buffer);return a};SyncBuffer.prototype.set_state=function(a){this.byteLength=a[0];this.buffer=a[1].slice().buffer};
function hex_dump(a){function b(k,m){return v86util.pad0(k.toString(16).toUpperCase(),m)}const c=[];let d=0;for(;d+15<a.length;d+=16){var e=b(d,5)+" ";for(var g=0;16>g;g++)e+=b(a[d+g],2)+" ";e+=" ";for(g=0;16>g;g++){var f=a[d+g];e+=33<=f&&34!==f&&92!==f&&126>=f?String.fromCharCode(f):"."}c.push(e)}for(e=b(d,5)+" ";d<a.length;d++)e+=b(a[d],2)+" ";g=d&15;e+=" ".repeat(16-g);e+=" ";for(f=0;f<g;f++){const k=a[d+f];e+=33<=k&&34!==k&&92!==k&&126>=k?String.fromCharCode(k):"."}c.push(e);return"\n"+
c.join("\n")+"\n"}if("undefined"!==typeof crypto&&crypto.getRandomValues){let a=new Int32Array(1);v86util.get_rand_int=function(){crypto.getRandomValues(a);return a[0]}}else if("undefined"!==typeof require){const a=require("crypto");v86util.get_rand_int=function(){return a.randomBytes(4).readInt32LE(0)}}else dbg_assert(!1,"Unsupported platform: No cryptographic random values");
(function(){if("function"===typeof Math.clz32)v86util.int_log2_byte=function(d){dbg_assert(0<d);dbg_assert(256>d);return 31-Math.clz32(d)},v86util.int_log2=function(d){dbg_assert(0<d);return 31-Math.clz32(d)};else{for(var a=new Int8Array(256),b=0,c=-2;256>b;b++)b&b-1||c++,a[b]=c;v86util.int_log2_byte=function(d){dbg_assert(0<d);dbg_assert(256>d);return a[d]};v86util.int_log2=function(d){d>>>=0;dbg_assert(0<d);var e=d>>>16;if(e){var g=e>>>8;return g?24+a[g]:16+a[e]}return(g=d>>>8)?8+a[g]:a[d]}}})();
function ByteQueue(a){var b=new Uint8Array(a),c,d;dbg_assert(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 FloatQueue(a){this.size=a;this.data=new Float32Array(a);this.length=this.end=this.start=0;dbg_assert(0===(a&a-1))}
FloatQueue.prototype.push=function(a){this.length===this.size?this.start=this.start+1&this.size-1:this.length++;this.data[this.end]=a;this.end=this.end+1&this.size-1};FloatQueue.prototype.shift=function(){if(this.length){var a=this.data[this.start];this.start=this.start+1&this.size-1;this.length--;return a}};
FloatQueue.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};FloatQueue.prototype.peek=function(){if(this.length)return this.data[this.start]};FloatQueue.prototype.clear=function(){this.length=this.end=this.start=0};function CircularQueue(a){this.data=[];this.index=0;this.size=a}
CircularQueue.prototype.add=function(a){this.data[this.index]=a;this.index=(this.index+1)%this.size};CircularQueue.prototype.toArray=function(){return[].slice.call(this.data,this.index).concat([].slice.call(this.data,0,this.index))};CircularQueue.prototype.clear=function(){this.data=[];this.index=0};CircularQueue.prototype.set=function(a){this.data=a;this.index=0};function dump_file(a,b){a instanceof Array||(a=[a]);a=new Blob(a);download(a,b)}
function download(a,b){var c=document.createElement("a");c.download=b;c.href=window.URL.createObjectURL(a);c.dataset.downloadurl=["application/octet-stream",c.download,c.href].join(":");document.createEvent?(a=document.createEvent("MouseEvent"),a.initMouseEvent("click",!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null),c.dispatchEvent(a)):c.click();window.URL.revokeObjectURL(c.href)}
v86util.Bitmap=function(a){"number"===typeof a?this.view=new Uint8Array(a+7>>3):a instanceof ArrayBuffer?this.view=new Uint8Array(a):console.assert(!1)};v86util.Bitmap.prototype.set=function(a,b){const c=a>>3;a=1<<(a&7);this.view[c]=b?this.view[c]|a:this.view[c]&~a};v86util.Bitmap.prototype.get=function(a){return this.view[a>>3]>>(a&7)&1};v86util.Bitmap.prototype.get_buffer=function(){return this.view.buffer};function hex_dump(a,b){var c=[];b=b||a.byteLength;for(var d,e,g=0;g<b>>4;g++){d=h(g<<4,5)+" ";for(var f=0;16>f;f++)e=a[(g<<4)+f],d+=h(e,2)+" ";d+=" ";for(f=0;16>f;f++)e=a[(g<<4)+f],d+=33>e||126<e?".":String.fromCharCode(e);c.push(d)}return"\n"+c.join("\n")}var CDROM_SECTOR_SIZE=2048,HD_SECTOR_SIZE=512;
function IDEDevice(a,b,c,d,e,g){this.master=new IDEInterface(this,a,b,d,e,0,g);this.slave=new IDEInterface(this,a,c,!1,e,1,g);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):dbg_assert(!1,"IDE device with nr "+e+" ignored",LOG_DISK);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,1,0,0,0,1,0,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,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(){dbg_log("lower irq",LOG_DISK);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)},function(){return this.current_interface.read_data(4)});a.io.register_read(this.ata_port|1,this,function(){dbg_log("Read error: "+h(this.current_interface.error&255)+" slave="+(this.current_interface===
this.slave),LOG_DISK);return this.current_interface.error&255});a.io.register_read(this.ata_port|2,this,function(){dbg_log("Read bytecount: "+h(this.current_interface.bytecount&255),LOG_DISK);return this.current_interface.bytecount&255});a.io.register_read(this.ata_port|3,this,function(){dbg_log("Read sector: "+h(this.current_interface.sector&255),LOG_DISK);return this.current_interface.sector&255});a.io.register_read(this.ata_port|4,this,function(){dbg_log("Read 1F4: "+h(this.current_interface.cylinder_low&
255),LOG_DISK);return this.current_interface.cylinder_low&255});a.io.register_read(this.ata_port|5,this,function(){dbg_log("Read 1F5: "+h(this.current_interface.cylinder_high&255),LOG_DISK);return this.current_interface.cylinder_high&255});a.io.register_read(this.ata_port|6,this,function(){dbg_log("Read 1F6",LOG_DISK);return this.current_interface.drive_head&255});a.io.register_write(this.ata_port|0,this,function(f){this.current_interface.write_data_port8(f)},function(f){this.current_interface.write_data_port16(f)},
function(f){this.current_interface.write_data_port32(f)});a.io.register_write(this.ata_port|1,this,function(f){dbg_log("1F1/lba_count: "+h(f),LOG_DISK);this.master.lba_count=(this.master.lba_count<<8|f)&65535;this.slave.lba_count=(this.slave.lba_count<<8|f)&65535});a.io.register_write(this.ata_port|2,this,function(f){dbg_log("1F2/bytecount: "+h(f),LOG_DISK);this.master.bytecount=(this.master.bytecount<<8|f)&65535;this.slave.bytecount=(this.slave.bytecount<<8|f)&65535});a.io.register_write(this.ata_port|
3,this,function(f){dbg_log("1F3/sector: "+h(f),LOG_DISK);this.master.sector=(this.master.sector<<8|f)&65535;this.slave.sector=(this.slave.sector<<8|f)&65535});a.io.register_write(this.ata_port|4,this,function(f){dbg_log("1F4/sector low: "+h(f),LOG_DISK);this.master.cylinder_low=(this.master.cylinder_low<<8|f)&65535;this.slave.cylinder_low=(this.slave.cylinder_low<<8|f)&65535});a.io.register_write(this.ata_port|5,this,function(f){dbg_log("1F5/sector high: "+h(f),LOG_DISK);this.master.cylinder_high=
(this.master.cylinder_high<<8|f)&65535;this.slave.cylinder_high=(this.slave.cylinder_high<<8|f)&65535});a.io.register_write(this.ata_port|6,this,function(f){var k=f&16;dbg_log("1F6/drive: "+h(f,2),LOG_DISK);k?(dbg_log("Slave",LOG_DISK),this.current_interface=this.slave):this.current_interface=this.master;this.master.drive_head=f;this.slave.drive_head=f;this.master.is_lba=this.slave.is_lba=f>>6&1;this.master.head=this.slave.head=f&15});this.dma_command=this.dma_status=this.prdt_addr=0;a.io.register_write(this.ata_port|
7,this,function(f){dbg_log("lower irq",LOG_DISK);this.cpu.device_lower_irq(this.irq);this.current_interface.ata_command(f)});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,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(){dbg_log("DMA read 0x8",LOG_DISK);return 0});a.io.register_read(this.master_port|10,this,function(){dbg_log("DMA read 0xA",LOG_DISK);return 0});a.devices.pci.register_device(this);DEBUG&&Object.seal(this)}
v86util.Bitmap=function(a){"number"===typeof a?this.view=new Uint8Array(a+7>>3):a instanceof ArrayBuffer?this.view=new Uint8Array(a):dbg_assert(!1,"v86util.Bitmap: Invalid argument")};v86util.Bitmap.prototype.set=function(a,b){const c=a>>3;a=1<<(a&7);this.view[c]=b?this.view[c]|a:this.view[c]&~a};v86util.Bitmap.prototype.get=function(a){return this.view[a>>3]>>(a&7)&1};v86util.Bitmap.prototype.get_buffer=function(){return this.view.buffer};
v86util.load_file="undefined"!==typeof XMLHttpRequest?load_file_nodejs:load_file;
function load_file(a,b,c){function d(){const m=c||0;setTimeout(()=>{load_file(a,b,m+1)},1E3*([1,1,2,3,5,8,13,21][m]||34))}var e=new XMLHttpRequest;e.open(b.method||"get",a,!0);e.responseType=b.as_json?"json":"arraybuffer";if(b.headers)for(var g=Object.keys(b.headers),f=0;f<g.length;f++){var k=g[f];e.setRequestHeader(k,b.headers[k])}b.range&&(g=b.range.start,e.setRequestHeader("Range","bytes="+g+"-"+(g+b.range.length-1)),e.onreadystatechange=function(){200===e.status&&e.abort()});e.onload=function(m){4===
e.readyState&&(200!==e.status&&206!==e.status?(console.error("Loading the image "+a+" failed (status %d)",e.status),500<=e.status&&600>e.status&&d()):e.response&&b.done&&b.done(e.response,e))};e.onerror=function(m){console.error("Loading the image "+a+" failed",m);d()};b.progress&&(e.onprogress=function(m){b.progress(m)});e.send(null)}
function load_file_nodejs(a,b){let c=require("fs");b.range?(dbg_assert(!b.as_json),c.open(a,"r",(d,e)=>{if(d)throw d;let g=b.range.length;var f=Buffer.allocUnsafe(g);c.read(e,f,0,g,b.range.start,(k,m)=>{if(k)throw k;dbg_assert(m===g);b.done&&b.done(new Uint8Array(f));c.close(e,l=>{if(l)throw l;})})})):c.readFile(a,{encoding:b.as_json?"utf-8":null},function(d,e){d?console.log("Could not read file:",a,d):(d=e,d=b.as_json?JSON.parse(d):(new Uint8Array(d)).buffer,b.done(d))})}
v86util.read_sized_string_from_mem=function(a,b,c){return String.fromCharCode(...(new Uint8Array(a.buffer,b>>>0,c>>>0)))};(function(){function a(f){dbg_assert(f instanceof ArrayBuffer);this.buffer=f;this.byteLength=f.byteLength;this.onprogress=this.onload=void 0}function b(f,k){this.filename=f;this.byteLength=k;this.block_cache=new Map;this.block_cache_is_write=new Set;this.onprogress=this.onload=void 0}function c(f,k,m,l){const n=f.match(/(.*)(\..*)/);n?(this.basename=n[1],this.extension=n[2]):(this.basename=f,this.extension="");this.block_cache=new Map;this.block_cache_is_write=new Set;this.byteLength=k;this.fixed_chunk_size=
m;this.partfile_alt_format=!!l;this.cache_reads=!!m;this.onprogress=this.onload=void 0}function d(f){this.file=f;this.byteLength=f.size;1073741824<f.size&&console.warn("SyncFileBuffer: Allocating buffer of "+(f.size>>20)+" MB ...");this.buffer=new ArrayBuffer(f.size);this.onprogress=this.onload=void 0}function e(f){this.file=f;this.byteLength=f.size;this.block_cache=new Map;this.block_cache_is_write=new Set;this.onprogress=this.onload=void 0}v86util.SyncBuffer=a;v86util.AsyncXHRBuffer=b;v86util.AsyncXHRPartfileBuffer=
c;v86util.AsyncFileBuffer=e;v86util.SyncFileBuffer=d;a.prototype.load=function(){this.onload&&this.onload({buffer:this.buffer})};a.prototype.get=function(f,k,m){dbg_assert(f+k<=this.byteLength);m(new Uint8Array(this.buffer,f,k))};a.prototype.set=function(f,k,m){dbg_assert(f+k.byteLength<=this.byteLength);(new Uint8Array(this.buffer,f,k.byteLength)).set(k);m()};a.prototype.get_buffer=function(f){f(this.buffer)};a.prototype.get_state=function(){const f=[];f[0]=this.byteLength;f[1]=new Uint8Array(this.buffer);
return f};a.prototype.set_state=function(f){this.byteLength=f[0];this.buffer=f[1].slice().buffer};b.prototype.load=function(){void 0!==this.byteLength?this.onload&&this.onload(Object.create(null)):g(this.filename,(f,k)=>{if(f)throw Error("Cannot use: "+this.filename+". "+f);dbg_assert(0<=k);this.byteLength=k;this.onload&&this.onload(Object.create(null))})};b.prototype.get_from_cache=function(f,k){var m=k/256;f/=256;for(var l=0;l<m;l++)if(!this.block_cache.get(f+l))return;if(1===m)return this.block_cache.get(f);
k=new Uint8Array(k);for(l=0;l<m;l++)k.set(this.block_cache.get(f+l),256*l);return k};b.prototype.get=function(f,k,m){dbg_assert(f+k<=this.byteLength);dbg_assert(0===f%256);dbg_assert(0===k%256);dbg_assert(k);var l=this.get_from_cache(f,k);l?m(l):v86util.load_file(this.filename,{done:function(n){n=new Uint8Array(n);this.handle_read(f,k,n);m(n)}.bind(this),range:{start:f,length:k}})};b.prototype.set=function(f,k,m){var l=k.length;dbg_assert(f+k.byteLength<=this.byteLength);dbg_assert(0===f%256);dbg_assert(0===
l%256);dbg_assert(l);f/=256;l/=256;for(var n=0;n<l;n++){var q=this.block_cache.get(f+n);void 0===q&&(q=new Uint8Array(256),this.block_cache.set(f+n,q));var t=k.subarray(256*n,256*(n+1));q.set(t);dbg_assert(q.byteLength===t.length);this.block_cache_is_write.add(f+n)}m()};b.prototype.handle_read=function(f,k,m){f/=256;k/=256;for(var l=0;l<k;l++){var n=this.block_cache.get(f+l);n?m.set(n,256*l):this.cache_reads&&(n=new Uint8Array(256),n.set(m.subarray(256*l,256*(l+1))),this.block_cache.set(f+l,n))}};
b.prototype.get_buffer=function(f){f()};b.prototype.get_state=function(){const f=[],k=[];for(let [m,l]of this.block_cache)dbg_assert(isFinite(m)),this.block_cache_is_write.has(m)&&k.push([m,l]);f[0]=k;return f};b.prototype.set_state=function(f){f=f[0];this.block_cache.clear();this.block_cache_is_write.clear();for(let [k,m]of f)dbg_assert(isFinite(k)),this.block_cache.set(k,m),this.block_cache_is_write.add(k)};c.prototype.load=function(){void 0===this.byteLength&&dbg_assert(!1);this.onload&&this.onload(Object.create(null))};
c.prototype.get=function(f,k,m){dbg_assert(f+k<=this.byteLength);dbg_assert(0===f%256);dbg_assert(0===k%256);dbg_assert(k);var l=this.get_from_cache(f,k);if(l)m(l);else if(this.fixed_chunk_size){const q=Math.floor(f/this.fixed_chunk_size),t=f-q*this.fixed_chunk_size;dbg_assert(0<=t);const r=Math.ceil((t+k)/this.fixed_chunk_size),p=new Uint8Array(r*this.fixed_chunk_size);let v=0;for(let u=0;u<r;u++){var n=(q+u)*this.fixed_chunk_size;l=this.partfile_alt_format?this.basename+"-"+(q+u+"").padStart(8,
"0")+this.extension:this.basename+"-"+n+"-"+(n+this.fixed_chunk_size)+this.extension;(n=this.get_from_cache(n,this.fixed_chunk_size))?(p.set(n,u*this.fixed_chunk_size),v++,v===r&&(l=p.subarray(t,t+k),m(l))):v86util.load_file(l,{done:function(x){var z=u*this.fixed_chunk_size;x=new Uint8Array(x);this.handle_read((q+u)*this.fixed_chunk_size,this.fixed_chunk_size|0,x);p.set(x,z);v++;v===r&&(z=p.subarray(t,t+k),m(z))}.bind(this)})}}else v86util.load_file(this.basename+"-"+f+"-"+(f+k)+this.extension,{done:function(q){dbg_assert(q.byteLength===
k);q=new Uint8Array(q);this.handle_read(f,k,q);m(q)}.bind(this)})};c.prototype.get_from_cache=b.prototype.get_from_cache;c.prototype.set=b.prototype.set;c.prototype.handle_read=b.prototype.handle_read;c.prototype.get_state=b.prototype.get_state;c.prototype.set_state=b.prototype.set_state;d.prototype.load=function(){this.load_next(0)};d.prototype.load_next=function(f){var k=new FileReader;k.onload=function(l){l=new Uint8Array(l.target.result);(new Uint8Array(this.buffer,f)).set(l);this.load_next(f+
4194304)}.bind(this);if(this.onprogress)this.onprogress({loaded:f,total:this.byteLength,lengthComputable:!0});if(f<this.byteLength){var m=this.file.slice(f,Math.min(f+4194304,this.byteLength));k.readAsArrayBuffer(m)}else this.file=void 0,this.onload&&this.onload({buffer:this.buffer})};d.prototype.get=a.prototype.get;d.prototype.set=a.prototype.set;d.prototype.get_buffer=a.prototype.get_buffer;d.prototype.get_state=a.prototype.get_state;d.prototype.set_state=a.prototype.set_state;e.prototype.load=
function(){this.onload&&this.onload(Object.create(null))};e.prototype.get=function(f,k,m){dbg_assert(0===f%256);dbg_assert(0===k%256);dbg_assert(k);var l=this.get_from_cache(f,k);l?m(l):(l=new FileReader,l.onload=function(n){n=new Uint8Array(n.target.result);this.handle_read(f,k,n);m(n)}.bind(this),l.readAsArrayBuffer(this.file.slice(f,f+k)))};e.prototype.get_from_cache=b.prototype.get_from_cache;e.prototype.set=b.prototype.set;e.prototype.handle_read=b.prototype.handle_read;e.prototype.get_state=
b.prototype.get_state;e.prototype.set_state=b.prototype.set_state;e.prototype.get_buffer=function(f){f()};e.prototype.get_as_file=function(f){for(var k=[],m=Array.from(this.block_cache.keys()).sort(function(r,p){return r-p}),l=0,n=0;n<m.length;n++){var q=m[n],t=this.block_cache.get(q);q*=256;dbg_assert(q>=l);q!==l&&(k.push(this.file.slice(l,q)),l=q);k.push(t);l+=t.length}l!==this.file.size&&k.push(this.file.slice(l));f=new File(k,f);dbg_assert(f.size===this.file.size);return f};var g="undefined"!==
typeof XMLHttpRequest?function(f,k){require("fs").stat(f,(m,l)=>{m?k(m):k(null,l.size)})}:function(f,k){v86util.load_file(f,{done:(m,l)=>{m=l.getResponseHeader("Content-Range")||"";(l=m.match(/\/(\d+)\s*$/))?k(null,+l[1]):k("`Range: bytes=...` header not supported (Got `"+m+"`)")},headers:{Range:"bytes=0-0"}})}})();var CDROM_SECTOR_SIZE=2048,HD_SECTOR_SIZE=512;
function IDEDevice(a,b,c,d,e,g){this.master=new IDEInterface(this,a,b,d,e,0,g);this.slave=new IDEInterface(this,a,c,!1,e,1,g);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):dbg_assert(!1,"IDE device with nr "+e+" ignored",LOG_DISK);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,0,0,0,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(){dbg_log("lower irq",
LOG_DISK);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)},function(){return this.current_interface.read_data(4)});a.io.register_read(this.ata_port|1,this,function(){dbg_log("Read error: "+h(this.current_interface.error&
255)+" slave="+(this.current_interface===this.slave),LOG_DISK);return this.current_interface.error&255});a.io.register_read(this.ata_port|2,this,function(){dbg_log("Read bytecount: "+h(this.current_interface.bytecount&255),LOG_DISK);return this.current_interface.bytecount&255});a.io.register_read(this.ata_port|3,this,function(){dbg_log("Read sector: "+h(this.current_interface.sector&255),LOG_DISK);return this.current_interface.sector&255});a.io.register_read(this.ata_port|4,this,function(){dbg_log("Read 1F4: "+
h(this.current_interface.cylinder_low&255),LOG_DISK);return this.current_interface.cylinder_low&255});a.io.register_read(this.ata_port|5,this,function(){dbg_log("Read 1F5: "+h(this.current_interface.cylinder_high&255),LOG_DISK);return this.current_interface.cylinder_high&255});a.io.register_read(this.ata_port|6,this,function(){dbg_log("Read 1F6",LOG_DISK);return this.current_interface.drive_head&255});a.io.register_write(this.ata_port|0,this,function(f){this.current_interface.write_data_port8(f)},
function(f){this.current_interface.write_data_port16(f)},function(f){this.current_interface.write_data_port32(f)});a.io.register_write(this.ata_port|1,this,function(f){dbg_log("1F1/lba_count: "+h(f),LOG_DISK);this.master.lba_count=(this.master.lba_count<<8|f)&65535;this.slave.lba_count=(this.slave.lba_count<<8|f)&65535});a.io.register_write(this.ata_port|2,this,function(f){dbg_log("1F2/bytecount: "+h(f),LOG_DISK);this.master.bytecount=(this.master.bytecount<<8|f)&65535;this.slave.bytecount=(this.slave.bytecount<<
8|f)&65535});a.io.register_write(this.ata_port|3,this,function(f){dbg_log("1F3/sector: "+h(f),LOG_DISK);this.master.sector=(this.master.sector<<8|f)&65535;this.slave.sector=(this.slave.sector<<8|f)&65535});a.io.register_write(this.ata_port|4,this,function(f){dbg_log("1F4/sector low: "+h(f),LOG_DISK);this.master.cylinder_low=(this.master.cylinder_low<<8|f)&65535;this.slave.cylinder_low=(this.slave.cylinder_low<<8|f)&65535});a.io.register_write(this.ata_port|5,this,function(f){dbg_log("1F5/sector high: "+
h(f),LOG_DISK);this.master.cylinder_high=(this.master.cylinder_high<<8|f)&65535;this.slave.cylinder_high=(this.slave.cylinder_high<<8|f)&65535});a.io.register_write(this.ata_port|6,this,function(f){var k=f&16;dbg_log("1F6/drive: "+h(f,2),LOG_DISK);k?(dbg_log("Slave",LOG_DISK),this.current_interface=this.slave):this.current_interface=this.master;this.master.drive_head=f;this.slave.drive_head=f;this.master.is_lba=this.slave.is_lba=f>>6&1;this.master.head=this.slave.head=f&15});this.dma_command=this.dma_status=
this.prdt_addr=0;a.io.register_write(this.ata_port|7,this,function(f){dbg_log("lower irq",LOG_DISK);this.cpu.device_lower_irq(this.irq);this.current_interface.ata_command(f)});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,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(){dbg_log("DMA read 0x8",LOG_DISK);return 0});a.io.register_read(this.master_port|10,this,function(){dbg_log("DMA read 0xA",LOG_DISK);return 0});a.devices.pci.register_device(this);DEBUG&&Object.seal(this)}
IDEDevice.prototype.read_status=function(){if(this.current_interface.buffer){var a=this.current_interface.status;dbg_log("ATA read status: "+h(a,2),LOG_DISK);return a}return 0};IDEDevice.prototype.write_control=function(a){dbg_log("set device control: "+h(a,2)+" interrupts "+(a&2?"disabled":"enabled"),LOG_DISK);a&4&&(dbg_log("Reset via control port",LOG_DISK),this.cpu.device_lower_irq(this.irq),this.master.device_reset(),this.slave.device_reset());this.device_control=a};
IDEDevice.prototype.dma_read_addr=function(){dbg_log("dma get address: "+h(this.prdt_addr,8),LOG_DISK);return this.prdt_addr};IDEDevice.prototype.dma_set_addr=function(a){dbg_log("dma set address: "+h(a,8),LOG_DISK);this.prdt_addr=a};IDEDevice.prototype.dma_read_status=function(){dbg_log("DMA read status: "+h(this.dma_status),LOG_DISK);return this.dma_status};IDEDevice.prototype.dma_write_status=function(a){dbg_log("DMA set status: "+h(a),LOG_DISK);this.dma_status&=~(a&6)};
IDEDevice.prototype.dma_read_command=function(){return this.dma_read_command8()|this.dma_read_status()<<16};IDEDevice.prototype.dma_read_command8=function(){dbg_log("DMA read command: "+h(this.dma_command),LOG_DISK);return this.dma_command};IDEDevice.prototype.dma_write_command=function(a){dbg_log("DMA write command: "+h(a),LOG_DISK);this.dma_write_command8(a&255);this.dma_write_status(a>>16&255)};
@@ -168,8 +186,8 @@ LOG_PCI),d[c]=g),1===e&&(dbg_assert(1===e),e=d[c]&65534,g=b&65534,dbg_log("io ba
" value="+h(b>>>0,8),LOG_PCI):(dbg_log("PCI write dev="+h(c>>3,2)+" ("+e.name+") addr="+h(a,4)+" value="+h(b>>>0,8),LOG_PCI),d[a>>>2]=b)};
PCI.prototype.register_device=function(a){dbg_assert(void 0!==a.pci_id);dbg_assert(void 0!==a.pci_space);dbg_assert(void 0!==a.pci_bars);var b=a.pci_id;dbg_log("PCI register bdf="+h(b)+" ("+a.name+")",LOG_PCI);dbg_assert(!this.devices[b]);dbg_assert(64<=a.pci_space.length);dbg_assert(b<this.devices.length);var c=new Int32Array(64);c.set(new Int32Array((new Uint8Array(a.pci_space)).buffer));this.device_spaces[b]=c;this.devices[b]=a;b=c.slice(4,10);for(var d=0;d<a.pci_bars.length;d++){var e=a.pci_bars[d];
if(e){var g=b[d],f=g&1;e.original_bar=g;e.entries=[];if(0!==f)for(dbg_assert(1===f),g&=-2,f=0;f<e.size;f++)e.entries[f]=this.io.ports[g+f]}}return c};
PCI.prototype.set_io_bars=function(a,b,c){var d=a.size;dbg_log("Move io bars: from="+h(b)+" to="+h(c)+" count="+d,LOG_PCI);for(var e=this.io.ports,g=0;g<d;g++){var f=e[b+g];e[b+g]=this.io.create_empty_entry();f.read8===this.io.empty_port_read8&&f.read16===this.io.empty_port_read16&&f.read32===this.io.empty_port_read32&&f.write8===this.io.empty_port_write&&f.write16===this.io.empty_port_write&&f.write32===this.io.empty_port_write&&dbg_log("Warning: Bad IO bar: Source not mapped, port="+h(b+g,4),LOG_PCI);
f=a.entries[g];var k=e[c+g];dbg_assert(f&&k);e[c+g]=f;k.read8!==this.io.empty_port_read8&&k.read16!==this.io.empty_port_read16&&k.read32!==this.io.empty_port_read32&&k.write8!==this.io.empty_port_write&&k.write16!==this.io.empty_port_write&&k.write32!==this.io.empty_port_write||dbg_log("Warning: Bad IO bar: Target already mapped, port="+h(c+g,4),LOG_PCI)}};
PCI.prototype.set_io_bars=function(a,b,c){var d=a.size;dbg_log("Move io bars: from="+h(b)+" to="+h(c)+" count="+d,LOG_PCI);for(var e=this.io.ports,g=0;g<d;g++){var f=e[b+g];4096<=b+g&&(e[b+g]=this.io.create_empty_entry());f.read8===this.io.empty_port_read8&&f.read16===this.io.empty_port_read16&&f.read32===this.io.empty_port_read32&&f.write8===this.io.empty_port_write&&f.write16===this.io.empty_port_write&&f.write32===this.io.empty_port_write&&dbg_log("Warning: Bad IO bar: Source not mapped, port="+
h(b+g,4),LOG_PCI);f=a.entries[g];var k=e[c+g];dbg_assert(f&&k);4096<=c+g&&(e[c+g]=f);k.read8!==this.io.empty_port_read8&&k.read16!==this.io.empty_port_read16&&k.read32!==this.io.empty_port_read32&&k.write8!==this.io.empty_port_write&&k.write16!==this.io.empty_port_write&&k.write32!==this.io.empty_port_write||dbg_log("Warning: Bad IO bar: Target already mapped, port="+h(c+g,4),LOG_PCI)}};
PCI.prototype.raise_irq=function(a){var b=this.device_spaces[a];dbg_assert(b);this.cpu.device_raise_irq(this.isa_bridge_space8[96+((b[15]>>8&255)-1+((a>>3)-1&255)&3)])};PCI.prototype.lower_irq=function(a){var b=this.device_spaces[a];dbg_assert(b);this.cpu.device_lower_irq(this.isa_bridge_space8[96+((b[15]>>8&255)+(a>>3&255)-2&3)])};function FloppyController(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.response_length=this.response_index=0;this.fda_image=b;this.fdb_image=c;this.last_head=this.last_cylinder=this.drive=this.status_reg2=this.status_reg1=this.status_reg0=0;this.last_sector=1;this.dor=0;if(b){var d={[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,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 e=b.byteLength;
c=d[e];c||(e=1474560<b.byteLength?2949120:1474560,c=d[e],dbg_log("Warning: Unkown floppy size: "+b.byteLength+", assuming "+e));a.devices.rtc.cmos_write(CMOS_FLOPPY_DRIVE_TYPE,c.type<<4);a=c.sectors;b=c.heads;c=c.tracks;this.sectors_per_track=a;this.number_of_heads=b;this.number_of_cylinders=c}else a.devices.rtc.cmos_write(CMOS_FLOPPY_DRIVE_TYPE,64),this.number_of_cylinders=this.number_of_heads=this.sectors_per_track=0;this.io.register_read(1008,this,this.port3F0_read);this.io.register_read(1010,
@@ -179,7 +197,7 @@ FloppyController.prototype.set_state=function(a){this.bytes_expecting=a[0];this.
FloppyController.prototype.port3F0_read=function(){dbg_log("3F0 read",LOG_FLOPPY);return 0};FloppyController.prototype.port3F4_read=function(){dbg_log("3F4 read",LOG_FLOPPY);var a=128;this.response_index<this.response_length&&(a|=80);0===(this.dor&8)&&(a|=32);return a};FloppyController.prototype.port3F7_read=function(){dbg_log("3F7 read",LOG_FLOPPY);return 0};
FloppyController.prototype.port3F5_read=function(){if(this.response_index<this.response_length)return dbg_log("3F5 read: "+this.response_data[this.response_index],LOG_FLOPPY),this.cpu.device_lower_irq(6),this.response_data[this.response_index++];dbg_log("3F5 read, empty",LOG_FLOPPY);return 255};
FloppyController.prototype.port3F5_write=function(a){if(this.fda_image)if(dbg_log("3F5 write "+h(a),LOG_FLOPPY),0<this.bytes_expecting){if(this.receiving_command[this.receiving_index++]=a,this.bytes_expecting--,0===this.bytes_expecting){if(DEBUG){a="3F5 command received: ";for(var b=0;b<this.receiving_index;b++)a+=h(this.receiving_command[b])+" ";dbg_log(a,LOG_FLOPPY)}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 4:this.next_command=this.check_drive_status;this.bytes_expecting=1;break;case 5:case 197:this.next_command=function(c){this.do_sector(!0,c)};this.bytes_expecting=8;break;case 230:this.next_command=function(c){this.do_sector(!1,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=
2;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 230:this.next_command=function(c){this.do_sector(!1,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:dbg_log("dump registers",LOG_FLOPPY);this.response_data[0]=128;this.response_index=0;this.response_length=1;this.bytes_expecting=0;break;default:dbg_assert(!1,"Unimplemented floppy command call "+h(a))}this.receiving_index=0}};FloppyController.prototype.port3F2_read=function(){dbg_log("read 3F2: DOR",LOG_FLOPPY);return this.dor};
FloppyController.prototype.port3F2_write=function(a){4===(a&4)&&0===(this.dor&4)&&this.cpu.device_raise_irq(6);dbg_log("start motors: "+h(a>>4),LOG_FLOPPY);dbg_log("enable dma: "+!!(a&8),LOG_FLOPPY);dbg_log("reset fdc: "+!!(a&4),LOG_FLOPPY);dbg_log("drive select: "+(a&3),LOG_FLOPPY);dbg_log("DOR = "+h(a),LOG_FLOPPY);this.dor=a};FloppyController.prototype.check_drive_status=function(a){dbg_log("check drive status",LOG_FLOPPY);this.response_index=0;this.response_length=1;this.response_data[0]=32};
FloppyController.prototype.seek=function(a){dbg_log("seek",LOG_FLOPPY);dbg_assert(0===(a[0]&3),"Unhandled seek drive");this.last_cylinder=a[1];this.last_head=a[0]>>2&1;this.raise_irq()};FloppyController.prototype.calibrate=function(a){dbg_log("floppy calibrate",LOG_FLOPPY);this.raise_irq()};FloppyController.prototype.check_interrupt_status=function(){dbg_log("floppy check interrupt status",LOG_FLOPPY);this.response_index=0;this.response_length=2;this.response_data[0]=32;this.response_data[1]=this.last_cylinder};
@@ -205,8 +223,8 @@ DMA.prototype.port_singlemask_write=function(a,b){a=(b&3)+a;b=b&4?1:0;dbg_log("s
DMA.prototype.port_multimask_read=function(a){var b=0|this.channel_mask[a+0];b|=this.channel_mask[a+1]<<1;b|=this.channel_mask[a+2]<<2;b|=this.channel_mask[a+3]<<3;dbg_log("multichannel mask read: "+h(b),LOG_DMA);return b};DMA.prototype.port_mode_write=function(a,b){a=(b&3)+a;dbg_log("mode write ["+a+"] = "+h(b),LOG_DMA);this.channel_mode[a]=b};DMA.prototype.portC_write=function(a){dbg_log("flipflop reset",LOG_DMA);this.lsb_msb_flipflop=0};
DMA.prototype.on_unmask=function(a,b){this.unmask_listeners.push({fn:a,this_value:b})};DMA.prototype.update_mask=function(a,b){if(this.channel_mask[a]!==b&&(this.channel_mask[a]=b,!b))for(dbg_log("firing on_unmask("+a+")",LOG_DMA),b=0;b<this.unmask_listeners.length;b++)this.unmask_listeners[b].fn.call(this.unmask_listeners[b].this_value,a)};
DMA.prototype.do_read=function(a,b,c,d,e){var g=this.count_get_8bit(d),f=this.address_get_8bit(d);dbg_log("DMA write channel "+d,LOG_DMA);dbg_log("to "+h(f)+" len "+h(g),LOG_DMA);c<g&&dbg_log("DMA should read more than provided: "+h(c)+" "+h(g),LOG_DMA);if(b+g>a.byteLength)dbg_log("DMA read outside of buffer",LOG_DMA),e(!0);else{var k=this.cpu;this.channel_addr[d]+=g;a.get(b,g,function(m){k.write_blob(m,f);e(!1)})}};
DMA.prototype.do_write=function(a,b,c,d,e){var g=this.channel_count[d]+1&65535,f=5<=d?2:1,k=g*f,m=this.address_get_8bit(d),l=!1,n=!1,p=this.channel_mode[d]&16;dbg_log("DMA write channel "+d,LOG_DMA);dbg_log("to "+h(m)+" len "+h(k),LOG_DMA);c<k?(dbg_log("DMA should read more than provided",LOG_DMA),g=Math.floor(c/f),k=g*f,l=!0):c>k&&(dbg_log("DMA attempted to read more than provided",LOG_DMA),n=!0);b+k>a.byteLength?(dbg_log("DMA write outside of buffer",LOG_DMA),e(!0)):(this.channel_addr[d]+=g,this.channel_count[d]-=
g,!l&&p&&(dbg_log("DMA autoinit",LOG_DMA),this.channel_addr[d]=this.channel_addr_init[d],this.channel_count[d]=this.channel_count_init[d]),a.set(b,this.cpu.mem8.subarray(m,m+k),()=>{n&&p?(dbg_log("DMA continuing from start",LOG_DMA),this.do_write(a,b+k,c-k,d,e)):e(!1)}))};DMA.prototype.address_get_8bit=function(a){var b=this.channel_addr[a];5<=a&&(b<<=1);b=b&65535|this.channel_page[a]<<16;return b|=this.channel_pagehi[a]<<24};
DMA.prototype.do_write=function(a,b,c,d,e){var g=this.channel_count[d]+1&65535,f=5<=d?2:1,k=g*f,m=this.address_get_8bit(d),l=!1,n=!1,q=this.channel_mode[d]&16;dbg_log("DMA write channel "+d,LOG_DMA);dbg_log("to "+h(m)+" len "+h(k),LOG_DMA);c<k?(dbg_log("DMA should read more than provided",LOG_DMA),g=Math.floor(c/f),k=g*f,l=!0):c>k&&(dbg_log("DMA attempted to read more than provided",LOG_DMA),n=!0);b+k>a.byteLength?(dbg_log("DMA write outside of buffer",LOG_DMA),e(!0)):(this.channel_addr[d]+=g,this.channel_count[d]-=
g,!l&&q&&(dbg_log("DMA autoinit",LOG_DMA),this.channel_addr[d]=this.channel_addr_init[d],this.channel_count[d]=this.channel_count_init[d]),a.set(b,this.cpu.mem8.subarray(m,m+k),()=>{n&&q?(dbg_log("DMA continuing from start",LOG_DMA),this.do_write(a,b+k,c-k,d,e)):e(!1)}))};DMA.prototype.address_get_8bit=function(a){var b=this.channel_addr[a];5<=a&&(b<<=1);b=b&65535|this.channel_page[a]<<16;return b|=this.channel_pagehi[a]<<24};
DMA.prototype.count_get_8bit=function(a){var b=this.channel_count[a]+1;5<=a&&(b*=2);return b};DMA.prototype.flipflop_get=function(a,b,c){c||(this.lsb_msb_flipflop^=1);return this.lsb_msb_flipflop?a&-256|b:a&-65281|b<<8};DMA.prototype.flipflop_read=function(a){return(this.lsb_msb_flipflop^=1)?a&255:a>>8&255};var OSCILLATOR_FREQ=1193.1816666;
function PIT(a,b){this.cpu=a;this.bus=b;this.counter_start_time=new Float64Array(3);this.counter_start_value=new Uint16Array(3);this.counter_next_low=new Uint8Array(4);this.counter_enabled=new Uint8Array(4);this.counter_mode=new Uint8Array(4);this.counter_read_mode=new Uint8Array(4);this.counter_latch=new Uint8Array(4);this.counter_latch_value=new Uint16Array(3);this.counter_reload=new Uint16Array(3);a.io.register_read(97,this,function(){var c=v86.microtick(),d=66.66666666666667*c&1;c=this.did_rollover(2,
c);return d<<4|c<<5});a.io.register_write(97,this,function(c){c&1?this.bus.send("pcspeaker-enable"):this.bus.send("pcspeaker-disable")});a.io.register_read(64,this,function(){return this.counter_read(0)});a.io.register_read(65,this,function(){return this.counter_read(1)});a.io.register_read(66,this,function(){return this.counter_read(2)});a.io.register_write(64,this,function(c){this.counter_write(0,c)});a.io.register_write(65,this,function(c){this.counter_write(1,c)});a.io.register_write(66,this,
@@ -221,24 +239,24 @@ LOG_PIT));3===this.counter_read_mode[a]&&(this.counter_next_low[a]^=1)};
PIT.prototype.port43_write=function(a){var b=a>>1&7,c=a&1,d=a>>6&3;a=a>>4&3;1===d&&dbg_log("Unimplemented timer1",LOG_PIT);3===d?dbg_log("Unimplemented read back",LOG_PIT):0===a?(this.counter_latch[d]=2,b=this.get_counter_value(d,v86.microtick()),dbg_log("latch: "+b,LOG_PIT),this.counter_latch_value[d]=b?b-1:0):(6<=b&&(b&=-5),dbg_log("Control: mode="+b+" ctr="+d+" read_mode="+a+" bcd="+c,LOG_PIT),this.counter_next_low[d]=1===a?0:1,0===d&&this.cpu.device_lower_irq(0),0!==b&&3!==b&&2!==b&&dbg_log("Unimplemented counter mode: "+
h(b),LOG_PIT),this.counter_mode[d]=b,this.counter_read_mode[d]=a,2===d&&this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]]))};PIT.prototype.dump=function(){const a=this.counter_reload[0];dbg_log("counter0 ticks every "+(a||65536)/OSCILLATOR_FREQ+"ms (reload="+a+")")};var VGA_BANK_SIZE=65536,MAX_XRES=2560,MAX_YRES=1600,MAX_BPP=32,VGA_LFB_ADDRESS=3758096384,VGA_PIXEL_BUFFER_SIZE=8*VGA_BANK_SIZE,VGA_MIN_MEMORY_SIZE=4*VGA_BANK_SIZE,VGA_HOST_MEMORY_SPACE_START=Uint32Array.from([655360,655360,720896,753664]),VGA_HOST_MEMORY_SPACE_SIZE=Uint32Array.from([131072,65536,32768,32768]);
function VGAScreen(a,b,c){this.cpu=a;this.bus=b;this.vga_memory_size=c;this.cursor_address=0;this.cursor_scanline_start=14;this.cursor_scanline_end=15;this.max_cols=80;this.max_rows=25;this.virtual_height=this.virtual_width=this.screen_height=this.screen_width=0;this.layers=[];this.start_address_latched=this.start_address=0;this.crtc=new Uint8Array(25);this.line_compare=this.offset_register=this.preset_row_scan=this.underline_location_register=this.vertical_blank_start=this.vertical_display_enable_end=
this.horizontal_blank_start=this.horizontal_display_enable_end=this.crtc_mode=0;this.graphical_mode_is_linear=!0;this.graphical_mode=!1;setTimeout(()=>{b.send("screen-set-mode",this.graphical_mode)},0);this.vga256_palette=new Int32Array(256);this.svga_height=this.svga_width=this.latch_dword=0;this.svga_enabled=!1;this.svga_bpp=32;this.svga_offset=this.svga_bank_offset=0;this.pci_space=[52,18,17,17,3,1,0,0,2,0,0,3,0,0,0,0,8,VGA_LFB_ADDRESS>>>8,VGA_LFB_ADDRESS>>>16,VGA_LFB_ADDRESS>>>24,0,0,0,0,0,0,
191,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,0,17,0,0,190,254,0,0,0,0,0,0,0,0,0,0,0,0];this.pci_id=144;this.pci_bars=[{size:c}];this.pci_rom_size=65536;this.pci_rom_address=4272947200;this.name="vga";this.stats={is_graphical:!1,res_x:0,res_y:0,bpp:0};this.dac_state=this.dac_color_index_read=this.dac_color_index_write=this.index_crtc=0;this.dac_map=new Uint8Array(16);this.attribute_controller_index=-1;this.palette_source=32;this.color_select=this.horizontal_panning=this.color_plane_enable=this.attribute_mode=
0;this.sequencer_index=-1;this.plane_write_bm=15;this.clocking_mode=this.sequencer_memory_mode=0;this.graphics_index=-1;this.planar_rotate_reg=this.planar_mode=this.plane_read=0;this.planar_bitmap=255;this.max_scan_line=this.color_dont_care=this.color_compare=this.miscellaneous_graphics_register=this.planar_setreset_enable=this.planar_setreset=0;this.port_3DA_value=this.miscellaneous_output_register=255;c=a.io;c.register_write(960,this,this.port3C0_write);c.register_read(960,this,this.port3C0_read,
this.port3C0_read16);c.register_read(961,this,this.port3C1_read);c.register_write(962,this,this.port3C2_write);c.register_write_consecutive(964,this,this.port3C4_write,this.port3C5_write);c.register_read(964,this,this.port3C4_read);c.register_read(965,this,this.port3C5_read);c.register_write_consecutive(974,this,this.port3CE_write,this.port3CF_write);c.register_read(974,this,this.port3CE_read);c.register_read(975,this,this.port3CF_read);c.register_write(967,this,this.port3C7_write);c.register_read(967,
this,this.port3C7_read);c.register_write(968,this,this.port3C8_write);c.register_read(968,this,this.port3C8_read);c.register_write(969,this,this.port3C9_write);c.register_read(969,this,this.port3C9_read);c.register_read(972,this,this.port3CC_read);c.register_write_consecutive(980,this,this.port3D4_write,this.port3D5_write);c.register_read(980,this,this.port3D4_read);c.register_read(981,this,this.port3D5_read,()=>{dbg_log("Warning: 16-bit read from 3D5",LOG_VGA);return this.port3D5_read()});c.register_read(970,
this,function(){dbg_log("3CA read",LOG_VGA);return 0});c.register_read(986,this,this.port3DA_read);c.register_read(954,this,this.port3DA_read);this.dispi_index=-1;this.dispi_enable_value=0;c.register_write(462,this,void 0,this.port1CE_write);c.register_write(463,this,void 0,this.port1CF_write);c.register_read(463,this,void 0,this.port1CF_read);void 0===this.vga_memory_size||this.vga_memory_size<VGA_MIN_MEMORY_SIZE?(this.vga_memory_size=VGA_MIN_MEMORY_SIZE,dbg_log("vga memory size rounded up to "+
this.vga_memory_size,LOG_VGA)):this.vga_memory_size&VGA_BANK_SIZE-1&&(this.vga_memory_size|=VGA_BANK_SIZE-1,this.vga_memory_size++);const d=a.svga_allocate_memory(this.vga_memory_size);this.svga_memory=v86util.view(Uint8Array,a.wasm_memory,d,this.vga_memory_size);this.diff_addr_min=this.vga_memory_size;this.diff_addr_max=0;this.diff_plot_min=this.vga_memory_size;this.diff_plot_max=0;this.image_data=null;b.register("screen-fill-buffer",function(){this.screen_fill_buffer()},this);this.vga_memory=new Uint8Array(4*
VGA_BANK_SIZE);this.plane0=new Uint8Array(this.vga_memory.buffer,0*VGA_BANK_SIZE,VGA_BANK_SIZE);this.plane1=new Uint8Array(this.vga_memory.buffer,1*VGA_BANK_SIZE,VGA_BANK_SIZE);this.plane2=new Uint8Array(this.vga_memory.buffer,2*VGA_BANK_SIZE,VGA_BANK_SIZE);this.plane3=new Uint8Array(this.vga_memory.buffer,3*VGA_BANK_SIZE,VGA_BANK_SIZE);this.pixel_buffer=new Uint8Array(VGA_PIXEL_BUFFER_SIZE);var e=this;c.mmap_register(655360,131072,function(g){return e.vga_memory_read(g)},function(g,f){e.vga_memory_write(g,
f)});a.devices.pci.register_device(this)}
this.horizontal_blank_start=this.horizontal_display_enable_end=this.crtc_mode=0;this.graphical_mode_is_linear=!0;this.graphical_mode=!1;setTimeout(()=>{b.send("screen-set-mode",this.graphical_mode)},0);this.vga256_palette=new Int32Array(256);this.svga_height=this.svga_width=this.latch_dword=0;this.svga_enabled=!1;this.svga_bpp=32;this.svga_offset=this.svga_bank_offset=0;this.pci_space=[52,18,17,17,3,1,0,0,0,0,0,3,0,0,0,0,8,VGA_LFB_ADDRESS>>>8,VGA_LFB_ADDRESS>>>16,VGA_LFB_ADDRESS>>>24,0,0,0,0,0,0,
191,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,0,17,0,0,190,254,0,0,0,0,0,0,0,0,0,0,0,0];this.pci_id=144;this.pci_bars=[{size:c}];this.pci_rom_size=65536;this.pci_rom_address=4272947200;this.name="vga";this.stats={is_graphical:!1,res_x:0,res_y:0,bpp:0};this.dac_state=this.dac_color_index_read=this.dac_color_index_write=this.index_crtc=0;this.dac_mask=255;this.dac_map=new Uint8Array(16);this.attribute_controller_index=-1;this.palette_source=32;this.color_select=this.horizontal_panning=this.color_plane_enable=
this.attribute_mode=0;this.sequencer_index=-1;this.plane_write_bm=15;this.clocking_mode=this.sequencer_memory_mode=0;this.graphics_index=-1;this.planar_rotate_reg=this.planar_mode=this.plane_read=0;this.planar_bitmap=255;this.max_scan_line=this.color_dont_care=this.color_compare=this.miscellaneous_graphics_register=this.planar_setreset_enable=this.planar_setreset=0;this.port_3DA_value=this.miscellaneous_output_register=255;c=a.io;c.register_write(960,this,this.port3C0_write);c.register_read(960,this,
this.port3C0_read,this.port3C0_read16);c.register_read(961,this,this.port3C1_read);c.register_write(962,this,this.port3C2_write);c.register_write_consecutive(964,this,this.port3C4_write,this.port3C5_write);c.register_read(964,this,this.port3C4_read);c.register_read(965,this,this.port3C5_read);c.register_write_consecutive(974,this,this.port3CE_write,this.port3CF_write);c.register_read(974,this,this.port3CE_read);c.register_read(975,this,this.port3CF_read);c.register_read(966,this,this.port3C6_read);
c.register_write(966,this,this.port3C6_write);c.register_write(967,this,this.port3C7_write);c.register_read(967,this,this.port3C7_read);c.register_write(968,this,this.port3C8_write);c.register_read(968,this,this.port3C8_read);c.register_write(969,this,this.port3C9_write);c.register_read(969,this,this.port3C9_read);c.register_read(972,this,this.port3CC_read);c.register_write_consecutive(980,this,this.port3D4_write,this.port3D5_write);c.register_read(980,this,this.port3D4_read);c.register_read(981,
this,this.port3D5_read,()=>{dbg_log("Warning: 16-bit read from 3D5",LOG_VGA);return this.port3D5_read()});c.register_read(970,this,function(){dbg_log("3CA read",LOG_VGA);return 0});c.register_read(986,this,this.port3DA_read);c.register_read(954,this,this.port3DA_read);this.dispi_index=-1;this.dispi_enable_value=0;c.register_write(462,this,void 0,this.port1CE_write);c.register_write(463,this,void 0,this.port1CF_write);c.register_read(463,this,void 0,this.port1CF_read);void 0===this.vga_memory_size||
this.vga_memory_size<VGA_MIN_MEMORY_SIZE?(this.vga_memory_size=VGA_MIN_MEMORY_SIZE,dbg_log("vga memory size rounded up to "+this.vga_memory_size,LOG_VGA)):this.vga_memory_size&VGA_BANK_SIZE-1&&(this.vga_memory_size|=VGA_BANK_SIZE-1,this.vga_memory_size++);const d=a.svga_allocate_memory(this.vga_memory_size);this.svga_memory=v86util.view(Uint8Array,a.wasm_memory,d,this.vga_memory_size);this.diff_addr_min=this.vga_memory_size;this.diff_addr_max=0;this.diff_plot_min=this.vga_memory_size;this.diff_plot_max=
0;this.image_data=null;b.register("screen-fill-buffer",function(){this.screen_fill_buffer()},this);this.vga_memory=new Uint8Array(4*VGA_BANK_SIZE);this.plane0=new Uint8Array(this.vga_memory.buffer,0*VGA_BANK_SIZE,VGA_BANK_SIZE);this.plane1=new Uint8Array(this.vga_memory.buffer,1*VGA_BANK_SIZE,VGA_BANK_SIZE);this.plane2=new Uint8Array(this.vga_memory.buffer,2*VGA_BANK_SIZE,VGA_BANK_SIZE);this.plane3=new Uint8Array(this.vga_memory.buffer,3*VGA_BANK_SIZE,VGA_BANK_SIZE);this.pixel_buffer=new Uint8Array(VGA_PIXEL_BUFFER_SIZE);
var e=this;c.mmap_register(655360,131072,function(g){return e.vga_memory_read(g)},function(g,f){e.vga_memory_write(g,f)});a.devices.pci.register_device(this)}
VGAScreen.prototype.get_state=function(){var a=[];a[0]=this.vga_memory_size;a[1]=this.cursor_address;a[2]=this.cursor_scanline_start;a[3]=this.cursor_scanline_end;a[4]=this.max_cols;a[5]=this.max_rows;a[6]=this.vga_memory;a[7]=this.dac_state;a[8]=this.start_address;a[9]=this.graphical_mode;a[10]=this.vga256_palette;a[11]=this.latch_dword;a[12]=this.color_compare;a[13]=this.color_dont_care;a[14]=this.miscellaneous_graphics_register;a[15]=this.svga_width;a[16]=this.svga_height;a[17]=this.crtc_mode;
a[18]=this.svga_enabled;a[19]=this.svga_bpp;a[20]=this.svga_bank_offset;a[21]=this.svga_offset;a[22]=this.index_crtc;a[23]=this.dac_color_index_write;a[24]=this.dac_color_index_read;a[25]=this.dac_map;a[26]=this.sequencer_index;a[27]=this.plane_write_bm;a[28]=this.sequencer_memory_mode;a[29]=this.graphics_index;a[30]=this.plane_read;a[31]=this.planar_mode;a[32]=this.planar_rotate_reg;a[33]=this.planar_bitmap;a[34]=this.max_scan_line;a[35]=this.miscellaneous_output_register;a[36]=this.port_3DA_value;
a[37]=this.dispi_index;a[38]=this.dispi_enable_value;a[39]=this.svga_memory;a[40]=this.graphical_mode_is_linear;a[41]=this.attribute_controller_index;a[42]=this.offset_register;a[43]=this.planar_setreset;a[44]=this.planar_setreset_enable;a[45]=this.start_address_latched;a[46]=this.crtc;a[47]=this.horizontal_display_enable_end;a[48]=this.horizontal_blank_start;a[49]=this.vertical_display_enable_end;a[50]=this.vertical_blank_start;a[51]=this.underline_location_register;a[52]=this.preset_row_scan;a[53]=
this.offset_register;a[54]=this.palette_source;a[55]=this.attribute_mode;a[56]=this.color_plane_enable;a[57]=this.horizontal_panning;a[58]=this.color_select;a[59]=this.clocking_mode;a[60]=this.line_compare;a[61]=this.pixel_buffer;return a};
this.offset_register;a[54]=this.palette_source;a[55]=this.attribute_mode;a[56]=this.color_plane_enable;a[57]=this.horizontal_panning;a[58]=this.color_select;a[59]=this.clocking_mode;a[60]=this.line_compare;a[61]=this.pixel_buffer;a[62]=this.dac_mask;return a};
VGAScreen.prototype.set_state=function(a){this.vga_memory_size=a[0];this.cursor_address=a[1];this.cursor_scanline_start=a[2];this.cursor_scanline_end=a[3];this.max_cols=a[4];this.max_rows=a[5];a[6]&&this.vga_memory.set(a[6]);this.dac_state=a[7];this.start_address=a[8];this.graphical_mode=a[9];this.vga256_palette=a[10];this.latch_dword=a[11];this.color_compare=a[12];this.color_dont_care=a[13];this.miscellaneous_graphics_register=a[14];this.svga_width=a[15];this.svga_height=a[16];this.crtc_mode=a[17];
this.svga_enabled=a[18];this.svga_bpp=a[19];this.svga_bank_offset=a[20];this.svga_offset=a[21];this.index_crtc=a[22];this.dac_color_index_write=a[23];this.dac_color_index_read=a[24];this.dac_map=a[25];this.sequencer_index=a[26];this.plane_write_bm=a[27];this.sequencer_memory_mode=a[28];this.graphics_index=a[29];this.plane_read=a[30];this.planar_mode=a[31];this.planar_rotate_reg=a[32];this.planar_bitmap=a[33];this.max_scan_line=a[34];this.miscellaneous_output_register=a[35];this.port_3DA_value=a[36];
this.dispi_index=a[37];this.dispi_enable_value=a[38];this.svga_memory.set(a[39]);this.graphical_mode_is_linear=a[40];this.attribute_controller_index=a[41];this.offset_register=a[42];this.planar_setreset=a[43];this.planar_setreset_enable=a[44];this.start_address_latched=a[45];this.crtc.set(a[46]);this.horizontal_display_enable_end=a[47];this.horizontal_blank_start=a[48];this.vertical_display_enable_end=a[49];this.vertical_blank_start=a[50];this.underline_location_register=a[51];this.preset_row_scan=
a[52];this.offset_register=a[53];this.palette_source=a[54];this.attribute_mode=a[55];this.color_plane_enable=a[56];this.horizontal_panning=a[57];this.color_select=a[58];this.clocking_mode=a[59];this.line_compare=a[60];a[61]&&this.pixel_buffer.set(a[61]);this.bus.send("screen-set-mode",this.graphical_mode);this.graphical_mode?(this.screen_height=this.screen_width=0,this.svga_enabled?(this.set_size_graphical(this.svga_width,this.svga_height,this.svga_bpp,this.svga_width,this.svga_height),this.update_layers()):
(this.update_vga_size(),this.update_layers(),this.complete_replot())):(this.set_size_text(this.max_cols,this.max_rows),this.update_cursor_scanline(),this.update_cursor());this.complete_redraw()};
a[52];this.offset_register=a[53];this.palette_source=a[54];this.attribute_mode=a[55];this.color_plane_enable=a[56];this.horizontal_panning=a[57];this.color_select=a[58];this.clocking_mode=a[59];this.line_compare=a[60];a[61]&&this.pixel_buffer.set(a[61]);this.dac_mask=void 0===a[62]?255:a[62];this.bus.send("screen-set-mode",this.graphical_mode);this.graphical_mode?(this.screen_height=this.screen_width=0,this.svga_enabled?(this.set_size_graphical(this.svga_width,this.svga_height,this.svga_bpp,this.svga_width,
this.svga_height),this.update_layers()):(this.update_vga_size(),this.update_layers(),this.complete_replot())):(this.set_size_text(this.max_cols,this.max_rows),this.update_cursor_scanline(),this.update_cursor());this.complete_redraw()};
VGAScreen.prototype.vga_memory_read=function(a){if(this.svga_enabled&&this.graphical_mode_is_linear)return this.cpu.read8((a-655360|this.svga_bank_offset)+VGA_LFB_ADDRESS|0);var b=this.miscellaneous_graphics_register>>2&3;a-=VGA_HOST_MEMORY_SPACE_START[b];if(0>a||a>=VGA_HOST_MEMORY_SPACE_SIZE[b])return dbg_log("vga read outside memory space: addr:"+h(a),LOG_VGA),0;this.latch_dword=this.plane0[a];this.latch_dword|=this.plane1[a]<<8;this.latch_dword|=this.plane2[a]<<16;this.latch_dword|=this.plane3[a]<<
24;if(this.planar_mode&8)return b=255,this.color_dont_care&1&&(b&=this.plane0[a]^~(this.color_compare&1?255:0)),this.color_dont_care&2&&(b&=this.plane1[a]^~(this.color_compare&2?255:0)),this.color_dont_care&4&&(b&=this.plane2[a]^~(this.color_compare&4?255:0)),this.color_dont_care&8&&(b&=this.plane3[a]^~(this.color_compare&8?255:0)),b;b=this.plane_read;this.graphical_mode?this.sequencer_memory_mode&8?(b=a&3,a&=-4):this.planar_mode&16&&(b=a&1,a&=-2):b=0;return this.vga_memory[b<<16|a]};
VGAScreen.prototype.vga_memory_write=function(a,b){if(this.svga_enabled&&this.graphical_mode&&this.graphical_mode_is_linear)this.cpu.write8((a-655360|this.svga_bank_offset)+VGA_LFB_ADDRESS|0,b);else{var c=this.miscellaneous_graphics_register>>2&3;a-=VGA_HOST_MEMORY_SPACE_START[c];0>a||a>=VGA_HOST_MEMORY_SPACE_SIZE[c]?dbg_log("vga write outside memory space: addr:"+h(a)+", value:"+h(b),LOG_VGA):this.graphical_mode?this.vga_memory_write_graphical(a,b):this.plane_write_bm&3&&this.vga_memory_write_text_mode(a,
@@ -246,8 +264,8 @@ b)}};
VGAScreen.prototype.vga_memory_write_graphical=function(a,b){var c=this.planar_mode&3,d=this.apply_feed(this.planar_bitmap),e=this.apply_expand(this.planar_setreset),g=this.apply_expand(this.planar_setreset_enable);switch(c){case 0:b=this.apply_rotate(b);var f=this.apply_feed(b);f=this.apply_setreset(f,g);f=this.apply_logical(f,this.latch_dword);f=this.apply_bitmask(f,d);break;case 1:f=this.latch_dword;break;case 2:f=this.apply_expand(b);f=this.apply_logical(f,this.latch_dword);f=this.apply_bitmask(f,d);
break;case 3:b=this.apply_rotate(b),d&=this.apply_feed(b),f=this.apply_bitmask(e,d)}b=15;switch(this.sequencer_memory_mode&12){case 0:b=5<<(a&1);a&=-2;break;case 8:case 12:b=1<<(a&3),a&=-4}b&=this.plane_write_bm;b&1&&(this.plane0[a]=f>>0&255);b&2&&(this.plane1[a]=f>>8&255);b&4&&(this.plane2[a]=f>>16&255);b&8&&(this.plane3[a]=f>>24&255);a=this.vga_addr_to_pixel(a);this.partial_replot(a,a+7)};VGAScreen.prototype.apply_feed=function(a){return a|a<<8|a<<16|a<<24};
VGAScreen.prototype.apply_expand=function(a){return(a&1?255:0)|(a&2?255:0)<<8|(a&4?255:0)<<16|(a&8?255:0)<<24};VGAScreen.prototype.apply_rotate=function(a){return(a|a<<8)>>>(this.planar_rotate_reg&7)&255};VGAScreen.prototype.apply_setreset=function(a,b){var c=this.apply_expand(this.planar_setreset);return(a|b&c)&(~b|c)};VGAScreen.prototype.apply_logical=function(a,b){switch(this.planar_rotate_reg&24){case 8:return a&b;case 16:return a|b;case 24:return a^b}return a};
VGAScreen.prototype.apply_bitmask=function(a,b){return b&a|~b&this.latch_dword};VGAScreen.prototype.text_mode_redraw=function(){for(var a=this.start_address<<1,b,c,d=0;d<this.max_rows;d++)for(var e=0;e<this.max_cols;e++)b=this.vga_memory[a],c=this.vga_memory[a|1],this.bus.send("screen-put-char",[d,e,b,this.vga256_palette[c>>4&15],this.vga256_palette[c&15]]),a+=2};
VGAScreen.prototype.vga_memory_write_text_mode=function(a,b){var c=(a>>1)-this.start_address,d=c/this.max_cols|0;c%=this.max_cols;if(a&1){var e=b;var g=this.vga_memory[a&-2]}else g=b,e=this.vga_memory[a|1];this.bus.send("screen-put-char",[d,c,g,this.vga256_palette[e>>4&15],this.vga256_palette[e&15]]);this.vga_memory[a]=b};
VGAScreen.prototype.apply_bitmask=function(a,b){return b&a|~b&this.latch_dword};VGAScreen.prototype.text_mode_redraw=function(){for(var a=this.start_address<<1,b,c,d=0;d<this.max_rows;d++)for(var e=0;e<this.max_cols;e++)b=this.vga_memory[a],c=this.vga_memory[a|1],this.bus.send("screen-put-char",[d,e,b,this.vga256_palette[this.dac_mask&this.dac_map[c>>4&15]],this.vga256_palette[this.dac_mask&this.dac_map[c&15]]]),a+=2};
VGAScreen.prototype.vga_memory_write_text_mode=function(a,b){var c=(a>>1)-this.start_address,d=c/this.max_cols|0;c%=this.max_cols;if(a&1){var e=b;var g=this.vga_memory[a&-2]}else g=b,e=this.vga_memory[a|1];this.bus.send("screen-put-char",[d,c,g,this.vga256_palette[this.dac_mask&this.dac_map[e>>4&15]],this.vga256_palette[this.dac_mask&this.dac_map[e&15]]]);this.vga_memory[a]=b};
VGAScreen.prototype.update_cursor=function(){var a=(this.cursor_address-this.start_address)/this.max_cols|0,b=(this.cursor_address-this.start_address)%this.max_cols;a=Math.min(this.max_rows-1,a);this.bus.send("screen-update-cursor",[a,b])};VGAScreen.prototype.complete_redraw=function(){dbg_log("complete redraw",LOG_VGA);this.graphical_mode?this.svga_enabled?this.cpu.svga_mark_dirty():(this.diff_addr_min=0,this.diff_addr_max=VGA_PIXEL_BUFFER_SIZE):this.text_mode_redraw()};
VGAScreen.prototype.complete_replot=function(){dbg_log("complete replot",LOG_VGA);this.graphical_mode&&!this.svga_enabled&&(this.diff_plot_min=0,this.diff_plot_max=VGA_PIXEL_BUFFER_SIZE,this.complete_redraw())};VGAScreen.prototype.partial_redraw=function(a,b){a<this.diff_addr_min&&(this.diff_addr_min=a);b>this.diff_addr_max&&(this.diff_addr_max=b)};
VGAScreen.prototype.partial_replot=function(a,b){a<this.diff_plot_min&&(this.diff_plot_min=a);b>this.diff_plot_max&&(this.diff_plot_max=b);this.partial_redraw(a,b)};VGAScreen.prototype.reset_diffs=function(){this.diff_addr_min=this.vga_memory_size;this.diff_addr_max=0;this.diff_plot_min=this.vga_memory_size;this.diff_plot_max=0};VGAScreen.prototype.destroy=function(){};
@@ -269,10 +287,10 @@ VGAScreen.prototype.port3C1_read=function(){if(16>this.attribute_controller_inde
LOG_VGA),this.color_plane_enable;case 19:return dbg_log("3C1 / horizontal panning read: "+h(this.horizontal_panning),LOG_VGA),this.horizontal_panning;case 20:return dbg_log("3C1 / color select read: "+h(this.color_select),LOG_VGA),this.color_select;default:dbg_log("3C1 / attribute controller read "+h(this.attribute_controller_index),LOG_VGA)}return 255};VGAScreen.prototype.port3C2_write=function(a){dbg_log("3C2 / miscellaneous output register = "+h(a),LOG_VGA);this.miscellaneous_output_register=a};
VGAScreen.prototype.port3C4_write=function(a){this.sequencer_index=a};VGAScreen.prototype.port3C4_read=function(){return this.sequencer_index};
VGAScreen.prototype.port3C5_write=function(a){switch(this.sequencer_index){case 1:dbg_log("clocking mode: "+h(a),LOG_VGA);var b=this.clocking_mode;this.clocking_mode=a;(b^a)&32&&this.update_layers();break;case 2:dbg_log("plane write mask: "+h(a),LOG_VGA);this.plane_write_bm=a;break;case 4:dbg_log("sequencer memory mode: "+h(a),LOG_VGA);this.sequencer_memory_mode=a;break;default:dbg_log("3C5 / sequencer write "+h(this.sequencer_index)+": "+h(a),LOG_VGA)}};
VGAScreen.prototype.port3C5_read=function(){dbg_log("3C5 / sequencer read "+h(this.sequencer_index),LOG_VGA);switch(this.sequencer_index){case 1:return this.clocking_mode;case 2:return this.plane_write_bm;case 4:return this.sequencer_memory_mode;case 6:return 18}return 0};VGAScreen.prototype.port3C7_write=function(a){dbg_log("3C7 write: "+h(a),LOG_VGA);this.dac_color_index_read=3*a;this.dac_state&=0};VGAScreen.prototype.port3C7_read=function(){return this.dac_state};
VGAScreen.prototype.port3C8_write=function(a){this.dac_color_index_write=3*a;this.dac_state|=3};VGAScreen.prototype.port3C8_read=function(){return this.dac_color_index_write/3&255};
VGAScreen.prototype.port3C9_write=function(a){var b=this.dac_color_index_write/3|0,c=this.dac_color_index_write%3,d=this.vga256_palette[b];a=255*(a&63)/63|0;0===c?d=d&-16711681|a<<16:1===c?d=d&-65281|a<<8:(d=d&-256|a,dbg_log("dac set color, index="+h(b)+" value="+h(d),LOG_VGA));this.vga256_palette[b]!==d&&(this.vga256_palette[b]=d,this.complete_redraw());this.dac_color_index_write++};
VGAScreen.prototype.port3C9_read=function(){dbg_log("3C9 read",LOG_VGA);var a=this.dac_color_index_read%3,b=this.vga256_palette[this.dac_color_index_read/3|0];this.dac_color_index_read++;return(b>>8*(2-a)&255)/255*63|0};VGAScreen.prototype.port3CC_read=function(){dbg_log("3CC read",LOG_VGA);return this.miscellaneous_output_register};VGAScreen.prototype.port3CE_write=function(a){this.graphics_index=a};VGAScreen.prototype.port3CE_read=function(){return this.graphics_index};
VGAScreen.prototype.port3C5_read=function(){dbg_log("3C5 / sequencer read "+h(this.sequencer_index),LOG_VGA);switch(this.sequencer_index){case 1:return this.clocking_mode;case 2:return this.plane_write_bm;case 4:return this.sequencer_memory_mode;case 6:return 18}return 0};VGAScreen.prototype.port3C6_write=function(a){this.dac_mask=a};VGAScreen.prototype.port3C6_read=function(){return this.dac_mask};
VGAScreen.prototype.port3C7_write=function(a){dbg_log("3C7 write: "+h(a),LOG_VGA);this.dac_color_index_read=3*a;this.dac_state&=0};VGAScreen.prototype.port3C7_read=function(){return this.dac_state};VGAScreen.prototype.port3C8_write=function(a){this.dac_color_index_write=3*a;this.dac_state|=3};VGAScreen.prototype.port3C8_read=function(){return this.dac_color_index_write/3&255};
VGAScreen.prototype.port3C9_write=function(a){var b=this.dac_color_index_write/3|0,c=this.dac_color_index_write%3,d=this.vga256_palette[b];if(0===(this.dispi_enable_value&32)){a&=63;const e=a&1;a=a<<2|e<<1|e}0===c?d=d&-16711681|a<<16:1===c?d=d&-65281|a<<8:(d=d&-256|a,dbg_log("dac set color, index="+h(b)+" value="+h(d),LOG_VGA));this.vga256_palette[b]!==d&&(this.vga256_palette[b]=d,this.complete_redraw());this.dac_color_index_write++};
VGAScreen.prototype.port3C9_read=function(){dbg_log("3C9 read",LOG_VGA);var a=this.vga256_palette[this.dac_color_index_read/3|0]>>8*(2-this.dac_color_index_read%3)&255;this.dac_color_index_read++;return this.dispi_enable_value&32?a:a>>2};VGAScreen.prototype.port3CC_read=function(){dbg_log("3CC read",LOG_VGA);return this.miscellaneous_output_register};VGAScreen.prototype.port3CE_write=function(a){this.graphics_index=a};VGAScreen.prototype.port3CE_read=function(){return this.graphics_index};
VGAScreen.prototype.port3CF_write=function(a){switch(this.graphics_index){case 0:this.planar_setreset=a;dbg_log("plane set/reset: "+h(a),LOG_VGA);break;case 1:this.planar_setreset_enable=a;dbg_log("plane set/reset enable: "+h(a),LOG_VGA);break;case 2:this.color_compare=a;dbg_log("color compare: "+h(a),LOG_VGA);break;case 3:this.planar_rotate_reg=a;dbg_log("plane rotate: "+h(a),LOG_VGA);break;case 4:this.plane_read=a;dbg_log("plane read: "+h(a),LOG_VGA);break;case 5:var b=this.planar_mode;this.planar_mode=
a;dbg_log("planar mode: "+h(a),LOG_VGA);(b^a)&96&&this.complete_replot();break;case 6:dbg_log("miscellaneous graphics register: "+h(a),LOG_VGA);this.miscellaneous_graphics_register!==a&&(this.miscellaneous_graphics_register=a,this.update_vga_size());break;case 7:this.color_dont_care=a;dbg_log("color don't care: "+h(a),LOG_VGA);break;case 8:this.planar_bitmap=a;dbg_log("planar bitmap: "+h(a),LOG_VGA);break;default:dbg_log("3CF / graphics write "+h(this.graphics_index)+": "+h(a),LOG_VGA)}};
VGAScreen.prototype.port3CF_read=function(){dbg_log("3CF / graphics read "+h(this.graphics_index),LOG_VGA);switch(this.graphics_index){case 0:return this.planar_setreset;case 1:return this.planar_setreset_enable;case 2:return this.color_compare;case 3:return this.planar_rotate_reg;case 4:return this.plane_read;case 5:return this.planar_mode;case 6:return this.miscellaneous_graphics_register;case 7:return this.color_dont_care;case 8:return this.planar_bitmap}return 0};
@@ -288,38 +306,40 @@ VGAScreen.prototype.port3D5_read=function(){dbg_log("3D5 read "+h(this.index_crt
case 12:return this.start_address&255;case 13:return this.start_address>>8;case 14:return this.cursor_address>>8;case 15:return this.cursor_address&255;case 18:return this.vertical_display_enable_end&255;case 19:return this.offset_register;case 20:return this.underline_location_register;case 21:return this.vertical_blank_start&255;case 23:return this.crtc_mode;case 24:return this.line_compare&255}return this.index_crtc<this.crtc.length?this.crtc[this.index_crtc]:0};
VGAScreen.prototype.port3DA_read=function(){dbg_log("3DA read - status 1 and clear attr index",LOG_VGA);var a=this.port_3DA_value;this.graphical_mode?(this.port_3DA_value^=1,this.port_3DA_value&=1):(this.port_3DA_value&1&&(this.port_3DA_value^=8),this.port_3DA_value^=1);this.attribute_controller_index=-1;return a};VGAScreen.prototype.port1CE_write=function(a){this.dispi_index=a};
VGAScreen.prototype.port1CF_write=function(a){dbg_log("1CF / dispi write "+h(this.dispi_index)+": "+h(a),LOG_VGA);switch(this.dispi_index){case 1:this.svga_width=a;this.svga_width>MAX_XRES&&(dbg_log("svga_width reduced from "+this.svga_width+" to "+MAX_XRES,LOG_VGA),this.svga_width=MAX_XRES);break;case 2:this.svga_height=a;this.svga_height>MAX_YRES&&(dbg_log("svga_height reduced from "+this.svga_height+" to "+MAX_YRES,LOG_VGA),this.svga_height=MAX_YRES);break;case 3:this.svga_bpp=a;break;case 4:this.svga_enabled=
1===(a&1);this.dispi_enable_value=a;break;case 5:dbg_log("SVGA bank offset: "+h(a<<16));this.svga_bank_offset=a<<16;break;case 9:const b=a*this.svga_width;dbg_log("SVGA offset: "+h(b)+" y="+h(a),LOG_VGA);this.svga_offset!==b&&(this.svga_offset=b,this.complete_redraw())}!this.svga_enabled||this.svga_width&&this.svga_height||(dbg_log("SVGA: disabled because of invalid width/height: "+this.svga_width+"x"+this.svga_height,LOG_VGA),this.svga_enabled=!1);dbg_assert(4!==this.svga_bpp,"unimplemented svga bpp: 4");
1===(a&1);this.dispi_enable_value=a;break;case 5:dbg_log("SVGA bank offset: "+h(a<<16),LOG_VGA);this.svga_bank_offset=a<<16;break;case 9:const b=a*this.svga_width;dbg_log("SVGA offset: "+h(b)+" y="+h(a),LOG_VGA);this.svga_offset!==b&&(this.svga_offset=b,this.complete_redraw())}!this.svga_enabled||this.svga_width&&this.svga_height||(dbg_log("SVGA: disabled because of invalid width/height: "+this.svga_width+"x"+this.svga_height,LOG_VGA),this.svga_enabled=!1);dbg_assert(4!==this.svga_bpp,"unimplemented svga bpp: 4");
dbg_assert(4===this.svga_bpp||8===this.svga_bpp||15===this.svga_bpp||16===this.svga_bpp||24===this.svga_bpp||32===this.svga_bpp,"unexpected svga bpp: "+this.svga_bpp);dbg_log("SVGA: enabled="+this.svga_enabled+", "+this.svga_width+"x"+this.svga_height+"x"+this.svga_bpp,LOG_VGA);this.svga_enabled&&4===this.dispi_index&&(this.set_size_graphical(this.svga_width,this.svga_height,this.svga_bpp,this.svga_width,this.svga_height),this.bus.send("screen-set-mode",!0),this.graphical_mode_is_linear=this.graphical_mode=
!0);this.svga_enabled||(this.svga_bank_offset=0);this.update_layers()};VGAScreen.prototype.port1CF_read=function(){dbg_log("1CF / dispi read "+h(this.dispi_index),LOG_VGA);return this.svga_register_read(this.dispi_index)};
VGAScreen.prototype.svga_register_read=function(a){switch(a){case 0:return 45248;case 1:return this.dispi_enable_value&2?MAX_XRES:this.svga_width;case 2:return this.dispi_enable_value&2?MAX_YRES:this.svga_height;case 3:return this.dispi_enable_value&2?MAX_BPP:this.svga_bpp;case 4:return this.dispi_enable_value;case 5:return this.svga_bank_offset>>>16;case 6:return this.screen_width?this.screen_width:1;case 8:return 0;case 10:return this.vga_memory_size/VGA_BANK_SIZE|0}return 255};
VGAScreen.prototype.vga_replot=function(){for(var a=this.diff_plot_min&-16,b=Math.min(this.diff_plot_max|15,VGA_PIXEL_BUFFER_SIZE-1),c=this.vga_addr_shift_count(),d=~this.crtc_mode&3,e=this.planar_mode&96,g=this.attribute_mode&64;a<=b;){var f=a>>>c;if(d){var k=a/this.virtual_width|0,m=a-this.virtual_width*k;switch(d){case 1:f=(k&1)<<13;k>>>=1;break;case 2:f=(k&1)<<14;k>>>=1;break;case 3:f=(k&3)<<13,k>>>=2}f|=(k*this.virtual_width+m>>>c)+this.start_address}k=this.plane0[f];m=this.plane1[f];var l=this.plane2[f],
n=this.plane3[f];f=new Uint8Array(8);switch(e){case 0:k<<=0;m<<=1;l<<=2;n<<=3;for(var p=7;0<=p;p--)f[7-p]=k>>p&1|m>>p&2|l>>p&4|n>>p&8;break;case 32:f[0]=k>>6&3|l>>4&12;f[1]=k>>4&3|l>>2&12;f[2]=k>>2&3|l>>0&12;f[3]=k>>0&3|l<<2&12;f[4]=m>>6&3|n>>4&12;f[5]=m>>4&3|n>>2&12;f[6]=m>>2&3|n>>0&12;f[7]=m>>0&3|n<<2&12;break;case 64:case 96:f[0]=k>>4&15,f[1]=k>>0&15,f[2]=m>>4&15,f[3]=m>>0&15,f[4]=l>>4&15,f[5]=l>>0&15,f[6]=n>>4&15,f[7]=n>>0&15}if(g)for(k=p=0;4>p;p++,a++,k+=2)this.pixel_buffer[a]=f[k]<<4|f[k+1];
else for(p=0;8>p;p++,a++)this.pixel_buffer[a]=f[p]}};
n=this.plane3[f];f=new Uint8Array(8);switch(e){case 0:k<<=0;m<<=1;l<<=2;n<<=3;for(var q=7;0<=q;q--)f[7-q]=k>>q&1|m>>q&2|l>>q&4|n>>q&8;break;case 32:f[0]=k>>6&3|l>>4&12;f[1]=k>>4&3|l>>2&12;f[2]=k>>2&3|l>>0&12;f[3]=k>>0&3|l<<2&12;f[4]=m>>6&3|n>>4&12;f[5]=m>>4&3|n>>2&12;f[6]=m>>2&3|n>>0&12;f[7]=m>>0&3|n<<2&12;break;case 64:case 96:f[0]=k>>4&15,f[1]=k>>0&15,f[2]=m>>4&15,f[3]=m>>0&15,f[4]=l>>4&15,f[5]=l>>0&15,f[6]=n>>4&15,f[7]=n>>0&15}if(g)for(k=q=0;4>q;q++,a++,k+=2)this.pixel_buffer[a]=f[k]<<4|f[k+1];
else for(q=0;8>q;q++,a++)this.pixel_buffer[a]=f[q]}};
VGAScreen.prototype.vga_redraw=function(){var a=this.diff_addr_min,b=Math.min(this.diff_addr_max,VGA_PIXEL_BUFFER_SIZE-1);const c=new Int32Array(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,this.virtual_width*this.virtual_height);var d=255,e=0;this.attribute_mode&128&&(d&=207,e|=this.color_select<<4&48);if(this.attribute_mode&64)for(;a<=b;a++){var g=this.pixel_buffer[a]&d|e;g=this.vga256_palette[g];c[a]=g&65280|g<<16|g>>16|4278190080}else for(d&=63,e|=this.color_select<<4&192;a<=b;a++)g=this.dac_map[this.pixel_buffer[a]&
this.color_plane_enable]&d|e,g=this.vga256_palette[g],c[a]=g&65280|g<<16|g>>16|4278190080};
VGAScreen.prototype.screen_fill_buffer=function(){if(this.graphical_mode){if(0===this.image_data.data.byteLength){var a=new Uint8ClampedArray(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,4*this.virtual_width*this.virtual_height);this.image_data=new ImageData(a,this.virtual_width,this.virtual_height);this.update_layers()}if(this.svga_enabled){a=0;let d=this.svga_height;if(8===this.svga_bpp){const e=new Int32Array(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,this.screen_width*this.screen_height),
g=new Uint8Array(this.cpu.wasm_memory.buffer,this.svga_memory.byteOffset,this.vga_memory_size);for(var b=0;b<e.length;b++){var c=this.vga256_palette[g[b]];e[b]=c&65280|c<<16|c>>16|4278190080}}else this.cpu.svga_fill_pixel_buffer(this.svga_bpp,this.svga_offset),b=15===this.svga_bpp?2:this.svga_bpp/8,a=((this.cpu.svga_dirty_bitmap_min_offset[0]/b|0)-this.svga_offset)/this.svga_width|0,d=(((this.cpu.svga_dirty_bitmap_max_offset[0]/b|0)-this.svga_offset)/this.svga_width|0)+1;a<d&&(a=Math.max(a,0),d=Math.min(d,
this.svga_height),this.bus.send("screen-fill-buffer-end",[{image_data:this.image_data,screen_x:0,screen_y:a,buffer_x:0,buffer_y:a,buffer_width:this.svga_width,buffer_height:d-a}]))}else this.vga_replot(),this.vga_redraw(),this.bus.send("screen-fill-buffer-end",this.layers);this.reset_diffs()}this.update_vertical_retrace()};let PS2_LOG_VERBOSE=!1;
function PS2(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=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 ByteQueue(1024);this.last_port60_byte=0;this.sample_rate=100;this.resolution=4;this.scaling2=!1;this.last_mouse_packet=-1;this.mouse_buffer=
new ByteQueue(1024);this.next_byte_is_aux=this.next_byte_is_ready=!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);this.command_register=5;this.controller_output_port=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)}
function PS2(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=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 ByteQueue(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 ByteQueue(1024);this.next_byte_is_aux=this.next_byte_is_ready=!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=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)}
PS2.prototype.get_state=function(){var a=[];a[0]=this.enable_mouse_stream;a[1]=this.use_mouse;a[2]=this.have_mouse;a[3]=this.mouse_delta_x;a[4]=this.mouse_delta_y;a[5]=this.mouse_clicks;a[6]=this.have_keyboard;a[7]=this.enable_keyboard_stream;a[8]=this.next_is_mouse_command;a[9]=this.next_read_sample;a[10]=this.next_read_led;a[11]=this.next_handle_scan_code_set;a[12]=this.next_read_rate;a[13]=this.next_read_resolution;a[15]=this.last_port60_byte;a[16]=this.sample_rate;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;return a};
this.scaling2;a[20]=this.command_register;a[21]=this.read_output_register;a[22]=this.read_command_register;a[23]=this.controller_output_port;a[24]=this.read_controller_output_port;a[25]=this.mouse_id;a[26]=this.mouse_detect_state;a[27]=this.mouse_reset_workaround;return a};
PS2.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=
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.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)};PS2.prototype.raise_irq=function(){this.next_byte_is_ready||(this.kbd_buffer.length?this.kbd_irq():this.mouse_buffer.length&&this.mouse_irq())};
PS2.prototype.mouse_irq=function(){this.next_byte_is_aux=this.next_byte_is_ready=!0;this.command_register&2&&(dbg_log("Mouse irq",LOG_PS2),this.cpu.device_lower_irq(12),this.cpu.device_raise_irq(12))};PS2.prototype.kbd_irq=function(){this.next_byte_is_ready=!0;this.next_byte_is_aux=!1;this.command_register&1&&(dbg_log("Keyboard irq",LOG_PS2),this.cpu.device_lower_irq(1),this.cpu.device_raise_irq(1))};
PS2.prototype.kbd_send_code=function(a){this.enable_keyboard_stream&&(dbg_log("adding kbd code: "+h(a),LOG_PS2),this.kbd_buffer.push(a),this.raise_irq())};PS2.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))}};
PS2.prototype.mouse_send_click=function(a,b,c){this.have_mouse&&this.use_mouse&&(this.mouse_clicks=a|c<<1|b<<2,this.enable_mouse_stream&&this.send_mouse_packet(0,0))};PS2.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);PS2_LOG_VERBOSE&&dbg_log("adding mouse packets: "+[c,a,b],LOG_PS2);this.raise_irq()};
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)};
PS2.prototype.raise_irq=function(){this.next_byte_is_ready||(this.kbd_buffer.length?this.kbd_irq():this.mouse_buffer.length&&this.mouse_irq())};PS2.prototype.mouse_irq=function(){this.next_byte_is_aux=this.next_byte_is_ready=!0;this.command_register&2&&(dbg_log("Mouse irq",LOG_PS2),this.cpu.device_lower_irq(12),this.cpu.device_raise_irq(12))};
PS2.prototype.kbd_irq=function(){this.next_byte_is_ready=!0;this.next_byte_is_aux=!1;this.command_register&1&&(dbg_log("Keyboard irq",LOG_PS2),this.cpu.device_lower_irq(1),this.cpu.device_raise_irq(1))};PS2.prototype.kbd_send_code=function(a){this.enable_keyboard_stream&&(dbg_log("adding kbd code: "+h(a),LOG_PS2),this.kbd_buffer.push(a),this.raise_irq())};
PS2.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))}};PS2.prototype.mouse_send_click=function(a,b,c){this.have_mouse&&this.use_mouse&&(this.mouse_clicks=a|c<<1|b<<2,this.enable_mouse_stream&&this.send_mouse_packet(0,0))};
PS2.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);PS2_LOG_VERBOSE&&dbg_log("adding mouse packets: "+[c,a,b],LOG_PS2);this.raise_irq()};
PS2.prototype.apply_scaling2=function(a){var b=a>>31;switch(Math.abs(a)){case 0:case 1:case 3:return a;case 2:return b;case 4:return 6*b;case 5:return 9*b;default:return a<<1}};
PS2.prototype.port60_read=function(){this.next_byte_is_ready=!1;if(!this.kbd_buffer.length&&!this.mouse_buffer.length)return dbg_log("Port 60 read: Empty",LOG_PS2),this.last_port60_byte;this.next_byte_is_aux?(this.cpu.device_lower_irq(12),this.last_port60_byte=this.mouse_buffer.shift(),dbg_log("Port 60 read (mouse): "+h(this.last_port60_byte),LOG_PS2)):(this.cpu.device_lower_irq(1),this.last_port60_byte=this.kbd_buffer.shift(),dbg_log("Port 60 read (kbd) : "+h(this.last_port60_byte),LOG_PS2));(this.kbd_buffer.length||
this.mouse_buffer.length)&&this.raise_irq();return this.last_port60_byte};PS2.prototype.port64_read=function(){var a=16;this.next_byte_is_ready&&(a|=1);this.next_byte_is_aux&&(a|=32);dbg_log("port 64 read: "+h(a),LOG_PS2);return a};
PS2.prototype.port60_write=function(a){dbg_log("port 60 write: "+h(a),LOG_PS2);if(this.read_command_register)this.command_register=a,this.read_command_register=!1,dbg_log("Keyboard command register = "+h(this.command_register),LOG_PS2);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,dbg_log("mouse sample rate: "+
h(a),LOG_PS2),this.sample_rate||(dbg_log("invalid sample rate, reset to 100",LOG_PS2),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,dbg_log("invalid resolution, resetting to 4",LOG_PS2)):(this.resolution=1<<a,dbg_log("resolution: "+this.resolution,LOG_PS2)),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(2);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,dbg_log("Port 60 data register write: "+h(a),LOG_PS2),this.have_mouse){this.kbd_buffer.clear();this.mouse_buffer.clear();this.mouse_buffer.push(250);switch(a){case 230:dbg_log("Scaling 1:1",LOG_PS2);this.scaling2=!1;break;case 231:dbg_log("Scaling 2:1",LOG_PS2);this.scaling2=
!0;break;case 232:this.next_read_resolution=!0;break;case 233:this.send_mouse_packet(0,0);break;case 235:dbg_log("unimplemented request single packet",LOG_PS2);this.send_mouse_packet(0,0);break;case 242:this.mouse_buffer.push(0);this.mouse_buffer.push(0);this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0;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:dbg_log("Mouse reset",LOG_PS2);this.mouse_buffer.push(170);this.mouse_buffer.push(0);this.use_mouse=!0;this.bus.send("mouse-enable",!0);this.enable_mouse_stream=!1;this.sample_rate=100;this.scaling2=!1;this.resolution=4;this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0;break;default:dbg_log("Unimplemented mouse command: "+h(a),LOG_PS2)}this.mouse_irq()}}else if(this.read_controller_output_port)this.read_controller_output_port=
!1,this.controller_output_port=a;else{dbg_log("Port 60 data register write: "+h(a),LOG_PS2);this.mouse_buffer.clear();this.kbd_buffer.clear();this.kbd_buffer.push(250);switch(a){case 237:this.next_read_led=!0;break;case 240:this.next_handle_scan_code_set=!0;break;case 242:this.kbd_buffer.push(171);this.kbd_buffer.push(83);break;case 243:this.next_read_rate=!0;break;case 244:dbg_log("kbd enable scanning",LOG_PS2);this.enable_keyboard_stream=!0;break;case 245:dbg_log("kbd disable scanning",LOG_PS2);
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:dbg_log("Unimplemented keyboard command: "+h(a),LOG_PS2)}this.kbd_irq()}};
PS2.prototype.port60_write=function(a){dbg_log("port 60 write: "+h(a),LOG_PS2);if(this.read_command_register)this.command_register=a,this.read_command_register=!1,dbg_log("Keyboard command register = "+h(this.command_register),LOG_PS2);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=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}dbg_log("mouse sample rate: "+h(a)+", mouse id: "+h(this.mouse_id),LOG_PS2);this.sample_rate||(dbg_log("invalid sample rate, reset to 100",
LOG_PS2),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,dbg_log("invalid resolution, resetting to 4",LOG_PS2)):(this.resolution=1<<a,dbg_log("resolution: "+this.resolution,LOG_PS2)),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(2);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,dbg_log("Port 60 data register write: "+h(a),LOG_PS2),this.have_mouse){this.kbd_buffer.clear();this.mouse_buffer.clear();this.mouse_buffer.push(250);switch(a){case 230:dbg_log("Scaling 1:1",LOG_PS2);this.scaling2=!1;break;case 231:dbg_log("Scaling 2:1",LOG_PS2);this.scaling2=!0;break;case 232:this.next_read_resolution=
!0;break;case 233:this.send_mouse_packet(0,0);break;case 235:dbg_log("unimplemented request single packet",LOG_PS2);this.send_mouse_packet(0,0);break;case 242:dbg_log("required id: "+h(this.mouse_id),LOG_PS2);this.mouse_buffer.push(this.mouse_id);this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0;this.raise_irq();break;case 243:this.next_read_sample=!0;break;case 244:this.use_mouse=this.enable_mouse_stream=!0;this.bus.send("mouse-enable",!0);this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=
0;break;case 245:this.enable_mouse_stream=!1;break;case 246:this.enable_mouse_stream=!1;this.sample_rate=100;this.scaling2=!1;this.resolution=4;break;case 255:dbg_log("Mouse reset",LOG_PS2);this.mouse_buffer.push(170);this.mouse_buffer.push(0);this.use_mouse=!0;this.bus.send("mouse-enable",!0);this.enable_mouse_stream=!1;this.sample_rate=100;this.scaling2=!1;this.resolution=4;this.mouse_reset_workaround||(this.mouse_id=0);this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0;break;default:dbg_log("Unimplemented mouse command: "+
h(a),LOG_PS2)}this.mouse_irq()}}else if(this.read_controller_output_port)this.read_controller_output_port=!1,this.controller_output_port=a;else{dbg_log("Port 60 data register write: "+h(a),LOG_PS2);this.mouse_buffer.clear();this.kbd_buffer.clear();this.kbd_buffer.push(250);switch(a){case 237:this.next_read_led=!0;break;case 240:this.next_handle_scan_code_set=!0;break;case 242:this.kbd_buffer.push(171);this.kbd_buffer.push(83);break;case 243:this.next_read_rate=!0;break;case 244:dbg_log("kbd enable scanning",
LOG_PS2);this.enable_keyboard_stream=!0;break;case 245:dbg_log("kbd disable scanning",LOG_PS2);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:dbg_log("Unimplemented keyboard command: "+h(a),LOG_PS2)}this.kbd_irq()}};
PS2.prototype.port64_write=function(a){dbg_log("port 64 write: "+h(a),LOG_PS2);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:dbg_log("Disable second port",LOG_PS2);this.command_register|=32;break;case 168:dbg_log("Enable second port",
LOG_PS2);this.command_register&=-33;break;case 169:this.kbd_buffer.clear();this.mouse_buffer.clear();this.kbd_buffer.push(0);this.kbd_irq();break;case 170:this.kbd_buffer.clear();this.mouse_buffer.clear();this.kbd_buffer.push(85);this.kbd_irq();break;case 171:this.kbd_buffer.clear();this.mouse_buffer.clear();this.kbd_buffer.push(0);this.kbd_irq();break;case 173:dbg_log("Disable Keyboard",LOG_PS2);this.command_register|=16;break;case 174:dbg_log("Enable Keyboard",LOG_PS2);this.command_register&=-17;
break;case 254:dbg_log("CPU reboot via PS2");this.cpu.reboot_internal();break;default:dbg_log("port 64: Unimplemented command byte: "+h(a),LOG_PS2)}};var PIC_LOG_VERBOSE=!1;
@@ -344,25 +364,26 @@ RTC.prototype.set_state=function(a){this.cmos_index=a[0];this.cmos_data=a[1];thi
RTC.prototype.timer=function(a,b){a=Date.now();this.rtc_time+=a-this.last_update;this.last_update=a;this.periodic_interrupt&&this.next_interrupt<a?(this.cpu.device_raise_irq(8),this.cmos_c|=192,this.next_interrupt+=this.periodic_interrupt_time*Math.ceil((a-this.next_interrupt)/this.periodic_interrupt_time)):this.next_interrupt_alarm&&this.next_interrupt_alarm<a&&(this.cpu.device_raise_irq(8),this.cmos_c|=160,this.next_interrupt_alarm=0);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)));return b};RTC.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};RTC.prototype.bcd_unpack=function(a){const b=a&15,c=a>>4&15;dbg_assert(256>a);dbg_assert(10>b);dbg_assert(10>c);return b+10*c};RTC.prototype.encode_time=function(a){return this.cmos_b&4?a:this.bcd_pack(a)};RTC.prototype.decode_time=function(a){return this.cmos_b&4?a:this.bcd_unpack(a)};
RTC.prototype.cmos_port_read=function(){var a=this.cmos_index;switch(a){case CMOS_RTC_SECONDS:return this.encode_time((new Date(this.rtc_time)).getUTCSeconds());case CMOS_RTC_MINUTES:return this.encode_time((new Date(this.rtc_time)).getUTCMinutes());case CMOS_RTC_HOURS:return this.encode_time((new Date(this.rtc_time)).getUTCHours());case CMOS_RTC_DAY_MONTH:return this.encode_time((new Date(this.rtc_time)).getUTCDate());case CMOS_RTC_MONTH:return this.encode_time((new Date(this.rtc_time)).getUTCMonth()+
1);case CMOS_RTC_YEAR:return this.encode_time((new Date(this.rtc_time)).getUTCFullYear()%100);case CMOS_STATUS_A:return this.cmos_a;case CMOS_STATUS_B:return this.cmos_b;case CMOS_STATUS_C:return this.cpu.device_lower_irq(8),dbg_log("cmos reg C read",LOG_RTC),a=this.cmos_c,this.cmos_c&=-241,a;case CMOS_STATUS_D:return 0;case CMOS_CENTURY:return this.encode_time((new Date(this.rtc_time)).getUTCFullYear()/100|0);default:return dbg_log("cmos read from index "+h(a),LOG_RTC),this.cmos_data[this.cmos_index]}};
1);case CMOS_RTC_YEAR:return this.encode_time((new Date(this.rtc_time)).getUTCFullYear()%100);case CMOS_STATUS_A:return 999<=v86.microtick()%1E3?this.cmos_a|128:this.cmos_a;case CMOS_STATUS_B:return this.cmos_b;case CMOS_STATUS_C:return this.cpu.device_lower_irq(8),dbg_log("cmos reg C read",LOG_RTC),a=this.cmos_c,this.cmos_c&=-241,a;case CMOS_STATUS_D:return 0;case CMOS_CENTURY:return this.encode_time((new Date(this.rtc_time)).getUTCFullYear()/100|0);default:return dbg_log("cmos read from index "+
h(a),LOG_RTC),this.cmos_data[this.cmos_index]}};
RTC.prototype.cmos_port_write=function(a){switch(this.cmos_index){case 10:this.cmos_a=a&127;this.periodic_interrupt_time=1E3/(32768>>(this.cmos_a&15)-1);dbg_log("Periodic interrupt, a="+h(this.cmos_a,2)+" t="+this.periodic_interrupt_time,LOG_RTC);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[CMOS_RTC_SECONDS_ALARM]),c=this.decode_time(this.cmos_data[CMOS_RTC_MINUTES_ALARM]),d=this.decode_time(this.cmos_data[CMOS_RTC_HOURS_ALARM]),
e=new Date(Date.UTC(a.getUTCFullYear(),a.getUTCMonth(),a.getUTCDate(),d,c,b));dbg_log("RTC alarm scheduled for "+e+" hh:mm:ss="+d+":"+c+":"+b+" ms_from_now="+(e-a),LOG_RTC);this.next_interrupt_alarm=+e}this.cmos_b&16&&dbg_log("Unimplemented: updated interrupt",LOG_RTC);dbg_log("cmos b="+h(this.cmos_b,2),LOG_RTC);break;case CMOS_RTC_SECONDS_ALARM:case CMOS_RTC_MINUTES_ALARM:case CMOS_RTC_HOURS_ALARM:this.cmos_write(this.cmos_index,a);break;default:dbg_log("cmos write index "+h(this.cmos_index)+": "+
h(a),LOG_RTC)}this.periodic_interrupt=64===(this.cmos_b&64)&&0<(this.cmos_a&15)};RTC.prototype.cmos_read=function(a){dbg_assert(128>a);return this.cmos_data[a]};RTC.prototype.cmos_write=function(a,b){dbg_log("cmos "+h(a)+" <- "+h(b),LOG_RTC);dbg_assert(128>a);this.cmos_data[a]=b};var DLAB=128,UART_IER_MSI=8,UART_IER_THRI=2,UART_IER_RDI=1,UART_IIR_MSI=0,UART_IIR_NO_INT=1,UART_IIR_THRI=2,UART_IIR_RDI=4,UART_IIR_RLSI=6,UART_IIR_CTI=12,UART_LSR_DATA_READY=1,UART_LSR_TX_EMPTY=32,UART_LSR_TRANSMITTER_EMPTY=64;
function UART(a,b,c){this.bus=c;this.cpu=a;this.ints=1<<UART_IIR_THRI;this.line_control=this.baud_rate=0;this.lsr=UART_LSR_TRANSMITTER_EMPTY|UART_LSR_TX_EMPTY;this.ier=this.fifo_control=0;this.iir=UART_IIR_NO_INT;this.irq=this.scratch_register=this.modem_status=this.modem_control=0;this.input=new ByteQueue(4096);this.current_line=[];switch(b){case 1016:this.com=0;this.irq=4;break;case 760:this.com=1;this.irq=3;break;case 1E3:this.com=2;this.irq=4;break;case 744:this.irq=this.com=3;break;default:dbg_log("Invalid serial port: "+
h(b),LOG_SERIAL),this.com=0,this.irq=4}this.bus.register("serial"+this.com+"-input",function(d){this.data_received(d)},this);a=a.io;a.register_write(b,this,function(d){this.write_data(d)},function(d){this.write_data(d&255);this.write_data(d>>8)});a.register_write(b|1,this,function(d){this.line_control&DLAB?(this.baud_rate=this.baud_rate&255|d<<8,dbg_log("baud rate: "+h(this.baud_rate),LOG_SERIAL)):(this.ier=d&15,dbg_log("interrupt enable: "+h(d),LOG_SERIAL),this.CheckInterrupt())});a.register_read(b,
this,function(){if(this.line_control&DLAB)return this.baud_rate&255;var d=this.input.shift();-1===d?dbg_log("Read input empty",LOG_SERIAL):dbg_log("Read input: "+h(d),LOG_SERIAL);0===this.input.length&&(this.lsr&=~UART_LSR_DATA_READY,this.ClearInterrupt(UART_IIR_CTI));return d});a.register_read(b|1,this,function(){return this.line_control&DLAB?this.baud_rate>>8:this.ier&15});a.register_read(b|2,this,function(){var d=this.iir&15|192;dbg_log("read interrupt identification: "+h(this.iir),LOG_SERIAL);
this.iir==UART_IIR_THRI&&this.ClearInterrupt(UART_IIR_THRI);return d});a.register_write(b|2,this,function(d){dbg_log("fifo control: "+h(d),LOG_SERIAL);this.fifo_control=d});a.register_read(b|3,this,function(){dbg_log("read line control: "+h(this.line_control),LOG_SERIAL);return this.line_control});a.register_write(b|3,this,function(d){dbg_log("line control: "+h(d),LOG_SERIAL);this.line_control=d});a.register_read(b|4,this,function(){return this.modem_control});a.register_write(b|4,this,function(d){dbg_log("modem control: "+
h(d),LOG_SERIAL);this.modem_control=d});a.register_read(b|5,this,function(){dbg_log("read line status: "+h(this.lsr),LOG_SERIAL);return this.lsr});a.register_write(b|5,this,function(d){dbg_log("Factory test write",LOG_SERIAL)});a.register_read(b|6,this,function(){dbg_log("read modem status: "+h(this.modem_status),LOG_SERIAL);return this.modem_status});a.register_write(b|6,this,function(d){dbg_log("Unkown register write (base+6)",LOG_SERIAL)});a.register_read(b|7,this,function(){return this.scratch_register});
a.register_write(b|7,this,function(d){this.scratch_register=d})}UART.prototype.get_state=function(){var a=[];a[0]=this.ints;a[1]=this.baud_rate;a[2]=this.line_control;a[3]=this.lsr;a[4]=this.fifo_control;a[5]=this.ier;a[6]=this.iir;a[7]=this.modem_control;a[8]=this.modem_status;a[9]=this.scratch_register;a[10]=this.irq;return a};
h(b),LOG_SERIAL),this.com=0,this.irq=4}this.bus.register("serial"+this.com+"-input",function(d){this.data_received(d)},this);a=a.io;a.register_write(b,this,function(d){this.write_data(d)},function(d){this.write_data(d&255);this.write_data(d>>8)});a.register_write(b|1,this,function(d){this.line_control&DLAB?(this.baud_rate=this.baud_rate&255|d<<8,dbg_log("baud rate: "+h(this.baud_rate),LOG_SERIAL)):(0===(this.ier&UART_IIR_THRI)&&d&UART_IIR_THRI&&this.ThrowInterrupt(UART_IIR_THRI),this.ier=d&15,dbg_log("interrupt enable: "+
h(d),LOG_SERIAL),this.CheckInterrupt())});a.register_read(b,this,function(){if(this.line_control&DLAB)return this.baud_rate&255;var d=this.input.shift();-1===d?dbg_log("Read input empty",LOG_SERIAL):dbg_log("Read input: "+h(d),LOG_SERIAL);0===this.input.length&&(this.lsr&=~UART_LSR_DATA_READY,this.ClearInterrupt(UART_IIR_CTI),this.ClearInterrupt(UART_IIR_RDI));return d});a.register_read(b|1,this,function(){return this.line_control&DLAB?this.baud_rate>>8:this.ier&15});a.register_read(b|2,this,function(){var d=
this.iir&15;dbg_log("read interrupt identification: "+h(this.iir),LOG_SERIAL);this.iir==UART_IIR_THRI&&this.ClearInterrupt(UART_IIR_THRI);this.fifo_control&1&&(d|=192);return d});a.register_write(b|2,this,function(d){dbg_log("fifo control: "+h(d),LOG_SERIAL);this.fifo_control=d});a.register_read(b|3,this,function(){dbg_log("read line control: "+h(this.line_control),LOG_SERIAL);return this.line_control});a.register_write(b|3,this,function(d){dbg_log("line control: "+h(d),LOG_SERIAL);this.line_control=
d});a.register_read(b|4,this,function(){return this.modem_control});a.register_write(b|4,this,function(d){dbg_log("modem control: "+h(d),LOG_SERIAL);this.modem_control=d});a.register_read(b|5,this,function(){dbg_log("read line status: "+h(this.lsr),LOG_SERIAL);return this.lsr});a.register_write(b|5,this,function(d){dbg_log("Factory test write",LOG_SERIAL)});a.register_read(b|6,this,function(){dbg_log("read modem status: "+h(this.modem_status),LOG_SERIAL);return this.modem_status});a.register_write(b|
6,this,function(d){dbg_log("Unkown register write (base+6)",LOG_SERIAL)});a.register_read(b|7,this,function(){return this.scratch_register});a.register_write(b|7,this,function(d){this.scratch_register=d})}UART.prototype.get_state=function(){var a=[];a[0]=this.ints;a[1]=this.baud_rate;a[2]=this.line_control;a[3]=this.lsr;a[4]=this.fifo_control;a[5]=this.ier;a[6]=this.iir;a[7]=this.modem_control;a[8]=this.modem_status;a[9]=this.scratch_register;a[10]=this.irq;return a};
UART.prototype.set_state=function(a){this.ints=a[0];this.baud_rate=a[1];this.line_control=a[2];this.lsr=a[3];this.fifo_control=a[4];this.ier=a[5];this.iir=a[6];this.modem_control=a[7];this.modem_status=a[8];this.scratch_register=a[9];this.irq=a[10]};
UART.prototype.CheckInterrupt=function(){this.ints&1<<UART_IIR_CTI&&this.ier&UART_IER_RDI?(this.iir=UART_IIR_CTI,this.cpu.device_raise_irq(this.irq)):this.ints&1<<UART_IIR_THRI&&this.ier&UART_IER_THRI?(this.iir=UART_IIR_THRI,this.cpu.device_raise_irq(this.irq)):this.ints&1<<UART_IIR_MSI&&this.ier&UART_IER_MSI?(this.iir=UART_IIR_MSI,this.cpu.device_raise_irq(this.irq)):(this.iir=UART_IIR_NO_INT,this.cpu.device_lower_irq(this.irq))};UART.prototype.ThrowInterrupt=function(a){this.ints|=1<<a;this.CheckInterrupt()};
UART.prototype.ClearInterrupt=function(a){this.ints&=~(1<<a);this.CheckInterrupt()};UART.prototype.data_received=function(a){dbg_log("input: "+h(a),LOG_SERIAL);this.input.push(a);this.lsr|=UART_LSR_DATA_READY;this.ThrowInterrupt(UART_IIR_CTI)};
UART.prototype.CheckInterrupt=function(){this.ints&1<<UART_IIR_CTI&&this.ier&UART_IER_RDI?(this.iir=UART_IIR_CTI,this.cpu.device_raise_irq(this.irq)):this.ints&1<<UART_IIR_RDI&&this.ier&UART_IER_RDI?(this.iir=UART_IIR_RDI,this.cpu.device_raise_irq(this.irq)):this.ints&1<<UART_IIR_THRI&&this.ier&UART_IER_THRI?(this.iir=UART_IIR_THRI,this.cpu.device_raise_irq(this.irq)):this.ints&1<<UART_IIR_MSI&&this.ier&UART_IER_MSI?(this.iir=UART_IIR_MSI,this.cpu.device_raise_irq(this.irq)):(this.iir=UART_IIR_NO_INT,
this.cpu.device_lower_irq(this.irq))};UART.prototype.ThrowInterrupt=function(a){this.ints|=1<<a;this.CheckInterrupt()};UART.prototype.ClearInterrupt=function(a){this.ints&=~(1<<a);this.CheckInterrupt()};UART.prototype.data_received=function(a){dbg_log("input: "+h(a),LOG_SERIAL);this.input.push(a);this.lsr|=UART_LSR_DATA_READY;this.fifo_control&1?this.ThrowInterrupt(UART_IIR_CTI):this.ThrowInterrupt(UART_IIR_RDI)};
UART.prototype.write_data=function(a){if(this.line_control&DLAB)this.baud_rate=this.baud_rate&-256|a;else if(dbg_log("data: "+h(a),LOG_SERIAL),this.ThrowInterrupt(UART_IIR_THRI),255!==a){var b=String.fromCharCode(a);this.bus.send("serial"+this.com+"-output-char",b);this.current_line.push(a);"\n"===b&&(a=String.fromCharCode.apply("",this.current_line).trimRight().replace(/[\x00-\x08\x0b-\x1f\x7f\x80-\xff]/g,""),dbg_log("SERIAL: "+a),this.bus.send("serial"+this.com+"-output-line",String.fromCharCode.apply("",
this.current_line)),this.current_line=[])}};var HPET_ADDR=4275044352,HPET_PERIOD=1E8,HPET_FREQ_MS=1E12/HPET_PERIOD,HPET_SUPPORT_64=0,HPET_COUNTER_CONFIG=16|HPET_SUPPORT_64<<5,HPET_COUNTER_CONFIG_MASK=32816,HPET_NUM_COUNTERS=4;
function HPET(a){function b(){return e?(Date.now()-g)*HPET_FREQ_MS+f|0:f}function c(){return HPET_SUPPORT_64?e?(Date.now()-g)*(HPET_FREQ_MS/4294967296)+k|0:k:0}var d=this,e=!1,g=Date.now(),f=0,k=0,m=!1,l=0,n=new Int32Array(HPET_NUM_COUNTERS<<1),p=new Int32Array(HPET_NUM_COUNTERS<<1),t=new Int32Array(HPET_NUM_COUNTERS<<1),r=0;this.legacy_mode=!1;this.timer=function(q){if(!e)return 100;q=b()>>>0;for(var v,u,x=0;x<HPET_NUM_COUNTERS;x++)if(v=n[x<<1],u=p[x<<1]>>>0,r<=q?u>r&&u<=q:u>r||u<=q)u=v&4,v&2?(u=
u&&!(l&1<<x),l|=1<<x):l&=~(1<<x),v&8&&(p[x<<1]+=t[x<<1]),u&&a.device_raise_irq(0);r=q;return 100};a.io.mmap_register(HPET_ADDR,16384,function(q){dbg_log("Read "+h(q,4)+" (ctr="+h(b()>>>0)+")",LOG_HPET);switch(q){case 0:return HPET_NUM_COUNTERS-1<<8|98305|HPET_SUPPORT_64<<13;case 4:return HPET_PERIOD;case 16:return d.legacy_mode<<1|e;case 240:return b();case 244:return c()}var v=q>>2&7,u=q-256>>5;if(256>q||u>=HPET_NUM_COUNTERS||5<v)return dbg_log("Read reserved address: "+h(q),LOG_HPET),0;dbg_log("Read counter: addr="+
h(q)+" counter="+h(u,2)+" reg="+h(v),LOG_HPET);switch(v){case 0:return n[u<<1]&~HPET_COUNTER_CONFIG_MASK|HPET_COUNTER_CONFIG;case 1:return n[u<<1|1];case 2:return p[u<<1];case 3:return p[u<<1|1];case 4:case 5:return 0}},function(q,v){dbg_log("Write "+h(q,4)+": "+h(v,2),LOG_HPET);switch(q){case 16:dbg_log("conf: enabled="+(v&1)+" legacy="+(v>>1&1),LOG_HPET);(e^v)&1&&(v&1?g=Date.now():(f=b(),k=c()));e=1===(v&1);d.legacy_mode=2===(v&2);return;case 32:l&=~v;return;case 240:f=v;return;case 244:k=v;return}var u=
q>>2&7,x=q-256>>5;if(256>q||x>=HPET_NUM_COUNTERS||2<u)dbg_log("Write reserved address: "+h(q)+" data="+h(v),LOG_HPET);else switch(dbg_log("Write counter: addr="+h(q)+" counter="+h(x,2)+" reg="+h(u)+" data="+h(v,2),LOG_HPET),u){case 0:n[x<<1]=v;break;case 2:m?(t[x<<1]=v,m=!1,dbg_log("Accumulator acc="+h(v>>>0,8)+" ctr="+h(x,2),LOG_HPET)):(p[x<<1]=v,n[x<<1]&64&&(m=!0,n[x<<1]&=-65));break;case 3:p[x<<1|1]=v}})};var PMTIMER_FREQ_SECONDS=3579545;
function HPET(a){function b(){return e?(Date.now()-g)*HPET_FREQ_MS+f|0:f}function c(){return HPET_SUPPORT_64?e?(Date.now()-g)*(HPET_FREQ_MS/4294967296)+k|0:k:0}var d=this,e=!1,g=Date.now(),f=0,k=0,m=!1,l=0,n=new Int32Array(HPET_NUM_COUNTERS<<1),q=new Int32Array(HPET_NUM_COUNTERS<<1),t=new Int32Array(HPET_NUM_COUNTERS<<1),r=0;this.legacy_mode=!1;this.timer=function(p){if(!e)return 100;p=b()>>>0;for(var v,u,x=0;x<HPET_NUM_COUNTERS;x++)if(v=n[x<<1],u=q[x<<1]>>>0,r<=p?u>r&&u<=p:u>r||u<=p)u=v&4,v&2?(u=
u&&!(l&1<<x),l|=1<<x):l&=~(1<<x),v&8&&(q[x<<1]+=t[x<<1]),u&&a.device_raise_irq(0);r=p;return 100};a.io.mmap_register(HPET_ADDR,16384,function(p){dbg_log("Read "+h(p,4)+" (ctr="+h(b()>>>0)+")",LOG_HPET);switch(p){case 0:return HPET_NUM_COUNTERS-1<<8|98305|HPET_SUPPORT_64<<13;case 4:return HPET_PERIOD;case 16:return d.legacy_mode<<1|e;case 240:return b();case 244:return c()}var v=p>>2&7,u=p-256>>5;if(256>p||u>=HPET_NUM_COUNTERS||5<v)return dbg_log("Read reserved address: "+h(p),LOG_HPET),0;dbg_log("Read counter: addr="+
h(p)+" counter="+h(u,2)+" reg="+h(v),LOG_HPET);switch(v){case 0:return n[u<<1]&~HPET_COUNTER_CONFIG_MASK|HPET_COUNTER_CONFIG;case 1:return n[u<<1|1];case 2:return q[u<<1];case 3:return q[u<<1|1];case 4:case 5:return 0}},function(p,v){dbg_log("Write "+h(p,4)+": "+h(v,2),LOG_HPET);switch(p){case 16:dbg_log("conf: enabled="+(v&1)+" legacy="+(v>>1&1),LOG_HPET);(e^v)&1&&(v&1?g=Date.now():(f=b(),k=c()));e=1===(v&1);d.legacy_mode=2===(v&2);return;case 32:l&=~v;return;case 240:f=v;return;case 244:k=v;return}var u=
p>>2&7,x=p-256>>5;if(256>p||x>=HPET_NUM_COUNTERS||2<u)dbg_log("Write reserved address: "+h(p)+" data="+h(v),LOG_HPET);else switch(dbg_log("Write counter: addr="+h(p)+" counter="+h(x,2)+" reg="+h(u)+" data="+h(v,2),LOG_HPET),u){case 0:n[x<<1]=v;break;case 2:m?(t[x<<1]=v,m=!1,dbg_log("Accumulator acc="+h(v>>>0,8)+" ctr="+h(x,2),LOG_HPET)):(q[x<<1]=v,n[x<<1]&64&&(m=!0,n[x<<1]&=-65));break;case 3:q[x<<1|1]=v}})};var PMTIMER_FREQ_SECONDS=3579545;
function ACPI(a){this.cpu=a;var b=a.io;a.devices.pci.register_device({pci_id:56,pci_space:[134,128,19,113,7,0,128,2,8,0,128,6,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,1,0,0],pci_bars:[],name:"acpi"});this.timer_imprecision_offset=this.timer_last_value=0;this.status=1;this.pm1_enable=this.pm1_status=0;this.last_timer=this.get_timer(v86.microtick());this.gpe=new Uint8Array(4);b.register_read(45056,this,void 0,function(){dbg_log("ACPI pm1_status read",
LOG_ACPI);return this.pm1_status});b.register_write(45056,this,void 0,function(c){dbg_log("ACPI pm1_status write: "+h(c,4),LOG_ACPI);this.pm1_status&=~c});b.register_read(45058,this,void 0,function(){dbg_log("ACPI pm1_enable read",LOG_ACPI);return this.pm1_enable});b.register_write(45058,this,void 0,function(c){dbg_log("ACPI pm1_enable write: "+h(c),LOG_ACPI);this.pm1_enable=c});b.register_read(45060,this,void 0,function(){dbg_log("ACPI status read",LOG_ACPI);return this.status});b.register_write(45060,
this,void 0,function(c){dbg_log("ACPI status write: "+h(c),LOG_ACPI);this.status=c});b.register_read(45064,this,void 0,void 0,function(){return this.get_timer(v86.microtick())&16777215});b.register_read(45024,this,function(){dbg_log("Read gpe#0",LOG_ACPI);return this.gpe[0]});b.register_read(45025,this,function(){dbg_log("Read gpe#1",LOG_ACPI);return this.gpe[1]});b.register_read(45026,this,function(){dbg_log("Read gpe#2",LOG_ACPI);return this.gpe[2]});b.register_read(45027,this,function(){dbg_log("Read gpe#3",
@@ -406,42 +427,49 @@ function save_object(a,b){if("object"!==typeof a||null===a)return dbg_assert("fu
a);c=a.get_state();a=[];for(var d=0;d<c.length;d++){var e=c[d];dbg_assert("function"!==typeof e);a[d]=save_object(e,b)}return a}function restore_buffers(a,b){if("object"!==typeof a||null===a)return dbg_assert("function"!==typeof a),a;if(a instanceof Array){for(var c=0;c<a.length;c++)a[c]=restore_buffers(a[c],b);return a}c=a.__state_type__;dbg_assert(void 0!==c);const d=CONSTRUCTOR_TABLE[c];dbg_assert(d,"Unkown type: "+c);return new d(b[a.buffer_id])}
CPU.prototype.save_state=function(){for(var a=[],b=save_object(this,a),c=[],d=0,e=0;e<a.length;e++){var g=a[e].byteLength;c[e]={offset:d,length:g};d+=g;d=d+3&-4}b=JSON.stringify({buffer_infos:c,state:b});b=(new TextEncoder).encode(b);e=STATE_INFO_BLOCK_START+b.length;e=e+3&-4;var f=e+d;d=new ArrayBuffer(f);var k=new Int32Array(d,0,STATE_INFO_BLOCK_START/4);(new Uint8Array(d,STATE_INFO_BLOCK_START,b.length)).set(b);g=new Uint8Array(d,e);k[STATE_INDEX_MAGIC]=STATE_MAGIC;k[STATE_INDEX_VERSION]=STATE_VERSION;
k[STATE_INDEX_TOTAL_LEN]=f;k[STATE_INDEX_INFO_LEN]=b.length;for(e=0;e<a.length;e++)f=a[e],dbg_assert(f.constructor===Uint8Array),g.set(f,c[e].offset);dbg_log("State: json size "+(b.byteLength>>10)+"k");dbg_log("State: Total buffers size "+(g.byteLength>>10)+"k");return d};
CPU.prototype.restore_state=function(a){function b(t,r){const q=t.length;if(q<STATE_INFO_BLOCK_START)throw new StateLoadError("Invalid length: "+q);t=new Int32Array(t.buffer,t.byteOffset,4);if(t[STATE_INDEX_MAGIC]!==STATE_MAGIC)throw new StateLoadError("Invalid header: "+h(t[STATE_INDEX_MAGIC]>>>0));if(t[STATE_INDEX_VERSION]!==STATE_VERSION)throw new StateLoadError("Version mismatch: dump="+t[STATE_INDEX_VERSION]+" we="+STATE_VERSION);if(r&&t[STATE_INDEX_TOTAL_LEN]!==q)throw new StateLoadError("Length doesn't match header: real="+
q+" header="+t[STATE_INDEX_TOTAL_LEN]);return t[STATE_INDEX_INFO_LEN]}function c(t){t=(new TextDecoder).decode(t);return JSON.parse(t)}a=new Uint8Array(a);if((new Uint32Array(a.buffer,0,1))[0]===ZSTD_MAGIC){var d=this.zstd_create_ctx(a.length);(new Uint8Array(this.wasm_memory.buffer,this.zstd_get_src_ptr(d),a.length)).set(a);var e=this.zstd_read(d,16),g=new Uint8Array(this.wasm_memory.buffer,e,16),f=b(g,!1);this.zstd_read_free(e,16);e=this.zstd_read(d,f);g=new Uint8Array(this.wasm_memory.buffer,e,
f);g=c(g);this.zstd_read_free(e,f);e=g.state;var k=g.buffer_infos;g=[];f=STATE_INFO_BLOCK_START+f;for(var m of k){k=(f+3&-4)-f;if(1048576<m.length){var l=this.zstd_read(d,k);this.zstd_read_free(l,k);l=new Uint8Array(m.length);g.push(l.buffer);for(var n=0;n<m.length;){var p=m.length-n;dbg_assert(0<=p);p=Math.min(p,1048576);const t=this.zstd_read(d,p);l.set(new Uint8Array(this.wasm_memory.buffer,t,p),n);this.zstd_read_free(t,p);n+=p}}else l=this.zstd_read(d,k+m.length),n=l+k,g.push(this.wasm_memory.buffer.slice(n,
n+m.length)),this.zstd_read_free(l,k+m.length);f+=k+m.length}e=restore_buffers(e,g);this.set_state(e);this.zstd_free_ctx(d)}else{d=b(a,!0);if(0>d||d+12>=a.length)throw new StateLoadError("Invalid info block length: "+d);m=a.subarray(STATE_INFO_BLOCK_START,STATE_INFO_BLOCK_START+d);e=c(m);m=e.state;e=e.buffer_infos;let t=STATE_INFO_BLOCK_START+d;t=t+3&-4;d=e.map(r=>{const q=t+r.offset;return a.buffer.slice(q,q+r.length)});m=restore_buffers(m,d);this.set_state(m)}};const NE2K_LOG_VERBOSE=!1;
CPU.prototype.restore_state=function(a){function b(t,r){const p=t.length;if(p<STATE_INFO_BLOCK_START)throw new StateLoadError("Invalid length: "+p);t=new Int32Array(t.buffer,t.byteOffset,4);if(t[STATE_INDEX_MAGIC]!==STATE_MAGIC)throw new StateLoadError("Invalid header: "+h(t[STATE_INDEX_MAGIC]>>>0));if(t[STATE_INDEX_VERSION]!==STATE_VERSION)throw new StateLoadError("Version mismatch: dump="+t[STATE_INDEX_VERSION]+" we="+STATE_VERSION);if(r&&t[STATE_INDEX_TOTAL_LEN]!==p)throw new StateLoadError("Length doesn't match header: real="+
p+" header="+t[STATE_INDEX_TOTAL_LEN]);return t[STATE_INDEX_INFO_LEN]}function c(t){t=(new TextDecoder).decode(t);return JSON.parse(t)}a=new Uint8Array(a);if((new Uint32Array(a.buffer,0,1))[0]===ZSTD_MAGIC){var d=this.zstd_create_ctx(a.length);(new Uint8Array(this.wasm_memory.buffer,this.zstd_get_src_ptr(d),a.length)).set(a);var e=this.zstd_read(d,16),g=new Uint8Array(this.wasm_memory.buffer,e,16),f=b(g,!1);this.zstd_read_free(e,16);e=this.zstd_read(d,f);g=new Uint8Array(this.wasm_memory.buffer,e,
f);g=c(g);this.zstd_read_free(e,f);e=g.state;var k=g.buffer_infos;g=[];f=STATE_INFO_BLOCK_START+f;for(var m of k){k=(f+3&-4)-f;if(1048576<m.length){var l=this.zstd_read(d,k);this.zstd_read_free(l,k);l=new Uint8Array(m.length);g.push(l.buffer);for(var n=0;n<m.length;){var q=m.length-n;dbg_assert(0<=q);q=Math.min(q,1048576);const t=this.zstd_read(d,q);l.set(new Uint8Array(this.wasm_memory.buffer,t,q),n);this.zstd_read_free(t,q);n+=q}}else l=this.zstd_read(d,k+m.length),n=l+k,g.push(this.wasm_memory.buffer.slice(n,
n+m.length)),this.zstd_read_free(l,k+m.length);f+=k+m.length}e=restore_buffers(e,g);this.set_state(e);this.zstd_free_ctx(d)}else{d=b(a,!0);if(0>d||d+12>=a.length)throw new StateLoadError("Invalid info block length: "+d);m=a.subarray(STATE_INFO_BLOCK_START,STATE_INFO_BLOCK_START+d);e=c(m);m=e.state;e=e.buffer_infos;let t=STATE_INFO_BLOCK_START+d;t=t+3&-4;d=e.map(r=>{const p=t+r.offset;return a.buffer.slice(p,p+r.length)});m=restore_buffers(m,d);this.set_state(m)}};const NE2K_LOG_VERBOSE=!1,NE2K_LOG_PACKETS=!1;
var E8390_CMD=0,EN0_CLDALO=1,EN0_STARTPG=1,EN0_CLDAHI=2,EN0_STOPPG=2,EN0_BOUNDARY=3,EN0_TSR=4,EN0_TPSR=4,EN0_NCR=5,EN0_TCNTLO=5,EN0_FIFO=6,EN0_TCNTHI=6,EN0_ISR=7,EN0_CRDALO=8,EN0_RSARLO=8,EN0_CRDAHI=9,EN0_RSARHI=9,EN0_RCNTLO=10,EN0_RCNTHI=11,EN0_RSR=12,EN0_RXCR=12,EN0_TXCR=13,EN0_COUNTER0=13,EN0_DCFG=14,EN0_COUNTER1=14,EN0_IMR=15,EN0_COUNTER2=15,NE_DATAPORT=16,NE_RESET=31,ENISR_RX=1,ENISR_TX=2,ENISR_RX_ERR=4,ENISR_TX_ERR=8,ENISR_OVER=16,ENISR_COUNTERS=32,ENISR_RDC=64,ENISR_RESET=128,ENISR_ALL=63,
ENRSR_RXOK=1,START_PAGE=64,START_RX_PAGE=76,STOP_PAGE=128;
function Ne2k(a,b,c){this.cpu=a;this.pci=a.devices.pci;this.preserve_mac_from_state_image=c;this.bus=b;this.bus.register("net0-receive",function(d){this.receive(d)},this);this.port=768;this.name="ne2k";this.pci_space=[236,16,41,128,3,1,0,0,0,0,0,2,0,0,0,0,this.port&255|1,this.port>>8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,0,17,0,0,184,254,0,0,0,0,0,0,0,0,0,1,0,0];this.pci_id=40;this.pci_bars=[{size:32}];this.imr=this.isr=0;this.cr=1;this.tpsr=this.tcnt=this.rcnt=this.dcfg=0;this.memory=
new Uint8Array(32768);this.txcr=this.rxcr=0;this.tsr=1;this.mac=new Uint8Array([0,34,21,255*Math.random()|0,255*Math.random()|0,255*Math.random()|0]);for(b=0;6>b;b++)this.memory[b<<1]=this.memory[b<<1|1]=this.mac[b];this.memory[28]=this.memory[29]=87;this.memory[30]=this.memory[31]=87;dbg_log("Mac: "+h(this.mac[0],2)+":"+h(this.mac[1],2)+":"+h(this.mac[2],2)+":"+h(this.mac[3],2)+":"+h(this.mac[4],2)+":"+h(this.mac[5],2),LOG_NET);this.rsar=0;this.pstart=START_PAGE;this.pstop=STOP_PAGE;this.boundary=
this.curpg=START_RX_PAGE;b=a.io;b.register_read(this.port|E8390_CMD,this,function(){dbg_log("Read cmd",LOG_NET);return this.cr});b.register_write(this.port|E8390_CMD,this,function(d){this.cr=d;dbg_log("Write command: "+h(d,2)+" newpg="+(this.cr>>6)+" txcr="+h(this.txcr,2),LOG_NET);this.cr&1||(d&24&&0===this.rcnt&&this.do_interrupt(ENISR_RDC),d&4&&(d=this.tpsr<<8,d=this.memory.subarray(d,d+this.tcnt),this.bus.send("net0-send",d),this.bus.send("eth-transmit-end",[d.length]),this.cr&=-5,this.do_interrupt(ENISR_TX),
dbg_log("Command: Transfer. length="+h(d.byteLength),LOG_NET)))});b.register_read(this.port|EN0_COUNTER0,this,function(){dbg_log("Read counter0",LOG_NET);return 0});b.register_read(this.port|EN0_COUNTER1,this,function(){dbg_log("Read counter1",LOG_NET);return 0});b.register_read(this.port|EN0_COUNTER2,this,function(){dbg_log("Read counter2",LOG_NET);return 0});b.register_read(this.port|NE_RESET,this,function(){var d=this.get_page();0===d?(dbg_log("Read reset",LOG_NET),this.do_interrupt(ENISR_RESET)):
(dbg_log("Read pg"+d+"/1f",LOG_NET),dbg_assert(!1));return 0});b.register_write(this.port|NE_RESET,this,function(d){var e=this.get_page();0===e?dbg_log("Write reset: "+h(d,2),LOG_NET):(dbg_log("Write pg"+e+"/1f: "+h(d),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_STARTPG,this,function(){var d=this.get_page();if(0===d)return this.pstart;if(1===d)return dbg_log("Read pg1/01 (mac[0])",LOG_NET),this.mac[0];if(2===d)return this.pstart;dbg_log("Read pg"+d+"/01");dbg_assert(!1);return 0});b.register_write(this.port|
EN0_STARTPG,this,function(d){var e=this.get_page();0===e?(dbg_log("start page: "+h(d,2),LOG_NET),this.pstart=d):1===e?(dbg_log("mac[0] = "+h(d),LOG_NET),this.mac[0]=d):3===e?dbg_log("Unimplemented: Write pg3/01 (9346CR): "+h(d),LOG_NET):(dbg_log("Write pg"+e+"/01: "+h(d),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_STOPPG,this,function(){var d=this.get_page();if(0===d)return this.pstop;if(1===d)return dbg_log("Read pg1/02 (mac[1])",LOG_NET),this.mac[1];if(2===d)return this.pstop;dbg_log("Read pg"+
d+"/02",LOG_NET);dbg_assert(!1);return 0});b.register_write(this.port|EN0_STOPPG,this,function(d){var e=this.get_page();0===e?(dbg_log("stop page: "+h(d,2),LOG_NET),d>this.memory.length>>8&&(d=this.memory.length>>8,dbg_log("XXX: Adjusting stop page to "+h(d),LOG_NET)),this.pstop=d):1===e?(dbg_log("mac[1] = "+h(d),LOG_NET),this.mac[1]=d):(dbg_log("Write pg"+e+"/02: "+h(d),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_ISR,this,function(){var d=this.get_page();if(0===d)return dbg_log("Read isr: "+
h(this.isr,2),LOG_NET),this.isr;if(1===d)return dbg_log("Read curpg: "+h(this.curpg,2),LOG_NET),this.curpg;dbg_assert(!1)});b.register_write(this.port|EN0_ISR,this,function(d){var e=this.get_page();0===e?(dbg_log("Write isr: "+h(d,2),LOG_NET),this.isr&=~d,this.update_irq()):1===e?(dbg_log("Write curpg: "+h(d,2),LOG_NET),this.curpg=d):dbg_assert(!1)});b.register_write(this.port|EN0_TXCR,this,function(d){var e=this.get_page();0===e?(this.txcr=d,dbg_log("Write tx config: "+h(d,2),LOG_NET)):dbg_log("Unimplemented: Write pg"+
e+"/0d "+h(d,2),LOG_NET)});b.register_write(this.port|EN0_DCFG,this,function(d){var e=this.get_page();0===e?(dbg_log("Write data configuration: "+h(d,2),LOG_NET),this.dcfg=d):dbg_log("Unimplemented: Write pg"+e+"/0e "+h(d,2),LOG_NET)});b.register_read(this.port|EN0_RCNTLO,this,function(){if(0===this.get_page())return dbg_log("Read pg0/0a",LOG_NET),80;dbg_assert(!1,"TODO");return 0});b.register_write(this.port|EN0_RCNTLO,this,function(d){var e=this.get_page();0===e?(dbg_log("Write remote byte count low: "+
h(d,2),LOG_NET),this.rcnt=this.rcnt&65280|d&255):dbg_log("Unimplemented: Write pg"+e+"/0a "+h(d,2),LOG_NET)});b.register_read(this.port|EN0_RCNTHI,this,function(){if(0===this.get_page())return dbg_log("Read pg0/0b",LOG_NET),67;dbg_assert(!1,"TODO");return 0});b.register_write(this.port|EN0_RCNTHI,this,function(d){var e=this.get_page();0===e?(dbg_log("Write remote byte count high: "+h(d,2),LOG_NET),this.rcnt=this.rcnt&255|d<<8&65280):dbg_log("Unimplemented: Write pg"+e+"/0b "+h(d,2),LOG_NET)});b.register_read(this.port|
EN0_RSARLO,this,function(){var d=this.get_page();if(0===d)return dbg_log("Read remote start address low",LOG_NET),this.rsar&255;dbg_log("Unimplemented: Read pg"+d+"/08",LOG_NET);dbg_assert(!1)});b.register_write(this.port|EN0_RSARLO,this,function(d){var e=this.get_page();0===e?(dbg_log("Write remote start address low: "+h(d,2),LOG_NET),this.rsar=this.rsar&65280|d&255):dbg_log("Unimplemented: Write pg"+e+"/08 "+h(d,2),LOG_NET)});b.register_read(this.port|EN0_RSARHI,this,function(){var d=this.get_page();
if(0===d)return dbg_log("Read remote start address high",LOG_NET),this.rsar>>8&255;dbg_log("Unimplemented: Read pg"+d+"/09",LOG_NET);dbg_assert(!1)});b.register_write(this.port|EN0_RSARHI,this,function(d){var e=this.get_page();0===e?(dbg_log("Write remote start address low: "+h(d,2),LOG_NET),this.rsar=this.rsar&255|d<<8&65280):dbg_log("Unimplemented: Write pg"+e+"/09 "+h(d,2),LOG_NET)});b.register_write(this.port|EN0_IMR,this,function(d){var e=this.get_page();0===e?(dbg_log("Write interrupt mask register: "+
h(d,2)+" isr="+h(this.isr,2),LOG_NET),this.imr=d,this.update_irq()):dbg_log("Unimplemented: Write pg"+e+"/0f "+h(d,2),LOG_NET)});b.register_read(this.port|EN0_BOUNDARY,this,function(){var d=this.get_page();if(0===d)return dbg_log("Read boundary: "+h(this.boundary,2),LOG_NET),this.boundary;if(1===d)return dbg_log("Read pg1/03 (mac[2])",LOG_NET),this.mac[2];3===d?dbg_log("Unimplemented: Read pg3/03 (CONFIG0)",LOG_NET):(dbg_log("Read pg"+d+"/03",LOG_NET),dbg_assert(!1));return 0});b.register_write(this.port|
EN0_BOUNDARY,this,function(d){var e=this.get_page();0===e?(dbg_log("Write boundary: "+h(d,2),LOG_NET),this.boundary=d):1===e?(dbg_log("mac[2] = "+h(d),LOG_NET),this.mac[2]=d):(dbg_log("Write pg"+e+"/03: "+h(d),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_TSR,this,function(){var d=this.get_page();if(0===d)return this.tsr;if(1===d)return dbg_log("Read pg1/04 (mac[3])",LOG_NET),this.mac[3];dbg_log("Read pg"+d+"/04",LOG_NET);dbg_assert(!1);return 0});b.register_write(this.port|EN0_TPSR,this,
function(d){var e=this.get_page();0===e?(dbg_log("Write tpsr: "+h(d,2),LOG_NET),this.tpsr=d):1===e?(dbg_log("mac[3] = "+h(d),LOG_NET),this.mac[3]=d):(dbg_log("Write pg"+e+"/04: "+h(d),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_TCNTLO,this,function(){var d=this.get_page();if(0===d)return dbg_log("Unimplemented: Read pg0/05 (NCR: Number of Collisions Register)",LOG_NET),0;if(1===d)return dbg_log("Read pg1/05 (mac[4])",LOG_NET),this.mac[4];3===d?dbg_log("Unimplemented: Read pg3/05 (CONFIG2)",
LOG_NET):(dbg_log("Read pg"+d+"/05",LOG_NET),dbg_assert(!1));return 0});b.register_write(this.port|EN0_TCNTLO,this,function(d){var e=this.get_page();0===e?(dbg_log("Write tcnt low: "+h(d,2),LOG_NET),this.tcnt=this.tcnt&-256|d):1===e?(dbg_log("mac[4] = "+h(d),LOG_NET),this.mac[4]=d):3===e?dbg_log("Unimplemented: Write pg3/05 (CONFIG2): "+h(d),LOG_NET):(dbg_log("Write pg"+e+"/05: "+h(d),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_TCNTHI,this,function(){var d=this.get_page();if(0===d)return dbg_assert(!1,
"TODO"),0;if(1===d)return dbg_log("Read pg1/06 (mac[5])",LOG_NET),this.mac[5];3===d?dbg_log("Unimplemented: Read pg3/06 (CONFIG3)",LOG_NET):(dbg_log("Read pg"+d+"/06",LOG_NET),dbg_assert(!1));return 0});b.register_write(this.port|EN0_TCNTHI,this,function(d){var e=this.get_page();0===e?(dbg_log("Write tcnt high: "+h(d,2),LOG_NET),this.tcnt=this.tcnt&255|d<<8):1===e?(dbg_log("mac[5] = "+h(d),LOG_NET),this.mac[5]=d):3===e?dbg_log("Unimplemented: Write pg3/06 (CONFIG3): "+h(d),LOG_NET):(dbg_log("Write pg"+
e+"/06: "+h(d),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_RSR,this,function(){var d=this.get_page();if(0===d)return 9;dbg_log("Unimplemented: Read pg"+d+"/0c",LOG_NET);dbg_assert(!1);return 0});b.register_write(this.port|EN0_RXCR,this,function(d){var e=this.get_page();0===e?(dbg_log("RX configuration reg write: "+h(d,2),LOG_NET),this.rxcr=d):dbg_log("Unimplemented: Write pg"+e+"/0c: "+h(d),LOG_NET)});b.register_read(this.port|NE_DATAPORT|0,this,this.data_port_read8,this.data_port_read16,
this.data_port_read32);b.register_write(this.port|NE_DATAPORT|0,this,this.data_port_write16,this.data_port_write16,this.data_port_write32);a.devices.pci.register_device(this)}Ne2k.prototype.get_state=function(){var a=[];a[0]=this.isr;a[1]=this.imr;a[2]=this.cr;a[3]=this.dcfg;a[4]=this.rcnt;a[5]=this.tcnt;a[6]=this.tpsr;a[7]=this.rsar;a[8]=this.pstart;a[9]=this.curpg;a[10]=this.boundary;a[11]=this.pstop;a[12]=this.rxcr;a[13]=this.txcr;a[14]=this.tsr;a[15]=this.mac;a[16]=this.memory;return a};
Ne2k.prototype.set_state=function(a){this.isr=a[0];this.imr=a[1];this.cr=a[2];this.dcfg=a[3];this.rcnt=a[4];this.tcnt=a[5];this.tpsr=a[6];this.rsar=a[7];this.pstart=a[8];this.curpg=a[9];this.boundary=a[10];this.pstop=a[11];this.rxcr=a[12];this.txcr=a[13];this.tsr=a[14];this.preserve_mac_from_state_image&&(this.mac=a[15],this.memory=a[16])};Ne2k.prototype.do_interrupt=function(a){dbg_log("Do interrupt "+h(a,2),LOG_NET);this.isr|=a;this.update_irq()};
Ne2k.prototype.update_irq=function(){this.imr&this.isr?this.pci.raise_irq(this.pci_id):this.pci.lower_irq(this.pci_id)};Ne2k.prototype.data_port_write=function(a){NE2K_LOG_VERBOSE&&dbg_log("Write data port: data="+h(a&255,2)+" rsar="+h(this.rsar,4)+" rcnt="+h(this.rcnt,4),LOG_NET);if(16>=this.rsar||this.rsar>=START_PAGE<<8&&this.rsar<STOP_PAGE<<8)this.memory[this.rsar]=a;this.rsar++;this.rcnt--;this.rsar>=this.pstop<<8&&(this.rsar+=this.pstart-this.pstop<<8);0===this.rcnt&&this.do_interrupt(ENISR_RDC)};
Ne2k.prototype.data_port_write16=function(a){this.data_port_write(a);this.dcfg&1&&this.data_port_write(a>>8)};Ne2k.prototype.data_port_write32=function(a){this.data_port_write(a);this.data_port_write(a>>8);this.data_port_write(a>>16);this.data_port_write(a>>24)};
Ne2k.prototype.data_port_read=function(){let a=0;this.rsar<STOP_PAGE<<8&&(a=this.memory[this.rsar]);NE2K_LOG_VERBOSE&&dbg_log("Read data port: data="+h(a,2)+" rsar="+h(this.rsar,4)+" rcnt="+h(this.rcnt,4),LOG_NET);this.rsar++;this.rcnt--;this.rsar>=this.pstop<<8&&(this.rsar+=this.pstart-this.pstop<<8);0===this.rcnt&&this.do_interrupt(ENISR_RDC);return a};Ne2k.prototype.data_port_read8=function(){return this.data_port_read16()&255};
Ne2k.prototype.data_port_read16=function(){return this.dcfg&1?this.data_port_read()|this.data_port_read()<<8:this.data_port_read()};Ne2k.prototype.data_port_read32=function(){return this.data_port_read()|this.data_port_read()<<8|this.data_port_read()<<16|this.data_port_read()<<24};
Ne2k.prototype.receive=function(a){if(!(this.cr&1)&&(this.bus.send("eth-receive-end",[a.length]),this.rxcr&16||this.rxcr&4&&255===a[0]&&255===a[1]&&255===a[2]&&255===a[3]&&255===a[4]&&255===a[5]||!(this.rxcr&8&&1===(a[0]&1)||a[0]!==this.mac[0]||a[1]!==this.mac[1]||a[2]!==this.mac[2]||a[3]!==this.mac[3]||a[4]!==this.mac[4]||a[5]!==this.mac[5]))){var b=this.curpg<<8,c=Math.max(60,a.length)+4,d=b+4,e=this.curpg+1+(c>>8),g=b+c,f=1+(c>>8),k=this.boundary>this.curpg?this.boundary-this.curpg:this.pstop-
this.curpg+this.boundary-this.pstart;k<f&&0!==this.boundary?dbg_log("Buffer full, dropping packet pstart="+h(this.pstart)+" pstop="+h(this.pstop)+" curpg="+h(this.curpg)+" needed="+h(f)+" boundary="+h(this.boundary)+" available="+h(k),LOG_NET):(g>this.pstop<<8?(dbg_assert(60<=a.length),g=(this.pstop<<8)-d,dbg_assert(0<=g),this.memory.set(a.subarray(0,g),d),this.memory.set(a.subarray(g),this.pstart<<8),dbg_log("rcv cut="+h(g),LOG_NET)):(this.memory.set(a,d),60>a.length&&this.memory.fill(0,d+a.length,
d+60)),e>=this.pstop&&(e+=this.pstart-this.pstop),this.memory[b]=ENRSR_RXOK,this.memory[b+1]=e,this.memory[b+2]=c,this.memory[b+3]=c>>8,this.curpg=e,dbg_log("rcv offset="+h(b)+" len="+h(c)+" next="+h(e),LOG_NET),this.do_interrupt(ENISR_RX))}};Ne2k.prototype.get_page=function(){return this.cr>>6&3};var DSP_COPYRIGHT="COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.",DSP_NO_COMMAND=0,DSP_BUFSIZE=64,DSP_DACSIZE=65536,SB_DMA_BUFSIZE=65536,SB_DMA_BLOCK_SAMPLES=1024,SB_DMA0=0,SB_DMA1=1,SB_DMA3=3,SB_DMA5=5,SB_DMA6=6,SB_DMA7=7,SB_DMA_CHANNEL_8BIT=SB_DMA1,SB_DMA_CHANNEL_16BIT=SB_DMA5,SB_IRQ2=2,SB_IRQ5=5,SB_IRQ7=7,SB_IRQ10=10,SB_IRQ=SB_IRQ5,SB_IRQ_8BIT=1,SB_IRQ_16BIT=2,SB_IRQ_MIDI=1,SB_IRQ_MPU=4,DSP_COMMAND_SIZES=new Uint8Array(256),DSP_COMMAND_HANDLERS=[],MIXER_READ_HANDLERS=[],MIXER_WRITE_HANDLERS=[],
function translate_mac_address(a,b,c){a[0]===b[0]&&a[1]===b[1]&&a[2]===b[2]&&a[3]===b[3]&&a[4]===b[4]&&a[5]===b[5]&&(dbg_log("Replace mac in eth destination field",LOG_NET),a[0]=c[0],a[1]=c[1],a[2]=c[2],a[3]=c[3],a[4]=c[4],a[5]=c[5]);a[6]===b[0]&&a[7]===b[1]&&a[8]===b[2]&&a[9]===b[3]&&a[10]===b[4]&&a[11]===b[5]&&(dbg_log("Replace mac in eth source field",LOG_NET),a[6]=c[0],a[7]=c[1],a[8]=c[2],a[9]=c[3],a[10]=c[4],a[11]=c[5]);var d=a[12]<<8|a[13];if(2048===d)if(a=a.subarray(14),d=a[0]>>4,4!==d)dbg_log("Expected ipv4.version==4 but got: "+
d,LOG_NET);else{if(dbg_assert(5===(a[0]&15),"TODO: ihl!=5"),17===a[9]){a=a.subarray(20);d=a[0]<<8|a[1];var e=a[2]<<8|a[3];dbg_log("udp srcport="+d+" dstport="+e+" checksum="+h(a[6]<<8|a[7],4),LOG_NET);if(67===d||67===e)if(d=a.subarray(8),e=d[236]<<24|d[237]<<16|d[238]<<8|d[239],1669485411!==e)dbg_log("dhcp packet didn't match magic: "+h(e,8));else for(d[28]===b[0]&&d[29]===b[1]&&d[30]===b[2]&&d[31]===b[3]&&d[32]===b[4]&&d[33]===b[5]&&(dbg_log("Replace mac in dhcp.chaddr",LOG_NET),d[28]=c[0],d[29]=
c[1],d[30]=c[2],d[31]=c[3],d[32]=c[4],d[33]=c[5],a[6]=a[7]=0),e=240;e<d.length;){const g=d[e++];if(255===g)break;const f=d[e++];61===g&&1===d[e+0]&&d[e+1]===b[0]&&d[e+2]===b[1]&&d[e+3]===b[2]&&d[e+4]===b[3]&&d[e+5]===b[4]&&d[e+6]===b[5]&&(dbg_log("Replace mac in dhcp.clientidentifier",LOG_NET),d[e+1]=c[0],d[e+2]=c[1],d[e+3]=c[2],d[e+4]=c[3],d[e+5]=c[4],d[e+6]=c[5],a[6]=a[7]=0);e+=f}}}else 2054===d&&(a=a.subarray(14),dbg_log("arp oper="+a[7]+" "+format_mac(a.subarray(8,14))+" "+format_mac(a.subarray(18,
24)),LOG_NET),a[8]===b[0]&&a[9]===b[1]&&a[10]===b[2]&&a[11]===b[3]&&a[12]===b[4]&&a[13]===b[5]&&(dbg_log("Replace mac in arp.sha",LOG_NET),a[8]=c[0],a[9]=c[1],a[10]=c[2],a[11]=c[3],a[12]=c[4],a[13]=c[5]))}function format_mac(a){return[a[0].toString(16).padStart(2,"0"),a[1].toString(16).padStart(2,"0"),a[2].toString(16).padStart(2,"0"),a[3].toString(16).padStart(2,"0"),a[4].toString(16).padStart(2,"0"),a[5].toString(16).padStart(2,"0")].join(":")}
function dump_packet(a,b){const c=a[12]<<8|a[13]<<0;if(2048===c){var d=a.subarray(14);const f=d[2]<<8|d[3];var e=d[9];if(17===e){var g=d.subarray(20);d=g[0]<<8|g[1];e=g[2]<<8|g[3];const k=g[6]<<8|g[7];67===d||67===e?(g=g.subarray(8).subarray(28,34),dbg_log(b+" len="+a.length+" ethertype="+h(c)+" ipv4.len="+f+" ipv4.proto="+h(a[23])+" udp.srcport="+d+" udp.dstport="+e+" udp.chksum="+h(k,4)+" dhcp.chaddr="+format_mac(g))):dbg_log(b+" len="+a.length+" ethertype="+h(c)+" ipv4.len="+f+" ipv4.proto="+h(a[23])+
" udp.srcport="+d+" udp.dstport="+e+" udp.chksum="+h(k,4))}else 1!==e&&dbg_log(b+" len="+a.length+" ethertype="+h(c)+" ipv4.len="+f+" ipv4.proto="+h(a[23]))}else a.subarray(14),dbg_log(b+" len="+a.length+" ethertype="+h(c)+" arp");dbg_log(hex_dump(a))}
function Ne2k(a,b,c,d){this.cpu=a;this.pci=a.devices.pci;this.preserve_mac_from_state_image=c;this.mac_address_translation=d;this.bus=b;this.bus.register("net0-receive",function(e){this.receive(e)},this);this.port=768;this.name="ne2k";this.pci_space=[236,16,41,128,3,1,0,0,0,0,0,2,0,0,0,0,this.port&255|1,this.port>>8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,0,17,0,0,184,254,0,0,0,0,0,0,0,0,0,1,0,0];this.pci_id=40;this.pci_bars=[{size:32}];this.imr=this.isr=0;this.cr=1;this.tpsr=this.tcnt=
this.rcnt=this.dcfg=0;this.memory=new Uint8Array(32768);this.txcr=this.rxcr=0;this.tsr=1;this.mac=new Uint8Array([0,34,21,255*Math.random()|0,255*Math.random()|0,255*Math.random()|0]);this.mac_address_in_state=null;for(b=0;6>b;b++)this.memory[b<<1]=this.memory[b<<1|1]=this.mac[b];this.memory[28]=this.memory[29]=87;this.memory[30]=this.memory[31]=87;dbg_log("Mac: "+format_mac(this.mac),LOG_NET);this.rsar=0;this.pstart=START_PAGE;this.pstop=STOP_PAGE;this.boundary=this.curpg=START_RX_PAGE;b=a.io;b.register_read(this.port|
E8390_CMD,this,function(){dbg_log("Read cmd",LOG_NET);return this.cr});b.register_write(this.port|E8390_CMD,this,function(e){this.cr=e;dbg_log("Write command: "+h(e,2)+" newpg="+(this.cr>>6)+" txcr="+h(this.txcr,2),LOG_NET);this.cr&1||(e&24&&0===this.rcnt&&this.do_interrupt(ENISR_RDC),e&4&&(e=this.tpsr<<8,e=this.memory.subarray(e,e+this.tcnt),NE2K_LOG_PACKETS&&dump_packet(e,"send"),this.mac_address_in_state&&(e=new Uint8Array(e),translate_mac_address(e,this.mac_address_in_state,this.mac)),this.bus.send("net0-send",
e),this.bus.send("eth-transmit-end",[e.length]),this.cr&=-5,this.do_interrupt(ENISR_TX),dbg_log("Command: Transfer. length="+h(e.byteLength),LOG_NET)))});b.register_read(this.port|EN0_COUNTER0,this,function(){dbg_log("Read counter0",LOG_NET);return 0});b.register_read(this.port|EN0_COUNTER1,this,function(){dbg_log("Read8 counter1",LOG_NET);return 0},function(){dbg_log("Read16 counter1",LOG_NET);return 0});b.register_read(this.port|EN0_COUNTER2,this,function(){dbg_log("Read counter2",LOG_NET);return 0});
b.register_read(this.port|NE_RESET,this,function(){this.get_page();dbg_log("Read reset",LOG_NET);this.do_interrupt(ENISR_RESET);return 0});b.register_write(this.port|NE_RESET,this,function(e){this.get_page();dbg_log("Write reset: "+h(e,2),LOG_NET)});b.register_read(this.port|EN0_STARTPG,this,function(){var e=this.get_page();if(0===e)return this.pstart;if(1===e)return dbg_log("Read pg1/01 (mac[0])",LOG_NET),this.mac[0];if(2===e)return this.pstart;dbg_log("Read pg"+e+"/01");dbg_assert(!1);return 0});
b.register_write(this.port|EN0_STARTPG,this,function(e){var g=this.get_page();0===g?(dbg_log("start page: "+h(e,2),LOG_NET),this.pstart=e):1===g?(dbg_log("mac[0] = "+h(e),LOG_NET),this.mac[0]=e):3===g?dbg_log("Unimplemented: Write pg3/01 (9346CR): "+h(e),LOG_NET):(dbg_log("Write pg"+g+"/01: "+h(e),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_STOPPG,this,function(){var e=this.get_page();if(0===e)return this.pstop;if(1===e)return dbg_log("Read pg1/02 (mac[1])",LOG_NET),this.mac[1];if(2===
e)return this.pstop;dbg_log("Read pg"+e+"/02",LOG_NET);dbg_assert(!1);return 0});b.register_write(this.port|EN0_STOPPG,this,function(e){var g=this.get_page();0===g?(dbg_log("stop page: "+h(e,2),LOG_NET),e>this.memory.length>>8&&(e=this.memory.length>>8,dbg_log("XXX: Adjusting stop page to "+h(e),LOG_NET)),this.pstop=e):1===g?(dbg_log("mac[1] = "+h(e),LOG_NET),this.mac[1]=e):(dbg_log("Write pg"+g+"/02: "+h(e),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_ISR,this,function(){var e=this.get_page();
if(0===e)return dbg_log("Read isr: "+h(this.isr,2),LOG_NET),this.isr;if(1===e)return dbg_log("Read curpg: "+h(this.curpg,2),LOG_NET),this.curpg;dbg_assert(!1)});b.register_write(this.port|EN0_ISR,this,function(e){var g=this.get_page();0===g?(dbg_log("Write isr: "+h(e,2),LOG_NET),this.isr&=~e,this.update_irq()):1===g?(dbg_log("Write curpg: "+h(e,2),LOG_NET),this.curpg=e):dbg_assert(!1)});b.register_write(this.port|EN0_TXCR,this,function(e){var g=this.get_page();0===g?(this.txcr=e,dbg_log("Write tx config: "+
h(e,2),LOG_NET)):dbg_log("Unimplemented: Write pg"+g+"/0d "+h(e,2),LOG_NET)});b.register_write(this.port|EN0_DCFG,this,function(e){var g=this.get_page();0===g?(dbg_log("Write data configuration: "+h(e,2),LOG_NET),this.dcfg=e):dbg_log("Unimplemented: Write pg"+g+"/0e "+h(e,2),LOG_NET)});b.register_read(this.port|EN0_RCNTLO,this,function(){if(0===this.get_page())return dbg_log("Read pg0/0a",LOG_NET),80;dbg_assert(!1,"TODO");return 0});b.register_write(this.port|EN0_RCNTLO,this,function(e){var g=this.get_page();
0===g?(dbg_log("Write remote byte count low: "+h(e,2),LOG_NET),this.rcnt=this.rcnt&65280|e&255):dbg_log("Unimplemented: Write pg"+g+"/0a "+h(e,2),LOG_NET)});b.register_read(this.port|EN0_RCNTHI,this,function(){if(0===this.get_page())return dbg_log("Read pg0/0b",LOG_NET),67;dbg_assert(!1,"TODO");return 0});b.register_write(this.port|EN0_RCNTHI,this,function(e){var g=this.get_page();0===g?(dbg_log("Write remote byte count high: "+h(e,2),LOG_NET),this.rcnt=this.rcnt&255|e<<8&65280):dbg_log("Unimplemented: Write pg"+
g+"/0b "+h(e,2),LOG_NET)});b.register_read(this.port|EN0_RSARLO,this,function(){var e=this.get_page();if(0===e)return dbg_log("Read remote start address low",LOG_NET),this.rsar&255;dbg_log("Unimplemented: Read pg"+e+"/08",LOG_NET);dbg_assert(!1)});b.register_write(this.port|EN0_RSARLO,this,function(e){var g=this.get_page();0===g?(dbg_log("Write remote start address low: "+h(e,2),LOG_NET),this.rsar=this.rsar&65280|e&255):dbg_log("Unimplemented: Write pg"+g+"/08 "+h(e,2),LOG_NET)});b.register_read(this.port|
EN0_RSARHI,this,function(){var e=this.get_page();if(0===e)return dbg_log("Read remote start address high",LOG_NET),this.rsar>>8&255;dbg_log("Unimplemented: Read pg"+e+"/09",LOG_NET);dbg_assert(!1)});b.register_write(this.port|EN0_RSARHI,this,function(e){var g=this.get_page();0===g?(dbg_log("Write remote start address low: "+h(e,2),LOG_NET),this.rsar=this.rsar&255|e<<8&65280):dbg_log("Unimplemented: Write pg"+g+"/09 "+h(e,2),LOG_NET)});b.register_write(this.port|EN0_IMR,this,function(e){var g=this.get_page();
0===g?(dbg_log("Write interrupt mask register: "+h(e,2)+" isr="+h(this.isr,2),LOG_NET),this.imr=e,this.update_irq()):dbg_log("Unimplemented: Write pg"+g+"/0f "+h(e,2),LOG_NET)});b.register_read(this.port|EN0_BOUNDARY,this,function(){var e=this.get_page();if(0===e)return dbg_log("Read boundary: "+h(this.boundary,2),LOG_NET),this.boundary;if(1===e)return dbg_log("Read pg1/03 (mac[2])",LOG_NET),this.mac[2];3===e?dbg_log("Unimplemented: Read pg3/03 (CONFIG0)",LOG_NET):(dbg_log("Read pg"+e+"/03",LOG_NET),
dbg_assert(!1));return 0});b.register_write(this.port|EN0_BOUNDARY,this,function(e){var g=this.get_page();0===g?(dbg_log("Write boundary: "+h(e,2),LOG_NET),this.boundary=e):1===g?(dbg_log("mac[2] = "+h(e),LOG_NET),this.mac[2]=e):(dbg_log("Write pg"+g+"/03: "+h(e),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_TSR,this,function(){var e=this.get_page();if(0===e)return this.tsr;if(1===e)return dbg_log("Read pg1/04 (mac[3])",LOG_NET),this.mac[3];dbg_log("Read pg"+e+"/04",LOG_NET);dbg_assert(!1);
return 0});b.register_write(this.port|EN0_TPSR,this,function(e){var g=this.get_page();0===g?(dbg_log("Write tpsr: "+h(e,2),LOG_NET),this.tpsr=e):1===g?(dbg_log("mac[3] = "+h(e),LOG_NET),this.mac[3]=e):(dbg_log("Write pg"+g+"/04: "+h(e),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_TCNTLO,this,function(){var e=this.get_page();if(0===e)return dbg_log("Unimplemented: Read pg0/05 (NCR: Number of Collisions Register)",LOG_NET),0;if(1===e)return dbg_log("Read pg1/05 (mac[4])",LOG_NET),this.mac[4];
3===e?dbg_log("Unimplemented: Read pg3/05 (CONFIG2)",LOG_NET):(dbg_log("Read pg"+e+"/05",LOG_NET),dbg_assert(!1));return 0});b.register_write(this.port|EN0_TCNTLO,this,function(e){var g=this.get_page();0===g?(dbg_log("Write tcnt low: "+h(e,2),LOG_NET),this.tcnt=this.tcnt&-256|e):1===g?(dbg_log("mac[4] = "+h(e),LOG_NET),this.mac[4]=e):3===g?dbg_log("Unimplemented: Write pg3/05 (CONFIG2): "+h(e),LOG_NET):(dbg_log("Write pg"+g+"/05: "+h(e),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_TCNTHI,
this,function(){var e=this.get_page();if(0===e)return dbg_assert(!1,"TODO"),0;if(1===e)return dbg_log("Read pg1/06 (mac[5])",LOG_NET),this.mac[5];3===e?dbg_log("Unimplemented: Read pg3/06 (CONFIG3)",LOG_NET):(dbg_log("Read pg"+e+"/06",LOG_NET),dbg_assert(!1));return 0});b.register_write(this.port|EN0_TCNTHI,this,function(e){var g=this.get_page();0===g?(dbg_log("Write tcnt high: "+h(e,2),LOG_NET),this.tcnt=this.tcnt&255|e<<8):1===g?(dbg_log("mac[5] = "+h(e),LOG_NET),this.mac[5]=e):3===g?dbg_log("Unimplemented: Write pg3/06 (CONFIG3): "+
h(e),LOG_NET):(dbg_log("Write pg"+g+"/06: "+h(e),LOG_NET),dbg_assert(!1))});b.register_read(this.port|EN0_RSR,this,function(){var e=this.get_page();if(0===e)return 9;dbg_log("Unimplemented: Read pg"+e+"/0c",LOG_NET);dbg_assert(!1);return 0});b.register_write(this.port|EN0_RXCR,this,function(e){var g=this.get_page();0===g?(dbg_log("RX configuration reg write: "+h(e,2),LOG_NET),this.rxcr=e):dbg_log("Unimplemented: Write pg"+g+"/0c: "+h(e),LOG_NET)});b.register_read(this.port|NE_DATAPORT|0,this,this.data_port_read8,
this.data_port_read16,this.data_port_read32);b.register_write(this.port|NE_DATAPORT|0,this,this.data_port_write16,this.data_port_write16,this.data_port_write32);a.devices.pci.register_device(this)}
Ne2k.prototype.get_state=function(){var a=[];a[0]=this.isr;a[1]=this.imr;a[2]=this.cr;a[3]=this.dcfg;a[4]=this.rcnt;a[5]=this.tcnt;a[6]=this.tpsr;a[7]=this.rsar;a[8]=this.pstart;a[9]=this.curpg;a[10]=this.boundary;a[11]=this.pstop;a[12]=this.rxcr;a[13]=this.txcr;a[14]=this.tsr;a[15]=this.mac;a[16]=this.memory;return a};
Ne2k.prototype.set_state=function(a){this.isr=a[0];this.imr=a[1];this.cr=a[2];this.dcfg=a[3];this.rcnt=a[4];this.tcnt=a[5];this.tpsr=a[6];this.rsar=a[7];this.pstart=a[8];this.curpg=a[9];this.boundary=a[10];this.pstop=a[11];this.rxcr=a[12];this.txcr=a[13];this.tsr=a[14];this.preserve_mac_from_state_image?(this.mac=a[15],this.memory=a[16]):this.mac_address_translation&&(this.mac_address_in_state=a[15],this.memory=a[16],dbg_log("Using mac address translation guest_os_mac="+format_mac(this.mac_address_in_state)+
" real_mac="+format_mac(this.mac),LOG_NET))};Ne2k.prototype.do_interrupt=function(a){dbg_log("Do interrupt "+h(a,2),LOG_NET);this.isr|=a;this.update_irq()};Ne2k.prototype.update_irq=function(){this.imr&this.isr?this.pci.raise_irq(this.pci_id):this.pci.lower_irq(this.pci_id)};
Ne2k.prototype.data_port_write=function(a){NE2K_LOG_VERBOSE&&dbg_log("Write data port: data="+h(a&255,2)+" rsar="+h(this.rsar,4)+" rcnt="+h(this.rcnt,4),LOG_NET);if(16>=this.rsar||this.rsar>=START_PAGE<<8&&this.rsar<STOP_PAGE<<8)this.memory[this.rsar]=a;this.rsar++;this.rcnt--;this.rsar>=this.pstop<<8&&(this.rsar+=this.pstart-this.pstop<<8);0===this.rcnt&&this.do_interrupt(ENISR_RDC)};Ne2k.prototype.data_port_write16=function(a){this.data_port_write(a);this.dcfg&1&&this.data_port_write(a>>8)};
Ne2k.prototype.data_port_write32=function(a){this.data_port_write(a);this.data_port_write(a>>8);this.data_port_write(a>>16);this.data_port_write(a>>24)};Ne2k.prototype.data_port_read=function(){let a=0;this.rsar<STOP_PAGE<<8&&(a=this.memory[this.rsar]);NE2K_LOG_VERBOSE&&dbg_log("Read data port: data="+h(a,2)+" rsar="+h(this.rsar,4)+" rcnt="+h(this.rcnt,4),LOG_NET);this.rsar++;this.rcnt--;this.rsar>=this.pstop<<8&&(this.rsar+=this.pstart-this.pstop<<8);0===this.rcnt&&this.do_interrupt(ENISR_RDC);return a};
Ne2k.prototype.data_port_read8=function(){return this.data_port_read16()&255};Ne2k.prototype.data_port_read16=function(){return this.dcfg&1?this.data_port_read()|this.data_port_read()<<8:this.data_port_read()};Ne2k.prototype.data_port_read32=function(){return this.data_port_read()|this.data_port_read()<<8|this.data_port_read()<<16|this.data_port_read()<<24};
Ne2k.prototype.receive=function(a){if(!(this.cr&1)&&(NE2K_LOG_PACKETS&&dump_packet(a,"receive"),this.bus.send("eth-receive-end",[a.length]),this.rxcr&16||this.rxcr&4&&255===a[0]&&255===a[1]&&255===a[2]&&255===a[3]&&255===a[4]&&255===a[5]||!(this.rxcr&8&&1===(a[0]&1)||a[0]!==this.mac[0]||a[1]!==this.mac[1]||a[2]!==this.mac[2]||a[3]!==this.mac[3]||a[4]!==this.mac[4]||a[5]!==this.mac[5]))){this.mac_address_in_state&&(a=new Uint8Array(a),translate_mac_address(a,this.mac,this.mac_address_in_state));var b=
this.curpg<<8,c=Math.max(60,a.length)+4,d=b+4,e=this.curpg+1+(c>>8),g=b+c,f=1+(c>>8),k=this.boundary>this.curpg?this.boundary-this.curpg:this.pstop-this.curpg+this.boundary-this.pstart;k<f&&0!==this.boundary?dbg_log("Buffer full, dropping packet pstart="+h(this.pstart)+" pstop="+h(this.pstop)+" curpg="+h(this.curpg)+" needed="+h(f)+" boundary="+h(this.boundary)+" available="+h(k),LOG_NET):(g>this.pstop<<8?(dbg_assert(60<=a.length),g=(this.pstop<<8)-d,dbg_assert(0<=g),this.memory.set(a.subarray(0,
g),d),this.memory.set(a.subarray(g),this.pstart<<8),dbg_log("rcv cut="+h(g),LOG_NET)):(this.memory.set(a,d),60>a.length&&this.memory.fill(0,d+a.length,d+60)),e>=this.pstop&&(e+=this.pstart-this.pstop),this.memory[b]=ENRSR_RXOK,this.memory[b+1]=e,this.memory[b+2]=c,this.memory[b+3]=c>>8,this.curpg=e,dbg_log("rcv offset="+h(b)+" len="+h(c)+" next="+h(e),LOG_NET),this.do_interrupt(ENISR_RX))}};Ne2k.prototype.get_page=function(){return this.cr>>6&3};var DSP_COPYRIGHT="COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.",DSP_NO_COMMAND=0,DSP_BUFSIZE=64,DSP_DACSIZE=65536,SB_DMA_BUFSIZE=65536,SB_DMA_BLOCK_SAMPLES=1024,SB_DMA0=0,SB_DMA1=1,SB_DMA3=3,SB_DMA5=5,SB_DMA6=6,SB_DMA7=7,SB_DMA_CHANNEL_8BIT=SB_DMA1,SB_DMA_CHANNEL_16BIT=SB_DMA5,SB_IRQ2=2,SB_IRQ5=5,SB_IRQ7=7,SB_IRQ10=10,SB_IRQ=SB_IRQ5,SB_IRQ_8BIT=1,SB_IRQ_16BIT=2,SB_IRQ_MIDI=1,SB_IRQ_MPU=4,DSP_COMMAND_SIZES=new Uint8Array(256),DSP_COMMAND_HANDLERS=[],MIXER_READ_HANDLERS=[],MIXER_WRITE_HANDLERS=[],
MIXER_REGISTER_IS_LEGACY=new Uint8Array(256),FM_HANDLERS=[];
function SB16(a,b){this.cpu=a;this.bus=b;this.write_buffer=new ByteQueue(DSP_BUFSIZE);this.read_buffer=new ByteQueue(DSP_BUFSIZE);this.read_buffer_lastvalue=0;this.command=DSP_NO_COMMAND;this.mixer_current_address=this.command_size=0;this.mixer_registers=new Uint8Array(256);this.mixer_reset();this.dummy_speaker_enabled=!1;this.test_register=0;this.dsp_signed=this.dsp_16bit=this.dsp_stereo=this.dsp_highspeed=!1;this.dac_buffers=[new FloatQueue(DSP_DACSIZE),new FloatQueue(DSP_DACSIZE)];this.dma=a.devices.dma;
this.dma_channel=this.dma_irq=this.dma_bytes_block=this.dma_bytes_left=this.dma_bytes_count=this.dma_sample_count=0;this.dma_channel_8bit=SB_DMA1;this.dma_channel_16bit=SB_DMA5;this.dma_autoinit=!1;this.dma_buffer=new ArrayBuffer(SB_DMA_BUFSIZE);this.dma_buffer_int8=new Int8Array(this.dma_buffer);this.dma_buffer_uint8=new Uint8Array(this.dma_buffer);this.dma_buffer_int16=new Int16Array(this.dma_buffer);this.dma_buffer_uint16=new Uint16Array(this.dma_buffer);this.dma_syncbuffer=new SyncBuffer(this.dma_buffer);
this.dma_channel=this.dma_irq=this.dma_bytes_block=this.dma_bytes_left=this.dma_bytes_count=this.dma_sample_count=0;this.dma_channel_8bit=SB_DMA1;this.dma_channel_16bit=SB_DMA5;this.dma_autoinit=!1;this.dma_buffer=new ArrayBuffer(SB_DMA_BUFSIZE);this.dma_buffer_int8=new Int8Array(this.dma_buffer);this.dma_buffer_uint8=new Uint8Array(this.dma_buffer);this.dma_buffer_int16=new Int16Array(this.dma_buffer);this.dma_buffer_uint16=new Uint16Array(this.dma_buffer);this.dma_syncbuffer=new v86util.SyncBuffer(this.dma_buffer);
this.dma_paused=this.dma_waiting_transfer=!1;this.sampling_rate=22050;b.send("dac-tell-sampling-rate",this.sampling_rate);this.bytes_per_sample=1;this.e2_value=170;this.e2_count=0;this.asp_registers=new Uint8Array(256);this.mpu_read_buffer=new ByteQueue(DSP_BUFSIZE);this.fm_current_address1=this.fm_current_address0=this.mpu_read_buffer_lastvalue=0;this.fm_waveform_select_enable=!1;this.irq=SB_IRQ5;this.irq_triggered=new Uint8Array(16);a.io.register_read_consecutive(544,this,this.port2x0_read,this.port2x1_read,
this.port2x2_read,this.port2x3_read);a.io.register_read_consecutive(904,this,this.port2x0_read,this.port2x1_read);a.io.register_read_consecutive(548,this,this.port2x4_read,this.port2x5_read);a.io.register_read(550,this,this.port2x6_read);a.io.register_read(551,this,this.port2x7_read);a.io.register_read(552,this,this.port2x8_read);a.io.register_read(553,this,this.port2x9_read);a.io.register_read(554,this,this.port2xA_read);a.io.register_read(555,this,this.port2xB_read);a.io.register_read(556,this,
this.port2xC_read);a.io.register_read(557,this,this.port2xD_read);a.io.register_read_consecutive(558,this,this.port2xE_read,this.port2xF_read);a.io.register_write_consecutive(544,this,this.port2x0_write,this.port2x1_write,this.port2x2_write,this.port2x3_write);a.io.register_write_consecutive(904,this,this.port2x0_write,this.port2x1_write);a.io.register_write_consecutive(548,this,this.port2x4_write,this.port2x5_write);a.io.register_write(550,this,this.port2x6_write);a.io.register_write(551,this,this.port2x7_write);
@@ -453,7 +481,7 @@ SB16.prototype.get_state=function(){var a=[];a[2]=this.read_buffer_lastvalue;a[3
this.dma_channel_16bit;a[23]=this.dma_autoinit;a[24]=this.dma_buffer_uint8;a[25]=this.dma_waiting_transfer;a[26]=this.dma_paused;a[27]=this.sampling_rate;a[28]=this.bytes_per_sample;a[29]=this.e2_value;a[30]=this.e2_count;a[31]=this.asp_registers;a[33]=this.mpu_read_buffer_last_value;a[34]=this.irq;a[35]=this.irq_triggered;return a};
SB16.prototype.set_state=function(a){this.read_buffer_lastvalue=a[2];this.command=a[3];this.command_size=a[4];this.mixer_current_address=a[5];this.mixer_registers=a[6];this.mixer_full_update();this.dummy_speaker_enabled=a[7];this.test_register=a[8];this.dsp_highspeed=a[9];this.dsp_stereo=a[10];this.dsp_16bit=a[11];this.dsp_signed=a[12];this.dma_sample_count=a[15];this.dma_bytes_count=a[16];this.dma_bytes_left=a[17];this.dma_bytes_block=a[18];this.dma_irq=a[19];this.dma_channel=a[20];this.dma_channel_8bit=
a[21];this.dma_channel_16bit=a[22];this.dma_autoinit=a[23];this.dma_buffer_uint8=a[24];this.dma_waiting_transfer=a[25];this.dma_paused=a[26];this.sampling_rate=a[27];this.bytes_per_sample=a[28];this.e2_value=a[29];this.e2_count=a[30];this.asp_registers=a[31];this.mpu_read_buffer_last_value=a[33];this.irq=a[34];this.irq_triggered=a[35];this.dma_buffer=this.dma_buffer_uint8.buffer;this.dma_buffer_int8=new Int8Array(this.dma_buffer);this.dma_buffer_int16=new Int16Array(this.dma_buffer);this.dma_buffer_uint16=
new Uint16Array(this.dma_buffer);this.dma_syncbuffer=new SyncBuffer(this.dma_buffer);this.dma_paused?this.bus.send("dac-disable"):this.bus.send("dac-enable")};SB16.prototype.port2x0_read=function(){dbg_log("220 read: fm music status port (unimplemented)",LOG_SB16);return 255};SB16.prototype.port2x1_read=function(){dbg_log("221 read: fm music data port (write only)",LOG_SB16);return 255};
new Uint16Array(this.dma_buffer);this.dma_syncbuffer=new v86util.SyncBuffer(this.dma_buffer);this.dma_paused?this.bus.send("dac-disable"):this.bus.send("dac-enable")};SB16.prototype.port2x0_read=function(){dbg_log("220 read: fm music status port (unimplemented)",LOG_SB16);return 255};SB16.prototype.port2x1_read=function(){dbg_log("221 read: fm music data port (write only)",LOG_SB16);return 255};
SB16.prototype.port2x2_read=function(){dbg_log("222 read: advanced fm music status port (unimplemented)",LOG_SB16);return 255};SB16.prototype.port2x3_read=function(){dbg_log("223 read: advanced music data port (write only)",LOG_SB16);return 255};SB16.prototype.port2x4_read=function(){dbg_log("224 read: mixer address port",LOG_SB16);return this.mixer_current_address};SB16.prototype.port2x5_read=function(){dbg_log("225 read: mixer data port",LOG_SB16);return this.mixer_read(this.mixer_current_address)};
SB16.prototype.port2x6_read=function(){dbg_log("226 read: (write only)",LOG_SB16);return 255};SB16.prototype.port2x7_read=function(){dbg_log("227 read: undocumented",LOG_SB16);return 255};SB16.prototype.port2x8_read=function(){dbg_log("228 read: fm music status port (unimplemented)",LOG_SB16);return 255};SB16.prototype.port2x9_read=function(){dbg_log("229 read: fm music data port (write only)",LOG_SB16);return 255};
SB16.prototype.port2xA_read=function(){dbg_log("22A read: read data",LOG_SB16);this.read_buffer.length&&(this.read_buffer_lastvalue=this.read_buffer.shift());dbg_log(" <- "+this.read_buffer_lastvalue+" "+h(this.read_buffer_lastvalue)+" '"+String.fromCharCode(this.read_buffer_lastvalue)+"'",LOG_SB16);return this.read_buffer_lastvalue};SB16.prototype.port2xB_read=function(){dbg_log("22B read: undocumented",LOG_SB16);return 255};
@@ -559,8 +587,8 @@ b,716,1);this.svga_dirty_bitmap_max_offset=v86util.view(Uint32Array,b,720,1);thi
CPU.prototype.create_jit_imports=function(){const a=Object.create(null);a.m=this.wm.exports.memory;for(let b of Object.keys(this.wm.exports))b.startsWith("_")||b.startsWith("zstd")||b.endsWith("_js")||(a[b]=this.wm.exports[b]);this.jit_imports=a};
CPU.prototype.wasm_patch=function(){const a=c=>this.wm.exports[c],b=c=>{const d=a(c);console.assert(d,"Missing import: "+c);return d};this.reset_cpu=b("reset_cpu");this.getiopl=b("getiopl");this.get_eflags=b("get_eflags");this.get_eflags_no_arith=b("get_eflags_no_arith");this.pic_call_irq=b("pic_call_irq");this.do_many_cycles_native=b("do_many_cycles_native");this.cycle_internal=b("cycle_internal");this.read8=b("read8");this.read16=b("read16");this.read32s=b("read32s");this.write8=b("write8");this.write16=
b("write16");this.write32=b("write32");this.in_mapped_range=b("in_mapped_range");this.fpu_load_tag_word=b("fpu_load_tag_word");this.fpu_load_status_word=b("fpu_load_status_word");this.fpu_get_sti_f64=b("fpu_get_sti_f64");this.translate_address_system_read=b("translate_address_system_read_js");this.get_seg_cs=b("get_seg_cs");this.get_real_eip=b("get_real_eip");this.clear_tlb=b("clear_tlb");this.full_clear_tlb=b("full_clear_tlb");this.set_tsc=b("set_tsc");this.store_current_tsc=b("store_current_tsc");
DEBUG&&(this.jit_force_generate_unsafe=a("jit_force_generate_unsafe"));this.jit_clear_cache=b("jit_clear_cache_js");this.jit_dirty_cache=b("jit_dirty_cache");this.codegen_finalize_finished=b("codegen_finalize_finished");this.allocate_memory=b("allocate_memory");this.zero_memory=b("zero_memory");this.svga_allocate_memory=b("svga_allocate_memory");this.svga_allocate_dest_buffer=b("svga_allocate_dest_buffer");this.svga_fill_pixel_buffer=b("svga_fill_pixel_buffer");this.svga_mark_dirty=b("svga_mark_dirty");
this.zstd_create_ctx=b("zstd_create_ctx");this.zstd_get_src_ptr=b("zstd_get_src_ptr");this.zstd_free_ctx=b("zstd_free_ctx");this.zstd_read=b("zstd_read");this.zstd_read_free=b("zstd_read_free")};CPU.prototype.jit_force_generate=function(a){this.jit_force_generate_unsafe?this.jit_force_generate_unsafe(a):dbg_assert(!1,"Not supported in this wasm build: jit_force_generate_unsafe")};
this.set_cpuid_level=b("set_cpuid_level");DEBUG&&(this.jit_force_generate_unsafe=a("jit_force_generate_unsafe"));this.jit_clear_cache=b("jit_clear_cache_js");this.jit_dirty_cache=b("jit_dirty_cache");this.codegen_finalize_finished=b("codegen_finalize_finished");this.allocate_memory=b("allocate_memory");this.zero_memory=b("zero_memory");this.svga_allocate_memory=b("svga_allocate_memory");this.svga_allocate_dest_buffer=b("svga_allocate_dest_buffer");this.svga_fill_pixel_buffer=b("svga_fill_pixel_buffer");
this.svga_mark_dirty=b("svga_mark_dirty");this.zstd_create_ctx=b("zstd_create_ctx");this.zstd_get_src_ptr=b("zstd_get_src_ptr");this.zstd_free_ctx=b("zstd_free_ctx");this.zstd_read=b("zstd_read");this.zstd_read_free=b("zstd_read_free")};CPU.prototype.jit_force_generate=function(a){this.jit_force_generate_unsafe?this.jit_force_generate_unsafe(a):dbg_assert(!1,"Not supported in this wasm build: jit_force_generate_unsafe")};
CPU.prototype.jit_clear_func=function(a){dbg_assert(0<=a&&a<WASM_TABLE_SIZE);this.wm.wasm_table.set(a+WASM_TABLE_OFFSET,null)};CPU.prototype.jit_clear_all_funcs=function(){const a=this.wm.wasm_table;for(let b=0;b<WASM_TABLE_SIZE;b++)a.set(WASM_TABLE_OFFSET+b,null)};
CPU.prototype.get_state=function(){var a=[];a[0]=this.memory_size[0];a[1]=this.segment_is_null;a[2]=this.segment_offsets;a[3]=this.segment_limits;a[4]=this.protected_mode[0];a[5]=this.idtr_offset[0];a[6]=this.idtr_size[0];a[7]=this.gdtr_offset[0];a[8]=this.gdtr_size[0];a[9]=this.page_fault[0];a[10]=this.cr;a[11]=this.cpl[0];a[13]=this.is_32[0];a[16]=this.stack_size_32[0];a[17]=this.in_hlt[0];a[18]=this.last_virt_eip[0];a[19]=this.eip_phys[0];a[22]=this.sysenter_cs[0];a[23]=this.sysenter_eip[0];a[24]=
this.sysenter_esp[0];a[25]=this.prefixes[0];a[26]=this.flags[0];a[27]=this.flags_changed[0];a[28]=this.last_op1[0];a[30]=this.last_op_size[0];a[37]=this.instruction_pointer[0];a[38]=this.previous_ip[0];a[39]=this.reg32;a[40]=this.sreg;a[41]=this.dreg;a[42]=this.reg_pdpte;this.store_current_tsc();a[43]=this.current_tsc;a[45]=this.devices.virtio_9p;a[46]=this.devices.apic;a[47]=this.devices.rtc;a[48]=this.devices.pci;a[49]=this.devices.dma;a[50]=this.devices.acpi;a[51]=this.devices.hpet;a[52]=this.devices.vga;
@@ -575,13 +603,13 @@ CPU.prototype.pack_memory=function(){dbg_assert(0===(this.mem8.length&4095));var
CPU.prototype.unpack_memory=function(a,b){this.zero_memory(this.memory_size[0]);const c=this.memory_size[0]>>12;let d=0;for(let g=0;g<c;g++)if(a.get(g)){var e=d<<12;e=b.subarray(e,e+4096);this.mem8.set(e,g<<12);d++}};CPU.prototype.main_run=function(){if(this.in_hlt[0]){var a=this.hlt_loop();if(this.in_hlt[0])return a}let b=a=v86.microtick();for(;b-a<TIME_PER_FRAME;){this.do_many_cycles();b=v86.microtick();const c=this.run_hardware_timers(b);this.handle_irqs();if(this.in_hlt[0])return c}return 0};
CPU.prototype.reboot_internal=function(){this.reset_cpu();this.fw_value=[];this.devices.virtio&&this.devices.virtio.reset();this.load_bios()};CPU.prototype.reset_memory=function(){this.mem8.fill(0)};
CPU.prototype.create_memory=function(a){1048576>a?a=1048576:0>(a|0)&&(a=Math.pow(2,31)-MMAP_BLOCK_SIZE);a=(a-1|MMAP_BLOCK_SIZE-1)+1|0;dbg_assert(0<(a|0));dbg_assert(0===(a&MMAP_BLOCK_SIZE-1));console.assert(0===this.memory_size[0],"Expected uninitialised memory");this.memory_size[0]=a;const b=this.allocate_memory(a);this.mem8=v86util.view(Uint8Array,this.wasm_memory,b,a);this.mem32s=v86util.view(Uint32Array,this.wasm_memory,b,a>>2)};goog.exportProperty(CPU.prototype,"create_memory",CPU.prototype.create_memory);
CPU.prototype.init=function(a,b){"number"===typeof a.log_level&&(LOG_LEVEL=a.log_level);this.create_memory("number"===typeof a.memory_size?a.memory_size:67108864);this.acpi_enabled[0]=+a.acpi;this.reset_cpu();var c=new IO(this);this.io=c;this.bios.main=a.bios;this.bios.vga=a.vga_bios;this.load_bios();if(a.bzimage){const {option_rom:e}=load_kernel(this.mem8,a.bzimage,a.initrd,a.cmdline||"");e&&this.option_roms.push(e)}c.register_read(179,this,function(){dbg_log("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++];dbg_assert(!1,"config port: Read past value");return 0});c.register_write(1296,this,void 0,function(e){function g(m){return new Uint8Array((new Int32Array([m])).buffer)}function f(m){return m>>8|m<<8&65280}function k(m){return m<<24|m<<8&16711680|m>>8&65280|m>>>24}dbg_log("bios config port, index="+
CPU.prototype.init=function(a,b){"number"===typeof a.log_level&&(LOG_LEVEL=a.log_level);this.create_memory("number"===typeof a.memory_size?a.memory_size:67108864);a.cpuid_level&&this.set_cpuid_level(a.cpuid_level);this.acpi_enabled[0]=+a.acpi;this.reset_cpu();var c=new IO(this);this.io=c;this.bios.main=a.bios;this.bios.vga=a.vga_bios;this.load_bios();if(a.bzimage){const {option_rom:e}=load_kernel(this.mem8,a.bzimage,a.initrd,a.cmdline||"");e&&this.option_roms.push(e)}c.register_read(179,this,function(){dbg_log("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++];dbg_assert(!1,"config port: Read past value");return 0});c.register_write(1296,this,void 0,function(e){function g(m){return new Uint8Array((new Int32Array([m])).buffer)}function f(m){return m>>8|m<<8&65280}function k(m){return m<<24|m<<8&16711680|m>>8&65280|m>>>24}dbg_log("bios config port, index="+
h(e));this.fw_pointer=0;if(e===FW_CFG_SIGNATURE)this.fw_value=g(FW_CFG_SIGNATURE_QEMU);else if(e===FW_CFG_ID)this.fw_value=g(0);else if(e===FW_CFG_RAM_SIZE)this.fw_value=g(this.memory_size[0]);else if(e===FW_CFG_NB_CPUS)this.fw_value=g(1);else if(e===FW_CFG_MAX_CPUS)this.fw_value=g(1);else if(e===FW_CFG_NUMA)this.fw_value=new Uint8Array(16);else if(e===FW_CFG_FILE_DIR){e=new Int32Array(4+64*this.option_roms.length);const m=new Uint8Array(e.buffer);e[0]=k(this.option_roms.length);for(let l=0;l<this.option_roms.length;l++){const {name:n,
data:p}=this.option_roms[l],t=4+64*l;dbg_assert(65536>FW_CFG_FILE_START+l);e[t+0>>2]=k(p.length);e[t+4>>2]=f(FW_CFG_FILE_START+l);dbg_assert(56>n.length);for(let r=0;r<n.length;r++)m[t+8+r]=n.charCodeAt(r)}this.fw_value=m}else e>=FW_CFG_CUSTOM_START&&e<FW_CFG_FILE_START?this.fw_value=g(0):e>=FW_CFG_FILE_START&&e-FW_CFG_FILE_START<this.option_roms.length?this.fw_value=this.option_roms[e-FW_CFG_FILE_START].data:(dbg_log("Warning: Unimplemented fw index: "+h(e)),this.fw_value=g(0))});DEBUG&&c.register_write(128,
this,function(e){});this.devices={};a.load_devices&&(this.devices.pic=new PIC(this),this.devices.pci=new PCI(this),this.acpi_enabled[0]&&(this.devices.ioapic=new IOAPIC(this),this.devices.apic=new APIC(this),this.devices.acpi=new ACPI(this)),this.devices.rtc=new RTC(this),this.fill_cmos(this.devices.rtc,a),this.devices.dma=new DMA(this),ENABLE_HPET&&(this.devices.hpet=new HPET(this)),this.devices.vga=new VGAScreen(this,b,a.vga_memory_size||8388608),this.devices.ps2=new PS2(this,b),this.devices.uart0=
new UART(this,1016,b),a.uart1&&(this.devices.uart1=new UART(this,760,b)),a.uart2&&(this.devices.uart2=new UART(this,1E3,b)),a.uart3&&(this.devices.uart3=new UART(this,744,b)),this.devices.fdc=new FloppyController(this,a.fda,a.fdb),c=0,a.hda&&(this.devices.hda=new IDEDevice(this,a.hda,a.hdb,!1,c++,b)),a.cdrom&&(this.devices.cdrom=new IDEDevice(this,a.cdrom,void 0,!0,c++,b)),this.devices.pit=new PIT(this,b),a.enable_ne2k&&(this.devices.net=new Ne2k(this,b,a.preserve_mac_from_state_image)),a.fs9p&&(this.devices.virtio_9p=
new Virtio9p(a.fs9p,this,b)),this.devices.sb16=new SB16(this,b));a.multiboot&&this.load_multiboot(a.multiboot);DEBUG&&this.debug.init()};
data:q}=this.option_roms[l],t=4+64*l;dbg_assert(65536>FW_CFG_FILE_START+l);e[t+0>>2]=k(q.length);e[t+4>>2]=f(FW_CFG_FILE_START+l);dbg_assert(56>n.length);for(let r=0;r<n.length;r++)m[t+8+r]=n.charCodeAt(r)}this.fw_value=m}else e>=FW_CFG_CUSTOM_START&&e<FW_CFG_FILE_START?this.fw_value=g(0):e>=FW_CFG_FILE_START&&e-FW_CFG_FILE_START<this.option_roms.length?this.fw_value=this.option_roms[e-FW_CFG_FILE_START].data:(dbg_log("Warning: Unimplemented fw index: "+h(e)),this.fw_value=g(0))});DEBUG&&(c.register_write(128,
this,function(e){}),c.register_read(128,this,function(){return 255}),c.register_write(233,this,function(e){}));this.devices={};a.load_devices&&(this.devices.pic=new PIC(this),this.devices.pci=new PCI(this),this.acpi_enabled[0]&&(this.devices.ioapic=new IOAPIC(this),this.devices.apic=new APIC(this),this.devices.acpi=new ACPI(this)),this.devices.rtc=new RTC(this),this.fill_cmos(this.devices.rtc,a),this.devices.dma=new DMA(this),ENABLE_HPET&&(this.devices.hpet=new HPET(this)),this.devices.vga=new VGAScreen(this,
b,a.vga_memory_size||8388608),this.devices.ps2=new PS2(this,b),this.devices.uart0=new UART(this,1016,b),a.uart1&&(this.devices.uart1=new UART(this,760,b)),a.uart2&&(this.devices.uart2=new UART(this,1E3,b)),a.uart3&&(this.devices.uart3=new UART(this,744,b)),this.devices.fdc=new FloppyController(this,a.fda,a.fdb),c=0,a.hda&&(this.devices.hda=new IDEDevice(this,a.hda,a.hdb,!1,c++,b)),a.cdrom&&(this.devices.cdrom=new IDEDevice(this,a.cdrom,void 0,!0,c++,b)),this.devices.pit=new PIT(this,b),a.enable_ne2k&&
(this.devices.net=new Ne2k(this,b,a.preserve_mac_from_state_image,a.mac_address_translation)),a.fs9p&&(this.devices.virtio_9p=new Virtio9p(a.fs9p,this,b)),this.devices.sb16=new SB16(this,b));a.multiboot&&this.load_multiboot(a.multiboot);DEBUG&&this.debug.init()};
CPU.prototype.load_multiboot=function(a){dbg_log("Trying multiboot from buffer of size "+a.byteLength,LOG_CPU);if(8192>a.byteLength){var b=new Int32Array(2048);(new Uint8Array(b.buffer)).set(new Uint8Array(a))}else b=new Int32Array(a,0,2048);for(var c=0;8192>c;c+=4)if(464367618===b[c>>2]){var d=b[c+4>>2];if(464367618+d+b[c+8>>2]|0)dbg_log("Multiboot checksum check failed",LOG_CPU);else{dbg_log("Multiboot magic found, flags: "+h(d>>>0,8),LOG_CPU);dbg_assert(0===(d&-65537),"TODO");this.reg32[REG_EAX]=
732803074;this.reg32[REG_EBX]=31744;this.write32(31744,0);this.cr[0]=1;this.protected_mode[0]=1;this.flags[0]=FLAGS_DEFAULT;this.is_32[0]=1;this.stack_size_32[0]=1;for(var e=0;6>e;e++)this.segment_is_null[e]=0,this.segment_offsets[e]=0,this.segment_limits[e]=4294967295,this.sreg[e]=45058;if(d&65536){dbg_log("Multiboot specifies its own address table",LOG_CPU);e=b[c+12>>2];var g=b[c+16>>2];d=b[c+20>>2];var f=b[c+24>>2];b=b[c+28>>2];dbg_log("header="+h(e,8)+" load="+h(g,8)+" load_end="+h(d,8)+" bss_end="+
h(f,8)+" entry="+h(b,8));dbg_assert(g<=e);c-=e-g;0===d?d=void 0:(dbg_assert(d>=g),d-=g);a=new Uint8Array(a,c,d);this.write_blob(a,g);this.instruction_pointer[0]=this.get_seg_cs()+b|0}else if(1179403647===b[0]){dbg_log("Multiboot image is in elf format",LOG_CPU);c=read_elf(a);this.instruction_pointer[0]=this.get_seg_cs()+c.header.entry|0;for(g of c.program_headers)0!==g.type&&(1===g.type?(dbg_assert(g.paddr===g.vaddr),dbg_assert(g.filesz<=g.memsz),g.paddr+g.memsz<this.memory_size[0]?g.filesz&&(c=new Uint8Array(a,
@@ -598,18 +626,18 @@ CPU.prototype.dump_function_code=function(a,b){if(DEBUG&&DUMP_GENERATED_WASM){va
CPU.prototype.hlt_loop=function(){if(this.get_eflags_no_arith()&FLAG_INTERRUPT){const a=this.run_hardware_timers(v86.microtick());this.handle_irqs();return a}return 100};
CPU.prototype.run_hardware_timers=function(a){if(ENABLE_HPET)var b=this.devices.pit.timer(a,this.devices.hpet.legacy_mode),c=this.devices.rtc.timer(a,this.devices.hpet.legacy_mode),d=this.devices.hpet.timer(a);else b=this.devices.pit.timer(a,!1),c=this.devices.rtc.timer(a,!1),d=100;let e=100,g=100;this.acpi_enabled[0]&&(e=this.devices.acpi.timer(a),g=this.devices.apic.timer(a));return Math.min(b,c,d,e,g)};
CPU.prototype.hlt_op=function(){0===(this.get_eflags_no_arith()&FLAG_INTERRUPT)&&this.bus.send("cpu-event-halt");this.in_hlt[0]=1;this.hlt_loop()};CPU.prototype.handle_irqs=function(){this.get_eflags_no_arith()&FLAG_INTERRUPT&&(this.pic_acknowledge(),this.next_tick_immediately())};CPU.prototype.pic_acknowledge=function(){dbg_assert(this.get_eflags_no_arith()&FLAG_INTERRUPT);this.devices.pic&&this.devices.pic.acknowledge_irq();this.devices.apic&&this.devices.apic.acknowledge_irq()};
CPU.prototype.device_raise_irq=function(a){dbg_assert(1===arguments.length);this.devices.pic&&this.devices.pic.set_irq(a);this.devices.ioapic&&this.devices.ioapic.set_irq(a)};CPU.prototype.device_lower_irq=function(a){this.devices.pic&&this.devices.pic.clear_irq(a);this.devices.ioapic&&this.devices.ioapic.clear_irq(a)};"undefined"!==typeof window?window.CPU=CPU:"undefined"!==typeof module&&"undefined"!==typeof module.exports?module.exports.CPU=CPU:"function"===typeof importScripts&&(self.CPU=CPU);CPU.prototype.debug_init=function(){function a(l){if(DEBUG){for(var n=e.protected_mode[0]?"prot":"real",p=e.get_eflags(),t=e.getiopl(),r=e.cpl[0],q=h(e.sreg[REG_CS],4)+":"+h(e.get_real_eip()>>>0,8),v=h(e.sreg[REG_SS],4)+":"+h(e.reg32[REG_ES]>>>0,8),u=e.is_32[0]?"32":"16",x=e.flags[0]&FLAG_INTERRUPT?1:0,z={[FLAG_CARRY]:"c",[FLAG_PARITY]:"p",[FLAG_ADJUST]:"a",[FLAG_ZERO]:"z",[FLAG_SIGN]:"s",[FLAG_TRAP]:"t",[FLAG_INTERRUPT]:"i",[FLAG_DIRECTION]:"d",[FLAG_OVERFLOW]:"o"},A="",B=0;16>B;B++)z[1<<B]&&(A=
p&1<<B?A+z[1<<B]:A+" ");return"mode="+n+"/"+u+" paging="+ +(0!==(e.cr[0]&CR0_PG))+" pae="+ +(0!==(e.cr[4]&CR4_PAE))+" iopl="+t+" cpl="+r+" if="+x+" cs:eip="+q+" cs_off="+h(e.get_seg_cs()>>>0,8)+" flgs="+h(e.get_eflags()>>>0,6)+" ("+A+") ss:esp="+v+" ssize="+ +e.stack_size_32[0]+(l?" in "+l:"")}}function b(){for(var l={eax:REG_EAX,ecx:REG_ECX,edx:REG_EDX,ebx:REG_EBX,esp:REG_ESP,ebp:REG_EBP,esi:REG_ESI,edi:REG_EDI},n="eax ecx edx ebx esp ebp esi edi".split(" "),p="",t="",r=0;4>r;r++)p+=n[r]+"="+h(e.reg32[l[n[r]]]>>>
0,8)+" ",t+=n[r+4]+"="+h(e.reg32[l[n[r+4]]]>>>0,8)+" ";p+=" ds="+h(e.sreg[REG_DS],4)+" es="+h(e.sreg[REG_ES],4)+" fs="+h(e.sreg[REG_FS],4);t+=" gs="+h(e.sreg[REG_GS],4)+" cs="+h(e.sreg[REG_CS],4)+" ss="+h(e.sreg[REG_SS],4);return[p,t]}function c(l,n,p){if(DEBUG){if(!(l&1))return!1;var t=128===(l&128);return{size:t,global:256===(l&256),accessed:32===(l&32),dirty:64===(l&64),cache_disable:16===(l&16),user:4===(l&4),read_write:2===(l&2),address:(t&&!p?l&(n?4292870144:4290772992):l&4294963200)>>>0}}}
function d(l,n,p){if(DEBUG)for(var t=n?512:1024,r=n?8:4,q=n?21:22,v=0;v<t;v++){var u=e.read32s(l+v*r),x=c(u,n,!0);if(x)if(u="",u+=x.size?"S ":" ",u+=x.accessed?"A ":" ",u+=x.cache_disable?"Cd ":" ",u+=x.user?"U ":" ",u+=x.read_write?"Rw ":" ",x.size)dbg_log("=== "+h(p+(v<<q)>>>0,8)+" -> "+h(x.address>>>0,8)+" | "+u);else{dbg_log("=== "+h(p+(v<<q)>>>0,8)+" | "+u);for(var z=0;z<t;z++){var A=x.address+z*r;u=e.read32s(A);var B=c(u,n,!1);B&&(u="",u+=B.cache_disable?"Cd ":" ",u+=B.user?"U ":" ",
u+=B.read_write?"Rw ":" ",u+=B.global?"G ":" ",u+=B.accessed?"A ":" ",u+=B.dirty?"Di ":" ",dbg_log("# "+h(p+(v<<q|z<<12)>>>0,8)+" -> "+h(B.address,8)+" | "+u+" (at "+h(A,8)+")"))}}}}var e=this,g={};this.debug=g;g.init=function(){function l(p){10===p?(dbg_log(n,LOG_BIOS),n=""):n+=String.fromCharCode(p)}if(DEBUG&&e.io){var n="";e.io.register_write(1026,this,l);e.io.register_write(1280,this,l)}};g.get_regs_short=b;g.dump_regs=function(){if(DEBUG){var l=b();dbg_log(l[0],LOG_CPU);dbg_log(l[1],
LOG_CPU)}};g.get_state=a;g.dump_state=function(l){DEBUG&&dbg_log(a(l),LOG_CPU)};g.dump_stack=function(l,n){if(DEBUG){var p=e.reg32[REG_ESP];dbg_log("========= STACK ==========");if(n>=l||void 0===n)l=5,n=-5;for(;l>n;l--){var t=" ";l||(t="=> ");t+=h(l,2)+" | ";dbg_log(t+h(p+4*l,8)+" | "+h(e.read32s(p+4*l)>>>0))}}};g.dump_page_structures=function(){if(e.cr[4]&CR4_PAE){dbg_log("PAE enabled");for(var l=0;4>l;l++){var n=e.read32s(e.cr[3]+8*l);n&1&&d(n&4294963200,!0,l<<30)}}else dbg_log("PAE disabled"),
d(e.cr[3],!1,0)};g.dump_gdt_ldt=function(){function l(n,p){for(var t=0;t<p;t+=8,n+=8){var r=e.read16(n+2)|e.read8(n+4)<<16|e.read8(n+7)<<24,q=e.read16(n)|(e.read8(n+6)&15)<<16,v=e.read8(n+5),u=e.read8(n+6)>>4,x="",z=v>>5&3;x=v&128?x+" P ":x+"NP ";v&16?(x=u&4?x+"32b ":x+"16b ",v&8?(x+="X ",v&4&&(x+="C ")):x+="R ",x+="RW "):x+="sys: "+h(v&15);u&8&&(q=q<<12|4095);dbg_log(h(t&-8,4)+" "+h(r>>>0,8)+" ("+h(q>>>0,8)+" bytes) "+x+"; dpl = "+z+", a = "+v.toString(2)+", f = "+u.toString(2))}}DEBUG&&(dbg_log("gdt: (len = "+
h(e.gdtr_size[0])+")"),l(e.translate_address_system_read(e.gdtr_offset[0]),e.gdtr_size[0]),dbg_log("\nldt: (len = "+h(e.segment_limits[REG_LDTR])+")"),l(e.translate_address_system_read(e.segment_offsets[REG_LDTR]),e.segment_limits[REG_LDTR]))};g.dump_idt=function(){if(DEBUG)for(var l=0;l<e.idtr_size[0];l+=8){var n=e.translate_address_system_read(e.idtr_offset[0]+l),p=e.read16(n)|e.read16(n+6)<<16,t=e.read16(n+2);n=e.read8(n+5);var r=n>>5&3;var q=5===(n&31)?"task gate ":14===(n&31)?"intr gate ":15===
(n&31)?"trap gate ":"invalid ";q=n&128?q+" P":q+"NP";dbg_log(h(l>>3,4)+" "+h(p>>>0,8)+", "+h(t,4)+"; "+q+"; dpl = "+r+", t = "+n.toString(2))}};g.get_memory_dump=function(l,n){if(DEBUG)return void 0===l?(l=0,n=e.memory_size[0]):void 0===n&&(n=l,l=0),e.mem8.slice(l,l+n).buffer};g.memory_hex_dump=function(l,n){if(DEBUG){n=n||64;for(var p,t,r=0;r<n>>4;r++){p=h(l+(r<<4),5)+" ";for(var q=0;16>q;q++)t=e.read8(l+(r<<4)+q),p+=h(t,2)+" ";p+=" ";for(q=0;16>q;q++)t=e.read8(l+(r<<4)+q),p+=33>t||126<t?".":
String.fromCharCode(t);dbg_log(p)}}};g.used_memory_dump=function(){if(DEBUG)for(var l=e.memory_size[0]/128/16|0,n,p=0;16>p;p++){n=h(128*p*l,8)+" | ";for(var t=0;128>t;t++)n+=0<e.mem32s[(128*p+t)*l]?"X":" ";dbg_log(n)}};g.debug_interrupt=function(l){};let f,k;g.dump_code=function(l,n,p){if(!k){if(void 0===f&&(f="function"===typeof require?require("./capstone-x86.min.js"):window.cs,void 0===f)){dbg_log("Warning: Missing capstone library, disassembly not available");return}k=[new f.Capstone(f.ARCH_X86,
f.MODE_16),new f.Capstone(f.ARCH_X86,f.MODE_32)]}try{k[l].disasm(n,p).forEach(function(t){dbg_log(h(t.address>>>0)+": "+v86util.pads(t.bytes.map(r=>h(r,2).slice(-2)).join(" "),20)+" "+t.mnemonic+" "+t.op_str)}),dbg_log("")}catch(t){dbg_log("Could not disassemble: "+Array.from(n).map(r=>h(r,2)).join(" "))}};let m;g.dump_wasm=function(l){if(void 0===m&&(m="function"===typeof require?require("./libwabt.js"):new window.WabtModule,void 0===m)){dbg_log("Warning: Missing libwabt, wasm dump not available");
return}l=l.slice();try{var n=m.readWasm(l,{readDebugNames:!1});n.generateNames();n.applyNames();const r=n.toText({foldExprs:!0,inlineExport:!0});dbg_log(r)}catch(r){var p=new Blob([l]),t=document.createElement("a");t.download="failed.wasm";t.href=window.URL.createObjectURL(p);t.dataset.downloadurl=["application/octet-stream",t.download,t.href].join(":");t.click();window.URL.revokeObjectURL(t.src);console.log(r.toString())}finally{n&&n.destroy()}}};const ELF_MAGIC=1179403647;
CPU.prototype.device_raise_irq=function(a){dbg_assert(1===arguments.length);this.devices.pic&&this.devices.pic.set_irq(a);this.devices.ioapic&&this.devices.ioapic.set_irq(a)};CPU.prototype.device_lower_irq=function(a){this.devices.pic&&this.devices.pic.clear_irq(a);this.devices.ioapic&&this.devices.ioapic.clear_irq(a)};"undefined"!==typeof window?window.CPU=CPU:"undefined"!==typeof module&&"undefined"!==typeof module.exports?module.exports.CPU=CPU:"function"===typeof importScripts&&(self.CPU=CPU);CPU.prototype.debug_init=function(){function a(l){if(DEBUG){for(var n=e.protected_mode[0]?"prot":"real",q=e.get_eflags(),t=e.getiopl(),r=e.cpl[0],p=h(e.sreg[REG_CS],4)+":"+h(e.get_real_eip()>>>0,8),v=h(e.sreg[REG_SS],4)+":"+h(e.reg32[REG_ES]>>>0,8),u=e.is_32[0]?"32":"16",x=e.flags[0]&FLAG_INTERRUPT?1:0,z={[FLAG_CARRY]:"c",[FLAG_PARITY]:"p",[FLAG_ADJUST]:"a",[FLAG_ZERO]:"z",[FLAG_SIGN]:"s",[FLAG_TRAP]:"t",[FLAG_INTERRUPT]:"i",[FLAG_DIRECTION]:"d",[FLAG_OVERFLOW]:"o"},C="",D=0;16>D;D++)z[1<<D]&&(C=
q&1<<D?C+z[1<<D]:C+" ");return"mode="+n+"/"+u+" paging="+ +(0!==(e.cr[0]&CR0_PG))+" pae="+ +(0!==(e.cr[4]&CR4_PAE))+" iopl="+t+" cpl="+r+" if="+x+" cs:eip="+p+" cs_off="+h(e.get_seg_cs()>>>0,8)+" flgs="+h(e.get_eflags()>>>0,6)+" ("+C+") ss:esp="+v+" ssize="+ +e.stack_size_32[0]+(l?" in "+l:"")}}function b(){if(DEBUG){for(var l={eax:REG_EAX,ecx:REG_ECX,edx:REG_EDX,ebx:REG_EBX,esp:REG_ESP,ebp:REG_EBP,esi:REG_ESI,edi:REG_EDI},n="eax ecx edx ebx esp ebp esi edi".split(" "),q="",t="",r=0;4>r;r++)q+=n[r]+
"="+h(e.reg32[l[n[r]]]>>>0,8)+" ",t+=n[r+4]+"="+h(e.reg32[l[n[r+4]]]>>>0,8)+" ";q+=" ds="+h(e.sreg[REG_DS],4)+" es="+h(e.sreg[REG_ES],4)+" fs="+h(e.sreg[REG_FS],4);t+=" gs="+h(e.sreg[REG_GS],4)+" cs="+h(e.sreg[REG_CS],4)+" ss="+h(e.sreg[REG_SS],4);return[q,t]}}function c(l,n,q){if(DEBUG){if(!(l&1))return!1;var t=128===(l&128);return{size:t,global:256===(l&256),accessed:32===(l&32),dirty:64===(l&64),cache_disable:16===(l&16),user:4===(l&4),read_write:2===(l&2),address:(t&&!q?l&(n?4292870144:4290772992):
l&4294963200)>>>0}}}function d(l,n,q){if(DEBUG)for(var t=n?512:1024,r=n?8:4,p=n?21:22,v=0;v<t;v++){var u=e.read32s(l+v*r),x=c(u,n,!0);if(x)if(u="",u+=x.size?"S ":" ",u+=x.accessed?"A ":" ",u+=x.cache_disable?"Cd ":" ",u+=x.user?"U ":" ",u+=x.read_write?"Rw ":" ",x.size)dbg_log("=== "+h(q+(v<<p)>>>0,8)+" -> "+h(x.address>>>0,8)+" | "+u);else{dbg_log("=== "+h(q+(v<<p)>>>0,8)+" | "+u);for(var z=0;z<t;z++){var C=x.address+z*r;u=e.read32s(C);var D=c(u,n,!1);D&&(u="",u+=D.cache_disable?"Cd ":" ",
u+=D.user?"U ":" ",u+=D.read_write?"Rw ":" ",u+=D.global?"G ":" ",u+=D.accessed?"A ":" ",u+=D.dirty?"Di ":" ",dbg_log("# "+h(q+(v<<p|z<<12)>>>0,8)+" -> "+h(D.address,8)+" | "+u+" (at "+h(C,8)+")"))}}}}var e=this,g={};this.debug=g;g.init=function(){function l(q){10===q?(dbg_log(n,LOG_BIOS),n=""):n+=String.fromCharCode(q)}if(DEBUG&&e.io){var n="";e.io.register_write(1026,this,l);e.io.register_write(1280,this,l)}};g.get_regs_short=b;g.dump_regs=function(){if(DEBUG){var l=b();dbg_log(l[0],
LOG_CPU);dbg_log(l[1],LOG_CPU)}};g.get_state=a;g.dump_state=function(l){DEBUG&&dbg_log(a(l),LOG_CPU)};g.dump_stack=function(l,n){if(DEBUG){var q=e.reg32[REG_ESP];dbg_log("========= STACK ==========");if(n>=l||void 0===n)l=5,n=-5;for(;l>n;l--){var t=" ";l||(t="=> ");t+=h(l,2)+" | ";dbg_log(t+h(q+4*l,8)+" | "+h(e.read32s(q+4*l)>>>0))}}};g.dump_page_structures=function(){if(e.cr[4]&CR4_PAE){dbg_log("PAE enabled");for(var l=0;4>l;l++){var n=e.read32s(e.cr[3]+8*l);n&1&&d(n&4294963200,!0,l<<30)}}else dbg_log("PAE disabled"),
d(e.cr[3],!1,0)};g.dump_gdt_ldt=function(){function l(n,q){for(var t=0;t<q;t+=8,n+=8){var r=e.read16(n+2)|e.read8(n+4)<<16|e.read8(n+7)<<24,p=e.read16(n)|(e.read8(n+6)&15)<<16,v=e.read8(n+5),u=e.read8(n+6)>>4,x="",z=v>>5&3;x=v&128?x+" P ":x+"NP ";v&16?(x=u&4?x+"32b ":x+"16b ",v&8?(x+="X ",v&4&&(x+="C ")):x+="R ",x+="RW "):x+="sys: "+h(v&15);u&8&&(p=p<<12|4095);dbg_log(h(t&-8,4)+" "+h(r>>>0,8)+" ("+h(p>>>0,8)+" bytes) "+x+"; dpl = "+z+", a = "+v.toString(2)+", f = "+u.toString(2))}}DEBUG&&(dbg_log("gdt: (len = "+
h(e.gdtr_size[0])+")"),l(e.translate_address_system_read(e.gdtr_offset[0]),e.gdtr_size[0]),dbg_log("\nldt: (len = "+h(e.segment_limits[REG_LDTR])+")"),l(e.translate_address_system_read(e.segment_offsets[REG_LDTR]),e.segment_limits[REG_LDTR]))};g.dump_idt=function(){if(DEBUG)for(var l=0;l<e.idtr_size[0];l+=8){var n=e.translate_address_system_read(e.idtr_offset[0]+l),q=e.read16(n)|e.read16(n+6)<<16,t=e.read16(n+2);n=e.read8(n+5);var r=n>>5&3;var p=5===(n&31)?"task gate ":14===(n&31)?"intr gate ":15===
(n&31)?"trap gate ":"invalid ";p=n&128?p+" P":p+"NP";dbg_log(h(l>>3,4)+" "+h(q>>>0,8)+", "+h(t,4)+"; "+p+"; dpl = "+r+", t = "+n.toString(2))}};g.get_memory_dump=function(l,n){if(DEBUG)return void 0===l?(l=0,n=e.memory_size[0]):void 0===n&&(n=l,l=0),e.mem8.slice(l,l+n).buffer};g.memory_hex_dump=function(l,n){if(DEBUG){n=n||64;for(var q,t,r=0;r<n>>4;r++){q=h(l+(r<<4),5)+" ";for(var p=0;16>p;p++)t=e.read8(l+(r<<4)+p),q+=h(t,2)+" ";q+=" ";for(p=0;16>p;p++)t=e.read8(l+(r<<4)+p),q+=33>t||126<t?".":
String.fromCharCode(t);dbg_log(q)}}};g.used_memory_dump=function(){if(DEBUG)for(var l=e.memory_size[0]/128/16|0,n,q=0;16>q;q++){n=h(128*q*l,8)+" | ";for(var t=0;128>t;t++)n+=0<e.mem32s[(128*q+t)*l]?"X":" ";dbg_log(n)}};g.debug_interrupt=function(l){};let f,k;g.dump_code=function(l,n,q){if(!k){if(void 0===f&&(f="function"===typeof require?require("./capstone-x86.min.js"):window.cs,void 0===f)){dbg_log("Warning: Missing capstone library, disassembly not available");return}k=[new f.Capstone(f.ARCH_X86,
f.MODE_16),new f.Capstone(f.ARCH_X86,f.MODE_32)]}try{k[l].disasm(n,q).forEach(function(t){dbg_log(h(t.address>>>0)+": "+v86util.pads(t.bytes.map(r=>h(r,2).slice(-2)).join(" "),20)+" "+t.mnemonic+" "+t.op_str)}),dbg_log("")}catch(t){dbg_log("Could not disassemble: "+Array.from(n).map(r=>h(r,2)).join(" "))}};let m;g.dump_wasm=function(l){if(void 0===m&&(m="function"===typeof require?require("./libwabt.js"):new window.WabtModule,void 0===m)){dbg_log("Warning: Missing libwabt, wasm dump not available");
return}l=l.slice();try{var n=m.readWasm(l,{readDebugNames:!1});n.generateNames();n.applyNames();const r=n.toText({foldExprs:!0,inlineExport:!0});dbg_log(r)}catch(r){var q=new Blob([l]),t=document.createElement("a");t.download="failed.wasm";t.href=window.URL.createObjectURL(q);t.dataset.downloadurl=["application/octet-stream",t.download,t.href].join(":");t.click();window.URL.revokeObjectURL(t.src);console.log(r.toString())}finally{n&&n.destroy()}}};const ELF_MAGIC=1179403647;
let types=DataView.prototype,U8={size:1,get:types.getUint8,set:types.setUint8},U16={size:2,get:types.getUint16,set:types.setUint16},U32={size:4,get:types.getUint32,set:types.setUint32},pad=function(a){return{size:a,get:b=>-1}},Header=create_struct([{magic:U32},{class:U8},{data:U8},{version0:U8},{osabi:U8},{abiversion:U8},{pad0:pad(7)},{type:U16},{machine:U16},{version1:U32},{entry:U32},{phoff:U32},{shoff:U32},{flags:U32},{ehsize:U16},{phentsize:U16},{phnum:U16},{shentsize:U16},{shnum:U16},{shstrndx:U16}]);
console.assert(52===Header.reduce((a,b)=>a+b.size,0));let ProgramHeader=create_struct([{type:U32},{offset:U32},{vaddr:U32},{paddr:U32},{filesz:U32},{memsz:U32},{flags:U32},{align:U32}]);console.assert(32===ProgramHeader.reduce((a,b)=>a+b.size,0));let SectionHeader=create_struct([{name:U32},{type:U32},{flags:U32},{addr:U32},{offset:U32},{size:U32},{link:U32},{info:U32},{addralign:U32},{entsize:U32}]);console.assert(40===SectionHeader.reduce((a,b)=>a+b.size,0));
function create_struct(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}})}
@@ -620,25 +648,25 @@ program_headers:d,sections_headers:a}}function read_struct(a,b){let c={},d=0;for
564,LINUX_BOOT_HDR_MIN_ALIGNMENT=565,LINUX_BOOT_HDR_XLOADFLAGS=566,LINUX_BOOT_HDR_CMDLINE_SIZE=568,LINUX_BOOT_HDR_PAYLOAD_OFFSET=584,LINUX_BOOT_HDR_PAYLOAD_LENGTH=588,LINUX_BOOT_HDR_PREF_ADDRESS=600,LINUX_BOOT_HDR_INIT_SIZE=608,LINUX_BOOT_HDR_CHECKSUM1=43605,LINUX_BOOT_HDR_CHECKSUM2=1400005704,LINUX_BOOT_HDR_TYPE_OF_LOADER_NOT_ASSIGNED=255,LINUX_BOOT_HDR_LOADFLAGS_LOADED_HIGH=1,LINUX_BOOT_HDR_LOADFLAGS_QUIET_FLAG=32,LINUX_BOOT_HDR_LOADFLAGS_KEEP_SEGMENTS=64,LINUX_BOOT_HDR_LOADFLAGS_CAN_USE_HEAPS=
128;
function load_kernel(a,b,c,d){dbg_log("Trying to load kernel of size "+b.byteLength);var e=new Uint8Array(b);const g=new Uint16Array(b),f=new Uint32Array(b);var k=e[LINUX_BOOT_HDR_SETUP_SECTS]||4,m=g[LINUX_BOOT_HDR_BOOT_FLAG>>1];if(m!==LINUX_BOOT_HDR_CHECKSUM1)dbg_log("Bad checksum1: "+h(m));else if(m=g[LINUX_BOOT_HDR_HEADER>>1]|g[LINUX_BOOT_HDR_HEADER+2>>1]<<16,m!==LINUX_BOOT_HDR_CHECKSUM2)dbg_log("Bad checksum2: "+h(m));else{m=g[LINUX_BOOT_HDR_VERSION>>1];dbg_assert(514<=m);var l=e[LINUX_BOOT_HDR_LOADFLAGS];dbg_assert(l&
LINUX_BOOT_HDR_LOADFLAGS_LOADED_HIGH);var n=g[LINUX_BOOT_HDR_XLOADFLAGS>>1],p=f[LINUX_BOOT_HDR_INITRD_ADDR_MAX>>2],t=f[LINUX_BOOT_HDR_KERNEL_ALIGNMENT>>2],r=e[LINUX_BOOT_HDR_RELOCATABLE_KERNEL],q=e[LINUX_BOOT_HDR_MIN_ALIGNMENT],v=f[LINUX_BOOT_HDR_CMDLINE_SIZE>>2],u=f[LINUX_BOOT_HDR_PAYLOAD_OFFSET>>2],x=f[LINUX_BOOT_HDR_PAYLOAD_LENGTH>>2],z=f[LINUX_BOOT_HDR_PREF_ADDRESS>>2],A=f[LINUX_BOOT_HDR_PREF_ADDRESS+4>>2],B=f[LINUX_BOOT_HDR_INIT_SIZE>>2];dbg_log("kernel boot protocol version: "+h(m));dbg_log("flags="+
h(l)+" xflags="+h(n));dbg_log("code32_start="+h(f[LINUX_BOOT_HDR_CODE32_START>>2]));dbg_log("initrd_addr_max="+h(p));dbg_log("kernel_alignment="+h(t));dbg_log("relocatable="+r);dbg_log("min_alignment="+h(q));dbg_log("cmdline max="+h(v));dbg_log("payload offset="+h(u)+" size="+h(x));dbg_log("pref_address="+h(A)+":"+h(z));dbg_log("init_size="+h(B));e[LINUX_BOOT_HDR_TYPE_OF_LOADER]=LINUX_BOOT_HDR_TYPE_OF_LOADER_NOT_ASSIGNED;e[LINUX_BOOT_HDR_LOADFLAGS]=l&~LINUX_BOOT_HDR_LOADFLAGS_QUIET_FLAG&~LINUX_BOOT_HDR_LOADFLAGS_KEEP_SEGMENTS|
LINUX_BOOT_HDR_LOADFLAGS_LOADED_HIGH);var n=g[LINUX_BOOT_HDR_XLOADFLAGS>>1],q=f[LINUX_BOOT_HDR_INITRD_ADDR_MAX>>2],t=f[LINUX_BOOT_HDR_KERNEL_ALIGNMENT>>2],r=e[LINUX_BOOT_HDR_RELOCATABLE_KERNEL],p=e[LINUX_BOOT_HDR_MIN_ALIGNMENT],v=f[LINUX_BOOT_HDR_CMDLINE_SIZE>>2],u=f[LINUX_BOOT_HDR_PAYLOAD_OFFSET>>2],x=f[LINUX_BOOT_HDR_PAYLOAD_LENGTH>>2],z=f[LINUX_BOOT_HDR_PREF_ADDRESS>>2],C=f[LINUX_BOOT_HDR_PREF_ADDRESS+4>>2],D=f[LINUX_BOOT_HDR_INIT_SIZE>>2];dbg_log("kernel boot protocol version: "+h(m));dbg_log("flags="+
h(l)+" xflags="+h(n));dbg_log("code32_start="+h(f[LINUX_BOOT_HDR_CODE32_START>>2]));dbg_log("initrd_addr_max="+h(q));dbg_log("kernel_alignment="+h(t));dbg_log("relocatable="+r);dbg_log("min_alignment="+h(p));dbg_log("cmdline max="+h(v));dbg_log("payload offset="+h(u)+" size="+h(x));dbg_log("pref_address="+h(C)+":"+h(z));dbg_log("init_size="+h(D));e[LINUX_BOOT_HDR_TYPE_OF_LOADER]=LINUX_BOOT_HDR_TYPE_OF_LOADER_NOT_ASSIGNED;e[LINUX_BOOT_HDR_LOADFLAGS]=l&~LINUX_BOOT_HDR_LOADFLAGS_QUIET_FLAG&~LINUX_BOOT_HDR_LOADFLAGS_KEEP_SEGMENTS|
LINUX_BOOT_HDR_LOADFLAGS_CAN_USE_HEAPS;g[LINUX_BOOT_HDR_HEAP_END_PTR>>1]=56832;g[LINUX_BOOT_HDR_VIDMODE>>1]=65535;dbg_log("heap_end_ptr="+h(56832));d+="\x00";dbg_assert(d.length<v);dbg_log("cmd_line_ptr="+h(581632));f[LINUX_BOOT_HDR_CMD_LINE_PTR>>2]=581632;for(e=0;e<d.length;e++)a[581632+e]=d.charCodeAt(e);k=512*(k+1);dbg_log("prot_mode_kernel_start="+h(k));d=new Uint8Array(b,0,k);b=new Uint8Array(b,k);e=k=0;c&&(k=67108864,e=c.byteLength,dbg_assert(1048576+b.length<k),a.set(new Uint8Array(c),k));
f[LINUX_BOOT_HDR_RAMDISK_IMAGE>>2]=k;f[LINUX_BOOT_HDR_RAMDISK_SIZE>>2]=e;dbg_assert(655360>524288+d.length);a.set(d,524288);a.set(b,1048576);return{option_rom:{name:"genroms/kernel.bin",data:make_linux_boot_rom(32768,57344)}}}}
function make_linux_boot_rom(a,b){const c=new Uint8Array(256);(new Uint16Array(c.buffer))[0]=43605;c[2]=1;var d=3;c[d++]=250;c[d++]=184;c[d++]=a>>0;c[d++]=a>>8;c[d++]=142;c[d++]=192;c[d++]=142;c[d++]=216;c[d++]=142;c[d++]=224;c[d++]=142;c[d++]=232;c[d++]=142;c[d++]=208;c[d++]=188;c[d++]=b>>0;c[d++]=b>>8;c[d++]=234;c[d++]=0;c[d++]=0;c[d++]=a+32>>0;c[d++]=a+32>>8;dbg_assert(512>d);a=d;b=c[a]=0;for(d=0;d<c.length;d++)b+=c[d];c[a]=-b;return c};var SHIFT_SCAN_CODE=42,SCAN_CODE_RELEASE=128;
function KeyboardAdapter(a){function b(r){!r.altKey&&k[56]&&g(56,!1);return e(r,!1)}function c(r){!r.altKey&&k[56]&&g(56,!1);return e(r,!0)}function d(r){r=Object.keys(k);for(var q,v=0;v<r.length;v++)q=+r[v],k[q]&&g(q,!1);k={}}function e(r,q){var v;if(v=m.bus)v=r.shiftKey&&r.ctrlKey&&(73===r.keyCode||74===r.keyCode||75===r.keyCode)||!m.emu_enabled?!1:r.target?r.target.classList.contains("phone_keyboard")||"INPUT"!==r.target.nodeName&&"TEXTAREA"!==r.target.nodeName:!0;if(v){a:{if(void 0!==r.code&&
(v=t[r.code],void 0!==v))break a;v=l[r.keyCode]}if(v)return g(v,q,r.repeat),r.preventDefault&&r.preventDefault(),!1;console.log("Missing char in map: keyCode="+(r.keyCode||-1).toString(16)+" code="+r.code)}}function g(r,q,v){if(q)k[r]&&!v&&g(r,!1);else if(!k[r])return;(k[r]=q)||(r|=128);255<r?(f(r>>8),f(r&255)):f(r)}function f(r){m.bus.send("keyboard-code",r)}var k={},m=this;this.emu_enabled=!0;var l=new Uint16Array([0,0,0,0,0,0,0,0,14,15,0,0,0,28,0,0,42,29,56,0,58,0,0,0,0,0,0,1,0,0,0,0,57,57417,
function KeyboardAdapter(a){function b(r){!r.altKey&&k[56]&&g(56,!1);return e(r,!1)}function c(r){!r.altKey&&k[56]&&g(56,!1);return e(r,!0)}function d(r){r=Object.keys(k);for(var p,v=0;v<r.length;v++)p=+r[v],k[p]&&g(p,!1);k={}}function e(r,p){var v;if(v=m.bus)v=r.shiftKey&&r.ctrlKey&&(73===r.keyCode||74===r.keyCode||75===r.keyCode)||!m.emu_enabled?!1:r.target?r.target.classList.contains("phone_keyboard")||"INPUT"!==r.target.nodeName&&"TEXTAREA"!==r.target.nodeName:!0;if(v){a:{if(void 0!==r.code&&
(v=t[r.code],void 0!==v))break a;v=l[r.keyCode]}if(v)return g(v,p,r.repeat),r.preventDefault&&r.preventDefault(),!1;console.log("Missing char in map: keyCode="+(r.keyCode||-1).toString(16)+" code="+r.code)}}function g(r,p,v){if(p)k[r]&&!v&&g(r,!1);else if(!k[r])return;(k[r]=p)||(r|=128);255<r?(f(r>>8),f(r&255)):f(r)}function f(r){m.bus.send("keyboard-code",r)}var k={},m=this;this.emu_enabled=!0;var l=new Uint16Array([0,0,0,0,0,0,0,0,14,15,0,0,0,28,0,0,42,29,56,0,58,0,0,0,0,0,0,1,0,0,0,0,57,57417,
57425,57423,57415,57419,57416,57421,80,0,0,0,0,82,83,0,11,2,3,4,5,6,7,8,9,10,0,39,0,13,0,0,0,30,48,46,32,18,33,34,35,23,36,37,38,50,49,24,25,16,19,31,20,22,47,17,45,21,44,57435,57436,57437,0,0,82,79,80,81,75,76,77,71,72,73,0,0,0,0,0,0,59,60,61,62,63,64,65,66,67,68,87,88,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0,0,39,13,51,12,52,53,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,43,27,40,0,57435,57400,0,0,0,
0,0,0,0,0,0,0,0,0,0,0]),n={8:8,10:13,32:32,39:222,44:188,45:189,46:190,47:191,48:48,49:49,50:50,51:51,52:52,53:53,54:54,55:55,56:56,57:57,59:186,61:187,91:219,92:220,93:221,96:192,97:65,98:66,99:67,100:68,101:69,102:70,103:71,104:72,105:73,106:74,107:75,108:76,109:77,110:78,111:79,112:80,113:81,114:82,115:83,116:84,117:85,118:86,119:87,120:88,121:89,122:90},p={33:49,34:222,35:51,36:52,37:53,38:55,40:57,41:48,42:56,43:187,58:186,60:188,62:190,63:191,64:50,65:65,66:66,67:67,68:68,69:69,70:70,71:71,
0,0,0,0,0,0,0,0,0,0,0]),n={8:8,10:13,32:32,39:222,44:188,45:189,46:190,47:191,48:48,49:49,50:50,51:51,52:52,53:53,54:54,55:55,56:56,57:57,59:186,61:187,91:219,92:220,93:221,96:192,97:65,98:66,99:67,100:68,101:69,102:70,103:71,104:72,105:73,106:74,107:75,108:76,109:77,110:78,111:79,112:80,113:81,114:82,115:83,116:84,117:85,118:86,119:87,120:88,121:89,122:90},q={33:49,34:222,35:51,36:52,37:53,38:55,40:57,41:48,42:56,43:187,58:186,60:188,62:190,63:191,64:50,65:65,66:66,67:67,68:68,69:69,70:70,71:71,
72:72,73:73,74:74,75:75,76:76,77:77,78:78,79:79,80:80,81:81,82:82,83:83,84:84,85:85,86:86,87:87,88:88,89:89,90:90,94:54,95:189,123:219,124:220,125:221,126:192},t={Escape:1,Digit1:2,Digit2:3,Digit3:4,Digit4:5,Digit5:6,Digit6:7,Digit7:8,Digit8:9,Digit9:10,Digit0:11,Minus:12,Equal:13,Backspace:14,Tab:15,KeyQ:16,KeyW:17,KeyE:18,KeyR:19,KeyT:20,KeyY:21,KeyU:22,KeyI:23,KeyO:24,KeyP:25,BracketLeft:26,BracketRight:27,Enter:28,ControlLeft:29,KeyA:30,KeyS:31,KeyD:32,KeyF:33,KeyG:34,KeyH:35,KeyJ:36,KeyK:37,
KeyL:38,Semicolon:39,Quote:40,Backquote:41,ShiftLeft:42,Backslash:43,KeyZ:44,KeyX:45,KeyC:46,KeyV:47,KeyB:48,KeyN:49,KeyM:50,Comma:51,Period:52,Slash:53,ShiftRight:54,NumpadMultiply:55,AltLeft:56,Space:57,CapsLock:58,F1:59,F2:60,F3:61,F4:62,F5:63,F6:64,F7:65,F8:66,F9:67,F10:68,NumLock:69,ScrollLock:70,Numpad7:71,Numpad8:72,Numpad9:73,NumpadSubtract:74,Numpad4:75,Numpad5:76,Numpad6:77,NumpadAdd:78,Numpad1:79,Numpad2:80,Numpad3:81,Numpad0:82,NumpadDecimal:83,IntlBackslash:86,F11:87,F12:88,NumpadEnter:57372,
ControlRight:57373,NumpadDivide:57397,AltRight:57400,Home:57415,ArrowUp:57416,PageUp:57417,ArrowLeft:57419,ArrowRight:57421,End:57423,ArrowDown:57424,PageDown:57425,Insert:57426,Delete:57427,OSLeft:57435,OSRight:57436,ContextMenu:57437};this.bus=a;this.destroy=function(){"undefined"!==typeof window&&(window.removeEventListener("keyup",b,!1),window.removeEventListener("keydown",c,!1),window.removeEventListener("blur",d,!1))};this.init=function(){"undefined"!==typeof window&&(this.destroy(),window.addEventListener("keyup",
b,!1),window.addEventListener("keydown",c,!1),window.addEventListener("blur",d,!1))};this.init();this.simulate_press=function(r){r={keyCode:r};e(r,!0);e(r,!1)};this.simulate_char=function(r){var q=r.charCodeAt(0);q in n?this.simulate_press(n[q]):q in p?(f(SHIFT_SCAN_CODE),this.simulate_press(p[q]),f(SHIFT_SCAN_CODE|SCAN_CODE_RELEASE)):console.log("ascii -> keyCode not found: ",q,r)}};function MouseAdapter(a,b){function c(u){if(!v.enabled||!v.emu_enabled)return!1;var x=b||document.body,z;if(!(z=document.pointerLockElement))a:{for(u=u.target;u.parentNode;){if(u===x){z=!0;break a}u=u.parentNode}z=!1}return z}function d(u){c(u)&&(u=u.changedTouches)&&u.length&&(u=u[u.length-1],r=u.clientX,q=u.clientY)}function e(u){if(n||t||p)v.bus.send("mouse-click",[!1,!1,!1]),n=t=p=!1}function g(u){if(v.bus&&c(u)){var x=0,z=0,A=u.changedTouches;A?A.length&&(A=A[A.length-1],x=A.clientX-r,z=A.clientY-
q,r=A.clientX,q=A.clientY,u.preventDefault()):"number"===typeof u.movementX?(x=u.movementX,z=u.movementY):"number"===typeof u.webkitMovementX?(x=u.webkitMovementX,z=u.webkitMovementY):"number"===typeof u.mozMovementX?(x=u.mozMovementX,z=u.mozMovementY):(x=u.clientX-r,z=u.clientY-q,r=u.clientX,q=u.clientY);v.bus.send("mouse-delta",[.15*x,-(.15*z)]);b&&v.bus.send("mouse-absolute",[u.pageX-b.offsetLeft,u.pageY-b.offsetTop,b.offsetWidth,b.offsetHeight])}}function f(u){c(u)&&m(u,!0)}function k(u){c(u)&&
m(u,!1)}function m(u,x){v.bus&&(1===u.which?n=x:2===u.which?t=x:3===u.which?p=x:dbg_log("Unknown event.which: "+u.which),v.bus.send("mouse-click",[n,t,p]),u.preventDefault())}function l(u){if(c(u)){var x=u.wheelDelta||-u.detail;0>x?x=-1:0<x&&(x=1);v.bus.send("mouse-wheel",[x,0]);u.preventDefault()}}var n=!1,p=!1,t=!1,r=0,q=0,v=this;this.enabled=!1;this.emu_enabled=!0;this.bus=a;this.bus.register("mouse-enable",function(u){this.enabled=u},this);this.destroy=function(){"undefined"!==typeof window&&
(window.removeEventListener("touchstart",d,!1),window.removeEventListener("touchend",e,!1),window.removeEventListener("touchmove",g,!1),window.removeEventListener("mousemove",g,!1),window.removeEventListener("mousedown",f,!1),window.removeEventListener("mouseup",k,!1),window.removeEventListener("DOMMouseScroll",l,!1),window.removeEventListener("mousewheel",l,{passive:!1}))};this.init=function(){"undefined"!==typeof window&&(this.destroy(),window.addEventListener("touchstart",d,!1),window.addEventListener("touchend",
e,!1),window.addEventListener("touchmove",g,!1),window.addEventListener("mousemove",g,!1),window.addEventListener("mousedown",f,!1),window.addEventListener("mouseup",k,!1),window.addEventListener("DOMMouseScroll",l,!1),window.addEventListener("mousewheel",l,{passive:!1}))};this.init()};var DAC_QUEUE_RESERVE=.2,AUDIOBUFFER_MINIMUM_SAMPLING_RATE=8E3;
b,!1),window.addEventListener("keydown",c,!1),window.addEventListener("blur",d,!1))};this.init();this.simulate_press=function(r){r={keyCode:r};e(r,!0);e(r,!1)};this.simulate_char=function(r){var p=r.charCodeAt(0);p in n?this.simulate_press(n[p]):p in q?(f(SHIFT_SCAN_CODE),this.simulate_press(q[p]),f(SHIFT_SCAN_CODE|SCAN_CODE_RELEASE)):console.log("ascii -> keyCode not found: ",p,r)}};function MouseAdapter(a,b){function c(u){if(!v.enabled||!v.emu_enabled)return!1;var x=b||document.body,z;if(!(z=document.pointerLockElement))a:{for(u=u.target;u.parentNode;){if(u===x){z=!0;break a}u=u.parentNode}z=!1}return z}function d(u){c(u)&&(u=u.changedTouches)&&u.length&&(u=u[u.length-1],r=u.clientX,p=u.clientY)}function e(u){if(n||t||q)v.bus.send("mouse-click",[!1,!1,!1]),n=t=q=!1}function g(u){if(v.bus&&c(u)&&v.is_running){var x=0,z=0,C=u.changedTouches;C?C.length&&(C=C[C.length-1],x=C.clientX-
r,z=C.clientY-p,r=C.clientX,p=C.clientY,u.preventDefault()):"number"===typeof u.movementX?(x=u.movementX,z=u.movementY):"number"===typeof u.webkitMovementX?(x=u.webkitMovementX,z=u.webkitMovementY):"number"===typeof u.mozMovementX?(x=u.mozMovementX,z=u.mozMovementY):(x=u.clientX-r,z=u.clientY-p,r=u.clientX,p=u.clientY);v.bus.send("mouse-delta",[.15*x,-(.15*z)]);b&&v.bus.send("mouse-absolute",[u.pageX-b.offsetLeft,u.pageY-b.offsetTop,b.offsetWidth,b.offsetHeight])}}function f(u){c(u)&&m(u,!0)}function k(u){c(u)&&
m(u,!1)}function m(u,x){v.bus&&(1===u.which?n=x:2===u.which?t=x:3===u.which?q=x:dbg_log("Unknown event.which: "+u.which),v.bus.send("mouse-click",[n,t,q]),u.preventDefault())}function l(u){if(c(u)){var x=u.wheelDelta||-u.detail;0>x?x=-1:0<x&&(x=1);v.bus.send("mouse-wheel",[x,0]);u.preventDefault()}}var n=!1,q=!1,t=!1,r=0,p=0,v=this;this.enabled=!1;this.emu_enabled=!0;this.bus=a;this.bus.register("mouse-enable",function(u){this.enabled=u},this);this.is_running=!1;this.bus.register("emulator-stopped",
function(){this.is_running=!1},this);this.bus.register("emulator-started",function(){this.is_running=!0},this);this.destroy=function(){"undefined"!==typeof window&&(window.removeEventListener("touchstart",d,!1),window.removeEventListener("touchend",e,!1),window.removeEventListener("touchmove",g,!1),window.removeEventListener("mousemove",g,!1),window.removeEventListener("mousedown",f,!1),window.removeEventListener("mouseup",k,!1),window.removeEventListener("wheel",l,{passive:!1}))};this.init=function(){"undefined"!==
typeof window&&(this.destroy(),window.addEventListener("touchstart",d,!1),window.addEventListener("touchend",e,!1),window.addEventListener("touchmove",g,!1),window.addEventListener("mousemove",g,!1),window.addEventListener("mousedown",f,!1),window.addEventListener("mouseup",k,!1),window.addEventListener("wheel",l,{passive:!1}))};this.init()};var DAC_QUEUE_RESERVE=.2,AUDIOBUFFER_MINIMUM_SAMPLING_RATE=8E3;
function SpeakerAdapter(a){if("undefined"!==typeof window)if(window.AudioContext||window.webkitAudioContext){var b=window.AudioWorklet?SpeakerWorkletDAC:SpeakerBufferSourceDAC;this.bus=a;this.audio_context=window.AudioContext?new AudioContext:new webkitAudioContext;this.mixer=new SpeakerMixer(a,this.audio_context);this.pcspeaker=new PCSpeaker(a,this.audio_context,this.mixer);this.dac=new b(a,this.audio_context,this.mixer);this.pcspeaker.start();a.register("emulator-stopped",function(){this.audio_context.suspend()},
this);a.register("emulator-started",function(){this.audio_context.resume()},this);a.register("speaker-confirm-initialized",function(){a.send("speaker-has-initialized")},this);a.send("speaker-has-initialized")}else console.warn("Web browser doesn't support Web Audio API")}
this);a.register("emulator-started",function(){this.audio_context.resume()},this);a.register("speaker-confirm-initialized",function(){a.send("speaker-has-initialized")},this);a.send("speaker-has-initialized")}else console.warn("Web browser doesn't support Web Audio API")}SpeakerAdapter.prototype.destroy=function(){this.audio_context&&this.audio_context.close();this.dac&&this.dac.node_processor&&this.dac.node_processor.port.close()};
function SpeakerMixer(a,b){function c(d){return function(e){d.gain.setValueAtTime(e,this.audio_context.currentTime)}}this.audio_context=b;this.sources=new Map;this.gain_right=this.gain_left=this.volume_right=this.volume_left=this.volume_both=1;this.node_treble_left=this.audio_context.createBiquadFilter();this.node_treble_right=this.audio_context.createBiquadFilter();this.node_treble_left.type="highshelf";this.node_treble_right.type="highshelf";this.node_treble_left.frequency.setValueAtTime(2E3,this.audio_context.currentTime);
this.node_treble_right.frequency.setValueAtTime(2E3,this.audio_context.currentTime);this.node_bass_left=this.audio_context.createBiquadFilter();this.node_bass_right=this.audio_context.createBiquadFilter();this.node_bass_left.type="lowshelf";this.node_bass_right.type="lowshelf";this.node_bass_left.frequency.setValueAtTime(200,this.audio_context.currentTime);this.node_bass_right.frequency.setValueAtTime(200,this.audio_context.currentTime);this.node_gain_left=this.audio_context.createGain();this.node_gain_right=
this.audio_context.createGain();this.node_merger=this.audio_context.createChannelMerger(2);this.input_left=this.node_treble_left;this.input_right=this.node_treble_right;this.node_treble_left.connect(this.node_bass_left);this.node_bass_left.connect(this.node_gain_left);this.node_gain_left.connect(this.node_merger,0,0);this.node_treble_right.connect(this.node_bass_right);this.node_bass_right.connect(this.node_gain_right);this.node_gain_right.connect(this.node_merger,0,1);this.node_merger.connect(this.audio_context.destination);
@@ -654,11 +682,11 @@ SpeakerMixerSource.prototype.set_volume=function(a,b){void 0===b&&(b=MIXER_CHANN
function PCSpeaker(a,b,c){this.node_oscillator=b.createOscillator();this.node_oscillator.type="square";this.node_oscillator.frequency.setValueAtTime(440,b.currentTime);this.mixer_connection=c.add_source(this.node_oscillator,MIXER_SRC_PCSPEAKER);this.mixer_connection.disconnect();a.register("pcspeaker-enable",function(){c.connect_source(MIXER_SRC_PCSPEAKER)},this);a.register("pcspeaker-disable",function(){c.disconnect_source(MIXER_SRC_PCSPEAKER)},this);a.register("pcspeaker-update",function(d){var e=
d[1],g=0;3===d[0]&&(g=1E3*OSCILLATOR_FREQ/e,g=Math.min(g,this.node_oscillator.frequency.maxValue),g=Math.max(g,0));this.node_oscillator.frequency.setValueAtTime(g,b.currentTime)},this)}PCSpeaker.prototype.start=function(){this.node_oscillator.start()};
function SpeakerWorkletDAC(a,b,c){this.bus=a;this.audio_context=b;this.enabled=!1;this.sampling_rate=48E3;b=function(){function f(l){if(0===l)return 1;l*=Math.PI;return Math.sin(l)/l}function k(){var l=Reflect.construct(AudioWorkletProcessor,[],k);l.kernel_size=3;l.queue_data=Array(1024);l.queue_start=0;l.queue_end=0;l.queue_length=0;l.queue_size=l.queue_data.length;l.queued_samples=0;l.source_buffer_previous=m;l.source_buffer_current=m;l.source_samples_per_destination=1;l.source_block_start=0;l.source_time=
0;l.source_offset=0;l.port.onmessage=n=>{switch(n.data.type){case "queue":l.queue_push(n.data.value);break;case "sampling-rate":l.source_samples_per_destination=n.data.value/sampleRate}};return l}var m=[new Float32Array(256),new Float32Array(256)];Reflect.setPrototypeOf(k.prototype,AudioWorkletProcessor.prototype);Reflect.setPrototypeOf(k,AudioWorkletProcessor);k.prototype.process=k.prototype.process=function(l,n,p){for(l=0;l<n[0][0].length;l++){for(var t=p=0,r=this.source_offset+this.kernel_size,
q=this.source_offset-this.kernel_size+1;q<=r;q++){var v=this.source_block_start+q;p+=this.get_sample(v,0)*this.kernel(this.source_time-q);t+=this.get_sample(v,1)*this.kernel(this.source_time-q)}if(isNaN(p)||isNaN(t))p=t=0,this.dbg_log("ERROR: NaN values! Ignoring for now.");n[0][0][l]=p;n[0][1][l]=t;this.source_time+=this.source_samples_per_destination;this.source_offset=Math.floor(this.source_time)}n=this.source_offset;n+=this.kernel_size+2;this.source_time-=this.source_offset;this.source_block_start+=
0;l.source_offset=0;l.port.onmessage=n=>{switch(n.data.type){case "queue":l.queue_push(n.data.value);break;case "sampling-rate":l.source_samples_per_destination=n.data.value/sampleRate}};return l}var m=[new Float32Array(256),new Float32Array(256)];Reflect.setPrototypeOf(k.prototype,AudioWorkletProcessor.prototype);Reflect.setPrototypeOf(k,AudioWorkletProcessor);k.prototype.process=k.prototype.process=function(l,n,q){for(l=0;l<n[0][0].length;l++){for(var t=q=0,r=this.source_offset+this.kernel_size,
p=this.source_offset-this.kernel_size+1;p<=r;p++){var v=this.source_block_start+p;q+=this.get_sample(v,0)*this.kernel(this.source_time-p);t+=this.get_sample(v,1)*this.kernel(this.source_time-p)}if(isNaN(q)||isNaN(t))q=t=0,this.dbg_log("ERROR: NaN values! Ignoring for now.");n[0][0][l]=q;n[0][1][l]=t;this.source_time+=this.source_samples_per_destination;this.source_offset=Math.floor(this.source_time)}n=this.source_offset;n+=this.kernel_size+2;this.source_time-=this.source_offset;this.source_block_start+=
this.source_offset;this.source_offset=0;this.ensure_enough_data(n);return!0};k.prototype.kernel=function(l){return f(l)*f(l/this.kernel_size)};k.prototype.get_sample=function(l,n){return 0>l?(l+=this.source_buffer_previous[0].length,this.source_buffer_previous[n][l]):this.source_buffer_current[n][l]};k.prototype.ensure_enough_data=function(l){var n=this.source_buffer_current[0].length;n-this.source_block_start<l&&(this.prepare_next_buffer(),this.source_block_start-=n)};k.prototype.prepare_next_buffer=
function(){256>this.queued_samples&&this.queue_length&&this.dbg_log("Not enough samples - should not happen during midway of playback");this.source_buffer_previous=this.source_buffer_current;this.source_buffer_current=this.queue_shift();var l=this.source_buffer_current[0].length;if(256>l){for(var n=this.queue_start,p=0;256>l&&p<this.queue_length;)l+=this.queue_data[n][0].length,n=n+1&this.queue_size-1,p++;l=Math.max(l,256);l=[new Float32Array(l),new Float32Array(l)];l[0].set(this.source_buffer_current[0]);
l[1].set(this.source_buffer_current[1]);n=this.source_buffer_current[0].length;for(var t=0;t<p;t++){var r=this.queue_shift();l[0].set(r[0],n);l[1].set(r[1],n);n+=r[0].length}this.source_buffer_current=l}this.pump()};k.prototype.pump=function(){1024>this.queued_samples/this.source_samples_per_destination&&this.port.postMessage({type:"pump"})};k.prototype.queue_push=function(l){this.queue_length<this.queue_size&&(this.queue_data[this.queue_end]=l,this.queue_end=this.queue_end+1&this.queue_size-1,this.queue_length++,
function(){256>this.queued_samples&&this.queue_length&&this.dbg_log("Not enough samples - should not happen during midway of playback");this.source_buffer_previous=this.source_buffer_current;this.source_buffer_current=this.queue_shift();var l=this.source_buffer_current[0].length;if(256>l){for(var n=this.queue_start,q=0;256>l&&q<this.queue_length;)l+=this.queue_data[n][0].length,n=n+1&this.queue_size-1,q++;l=Math.max(l,256);l=[new Float32Array(l),new Float32Array(l)];l[0].set(this.source_buffer_current[0]);
l[1].set(this.source_buffer_current[1]);n=this.source_buffer_current[0].length;for(var t=0;t<q;t++){var r=this.queue_shift();l[0].set(r[0],n);l[1].set(r[1],n);n+=r[0].length}this.source_buffer_current=l}this.pump()};k.prototype.pump=function(){1024>this.queued_samples/this.source_samples_per_destination&&this.port.postMessage({type:"pump"})};k.prototype.queue_push=function(l){this.queue_length<this.queue_size&&(this.queue_data[this.queue_end]=l,this.queue_end=this.queue_end+1&this.queue_size-1,this.queue_length++,
this.queued_samples+=l[0].length,this.pump())};k.prototype.queue_shift=function(){if(!this.queue_length)return m;var l=this.queue_data[this.queue_start];this.queue_data[this.queue_start]=null;this.queue_start=this.queue_start+1&this.queue_size-1;this.queue_length--;this.queued_samples-=l[0].length;return l};k.prototype.dbg_log=function(l){DEBUG&&this.port.postMessage({type:"debug-log",value:l})};registerProcessor("dac-processor",k)}.toString();var d=b.indexOf("{")+1,e=b.lastIndexOf("}");b=b.substring(d,
e);DEBUG&&(b="var DEBUG = true;\n"+b);b=new Blob([b],{type:"application/javascript"});var g=URL.createObjectURL(b);this.node_processor=null;this.node_output=this.audio_context.createGain();this.audio_context.audioWorklet.addModule(g).then(()=>{URL.revokeObjectURL(g);this.node_processor=new AudioWorkletNode(this.audio_context,"dac-processor",{numberOfInputs:0,numberOfOutputs:1,outputChannelCount:[2],parameterData:{},processorOptions:{}});this.node_processor.port.postMessage({type:"sampling-rate",value:this.sampling_rate});
this.node_processor.port.onmessage=f=>{switch(f.data.type){case "pump":this.pump();break;case "debug-log":dbg_log("SpeakerWorkletDAC - Worklet: "+f.data.value)}};this.node_processor.connect(this.node_output)});this.mixer_connection=c.add_source(this.node_output,MIXER_SRC_DAC);this.mixer_connection.set_gain_hidden(3);a.register("dac-send-data",function(f){this.queue(f)},this);a.register("dac-enable",function(f){this.enabled=!0},this);a.register("dac-disable",function(){this.enabled=!1},this);a.register("dac-tell-sampling-rate",
@@ -676,71 +704,54 @@ SpeakerDACDebugger.prototype.download_csv=function(a){a=this.output_history[a];f
function(k){this.show_char(k)},this);this.destroy=function(){a.removeEventListener("keypress",c,!1);a.removeEventListener("keydown",d,!1);a.removeEventListener("paste",e,!1);window.removeEventListener("mousedown",g,!1)};this.init=function(){this.destroy();a.style.display="block";a.addEventListener("keypress",c,!1);a.addEventListener("keydown",d,!1);a.addEventListener("paste",e,!1);window.addEventListener("mousedown",g,!1)};this.init();this.show_char=function(k){"\b"===k?(this.text=this.text.slice(0,
-1),this.update()):"\r"!==k&&(this.text+=k,"\n"===k&&(this.text_new_line=!0),this.update())};this.update=function(){var k=Date.now(),m=k-this.last_update;16>m?void 0===this.update_timer&&(this.update_timer=setTimeout(()=>{this.update_timer=void 0;var l=Date.now();dbg_assert(15<=l-this.last_update);this.last_update=l;this.render()},16-m)):(void 0!==this.update_timer&&(clearTimeout(this.update_timer),this.update_timer=void 0),this.last_update=k,this.render())};this.render=function(){a.value=this.text;
this.text_new_line&&(this.text_new_line=!1,a.scrollTop=1E9)};this.send_char=function(k){f.bus&&f.bus.send("serial0-input",k)}}function SerialRecordingAdapter(a){this.text="";a.register("serial0-output-char",function(b){this.text+=b},this)}
function SerialAdapterXtermJS(a,b){this.element=a;if(window.Terminal){var c=this.term=new window.Terminal;c.setOption("logLevel","off");c.write("This is the serial console. Whatever you type or paste here will be sent to COM1");c.onData(function(d){for(let e=0;e<d.length;e++)b.send("serial0-input",d.charCodeAt(e))});b.register("serial0-output-char",function(d){c.write(d)},this)}}SerialAdapterXtermJS.prototype.show=function(){this.term&&this.term.open(this.element)};function NetworkAdapter(a,b){this.bus=b;this.socket=void 0;this.send_queue=[];this.url=a;this.reconnect_interval=1E4;this.last_connect_attempt=Date.now()-this.reconnect_interval;this.send_queue_limit=64;this.bus.register("net0-send",function(c){this.send(c)},this)}NetworkAdapter.prototype.handle_message=function(a){this.bus&&this.bus.send("net0-receive",new Uint8Array(a.data))};NetworkAdapter.prototype.handle_close=function(a){this.connect();setTimeout(this.connect.bind(this),this.reconnect_interval)};
function SerialAdapterXtermJS(a,b){this.element=a;if(window.Terminal){var c=this.term=new window.Terminal;c.setOption("logLevel","off");c.write("This is the serial console. Whatever you type or paste here will be sent to COM1");var d=c.onData(function(e){for(let g=0;g<e.length;g++)b.send("serial0-input",e.charCodeAt(g))});b.register("serial0-output-char",function(e){c.write(e)},this);this.destroy=function(){d.dispose();c.dispose()}}}SerialAdapterXtermJS.prototype.show=function(){this.term&&this.term.open(this.element)};function NetworkAdapter(a,b){this.bus=b;this.socket=void 0;this.send_queue=[];this.url=a;this.reconnect_interval=1E4;this.last_connect_attempt=Date.now()-this.reconnect_interval;this.send_queue_limit=64;this.bus.register("net0-send",function(c){this.send(c)},this)}NetworkAdapter.prototype.handle_message=function(a){this.bus&&this.bus.send("net0-receive",new Uint8Array(a.data))};NetworkAdapter.prototype.handle_close=function(a){this.connect();setTimeout(this.connect.bind(this),this.reconnect_interval)};
NetworkAdapter.prototype.handle_open=function(a){for(a=0;a<this.send_queue.length;a++)this.send(this.send_queue[a]);this.send_queue=[]};NetworkAdapter.prototype.handle_error=function(a){};NetworkAdapter.prototype.destroy=function(){this.socket&&this.socket.close()};
NetworkAdapter.prototype.connect=function(){if("undefined"!==typeof WebSocket){if(this.socket){var a=this.socket.readyState;if(0===a||1===a)return}a=Date.now();this.last_connect_attempt+this.reconnect_interval>a||(this.last_connect_attempt=Date.now(),this.socket=new WebSocket(this.url),this.socket.binaryType="arraybuffer",this.socket.onopen=this.handle_open.bind(this),this.socket.onmessage=this.handle_message.bind(this),this.socket.onclose=this.handle_close.bind(this),this.socket.onerror=this.handle_error.bind(this))}};
NetworkAdapter.prototype.send=function(a){this.socket&&1===this.socket.readyState?this.socket.send(a):(this.send_queue.push(a),this.send_queue.length>2*this.send_queue_limit&&(this.send_queue=this.send_queue.slice(-this.send_queue_limit)),this.connect())};NetworkAdapter.prototype.change_proxy=function(a){this.url=a;this.socket&&(this.socket.onclose=function(){},this.socket.onerror=function(){},this.socket.close(),this.socket=void 0)};var ASYNC_SAFE=!1;
(function(){function a(k,m,l){function n(){const v=l||0;setTimeout(()=>{a(k,m,v+1)},1E3*([1,1,2,3,5,8,13,21][v]||34))}var p=new XMLHttpRequest;p.open(m.method||"get",k,!0);p.responseType=m.as_json?"json":"arraybuffer";if(m.headers)for(var t=Object.keys(m.headers),r=0;r<t.length;r++){var q=t[r];p.setRequestHeader(q,m.headers[q])}m.range&&(t=m.range.start,p.setRequestHeader("Range","bytes="+t+"-"+(t+m.range.length-1)),p.onreadystatechange=function(){200===p.status&&p.abort()});p.onload=function(v){4===
p.readyState&&(200!==p.status&&206!==p.status?(console.error("Loading the image "+k+" failed (status %d)",p.status),500<=p.status&&600>p.status&&n()):p.response&&m.done&&m.done(p.response,p))};p.onerror=function(v){console.error("Loading the image "+k+" failed",v);n()};m.progress&&(p.onprogress=function(v){m.progress(v)});p.send(null)}function b(k,m){let l=require("fs");m.range?(dbg_assert(!m.as_json),l.open(k,"r",(n,p)=>{if(n)throw n;let t=m.range.length;var r=Buffer.allocUnsafe(t);l.read(p,r,0,
t,m.range.start,(q,v)=>{if(q)throw q;dbg_assert(v===t);m.done&&m.done(new Uint8Array(r));l.close(p,u=>{if(u)throw u;})})})):l.readFile(k,{encoding:m.as_json?"utf-8":null},function(n,p){n?console.log("Could not read file:",k,n):(n=p,n=m.as_json?JSON.parse(n):(new Uint8Array(n)).buffer,m.done(n))})}function c(k,m){this.filename=k;this.block_size=256;this.byteLength=m;this.loaded_blocks=Object.create(null);this.onprogress=this.onload=void 0}function d(k,m,l){const n=k.match(/(.*)(\..*)/);n?(this.basename=
n[1],this.extension=n[2]):(this.basename=k,this.extension="");this.block_size=256;this.byteLength=m;this.use_fixed_chunk_size="number"===typeof l;this.fixed_chunk_size=l;this.loaded_blocks=Object.create(null);this.onprogress=this.onload=void 0}function e(k){this.file=k;this.byteLength=k.size;1073741824<k.size&&console.warn("SyncFileBuffer: Allocating buffer of "+(k.size>>20)+" MB ...");this.buffer=new ArrayBuffer(k.size);this.onprogress=this.onload=void 0}function g(k){this.file=k;this.byteLength=
k.size;this.block_size=256;this.loaded_blocks=Object.create(null);this.onprogress=this.onload=void 0}v86util.load_file="undefined"!==typeof XMLHttpRequest?b:a;v86util.AsyncXHRBuffer=c;v86util.AsyncXHRPartfileBuffer=d;v86util.AsyncFileBuffer=g;v86util.SyncFileBuffer=e;v86util.read_sized_string_from_mem=function(k,m,l){return String.fromCharCode(...(new Uint8Array(k.buffer,m>>>0,l>>>0)))};var f="undefined"===typeof XMLHttpRequest?function(k,m){require("fs").stat(k,(l,n)=>{l?m(l):m(null,n.size)})}:function(k,
m){v86util.load_file(k,{done:(l,n)=>{l=n.getResponseHeader("Content-Range")||"";(n=l.match(/\/(\d+)\s*$/))?m(null,+n[1]):m("`Range: bytes=...` header not supported (Got `"+l+"`)")},headers:{Range:"bytes=0-0"}})};c.prototype.load=function(){void 0!==this.byteLength?this.onload&&this.onload(Object.create(null)):f(this.filename,(k,m)=>{if(k)throw Error("Cannot use: "+this.filename+". "+k);dbg_assert(0<=m);this.byteLength=m;this.onload&&this.onload(Object.create(null))})};c.prototype.get_from_cache=function(k,
m,l){l=m/this.block_size;k/=this.block_size;for(var n=0;n<l;n++)if(!this.loaded_blocks[k+n])return;if(1===l)return this.loaded_blocks[k];m=new Uint8Array(m);for(n=0;n<l;n++)m.set(this.loaded_blocks[k+n],n*this.block_size);return m};c.prototype.get=function(k,m,l){console.assert(k+m<=this.byteLength);console.assert(0===k%this.block_size);console.assert(0===m%this.block_size);console.assert(m);var n=this.get_from_cache(k,m,l);n?ASYNC_SAFE?setTimeout(l.bind(this,n),0):l(n):v86util.load_file(this.filename,
{done:function(p){p=new Uint8Array(p);this.handle_read(k,m,p);l(p)}.bind(this),range:{start:k,length:m}})};c.prototype.set=function(k,m,l){console.assert(k+m.byteLength<=this.byteLength);var n=m.length;console.assert(0===k%this.block_size);console.assert(0===n%this.block_size);console.assert(n);k/=this.block_size;n/=this.block_size;for(var p=0;p<n;p++){var t=this.loaded_blocks[k+p];void 0===t&&(t=this.loaded_blocks[k+p]=new Uint8Array(this.block_size));var r=m.subarray(p*this.block_size,(p+1)*this.block_size);
t.set(r);console.assert(t.byteLength===r.length)}l()};c.prototype.handle_read=function(k,m,l){k/=this.block_size;m/=this.block_size;for(var n=0;n<m;n++){var p=this.loaded_blocks[k+n];p&&l.set(p,n*this.block_size)}};c.prototype.get_buffer=function(k){k()};c.prototype.get_written_blocks=function(){var k=Object.keys(this.loaded_blocks).length;k=new Uint8Array(k*this.block_size);var m=[],l=0;for(p of Object.keys(this.loaded_blocks)){var n=this.loaded_blocks[p];dbg_assert(n.length===this.block_size);var p=
+p;m.push(p);k.set(n,l*this.block_size);l++}return{buffer:k,indices:m,block_size:this.block_size}};c.prototype.get_state=function(){const k=[],m=[];for(let [l,n]of Object.entries(this.loaded_blocks))dbg_assert(isFinite(+l)),m.push([+l,n]);k[0]=m;return k};c.prototype.set_state=function(k){k=k[0];this.loaded_blocks=Object.create(null);for(let [m,l]of Object.values(k))this.loaded_blocks[m]=l};d.prototype.load=function(){void 0===this.byteLength&&dbg_assert(!1);this.onload&&this.onload(Object.create(null))};
d.prototype.get_from_cache=c.prototype.get_from_cache;d.prototype.get=function(k,m,l){console.assert(k+m<=this.byteLength);console.assert(0===k%this.block_size);console.assert(0===m%this.block_size);console.assert(m);var n=this.get_from_cache(k,m,l);if(n)ASYNC_SAFE?setTimeout(l.bind(this,n),0):l(n);else if(this.use_fixed_chunk_size){n=Math.floor(k/this.fixed_chunk_size);const p=k-n*this.fixed_chunk_size;dbg_assert(0<=p);const t=Math.floor(m/this.fixed_chunk_size)+(0===p?1:2),r=new Uint8Array(p+t*
this.fixed_chunk_size);let q=0;for(let v=0;v<t;v++){const u=this.basename+"-"+(n+v+"").padStart(8,"0")+this.extension;v86util.load_file(u,{done:function(x){var z=v*this.fixed_chunk_size;x=new Uint8Array(x);r.set(x,z);q++;q===t&&(z=r.subarray(p,p+m),this.handle_read(k,m,z),l(z))}.bind(this)})}}else v86util.load_file(this.basename+"-"+k+"-"+(k+m)+this.extension,{done:function(p){dbg_assert(p.byteLength===m);p=new Uint8Array(p);this.handle_read(k,m,p);l(p)}.bind(this)})};d.prototype.set=c.prototype.set;
d.prototype.handle_read=c.prototype.handle_read;d.prototype.get_written_blocks=c.prototype.get_written_blocks;d.prototype.get_state=c.prototype.get_state;d.prototype.set_state=c.prototype.set_state;e.prototype.load=function(){this.load_next(0)};e.prototype.load_next=function(k){var m=new FileReader;m.onload=function(n){n=new Uint8Array(n.target.result);(new Uint8Array(this.buffer,k)).set(n);this.load_next(k+4194304)}.bind(this);if(this.onprogress)this.onprogress({loaded:k,total:this.byteLength,lengthComputable:!0});
if(k<this.byteLength){var l=this.file.slice(k,Math.min(k+4194304,this.byteLength));m.readAsArrayBuffer(l)}else this.file=void 0,this.onload&&this.onload({buffer:this.buffer})};e.prototype.get=function(k,m,l){console.assert(k+m<=this.byteLength);l(new Uint8Array(this.buffer,k,m))};e.prototype.set=function(k,m,l){console.assert(k+m.byteLength<=this.byteLength);(new Uint8Array(this.buffer,k,m.byteLength)).set(m);l()};e.prototype.get_buffer=function(k){k(this.buffer)};e.prototype.get_state=function(){const k=
[];k[0]=this.byteLength;k[1]=new Uint8Array(this.buffer);return k};e.prototype.set_state=function(k){this.byteLength=k[0];this.buffer=k[1].slice().buffer};g.prototype.load=function(){this.onload&&this.onload(Object.create(null))};g.prototype.get=function(k,m,l){console.assert(0===k%this.block_size);console.assert(0===m%this.block_size);console.assert(m);var n=this.get_from_cache(k,m,l);n?l(n):(n=new FileReader,n.onload=function(p){p=new Uint8Array(p.target.result);this.handle_read(k,m,p);l(p)}.bind(this),
n.readAsArrayBuffer(this.file.slice(k,k+m)))};g.prototype.get_from_cache=c.prototype.get_from_cache;g.prototype.set=c.prototype.set;g.prototype.handle_read=c.prototype.handle_read;g.prototype.get_state=c.prototype.get_state;g.prototype.get_buffer=function(k){k()};g.prototype.get_as_file=function(k){for(var m=[],l=Object.keys(this.loaded_blocks).map(Number).sort(function(q,v){return q-v}),n=0,p=0;p<l.length;p++){var t=l[p],r=this.loaded_blocks[t];t*=this.block_size;console.assert(t>=n);t!==n&&(m.push(this.file.slice(n,
t)),n=t);m.push(r);n+=r.length}n!==this.file.size&&m.push(this.file.slice(n));k=new File(m,k);console.assert(k.size===this.file.size);return k}})();function V86Starter(a){this.cpu_is_running=!1;var b=Bus.create();this.bus=b[0];this.emulator_bus=b[1];var c,d;const e=new WebAssembly.Table({element:"anyfunc",initial:WASM_TABLE_SIZE+WASM_TABLE_OFFSET});b={cpu_exception_hook:f=>this.cpu_exception_hook&&this.cpu_exception_hook(f),hlt_op:function(){return c.hlt_op()},abort:function(){dbg_assert(!1)},microtick:v86.microtick,get_rand_int:function(){return v86util.get_rand_int()},pic_acknowledge:function(){c.pic_acknowledge()},io_port_read8:function(f){return c.io.port_read8(f)},
NetworkAdapter.prototype.send=function(a){this.socket&&1===this.socket.readyState?this.socket.send(a):(this.send_queue.push(a),this.send_queue.length>2*this.send_queue_limit&&(this.send_queue=this.send_queue.slice(-this.send_queue_limit)),this.connect())};NetworkAdapter.prototype.change_proxy=function(a){this.url=a;this.socket&&(this.socket.onclose=function(){},this.socket.onerror=function(){},this.socket.close(),this.socket=void 0)};function V86Starter(a){this.cpu_is_running=!1;var b=Bus.create();this.bus=b[0];this.emulator_bus=b[1];var c,d;const e=new WebAssembly.Table({element:"anyfunc",initial:WASM_TABLE_SIZE+WASM_TABLE_OFFSET});b={cpu_exception_hook:f=>this.cpu_exception_hook&&this.cpu_exception_hook(f),hlt_op:function(){return c.hlt_op()},abort:function(){dbg_assert(!1)},microtick:v86.microtick,get_rand_int:function(){return v86util.get_rand_int()},pic_acknowledge:function(){c.pic_acknowledge()},io_port_read8:function(f){return c.io.port_read8(f)},
io_port_read16:function(f){return c.io.port_read16(f)},io_port_read32:function(f){return c.io.port_read32(f)},io_port_write8:function(f,k){c.io.port_write8(f,k)},io_port_write16:function(f,k){c.io.port_write16(f,k)},io_port_write32:function(f,k){c.io.port_write32(f,k)},mmap_read8:function(f){return c.mmap_read8(f)},mmap_read16:function(f){return c.mmap_read16(f)},mmap_read32:function(f){return c.mmap_read32(f)},mmap_write8:function(f,k){c.mmap_write8(f,k)},mmap_write16:function(f,k){c.mmap_write16(f,
k)},mmap_write32:function(f,k){c.mmap_write32(f,k)},mmap_write64:function(f,k,m){c.mmap_write64(f,k,m)},mmap_write128:function(f,k,m,l,n){c.mmap_write128(f,k,m,l,n)},log_from_wasm:function(f,k){f=v86util.read_sized_string_from_mem(d,f,k);dbg_log(f,LOG_CPU)},console_log_from_wasm:function(f,k){f=v86util.read_sized_string_from_mem(d,f,k);console.error(f)},dbg_trace_from_wasm:function(){dbg_trace()},codegen_finalize:(f,k,m,l,n)=>{c.codegen_finalize(f,k,m,l,n)},jit_clear_func:f=>c.jit_clear_func(f),jit_clear_all_funcs:()=>
c.jit_clear_all_funcs(),__indirect_function_table:e};let g=a.wasm_fn;g||(g=async f=>new Promise(k=>{let m=DEBUG?"v86-debug.wasm":"v86.wasm",l="v86-fallback.wasm";if(a.wasm_path){m=a.wasm_path;const n=m.lastIndexOf("/");l=(-1===n?"":m.substr(0,n))+"/"+l}else"undefined"===typeof window&&"string"===typeof __dirname?(m=__dirname+"/"+m,l=__dirname+"/"+l):(m="build/"+m,l="build/"+l);v86util.load_file(m,{done:n=>{WebAssembly.instantiate(n,f).then(({instance:p})=>{k(p.exports)},p=>{v86util.load_file(l,{done:t=>
{WebAssembly.instantiate(t,f).then(({instance:r})=>{k(r.exports)})}})})},progress:n=>{this.emulator_bus.send("download-progress",{file_index:0,file_count:1,file_name:m,lengthComputable:n.lengthComputable,total:n.total,loaded:n.loaded})}})}));g({env:b}).then(f=>{d=f.memory;f.rust_init();f=this.v86=new v86(this.emulator_bus,{exports:f,wasm_table:e});c=f.cpu;this.continue_init(f,a)})}
V86Starter.prototype.continue_init=async function(a,b){function c(r,q){switch(r){case "hda":g.hda=this.disk_images.hda=q;break;case "hdb":g.hdb=this.disk_images.hdb=q;break;case "cdrom":g.cdrom=this.disk_images.cdrom=q;break;case "fda":g.fda=this.disk_images.fda=q;break;case "fdb":g.fdb=this.disk_images.fdb=q;break;case "multiboot":g.multiboot=this.disk_images.multiboot=q.buffer;break;case "bzimage":g.bzimage=this.disk_images.bzimage=q.buffer;break;case "initrd":g.initrd=this.disk_images.initrd=q.buffer;
break;case "bios":g.bios=q.buffer;break;case "vga_bios":g.vga_bios=q.buffer;break;case "initial_state":g.initial_state=q.buffer;break;case "fs9p_json":g.fs9p_json=q;break;default:dbg_assert(!1,r)}}function d(r,q){if(q)if(q.get&&q.set&&q.load)f.push({name:r,loadable:q});else{q={buffer:q.buffer,async:q.async,url:q.url,size:q.size,fixed_chunk_size:q.fixed_chunk_size,use_parts:q.use_parts};if("bios"===r||"vga_bios"===r||"initial_state"===r||"multiboot"===r||"bzimage"===r||"initrd"===r)q.async=!1;q.buffer instanceof
ArrayBuffer?(q=new SyncBuffer(q.buffer),f.push({name:r,loadable:q})):"undefined"!==typeof File&&q.buffer instanceof File?(void 0===q.async&&(q.async=268435456<=q.buffer.size),q=q.async?new v86util.AsyncFileBuffer(q.buffer):new v86util.SyncFileBuffer(q.buffer),f.push({name:r,loadable:q})):q.url?q.async?(q=q.use_parts?new v86util.AsyncXHRPartfileBuffer(q.url,q.size,q.fixed_chunk_size):new v86util.AsyncXHRBuffer(q.url,q.size),f.push({name:r,loadable:q})):f.push({name:r,url:q.url,size:q.size}):dbg_log("Ignored file: url="+
q.url+" buffer="+q.buffer)}}function e(){function r(){this.serial_adapter&&this.serial_adapter.show&&this.serial_adapter.show();this.bus.send("cpu-init",g);g.initial_state&&(a.restore_state(g.initial_state),g.initial_state=void 0);b.autostart&&this.bus.send("cpu-run");this.emulator_bus.send("emulator-loaded")}if(g.fs9p&&g.fs9p_json)if(g.initial_state?dbg_log("Filesystem basefs ignored: Overridden by state image"):g.fs9p.load_from_json(g.fs9p_json),b.bzimage_initrd_from_filesystem){const {bzimage:q,
initrd:v}=this.get_bzimage_initrd_from_filesystem(g.fs9p);dbg_log("Found bzimage: "+q+" and initrd: "+v);Promise.all([g.fs9p.read_file(v),g.fs9p.read_file(q)]).then(([u,x])=>{c.call(this,"initrd",new SyncBuffer(u.buffer));c.call(this,"bzimage",new SyncBuffer(x.buffer));r.call(this)})}else r.call(this);else console.assert(!b.bzimage_initrd_from_filesystem,"bzimage_initrd_from_filesystem: Requires a filesystem"),r.call(this)}this.bus.register("emulator-stopped",function(){this.cpu_is_running=!1},this);
this.bus.register("emulator-started",function(){this.cpu_is_running=!0},this);var g={};this.disk_images={fda:void 0,fdb:void 0,hda:void 0,hdb:void 0,cdrom:void 0};g.acpi=b.acpi;g.load_devices=!0;g.log_level=b.log_level;g.memory_size=b.memory_size||67108864;g.vga_memory_size=b.vga_memory_size||8388608;g.boot_order=b.boot_order||531;g.fastboot=b.fastboot||!1;g.fda=void 0;g.fdb=void 0;g.uart1=b.uart1;g.uart2=b.uart2;g.uart3=b.uart3;g.cmdline=b.cmdline;g.preserve_mac_from_state_image=b.preserve_mac_from_state_image;
b.network_adapter?this.network_adapter=b.network_adapter(this.bus):b.network_relay_url&&(this.network_adapter=new NetworkAdapter(b.network_relay_url,this.bus));g.enable_ne2k=!0;b.disable_keyboard||(this.keyboard_adapter=new KeyboardAdapter(this.bus));b.disable_mouse||(this.mouse_adapter=new MouseAdapter(this.bus,b.screen_container));b.screen_container?this.screen_adapter=new ScreenAdapter(b.screen_container,this.bus):b.screen_dummy&&(this.screen_adapter=new DummyScreenAdapter(this.bus));b.serial_container&&
(this.serial_adapter=new SerialAdapter(b.serial_container,this.bus));b.serial_container_xtermjs&&(this.serial_adapter=new SerialAdapterXtermJS(b.serial_container_xtermjs,this.bus));b.disable_speaker||(this.speaker_adapter=new SpeakerAdapter(this.bus));var f=[];b.state&&console.warn("Warning: Unknown option 'state'. Did you mean 'initial_state'?");for(var k="bios vga_bios cdrom hda hdb fda fdb initial_state multiboot bzimage initrd".split(" "),m=0;m<k.length;m++)d(k[m],b[k[m]]);if(b.filesystem){k=
b.filesystem.basefs;m=b.filesystem.baseurl;let r=new MemoryFileStorage;m&&(r=new ServerFileStorageWrapper(r,m));g.fs9p=this.fs9p=new FS(r);if(k){console.assert(m,"Filesystem: baseurl must be specified");if("object"===typeof k){var l=k.size;k=k.url}dbg_assert("string"===typeof k);f.push({name:"fs9p_json",url:k,size:l,as_json:!0})}}var n=this,p=f.length,t=function(r){if(r===p)setTimeout(e.bind(this),0);else{var q=f[r];q.loadable?(q.loadable.onload=function(v){c.call(this,q.name,q.loadable);t(r+1)}.bind(this),
q.loadable.load()):v86util.load_file(q.url,{done:function(v){c.call(this,q.name,q.as_json?v:new SyncBuffer(v));t(r+1)}.bind(this),progress:function(v){200===v.target.status?n.emulator_bus.send("download-progress",{file_index:r,file_count:p,file_name:q.url,lengthComputable:v.lengthComputable,total:v.total||q.size,loaded:v.loaded}):n.emulator_bus.send("download-error",{file_index:r,file_count:p,file_name:q.url,request:v.target})},as_json:q.as_json})}}.bind(this);t(0)};
V86Starter.prototype.get_bzimage_initrd_from_filesystem=function(a){const b=(a.read_dir("/")||[]).map(e=>"/"+e);a=(a.read_dir("/boot/")||[]).map(e=>"/boot/"+e);let c,d;for(let e of[].concat(b,a)){const g=/old/i.test(e)||/fallback/i.test(e),f=/vmlinuz/i.test(e)||/bzimage/i.test(e),k=/initrd/i.test(e)||/initramfs/i.test(e);!f||d&&g||(d=e);!k||c&&g||(c=e)}c&&d||(console.log("Failed to find bzimage or initrd in filesystem. Files:"),console.log(b.join(" ")),console.log(a.join(" ")));return{initrd:c,bzimage:d}};
V86Starter.prototype.run=function(){this.bus.send("cpu-run")};goog.exportProperty(V86Starter.prototype,"run",V86Starter.prototype.run);V86Starter.prototype.stop=function(){this.bus.send("cpu-stop")};goog.exportProperty(V86Starter.prototype,"stop",V86Starter.prototype.stop);
V86Starter.prototype.destroy=function(){this.stop();this.v86.destroy();this.keyboard_adapter&&this.keyboard_adapter.destroy();this.network_adapter&&this.network_adapter.destroy();this.mouse_adapter&&this.mouse_adapter.destroy();this.screen_adapter&&this.screen_adapter.destroy();this.serial_adapter&&this.serial_adapter.destroy()};goog.exportProperty(V86Starter.prototype,"destroy",V86Starter.prototype.destroy);V86Starter.prototype.restart=function(){this.bus.send("cpu-restart")};
goog.exportProperty(V86Starter.prototype,"restart",V86Starter.prototype.restart);V86Starter.prototype.add_listener=function(a,b){this.bus.register(a,b,this)};goog.exportProperty(V86Starter.prototype,"add_listener",V86Starter.prototype.add_listener);V86Starter.prototype.remove_listener=function(a,b){this.bus.unregister(a,b)};goog.exportProperty(V86Starter.prototype,"remove_listener",V86Starter.prototype.remove_listener);V86Starter.prototype.restore_state=function(a){this.v86.restore_state(a)};
goog.exportProperty(V86Starter.prototype,"restore_state",V86Starter.prototype.restore_state);V86Starter.prototype.save_state=function(a){setTimeout(function(){try{a(null,this.v86.save_state())}catch(b){a(b,null)}}.bind(this),0)};goog.exportProperty(V86Starter.prototype,"save_state",V86Starter.prototype.save_state);
k)},mmap_write32:function(f,k){c.mmap_write32(f,k)},mmap_write64:function(f,k,m){c.mmap_write64(f,k,m)},mmap_write128:function(f,k,m,l,n){c.mmap_write128(f,k,m,l,n)},log_from_wasm:function(f,k){f=v86util.read_sized_string_from_mem(d,f,k);dbg_log(f,LOG_CPU)},console_log_from_wasm:function(f,k){f=v86util.read_sized_string_from_mem(d,f,k);console.error(f)},dbg_trace_from_wasm:function(){dbg_trace(LOG_CPU)},codegen_finalize:(f,k,m,l,n)=>{c.codegen_finalize(f,k,m,l,n)},jit_clear_func:f=>c.jit_clear_func(f),
jit_clear_all_funcs:()=>c.jit_clear_all_funcs(),__indirect_function_table:e};let g=a.wasm_fn;g||(g=f=>new Promise(k=>{let m=DEBUG?"v86-debug.wasm":"v86.wasm",l="v86-fallback.wasm";if(a.wasm_path){m=a.wasm_path;const n=m.lastIndexOf("/");l=(-1===n?"":m.substr(0,n))+"/"+l}else"undefined"===typeof window&&"string"===typeof __dirname?(m=__dirname+"/"+m,l=__dirname+"/"+l):(m="build/"+m,l="build/"+l);v86util.load_file(m,{done:async n=>{try{const {instance:q}=await WebAssembly.instantiate(n,f);k(q.exports)}catch(q){v86util.load_file(l,
{done:async t=>{({instance:t}=await WebAssembly.instantiate(t,f));k(t.exports)}})}},progress:n=>{this.emulator_bus.send("download-progress",{file_index:0,file_count:1,file_name:m,lengthComputable:n.lengthComputable,total:n.total,loaded:n.loaded})}})}));g({env:b}).then(f=>{d=f.memory;f.rust_init();f=this.v86=new v86(this.emulator_bus,{exports:f,wasm_table:e});c=f.cpu;this.continue_init(f,a)})}
V86Starter.prototype.continue_init=async function(a,b){function c(r,p){switch(r){case "hda":g.hda=this.disk_images.hda=p;break;case "hdb":g.hdb=this.disk_images.hdb=p;break;case "cdrom":g.cdrom=this.disk_images.cdrom=p;break;case "fda":g.fda=this.disk_images.fda=p;break;case "fdb":g.fdb=this.disk_images.fdb=p;break;case "multiboot":g.multiboot=this.disk_images.multiboot=p.buffer;break;case "bzimage":g.bzimage=this.disk_images.bzimage=p.buffer;break;case "initrd":g.initrd=this.disk_images.initrd=p.buffer;
break;case "bios":g.bios=p.buffer;break;case "vga_bios":g.vga_bios=p.buffer;break;case "initial_state":g.initial_state=p.buffer;break;case "fs9p_json":g.fs9p_json=p;break;default:dbg_assert(!1,r)}}function d(r,p){if(p)if(p.get&&p.set&&p.load)f.push({name:r,loadable:p});else{if("bios"===r||"vga_bios"===r||"initial_state"===r||"multiboot"===r||"bzimage"===r||"initrd"===r)p.async=!1;p.buffer instanceof ArrayBuffer?(p=new v86util.SyncBuffer(p.buffer),f.push({name:r,loadable:p})):"undefined"!==typeof File&&
p.buffer instanceof File?(void 0===p.async&&(p.async=268435456<=p.buffer.size),p=p.async?new v86util.AsyncFileBuffer(p.buffer):new v86util.SyncFileBuffer(p.buffer),f.push({name:r,loadable:p})):p.url?p.async?(p=p.use_parts?new v86util.AsyncXHRPartfileBuffer(p.url,p.size,p.fixed_chunk_size):new v86util.AsyncXHRBuffer(p.url,p.size),f.push({name:r,loadable:p})):f.push({name:r,url:p.url,size:p.size}):dbg_log("Ignored file: url="+p.url+" buffer="+p.buffer)}}async function e(){if(g.fs9p&&g.fs9p_json){if(g.initial_state?
dbg_log("Filesystem basefs ignored: Overridden by state image"):g.fs9p.load_from_json(g.fs9p_json),b.bzimage_initrd_from_filesystem){const {bzimage_path:r,initrd_path:p}=this.get_bzimage_initrd_from_filesystem(g.fs9p);dbg_log("Found bzimage: "+r+" and initrd: "+p);const [v,u]=await Promise.all([g.fs9p.read_file(p),g.fs9p.read_file(r)]);c.call(this,"initrd",new v86util.SyncBuffer(v.buffer));c.call(this,"bzimage",new v86util.SyncBuffer(u.buffer))}}else dbg_assert(!b.bzimage_initrd_from_filesystem,"bzimage_initrd_from_filesystem: Requires a filesystem");
this.serial_adapter&&this.serial_adapter.show&&this.serial_adapter.show();this.bus.send("cpu-init",g);g.initial_state&&(a.restore_state(g.initial_state),g.initial_state=void 0);b.autostart&&this.bus.send("cpu-run");this.emulator_bus.send("emulator-loaded")}this.bus.register("emulator-stopped",function(){this.cpu_is_running=!1},this);this.bus.register("emulator-started",function(){this.cpu_is_running=!0},this);var g={};this.disk_images={fda:void 0,fdb:void 0,hda:void 0,hdb:void 0,cdrom:void 0};g.acpi=
b.acpi;g.load_devices=!0;g.log_level=b.log_level;g.memory_size=b.memory_size||67108864;g.vga_memory_size=b.vga_memory_size||8388608;g.boot_order=b.boot_order||531;g.fastboot=b.fastboot||!1;g.fda=void 0;g.fdb=void 0;g.uart1=b.uart1;g.uart2=b.uart2;g.uart3=b.uart3;g.cmdline=b.cmdline;g.preserve_mac_from_state_image=b.preserve_mac_from_state_image;g.mac_address_translation=b.mac_address_translation;g.cpuid_level=b.cpuid_level;b.network_adapter?this.network_adapter=b.network_adapter(this.bus):b.network_relay_url&&
(this.network_adapter=new NetworkAdapter(b.network_relay_url,this.bus));g.enable_ne2k=!0;b.disable_keyboard||(this.keyboard_adapter=new KeyboardAdapter(this.bus));b.disable_mouse||(this.mouse_adapter=new MouseAdapter(this.bus,b.screen_container));b.screen_container?this.screen_adapter=new ScreenAdapter(b.screen_container,this.bus):b.screen_dummy&&(this.screen_adapter=new DummyScreenAdapter(this.bus));b.serial_container&&(this.serial_adapter=new SerialAdapter(b.serial_container,this.bus));b.serial_container_xtermjs&&
(this.serial_adapter=new SerialAdapterXtermJS(b.serial_container_xtermjs,this.bus));b.disable_speaker||(this.speaker_adapter=new SpeakerAdapter(this.bus));var f=[];b.state&&console.warn("Warning: Unknown option 'state'. Did you mean 'initial_state'?");for(var k="bios vga_bios cdrom hda hdb fda fdb initial_state multiboot bzimage initrd".split(" "),m=0;m<k.length;m++)d(k[m],b[k[m]]);if(b.filesystem){k=b.filesystem.basefs;m=b.filesystem.baseurl;let r=new MemoryFileStorage;m&&(r=new ServerFileStorageWrapper(r,
m));g.fs9p=this.fs9p=new FS(r);if(k){dbg_assert(m,"Filesystem: baseurl must be specified");if("object"===typeof k){var l=k.size;k=k.url}dbg_assert("string"===typeof k);f.push({name:"fs9p_json",url:k,size:l,as_json:!0})}}var n=this,q=f.length,t=function(r){if(r===q)setTimeout(e.bind(this),0);else{var p=f[r];p.loadable?(p.loadable.onload=function(v){c.call(this,p.name,p.loadable);t(r+1)}.bind(this),p.loadable.load()):v86util.load_file(p.url,{done:function(v){c.call(this,p.name,p.as_json?v:new v86util.SyncBuffer(v));
t(r+1)}.bind(this),progress:function(v){200===v.target.status?n.emulator_bus.send("download-progress",{file_index:r,file_count:q,file_name:p.url,lengthComputable:v.lengthComputable,total:v.total||p.size,loaded:v.loaded}):n.emulator_bus.send("download-error",{file_index:r,file_count:q,file_name:p.url,request:v.target})},as_json:p.as_json})}}.bind(this);t(0)};
V86Starter.prototype.get_bzimage_initrd_from_filesystem=function(a){const b=(a.read_dir("/")||[]).map(e=>"/"+e);a=(a.read_dir("/boot/")||[]).map(e=>"/boot/"+e);let c,d;for(let e of[].concat(b,a)){const g=/old/i.test(e)||/fallback/i.test(e),f=/vmlinuz/i.test(e)||/bzimage/i.test(e),k=/initrd/i.test(e)||/initramfs/i.test(e);!f||d&&g||(d=e);!k||c&&g||(c=e)}c&&d||(console.log("Failed to find bzimage or initrd in filesystem. Files:"),console.log(b.join(" ")),console.log(a.join(" ")));return{initrd_path:c,
bzimage_path:d}};V86Starter.prototype.run=async function(){this.bus.send("cpu-run")};goog.exportProperty(V86Starter.prototype,"run",V86Starter.prototype.run);V86Starter.prototype.stop=async function(){this.cpu_is_running&&await new Promise(a=>{const b=()=>{this.remove_listener("emulator-stopped",b);a()};this.add_listener("emulator-stopped",b);this.bus.send("cpu-stop")})};goog.exportProperty(V86Starter.prototype,"stop",V86Starter.prototype.stop);
V86Starter.prototype.destroy=async function(){await this.stop();this.v86.destroy();this.keyboard_adapter&&this.keyboard_adapter.destroy();this.network_adapter&&this.network_adapter.destroy();this.mouse_adapter&&this.mouse_adapter.destroy();this.screen_adapter&&this.screen_adapter.destroy();this.serial_adapter&&this.serial_adapter.destroy();this.speaker_adapter&&this.speaker_adapter.destroy()};goog.exportProperty(V86Starter.prototype,"destroy",V86Starter.prototype.destroy);
V86Starter.prototype.restart=function(){this.bus.send("cpu-restart")};goog.exportProperty(V86Starter.prototype,"restart",V86Starter.prototype.restart);V86Starter.prototype.add_listener=function(a,b){this.bus.register(a,b,this)};goog.exportProperty(V86Starter.prototype,"add_listener",V86Starter.prototype.add_listener);V86Starter.prototype.remove_listener=function(a,b){this.bus.unregister(a,b)};goog.exportProperty(V86Starter.prototype,"remove_listener",V86Starter.prototype.remove_listener);
V86Starter.prototype.restore_state=async function(a){dbg_assert(1===arguments.length);this.v86.restore_state(a)};goog.exportProperty(V86Starter.prototype,"restore_state",V86Starter.prototype.restore_state);V86Starter.prototype.save_state=async function(){dbg_assert(0===arguments.length);return this.v86.save_state()};goog.exportProperty(V86Starter.prototype,"save_state",V86Starter.prototype.save_state);
V86Starter.prototype.get_statistics=function(){console.warn("V86Starter.prototype.get_statistics is deprecated. Use events instead.");var a={cpu:{instruction_counter:this.get_instruction_counter()}};if(!this.v86)return a;var b=this.v86.cpu.devices;b.hda&&(a.hda=b.hda.stats);b.cdrom&&(a.cdrom=b.cdrom.stats);b.ps2&&(a.mouse={enabled:b.ps2.use_mouse});b.vga&&(a.vga={is_graphical:b.vga.stats.is_graphical});return a};goog.exportProperty(V86Starter.prototype,"get_statistics",V86Starter.prototype.get_statistics);
V86Starter.prototype.get_instruction_counter=function(){return this.v86?this.v86.cpu.instruction_counter[0]>>>0:0};goog.exportProperty(V86Starter.prototype,"get_instruction_counter",V86Starter.prototype.get_instruction_counter);V86Starter.prototype.is_running=function(){return this.cpu_is_running};goog.exportProperty(V86Starter.prototype,"is_running",V86Starter.prototype.is_running);V86Starter.prototype.keyboard_send_scancodes=function(a){for(var b=0;b<a.length;b++)this.bus.send("keyboard-code",a[b])};
goog.exportProperty(V86Starter.prototype,"keyboard_send_scancodes",V86Starter.prototype.keyboard_send_scancodes);V86Starter.prototype.keyboard_send_keys=function(a){for(var b=0;b<a.length;b++)this.keyboard_adapter.simulate_press(a[b])};goog.exportProperty(V86Starter.prototype,"keyboard_send_keys",V86Starter.prototype.keyboard_send_keys);V86Starter.prototype.keyboard_send_text=function(a){for(var b=0;b<a.length;b++)this.keyboard_adapter.simulate_char(a[b])};
goog.exportProperty(V86Starter.prototype,"keyboard_send_text",V86Starter.prototype.keyboard_send_text);V86Starter.prototype.screen_make_screenshot=function(){this.screen_adapter&&this.screen_adapter.make_screenshot()};goog.exportProperty(V86Starter.prototype,"screen_make_screenshot",V86Starter.prototype.screen_make_screenshot);V86Starter.prototype.screen_set_scale=function(a,b){this.screen_adapter&&this.screen_adapter.set_scale(a,b)};goog.exportProperty(V86Starter.prototype,"screen_set_scale",V86Starter.prototype.screen_set_scale);
V86Starter.prototype.screen_go_fullscreen=function(){if(this.screen_adapter){var a=document.getElementById("screen_container");if(a){var b=a.requestFullScreen||a.webkitRequestFullscreen||a.mozRequestFullScreen||a.msRequestFullScreen;b&&(b.call(a),(a=document.getElementsByClassName("phone_keyboard")[0])&&a.focus());this.lock_mouse()}}};goog.exportProperty(V86Starter.prototype,"screen_go_fullscreen",V86Starter.prototype.screen_go_fullscreen);
V86Starter.prototype.screen_go_fullscreen=function(){if(this.screen_adapter){var a=document.getElementById("screen_container");if(a){var b=a.requestFullScreen||a.webkitRequestFullscreen||a.mozRequestFullScreen||a.msRequestFullScreen;b&&(b.call(a),(a=document.getElementsByClassName("phone_keyboard")[0])&&a.focus());try{navigator.keyboard.lock()}catch(c){}this.lock_mouse()}}};goog.exportProperty(V86Starter.prototype,"screen_go_fullscreen",V86Starter.prototype.screen_go_fullscreen);
V86Starter.prototype.lock_mouse=function(){var a=document.body,b=a.requestPointerLock||a.mozRequestPointerLock||a.webkitRequestPointerLock;b&&b.call(a)};goog.exportProperty(V86Starter.prototype,"lock_mouse",V86Starter.prototype.lock_mouse);V86Starter.prototype.mouse_set_status=function(a){this.mouse_adapter&&(this.mouse_adapter.emu_enabled=a)};V86Starter.prototype.keyboard_set_status=function(a){this.keyboard_adapter&&(this.keyboard_adapter.emu_enabled=a)};
goog.exportProperty(V86Starter.prototype,"keyboard_set_status",V86Starter.prototype.keyboard_set_status);V86Starter.prototype.serial0_send=function(a){for(var b=0;b<a.length;b++)this.bus.send("serial0-input",a.charCodeAt(b))};goog.exportProperty(V86Starter.prototype,"serial0_send",V86Starter.prototype.serial0_send);V86Starter.prototype.serial_send_bytes=function(a,b){for(var c=0;c<b.length;c++)this.bus.send("serial"+a+"-input",b[c])};goog.exportProperty(V86Starter.prototype,"serial_send_bytes",V86Starter.prototype.serial_send_bytes);
V86Starter.prototype.mount_fs=async function(a,b,c,d){let e=new MemoryFileStorage;b&&(e=new ServerFileStorageWrapper(e,b));const g=new FS(e,this.fs9p.qidcounter),f=()=>{const k=this.fs9p.Mount(a,g);d&&(k===-ENOENT?d(new FileNotFoundError):k===-EEXIST?d(new FileExistsError):0>k?(dbg_assert(!1,"Unexpected error code: "+-k),d(Error("Failed to mount. Error number: "+-k))):d(null))};b?(dbg_assert("object"===typeof c,"Filesystem: basefs must be a JSON object"),g.load_from_json(c,()=>f())):f()};
goog.exportProperty(V86Starter.prototype,"mount_fs",V86Starter.prototype.mount_fs);V86Starter.prototype.create_file=function(a,b,c){c=c||function(){};var d=this.fs9p;if(d){var e=a.split("/");e=e[e.length-1];a=d.SearchPath(a).parentid;""!==e&&-1!==a?d.CreateBinaryFile(e,a,b).then(()=>c(null)):setTimeout(function(){c(new FileNotFoundError)},0)}};goog.exportProperty(V86Starter.prototype,"create_file",V86Starter.prototype.create_file);
V86Starter.prototype.read_file=function(a,b){var c=this.fs9p;c&&c.read_file(a).then(d=>{d?b(null,d):b(new FileNotFoundError,null)})};goog.exportProperty(V86Starter.prototype,"read_file",V86Starter.prototype.read_file);
goog.exportProperty(V86Starter.prototype,"mount_fs",V86Starter.prototype.mount_fs);V86Starter.prototype.create_file=async function(a,b){dbg_assert(2===arguments.length);var c=this.fs9p;if(c){var d=a.split("/");d=d[d.length-1];var e=c.SearchPath(a).parentid;if(""!==d&&-1!==e)await c.CreateBinaryFile(d,e,b);else return Promise.reject(new FileNotFoundError)}};goog.exportProperty(V86Starter.prototype,"create_file",V86Starter.prototype.create_file);
V86Starter.prototype.read_file=async function(a){dbg_assert(1===arguments.length);var b=this.fs9p;if(b)return(b=await b.read_file(a))?b:Promise.reject(new FileNotFoundError)};goog.exportProperty(V86Starter.prototype,"read_file",V86Starter.prototype.read_file);
V86Starter.prototype.automatically=function(a){const b=c=>{const d=c[0];if(d){var e=c.slice(1);if(d.sleep)setTimeout(()=>b(e),1E3*d.sleep);else if(d.vga_text){const g=this.screen_adapter.get_text_screen();for(let f of g)if(f.includes(d.vga_text)){b(e);return}setTimeout(()=>b(c),1E3)}else d.keyboard_send?(d.keyboard_send instanceof Array?this.keyboard_send_scancodes(d.keyboard_send):(dbg_assert("string"===typeof d.keyboard_send),this.keyboard_send_text(d.keyboard_send)),b(e)):d.call?(d.call(),b(e)):
console.assert(!1,d)}};b(a)};V86Starter.prototype.read_memory=function(a,b){return this.v86.cpu.read_blob(a,b)};V86Starter.prototype.write_memory=function(a,b){this.v86.cpu.write_blob(a,b)};function FileExistsError(a){this.message=a||"File already exists"}FileExistsError.prototype=Error.prototype;function FileNotFoundError(a){this.message=a||"File not found"}FileNotFoundError.prototype=Error.prototype;
dbg_assert(!1,d)}};b(a)};V86Starter.prototype.read_memory=function(a,b){return this.v86.cpu.read_blob(a,b)};V86Starter.prototype.write_memory=function(a,b){this.v86.cpu.write_blob(a,b)};function FileExistsError(a){this.message=a||"File already exists"}FileExistsError.prototype=Error.prototype;function FileNotFoundError(a){this.message=a||"File not found"}FileNotFoundError.prototype=Error.prototype;
"undefined"!==typeof window?(window.V86Starter=V86Starter,window.V86=V86Starter):"undefined"!==typeof module&&"undefined"!==typeof module.exports?(module.exports.V86Starter=V86Starter,module.exports.V86=V86Starter):"function"===typeof importScripts&&(self.V86Starter=V86Starter,self.V86=V86Starter);var WorkerBus={Connector:function(a){this.listeners={};this.pair=a;a.addEventListener("message",function(b){b=b.data;for(var c=this.listeners[b[0]],d=0;d<c.length;d++){var e=c[d];e.fn.call(e.this_value,b[1])}}.bind(this),!1)}};WorkerBus.Connector.prototype.register=function(a,b,c){var d=this.listeners[a];void 0===d&&(d=this.listeners[a]=[]);d.push({fn:b,this_value:c})};WorkerBus.Connector.prototype.send=function(a,b,c){dbg_assert(1<=arguments.length);this.pair&&this.pair.postMessage([a,b],c)};
WorkerBus.init=function(a){return new WorkerBus.Connector(a)};function DummyScreenAdapter(a){var b,c,d,e,g;this.bus=a;a.register("screen-set-mode",function(f){this.set_mode(f)},this);a.register("screen-fill-buffer-end",function(f){this.update_buffer(f[0],f[1])},this);a.register("screen-put-char",function(f){this.put_char(f[0],f[1],f[2],f[3],f[4])},this);a.register("screen-text-scroll",function(f){console.log("scroll",f)},this);a.register("screen-update-cursor",function(f){this.update_cursor(f[0],f[1])},this);a.register("screen-update-cursor-scanline",function(f){this.update_cursor_scanline(f[0],
f[1])},this);a.register("screen-set-size-text",function(f){this.set_size_text(f[0],f[1])},this);a.register("screen-set-size-graphical",function(f){this.set_size_graphical(f[0],f[1])},this);this.put_char=function(f,k,m,l,n){f<g&&k<e&&(f=3*(f*e+k),d[f]=m,d[f+1]=l,d[f+2]=n)};this.destroy=function(){};this.set_mode=function(f){};this.clear_screen=function(){};this.set_size_text=function(f,k){if(f!==e||k!==g)d=new Int32Array(f*k*3),e=f,g=k};this.set_size_graphical=function(f,k){};this.set_scale=function(f,
k){};this.update_cursor_scanline=function(f,k){};this.update_cursor=function(f,k){if(f!==b||k!==c)b=f,c=k};this.update_buffer=function(f,k){};this.get_text_screen=function(){for(var f=[],k=0;k<g;k++)f.push(this.get_text_row(k));return f};this.get_text_row=function(f){var k="";f=3*f*e;for(var m=0;m<e;m++)k+=String.fromCharCode(d[f+3*m]);return k}};const print_stats={stats_to_string:function(a){return print_stats.print_misc_stats(a)+print_stats.print_instruction_counts(a)},print_misc_stats:function(a){let b="";var c="COMPILE COMPILE_SKIPPED_NO_NEW_ENTRY_POINTS COMPILE_SUCCESS COMPILE_WRONG_ADDRESS_SPACE COMPILE_CUT_OFF_AT_END_OF_PAGE COMPILE_WITH_LOOP_SAFETY COMPILE_PAGE COMPILE_PAGE/COMPILE_SUCCESS COMPILE_PAGE_SKIPPED_NO_NEW_ENTRY_POINTS COMPILE_BASIC_BLOCK COMPILE_DUPLICATED_BASIC_BLOCK COMPILE_WASM_BLOCK COMPILE_WASM_LOOP COMPILE_DISPATCHER COMPILE_ENTRY_POINT COMPILE_WASM_TOTAL_BYTES COMPILE_WASM_TOTAL_BYTES/COMPILE_PAGE JIT_CACHE_OVERRIDE JIT_CACHE_OVERRIDE_DIFFERENT_STATE_FLAGS RUN_INTERPRETED RUN_INTERPRETED_PENDING RUN_INTERPRETED_NEAR_END_OF_PAGE RUN_INTERPRETED_DIFFERENT_STATE RUN_INTERPRETED_MISSED_COMPILED_ENTRY_RUN_INTERPRETED RUN_INTERPRETED_MISSED_COMPILED_ENTRY_LOOKUP RUN_INTERPRETED_STEPS RUN_FROM_CACHE RUN_FROM_CACHE_STEPS RUN_FROM_CACHE_STEPS/RUN_FROM_CACHE RUN_FROM_CACHE_STEPS/RUN_INTERPRETED_STEPS DIRECT_EXIT INDIRECT_JUMP INDIRECT_JUMP_NO_ENTRY NORMAL_PAGE_CHANGE NORMAL_FALLTHRU NORMAL_FALLTHRU_WITH_TARGET_BLOCK NORMAL_BRANCH NORMAL_BRANCH_WITH_TARGET_BLOCK CONDITIONAL_JUMP CONDITIONAL_JUMP_PAGE_CHANGE CONDITIONAL_JUMP_EXIT CONDITIONAL_JUMP_FALLTHRU CONDITIONAL_JUMP_FALLTHRU_WITH_TARGET_BLOCK CONDITIONAL_JUMP_BRANCH CONDITIONAL_JUMP_BRANCH_WITH_TARGET_BLOCK DISPATCHER_SMALL DISPATCHER_LARGE LOOP LOOP_SAFETY CONDITION_OPTIMISED CONDITION_UNOPTIMISED FAILED_PAGE_CHANGE SAFE_READ_FAST SAFE_READ_SLOW_PAGE_CROSSED SAFE_READ_SLOW_NOT_VALID SAFE_READ_SLOW_NOT_USER SAFE_READ_SLOW_IN_MAPPED_RANGE SAFE_WRITE_FAST SAFE_WRITE_SLOW_PAGE_CROSSED SAFE_WRITE_SLOW_NOT_VALID SAFE_WRITE_SLOW_NOT_USER SAFE_WRITE_SLOW_IN_MAPPED_RANGE SAFE_WRITE_SLOW_READ_ONLY SAFE_WRITE_SLOW_HAS_CODE SAFE_READ_WRITE_FAST SAFE_READ_WRITE_SLOW_PAGE_CROSSED SAFE_READ_WRITE_SLOW_NOT_VALID SAFE_READ_WRITE_SLOW_NOT_USER SAFE_READ_WRITE_SLOW_IN_MAPPED_RANGE SAFE_READ_WRITE_SLOW_READ_ONLY SAFE_READ_WRITE_SLOW_HAS_CODE PAGE_FAULT TLB_MISS DO_RUN DO_MANY_CYCLES CYCLE_INTERNAL INVALIDATE_ALL_MODULES_NO_FREE_WASM_INDICES INVALIDATE_MODULE_WRITTEN_WHILE_COMPILED INVALIDATE_MODULE_UNUSED_AFTER_OVERWRITE INVALIDATE_MODULE_DIRTY_PAGE INVALIDATE_PAGE_HAD_CODE INVALIDATE_PAGE_HAD_ENTRY_POINTS DIRTY_PAGE_DID_NOT_HAVE_CODE RUN_FROM_CACHE_EXIT_SAME_PAGE RUN_FROM_CACHE_EXIT_NEAR_END_OF_PAGE RUN_FROM_CACHE_EXIT_DIFFERENT_PAGE CLEAR_TLB FULL_CLEAR_TLB TLB_FULL TLB_GLOBAL_FULL MODRM_SIMPLE_REG MODRM_SIMPLE_REG_WITH_OFFSET MODRM_SIMPLE_CONST_OFFSET MODRM_COMPLEX SEG_OFFSET_OPTIMISED SEG_OFFSET_NOT_OPTIMISED".split(" "),
d=0;const e={};for(let f=0;f<c.length;f++){const k=c[f];var g=void 0;if(k.includes("/")){d++;const [m,l]=k.split("/");g=e[m]/e[l]}else g=e[k]=a.wm.exports.profiler_stat_get(f-d),g=1E8<=g?Math.round(g/1E6)+"m":1E5<=g?Math.round(g/1E3)+"k":g;b+=k+"="+g+"\n"}b+="\n";c=a.wm.exports.get_valid_tlb_entries_count();d=a.wm.exports.get_valid_global_tlb_entries_count();b=b+("TLB_ENTRIES="+c+" ("+d+" global, "+(c-d)+" non-global)\nWASM_TABLE_FREE=")+(a.wm.exports.jit_get_wasm_table_index_free_list_count()+"\n");
b+="JIT_CACHE_SIZE="+a.wm.exports.jit_get_cache_size()+"\n";b+="FLAT_SEGMENTS="+a.wm.exports.has_flat_segmentation()+"\n";b+="do_many_cycles avg: "+(a.do_many_cycles_total/a.do_many_cycles_count||0)+"\n";b+="wasm memory size: "+(a.wasm_memory.buffer.byteLength>>20)+"m\n";b=b+"Config:\nMAX_PAGES="+(a.wm.exports.get_config(0)+"\n");b+="JIT_USE_LOOP_SAFETY="+a.wm.exports.get_config(1)+"\n";return b+="MAX_EXTRA_BASIC_BLOCKS="+a.wm.exports.get_config(2)+"\n"},print_instruction_counts:function(a){return[print_stats.print_instruction_counts_offset(a,
!1,!1,!1,!1),print_stats.print_instruction_counts_offset(a,!0,!1,!1,!1),print_stats.print_instruction_counts_offset(a,!1,!0,!1,!1),print_stats.print_instruction_counts_offset(a,!1,!1,!0,!1),print_stats.print_instruction_counts_offset(a,!1,!1,!1,!0)].join("\n\n")},print_instruction_counts_offset:function(a,b,c,d,e){let g="";var f=[],k=b?"compiled":c?"jit exit":d?"unguarded register":e?"wasm size":"executed";for(let n=0;256>n;n++)for(let p=0;8>p;p++)for(let t of[!1,!0]){var m=a.wm.exports.get_opstats_buffer(b,
c,d,e,n,!1,t,p);f.push({opcode:n,count:m,is_mem:t,fixed_g:p});m=a.wm.exports.get_opstats_buffer(b,c,d,e,n,!0,t,p);f.push({opcode:3840|n,count:m,is_mem:t,fixed_g:p})}a=0;b=new Set([38,46,54,62,100,101,102,103,240,242,243]);for(let {count:n,opcode:p}of f)b.has(p)||(a+=n);if(0===a)return"";c=new Uint32Array(256);b=new Uint32Array(256);for(let {opcode:n,count:p}of f)3840==(n&65280)?b[n&255]+=p:c[n&255]+=p;g=g+"------------------\nTotal: "+(a+"\n");const l=1E7<a?1E3:1;d=Math.max.apply(Math,f.map(({count:n})=>
Math.round(n/l)));d=String(d).length;g+=`Instruction counts ${k} (in ${l}):\n`;for(e=0;256>e;e++)g+=h(e,2).slice(2)+":"+v86util.pads(Math.round(c[e]/l),d),g=15==e%16?g+"\n":g+" ";g=g+"\n"+`Instruction counts ${k} (0f, in ${l}):\n`;for(k=0;256>k;k++)g+=h(k&255,2).slice(2)+":"+v86util.pads(Math.round(b[k]/l),d),g=15==k%16?g+"\n":g+" ";g+="\n";f=f.filter(({count:n})=>n).sort(({count:n},{count:p})=>p-n);for(let {opcode:n,is_mem:p,fixed_g:t,count:r}of f.slice(0,200))f=n.toString(16)+"_"+t+(p?"_m":"_r"),
g+=f+":"+(r/a*100).toFixed(2)+" ";return g+"\n"}};"undefined"!==typeof module&&"undefined"!==typeof module.exports&&(module.exports.print_stats=print_stats);function FileStorageInterface(){}FileStorageInterface.prototype.read=function(a,b,c){};FileStorageInterface.prototype.cache=function(a,b){};FileStorageInterface.prototype.uncache=function(a){};function MemoryFileStorage(){this.filedata=new Map}MemoryFileStorage.prototype.read=async function(a,b,c){dbg_assert(a,"MemoryFileStorage read: sha256sum should be a non-empty string");return(a=this.filedata.get(a))?a.subarray(b,b+c):null};
!1,!1,!1,!1),print_stats.print_instruction_counts_offset(a,!0,!1,!1,!1),print_stats.print_instruction_counts_offset(a,!1,!0,!1,!1),print_stats.print_instruction_counts_offset(a,!1,!1,!0,!1),print_stats.print_instruction_counts_offset(a,!1,!1,!1,!0)].join("\n\n")},print_instruction_counts_offset:function(a,b,c,d,e){let g="";var f=[],k=b?"compiled":c?"jit exit":d?"unguarded register":e?"wasm size":"executed";for(let n=0;256>n;n++)for(let q=0;8>q;q++)for(let t of[!1,!0]){var m=a.wm.exports.get_opstats_buffer(b,
c,d,e,n,!1,t,q);f.push({opcode:n,count:m,is_mem:t,fixed_g:q});m=a.wm.exports.get_opstats_buffer(b,c,d,e,n,!0,t,q);f.push({opcode:3840|n,count:m,is_mem:t,fixed_g:q})}a=0;b=new Set([38,46,54,62,100,101,102,103,240,242,243]);for(let {count:n,opcode:q}of f)b.has(q)||(a+=n);if(0===a)return"";c=new Uint32Array(256);b=new Uint32Array(256);for(let {opcode:n,count:q}of f)3840==(n&65280)?b[n&255]+=q:c[n&255]+=q;g=g+"------------------\nTotal: "+(a+"\n");const l=1E7<a?1E3:1;d=Math.max.apply(Math,f.map(({count:n})=>
Math.round(n/l)));d=String(d).length;g+=`Instruction counts ${k} (in ${l}):\n`;for(e=0;256>e;e++)g+=e.toString(16).padStart(2,"0")+":"+v86util.pads(Math.round(c[e]/l),d),g=15==e%16?g+"\n":g+" ";g=g+"\n"+`Instruction counts ${k} (0f, in ${l}):\n`;for(k=0;256>k;k++)g+=(k&255).toString(16).padStart(2,"0")+":"+v86util.pads(Math.round(b[k]/l),d),g=15==k%16?g+"\n":g+" ";g+="\n";f=f.filter(({count:n})=>n).sort(({count:n},{count:q})=>q-n);for(let {opcode:n,is_mem:q,fixed_g:t,count:r}of f.slice(0,200))f=n.toString(16)+
"_"+t+(q?"_m":"_r"),g+=f+":"+(r/a*100).toFixed(2)+" ";return g+"\n"}};"undefined"!==typeof module&&"undefined"!==typeof module.exports&&(module.exports.print_stats=print_stats);function FileStorageInterface(){}FileStorageInterface.prototype.read=function(a,b,c){};FileStorageInterface.prototype.cache=function(a,b){};FileStorageInterface.prototype.uncache=function(a){};function MemoryFileStorage(){this.filedata=new Map}MemoryFileStorage.prototype.read=async function(a,b,c){dbg_assert(a,"MemoryFileStorage read: sha256sum should be a non-empty string");return(a=this.filedata.get(a))?a.subarray(b,b+c):null};
MemoryFileStorage.prototype.cache=async function(a,b){dbg_assert(a,"MemoryFileStorage cache: sha256sum should be a non-empty string");this.filedata.set(a,b)};MemoryFileStorage.prototype.uncache=function(a){this.filedata.delete(a)};function ServerFileStorageWrapper(a,b){dbg_assert(b,"ServerMemoryFileStorage: baseurl should not be empty");this.storage=a;this.baseurl=b}
ServerFileStorageWrapper.prototype.load_from_server=function(a){return new Promise((b,c)=>{v86util.load_file(this.baseurl+a,{done:d=>{const e=new Uint8Array(d);this.cache(a,e).then(()=>b(e))}})})};ServerFileStorageWrapper.prototype.read=async function(a,b,c){const d=await this.storage.read(a,b,c);return d?d:(await this.load_from_server(a)).subarray(b,b+c)};ServerFileStorageWrapper.prototype.cache=async function(a,b){return await this.storage.cache(a,b)};
ServerFileStorageWrapper.prototype.uncache=function(a){this.storage.uncache(a)};"undefined"!==typeof window?(window.MemoryFileStorage=MemoryFileStorage,window.ServerFileStorageWrapper=ServerFileStorageWrapper):"undefined"!==typeof module&&"undefined"!==typeof module.exports?(module.exports.MemoryFileStorage=MemoryFileStorage,module.exports.ServerFileStorageWrapper=ServerFileStorageWrapper):"function"===typeof importScripts&&(self.MemoryFileStorage=MemoryFileStorage,self.ServerFileStorageWrapper=ServerFileStorageWrapper);var S_IRWXUGO=511,S_IFMT=61440,S_IFSOCK=49152,S_IFLNK=40960,S_IFREG=32768,S_IFBLK=24576,S_IFDIR=16384,S_IFCHR=8192,O_RDONLY=0,O_WRONLY=1,O_RDWR=2,O_ACCMODE=3,STATUS_INVALID=-1,STATUS_OK=0,STATUS_ON_STORAGE=2,STATUS_UNLINKED=4,STATUS_FORWARDING=5,JSONFS_VERSION=3,JSONFS_IDX_NAME=0,JSONFS_IDX_SIZE=1,JSONFS_IDX_MTIME=2,JSONFS_IDX_MODE=3,JSONFS_IDX_UID=4,JSONFS_IDX_GID=5,JSONFS_IDX_TARGET=6,JSONFS_IDX_SHA256=6;
ServerFileStorageWrapper.prototype.load_from_server=function(a){return new Promise((b,c)=>{v86util.load_file(this.baseurl+a,{done:async d=>{d=new Uint8Array(d);await this.cache(a,d);b(d)}})})};ServerFileStorageWrapper.prototype.read=async function(a,b,c){const d=await this.storage.read(a,b,c);return d?d:(await this.load_from_server(a)).subarray(b,b+c)};ServerFileStorageWrapper.prototype.cache=async function(a,b){return await this.storage.cache(a,b)};ServerFileStorageWrapper.prototype.uncache=function(a){this.storage.uncache(a)};
"undefined"!==typeof window?(window.MemoryFileStorage=MemoryFileStorage,window.ServerFileStorageWrapper=ServerFileStorageWrapper):"undefined"!==typeof module&&"undefined"!==typeof module.exports?(module.exports.MemoryFileStorage=MemoryFileStorage,module.exports.ServerFileStorageWrapper=ServerFileStorageWrapper):"function"===typeof importScripts&&(self.MemoryFileStorage=MemoryFileStorage,self.ServerFileStorageWrapper=ServerFileStorageWrapper);var S_IRWXUGO=511,S_IFMT=61440,S_IFSOCK=49152,S_IFLNK=40960,S_IFREG=32768,S_IFBLK=24576,S_IFDIR=16384,S_IFCHR=8192,O_RDONLY=0,O_WRONLY=1,O_RDWR=2,O_ACCMODE=3,STATUS_INVALID=-1,STATUS_OK=0,STATUS_ON_STORAGE=2,STATUS_UNLINKED=4,STATUS_FORWARDING=5,JSONFS_VERSION=3,JSONFS_IDX_NAME=0,JSONFS_IDX_SIZE=1,JSONFS_IDX_MTIME=2,JSONFS_IDX_MODE=3,JSONFS_IDX_UID=4,JSONFS_IDX_GID=5,JSONFS_IDX_TARGET=6,JSONFS_IDX_SHA256=6;
function FS(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)}FS.prototype.get_state=function(){let a=[];a[0]=this.inodes;a[1]=this.qidcounter.last_qidnumber;a[2]=[];for(const [b,c]of Object.entries(this.inodedata))0===(this.inodes[b].mode&S_IFDIR)&&a[2].push([b,c]);a[3]=this.total_size;a[4]=this.used_size;return a=a.concat(this.mounts)};
FS.prototype.set_state=function(a){this.inodes=a[0].map(b=>{const c=new Inode(0);c.set_state(b);return c});this.qidcounter.last_qidnumber=a[1];this.inodedata={};for(let [b,c]of a[2])c.buffer.byteLength!==c.byteLength&&(c=c.slice()),this.inodedata[b]=c;this.total_size=a[3];this.used_size=a[4];this.mounts=a.slice(5)};
FS.prototype.AddEvent=function(a,b){var c=this.inodes[a];c.status==STATUS_OK||c.status==STATUS_ON_STORAGE?b():this.is_forwarder(c)?this.follow_fs(c).AddEvent(c.foreign_id,b):this.events.push({id:a,OnEvent:b})};FS.prototype.HandleEvent=function(a){var b=this.inodes[a];this.is_forwarder(b)&&this.follow_fs(b).HandleEvent(b.foreign_id);b=[];for(var c=0;c<this.events.length;c++)this.events[c].id==a?this.events[c].OnEvent():b.push(this.events[c]);this.events=b};
@@ -766,7 +777,7 @@ FS.prototype.CreateBinaryFile=async function(a,b,c){var d=this.inodes[b];if(this
FS.prototype.OpenInode=function(a,b){var c=this.inodes[a];if(this.is_forwarder(c))return this.follow_fs(c).OpenInode(c.foreign_id,b);(c.mode&S_IFMT)==S_IFDIR&&this.FillDirectory(a);return!0};FS.prototype.CloseInode=async function(a){var b=this.inodes[a];if(this.is_forwarder(b))return await this.follow_fs(b).CloseInode(b.foreign_id);b.status===STATUS_ON_STORAGE&&this.storage.uncache(b.sha256sum);b.status==STATUS_UNLINKED&&(b.status=STATUS_INVALID,await this.DeleteData(a))};
FS.prototype.Rename=async function(a,b,c,d){if(a==c&&b==d)return 0;var e=this.Search(a,b);if(-1===e)return-ENOENT;var g=this.GetFullPath(a)+"/"+b;if(-1!=this.Search(c,d)){var f=this.Unlink(c,d);if(0>f)return f}var k=this.inodes[e],m=this.inodes[a];f=this.inodes[c];if(this.is_forwarder(m)||this.is_forwarder(f))if(this.is_forwarder(m)&&m.mount_id===f.mount_id){if(a=await this.follow_fs(m).Rename(m.foreign_id,b,f.foreign_id,d),0>a)return a}else{if(this.is_a_root(e))return dbg_log("XXX: Attempted to move mountpoint ("+
b+") - skipped",LOG_9P),-EPERM;if(!this.IsDirectory(e)&&1<this.GetInode(e).nlinks)return dbg_log("XXX: Attempted to move hardlinked file ("+b+") across filesystems - skipped",LOG_9P),-EPERM;m=this.divert(a,b);const l=this.GetInode(e),n=await this.Read(m,0,l.size);this.is_forwarder(f)?(c=this.follow_fs(f),d=this.IsDirectory(m)?c.CreateDirectory(d,f.foreign_id):c.CreateFile(d,f.foreign_id),c=c.GetInode(d),this.copy_inode(l,c),this.set_forwarder(e,f.mount_id,d)):(this.delete_forwarder(k),this.copy_inode(l,
k),this.link_under_dir(c,e,d));await this.ChangeSize(e,l.size);n&&n.length&&await this.Write(e,0,n.length,n);if(this.IsDirectory(e))for(const p of this.GetChildren(m))if(f=await this.Rename(m,p,e,p),0>f)return f;await this.DeleteData(m);a=this.Unlink(a,b);if(0>a)return a}else this.unlink_from_dir(a,b),this.link_under_dir(c,e,d),k.qid.version++;this.NotifyListeners(e,"rename",{oldpath:g});return 0};
k),this.link_under_dir(c,e,d));await this.ChangeSize(e,l.size);n&&n.length&&await this.Write(e,0,n.length,n);if(this.IsDirectory(e))for(const q of this.GetChildren(m))if(f=await this.Rename(m,q,e,q),0>f)return f;await this.DeleteData(m);a=this.Unlink(a,b);if(0>a)return a}else this.unlink_from_dir(a,b),this.link_under_dir(c,e,d),k.qid.version++;this.NotifyListeners(e,"rename",{oldpath:g});return 0};
FS.prototype.Write=async function(a,b,c,d){this.NotifyListeners(a,"write");var e=this.inodes[a];if(this.is_forwarder(e))a=e.foreign_id,await this.follow_fs(e).Write(a,b,c,d);else{var g=await this.get_buffer(a);!g||g.length<b+c?(await this.ChangeSize(a,Math.floor(3*(b+c)/2)),e.size=b+c,g=await this.get_buffer(a)):e.size<b+c&&(e.size=b+c);d&&g.set(d.subarray(0,c),b);await this.set_data(a,g)}};
FS.prototype.Read=async function(a,b,c){const d=this.inodes[a];return this.is_forwarder(d)?(a=d.foreign_id,await this.follow_fs(d).Read(a,b,c)):await this.get_data(a,b,c)};FS.prototype.Search=function(a,b){a=this.inodes[a];if(this.is_forwarder(a)){const c=a.foreign_id;b=this.follow_fs(a).Search(c,b);return-1===b?-1:this.get_forwarder(a.mount_id,b)}b=a.direntries.get(b);return void 0===b?-1:b};
FS.prototype.CountUsedInodes=function(){let a=this.inodes.length;for(const {fs:b,backtrack:c}of this.mounts)a+=b.CountUsedInodes(),a-=c.size;return a};FS.prototype.CountFreeInodes=function(){let a=1048576;for(const {fs:b}of this.mounts)a+=b.CountFreeInodes();return a};FS.prototype.GetTotalSize=function(){let a=this.used_size;for(const {fs:b}of this.mounts)a+=b.GetTotalSize();return a};FS.prototype.GetSpace=function(){let a=this.total_size;for(const {fs:b}of this.mounts)a+=b.GetSpace();return this.total_size};