v86 0.3.4 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (369) hide show
  1. package/Readme.md +4 -4
  2. package/bios/seabios/.config +113 -0
  3. package/bios/seabios/.config.old +114 -0
  4. package/bios/seabios/.gitignore +4 -0
  5. package/bios/seabios/COPYING +674 -0
  6. package/bios/seabios/COPYING.LESSER +165 -0
  7. package/bios/seabios/Makefile +286 -0
  8. package/bios/seabios/README +17 -0
  9. package/bios/seabios/docs/Build_overview.md +104 -0
  10. package/bios/seabios/docs/Contributing.md +20 -0
  11. package/bios/seabios/docs/Debugging.md +111 -0
  12. package/bios/seabios/docs/Developer_Documentation.md +25 -0
  13. package/bios/seabios/docs/Developer_links.md +86 -0
  14. package/bios/seabios/docs/Download.md +27 -0
  15. package/bios/seabios/docs/Execution_and_code_flow.md +178 -0
  16. package/bios/seabios/docs/Linking_overview.md +160 -0
  17. package/bios/seabios/docs/Mailinglist.md +8 -0
  18. package/bios/seabios/docs/Memory_Model.md +253 -0
  19. package/bios/seabios/docs/README +5 -0
  20. package/bios/seabios/docs/Releases.md +482 -0
  21. package/bios/seabios/docs/Runtime_config.md +193 -0
  22. package/bios/seabios/docs/SeaBIOS.md +17 -0
  23. package/bios/seabios/docs/SeaVGABIOS.md +39 -0
  24. package/bios/seabios/out/autoconf.h +117 -0
  25. package/bios/seabios/out/include/config/acpi/dsdt.h +0 -0
  26. package/bios/seabios/out/include/config/acpi.h +0 -0
  27. package/bios/seabios/out/include/config/ahci.h +0 -0
  28. package/bios/seabios/out/include/config/apmbios.h +0 -0
  29. package/bios/seabios/out/include/config/ata/dma.h +0 -0
  30. package/bios/seabios/out/include/config/ata/pio32.h +0 -0
  31. package/bios/seabios/out/include/config/ata.h +0 -0
  32. package/bios/seabios/out/include/config/auto.conf +69 -0
  33. package/bios/seabios/out/include/config/auto.conf.cmd +9 -0
  34. package/bios/seabios/out/include/config/boot.h +0 -0
  35. package/bios/seabios/out/include/config/bootorder.h +0 -0
  36. package/bios/seabios/out/include/config/build/vgabios.h +0 -0
  37. package/bios/seabios/out/include/config/call32/smm.h +0 -0
  38. package/bios/seabios/out/include/config/cdrom/boot.h +0 -0
  39. package/bios/seabios/out/include/config/cdrom/emu.h +0 -0
  40. package/bios/seabios/out/include/config/debug/level.h +0 -0
  41. package/bios/seabios/out/include/config/drives.h +0 -0
  42. package/bios/seabios/out/include/config/entry/extrastack.h +0 -0
  43. package/bios/seabios/out/include/config/esp/scsi.h +0 -0
  44. package/bios/seabios/out/include/config/flash/floppy.h +0 -0
  45. package/bios/seabios/out/include/config/floppy.h +0 -0
  46. package/bios/seabios/out/include/config/fw/romfile/load.h +0 -0
  47. package/bios/seabios/out/include/config/hardware/irq.h +0 -0
  48. package/bios/seabios/out/include/config/kbd/call/int15/4f.h +0 -0
  49. package/bios/seabios/out/include/config/keyboard.h +0 -0
  50. package/bios/seabios/out/include/config/lpt.h +0 -0
  51. package/bios/seabios/out/include/config/lsi/scsi.h +0 -0
  52. package/bios/seabios/out/include/config/malloc/uppermemory.h +0 -0
  53. package/bios/seabios/out/include/config/megasas.h +0 -0
  54. package/bios/seabios/out/include/config/mouse.h +0 -0
  55. package/bios/seabios/out/include/config/mpt/scsi.h +0 -0
  56. package/bios/seabios/out/include/config/mptable.h +0 -0
  57. package/bios/seabios/out/include/config/mtrr/init.h +0 -0
  58. package/bios/seabios/out/include/config/optionroms.h +0 -0
  59. package/bios/seabios/out/include/config/override/pci/id.h +0 -0
  60. package/bios/seabios/out/include/config/pcibios.h +0 -0
  61. package/bios/seabios/out/include/config/pirtable.h +0 -0
  62. package/bios/seabios/out/include/config/pmm.h +0 -0
  63. package/bios/seabios/out/include/config/pmtimer.h +0 -0
  64. package/bios/seabios/out/include/config/pnpbios.h +0 -0
  65. package/bios/seabios/out/include/config/ps2port.h +0 -0
  66. package/bios/seabios/out/include/config/pvscsi.h +0 -0
  67. package/bios/seabios/out/include/config/qemu/hardware.h +0 -0
  68. package/bios/seabios/out/include/config/qemu.h +0 -0
  69. package/bios/seabios/out/include/config/rom/size.h +0 -0
  70. package/bios/seabios/out/include/config/rtc/timer.h +0 -0
  71. package/bios/seabios/out/include/config/s3/resume.h +0 -0
  72. package/bios/seabios/out/include/config/sdcard.h +0 -0
  73. package/bios/seabios/out/include/config/serial.h +0 -0
  74. package/bios/seabios/out/include/config/tcgbios.h +0 -0
  75. package/bios/seabios/out/include/config/threads.h +0 -0
  76. package/bios/seabios/out/include/config/tristate.conf +4 -0
  77. package/bios/seabios/out/include/config/tsc/timer.h +0 -0
  78. package/bios/seabios/out/include/config/use/smm.h +0 -0
  79. package/bios/seabios/out/include/config/vga/allocate/extra/stack.h +0 -0
  80. package/bios/seabios/out/include/config/vga/bochs/stdvga.h +0 -0
  81. package/bios/seabios/out/include/config/vga/bochs.h +0 -0
  82. package/bios/seabios/out/include/config/vga/did.h +0 -0
  83. package/bios/seabios/out/include/config/vga/extra/stack/size.h +0 -0
  84. package/bios/seabios/out/include/config/vga/fixup/asm.h +0 -0
  85. package/bios/seabios/out/include/config/vga/pci.h +0 -0
  86. package/bios/seabios/out/include/config/vga/stdvga/ports.h +0 -0
  87. package/bios/seabios/out/include/config/vga/vbe.h +0 -0
  88. package/bios/seabios/out/include/config/vga/vid.h +0 -0
  89. package/bios/seabios/out/include/config/vgahooks.h +0 -0
  90. package/bios/seabios/out/include/config/virtio/blk.h +0 -0
  91. package/bios/seabios/out/include/config/virtio/scsi.h +0 -0
  92. package/bios/seabios/out/include/config/xen.h +0 -0
  93. package/bios/seabios/out/scripts/kconfig/conf +0 -0
  94. package/bios/seabios/out/scripts/kconfig/conf.o +0 -0
  95. package/bios/seabios/out/scripts/kconfig/zconf.hash.c +289 -0
  96. package/bios/seabios/out/scripts/kconfig/zconf.lex.c +2420 -0
  97. package/bios/seabios/out/scripts/kconfig/zconf.tab.c +2538 -0
  98. package/bios/seabios/out/scripts/kconfig/zconf.tab.o +0 -0
  99. package/bios/seabios/scripts/acpi_extract.py +366 -0
  100. package/bios/seabios/scripts/acpi_extract_preprocess.py +41 -0
  101. package/bios/seabios/scripts/buildrom.py +56 -0
  102. package/bios/seabios/scripts/buildversion.py +134 -0
  103. package/bios/seabios/scripts/checkrom.py +95 -0
  104. package/bios/seabios/scripts/checkstack.py +226 -0
  105. package/bios/seabios/scripts/checksum.py +16 -0
  106. package/bios/seabios/scripts/encodeint.py +21 -0
  107. package/bios/seabios/scripts/gen-offsets.sh +17 -0
  108. package/bios/seabios/scripts/kconfig/.gitignore +22 -0
  109. package/bios/seabios/scripts/kconfig/Makefile +331 -0
  110. package/bios/seabios/scripts/kconfig/POTFILES.in +12 -0
  111. package/bios/seabios/scripts/kconfig/check.sh +13 -0
  112. package/bios/seabios/scripts/kconfig/conf.c +718 -0
  113. package/bios/seabios/scripts/kconfig/confdata.c +1250 -0
  114. package/bios/seabios/scripts/kconfig/expr.c +1168 -0
  115. package/bios/seabios/scripts/kconfig/expr.h +241 -0
  116. package/bios/seabios/scripts/kconfig/gconf.c +1542 -0
  117. package/bios/seabios/scripts/kconfig/gconf.glade +661 -0
  118. package/bios/seabios/scripts/kconfig/images.c +326 -0
  119. package/bios/seabios/scripts/kconfig/kxgettext.c +235 -0
  120. package/bios/seabios/scripts/kconfig/lex.zconf.c +2430 -0
  121. package/bios/seabios/scripts/kconfig/list.h +131 -0
  122. package/bios/seabios/scripts/kconfig/lkc.h +200 -0
  123. package/bios/seabios/scripts/kconfig/lkc_proto.h +57 -0
  124. package/bios/seabios/scripts/kconfig/lxdialog/.gitignore +4 -0
  125. package/bios/seabios/scripts/kconfig/lxdialog/BIG.FAT.WARNING +4 -0
  126. package/bios/seabios/scripts/kconfig/lxdialog/check-lxdialog.sh +87 -0
  127. package/bios/seabios/scripts/kconfig/lxdialog/checklist.c +332 -0
  128. package/bios/seabios/scripts/kconfig/lxdialog/dialog.h +257 -0
  129. package/bios/seabios/scripts/kconfig/lxdialog/inputbox.c +301 -0
  130. package/bios/seabios/scripts/kconfig/lxdialog/menubox.c +437 -0
  131. package/bios/seabios/scripts/kconfig/lxdialog/textbox.c +408 -0
  132. package/bios/seabios/scripts/kconfig/lxdialog/util.c +713 -0
  133. package/bios/seabios/scripts/kconfig/lxdialog/yesno.c +114 -0
  134. package/bios/seabios/scripts/kconfig/mconf.c +1036 -0
  135. package/bios/seabios/scripts/kconfig/menu.c +697 -0
  136. package/bios/seabios/scripts/kconfig/merge_config.sh +150 -0
  137. package/bios/seabios/scripts/kconfig/nconf.c +1556 -0
  138. package/bios/seabios/scripts/kconfig/nconf.gui.c +656 -0
  139. package/bios/seabios/scripts/kconfig/nconf.h +96 -0
  140. package/bios/seabios/scripts/kconfig/qconf.cc +1795 -0
  141. package/bios/seabios/scripts/kconfig/qconf.h +338 -0
  142. package/bios/seabios/scripts/kconfig/streamline_config.pl +647 -0
  143. package/bios/seabios/scripts/kconfig/symbol.c +1373 -0
  144. package/bios/seabios/scripts/kconfig/util.c +157 -0
  145. package/bios/seabios/scripts/kconfig/zconf.gperf +48 -0
  146. package/bios/seabios/scripts/kconfig/zconf.hash.c_shipped +289 -0
  147. package/bios/seabios/scripts/kconfig/zconf.l +363 -0
  148. package/bios/seabios/scripts/kconfig/zconf.lex.c_shipped +2420 -0
  149. package/bios/seabios/scripts/kconfig/zconf.tab.c_shipped +2538 -0
  150. package/bios/seabios/scripts/kconfig/zconf.y +733 -0
  151. package/bios/seabios/scripts/layoutrom.py +705 -0
  152. package/bios/seabios/scripts/python23compat.py +14 -0
  153. package/bios/seabios/scripts/readserial.py +190 -0
  154. package/bios/seabios/scripts/tarball.sh +36 -0
  155. package/bios/seabios/scripts/test-build.sh +90 -0
  156. package/bios/seabios/scripts/transdump.py +53 -0
  157. package/bios/seabios/scripts/vgafixup.py +96 -0
  158. package/bios/seabios/src/Kconfig +579 -0
  159. package/bios/seabios/src/apm.c +215 -0
  160. package/bios/seabios/src/asm-offsets.c +23 -0
  161. package/bios/seabios/src/biosvar.h +130 -0
  162. package/bios/seabios/src/block.c +623 -0
  163. package/bios/seabios/src/block.h +121 -0
  164. package/bios/seabios/src/bmp.c +117 -0
  165. package/bios/seabios/src/boot.c +793 -0
  166. package/bios/seabios/src/bootsplash.c +255 -0
  167. package/bios/seabios/src/bregs.h +80 -0
  168. package/bios/seabios/src/byteorder.h +71 -0
  169. package/bios/seabios/src/cdrom.c +322 -0
  170. package/bios/seabios/src/clock.c +506 -0
  171. package/bios/seabios/src/code16gcc.s +1 -0
  172. package/bios/seabios/src/config.h +108 -0
  173. package/bios/seabios/src/cp437.c +275 -0
  174. package/bios/seabios/src/cp437.h +1 -0
  175. package/bios/seabios/src/disk.c +779 -0
  176. package/bios/seabios/src/e820map.c +152 -0
  177. package/bios/seabios/src/e820map.h +26 -0
  178. package/bios/seabios/src/entryfuncs.S +165 -0
  179. package/bios/seabios/src/farptr.h +208 -0
  180. package/bios/seabios/src/font.c +139 -0
  181. package/bios/seabios/src/fw/acpi-dsdt-cpu-hotplug.dsl +78 -0
  182. package/bios/seabios/src/fw/acpi-dsdt-dbug.dsl +26 -0
  183. package/bios/seabios/src/fw/acpi-dsdt-hpet.dsl +36 -0
  184. package/bios/seabios/src/fw/acpi-dsdt-isa.dsl +102 -0
  185. package/bios/seabios/src/fw/acpi-dsdt-pci-crs.dsl +90 -0
  186. package/bios/seabios/src/fw/acpi-dsdt.dsl +342 -0
  187. package/bios/seabios/src/fw/acpi-dsdt.hex +554 -0
  188. package/bios/seabios/src/fw/acpi.c +685 -0
  189. package/bios/seabios/src/fw/biostables.c +491 -0
  190. package/bios/seabios/src/fw/coreboot.c +569 -0
  191. package/bios/seabios/src/fw/csm.c +347 -0
  192. package/bios/seabios/src/fw/dev-pci.h +52 -0
  193. package/bios/seabios/src/fw/dev-piix.h +29 -0
  194. package/bios/seabios/src/fw/dev-q35.h +52 -0
  195. package/bios/seabios/src/fw/lzmadecode.c +398 -0
  196. package/bios/seabios/src/fw/lzmadecode.h +67 -0
  197. package/bios/seabios/src/fw/mptable.c +197 -0
  198. package/bios/seabios/src/fw/mtrr.c +105 -0
  199. package/bios/seabios/src/fw/multiboot.c +111 -0
  200. package/bios/seabios/src/fw/paravirt.c +624 -0
  201. package/bios/seabios/src/fw/paravirt.h +63 -0
  202. package/bios/seabios/src/fw/pciinit.c +1187 -0
  203. package/bios/seabios/src/fw/pirtable.c +103 -0
  204. package/bios/seabios/src/fw/q35-acpi-dsdt.dsl +450 -0
  205. package/bios/seabios/src/fw/romfile_loader.c +259 -0
  206. package/bios/seabios/src/fw/romfile_loader.h +91 -0
  207. package/bios/seabios/src/fw/shadow.c +208 -0
  208. package/bios/seabios/src/fw/smbios.c +585 -0
  209. package/bios/seabios/src/fw/smm.c +269 -0
  210. package/bios/seabios/src/fw/smp.c +194 -0
  211. package/bios/seabios/src/fw/ssdt-misc.dsl +104 -0
  212. package/bios/seabios/src/fw/ssdt-misc.hex +88 -0
  213. package/bios/seabios/src/fw/ssdt-pcihp.dsl +36 -0
  214. package/bios/seabios/src/fw/ssdt-pcihp.hex +38 -0
  215. package/bios/seabios/src/fw/ssdt-proc.dsl +48 -0
  216. package/bios/seabios/src/fw/ssdt-proc.hex +35 -0
  217. package/bios/seabios/src/fw/xen.c +149 -0
  218. package/bios/seabios/src/fw/xen.h +125 -0
  219. package/bios/seabios/src/gen-defs.h +19 -0
  220. package/bios/seabios/src/hw/ahci.c +697 -0
  221. package/bios/seabios/src/hw/ahci.h +201 -0
  222. package/bios/seabios/src/hw/ata.c +1046 -0
  223. package/bios/seabios/src/hw/ata.h +163 -0
  224. package/bios/seabios/src/hw/blockcmd.c +372 -0
  225. package/bios/seabios/src/hw/blockcmd.h +114 -0
  226. package/bios/seabios/src/hw/dma.c +67 -0
  227. package/bios/seabios/src/hw/esp-scsi.c +241 -0
  228. package/bios/seabios/src/hw/esp-scsi.h +8 -0
  229. package/bios/seabios/src/hw/floppy.c +741 -0
  230. package/bios/seabios/src/hw/lsi-scsi.c +221 -0
  231. package/bios/seabios/src/hw/lsi-scsi.h +8 -0
  232. package/bios/seabios/src/hw/megasas.c +405 -0
  233. package/bios/seabios/src/hw/megasas.h +8 -0
  234. package/bios/seabios/src/hw/mpt-scsi.c +319 -0
  235. package/bios/seabios/src/hw/mpt-scsi.h +8 -0
  236. package/bios/seabios/src/hw/nvme-int.h +199 -0
  237. package/bios/seabios/src/hw/nvme.c +708 -0
  238. package/bios/seabios/src/hw/nvme.h +17 -0
  239. package/bios/seabios/src/hw/pci.c +133 -0
  240. package/bios/seabios/src/hw/pci.h +47 -0
  241. package/bios/seabios/src/hw/pci_ids.h +2632 -0
  242. package/bios/seabios/src/hw/pci_regs.h +556 -0
  243. package/bios/seabios/src/hw/pcidevice.c +192 -0
  244. package/bios/seabios/src/hw/pcidevice.h +76 -0
  245. package/bios/seabios/src/hw/pic.c +115 -0
  246. package/bios/seabios/src/hw/pic.h +60 -0
  247. package/bios/seabios/src/hw/ps2port.c +543 -0
  248. package/bios/seabios/src/hw/ps2port.h +67 -0
  249. package/bios/seabios/src/hw/pvscsi.c +333 -0
  250. package/bios/seabios/src/hw/pvscsi.h +8 -0
  251. package/bios/seabios/src/hw/ramdisk.c +108 -0
  252. package/bios/seabios/src/hw/rtc.c +100 -0
  253. package/bios/seabios/src/hw/rtc.h +75 -0
  254. package/bios/seabios/src/hw/sdcard.c +572 -0
  255. package/bios/seabios/src/hw/serialio.c +113 -0
  256. package/bios/seabios/src/hw/serialio.h +29 -0
  257. package/bios/seabios/src/hw/timer.c +259 -0
  258. package/bios/seabios/src/hw/tpm_drivers.c +636 -0
  259. package/bios/seabios/src/hw/tpm_drivers.h +127 -0
  260. package/bios/seabios/src/hw/usb-ehci.c +650 -0
  261. package/bios/seabios/src/hw/usb-ehci.h +177 -0
  262. package/bios/seabios/src/hw/usb-hid.c +442 -0
  263. package/bios/seabios/src/hw/usb-hid.h +29 -0
  264. package/bios/seabios/src/hw/usb-hub.c +205 -0
  265. package/bios/seabios/src/hw/usb-hub.h +64 -0
  266. package/bios/seabios/src/hw/usb-msc.c +222 -0
  267. package/bios/seabios/src/hw/usb-msc.h +10 -0
  268. package/bios/seabios/src/hw/usb-ohci.c +568 -0
  269. package/bios/seabios/src/hw/usb-ohci.h +144 -0
  270. package/bios/seabios/src/hw/usb-uas.c +289 -0
  271. package/bios/seabios/src/hw/usb-uas.h +9 -0
  272. package/bios/seabios/src/hw/usb-uhci.c +571 -0
  273. package/bios/seabios/src/hw/usb-uhci.h +128 -0
  274. package/bios/seabios/src/hw/usb-xhci.c +1161 -0
  275. package/bios/seabios/src/hw/usb-xhci.h +133 -0
  276. package/bios/seabios/src/hw/usb.c +499 -0
  277. package/bios/seabios/src/hw/usb.h +254 -0
  278. package/bios/seabios/src/hw/virtio-blk.c +211 -0
  279. package/bios/seabios/src/hw/virtio-blk.h +43 -0
  280. package/bios/seabios/src/hw/virtio-pci.c +501 -0
  281. package/bios/seabios/src/hw/virtio-pci.h +151 -0
  282. package/bios/seabios/src/hw/virtio-ring.c +147 -0
  283. package/bios/seabios/src/hw/virtio-ring.h +121 -0
  284. package/bios/seabios/src/hw/virtio-scsi.c +220 -0
  285. package/bios/seabios/src/hw/virtio-scsi.h +47 -0
  286. package/bios/seabios/src/jpeg.c +1055 -0
  287. package/bios/seabios/src/kbd.c +599 -0
  288. package/bios/seabios/src/list.h +91 -0
  289. package/bios/seabios/src/malloc.c +561 -0
  290. package/bios/seabios/src/malloc.h +70 -0
  291. package/bios/seabios/src/memmap.h +21 -0
  292. package/bios/seabios/src/misc.c +195 -0
  293. package/bios/seabios/src/mouse.c +342 -0
  294. package/bios/seabios/src/optionroms.c +475 -0
  295. package/bios/seabios/src/output.c +584 -0
  296. package/bios/seabios/src/output.h +68 -0
  297. package/bios/seabios/src/pcibios.c +241 -0
  298. package/bios/seabios/src/pmm.c +176 -0
  299. package/bios/seabios/src/pnpbios.c +88 -0
  300. package/bios/seabios/src/post.c +337 -0
  301. package/bios/seabios/src/resume.c +157 -0
  302. package/bios/seabios/src/romfile.c +146 -0
  303. package/bios/seabios/src/romfile.h +21 -0
  304. package/bios/seabios/src/romlayout.S +698 -0
  305. package/bios/seabios/src/sercon.c +677 -0
  306. package/bios/seabios/src/serial.c +317 -0
  307. package/bios/seabios/src/sha1.c +147 -0
  308. package/bios/seabios/src/sha1.h +8 -0
  309. package/bios/seabios/src/stacks.c +771 -0
  310. package/bios/seabios/src/stacks.h +68 -0
  311. package/bios/seabios/src/std/LegacyBios.h +985 -0
  312. package/bios/seabios/src/std/acpi.h +323 -0
  313. package/bios/seabios/src/std/bda.h +174 -0
  314. package/bios/seabios/src/std/disk.h +175 -0
  315. package/bios/seabios/src/std/mptable.h +77 -0
  316. package/bios/seabios/src/std/multiboot.h +260 -0
  317. package/bios/seabios/src/std/optionrom.h +59 -0
  318. package/bios/seabios/src/std/pirtable.h +35 -0
  319. package/bios/seabios/src/std/pmm.h +19 -0
  320. package/bios/seabios/src/std/pnpbios.h +24 -0
  321. package/bios/seabios/src/std/smbios.h +167 -0
  322. package/bios/seabios/src/std/tcg.h +554 -0
  323. package/bios/seabios/src/std/vbe.h +156 -0
  324. package/bios/seabios/src/std/vga.h +63 -0
  325. package/bios/seabios/src/string.c +251 -0
  326. package/bios/seabios/src/string.h +31 -0
  327. package/bios/seabios/src/system.c +357 -0
  328. package/bios/seabios/src/tcgbios.c +2014 -0
  329. package/bios/seabios/src/tcgbios.h +19 -0
  330. package/bios/seabios/src/types.h +156 -0
  331. package/bios/seabios/src/util.h +251 -0
  332. package/bios/seabios/src/version.c +5 -0
  333. package/bios/seabios/src/vgahooks.c +355 -0
  334. package/bios/seabios/src/x86.c +23 -0
  335. package/bios/seabios/src/x86.h +277 -0
  336. package/bios/seabios/vgasrc/Kconfig +211 -0
  337. package/bios/seabios/vgasrc/bochsdisplay.c +59 -0
  338. package/bios/seabios/vgasrc/bochsvga.c +447 -0
  339. package/bios/seabios/vgasrc/bochsvga.h +57 -0
  340. package/bios/seabios/vgasrc/cbvga.c +337 -0
  341. package/bios/seabios/vgasrc/clext.c +627 -0
  342. package/bios/seabios/vgasrc/geodevga.c +434 -0
  343. package/bios/seabios/vgasrc/geodevga.h +89 -0
  344. package/bios/seabios/vgasrc/ramfb.c +163 -0
  345. package/bios/seabios/vgasrc/stdvga.c +485 -0
  346. package/bios/seabios/vgasrc/stdvga.h +81 -0
  347. package/bios/seabios/vgasrc/stdvgaio.c +186 -0
  348. package/bios/seabios/vgasrc/stdvgamodes.c +534 -0
  349. package/bios/seabios/vgasrc/swcursor.c +96 -0
  350. package/bios/seabios/vgasrc/vbe.c +432 -0
  351. package/bios/seabios/vgasrc/vgabios.c +1131 -0
  352. package/bios/seabios/vgasrc/vgabios.h +88 -0
  353. package/bios/seabios/vgasrc/vgaentry.S +161 -0
  354. package/bios/seabios/vgasrc/vgafb.c +661 -0
  355. package/bios/seabios/vgasrc/vgafb.h +42 -0
  356. package/bios/seabios/vgasrc/vgafonts.c +785 -0
  357. package/bios/seabios/vgasrc/vgahw.h +152 -0
  358. package/bios/seabios/vgasrc/vgainit.c +202 -0
  359. package/bios/seabios/vgasrc/vgalayout.lds.S +23 -0
  360. package/bios/seabios/vgasrc/vgautil.h +103 -0
  361. package/bios/seabios/vgasrc/vgaversion.c +6 -0
  362. package/build/binaries.js +1 -1
  363. package/build/index-debug.cjs +1 -1
  364. package/build/index-debug.js +1 -1
  365. package/build/index.cjs +1 -1
  366. package/build/index.js +1 -1
  367. package/build/v86-debug.wasm +0 -0
  368. package/build/v86.wasm +0 -0
  369. package/package.json +1 -1
package/build/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});function t(t){this.cpu=t;var e=t.io;t.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),e.register_read(45056,this,void 0,(function(){return dbg_log("ACPI pm1_status read",LOG_ACPI),this.pm1_status})),e.register_write(45056,this,void 0,(function(t){dbg_log("ACPI pm1_status write: "+h(t,4),LOG_ACPI),this.pm1_status&=~t})),e.register_read(45058,this,void 0,(function(){return dbg_log("ACPI pm1_enable read",LOG_ACPI),this.pm1_enable})),e.register_write(45058,this,void 0,(function(t){dbg_log("ACPI pm1_enable write: "+h(t),LOG_ACPI),this.pm1_enable=t})),e.register_read(45060,this,void 0,(function(){return dbg_log("ACPI status read",LOG_ACPI),this.status})),e.register_write(45060,this,void 0,(function(t){dbg_log("ACPI status write: "+h(t),LOG_ACPI),this.status=t})),e.register_read(45064,this,void 0,void 0,(function(){return 16777215&this.get_timer(v86.microtick())})),e.register_read(45024,this,(function(){return dbg_log("Read gpe#0",LOG_ACPI),this.gpe[0]})),e.register_read(45025,this,(function(){return dbg_log("Read gpe#1",LOG_ACPI),this.gpe[1]})),e.register_read(45026,this,(function(){return dbg_log("Read gpe#2",LOG_ACPI),this.gpe[2]})),e.register_read(45027,this,(function(){return dbg_log("Read gpe#3",LOG_ACPI),this.gpe[3]})),e.register_write(45024,this,(function(t){dbg_log("Write gpe#0: "+h(t),LOG_ACPI),this.gpe[0]=t})),e.register_write(45025,this,(function(t){dbg_log("Write gpe#1: "+h(t),LOG_ACPI),this.gpe[1]=t})),e.register_write(45026,this,(function(t){dbg_log("Write gpe#2: "+h(t),LOG_ACPI),this.gpe[2]=t})),e.register_write(45027,this,(function(t){dbg_log("Write gpe#3: "+h(t),LOG_ACPI),this.gpe[3]=t}))}t.prototype.timer=function(t){var e=0!=(8388608&((t=this.get_timer(t))^this.last_timer));return 1&this.pm1_enable&&e?(dbg_log("ACPI raise irq",LOG_ACPI),this.pm1_status|=1,this.cpu.device_raise_irq(9)):this.cpu.device_lower_irq(9),this.last_timer=t,100},t.prototype.get_timer=function(t){return(t=Math.round(3579.545*t))===this.timer_last_value?this.timer_imprecision_offset<3579.545&&this.timer_imprecision_offset++:(dbg_assert(t>this.timer_last_value),this.timer_last_value+this.timer_imprecision_offset<=t?(this.timer_imprecision_offset=0,this.timer_last_value=t):dbg_log("Warning: Overshot pmtimer, waiting; current="+t+" last="+this.timer_last_value+" offset="+this.timer_imprecision_offset,LOG_ACPI)),this.timer_last_value+this.timer_imprecision_offset},t.prototype.get_state=function(){var t=[];return t[0]=this.status,t[1]=this.pm1_status,t[2]=this.pm1_enable,t[3]=this.gpe,t},t.prototype.set_state=function(t){this.status=t[0],this.pm1_status=t[1],this.pm1_enable=t[2],this.gpe=t[3]};var e="Fixed (0);Lowest Prio (1);SMI (2);Reserved (3);NMI (4);INIT (5);Reserved (6);ExtINT (7)".split(";"),i=["physical","logical"];function s(t){this.cpu=t,this.timer_divider=this.apic_id=0,this.timer_divider_shift=1,this.timer_current_count=this.timer_initial_count=0,this.next_tick=v86.microtick(),this.lvt_error=this.lvt_int1=this.lvt_int0=this.lvt_perf_counter=this.lvt_timer=IOAPIC_CONFIG_MASKED,this.icr1=this.icr0=this.tpr=0,this.irr=new Int32Array(8),this.isr=new Int32Array(8),this.tmr=new Int32Array(8),this.spurious_vector=254,this.destination_format=-1,this.read_error=this.error=this.local_destination=0,t.io.mmap_register(4276092928,1048576,(t=>{dbg_log("Unsupported read8 from apic: "+h(t>>>0),LOG_APIC);var e=3&t;return this.read32(-4&t)>>8*e&255}),((t,e)=>{dbg_log("Unsupported write8 from apic: "+h(t)+" <- "+h(e),LOG_APIC),dbg_trace(),dbg_assert(!1)}),(t=>this.read32(t)),((t,e)=>this.write32(t,e)))}s.prototype.read32=function(t){switch(t=t-4276092928|0){case 32:return dbg_log("APIC read id",LOG_APIC),this.apic_id;case 48:return dbg_log("APIC read version",LOG_APIC),327700;case 128:return this.tpr;case 208:return dbg_log("Read local destination",LOG_APIC),this.local_destination;case 224:return dbg_log("Read destination format",LOG_APIC),this.destination_format;case 240:return this.spurious_vector;case 256:case 272:case 288:case 304:case 320:case 336:case 352:case 368:return t=t-256>>4,dbg_log("Read isr "+t+": "+h(this.isr[t]>>>0,8),LOG_APIC),this.isr[t];case 384:case 400:case 416:case 432:case 448:case 464:case 480:case 496:return t=t-384>>4,dbg_log("Read tmr "+t+": "+h(this.tmr[t]>>>0,8),LOG_APIC),this.tmr[t];case 512:case 528:case 544:case 560:case 576:case 592:case 608:case 624:return t=t-512>>4,dbg_log("Read irr "+t+": "+h(this.irr[t]>>>0,8),LOG_APIC),this.irr[t];case 640:return dbg_log("Read error: "+h(this.read_error>>>0,8),LOG_APIC),this.read_error;case 768:return this.icr0;case 784:return dbg_log("APIC read icr1",LOG_APIC),this.icr1;case 800:return dbg_log("read timer lvt",LOG_APIC),this.lvt_timer;case 832:return dbg_log("read lvt perf counter",LOG_APIC),this.lvt_perf_counter;case 848:return dbg_log("read lvt int0",LOG_APIC),this.lvt_int0;case 864:return dbg_log("read lvt int1",LOG_APIC),this.lvt_int1;case 880:return dbg_log("read lvt error",LOG_APIC),this.lvt_error;case 992:return dbg_log("read timer divider",LOG_APIC),this.timer_divider;case 896:return dbg_log("read timer initial count",LOG_APIC),this.timer_initial_count;case 912:return dbg_log("read timer current count: "+h(this.timer_current_count>>>0,8),LOG_APIC),this.timer_current_count;default:return dbg_log("APIC read "+h(t),LOG_APIC),dbg_assert(!1),0}},s.prototype.write32=function(t,s){switch(t=t-4276092928|0){case 48:dbg_log("APIC write version: "+h(s>>>0,8)+", ignored",LOG_APIC);break;case 128:this.tpr=255&s,this.check_vector();break;case 176:-1!==(s=this.highest_isr())?(this.register_clear_bit(this.isr,s),this.register_get_bit(this.tmr,s)&&this.cpu.devices.ioapic.remote_eoi(s),this.check_vector()):dbg_log("Bad eoi: No isr set",LOG_APIC);break;case 208:dbg_log("Set local destination: "+h(s>>>0,8),LOG_APIC),this.local_destination=4278190080&s;break;case 224:dbg_log("Set destination format: "+h(s>>>0,8),LOG_APIC),this.destination_format=16777215|s;break;case 240:dbg_log("Set spurious vector: "+h(s>>>0,8),LOG_APIC),this.spurious_vector=s;break;case 640:dbg_log("Write error: "+h(s>>>0,8),LOG_APIC),this.read_error=this.error,this.error=0;break;case 768:t=255&s;var r=s>>8&7,a=s>>11&1,o=s>>15&1,_=s>>18&3,n=this.icr1>>>24;dbg_log("APIC write icr0: "+h(s,8)+" vector="+h(t,2)+" destination_mode="+i[a]+" delivery_mode="+e[r]+" destination_shorthand="+["no","self","all with self","all without self"][_],LOG_APIC),this.icr0=-4097&s,0===_?this.route(t,r,o,n,a):1===_?this.deliver(t,IOAPIC_DELIVERY_FIXED,o):2===_?this.deliver(t,r,o):3!==_&&dbg_assert(!1);break;case 784:dbg_log("APIC write icr1: "+h(s>>>0,8),LOG_APIC),this.icr1=s;break;case 800:dbg_log("timer lvt: "+h(s>>>0,8),LOG_APIC),this.lvt_timer=s;break;case 832:dbg_log("lvt perf counter: "+h(s>>>0,8),LOG_APIC),this.lvt_perf_counter=s;break;case 848:dbg_log("lvt int0: "+h(s>>>0,8),LOG_APIC),this.lvt_int0=s;break;case 864:dbg_log("lvt int1: "+h(s>>>0,8),LOG_APIC),this.lvt_int1=s;break;case 880:dbg_log("lvt error: "+h(s>>>0,8),LOG_APIC),this.lvt_error=s;break;case 992:dbg_log("timer divider: "+h(s>>>0,8),LOG_APIC),this.timer_divider=s,s=3&s|(8&s)>>1,this.timer_divider_shift=7===s?0:s+1;break;case 896:dbg_log("timer initial: "+h(s>>>0,8),LOG_APIC),this.timer_initial_count=s>>>0,this.timer_current_count=s>>>0,this.next_tick=v86.microtick(),this.timer_active=!0;break;case 912:dbg_log("timer current: "+h(s>>>0,8),LOG_APIC),dbg_assert(!1,"read-only register");break;default:dbg_log("APIC write32 "+h(t)+" <- "+h(s>>>0,8),LOG_APIC),dbg_assert(!1)}},s.prototype.timer=function(t){if(0===this.timer_current_count)return 100;let e=APIC_TIMER_FREQ/(1<<this.timer_divider_shift);return t=(t-this.next_tick)*e>>>0,this.next_tick+=t/e,this.timer_current_count-=t,0>=this.timer_current_count&&(131072===(t=393216&this.lvt_timer)?(this.timer_current_count%=this.timer_initial_count,0>=this.timer_current_count&&(this.timer_current_count+=this.timer_initial_count),dbg_assert(0!==this.timer_current_count),0==(this.lvt_timer&IOAPIC_CONFIG_MASKED)&&this.deliver(255&this.lvt_timer,IOAPIC_DELIVERY_FIXED,!1)):0===t&&(this.timer_current_count=0,dbg_log("APIC timer one shot end",LOG_APIC),0==(this.lvt_timer&IOAPIC_CONFIG_MASKED)&&this.deliver(255&this.lvt_timer,IOAPIC_DELIVERY_FIXED,!1))),Math.max(0,this.timer_current_count/e)},s.prototype.route=function(t,e,i,s,r){this.deliver(t,e,i)},s.prototype.deliver=function(t,e,i){e!==IOAPIC_DELIVERY_INIT&&e!==IOAPIC_DELIVERY_NMI&&((16>t||255===t)&&dbg_assert(!1,"TODO: Invalid vector"),this.register_get_bit(this.irr,t)?dbg_log("Not delivered: irr already set, vector="+h(t,2),LOG_APIC):(this.register_set_bit(this.irr,t),i?this.register_set_bit(this.tmr,t):this.register_clear_bit(this.tmr,t),this.check_vector()))},s.prototype.highest_irr=function(){var t=this.register_get_highest_bit(this.irr);return dbg_assert(255!==t),dbg_assert(16<=t||-1===t),t},s.prototype.highest_isr=function(){var t=this.register_get_highest_bit(this.isr);return dbg_assert(255!==t),dbg_assert(16<=t||-1===t),t},s.prototype.check_vector=function(){var t=this.highest_irr();-1!==t&&(this.highest_isr()>=t||(240&t)<=(240&this.tpr)||this.cpu.handle_irqs())},s.prototype.acknowledge_irq=function(){var t=this.highest_irr();-1===t||this.highest_isr()>=t||(240&t)<=(240&this.tpr)||(this.register_clear_bit(this.irr,t),this.register_set_bit(this.isr,t),this.cpu.pic_call_irq(t),this.check_vector())},s.prototype.get_state=function(){var t=[];return t[0]=this.apic_id,t[1]=this.timer_divider,t[2]=this.timer_divider_shift,t[3]=this.timer_initial_count,t[4]=this.timer_current_count,t[5]=this.next_tick,t[6]=this.lvt_timer,t[7]=this.lvt_perf_counter,t[8]=this.lvt_int0,t[9]=this.lvt_int1,t[10]=this.lvt_error,t[11]=this.tpr,t[12]=this.icr0,t[13]=this.icr1,t[14]=this.irr,t[15]=this.isr,t[16]=this.tmr,t[17]=this.spurious_vector,t[18]=this.destination_format,t[19]=this.local_destination,t[20]=this.error,t[21]=this.read_error,t},s.prototype.set_state=function(t){this.apic_id=t[0],this.timer_divider=t[1],this.timer_divider_shift=t[2],this.timer_initial_count=t[3],this.timer_current_count=t[4],this.next_tick=t[5],this.lvt_timer=t[6],this.lvt_perf_counter=t[7],this.lvt_int0=t[8],this.lvt_int1=t[9],this.lvt_error=t[10],this.tpr=t[11],this.icr0=t[12],this.icr1=t[13],this.irr=t[14],this.isr=t[15],this.tmr=t[16],this.spurious_vector=t[17],this.destination_format=t[18],this.local_destination=t[19],this.error=t[20],this.read_error=t[21]},s.prototype.register_get_bit=function(t,e){return dbg_assert(0<=e&&256>e),t[e>>5]>>(31&e)&1},s.prototype.register_set_bit=function(t,e){dbg_assert(0<=e&&256>e),t[e>>5]|=1<<(31&e)},s.prototype.register_clear_bit=function(t,e){dbg_assert(0<=e&&256>e),t[e>>5]&=~(1<<(31&e))},s.prototype.register_get_highest_bit=function(t){for(var e=7;0<=e;e--){var i=t[e];if(i)return v86util.int_log2(i>>>0)|e<<5}return-1};var r={};function a(){this.listeners={},this.pair=void 0}a.prototype.register=function(t,e,i){var s=this.listeners[t];void 0===s&&(s=this.listeners[t]=[]),s.push({fn:e,this_value:i})},a.prototype.unregister=function(t,e){var i=this.listeners[t];void 0!==i&&(this.listeners[t]=i.filter((function(t){return t.fn!==e})))},a.prototype.send=function(t,e,i){if(this.pair&&void 0!==(t=this.pair.listeners[t]))for(i=0;i<t.length;i++){var s=t[i];s.fn.call(s.this_value,e)}},a.prototype.send_async=function(t,e){dbg_assert(1===arguments.length||2===arguments.length),setTimeout(this.send.bind(this,t,e),0)},r.create=function(){var t=new a,e=new a;return t.pair=e,e.pair=t,[t,e]};var o=[[1,""],[2,"CPU"],[32768,"DISK"],[4,"FPU"],[8,"MEM"],[16,"DMA"],[32,"IO"],[64,"PS2"],[128,"PIC"],[256,"VGA"],[512,"PIT"],[1024,"MOUS"],[2048,"PCI"],[4096,"BIOS"],[8192,"FLOP"],[16384,"SERI"],[65536,"RTC"],[131072,"HPET"],[262144,"ACPI"],[524288,"APIC"],[1048576,"NET"],[2097152,"VIO"],[4194304,"9P"],[8388608,"SB16"]],_=function(){};function n(t,e,i){}var d=d||{};d.exportSymbol=function(){},d.exportProperty=function(){};var c=c||{};function p(t,e){return t=t?t.toString(16):"","0x"+c.pad0(t.toUpperCase(),e||1)}if(c.pads=function(t,e){return(t||0===t?t+"":"").padEnd(e," ")},c.pad0=function(t,e){return(t||0===t?t+"":"").padStart(e,"0")},c.zeros=function(t){return Array(t).fill(0)},c.range=function(t){return Array.from(Array(t).keys())},c.view=function(t,e,i,s){return new Proxy({},{get:function(r,a,o){return"function"==typeof(o=(r=new t(e.buffer,i,s))[a])?o.bind(r):(/^\d+$/.test(a),o)},set:function(r,a,o,_){return/^\d+$/.test(a),new t(e.buffer,i,s)[a]=o,!0}})},"undefined"!=typeof crypto&&crypto.getRandomValues){let t=new Int32Array(1);c.get_rand_int=function(){return crypto.getRandomValues(t),t[0]}}function u(t){this.buffer=t,this.byteLength=t.byteLength,this.onprogress=this.onload=void 0}function l(t){var e,i,s=new Uint8Array(t);this.length=0,this.push=function(e){this.length!==t&&this.length++,s[i]=e,i=i+1&t-1},this.shift=function(){if(this.length){var i=s[e];return e=e+1&t-1,this.length--,i}return-1},this.peek=function(){return this.length?s[e]:-1},this.clear=function(){this.length=i=e=0},this.clear()}function f(t){this.size=t,this.data=new Float32Array(t),this.length=this.end=this.start=0}function m(t){this.data=[],this.index=0,this.size=t}function g(t,e){var i=document.createElement("a");i.download=e,i.href=window.URL.createObjectURL(t),i.dataset.downloadurl=["application/octet-stream",i.download,i.href].join(":"),document.createEvent?((t=document.createEvent("MouseEvent")).initMouseEvent("click",!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null),i.dispatchEvent(t)):i.click(),window.URL.revokeObjectURL(i.href)}function b(t){this.cpu=t,this.channel_page=new Uint8Array(8),this.channel_pagehi=new Uint8Array(8),this.channel_addr=new Uint16Array(8),this.channel_addr_init=new Uint16Array(8),this.channel_count=new Uint16Array(8),this.channel_count_init=new Uint16Array(8),this.channel_mask=new Uint8Array(8),this.channel_mode=new Uint8Array(8),this.unmask_listeners=[],this.lsb_msb_flipflop=0,(t=t.io).register_write(0,this,this.port_addr_write.bind(this,0)),t.register_write(2,this,this.port_addr_write.bind(this,1)),t.register_write(4,this,this.port_addr_write.bind(this,2)),t.register_write(6,this,this.port_addr_write.bind(this,3)),t.register_write(1,this,this.port_count_write.bind(this,0)),t.register_write(3,this,this.port_count_write.bind(this,1)),t.register_write(5,this,this.port_count_write.bind(this,2)),t.register_write(7,this,this.port_count_write.bind(this,3)),t.register_read(0,this,this.port_addr_read.bind(this,0)),t.register_read(2,this,this.port_addr_read.bind(this,1)),t.register_read(4,this,this.port_addr_read.bind(this,2)),t.register_read(6,this,this.port_addr_read.bind(this,3)),t.register_read(1,this,this.port_count_read.bind(this,0)),t.register_read(3,this,this.port_count_read.bind(this,1)),t.register_read(5,this,this.port_count_read.bind(this,2)),t.register_read(7,this,this.port_count_read.bind(this,3)),t.register_write(192,this,this.port_addr_write.bind(this,4)),t.register_write(196,this,this.port_addr_write.bind(this,5)),t.register_write(200,this,this.port_addr_write.bind(this,6)),t.register_write(204,this,this.port_addr_write.bind(this,7)),t.register_write(194,this,this.port_count_write.bind(this,4)),t.register_write(198,this,this.port_count_write.bind(this,5)),t.register_write(202,this,this.port_count_write.bind(this,6)),t.register_write(206,this,this.port_count_write.bind(this,7)),t.register_read(192,this,this.port_addr_read.bind(this,4)),t.register_read(196,this,this.port_addr_read.bind(this,5)),t.register_read(200,this,this.port_addr_read.bind(this,6)),t.register_read(204,this,this.port_addr_read.bind(this,7)),t.register_read(194,this,this.port_count_read.bind(this,4)),t.register_read(198,this,this.port_count_read.bind(this,5)),t.register_read(202,this,this.port_count_read.bind(this,6)),t.register_read(206,this,this.port_count_read.bind(this,7)),t.register_write(135,this,this.port_page_write.bind(this,0)),t.register_write(131,this,this.port_page_write.bind(this,1)),t.register_write(129,this,this.port_page_write.bind(this,2)),t.register_write(130,this,this.port_page_write.bind(this,3)),t.register_write(143,this,this.port_page_write.bind(this,4)),t.register_write(139,this,this.port_page_write.bind(this,5)),t.register_write(137,this,this.port_page_write.bind(this,6)),t.register_write(138,this,this.port_page_write.bind(this,7)),t.register_read(135,this,this.port_page_read.bind(this,0)),t.register_read(131,this,this.port_page_read.bind(this,1)),t.register_read(129,this,this.port_page_read.bind(this,2)),t.register_read(130,this,this.port_page_read.bind(this,3)),t.register_read(143,this,this.port_page_read.bind(this,4)),t.register_read(139,this,this.port_page_read.bind(this,5)),t.register_read(137,this,this.port_page_read.bind(this,6)),t.register_read(138,this,this.port_page_read.bind(this,7)),t.register_write(1159,this,this.port_pagehi_write.bind(this,0)),t.register_write(1155,this,this.port_pagehi_write.bind(this,1)),t.register_write(1153,this,this.port_pagehi_write.bind(this,2)),t.register_write(1154,this,this.port_pagehi_write.bind(this,3)),t.register_write(1163,this,this.port_pagehi_write.bind(this,5)),t.register_write(1161,this,this.port_pagehi_write.bind(this,6)),t.register_write(1162,this,this.port_pagehi_write.bind(this,7)),t.register_read(1159,this,this.port_pagehi_read.bind(this,0)),t.register_read(1155,this,this.port_pagehi_read.bind(this,1)),t.register_read(1153,this,this.port_pagehi_read.bind(this,2)),t.register_read(1154,this,this.port_pagehi_read.bind(this,3)),t.register_read(1163,this,this.port_pagehi_read.bind(this,5)),t.register_read(1161,this,this.port_pagehi_read.bind(this,6)),t.register_read(1162,this,this.port_pagehi_read.bind(this,7)),t.register_write(10,this,this.port_singlemask_write.bind(this,0)),t.register_write(212,this,this.port_singlemask_write.bind(this,4)),t.register_write(15,this,this.port_multimask_write.bind(this,0)),t.register_write(222,this,this.port_multimask_write.bind(this,4)),t.register_read(15,this,this.port_multimask_read.bind(this,0)),t.register_read(222,this,this.port_multimask_read.bind(this,4)),t.register_write(11,this,this.port_mode_write.bind(this,0)),t.register_write(214,this,this.port_mode_write.bind(this,4)),t.register_write(12,this,this.portC_write),t.register_write(216,this,this.portC_write)}u.prototype.load=function(){this.onload&&this.onload({buffer:this.buffer})},u.prototype.get=function(t,e,i){this.byteLength,i(new Uint8Array(this.buffer,t,e))},u.prototype.set=function(t,e,i){e.byteLength,this.byteLength,new Uint8Array(this.buffer,t,e.byteLength).set(e),i()},u.prototype.get_buffer=function(t){t(this.buffer)},u.prototype.get_state=function(){let t=[];return t[0]=this.byteLength,t[1]=new Uint8Array(this.buffer),t},u.prototype.set_state=function(t){this.byteLength=t[0],this.buffer=t[1].slice().buffer},function(){if("function"==typeof Math.clz32)c.int_log2_byte=function(t){return 31-Math.clz32(t)},c.int_log2=function(t){return 31-Math.clz32(t)};else{for(var t=new Int8Array(256),e=0,i=-2;256>e;e++)e&e-1||i++,t[e]=i;c.int_log2_byte=function(e){return t[e]},c.int_log2=function(e){var i=(e>>>=0)>>>16;if(i){var s=i>>>8;return s?24+t[s]:16+t[i]}return(s=e>>>8)?8+t[s]:t[e]}}}(),f.prototype.push=function(t){this.length===this.size?this.start=this.start+1&this.size-1:this.length++,this.data[this.end]=t,this.end=this.end+1&this.size-1},f.prototype.shift=function(){if(this.length){var t=this.data[this.start];return this.start=this.start+1&this.size-1,this.length--,t}},f.prototype.shift_block=function(t){var e=new Float32Array(t);t>this.length&&(t=this.length);var i=this.start+t,s=this.data.subarray(this.start,i);return e.set(s),i>=this.size&&(i-=this.size,e.set(this.data.subarray(0,i),s.length)),this.start=i,this.length-=t,e},f.prototype.peek=function(){if(this.length)return this.data[this.start]},f.prototype.clear=function(){this.length=this.end=this.start=0},m.prototype.add=function(t){this.data[this.index]=t,this.index=(this.index+1)%this.size},m.prototype.toArray=function(){return[].slice.call(this.data,this.index).concat([].slice.call(this.data,0,this.index))},m.prototype.clear=function(){this.data=[],this.index=0},m.prototype.set=function(t){this.data=t,this.index=0},c.Bitmap=function(t){"number"==typeof t?this.view=new Uint8Array(t+7>>3):t instanceof ArrayBuffer?this.view=new Uint8Array(t):console.assert(!1)},c.Bitmap.prototype.set=function(t,e){let i=t>>3;t=1<<(7&t),this.view[i]=e?this.view[i]|t:this.view[i]&~t},c.Bitmap.prototype.get=function(t){return this.view[t>>3]>>(7&t)&1},c.Bitmap.prototype.get_buffer=function(){return this.view.buffer},b.prototype.get_state=function(){return[this.channel_page,this.channel_pagehi,this.channel_addr,this.channel_addr_init,this.channel_count,this.channel_count_init,this.channel_mask,this.channel_mode,this.lsb_msb_flipflop]},b.prototype.set_state=function(t){this.channel_page=t[0],this.channel_pagehi=t[1],this.channel_addr=t[2],this.channel_addr_init=t[3],this.channel_count=t[4],this.channel_count_init=t[5],this.channel_mask=t[6],this.channel_mode=t[7],this.lsb_msb_flipflop=t[8]},b.prototype.port_count_write=function(t,e){_("count write ["+t+"] = "+p(e),16),this.channel_count[t]=this.flipflop_get(this.channel_count[t],e,!1),this.channel_count_init[t]=this.flipflop_get(this.channel_count_init[t],e,!0)},b.prototype.port_count_read=function(t){return _("count read ["+t+"] -> "+p(this.channel_count[t]),16),this.flipflop_read(this.channel_count[t])},b.prototype.port_addr_write=function(t,e){_("addr write ["+t+"] = "+p(e),16),this.channel_addr[t]=this.flipflop_get(this.channel_addr[t],e,!1),this.channel_addr_init[t]=this.flipflop_get(this.channel_addr_init[t],e,!0)},b.prototype.port_addr_read=function(t){return _("addr read ["+t+"] -> "+p(this.channel_addr[t]),16),this.flipflop_read(this.channel_addr[t])},b.prototype.port_pagehi_write=function(t,e){_("pagehi write ["+t+"] = "+p(e),16),this.channel_pagehi[t]=e},b.prototype.port_pagehi_read=function(t){return _("pagehi read ["+t+"]",16),this.channel_pagehi[t]},b.prototype.port_page_write=function(t,e){_("page write ["+t+"] = "+p(e),16),this.channel_page[t]=e},b.prototype.port_page_read=function(t){return _("page read ["+t+"]",16),this.channel_page[t]},b.prototype.port_singlemask_write=function(t,e){_("singlechannel mask write ["+(t=(3&e)+t)+"] = "+(e=4&e?1:0),16),this.update_mask(t,e)},b.prototype.port_multimask_write=function(t,e){_("multichannel mask write: "+p(e),16);for(var i=0;4>i;i++)this.update_mask(t+i,e&1<<i)},b.prototype.port_multimask_read=function(t){var e=0|this.channel_mask[t+0];return e|=this.channel_mask[t+1]<<1,e|=this.channel_mask[t+2]<<2,e|=this.channel_mask[t+3]<<3,_("multichannel mask read: "+p(e),16),e},b.prototype.port_mode_write=function(t,e){_("mode write ["+(t=(3&e)+t)+"] = "+p(e),16),this.channel_mode[t]=e},b.prototype.portC_write=function(t){_("flipflop reset",16),this.lsb_msb_flipflop=0},b.prototype.on_unmask=function(t,e){this.unmask_listeners.push({fn:t,this_value:e})},b.prototype.update_mask=function(t,e){if(this.channel_mask[t]!==e&&(this.channel_mask[t]=e,!e))for(_("firing on_unmask("+t+")",16),e=0;e<this.unmask_listeners.length;e++)this.unmask_listeners[e].fn.call(this.unmask_listeners[e].this_value,t)},b.prototype.do_read=function(t,e,i,s,r){var a=this.count_get_8bit(s),o=this.address_get_8bit(s);if(_("DMA write channel "+s,16),_("to "+p(o)+" len "+p(a),16),i<a&&_("DMA should read more than provided: "+p(i)+" "+p(a),16),e+a>t.byteLength)_("DMA read outside of buffer",16),r(!0);else{var n=this.cpu;this.channel_addr[s]+=a,t.get(e,a,(function(t){n.write_blob(t,o),r(!1)}))}},b.prototype.do_write=function(t,e,i,s,r){var a=this.channel_count[s]+1&65535,o=5<=s?2:1,n=a*o,h=this.address_get_8bit(s),d=!1,c=!1,u=16&this.channel_mode[s];_("DMA write channel "+s,16),_("to "+p(h)+" len "+p(n),16),i<n?(_("DMA should read more than provided",16),a=Math.floor(i/o),n=a*o,d=!0):i>n&&(_("DMA attempted to read more than provided",16),c=!0),e+n>t.byteLength?(_("DMA write outside of buffer",16),r(!0)):(this.channel_addr[s]+=a,this.channel_count[s]-=a,!d&&u&&(_("DMA autoinit",16),this.channel_addr[s]=this.channel_addr_init[s],this.channel_count[s]=this.channel_count_init[s]),t.set(e,this.cpu.mem8.subarray(h,h+n),(()=>{c&&u?(_("DMA continuing from start",16),this.do_write(t,e+n,i-n,s,r)):r(!1)})))},b.prototype.address_get_8bit=function(t){var e=this.channel_addr[t];return 5<=t&&(e<<=1),(e=65535&e|this.channel_page[t]<<16)|this.channel_pagehi[t]<<24},b.prototype.count_get_8bit=function(t){var e=this.channel_count[t]+1;return 5<=t&&(e*=2),e},b.prototype.flipflop_get=function(t,e,i){return i||(this.lsb_msb_flipflop^=1),this.lsb_msb_flipflop?-256&t|e:-65281&t|e<<8},b.prototype.flipflop_read=function(t){return(this.lsb_msb_flipflop^=1)?255&t:t>>8&255};function y(t){this.cpu=t,this.cmos_index=0,this.cmos_data=new Uint8Array(128),this.last_update=this.rtc_time=Date.now(),this.next_interrupt_alarm=this.next_interrupt=0,this.periodic_interrupt=!1,this.periodic_interrupt_time=.9765625,this.cmos_a=38,this.cmos_b=2,this.nmi_disabled=this.cmos_c=0,t.io.register_write(112,this,(function(t){this.cmos_index=127&t,this.nmi_disabled=t>>7})),t.io.register_write(113,this,this.cmos_port_write),t.io.register_read(113,this,this.cmos_port_read)}function v(t,e,i){if(this.io=t.io,this.cpu=t,this.dma=t.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.floppy_size=this.response_length=this.response_index=0,this.fda_image=e,this.fdb_image=i,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,e){if(this.floppy_size=e.byteLength,!(i={160:{type:1,tracks:40,sectors:8,heads:1},180:{type:1,tracks:40,sectors:9,heads:1},200:{type:1,tracks:40,sectors:10,heads:1},320:{type:1,tracks:40,sectors:8,heads:2},360:{type:1,tracks:40,sectors:9,heads:2},400:{type:1,tracks:40,sectors:10,heads:2},720:{type:3,tracks:80,sectors:9,heads:2},1200:{type:2,tracks:80,sectors:15,heads:2},1440:{type:4,tracks:80,sectors:18,heads:2},1722:{type:5,tracks:82,sectors:21,heads:2},2880:{type:5,tracks:80,sectors:36,heads:2},0:{type:1,tracks:1,sectors:1,heads:1}}[this.floppy_size>>10])||0!=(1023&this.floppy_size)&&512!==this.floppy_size)throw"Unknown floppy size: "+p(e.byteLength);t.devices.rtc.cmos_write(16,i.type<<4),t=i.sectors,e=i.heads,i=i.tracks,this.sectors_per_track=t,this.number_of_heads=e,this.number_of_cylinders=i}else t.devices.rtc.cmos_write(16,64),this.floppy_size=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,this,this.port3F2_read),this.io.register_read(1012,this,this.port3F4_read),this.io.register_read(1013,this,this.port3F5_read),this.io.register_read(1015,this,this.port3F7_read),this.io.register_write(1010,this,this.port3F2_write),this.io.register_write(1013,this,this.port3F5_write)}y.prototype.get_state=function(){var t=[];return t[0]=this.cmos_index,t[1]=this.cmos_data,t[2]=this.rtc_time,t[3]=this.last_update,t[4]=this.next_interrupt,t[5]=this.next_interrupt_alarm,t[6]=this.periodic_interrupt,t[7]=this.periodic_interrupt_time,t[8]=this.cmos_a,t[9]=this.cmos_b,t[10]=this.cmos_c,t[11]=this.nmi_disabled,t},y.prototype.set_state=function(t){this.cmos_index=t[0],this.cmos_data=t[1],this.rtc_time=t[2],this.last_update=t[3],this.next_interrupt=t[4],this.next_interrupt_alarm=t[5],this.periodic_interrupt=t[6],this.periodic_interrupt_time=t[7],this.cmos_a=t[8],this.cmos_b=t[9],this.cmos_c=t[10],this.nmi_disabled=t[11]},y.prototype.timer=function(t,e){return t=Date.now(),this.rtc_time+=t-this.last_update,this.last_update=t,this.periodic_interrupt&&this.next_interrupt<t?(this.cpu.device_raise_irq(8),this.cmos_c|=192,this.next_interrupt+=this.periodic_interrupt_time*Math.ceil((t-this.next_interrupt)/this.periodic_interrupt_time)):this.next_interrupt_alarm&&this.next_interrupt_alarm<t&&(this.cpu.device_raise_irq(8),this.cmos_c|=160,this.next_interrupt_alarm=0),e=100,this.periodic_interrupt&&this.next_interrupt&&(e=Math.min(e,Math.max(0,this.next_interrupt-t))),this.next_interrupt_alarm&&(e=Math.min(e,Math.max(0,this.next_interrupt_alarm-t))),e},y.prototype.bcd_pack=function(t){for(var e,i=0,s=0;t;)s|=(e=t%10)<<4*i,i++,t=(t-e)/10;return s},y.prototype.bcd_unpack=function(t){return(15&t)+10*(t>>4&15)},y.prototype.encode_time=function(t){return 4&this.cmos_b?t:this.bcd_pack(t)},y.prototype.decode_time=function(t){return 4&this.cmos_b?t:this.bcd_unpack(t)},y.prototype.cmos_port_read=function(){var t=this.cmos_index;switch(t){case 0:return this.encode_time(new Date(this.rtc_time).getUTCSeconds());case 2:return this.encode_time(new Date(this.rtc_time).getUTCMinutes());case 4:return this.encode_time(new Date(this.rtc_time).getUTCHours());case 7:return this.encode_time(new Date(this.rtc_time).getUTCDate());case 8:return this.encode_time(new Date(this.rtc_time).getUTCMonth()+1);case 9:return this.encode_time(new Date(this.rtc_time).getUTCFullYear()%100);case 10:return this.cmos_a;case 11:return this.cmos_b;case 12:return this.cpu.device_lower_irq(8),_("cmos reg C read",65536),t=this.cmos_c,this.cmos_c&=-241,t;case 13:return 0;case 50:return this.encode_time(new Date(this.rtc_time).getUTCFullYear()/100|0);default:return _("cmos read from index "+p(t),65536),this.cmos_data[this.cmos_index]}},y.prototype.cmos_port_write=function(t){switch(this.cmos_index){case 10:this.cmos_a=127&t,this.periodic_interrupt_time=1e3/(32768>>(15&this.cmos_a)-1),_("Periodic interrupt, a="+p(this.cmos_a,2)+" t="+this.periodic_interrupt_time,65536);break;case 11:if(this.cmos_b=t,64&this.cmos_b&&(this.next_interrupt=Date.now()),32&this.cmos_b){t=new Date;let e=this.decode_time(this.cmos_data[1]),i=this.decode_time(this.cmos_data[3]),s=this.decode_time(this.cmos_data[5]),r=new Date(Date.UTC(t.getUTCFullYear(),t.getUTCMonth(),t.getUTCDate(),s,i,e));_("RTC alarm scheduled for "+r+" hh:mm:ss="+s+":"+i+":"+e+" ms_from_now="+(r-t),65536),this.next_interrupt_alarm=+r}16&this.cmos_b&&_("Unimplemented: updated interrupt",65536),_("cmos b="+p(this.cmos_b,2),65536);break;case 1:case 3:case 5:this.cmos_write(this.cmos_index,t);break;default:_("cmos write index "+p(this.cmos_index)+": "+p(t),65536)}this.periodic_interrupt=64==(64&this.cmos_b)&&0<(15&this.cmos_a)},y.prototype.cmos_read=function(t){return this.cmos_data[t]},y.prototype.cmos_write=function(t,e){_("cmos "+p(t)+" <- "+p(e),65536),this.cmos_data[t]=e},v.prototype.get_state=function(){var t=[];return t[0]=this.bytes_expecting,t[1]=this.receiving_command,t[2]=this.receiving_index,t[4]=this.response_data,t[5]=this.response_index,t[6]=this.response_length,t[7]=this.floppy_size,t[8]=this.status_reg0,t[9]=this.status_reg1,t[10]=this.status_reg2,t[11]=this.drive,t[12]=this.last_cylinder,t[13]=this.last_head,t[14]=this.last_sector,t[15]=this.dor,t[16]=this.sectors_per_track,t[17]=this.number_of_heads,t[18]=this.number_of_cylinders,t},v.prototype.set_state=function(t){this.bytes_expecting=t[0],this.receiving_command=t[1],this.receiving_index=t[2],this.next_command=t[3],this.response_data=t[4],this.response_index=t[5],this.response_length=t[6],this.floppy_size=t[7],this.status_reg0=t[8],this.status_reg1=t[9],this.status_reg2=t[10],this.drive=t[11],this.last_cylinder=t[12],this.last_head=t[13],this.last_sector=t[14],this.dor=t[15],this.sectors_per_track=t[16],this.number_of_heads=t[17],this.number_of_cylinders=t[18]},v.prototype.port3F0_read=function(){return _("3F0 read",8192),0},v.prototype.port3F4_read=function(){_("3F4 read",8192);var t=128;return this.response_index<this.response_length&&(t|=80),0==(8&this.dor)&&(t|=32),t},v.prototype.port3F7_read=function(){return _("3F7 read",8192),0},v.prototype.port3F5_read=function(){return this.response_index<this.response_length?(_("3F5 read: "+this.response_data[this.response_index],8192),this.cpu.device_lower_irq(6),this.response_data[this.response_index++]):(_("3F5 read, empty",8192),255)},v.prototype.port3F5_write=function(t){if(this.fda_image)if(_("3F5 write "+p(t),8192),0<this.bytes_expecting)this.receiving_command[this.receiving_index++]=t,this.bytes_expecting--,0===this.bytes_expecting&&this.next_command.call(this,this.receiving_command);else{switch(t){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(t){this.do_sector(!0,t)},this.bytes_expecting=8;break;case 230:this.next_command=function(t){this.do_sector(!1,t)},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:_("dump registers",8192),this.response_data[0]=128,this.response_index=0,this.response_length=1,this.bytes_expecting=0;break;default:p(t)}this.receiving_index=0}},v.prototype.port3F2_read=function(){return _("read 3F2: DOR",8192),this.dor},v.prototype.port3F2_write=function(t){4==(4&t)&&0==(4&this.dor)&&this.cpu.device_raise_irq(6),_("start motors: "+p(t>>4),8192),_("enable dma: "+!!(8&t),8192),_("reset fdc: "+!!(4&t),8192),_("drive select: "+(3&t),8192),_("DOR = "+p(t),8192),this.dor=t},v.prototype.check_drive_status=function(t){_("check drive status",8192),this.response_index=0,this.response_length=1,this.response_data[0]=32},v.prototype.seek=function(t){_("seek",8192),t[0],this.last_cylinder=t[1],this.last_head=t[0]>>2&1,this.raise_irq()},v.prototype.calibrate=function(t){_("floppy calibrate",8192),this.raise_irq()},v.prototype.check_interrupt_status=function(){_("floppy check interrupt status",8192),this.response_index=0,this.response_length=2,this.response_data[0]=32,this.response_data[1]=this.last_cylinder},v.prototype.do_sector=function(t,e){var i=e[2],s=e[1],r=e[3],a=128<<e[4],o=e[5]-e[3]+1,n=((i+this.number_of_heads*s)*this.sectors_per_track+r-1)*a;_("Floppy "+(t?"Write":"Read"),8192),_("from "+p(n)+" length "+p(o*a),8192),_(s+" / "+i+" / "+r,8192),e[4]||_("FDC: sector count is zero, use data length instead",8192),this.fda_image&&(t?this.dma.do_write(this.fda_image,n,o*a,2,this.done.bind(this,e,s,i,r)):this.dma.do_read(this.fda_image,n,o*a,2,this.done.bind(this,e,s,i,r)))},v.prototype.done=function(t,e,i,s,r){r||(++s>this.sectors_per_track&&(s=1,++i>=this.number_of_heads&&(i=0,e++)),this.last_cylinder=e,this.last_head=i,this.last_sector=s,this.response_index=0,this.response_length=7,this.response_data[0]=i<<2|32,this.response_data[1]=0,this.response_data[2]=0,this.response_data[3]=e,this.response_data[4]=i,this.response_data[5]=s,this.response_data[6]=t[4],this.raise_irq())},v.prototype.fix_drive_data=function(t){_("floppy fix drive data "+t,8192)},v.prototype.read_sector_id=function(t){_("floppy read sector id "+t,8192),this.response_index=0,this.response_length=7,this.response_data[0]=0,this.response_data[1]=0,this.response_data[2]=0,this.response_data[3]=0,this.response_data[4]=0,this.response_data[5]=0,this.response_data[6]=0,this.raise_irq()},v.prototype.raise_irq=function(){8&this.dor&&this.cpu.device_raise_irq(6)};function w(t,e,i,s,r,a){this.master=new x(this,t,e,s,r,0,a),this.slave=new x(this,t,i,!1,r,1,a),this.current_interface=this.master,this.cpu=t,0===r?(this.ata_port=496,this.irq=14,this.pci_id=240):1===r&&(this.ata_port=368,this.irq=15,this.pci_id=248),this.ata_port_high=516|this.ata_port,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,255&this.master_port|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"+r,this.device_control=2,t.io.register_read(7|this.ata_port,this,(function(){return _("lower irq",32768),this.cpu.device_lower_irq(this.irq),this.read_status()})),t.io.register_read(2|this.ata_port_high,this,this.read_status),t.io.register_write(2|this.ata_port_high,this,this.write_control),t.io.register_read(0|this.ata_port,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)})),t.io.register_read(1|this.ata_port,this,(function(){return _("Read error: "+p(255&this.current_interface.error)+" slave="+(this.current_interface===this.slave),32768),255&this.current_interface.error})),t.io.register_read(2|this.ata_port,this,(function(){return _("Read bytecount: "+p(255&this.current_interface.bytecount),32768),255&this.current_interface.bytecount})),t.io.register_read(3|this.ata_port,this,(function(){return _("Read sector: "+p(255&this.current_interface.sector),32768),255&this.current_interface.sector})),t.io.register_read(4|this.ata_port,this,(function(){return _("Read 1F4: "+p(255&this.current_interface.cylinder_low),32768),255&this.current_interface.cylinder_low})),t.io.register_read(5|this.ata_port,this,(function(){return _("Read 1F5: "+p(255&this.current_interface.cylinder_high),32768),255&this.current_interface.cylinder_high})),t.io.register_read(6|this.ata_port,this,(function(){return _("Read 1F6",32768),255&this.current_interface.drive_head})),t.io.register_write(0|this.ata_port,this,(function(t){this.current_interface.write_data_port8(t)}),(function(t){this.current_interface.write_data_port16(t)}),(function(t){this.current_interface.write_data_port32(t)})),t.io.register_write(1|this.ata_port,this,(function(t){_("1F1/lba_count: "+p(t),32768),this.master.lba_count=65535&(this.master.lba_count<<8|t),this.slave.lba_count=65535&(this.slave.lba_count<<8|t)})),t.io.register_write(2|this.ata_port,this,(function(t){_("1F2/bytecount: "+p(t),32768),this.master.bytecount=65535&(this.master.bytecount<<8|t),this.slave.bytecount=65535&(this.slave.bytecount<<8|t)})),t.io.register_write(3|this.ata_port,this,(function(t){_("1F3/sector: "+p(t),32768),this.master.sector=65535&(this.master.sector<<8|t),this.slave.sector=65535&(this.slave.sector<<8|t)})),t.io.register_write(4|this.ata_port,this,(function(t){_("1F4/sector low: "+p(t),32768),this.master.cylinder_low=65535&(this.master.cylinder_low<<8|t),this.slave.cylinder_low=65535&(this.slave.cylinder_low<<8|t)})),t.io.register_write(5|this.ata_port,this,(function(t){_("1F5/sector high: "+p(t),32768),this.master.cylinder_high=65535&(this.master.cylinder_high<<8|t),this.slave.cylinder_high=65535&(this.slave.cylinder_high<<8|t)})),t.io.register_write(6|this.ata_port,this,(function(t){var e=16&t;_("1F6/drive: "+p(t,2),32768),e?(_("Slave",32768),this.current_interface=this.slave):this.current_interface=this.master,this.master.drive_head=t,this.slave.drive_head=t,this.master.is_lba=this.slave.is_lba=t>>6&1,this.master.head=this.slave.head=15&t})),this.dma_command=this.dma_status=this.prdt_addr=0,t.io.register_write(7|this.ata_port,this,(function(t){_("lower irq",32768),this.cpu.device_lower_irq(this.irq),this.current_interface.ata_command(t)})),t.io.register_read(4|this.master_port,this,void 0,void 0,this.dma_read_addr),t.io.register_write(4|this.master_port,this,void 0,void 0,this.dma_set_addr),t.io.register_read(this.master_port,this,this.dma_read_command8,void 0,this.dma_read_command),t.io.register_write(this.master_port,this,this.dma_write_command8,void 0,this.dma_write_command),t.io.register_read(2|this.master_port,this,this.dma_read_status),t.io.register_write(2|this.master_port,this,this.dma_write_status),t.io.register_read(8|this.master_port,this,(function(){return _("DMA read 0x8",32768),0})),t.io.register_read(10|this.master_port,this,(function(){return _("DMA read 0xA",32768),0})),t.devices.pci.register_device(this)}function x(t,e,i,s,r,a,o){this.device=t,this.bus=o,this.nr=r,this.cpu=e,this.buffer=i,this.sector_size=s?2048:512,this.is_atapi=s,this.cylinder_count=this.sectors_per_track=this.head_count=this.sector_count=0,this.buffer&&(this.sector_count=this.buffer.byteLength/this.sector_size,this.sector_count!==(0|this.sector_count)&&(_("Warning: Disk size not aligned with sector size",32768),this.sector_count=Math.ceil(this.sector_count)),s?(this.head_count=1,this.sectors_per_track=0):(this.head_count=16,this.sectors_per_track=63),this.cylinder_count=this.sector_count/this.head_count/this.sectors_per_track,this.cylinder_count!==(0|this.cylinder_count)&&(_("Warning: Rounding up cylinder count. Choose different head number",32768),this.cylinder_count=Math.floor(this.cylinder_count)),(t=e.devices.rtc).cmos_write(57,t.cmos_read(57)|1<<4*this.nr),t.cmos_write(18,15&t.cmos_read(18)|240),e=27,t.cmos_write(e+0,255&this.cylinder_count),t.cmos_write(e+1,this.cylinder_count>>8&255),t.cmos_write(e+2,255&this.head_count),t.cmos_write(e+3,255),t.cmos_write(e+4,255),t.cmos_write(e+5,200),t.cmos_write(e+6,255&this.cylinder_count),t.cmos_write(e+7,this.cylinder_count>>8&255),t.cmos_write(e+8,255&this.sectors_per_track)),this.stats={sectors_read:0,sectors_written:0,bytes_read:0,bytes_written:0,loading:!1},this.buffer=i,this.drive_head=this.head=this.cylinder_high=this.cylinder_low=this.lba_count=this.sector=this.bytecount=this.is_lba=0,this.status=80,this.sectors_per_drq=128,this.data_pointer=this.error=0,this.data=new Uint8Array(65536),this.data16=new Uint16Array(this.data.buffer),this.data32=new Int32Array(this.data.buffer),this.data_end=this.data_length=0,this.current_atapi_command=this.current_command=-1,this.last_io_id=this.write_dest=0,this.in_progress_io_ids=new Set,this.cancelled_io_ids=new Set,Object.seal(this)}function A(t){this.ports=[],this.cpu=t;for(var e=0;65536>e;e++)this.ports[e]=this.create_empty_entry();var i=t.memory_size[0];for(e=0;e<<17<i;e++)t.memory_map_read8[e]=t.memory_map_write8[e]=void 0,t.memory_map_read32[e]=t.memory_map_write32[e]=void 0;this.mmap_register(i,4294967296-i,(function(t){return _("Read from unmapped memory space, addr="+p(t>>>0,8),32),255}),(function(t,e){_("Write to unmapped memory space, addr="+p(t>>>0,8)+" value="+p(e,2),32)}),(function(t){return _("Read from unmapped memory space, addr="+p(t>>>0,8),32),-1}),(function(t,e){_("Write to unmapped memory space, addr="+p(t>>>0,8)+" value="+p(e>>>0,8),32)}))}w.prototype.read_status=function(){if(this.current_interface.buffer){var t=this.current_interface.status;return _("ATA read status: "+p(t,2),32768),t}return 0},w.prototype.write_control=function(t){_("set device control: "+p(t,2)+" interrupts "+(2&t?"disabled":"enabled"),32768),4&t&&(_("Reset via control port",32768),this.cpu.device_lower_irq(this.irq),this.master.device_reset(),this.slave.device_reset()),this.device_control=t},w.prototype.dma_read_addr=function(){return _("dma get address: "+p(this.prdt_addr,8),32768),this.prdt_addr},w.prototype.dma_set_addr=function(t){_("dma set address: "+p(t,8),32768),this.prdt_addr=t},w.prototype.dma_read_status=function(){return _("DMA read status: "+p(this.dma_status),32768),this.dma_status},w.prototype.dma_write_status=function(t){_("DMA set status: "+p(t),32768),this.dma_status&=~(6&t)},w.prototype.dma_read_command=function(){return this.dma_read_command8()|this.dma_read_status()<<16},w.prototype.dma_read_command8=function(){return _("DMA read command: "+p(this.dma_command),32768),this.dma_command},w.prototype.dma_write_command=function(t){_("DMA write command: "+p(t),32768),this.dma_write_command8(255&t),this.dma_write_status(t>>16&255)},w.prototype.dma_write_command8=function(t){_("DMA write command8: "+p(t),32768);let e=this.dma_command;if(this.dma_command=9&t,(1&e)!=(1&t))if(0==(1&t))this.dma_status&=-2;else switch(this.dma_status|=1,this.current_interface.current_command){case 37:case 200:this.current_interface.do_ata_read_sectors_dma();break;case 202:case 53:this.current_interface.do_ata_write_sectors_dma();break;case 160:this.current_interface.do_atapi_dma();break;default:_("Spurious dma command write, current command: "+p(this.current_interface.current_command),32768)}},w.prototype.push_irq=function(){0==(2&this.device_control)&&(_("push irq",32768),this.dma_status|=4,this.cpu.device_raise_irq(this.irq))},w.prototype.get_state=function(){var t=[];return t[0]=this.master,t[1]=this.slave,t[2]=this.ata_port,t[3]=this.irq,t[4]=this.pci_id,t[5]=this.ata_port_high,t[6]=this.master_port,t[7]=this.name,t[8]=this.device_control,t[9]=this.prdt_addr,t[10]=this.dma_status,t[11]=this.current_interface===this.master,t[12]=this.dma_command,t},w.prototype.set_state=function(t){this.master.set_state(t[0]),this.slave.set_state(t[1]),this.ata_port=t[2],this.irq=t[3],this.pci_id=t[4],this.ata_port_high=t[5],this.master_port=t[6],this.name=t[7],this.device_control=t[8],this.prdt_addr=t[9],this.dma_status=t[10],this.current_interface=t[11]?this.master:this.slave,this.dma_command=t[12]},x.prototype.device_reset=function(){this.is_atapi?(this.status=0,this.sector=this.error=this.bytecount=1,this.cylinder_low=20,this.cylinder_high=235):(this.status=81,this.sector=this.error=this.bytecount=1,this.cylinder_high=this.cylinder_low=0),this.cancel_io_operations()},x.prototype.push_irq=function(){this.device.push_irq()},x.prototype.ata_command=function(t){if(_("ATA Command: "+p(t)+" slave="+(this.drive_head>>4&1),32768),this.buffer)switch(this.current_command=t,this.error=0,t){case 8:_("ATA device reset",32768),this.data_length=this.data_end=this.data_pointer=0,this.device_reset(),this.push_irq();break;case 16:this.status=80,this.cylinder_low=0,this.push_irq();break;case 248:this.status=80,t=this.sector_count-1,this.sector=255&t,this.cylinder_low=t>>8&255,this.cylinder_high=t>>16&255,this.drive_head=240&this.drive_head|t>>24&15,this.push_irq();break;case 39:this.status=80,t=this.sector_count-1,this.sector=255&t,this.cylinder_low=t>>8&255,this.cylinder_high=t>>16&255,this.sector|=t>>24<<8&65280,this.push_irq();break;case 32:case 36:case 41:case 196:this.ata_read_sectors(t);break;case 48:case 52:case 57:case 197:this.ata_write_sectors(t);break;case 144:this.push_irq(),this.error=257,this.status=80;break;case 145:case 222:this.status=80,this.push_irq();break;case 160:this.is_atapi&&(this.status=88,this.data_allocate(12),this.data_end=12,this.bytecount=1,this.push_irq());break;case 161:_("ATA identify packet device",32768),this.is_atapi?(this.create_identify_packet(),this.status=88,this.cylinder_low=20,this.cylinder_high=235):this.status=65,this.push_irq();break;case 198:_("Logical sectors per DRQ Block: "+p(255&this.bytecount),32768),this.sectors_per_drq=255&this.bytecount,this.status=80,this.push_irq();break;case 37:case 200:this.ata_read_sectors_dma(t);break;case 53:case 202:this.ata_write_sectors_dma(t);break;case 64:_("read verify sectors",32768),this.status=80,this.push_irq();break;case 218:_("Unimplemented: get media status",32768),this.status=65,this.error=4,this.push_irq();break;case 224:_("ATA standby immediate",32768),this.status=80,this.push_irq();break;case 225:_("ATA idle immediate",32768),this.status=80,this.push_irq();break;case 231:_("ATA flush cache",32768),this.status=80,this.push_irq();break;case 236:if(_("ATA identify device",32768),this.is_atapi){this.status=65,this.error=4,this.push_irq();break}this.create_identify_packet(),this.status=88,this.push_irq();break;case 234:_("flush cache ext",32768),this.status=80,this.push_irq();break;case 239:_("set features: "+p(255&this.bytecount),32768),this.status=80,this.push_irq();break;case 245:_("security freeze lock",32768),this.status=80,this.push_irq();break;case 249:_("Unimplemented: set max address",32768),this.status=65,this.error=4;break;default:p(t),this.status=65,this.error=4}else _("abort: No buffer",32768),this.error=4,this.status=65,this.push_irq()},x.prototype.atapi_handle=function(){switch(_("ATAPI Command: "+p(this.data[0])+" slave="+(this.drive_head>>4&1),32768),this.data_pointer=0,this.current_atapi_command=this.data[0],this.current_atapi_command){case 0:_("test unit ready",32768),this.data_allocate(0),this.data_end=this.data_length,this.status=80;break;case 3:this.data_allocate(this.data[4]),this.data_end=this.data_length,this.status=88,this.data[0]=240,this.data[2]=5,this.data[7]=8;break;case 18:var t=this.data[4];this.status=88,_("inquiry: "+p(this.data[1],2)+" length="+t,32768),this.data.set([5,128,1,49,31,0,0,0,83,79,78,89,32,32,32,32,67,68,45,82,79,77,32,67,68,85,45,49,48,48,48,32,49,46,49,97]),this.data_end=this.data_length=Math.min(36,t);break;case 26:this.data_allocate(this.data[4]),this.data_end=this.data_length,this.status=88;break;case 30:case 81:this.data_allocate(0),this.data_end=this.data_length,this.status=80;break;case 37:t=this.sector_count-1,this.data_set(new Uint8Array([t>>24&255,t>>16&255,t>>8&255,255&t,0,0,this.sector_size>>8&255,255&this.sector_size])),this.data_end=this.data_length,this.status=88;break;case 40:1&this.lba_count?this.atapi_read_dma(this.data):this.atapi_read(this.data);break;case 66:t=this.data[8],this.data_allocate(Math.min(8,t)),this.data_end=this.data_length,_("read q subcode: length="+t,32768),this.status=88;break;case 67:t=this.data[8]|this.data[7]<<8;var e=this.data[9]>>6;this.data_allocate(t),this.data_end=this.data_length,_("read toc: "+p(e,2)+" length="+t+" "+(2&this.data[1])+" "+p(this.data[6]),32768),0===e?(t=this.sector_count,this.data.set(new Uint8Array([0,18,1,1,0,20,1,0,0,0,0,0,0,22,170,0,t>>24,t>>16&255,t>>8&255,255&t]))):1===e&&this.data.set(new Uint8Array([0,10,1,1,0,0,0,0,0,0,0,0])),this.status=88;break;case 70:t=this.data[8]|this.data[7]<<8,t=Math.min(t,32),this.data_allocate(t),this.data_end=this.data_length,this.data[0]=t-4>>24&255,this.data[1]=t-4>>16&255,this.data[2]=t-4>>8&255,this.data[3]=t-4&255,this.data[6]=8,this.data[10]=3,this.status=88;break;case 82:_("Unimplemented ATAPI command: "+p(this.data[0]),32768),this.status=81,this.data_length=0,this.error=80;break;case 90:t=this.data[8]|this.data[7]<<8,e=this.data[2],_("mode sense: "+p(e)+" length="+t,32768),42===e&&this.data_allocate(Math.min(30,t)),this.data_end=this.data_length,this.status=88;break;case 189:this.data_allocate(this.data[9]|this.data[8]<<8),this.data_end=this.data_length,this.data[5]=1,this.status=88;break;case 74:default:this.status=81,this.data_length=0,this.error=80,_("Unimplemented ATAPI command: "+p(this.data[0]),32768);break;case 190:_("Unimplemented ATAPI command: "+p(this.data[0]),32768),this.data_allocate(0),this.data_end=this.data_length,this.status=80}this.bytecount=-8&this.bytecount|2,0==(128&this.status)&&this.push_irq(),0==(128&this.status)&&0===this.data_length&&(this.bytecount|=1,this.status&=-9)},x.prototype.do_write=function(){this.status=80,this.data_length,this.data.length;var t=this.data.subarray(0,this.data_length);this.data_length,this.ata_advance(this.current_command,this.data_length/512),this.push_irq(),this.buffer.set(this.write_dest,t,(function(){})),this.report_write(this.data_length)},x.prototype.atapi_read=function(t){var e=t[2]<<24|t[3]<<16|t[4]<<8|t[5],i=t[7]<<8|t[8];t=t[1];var s=i*this.sector_size,r=e*this.sector_size;_("CD read lba="+p(e)+" lbacount="+p(i)+" bytecount="+p(s)+" flags="+p(t),32768),this.data_length=0;var a=this.cylinder_high<<8&65280|255&this.cylinder_low;_(p(this.cylinder_high,2)+" "+p(this.cylinder_low,2),32768),this.cylinder_low=this.cylinder_high=0,65535===a&&a--,a>s&&(a=s),r>=this.buffer.byteLength?(p(r+s),p(this.buffer.byteLength),this.status=255,this.push_irq()):0===s?(this.status=80,this.data_pointer=0):(s=Math.min(s,this.buffer.byteLength-r),this.status=208,this.report_read_start(),this.read_buffer(r,s,(t=>{_("cd read: data arrived",32768),this.data_set(t),this.status=88,this.bytecount=-8&this.bytecount|2,this.push_irq(),this.data_end=a&=-4,this.data_end>this.data_length&&(this.data_end=this.data_length),this.cylinder_low=255&this.data_end,this.cylinder_high=this.data_end>>8&255,this.report_read_end(s)})))},x.prototype.atapi_read_dma=function(t){var e=t[2]<<24|t[3]<<16|t[4]<<8|t[5],i=t[7]<<8|t[8];t=t[1];var s=i*this.sector_size,r=e*this.sector_size;_("CD read DMA lba="+p(e)+" lbacount="+p(i)+" bytecount="+p(s)+" flags="+p(t),32768),r>=this.buffer.byteLength?(p(r+s),p(this.buffer.byteLength),this.status=255,this.push_irq()):(this.status=208,this.report_read_start(),this.read_buffer(r,s,(t=>{_("atapi_read_dma: Data arrived"),this.report_read_end(s),this.status=88,this.bytecount=-8&this.bytecount|2,this.data_set(t),this.do_atapi_dma()})))},x.prototype.do_atapi_dma=function(){if(0==(1&this.device.dma_status))_("do_atapi_dma: Status not set",32768);else if(0==(8&this.status))_("do_atapi_dma: DRQ not set",32768);else{_("atapi dma transfer len="+this.data_length,32768);var t=this.device.prdt_addr,e=0,i=this.data;do{var s=this.cpu.read32s(t),r=this.cpu.read16(t+4),a=128&this.cpu.read8(t+7);if(r||(r=65536),_("dma read dest="+p(s)+" count="+p(r)+" datalen="+p(this.data_length),32768),this.cpu.write_blob(i.subarray(e,Math.min(e+r,this.data_length)),s),t+=8,(e+=r)>=this.data_length&&!a){_("leave early end="+ +a+" offset="+p(e)+" data_length="+p(this.data_length)+" cmd="+p(this.current_command),32768);break}}while(!a);_("end offset="+e,32768),this.status=80,this.device.dma_status&=-2,this.bytecount=-8&this.bytecount|3,this.push_irq()}},x.prototype.read_data=function(t){if(this.data_pointer<this.data_end){this.data_pointer,this.data_end,this.data_pointer,p(this.data_pointer);var e=1===t?this.data[this.data_pointer]:2===t?this.data16[this.data_pointer>>>1]:this.data32[this.data_pointer>>>2];return this.data_pointer+=t,0==(this.data_pointer&(0==(4095&this.data_end)?4095:255))&&_("Read 1F0: "+p(this.data[this.data_pointer],2)+" cur="+p(this.data_pointer)+" cnt="+p(this.data_length),32768),this.data_pointer>=this.data_end&&this.read_end(),e}return _("Read 1F0: empty",32768),this.data_pointer+=t,0},x.prototype.read_end=function(){if(_("read_end cmd="+p(this.current_command)+" data_pointer="+p(this.data_pointer)+" end="+p(this.data_end)+" length="+p(this.data_length),32768),160===this.current_command)if(this.data_end===this.data_length)this.status=80,this.bytecount=-8&this.bytecount|3,this.push_irq();else{this.status=88,this.bytecount=-8&this.bytecount|2,this.push_irq();var t=this.cylinder_high<<8&65280|255&this.cylinder_low;this.data_end+t>this.data_length?(this.cylinder_low=this.data_length-this.data_end&255,this.cylinder_high=this.data_length-this.data_end>>8&255,this.data_end=this.data_length):this.data_end+=t,_("data_end="+p(this.data_end),32768)}else this.error=0,this.data_pointer>=this.data_length?this.status=80:(196===this.current_command||41===this.current_command?t=Math.min(this.sectors_per_drq,(this.data_length-this.data_end)/512):(32===this.current_command||this.current_command,t=1),this.ata_advance(this.current_command,t),this.data_end+=512*t,this.status=88),this.push_irq()},x.prototype.write_data_port=function(t,e){this.data_pointer,this.data_pointer>=this.data_end?_("Redundant write to data port: "+p(t)+" count="+p(this.data_end)+" cur="+p(this.data_pointer),32768):((0==(this.data_pointer+e&(0==(4095&this.data_end)?4095:255))||20>this.data_end)&&_("Data port: "+p(t>>>0)+" count="+p(this.data_end)+" cur="+p(this.data_pointer),32768),1===e?this.data[this.data_pointer++]=t:2===e?(this.data16[this.data_pointer>>>1]=t,this.data_pointer+=2):(this.data32[this.data_pointer>>>2]=t,this.data_pointer+=4),this.data_pointer,this.data_end,this.data_pointer===this.data_end&&this.write_end())},x.prototype.write_data_port8=function(t){this.write_data_port(t,1)},x.prototype.write_data_port16=function(t){this.write_data_port(t,2)},x.prototype.write_data_port32=function(t){this.write_data_port(t,4)},x.prototype.write_end=function(){160===this.current_command?this.atapi_handle():(_("write_end data_pointer="+p(this.data_pointer)+" data_length="+p(this.data_length),32768),this.data_pointer>=this.data_length?this.do_write():(48===this.current_command||52===this.current_command||this.current_command,p(this.current_command),this.status=88,this.data_end+=512,this.push_irq()))},x.prototype.ata_advance=function(t,e){_("Advance sectors="+e+" old_bytecount="+this.bytecount,32768),this.bytecount-=e,36===t||41===t||52===t||57===t||37===t||53===t?(t=e+this.get_lba48(),this.sector=255&t|t>>16&65280,this.cylinder_low=t>>8&255,this.cylinder_high=t>>16&255):this.is_lba?(t=e+this.get_lba28(),this.sector=255&t,this.cylinder_low=t>>8&255,this.cylinder_high=t>>16&255,this.head=-16&this.head|15&t):(e=(t=e+this.get_chs())/(this.head_count*this.sectors_per_track)|0,this.cylinder_low=255&e,this.cylinder_high=e>>8&255,this.head=(t/this.sectors_per_track|0)%this.head_count&15,this.sector=t%this.sectors_per_track+1&255,this.get_chs())},x.prototype.ata_read_sectors=function(t){var e=36===t||41===t,i=this.get_count(e);e=this.get_lba(e);var s=32===t||36===t,r=i*this.sector_size,a=e*this.sector_size;_("ATA read cmd="+p(t)+" mode="+(this.is_lba?"lba":"chs")+" lba="+p(e)+" lbacount="+p(i)+" bytecount="+p(r),32768),a+r>this.buffer.byteLength?(this.status=255,this.push_irq()):(this.status=192,this.report_read_start(),this.read_buffer(a,r,(e=>{_("ata_read: Data arrived",32768),this.data_set(e),this.status=88,this.data_end=s?512:Math.min(r,512*this.sectors_per_drq),this.ata_advance(t,s?1:Math.min(i,this.sectors_per_track)),this.push_irq(),this.report_read_end(r)})))},x.prototype.ata_read_sectors_dma=function(t){var e=37===t;t=this.get_count(e),e=this.get_lba(e);var i=t*this.sector_size,s=e*this.sector_size;_("ATA DMA read lba="+p(e)+" lbacount="+p(t)+" bytecount="+p(i),32768),s+i>this.buffer.byteLength?(this.status=255,this.push_irq()):(this.status=88,this.device.dma_status|=1)},x.prototype.do_ata_read_sectors_dma=function(){var t=37===this.current_command,e=this.get_count(t);t=this.get_lba(t);var i=e*this.sector_size,s=t*this.sector_size;this.buffer.byteLength,this.report_read_start(),this.device.prdt_addr,this.read_buffer(s,i,(t=>{_("do_ata_read_sectors_dma: Data arrived",32768);var s=this.device.prdt_addr,r=0;do{var a=this.cpu.read32s(s),o=this.cpu.read16(s+4),n=128&this.cpu.read8(s+7);o||(o=65536,_("dma: prd count was 0",32768)),_("dma read transfer dest="+p(a)+" prd_count="+p(o),32768),this.cpu.write_blob(t.subarray(r,r+o),a),r+=o,s+=8}while(!n);this.ata_advance(this.current_command,e),this.status=80,this.device.dma_status&=-2,this.current_command=-1,this.push_irq(),this.report_read_end(i)}))},x.prototype.ata_write_sectors=function(t){var e=52===t||57===t,i=this.get_count(e);e=this.get_lba(e),t=48===t||52===t;var s=i*this.sector_size,r=e*this.sector_size;_("ATA write lba="+p(e)+" mode="+(this.is_lba?"lba":"chs")+" lbacount="+p(i)+" bytecount="+p(s),32768),r+s>this.buffer.byteLength?(this.status=255,this.push_irq()):(this.status=88,this.data_allocate_noclear(s),this.data_end=t?512:Math.min(s,512*this.sectors_per_drq),this.write_dest=r)},x.prototype.ata_write_sectors_dma=function(t){var e=53===t;t=this.get_count(e),e=this.get_lba(e);var i=t*this.sector_size,s=e*this.sector_size;_("ATA DMA write lba="+p(e)+" lbacount="+p(t)+" bytecount="+p(i),32768),s+i>this.buffer.byteLength?(this.status=255,this.push_irq()):(this.status=88,this.device.dma_status|=1)},x.prototype.do_ata_write_sectors_dma=function(){var t=53===this.current_command,e=this.get_count(t),i=this.get_lba(t);t=e*this.sector_size,i*=this.sector_size;var s=this.device.prdt_addr,r=0;_("prdt addr: "+p(s,8),32768);let a=new Uint8Array(t);do{var o=this.cpu.read32s(s),n=this.cpu.read16(s+4),h=128&this.cpu.read8(s+7);n||(n=65536,_("dma: prd count was 0",32768)),_("dma write transfer dest="+p(o)+" prd_count="+p(n),32768),(o=this.cpu.mem8.subarray(o,o+n)).length,a.set(o,r),r+=n,s+=8}while(!h);a.length,this.buffer.set(i,a,(()=>{_("dma write completed",32768),this.ata_advance(this.current_command,e),this.status=80,this.push_irq(),this.device.dma_status&=-2,this.current_command=-1})),this.report_write(t)},x.prototype.get_chs=function(){var t=255&this.cylinder_low|this.cylinder_high<<8&65280,e=this.head,i=255&this.sector;return _("get_chs: c="+t+" h="+e+" s="+i,32768),(t*this.head_count+e)*this.sectors_per_track+i-1},x.prototype.get_lba28=function(){return 255&this.sector|this.cylinder_low<<8&65280|this.cylinder_high<<16&16711680|(15&this.head)<<24},x.prototype.get_lba48=function(){return(255&this.sector|this.cylinder_low<<8&65280|this.cylinder_high<<16&16711680|this.sector>>8<<24&4278190080)>>>0},x.prototype.get_lba=function(t){return t?this.get_lba48():this.is_lba?this.get_lba28():this.get_chs()},x.prototype.get_count=function(t){return t?0===(t=this.bytecount)&&(t=65536):0===(t=255&this.bytecount)&&(t=256),t},x.prototype.create_identify_packet=function(){if(16&this.drive_head)this.data_allocate(0);else{for(var t=0;512>t;t++)this.data[t]=0;t=Math.min(16383,this.cylinder_count),this.data_set([64,this.is_atapi?133:0,t,t>>8,0,0,this.head_count,this.head_count>>8,this.sectors_per_track/512,this.sectors_per_track/512>>8,0,2,this.sectors_per_track,this.sectors_per_track>>8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,2,4,0,0,0,0,0,0,0,0,0,56,118,32,54,68,72,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,128,0,1,0,0,2,0,0,0,2,0,2,7,0,t,t>>8,this.head_count,this.head_count>>8,this.sectors_per_track,0,255&this.sector_count,this.sector_count>>8&255,this.sector_count>>16&255,this.sector_count>>24&255,0,0,255&this.sector_count,this.sector_count>>8&255,this.sector_count>>16&255,this.sector_count>>24&255,0,0,160===this.current_command?0:7,160===this.current_command?0:4,0,0,30,0,30,0,30,0,30,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,126,0,0,0,0,0,0,116,0,64,0,64,0,116,0,64,0,0,0,0,0,0,0,0,0,0,1,96,0,0,0,0,0,0,0,0,0,0,0,0,255&this.sector_count,this.sector_count>>8&255,this.sector_count>>16&255,this.sector_count>>24&255]),this.data_end=this.data_length=512}},x.prototype.data_allocate=function(t){this.data_allocate_noclear(t);for(var e=0;e<t+3>>2;e++)this.data32[e]=0},x.prototype.data_allocate_noclear=function(t){this.data.length<t&&(this.data=new Uint8Array(t+3&-4),this.data16=new Uint16Array(this.data.buffer),this.data32=new Int32Array(this.data.buffer)),this.data_length=t,this.data_pointer=0},x.prototype.data_set=function(t){this.data_allocate_noclear(t.length),this.data.set(t)},x.prototype.report_read_start=function(){this.stats.loading=!0,this.bus.send("ide-read-start")},x.prototype.report_read_end=function(t){this.stats.loading=!1;var e=t/this.sector_size|0;this.stats.sectors_read+=e,this.stats.bytes_read+=t,this.bus.send("ide-read-end",[this.nr,t,e])},x.prototype.report_write=function(t){var e=t/this.sector_size|0;this.stats.sectors_written+=e,this.stats.bytes_written+=t,this.bus.send("ide-write-end",[this.nr,t,e])},x.prototype.read_buffer=function(t,e,i){let s=this.last_io_id++;this.in_progress_io_ids.add(s),this.buffer.get(t,e,(t=>{this.cancelled_io_ids.delete(s)?this.in_progress_io_ids.has(s):(this.in_progress_io_ids.delete(s),i(t))}))},x.prototype.cancel_io_operations=function(){for(let t of this.in_progress_io_ids)this.cancelled_io_ids.add(t);this.in_progress_io_ids.clear()},x.prototype.get_state=function(){var t=[];return t[0]=this.bytecount,t[1]=this.cylinder_count,t[2]=this.cylinder_high,t[3]=this.cylinder_low,t[4]=this.data_pointer,t[5]=0,t[6]=0,t[7]=0,t[8]=0,t[9]=this.drive_head,t[10]=this.error,t[11]=this.head,t[12]=this.head_count,t[13]=this.is_atapi,t[14]=this.is_lba,t[15]=this.lba_count,t[16]=this.data,t[17]=this.data_length,t[18]=this.sector,t[19]=this.sector_count,t[20]=this.sector_size,t[21]=this.sectors_per_drq,t[22]=this.sectors_per_track,t[23]=this.status,t[24]=this.write_dest,t[25]=this.current_command,t[26]=this.data_end,t[27]=this.current_atapi_command,t[28]=this.buffer,t},x.prototype.set_state=function(t){this.bytecount=t[0],this.cylinder_count=t[1],this.cylinder_high=t[2],this.cylinder_low=t[3],this.data_pointer=t[4],this.drive_head=t[9],this.error=t[10],this.head=t[11],this.head_count=t[12],this.is_atapi=t[13],this.is_lba=t[14],this.lba_count=t[15],this.data=t[16],this.data_length=t[17],this.sector=t[18],this.sector_count=t[19],this.sector_size=t[20],this.sectors_per_drq=t[21],this.sectors_per_track=t[22],this.status=t[23],this.write_dest=t[24],this.current_command=t[25],this.data_end=t[26],this.current_atapi_command=t[27],this.data16=new Uint16Array(this.data.buffer),this.data32=new Int32Array(this.data.buffer),this.buffer&&this.buffer.set_state(t[28])},A.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}},A.prototype.empty_port_read8=function(){return 255},A.prototype.empty_port_read16=function(){return 65535},A.prototype.empty_port_read32=function(){return-1},A.prototype.empty_port_write=function(t){},A.prototype.register_read=function(t,e,i,s,r){i&&(this.ports[t].read8=i),s&&(this.ports[t].read16=s),r&&(this.ports[t].read32=r),this.ports[t].device=e},A.prototype.register_write=function(t,e,i,s,r){i&&(this.ports[t].write8=i),s&&(this.ports[t].write16=s),r&&(this.ports[t].write32=r),this.ports[t].device=e},A.prototype.register_read_consecutive=function(t,e,i,s,r,a){function o(){return i.call(this)|s.call(this)<<8}r&&a?(this.register_read(t,e,i,o,(function(){return i.call(this)|s.call(this)<<8|r.call(this)<<16|a.call(this)<<24})),this.register_read(t+1,e,s),this.register_read(t+2,e,r,(function(){return r.call(this)|a.call(this)<<8})),this.register_read(t+3,e,a)):(this.register_read(t,e,i,o),this.register_read(t+1,e,s))},A.prototype.register_write_consecutive=function(t,e,i,s,r,a){function o(t){i.call(this,255&t),s.call(this,t>>8&255)}r&&a?(this.register_write(t,e,i,o,(function(t){i.call(this,255&t),s.call(this,t>>8&255),r.call(this,t>>16&255),a.call(this,t>>>24)})),this.register_write(t+1,e,s),this.register_write(t+2,e,r,(function(t){r.call(this,255&t),a.call(this,t>>8&255)})),this.register_write(t+3,e,a)):(this.register_write(t,e,i,o),this.register_write(t+1,e,s))},A.prototype.mmap_read32_shim=function(t){var e=this.cpu.memory_map_read8[t>>>17];return e(t)|e(t+1)<<8|e(t+2)<<16|e(t+3)<<24},A.prototype.mmap_write32_shim=function(t,e){var i=this.cpu.memory_map_write8[t>>>17];i(t,255&e),i(t+1,e>>8&255),i(t+2,e>>16&255),i(t+3,e>>>24)},A.prototype.mmap_register=function(t,e,i,s,r,a){for(_("mmap_register addr="+p(t>>>0,8)+" size="+p(e,8),32),r||(r=this.mmap_read32_shim.bind(this)),a||(a=this.mmap_write32_shim.bind(this)),t>>>=17;0<e;t++)this.cpu.memory_map_read8[t]=i,this.cpu.memory_map_write8[t]=s,this.cpu.memory_map_read32[t]=r,this.cpu.memory_map_write32[t]=a,e-=131072},A.prototype.port_write8=function(t,e){var i=this.ports[t];return i.write8===this.empty_port_write&&_("write8 port #"+p(t,4)+" <- "+p(e,2)+this.get_port_description(t),32),i.write8.call(i.device,e)},A.prototype.port_write16=function(t,e){var i=this.ports[t];return i.write16===this.empty_port_write&&_("write16 port #"+p(t,4)+" <- "+p(e,4)+this.get_port_description(t),32),i.write16.call(i.device,e)},A.prototype.port_write32=function(t,e){var i=this.ports[t];return i.write32===this.empty_port_write&&_("write32 port #"+p(t,4)+" <- "+p(e>>>0,8)+this.get_port_description(t),32),i.write32.call(i.device,e)},A.prototype.port_read8=function(t){var e=this.ports[t];return e.read8===this.empty_port_read8&&_("read8 port #"+p(t,4)+this.get_port_description(t),32),e=e.read8.call(e.device),p(t),e},A.prototype.port_read16=function(t){var e=this.ports[t];return e.read16===this.empty_port_read16&&_("read16 port #"+p(t,4)+this.get_port_description(t),32),e=e.read16.call(e.device),p(t),e},A.prototype.port_read32=function(t){var e=this.ports[t];return e.read32===this.empty_port_read32&&_("read32 port #"+p(t,4)+this.get_port_description(t),32),e.read32.call(e.device)};var k={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"};A.prototype.get_port_description=function(t){return k[t]?" ("+k[t]+")":""};function I(t){this.cpu=t,this.ioredtbl_config=new Int32Array(24),this.ioredtbl_destination=new Int32Array(24);for(var e=0;e<this.ioredtbl_config.length;e++)this.ioredtbl_config[e]=65536;this.ioregsel=0,this.ioapic_id=0,this.irq_value=this.irr=0,dbg_assert(32<=MMAP_BLOCK_SIZE),t.io.mmap_register(4273995776,MMAP_BLOCK_SIZE,(t=>(t=t-4273995776|0)>=16&&t<20?(t-=16,dbg_log("ioapic read8 byte "+t+" "+h(this.ioregsel),LOG_APIC),this.read(this.ioregsel)>>8*t&255):(dbg_log("Unexpected IOAPIC register read: "+h(t>>>0),LOG_APIC),dbg_assert(!1),0)),((t,e)=>{dbg_assert(!1,"unsupported write8 from ioapic: "+h(t>>>0))}),(t=>0===(t=t-4273995776|0)?this.ioregsel:16===t?this.read(this.ioregsel):(dbg_log("Unexpected IOAPIC register read: "+h(t>>>0),LOG_APIC),dbg_assert(!1),0)),((t,e)=>{0===(t=t-4273995776|0)?this.ioregsel=e:16===t?this.write(this.ioregsel,e):(dbg_log("Unexpected IOAPIC register write: "+h(t>>>0)+" <- "+h(e>>>0,8),LOG_APIC),dbg_assert(!1))}))}function O(t,e){this.stopped=this.running=!1,this.tick_counter=0,this.worker=null,this.cpu=new it(t,e,(()=>{this.idle&&this.next_tick(0)})),this.bus=t,t.register("cpu-init",this.init,this),t.register("cpu-run",this.run,this),t.register("cpu-stop",this.stop,this),t.register("cpu-restart",this.restart,this),this.register_yield()}if(I.prototype.remote_eoi=function(t){for(var e=0;e<24;e++){var i=this.ioredtbl_config[e];(255&i)===t&&16384&i&&(dbg_log("Clear remote IRR for irq="+h(e),LOG_APIC),this.ioredtbl_config[e]&=-16385,this.check_irq(e))}},I.prototype.check_irq=function(t){var e=1<<t;if(0!=(this.irr&e)){var i=this.ioredtbl_config[t];if(0==(65536&i)){var s=i>>8&7,r=i>>11&1,a=255&i,o=this.ioredtbl_destination[t]>>>24,_=32768==(32768&i);if(0==(32768&i))this.irr&=~e;else if(this.ioredtbl_config[t]|=16384,16384&i)return void dbg_log("No route: level interrupt and remote IRR still set",LOG_APIC);0===s||1===s?this.cpu.devices.apic.route(a,s,_,o,r):dbg_assert(!1,"TODO"),this.ioredtbl_config[t]&=-4097}}},I.prototype.set_irq=function(t){if(t>=24)dbg_assert(!1,"Bad irq: "+t,LOG_APIC);else{var e=1<<t;0==(this.irq_value&e)&&(APIC_LOG_VERBOSE&&dbg_log("apic set irq "+t,LOG_APIC),this.irq_value|=e,65536!=(98304&this.ioredtbl_config[t])&&(this.irr|=e,this.check_irq(t)))}},I.prototype.clear_irq=function(t){if(t>=24)dbg_assert(!1,"Bad irq: "+t,LOG_APIC);else{var e=1<<t;(this.irq_value&e)===e&&(this.irq_value&=~e,32768&this.ioredtbl_config[t]&&(this.irr&=~e))}},I.prototype.read=function(t){if(0===t)return dbg_log("IOAPIC Read id",LOG_APIC),this.ioapic_id<<24;if(1===t)return dbg_log("IOAPIC Read version",LOG_APIC),1507345;if(2===t)return dbg_log("IOAPIC Read arbitration id",LOG_APIC),this.ioapic_id<<24;if(16<=t&&t<64){var e=t-16>>1;return 1&t?(t=this.ioredtbl_destination[e],dbg_log("IOAPIC Read destination irq="+h(e)+" -> "+h(t,8),LOG_APIC)):(t=this.ioredtbl_config[e],dbg_log("IOAPIC Read config irq="+h(e)+" -> "+h(t,8),LOG_APIC)),t}return dbg_log("IOAPIC register read outside of range "+h(t),LOG_APIC),dbg_assert(!1),0},I.prototype.write=function(t,e){if(0===t)this.ioapic_id=e>>>24&15;else if(1===t||2===t)dbg_log("Invalid write: "+t,LOG_APIC);else if(16<=t&&t<64){var i=t-16>>1;if(1&t)this.ioredtbl_destination[i]=4278190080&e,dbg_log("Write destination "+h(e>>>0,8)+" irq="+h(i)+" dest="+h(e>>>24,2),LOG_APIC);else{this.ioredtbl_config[i]=110591&e|-110592&this.ioredtbl_config[i],t=255&e;var s=e>>8&7,r=e>>11&1,a=e>>15&1,o=e>>16&1;dbg_log("Write config "+h(e>>>0,8)+" irq="+h(i)+" vector="+h(t,2)+" deliverymode="+DELIVERY_MODES[s]+" destmode="+DESTINATION_MODES[r]+" is_level="+a+" disabled="+o,LOG_APIC),this.check_irq(i)}}else dbg_log("IOAPIC register write outside of range "+h(t)+": "+h(e>>>0,8),LOG_APIC),dbg_assert(!1)},I.prototype.get_state=function(){var t=[];return t[0]=this.ioredtbl_config,t[1]=this.ioredtbl_destination,t[2]=this.ioregsel,t[3]=this.ioapic_id,t[4]=this.irr,t[5]=this.irq_value,t},I.prototype.set_state=function(t){this.ioredtbl_config=t[0],this.ioredtbl_destination=t[1],this.ioregsel=t[2],this.ioapic_id=t[3],this.irr=t[4],this.irq_value=t[5]},O.prototype.run=function(){this.stopped=!1,this.running||(this.running=!0,this.bus.send("emulator-started")),this.next_tick(0)},O.prototype.do_tick=function(){if(this.stopped||!this.running)this.stopped=this.running=!1,this.bus.send("emulator-stopped");else{this.idle=!1;var t=this.cpu.main_run();this.next_tick(t)}},O.prototype.next_tick=function(t){let e=++this.tick_counter;this.idle=!0,this.yield(t,e)},O.prototype.yield_callback=function(t){t===this.tick_counter&&this.do_tick()},O.prototype.stop=function(){this.running&&(this.stopped=!0)},O.prototype.destroy=function(){this.unregister_yield()},O.prototype.restart=function(){this.cpu.reset_cpu(),this.cpu.load_bios()},O.prototype.init=function(t){this.cpu.init(t,this.bus),this.bus.send("emulator-ready")},"undefined"!=typeof process&&global.setImmediate)O.prototype.yield=function(t,e){1>t?global.setImmediate((t=>this.yield_callback(t)),e):setTimeout((t=>this.yield_callback(t)),t,e)},O.prototype.register_yield=function(){},O.prototype.unregister_yield=function(){};else if("undefined"!=typeof Worker){function t(){globalThis.onmessage=function(t){let e=t.data.t;1>e?postMessage(t.data.tick):setTimeout((()=>postMessage(t.data.tick)),e)}}O.prototype.register_yield=function(){let e=URL.createObjectURL(new Blob(["("+t.toString()+")()"],{type:"text/javascript"}));this.worker=new Worker(e),this.worker.onmessage=t=>this.yield_callback(t.data),URL.revokeObjectURL(e)},O.prototype.yield=function(t,e){this.worker.postMessage({t:t,tick:e})},O.prototype.unregister_yield=function(){this.worker.terminate(),this.worker=null}}else O.prototype.yield=function(t){setTimeout((()=>{this.do_tick()}),t)},O.prototype.register_yield=function(){},O.prototype.unregister_yield=function(){};O.prototype.save_state=function(){return this.cpu.save_state()},O.prototype.restore_state=function(t){return this.cpu.restore_state(t)},O.microtick="object"==typeof performance&&performance.now?performance.now.bind(performance):"object"==typeof process&&process.hrtime?function(){var t=process.hrtime();return 1e3*t[0]+t[1]/1e6}:Date.now;function R(t,e,i){for(this.cpu=t,this.pci=t.devices.pci,this.preserve_mac_from_state_image=i,this.bus=e,this.bus.register("net0-receive",(function(t){this.receive(t)}),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,255&this.port|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]),e=0;6>e;e++)this.memory[e<<1]=this.memory[e<<1|1]=this.mac[e];this.memory[28]=this.memory[29]=87,this.memory[30]=this.memory[31]=87,_("Mac: "+p(this.mac[0],2)+":"+p(this.mac[1],2)+":"+p(this.mac[2],2)+":"+p(this.mac[3],2)+":"+p(this.mac[4],2)+":"+p(this.mac[5],2),1048576),this.rsar=0,this.pstart=64,this.pstop=128,this.boundary=this.curpg=76,(e=t.io).register_read(0|this.port,this,(function(){return _("Read cmd",1048576),this.cr})),e.register_write(0|this.port,this,(function(t){this.cr=t,_("Write command: "+p(t,2)+" newpg="+(this.cr>>6)+" txcr="+p(this.txcr,2),1048576),1&this.cr||(24&t&&0===this.rcnt&&this.do_interrupt(64),4&t&&(t=this.tpsr<<8,t=this.memory.subarray(t,t+this.tcnt),this.bus.send("net0-send",t),this.bus.send("eth-transmit-end",[t.length]),this.cr&=-5,this.do_interrupt(2),_("Command: Transfer. length="+p(t.byteLength),1048576)))})),e.register_read(13|this.port,this,(function(){return _("Read counter0",1048576),0})),e.register_read(14|this.port,this,(function(){return _("Read counter1",1048576),0})),e.register_read(15|this.port,this,(function(){return _("Read counter2",1048576),0})),e.register_read(31|this.port,this,(function(){var t=this.get_page();return 0===t?(_("Read reset",1048576),this.do_interrupt(128)):_("Read pg"+t+"/1f",1048576),0})),e.register_write(31|this.port,this,(function(t){var e=this.get_page();_(0===e?"Write reset: "+p(t,2):"Write pg"+e+"/1f: "+p(t),1048576)})),e.register_read(1|this.port,this,(function(){var t=this.get_page();return 0===t?this.pstart:1===t?(_("Read pg1/01 (mac[0])",1048576),this.mac[0]):2===t?this.pstart:(_("Read pg"+t+"/01"),0)})),e.register_write(1|this.port,this,(function(t){var e=this.get_page();0===e?(_("start page: "+p(t,2),1048576),this.pstart=t):1===e?(_("mac[0] = "+p(t),1048576),this.mac[0]=t):_(3===e?"Unimplemented: Write pg3/01 (9346CR): "+p(t):"Write pg"+e+"/01: "+p(t),1048576)})),e.register_read(2|this.port,this,(function(){var t=this.get_page();return 0===t?this.pstop:1===t?(_("Read pg1/02 (mac[1])",1048576),this.mac[1]):2===t?this.pstop:(_("Read pg"+t+"/02",1048576),0)})),e.register_write(2|this.port,this,(function(t){var e=this.get_page();0===e?(_("stop page: "+p(t,2),1048576),t>this.memory.length>>8&&(t=this.memory.length>>8,_("XXX: Adjusting stop page to "+p(t),1048576)),this.pstop=t):1===e?(_("mac[1] = "+p(t),1048576),this.mac[1]=t):_("Write pg"+e+"/02: "+p(t),1048576)})),e.register_read(7|this.port,this,(function(){var t=this.get_page();return 0===t?(_("Read isr: "+p(this.isr,2),1048576),this.isr):1===t?(_("Read curpg: "+p(this.curpg,2),1048576),this.curpg):void 0})),e.register_write(7|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write isr: "+p(t,2),1048576),this.isr&=~t,this.update_irq()):1===e&&(_("Write curpg: "+p(t,2),1048576),this.curpg=t)})),e.register_write(13|this.port,this,(function(t){var e=this.get_page();0===e?(this.txcr=t,_("Write tx config: "+p(t,2),1048576)):_("Unimplemented: Write pg"+e+"/0d "+p(t,2),1048576)})),e.register_write(14|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write data configuration: "+p(t,2),1048576),this.dcfg=t):_("Unimplemented: Write pg"+e+"/0e "+p(t,2),1048576)})),e.register_read(10|this.port,this,(function(){return 0===this.get_page()?(_("Read pg0/0a",1048576),80):0})),e.register_write(10|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write remote byte count low: "+p(t,2),1048576),this.rcnt=65280&this.rcnt|255&t):_("Unimplemented: Write pg"+e+"/0a "+p(t,2),1048576)})),e.register_read(11|this.port,this,(function(){return 0===this.get_page()?(_("Read pg0/0b",1048576),67):0})),e.register_write(11|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write remote byte count high: "+p(t,2),1048576),this.rcnt=255&this.rcnt|t<<8&65280):_("Unimplemented: Write pg"+e+"/0b "+p(t,2),1048576)})),e.register_read(8|this.port,this,(function(){var t=this.get_page();if(0===t)return _("Read remote start address low",1048576),255&this.rsar;_("Unimplemented: Read pg"+t+"/08",1048576)})),e.register_write(8|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write remote start address low: "+p(t,2),1048576),this.rsar=65280&this.rsar|255&t):_("Unimplemented: Write pg"+e+"/08 "+p(t,2),1048576)})),e.register_read(9|this.port,this,(function(){var t=this.get_page();if(0===t)return _("Read remote start address high",1048576),this.rsar>>8&255;_("Unimplemented: Read pg"+t+"/09",1048576)})),e.register_write(9|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write remote start address low: "+p(t,2),1048576),this.rsar=255&this.rsar|t<<8&65280):_("Unimplemented: Write pg"+e+"/09 "+p(t,2),1048576)})),e.register_write(15|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write interrupt mask register: "+p(t,2)+" isr="+p(this.isr,2),1048576),this.imr=t,this.update_irq()):_("Unimplemented: Write pg"+e+"/0f "+p(t,2),1048576)})),e.register_read(3|this.port,this,(function(){var t=this.get_page();return 0===t?(_("Read boundary: "+p(this.boundary,2),1048576),this.boundary):1===t?(_("Read pg1/03 (mac[2])",1048576),this.mac[2]):(_(3===t?"Unimplemented: Read pg3/03 (CONFIG0)":"Read pg"+t+"/03",1048576),0)})),e.register_write(3|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write boundary: "+p(t,2),1048576),this.boundary=t):1===e?(_("mac[2] = "+p(t),1048576),this.mac[2]=t):_("Write pg"+e+"/03: "+p(t),1048576)})),e.register_read(4|this.port,this,(function(){var t=this.get_page();return 0===t?this.tsr:1===t?(_("Read pg1/04 (mac[3])",1048576),this.mac[3]):(_("Read pg"+t+"/04",1048576),0)})),e.register_write(4|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write tpsr: "+p(t,2),1048576),this.tpsr=t):1===e?(_("mac[3] = "+p(t),1048576),this.mac[3]=t):_("Write pg"+e+"/04: "+p(t),1048576)})),e.register_read(5|this.port,this,(function(){var t=this.get_page();return 0===t?(_("Unimplemented: Read pg0/05 (NCR: Number of Collisions Register)",1048576),0):1===t?(_("Read pg1/05 (mac[4])",1048576),this.mac[4]):(_(3===t?"Unimplemented: Read pg3/05 (CONFIG2)":"Read pg"+t+"/05",1048576),0)})),e.register_write(5|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write tcnt low: "+p(t,2),1048576),this.tcnt=-256&this.tcnt|t):1===e?(_("mac[4] = "+p(t),1048576),this.mac[4]=t):_(3===e?"Unimplemented: Write pg3/05 (CONFIG2): "+p(t):"Write pg"+e+"/05: "+p(t),1048576)})),e.register_read(6|this.port,this,(function(){var t=this.get_page();return 0===t?0:1===t?(_("Read pg1/06 (mac[5])",1048576),this.mac[5]):(_(3===t?"Unimplemented: Read pg3/06 (CONFIG3)":"Read pg"+t+"/06",1048576),0)})),e.register_write(6|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write tcnt high: "+p(t,2),1048576),this.tcnt=255&this.tcnt|t<<8):1===e?(_("mac[5] = "+p(t),1048576),this.mac[5]=t):_(3===e?"Unimplemented: Write pg3/06 (CONFIG3): "+p(t):"Write pg"+e+"/06: "+p(t),1048576)})),e.register_read(12|this.port,this,(function(){var t=this.get_page();return 0===t?9:(_("Unimplemented: Read pg"+t+"/0c",1048576),0)})),e.register_write(12|this.port,this,(function(t){var e=this.get_page();0===e?(_("RX configuration reg write: "+p(t,2),1048576),this.rxcr=t):_("Unimplemented: Write pg"+e+"/0c: "+p(t),1048576)})),e.register_read(16|this.port,this,this.data_port_read8,this.data_port_read16,this.data_port_read32),e.register_write(16|this.port,this,this.data_port_write16,this.data_port_write16,this.data_port_write32),t.devices.pci.register_device(this)}R.prototype.get_state=function(){var t=[];return t[0]=this.isr,t[1]=this.imr,t[2]=this.cr,t[3]=this.dcfg,t[4]=this.rcnt,t[5]=this.tcnt,t[6]=this.tpsr,t[7]=this.rsar,t[8]=this.pstart,t[9]=this.curpg,t[10]=this.boundary,t[11]=this.pstop,t[12]=this.rxcr,t[13]=this.txcr,t[14]=this.tsr,t[15]=this.mac,t[16]=this.memory,t},R.prototype.set_state=function(t){this.isr=t[0],this.imr=t[1],this.cr=t[2],this.dcfg=t[3],this.rcnt=t[4],this.tcnt=t[5],this.tpsr=t[6],this.rsar=t[7],this.pstart=t[8],this.curpg=t[9],this.boundary=t[10],this.pstop=t[11],this.rxcr=t[12],this.txcr=t[13],this.tsr=t[14],this.preserve_mac_from_state_image&&(this.mac=t[15],this.memory=t[16])},R.prototype.do_interrupt=function(t){_("Do interrupt "+p(t,2),1048576),this.isr|=t,this.update_irq()},R.prototype.update_irq=function(){this.imr&this.isr?this.pci.raise_irq(this.pci_id):this.pci.lower_irq(this.pci_id)},R.prototype.data_port_write=function(t){(16>=this.rsar||this.rsar>=16384&&this.rsar<32768)&&(this.memory[this.rsar]=t),this.rsar++,this.rcnt--,this.rsar>=this.pstop<<8&&(this.rsar+=this.pstart-this.pstop<<8),0===this.rcnt&&this.do_interrupt(64)},R.prototype.data_port_write16=function(t){this.data_port_write(t),1&this.dcfg&&this.data_port_write(t>>8)},R.prototype.data_port_write32=function(t){this.data_port_write(t),this.data_port_write(t>>8),this.data_port_write(t>>16),this.data_port_write(t>>24)},R.prototype.data_port_read=function(){let t=0;return this.rsar<32768&&(t=this.memory[this.rsar]),this.rsar++,this.rcnt--,this.rsar>=this.pstop<<8&&(this.rsar+=this.pstart-this.pstop<<8),0===this.rcnt&&this.do_interrupt(64),t},R.prototype.data_port_read8=function(){return 255&this.data_port_read16()},R.prototype.data_port_read16=function(){return 1&this.dcfg?this.data_port_read()|this.data_port_read()<<8:this.data_port_read()},R.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},R.prototype.receive=function(t){if(!(1&this.cr)&&(this.bus.send("eth-receive-end",[t.length]),16&this.rxcr||4&this.rxcr&&255===t[0]&&255===t[1]&&255===t[2]&&255===t[3]&&255===t[4]&&255===t[5]||!(8&this.rxcr&&1==(1&t[0])||t[0]!==this.mac[0]||t[1]!==this.mac[1]||t[2]!==this.mac[2]||t[3]!==this.mac[3]||t[4]!==this.mac[4]||t[5]!==this.mac[5]))){var e=this.curpg<<8,i=Math.max(60,t.length)+4,s=e+4,r=this.curpg+1+(i>>8),a=e+i,o=1+(i>>8),n=this.boundary>this.curpg?this.boundary-this.curpg:this.pstop-this.curpg+this.boundary-this.pstart;n<o&&0!==this.boundary?_("Buffer full, dropping packet pstart="+p(this.pstart)+" pstop="+p(this.pstop)+" curpg="+p(this.curpg)+" needed="+p(o)+" boundary="+p(this.boundary)+" available="+p(n),1048576):(a>this.pstop<<8?(t.length,a=(this.pstop<<8)-s,this.memory.set(t.subarray(0,a),s),this.memory.set(t.subarray(a),this.pstart<<8),_("rcv cut="+p(a),1048576)):(this.memory.set(t,s),60>t.length&&this.memory.fill(0,s+t.length,s+60)),r>=this.pstop&&(r+=this.pstart-this.pstop),this.memory[e]=1,this.memory[e+1]=r,this.memory[e+2]=i,this.memory[e+3]=i>>8,this.curpg=r,_("rcv offset="+p(e)+" len="+p(i)+" next="+p(r),1048576),this.do_interrupt(1))}},R.prototype.get_page=function(){return this.cr>>6&3};function E(t){this.pci_addr=new Uint8Array(4),this.pci_value=new Uint8Array(4),this.pci_response=new Uint8Array(4),this.pci_status=new Uint8Array(4),this.pci_addr32=new Int32Array(this.pci_addr.buffer),this.pci_value32=new Int32Array(this.pci_value.buffer),this.pci_response32=new Int32Array(this.pci_response.buffer),this.pci_status32=new Int32Array(this.pci_status.buffer),this.device_spaces=[],this.devices=[],this.cpu=t;for(var e=0;256>e;e++)this.device_spaces[e]=void 0,this.devices[e]=void 0;this.io=t.io,t.io.register_write(3324,this,(function(t){this.pci_write8(this.pci_addr32[0],t)}),(function(t){this.pci_write16(this.pci_addr32[0],t)}),(function(t){this.pci_write32(this.pci_addr32[0],t)})),t.io.register_write(3325,this,(function(t){this.pci_write8(this.pci_addr32[0]+1|0,t)})),t.io.register_write(3326,this,(function(t){this.pci_write8(this.pci_addr32[0]+2|0,t)}),(function(t){this.pci_write16(this.pci_addr32[0]+2|0,t)})),t.io.register_write(3327,this,(function(t){this.pci_write8(this.pci_addr32[0]+3|0,t)})),t.io.register_read_consecutive(3324,this,(function(){return this.pci_response[0]}),(function(){return this.pci_response[1]}),(function(){return this.pci_response[2]}),(function(){return this.pci_response[3]})),t.io.register_read_consecutive(3320,this,(function(){return this.pci_status[0]}),(function(){return this.pci_status[1]}),(function(){return this.pci_status[2]}),(function(){return this.pci_status[3]})),t.io.register_write_consecutive(3320,this,(function(t){this.pci_addr[0]=252&t}),(function(e){2==(6&this.pci_addr[1])&&6==(6&e)?(_("CPU reboot via PCI"),t.reboot_internal()):this.pci_addr[1]=e}),(function(t){this.pci_addr[2]=t}),(function(t){this.pci_addr[3]=t,this.pci_query()})),this.register_device({pci_id:0,pci_space:[134,128,55,18,0,0,0,0,2,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0],pci_bars:[],name:"82441FX PMC"}),this.isa_bridge={pci_id:8,pci_space:[134,128,0,112,7,0,0,2,0,0,1,6,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],pci_bars:[],name:"82371SB PIIX3 ISA"},this.isa_bridge_space=this.register_device(this.isa_bridge),this.isa_bridge_space8=new Uint8Array(this.isa_bridge_space.buffer)}E.prototype.get_state=function(){for(var t=[],e=0;256>e;e++)t[e]=this.device_spaces[e];return t[256]=this.pci_addr,t[257]=this.pci_value,t[258]=this.pci_response,t[259]=this.pci_status,t},E.prototype.set_state=function(t){for(var e=0;256>e;e++){var i=this.devices[e],s=t[e];if(i&&s){for(var r=0;r<i.pci_bars.length;r++){var a=s[4+r];if(1&a){var o=i.pci_bars[r];this.set_io_bars(o,65534&o.original_bar,65534&a)}}this.device_spaces[e].set(s)}else i&&_("Warning: While restoring PCI device: Device exists in current configuration but not in snapshot ("+i.name+")"),s&&_("Warning: While restoring PCI device: Device doesn't exist in current configuration but does in snapshot (device "+p(e,2)+")")}this.pci_addr.set(t[256]),this.pci_value.set(t[257]),this.pci_response.set(t[258]),this.pci_status.set(t[259])},E.prototype.pci_query=function(){var t=this.pci_addr[2]<<8|this.pci_addr[1],e=252&this.pci_addr[0],i=t>>3&31,s="query enabled="+(this.pci_addr[3]>>7)+" bdf="+p(t,4);s+=" dev="+p(i,2),s+=" addr="+p(e,2),void 0!==(i=this.device_spaces[t])?(this.pci_status32[0]=-2147483648,this.pci_response32[0]=e<i.byteLength?i[e>>2]:0,s+=" "+p(this.pci_addr32[0]>>>0,8)+" -> "+p(this.pci_response32[0]>>>0,8),e>=i.byteLength&&(s+=" (undef)"),s+=" ("+this.devices[t].name+")",_(s,2048)):(this.pci_response32[0]=-1,this.pci_status32[0]=0)},E.prototype.pci_write8=function(t,e){var i=t>>8&65535;t&=255;var s=new Uint8Array(this.device_spaces[i].buffer),r=this.devices[i];s&&(p(t),_("PCI write8 dev="+p(i>>3,2)+" ("+r.name+") addr="+p(t,4)+" value="+p(e,2),2048),s[t]=e)},E.prototype.pci_write16=function(t,e){var i=t>>8&65535;t&=255;var s=new Uint16Array(this.device_spaces[i].buffer),r=this.devices[i];s&&(16<=t&&44>t?_("Warning: PCI: Expected 32-bit write, got 16-bit (addr: "+p(t)+")"):(p(t),_("PCI writ16 dev="+p(i>>3,2)+" ("+r.name+") addr="+p(t,4)+" value="+p(e,4),2048),s[t>>>1]=e))},E.prototype.pci_write32=function(t,e){var i=t>>8&65535;t&=255;var s=this.device_spaces[i],r=this.devices[i];if(s)if(16<=t&&40>t){var a=t-16>>2,o=r.pci_bars[a];_("BAR"+a+" exists="+(o?"y":"n")+" changed to "+p(e>>>0)+" dev="+p(i>>3,2)+" ("+r.name+") ",2048),o?(o.size,o.size,r=1&s[i=t>>2],-1==(3|e|o.size-1)?(e=~(o.size-1)|r,0===r&&(s[i]=e)):0===r&&((-16&e)!=(-16&(a=o.original_bar))&&_("Warning: Changing memory bar not supported, ignored",2048),s[i]=a),1===r&&(r=65534&s[i],a=65534&e,_("io bar changed from "+p(r>>>0,8)+" to "+p(a>>>0,8)+" size="+o.size,2048),this.set_io_bars(o,r,a),s[i]=1|e)):s[t>>2]=0,_("BAR effective value: "+p(s[t>>2]>>>0),2048)}else 48===t?(_("PCI write rom address dev="+p(i>>3,2)+" ("+r.name+") value="+p(e>>>0,8),2048),s[t>>2]=r.pci_rom_size?-1==(2047|e)?0|-r.pci_rom_size:0|r.pci_rom_address:0):4===t?_("PCI write dev="+p(i>>3,2)+" ("+r.name+") addr="+p(t,4)+" value="+p(e>>>0,8),2048):(_("PCI write dev="+p(i>>3,2)+" ("+r.name+") addr="+p(t,4)+" value="+p(e>>>0,8),2048),s[t>>>2]=e)},E.prototype.register_device=function(t){t.pci_id,t.pci_space,t.pci_bars;var e=t.pci_id;_("PCI register bdf="+p(e)+" ("+t.name+")",2048),this.devices[e],t.pci_space.length,this.devices.length;var i=new Int32Array(64);i.set(new Int32Array(new Uint8Array(t.pci_space).buffer)),this.device_spaces[e]=i,this.devices[e]=t,e=i.slice(4,10);for(var s=0;s<t.pci_bars.length;s++){var r=t.pci_bars[s];if(r){var a=e[s],o=1&a;if(r.original_bar=a,r.entries=[],0!==o)for(a&=-2,o=0;o<r.size;o++)r.entries[o]=this.io.ports[a+o]}}return i},E.prototype.set_io_bars=function(t,e,i){var s=t.size;_("Move io bars: from="+p(e)+" to="+p(i)+" count="+s,2048);for(var r=this.io.ports,a=0;a<s;a++){var o=r[e+a];r[e+a]=this.io.create_empty_entry(),o.read8===this.io.empty_port_read8&&o.read16===this.io.empty_port_read16&&o.read32===this.io.empty_port_read32&&o.write8===this.io.empty_port_write&&o.write16===this.io.empty_port_write&&o.write32===this.io.empty_port_write&&_("Warning: Bad IO bar: Source not mapped, port="+p(e+a,4),2048),o=r[i+a],r[i+a]=t.entries[a],o.read8!==this.io.empty_port_read8&&o.read16!==this.io.empty_port_read16&&o.read32!==this.io.empty_port_read32&&o.write8!==this.io.empty_port_write&&o.write16!==this.io.empty_port_write&&o.write32!==this.io.empty_port_write||_("Warning: Bad IO bar: Target already mapped, port="+p(i+a,4),2048)}},E.prototype.raise_irq=function(t){this.cpu.device_raise_irq(this.isa_bridge_space8[96+((this.device_spaces[t][15]>>8&255)-1+((t>>3)-1&255)&3)])},E.prototype.lower_irq=function(t){this.cpu.device_lower_irq(this.isa_bridge_space8[96+((this.device_spaces[t][15]>>8&255)+(t>>3&255)-2&3)])};function C(t,e){this.irq_value=this.irr=this.isr=this.irq_map=this.irq_mask=0,this.requested_irq=-1,this.master=e,this.is_master=void 0===this.master,this.slave=void 0,this.name=this.is_master?"master":"slave ",this.expect_icw4=!1,this.read_isr=this.state=0,this.auto_eoi=1,this.elcr=this.special_mask_mode=0,this.cpu=t,this.is_master?(this.slave=new C(this.cpu,this),this.check_irqs=function(){if(0<=this.requested_irq)this.cpu.handle_irqs();else{var t=this.irr&this.irq_mask;if(t){t&=-t;var e=this.special_mask_mode?this.irq_mask:-1;this.isr&&(this.isr&-this.isr&e)<=t?_("master> higher prio: isr="+p(this.isr,2)+" mask="+p(255&this.irq_mask,2)+" irq="+p(t,2),128):(this.requested_irq=c.int_log2_byte(t),this.cpu.handle_irqs())}}},this.acknowledge_irq=function(){if(-1!==this.requested_irq)if(0===this.irr)this.requested_irq=-1;else{this.irr,this.requested_irq;var t=1<<this.requested_irq;0==(this.elcr&t)&&(this.irr&=~t),this.auto_eoi||(this.isr|=t),2===this.requested_irq?this.slave.acknowledge_irq():this.cpu.pic_call_irq(this.irq_map|this.requested_irq),this.requested_irq=-1,this.check_irqs()}}):(this.check_irqs=function(){if(0<=this.requested_irq)this.cpu.handle_irqs();else{var t=this.irr&this.irq_mask;if(t){t&=-t;var e=this.special_mask_mode?this.irq_mask:-1;this.isr&&(this.isr&-this.isr&e)<=t||(this.requested_irq=c.int_log2_byte(t),this.master.set_irq(2))}}},this.acknowledge_irq=function(){if(-1!==this.requested_irq)if(0===this.irr)this.requested_irq=-1,this.master.irq_value&=-5,this.cpu.pic_call_irq(7|this.irq_map);else{this.irr,this.requested_irq;var t=1<<this.requested_irq;0==(this.elcr&t)&&(this.irr&=~t),this.auto_eoi||(this.isr|=t),this.master.irq_value&=-5,this.cpu.pic_call_irq(this.irq_map|this.requested_irq),this.requested_irq=-1,this.check_irqs()}}),this.dump=function(){_("mask: "+p(255&this.irq_mask),128),_("base: "+p(this.irq_map),128),_("requested: "+p(this.irr),128),_("serviced: "+p(this.isr),128),this.is_master&&this.slave.dump()},this.is_master?(t=32,e=1232):(t=160,e=1233),this.cpu.io.register_write(t,this,this.port20_write),this.cpu.io.register_read(t,this,this.port20_read),this.cpu.io.register_write(1|t,this,this.port21_write),this.cpu.io.register_read(1|t,this,this.port21_read),this.cpu.io.register_write(e,this,this.port4D0_write),this.cpu.io.register_read(e,this,this.port4D0_read),this.is_master?(this.set_irq=function(t){8<=t?this.slave.set_irq(t-8):(t=1<<t,0==(this.irq_value&t)&&(this.irr|=t,this.irq_value|=t,this.check_irqs()))},this.clear_irq=function(t){8<=t?this.slave.clear_irq(t-8):(t=1<<t,this.irq_value&t&&(this.irq_value&=~t,this.irr&=~t,this.check_irqs()))}):(this.set_irq=function(t){t=1<<t,0==(this.irq_value&t)&&(this.irr|=t,this.irq_value|=t,this.check_irqs())},this.clear_irq=function(t){t=1<<t,this.irq_value&t&&(this.irq_value&=~t,this.irr&=~t,this.check_irqs())}),this.get_isr=function(){return this.isr}}C.prototype.get_state=function(){var t=[];return t[0]=this.irq_mask,t[1]=this.irq_map,t[2]=this.isr,t[3]=this.irr,t[4]=this.is_master,t[5]=this.slave,t[6]=this.expect_icw4,t[7]=this.state,t[8]=this.read_isr,t[9]=this.auto_eoi,t[10]=this.elcr,t},C.prototype.set_state=function(t){this.irq_mask=t[0],this.irq_map=t[1],this.isr=t[2],this.irr=t[3],this.is_master=t[4],this.slave&&this.slave.set_state(t[5]),this.expect_icw4=t[6],this.state=t[7],this.read_isr=t[8],this.auto_eoi=t[9],this.elcr=t[10]},C.prototype.port20_write=function(t){if(16&t)_("icw1 = "+p(t),128),this.irq_value=this.irq_mask=this.irr=this.isr=0,this.auto_eoi=1,this.requested_irq=-1,this.expect_icw4=1&t,this.state=1;else if(8&t)_("ocw3: "+p(t),128),2&t&&(this.read_isr=1&t),64&t&&(this.special_mask_mode=32==(32&t),_("special mask mode: "+this.special_mask_mode,128));else{_("eoi: "+p(t)+" ("+this.name+")",128);var e=t>>5;1===e?(this.isr&=this.isr-1,_("new isr: "+p(this.isr,2),128)):3===e?this.isr&=~(1<<(7&t)):192==(200&t)?_("lowest priority: "+p(7&t),128):(_("Unknown eoi: "+p(t),128),this.isr&=this.isr-1),this.check_irqs()}},C.prototype.port20_read=function(){return this.read_isr?(_("read port 20h (isr): "+p(this.isr),128),this.isr):(_("read port 20h (irr): "+p(this.irr),128),this.irr)},C.prototype.port21_write=function(t){0===this.state?this.expect_icw4?(this.expect_icw4=!1,this.auto_eoi=2&t,_("icw4: "+p(t)+" autoeoi="+this.auto_eoi,128)):(this.irq_mask=~t,this.check_irqs()):1===this.state?(this.irq_map=t,_("interrupts are mapped to "+p(this.irq_map)+" ("+this.name+")",128),this.state++):2===this.state&&(this.state=0,_("icw3: "+p(t),128))},C.prototype.port21_read=function(){return _("21h read "+p(255&~this.irq_mask),128),255&~this.irq_mask},C.prototype.port4D0_read=function(){return _("elcr read: "+p(this.elcr,2),128),this.elcr},C.prototype.port4D0_write=function(t){_("elcr write: "+p(t,2),128),this.elcr=t};function T(t,e){this.cpu=t,this.bus=e,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),t.io.register_read(97,this,(function(){var t=O.microtick();return(66.66666666666667*t&1)<<4|(t=this.did_rollover(2,t))<<5})),t.io.register_write(97,this,(function(t){1&t?this.bus.send("pcspeaker-enable"):this.bus.send("pcspeaker-disable")})),t.io.register_read(64,this,(function(){return this.counter_read(0)})),t.io.register_read(65,this,(function(){return this.counter_read(1)})),t.io.register_read(66,this,(function(){return this.counter_read(2)})),t.io.register_write(64,this,(function(t){this.counter_write(0,t)})),t.io.register_write(65,this,(function(t){this.counter_write(1,t)})),t.io.register_write(66,this,(function(t){this.counter_write(2,t),this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]])})),t.io.register_write(67,this,this.port43_write)}T.prototype.get_state=function(){var t=[];return t[0]=this.counter_next_low,t[1]=this.counter_enabled,t[2]=this.counter_mode,t[3]=this.counter_read_mode,t[4]=this.counter_latch,t[5]=this.counter_latch_value,t[6]=this.counter_reload,t[7]=this.counter_start_time,t[8]=this.counter_start_value,t},T.prototype.set_state=function(t){this.counter_next_low=t[0],this.counter_enabled=t[1],this.counter_mode=t[2],this.counter_read_mode=t[3],this.counter_latch=t[4],this.counter_latch_value=t[5],this.counter_reload=t[6],this.counter_start_time=t[7],this.counter_start_value=t[8]},T.prototype.timer=function(t,e){var i=100;return e||(this.counter_enabled[0]&&this.did_rollover(0,t)?(this.counter_start_value[0]=this.get_counter_value(0,t),this.counter_start_time[0]=t,_("pit interrupt. new value: "+this.counter_start_value[0],512),this.cpu.device_lower_irq(0),this.cpu.device_raise_irq(0),0===this.counter_mode[0]&&(this.counter_enabled[0]=0)):this.cpu.device_lower_irq(0),this.counter_enabled[0]&&(i=(this.counter_start_value[0]-Math.floor(1193.1816666*(t-this.counter_start_time[0])))/1193.1816666)),i},T.prototype.get_counter_value=function(t,e){if(!this.counter_enabled[t])return 0;var i=e-this.counter_start_time[t],s=Math.floor(1193.1816666*i);return e=this.counter_start_value[t]-s,_("diff="+i+" dticks="+s+" value="+e+" reload="+this.counter_reload[t],512),e>=(i=this.counter_reload[t])?(_("Warning: Counter"+t+" value "+e+" is larger than reload "+i,512),e%=i):0>e&&(e=e%i+i),e},T.prototype.did_rollover=function(t,e){return 0>(e-=this.counter_start_time[t])?(_("Warning: PIT timer difference is negative, resetting (timer "+t+")"),!0):this.counter_start_value[t]<Math.floor(1193.1816666*e)},T.prototype.counter_read=function(t){var e=this.counter_latch[t];return e?(this.counter_latch[t]--,2===e?255&this.counter_latch_value[t]:this.counter_latch_value[t]>>8):(e=this.counter_next_low[t],3===this.counter_mode[t]&&(this.counter_next_low[t]^=1),t=this.get_counter_value(t,O.microtick()),e?255&t:t>>8)},T.prototype.counter_write=function(t,e){this.counter_reload[t]=this.counter_next_low[t]?-256&this.counter_reload[t]|e:255&this.counter_reload[t]|e<<8,3===this.counter_read_mode[t]&&this.counter_next_low[t]||(this.counter_reload[t]||(this.counter_reload[t]=65535),this.counter_start_value[t]=this.counter_reload[t],this.counter_enabled[t]=!0,this.counter_start_time[t]=O.microtick(),_("counter"+t+" reload="+p(this.counter_reload[t])+" tick="+(this.counter_reload[t]||65536)/1193.1816666+"ms",512)),3===this.counter_read_mode[t]&&(this.counter_next_low[t]^=1)},T.prototype.port43_write=function(t){var e=t>>1&7,i=1&t,s=t>>6&3;t=t>>4&3,1===s&&_("Unimplemented timer1",512),3===s?_("Unimplemented read back",512):0===t?(this.counter_latch[s]=2,e=this.get_counter_value(s,O.microtick()),_("latch: "+e,512),this.counter_latch_value[s]=e?e-1:0):(6<=e&&(e&=-5),_("Control: mode="+e+" ctr="+s+" read_mode="+t+" bcd="+i,512),this.counter_next_low[s]=1===t?0:1,0===s&&this.cpu.device_lower_irq(0),0!==e&&3!==e&&2!==e&&_("Unimplemented counter mode: "+p(e),512),this.counter_mode[s]=e,this.counter_read_mode[s]=t,2===s&&this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]]))},T.prototype.dump=function(){let t=this.counter_reload[0];_("counter0 ticks every "+(t||65536)/1193.1816666+"ms (reload="+t+")")};function q(t,e){this.cpu=t,this.bus=e,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 l(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 l(1024),this.next_byte_is_aux=this.next_byte_is_ready=!1,this.bus.register("keyboard-code",(function(t){this.kbd_send_code(t)}),this),this.bus.register("mouse-click",(function(t){this.mouse_send_click(t[0],t[1],t[2])}),this),this.bus.register("mouse-delta",(function(t){this.mouse_send_delta(t[0],t[1])}),this),this.bus.register("mouse-wheel",(function(t){}),this),this.command_register=5,this.controller_output_port=0,this.read_controller_output_port=this.read_command_register=this.read_output_register=!1,t.io.register_read(96,this,this.port60_read),t.io.register_read(100,this,this.port64_read),t.io.register_write(96,this,this.port60_write),t.io.register_write(100,this,this.port64_write)}q.prototype.get_state=function(){var t=[];return t[0]=this.enable_mouse_stream,t[1]=this.use_mouse,t[2]=this.have_mouse,t[3]=this.mouse_delta_x,t[4]=this.mouse_delta_y,t[5]=this.mouse_clicks,t[6]=this.have_keyboard,t[7]=this.enable_keyboard_stream,t[8]=this.next_is_mouse_command,t[9]=this.next_read_sample,t[10]=this.next_read_led,t[11]=this.next_handle_scan_code_set,t[12]=this.next_read_rate,t[13]=this.next_read_resolution,t[15]=this.last_port60_byte,t[16]=this.sample_rate,t[17]=this.resolution,t[18]=this.scaling2,t[20]=this.command_register,t[21]=this.read_output_register,t[22]=this.read_command_register,t[23]=this.controller_output_port,t[24]=this.read_controller_output_port,t},q.prototype.set_state=function(t){this.enable_mouse_stream=t[0],this.use_mouse=t[1],this.have_mouse=t[2],this.mouse_delta_x=t[3],this.mouse_delta_y=t[4],this.mouse_clicks=t[5],this.have_keyboard=t[6],this.enable_keyboard_stream=t[7],this.next_is_mouse_command=t[8],this.next_read_sample=t[9],this.next_read_led=t[10],this.next_handle_scan_code_set=t[11],this.next_read_rate=t[12],this.next_read_resolution=t[13],this.last_port60_byte=t[15],this.sample_rate=t[16],this.resolution=t[17],this.scaling2=t[18],this.command_register=t[20],this.read_output_register=t[21],this.read_command_register=t[22],this.controller_output_port=t[23],this.read_controller_output_port=t[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)},q.prototype.raise_irq=function(){this.next_byte_is_ready||(this.kbd_buffer.length?this.kbd_irq():this.mouse_buffer.length&&this.mouse_irq())},q.prototype.mouse_irq=function(){this.next_byte_is_aux=this.next_byte_is_ready=!0,2&this.command_register&&(_("Mouse irq",64),this.cpu.device_lower_irq(12),this.cpu.device_raise_irq(12))},q.prototype.kbd_irq=function(){this.next_byte_is_ready=!0,this.next_byte_is_aux=!1,1&this.command_register&&(_("Keyboard irq",64),this.cpu.device_lower_irq(1),this.cpu.device_raise_irq(1))},q.prototype.kbd_send_code=function(t){this.enable_keyboard_stream&&(_("adding kbd code: "+p(t),64),this.kbd_buffer.push(t),this.raise_irq())},q.prototype.mouse_send_delta=function(t,e){if(this.have_mouse&&this.use_mouse){var i=this.resolution*this.sample_rate/80;this.mouse_delta_x+=t*i,this.mouse_delta_y+=e*i,this.enable_mouse_stream&&(t=0|this.mouse_delta_x,e=0|this.mouse_delta_y,t||e)&&(this.mouse_delta_x-=t,this.mouse_delta_y-=e,this.send_mouse_packet(t,e))}},q.prototype.mouse_send_click=function(t,e,i){this.have_mouse&&this.use_mouse&&(this.mouse_clicks=t|i<<1|e<<2,this.enable_mouse_stream&&this.send_mouse_packet(0,0))},q.prototype.send_mouse_packet=function(t,e){var i=(0>e)<<5|(0>t)<<4|8|this.mouse_clicks;this.last_mouse_packet=Date.now(),this.mouse_buffer.push(i),this.mouse_buffer.push(t),this.mouse_buffer.push(e),this.raise_irq()},q.prototype.apply_scaling2=function(t){var e=t>>31;switch(Math.abs(t)){case 0:case 1:case 3:return t;case 2:return e;case 4:return 6*e;case 5:return 9*e;default:return t<<1}},q.prototype.port60_read=function(){return this.next_byte_is_ready=!1,this.kbd_buffer.length||this.mouse_buffer.length?(this.next_byte_is_aux?(this.cpu.device_lower_irq(12),this.last_port60_byte=this.mouse_buffer.shift(),_("Port 60 read (mouse): "+p(this.last_port60_byte),64)):(this.cpu.device_lower_irq(1),this.last_port60_byte=this.kbd_buffer.shift(),_("Port 60 read (kbd) : "+p(this.last_port60_byte),64)),(this.kbd_buffer.length||this.mouse_buffer.length)&&this.raise_irq(),this.last_port60_byte):(_("Port 60 read: Empty",64),this.last_port60_byte)},q.prototype.port64_read=function(){var t=16;return this.next_byte_is_ready&&(t|=1),this.next_byte_is_aux&&(t|=32),_("port 64 read: "+p(t),64),t},q.prototype.port60_write=function(t){if(_("port 60 write: "+p(t),64),this.read_command_register)this.command_register=t,this.read_command_register=!1,_("Keyboard command register = "+p(this.command_register),64);else if(this.read_output_register)this.read_output_register=!1,this.mouse_buffer.clear(),this.mouse_buffer.push(t),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=t,_("mouse sample rate: "+p(t),64),this.sample_rate||(_("invalid sample rate, reset to 100",64),this.sample_rate=100),this.mouse_irq();else if(this.next_read_resolution)this.next_read_resolution=!1,this.mouse_buffer.clear(),this.mouse_buffer.push(250),3<t?(this.resolution=4,_("invalid resolution, resetting to 4",64)):(this.resolution=1<<t,_("resolution: "+this.resolution,64)),this.mouse_irq();else if(this.next_read_led)this.next_read_led=!1,this.kbd_buffer.push(250),this.kbd_irq();else if(this.next_handle_scan_code_set)this.next_handle_scan_code_set=!1,this.kbd_buffer.push(250),this.kbd_irq(),t||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,_("Port 60 data register write: "+p(t),64),this.have_mouse){switch(this.kbd_buffer.clear(),this.mouse_buffer.clear(),this.mouse_buffer.push(250),t){case 230:_("Scaling 1:1",64),this.scaling2=!1;break;case 231:_("Scaling 2:1",64),this.scaling2=!0;break;case 232:this.next_read_resolution=!0;break;case 233:this.send_mouse_packet(0,0);break;case 235:_("unimplemented request single packet",64),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:_("Mouse reset",64),this.mouse_buffer.push(170),this.mouse_buffer.push(0),this.use_mouse=!0,this.bus.send("mouse-enable",!0),this.enable_mouse_stream=!1,this.sample_rate=100,this.scaling2=!1,this.resolution=4,this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0;break;default:_("Unimplemented mouse command: "+p(t),64)}this.mouse_irq()}}else if(this.read_controller_output_port)this.read_controller_output_port=!1,this.controller_output_port=t;else{switch(_("Port 60 data register write: "+p(t),64),this.mouse_buffer.clear(),this.kbd_buffer.clear(),this.kbd_buffer.push(250),t){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:_("kbd enable scanning",64),this.enable_keyboard_stream=!0;break;case 245:_("kbd disable scanning",64),this.enable_keyboard_stream=!1;break;case 246:break;case 255:this.kbd_buffer.clear(),this.kbd_buffer.push(250),this.kbd_buffer.push(170),this.kbd_buffer.push(0);break;default:_("Unimplemented keyboard command: "+p(t),64)}this.kbd_irq()}},q.prototype.port64_write=function(t){switch(_("port 64 write: "+p(t),64),t){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:_("Disable second port",64),this.command_register|=32;break;case 168:_("Enable second port",64),this.command_register&=-33;break;case 169:case 171: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 173:_("Disable Keyboard",64),this.command_register|=16;break;case 174:_("Enable Keyboard",64),this.command_register&=-17;break;case 254:_("CPU reboot via PS2"),this.cpu.reboot_internal();break;default:_("port 64: Unimplemented command byte: "+p(t),64)}};var L="COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.",S=new Uint8Array(256),D=[],P=[],z=[],M=new Uint8Array(256),U=[];function G(t,e){this.cpu=t,this.bus=e,this.write_buffer=new l(64),this.read_buffer=new l(64),this.read_buffer_lastvalue=0,this.command=0,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 f(65536),new f(65536)],this.dma=t.devices.dma,this.dma_channel=this.dma_irq=this.dma_bytes_block=this.dma_bytes_left=this.dma_bytes_count=this.dma_sample_count=0,this.dma_channel_8bit=1,this.dma_channel_16bit=5,this.dma_autoinit=!1,this.dma_buffer=new ArrayBuffer(65536),this.dma_buffer_int8=new Int8Array(this.dma_buffer),this.dma_buffer_uint8=new Uint8Array(this.dma_buffer),this.dma_buffer_int16=new Int16Array(this.dma_buffer),this.dma_buffer_uint16=new Uint16Array(this.dma_buffer),this.dma_syncbuffer=new u(this.dma_buffer),this.dma_paused=this.dma_waiting_transfer=!1,this.sampling_rate=22050,e.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 l(64),this.fm_current_address1=this.fm_current_address0=this.mpu_read_buffer_lastvalue=0,this.fm_waveform_select_enable=!1,this.irq=5,this.irq_triggered=new Uint8Array(16),t.io.register_read_consecutive(544,this,this.port2x0_read,this.port2x1_read,this.port2x2_read,this.port2x3_read),t.io.register_read_consecutive(904,this,this.port2x0_read,this.port2x1_read),t.io.register_read_consecutive(548,this,this.port2x4_read,this.port2x5_read),t.io.register_read(550,this,this.port2x6_read),t.io.register_read(551,this,this.port2x7_read),t.io.register_read(552,this,this.port2x8_read),t.io.register_read(553,this,this.port2x9_read),t.io.register_read(554,this,this.port2xA_read),t.io.register_read(555,this,this.port2xB_read),t.io.register_read(556,this,this.port2xC_read),t.io.register_read(557,this,this.port2xD_read),t.io.register_read_consecutive(558,this,this.port2xE_read,this.port2xF_read),t.io.register_write_consecutive(544,this,this.port2x0_write,this.port2x1_write,this.port2x2_write,this.port2x3_write),t.io.register_write_consecutive(904,this,this.port2x0_write,this.port2x1_write),t.io.register_write_consecutive(548,this,this.port2x4_write,this.port2x5_write),t.io.register_write(550,this,this.port2x6_write),t.io.register_write(551,this,this.port2x7_write),t.io.register_write_consecutive(552,this,this.port2x8_write,this.port2x9_write),t.io.register_write(554,this,this.port2xA_write),t.io.register_write(555,this,this.port2xB_write),t.io.register_write(556,this,this.port2xC_write),t.io.register_write(557,this,this.port2xD_write),t.io.register_write(558,this,this.port2xE_write),t.io.register_write(559,this,this.port2xF_write),t.io.register_read_consecutive(816,this,this.port3x0_read,this.port3x1_read),t.io.register_write_consecutive(816,this,this.port3x0_write,this.port3x1_write),this.dma.on_unmask(this.dma_on_unmask,this),e.register("dac-request-data",(function(){this.dac_handle_request()}),this),e.register("speaker-has-initialized",(function(){this.mixer_reset()}),this),e.send("speaker-confirm-initialized"),this.dsp_reset()}function N(t,e,i){i||(i=G.prototype.dsp_default_handler);for(var s=0;s<t.length;s++)S[t[s]]=e,D[t[s]]=i}function F(t){for(var e=[],i=0;16>i;i++)e.push(t+i);return e}G.prototype.dsp_reset=function(){this.write_buffer.clear(),this.read_buffer.clear(),this.command=0,this.command_size=0,this.dummy_speaker_enabled=!1,this.test_register=0,this.dsp_signed=this.dsp_16bit=this.dsp_stereo=this.dsp_highspeed=!1,this.dac_buffers[0].clear(),this.dac_buffers[1].clear(),this.dma_channel=this.dma_irq=this.dma_bytes_block=this.dma_bytes_left=this.dma_bytes_count=this.dma_sample_count=0,this.dma_autoinit=!1,this.dma_buffer_uint8.fill(0),this.dma_paused=this.dma_waiting_transfer=!1,this.e2_value=170,this.e2_count=0,this.sampling_rate=22050,this.bytes_per_sample=1,this.lower_irq(1),this.irq_triggered.fill(0),this.asp_registers.fill(0),this.asp_registers[5]=1,this.asp_registers[9]=248},G.prototype.get_state=function(){var t=[];return t[2]=this.read_buffer_lastvalue,t[3]=this.command,t[4]=this.command_size,t[5]=this.mixer_current_address,t[6]=this.mixer_registers,t[7]=this.dummy_speaker_enabled,t[8]=this.test_register,t[9]=this.dsp_highspeed,t[10]=this.dsp_stereo,t[11]=this.dsp_16bit,t[12]=this.dsp_signed,t[15]=this.dma_sample_count,t[16]=this.dma_bytes_count,t[17]=this.dma_bytes_left,t[18]=this.dma_bytes_block,t[19]=this.dma_irq,t[20]=this.dma_channel,t[21]=this.dma_channel_8bit,t[22]=this.dma_channel_16bit,t[23]=this.dma_autoinit,t[24]=this.dma_buffer_uint8,t[25]=this.dma_waiting_transfer,t[26]=this.dma_paused,t[27]=this.sampling_rate,t[28]=this.bytes_per_sample,t[29]=this.e2_value,t[30]=this.e2_count,t[31]=this.asp_registers,t[33]=this.mpu_read_buffer_last_value,t[34]=this.irq,t[35]=this.irq_triggered,t},G.prototype.set_state=function(t){this.read_buffer_lastvalue=t[2],this.command=t[3],this.command_size=t[4],this.mixer_current_address=t[5],this.mixer_registers=t[6],this.mixer_full_update(),this.dummy_speaker_enabled=t[7],this.test_register=t[8],this.dsp_highspeed=t[9],this.dsp_stereo=t[10],this.dsp_16bit=t[11],this.dsp_signed=t[12],this.dma_sample_count=t[15],this.dma_bytes_count=t[16],this.dma_bytes_left=t[17],this.dma_bytes_block=t[18],this.dma_irq=t[19],this.dma_channel=t[20],this.dma_channel_8bit=t[21],this.dma_channel_16bit=t[22],this.dma_autoinit=t[23],this.dma_buffer_uint8=t[24],this.dma_waiting_transfer=t[25],this.dma_paused=t[26],this.sampling_rate=t[27],this.bytes_per_sample=t[28],this.e2_value=t[29],this.e2_count=t[30],this.asp_registers=t[31],this.mpu_read_buffer_last_value=t[33],this.irq=t[34],this.irq_triggered=t[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 u(this.dma_buffer),this.dma_paused?this.bus.send("dac-disable"):this.bus.send("dac-enable")},G.prototype.port2x0_read=function(){return _("220 read: fm music status port (unimplemented)",8388608),255},G.prototype.port2x1_read=function(){return _("221 read: fm music data port (write only)",8388608),255},G.prototype.port2x2_read=function(){return _("222 read: advanced fm music status port (unimplemented)",8388608),255},G.prototype.port2x3_read=function(){return _("223 read: advanced music data port (write only)",8388608),255},G.prototype.port2x4_read=function(){return _("224 read: mixer address port",8388608),this.mixer_current_address},G.prototype.port2x5_read=function(){return _("225 read: mixer data port",8388608),this.mixer_read(this.mixer_current_address)},G.prototype.port2x6_read=function(){return _("226 read: (write only)",8388608),255},G.prototype.port2x7_read=function(){return _("227 read: undocumented",8388608),255},G.prototype.port2x8_read=function(){return _("228 read: fm music status port (unimplemented)",8388608),255},G.prototype.port2x9_read=function(){return _("229 read: fm music data port (write only)",8388608),255},G.prototype.port2xA_read=function(){return _("22A read: read data",8388608),this.read_buffer.length&&(this.read_buffer_lastvalue=this.read_buffer.shift()),_(" <- "+this.read_buffer_lastvalue+" "+p(this.read_buffer_lastvalue)+" '"+String.fromCharCode(this.read_buffer_lastvalue)+"'",8388608),this.read_buffer_lastvalue},G.prototype.port2xB_read=function(){return _("22B read: undocumented",8388608),255},G.prototype.port2xC_read=function(){return _("22C read: write-buffer status",8388608),127},G.prototype.port2xD_read=function(){return _("22D read: undocumented",8388608),255},G.prototype.port2xE_read=function(){return _("22E read: read-buffer status / irq 8bit ack.",8388608),this.irq_triggered[1]&&this.lower_irq(1),(this.read_buffer.length&&!this.dsp_highspeed)<<7|127},G.prototype.port2xF_read=function(){return _("22F read: irq 16bit ack",8388608),this.lower_irq(2),0},G.prototype.port2x0_write=function(t){_("220 write: (unimplemented) fm register 0 address = "+p(t),8388608),this.fm_current_address0=0},G.prototype.port2x1_write=function(t){_("221 write: (unimplemented) fm register 0 data = "+p(t),8388608);var e=U[this.fm_current_address0];e||(e=this.fm_default_write),e.call(this,t,0,this.fm_current_address0)},G.prototype.port2x2_write=function(t){_("222 write: (unimplemented) fm register 1 address = "+p(t),8388608),this.fm_current_address1=0},G.prototype.port2x3_write=function(t){_("223 write: (unimplemented) fm register 1 data ="+p(t),8388608);var e=U[this.fm_current_address1];e||(e=this.fm_default_write),e.call(this,t,1,this.fm_current_address1)},G.prototype.port2x4_write=function(t){_("224 write: mixer address = "+p(t),8388608),this.mixer_current_address=t},G.prototype.port2x5_write=function(t){_("225 write: mixer data = "+p(t),8388608),this.mixer_write(this.mixer_current_address,t)},G.prototype.port2x6_write=function(t){_("226 write: reset = "+p(t),8388608),this.dsp_highspeed?(_(" -> exit highspeed",8388608),this.dsp_highspeed=!1):t&&(_(" -> reset",8388608),this.dsp_reset()),this.read_buffer.clear(),this.read_buffer.push(170)},G.prototype.port2x7_write=function(t){_("227 write: undocumented",8388608)},G.prototype.port2x8_write=function(t){_("228 write: fm music register port (unimplemented)",8388608)},G.prototype.port2x9_write=function(t){_("229 write: fm music data port (unimplemented)",8388608)},G.prototype.port2xA_write=function(t){_("22A write: dsp read data port (read only)",8388608)},G.prototype.port2xB_write=function(t){_("22B write: undocumented",8388608)},G.prototype.port2xC_write=function(t){_("22C write: write command/data",8388608),0===this.command?(_("22C write: command = "+p(t),8388608),this.command=t,this.write_buffer.clear(),this.command_size=S[t]):(_("22C write: data: "+p(t),8388608),this.write_buffer.push(t)),this.write_buffer.length>=this.command_size&&this.command_do()},G.prototype.port2xD_write=function(t){_("22D write: undocumented",8388608)},G.prototype.port2xE_write=function(t){_("22E write: dsp read buffer status (read only)",8388608)},G.prototype.port2xF_write=function(t){_("22F write: undocumented",8388608)},G.prototype.port3x0_read=function(){return _("330 read: mpu data",8388608),this.mpu_read_buffer.length&&(this.mpu_read_buffer_lastvalue=this.mpu_read_buffer.shift()),_(" <- "+p(this.mpu_read_buffer_lastvalue),8388608),this.mpu_read_buffer_lastvalue},G.prototype.port3x0_write=function(t){_("330 write: mpu data (unimplemented) : "+p(t),8388608)},G.prototype.port3x1_read=function(){return _("331 read: mpu status",8388608),0|128*!this.mpu_read_buffer.length},G.prototype.port3x1_write=function(t){_("331 write: mpu command: "+p(t),8388608),255==t&&(this.mpu_read_buffer.clear(),this.mpu_read_buffer.push(254))},G.prototype.command_do=function(){var t=D[this.command];t||(t=this.dsp_default_handler),t.call(this),this.command=0,this.command_size=0,this.write_buffer.clear()},G.prototype.dsp_default_handler=function(){_("Unhandled command: "+p(this.command),8388608)},N([14],2,(function(){this.asp_registers[this.write_buffer.shift()]=this.write_buffer.shift()})),N([15],1,(function(){this.read_buffer.clear(),this.read_buffer.push(this.asp_registers[this.write_buffer.shift()])})),N([16],1,(function(){var t=Z(this.write_buffer.shift(),127.5,-1);this.dac_buffers[0].push(t),this.dac_buffers[1].push(t),this.bus.send("dac-enable")})),N([20,21],2,(function(){this.dma_irq=1,this.dma_channel=this.dma_channel_8bit,this.dsp_highspeed=this.dsp_16bit=this.dsp_signed=this.dma_autoinit=!1,this.dma_transfer_size_set(),this.dma_transfer_start()})),N([22],2),N([23],2),N([28],0,(function(){this.dma_irq=1,this.dma_channel=this.dma_channel_8bit,this.dma_autoinit=!0,this.dsp_highspeed=this.dsp_16bit=this.dsp_signed=!1,this.dma_transfer_start()})),N([31],0),N([32],0,(function(){this.read_buffer.clear(),this.read_buffer.push(127)})),N([36],2),N([44],0),N([48],0),N([49],0),N([52],0),N([53],0),N([54],0),N([55],0),N([56],0),N([64],1,(function(){this.sampling_rate_change(1e6/(256-this.write_buffer.shift())/this.get_channel_count())})),N([65,66],2,(function(){this.sampling_rate_change(this.write_buffer.shift()<<8|this.write_buffer.shift())})),N([72],2,(function(){this.dma_transfer_size_set()})),N([116],2),N([117],2),N([118],2),N([119],2),N([125],0),N([127],0),N([128],2),N([144],0,(function(){this.dma_irq=1,this.dma_channel=this.dma_channel_8bit,this.dma_autoinit=!0,this.dsp_signed=!1,this.dsp_highspeed=!0,this.dsp_16bit=!1,this.dma_transfer_start()})),N([145],0),N([152],0),N([153],0),N([160],0),N([168],0),N(F(176),3,(function(){if(8&this.command)this.dsp_default_handler();else{var t=this.write_buffer.shift();this.dma_irq=2,this.dma_channel=this.dma_channel_16bit,this.dma_autoinit=!!(4&this.command),this.dsp_signed=!!(16&t),this.dsp_stereo=!!(32&t),this.dsp_16bit=!0,this.dma_transfer_size_set(),this.dma_transfer_start()}})),N(F(192),3,(function(){if(8&this.command)this.dsp_default_handler();else{var t=this.write_buffer.shift();this.dma_irq=1,this.dma_channel=this.dma_channel_8bit,this.dma_autoinit=!!(4&this.command),this.dsp_signed=!!(16&t),this.dsp_stereo=!!(32&t),this.dsp_16bit=!1,this.dma_transfer_size_set(),this.dma_transfer_start()}})),N([208],0,(function(){this.dma_paused=!0,this.bus.send("dac-disable")})),N([209],0,(function(){this.dummy_speaker_enabled=!0})),N([211],0,(function(){this.dummy_speaker_enabled=!1})),N([212],0,(function(){this.dma_paused=!1,this.bus.send("dac-enable")})),N([213],0,(function(){this.dma_paused=!0,this.bus.send("dac-disable")})),N([214],0,(function(){this.dma_paused=!1,this.bus.send("dac-enable")})),N([216],0,(function(){this.read_buffer.clear(),this.read_buffer.push(255*this.dummy_speaker_enabled)})),N([217,218],0,(function(){this.dma_autoinit=!1})),N([224],1,(function(){this.read_buffer.clear(),this.read_buffer.push(~this.write_buffer.shift())})),N([225],0,(function(){this.read_buffer.clear(),this.read_buffer.push(4),this.read_buffer.push(5)})),N([226],1),N([227],0,(function(){this.read_buffer.clear();for(var t=0;t<L.length;t++)this.read_buffer.push(L.charCodeAt(t));this.read_buffer.push(0)})),N([228],1,(function(){this.test_register=this.write_buffer.shift()})),N([232],0,(function(){this.read_buffer.clear(),this.read_buffer.push(this.test_register)})),N([242,243],0,(function(){this.raise_irq()}));var B=new Uint8Array(256);function V(t,e){e||(e=G.prototype.mixer_default_read),P[t]=e}function W(t,e){e||(e=G.prototype.mixer_default_write),z[t]=e}function H(t,e,i){M[t]=1,P[t]=function(){return 240&this.mixer_registers[e]|this.mixer_registers[i]>>>4},z[t]=function(s){this.mixer_registers[t]=s;var r=s<<4&240|15&this.mixer_registers[i];this.mixer_write(e,240&s|15&this.mixer_registers[e]),this.mixer_write(i,r)}}function X(t,e,i){P[t]=G.prototype.mixer_default_read,z[t]=function(s){this.mixer_registers[t]=s,this.bus.send("mixer-volume",[e,i,(s>>>2)-62])}}function j(t,e){e||(e=G.prototype.fm_default_write);for(var i=0;i<t.length;i++)U[t[i]]=e}function K(t,e){for(var i=[];t<=e;t++)i.push(t);return i}B[14]=255,B[15]=7,B[55]=56,N([249],1,(function(){var t=this.write_buffer.shift();_("dsp 0xf9: unknown function. input: "+t,8388608),this.read_buffer.clear(),this.read_buffer.push(B[t])})),G.prototype.mixer_read=function(t){var e=P[t];return e?e=e.call(this):(e=this.mixer_registers[t],_("unhandled mixer register read. addr:"+p(t)+" data:"+p(e),8388608)),e},G.prototype.mixer_write=function(t,e){var i=z[t];i?i.call(this,e):_("unhandled mixer register write. addr:"+p(t)+" data:"+p(e),8388608)},G.prototype.mixer_default_read=function(){return _("mixer register read. addr:"+p(this.mixer_current_address),8388608),this.mixer_registers[this.mixer_current_address]},G.prototype.mixer_default_write=function(t){_("mixer register write. addr:"+p(this.mixer_current_address)+" data:"+p(t),8388608),this.mixer_registers[this.mixer_current_address]=t},G.prototype.mixer_reset=function(){this.mixer_registers[4]=204,this.mixer_registers[34]=204,this.mixer_registers[38]=204,this.mixer_registers[40]=0,this.mixer_registers[46]=0,this.mixer_registers[10]=0,this.mixer_registers[48]=192,this.mixer_registers[49]=192,this.mixer_registers[50]=192,this.mixer_registers[51]=192,this.mixer_registers[52]=192,this.mixer_registers[53]=192,this.mixer_registers[54]=0,this.mixer_registers[55]=0,this.mixer_registers[56]=0,this.mixer_registers[57]=0,this.mixer_registers[59]=0,this.mixer_registers[60]=31,this.mixer_registers[61]=21,this.mixer_registers[62]=11,this.mixer_registers[63]=0,this.mixer_registers[64]=0,this.mixer_registers[65]=0,this.mixer_registers[66]=0,this.mixer_registers[67]=0,this.mixer_registers[68]=128,this.mixer_registers[69]=128,this.mixer_registers[70]=128,this.mixer_registers[71]=128,this.mixer_full_update()},G.prototype.mixer_full_update=function(){for(var t=1;t<this.mixer_registers.length;t++)M[t]||this.mixer_write(t,this.mixer_registers[t])},V(0,(function(){return this.mixer_reset(),0})),W(0),H(4,50,51),H(34,48,49),H(38,52,53),H(40,54,55),H(46,56,57),X(48,0,0),X(49,0,1),X(50,2,0),X(51,2,1),V(59),W(59,(function(t){this.mixer_registers[59]=t,this.bus.send("mixer-volume",[1,2,6*(t>>>6)-18])})),V(65),W(65,(function(t){this.mixer_registers[65]=t,this.bus.send("mixer-gain-left",6*(t>>>6))})),V(66),W(66,(function(t){this.mixer_registers[66]=t,this.bus.send("mixer-gain-right",6*(t>>>6))})),V(68),W(68,(function(t){this.mixer_registers[68]=t,t>>>=3,this.bus.send("mixer-treble-left",t-(16>t?14:16))})),V(69),W(69,(function(t){this.mixer_registers[69]=t,t>>>=3,this.bus.send("mixer-treble-right",t-(16>t?14:16))})),V(70),W(70,(function(t){this.mixer_registers[70]=t,t>>>=3,this.bus.send("mixer-bass-right",t-(16>t?14:16))})),V(71),W(71,(function(t){this.mixer_registers[71]=t,t>>>=3,this.bus.send("mixer-bass-right",t-(16>t?14:16))})),V(128,(function(){switch(this.irq){case 2:return 1;case 5:return 2;case 7:return 4;case 10:return 8;default:return 0}})),W(128,(function(t){1&t&&(this.irq=2),2&t&&(this.irq=5),4&t&&(this.irq=7),8&t&&(this.irq=10)})),V(129,(function(){var t=0;switch(this.dma_channel_8bit){case 0:t|=1;break;case 1:t|=2;break;case 3:t|=8}switch(this.dma_channel_16bit){case 5:t|=32;break;case 6:t|=64;break;case 7:t|=128}return t})),W(129,(function(t){1&t&&(this.dma_channel_8bit=0),2&t&&(this.dma_channel_8bit=1),8&t&&(this.dma_channel_8bit=3),32&t&&(this.dma_channel_16bit=5),64&t&&(this.dma_channel_16bit=6),128&t&&(this.dma_channel_16bit=7)})),V(130,(function(){for(var t=32,e=0;16>e;e++)t|=e*this.irq_triggered[e];return t})),G.prototype.fm_default_write=function(t,e,i){_("unhandled fm register write. addr:"+e+"|"+p(i)+" data:"+p(t),8388608)};var Y=new Uint8Array(32);function Q(t,e){return 18*t+Y[e]}function Z(t,e,i){return function(t,e,i){return(t<e)*e+(t>i)*i+(e<=t&&t<=i)*t}(t/e+i,-1,1)}Y[0]=0,Y[1]=1,Y[2]=2,Y[3]=3,Y[4]=4,Y[5]=5,Y[8]=6,Y[9]=7,Y[10]=8,Y[11]=9,Y[12]=10,Y[13]=11,Y[16]=12,Y[17]=13,Y[18]=14,Y[19]=15,Y[20]=16,Y[21]=17,j([1],(function(t,e,i){this.fm_waveform_select_enable[e]=1&t,this.fm_update_waveforms()})),j([2]),j([3]),j([4],(function(t,e,i){})),j([5],(function(t,e,i){0===e&&this.fm_default_write(t,e,i)})),j([8],(function(t,e,i){})),j(K(32,53),(function(t,e,i){Q(e,i-32)})),j(K(64,85),(function(t,e,i){Q(e,i-64)})),j(K(96,117),(function(t,e,i){Q(e,i-96)})),j(K(128,149),(function(t,e,i){Q(e,i-128)})),j(K(160,168),(function(t,e,i){})),j(K(176,184),(function(t,e,i){})),j([189],(function(t,e,i){})),j(K(192,200),(function(t,e,i){})),j(K(224,245),(function(t,e,i){Q(e,i-224)})),G.prototype.fm_update_waveforms=function(){},G.prototype.sampling_rate_change=function(t){this.sampling_rate=t,this.bus.send("dac-tell-sampling-rate",t)},G.prototype.get_channel_count=function(){return this.dsp_stereo?2:1},G.prototype.dma_transfer_size_set=function(){this.dma_sample_count=1+(this.write_buffer.shift()<<0)+(this.write_buffer.shift()<<8)},G.prototype.dma_transfer_start=function(){_("begin dma transfer",8388608),this.bytes_per_sample=1,this.dsp_16bit&&(this.bytes_per_sample*=2),this.dma_bytes_count=this.dma_sample_count*this.bytes_per_sample,this.dma_bytes_block=1024*this.bytes_per_sample,this.dma_bytes_block=Math.min(Math.max(this.dma_bytes_count>>2&-4,32),this.dma_bytes_block),this.dma_waiting_transfer=!0,this.dma.channel_mask[this.dma_channel]||this.dma_on_unmask(this.dma_channel)},G.prototype.dma_on_unmask=function(t){t===this.dma_channel&&this.dma_waiting_transfer&&(this.dma_waiting_transfer=!1,this.dma_bytes_left=this.dma_bytes_count,this.dma_paused=!1,this.bus.send("dac-enable"))},G.prototype.dma_transfer_next=function(){_("dma transfering next block",8388608);var t=Math.min(this.dma_bytes_left,this.dma_bytes_block),e=Math.floor(t/this.bytes_per_sample);this.dma.do_write(this.dma_syncbuffer,0,t,this.dma_channel,(i=>{_("dma block transfer "+(i?"unsuccessful":"successful"),8388608),i||(this.dma_to_dac(e),this.dma_bytes_left-=t,this.dma_bytes_left||(this.raise_irq(this.dma_irq),this.dma_autoinit&&(this.dma_bytes_left=this.dma_bytes_count)))}))},G.prototype.dma_to_dac=function(t){for(var e=this.dsp_16bit?32767.5:127.5,i=this.dsp_signed?0:-1,s=this.dsp_stereo?1:2,r=this.dsp_16bit?this.dsp_signed?this.dma_buffer_int16:this.dma_buffer_uint16:this.dsp_signed?this.dma_buffer_int8:this.dma_buffer_uint8,a=0,o=0;o<t;o++)for(var _=Z(r[o],e,i),n=0;n<s;n++)this.dac_buffers[a].push(_),a^=1;this.dac_send()},G.prototype.dac_handle_request=function(){!this.dma_bytes_left||this.dma_paused?this.dac_send():this.dma_transfer_next()},G.prototype.dac_send=function(){if(this.dac_buffers[0].length){var t=this.dac_buffers[0].shift_block(this.dac_buffers[0].length),e=this.dac_buffers[1].shift_block(this.dac_buffers[1].length);this.bus.send("dac-send-data",[t,e],[t.buffer,e.buffer])}},G.prototype.raise_irq=function(t){_("raise irq",8388608),this.irq_triggered[t]=1,this.cpu.device_raise_irq(this.irq)},G.prototype.lower_irq=function(t){_("lower irq",8388608),this.irq_triggered[t]=0,this.cpu.device_lower_irq(this.irq)};function J(t,e,i){switch(this.bus=i,this.cpu=t,this.ints=4,this.line_control=this.baud_rate=0,this.lsr=96,this.ier=this.fifo_control=0,this.iir=1,this.irq=this.scratch_register=this.modem_status=this.modem_control=0,this.input=new l(4096),this.current_line=[],e){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:_("Invalid serial port: "+p(e),16384),this.com=0,this.irq=4}this.bus.register("serial"+this.com+"-input",(function(t){this.data_received(t)}),this),(t=t.io).register_write(e,this,(function(t){this.write_data(t)}),(function(t){this.write_data(255&t),this.write_data(t>>8)})),t.register_write(1|e,this,(function(t){128&this.line_control?(this.baud_rate=255&this.baud_rate|t<<8,_("baud rate: "+p(this.baud_rate),16384)):(this.ier=15&t,_("interrupt enable: "+p(t),16384),this.CheckInterrupt())})),t.register_read(e,this,(function(){if(128&this.line_control)return 255&this.baud_rate;var t=this.input.shift();return _(-1===t?"Read input empty":"Read input: "+p(t),16384),0===this.input.length&&(this.lsr&=-2,this.ClearInterrupt(12)),t})),t.register_read(1|e,this,(function(){return 128&this.line_control?this.baud_rate>>8:15&this.ier})),t.register_read(2|e,this,(function(){var t=15&this.iir|192;return _("read interrupt identification: "+p(this.iir),16384),t})),t.register_write(2|e,this,(function(t){_("fifo control: "+p(t),16384),this.fifo_control=t})),t.register_read(3|e,this,(function(){return _("read line control: "+p(this.line_control),16384),this.line_control})),t.register_write(3|e,this,(function(t){_("line control: "+p(t),16384),this.line_control=t})),t.register_read(4|e,this,(function(){return this.modem_control})),t.register_write(4|e,this,(function(t){_("modem control: "+p(t),16384),this.modem_control=t})),t.register_read(5|e,this,(function(){return _("read line status: "+p(this.lsr),16384),this.lsr})),t.register_write(5|e,this,(function(t){_("Factory test write",16384)})),t.register_read(6|e,this,(function(){return _("read modem status: "+p(this.modem_status),16384),this.modem_status})),t.register_write(6|e,this,(function(t){_("Unkown register write (base+6)",16384)})),t.register_read(7|e,this,(function(){return this.scratch_register})),t.register_write(7|e,this,(function(t){this.scratch_register=t}))}J.prototype.get_state=function(){var t=[];return t[0]=this.ints,t[1]=this.baud_rate,t[2]=this.line_control,t[3]=this.lsr,t[4]=this.fifo_control,t[5]=this.ier,t[6]=this.iir,t[7]=this.modem_control,t[8]=this.modem_status,t[9]=this.scratch_register,t[10]=this.irq,t},J.prototype.set_state=function(t){this.ints=t[0],this.baud_rate=t[1],this.line_control=t[2],this.lsr=t[3],this.fifo_control=t[4],this.ier=t[5],this.iir=t[6],this.modem_control=t[7],this.modem_status=t[8],this.scratch_register=t[9],this.irq=t[10]},J.prototype.CheckInterrupt=function(){4096&this.ints&&1&this.ier?(this.iir=12,this.cpu.device_raise_irq(this.irq)):4&this.ints&&2&this.ier?(this.iir=2,this.cpu.device_raise_irq(this.irq)):1&this.ints&&8&this.ier?(this.iir=0,this.cpu.device_raise_irq(this.irq)):(this.iir=1,this.cpu.device_lower_irq(this.irq))},J.prototype.ThrowInterrupt=function(t){this.ints|=1<<t,this.CheckInterrupt()},J.prototype.ClearInterrupt=function(t){this.ints&=~(1<<t),this.CheckInterrupt()},J.prototype.data_received=function(t){_("input: "+p(t),16384),this.input.push(t),this.lsr|=1,this.ThrowInterrupt(12)},J.prototype.write_data=function(t){if(128&this.line_control)this.baud_rate=-256&this.baud_rate|t;else if(_("data: "+p(t),16384),this.ThrowInterrupt(2),255!==t){var e=String.fromCharCode(t);this.bus.send("serial"+this.com+"-output-char",e),this.current_line.push(t),"\n"===e&&(t=String.fromCharCode.apply("",this.current_line).trimRight().replace(/[\x00-\x08\x0b-\x1f\x7f\x80-\xff]/g,""),_("SERIAL: "+t),this.bus.send("serial"+this.com+"-output-line",String.fromCharCode.apply("",this.current_line)),this.current_line=[])}};var $=Uint32Array.from([655360,655360,720896,753664]),tt=Uint32Array.from([131072,65536,32768,32768]);function et(t,e,i){this.cpu=t,this.bus=e,this.vga_memory_size=i,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((()=>{e.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,14680064,57344,224,0,0,0,0,0,0,191,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,0,17,0,0,190,254,0,0,0,0,0,0,0,0,0,0,0,0],this.pci_id=144,this.pci_bars=[{size:i}],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,(i=t.io).register_write(960,this,this.port3C0_write),i.register_read(960,this,this.port3C0_read,this.port3C0_read16),i.register_read(961,this,this.port3C1_read),i.register_write(962,this,this.port3C2_write),i.register_write_consecutive(964,this,this.port3C4_write,this.port3C5_write),i.register_read(964,this,this.port3C4_read),i.register_read(965,this,this.port3C5_read),i.register_write_consecutive(974,this,this.port3CE_write,this.port3CF_write),i.register_read(974,this,this.port3CE_read),i.register_read(975,this,this.port3CF_read),i.register_write(967,this,this.port3C7_write),i.register_read(967,this,this.port3C7_read),i.register_write(968,this,this.port3C8_write),i.register_read(968,this,this.port3C8_read),i.register_write(969,this,this.port3C9_write),i.register_read(969,this,this.port3C9_read),i.register_read(972,this,this.port3CC_read),i.register_write_consecutive(980,this,this.port3D4_write,this.port3D5_write),i.register_read(980,this,this.port3D4_read),i.register_read(981,this,this.port3D5_read,(()=>(_("Warning: 16-bit read from 3D5",256),this.port3D5_read()))),i.register_read(970,this,(function(){return _("3CA read",256),0})),i.register_read(986,this,this.port3DA_read),i.register_read(954,this,this.port3DA_read),this.dispi_index=-1,this.dispi_enable_value=0,i.register_write(462,this,void 0,this.port1CE_write),i.register_write(463,this,void 0,this.port1CF_write),i.register_read(463,this,void 0,this.port1CF_read),void 0===this.vga_memory_size||this.vga_memory_size<262144?(this.vga_memory_size=262144,_("vga memory size rounded up to "+this.vga_memory_size,256)):65535&this.vga_memory_size&&(this.vga_memory_size|=65535,this.vga_memory_size++);let s=t.svga_allocate_memory(this.vga_memory_size);this.svga_memory=c.view(Uint8Array,t.wasm_memory,s,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,e.register("screen-fill-buffer",(function(){this.screen_fill_buffer()}),this),this.vga_memory=new Uint8Array(262144),this.plane0=new Uint8Array(this.vga_memory.buffer,0,65536),this.plane1=new Uint8Array(this.vga_memory.buffer,65536,65536),this.plane2=new Uint8Array(this.vga_memory.buffer,131072,65536),this.plane3=new Uint8Array(this.vga_memory.buffer,196608,65536),this.pixel_buffer=new Uint8Array(524288);var r=this;i.mmap_register(655360,131072,(function(t){return r.vga_memory_read(t)}),(function(t,e){r.vga_memory_write(t,e)})),t.devices.pci.register_device(this)}et.prototype.get_state=function(){var t=[];return t[0]=this.vga_memory_size,t[1]=this.cursor_address,t[2]=this.cursor_scanline_start,t[3]=this.cursor_scanline_end,t[4]=this.max_cols,t[5]=this.max_rows,t[6]=this.vga_memory,t[7]=this.dac_state,t[8]=this.start_address,t[9]=this.graphical_mode,t[10]=this.vga256_palette,t[11]=this.latch_dword,t[12]=this.color_compare,t[13]=this.color_dont_care,t[14]=this.miscellaneous_graphics_register,t[15]=this.svga_width,t[16]=this.svga_height,t[17]=this.crtc_mode,t[18]=this.svga_enabled,t[19]=this.svga_bpp,t[20]=this.svga_bank_offset,t[21]=this.svga_offset,t[22]=this.index_crtc,t[23]=this.dac_color_index_write,t[24]=this.dac_color_index_read,t[25]=this.dac_map,t[26]=this.sequencer_index,t[27]=this.plane_write_bm,t[28]=this.sequencer_memory_mode,t[29]=this.graphics_index,t[30]=this.plane_read,t[31]=this.planar_mode,t[32]=this.planar_rotate_reg,t[33]=this.planar_bitmap,t[34]=this.max_scan_line,t[35]=this.miscellaneous_output_register,t[36]=this.port_3DA_value,t[37]=this.dispi_index,t[38]=this.dispi_enable_value,t[39]=this.svga_memory,t[40]=this.graphical_mode_is_linear,t[41]=this.attribute_controller_index,t[42]=this.offset_register,t[43]=this.planar_setreset,t[44]=this.planar_setreset_enable,t[45]=this.start_address_latched,t[46]=this.crtc,t[47]=this.horizontal_display_enable_end,t[48]=this.horizontal_blank_start,t[49]=this.vertical_display_enable_end,t[50]=this.vertical_blank_start,t[51]=this.underline_location_register,t[52]=this.preset_row_scan,t[53]=this.offset_register,t[54]=this.palette_source,t[55]=this.attribute_mode,t[56]=this.color_plane_enable,t[57]=this.horizontal_panning,t[58]=this.color_select,t[59]=this.clocking_mode,t[60]=this.line_compare,t[61]=this.pixel_buffer,t},et.prototype.set_state=function(t){this.vga_memory_size=t[0],this.cursor_address=t[1],this.cursor_scanline_start=t[2],this.cursor_scanline_end=t[3],this.max_cols=t[4],this.max_rows=t[5],t[6]&&this.vga_memory.set(t[6]),this.dac_state=t[7],this.start_address=t[8],this.graphical_mode=t[9],this.vga256_palette=t[10],this.latch_dword=t[11],this.color_compare=t[12],this.color_dont_care=t[13],this.miscellaneous_graphics_register=t[14],this.svga_width=t[15],this.svga_height=t[16],this.crtc_mode=t[17],this.svga_enabled=t[18],this.svga_bpp=t[19],this.svga_bank_offset=t[20],this.svga_offset=t[21],this.index_crtc=t[22],this.dac_color_index_write=t[23],this.dac_color_index_read=t[24],this.dac_map=t[25],this.sequencer_index=t[26],this.plane_write_bm=t[27],this.sequencer_memory_mode=t[28],this.graphics_index=t[29],this.plane_read=t[30],this.planar_mode=t[31],this.planar_rotate_reg=t[32],this.planar_bitmap=t[33],this.max_scan_line=t[34],this.miscellaneous_output_register=t[35],this.port_3DA_value=t[36],this.dispi_index=t[37],this.dispi_enable_value=t[38],this.svga_memory.set(t[39]),this.graphical_mode_is_linear=t[40],this.attribute_controller_index=t[41],this.offset_register=t[42],this.planar_setreset=t[43],this.planar_setreset_enable=t[44],this.start_address_latched=t[45],this.crtc.set(t[46]),this.horizontal_display_enable_end=t[47],this.horizontal_blank_start=t[48],this.vertical_display_enable_end=t[49],this.vertical_blank_start=t[50],this.underline_location_register=t[51],this.preset_row_scan=t[52],this.offset_register=t[53],this.palette_source=t[54],this.attribute_mode=t[55],this.color_plane_enable=t[56],this.horizontal_panning=t[57],this.color_select=t[58],this.clocking_mode=t[59],this.line_compare=t[60],t[61]&&this.pixel_buffer.set(t[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()},et.prototype.vga_memory_read=function(t){if(this.svga_enabled&&this.graphical_mode_is_linear)return this.cpu.read8(3758096384+(t-655360|this.svga_bank_offset)|0);var e=this.miscellaneous_graphics_register>>2&3;return 0>(t-=$[e])||t>=tt[e]?(_("vga read outside memory space: addr:"+p(t),256),0):(this.latch_dword=this.plane0[t],this.latch_dword|=this.plane1[t]<<8,this.latch_dword|=this.plane2[t]<<16,this.latch_dword|=this.plane3[t]<<24,8&this.planar_mode?(e=255,1&this.color_dont_care&&(e&=this.plane0[t]^~(1&this.color_compare?255:0)),2&this.color_dont_care&&(e&=this.plane1[t]^~(2&this.color_compare?255:0)),4&this.color_dont_care&&(e&=this.plane2[t]^~(4&this.color_compare?255:0)),8&this.color_dont_care&&(e&=this.plane3[t]^~(8&this.color_compare?255:0)),e):(e=this.plane_read,this.graphical_mode?8&this.sequencer_memory_mode?(e=3&t,t&=-4):16&this.planar_mode&&(e=1&t,t&=-2):e=0,this.vga_memory[e<<16|t]))},et.prototype.vga_memory_write=function(t,e){if(this.svga_enabled&&this.graphical_mode&&this.graphical_mode_is_linear)this.cpu.write8(3758096384+(t-655360|this.svga_bank_offset)|0,e);else{var i=this.miscellaneous_graphics_register>>2&3;0>(t-=$[i])||t>=tt[i]?_("vga write outside memory space: addr:"+p(t)+", value:"+p(e),256):this.graphical_mode?this.vga_memory_write_graphical(t,e):3&this.plane_write_bm&&this.vga_memory_write_text_mode(t,e)}},et.prototype.vga_memory_write_graphical=function(t,e){var i=3&this.planar_mode,s=this.apply_feed(this.planar_bitmap),r=this.apply_expand(this.planar_setreset),a=this.apply_expand(this.planar_setreset_enable);switch(i){case 0:e=this.apply_rotate(e);var o=this.apply_feed(e);o=this.apply_setreset(o,a),o=this.apply_logical(o,this.latch_dword),o=this.apply_bitmask(o,s);break;case 1:o=this.latch_dword;break;case 2:o=this.apply_expand(e),o=this.apply_logical(o,this.latch_dword),o=this.apply_bitmask(o,s);break;case 3:e=this.apply_rotate(e),s&=this.apply_feed(e),o=this.apply_bitmask(r,s)}switch(e=15,12&this.sequencer_memory_mode){case 0:e=5<<(1&t),t&=-2;break;case 8:case 12:e=1<<(3&t),t&=-4}1&(e&=this.plane_write_bm)&&(this.plane0[t]=o>>0&255),2&e&&(this.plane1[t]=o>>8&255),4&e&&(this.plane2[t]=o>>16&255),8&e&&(this.plane3[t]=o>>24&255),t=this.vga_addr_to_pixel(t),this.partial_replot(t,t+7)},et.prototype.apply_feed=function(t){return t|t<<8|t<<16|t<<24},et.prototype.apply_expand=function(t){return(1&t?255:0)|(2&t?255:0)<<8|(4&t?255:0)<<16|(8&t?255:0)<<24},et.prototype.apply_rotate=function(t){return(t|t<<8)>>>(7&this.planar_rotate_reg)&255},et.prototype.apply_setreset=function(t,e){var i=this.apply_expand(this.planar_setreset);return(t|e&i)&(~e|i)},et.prototype.apply_logical=function(t,e){switch(24&this.planar_rotate_reg){case 8:return t&e;case 16:return t|e;case 24:return t^e}return t},et.prototype.apply_bitmask=function(t,e){return e&t|~e&this.latch_dword},et.prototype.text_mode_redraw=function(){for(var t,e,i=this.start_address<<1,s=0;s<this.max_rows;s++)for(var r=0;r<this.max_cols;r++)t=this.vga_memory[i],e=this.vga_memory[1|i],this.bus.send("screen-put-char",[s,r,t,this.vga256_palette[e>>4&15],this.vga256_palette[15&e]]),i+=2},et.prototype.vga_memory_write_text_mode=function(t,e){var i=(t>>1)-this.start_address,s=i/this.max_cols|0;if(i%=this.max_cols,1&t)var r=e,a=this.vga_memory[-2&t];else a=e,r=this.vga_memory[1|t];this.bus.send("screen-put-char",[s,i,a,this.vga256_palette[r>>4&15],this.vga256_palette[15&r]]),this.vga_memory[t]=e},et.prototype.update_cursor=function(){var t=(this.cursor_address-this.start_address)/this.max_cols|0,e=(this.cursor_address-this.start_address)%this.max_cols;t=Math.min(this.max_rows-1,t),this.bus.send("screen-update-cursor",[t,e])},et.prototype.complete_redraw=function(){_("complete redraw",256),this.graphical_mode?this.svga_enabled?this.cpu.svga_mark_dirty():(this.diff_addr_min=0,this.diff_addr_max=524288):this.text_mode_redraw()},et.prototype.complete_replot=function(){_("complete replot",256),this.graphical_mode&&!this.svga_enabled&&(this.diff_plot_min=0,this.diff_plot_max=524288,this.complete_redraw())},et.prototype.partial_redraw=function(t,e){t<this.diff_addr_min&&(this.diff_addr_min=t),e>this.diff_addr_max&&(this.diff_addr_max=e)},et.prototype.partial_replot=function(t,e){t<this.diff_plot_min&&(this.diff_plot_min=t),e>this.diff_plot_max&&(this.diff_plot_max=e),this.partial_redraw(t,e)},et.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},et.prototype.destroy=function(){},et.prototype.vga_bytes_per_line=function(){var t=this.offset_register<<2;return 64&this.underline_location_register?t<<=1:64&this.crtc_mode&&(t>>>=1),t},et.prototype.vga_addr_shift_count=function(){var t=128+(~this.underline_location_register&this.crtc_mode&64);return t-=64&this.underline_location_register,(t-=64&this.attribute_mode)>>>6},et.prototype.vga_addr_to_pixel=function(t){var e=this.vga_addr_shift_count();if(3&~this.crtc_mode){var i=t-this.start_address;i&=this.crtc_mode<<13|-24577;var s=(i<<=e)/this.virtual_width|0;switch(i%=this.virtual_width,3&this.crtc_mode){case 2:s=s<<1|t>>13&1;break;case 1:s=s<<1|t>>14&1;break;case 0:s=s<<2|t>>13&3}return s*this.virtual_width+i+(this.start_address<<e)}return t<<e},et.prototype.scan_line_to_screen_row=function(t){return 128&this.max_scan_line&&(t>>>=1),t=Math.ceil(t/(1+(31&this.max_scan_line))),1&this.crtc_mode||(t<<=1),2&this.crtc_mode||(t<<=1),t},et.prototype.set_size_text=function(t,e){this.max_cols=t,this.max_rows=e,this.bus.send("screen-set-size-text",[t,e])},et.prototype.set_size_graphical=function(t,e,i,s,r){if(!this.stats.is_graphical||this.stats.bpp!==i||this.screen_width!==t||this.screen_height!==e||this.virtual_width!==s||this.virtual_height!==r){if(this.screen_width=t,this.screen_height=e,this.virtual_width=s,this.virtual_height=r,this.stats.bpp=i,this.stats.is_graphical=!0,this.stats.res_x=t,this.stats.res_y=e,"undefined"!=typeof ImageData){let i=t*e,s=this.cpu.svga_allocate_dest_buffer(i);this.dest_buffet_offset=s,this.image_data=new ImageData(new Uint8ClampedArray(this.cpu.wasm_memory.buffer,s,4*i),t,e),this.cpu.svga_mark_dirty()}this.bus.send("screen-set-size-graphical",[t,e,s,r,i])}},et.prototype.update_vga_size=function(){if(!this.svga_enabled){var t=Math.min(1+this.horizontal_display_enable_end,this.horizontal_blank_start),e=Math.min(1+this.vertical_display_enable_end,this.vertical_blank_start);if(t&&e)if(this.graphical_mode){t<<=3;var i=this.offset_register<<4;64&this.attribute_mode&&(t>>>=1,i>>>=1),e=this.scan_line_to_screen_row(e);var s=Math.ceil(tt[0]/this.vga_bytes_per_line());this.set_size_graphical(t,e,8,i,s),this.update_vertical_retrace(),this.update_layers()}else 128&this.max_scan_line&&(e>>>=1),i=e/(1+(31&this.max_scan_line))|0,t&&i&&this.set_size_text(t,i)}},et.prototype.update_layers=function(){if(this.graphical_mode||this.text_mode_redraw(),this.svga_enabled)this.layers=[];else if(this.virtual_width&&this.screen_width)if(!this.palette_source||32&this.clocking_mode)this.layers=[],this.bus.send("screen-clear");else{var t=this.start_address_latched,e=this.horizontal_panning;64&this.attribute_mode&&(e>>>=1);var i=this.preset_row_scan>>5&3,s=this.vga_addr_to_pixel(t+i);t=s/this.virtual_width|0;var r=s%this.virtual_width+e;s=this.scan_line_to_screen_row(1+this.line_compare),s=Math.min(s,this.screen_height);var a=this.screen_height-s;this.layers=[],r=-r;for(var o=0;r<this.screen_width;r+=this.virtual_width,o++)this.layers.push({image_data:this.image_data,screen_x:r,screen_y:0,buffer_x:0,buffer_y:t+o,buffer_width:this.virtual_width,buffer_height:s});for(t=0,32&this.attribute_mode||(t=this.vga_addr_to_pixel(i)+e),r=-t,o=0;r<this.screen_width;r+=this.virtual_width,o++)this.layers.push({image_data:this.image_data,screen_x:r,screen_y:s,buffer_x:0,buffer_y:o,buffer_width:this.virtual_width,buffer_height:a})}},et.prototype.update_vertical_retrace=function(){this.port_3DA_value|=8,this.start_address_latched!==this.start_address&&(this.start_address_latched=this.start_address,this.update_layers())},et.prototype.update_cursor_scanline=function(){this.bus.send("screen-update-cursor-scanline",[this.cursor_scanline_start,this.cursor_scanline_end])},et.prototype.port3C0_write=function(t){if(-1===this.attribute_controller_index)_("attribute controller index register: "+p(t),256),this.attribute_controller_index=31&t,_("attribute actual index: "+p(this.attribute_controller_index),256),this.palette_source!==(32&t)&&(this.palette_source=32&t,this.update_layers());else{if(16>this.attribute_controller_index)_("internal palette: "+p(this.attribute_controller_index)+" -> "+p(t),256),this.dac_map[this.attribute_controller_index]=t,64&this.attribute_mode||this.complete_redraw();else switch(this.attribute_controller_index){case 16:if(_("3C0 / attribute mode control: "+p(t),256),this.attribute_mode!==t){var e=this.attribute_mode;this.attribute_mode=t;var i=0<(1&t);this.svga_enabled||this.graphical_mode===i||(this.graphical_mode=i,this.bus.send("screen-set-mode",this.graphical_mode)),64&(e^t)&&this.complete_replot(),this.update_vga_size(),this.complete_redraw()}break;case 18:_("3C0 / color plane enable: "+p(t),256),this.color_plane_enable!==t&&(this.color_plane_enable=t,this.complete_redraw());break;case 19:_("3C0 / horizontal panning: "+p(t),256),this.horizontal_panning!==t&&(this.horizontal_panning=15&t,this.update_layers());break;case 20:_("3C0 / color select: "+p(t),256),this.color_select!==t&&(this.color_select=t,this.complete_redraw());break;default:_("3C0 / attribute controller write "+p(this.attribute_controller_index)+": "+p(t),256)}this.attribute_controller_index=-1}},et.prototype.port3C0_read=function(){return _("3C0 read",256),this.attribute_controller_index|this.palette_source},et.prototype.port3C0_read16=function(){return _("3C0 read16",256),255&this.port3C0_read()|this.port3C1_read()<<8&65280},et.prototype.port3C1_read=function(){if(16>this.attribute_controller_index)return _("3C1 / internal palette read: "+p(this.attribute_controller_index)+" -> "+p(this.dac_map[this.attribute_controller_index]),256),255&this.dac_map[this.attribute_controller_index];switch(this.attribute_controller_index){case 16:return _("3C1 / attribute mode read: "+p(this.attribute_mode),256),this.attribute_mode;case 18:return _("3C1 / color plane enable read: "+p(this.color_plane_enable),256),this.color_plane_enable;case 19:return _("3C1 / horizontal panning read: "+p(this.horizontal_panning),256),this.horizontal_panning;case 20:return _("3C1 / color select read: "+p(this.color_select),256),this.color_select;default:_("3C1 / attribute controller read "+p(this.attribute_controller_index),256)}return 255},et.prototype.port3C2_write=function(t){_("3C2 / miscellaneous output register = "+p(t),256),this.miscellaneous_output_register=t},et.prototype.port3C4_write=function(t){this.sequencer_index=t},et.prototype.port3C4_read=function(){return this.sequencer_index},et.prototype.port3C5_write=function(t){switch(this.sequencer_index){case 1:_("clocking mode: "+p(t),256);var e=this.clocking_mode;this.clocking_mode=t,32&(e^t)&&this.update_layers();break;case 2:_("plane write mask: "+p(t),256),this.plane_write_bm=t;break;case 4:_("sequencer memory mode: "+p(t),256),this.sequencer_memory_mode=t;break;default:_("3C5 / sequencer write "+p(this.sequencer_index)+": "+p(t),256)}},et.prototype.port3C5_read=function(){switch(_("3C5 / sequencer read "+p(this.sequencer_index),256),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},et.prototype.port3C7_write=function(t){_("3C7 write: "+p(t),256),this.dac_color_index_read=3*t,this.dac_state&=0},et.prototype.port3C7_read=function(){return this.dac_state},et.prototype.port3C8_write=function(t){this.dac_color_index_write=3*t,this.dac_state|=3},et.prototype.port3C8_read=function(){return this.dac_color_index_write/3&255},et.prototype.port3C9_write=function(t){var e=this.dac_color_index_write/3|0,i=this.dac_color_index_write%3,s=this.vga256_palette[e];t=255*(63&t)/63|0,0===i?s=-16711681&s|t<<16:1===i?s=-65281&s|t<<8:(s=-256&s|t,_("dac set color, index="+p(e)+" value="+p(s),256)),this.vga256_palette[e]!==s&&(this.vga256_palette[e]=s,this.complete_redraw()),this.dac_color_index_write++},et.prototype.port3C9_read=function(){_("3C9 read",256);var t=this.dac_color_index_read%3,e=this.vga256_palette[this.dac_color_index_read/3|0];return this.dac_color_index_read++,(e>>8*(2-t)&255)/255*63|0},et.prototype.port3CC_read=function(){return _("3CC read",256),this.miscellaneous_output_register},et.prototype.port3CE_write=function(t){this.graphics_index=t},et.prototype.port3CE_read=function(){return this.graphics_index},et.prototype.port3CF_write=function(t){switch(this.graphics_index){case 0:this.planar_setreset=t,_("plane set/reset: "+p(t),256);break;case 1:this.planar_setreset_enable=t,_("plane set/reset enable: "+p(t),256);break;case 2:this.color_compare=t,_("color compare: "+p(t),256);break;case 3:this.planar_rotate_reg=t,_("plane rotate: "+p(t),256);break;case 4:this.plane_read=t,_("plane read: "+p(t),256);break;case 5:var e=this.planar_mode;this.planar_mode=t,_("planar mode: "+p(t),256),96&(e^t)&&this.complete_replot();break;case 6:_("miscellaneous graphics register: "+p(t),256),this.miscellaneous_graphics_register!==t&&(this.miscellaneous_graphics_register=t,this.update_vga_size());break;case 7:this.color_dont_care=t,_("color don't care: "+p(t),256);break;case 8:this.planar_bitmap=t,_("planar bitmap: "+p(t),256);break;default:_("3CF / graphics write "+p(this.graphics_index)+": "+p(t),256)}},et.prototype.port3CF_read=function(){switch(_("3CF / graphics read "+p(this.graphics_index),256),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},et.prototype.port3D4_write=function(t){_("3D4 / crtc index: "+t,256),this.index_crtc=t},et.prototype.port3D4_read=function(){return _("3D4 read / crtc index: "+this.index_crtc,256),this.index_crtc},et.prototype.port3D5_write=function(t){switch(this.index_crtc){case 1:_("3D5 / hdisp enable end write: "+p(t),256),this.horizontal_display_enable_end!==t&&(this.horizontal_display_enable_end=t,this.update_vga_size());break;case 2:this.horizontal_blank_start!==t&&(this.horizontal_blank_start=t,this.update_vga_size());break;case 7:_("3D5 / overflow register write: "+p(t),256);var e=this.vertical_display_enable_end;this.vertical_display_enable_end&=255,this.vertical_display_enable_end=this.vertical_display_enable_end|t<<3&512|t<<7&256,e!=this.vertical_display_enable_end&&this.update_vga_size(),this.line_compare=767&this.line_compare|t<<4&256,e=this.vertical_blank_start,this.vertical_blank_start=767&this.vertical_blank_start|t<<5&256,e!==this.vertical_blank_start&&this.update_vga_size(),this.update_layers();break;case 8:_("3D5 / preset row scan write: "+p(t),256),this.preset_row_scan=t,this.update_layers();break;case 9:_("3D5 / max scan line write: "+p(t),256),this.max_scan_line=t,this.line_compare=511&this.line_compare|t<<3&512,e=this.vertical_blank_start,this.vertical_blank_start=511&this.vertical_blank_start|t<<4&512,e!==this.vertical_blank_start&&this.update_vga_size(),this.update_layers();break;case 10:_("3D5 / cursor scanline start write: "+p(t),256),this.cursor_scanline_start=t,this.update_cursor_scanline();break;case 11:_("3D5 / cursor scanline end write: "+p(t),256),this.cursor_scanline_end=t,this.update_cursor_scanline();break;case 12:(this.start_address>>8&255)!==t&&(this.start_address=255&this.start_address|t<<8,this.update_layers(),3&~this.crtc_mode&&this.complete_replot()),_("3D5 / start addr hi write: "+p(t)+" -> "+p(this.start_address,4),256);break;case 13:(255&this.start_address)!==t&&(this.start_address=65280&this.start_address|t,this.update_layers(),3&~this.crtc_mode&&this.complete_replot()),_("3D5 / start addr lo write: "+p(t)+" -> "+p(this.start_address,4),256);break;case 14:_("3D5 / cursor address hi write: "+p(t),256),this.cursor_address=255&this.cursor_address|t<<8,this.update_cursor();break;case 15:_("3D5 / cursor address lo write: "+p(t),256),this.cursor_address=65280&this.cursor_address|t,this.update_cursor();break;case 18:_("3D5 / vdisp enable end write: "+p(t),256),(255&this.vertical_display_enable_end)!==t&&(this.vertical_display_enable_end=768&this.vertical_display_enable_end|t,this.update_vga_size());break;case 19:_("3D5 / offset register write: "+p(t),256),this.offset_register!==t&&(this.offset_register=t,this.update_vga_size(),3&~this.crtc_mode&&this.complete_replot());break;case 20:_("3D5 / underline location write: "+p(t),256),this.underline_location_register!==t&&(e=this.underline_location_register,this.underline_location_register=t,this.update_vga_size(),64&(e^t)&&this.complete_replot());break;case 21:_("3D5 / vertical blank start write: "+p(t),256),(255&this.vertical_blank_start)!==t&&(this.vertical_blank_start=768&this.vertical_blank_start|t,this.update_vga_size());break;case 23:_("3D5 / crtc mode write: "+p(t),256),this.crtc_mode!==t&&(e=this.crtc_mode,this.crtc_mode=t,this.update_vga_size(),67&(e^t)&&this.complete_replot());break;case 24:_("3D5 / line compare write: "+p(t),256),this.line_compare=768&this.line_compare|t,this.update_layers();break;default:this.index_crtc<this.crtc.length&&(this.crtc[this.index_crtc]=t),_("3D5 / CRTC write "+p(this.index_crtc)+": "+p(t),256)}},et.prototype.port3D5_read=function(){switch(_("3D5 read "+p(this.index_crtc),256),this.index_crtc){case 1:return this.horizontal_display_enable_end;case 2:return this.horizontal_blank_start;case 7:return this.vertical_display_enable_end>>7&2|this.vertical_blank_start>>5&8|this.line_compare>>4&16|this.vertical_display_enable_end>>3&64;case 8:return this.preset_row_scan;case 9:return this.max_scan_line;case 10:return this.cursor_scanline_start;case 11:return this.cursor_scanline_end;case 12:return 255&this.start_address;case 13:return this.start_address>>8;case 14:return this.cursor_address>>8;case 15:return 255&this.cursor_address;case 18:return 255&this.vertical_display_enable_end;case 19:return this.offset_register;case 20:return this.underline_location_register;case 21:return 255&this.vertical_blank_start;case 23:return this.crtc_mode;case 24:return 255&this.line_compare}return this.index_crtc<this.crtc.length?this.crtc[this.index_crtc]:0},et.prototype.port3DA_read=function(){_("3DA read - status 1 and clear attr index",256);var t=this.port_3DA_value;return this.graphical_mode?(this.port_3DA_value^=1,this.port_3DA_value&=1):(1&this.port_3DA_value&&(this.port_3DA_value^=8),this.port_3DA_value^=1),this.attribute_controller_index=-1,t},et.prototype.port1CE_write=function(t){this.dispi_index=t},et.prototype.port1CF_write=function(t){switch(_("1CF / dispi write "+p(this.dispi_index)+": "+p(t),256),this.dispi_index){case 1:this.svga_width=t,this.svga_width>2560&&(_("svga_width reduced from "+this.svga_width+" to 2560",256),this.svga_width=2560);break;case 2:this.svga_height=t,this.svga_height>1600&&(_("svga_height reduced from "+this.svga_height+" to 1600",256),this.svga_height=1600);break;case 3:this.svga_bpp=t;break;case 4:this.svga_enabled=1==(1&t),this.dispi_enable_value=t;break;case 5:_("SVGA bank offset: "+p(t<<16)),this.svga_bank_offset=t<<16;break;case 9:let e=t*this.svga_width;_("SVGA offset: "+p(e)+" y="+p(t),256),this.svga_offset!==e&&(this.svga_offset=e,this.complete_redraw())}!this.svga_enabled||this.svga_width&&this.svga_height||(_("SVGA: disabled because of invalid width/height: "+this.svga_width+"x"+this.svga_height,256),this.svga_enabled=!1),this.svga_bpp,4===this.svga_bpp||8===this.svga_bpp||15===this.svga_bpp||16===this.svga_bpp||24===this.svga_bpp||this.svga_bpp,this.svga_bpp,_("SVGA: enabled="+this.svga_enabled+", "+this.svga_width+"x"+this.svga_height+"x"+this.svga_bpp,256),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()},et.prototype.port1CF_read=function(){return _("1CF / dispi read "+p(this.dispi_index),256),this.svga_register_read(this.dispi_index)},et.prototype.svga_register_read=function(t){switch(t){case 0:return 45248;case 1:return 2&this.dispi_enable_value?2560:this.svga_width;case 2:return 2&this.dispi_enable_value?1600:this.svga_height;case 3:return 2&this.dispi_enable_value?32:this.svga_bpp;case 4:return this.dispi_enable_value;case 5:return this.svga_bank_offset>>>16;case 6:return this.screen_width?this.screen_width:1;case 8:return 0;case 10:return this.vga_memory_size/65536|0}return 255},et.prototype.vga_replot=function(){for(var t=-16&this.diff_plot_min,e=Math.min(15|this.diff_plot_max,524287),i=this.vga_addr_shift_count(),s=3&~this.crtc_mode,r=96&this.planar_mode,a=64&this.attribute_mode;t<=e;){var o=t>>>i;if(s){var _=t/this.virtual_width|0,n=t-this.virtual_width*_;switch(s){case 1:o=(1&_)<<13,_>>>=1;break;case 2:o=(1&_)<<14,_>>>=1;break;case 3:o=(3&_)<<13,_>>>=2}o|=(_*this.virtual_width+n>>>i)+this.start_address}_=this.plane0[o],n=this.plane1[o];var h=this.plane2[o],d=this.plane3[o];switch(o=new Uint8Array(8),r){case 0:_<<=0,n<<=1,h<<=2,d<<=3;for(var c=7;0<=c;c--)o[7-c]=_>>c&1|n>>c&2|h>>c&4|d>>c&8;break;case 32:o[0]=_>>6&3|h>>4&12,o[1]=_>>4&3|h>>2&12,o[2]=_>>2&3|h>>0&12,o[3]=_>>0&3|h<<2&12,o[4]=n>>6&3|d>>4&12,o[5]=n>>4&3|d>>2&12,o[6]=n>>2&3|d>>0&12,o[7]=n>>0&3|d<<2&12;break;case 64:case 96:o[0]=_>>4&15,o[1]=_>>0&15,o[2]=n>>4&15,o[3]=n>>0&15,o[4]=h>>4&15,o[5]=h>>0&15,o[6]=d>>4&15,o[7]=d>>0&15}if(a)for(_=c=0;4>c;c++,t++,_+=2)this.pixel_buffer[t]=o[_]<<4|o[_+1];else for(c=0;8>c;c++,t++)this.pixel_buffer[t]=o[c]}},et.prototype.vga_redraw=function(){var t=this.diff_addr_min,e=Math.min(this.diff_addr_max,524287);let i=new Int32Array(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,this.screen_width*this.screen_height);var s=255,r=0;if(128&this.attribute_mode&&(s&=207,r|=this.color_select<<4&48),64&this.attribute_mode)for(;t<=e;t++){var a=this.pixel_buffer[t]&s|r;a=this.vga256_palette[a],i[t]=65280&a|a<<16|a>>16|4278190080}else for(s&=63,r|=this.color_select<<4&192;t<=e;t++)a=this.dac_map[this.pixel_buffer[t]&this.color_plane_enable]&s|r,a=this.vga256_palette[a],i[t]=65280&a|a<<16|a>>16|4278190080},et.prototype.screen_fill_buffer=function(){if(this.graphical_mode){if(0===this.image_data.data.byteLength){var t=new Uint8ClampedArray(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,4*this.screen_width*this.screen_height);this.image_data=new ImageData(t,this.screen_width,this.screen_height),this.update_layers()}if(this.svga_enabled){t=0;let s=this.svga_height;if(8===this.svga_bpp){let t=new Int32Array(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,this.screen_width*this.screen_height),s=new Uint8Array(this.cpu.wasm_memory.buffer,this.svga_memory.byteOffset,this.vga_memory_size);for(var e=0;e<t.length;e++){var i=this.vga256_palette[s[e]];t[e]=65280&i|i<<16|i>>16|4278190080}}else this.cpu.svga_fill_pixel_buffer(this.svga_bpp,this.svga_offset),e=15===this.svga_bpp?2:this.svga_bpp/8,t=((this.cpu.svga_dirty_bitmap_min_offset[0]/e|0)-this.svga_offset)/this.svga_width,s=((this.cpu.svga_dirty_bitmap_max_offset[0]/e|0)-this.svga_offset)/this.svga_width+1;t<s&&(t=Math.max(t,0),s=Math.min(s,this.svga_height),this.bus.send("screen-fill-buffer-end",[{image_data:this.image_data,screen_x:0,screen_y:t,buffer_x:0,buffer_y:t,buffer_width:this.svga_width,buffer_height:s-t}]))}else this.vga_replot(),this.vga_redraw(),this.bus.send("screen-fill-buffer-end",this.layers);this.reset_diffs()}this.update_vertical_retrace()};function it(t,e,i){this.next_tick_immediately=i,this.wm=e,this.wasm_patch(),this.create_jit_imports(),this.wasm_memory=e=this.wm.exports.memory,this.memory_size=c.view(Uint32Array,e,812,1),this.mem8=new Uint8Array(0),this.mem32s=new Int32Array(this.mem8.buffer),this.segment_is_null=c.view(Uint8Array,e,724,8),this.segment_offsets=c.view(Int32Array,e,736,8),this.segment_limits=c.view(Uint32Array,e,768,8),this.protected_mode=c.view(Int32Array,e,800,1),this.idtr_size=c.view(Int32Array,e,564,1),this.idtr_offset=c.view(Int32Array,e,568,1),this.gdtr_size=c.view(Int32Array,e,572,1),this.gdtr_offset=c.view(Int32Array,e,576,1),this.tss_size_32=c.view(Int32Array,e,1128,1),this.page_fault=c.view(Uint32Array,e,540,8),this.cr=c.view(Int32Array,e,580,8),this.cpl=c.view(Uint8Array,e,612,1),this.is_32=c.view(Int32Array,e,804,1),this.stack_size_32=c.view(Int32Array,e,808,1),this.in_hlt=c.view(Uint8Array,e,616,1),this.last_virt_eip=c.view(Int32Array,e,620,1),this.eip_phys=c.view(Int32Array,e,624,1),this.sysenter_cs=c.view(Int32Array,e,636,1),this.sysenter_esp=c.view(Int32Array,e,640,1),this.sysenter_eip=c.view(Int32Array,e,644,1),this.prefixes=c.view(Int32Array,e,648,1),this.flags=c.view(Int32Array,e,120,1),this.flags_changed=c.view(Int32Array,e,116,1),this.last_op1=c.view(Int32Array,e,96,1),this.last_op_size=c.view(Int32Array,e,104,1),this.last_result=c.view(Int32Array,e,112,1),this.current_tsc=c.view(Uint32Array,e,960,2),this.devices={},this.instruction_pointer=c.view(Int32Array,e,556,1),this.previous_ip=c.view(Int32Array,e,560,1),this.apic_enabled=c.view(Uint8Array,e,548,1),this.acpi_enabled=c.view(Uint8Array,e,552,1),this.memory_map_read8=[],this.memory_map_write8=[],this.memory_map_read32=[],this.memory_map_write32=[],this.bios={main:null,vga:null},this.instruction_counter=c.view(Uint32Array,e,664,1),this.reg32=c.view(Int32Array,e,64,8),this.fpu_st=c.view(Int32Array,e,1152,32),this.fpu_stack_empty=c.view(Uint8Array,e,816,1),this.fpu_stack_empty[0]=255,this.fpu_stack_ptr=c.view(Uint8Array,e,1032,1),this.fpu_stack_ptr[0]=0,this.fpu_control_word=c.view(Uint16Array,e,1036,1),this.fpu_control_word[0]=895,this.fpu_status_word=c.view(Uint16Array,e,1040,1),this.fpu_status_word[0]=0,this.fpu_ip=c.view(Int32Array,e,1048,1),this.fpu_ip[0]=0,this.fpu_ip_selector=c.view(Int32Array,e,1052,1),this.fpu_ip_selector[0]=0,this.fpu_opcode=c.view(Int32Array,e,1044,1),this.fpu_opcode[0]=0,this.fpu_dp=c.view(Int32Array,e,1056,1),this.fpu_dp[0]=0,this.fpu_dp_selector=c.view(Int32Array,e,1060,1),this.fpu_dp_selector[0]=0,this.reg_xmm32s=c.view(Int32Array,e,832,32),this.mxcsr=c.view(Int32Array,e,824,1),this.sreg=c.view(Uint16Array,e,668,8),this.dreg=c.view(Int32Array,e,684,8),this.reg_pdpte=c.view(Int32Array,e,968,8),this.svga_dirty_bitmap_min_offset=c.view(Uint32Array,e,716,1),this.svga_dirty_bitmap_max_offset=c.view(Uint32Array,e,720,1),this.fw_value=[],this.fw_pointer=0,this.option_roms=[],this.io=void 0,this.bus=t,this.set_tsc(0,0),this.debug_init()}it.prototype.clear_opstats=function(){new Uint8Array(this.wasm_memory.buffer,32768,131072).fill(0),this.wm.exports.profiler_init()},it.prototype.create_jit_imports=function(){let t=Object.create(null);t.m=this.wm.exports.memory;for(let e of Object.keys(this.wm.exports))e.startsWith("_")||e.startsWith("zstd")||e.endsWith("_js")||(t[e]=this.wm.exports[e]);this.jit_imports=t},it.prototype.wasm_patch=function(){let t=t=>this.wm.exports[t],e=e=>{const i=t(e);return console.assert(i,"Missing import: "+e),i};this.reset_cpu=e("reset_cpu"),this.getiopl=e("getiopl"),this.get_eflags=e("get_eflags"),this.get_eflags_no_arith=e("get_eflags_no_arith"),this.pic_call_irq=e("pic_call_irq"),this.do_many_cycles_native=e("do_many_cycles_native"),this.cycle_internal=e("cycle_internal"),this.read8=e("read8"),this.read16=e("read16"),this.read32s=e("read32s"),this.write8=e("write8"),this.write16=e("write16"),this.write32=e("write32"),this.in_mapped_range=e("in_mapped_range"),this.fpu_load_tag_word=e("fpu_load_tag_word"),this.fpu_load_status_word=e("fpu_load_status_word"),this.fpu_get_sti_f64=e("fpu_get_sti_f64"),this.translate_address_system_read=e("translate_address_system_read_js"),this.get_seg_cs=e("get_seg_cs"),this.get_real_eip=e("get_real_eip"),this.clear_tlb=e("clear_tlb"),this.full_clear_tlb=e("full_clear_tlb"),this.set_tsc=e("set_tsc"),this.store_current_tsc=e("store_current_tsc"),this.jit_clear_cache=e("jit_clear_cache_js"),this.jit_dirty_cache=e("jit_dirty_cache"),this.codegen_finalize_finished=e("codegen_finalize_finished"),this.allocate_memory=e("allocate_memory"),this.zero_memory=e("zero_memory"),this.svga_allocate_memory=e("svga_allocate_memory"),this.svga_allocate_dest_buffer=e("svga_allocate_dest_buffer"),this.svga_fill_pixel_buffer=e("svga_fill_pixel_buffer"),this.svga_mark_dirty=e("svga_mark_dirty"),this.zstd_create_ctx=e("zstd_create_ctx"),this.zstd_get_src_ptr=e("zstd_get_src_ptr"),this.zstd_free_ctx=e("zstd_free_ctx"),this.zstd_read=e("zstd_read"),this.zstd_read_free=e("zstd_read_free")},it.prototype.jit_force_generate=function(t){this.jit_force_generate_unsafe&&this.jit_force_generate_unsafe(t)},it.prototype.jit_clear_func=function(t){this.wm.wasm_table.set(t+1024,null)},it.prototype.jit_clear_all_funcs=function(){let t=this.wm.wasm_table;for(let e=0;e<900;e++)t.set(1024+e,null)},it.prototype.get_state=function(){var t=[];t[0]=this.memory_size[0],t[1]=this.segment_is_null,t[2]=this.segment_offsets,t[3]=this.segment_limits,t[4]=this.protected_mode[0],t[5]=this.idtr_offset[0],t[6]=this.idtr_size[0],t[7]=this.gdtr_offset[0],t[8]=this.gdtr_size[0],t[9]=this.page_fault[0],t[10]=this.cr,t[11]=this.cpl[0],t[13]=this.is_32[0],t[16]=this.stack_size_32[0],t[17]=this.in_hlt[0],t[18]=this.last_virt_eip[0],t[19]=this.eip_phys[0],t[22]=this.sysenter_cs[0],t[23]=this.sysenter_eip[0],t[24]=this.sysenter_esp[0],t[25]=this.prefixes[0],t[26]=this.flags[0],t[27]=this.flags_changed[0],t[28]=this.last_op1[0],t[30]=this.last_op_size[0],t[37]=this.instruction_pointer[0],t[38]=this.previous_ip[0],t[39]=this.reg32,t[40]=this.sreg,t[41]=this.dreg,t[42]=this.reg_pdpte,this.store_current_tsc(),t[43]=this.current_tsc,t[45]=this.devices.virtio_9p,t[46]=this.devices.apic,t[47]=this.devices.rtc,t[48]=this.devices.pci,t[49]=this.devices.dma,t[50]=this.devices.acpi,t[51]=this.devices.hpet,t[52]=this.devices.vga,t[53]=this.devices.ps2,t[54]=this.devices.uart0,t[55]=this.devices.fdc,t[56]=this.devices.cdrom,t[57]=this.devices.hda,t[58]=this.devices.pit,t[59]=this.devices.net,t[60]=this.devices.pic,t[61]=this.devices.sb16,t[62]=this.fw_value,t[63]=this.devices.ioapic,t[64]=this.tss_size_32[0],t[66]=this.reg_xmm32s,t[67]=this.fpu_st,t[68]=this.fpu_stack_empty[0],t[69]=this.fpu_stack_ptr[0],t[70]=this.fpu_control_word[0],t[71]=this.fpu_ip[0],t[72]=this.fpu_ip_selector[0],t[73]=this.fpu_dp[0],t[74]=this.fpu_dp_selector[0],t[75]=this.fpu_opcode[0];let{packed_memory:e,bitmap:i}=this.pack_memory();return t[77]=e,t[78]=new Uint8Array(i.get_buffer()),t[79]=this.devices.uart1,t[80]=this.devices.uart2,t[81]=this.devices.uart3,t},it.prototype.set_state=function(t){this.memory_size[0]=t[0],this.mem8.length!==this.memory_size[0]&&console.warn("Note: Memory size mismatch. we="+this.mem8.length+" state="+this.memory_size[0]),this.segment_is_null.set(t[1]),this.segment_offsets.set(t[2]),this.segment_limits.set(t[3]),this.protected_mode[0]=t[4],this.idtr_offset[0]=t[5],this.idtr_size[0]=t[6],this.gdtr_offset[0]=t[7],this.gdtr_size[0]=t[8],this.page_fault[0]=t[9],this.cr.set(t[10]),this.cpl[0]=t[11],this.is_32[0]=t[13],this.stack_size_32[0]=t[16],this.in_hlt[0]=t[17],this.last_virt_eip[0]=t[18],this.eip_phys[0]=t[19],this.sysenter_cs[0]=t[22],this.sysenter_eip[0]=t[23],this.sysenter_esp[0]=t[24],this.prefixes[0]=t[25],this.flags[0]=t[26],this.flags_changed[0]=t[27],this.last_op1[0]=t[28],this.last_op_size[0]=t[30],this.instruction_pointer[0]=t[37],this.previous_ip[0]=t[38],this.reg32.set(t[39]),this.sreg.set(t[40]),this.dreg.set(t[41]),t[42]&&this.reg_pdpte.set(t[42]),this.set_tsc(t[43][0],t[43][1]),this.devices.virtio_9p&&this.devices.virtio_9p.set_state(t[45]),this.devices.apic&&this.devices.apic.set_state(t[46]),this.devices.rtc&&this.devices.rtc.set_state(t[47]),this.devices.pci&&this.devices.pci.set_state(t[48]),this.devices.dma&&this.devices.dma.set_state(t[49]),this.devices.acpi&&this.devices.acpi.set_state(t[50]),this.devices.hpet&&this.devices.hpet.set_state(t[51]),this.devices.vga&&this.devices.vga.set_state(t[52]),this.devices.ps2&&this.devices.ps2.set_state(t[53]),this.devices.uart0&&this.devices.uart0.set_state(t[54]),this.devices.fdc&&this.devices.fdc.set_state(t[55]),this.devices.cdrom&&this.devices.cdrom.set_state(t[56]),this.devices.hda&&this.devices.hda.set_state(t[57]),this.devices.pit&&this.devices.pit.set_state(t[58]),this.devices.net&&this.devices.net.set_state(t[59]),this.devices.pic&&this.devices.pic.set_state(t[60]),this.devices.sb16&&this.devices.sb16.set_state(t[61]),this.devices.uart1&&this.devices.uart1.set_state(t[79]),this.devices.uart2&&this.devices.uart2.set_state(t[80]),this.devices.uart3&&this.devices.uart3.set_state(t[81]),this.fw_value=t[62],this.devices.ioapic&&this.devices.ioapic.set_state(t[63]),this.tss_size_32[0]=t[64],this.reg_xmm32s.set(t[66]),this.fpu_st.set(t[67]),this.fpu_stack_empty[0]=t[68],this.fpu_stack_ptr[0]=t[69],this.fpu_control_word[0]=t[70],this.fpu_ip[0]=t[71],this.fpu_ip_selector[0]=t[72],this.fpu_dp[0]=t[73],this.fpu_dp_selector[0]=t[74],this.fpu_opcode[0]=t[75];let e=new c.Bitmap(t[78].buffer);this.unpack_memory(e,t[77]),this.full_clear_tlb(),this.jit_clear_cache()},it.prototype.pack_memory=function(){this.mem8.length;for(var t=this.mem8.length>>12,e=[],i=0;i<t;i++){var s=i<<12;s=this.mem32s.subarray(s>>2,s+4096>>2);let t=!0;for(let e=0;e<s.length;e++)if(0!==s[e]){t=!1;break}t||e.push(i)}t=new c.Bitmap(t),i=new Uint8Array(e.length<<12);for(let[s,r]of e.entries())t.set(r,1),e=r<<12,e=this.mem8.subarray(e,e+4096),i.set(e,s<<12);return{bitmap:t,packed_memory:i}},it.prototype.unpack_memory=function(t,e){this.zero_memory(this.memory_size[0]);let i=this.memory_size[0]>>12,s=0;for(let a=0;a<i;a++)if(t.get(a)){var r=s<<12;r=e.subarray(r,r+4096),this.mem8.set(r,a<<12),s++}},it.prototype.main_run=function(){if(this.in_hlt[0]){var t=this.hlt_loop();if(this.in_hlt[0])return t}let e=t=O.microtick();for(;e-t<1;){this.do_many_cycles(),e=O.microtick();let t=this.run_hardware_timers(e);if(this.handle_irqs(),this.in_hlt[0])return t}return 0},it.prototype.reboot_internal=function(){this.reset_cpu(),this.fw_value=[],this.devices.virtio&&this.devices.virtio.reset(),this.load_bios()},it.prototype.reset_memory=function(){this.mem8.fill(0)},it.prototype.create_memory=function(t){1048576>t?t=1048576:0>(0|t)&&(t=Math.pow(2,31)-131072),t=1+(t-1|131071)|0,console.assert(0===this.memory_size[0],"Expected uninitialised memory"),this.memory_size[0]=t;let e=this.allocate_memory(t);this.mem8=c.view(Uint8Array,this.wasm_memory,e,t),this.mem32s=c.view(Uint32Array,this.wasm_memory,e,t>>2)},d.exportProperty(it.prototype,"create_memory",it.prototype.create_memory),it.prototype.init=function(e,i){"number"==typeof e.log_level&&(LOG_LEVEL=e.log_level),this.create_memory("number"==typeof e.memory_size?e.memory_size:67108864),this.acpi_enabled[0]=+e.acpi,this.reset_cpu();var r=new A(this);if(this.io=r,this.bios.main=e.bios,this.bios.vga=e.vga_bios,this.load_bios(),e.bzimage){let{option_rom:t}=load_kernel(this.mem8,e.bzimage,e.initrd,e.cmdline||"");t&&this.option_roms.push(t)}r.register_read(179,this,(function(){return _("port 0xB3 read"),0}));var a=0;r.register_read(146,this,(function(){return a})),r.register_write(146,this,(function(t){a=t})),r.register_read(1297,this,(function(){return this.fw_pointer<this.fw_value.length?this.fw_value[this.fw_pointer++]:0})),r.register_write(1296,this,void 0,(function(t){function e(t){return new Uint8Array(new Int32Array([t]).buffer)}function i(t){return t>>8|t<<8&65280}function s(t){return t<<24|t<<8&16711680|t>>8&65280|t>>>24}if(_("bios config port, index="+p(t)),this.fw_pointer=0,0===t)this.fw_value=e(1431127377);else if(1===t)this.fw_value=e(0);else if(3===t)this.fw_value=e(this.memory_size[0]);else if(5===t)this.fw_value=e(1);else if(15===t)this.fw_value=e(1);else if(13===t)this.fw_value=new Uint8Array(16);else if(25===t){t=new Int32Array(4+64*this.option_roms.length);let e=new Uint8Array(t.buffer);t[0]=s(this.option_roms.length);for(let r=0;r<this.option_roms.length;r++){let{name:a,data:o}=this.option_roms[r],_=4+64*r;t[_+0>>2]=s(o.length),t[_+4>>2]=i(49152+r),a.length;for(let t=0;t<a.length;t++)e[_+8+t]=a.charCodeAt(t)}this.fw_value=e}else t>=32768&&t<49152?this.fw_value=e(0):t>=49152&&t-49152<this.option_roms.length?this.fw_value=this.option_roms[t-49152].data:(_("Warning: Unimplemented fw index: "+p(t)),this.fw_value=e(0))})),this.devices={},e.load_devices&&(this.devices.pic=new C(this),this.devices.pci=new E(this),this.acpi_enabled[0]&&(this.devices.ioapic=new I(this),this.devices.apic=new s(this),this.devices.acpi=new t(this)),this.devices.rtc=new y(this),this.fill_cmos(this.devices.rtc,e),this.devices.dma=new b(this),this.devices.vga=new et(this,i,e.vga_memory_size||8388608),this.devices.ps2=new q(this,i),this.devices.uart0=new J(this,1016,i),e.uart1&&(this.devices.uart1=new J(this,760,i)),e.uart2&&(this.devices.uart2=new J(this,1e3,i)),e.uart3&&(this.devices.uart3=new J(this,744,i)),this.devices.fdc=new v(this,e.fda,e.fdb),r=0,e.hda&&(this.devices.hda=new w(this,e.hda,e.hdb,!1,r++,i)),e.cdrom&&(this.devices.cdrom=new w(this,e.cdrom,void 0,!0,r++,i)),this.devices.pit=new T(this,i),e.enable_ne2k&&(this.devices.net=new R(this,i,e.preserve_mac_from_state_image)),e.fs9p&&(this.devices.virtio_9p=new Virtio9p(e.fs9p,this,i)),this.devices.sb16=new G(this,i)),e.multiboot&&this.load_multiboot(e.multiboot)},it.prototype.load_multiboot=function(t){if(_("Trying multiboot from buffer of size "+t.byteLength,LOG_CPU),8192>t.byteLength){var e=new Int32Array(2048);new Uint8Array(e.buffer).set(new Uint8Array(t))}else e=new Int32Array(t,0,2048);for(var i=0;8192>i;i+=4)if(464367618===e[i>>2]){var s=e[i+4>>2];if(!(464367618+s+e[i+8>>2]|0)){_("Multiboot magic found, flags: "+p(s>>>0,8),LOG_CPU),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 r=0;6>r;r++)this.segment_is_null[r]=0,this.segment_offsets[r]=0,this.segment_limits[r]=4294967295,this.sreg[r]=45058;if(65536&s){_("Multiboot specifies its own address table",LOG_CPU),s=e[i+12>>2];var a=e[i+16>>2];r=e[i+20>>2];var o=e[i+24>>2];e=e[i+28>>2],_("header="+p(s,8)+" load="+p(a,8)+" load_end="+p(r,8)+" bss_end="+p(o,8)+" entry="+p(e,8)),t=new Uint8Array(t,i-(s-a),0===r?void 0:r-a),this.write_blob(t,a),this.instruction_pointer[0]=this.get_seg_cs()+e|0}else if(1179403647===e[0])for(a of(_("Multiboot image is in elf format",LOG_CPU),i=read_elf(t),this.instruction_pointer[0]=this.get_seg_cs()+i.header.entry|0,i.program_headers))0!==a.type&&(1===a.type?(a.paddr,a.vaddr,a.filesz,a.memsz,a.paddr+a.memsz<this.memory_size[0]?a.filesz&&(i=new Uint8Array(t,a.offset,a.filesz),this.write_blob(i,a.paddr)):_("Warning: Skipped loading section, paddr="+p(a.paddr)+" memsz="+a.memsz,LOG_CPU)):2!==a.type&&3!==a.type&&4!==a.type&&6!==a.type&&1685382480!==a.type&&1685382481!==a.type&&1685382483!==a.type&&p(a.type));this.io.register_write_consecutive(244,this,(function(t){throw console.log("Test exited with code "+p(t,2)),"HALT"}),(function(){}),(function(){}),(function(){}));for(let t=14;15>=t;t++)this.io.register_write(8192+t,this,(function(e){_("kvm-unit-test: Set irq "+p(t)+" to "+p(e,2)),e?this.device_raise_irq(t):this.device_lower_irq(t)}));_("Starting multiboot kernel at:",LOG_CPU),this.debug.dump_state(),this.debug.dump_regs();break}_("Multiboot checksum check failed",LOG_CPU)}},it.prototype.fill_cmos=function(t,e){var i=e.boot_order||531;t.cmos_write(56,1|i>>4&240),t.cmos_write(61,255&i),t.cmos_write(21,128),t.cmos_write(22,2),i=0,1048576<=this.memory_size[0]&&(i=this.memory_size[0]-1048576>>10,i=Math.min(i,65535)),t.cmos_write(23,255&i),t.cmos_write(24,i>>8&255),t.cmos_write(48,255&i),t.cmos_write(49,i>>8&255),i=0,16777216<=this.memory_size[0]&&(i=this.memory_size[0]-16777216>>16,i=Math.min(i,65535)),t.cmos_write(52,255&i),t.cmos_write(53,i>>8&255),t.cmos_write(91,0),t.cmos_write(92,0),t.cmos_write(93,0),t.cmos_write(20,47),t.cmos_write(95,0),e.fastboot&&t.cmos_write(63,1)},it.prototype.load_bios=function(){var t=this.bios.main,e=this.bios.vga;if(t){var i=new Uint8Array(t);if(this.write_blob(i,1048576-t.byteLength),e){var s=new Uint8Array(e);this.write_blob(s,786432),this.io.mmap_register(4272947200,1048576,(function(t){return(t=t-4272947200|0)<s.length?s[t]:0}),(function(t,e){}))}else _("Warning: No VGA BIOS");this.io.mmap_register(4293918720,1048576,function(t){return this.mem8[1048575&t]}.bind(this),function(t,e){this.mem8[1048575&t]=e}.bind(this))}else _("Warning: No BIOS")},it.prototype.do_many_cycles=function(){this.do_many_cycles_native()},it.prototype.cycle=function(){this.cycle_internal()},d.exportProperty(it.prototype,"cycle",it.prototype.cycle),it.prototype.codegen_finalize=function(t,e,i,s,r){let a=new Uint8Array(this.wasm_memory.buffer,s>>>0,r>>>0);WebAssembly.instantiate(a,{e:this.jit_imports}).then((s=>{s=s.instance.exports.f,this.codegen_finalize_finished(t,e,i),this.wm.wasm_table.set(t+1024,s),this.test_hook_did_finalize_wasm&&this.test_hook_did_finalize_wasm(a)}))},it.prototype.log_uncompiled_code=function(t,e){},it.prototype.dump_function_code=function(t,e){},it.prototype.hlt_loop=function(){if(512&this.get_eflags_no_arith()){let t=this.run_hardware_timers(O.microtick());return this.handle_irqs(),t}return 100},it.prototype.run_hardware_timers=function(t){var e=this.devices.pit.timer(t,!1),i=this.devices.rtc.timer(t,!1);let s=100,r=100;return this.acpi_enabled[0]&&(s=this.devices.acpi.timer(t),r=this.devices.apic.timer(t)),Math.min(e,i,100,s,r)},it.prototype.hlt_op=function(){0==(512&this.get_eflags_no_arith())&&this.bus.send("cpu-event-halt"),this.in_hlt[0]=1,this.hlt_loop()},it.prototype.handle_irqs=function(){512&this.get_eflags_no_arith()&&(this.pic_acknowledge(),this.next_tick_immediately())},it.prototype.pic_acknowledge=function(){this.get_eflags_no_arith(),this.devices.pic&&this.devices.pic.acknowledge_irq(),this.devices.apic&&this.devices.apic.acknowledge_irq()},it.prototype.device_raise_irq=function(t){this.devices.pic&&this.devices.pic.set_irq(t),this.devices.ioapic&&this.devices.ioapic.set_irq(t)},it.prototype.device_lower_irq=function(t){this.devices.pic&&this.devices.pic.clear_irq(t),this.devices.ioapic&&this.devices.ioapic.clear_irq(t)},it.prototype.debug_init=function(){var t=this,e={};let i,s,r;this.debug=e,e.init=function(){},e.get_regs_short=function(){var e={eax:REG_EAX,ecx:REG_ECX,edx:REG_EDX,ebx:REG_EBX,esp:REG_ESP,ebp:REG_EBP,esi:REG_ESI,edi:REG_EDI},i="eax ecx edx ebx esp ebp esi edi".split(" ");REG_CS,REG_DS,REG_ES,REG_FS,REG_GS,REG_SS;for(var s="",r="",a=0;4>a;a++)s+=i[a]+"="+h(t.reg32[e[i[a]]]>>>0,8)+" ",r+=i[a+4]+"="+h(t.reg32[e[i[a+4]]]>>>0,8)+" ";return[s+=" ds="+h(t.sreg[REG_DS],4)+" es="+h(t.sreg[REG_ES],4)+" fs="+h(t.sreg[REG_FS],4),r+=" gs="+h(t.sreg[REG_GS],4)+" cs="+h(t.sreg[REG_CS],4)+" ss="+h(t.sreg[REG_SS],4)]},e.dump_regs=function(){},e.get_state=function(t){},e.dump_state=function(t){},e.dump_stack=function(t,e){},e.dump_page_structures=function(){if(t.cr[4]&CR4_PAE){dbg_log("PAE enabled");for(var e=0;4>e;e++)t.read32s(t.cr[3]+8*e)}else dbg_log("PAE disabled")},e.dump_gdt_ldt=function(){},e.dump_idt=function(){},e.get_memory_dump=function(t,e){},e.memory_hex_dump=function(t,e){},e.used_memory_dump=function(){},e.debug_interrupt=function(t){},e.dump_code=function(t,e,r){if(!s){if(void 0===i&&("function"!=typeof require&&(i=window.cs),void 0===i))return void dbg_log("Warning: Missing capstone library, disassembly not available");s=[new i.Capstone(i.ARCH_X86,i.MODE_16),new i.Capstone(i.ARCH_X86,i.MODE_32)]}try{s[t].disasm(e,r).forEach((function(t){dbg_log(h(t.address>>>0)+": "+v86util.pads(t.bytes.map((t=>h(t,2).slice(-2))).join(" "),20)+" "+t.mnemonic+" "+t.op_str)})),dbg_log("")}catch(t){dbg_log("Could not disassemble: "+Array.from(e).map((t=>h(t,2))).join(" "))}},e.dump_wasm=function(t){if(void 0!==r||("function"!=typeof require&&(r=new window.WabtModule),void 0!==r)){t=t.slice();try{var e=r.readWasm(t,{readDebugNames:!1});e.generateNames(),e.applyNames();let i=e.toText({foldExprs:!0,inlineExport:!0});dbg_log(i)}catch(e){var i=new Blob([t]),s=document.createElement("a");s.download="failed.wasm",s.href=window.URL.createObjectURL(i),s.dataset.downloadurl=["application/octet-stream",s.download,s.href].join(":"),s.click(),window.URL.revokeObjectURL(s.src),console.log(e.toString())}finally{e&&e.destroy()}}else dbg_log("Warning: Missing libwabt, wasm dump not available")}};let st=DataView.prototype,rt={size:1,get:st.getUint8,set:st.setUint8},at={size:2,get:st.getUint16,set:st.setUint16},ot={size:4,get:st.getUint32,set:st.setUint32},_t=ct([{magic:ot},{class:rt},{data:rt},{version0:rt},{osabi:rt},{abiversion:rt},{pad0:(nt=7,{size:nt,get:t=>-1})},{type:at},{machine:at},{version1:ot},{entry:ot},{phoff:ot},{shoff:ot},{flags:ot},{ehsize:at},{phentsize:at},{phnum:at},{shentsize:at},{shnum:at},{shstrndx:at}]);var nt;console.assert(52===_t.reduce(((t,e)=>t+e.size),0));let ht=ct([{type:ot},{offset:ot},{vaddr:ot},{paddr:ot},{filesz:ot},{memsz:ot},{flags:ot},{align:ot}]);console.assert(32===ht.reduce(((t,e)=>t+e.size),0));let dt=ct([{name:ot},{type:ot},{flags:ot},{addr:ot},{offset:ot},{size:ot},{link:ot},{info:ot},{addralign:ot},{entsize:ot}]);function ct(t){return t.map((function(t){var e=Object.keys(t);return console.assert(1===e.length),t=t[e=e[0]],console.assert(0<t.size),{name:e,type:t,size:t.size,get:t.get,set:t.set}}))}function pt(t,e){let i={},s=0;for(let r of e)e=r.get.call(t,s,!0),console.assert(void 0===i[r.name]),i[r.name]=e,s+=r.size;return[i,s]}function ut(t,e,i){let s=[],r=0;for(var a=0;a<i;a++){let[i,a]=pt(lt(t,r),e);s.push(i),r+=a}return[s,r]}function lt(t,e,i){return new DataView(t.buffer,t.byteOffset+e,i)}console.assert(40===dt.reduce(((t,e)=>t+e.size),0));function ft(t,e){let i=new Uint8Array(256);new Uint16Array(i.buffer)[0]=43605,i[2]=1;var s=3;for(i[s++]=250,i[s++]=184,i[s++]=t>>0,i[s++]=t>>8,i[s++]=142,i[s++]=192,i[s++]=142,i[s++]=216,i[s++]=142,i[s++]=224,i[s++]=142,i[s++]=232,i[s++]=142,i[s++]=208,i[s++]=188,i[s++]=e>>0,i[s++]=e>>8,i[s++]=234,i[s++]=0,i[s++]=0,i[s++]=t+32>>0,i[s++]=t+32>>8,dbg_assert(512>s),e=i[t=s]=0,s=0;s<i.length;s++)e+=i[s];return i[t]=-e,i}it.prototype.mmap_read8=function(t){return this.memory_map_read8[t>>>17](t)},it.prototype.mmap_write8=function(t,e){this.memory_map_write8[t>>>17](t,e)},it.prototype.mmap_read16=function(t){var e=this.memory_map_read8[t>>>17];return e(t)|e(t+1|0)<<8},it.prototype.mmap_write16=function(t,e){var i=this.memory_map_write8[t>>>17];i(t,255&e),i(t+1|0,e>>8)},it.prototype.mmap_read32=function(t){return this.memory_map_read32[t>>>17](t)},it.prototype.mmap_write32=function(t,e){this.memory_map_write32[t>>>17](t,e)},it.prototype.mmap_write64=function(t,e,i){var s=this.memory_map_write32[t>>>17];s(t,e),s(t+4,i)},it.prototype.mmap_write128=function(t,e,i,s,r){var a=this.memory_map_write32[t>>>17];a(t,e),a(t+4,i),a(t+8,s),a(t+12,r)},it.prototype.write_blob=function(t,e){t&&t.length,t.length&&(this.in_mapped_range(e),this.in_mapped_range(e+t.length-1),this.jit_dirty_cache(e,e+t.length),this.mem8.set(t,e))},it.prototype.read_blob=function(t,e){return e&&(this.in_mapped_range(t),this.in_mapped_range(t+e-1)),this.mem8.subarray(t,t+e)};function mt(t){this.message=t}mt.prototype=Error();let gt={Uint8Array:Uint8Array,Int8Array:Int8Array,Uint16Array:Uint16Array,Int16Array:Int16Array,Uint32Array:Uint32Array,Int32Array:Int32Array,Float32Array:Float32Array,Float64Array:Float64Array};function bt(t,e){if("object"!=typeof t||null===t)return dbg_assert("function"!=typeof t),t;if(t instanceof Array)return t.map((t=>bt(t,e)));if(t.constructor===Object&&(console.log(t),dbg_assert(t.constructor!==Object,"Expected non-object")),t.BYTES_PER_ELEMENT){var i=new Uint8Array(t.buffer,t.byteOffset,t.length*t.BYTES_PER_ELEMENT);return t=t.constructor.name.replace("bound ",""),dbg_assert(gt[t]),{__state_type__:t,buffer_id:e.push(i)-1}}i=t.get_state(),t=[];for(var s=0;s<i.length;s++){var r=i[s];dbg_assert("function"!=typeof r),t[s]=bt(r,e)}return t}function yt(t,e){if("object"!=typeof t||null===t)return dbg_assert("function"!=typeof t),t;if(t instanceof Array){for(var i=0;i<t.length;i++)t[i]=yt(t[i],e);return t}i=t.__state_type__,dbg_assert(void 0!==i);let s=gt[i];return dbg_assert(s,"Unkown type: "+i),new s(e[t.buffer_id])}it.prototype.save_state=function(){for(var t=[],e=bt(this,t),i=[],s=0,r=0;r<t.length;r++){var a=t[r].byteLength;i[r]={offset:s,length:a},s=(s+=a)+3&-4}e=JSON.stringify({buffer_infos:i,state:e});var o=(r=(r=16+(e=(new TextEncoder).encode(e)).length)+3&-4)+s;s=new ArrayBuffer(o);var _=new Int32Array(s,0,4);for(new Uint8Array(s,16,e.length).set(e),a=new Uint8Array(s,r),_[0]=-2039052682,_[1]=6,_[2]=o,_[3]=e.length,r=0;r<t.length;r++)o=t[r],dbg_assert(o.constructor===Uint8Array),a.set(o,i[r].offset);return dbg_log("State: json size "+(e.byteLength>>10)+"k"),dbg_log("State: Total buffers size "+(a.byteLength>>10)+"k"),s},it.prototype.restore_state=function(t){function e(t,e){let i=t.length;if(i<16)throw new mt("Invalid length: "+i);if(-2039052682!==(t=new Int32Array(t.buffer,t.byteOffset,4))[0])throw new mt("Invalid header: "+h(t[0]>>>0));if(6!==t[1])throw new mt("Version mismatch: dump="+t[1]+" we=6");if(e&&t[2]!==i)throw new mt("Length doesn't match header: real="+i+" header="+t[2]);return t[3]}function i(t){return t=(new TextDecoder).decode(t),JSON.parse(t)}if(t=new Uint8Array(t),4247762216===new Uint32Array(t.buffer,0,1)[0]){var s=this.zstd_create_ctx(t.length);new Uint8Array(this.wasm_memory.buffer,this.zstd_get_src_ptr(s),t.length).set(t);var r=this.zstd_read(s,16),a=new Uint8Array(this.wasm_memory.buffer,r,16),o=e(a,!1);this.zstd_read_free(r,16),r=this.zstd_read(s,o),a=i(a=new Uint8Array(this.wasm_memory.buffer,r,o)),this.zstd_read_free(r,o),r=a.state;var _=a.buffer_infos;for(var n of(a=[],o=16+o,_)){if(_=(o+3&-4)-o,1048576<n.length){var d=this.zstd_read(s,_);this.zstd_read_free(d,_),d=new Uint8Array(n.length),a.push(d.buffer);for(var c=0;c<n.length;){var p=n.length-c;dbg_assert(0<=p),p=Math.min(p,1048576);let t=this.zstd_read(s,p);d.set(new Uint8Array(this.wasm_memory.buffer,t,p),c),this.zstd_read_free(t,p),c+=p}}else c=(d=this.zstd_read(s,_+n.length))+_,a.push(this.wasm_memory.buffer.slice(c,c+n.length)),this.zstd_read_free(d,_+n.length);o+=_+n.length}r=yt(r,a),this.set_state(r),this.zstd_free_ctx(s)}else{if(0>(s=e(t,!0))||s+12>=t.length)throw new mt("Invalid info block length: "+s);n=(r=i(n=t.subarray(16,16+s))).state,r=r.buffer_infos;let a=16+s;a=a+3&-4,n=yt(n,s=r.map((e=>{const i=a+e.offset;return t.buffer.slice(i,i+e.length)}))),this.set_state(n)}};function vt(t,e){for(var i of(t.io,this.cpu=t,this.pci=t.devices.pci,this.device_id=e.device_id,this.pci_space=[244,26,255&e.device_id,e.device_id>>8,7,5,16,0,1,0,2,0,0,0,0,0,1,168,0,0,0,16,191,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,255&e.subsystem_device_id,e.subsystem_device_id>>8,0,0,0,0,64,0,0,0,0,0,0,0,0,1,0,0],this.pci_space=this.pci_space.concat(v86util.zeros(256-this.pci_space.length)),this.pci_id=e.pci_id,this.pci_bars=[],this.name=e.name,this.driver_feature_select=this.device_feature_select=0,this.device_feature=new Uint32Array(4),this.driver_feature=new Uint32Array(4),e.common.features))dbg_assert(0<=i,"VirtIO device<"+this.name+"> feature bit numbers must be non-negative"),dbg_assert(128>i,"VirtIO device<"+this.name+"> feature bit numbers assumed less than 128 in implementation"),this.device_feature[i>>>5]|=1<<(31&i),this.driver_feature[i>>>5]|=1<<(31&i);for(var s of(dbg_assert(e.common.features.includes(32),"VirtIO device<"+this.name+"> only non-transitional devices are supported"),this.features_ok=!0,this.device_status=0,this.config_has_changed=!1,this.config_generation=0,this.queues=[],e.common.queues))this.queues.push(new wt(t,this,s));if(this.queue_select=0,this.queue_selected=this.queues[0],this.isr_status=0,DEBUG){for(var r of(i=new Set,this.queues.map((t=>t.notify_offset))))s=e.notification.single_handler?0:r,i.add(s),dbg_assert(e.notification.handlers[s],"VirtIO device<"+this.name+"> every queue's notifier must exist");for(let[t,s]of e.notification.handlers.entries())dbg_assert(!s||i.has(t),"VirtIO device<"+this.name+"> no defined notify handler should be unused")}(r=[]).push(this.create_common_capability(e.common)),r.push(this.create_notification_capability(e.notification)),r.push(this.create_isr_capability(e.isr_status)),e.device_specific&&r.push(this.create_device_specific_capability(e.device_specific)),this.init_capabilities(r),t.devices.pci.register_device(this),this.reset()}function wt(t,e,i){this.cpu=t,this.virtio=e,this.size_supported=this.size=i.size_supported,this.mask=this.size-1,this.enabled=!1,this.notify_offset=i.notify_offset,this.num_staged_replies=this.used_addr=this.avail_last_idx=this.avail_addr=this.desc_addr=0,this.reset()}function xt(t,e){this.cpu=t.cpu,this.virtio=t.virtio,this.head_idx=e,this.read_buffers=[],this.length_readable=this.read_buffer_offset=this.read_buffer_idx=0,this.write_buffers=[],this.length_writable=this.length_written=this.write_buffer_offset=this.write_buffer_idx=0;let i=t.desc_addr,s=0,r=t.size,a=!1,o=this.virtio.is_feature_negotiated(28);for(dbg_log("<<< Descriptor chain start",LOG_VIRTIO);;){let _=t.get_descriptor(i,e);if(dbg_log("descriptor: idx="+e+" addr="+h(_.addr_high,8)+":"+h(_.addr_low,8)+" len="+h(_.len,8)+" flags="+h(_.flags,4)+" next="+h(_.next,4),LOG_VIRTIO),o&&4&_.flags)DEBUG&&1&_.flags&&dbg_log("Driver bug: has set VIRTQ_DESC_F_NEXT flag in an indirect table descriptor",LOG_VIRTIO),i=_.addr_low,s=e=0,r=_.len/16,dbg_log("start indirect",LOG_VIRTIO);else{if(2&_.flags)a=!0,this.write_buffers.push(_),this.length_writable+=_.len;else{if(a){dbg_log("Driver bug: readonly buffer after writeonly buffer within chain",LOG_VIRTIO);break}this.read_buffers.push(_),this.length_readable+=_.len}if(s++,s>r){dbg_log("Driver bug: descriptor chain cycle detected",LOG_VIRTIO);break}if(!(1&_.flags))break;e=_.next}}dbg_log("Descriptor chain end >>>",LOG_VIRTIO)}function At(t,e){this.bus=e,this.socket=void 0,this.send_queue=[],this.url=t,this.reconnect_interval=1e4,this.last_connect_attempt=Date.now()-this.reconnect_interval,this.send_queue_limit=64,this.bus.register("net0-send",(function(t){this.send(t)}),this)}vt.prototype.create_common_capability=function(t){return{type:1,bar:0,port:t.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array(0),struct:[{bytes:4,name:"device_feature_select",read:()=>this.device_feature_select,write:t=>{this.device_feature_select=t}},{bytes:4,name:"device_feature",read:()=>this.device_feature[this.device_feature_select]||0,write:t=>{}},{bytes:4,name:"driver_feature_select",read:()=>this.driver_feature_select,write:t=>{this.driver_feature_select=t}},{bytes:4,name:"driver_feature",read:()=>this.driver_feature[this.driver_feature_select]||0,write:t=>{let e=this.device_feature[this.driver_feature_select];this.driver_feature_select<this.driver_feature.length&&(this.driver_feature[this.driver_feature_select]=t&e),this.features_ok=this.features_ok&&!(t&~e)}},{bytes:2,name:"msix_config",read:()=>(dbg_log("No msi-x capability supported.",LOG_VIRTIO),65535),write:t=>{dbg_log("No msi-x capability supported.",LOG_VIRTIO)}},{bytes:2,name:"num_queues",read:()=>this.queues.length,write:t=>{}},{bytes:1,name:"device_status",read:()=>this.device_status,write:e=>{0===e?(dbg_log("Reset device<"+this.name+">",LOG_VIRTIO),this.reset()):128&e?dbg_log("Warning: Device<"+this.name+"> status failed",LOG_VIRTIO):dbg_log("Device<"+this.name+"> status: "+(1&e?"ACKNOWLEDGE ":"")+(2&e?"DRIVER ":"")+(4&e?"DRIVER_OK":"")+(8&e?"FEATURES_OK ":"")+(64&e?"DEVICE_NEEDS_RESET":""),LOG_VIRTIO),e&~this.device_status&4&&64&this.device_status&&this.notify_config_changes(),this.features_ok||(DEBUG&&8&e&&dbg_log("Removing FEATURES_OK",LOG_VIRTIO),e&=-9),this.device_status=e,e&~this.device_status&4&&t.on_driver_ok()}},{bytes:1,name:"config_generation",read:()=>this.config_generation,write:t=>{}},{bytes:2,name:"queue_select",read:()=>this.queue_select,write:t=>{this.queue_select=t,this.queue_select<this.queues.length?this.queues_selected=this.queues[this.queue_select]:this.queue_selected=null}},{bytes:2,name:"queue_size",read:()=>this.queue_selected?this.queue_selected.size:0,write:t=>{this.queue_selected&&(t&t-1&&(dbg_log("Warning: dev<"+this.name+"> Given queue size was not a power of 2. Rounding up to next power of 2.",LOG_VIRTIO),t=1<<v86util.int_log2(t-1)+1),t>this.queue_selected.size_supported&&(dbg_log("Warning: dev<"+this.name+"> Trying to set queue size greater than supported. Clamping to supported size.",LOG_VIRTIO),t=this.queue_selected.size_supported),this.queue_selected.set_size(t))}},{bytes:2,name:"queue_msix_vector",read:()=>(dbg_log("No msi-x capability supported.",LOG_VIRTIO),65535),write:t=>{dbg_log("No msi-x capability supported.",LOG_VIRTIO)}},{bytes:2,name:"queue_enable",read:()=>this.queue_selected?0|this.queue_selected.enabled:0,write:t=>{this.queue_selected&&(1===t?this.queue_selected.is_configured()?this.queue_selected.enable():dbg_log("Driver bug: tried enabling unconfigured queue",LOG_VIRTIO):0===t&&dbg_log("Driver bug: tried writing 0 to queue_enable",LOG_VIRTIO))}},{bytes:2,name:"queue_notify_off",read:()=>this.queue_selected?this.queue_selected.notify_offset:0,write:t=>{}},{bytes:4,name:"queue_desc (low dword)",read:()=>this.queue_selected?this.queue_selected.desc_addr:0,write:t=>{this.queue_selected&&(this.queue_selected.desc_addr=t)}},{bytes:4,name:"queue_desc (high dword)",read:()=>0,write:t=>{dbg_log("Warning: High dword of 64 bit queue_desc ignored",LOG_VIRTIO)}},{bytes:4,name:"queue_avail (low dword)",read:()=>this.queue_selected?this.queue_selected.avail_addr:0,write:t=>{this.queue_selected&&(this.queue_selected.avail_addr=t)}},{bytes:4,name:"queue_avail (high dword)",read:()=>0,write:t=>{dbg_log("Warning: High dword of 64 bit queue_avail ignored",LOG_VIRTIO)}},{bytes:4,name:"queue_used (low dword)",read:()=>this.queue_selected?this.queue_selected.used_addr:0,write:t=>{this.queue_selected&&(this.queue_selected.used_addr=t)}},{bytes:4,name:"queue_used (high dword)",read:()=>0,write:t=>{dbg_log("Warning: High dword of 64 bit queue_used ignored",LOG_VIRTIO)}}]}},vt.prototype.create_notification_capability=function(t){let e,i=[];t.single_handler?(dbg_assert(1===t.handlers.length,"VirtIO device<"+this.name+"> too many notify handlers specified: expected single handler"),e=0):e=2;for(let[e,s]of t.handlers.entries())i.push({bytes:2,name:"notify"+e,read:()=>65535,write:s||(t=>{})});return{type:2,bar:1,port:t.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array([255&e,e>>8&255,e>>16&255,e>>24]),struct:i}},vt.prototype.create_isr_capability=function(t){return{type:3,bar:2,port:t.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array(0),struct:[{bytes:1,name:"isr_status",read:()=>{let t=this.isr_status;return this.lower_irq(),t},write:t=>{}}]}},vt.prototype.create_device_specific_capability=function(t){return dbg_assert(3&~t.offset,"VirtIO device<"+this.name+"> device specific cap offset must be 4-byte aligned"),{type:4,bar:3,port:t.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array(0),struct:t.struct}},vt.prototype.init_capabilities=function(t){let e=this.pci_space[52]=64;var i=e;for(let r of t){e=(i=e)+(t=16+r.extra.length),dbg_assert(256>=e,"VirtIO device<"+this.name+"> can't fit all capabilities into 256byte configspace"),dbg_assert(0<=r.bar&&6>r.bar,"VirtIO device<"+this.name+"> capability invalid bar number");var s=r.struct.reduce(((t,e)=>t+e.bytes),0);s=16>(s+=r.offset)?16:1<<v86util.int_log2(s-1)+1,dbg_assert(0==(r.port&s-1),"VirtIO device<"+this.name+"> capability port should be aligned to pci bar size"),this.pci_bars[r.bar]={size:s},this.pci_space[i]=9,this.pci_space[i+1]=e,this.pci_space[i+2]=t,this.pci_space[i+3]=r.type,this.pci_space[i+4]=r.bar,this.pci_space[i+5]=0,this.pci_space[i+6]=0,this.pci_space[i+7]=0,this.pci_space[i+8]=255&r.offset,this.pci_space[i+9]=r.offset>>>8&255,this.pci_space[i+10]=r.offset>>>16&255,this.pci_space[i+11]=r.offset>>>24,this.pci_space[i+12]=255&s,this.pci_space[i+13]=s>>>8&255,this.pci_space[i+14]=s>>>16&255,this.pci_space[i+15]=s>>>24;for(let[t,e]of r.extra.entries())this.pci_space[i+16+t]=e;i=16+4*r.bar,this.pci_space[i]=254&r.port|!r.use_mmio,this.pci_space[i+1]=r.port>>>8&255,this.pci_space[i+2]=r.port>>>16&255,this.pci_space[i+3]=r.port>>>24&255,i=r.port+r.offset;for(let e of r.struct){let a=e.read;if(t=e.write,DEBUG&&(a=()=>{let t=e.read();return dbg_log("Device<"+this.name+"> cap["+r.type+"] read["+e.name+"] => "+h(t,8*e.bytes),LOG_VIRTIO),t},t=t=>{dbg_log("Device<"+this.name+"> cap["+r.type+"] write["+e.name+"] <= "+h(t,8*e.bytes),LOG_VIRTIO),e.write(t)}),r.use_mmio)dbg_assert(!1,"VirtIO device <"+this.name+"> mmio capability not implemented.");else{s=function(t){return dbg_log("Warning: 8-bit read from 16-bit virtio port",LOG_VIRTIO),a(-2&t)>>((1&t)<<3)&255};let r=function(t){return dbg_log("Warning: 8-bit read from 32-bit virtio port",LOG_VIRTIO),a(-4&t)>>((3&t)<<3)&255};switch(e.bytes){case 4:this.cpu.io.register_read(i,this,r,void 0,a),this.cpu.io.register_write(i,this,void 0,void 0,t);break;case 2:this.cpu.io.register_read(i,this,s,a),this.cpu.io.register_write(i,this,void 0,t);break;case 1:this.cpu.io.register_read(i,this,a),this.cpu.io.register_write(i,this,t);break;default:dbg_assert(!1,"VirtIO device <"+this.name+"> invalid capability field width of "+e.bytes+" bytes")}}i+=e.bytes}}i=20,dbg_assert(256>=e+i,"VirtIO device<"+this.name+"> can't fit all capabilities into 256byte configspace"),this.pci_space[e]=9,this.pci_space[e+1]=0,this.pci_space[e+2]=i,this.pci_space[e+3]=5,this.pci_space[e+4]=0,this.pci_space[e+5]=0,this.pci_space[e+6]=0,this.pci_space[e+7]=0,this.pci_space[e+8]=0,this.pci_space[e+9]=0,this.pci_space[e+10]=0,this.pci_space[e+11]=0,this.pci_space[e+12]=0,this.pci_space[e+13]=0,this.pci_space[e+14]=0,this.pci_space[e+15]=0,this.pci_space[e+16]=0,this.pci_space[e+17]=0,this.pci_space[e+18]=0,this.pci_space[e+19]=0},vt.prototype.get_state=function(){let t=[];return t[0]=this.device_feature_select,t[1]=this.driver_feature_select,t[2]=this.device_feature,t[3]=this.driver_feature,t[4]=this.features_ok,t[5]=this.device_status,t[6]=this.config_has_changed,t[7]=this.config_generation,t[8]=this.isr_status,t[9]=this.queue_select,t.concat(this.queues)},vt.prototype.set_state=function(t){this.device_feature_select=t[0],this.driver_feature_select=t[1],this.device_feature=t[2],this.driver_feature=t[3],this.features_ok=t[4],this.device_status=t[5],this.config_has_changed=t[6],this.config_generation=t[7],this.isr_status=t[8],this.queue_select=t[9];let e=0;for(let i of t.slice(10))this.queues[e].set_state(i),e++;this.queue_selected=this.queues[this.queue_select]||null},vt.prototype.reset=function(){this.driver_feature_select=this.device_feature_select=0,this.driver_feature.set(this.device_feature),this.features_ok=!0,this.queue_select=this.device_status=0,this.queue_selected=this.queues[0];for(let t of this.queues)t.reset();this.config_has_changed=!1,this.config_generation=0,this.lower_irq()},vt.prototype.notify_config_changes=function(){this.config_has_changed=!0,4&this.device_status?this.raise_irq(2):dbg_assert(!1,"VirtIO device<"+this.name+"> attempted to notify driver before DRIVER_OK")},vt.prototype.update_config_generation=function(){this.config_has_changed&&(this.config_generation++,this.config_generation&=255,this.config_has_changed=!1)},vt.prototype.is_feature_negotiated=function(t){return 0<(this.driver_feature[t>>>5]&1<<(31&t))},vt.prototype.needs_reset=function(){dbg_log("Device<"+this.name+"> experienced error - requires reset",LOG_VIRTIO),this.device_status|=64,4&this.device_status&&this.notify_config_changes()},vt.prototype.raise_irq=function(t){dbg_log("Raise irq "+h(t),LOG_VIRTIO),this.isr_status|=t,this.pci.raise_irq(this.pci_id)},vt.prototype.lower_irq=function(){dbg_log("Lower irq ",LOG_VIRTIO),this.isr_status=0,this.pci.lower_irq(this.pci_id)},wt.prototype.get_state=function(){let t=[];return t[0]=this.size,t[1]=this.size_supported,t[2]=this.enabled,t[3]=this.notify_offset,t[4]=this.desc_addr,t[5]=this.avail_addr,t[6]=this.avail_last_idx,t[7]=this.used_addr,t[8]=this.num_staged_replies,t},wt.prototype.set_state=function(t){this.size=t[0],this.size_supported=t[1],this.enabled=t[2],this.notify_offset=t[3],this.desc_addr=t[4],this.avail_addr=t[5],this.avail_last_idx=t[6],this.used_addr=t[7],this.num_staged_replies=t[8],this.mask=this.size-1},wt.prototype.reset=function(){this.enabled=!1,this.num_staged_replies=this.used_addr=this.avail_last_idx=this.avail_addr=this.desc_addr=0,this.set_size(this.size_supported)},wt.prototype.is_configured=function(){return this.desc_addr&&this.avail_addr&&this.used_addr},wt.prototype.enable=function(){dbg_assert(this.is_configured(),"VirtQueue must be configured before enabled"),this.enabled=!0},wt.prototype.set_size=function(t){dbg_assert(0==(t&t-1),"VirtQueue size must be power of 2 or zero"),dbg_assert(t<=this.size_supported,"VirtQueue size must be within supported size"),this.size=t,this.mask=t-1},wt.prototype.count_requests=function(){return dbg_assert(this.avail_addr,"VirtQueue addresses must be configured before use"),this.avail_get_idx()-this.avail_last_idx&this.mask},wt.prototype.has_request=function(){return dbg_assert(this.avail_addr,"VirtQueue addresses must be configured before use"),(this.avail_get_idx()&this.mask)!==this.avail_last_idx},wt.prototype.pop_request=function(){dbg_assert(this.avail_addr,"VirtQueue addresses must be configured before use"),dbg_assert(this.has_request(),"VirtQueue must not pop nonexistent request");var t=this.avail_get_entry(this.avail_last_idx);return dbg_log("Pop request: avail_last_idx="+this.avail_last_idx+" desc_idx="+t,LOG_VIRTIO),t=new xt(this,t),this.avail_last_idx=this.avail_last_idx+1&this.mask,t},wt.prototype.push_reply=function(t){dbg_assert(this.used_addr,"VirtQueue addresses must be configured before use"),dbg_assert(this.num_staged_replies<this.size,"VirtQueue replies must not exceed queue size");let e=this.used_get_idx()+this.num_staged_replies&this.mask;dbg_log("Push reply: used_idx="+e+" desc_idx="+t.head_idx,LOG_VIRTIO),this.used_set_entry(e,t.head_idx,t.length_written),this.num_staged_replies++},wt.prototype.flush_replies=function(){if(dbg_assert(this.used_addr,"VirtQueue addresses must be configured before use"),0===this.num_staged_replies)dbg_log("flush_replies: Nothing to flush",LOG_VIRTIO);else{dbg_log("Flushing "+this.num_staged_replies+" replies",LOG_VIRTIO);var t=this.used_get_idx()+this.num_staged_replies&65535;this.used_set_idx(t),this.num_staged_replies=0,this.virtio.is_feature_negotiated(29)?(this.avail_get_used_event(),this.virtio.raise_irq(1)):1&~this.avail_get_flags()&&this.virtio.raise_irq(1)}},wt.prototype.notify_me_after=function(t){dbg_assert(0<=t,"Must skip a non-negative number of requests"),t=this.avail_get_idx()+t&65535,this.used_set_avail_event(t)},wt.prototype.get_descriptor=function(t,e){return{addr_low:this.cpu.read32s(t+16*e),addr_high:this.cpu.read32s(t+16*e+4),len:this.cpu.read32s(t+16*e+8),flags:this.cpu.read16(t+16*e+12),next:this.cpu.read16(t+16*e+14)}},wt.prototype.avail_get_flags=function(){return this.cpu.read16(this.avail_addr)},wt.prototype.avail_get_idx=function(){return this.cpu.read16(this.avail_addr+2)},wt.prototype.avail_get_entry=function(t){return this.cpu.read16(this.avail_addr+4+2*t)},wt.prototype.avail_get_used_event=function(){return this.cpu.read16(this.avail_addr+4+2*this.size)},wt.prototype.used_get_flags=function(){return this.cpu.read16(this.used_addr)},wt.prototype.used_set_flags=function(t){this.cpu.write16(this.used_addr,t)},wt.prototype.used_get_idx=function(){return this.cpu.read16(this.used_addr+2)},wt.prototype.used_set_idx=function(t){this.cpu.write16(this.used_addr+2,t)},wt.prototype.used_set_entry=function(t,e,i){this.cpu.write32(this.used_addr+4+8*t,e),this.cpu.write32(this.used_addr+8+8*t,i)},wt.prototype.used_set_avail_event=function(t){this.cpu.write16(this.used_addr+4+8*this.size,t)},xt.prototype.get_next_blob=function(t){let e=0,i=t.length;for(;i;){if(this.read_buffer_idx===this.read_buffers.length){dbg_log("Device<"+this.virtio.name+"> Read more than device-readable buffers has",LOG_VIRTIO);break}var s=this.read_buffers[this.read_buffer_idx];let r=s.addr_low+this.read_buffer_offset;(s=s.len-this.read_buffer_offset)>i?(s=i,this.read_buffer_offset+=i):(this.read_buffer_idx++,this.read_buffer_offset=0),t.set(this.cpu.read_blob(r,s),e),e+=s,i-=s}return e},xt.prototype.set_next_blob=function(t){let e=0,i=t.length;for(;i;){if(this.write_buffer_idx===this.write_buffers.length){dbg_log("Device<"+this.virtio.name+"> Write more than device-writable capacity",LOG_VIRTIO);break}var s=this.write_buffers[this.write_buffer_idx];let r=s.addr_low+this.write_buffer_offset;(s=s.len-this.write_buffer_offset)>i?(s=i,this.write_buffer_offset+=i):(this.write_buffer_idx++,this.write_buffer_offset=0),this.cpu.write_blob(t.subarray(e,e+s),r),e+=s,i-=s}return this.length_written+=e,e},At.prototype.handle_message=function(t){this.bus&&this.bus.send("net0-receive",new Uint8Array(t.data))},At.prototype.handle_close=function(t){this.connect(),setTimeout(this.connect.bind(this),this.reconnect_interval)},At.prototype.handle_open=function(t){for(t=0;t<this.send_queue.length;t++)this.send(this.send_queue[t]);this.send_queue=[]},At.prototype.handle_error=function(t){},At.prototype.destroy=function(){this.socket&&this.socket.close()},At.prototype.connect=function(){if("undefined"!=typeof WebSocket){if(this.socket){var t=this.socket.readyState;if(0===t||1===t)return}t=Date.now(),this.last_connect_attempt+this.reconnect_interval>t||(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))}},At.prototype.send=function(t){this.socket&&1===this.socket.readyState?this.socket.send(t):(this.send_queue.push(t),this.send_queue.length>2*this.send_queue_limit&&(this.send_queue=this.send_queue.slice(-this.send_queue_limit)),this.connect())},At.prototype.change_proxy=function(t){this.url=t,this.socket&&(this.socket.onclose=function(){},this.socket.onerror=function(){},this.socket.close(),this.socket=void 0)};function kt(t){function e(t){return!t.altKey&&_[56]&&a(56,!1),r(t,!1)}function i(t){return!t.altKey&&_[56]&&a(56,!1),r(t,!0)}function s(t){t=Object.keys(_);for(var e,i=0;i<t.length;i++)e=+t[i],_[e]&&a(e,!1);_={}}function r(t,e){var i;if((i=n.bus)&&(i=!(t.shiftKey&&t.ctrlKey&&(73===t.keyCode||74===t.keyCode||75===t.keyCode)||!n.emu_enabled)&&(!t.target||(t.target.classList.contains("phone_keyboard")||"INPUT"!==t.target.nodeName&&"TEXTAREA"!==t.target.nodeName))),i){if(void 0!==t.code&&void 0!==(i=p[t.code])||(i=h[t.keyCode]),i)return a(i,e,t.repeat),t.preventDefault&&t.preventDefault(),!1;console.log("Missing char in map: keyCode="+(t.keyCode||-1).toString(16)+" code="+t.code)}}function a(t,e,i){if(e)_[t]&&!i&&a(t,!1);else if(!_[t])return;(_[t]=e)||(t|=128),255<t?(o(t>>8),o(255&t)):o(t)}function o(t){n.bus.send("keyboard-code",t)}var _={},n=this;this.emu_enabled=!0;var h=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]),d={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},c={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},p={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=t,this.destroy=function(){"undefined"!=typeof window&&(window.removeEventListener("keyup",e,!1),window.removeEventListener("keydown",i,!1),window.removeEventListener("blur",s,!1))},this.init=function(){"undefined"!=typeof window&&(this.destroy(),window.addEventListener("keyup",e,!1),window.addEventListener("keydown",i,!1),window.addEventListener("blur",s,!1))},this.init(),this.simulate_press=function(t){r(t={keyCode:t},!0),r(t,!1)},this.simulate_char=function(t){var e=t.charCodeAt(0);e in d?this.simulate_press(d[e]):e in c?(o(42),this.simulate_press(c[e]),o(170)):console.log("ascii -> keyCode not found: ",e,t)}}function It(t,e){function i(t){if(!f.enabled||!f.emu_enabled)return!1;var i,s=e||document.body;if(!(i=document.pointerLockElement))t:{for(t=t.target;t.parentNode;){if(t===s){i=!0;break t}t=t.parentNode}i=!1}return i}function s(t){i(t)&&(t=t.changedTouches)&&t.length&&(t=t[t.length-1],u=t.clientX,l=t.clientY)}function r(t){(d||p||c)&&(f.bus.send("mouse-click",[!1,!1,!1]),d=p=c=!1)}function a(t){if(f.bus&&i(t)){var s=0,r=0,a=t.changedTouches;a?a.length&&(s=(a=a[a.length-1]).clientX-u,r=a.clientY-l,u=a.clientX,l=a.clientY,t.preventDefault()):"number"==typeof t.movementX?(s=t.movementX,r=t.movementY):"number"==typeof t.webkitMovementX?(s=t.webkitMovementX,r=t.webkitMovementY):"number"==typeof t.mozMovementX?(s=t.mozMovementX,r=t.mozMovementY):(s=t.clientX-u,r=t.clientY-l,u=t.clientX,l=t.clientY),f.bus.send("mouse-delta",[.15*s,-.15*r]),e&&f.bus.send("mouse-absolute",[t.pageX-e.offsetLeft,t.pageY-e.offsetTop,e.offsetWidth,e.offsetHeight])}}function o(t){i(t)&&n(t,!0)}function _(t){i(t)&&n(t,!1)}function n(t,e){f.bus&&(1===t.which?d=e:2===t.which?p=e:3===t.which?c=e:dbg_log("Unknown event.which: "+t.which),f.bus.send("mouse-click",[d,p,c]),t.preventDefault())}function h(t){if(i(t)){var e=t.wheelDelta||-t.detail;0>e?e=-1:0<e&&(e=1),f.bus.send("mouse-wheel",[e,0]),t.preventDefault()}}var d=!1,c=!1,p=!1,u=0,l=0,f=this;this.enabled=!1,this.emu_enabled=!0,this.bus=t,this.bus.register("mouse-enable",(function(t){this.enabled=t}),this),this.destroy=function(){"undefined"!=typeof window&&(window.removeEventListener("touchstart",s,!1),window.removeEventListener("touchend",r,!1),window.removeEventListener("touchmove",a,!1),window.removeEventListener("mousemove",a,!1),window.removeEventListener("mousedown",o,!1),window.removeEventListener("mouseup",_,!1),window.removeEventListener("DOMMouseScroll",h,!1),window.removeEventListener("mousewheel",h,{passive:!1}))},this.init=function(){"undefined"!=typeof window&&(this.destroy(),window.addEventListener("touchstart",s,!1),window.addEventListener("touchend",r,!1),window.addEventListener("touchmove",a,!1),window.addEventListener("mousemove",a,!1),window.addEventListener("mousedown",o,!1),window.addEventListener("mouseup",_,!1),window.addEventListener("DOMMouseScroll",h,!1),window.addEventListener("mousewheel",h,{passive:!1}))},this.init()}function Ot(t,e){function i(t){return t=t.toString(16),"#"+"0".repeat(6-t.length)+t}function s(t,e,i,s){t.style.width="",t.style.height="",s&&(t.style.transform="");var r=t.getBoundingClientRect();s?t.style.transform=(1===e?"":" scaleX("+e+")")+(1===i?"":" scaleY("+i+")"):(0==e%1&&0==i%1?(d.style.imageRendering="crisp-edges",d.style.imageRendering="pixelated",d.style["-ms-interpolation-mode"]="nearest-neighbor"):(d.style.imageRendering="",d.style["-ms-interpolation-mode"]=""),0!=(s=window.devicePixelRatio||1)%1&&(e/=s,i/=s)),1!==e&&(t.style.width=r.width*e+"px"),1!==i&&(t.style.height=r.height*i+"px")}console.assert(t,"1st argument must be a DOM container");var r,a,o,_,n,h,d=t.getElementsByTagName("canvas")[0],c=d.getContext("2d",{alpha:!1}),p=t.getElementsByTagName("div")[0],u=document.createElement("div"),l=1,f=1,m=1,g=!1,b=!1,y=this;t=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 v,w=new Uint16Array([32,9786,9787,9829,9830,9827,9824,8226,9688,9675,9689,9794,9792,9834,9835,9788,9658,9668,8597,8252,182,167,9644,8616,8593,8595,8594,8592,8735,8596,9650,9660]),x=[],A=0;256>A;A++)v=127<A?t[A-128]:32>A?w[A]:A,x[A]=String.fromCharCode(v);c.imageSmoothingEnabled=!1,u.style.position="absolute",u.style.backgroundColor="#ccc",u.style.width="7px",u.style.display="inline-block",p.style.display="block",d.style.display="none",this.bus=e,e.register("screen-set-mode",(function(t){this.set_mode(t)}),this),e.register("screen-fill-buffer-end",(function(t){this.update_buffer(t)}),this),e.register("screen-put-char",(function(t){this.put_char(t[0],t[1],t[2],t[3],t[4])}),this),e.register("screen-update-cursor",(function(t){this.update_cursor(t[0],t[1])}),this),e.register("screen-update-cursor-scanline",(function(t){this.update_cursor_scanline(t[0],t[1])}),this),e.register("screen-clear",(function(){this.clear_screen()}),this),e.register("screen-set-size-text",(function(t){this.set_size_text(t[0],t[1])}),this),e.register("screen-set-size-graphical",(function(t){this.set_size_graphical(t[0],t[1],t[2],t[3])}),this),this.init=function(){this.set_size_text(80,25),this.timer()},this.make_screenshot=function(){try{let t=new Image;t.src=d.toDataURL("image/png"),window.open("").document.write(t.outerHTML)}catch(t){}},this.put_char=function(t,e,i,s,r){t<h&&e<n&&(_[e=3*(t*n+e)]=i,_[e+1]=s,_[e+2]=r,o[t]=1)},this.timer=function(){b||requestAnimationFrame(g?I:k)};var k=function(){for(var t=0;t<h;t++)o[t]&&(y.text_update_row(t),o[t]=0);this.timer()}.bind(this),I=function(){this.bus.send("screen-fill-buffer"),this.timer()}.bind(this);this.destroy=function(){b=!0},this.set_mode=function(t){(g=t)?(p.style.display="none",d.style.display="block"):(p.style.display="block",d.style.display="none")},this.clear_screen=function(){c.fillStyle="#000",c.fillRect(0,0,d.width,d.height)},this.set_size_text=function(t,e){if(t!==n||e!==h){for(o=new Int8Array(e),_=new Int32Array(t*e*3),n=t,h=e;p.childNodes.length>e;)p.removeChild(p.firstChild);for(;p.childNodes.length<e;)p.appendChild(document.createElement("div"));for(t=0;t<e;t++)this.text_update_row(t);s(p,l,f,!0)}},this.set_size_graphical=function(t,e,i,r){d.style.display="block",d.width=t,d.height=e,m=640>=t&&2*t<window.innerWidth&&2*t<window.innerHeight?2:1,s(d,l*m,f*m,!1)},this.set_scale=function(t,e){s(p,l=t,f=e,!0),s(d,l*m,f*m,!1)},this.set_scale(l,f),this.update_cursor_scanline=function(t,e){32&t?u.style.display="none":(u.style.display="inline",u.style.height=Math.min(15,e-t)+"px",u.style.marginTop=Math.min(15,t)+"px")},this.update_cursor=function(t,e){t===r&&e===a||(o[t]=1,o[r]=1,r=t,a=e)},this.text_update_row=function(t){for(var e,s=3*t*n,o=p.childNodes[t],h=document.createElement("div"),d=0;d<n;){var c=document.createElement("span"),l=_[s+1],f=_[s+2];for(c.style.backgroundColor=i(l),c.style.color=i(f),e="";d<n&&_[s+1]===l&&_[s+2]===f;){var m=_[s];if(e+=x[m],x[m],d++,s+=3,t===r){if(d===a)break;if(d===a+1){h.appendChild(u);break}}}c.textContent=e,h.appendChild(c)}o.parentNode.replaceChild(h,o)},this.update_buffer=function(t){t.forEach((t=>{c.putImageData(t.image_data,t.screen_x-t.buffer_x,t.screen_y-t.buffer_y,t.buffer_x,t.buffer_y,t.buffer_width,t.buffer_height)}))},this.init()}function Rt(t){var e,i,s;this.bus=t,t.register("screen-set-mode",(function(t){this.set_mode(t)}),this),t.register("screen-fill-buffer-end",(function(t){this.update_buffer(t[0],t[1])}),this),t.register("screen-put-char",(function(t){this.put_char(t[0],t[1],t[2],t[3],t[4])}),this),t.register("screen-text-scroll",(function(t){console.log("scroll",t)}),this),t.register("screen-update-cursor",(function(t){this.update_cursor(t[0],t[1])}),this),t.register("screen-update-cursor-scanline",(function(t){this.update_cursor_scanline(t[0],t[1])}),this),t.register("screen-set-size-text",(function(t){this.set_size_text(t[0],t[1])}),this),t.register("screen-set-size-graphical",(function(t){this.set_size_graphical(t[0],t[1])}),this),this.put_char=function(t,r,a,o,_){t<s&&r<i&&(e[t=3*(t*i+r)]=a,e[t+1]=o,e[t+2]=_)},this.destroy=function(){},this.set_mode=function(t){},this.clear_screen=function(){},this.set_size_text=function(t,r){t===i&&r===s||(e=new Int32Array(t*r*3),i=t,s=r)},this.set_size_graphical=function(t,e){},this.set_scale=function(t,e){},this.update_cursor_scanline=function(t,e){},this.update_cursor=function(t,e){},this.update_buffer=function(t,e){},this.get_text_screen=function(){for(var t=[],e=0;e<s;e++)t.push(this.get_text_row(e));return t},this.get_text_row=function(t){var s="";t=3*t*i;for(var r=0;r<i;r++)s+=String.fromCharCode(e[t+3*r]);return s}}function Et(t,e){function i(t){o.bus&&o.enabled&&(o.send_char(t.which),t.preventDefault())}function s(t){var e=t.which;8===e?(o.send_char(127),t.preventDefault()):9===e&&(o.send_char(9),t.preventDefault())}function r(t){if(o.enabled){for(var e=t.clipboardData.getData("text/plain"),i=0;i<e.length;i++)o.send_char(e.charCodeAt(i));t.preventDefault()}}function a(e){e.target!==t&&t.blur()}var o=this;this.enabled=!0,this.bus=e,this.text="",this.text_new_line=!1,this.last_update=0,this.bus.register("serial0-output-char",(function(t){this.show_char(t)}),this),this.destroy=function(){t.removeEventListener("keypress",i,!1),t.removeEventListener("keydown",s,!1),t.removeEventListener("paste",r,!1),window.removeEventListener("mousedown",a,!1)},this.init=function(){this.destroy(),t.style.display="block",t.addEventListener("keypress",i,!1),t.addEventListener("keydown",s,!1),t.addEventListener("paste",r,!1),window.addEventListener("mousedown",a,!1)},this.init(),this.show_char=function(t){"\b"===t?(this.text=this.text.slice(0,-1),this.update()):"\r"!==t&&(this.text+=t,"\n"===t&&(this.text_new_line=!0),this.update())},this.update=function(){var t=Date.now(),e=t-this.last_update;16>e?void 0===this.update_timer&&(this.update_timer=setTimeout((()=>{this.update_timer=void 0;var t=Date.now();dbg_assert(15<=t-this.last_update),this.last_update=t,this.render()}),16-e)):(void 0!==this.update_timer&&(clearTimeout(this.update_timer),this.update_timer=void 0),this.last_update=t,this.render())},this.render=function(){t.value=this.text,this.text_new_line&&(this.text_new_line=!1,t.scrollTop=1e9)},this.send_char=function(t){o.bus&&o.bus.send("serial0-input",t)}}function Ct(t,e){if(this.element=t,window.Terminal){var i=this.term=new window.Terminal;i.setOption("logLevel","off"),i.write("This is the serial console. Whatever you type or paste here will be sent to COM1"),i.onData((function(t){for(let i=0;i<t.length;i++)e.send("serial0-input",t.charCodeAt(i))})),e.register("serial0-output-char",(function(t){i.write(t)}),this)}}Ct.prototype.show=function(){this.term&&this.term.open(this.element)};function Tt(t){if("undefined"!=typeof window)if(window.AudioContext||window.webkitAudioContext){var e=window.AudioWorklet?Dt:Pt;this.bus=t,this.audio_context=window.AudioContext?new AudioContext:new webkitAudioContext,this.mixer=new qt(t,this.audio_context),this.pcspeaker=new St(t,this.audio_context,this.mixer),this.dac=new e(t,this.audio_context,this.mixer),this.pcspeaker.start(),t.register("emulator-stopped",(function(){this.audio_context.suspend()}),this),t.register("emulator-started",(function(){this.audio_context.resume()}),this),t.register("speaker-confirm-initialized",(function(){t.send("speaker-has-initialized")}),this),t.send("speaker-has-initialized")}else console.warn("Web browser doesn't support Web Audio API")}function qt(t,e){function i(t){return function(e){t.gain.setValueAtTime(e,this.audio_context.currentTime)}}this.audio_context=e,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),t.register("mixer-connect",(function(t){this.connect_source(t[0],t[1])}),this),t.register("mixer-disconnect",(function(t){this.disconnect_source(t[0],t[1])}),this),t.register("mixer-volume",(function(t){var e=t[0],i=t[1];t=Math.pow(10,t[2]/20),void 0!==(e=0===e?this:this.sources.get(e))&&e.set_volume(t,i)}),this),t.register("mixer-gain-left",(function(t){this.gain_left=Math.pow(10,t/20),this.update()}),this),t.register("mixer-gain-right",(function(t){this.gain_right=Math.pow(10,t/20),this.update()}),this),t.register("mixer-treble-left",i(this.node_treble_left),this),t.register("mixer-treble-right",i(this.node_treble_right),this),t.register("mixer-bass-left",i(this.node_bass_left),this),t.register("mixer-bass-right",i(this.node_bass_right),this)}function Lt(t,e,i,s){this.audio_context=t,this.connected_right=this.connected_left=!0,this.volume_right=this.volume_left=this.volume_both=this.gain_hidden=1,this.node_splitter=t.createChannelSplitter(2),this.node_gain_left=t.createGain(),this.node_gain_right=t.createGain(),e.connect(this.node_splitter),this.node_splitter.connect(this.node_gain_left,0),this.node_gain_left.connect(i),this.node_splitter.connect(this.node_gain_right,1),this.node_gain_right.connect(s)}function St(t,e,i){this.node_oscillator=e.createOscillator(),this.node_oscillator.type="square",this.node_oscillator.frequency.setValueAtTime(440,e.currentTime),this.mixer_connection=i.add_source(this.node_oscillator,1),this.mixer_connection.disconnect(),t.register("pcspeaker-enable",(function(){i.connect_source(1)}),this),t.register("pcspeaker-disable",(function(){i.disconnect_source(1)}),this),t.register("pcspeaker-update",(function(t){var i=t[1],s=0;3===t[0]&&(s=1e3*1193.1816666/i,s=Math.min(s,this.node_oscillator.frequency.maxValue),s=Math.max(s,0)),this.node_oscillator.frequency.setValueAtTime(s,e.currentTime)}),this)}function Dt(t,e,i){this.bus=t,this.audio_context=e,this.enabled=!1,this.sampling_rate=48e3;var s=(e=function(){function t(t){return 0===t?1:(t*=Math.PI,Math.sin(t)/t)}function e(){var t=Reflect.construct(AudioWorkletProcessor,[],e);return t.kernel_size=3,t.queue_data=Array(1024),t.queue_start=0,t.queue_end=0,t.queue_length=0,t.queue_size=t.queue_data.length,t.queued_samples=0,t.source_buffer_previous=i,t.source_buffer_current=i,t.source_samples_per_destination=1,t.source_block_start=0,t.source_time=0,t.source_offset=0,t.port.onmessage=e=>{switch(e.data.type){case"queue":t.queue_push(e.data.value);break;case"sampling-rate":t.source_samples_per_destination=e.data.value/sampleRate}},t}var i=[new Float32Array(256),new Float32Array(256)];Reflect.setPrototypeOf(e.prototype,AudioWorkletProcessor.prototype),Reflect.setPrototypeOf(e,AudioWorkletProcessor),e.prototype.process=e.prototype.process=function(t,e,i){for(t=0;t<e[0][0].length;t++){for(var s=i=0,r=this.source_offset+this.kernel_size,a=this.source_offset-this.kernel_size+1;a<=r;a++){var o=this.source_block_start+a;i+=this.get_sample(o,0)*this.kernel(this.source_time-a),s+=this.get_sample(o,1)*this.kernel(this.source_time-a)}(isNaN(i)||isNaN(s))&&(i=s=0,this.dbg_log("ERROR: NaN values! Ignoring for now.")),e[0][0][t]=i,e[0][1][t]=s,this.source_time+=this.source_samples_per_destination,this.source_offset=Math.floor(this.source_time)}return e=this.source_offset,e+=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(e),!0},e.prototype.kernel=function(e){return t(e)*t(e/this.kernel_size)},e.prototype.get_sample=function(t,e){return 0>t?(t+=this.source_buffer_previous[0].length,this.source_buffer_previous[e][t]):this.source_buffer_current[e][t]},e.prototype.ensure_enough_data=function(t){var e=this.source_buffer_current[0].length;e-this.source_block_start<t&&(this.prepare_next_buffer(),this.source_block_start-=e)},e.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 t=this.source_buffer_current[0].length;if(256>t){for(var e=this.queue_start,i=0;256>t&&i<this.queue_length;)t+=this.queue_data[e][0].length,e=e+1&this.queue_size-1,i++;t=Math.max(t,256),(t=[new Float32Array(t),new Float32Array(t)])[0].set(this.source_buffer_current[0]),t[1].set(this.source_buffer_current[1]),e=this.source_buffer_current[0].length;for(var s=0;s<i;s++){var r=this.queue_shift();t[0].set(r[0],e),t[1].set(r[1],e),e+=r[0].length}this.source_buffer_current=t}this.pump()},e.prototype.pump=function(){1024>this.queued_samples/this.source_samples_per_destination&&this.port.postMessage({type:"pump"})},e.prototype.queue_push=function(t){this.queue_length<this.queue_size&&(this.queue_data[this.queue_end]=t,this.queue_end=this.queue_end+1&this.queue_size-1,this.queue_length++,this.queued_samples+=t[0].length,this.pump())},e.prototype.queue_shift=function(){if(!this.queue_length)return i;var t=this.queue_data[this.queue_start];return this.queue_data[this.queue_start]=null,this.queue_start=this.queue_start+1&this.queue_size-1,this.queue_length--,this.queued_samples-=t[0].length,t},e.prototype.dbg_log=function(t){},registerProcessor("dac-processor",e)}.toString()).indexOf("{")+1,r=e.lastIndexOf("}");e=e.substring(s,r),e=new Blob([e],{type:"application/javascript"});var a=URL.createObjectURL(e);this.node_processor=null,this.node_output=this.audio_context.createGain(),this.audio_context.audioWorklet.addModule(a).then((()=>{URL.revokeObjectURL(a),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=t=>{switch(t.data.type){case"pump":this.pump();break;case"debug-log":_("SpeakerWorkletDAC - Worklet: "+t.data.value)}},this.node_processor.connect(this.node_output)})),this.mixer_connection=i.add_source(this.node_output,2),this.mixer_connection.set_gain_hidden(3),t.register("dac-send-data",(function(t){this.queue(t)}),this),t.register("dac-enable",(function(t){this.enabled=!0}),this),t.register("dac-disable",(function(){this.enabled=!1}),this),t.register("dac-tell-sampling-rate",(function(t){this.sampling_rate=t,this.node_processor&&this.node_processor.port.postMessage({type:"sampling-rate",value:t})}),this)}function Pt(t,e,i){this.bus=t,this.audio_context=e,this.enabled=!1,this.sampling_rate=22050,this.buffered_time=0,this.rate_ratio=1,this.node_lowpass=this.audio_context.createBiquadFilter(),this.node_lowpass.type="lowpass",this.node_output=this.node_lowpass,this.mixer_connection=i.add_source(this.node_output,2),this.mixer_connection.set_gain_hidden(3),t.register("dac-send-data",(function(t){this.queue(t)}),this),t.register("dac-enable",(function(t){this.enabled=!0,this.pump()}),this),t.register("dac-disable",(function(){this.enabled=!1}),this),t.register("dac-tell-sampling-rate",(function(t){this.sampling_rate=t,this.rate_ratio=Math.ceil(8e3/t),this.node_lowpass.frequency.setValueAtTime(t/2,this.audio_context.currentTime)}),this)}function zt(t){this.cpu_is_running=!1;var e,i,s=r.create();this.bus=s[0],this.emulator_bus=s[1];let a=new WebAssembly.Table({element:"anyfunc",initial:1924}),o={cpu_exception_hook:t=>this.cpu_exception_hook&&this.cpu_exception_hook(t),hlt_op:function(){return e.hlt_op()},abort:function(){},microtick:O.microtick,get_rand_int:function(){return c.get_rand_int()},pic_acknowledge:function(){e.pic_acknowledge()},io_port_read8:function(t){return e.io.port_read8(t)},io_port_read16:function(t){return e.io.port_read16(t)},io_port_read32:function(t){return e.io.port_read32(t)},io_port_write8:function(t,i){e.io.port_write8(t,i)},io_port_write16:function(t,i){e.io.port_write16(t,i)},io_port_write32:function(t,i){e.io.port_write32(t,i)},mmap_read8:function(t){return e.mmap_read8(t)},mmap_read16:function(t){return e.mmap_read16(t)},mmap_read32:function(t){return e.mmap_read32(t)},mmap_write8:function(t,i){e.mmap_write8(t,i)},mmap_write16:function(t,i){e.mmap_write16(t,i)},mmap_write32:function(t,i){e.mmap_write32(t,i)},mmap_write64:function(t,i,s){e.mmap_write64(t,i,s)},mmap_write128:function(t,i,s,r,a){e.mmap_write128(t,i,s,r,a)},log_from_wasm:function(t,e){t=c.read_sized_string_from_mem(i,t,e),_(t,LOG_CPU)},console_log_from_wasm:function(t,e){t=c.read_sized_string_from_mem(i,t,e),console.error(t)},dbg_trace_from_wasm:function(){},codegen_finalize:(t,i,s,r,a)=>{e.codegen_finalize(t,i,s,r,a)},jit_clear_func:t=>e.jit_clear_func(t),jit_clear_all_funcs:()=>e.jit_clear_all_funcs(),__indirect_function_table:a},n="v86.wasm",h="v86-fallback.wasm";t.wasm_path?(n=t.wasm_path,s=n.lastIndexOf("/"),h=(-1===s?"":n.substr(0,s))+"/"+h):"undefined"==typeof window&&"string"==typeof __dirname?(n=__dirname+"/"+n,h=__dirname+"/"+h):(n="build/"+n,h="build/"+h),t.wasm_fn?t.wasm_fn({env:o}).then((s=>{i=s.memory,s.rust_init(),s=this.v86=new O(this.emulator_bus,{exports:s,wasm_table:a}),e=s.cpu,this.continue_init(s,t)})):c.load_file(n,{done:s=>{WebAssembly.instantiate(s,{env:o}).then((({instance:s})=>{s=s.exports,i=s.memory,s.rust_init(),s=this.v86=new O(this.emulator_bus,{exports:s,wasm_table:a}),e=s.cpu,this.continue_init(s,t)}),(s=>{c.load_file(h,{done:s=>{WebAssembly.instantiate(s,{env:o}).then((({instance:s})=>{s=s.exports,i=s.memory,s.rust_init(),s=this.v86=new O(this.emulator_bus,{exports:s,wasm_table:a}),e=s.cpu,this.continue_init(s,t)}))}})}))},progress:t=>{this.emulator_bus.send("download-progress",{file_index:0,file_count:1,file_name:n,lengthComputable:t.lengthComputable,total:t.total,loaded:t.loaded})}})}function Mt(t){this.message=t||"File already exists"}function Ut(t){this.message=t||"File not found"}qt.prototype.add_source=function(t,e){return t=new Lt(this.audio_context,t,this.input_left,this.input_right),this.sources.has(e),this.sources.set(e,t),t},qt.prototype.connect_source=function(t,e){void 0!==(t=this.sources.get(t))&&t.connect(e)},qt.prototype.disconnect_source=function(t,e){void 0!==(t=this.sources.get(t))&&t.disconnect(e)},qt.prototype.set_volume=function(t,e){switch(void 0===e&&(e=2),e){case 0:this.volume_left=t;break;case 1:this.volume_right=t;break;case 2:this.volume_both=t;break;default:return}this.update()},qt.prototype.update=function(){var t=this.volume_both*this.volume_right*this.gain_right;this.node_gain_left.gain.setValueAtTime(this.volume_both*this.volume_left*this.gain_left,this.audio_context.currentTime),this.node_gain_right.gain.setValueAtTime(t,this.audio_context.currentTime)},Lt.prototype.update=function(){var t=this.connected_right*this.gain_hidden*this.volume_both*this.volume_right;this.node_gain_left.gain.setValueAtTime(this.connected_left*this.gain_hidden*this.volume_both*this.volume_left,this.audio_context.currentTime),this.node_gain_right.gain.setValueAtTime(t,this.audio_context.currentTime)},Lt.prototype.connect=function(t){var e=!t||2===t;(e||0===t)&&(this.connected_left=!0),(e||1===t)&&(this.connected_right=!0),this.update()},Lt.prototype.disconnect=function(t){var e=!t||2===t;(e||0===t)&&(this.connected_left=!1),(e||1===t)&&(this.connected_right=!1),this.update()},Lt.prototype.set_volume=function(t,e){switch(void 0===e&&(e=2),e){case 0:this.volume_left=t;break;case 1:this.volume_right=t;break;case 2:this.volume_both=t;break;default:return}this.update()},Lt.prototype.set_gain_hidden=function(t){this.gain_hidden=t},St.prototype.start=function(){this.node_oscillator.start()},Dt.prototype.queue=function(t){this.node_processor&&this.node_processor.port.postMessage({type:"queue",value:t},[t[0].buffer,t[1].buffer])},Dt.prototype.pump=function(){this.enabled&&this.bus.send("dac-request-data")},Pt.prototype.queue=function(t){var e=t[0].length,i=e/this.sampling_rate;if(1<this.rate_ratio)for(var s=this.audio_context.createBuffer(2,e*this.rate_ratio,this.sampling_rate*this.rate_ratio),r=s.getChannelData(0),a=s.getChannelData(1),o=0,n=0;n<e;n++)for(var h=0;h<this.rate_ratio;h++,o++)r[o]=t[0][n],a[o]=t[1][n];else(s=this.audio_context.createBuffer(2,e,this.sampling_rate)).copyToChannel?(s.copyToChannel(t[0],0),s.copyToChannel(t[1],1)):(s.getChannelData(0).set(t[0]),s.getChannelData(1).set(t[1]));if((t=this.audio_context.createBufferSource()).buffer=s,t.connect(this.node_lowpass),t.addEventListener("ended",this.pump.bind(this)),s=this.audio_context.currentTime,this.buffered_time<s)for(_("Speaker DAC - Creating/Recreating reserve - shouldn't occur frequently during playback"),this.buffered_time=s,s=.2-i,e=0;e<=s;)e+=i,this.buffered_time+=i,setTimeout((()=>this.pump()),1e3*e);t.start(this.buffered_time),this.buffered_time+=i,setTimeout((()=>this.pump()),0)},Pt.prototype.pump=function(){this.enabled&&(this.buffered_time-this.audio_context.currentTime>.2||this.bus.send("dac-request-data"))},zt.prototype.continue_init=async function(t,e){function i(t,e){switch(t){case"hda":r.hda=this.disk_images.hda=e;break;case"hdb":r.hdb=this.disk_images.hdb=e;break;case"cdrom":r.cdrom=this.disk_images.cdrom=e;break;case"fda":r.fda=this.disk_images.fda=e;break;case"fdb":r.fdb=this.disk_images.fdb=e;break;case"multiboot":r.multiboot=this.disk_images.multiboot=e.buffer;break;case"bzimage":r.bzimage=this.disk_images.bzimage=e.buffer;break;case"initrd":r.initrd=this.disk_images.initrd=e.buffer;break;case"bios":r.bios=e.buffer;break;case"vga_bios":r.vga_bios=e.buffer;break;case"initial_state":r.initial_state=e.buffer;break;case"fs9p_json":r.fs9p_json=e}}function s(){function s(){this.serial_adapter&&this.serial_adapter.show&&this.serial_adapter.show(),this.bus.send("cpu-init",r),r.initial_state&&(t.restore_state(r.initial_state),r.initial_state=void 0),e.autostart&&this.bus.send("cpu-run"),this.emulator_bus.send("emulator-loaded")}if(r.fs9p&&r.fs9p_json)if(r.initial_state?_("Filesystem basefs ignored: Overridden by state image"):r.fs9p.load_from_json(r.fs9p_json),e.bzimage_initrd_from_filesystem){let{bzimage:t,initrd:e}=this.get_bzimage_initrd_from_filesystem(r.fs9p);_("Found bzimage: "+t+" and initrd: "+e),Promise.all([r.fs9p.read_file(e),r.fs9p.read_file(t)]).then((([t,e])=>{i.call(this,"initrd",new u(t.buffer)),i.call(this,"bzimage",new u(e.buffer)),s.call(this)}))}else s.call(this);else console.assert(!e.bzimage_initrd_from_filesystem,"bzimage_initrd_from_filesystem: Requires a filesystem"),s.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 r={};this.disk_images={fda:void 0,fdb:void 0,hda:void 0,hdb:void 0,cdrom:void 0},r.acpi=e.acpi,r.load_devices=!0,r.log_level=e.log_level,r.memory_size=e.memory_size||67108864,r.vga_memory_size=e.vga_memory_size||8388608,r.boot_order=e.boot_order||531,r.fastboot=e.fastboot||!1,r.fda=void 0,r.fdb=void 0,r.uart1=e.uart1,r.uart2=e.uart2,r.uart3=e.uart3,r.cmdline=e.cmdline,r.preserve_mac_from_state_image=e.preserve_mac_from_state_image,e.network_adapter?this.network_adapter=e.network_adapter(this.bus):e.network_relay_url&&(this.network_adapter=new At(e.network_relay_url,this.bus)),r.enable_ne2k=!0,e.disable_keyboard||(this.keyboard_adapter=new kt(this.bus)),e.disable_mouse||(this.mouse_adapter=new It(this.bus,e.screen_container)),e.screen_container?this.screen_adapter=new Ot(e.screen_container,this.bus):e.screen_dummy&&(this.screen_adapter=new Rt(this.bus)),e.serial_container&&(this.serial_adapter=new Et(e.serial_container,this.bus)),e.serial_container_xtermjs&&(this.serial_adapter=new Ct(e.serial_container_xtermjs,this.bus)),e.disable_speaker||(this.speaker_adapter=new Tt(this.bus));var a,o,n=[];e.state&&console.warn("Warning: Unknown option 'state'. Did you mean 'initial_state'?");for(var h="bios vga_bios cdrom hda hdb fda fdb initial_state multiboot bzimage initrd".split(" "),d=0;d<h.length;d++)a=h[d],(o=e[h[d]])&&(o.get&&o.set&&o.load?n.push({name:a,loadable:o}):(o={buffer:o.buffer,async:o.async,url:o.url,size:o.size,fixed_chunk_size:o.fixed_chunk_size,use_parts:o.use_parts},"bios"!==a&&"vga_bios"!==a&&"initial_state"!==a&&"multiboot"!==a&&"bzimage"!==a&&"initrd"!==a||(o.async=!1),o.buffer instanceof ArrayBuffer?(o=new u(o.buffer),n.push({name:a,loadable:o})):"undefined"!=typeof File&&o.buffer instanceof File?(void 0===o.async&&(o.async=268435456<=o.buffer.size),o=o.async?new c.AsyncFileBuffer(o.buffer):new c.SyncFileBuffer(o.buffer),n.push({name:a,loadable:o})):o.url?o.async?(o=o.use_parts?new c.AsyncXHRPartfileBuffer(o.url,o.size,o.fixed_chunk_size):new c.AsyncXHRBuffer(o.url,o.size),n.push({name:a,loadable:o})):n.push({name:a,url:o.url,size:o.size}):_("Ignored file: url="+o.url+" buffer="+o.buffer)));if(e.filesystem){h=e.filesystem.basefs,d=e.filesystem.baseurl;let t=new MemoryFileStorage;if(d&&(t=new ServerFileStorageWrapper(t,d)),r.fs9p=this.fs9p=new FS(t),h){if(console.assert(d,"Filesystem: baseurl must be specified"),"object"==typeof h){var p=h.size;h=h.url}n.push({name:"fs9p_json",url:h,size:p,as_json:!0})}}var l=this,f=n.length,m=function(t){if(t===f)setTimeout(s.bind(this),0);else{var e=n[t];e.loadable?(e.loadable.onload=function(s){i.call(this,e.name,e.loadable),m(t+1)}.bind(this),e.loadable.load()):c.load_file(e.url,{done:function(s){i.call(this,e.name,e.as_json?s:new u(s)),m(t+1)}.bind(this),progress:function(i){200===i.target.status?l.emulator_bus.send("download-progress",{file_index:t,file_count:f,file_name:e.url,lengthComputable:i.lengthComputable,total:i.total||e.size,loaded:i.loaded}):l.emulator_bus.send("download-error",{file_index:t,file_count:f,file_name:e.url,request:i.target})},as_json:e.as_json})}}.bind(this);m(0)},zt.prototype.get_bzimage_initrd_from_filesystem=function(t){let e,i,s=(t.read_dir("/")||[]).map((t=>"/"+t));t=(t.read_dir("/boot/")||[]).map((t=>"/boot/"+t));for(let r of[].concat(s,t)){let t=/old/i.test(r)||/fallback/i.test(r),s=/vmlinuz/i.test(r)||/bzimage/i.test(r),a=/initrd/i.test(r)||/initramfs/i.test(r);!s||i&&t||(i=r),!a||e&&t||(e=r)}return e&&i||(console.log("Failed to find bzimage or initrd in filesystem. Files:"),console.log(s.join(" ")),console.log(t.join(" "))),{initrd:e,bzimage:i}},zt.prototype.run=function(){this.bus.send("cpu-run")},d.exportProperty(zt.prototype,"run",zt.prototype.run),zt.prototype.stop=function(){this.bus.send("cpu-stop")},d.exportProperty(zt.prototype,"stop",zt.prototype.stop),zt.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()},d.exportProperty(zt.prototype,"destroy",zt.prototype.destroy),zt.prototype.restart=function(){this.bus.send("cpu-restart")},d.exportProperty(zt.prototype,"restart",zt.prototype.restart),zt.prototype.add_listener=function(t,e){this.bus.register(t,e,this)},d.exportProperty(zt.prototype,"add_listener",zt.prototype.add_listener),zt.prototype.remove_listener=function(t,e){this.bus.unregister(t,e)},d.exportProperty(zt.prototype,"remove_listener",zt.prototype.remove_listener),zt.prototype.restore_state=function(t){this.v86.restore_state(t)},d.exportProperty(zt.prototype,"restore_state",zt.prototype.restore_state),zt.prototype.save_state=function(t){setTimeout(function(){try{t(null,this.v86.save_state())}catch(e){t(e,null)}}.bind(this),0)},d.exportProperty(zt.prototype,"save_state",zt.prototype.save_state),zt.prototype.get_statistics=function(){console.warn("V86Starter.prototype.get_statistics is deprecated. Use events instead.");var t={cpu:{instruction_counter:this.get_instruction_counter()}};if(!this.v86)return t;var e=this.v86.cpu.devices;return e.hda&&(t.hda=e.hda.stats),e.cdrom&&(t.cdrom=e.cdrom.stats),e.ps2&&(t.mouse={enabled:e.ps2.use_mouse}),e.vga&&(t.vga={is_graphical:e.vga.stats.is_graphical}),t},d.exportProperty(zt.prototype,"get_statistics",zt.prototype.get_statistics),zt.prototype.get_instruction_counter=function(){return this.v86?this.v86.cpu.instruction_counter[0]>>>0:0},d.exportProperty(zt.prototype,"get_instruction_counter",zt.prototype.get_instruction_counter),zt.prototype.is_running=function(){return this.cpu_is_running},d.exportProperty(zt.prototype,"is_running",zt.prototype.is_running),zt.prototype.keyboard_send_scancodes=function(t){for(var e=0;e<t.length;e++)this.bus.send("keyboard-code",t[e])},d.exportProperty(zt.prototype,"keyboard_send_scancodes",zt.prototype.keyboard_send_scancodes),zt.prototype.keyboard_send_keys=function(t){for(var e=0;e<t.length;e++)this.keyboard_adapter.simulate_press(t[e])},d.exportProperty(zt.prototype,"keyboard_send_keys",zt.prototype.keyboard_send_keys),zt.prototype.keyboard_send_text=function(t){for(var e=0;e<t.length;e++)this.keyboard_adapter.simulate_char(t[e])},d.exportProperty(zt.prototype,"keyboard_send_text",zt.prototype.keyboard_send_text),zt.prototype.screen_make_screenshot=function(){this.screen_adapter&&this.screen_adapter.make_screenshot()},d.exportProperty(zt.prototype,"screen_make_screenshot",zt.prototype.screen_make_screenshot),zt.prototype.screen_set_scale=function(t,e){this.screen_adapter&&this.screen_adapter.set_scale(t,e)},d.exportProperty(zt.prototype,"screen_set_scale",zt.prototype.screen_set_scale),zt.prototype.screen_go_fullscreen=function(){if(this.screen_adapter){var t=document.getElementById("screen_container");if(t){var e=t.requestFullScreen||t.webkitRequestFullscreen||t.mozRequestFullScreen||t.msRequestFullScreen;e&&(e.call(t),(t=document.getElementsByClassName("phone_keyboard")[0])&&t.focus()),this.lock_mouse()}}},d.exportProperty(zt.prototype,"screen_go_fullscreen",zt.prototype.screen_go_fullscreen),zt.prototype.lock_mouse=function(){var t=document.body,e=t.requestPointerLock||t.mozRequestPointerLock||t.webkitRequestPointerLock;e&&e.call(t)},d.exportProperty(zt.prototype,"lock_mouse",zt.prototype.lock_mouse),zt.prototype.mouse_set_status=function(t){this.mouse_adapter&&(this.mouse_adapter.emu_enabled=t)},zt.prototype.keyboard_set_status=function(t){this.keyboard_adapter&&(this.keyboard_adapter.emu_enabled=t)},d.exportProperty(zt.prototype,"keyboard_set_status",zt.prototype.keyboard_set_status),zt.prototype.serial0_send=function(t){for(var e=0;e<t.length;e++)this.bus.send("serial0-input",t.charCodeAt(e))},d.exportProperty(zt.prototype,"serial0_send",zt.prototype.serial0_send),zt.prototype.serial_send_bytes=function(t,e){for(var i=0;i<e.length;i++)this.bus.send("serial"+t+"-input",e[i])},d.exportProperty(zt.prototype,"serial_send_bytes",zt.prototype.serial_send_bytes),zt.prototype.mount_fs=async function(t,e,i,s){let r=new MemoryFileStorage;e&&(r=new ServerFileStorageWrapper(r,e));let a=new FS(r,this.fs9p.qidcounter),o=()=>{const e=this.fs9p.Mount(t,a);s&&(e===-ENOENT?s(new Ut):e===-EEXIST?s(new Mt):s(0>e?Error("Failed to mount. Error number: "+-e):null))};e?a.load_from_json(i,(()=>o())):o()},d.exportProperty(zt.prototype,"mount_fs",zt.prototype.mount_fs),zt.prototype.create_file=function(t,e,i){i=i||function(){};var s=this.fs9p;if(s){var r=t.split("/");r=r[r.length-1],t=s.SearchPath(t).parentid,""!==r&&-1!==t?s.CreateBinaryFile(r,t,e).then((()=>i(null))):setTimeout((function(){i(new Ut)}),0)}},d.exportProperty(zt.prototype,"create_file",zt.prototype.create_file),zt.prototype.read_file=function(t,e){var i=this.fs9p;i&&i.read_file(t).then((t=>{t?e(null,t):e(new Ut,null)}))},d.exportProperty(zt.prototype,"read_file",zt.prototype.read_file),zt.prototype.automatically=function(t){let e=t=>{const i=t[0];if(i){var s=t.slice(1);if(i.sleep)setTimeout((()=>e(s)),1e3*i.sleep);else if(i.vga_text){const r=this.screen_adapter.get_text_screen();for(let t of r)if(t.includes(i.vga_text))return void e(s);setTimeout((()=>e(t)),1e3)}else i.keyboard_send?(i.keyboard_send instanceof Array?this.keyboard_send_scancodes(i.keyboard_send):(i.keyboard_send,this.keyboard_send_text(i.keyboard_send)),e(s)):i.call?(i.call(),e(s)):console.assert(!1,i)}};e(t)},zt.prototype.read_memory=function(t,e){return this.v86.cpu.read_blob(t,e)},zt.prototype.write_memory=function(t,e){this.v86.cpu.write_blob(t,e)},Mt.prototype=Error.prototype,Ut.prototype=Error.prototype,"undefined"!=typeof window?(window.V86Starter=zt,window.V86=zt):"undefined"!=typeof module&&void 0!==module.exports?(module.exports.V86Starter=zt,module.exports.V86=zt):"function"==typeof importScripts&&(self.V86Starter=zt,self.V86=zt),function(){function t(t,e){this.filename=t,this.block_size=256,this.byteLength=e,this.loaded_blocks=Object.create(null),this.onprogress=this.onload=void 0}function e(t,e,i){let s=t.match(/(.*)(\..*)/);s?(this.basename=s[1],this.extension=s[2]):(this.basename=t,this.extension=""),this.block_size=256,this.byteLength=e,this.use_fixed_chunk_size="number"==typeof i,this.fixed_chunk_size=i,this.loaded_blocks=Object.create(null),this.onprogress=this.onload=void 0}function i(t){this.file=t,this.byteLength=t.size,1073741824<t.size&&console.warn("SyncFileBuffer: Allocating buffer of "+(t.size>>20)+" MB ..."),this.buffer=new ArrayBuffer(t.size),this.onprogress=this.onload=void 0}function s(t){this.file=t,this.byteLength=t.size,this.block_size=256,this.loaded_blocks=Object.create(null),this.onprogress=this.onload=void 0}c.load_file="undefined"==typeof XMLHttpRequest?load_file_nodejs:function t(e,i,s){function r(){let r=s||0;setTimeout((()=>{t(e,i,r+1)}),1e3*([1,1,2,3,5,8,13,21][r]||34))}var a=new XMLHttpRequest;if(a.open(i.method||"get",e,!0),a.responseType=i.as_json?"json":"arraybuffer",i.headers)for(var o=Object.keys(i.headers),_=0;_<o.length;_++){var n=o[_];a.setRequestHeader(n,i.headers[n])}i.range&&(o=i.range.start,a.setRequestHeader("Range","bytes="+o+"-"+(o+i.range.length-1)),a.onreadystatechange=function(){200===a.status&&a.abort()}),a.onload=function(t){4===a.readyState&&(200!==a.status&&206!==a.status?(console.error("Loading the image "+e+" failed (status %d)",a.status),500<=a.status&&600>a.status&&r()):a.response&&i.done&&i.done(a.response,a))},a.onerror=function(t){console.error("Loading the image "+e+" failed",t),r()},i.progress&&(a.onprogress=function(t){i.progress(t)}),a.send(null)},c.AsyncXHRBuffer=t,c.AsyncXHRPartfileBuffer=e,c.AsyncFileBuffer=s,c.SyncFileBuffer=i,c.read_sized_string_from_mem=function(t,e,i){return String.fromCharCode(...new Uint8Array(t.buffer,e>>>0,i>>>0))};t.prototype.load=function(){var t,e;void 0!==this.byteLength?this.onload&&this.onload(Object.create(null)):(t=this.filename,e=(t,e)=>{if(t)throw Error("Cannot use: "+this.filename+". "+t);dbg_assert(0<=e),this.byteLength=e,this.onload&&this.onload(Object.create(null))},c.load_file(t,{done:(t,i)=>{t=i.getResponseHeader("Content-Range")||"",(i=t.match(/\/(\d+)\s*$/))?e(null,+i[1]):e("`Range: bytes=...` header not supported (Got `"+t+"`)")},headers:{Range:"bytes=0-0"}}))},t.prototype.get_from_cache=function(t,e,i){i=e/this.block_size,t/=this.block_size;for(var s=0;s<i;s++)if(!this.loaded_blocks[t+s])return;if(1===i)return this.loaded_blocks[t];for(e=new Uint8Array(e),s=0;s<i;s++)e.set(this.loaded_blocks[t+s],s*this.block_size);return e},t.prototype.get=function(t,e,i){console.assert(t+e<=this.byteLength),console.assert(0==t%this.block_size),console.assert(0==e%this.block_size),console.assert(e);var s=this.get_from_cache(t,e,i);s?i(s):c.load_file(this.filename,{done:function(s){s=new Uint8Array(s),this.handle_read(t,e,s),i(s)}.bind(this),range:{start:t,length:e}})},t.prototype.set=function(t,e,i){console.assert(t+e.byteLength<=this.byteLength);var s=e.length;console.assert(0==t%this.block_size),console.assert(0==s%this.block_size),console.assert(s),t/=this.block_size,s/=this.block_size;for(var r=0;r<s;r++){var a=this.loaded_blocks[t+r];void 0===a&&(a=this.loaded_blocks[t+r]=new Uint8Array(this.block_size));var o=e.subarray(r*this.block_size,(r+1)*this.block_size);a.set(o),console.assert(a.byteLength===o.length)}i()},t.prototype.handle_read=function(t,e,i){t/=this.block_size,e/=this.block_size;for(var s=0;s<e;s++){var r=this.loaded_blocks[t+s];r&&i.set(r,s*this.block_size)}},t.prototype.get_buffer=function(t){t()},t.prototype.get_written_blocks=function(){var t=Object.keys(this.loaded_blocks).length;t=new Uint8Array(t*this.block_size);var e=[],i=0;for(r of Object.keys(this.loaded_blocks)){var s=this.loaded_blocks[r];dbg_assert(s.length===this.block_size);var r=+r;e.push(r),t.set(s,i*this.block_size),i++}return{buffer:t,indices:e,block_size:this.block_size}},t.prototype.get_state=function(){let t=[],e=[];for(let[t,i]of Object.entries(this.loaded_blocks))dbg_assert(isFinite(+t)),e.push([+t,i]);return t[0]=e,t},t.prototype.set_state=function(t){t=t[0],this.loaded_blocks=Object.create(null);for(let[e,i]of Object.values(t))this.loaded_blocks[e]=i},e.prototype.load=function(){void 0===this.byteLength&&dbg_assert(!1),this.onload&&this.onload(Object.create(null))},e.prototype.get_from_cache=t.prototype.get_from_cache,e.prototype.get=function(t,e,i){console.assert(t+e<=this.byteLength),console.assert(0==t%this.block_size),console.assert(0==e%this.block_size),console.assert(e);var s=this.get_from_cache(t,e,i);if(s)i(s);else if(this.use_fixed_chunk_size){s=Math.floor(t/this.fixed_chunk_size);let r=t-s*this.fixed_chunk_size;dbg_assert(0<=r);let a=Math.floor(e/this.fixed_chunk_size)+(0===r?1:2),o=new Uint8Array(r+a*this.fixed_chunk_size),_=0;for(let n=0;n<a;n++){let h=this.basename+"-"+(s+n+"").padStart(8,"0")+this.extension;c.load_file(h,{done:function(s){var h=n*this.fixed_chunk_size;s=new Uint8Array(s),o.set(s,h),_++,_===a&&(h=o.subarray(r,r+e),this.handle_read(t,e,h),i(h))}.bind(this)})}}else c.load_file(this.basename+"-"+t+"-"+(t+e)+this.extension,{done:function(s){dbg_assert(s.byteLength===e),s=new Uint8Array(s),this.handle_read(t,e,s),i(s)}.bind(this)})},e.prototype.set=t.prototype.set,e.prototype.handle_read=t.prototype.handle_read,e.prototype.get_written_blocks=t.prototype.get_written_blocks,e.prototype.get_state=t.prototype.get_state,e.prototype.set_state=t.prototype.set_state,i.prototype.load=function(){this.load_next(0)},i.prototype.load_next=function(t){var e=new FileReader;if(e.onload=function(e){e=new Uint8Array(e.target.result),new Uint8Array(this.buffer,t).set(e),this.load_next(t+4194304)}.bind(this),this.onprogress&&this.onprogress({loaded:t,total:this.byteLength,lengthComputable:!0}),t<this.byteLength){var i=this.file.slice(t,Math.min(t+4194304,this.byteLength));e.readAsArrayBuffer(i)}else this.file=void 0,this.onload&&this.onload({buffer:this.buffer})},i.prototype.get=function(t,e,i){console.assert(t+e<=this.byteLength),i(new Uint8Array(this.buffer,t,e))},i.prototype.set=function(t,e,i){console.assert(t+e.byteLength<=this.byteLength),new Uint8Array(this.buffer,t,e.byteLength).set(e),i()},i.prototype.get_buffer=function(t){t(this.buffer)},i.prototype.get_state=function(){let t=[];return t[0]=this.byteLength,t[1]=new Uint8Array(this.buffer),t},i.prototype.set_state=function(t){this.byteLength=t[0],this.buffer=t[1].slice().buffer},s.prototype.load=function(){this.onload&&this.onload(Object.create(null))},s.prototype.get=function(t,e,i){console.assert(0==t%this.block_size),console.assert(0==e%this.block_size),console.assert(e);var s=this.get_from_cache(t,e,i);s?i(s):((s=new FileReader).onload=function(s){s=new Uint8Array(s.target.result),this.handle_read(t,e,s),i(s)}.bind(this),s.readAsArrayBuffer(this.file.slice(t,t+e)))},s.prototype.get_from_cache=t.prototype.get_from_cache,s.prototype.set=t.prototype.set,s.prototype.handle_read=t.prototype.handle_read,s.prototype.get_state=t.prototype.get_state,s.prototype.get_buffer=function(t){t()},s.prototype.get_as_file=function(t){for(var e=[],i=Object.keys(this.loaded_blocks).map(Number).sort((function(t,e){return t-e})),s=0,r=0;r<i.length;r++){var a=i[r],o=this.loaded_blocks[a];a*=this.block_size,console.assert(a>=s),a!==s&&(e.push(this.file.slice(s,a)),s=a),e.push(o),s+=o.length}return s!==this.file.size&&e.push(this.file.slice(s)),t=new File(e,t),console.assert(t.size===this.file.size),t}}(),exports.ACPI=t,exports.APIC=s,exports.APIC_ADDRESS=4276092928,exports.APIC_LOG_VERBOSE=!1,exports.APIC_TIMER_FREQ=1e6,exports.APIC_TIMER_MODE_MASK=393216,exports.APIC_TIMER_MODE_ONE_SHOT=0,exports.APIC_TIMER_MODE_PERIODIC=131072,exports.APIC_TIMER_MODE_TSC=262144,exports.Bus=r,exports.BusConnector=a,exports.ByteQueue=l,exports.CDROM_SECTOR_SIZE=2048,exports.CMOS_BIOS_BOOTFLAG1=56,exports.CMOS_BIOS_BOOTFLAG2=61,exports.CMOS_BIOS_DISKTRANSFLAG=57,exports.CMOS_BIOS_SMP_COUNT=95,exports.CMOS_CENTURY=50,exports.CMOS_DISK_DATA=18,exports.CMOS_DISK_DRIVE1_CYL=27,exports.CMOS_DISK_DRIVE1_TYPE=25,exports.CMOS_DISK_DRIVE2_CYL=36,exports.CMOS_DISK_DRIVE2_TYPE=26,exports.CMOS_EQUIPMENT_INFO=20,exports.CMOS_FLOPPY_DRIVE_TYPE=16,exports.CMOS_MEM_BASE_HIGH=22,exports.CMOS_MEM_BASE_LOW=21,exports.CMOS_MEM_EXTMEM2_HIGH=53,exports.CMOS_MEM_EXTMEM2_LOW=52,exports.CMOS_MEM_EXTMEM_HIGH=49,exports.CMOS_MEM_EXTMEM_LOW=48,exports.CMOS_MEM_HIGHMEM_HIGH=93,exports.CMOS_MEM_HIGHMEM_LOW=91,exports.CMOS_MEM_HIGHMEM_MID=92,exports.CMOS_MEM_OLD_EXT_HIGH=24,exports.CMOS_MEM_OLD_EXT_LOW=23,exports.CMOS_RESET_CODE=15,exports.CMOS_RTC_DAY_MONTH=7,exports.CMOS_RTC_DAY_WEEK=6,exports.CMOS_RTC_HOURS=4,exports.CMOS_RTC_HOURS_ALARM=5,exports.CMOS_RTC_MINUTES=2,exports.CMOS_RTC_MINUTES_ALARM=3,exports.CMOS_RTC_MONTH=8,exports.CMOS_RTC_SECONDS=0,exports.CMOS_RTC_SECONDS_ALARM=1,exports.CMOS_RTC_YEAR=9,exports.CMOS_STATUS_A=10,exports.CMOS_STATUS_B=11,exports.CMOS_STATUS_C=12,exports.CMOS_STATUS_D=13,exports.CPU=it,exports.CPU_LOG_VERBOSE=!1,exports.CR0_PG=-2147483648,exports.CR4_PAE=32,exports.CircularQueue=m,exports.DEBUG=false,exports.DEBUG_SCREEN_LAYERS=false,exports.DELIVERY_MODES=e,exports.DESTINATION_MODES=i,exports.DLAB=128,exports.DMA=b,exports.DSP_BUFSIZE=64,exports.DSP_COMMAND_HANDLERS=D,exports.DSP_COMMAND_SIZES=S,exports.DSP_COPYRIGHT=L,exports.DSP_DACSIZE=65536,exports.DSP_NO_COMMAND=0,exports.DUMP_GENERATED_WASM=!1,exports.DUMP_UNCOMPILED_ASSEMBLY=!1,exports.E8390_CMD=0,exports.ELF_MAGIC=1179403647,exports.EN0_BOUNDARY=3,exports.EN0_CLDAHI=2,exports.EN0_CLDALO=1,exports.EN0_COUNTER0=13,exports.EN0_COUNTER1=14,exports.EN0_COUNTER2=15,exports.EN0_CRDAHI=9,exports.EN0_CRDALO=8,exports.EN0_DCFG=14,exports.EN0_FIFO=6,exports.EN0_IMR=15,exports.EN0_ISR=7,exports.EN0_NCR=5,exports.EN0_RCNTHI=11,exports.EN0_RCNTLO=10,exports.EN0_RSARHI=9,exports.EN0_RSARLO=8,exports.EN0_RSR=12,exports.EN0_RXCR=12,exports.EN0_STARTPG=1,exports.EN0_STOPPG=2,exports.EN0_TCNTHI=6,exports.EN0_TCNTLO=5,exports.EN0_TPSR=4,exports.EN0_TSR=4,exports.EN0_TXCR=13,exports.ENABLE_HPET=false,exports.ENISR_ALL=63,exports.ENISR_COUNTERS=32,exports.ENISR_OVER=16,exports.ENISR_RDC=64,exports.ENISR_RESET=128,exports.ENISR_RX=1,exports.ENISR_RX_ERR=4,exports.ENISR_TX=2,exports.ENISR_TX_ERR=8,exports.ENRSR_RXOK=1,exports.FLAGS_DEFAULT=2,exports.FLAG_AC=262144,exports.FLAG_ADJUST=16,exports.FLAG_CARRY=1,exports.FLAG_DIRECTION=1024,exports.FLAG_ID=2097152,exports.FLAG_INTERRUPT=512,exports.FLAG_IOPL=12288,exports.FLAG_NT=16384,exports.FLAG_OVERFLOW=2048,exports.FLAG_PARITY=4,exports.FLAG_RF=65536,exports.FLAG_SIGN=128,exports.FLAG_TRAP=256,exports.FLAG_VIF=524288,exports.FLAG_VIP=1048576,exports.FLAG_VM=131072,exports.FLAG_ZERO=64,exports.FM_HANDLERS=U,exports.FW_CFG_CUSTOM_START=32768,exports.FW_CFG_FILE_DIR=25,exports.FW_CFG_FILE_START=49152,exports.FW_CFG_ID=1,exports.FW_CFG_MAX_CPUS=15,exports.FW_CFG_NB_CPUS=5,exports.FW_CFG_NUMA=13,exports.FW_CFG_RAM_SIZE=3,exports.FW_CFG_SIGNATURE=0,exports.FW_CFG_SIGNATURE_QEMU=1431127377,exports.FloatQueue=f,exports.FloppyController=v,exports.HD_SECTOR_SIZE=512,exports.HPET=function(t){function e(){return s?1e4*(Date.now()-r)+a|0:a}var i=this,s=!1,r=Date.now(),a=0,o=!1,_=0,n=new Int32Array(8),d=new Int32Array(8),c=new Int32Array(8),p=0;this.legacy_mode=!1,this.timer=function(i){if(!s)return 100;i=e()>>>0;for(var r,a,o=0;o<4;o++)r=n[o<<1],a=d[o<<1]>>>0,(p<=i?a>p&&a<=i:a>p||a<=i)&&(a=4&r,2&r?(a=a&&!(_&1<<o),_|=1<<o):_&=~(1<<o),8&r&&(d[o<<1]+=c[o<<1]),a&&t.device_raise_irq(0));return p=i,100},t.io.mmap_register(4275044352,16384,(function(t){switch(dbg_log("Read "+h(t,4)+" (ctr="+h(e()>>>0)+")",LOG_HPET),t){case 0:return 99073;case 4:return 1e8;case 16:return i.legacy_mode<<1|s;case 240:return e();case 244:return 0}var r=t>>2&7,a=t-256>>5;if(256>t||a>=4||5<r)return dbg_log("Read reserved address: "+h(t),LOG_HPET),0;switch(dbg_log("Read counter: addr="+h(t)+" counter="+h(a,2)+" reg="+h(r),LOG_HPET),r){case 0:return-32817&n[a<<1]|16;case 1:return n[a<<1|1];case 2:return d[a<<1];case 3:return d[a<<1|1];case 4:case 5:return 0}}),(function(t,p){switch(dbg_log("Write "+h(t,4)+": "+h(p,2),LOG_HPET),t){case 16:return dbg_log("conf: enabled="+(1&p)+" legacy="+(p>>1&1),LOG_HPET),1&(s^p)&&(1&p?r=Date.now():a=e()),s=1==(1&p),void(i.legacy_mode=2==(2&p));case 32:return void(_&=~p);case 240:return void(a=p);case 244:return}var u=t>>2&7,l=t-256>>5;if(256>t||l>=4||2<u)dbg_log("Write reserved address: "+h(t)+" data="+h(p),LOG_HPET);else switch(dbg_log("Write counter: addr="+h(t)+" counter="+h(l,2)+" reg="+h(u)+" data="+h(p,2),LOG_HPET),u){case 0:n[l<<1]=p;break;case 2:o?(c[l<<1]=p,o=!1,dbg_log("Accumulator acc="+h(p>>>0,8)+" ctr="+h(l,2),LOG_HPET)):(d[l<<1]=p,64&n[l<<1]&&(o=!0,n[l<<1]&=-65));break;case 3:d[l<<1|1]=p}}))},exports.HPET_ADDR=4275044352,exports.HPET_COUNTER_CONFIG=16,exports.HPET_COUNTER_CONFIG_MASK=32816,exports.HPET_FREQ_MS=1e4,exports.HPET_NUM_COUNTERS=4,exports.HPET_PERIOD=1e8,exports.HPET_SUPPORT_64=0,exports.IDEDevice=w,exports.IDEInterface=x,exports.IO=A,exports.IOAPIC=I,exports.IOAPIC_ADDRESS=4273995776,exports.IOAPIC_CONFIG_DELIVS=4096,exports.IOAPIC_CONFIG_MASKED=65536,exports.IOAPIC_CONFIG_READONLY_MASK=-110592,exports.IOAPIC_CONFIG_REMOTE_IRR=16384,exports.IOAPIC_CONFIG_TRIGGER_MODE_LEVEL=32768,exports.IOAPIC_DELIVERY_FIXED=0,exports.IOAPIC_DELIVERY_INIT=5,exports.IOAPIC_DELIVERY_LOWEST_PRIORITY=1,exports.IOAPIC_DELIVERY_NMI=4,exports.IOAPIC_ID=0,exports.IOAPIC_IRQ_COUNT=24,exports.IOREGSEL=0,exports.IOWIN=16,exports.LINUX_BOOT_HDR_BOOT_FLAG=510,exports.LINUX_BOOT_HDR_CHECKSUM1=43605,exports.LINUX_BOOT_HDR_CHECKSUM2=1400005704,exports.LINUX_BOOT_HDR_CMDLINE_SIZE=568,exports.LINUX_BOOT_HDR_CMD_LINE_PTR=552,exports.LINUX_BOOT_HDR_CODE32_START=532,exports.LINUX_BOOT_HDR_HEADER=514,exports.LINUX_BOOT_HDR_HEAP_END_PTR=548,exports.LINUX_BOOT_HDR_INITRD_ADDR_MAX=556,exports.LINUX_BOOT_HDR_INIT_SIZE=608,exports.LINUX_BOOT_HDR_KERNEL_ALIGNMENT=560,exports.LINUX_BOOT_HDR_LOADFLAGS=529,exports.LINUX_BOOT_HDR_LOADFLAGS_CAN_USE_HEAPS=128,exports.LINUX_BOOT_HDR_LOADFLAGS_KEEP_SEGMENTS=64,exports.LINUX_BOOT_HDR_LOADFLAGS_LOADED_HIGH=1,exports.LINUX_BOOT_HDR_LOADFLAGS_QUIET_FLAG=32,exports.LINUX_BOOT_HDR_MIN_ALIGNMENT=565,exports.LINUX_BOOT_HDR_PAYLOAD_LENGTH=588,exports.LINUX_BOOT_HDR_PAYLOAD_OFFSET=584,exports.LINUX_BOOT_HDR_PREF_ADDRESS=600,exports.LINUX_BOOT_HDR_RAMDISK_IMAGE=536,exports.LINUX_BOOT_HDR_RAMDISK_SIZE=540,exports.LINUX_BOOT_HDR_RELOCATABLE_KERNEL=564,exports.LINUX_BOOT_HDR_SETUP_SECTS=497,exports.LINUX_BOOT_HDR_SYSSIZE=500,exports.LINUX_BOOT_HDR_TYPE_OF_LOADER=528,exports.LINUX_BOOT_HDR_TYPE_OF_LOADER_NOT_ASSIGNED=255,exports.LINUX_BOOT_HDR_VERSION=518,exports.LINUX_BOOT_HDR_VIDMODE=506,exports.LINUX_BOOT_HDR_XLOADFLAGS=566,exports.LOG_9P=4194304,exports.LOG_ACPI=262144,exports.LOG_ALL=-1,exports.LOG_ALL_IO=false,exports.LOG_APIC=524288,exports.LOG_BIOS=4096,exports.LOG_CPU=2,exports.LOG_DISK=32768,exports.LOG_DMA=16,exports.LOG_FLOPPY=8192,exports.LOG_FPU=4,exports.LOG_HPET=131072,exports.LOG_IO=32,exports.LOG_LEVEL=-15786961,exports.LOG_MEM=8,exports.LOG_MOUSE=1024,exports.LOG_NAMES=o,exports.LOG_NET=1048576,exports.LOG_NONE=0,exports.LOG_OTHER=1,exports.LOG_PCI=2048,exports.LOG_PIC=128,exports.LOG_PIT=512,exports.LOG_PS2=64,exports.LOG_RTC=65536,exports.LOG_SB16=8388608,exports.LOG_SERIAL=16384,exports.LOG_TO_FILE=!1,exports.LOG_VGA=256,exports.LOG_VIRTIO=2097152,exports.MAX_BPP=32,exports.MAX_XRES=2560,exports.MAX_YRES=1600,exports.MIXER_CHANNEL_BOTH=2,exports.MIXER_CHANNEL_LEFT=0,exports.MIXER_CHANNEL_RIGHT=1,exports.MIXER_READ_HANDLERS=P,exports.MIXER_REGISTER_IS_LEGACY=M,exports.MIXER_SRC_DAC=2,exports.MIXER_SRC_MASTER=0,exports.MIXER_SRC_PCSPEAKER=1,exports.MIXER_WRITE_HANDLERS=z,exports.MMAP_BLOCK_BITS=17,exports.MMAP_BLOCK_SIZE=131072,exports.NE2K_LOG_VERBOSE=!1,exports.NE_DATAPORT=16,exports.NE_RESET=31,exports.Ne2k=R,exports.OSCILLATOR_FREQ=1193.1816666,exports.PCI=E,exports.PCI_CONFIG_ADDRESS=3320,exports.PCI_CONFIG_DATA=3324,exports.PIC=C,exports.PIC_LOG_VERBOSE=!1,exports.PIT=T,exports.PMTIMER_FREQ_SECONDS=3579545,exports.PS2=q,exports.PS2_LOG_VERBOSE=!1,exports.REG_CS=1,exports.REG_DS=3,exports.REG_EAX=0,exports.REG_EBP=5,exports.REG_EBX=3,exports.REG_ECX=1,exports.REG_EDI=7,exports.REG_EDX=2,exports.REG_ES=0,exports.REG_ESI=6,exports.REG_ESP=4,exports.REG_FS=4,exports.REG_GS=5,exports.REG_LDTR=7,exports.REG_SS=2,exports.RTC=y,exports.SB16=G,exports.SB_DMA0=0,exports.SB_DMA1=1,exports.SB_DMA3=3,exports.SB_DMA5=5,exports.SB_DMA6=6,exports.SB_DMA7=7,exports.SB_DMA_BLOCK_SAMPLES=1024,exports.SB_DMA_BUFSIZE=65536,exports.SB_DMA_CHANNEL_16BIT=5,exports.SB_DMA_CHANNEL_8BIT=1,exports.SB_IRQ=5,exports.SB_IRQ10=10,exports.SB_IRQ2=2,exports.SB_IRQ5=5,exports.SB_IRQ7=7,exports.SB_IRQ_16BIT=2,exports.SB_IRQ_8BIT=1,exports.SB_IRQ_MIDI=1,exports.SB_IRQ_MPU=4,exports.START_PAGE=64,exports.START_RX_PAGE=76,exports.STATE_INDEX_INFO_LEN=3,exports.STATE_INDEX_MAGIC=0,exports.STATE_INDEX_TOTAL_LEN=2,exports.STATE_INDEX_VERSION=1,exports.STATE_INFO_BLOCK_START=16,exports.STATE_MAGIC=-2039052682,exports.STATE_VERSION=6,exports.STOP_PAGE=128,exports.SyncBuffer=u,exports.TIME_PER_FRAME=1,exports.TRACK_FILENAMES=!1,exports.TSC_RATE=1e6,exports.UART=J,exports.UART_IER_MSI=8,exports.UART_IER_RDI=1,exports.UART_IER_THRI=2,exports.UART_IIR_CTI=12,exports.UART_IIR_MSI=0,exports.UART_IIR_NO_INT=1,exports.UART_IIR_RDI=4,exports.UART_IIR_RLSI=6,exports.UART_IIR_THRI=2,exports.UART_LSR_DATA_READY=1,exports.UART_LSR_TRANSMITTER_EMPTY=64,exports.UART_LSR_TX_EMPTY=32,exports.V86Starter=zt,exports.VGAScreen=et,exports.VGA_BANK_SIZE=65536,exports.VGA_HOST_MEMORY_SPACE_SIZE=tt,exports.VGA_HOST_MEMORY_SPACE_START=$,exports.VGA_LFB_ADDRESS=3758096384,exports.VGA_MIN_MEMORY_SIZE=262144,exports.VGA_PIXEL_BUFFER_SIZE=524288,exports.VIRTIO_F_RING_EVENT_IDX=29,exports.VIRTIO_F_RING_INDIRECT_DESC=28,exports.VIRTIO_F_VERSION_1=32,exports.VIRTIO_ISR_DEVICE_CFG=2,exports.VIRTIO_ISR_QUEUE=1,exports.VIRTIO_PCI_CAP_COMMON_CFG=1,exports.VIRTIO_PCI_CAP_DEVICE_CFG=4,exports.VIRTIO_PCI_CAP_ISR_CFG=3,exports.VIRTIO_PCI_CAP_LENGTH=16,exports.VIRTIO_PCI_CAP_NOTIFY_CFG=2,exports.VIRTIO_PCI_CAP_PCI_CFG=5,exports.VIRTIO_PCI_CAP_VENDOR=9,exports.VIRTIO_PCI_VENDOR_ID=6900,exports.VIRTIO_STATUS_ACKNOWLEDGE=1,exports.VIRTIO_STATUS_DEVICE_NEEDS_RESET=64,exports.VIRTIO_STATUS_DRIVER=2,exports.VIRTIO_STATUS_DRIVER_OK=4,exports.VIRTIO_STATUS_FAILED=128,exports.VIRTIO_STATUS_FEATURES_OK=8,exports.VIRTQ_AVAIL_BASESIZE=6,exports.VIRTQ_AVAIL_ENTRYSIZE=2,exports.VIRTQ_AVAIL_F_NO_INTERRUPT=1,exports.VIRTQ_DESC_ENTRYSIZE=16,exports.VIRTQ_DESC_F_INDIRECT=4,exports.VIRTQ_DESC_F_NEXT=1,exports.VIRTQ_DESC_F_WRITE=2,exports.VIRTQ_IDX_MASK=65535,exports.VIRTQ_USED_BASESIZE=6,exports.VIRTQ_USED_ENTRYSIZE=8,exports.VIRTQ_USED_F_NO_NOTIFY=1,exports.VirtIO=vt,exports.WASM_TABLE_OFFSET=1024,exports.WASM_TABLE_SIZE=900,exports.ZSTD_MAGIC=4247762216,exports.dbg_assert=n,exports.dbg_assert_failed=function(t){if(console.trace(),t)throw"Assert failed: "+t;throw"Assert failed"},exports.dbg_log=_,exports.dbg_trace=function(t){},exports.download=g,exports.dump_file=function(t,e){t instanceof Array||(t=[t]),g(t=new Blob(t),e)},exports.h=p,exports.load_kernel=function(t,e,i,s){dbg_log("Trying to load kernel of size "+e.byteLength);var r=new Uint8Array(e);let a=new Uint16Array(e),o=new Uint32Array(e);var _=r[497]||4;o[125],a[253];var n=a[255];if(43605!==n)dbg_log("Bad checksum1: "+h(n));else{if(1400005704==(n=a[257]|a[258]<<16)){n=a[259],dbg_assert(514<=n);var d=r[529];dbg_assert(1&d);var c=a[283],p=o[139],u=o[140],l=r[564],f=r[565],m=o[142],g=o[146],b=o[147],y=o[150],v=o[151],w=o[152];for(dbg_log("kernel boot protocol version: "+h(n)),dbg_log("flags="+h(d)+" xflags="+h(c)),dbg_log("code32_start="+h(o[133])),dbg_log("initrd_addr_max="+h(p)),dbg_log("kernel_alignment="+h(u)),dbg_log("relocatable="+l),dbg_log("min_alignment="+h(f)),dbg_log("cmdline max="+h(m)),dbg_log("payload offset="+h(g)+" size="+h(b)),dbg_log("pref_address="+h(v)+":"+h(y)),dbg_log("init_size="+h(w)),r[528]=255,r[529]=-33&d&-65|128,a[274]=56832,a[253]=65535,dbg_log("heap_end_ptr="+h(56832)),s+="\0",dbg_assert(s.length<m),dbg_log("cmd_line_ptr="+h(581632)),o[138]=581632,r=0;r<s.length;r++)t[581632+r]=s.charCodeAt(r);return _=512*(_+1),dbg_log("prot_mode_kernel_start="+h(_)),s=new Uint8Array(e,0,_),e=new Uint8Array(e,_),r=_=0,i&&(_=67108864,r=i.byteLength,dbg_assert(1048576+e.length<_),t.set(new Uint8Array(i),_)),o[134]=_,o[135]=r,dbg_assert(655360>524288+s.length),t.set(s,524288),t.set(e,1048576),{option_rom:{name:"genroms/kernel.bin",data:ft(32768,57344)}}}dbg_log("Bad checksum2: "+h(n))}},exports.read_elf=function(t){t=new DataView(t);let[e,i]=pt(t,_t);if(console.assert(52===i),DEBUG){for(var s of Object.keys(e))dbg_log(s+": 0x"+e[s].toString(16));dbg_log(e)}if(console.assert(1179403647===e.magic,"Bad magic"),console.assert(1===e.class,"Unimplemented: 64 bit elf"),console.assert(1===e.data,"Unimplemented: big endian"),console.assert(1===e.version0,"Bad version0"),console.assert(2===e.type,"Unimplemented type"),console.assert(1===e.version1,"Bad version1"),console.assert(52===e.ehsize,"Bad header size"),console.assert(32===e.phentsize,"Bad program header size"),console.assert(40===e.shentsize,"Bad section header size"),[s]=ut(lt(t,e.phoff,e.phentsize*e.phnum),ht,e.phnum),[t]=ut(lt(t,e.shoff,e.shentsize*e.shnum),dt,e.shnum),DEBUG&&LOG_LEVEL){console.log("%d program headers:",s.length);for(let t of s)console.log("type=%s offset=%s vaddr=%s paddr=%s filesz=%s memsz=%s flags=%s align=%s",t.type.toString(16),t.offset.toString(16),t.vaddr.toString(16),t.paddr.toString(16),t.filesz.toString(16),t.memsz.toString(16),t.flags.toString(16),t.align.toString(16));console.log("%d program headers:",t.length);for(let e of t)console.log("name=%s type=%s flags=%s addr=%s offset=%s size=%s link=%s info=%s addralign=%s entsize=%s",e.name.toString(16),e.type.toString(16),e.flags.toString(16),e.addr.toString(16),e.offset.toString(16),e.size.toString(16),e.link.toString(16),e.info.toString(16),e.addralign.toString(16),e.entsize.toString(16))}return{header:e,program_headers:s,sections_headers:t}},exports.v86=O,exports.v86util=c;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});function t(t){this.cpu=t;var e=t.io;t.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),e.register_read(45056,this,void 0,(function(){return dbg_log("ACPI pm1_status read",LOG_ACPI),this.pm1_status})),e.register_write(45056,this,void 0,(function(t){dbg_log("ACPI pm1_status write: "+h(t,4),LOG_ACPI),this.pm1_status&=~t})),e.register_read(45058,this,void 0,(function(){return dbg_log("ACPI pm1_enable read",LOG_ACPI),this.pm1_enable})),e.register_write(45058,this,void 0,(function(t){dbg_log("ACPI pm1_enable write: "+h(t),LOG_ACPI),this.pm1_enable=t})),e.register_read(45060,this,void 0,(function(){return dbg_log("ACPI status read",LOG_ACPI),this.status})),e.register_write(45060,this,void 0,(function(t){dbg_log("ACPI status write: "+h(t),LOG_ACPI),this.status=t})),e.register_read(45064,this,void 0,void 0,(function(){return 16777215&this.get_timer(v86.microtick())})),e.register_read(45024,this,(function(){return dbg_log("Read gpe#0",LOG_ACPI),this.gpe[0]})),e.register_read(45025,this,(function(){return dbg_log("Read gpe#1",LOG_ACPI),this.gpe[1]})),e.register_read(45026,this,(function(){return dbg_log("Read gpe#2",LOG_ACPI),this.gpe[2]})),e.register_read(45027,this,(function(){return dbg_log("Read gpe#3",LOG_ACPI),this.gpe[3]})),e.register_write(45024,this,(function(t){dbg_log("Write gpe#0: "+h(t),LOG_ACPI),this.gpe[0]=t})),e.register_write(45025,this,(function(t){dbg_log("Write gpe#1: "+h(t),LOG_ACPI),this.gpe[1]=t})),e.register_write(45026,this,(function(t){dbg_log("Write gpe#2: "+h(t),LOG_ACPI),this.gpe[2]=t})),e.register_write(45027,this,(function(t){dbg_log("Write gpe#3: "+h(t),LOG_ACPI),this.gpe[3]=t}))}t.prototype.timer=function(t){var e=0!=(8388608&((t=this.get_timer(t))^this.last_timer));return 1&this.pm1_enable&&e?(dbg_log("ACPI raise irq",LOG_ACPI),this.pm1_status|=1,this.cpu.device_raise_irq(9)):this.cpu.device_lower_irq(9),this.last_timer=t,100},t.prototype.get_timer=function(t){return(t=Math.round(3579.545*t))===this.timer_last_value?this.timer_imprecision_offset<3579.545&&this.timer_imprecision_offset++:(dbg_assert(t>this.timer_last_value),this.timer_last_value+this.timer_imprecision_offset<=t?(this.timer_imprecision_offset=0,this.timer_last_value=t):dbg_log("Warning: Overshot pmtimer, waiting; current="+t+" last="+this.timer_last_value+" offset="+this.timer_imprecision_offset,LOG_ACPI)),this.timer_last_value+this.timer_imprecision_offset},t.prototype.get_state=function(){var t=[];return t[0]=this.status,t[1]=this.pm1_status,t[2]=this.pm1_enable,t[3]=this.gpe,t},t.prototype.set_state=function(t){this.status=t[0],this.pm1_status=t[1],this.pm1_enable=t[2],this.gpe=t[3]};var e="Fixed (0);Lowest Prio (1);SMI (2);Reserved (3);NMI (4);INIT (5);Reserved (6);ExtINT (7)".split(";"),i=["physical","logical"];function s(t){this.cpu=t,this.timer_divider=this.apic_id=0,this.timer_divider_shift=1,this.timer_current_count=this.timer_initial_count=0,this.next_tick=v86.microtick(),this.lvt_error=this.lvt_int1=this.lvt_int0=this.lvt_perf_counter=this.lvt_timer=IOAPIC_CONFIG_MASKED,this.icr1=this.icr0=this.tpr=0,this.irr=new Int32Array(8),this.isr=new Int32Array(8),this.tmr=new Int32Array(8),this.spurious_vector=254,this.destination_format=-1,this.read_error=this.error=this.local_destination=0,t.io.mmap_register(4276092928,1048576,(t=>{dbg_log("Unsupported read8 from apic: "+h(t>>>0),LOG_APIC);var e=3&t;return this.read32(-4&t)>>8*e&255}),((t,e)=>{dbg_log("Unsupported write8 from apic: "+h(t)+" <- "+h(e),LOG_APIC),dbg_trace(),dbg_assert(!1)}),(t=>this.read32(t)),((t,e)=>this.write32(t,e)))}s.prototype.read32=function(t){switch(t=t-4276092928|0){case 32:return dbg_log("APIC read id",LOG_APIC),this.apic_id;case 48:return dbg_log("APIC read version",LOG_APIC),327700;case 128:return this.tpr;case 208:return dbg_log("Read local destination",LOG_APIC),this.local_destination;case 224:return dbg_log("Read destination format",LOG_APIC),this.destination_format;case 240:return this.spurious_vector;case 256:case 272:case 288:case 304:case 320:case 336:case 352:case 368:return t=t-256>>4,dbg_log("Read isr "+t+": "+h(this.isr[t]>>>0,8),LOG_APIC),this.isr[t];case 384:case 400:case 416:case 432:case 448:case 464:case 480:case 496:return t=t-384>>4,dbg_log("Read tmr "+t+": "+h(this.tmr[t]>>>0,8),LOG_APIC),this.tmr[t];case 512:case 528:case 544:case 560:case 576:case 592:case 608:case 624:return t=t-512>>4,dbg_log("Read irr "+t+": "+h(this.irr[t]>>>0,8),LOG_APIC),this.irr[t];case 640:return dbg_log("Read error: "+h(this.read_error>>>0,8),LOG_APIC),this.read_error;case 768:return this.icr0;case 784:return dbg_log("APIC read icr1",LOG_APIC),this.icr1;case 800:return dbg_log("read timer lvt",LOG_APIC),this.lvt_timer;case 832:return dbg_log("read lvt perf counter",LOG_APIC),this.lvt_perf_counter;case 848:return dbg_log("read lvt int0",LOG_APIC),this.lvt_int0;case 864:return dbg_log("read lvt int1",LOG_APIC),this.lvt_int1;case 880:return dbg_log("read lvt error",LOG_APIC),this.lvt_error;case 992:return dbg_log("read timer divider",LOG_APIC),this.timer_divider;case 896:return dbg_log("read timer initial count",LOG_APIC),this.timer_initial_count;case 912:return dbg_log("read timer current count: "+h(this.timer_current_count>>>0,8),LOG_APIC),this.timer_current_count;default:return dbg_log("APIC read "+h(t),LOG_APIC),dbg_assert(!1),0}},s.prototype.write32=function(t,s){switch(t=t-4276092928|0){case 48:dbg_log("APIC write version: "+h(s>>>0,8)+", ignored",LOG_APIC);break;case 128:this.tpr=255&s,this.check_vector();break;case 176:-1!==(s=this.highest_isr())?(this.register_clear_bit(this.isr,s),this.register_get_bit(this.tmr,s)&&this.cpu.devices.ioapic.remote_eoi(s),this.check_vector()):dbg_log("Bad eoi: No isr set",LOG_APIC);break;case 208:dbg_log("Set local destination: "+h(s>>>0,8),LOG_APIC),this.local_destination=4278190080&s;break;case 224:dbg_log("Set destination format: "+h(s>>>0,8),LOG_APIC),this.destination_format=16777215|s;break;case 240:dbg_log("Set spurious vector: "+h(s>>>0,8),LOG_APIC),this.spurious_vector=s;break;case 640:dbg_log("Write error: "+h(s>>>0,8),LOG_APIC),this.read_error=this.error,this.error=0;break;case 768:t=255&s;var r=s>>8&7,a=s>>11&1,o=s>>15&1,_=s>>18&3,n=this.icr1>>>24;dbg_log("APIC write icr0: "+h(s,8)+" vector="+h(t,2)+" destination_mode="+i[a]+" delivery_mode="+e[r]+" destination_shorthand="+["no","self","all with self","all without self"][_],LOG_APIC),this.icr0=-4097&s,0===_?this.route(t,r,o,n,a):1===_?this.deliver(t,IOAPIC_DELIVERY_FIXED,o):2===_?this.deliver(t,r,o):3!==_&&dbg_assert(!1);break;case 784:dbg_log("APIC write icr1: "+h(s>>>0,8),LOG_APIC),this.icr1=s;break;case 800:dbg_log("timer lvt: "+h(s>>>0,8),LOG_APIC),this.lvt_timer=s;break;case 832:dbg_log("lvt perf counter: "+h(s>>>0,8),LOG_APIC),this.lvt_perf_counter=s;break;case 848:dbg_log("lvt int0: "+h(s>>>0,8),LOG_APIC),this.lvt_int0=s;break;case 864:dbg_log("lvt int1: "+h(s>>>0,8),LOG_APIC),this.lvt_int1=s;break;case 880:dbg_log("lvt error: "+h(s>>>0,8),LOG_APIC),this.lvt_error=s;break;case 992:dbg_log("timer divider: "+h(s>>>0,8),LOG_APIC),this.timer_divider=s,s=3&s|(8&s)>>1,this.timer_divider_shift=7===s?0:s+1;break;case 896:dbg_log("timer initial: "+h(s>>>0,8),LOG_APIC),this.timer_initial_count=s>>>0,this.timer_current_count=s>>>0,this.next_tick=v86.microtick(),this.timer_active=!0;break;case 912:dbg_log("timer current: "+h(s>>>0,8),LOG_APIC),dbg_assert(!1,"read-only register");break;default:dbg_log("APIC write32 "+h(t)+" <- "+h(s>>>0,8),LOG_APIC),dbg_assert(!1)}},s.prototype.timer=function(t){if(0===this.timer_current_count)return 100;let e=APIC_TIMER_FREQ/(1<<this.timer_divider_shift);return t=(t-this.next_tick)*e>>>0,this.next_tick+=t/e,this.timer_current_count-=t,0>=this.timer_current_count&&(131072===(t=393216&this.lvt_timer)?(this.timer_current_count%=this.timer_initial_count,0>=this.timer_current_count&&(this.timer_current_count+=this.timer_initial_count),dbg_assert(0!==this.timer_current_count),0==(this.lvt_timer&IOAPIC_CONFIG_MASKED)&&this.deliver(255&this.lvt_timer,IOAPIC_DELIVERY_FIXED,!1)):0===t&&(this.timer_current_count=0,dbg_log("APIC timer one shot end",LOG_APIC),0==(this.lvt_timer&IOAPIC_CONFIG_MASKED)&&this.deliver(255&this.lvt_timer,IOAPIC_DELIVERY_FIXED,!1))),Math.max(0,this.timer_current_count/e)},s.prototype.route=function(t,e,i,s,r){this.deliver(t,e,i)},s.prototype.deliver=function(t,e,i){e!==IOAPIC_DELIVERY_INIT&&e!==IOAPIC_DELIVERY_NMI&&((16>t||255===t)&&dbg_assert(!1,"TODO: Invalid vector"),this.register_get_bit(this.irr,t)?dbg_log("Not delivered: irr already set, vector="+h(t,2),LOG_APIC):(this.register_set_bit(this.irr,t),i?this.register_set_bit(this.tmr,t):this.register_clear_bit(this.tmr,t),this.check_vector()))},s.prototype.highest_irr=function(){var t=this.register_get_highest_bit(this.irr);return dbg_assert(255!==t),dbg_assert(16<=t||-1===t),t},s.prototype.highest_isr=function(){var t=this.register_get_highest_bit(this.isr);return dbg_assert(255!==t),dbg_assert(16<=t||-1===t),t},s.prototype.check_vector=function(){var t=this.highest_irr();-1!==t&&(this.highest_isr()>=t||(240&t)<=(240&this.tpr)||this.cpu.handle_irqs())},s.prototype.acknowledge_irq=function(){var t=this.highest_irr();-1===t||this.highest_isr()>=t||(240&t)<=(240&this.tpr)||(this.register_clear_bit(this.irr,t),this.register_set_bit(this.isr,t),this.cpu.pic_call_irq(t),this.check_vector())},s.prototype.get_state=function(){var t=[];return t[0]=this.apic_id,t[1]=this.timer_divider,t[2]=this.timer_divider_shift,t[3]=this.timer_initial_count,t[4]=this.timer_current_count,t[5]=this.next_tick,t[6]=this.lvt_timer,t[7]=this.lvt_perf_counter,t[8]=this.lvt_int0,t[9]=this.lvt_int1,t[10]=this.lvt_error,t[11]=this.tpr,t[12]=this.icr0,t[13]=this.icr1,t[14]=this.irr,t[15]=this.isr,t[16]=this.tmr,t[17]=this.spurious_vector,t[18]=this.destination_format,t[19]=this.local_destination,t[20]=this.error,t[21]=this.read_error,t},s.prototype.set_state=function(t){this.apic_id=t[0],this.timer_divider=t[1],this.timer_divider_shift=t[2],this.timer_initial_count=t[3],this.timer_current_count=t[4],this.next_tick=t[5],this.lvt_timer=t[6],this.lvt_perf_counter=t[7],this.lvt_int0=t[8],this.lvt_int1=t[9],this.lvt_error=t[10],this.tpr=t[11],this.icr0=t[12],this.icr1=t[13],this.irr=t[14],this.isr=t[15],this.tmr=t[16],this.spurious_vector=t[17],this.destination_format=t[18],this.local_destination=t[19],this.error=t[20],this.read_error=t[21]},s.prototype.register_get_bit=function(t,e){return dbg_assert(0<=e&&256>e),t[e>>5]>>(31&e)&1},s.prototype.register_set_bit=function(t,e){dbg_assert(0<=e&&256>e),t[e>>5]|=1<<(31&e)},s.prototype.register_clear_bit=function(t,e){dbg_assert(0<=e&&256>e),t[e>>5]&=~(1<<(31&e))},s.prototype.register_get_highest_bit=function(t){for(var e=7;0<=e;e--){var i=t[e];if(i)return v86util.int_log2(i>>>0)|e<<5}return-1};var r={};function a(){this.listeners={},this.pair=void 0}a.prototype.register=function(t,e,i){var s=this.listeners[t];void 0===s&&(s=this.listeners[t]=[]),s.push({fn:e,this_value:i})},a.prototype.unregister=function(t,e){var i=this.listeners[t];void 0!==i&&(this.listeners[t]=i.filter((function(t){return t.fn!==e})))},a.prototype.send=function(t,e,i){if(this.pair&&void 0!==(t=this.pair.listeners[t]))for(i=0;i<t.length;i++){var s=t[i];s.fn.call(s.this_value,e)}},a.prototype.send_async=function(t,e){dbg_assert(1===arguments.length||2===arguments.length),setTimeout(this.send.bind(this,t,e),0)},r.create=function(){var t=new a,e=new a;return t.pair=e,e.pair=t,[t,e]};var o=[[1,""],[2,"CPU"],[32768,"DISK"],[4,"FPU"],[8,"MEM"],[16,"DMA"],[32,"IO"],[64,"PS2"],[128,"PIC"],[256,"VGA"],[512,"PIT"],[1024,"MOUS"],[2048,"PCI"],[4096,"BIOS"],[8192,"FLOP"],[16384,"SERI"],[65536,"RTC"],[131072,"HPET"],[262144,"ACPI"],[524288,"APIC"],[1048576,"NET"],[2097152,"VIO"],[4194304,"9P"],[8388608,"SB16"]],_=function(){};function n(t,e,i){}var d=d||{};d.exportSymbol=function(){},d.exportProperty=function(){};var c=c||{};function p(t,e){return t=t?t.toString(16):"","0x"+c.pad0(t.toUpperCase(),e||1)}if(c.pads=function(t,e){return(t||0===t?t+"":"").padEnd(e," ")},c.pad0=function(t,e){return(t||0===t?t+"":"").padStart(e,"0")},c.zeros=function(t){return Array(t).fill(0)},c.range=function(t){return Array.from(Array(t).keys())},c.view=function(t,e,i,s){return new Proxy({},{get:function(r,a,o){return"function"==typeof(o=(r=new t(e.buffer,i,s))[a])?o.bind(r):(/^\d+$/.test(a),o)},set:function(r,a,o,_){return/^\d+$/.test(a),new t(e.buffer,i,s)[a]=o,!0}})},"undefined"!=typeof crypto&&crypto.getRandomValues){let t=new Int32Array(1);c.get_rand_int=function(){return crypto.getRandomValues(t),t[0]}}function u(t){this.buffer=t,this.byteLength=t.byteLength,this.onprogress=this.onload=void 0}function l(t){var e,i,s=new Uint8Array(t);this.length=0,this.push=function(e){this.length!==t&&this.length++,s[i]=e,i=i+1&t-1},this.shift=function(){if(this.length){var i=s[e];return e=e+1&t-1,this.length--,i}return-1},this.peek=function(){return this.length?s[e]:-1},this.clear=function(){this.length=i=e=0},this.clear()}function f(t){this.size=t,this.data=new Float32Array(t),this.length=this.end=this.start=0}function m(t){this.data=[],this.index=0,this.size=t}function g(t,e){var i=document.createElement("a");i.download=e,i.href=window.URL.createObjectURL(t),i.dataset.downloadurl=["application/octet-stream",i.download,i.href].join(":"),document.createEvent?((t=document.createEvent("MouseEvent")).initMouseEvent("click",!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null),i.dispatchEvent(t)):i.click(),window.URL.revokeObjectURL(i.href)}function b(t){this.cpu=t,this.channel_page=new Uint8Array(8),this.channel_pagehi=new Uint8Array(8),this.channel_addr=new Uint16Array(8),this.channel_addr_init=new Uint16Array(8),this.channel_count=new Uint16Array(8),this.channel_count_init=new Uint16Array(8),this.channel_mask=new Uint8Array(8),this.channel_mode=new Uint8Array(8),this.unmask_listeners=[],this.lsb_msb_flipflop=0,(t=t.io).register_write(0,this,this.port_addr_write.bind(this,0)),t.register_write(2,this,this.port_addr_write.bind(this,1)),t.register_write(4,this,this.port_addr_write.bind(this,2)),t.register_write(6,this,this.port_addr_write.bind(this,3)),t.register_write(1,this,this.port_count_write.bind(this,0)),t.register_write(3,this,this.port_count_write.bind(this,1)),t.register_write(5,this,this.port_count_write.bind(this,2)),t.register_write(7,this,this.port_count_write.bind(this,3)),t.register_read(0,this,this.port_addr_read.bind(this,0)),t.register_read(2,this,this.port_addr_read.bind(this,1)),t.register_read(4,this,this.port_addr_read.bind(this,2)),t.register_read(6,this,this.port_addr_read.bind(this,3)),t.register_read(1,this,this.port_count_read.bind(this,0)),t.register_read(3,this,this.port_count_read.bind(this,1)),t.register_read(5,this,this.port_count_read.bind(this,2)),t.register_read(7,this,this.port_count_read.bind(this,3)),t.register_write(192,this,this.port_addr_write.bind(this,4)),t.register_write(196,this,this.port_addr_write.bind(this,5)),t.register_write(200,this,this.port_addr_write.bind(this,6)),t.register_write(204,this,this.port_addr_write.bind(this,7)),t.register_write(194,this,this.port_count_write.bind(this,4)),t.register_write(198,this,this.port_count_write.bind(this,5)),t.register_write(202,this,this.port_count_write.bind(this,6)),t.register_write(206,this,this.port_count_write.bind(this,7)),t.register_read(192,this,this.port_addr_read.bind(this,4)),t.register_read(196,this,this.port_addr_read.bind(this,5)),t.register_read(200,this,this.port_addr_read.bind(this,6)),t.register_read(204,this,this.port_addr_read.bind(this,7)),t.register_read(194,this,this.port_count_read.bind(this,4)),t.register_read(198,this,this.port_count_read.bind(this,5)),t.register_read(202,this,this.port_count_read.bind(this,6)),t.register_read(206,this,this.port_count_read.bind(this,7)),t.register_write(135,this,this.port_page_write.bind(this,0)),t.register_write(131,this,this.port_page_write.bind(this,1)),t.register_write(129,this,this.port_page_write.bind(this,2)),t.register_write(130,this,this.port_page_write.bind(this,3)),t.register_write(143,this,this.port_page_write.bind(this,4)),t.register_write(139,this,this.port_page_write.bind(this,5)),t.register_write(137,this,this.port_page_write.bind(this,6)),t.register_write(138,this,this.port_page_write.bind(this,7)),t.register_read(135,this,this.port_page_read.bind(this,0)),t.register_read(131,this,this.port_page_read.bind(this,1)),t.register_read(129,this,this.port_page_read.bind(this,2)),t.register_read(130,this,this.port_page_read.bind(this,3)),t.register_read(143,this,this.port_page_read.bind(this,4)),t.register_read(139,this,this.port_page_read.bind(this,5)),t.register_read(137,this,this.port_page_read.bind(this,6)),t.register_read(138,this,this.port_page_read.bind(this,7)),t.register_write(1159,this,this.port_pagehi_write.bind(this,0)),t.register_write(1155,this,this.port_pagehi_write.bind(this,1)),t.register_write(1153,this,this.port_pagehi_write.bind(this,2)),t.register_write(1154,this,this.port_pagehi_write.bind(this,3)),t.register_write(1163,this,this.port_pagehi_write.bind(this,5)),t.register_write(1161,this,this.port_pagehi_write.bind(this,6)),t.register_write(1162,this,this.port_pagehi_write.bind(this,7)),t.register_read(1159,this,this.port_pagehi_read.bind(this,0)),t.register_read(1155,this,this.port_pagehi_read.bind(this,1)),t.register_read(1153,this,this.port_pagehi_read.bind(this,2)),t.register_read(1154,this,this.port_pagehi_read.bind(this,3)),t.register_read(1163,this,this.port_pagehi_read.bind(this,5)),t.register_read(1161,this,this.port_pagehi_read.bind(this,6)),t.register_read(1162,this,this.port_pagehi_read.bind(this,7)),t.register_write(10,this,this.port_singlemask_write.bind(this,0)),t.register_write(212,this,this.port_singlemask_write.bind(this,4)),t.register_write(15,this,this.port_multimask_write.bind(this,0)),t.register_write(222,this,this.port_multimask_write.bind(this,4)),t.register_read(15,this,this.port_multimask_read.bind(this,0)),t.register_read(222,this,this.port_multimask_read.bind(this,4)),t.register_write(11,this,this.port_mode_write.bind(this,0)),t.register_write(214,this,this.port_mode_write.bind(this,4)),t.register_write(12,this,this.portC_write),t.register_write(216,this,this.portC_write)}u.prototype.load=function(){this.onload&&this.onload({buffer:this.buffer})},u.prototype.get=function(t,e,i){this.byteLength,i(new Uint8Array(this.buffer,t,e))},u.prototype.set=function(t,e,i){e.byteLength,this.byteLength,new Uint8Array(this.buffer,t,e.byteLength).set(e),i()},u.prototype.get_buffer=function(t){t(this.buffer)},u.prototype.get_state=function(){let t=[];return t[0]=this.byteLength,t[1]=new Uint8Array(this.buffer),t},u.prototype.set_state=function(t){this.byteLength=t[0],this.buffer=t[1].slice().buffer},function(){if("function"==typeof Math.clz32)c.int_log2_byte=function(t){return 31-Math.clz32(t)},c.int_log2=function(t){return 31-Math.clz32(t)};else{for(var t=new Int8Array(256),e=0,i=-2;256>e;e++)e&e-1||i++,t[e]=i;c.int_log2_byte=function(e){return t[e]},c.int_log2=function(e){var i=(e>>>=0)>>>16;if(i){var s=i>>>8;return s?24+t[s]:16+t[i]}return(s=e>>>8)?8+t[s]:t[e]}}}(),f.prototype.push=function(t){this.length===this.size?this.start=this.start+1&this.size-1:this.length++,this.data[this.end]=t,this.end=this.end+1&this.size-1},f.prototype.shift=function(){if(this.length){var t=this.data[this.start];return this.start=this.start+1&this.size-1,this.length--,t}},f.prototype.shift_block=function(t){var e=new Float32Array(t);t>this.length&&(t=this.length);var i=this.start+t,s=this.data.subarray(this.start,i);return e.set(s),i>=this.size&&(i-=this.size,e.set(this.data.subarray(0,i),s.length)),this.start=i,this.length-=t,e},f.prototype.peek=function(){if(this.length)return this.data[this.start]},f.prototype.clear=function(){this.length=this.end=this.start=0},m.prototype.add=function(t){this.data[this.index]=t,this.index=(this.index+1)%this.size},m.prototype.toArray=function(){return[].slice.call(this.data,this.index).concat([].slice.call(this.data,0,this.index))},m.prototype.clear=function(){this.data=[],this.index=0},m.prototype.set=function(t){this.data=t,this.index=0},c.Bitmap=function(t){"number"==typeof t?this.view=new Uint8Array(t+7>>3):t instanceof ArrayBuffer?this.view=new Uint8Array(t):console.assert(!1)},c.Bitmap.prototype.set=function(t,e){let i=t>>3;t=1<<(7&t),this.view[i]=e?this.view[i]|t:this.view[i]&~t},c.Bitmap.prototype.get=function(t){return this.view[t>>3]>>(7&t)&1},c.Bitmap.prototype.get_buffer=function(){return this.view.buffer},b.prototype.get_state=function(){return[this.channel_page,this.channel_pagehi,this.channel_addr,this.channel_addr_init,this.channel_count,this.channel_count_init,this.channel_mask,this.channel_mode,this.lsb_msb_flipflop]},b.prototype.set_state=function(t){this.channel_page=t[0],this.channel_pagehi=t[1],this.channel_addr=t[2],this.channel_addr_init=t[3],this.channel_count=t[4],this.channel_count_init=t[5],this.channel_mask=t[6],this.channel_mode=t[7],this.lsb_msb_flipflop=t[8]},b.prototype.port_count_write=function(t,e){_("count write ["+t+"] = "+p(e),16),this.channel_count[t]=this.flipflop_get(this.channel_count[t],e,!1),this.channel_count_init[t]=this.flipflop_get(this.channel_count_init[t],e,!0)},b.prototype.port_count_read=function(t){return _("count read ["+t+"] -> "+p(this.channel_count[t]),16),this.flipflop_read(this.channel_count[t])},b.prototype.port_addr_write=function(t,e){_("addr write ["+t+"] = "+p(e),16),this.channel_addr[t]=this.flipflop_get(this.channel_addr[t],e,!1),this.channel_addr_init[t]=this.flipflop_get(this.channel_addr_init[t],e,!0)},b.prototype.port_addr_read=function(t){return _("addr read ["+t+"] -> "+p(this.channel_addr[t]),16),this.flipflop_read(this.channel_addr[t])},b.prototype.port_pagehi_write=function(t,e){_("pagehi write ["+t+"] = "+p(e),16),this.channel_pagehi[t]=e},b.prototype.port_pagehi_read=function(t){return _("pagehi read ["+t+"]",16),this.channel_pagehi[t]},b.prototype.port_page_write=function(t,e){_("page write ["+t+"] = "+p(e),16),this.channel_page[t]=e},b.prototype.port_page_read=function(t){return _("page read ["+t+"]",16),this.channel_page[t]},b.prototype.port_singlemask_write=function(t,e){_("singlechannel mask write ["+(t=(3&e)+t)+"] = "+(e=4&e?1:0),16),this.update_mask(t,e)},b.prototype.port_multimask_write=function(t,e){_("multichannel mask write: "+p(e),16);for(var i=0;4>i;i++)this.update_mask(t+i,e&1<<i)},b.prototype.port_multimask_read=function(t){var e=0|this.channel_mask[t+0];return e|=this.channel_mask[t+1]<<1,e|=this.channel_mask[t+2]<<2,e|=this.channel_mask[t+3]<<3,_("multichannel mask read: "+p(e),16),e},b.prototype.port_mode_write=function(t,e){_("mode write ["+(t=(3&e)+t)+"] = "+p(e),16),this.channel_mode[t]=e},b.prototype.portC_write=function(t){_("flipflop reset",16),this.lsb_msb_flipflop=0},b.prototype.on_unmask=function(t,e){this.unmask_listeners.push({fn:t,this_value:e})},b.prototype.update_mask=function(t,e){if(this.channel_mask[t]!==e&&(this.channel_mask[t]=e,!e))for(_("firing on_unmask("+t+")",16),e=0;e<this.unmask_listeners.length;e++)this.unmask_listeners[e].fn.call(this.unmask_listeners[e].this_value,t)},b.prototype.do_read=function(t,e,i,s,r){var a=this.count_get_8bit(s),o=this.address_get_8bit(s);if(_("DMA write channel "+s,16),_("to "+p(o)+" len "+p(a),16),i<a&&_("DMA should read more than provided: "+p(i)+" "+p(a),16),e+a>t.byteLength)_("DMA read outside of buffer",16),r(!0);else{var n=this.cpu;this.channel_addr[s]+=a,t.get(e,a,(function(t){n.write_blob(t,o),r(!1)}))}},b.prototype.do_write=function(t,e,i,s,r){var a=this.channel_count[s]+1&65535,o=5<=s?2:1,n=a*o,h=this.address_get_8bit(s),d=!1,c=!1,u=16&this.channel_mode[s];_("DMA write channel "+s,16),_("to "+p(h)+" len "+p(n),16),i<n?(_("DMA should read more than provided",16),a=Math.floor(i/o),n=a*o,d=!0):i>n&&(_("DMA attempted to read more than provided",16),c=!0),e+n>t.byteLength?(_("DMA write outside of buffer",16),r(!0)):(this.channel_addr[s]+=a,this.channel_count[s]-=a,!d&&u&&(_("DMA autoinit",16),this.channel_addr[s]=this.channel_addr_init[s],this.channel_count[s]=this.channel_count_init[s]),t.set(e,this.cpu.mem8.subarray(h,h+n),(()=>{c&&u?(_("DMA continuing from start",16),this.do_write(t,e+n,i-n,s,r)):r(!1)})))},b.prototype.address_get_8bit=function(t){var e=this.channel_addr[t];return 5<=t&&(e<<=1),(e=65535&e|this.channel_page[t]<<16)|this.channel_pagehi[t]<<24},b.prototype.count_get_8bit=function(t){var e=this.channel_count[t]+1;return 5<=t&&(e*=2),e},b.prototype.flipflop_get=function(t,e,i){return i||(this.lsb_msb_flipflop^=1),this.lsb_msb_flipflop?-256&t|e:-65281&t|e<<8},b.prototype.flipflop_read=function(t){return(this.lsb_msb_flipflop^=1)?255&t:t>>8&255};function y(t){this.cpu=t,this.cmos_index=0,this.cmos_data=new Uint8Array(128),this.last_update=this.rtc_time=Date.now(),this.next_interrupt_alarm=this.next_interrupt=0,this.periodic_interrupt=!1,this.periodic_interrupt_time=.9765625,this.cmos_a=38,this.cmos_b=2,this.nmi_disabled=this.cmos_c=0,t.io.register_write(112,this,(function(t){this.cmos_index=127&t,this.nmi_disabled=t>>7})),t.io.register_write(113,this,this.cmos_port_write),t.io.register_read(113,this,this.cmos_port_read)}function v(t,e,i){if(this.io=t.io,this.cpu=t,this.dma=t.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=e,this.fdb_image=i,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,e){var s={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 r=e.byteLength;(i=s[r])||(r=1474560<e.byteLength?2949120:1474560,i=s[r],_("Warning: Unkown floppy size: "+e.byteLength+", assuming "+r)),t.devices.rtc.cmos_write(16,i.type<<4),t=i.sectors,e=i.heads,i=i.tracks,this.sectors_per_track=t,this.number_of_heads=e,this.number_of_cylinders=i}else t.devices.rtc.cmos_write(16,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,this,this.port3F2_read),this.io.register_read(1012,this,this.port3F4_read),this.io.register_read(1013,this,this.port3F5_read),this.io.register_read(1015,this,this.port3F7_read),this.io.register_write(1010,this,this.port3F2_write),this.io.register_write(1013,this,this.port3F5_write)}y.prototype.get_state=function(){var t=[];return t[0]=this.cmos_index,t[1]=this.cmos_data,t[2]=this.rtc_time,t[3]=this.last_update,t[4]=this.next_interrupt,t[5]=this.next_interrupt_alarm,t[6]=this.periodic_interrupt,t[7]=this.periodic_interrupt_time,t[8]=this.cmos_a,t[9]=this.cmos_b,t[10]=this.cmos_c,t[11]=this.nmi_disabled,t},y.prototype.set_state=function(t){this.cmos_index=t[0],this.cmos_data=t[1],this.rtc_time=t[2],this.last_update=t[3],this.next_interrupt=t[4],this.next_interrupt_alarm=t[5],this.periodic_interrupt=t[6],this.periodic_interrupt_time=t[7],this.cmos_a=t[8],this.cmos_b=t[9],this.cmos_c=t[10],this.nmi_disabled=t[11]},y.prototype.timer=function(t,e){return t=Date.now(),this.rtc_time+=t-this.last_update,this.last_update=t,this.periodic_interrupt&&this.next_interrupt<t?(this.cpu.device_raise_irq(8),this.cmos_c|=192,this.next_interrupt+=this.periodic_interrupt_time*Math.ceil((t-this.next_interrupt)/this.periodic_interrupt_time)):this.next_interrupt_alarm&&this.next_interrupt_alarm<t&&(this.cpu.device_raise_irq(8),this.cmos_c|=160,this.next_interrupt_alarm=0),e=100,this.periodic_interrupt&&this.next_interrupt&&(e=Math.min(e,Math.max(0,this.next_interrupt-t))),this.next_interrupt_alarm&&(e=Math.min(e,Math.max(0,this.next_interrupt_alarm-t))),e},y.prototype.bcd_pack=function(t){for(var e,i=0,s=0;t;)s|=(e=t%10)<<4*i,i++,t=(t-e)/10;return s},y.prototype.bcd_unpack=function(t){return(15&t)+10*(t>>4&15)},y.prototype.encode_time=function(t){return 4&this.cmos_b?t:this.bcd_pack(t)},y.prototype.decode_time=function(t){return 4&this.cmos_b?t:this.bcd_unpack(t)},y.prototype.cmos_port_read=function(){var t=this.cmos_index;switch(t){case 0:return this.encode_time(new Date(this.rtc_time).getUTCSeconds());case 2:return this.encode_time(new Date(this.rtc_time).getUTCMinutes());case 4:return this.encode_time(new Date(this.rtc_time).getUTCHours());case 7:return this.encode_time(new Date(this.rtc_time).getUTCDate());case 8:return this.encode_time(new Date(this.rtc_time).getUTCMonth()+1);case 9:return this.encode_time(new Date(this.rtc_time).getUTCFullYear()%100);case 10:return this.cmos_a;case 11:return this.cmos_b;case 12:return this.cpu.device_lower_irq(8),_("cmos reg C read",65536),t=this.cmos_c,this.cmos_c&=-241,t;case 13:return 0;case 50:return this.encode_time(new Date(this.rtc_time).getUTCFullYear()/100|0);default:return _("cmos read from index "+p(t),65536),this.cmos_data[this.cmos_index]}},y.prototype.cmos_port_write=function(t){switch(this.cmos_index){case 10:this.cmos_a=127&t,this.periodic_interrupt_time=1e3/(32768>>(15&this.cmos_a)-1),_("Periodic interrupt, a="+p(this.cmos_a,2)+" t="+this.periodic_interrupt_time,65536);break;case 11:if(this.cmos_b=t,64&this.cmos_b&&(this.next_interrupt=Date.now()),32&this.cmos_b){t=new Date;let e=this.decode_time(this.cmos_data[1]),i=this.decode_time(this.cmos_data[3]),s=this.decode_time(this.cmos_data[5]),r=new Date(Date.UTC(t.getUTCFullYear(),t.getUTCMonth(),t.getUTCDate(),s,i,e));_("RTC alarm scheduled for "+r+" hh:mm:ss="+s+":"+i+":"+e+" ms_from_now="+(r-t),65536),this.next_interrupt_alarm=+r}16&this.cmos_b&&_("Unimplemented: updated interrupt",65536),_("cmos b="+p(this.cmos_b,2),65536);break;case 1:case 3:case 5:this.cmos_write(this.cmos_index,t);break;default:_("cmos write index "+p(this.cmos_index)+": "+p(t),65536)}this.periodic_interrupt=64==(64&this.cmos_b)&&0<(15&this.cmos_a)},y.prototype.cmos_read=function(t){return this.cmos_data[t]},y.prototype.cmos_write=function(t,e){_("cmos "+p(t)+" <- "+p(e),65536),this.cmos_data[t]=e},v.prototype.get_state=function(){var t=[];return t[0]=this.bytes_expecting,t[1]=this.receiving_command,t[2]=this.receiving_index,t[4]=this.response_data,t[5]=this.response_index,t[6]=this.response_length,t[8]=this.status_reg0,t[9]=this.status_reg1,t[10]=this.status_reg2,t[11]=this.drive,t[12]=this.last_cylinder,t[13]=this.last_head,t[14]=this.last_sector,t[15]=this.dor,t[16]=this.sectors_per_track,t[17]=this.number_of_heads,t[18]=this.number_of_cylinders,t},v.prototype.set_state=function(t){this.bytes_expecting=t[0],this.receiving_command=t[1],this.receiving_index=t[2],this.next_command=t[3],this.response_data=t[4],this.response_index=t[5],this.response_length=t[6],this.status_reg0=t[8],this.status_reg1=t[9],this.status_reg2=t[10],this.drive=t[11],this.last_cylinder=t[12],this.last_head=t[13],this.last_sector=t[14],this.dor=t[15],this.sectors_per_track=t[16],this.number_of_heads=t[17],this.number_of_cylinders=t[18]},v.prototype.port3F0_read=function(){return _("3F0 read",8192),0},v.prototype.port3F4_read=function(){_("3F4 read",8192);var t=128;return this.response_index<this.response_length&&(t|=80),0==(8&this.dor)&&(t|=32),t},v.prototype.port3F7_read=function(){return _("3F7 read",8192),0},v.prototype.port3F5_read=function(){return this.response_index<this.response_length?(_("3F5 read: "+this.response_data[this.response_index],8192),this.cpu.device_lower_irq(6),this.response_data[this.response_index++]):(_("3F5 read, empty",8192),255)},v.prototype.port3F5_write=function(t){if(this.fda_image)if(_("3F5 write "+p(t),8192),0<this.bytes_expecting)this.receiving_command[this.receiving_index++]=t,this.bytes_expecting--,0===this.bytes_expecting&&this.next_command.call(this,this.receiving_command);else{switch(t){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(t){this.do_sector(!0,t)},this.bytes_expecting=8;break;case 230:this.next_command=function(t){this.do_sector(!1,t)},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:_("dump registers",8192),this.response_data[0]=128,this.response_index=0,this.response_length=1,this.bytes_expecting=0;break;default:p(t)}this.receiving_index=0}},v.prototype.port3F2_read=function(){return _("read 3F2: DOR",8192),this.dor},v.prototype.port3F2_write=function(t){4==(4&t)&&0==(4&this.dor)&&this.cpu.device_raise_irq(6),_("start motors: "+p(t>>4),8192),_("enable dma: "+!!(8&t),8192),_("reset fdc: "+!!(4&t),8192),_("drive select: "+(3&t),8192),_("DOR = "+p(t),8192),this.dor=t},v.prototype.check_drive_status=function(t){_("check drive status",8192),this.response_index=0,this.response_length=1,this.response_data[0]=32},v.prototype.seek=function(t){_("seek",8192),t[0],this.last_cylinder=t[1],this.last_head=t[0]>>2&1,this.raise_irq()},v.prototype.calibrate=function(t){_("floppy calibrate",8192),this.raise_irq()},v.prototype.check_interrupt_status=function(){_("floppy check interrupt status",8192),this.response_index=0,this.response_length=2,this.response_data[0]=32,this.response_data[1]=this.last_cylinder},v.prototype.do_sector=function(t,e){var i=e[2],s=e[1],r=e[3],a=128<<e[4],o=e[5]-e[3]+1,n=((i+this.number_of_heads*s)*this.sectors_per_track+r-1)*a;_("Floppy "+(t?"Write":"Read"),8192),_("from "+p(n)+" length "+p(o*a),8192),_(s+" / "+i+" / "+r,8192),e[4]||_("FDC: sector count is zero, use data length instead",8192),this.fda_image&&(t?this.dma.do_write(this.fda_image,n,o*a,2,this.done.bind(this,e,s,i,r)):this.dma.do_read(this.fda_image,n,o*a,2,this.done.bind(this,e,s,i,r)))},v.prototype.done=function(t,e,i,s,r){r||(++s>this.sectors_per_track&&(s=1,++i>=this.number_of_heads&&(i=0,e++)),this.last_cylinder=e,this.last_head=i,this.last_sector=s,this.response_index=0,this.response_length=7,this.response_data[0]=i<<2|32,this.response_data[1]=0,this.response_data[2]=0,this.response_data[3]=e,this.response_data[4]=i,this.response_data[5]=s,this.response_data[6]=t[4],this.raise_irq())},v.prototype.fix_drive_data=function(t){_("floppy fix drive data "+t,8192)},v.prototype.read_sector_id=function(t){_("floppy read sector id "+t,8192),this.response_index=0,this.response_length=7,this.response_data[0]=0,this.response_data[1]=0,this.response_data[2]=0,this.response_data[3]=0,this.response_data[4]=0,this.response_data[5]=0,this.response_data[6]=0,this.raise_irq()},v.prototype.raise_irq=function(){8&this.dor&&this.cpu.device_raise_irq(6)};function w(t,e,i,s,r,a){this.master=new x(this,t,e,s,r,0,a),this.slave=new x(this,t,i,!1,r,1,a),this.current_interface=this.master,this.cpu=t,0===r?(this.ata_port=496,this.irq=14,this.pci_id=240):1===r&&(this.ata_port=368,this.irq=15,this.pci_id=248),this.ata_port_high=516|this.ata_port,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,255&this.master_port|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"+r,this.device_control=2,t.io.register_read(7|this.ata_port,this,(function(){return _("lower irq",32768),this.cpu.device_lower_irq(this.irq),this.read_status()})),t.io.register_read(2|this.ata_port_high,this,this.read_status),t.io.register_write(2|this.ata_port_high,this,this.write_control),t.io.register_read(0|this.ata_port,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)})),t.io.register_read(1|this.ata_port,this,(function(){return _("Read error: "+p(255&this.current_interface.error)+" slave="+(this.current_interface===this.slave),32768),255&this.current_interface.error})),t.io.register_read(2|this.ata_port,this,(function(){return _("Read bytecount: "+p(255&this.current_interface.bytecount),32768),255&this.current_interface.bytecount})),t.io.register_read(3|this.ata_port,this,(function(){return _("Read sector: "+p(255&this.current_interface.sector),32768),255&this.current_interface.sector})),t.io.register_read(4|this.ata_port,this,(function(){return _("Read 1F4: "+p(255&this.current_interface.cylinder_low),32768),255&this.current_interface.cylinder_low})),t.io.register_read(5|this.ata_port,this,(function(){return _("Read 1F5: "+p(255&this.current_interface.cylinder_high),32768),255&this.current_interface.cylinder_high})),t.io.register_read(6|this.ata_port,this,(function(){return _("Read 1F6",32768),255&this.current_interface.drive_head})),t.io.register_write(0|this.ata_port,this,(function(t){this.current_interface.write_data_port8(t)}),(function(t){this.current_interface.write_data_port16(t)}),(function(t){this.current_interface.write_data_port32(t)})),t.io.register_write(1|this.ata_port,this,(function(t){_("1F1/lba_count: "+p(t),32768),this.master.lba_count=65535&(this.master.lba_count<<8|t),this.slave.lba_count=65535&(this.slave.lba_count<<8|t)})),t.io.register_write(2|this.ata_port,this,(function(t){_("1F2/bytecount: "+p(t),32768),this.master.bytecount=65535&(this.master.bytecount<<8|t),this.slave.bytecount=65535&(this.slave.bytecount<<8|t)})),t.io.register_write(3|this.ata_port,this,(function(t){_("1F3/sector: "+p(t),32768),this.master.sector=65535&(this.master.sector<<8|t),this.slave.sector=65535&(this.slave.sector<<8|t)})),t.io.register_write(4|this.ata_port,this,(function(t){_("1F4/sector low: "+p(t),32768),this.master.cylinder_low=65535&(this.master.cylinder_low<<8|t),this.slave.cylinder_low=65535&(this.slave.cylinder_low<<8|t)})),t.io.register_write(5|this.ata_port,this,(function(t){_("1F5/sector high: "+p(t),32768),this.master.cylinder_high=65535&(this.master.cylinder_high<<8|t),this.slave.cylinder_high=65535&(this.slave.cylinder_high<<8|t)})),t.io.register_write(6|this.ata_port,this,(function(t){var e=16&t;_("1F6/drive: "+p(t,2),32768),e?(_("Slave",32768),this.current_interface=this.slave):this.current_interface=this.master,this.master.drive_head=t,this.slave.drive_head=t,this.master.is_lba=this.slave.is_lba=t>>6&1,this.master.head=this.slave.head=15&t})),this.dma_command=this.dma_status=this.prdt_addr=0,t.io.register_write(7|this.ata_port,this,(function(t){_("lower irq",32768),this.cpu.device_lower_irq(this.irq),this.current_interface.ata_command(t)})),t.io.register_read(4|this.master_port,this,void 0,void 0,this.dma_read_addr),t.io.register_write(4|this.master_port,this,void 0,void 0,this.dma_set_addr),t.io.register_read(this.master_port,this,this.dma_read_command8,void 0,this.dma_read_command),t.io.register_write(this.master_port,this,this.dma_write_command8,void 0,this.dma_write_command),t.io.register_read(2|this.master_port,this,this.dma_read_status),t.io.register_write(2|this.master_port,this,this.dma_write_status),t.io.register_read(8|this.master_port,this,(function(){return _("DMA read 0x8",32768),0})),t.io.register_read(10|this.master_port,this,(function(){return _("DMA read 0xA",32768),0})),t.devices.pci.register_device(this)}function x(t,e,i,s,r,a,o){this.device=t,this.bus=o,this.nr=r,this.cpu=e,this.buffer=i,this.sector_size=s?2048:512,this.is_atapi=s,this.cylinder_count=this.sectors_per_track=this.head_count=this.sector_count=0,this.buffer&&(this.sector_count=this.buffer.byteLength/this.sector_size,this.sector_count!==(0|this.sector_count)&&(_("Warning: Disk size not aligned with sector size",32768),this.sector_count=Math.ceil(this.sector_count)),s?(this.head_count=1,this.sectors_per_track=0):(this.head_count=16,this.sectors_per_track=63),this.cylinder_count=this.sector_count/this.head_count/this.sectors_per_track,this.cylinder_count!==(0|this.cylinder_count)&&(_("Warning: Rounding up cylinder count. Choose different head number",32768),this.cylinder_count=Math.floor(this.cylinder_count)),(t=e.devices.rtc).cmos_write(57,t.cmos_read(57)|1<<4*this.nr),t.cmos_write(18,15&t.cmos_read(18)|240),e=27,t.cmos_write(e+0,255&this.cylinder_count),t.cmos_write(e+1,this.cylinder_count>>8&255),t.cmos_write(e+2,255&this.head_count),t.cmos_write(e+3,255),t.cmos_write(e+4,255),t.cmos_write(e+5,200),t.cmos_write(e+6,255&this.cylinder_count),t.cmos_write(e+7,this.cylinder_count>>8&255),t.cmos_write(e+8,255&this.sectors_per_track)),this.stats={sectors_read:0,sectors_written:0,bytes_read:0,bytes_written:0,loading:!1},this.buffer=i,this.drive_head=this.head=this.cylinder_high=this.cylinder_low=this.lba_count=this.sector=this.bytecount=this.is_lba=0,this.status=80,this.sectors_per_drq=128,this.data_pointer=this.error=0,this.data=new Uint8Array(65536),this.data16=new Uint16Array(this.data.buffer),this.data32=new Int32Array(this.data.buffer),this.data_end=this.data_length=0,this.current_atapi_command=this.current_command=-1,this.last_io_id=this.write_dest=0,this.in_progress_io_ids=new Set,this.cancelled_io_ids=new Set,Object.seal(this)}function k(t){this.ports=[],this.cpu=t;for(var e=0;65536>e;e++)this.ports[e]=this.create_empty_entry();var i=t.memory_size[0];for(e=0;e<<17<i;e++)t.memory_map_read8[e]=t.memory_map_write8[e]=void 0,t.memory_map_read32[e]=t.memory_map_write32[e]=void 0;this.mmap_register(i,4294967296-i,(function(t){return _("Read from unmapped memory space, addr="+p(t>>>0,8),32),255}),(function(t,e){_("Write to unmapped memory space, addr="+p(t>>>0,8)+" value="+p(e,2),32)}),(function(t){return _("Read from unmapped memory space, addr="+p(t>>>0,8),32),-1}),(function(t,e){_("Write to unmapped memory space, addr="+p(t>>>0,8)+" value="+p(e>>>0,8),32)}))}w.prototype.read_status=function(){if(this.current_interface.buffer){var t=this.current_interface.status;return _("ATA read status: "+p(t,2),32768),t}return 0},w.prototype.write_control=function(t){_("set device control: "+p(t,2)+" interrupts "+(2&t?"disabled":"enabled"),32768),4&t&&(_("Reset via control port",32768),this.cpu.device_lower_irq(this.irq),this.master.device_reset(),this.slave.device_reset()),this.device_control=t},w.prototype.dma_read_addr=function(){return _("dma get address: "+p(this.prdt_addr,8),32768),this.prdt_addr},w.prototype.dma_set_addr=function(t){_("dma set address: "+p(t,8),32768),this.prdt_addr=t},w.prototype.dma_read_status=function(){return _("DMA read status: "+p(this.dma_status),32768),this.dma_status},w.prototype.dma_write_status=function(t){_("DMA set status: "+p(t),32768),this.dma_status&=~(6&t)},w.prototype.dma_read_command=function(){return this.dma_read_command8()|this.dma_read_status()<<16},w.prototype.dma_read_command8=function(){return _("DMA read command: "+p(this.dma_command),32768),this.dma_command},w.prototype.dma_write_command=function(t){_("DMA write command: "+p(t),32768),this.dma_write_command8(255&t),this.dma_write_status(t>>16&255)},w.prototype.dma_write_command8=function(t){_("DMA write command8: "+p(t),32768);let e=this.dma_command;if(this.dma_command=9&t,(1&e)!=(1&t))if(0==(1&t))this.dma_status&=-2;else switch(this.dma_status|=1,this.current_interface.current_command){case 37:case 200:this.current_interface.do_ata_read_sectors_dma();break;case 202:case 53:this.current_interface.do_ata_write_sectors_dma();break;case 160:this.current_interface.do_atapi_dma();break;default:_("Spurious dma command write, current command: "+p(this.current_interface.current_command),32768)}},w.prototype.push_irq=function(){0==(2&this.device_control)&&(_("push irq",32768),this.dma_status|=4,this.cpu.device_raise_irq(this.irq))},w.prototype.get_state=function(){var t=[];return t[0]=this.master,t[1]=this.slave,t[2]=this.ata_port,t[3]=this.irq,t[4]=this.pci_id,t[5]=this.ata_port_high,t[6]=this.master_port,t[7]=this.name,t[8]=this.device_control,t[9]=this.prdt_addr,t[10]=this.dma_status,t[11]=this.current_interface===this.master,t[12]=this.dma_command,t},w.prototype.set_state=function(t){this.master.set_state(t[0]),this.slave.set_state(t[1]),this.ata_port=t[2],this.irq=t[3],this.pci_id=t[4],this.ata_port_high=t[5],this.master_port=t[6],this.name=t[7],this.device_control=t[8],this.prdt_addr=t[9],this.dma_status=t[10],this.current_interface=t[11]?this.master:this.slave,this.dma_command=t[12]},x.prototype.device_reset=function(){this.is_atapi?(this.status=0,this.sector=this.error=this.bytecount=1,this.cylinder_low=20,this.cylinder_high=235):(this.status=81,this.sector=this.error=this.bytecount=1,this.cylinder_high=this.cylinder_low=0),this.cancel_io_operations()},x.prototype.push_irq=function(){this.device.push_irq()},x.prototype.ata_command=function(t){if(_("ATA Command: "+p(t)+" slave="+(this.drive_head>>4&1),32768),this.buffer)switch(this.current_command=t,this.error=0,t){case 8:_("ATA device reset",32768),this.data_length=this.data_end=this.data_pointer=0,this.device_reset(),this.push_irq();break;case 16:this.status=80,this.cylinder_low=0,this.push_irq();break;case 248:this.status=80,t=this.sector_count-1,this.sector=255&t,this.cylinder_low=t>>8&255,this.cylinder_high=t>>16&255,this.drive_head=240&this.drive_head|t>>24&15,this.push_irq();break;case 39:this.status=80,t=this.sector_count-1,this.sector=255&t,this.cylinder_low=t>>8&255,this.cylinder_high=t>>16&255,this.sector|=t>>24<<8&65280,this.push_irq();break;case 32:case 36:case 41:case 196:this.ata_read_sectors(t);break;case 48:case 52:case 57:case 197:this.ata_write_sectors(t);break;case 144:this.push_irq(),this.error=257,this.status=80;break;case 145:case 222:this.status=80,this.push_irq();break;case 160:this.is_atapi&&(this.status=88,this.data_allocate(12),this.data_end=12,this.bytecount=1,this.push_irq());break;case 161:_("ATA identify packet device",32768),this.is_atapi?(this.create_identify_packet(),this.status=88,this.cylinder_low=20,this.cylinder_high=235):this.status=65,this.push_irq();break;case 198:_("Logical sectors per DRQ Block: "+p(255&this.bytecount),32768),this.sectors_per_drq=255&this.bytecount,this.status=80,this.push_irq();break;case 37:case 200:this.ata_read_sectors_dma(t);break;case 53:case 202:this.ata_write_sectors_dma(t);break;case 64:_("read verify sectors",32768),this.status=80,this.push_irq();break;case 218:_("Unimplemented: get media status",32768),this.status=65,this.error=4,this.push_irq();break;case 224:_("ATA standby immediate",32768),this.status=80,this.push_irq();break;case 225:_("ATA idle immediate",32768),this.status=80,this.push_irq();break;case 231:_("ATA flush cache",32768),this.status=80,this.push_irq();break;case 236:if(_("ATA identify device",32768),this.is_atapi){this.status=65,this.error=4,this.push_irq();break}this.create_identify_packet(),this.status=88,this.push_irq();break;case 234:_("flush cache ext",32768),this.status=80,this.push_irq();break;case 239:_("set features: "+p(255&this.bytecount),32768),this.status=80,this.push_irq();break;case 245:_("security freeze lock",32768),this.status=80,this.push_irq();break;case 249:_("Unimplemented: set max address",32768),this.status=65,this.error=4;break;default:p(t),this.status=65,this.error=4}else _("abort: No buffer",32768),this.error=4,this.status=65,this.push_irq()},x.prototype.atapi_handle=function(){switch(_("ATAPI Command: "+p(this.data[0])+" slave="+(this.drive_head>>4&1),32768),this.data_pointer=0,this.current_atapi_command=this.data[0],this.current_atapi_command){case 0:_("test unit ready",32768),this.data_allocate(0),this.data_end=this.data_length,this.status=80;break;case 3:this.data_allocate(this.data[4]),this.data_end=this.data_length,this.status=88,this.data[0]=240,this.data[2]=5,this.data[7]=8;break;case 18:var t=this.data[4];this.status=88,_("inquiry: "+p(this.data[1],2)+" length="+t,32768),this.data.set([5,128,1,49,31,0,0,0,83,79,78,89,32,32,32,32,67,68,45,82,79,77,32,67,68,85,45,49,48,48,48,32,49,46,49,97]),this.data_end=this.data_length=Math.min(36,t);break;case 26:this.data_allocate(this.data[4]),this.data_end=this.data_length,this.status=88;break;case 30:case 81:this.data_allocate(0),this.data_end=this.data_length,this.status=80;break;case 37:t=this.sector_count-1,this.data_set(new Uint8Array([t>>24&255,t>>16&255,t>>8&255,255&t,0,0,this.sector_size>>8&255,255&this.sector_size])),this.data_end=this.data_length,this.status=88;break;case 40:1&this.lba_count?this.atapi_read_dma(this.data):this.atapi_read(this.data);break;case 66:t=this.data[8],this.data_allocate(Math.min(8,t)),this.data_end=this.data_length,_("read q subcode: length="+t,32768),this.status=88;break;case 67:t=this.data[8]|this.data[7]<<8;var e=this.data[9]>>6;this.data_allocate(t),this.data_end=this.data_length,_("read toc: "+p(e,2)+" length="+t+" "+(2&this.data[1])+" "+p(this.data[6]),32768),0===e?(t=this.sector_count,this.data.set(new Uint8Array([0,18,1,1,0,20,1,0,0,0,0,0,0,22,170,0,t>>24,t>>16&255,t>>8&255,255&t]))):1===e&&this.data.set(new Uint8Array([0,10,1,1,0,0,0,0,0,0,0,0])),this.status=88;break;case 70:t=this.data[8]|this.data[7]<<8,t=Math.min(t,32),this.data_allocate(t),this.data_end=this.data_length,this.data[0]=t-4>>24&255,this.data[1]=t-4>>16&255,this.data[2]=t-4>>8&255,this.data[3]=t-4&255,this.data[6]=8,this.data[10]=3,this.status=88;break;case 82:_("Unimplemented ATAPI command: "+p(this.data[0]),32768),this.status=81,this.data_length=0,this.error=80;break;case 90:t=this.data[8]|this.data[7]<<8,e=this.data[2],_("mode sense: "+p(e)+" length="+t,32768),42===e&&this.data_allocate(Math.min(30,t)),this.data_end=this.data_length,this.status=88;break;case 189:this.data_allocate(this.data[9]|this.data[8]<<8),this.data_end=this.data_length,this.data[5]=1,this.status=88;break;case 74:default:this.status=81,this.data_length=0,this.error=80,_("Unimplemented ATAPI command: "+p(this.data[0]),32768);break;case 190:_("Unimplemented ATAPI command: "+p(this.data[0]),32768),this.data_allocate(0),this.data_end=this.data_length,this.status=80}this.bytecount=-8&this.bytecount|2,0==(128&this.status)&&this.push_irq(),0==(128&this.status)&&0===this.data_length&&(this.bytecount|=1,this.status&=-9)},x.prototype.do_write=function(){this.status=80,this.data_length,this.data.length;var t=this.data.subarray(0,this.data_length);this.data_length,this.ata_advance(this.current_command,this.data_length/512),this.push_irq(),this.buffer.set(this.write_dest,t,(function(){})),this.report_write(this.data_length)},x.prototype.atapi_read=function(t){var e=t[2]<<24|t[3]<<16|t[4]<<8|t[5],i=t[7]<<8|t[8];t=t[1];var s=i*this.sector_size,r=e*this.sector_size;_("CD read lba="+p(e)+" lbacount="+p(i)+" bytecount="+p(s)+" flags="+p(t),32768),this.data_length=0;var a=this.cylinder_high<<8&65280|255&this.cylinder_low;_(p(this.cylinder_high,2)+" "+p(this.cylinder_low,2),32768),this.cylinder_low=this.cylinder_high=0,65535===a&&a--,a>s&&(a=s),r>=this.buffer.byteLength?(p(r+s),p(this.buffer.byteLength),this.status=255,this.push_irq()):0===s?(this.status=80,this.data_pointer=0):(s=Math.min(s,this.buffer.byteLength-r),this.status=208,this.report_read_start(),this.read_buffer(r,s,(t=>{_("cd read: data arrived",32768),this.data_set(t),this.status=88,this.bytecount=-8&this.bytecount|2,this.push_irq(),this.data_end=a&=-4,this.data_end>this.data_length&&(this.data_end=this.data_length),this.cylinder_low=255&this.data_end,this.cylinder_high=this.data_end>>8&255,this.report_read_end(s)})))},x.prototype.atapi_read_dma=function(t){var e=t[2]<<24|t[3]<<16|t[4]<<8|t[5],i=t[7]<<8|t[8];t=t[1];var s=i*this.sector_size,r=e*this.sector_size;_("CD read DMA lba="+p(e)+" lbacount="+p(i)+" bytecount="+p(s)+" flags="+p(t),32768),r>=this.buffer.byteLength?(p(r+s),p(this.buffer.byteLength),this.status=255,this.push_irq()):(this.status=208,this.report_read_start(),this.read_buffer(r,s,(t=>{_("atapi_read_dma: Data arrived"),this.report_read_end(s),this.status=88,this.bytecount=-8&this.bytecount|2,this.data_set(t),this.do_atapi_dma()})))},x.prototype.do_atapi_dma=function(){if(0==(1&this.device.dma_status))_("do_atapi_dma: Status not set",32768);else if(0==(8&this.status))_("do_atapi_dma: DRQ not set",32768);else{_("atapi dma transfer len="+this.data_length,32768);var t=this.device.prdt_addr,e=0,i=this.data;do{var s=this.cpu.read32s(t),r=this.cpu.read16(t+4),a=128&this.cpu.read8(t+7);if(r||(r=65536),_("dma read dest="+p(s)+" count="+p(r)+" datalen="+p(this.data_length),32768),this.cpu.write_blob(i.subarray(e,Math.min(e+r,this.data_length)),s),t+=8,(e+=r)>=this.data_length&&!a){_("leave early end="+ +a+" offset="+p(e)+" data_length="+p(this.data_length)+" cmd="+p(this.current_command),32768);break}}while(!a);_("end offset="+e,32768),this.status=80,this.device.dma_status&=-2,this.bytecount=-8&this.bytecount|3,this.push_irq()}},x.prototype.read_data=function(t){if(this.data_pointer<this.data_end){this.data_pointer,this.data_end,this.data_pointer,p(this.data_pointer);var e=1===t?this.data[this.data_pointer]:2===t?this.data16[this.data_pointer>>>1]:this.data32[this.data_pointer>>>2];return this.data_pointer+=t,0==(this.data_pointer&(0==(4095&this.data_end)?4095:255))&&_("Read 1F0: "+p(this.data[this.data_pointer],2)+" cur="+p(this.data_pointer)+" cnt="+p(this.data_length),32768),this.data_pointer>=this.data_end&&this.read_end(),e}return _("Read 1F0: empty",32768),this.data_pointer+=t,0},x.prototype.read_end=function(){if(_("read_end cmd="+p(this.current_command)+" data_pointer="+p(this.data_pointer)+" end="+p(this.data_end)+" length="+p(this.data_length),32768),160===this.current_command)if(this.data_end===this.data_length)this.status=80,this.bytecount=-8&this.bytecount|3,this.push_irq();else{this.status=88,this.bytecount=-8&this.bytecount|2,this.push_irq();var t=this.cylinder_high<<8&65280|255&this.cylinder_low;this.data_end+t>this.data_length?(this.cylinder_low=this.data_length-this.data_end&255,this.cylinder_high=this.data_length-this.data_end>>8&255,this.data_end=this.data_length):this.data_end+=t,_("data_end="+p(this.data_end),32768)}else this.error=0,this.data_pointer>=this.data_length?this.status=80:(196===this.current_command||41===this.current_command?t=Math.min(this.sectors_per_drq,(this.data_length-this.data_end)/512):(32===this.current_command||this.current_command,t=1),this.ata_advance(this.current_command,t),this.data_end+=512*t,this.status=88),this.push_irq()},x.prototype.write_data_port=function(t,e){this.data_pointer,this.data_pointer>=this.data_end?_("Redundant write to data port: "+p(t)+" count="+p(this.data_end)+" cur="+p(this.data_pointer),32768):((0==(this.data_pointer+e&(0==(4095&this.data_end)?4095:255))||20>this.data_end)&&_("Data port: "+p(t>>>0)+" count="+p(this.data_end)+" cur="+p(this.data_pointer),32768),1===e?this.data[this.data_pointer++]=t:2===e?(this.data16[this.data_pointer>>>1]=t,this.data_pointer+=2):(this.data32[this.data_pointer>>>2]=t,this.data_pointer+=4),this.data_pointer,this.data_end,this.data_pointer===this.data_end&&this.write_end())},x.prototype.write_data_port8=function(t){this.write_data_port(t,1)},x.prototype.write_data_port16=function(t){this.write_data_port(t,2)},x.prototype.write_data_port32=function(t){this.write_data_port(t,4)},x.prototype.write_end=function(){160===this.current_command?this.atapi_handle():(_("write_end data_pointer="+p(this.data_pointer)+" data_length="+p(this.data_length),32768),this.data_pointer>=this.data_length?this.do_write():(48===this.current_command||52===this.current_command||this.current_command,p(this.current_command),this.status=88,this.data_end+=512,this.push_irq()))},x.prototype.ata_advance=function(t,e){_("Advance sectors="+e+" old_bytecount="+this.bytecount,32768),this.bytecount-=e,36===t||41===t||52===t||57===t||37===t||53===t?(t=e+this.get_lba48(),this.sector=255&t|t>>16&65280,this.cylinder_low=t>>8&255,this.cylinder_high=t>>16&255):this.is_lba?(t=e+this.get_lba28(),this.sector=255&t,this.cylinder_low=t>>8&255,this.cylinder_high=t>>16&255,this.head=-16&this.head|15&t):(e=(t=e+this.get_chs())/(this.head_count*this.sectors_per_track)|0,this.cylinder_low=255&e,this.cylinder_high=e>>8&255,this.head=(t/this.sectors_per_track|0)%this.head_count&15,this.sector=t%this.sectors_per_track+1&255,this.get_chs())},x.prototype.ata_read_sectors=function(t){var e=36===t||41===t,i=this.get_count(e);e=this.get_lba(e);var s=32===t||36===t,r=i*this.sector_size,a=e*this.sector_size;_("ATA read cmd="+p(t)+" mode="+(this.is_lba?"lba":"chs")+" lba="+p(e)+" lbacount="+p(i)+" bytecount="+p(r),32768),a+r>this.buffer.byteLength?(this.status=255,this.push_irq()):(this.status=192,this.report_read_start(),this.read_buffer(a,r,(e=>{_("ata_read: Data arrived",32768),this.data_set(e),this.status=88,this.data_end=s?512:Math.min(r,512*this.sectors_per_drq),this.ata_advance(t,s?1:Math.min(i,this.sectors_per_track)),this.push_irq(),this.report_read_end(r)})))},x.prototype.ata_read_sectors_dma=function(t){var e=37===t;t=this.get_count(e),e=this.get_lba(e);var i=t*this.sector_size,s=e*this.sector_size;_("ATA DMA read lba="+p(e)+" lbacount="+p(t)+" bytecount="+p(i),32768),s+i>this.buffer.byteLength?(this.status=255,this.push_irq()):(this.status=88,this.device.dma_status|=1)},x.prototype.do_ata_read_sectors_dma=function(){var t=37===this.current_command,e=this.get_count(t);t=this.get_lba(t);var i=e*this.sector_size,s=t*this.sector_size;this.buffer.byteLength,this.report_read_start(),this.device.prdt_addr,this.read_buffer(s,i,(t=>{_("do_ata_read_sectors_dma: Data arrived",32768);var s=this.device.prdt_addr,r=0;do{var a=this.cpu.read32s(s),o=this.cpu.read16(s+4),n=128&this.cpu.read8(s+7);o||(o=65536,_("dma: prd count was 0",32768)),_("dma read transfer dest="+p(a)+" prd_count="+p(o),32768),this.cpu.write_blob(t.subarray(r,r+o),a),r+=o,s+=8}while(!n);this.ata_advance(this.current_command,e),this.status=80,this.device.dma_status&=-2,this.current_command=-1,this.push_irq(),this.report_read_end(i)}))},x.prototype.ata_write_sectors=function(t){var e=52===t||57===t,i=this.get_count(e);e=this.get_lba(e),t=48===t||52===t;var s=i*this.sector_size,r=e*this.sector_size;_("ATA write lba="+p(e)+" mode="+(this.is_lba?"lba":"chs")+" lbacount="+p(i)+" bytecount="+p(s),32768),r+s>this.buffer.byteLength?(this.status=255,this.push_irq()):(this.status=88,this.data_allocate_noclear(s),this.data_end=t?512:Math.min(s,512*this.sectors_per_drq),this.write_dest=r)},x.prototype.ata_write_sectors_dma=function(t){var e=53===t;t=this.get_count(e),e=this.get_lba(e);var i=t*this.sector_size,s=e*this.sector_size;_("ATA DMA write lba="+p(e)+" lbacount="+p(t)+" bytecount="+p(i),32768),s+i>this.buffer.byteLength?(this.status=255,this.push_irq()):(this.status=88,this.device.dma_status|=1)},x.prototype.do_ata_write_sectors_dma=function(){var t=53===this.current_command,e=this.get_count(t),i=this.get_lba(t);t=e*this.sector_size,i*=this.sector_size;var s=this.device.prdt_addr,r=0;_("prdt addr: "+p(s,8),32768);let a=new Uint8Array(t);do{var o=this.cpu.read32s(s),n=this.cpu.read16(s+4),h=128&this.cpu.read8(s+7);n||(n=65536,_("dma: prd count was 0",32768)),_("dma write transfer dest="+p(o)+" prd_count="+p(n),32768),(o=this.cpu.mem8.subarray(o,o+n)).length,a.set(o,r),r+=n,s+=8}while(!h);a.length,this.buffer.set(i,a,(()=>{_("dma write completed",32768),this.ata_advance(this.current_command,e),this.status=80,this.push_irq(),this.device.dma_status&=-2,this.current_command=-1})),this.report_write(t)},x.prototype.get_chs=function(){var t=255&this.cylinder_low|this.cylinder_high<<8&65280,e=this.head,i=255&this.sector;return _("get_chs: c="+t+" h="+e+" s="+i,32768),(t*this.head_count+e)*this.sectors_per_track+i-1},x.prototype.get_lba28=function(){return 255&this.sector|this.cylinder_low<<8&65280|this.cylinder_high<<16&16711680|(15&this.head)<<24},x.prototype.get_lba48=function(){return(255&this.sector|this.cylinder_low<<8&65280|this.cylinder_high<<16&16711680|this.sector>>8<<24&4278190080)>>>0},x.prototype.get_lba=function(t){return t?this.get_lba48():this.is_lba?this.get_lba28():this.get_chs()},x.prototype.get_count=function(t){return t?0===(t=this.bytecount)&&(t=65536):0===(t=255&this.bytecount)&&(t=256),t},x.prototype.create_identify_packet=function(){if(16&this.drive_head)this.data_allocate(0);else{for(var t=0;512>t;t++)this.data[t]=0;t=Math.min(16383,this.cylinder_count),this.data_set([64,this.is_atapi?133:0,t,t>>8,0,0,this.head_count,this.head_count>>8,this.sectors_per_track/512,this.sectors_per_track/512>>8,0,2,this.sectors_per_track,this.sectors_per_track>>8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,2,4,0,0,0,0,0,0,0,0,0,56,118,32,54,68,72,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,128,0,1,0,0,2,0,0,0,2,0,2,7,0,t,t>>8,this.head_count,this.head_count>>8,this.sectors_per_track,0,255&this.sector_count,this.sector_count>>8&255,this.sector_count>>16&255,this.sector_count>>24&255,0,0,255&this.sector_count,this.sector_count>>8&255,this.sector_count>>16&255,this.sector_count>>24&255,0,0,160===this.current_command?0:7,160===this.current_command?0:4,0,0,30,0,30,0,30,0,30,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,126,0,0,0,0,0,0,116,0,64,0,64,0,116,0,64,0,0,0,0,0,0,0,0,0,0,1,96,0,0,0,0,0,0,0,0,0,0,0,0,255&this.sector_count,this.sector_count>>8&255,this.sector_count>>16&255,this.sector_count>>24&255]),this.data_end=this.data_length=512}},x.prototype.data_allocate=function(t){this.data_allocate_noclear(t);for(var e=0;e<t+3>>2;e++)this.data32[e]=0},x.prototype.data_allocate_noclear=function(t){this.data.length<t&&(this.data=new Uint8Array(t+3&-4),this.data16=new Uint16Array(this.data.buffer),this.data32=new Int32Array(this.data.buffer)),this.data_length=t,this.data_pointer=0},x.prototype.data_set=function(t){this.data_allocate_noclear(t.length),this.data.set(t)},x.prototype.report_read_start=function(){this.stats.loading=!0,this.bus.send("ide-read-start")},x.prototype.report_read_end=function(t){this.stats.loading=!1;var e=t/this.sector_size|0;this.stats.sectors_read+=e,this.stats.bytes_read+=t,this.bus.send("ide-read-end",[this.nr,t,e])},x.prototype.report_write=function(t){var e=t/this.sector_size|0;this.stats.sectors_written+=e,this.stats.bytes_written+=t,this.bus.send("ide-write-end",[this.nr,t,e])},x.prototype.read_buffer=function(t,e,i){let s=this.last_io_id++;this.in_progress_io_ids.add(s),this.buffer.get(t,e,(t=>{this.cancelled_io_ids.delete(s)?this.in_progress_io_ids.has(s):(this.in_progress_io_ids.delete(s),i(t))}))},x.prototype.cancel_io_operations=function(){for(let t of this.in_progress_io_ids)this.cancelled_io_ids.add(t);this.in_progress_io_ids.clear()},x.prototype.get_state=function(){var t=[];return t[0]=this.bytecount,t[1]=this.cylinder_count,t[2]=this.cylinder_high,t[3]=this.cylinder_low,t[4]=this.data_pointer,t[5]=0,t[6]=0,t[7]=0,t[8]=0,t[9]=this.drive_head,t[10]=this.error,t[11]=this.head,t[12]=this.head_count,t[13]=this.is_atapi,t[14]=this.is_lba,t[15]=this.lba_count,t[16]=this.data,t[17]=this.data_length,t[18]=this.sector,t[19]=this.sector_count,t[20]=this.sector_size,t[21]=this.sectors_per_drq,t[22]=this.sectors_per_track,t[23]=this.status,t[24]=this.write_dest,t[25]=this.current_command,t[26]=this.data_end,t[27]=this.current_atapi_command,t[28]=this.buffer,t},x.prototype.set_state=function(t){this.bytecount=t[0],this.cylinder_count=t[1],this.cylinder_high=t[2],this.cylinder_low=t[3],this.data_pointer=t[4],this.drive_head=t[9],this.error=t[10],this.head=t[11],this.head_count=t[12],this.is_atapi=t[13],this.is_lba=t[14],this.lba_count=t[15],this.data=t[16],this.data_length=t[17],this.sector=t[18],this.sector_count=t[19],this.sector_size=t[20],this.sectors_per_drq=t[21],this.sectors_per_track=t[22],this.status=t[23],this.write_dest=t[24],this.current_command=t[25],this.data_end=t[26],this.current_atapi_command=t[27],this.data16=new Uint16Array(this.data.buffer),this.data32=new Int32Array(this.data.buffer),this.buffer&&this.buffer.set_state(t[28])},k.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}},k.prototype.empty_port_read8=function(){return 255},k.prototype.empty_port_read16=function(){return 65535},k.prototype.empty_port_read32=function(){return-1},k.prototype.empty_port_write=function(t){},k.prototype.register_read=function(t,e,i,s,r){i&&(this.ports[t].read8=i),s&&(this.ports[t].read16=s),r&&(this.ports[t].read32=r),this.ports[t].device=e},k.prototype.register_write=function(t,e,i,s,r){i&&(this.ports[t].write8=i),s&&(this.ports[t].write16=s),r&&(this.ports[t].write32=r),this.ports[t].device=e},k.prototype.register_read_consecutive=function(t,e,i,s,r,a){function o(){return i.call(this)|s.call(this)<<8}r&&a?(this.register_read(t,e,i,o,(function(){return i.call(this)|s.call(this)<<8|r.call(this)<<16|a.call(this)<<24})),this.register_read(t+1,e,s),this.register_read(t+2,e,r,(function(){return r.call(this)|a.call(this)<<8})),this.register_read(t+3,e,a)):(this.register_read(t,e,i,o),this.register_read(t+1,e,s))},k.prototype.register_write_consecutive=function(t,e,i,s,r,a){function o(t){i.call(this,255&t),s.call(this,t>>8&255)}r&&a?(this.register_write(t,e,i,o,(function(t){i.call(this,255&t),s.call(this,t>>8&255),r.call(this,t>>16&255),a.call(this,t>>>24)})),this.register_write(t+1,e,s),this.register_write(t+2,e,r,(function(t){r.call(this,255&t),a.call(this,t>>8&255)})),this.register_write(t+3,e,a)):(this.register_write(t,e,i,o),this.register_write(t+1,e,s))},k.prototype.mmap_read32_shim=function(t){var e=this.cpu.memory_map_read8[t>>>17];return e(t)|e(t+1)<<8|e(t+2)<<16|e(t+3)<<24},k.prototype.mmap_write32_shim=function(t,e){var i=this.cpu.memory_map_write8[t>>>17];i(t,255&e),i(t+1,e>>8&255),i(t+2,e>>16&255),i(t+3,e>>>24)},k.prototype.mmap_register=function(t,e,i,s,r,a){for(_("mmap_register addr="+p(t>>>0,8)+" size="+p(e,8),32),r||(r=this.mmap_read32_shim.bind(this)),a||(a=this.mmap_write32_shim.bind(this)),t>>>=17;0<e;t++)this.cpu.memory_map_read8[t]=i,this.cpu.memory_map_write8[t]=s,this.cpu.memory_map_read32[t]=r,this.cpu.memory_map_write32[t]=a,e-=131072},k.prototype.port_write8=function(t,e){var i=this.ports[t];return i.write8===this.empty_port_write&&_("write8 port #"+p(t,4)+" <- "+p(e,2)+this.get_port_description(t),32),i.write8.call(i.device,e)},k.prototype.port_write16=function(t,e){var i=this.ports[t];return i.write16===this.empty_port_write&&_("write16 port #"+p(t,4)+" <- "+p(e,4)+this.get_port_description(t),32),i.write16.call(i.device,e)},k.prototype.port_write32=function(t,e){var i=this.ports[t];return i.write32===this.empty_port_write&&_("write32 port #"+p(t,4)+" <- "+p(e>>>0,8)+this.get_port_description(t),32),i.write32.call(i.device,e)},k.prototype.port_read8=function(t){var e=this.ports[t];return e.read8===this.empty_port_read8&&_("read8 port #"+p(t,4)+this.get_port_description(t),32),e=e.read8.call(e.device),p(t),e},k.prototype.port_read16=function(t){var e=this.ports[t];return e.read16===this.empty_port_read16&&_("read16 port #"+p(t,4)+this.get_port_description(t),32),e=e.read16.call(e.device),p(t),e},k.prototype.port_read32=function(t){var e=this.ports[t];return e.read32===this.empty_port_read32&&_("read32 port #"+p(t,4)+this.get_port_description(t),32),e.read32.call(e.device)};var A={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"};k.prototype.get_port_description=function(t){return A[t]?" ("+A[t]+")":""};function I(t){this.cpu=t,this.ioredtbl_config=new Int32Array(24),this.ioredtbl_destination=new Int32Array(24);for(var e=0;e<this.ioredtbl_config.length;e++)this.ioredtbl_config[e]=65536;this.ioregsel=0,this.ioapic_id=0,this.irq_value=this.irr=0,dbg_assert(32<=MMAP_BLOCK_SIZE),t.io.mmap_register(4273995776,MMAP_BLOCK_SIZE,(t=>(t=t-4273995776|0)>=16&&t<20?(t-=16,dbg_log("ioapic read8 byte "+t+" "+h(this.ioregsel),LOG_APIC),this.read(this.ioregsel)>>8*t&255):(dbg_log("Unexpected IOAPIC register read: "+h(t>>>0),LOG_APIC),dbg_assert(!1),0)),((t,e)=>{dbg_assert(!1,"unsupported write8 from ioapic: "+h(t>>>0))}),(t=>0===(t=t-4273995776|0)?this.ioregsel:16===t?this.read(this.ioregsel):(dbg_log("Unexpected IOAPIC register read: "+h(t>>>0),LOG_APIC),dbg_assert(!1),0)),((t,e)=>{0===(t=t-4273995776|0)?this.ioregsel=e:16===t?this.write(this.ioregsel,e):(dbg_log("Unexpected IOAPIC register write: "+h(t>>>0)+" <- "+h(e>>>0,8),LOG_APIC),dbg_assert(!1))}))}function O(t,e){this.stopped=this.running=!1,this.tick_counter=0,this.worker=null,this.cpu=new it(t,e,(()=>{this.idle&&this.next_tick(0)})),this.bus=t,t.register("cpu-init",this.init,this),t.register("cpu-run",this.run,this),t.register("cpu-stop",this.stop,this),t.register("cpu-restart",this.restart,this),this.register_yield()}if(I.prototype.remote_eoi=function(t){for(var e=0;e<24;e++){var i=this.ioredtbl_config[e];(255&i)===t&&16384&i&&(dbg_log("Clear remote IRR for irq="+h(e),LOG_APIC),this.ioredtbl_config[e]&=-16385,this.check_irq(e))}},I.prototype.check_irq=function(t){var e=1<<t;if(0!=(this.irr&e)){var i=this.ioredtbl_config[t];if(0==(65536&i)){var s=i>>8&7,r=i>>11&1,a=255&i,o=this.ioredtbl_destination[t]>>>24,_=32768==(32768&i);if(0==(32768&i))this.irr&=~e;else if(this.ioredtbl_config[t]|=16384,16384&i)return void dbg_log("No route: level interrupt and remote IRR still set",LOG_APIC);0===s||1===s?this.cpu.devices.apic.route(a,s,_,o,r):dbg_assert(!1,"TODO"),this.ioredtbl_config[t]&=-4097}}},I.prototype.set_irq=function(t){if(t>=24)dbg_assert(!1,"Bad irq: "+t,LOG_APIC);else{var e=1<<t;0==(this.irq_value&e)&&(APIC_LOG_VERBOSE&&dbg_log("apic set irq "+t,LOG_APIC),this.irq_value|=e,65536!=(98304&this.ioredtbl_config[t])&&(this.irr|=e,this.check_irq(t)))}},I.prototype.clear_irq=function(t){if(t>=24)dbg_assert(!1,"Bad irq: "+t,LOG_APIC);else{var e=1<<t;(this.irq_value&e)===e&&(this.irq_value&=~e,32768&this.ioredtbl_config[t]&&(this.irr&=~e))}},I.prototype.read=function(t){if(0===t)return dbg_log("IOAPIC Read id",LOG_APIC),this.ioapic_id<<24;if(1===t)return dbg_log("IOAPIC Read version",LOG_APIC),1507345;if(2===t)return dbg_log("IOAPIC Read arbitration id",LOG_APIC),this.ioapic_id<<24;if(16<=t&&t<64){var e=t-16>>1;return 1&t?(t=this.ioredtbl_destination[e],dbg_log("IOAPIC Read destination irq="+h(e)+" -> "+h(t,8),LOG_APIC)):(t=this.ioredtbl_config[e],dbg_log("IOAPIC Read config irq="+h(e)+" -> "+h(t,8),LOG_APIC)),t}return dbg_log("IOAPIC register read outside of range "+h(t),LOG_APIC),dbg_assert(!1),0},I.prototype.write=function(t,e){if(0===t)this.ioapic_id=e>>>24&15;else if(1===t||2===t)dbg_log("Invalid write: "+t,LOG_APIC);else if(16<=t&&t<64){var i=t-16>>1;if(1&t)this.ioredtbl_destination[i]=4278190080&e,dbg_log("Write destination "+h(e>>>0,8)+" irq="+h(i)+" dest="+h(e>>>24,2),LOG_APIC);else{this.ioredtbl_config[i]=110591&e|-110592&this.ioredtbl_config[i],t=255&e;var s=e>>8&7,r=e>>11&1,a=e>>15&1,o=e>>16&1;dbg_log("Write config "+h(e>>>0,8)+" irq="+h(i)+" vector="+h(t,2)+" deliverymode="+DELIVERY_MODES[s]+" destmode="+DESTINATION_MODES[r]+" is_level="+a+" disabled="+o,LOG_APIC),this.check_irq(i)}}else dbg_log("IOAPIC register write outside of range "+h(t)+": "+h(e>>>0,8),LOG_APIC),dbg_assert(!1)},I.prototype.get_state=function(){var t=[];return t[0]=this.ioredtbl_config,t[1]=this.ioredtbl_destination,t[2]=this.ioregsel,t[3]=this.ioapic_id,t[4]=this.irr,t[5]=this.irq_value,t},I.prototype.set_state=function(t){this.ioredtbl_config=t[0],this.ioredtbl_destination=t[1],this.ioregsel=t[2],this.ioapic_id=t[3],this.irr=t[4],this.irq_value=t[5]},O.prototype.run=function(){this.stopped=!1,this.running||(this.running=!0,this.bus.send("emulator-started")),this.next_tick(0)},O.prototype.do_tick=function(){if(this.stopped||!this.running)this.stopped=this.running=!1,this.bus.send("emulator-stopped");else{this.idle=!1;var t=this.cpu.main_run();this.next_tick(t)}},O.prototype.next_tick=function(t){let e=++this.tick_counter;this.idle=!0,this.yield(t,e)},O.prototype.yield_callback=function(t){t===this.tick_counter&&this.do_tick()},O.prototype.stop=function(){this.running&&(this.stopped=!0)},O.prototype.destroy=function(){this.unregister_yield()},O.prototype.restart=function(){this.cpu.reset_cpu(),this.cpu.load_bios()},O.prototype.init=function(t){this.cpu.init(t,this.bus),this.bus.send("emulator-ready")},"undefined"!=typeof process&&global.setImmediate)O.prototype.yield=function(t,e){1>t?global.setImmediate((t=>this.yield_callback(t)),e):setTimeout((t=>this.yield_callback(t)),t,e)},O.prototype.register_yield=function(){},O.prototype.unregister_yield=function(){};else if("undefined"!=typeof Worker){function t(){globalThis.onmessage=function(t){let e=t.data.t;1>e?postMessage(t.data.tick):setTimeout((()=>postMessage(t.data.tick)),e)}}O.prototype.register_yield=function(){let e=URL.createObjectURL(new Blob(["("+t.toString()+")()"],{type:"text/javascript"}));this.worker=new Worker(e),this.worker.onmessage=t=>this.yield_callback(t.data),URL.revokeObjectURL(e)},O.prototype.yield=function(t,e){this.worker.postMessage({t:t,tick:e})},O.prototype.unregister_yield=function(){this.worker.terminate(),this.worker=null}}else O.prototype.yield=function(t){setTimeout((()=>{this.do_tick()}),t)},O.prototype.register_yield=function(){},O.prototype.unregister_yield=function(){};O.prototype.save_state=function(){return this.cpu.save_state()},O.prototype.restore_state=function(t){return this.cpu.restore_state(t)},O.microtick="object"==typeof performance&&performance.now?performance.now.bind(performance):"object"==typeof process&&process.hrtime?function(){var t=process.hrtime();return 1e3*t[0]+t[1]/1e6}:Date.now;function R(t,e,i){for(this.cpu=t,this.pci=t.devices.pci,this.preserve_mac_from_state_image=i,this.bus=e,this.bus.register("net0-receive",(function(t){this.receive(t)}),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,255&this.port|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]),e=0;6>e;e++)this.memory[e<<1]=this.memory[e<<1|1]=this.mac[e];this.memory[28]=this.memory[29]=87,this.memory[30]=this.memory[31]=87,_("Mac: "+p(this.mac[0],2)+":"+p(this.mac[1],2)+":"+p(this.mac[2],2)+":"+p(this.mac[3],2)+":"+p(this.mac[4],2)+":"+p(this.mac[5],2),1048576),this.rsar=0,this.pstart=64,this.pstop=128,this.boundary=this.curpg=76,(e=t.io).register_read(0|this.port,this,(function(){return _("Read cmd",1048576),this.cr})),e.register_write(0|this.port,this,(function(t){this.cr=t,_("Write command: "+p(t,2)+" newpg="+(this.cr>>6)+" txcr="+p(this.txcr,2),1048576),1&this.cr||(24&t&&0===this.rcnt&&this.do_interrupt(64),4&t&&(t=this.tpsr<<8,t=this.memory.subarray(t,t+this.tcnt),this.bus.send("net0-send",t),this.bus.send("eth-transmit-end",[t.length]),this.cr&=-5,this.do_interrupt(2),_("Command: Transfer. length="+p(t.byteLength),1048576)))})),e.register_read(13|this.port,this,(function(){return _("Read counter0",1048576),0})),e.register_read(14|this.port,this,(function(){return _("Read counter1",1048576),0})),e.register_read(15|this.port,this,(function(){return _("Read counter2",1048576),0})),e.register_read(31|this.port,this,(function(){var t=this.get_page();return 0===t?(_("Read reset",1048576),this.do_interrupt(128)):_("Read pg"+t+"/1f",1048576),0})),e.register_write(31|this.port,this,(function(t){var e=this.get_page();_(0===e?"Write reset: "+p(t,2):"Write pg"+e+"/1f: "+p(t),1048576)})),e.register_read(1|this.port,this,(function(){var t=this.get_page();return 0===t?this.pstart:1===t?(_("Read pg1/01 (mac[0])",1048576),this.mac[0]):2===t?this.pstart:(_("Read pg"+t+"/01"),0)})),e.register_write(1|this.port,this,(function(t){var e=this.get_page();0===e?(_("start page: "+p(t,2),1048576),this.pstart=t):1===e?(_("mac[0] = "+p(t),1048576),this.mac[0]=t):_(3===e?"Unimplemented: Write pg3/01 (9346CR): "+p(t):"Write pg"+e+"/01: "+p(t),1048576)})),e.register_read(2|this.port,this,(function(){var t=this.get_page();return 0===t?this.pstop:1===t?(_("Read pg1/02 (mac[1])",1048576),this.mac[1]):2===t?this.pstop:(_("Read pg"+t+"/02",1048576),0)})),e.register_write(2|this.port,this,(function(t){var e=this.get_page();0===e?(_("stop page: "+p(t,2),1048576),t>this.memory.length>>8&&(t=this.memory.length>>8,_("XXX: Adjusting stop page to "+p(t),1048576)),this.pstop=t):1===e?(_("mac[1] = "+p(t),1048576),this.mac[1]=t):_("Write pg"+e+"/02: "+p(t),1048576)})),e.register_read(7|this.port,this,(function(){var t=this.get_page();return 0===t?(_("Read isr: "+p(this.isr,2),1048576),this.isr):1===t?(_("Read curpg: "+p(this.curpg,2),1048576),this.curpg):void 0})),e.register_write(7|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write isr: "+p(t,2),1048576),this.isr&=~t,this.update_irq()):1===e&&(_("Write curpg: "+p(t,2),1048576),this.curpg=t)})),e.register_write(13|this.port,this,(function(t){var e=this.get_page();0===e?(this.txcr=t,_("Write tx config: "+p(t,2),1048576)):_("Unimplemented: Write pg"+e+"/0d "+p(t,2),1048576)})),e.register_write(14|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write data configuration: "+p(t,2),1048576),this.dcfg=t):_("Unimplemented: Write pg"+e+"/0e "+p(t,2),1048576)})),e.register_read(10|this.port,this,(function(){return 0===this.get_page()?(_("Read pg0/0a",1048576),80):0})),e.register_write(10|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write remote byte count low: "+p(t,2),1048576),this.rcnt=65280&this.rcnt|255&t):_("Unimplemented: Write pg"+e+"/0a "+p(t,2),1048576)})),e.register_read(11|this.port,this,(function(){return 0===this.get_page()?(_("Read pg0/0b",1048576),67):0})),e.register_write(11|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write remote byte count high: "+p(t,2),1048576),this.rcnt=255&this.rcnt|t<<8&65280):_("Unimplemented: Write pg"+e+"/0b "+p(t,2),1048576)})),e.register_read(8|this.port,this,(function(){var t=this.get_page();if(0===t)return _("Read remote start address low",1048576),255&this.rsar;_("Unimplemented: Read pg"+t+"/08",1048576)})),e.register_write(8|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write remote start address low: "+p(t,2),1048576),this.rsar=65280&this.rsar|255&t):_("Unimplemented: Write pg"+e+"/08 "+p(t,2),1048576)})),e.register_read(9|this.port,this,(function(){var t=this.get_page();if(0===t)return _("Read remote start address high",1048576),this.rsar>>8&255;_("Unimplemented: Read pg"+t+"/09",1048576)})),e.register_write(9|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write remote start address low: "+p(t,2),1048576),this.rsar=255&this.rsar|t<<8&65280):_("Unimplemented: Write pg"+e+"/09 "+p(t,2),1048576)})),e.register_write(15|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write interrupt mask register: "+p(t,2)+" isr="+p(this.isr,2),1048576),this.imr=t,this.update_irq()):_("Unimplemented: Write pg"+e+"/0f "+p(t,2),1048576)})),e.register_read(3|this.port,this,(function(){var t=this.get_page();return 0===t?(_("Read boundary: "+p(this.boundary,2),1048576),this.boundary):1===t?(_("Read pg1/03 (mac[2])",1048576),this.mac[2]):(_(3===t?"Unimplemented: Read pg3/03 (CONFIG0)":"Read pg"+t+"/03",1048576),0)})),e.register_write(3|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write boundary: "+p(t,2),1048576),this.boundary=t):1===e?(_("mac[2] = "+p(t),1048576),this.mac[2]=t):_("Write pg"+e+"/03: "+p(t),1048576)})),e.register_read(4|this.port,this,(function(){var t=this.get_page();return 0===t?this.tsr:1===t?(_("Read pg1/04 (mac[3])",1048576),this.mac[3]):(_("Read pg"+t+"/04",1048576),0)})),e.register_write(4|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write tpsr: "+p(t,2),1048576),this.tpsr=t):1===e?(_("mac[3] = "+p(t),1048576),this.mac[3]=t):_("Write pg"+e+"/04: "+p(t),1048576)})),e.register_read(5|this.port,this,(function(){var t=this.get_page();return 0===t?(_("Unimplemented: Read pg0/05 (NCR: Number of Collisions Register)",1048576),0):1===t?(_("Read pg1/05 (mac[4])",1048576),this.mac[4]):(_(3===t?"Unimplemented: Read pg3/05 (CONFIG2)":"Read pg"+t+"/05",1048576),0)})),e.register_write(5|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write tcnt low: "+p(t,2),1048576),this.tcnt=-256&this.tcnt|t):1===e?(_("mac[4] = "+p(t),1048576),this.mac[4]=t):_(3===e?"Unimplemented: Write pg3/05 (CONFIG2): "+p(t):"Write pg"+e+"/05: "+p(t),1048576)})),e.register_read(6|this.port,this,(function(){var t=this.get_page();return 0===t?0:1===t?(_("Read pg1/06 (mac[5])",1048576),this.mac[5]):(_(3===t?"Unimplemented: Read pg3/06 (CONFIG3)":"Read pg"+t+"/06",1048576),0)})),e.register_write(6|this.port,this,(function(t){var e=this.get_page();0===e?(_("Write tcnt high: "+p(t,2),1048576),this.tcnt=255&this.tcnt|t<<8):1===e?(_("mac[5] = "+p(t),1048576),this.mac[5]=t):_(3===e?"Unimplemented: Write pg3/06 (CONFIG3): "+p(t):"Write pg"+e+"/06: "+p(t),1048576)})),e.register_read(12|this.port,this,(function(){var t=this.get_page();return 0===t?9:(_("Unimplemented: Read pg"+t+"/0c",1048576),0)})),e.register_write(12|this.port,this,(function(t){var e=this.get_page();0===e?(_("RX configuration reg write: "+p(t,2),1048576),this.rxcr=t):_("Unimplemented: Write pg"+e+"/0c: "+p(t),1048576)})),e.register_read(16|this.port,this,this.data_port_read8,this.data_port_read16,this.data_port_read32),e.register_write(16|this.port,this,this.data_port_write16,this.data_port_write16,this.data_port_write32),t.devices.pci.register_device(this)}R.prototype.get_state=function(){var t=[];return t[0]=this.isr,t[1]=this.imr,t[2]=this.cr,t[3]=this.dcfg,t[4]=this.rcnt,t[5]=this.tcnt,t[6]=this.tpsr,t[7]=this.rsar,t[8]=this.pstart,t[9]=this.curpg,t[10]=this.boundary,t[11]=this.pstop,t[12]=this.rxcr,t[13]=this.txcr,t[14]=this.tsr,t[15]=this.mac,t[16]=this.memory,t},R.prototype.set_state=function(t){this.isr=t[0],this.imr=t[1],this.cr=t[2],this.dcfg=t[3],this.rcnt=t[4],this.tcnt=t[5],this.tpsr=t[6],this.rsar=t[7],this.pstart=t[8],this.curpg=t[9],this.boundary=t[10],this.pstop=t[11],this.rxcr=t[12],this.txcr=t[13],this.tsr=t[14],this.preserve_mac_from_state_image&&(this.mac=t[15],this.memory=t[16])},R.prototype.do_interrupt=function(t){_("Do interrupt "+p(t,2),1048576),this.isr|=t,this.update_irq()},R.prototype.update_irq=function(){this.imr&this.isr?this.pci.raise_irq(this.pci_id):this.pci.lower_irq(this.pci_id)},R.prototype.data_port_write=function(t){(16>=this.rsar||this.rsar>=16384&&this.rsar<32768)&&(this.memory[this.rsar]=t),this.rsar++,this.rcnt--,this.rsar>=this.pstop<<8&&(this.rsar+=this.pstart-this.pstop<<8),0===this.rcnt&&this.do_interrupt(64)},R.prototype.data_port_write16=function(t){this.data_port_write(t),1&this.dcfg&&this.data_port_write(t>>8)},R.prototype.data_port_write32=function(t){this.data_port_write(t),this.data_port_write(t>>8),this.data_port_write(t>>16),this.data_port_write(t>>24)},R.prototype.data_port_read=function(){let t=0;return this.rsar<32768&&(t=this.memory[this.rsar]),this.rsar++,this.rcnt--,this.rsar>=this.pstop<<8&&(this.rsar+=this.pstart-this.pstop<<8),0===this.rcnt&&this.do_interrupt(64),t},R.prototype.data_port_read8=function(){return 255&this.data_port_read16()},R.prototype.data_port_read16=function(){return 1&this.dcfg?this.data_port_read()|this.data_port_read()<<8:this.data_port_read()},R.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},R.prototype.receive=function(t){if(!(1&this.cr)&&(this.bus.send("eth-receive-end",[t.length]),16&this.rxcr||4&this.rxcr&&255===t[0]&&255===t[1]&&255===t[2]&&255===t[3]&&255===t[4]&&255===t[5]||!(8&this.rxcr&&1==(1&t[0])||t[0]!==this.mac[0]||t[1]!==this.mac[1]||t[2]!==this.mac[2]||t[3]!==this.mac[3]||t[4]!==this.mac[4]||t[5]!==this.mac[5]))){var e=this.curpg<<8,i=Math.max(60,t.length)+4,s=e+4,r=this.curpg+1+(i>>8),a=e+i,o=1+(i>>8),n=this.boundary>this.curpg?this.boundary-this.curpg:this.pstop-this.curpg+this.boundary-this.pstart;n<o&&0!==this.boundary?_("Buffer full, dropping packet pstart="+p(this.pstart)+" pstop="+p(this.pstop)+" curpg="+p(this.curpg)+" needed="+p(o)+" boundary="+p(this.boundary)+" available="+p(n),1048576):(a>this.pstop<<8?(t.length,a=(this.pstop<<8)-s,this.memory.set(t.subarray(0,a),s),this.memory.set(t.subarray(a),this.pstart<<8),_("rcv cut="+p(a),1048576)):(this.memory.set(t,s),60>t.length&&this.memory.fill(0,s+t.length,s+60)),r>=this.pstop&&(r+=this.pstart-this.pstop),this.memory[e]=1,this.memory[e+1]=r,this.memory[e+2]=i,this.memory[e+3]=i>>8,this.curpg=r,_("rcv offset="+p(e)+" len="+p(i)+" next="+p(r),1048576),this.do_interrupt(1))}},R.prototype.get_page=function(){return this.cr>>6&3};function E(t){this.pci_addr=new Uint8Array(4),this.pci_value=new Uint8Array(4),this.pci_response=new Uint8Array(4),this.pci_status=new Uint8Array(4),this.pci_addr32=new Int32Array(this.pci_addr.buffer),this.pci_value32=new Int32Array(this.pci_value.buffer),this.pci_response32=new Int32Array(this.pci_response.buffer),this.pci_status32=new Int32Array(this.pci_status.buffer),this.device_spaces=[],this.devices=[],this.cpu=t;for(var e=0;256>e;e++)this.device_spaces[e]=void 0,this.devices[e]=void 0;this.io=t.io,t.io.register_write(3324,this,(function(t){this.pci_write8(this.pci_addr32[0],t)}),(function(t){this.pci_write16(this.pci_addr32[0],t)}),(function(t){this.pci_write32(this.pci_addr32[0],t)})),t.io.register_write(3325,this,(function(t){this.pci_write8(this.pci_addr32[0]+1|0,t)})),t.io.register_write(3326,this,(function(t){this.pci_write8(this.pci_addr32[0]+2|0,t)}),(function(t){this.pci_write16(this.pci_addr32[0]+2|0,t)})),t.io.register_write(3327,this,(function(t){this.pci_write8(this.pci_addr32[0]+3|0,t)})),t.io.register_read_consecutive(3324,this,(function(){return this.pci_response[0]}),(function(){return this.pci_response[1]}),(function(){return this.pci_response[2]}),(function(){return this.pci_response[3]})),t.io.register_read_consecutive(3320,this,(function(){return this.pci_status[0]}),(function(){return this.pci_status[1]}),(function(){return this.pci_status[2]}),(function(){return this.pci_status[3]})),t.io.register_write_consecutive(3320,this,(function(t){this.pci_addr[0]=252&t}),(function(e){2==(6&this.pci_addr[1])&&6==(6&e)?(_("CPU reboot via PCI"),t.reboot_internal()):this.pci_addr[1]=e}),(function(t){this.pci_addr[2]=t}),(function(t){this.pci_addr[3]=t,this.pci_query()})),this.register_device({pci_id:0,pci_space:[134,128,55,18,0,0,0,0,2,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0],pci_bars:[],name:"82441FX PMC"}),this.isa_bridge={pci_id:8,pci_space:[134,128,0,112,7,0,0,2,0,0,1,6,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],pci_bars:[],name:"82371SB PIIX3 ISA"},this.isa_bridge_space=this.register_device(this.isa_bridge),this.isa_bridge_space8=new Uint8Array(this.isa_bridge_space.buffer)}E.prototype.get_state=function(){for(var t=[],e=0;256>e;e++)t[e]=this.device_spaces[e];return t[256]=this.pci_addr,t[257]=this.pci_value,t[258]=this.pci_response,t[259]=this.pci_status,t},E.prototype.set_state=function(t){for(var e=0;256>e;e++){var i=this.devices[e],s=t[e];if(i&&s){for(var r=0;r<i.pci_bars.length;r++){var a=s[4+r];if(1&a){var o=i.pci_bars[r];this.set_io_bars(o,65534&o.original_bar,65534&a)}}this.device_spaces[e].set(s)}else i&&_("Warning: While restoring PCI device: Device exists in current configuration but not in snapshot ("+i.name+")"),s&&_("Warning: While restoring PCI device: Device doesn't exist in current configuration but does in snapshot (device "+p(e,2)+")")}this.pci_addr.set(t[256]),this.pci_value.set(t[257]),this.pci_response.set(t[258]),this.pci_status.set(t[259])},E.prototype.pci_query=function(){var t=this.pci_addr[2]<<8|this.pci_addr[1],e=252&this.pci_addr[0],i=t>>3&31,s="query enabled="+(this.pci_addr[3]>>7)+" bdf="+p(t,4);s+=" dev="+p(i,2),s+=" addr="+p(e,2),void 0!==(i=this.device_spaces[t])?(this.pci_status32[0]=-2147483648,this.pci_response32[0]=e<i.byteLength?i[e>>2]:0,s+=" "+p(this.pci_addr32[0]>>>0,8)+" -> "+p(this.pci_response32[0]>>>0,8),e>=i.byteLength&&(s+=" (undef)"),s+=" ("+this.devices[t].name+")",_(s,2048)):(this.pci_response32[0]=-1,this.pci_status32[0]=0)},E.prototype.pci_write8=function(t,e){var i=t>>8&65535;t&=255;var s=new Uint8Array(this.device_spaces[i].buffer),r=this.devices[i];s&&(p(t),_("PCI write8 dev="+p(i>>3,2)+" ("+r.name+") addr="+p(t,4)+" value="+p(e,2),2048),s[t]=e)},E.prototype.pci_write16=function(t,e){var i=t>>8&65535;t&=255;var s=new Uint16Array(this.device_spaces[i].buffer),r=this.devices[i];s&&(16<=t&&44>t?_("Warning: PCI: Expected 32-bit write, got 16-bit (addr: "+p(t)+")"):(p(t),_("PCI writ16 dev="+p(i>>3,2)+" ("+r.name+") addr="+p(t,4)+" value="+p(e,4),2048),s[t>>>1]=e))},E.prototype.pci_write32=function(t,e){var i=t>>8&65535;t&=255;var s=this.device_spaces[i],r=this.devices[i];if(s)if(16<=t&&40>t){var a=t-16>>2,o=r.pci_bars[a];_("BAR"+a+" exists="+(o?"y":"n")+" changed to "+p(e>>>0)+" dev="+p(i>>3,2)+" ("+r.name+") ",2048),o?(o.size,o.size,r=1&s[i=t>>2],-1==(3|e|o.size-1)?(e=~(o.size-1)|r,0===r&&(s[i]=e)):0===r&&((-16&e)!=(-16&(a=o.original_bar))&&_("Warning: Changing memory bar not supported, ignored",2048),s[i]=a),1===r&&(r=65534&s[i],a=65534&e,_("io bar changed from "+p(r>>>0,8)+" to "+p(a>>>0,8)+" size="+o.size,2048),this.set_io_bars(o,r,a),s[i]=1|e)):s[t>>2]=0,_("BAR effective value: "+p(s[t>>2]>>>0),2048)}else 48===t?(_("PCI write rom address dev="+p(i>>3,2)+" ("+r.name+") value="+p(e>>>0,8),2048),s[t>>2]=r.pci_rom_size?-1==(2047|e)?0|-r.pci_rom_size:0|r.pci_rom_address:0):4===t?_("PCI write dev="+p(i>>3,2)+" ("+r.name+") addr="+p(t,4)+" value="+p(e>>>0,8),2048):(_("PCI write dev="+p(i>>3,2)+" ("+r.name+") addr="+p(t,4)+" value="+p(e>>>0,8),2048),s[t>>>2]=e)},E.prototype.register_device=function(t){t.pci_id,t.pci_space,t.pci_bars;var e=t.pci_id;_("PCI register bdf="+p(e)+" ("+t.name+")",2048),this.devices[e],t.pci_space.length,this.devices.length;var i=new Int32Array(64);i.set(new Int32Array(new Uint8Array(t.pci_space).buffer)),this.device_spaces[e]=i,this.devices[e]=t,e=i.slice(4,10);for(var s=0;s<t.pci_bars.length;s++){var r=t.pci_bars[s];if(r){var a=e[s],o=1&a;if(r.original_bar=a,r.entries=[],0!==o)for(a&=-2,o=0;o<r.size;o++)r.entries[o]=this.io.ports[a+o]}}return i},E.prototype.set_io_bars=function(t,e,i){var s=t.size;_("Move io bars: from="+p(e)+" to="+p(i)+" count="+s,2048);for(var r=this.io.ports,a=0;a<s;a++){var o=r[e+a];r[e+a]=this.io.create_empty_entry(),o.read8===this.io.empty_port_read8&&o.read16===this.io.empty_port_read16&&o.read32===this.io.empty_port_read32&&o.write8===this.io.empty_port_write&&o.write16===this.io.empty_port_write&&o.write32===this.io.empty_port_write&&_("Warning: Bad IO bar: Source not mapped, port="+p(e+a,4),2048),o=r[i+a],r[i+a]=t.entries[a],o.read8!==this.io.empty_port_read8&&o.read16!==this.io.empty_port_read16&&o.read32!==this.io.empty_port_read32&&o.write8!==this.io.empty_port_write&&o.write16!==this.io.empty_port_write&&o.write32!==this.io.empty_port_write||_("Warning: Bad IO bar: Target already mapped, port="+p(i+a,4),2048)}},E.prototype.raise_irq=function(t){this.cpu.device_raise_irq(this.isa_bridge_space8[96+((this.device_spaces[t][15]>>8&255)-1+((t>>3)-1&255)&3)])},E.prototype.lower_irq=function(t){this.cpu.device_lower_irq(this.isa_bridge_space8[96+((this.device_spaces[t][15]>>8&255)+(t>>3&255)-2&3)])};function C(t,e){this.irq_value=this.irr=this.isr=this.irq_map=this.irq_mask=0,this.requested_irq=-1,this.master=e,this.is_master=void 0===this.master,this.slave=void 0,this.name=this.is_master?"master":"slave ",this.expect_icw4=!1,this.read_isr=this.state=0,this.auto_eoi=1,this.elcr=this.special_mask_mode=0,this.cpu=t,this.is_master?(this.slave=new C(this.cpu,this),this.check_irqs=function(){if(0<=this.requested_irq)this.cpu.handle_irqs();else{var t=this.irr&this.irq_mask;if(t){t&=-t;var e=this.special_mask_mode?this.irq_mask:-1;this.isr&&(this.isr&-this.isr&e)<=t?_("master> higher prio: isr="+p(this.isr,2)+" mask="+p(255&this.irq_mask,2)+" irq="+p(t,2),128):(this.requested_irq=c.int_log2_byte(t),this.cpu.handle_irqs())}}},this.acknowledge_irq=function(){if(-1!==this.requested_irq)if(0===this.irr)this.requested_irq=-1;else{this.irr,this.requested_irq;var t=1<<this.requested_irq;0==(this.elcr&t)&&(this.irr&=~t),this.auto_eoi||(this.isr|=t),2===this.requested_irq?this.slave.acknowledge_irq():this.cpu.pic_call_irq(this.irq_map|this.requested_irq),this.requested_irq=-1,this.check_irqs()}}):(this.check_irqs=function(){if(0<=this.requested_irq)this.cpu.handle_irqs();else{var t=this.irr&this.irq_mask;if(t){t&=-t;var e=this.special_mask_mode?this.irq_mask:-1;this.isr&&(this.isr&-this.isr&e)<=t||(this.requested_irq=c.int_log2_byte(t),this.master.set_irq(2))}}},this.acknowledge_irq=function(){if(-1!==this.requested_irq)if(0===this.irr)this.requested_irq=-1,this.master.irq_value&=-5,this.cpu.pic_call_irq(7|this.irq_map);else{this.irr,this.requested_irq;var t=1<<this.requested_irq;0==(this.elcr&t)&&(this.irr&=~t),this.auto_eoi||(this.isr|=t),this.master.irq_value&=-5,this.cpu.pic_call_irq(this.irq_map|this.requested_irq),this.requested_irq=-1,this.check_irqs()}}),this.dump=function(){_("mask: "+p(255&this.irq_mask),128),_("base: "+p(this.irq_map),128),_("requested: "+p(this.irr),128),_("serviced: "+p(this.isr),128),this.is_master&&this.slave.dump()},this.is_master?(t=32,e=1232):(t=160,e=1233),this.cpu.io.register_write(t,this,this.port20_write),this.cpu.io.register_read(t,this,this.port20_read),this.cpu.io.register_write(1|t,this,this.port21_write),this.cpu.io.register_read(1|t,this,this.port21_read),this.cpu.io.register_write(e,this,this.port4D0_write),this.cpu.io.register_read(e,this,this.port4D0_read),this.is_master?(this.set_irq=function(t){8<=t?this.slave.set_irq(t-8):(t=1<<t,0==(this.irq_value&t)&&(this.irr|=t,this.irq_value|=t,this.check_irqs()))},this.clear_irq=function(t){8<=t?this.slave.clear_irq(t-8):(t=1<<t,this.irq_value&t&&(this.irq_value&=~t,this.irr&=~t,this.check_irqs()))}):(this.set_irq=function(t){t=1<<t,0==(this.irq_value&t)&&(this.irr|=t,this.irq_value|=t,this.check_irqs())},this.clear_irq=function(t){t=1<<t,this.irq_value&t&&(this.irq_value&=~t,this.irr&=~t,this.check_irqs())}),this.get_isr=function(){return this.isr}}C.prototype.get_state=function(){var t=[];return t[0]=this.irq_mask,t[1]=this.irq_map,t[2]=this.isr,t[3]=this.irr,t[4]=this.is_master,t[5]=this.slave,t[6]=this.expect_icw4,t[7]=this.state,t[8]=this.read_isr,t[9]=this.auto_eoi,t[10]=this.elcr,t},C.prototype.set_state=function(t){this.irq_mask=t[0],this.irq_map=t[1],this.isr=t[2],this.irr=t[3],this.is_master=t[4],this.slave&&this.slave.set_state(t[5]),this.expect_icw4=t[6],this.state=t[7],this.read_isr=t[8],this.auto_eoi=t[9],this.elcr=t[10]},C.prototype.port20_write=function(t){if(16&t)_("icw1 = "+p(t),128),this.irq_value=this.irq_mask=this.irr=this.isr=0,this.auto_eoi=1,this.requested_irq=-1,this.expect_icw4=1&t,this.state=1;else if(8&t)_("ocw3: "+p(t),128),2&t&&(this.read_isr=1&t),64&t&&(this.special_mask_mode=32==(32&t),_("special mask mode: "+this.special_mask_mode,128));else{_("eoi: "+p(t)+" ("+this.name+")",128);var e=t>>5;1===e?(this.isr&=this.isr-1,_("new isr: "+p(this.isr,2),128)):3===e?this.isr&=~(1<<(7&t)):192==(200&t)?_("lowest priority: "+p(7&t),128):(_("Unknown eoi: "+p(t),128),this.isr&=this.isr-1),this.check_irqs()}},C.prototype.port20_read=function(){return this.read_isr?(_("read port 20h (isr): "+p(this.isr),128),this.isr):(_("read port 20h (irr): "+p(this.irr),128),this.irr)},C.prototype.port21_write=function(t){0===this.state?this.expect_icw4?(this.expect_icw4=!1,this.auto_eoi=2&t,_("icw4: "+p(t)+" autoeoi="+this.auto_eoi,128)):(this.irq_mask=~t,this.check_irqs()):1===this.state?(this.irq_map=t,_("interrupts are mapped to "+p(this.irq_map)+" ("+this.name+")",128),this.state++):2===this.state&&(this.state=0,_("icw3: "+p(t),128))},C.prototype.port21_read=function(){return _("21h read "+p(255&~this.irq_mask),128),255&~this.irq_mask},C.prototype.port4D0_read=function(){return _("elcr read: "+p(this.elcr,2),128),this.elcr},C.prototype.port4D0_write=function(t){_("elcr write: "+p(t,2),128),this.elcr=t};function T(t,e){this.cpu=t,this.bus=e,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),t.io.register_read(97,this,(function(){var t=O.microtick();return(66.66666666666667*t&1)<<4|(t=this.did_rollover(2,t))<<5})),t.io.register_write(97,this,(function(t){1&t?this.bus.send("pcspeaker-enable"):this.bus.send("pcspeaker-disable")})),t.io.register_read(64,this,(function(){return this.counter_read(0)})),t.io.register_read(65,this,(function(){return this.counter_read(1)})),t.io.register_read(66,this,(function(){return this.counter_read(2)})),t.io.register_write(64,this,(function(t){this.counter_write(0,t)})),t.io.register_write(65,this,(function(t){this.counter_write(1,t)})),t.io.register_write(66,this,(function(t){this.counter_write(2,t),this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]])})),t.io.register_write(67,this,this.port43_write)}T.prototype.get_state=function(){var t=[];return t[0]=this.counter_next_low,t[1]=this.counter_enabled,t[2]=this.counter_mode,t[3]=this.counter_read_mode,t[4]=this.counter_latch,t[5]=this.counter_latch_value,t[6]=this.counter_reload,t[7]=this.counter_start_time,t[8]=this.counter_start_value,t},T.prototype.set_state=function(t){this.counter_next_low=t[0],this.counter_enabled=t[1],this.counter_mode=t[2],this.counter_read_mode=t[3],this.counter_latch=t[4],this.counter_latch_value=t[5],this.counter_reload=t[6],this.counter_start_time=t[7],this.counter_start_value=t[8]},T.prototype.timer=function(t,e){var i=100;return e||(this.counter_enabled[0]&&this.did_rollover(0,t)?(this.counter_start_value[0]=this.get_counter_value(0,t),this.counter_start_time[0]=t,_("pit interrupt. new value: "+this.counter_start_value[0],512),this.cpu.device_lower_irq(0),this.cpu.device_raise_irq(0),0===this.counter_mode[0]&&(this.counter_enabled[0]=0)):this.cpu.device_lower_irq(0),this.counter_enabled[0]&&(i=(this.counter_start_value[0]-Math.floor(1193.1816666*(t-this.counter_start_time[0])))/1193.1816666)),i},T.prototype.get_counter_value=function(t,e){if(!this.counter_enabled[t])return 0;var i=e-this.counter_start_time[t],s=Math.floor(1193.1816666*i);return e=this.counter_start_value[t]-s,_("diff="+i+" dticks="+s+" value="+e+" reload="+this.counter_reload[t],512),e>=(i=this.counter_reload[t])?(_("Warning: Counter"+t+" value "+e+" is larger than reload "+i,512),e%=i):0>e&&(e=e%i+i),e},T.prototype.did_rollover=function(t,e){return 0>(e-=this.counter_start_time[t])?(_("Warning: PIT timer difference is negative, resetting (timer "+t+")"),!0):this.counter_start_value[t]<Math.floor(1193.1816666*e)},T.prototype.counter_read=function(t){var e=this.counter_latch[t];return e?(this.counter_latch[t]--,2===e?255&this.counter_latch_value[t]:this.counter_latch_value[t]>>8):(e=this.counter_next_low[t],3===this.counter_mode[t]&&(this.counter_next_low[t]^=1),t=this.get_counter_value(t,O.microtick()),e?255&t:t>>8)},T.prototype.counter_write=function(t,e){this.counter_reload[t]=this.counter_next_low[t]?-256&this.counter_reload[t]|e:255&this.counter_reload[t]|e<<8,3===this.counter_read_mode[t]&&this.counter_next_low[t]||(this.counter_reload[t]||(this.counter_reload[t]=65535),this.counter_start_value[t]=this.counter_reload[t],this.counter_enabled[t]=!0,this.counter_start_time[t]=O.microtick(),_("counter"+t+" reload="+p(this.counter_reload[t])+" tick="+(this.counter_reload[t]||65536)/1193.1816666+"ms",512)),3===this.counter_read_mode[t]&&(this.counter_next_low[t]^=1)},T.prototype.port43_write=function(t){var e=t>>1&7,i=1&t,s=t>>6&3;t=t>>4&3,1===s&&_("Unimplemented timer1",512),3===s?_("Unimplemented read back",512):0===t?(this.counter_latch[s]=2,e=this.get_counter_value(s,O.microtick()),_("latch: "+e,512),this.counter_latch_value[s]=e?e-1:0):(6<=e&&(e&=-5),_("Control: mode="+e+" ctr="+s+" read_mode="+t+" bcd="+i,512),this.counter_next_low[s]=1===t?0:1,0===s&&this.cpu.device_lower_irq(0),0!==e&&3!==e&&2!==e&&_("Unimplemented counter mode: "+p(e),512),this.counter_mode[s]=e,this.counter_read_mode[s]=t,2===s&&this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]]))},T.prototype.dump=function(){let t=this.counter_reload[0];_("counter0 ticks every "+(t||65536)/1193.1816666+"ms (reload="+t+")")};function q(t,e){this.cpu=t,this.bus=e,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 l(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 l(1024),this.next_byte_is_aux=this.next_byte_is_ready=!1,this.bus.register("keyboard-code",(function(t){this.kbd_send_code(t)}),this),this.bus.register("mouse-click",(function(t){this.mouse_send_click(t[0],t[1],t[2])}),this),this.bus.register("mouse-delta",(function(t){this.mouse_send_delta(t[0],t[1])}),this),this.bus.register("mouse-wheel",(function(t){this.wheel_movement-=t[0],this.wheel_movement-=2*t[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,t.io.register_read(96,this,this.port60_read),t.io.register_read(100,this,this.port64_read),t.io.register_write(96,this,this.port60_write),t.io.register_write(100,this,this.port64_write)}q.prototype.get_state=function(){var t=[];return t[0]=this.enable_mouse_stream,t[1]=this.use_mouse,t[2]=this.have_mouse,t[3]=this.mouse_delta_x,t[4]=this.mouse_delta_y,t[5]=this.mouse_clicks,t[6]=this.have_keyboard,t[7]=this.enable_keyboard_stream,t[8]=this.next_is_mouse_command,t[9]=this.next_read_sample,t[10]=this.next_read_led,t[11]=this.next_handle_scan_code_set,t[12]=this.next_read_rate,t[13]=this.next_read_resolution,t[15]=this.last_port60_byte,t[16]=this.sample_rate,t[17]=this.resolution,t[18]=this.scaling2,t[20]=this.command_register,t[21]=this.read_output_register,t[22]=this.read_command_register,t[23]=this.controller_output_port,t[24]=this.read_controller_output_port,t[25]=this.mouse_id,t[26]=this.mouse_detect_state,t[27]=this.mouse_reset_workaround,t},q.prototype.set_state=function(t){this.enable_mouse_stream=t[0],this.use_mouse=t[1],this.have_mouse=t[2],this.mouse_delta_x=t[3],this.mouse_delta_y=t[4],this.mouse_clicks=t[5],this.have_keyboard=t[6],this.enable_keyboard_stream=t[7],this.next_is_mouse_command=t[8],this.next_read_sample=t[9],this.next_read_led=t[10],this.next_handle_scan_code_set=t[11],this.next_read_rate=t[12],this.next_read_resolution=t[13],this.last_port60_byte=t[15],this.sample_rate=t[16],this.resolution=t[17],this.scaling2=t[18],this.command_register=t[20],this.read_output_register=t[21],this.read_command_register=t[22],this.controller_output_port=t[23],this.read_controller_output_port=t[24],this.mouse_id=t[25]||0,this.mouse_detect_state=t[26]||0,this.mouse_reset_workaround=t[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)},q.prototype.raise_irq=function(){this.next_byte_is_ready||(this.kbd_buffer.length?this.kbd_irq():this.mouse_buffer.length&&this.mouse_irq())},q.prototype.mouse_irq=function(){this.next_byte_is_aux=this.next_byte_is_ready=!0,2&this.command_register&&(_("Mouse irq",64),this.cpu.device_lower_irq(12),this.cpu.device_raise_irq(12))},q.prototype.kbd_irq=function(){this.next_byte_is_ready=!0,this.next_byte_is_aux=!1,1&this.command_register&&(_("Keyboard irq",64),this.cpu.device_lower_irq(1),this.cpu.device_raise_irq(1))},q.prototype.kbd_send_code=function(t){this.enable_keyboard_stream&&(_("adding kbd code: "+p(t),64),this.kbd_buffer.push(t),this.raise_irq())},q.prototype.mouse_send_delta=function(t,e){if(this.have_mouse&&this.use_mouse){var i=this.resolution*this.sample_rate/80;this.mouse_delta_x+=t*i,this.mouse_delta_y+=e*i,this.enable_mouse_stream&&(t=0|this.mouse_delta_x,e=0|this.mouse_delta_y,t||e)&&(this.mouse_delta_x-=t,this.mouse_delta_y-=e,this.send_mouse_packet(t,e))}},q.prototype.mouse_send_click=function(t,e,i){this.have_mouse&&this.use_mouse&&(this.mouse_clicks=t|i<<1|e<<2,this.enable_mouse_stream&&this.send_mouse_packet(0,0))},q.prototype.send_mouse_packet=function(t,e){var i=(0>e)<<5|(0>t)<<4|8|this.mouse_clicks;this.last_mouse_packet=Date.now(),this.mouse_buffer.push(i),this.mouse_buffer.push(t),this.mouse_buffer.push(e),4===this.mouse_id?(this.mouse_buffer.push(0|15&this.wheel_movement),this.wheel_movement=0):3===this.mouse_id&&(this.mouse_buffer.push(255&this.wheel_movement),this.wheel_movement=0),this.raise_irq()},q.prototype.apply_scaling2=function(t){var e=t>>31;switch(Math.abs(t)){case 0:case 1:case 3:return t;case 2:return e;case 4:return 6*e;case 5:return 9*e;default:return t<<1}},q.prototype.port60_read=function(){return this.next_byte_is_ready=!1,this.kbd_buffer.length||this.mouse_buffer.length?(this.next_byte_is_aux?(this.cpu.device_lower_irq(12),this.last_port60_byte=this.mouse_buffer.shift(),_("Port 60 read (mouse): "+p(this.last_port60_byte),64)):(this.cpu.device_lower_irq(1),this.last_port60_byte=this.kbd_buffer.shift(),_("Port 60 read (kbd) : "+p(this.last_port60_byte),64)),(this.kbd_buffer.length||this.mouse_buffer.length)&&this.raise_irq(),this.last_port60_byte):(_("Port 60 read: Empty",64),this.last_port60_byte)},q.prototype.port64_read=function(){var t=16;return this.next_byte_is_ready&&(t|=1),this.next_byte_is_aux&&(t|=32),_("port 64 read: "+p(t),64),t},q.prototype.port60_write=function(t){if(_("port 60 write: "+p(t),64),this.read_command_register)this.command_register=t,this.read_command_register=!1,_("Keyboard command register = "+p(this.command_register),64);else if(this.read_output_register)this.read_output_register=!1,this.mouse_buffer.clear(),this.mouse_buffer.push(t),this.mouse_irq();else if(this.next_read_sample){switch(this.next_read_sample=!1,this.mouse_buffer.clear(),this.mouse_buffer.push(250),this.sample_rate=t,this.mouse_detect_state){case-1:60===t?(this.mouse_reset_workaround=!0,this.mouse_detect_state=0):(this.mouse_reset_workaround=!1,this.mouse_detect_state=200===t?1:0);break;case 0:200===t&&(this.mouse_detect_state=1);break;case 1:this.mouse_detect_state=100===t?2:200===t?3:0;break;case 2:80===t&&(this.mouse_id=3),this.mouse_detect_state=-1;break;case 3:80===t&&(this.mouse_id=4),this.mouse_detect_state=-1}_("mouse sample rate: "+p(t)+", mouse id: "+p(this.mouse_id),64),this.sample_rate||(_("invalid sample rate, reset to 100",64),this.sample_rate=100),this.mouse_irq()}else if(this.next_read_resolution)this.next_read_resolution=!1,this.mouse_buffer.clear(),this.mouse_buffer.push(250),3<t?(this.resolution=4,_("invalid resolution, resetting to 4",64)):(this.resolution=1<<t,_("resolution: "+this.resolution,64)),this.mouse_irq();else if(this.next_read_led)this.next_read_led=!1,this.kbd_buffer.push(250),this.kbd_irq();else if(this.next_handle_scan_code_set)this.next_handle_scan_code_set=!1,this.kbd_buffer.push(250),this.kbd_irq(),t||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,_("Port 60 data register write: "+p(t),64),this.have_mouse){switch(this.kbd_buffer.clear(),this.mouse_buffer.clear(),this.mouse_buffer.push(250),t){case 230:_("Scaling 1:1",64),this.scaling2=!1;break;case 231:_("Scaling 2:1",64),this.scaling2=!0;break;case 232:this.next_read_resolution=!0;break;case 233:this.send_mouse_packet(0,0);break;case 235:_("unimplemented request single packet",64),this.send_mouse_packet(0,0);break;case 242:_("required id: "+p(this.mouse_id),64),this.mouse_buffer.push(this.mouse_id),this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0,this.raise_irq();break;case 243:this.next_read_sample=!0;break;case 244:this.use_mouse=this.enable_mouse_stream=!0,this.bus.send("mouse-enable",!0),this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0;break;case 245:this.enable_mouse_stream=!1;break;case 246:this.enable_mouse_stream=!1,this.sample_rate=100,this.scaling2=!1,this.resolution=4;break;case 255:_("Mouse reset",64),this.mouse_buffer.push(170),this.mouse_buffer.push(0),this.use_mouse=!0,this.bus.send("mouse-enable",!0),this.enable_mouse_stream=!1,this.sample_rate=100,this.scaling2=!1,this.resolution=4,this.mouse_reset_workaround||(this.mouse_id=0),this.mouse_clicks=this.mouse_delta_x=this.mouse_delta_y=0;break;default:_("Unimplemented mouse command: "+p(t),64)}this.mouse_irq()}}else if(this.read_controller_output_port)this.read_controller_output_port=!1,this.controller_output_port=t;else{switch(_("Port 60 data register write: "+p(t),64),this.mouse_buffer.clear(),this.kbd_buffer.clear(),this.kbd_buffer.push(250),t){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:_("kbd enable scanning",64),this.enable_keyboard_stream=!0;break;case 245:_("kbd disable scanning",64),this.enable_keyboard_stream=!1;break;case 246:break;case 255:this.kbd_buffer.clear(),this.kbd_buffer.push(250),this.kbd_buffer.push(170),this.kbd_buffer.push(0);break;default:_("Unimplemented keyboard command: "+p(t),64)}this.kbd_irq()}},q.prototype.port64_write=function(t){switch(_("port 64 write: "+p(t),64),t){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:_("Disable second port",64),this.command_register|=32;break;case 168:_("Enable second port",64),this.command_register&=-33;break;case 169:case 171: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 173:_("Disable Keyboard",64),this.command_register|=16;break;case 174:_("Enable Keyboard",64),this.command_register&=-17;break;case 254:_("CPU reboot via PS2"),this.cpu.reboot_internal();break;default:_("port 64: Unimplemented command byte: "+p(t),64)}};var L="COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.",S=new Uint8Array(256),D=[],P=[],z=[],M=new Uint8Array(256),U=[];function G(t,e){this.cpu=t,this.bus=e,this.write_buffer=new l(64),this.read_buffer=new l(64),this.read_buffer_lastvalue=0,this.command=0,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 f(65536),new f(65536)],this.dma=t.devices.dma,this.dma_channel=this.dma_irq=this.dma_bytes_block=this.dma_bytes_left=this.dma_bytes_count=this.dma_sample_count=0,this.dma_channel_8bit=1,this.dma_channel_16bit=5,this.dma_autoinit=!1,this.dma_buffer=new ArrayBuffer(65536),this.dma_buffer_int8=new Int8Array(this.dma_buffer),this.dma_buffer_uint8=new Uint8Array(this.dma_buffer),this.dma_buffer_int16=new Int16Array(this.dma_buffer),this.dma_buffer_uint16=new Uint16Array(this.dma_buffer),this.dma_syncbuffer=new u(this.dma_buffer),this.dma_paused=this.dma_waiting_transfer=!1,this.sampling_rate=22050,e.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 l(64),this.fm_current_address1=this.fm_current_address0=this.mpu_read_buffer_lastvalue=0,this.fm_waveform_select_enable=!1,this.irq=5,this.irq_triggered=new Uint8Array(16),t.io.register_read_consecutive(544,this,this.port2x0_read,this.port2x1_read,this.port2x2_read,this.port2x3_read),t.io.register_read_consecutive(904,this,this.port2x0_read,this.port2x1_read),t.io.register_read_consecutive(548,this,this.port2x4_read,this.port2x5_read),t.io.register_read(550,this,this.port2x6_read),t.io.register_read(551,this,this.port2x7_read),t.io.register_read(552,this,this.port2x8_read),t.io.register_read(553,this,this.port2x9_read),t.io.register_read(554,this,this.port2xA_read),t.io.register_read(555,this,this.port2xB_read),t.io.register_read(556,this,this.port2xC_read),t.io.register_read(557,this,this.port2xD_read),t.io.register_read_consecutive(558,this,this.port2xE_read,this.port2xF_read),t.io.register_write_consecutive(544,this,this.port2x0_write,this.port2x1_write,this.port2x2_write,this.port2x3_write),t.io.register_write_consecutive(904,this,this.port2x0_write,this.port2x1_write),t.io.register_write_consecutive(548,this,this.port2x4_write,this.port2x5_write),t.io.register_write(550,this,this.port2x6_write),t.io.register_write(551,this,this.port2x7_write),t.io.register_write_consecutive(552,this,this.port2x8_write,this.port2x9_write),t.io.register_write(554,this,this.port2xA_write),t.io.register_write(555,this,this.port2xB_write),t.io.register_write(556,this,this.port2xC_write),t.io.register_write(557,this,this.port2xD_write),t.io.register_write(558,this,this.port2xE_write),t.io.register_write(559,this,this.port2xF_write),t.io.register_read_consecutive(816,this,this.port3x0_read,this.port3x1_read),t.io.register_write_consecutive(816,this,this.port3x0_write,this.port3x1_write),this.dma.on_unmask(this.dma_on_unmask,this),e.register("dac-request-data",(function(){this.dac_handle_request()}),this),e.register("speaker-has-initialized",(function(){this.mixer_reset()}),this),e.send("speaker-confirm-initialized"),this.dsp_reset()}function N(t,e,i){i||(i=G.prototype.dsp_default_handler);for(var s=0;s<t.length;s++)S[t[s]]=e,D[t[s]]=i}function F(t){for(var e=[],i=0;16>i;i++)e.push(t+i);return e}G.prototype.dsp_reset=function(){this.write_buffer.clear(),this.read_buffer.clear(),this.command=0,this.command_size=0,this.dummy_speaker_enabled=!1,this.test_register=0,this.dsp_signed=this.dsp_16bit=this.dsp_stereo=this.dsp_highspeed=!1,this.dac_buffers[0].clear(),this.dac_buffers[1].clear(),this.dma_channel=this.dma_irq=this.dma_bytes_block=this.dma_bytes_left=this.dma_bytes_count=this.dma_sample_count=0,this.dma_autoinit=!1,this.dma_buffer_uint8.fill(0),this.dma_paused=this.dma_waiting_transfer=!1,this.e2_value=170,this.e2_count=0,this.sampling_rate=22050,this.bytes_per_sample=1,this.lower_irq(1),this.irq_triggered.fill(0),this.asp_registers.fill(0),this.asp_registers[5]=1,this.asp_registers[9]=248},G.prototype.get_state=function(){var t=[];return t[2]=this.read_buffer_lastvalue,t[3]=this.command,t[4]=this.command_size,t[5]=this.mixer_current_address,t[6]=this.mixer_registers,t[7]=this.dummy_speaker_enabled,t[8]=this.test_register,t[9]=this.dsp_highspeed,t[10]=this.dsp_stereo,t[11]=this.dsp_16bit,t[12]=this.dsp_signed,t[15]=this.dma_sample_count,t[16]=this.dma_bytes_count,t[17]=this.dma_bytes_left,t[18]=this.dma_bytes_block,t[19]=this.dma_irq,t[20]=this.dma_channel,t[21]=this.dma_channel_8bit,t[22]=this.dma_channel_16bit,t[23]=this.dma_autoinit,t[24]=this.dma_buffer_uint8,t[25]=this.dma_waiting_transfer,t[26]=this.dma_paused,t[27]=this.sampling_rate,t[28]=this.bytes_per_sample,t[29]=this.e2_value,t[30]=this.e2_count,t[31]=this.asp_registers,t[33]=this.mpu_read_buffer_last_value,t[34]=this.irq,t[35]=this.irq_triggered,t},G.prototype.set_state=function(t){this.read_buffer_lastvalue=t[2],this.command=t[3],this.command_size=t[4],this.mixer_current_address=t[5],this.mixer_registers=t[6],this.mixer_full_update(),this.dummy_speaker_enabled=t[7],this.test_register=t[8],this.dsp_highspeed=t[9],this.dsp_stereo=t[10],this.dsp_16bit=t[11],this.dsp_signed=t[12],this.dma_sample_count=t[15],this.dma_bytes_count=t[16],this.dma_bytes_left=t[17],this.dma_bytes_block=t[18],this.dma_irq=t[19],this.dma_channel=t[20],this.dma_channel_8bit=t[21],this.dma_channel_16bit=t[22],this.dma_autoinit=t[23],this.dma_buffer_uint8=t[24],this.dma_waiting_transfer=t[25],this.dma_paused=t[26],this.sampling_rate=t[27],this.bytes_per_sample=t[28],this.e2_value=t[29],this.e2_count=t[30],this.asp_registers=t[31],this.mpu_read_buffer_last_value=t[33],this.irq=t[34],this.irq_triggered=t[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 u(this.dma_buffer),this.dma_paused?this.bus.send("dac-disable"):this.bus.send("dac-enable")},G.prototype.port2x0_read=function(){return _("220 read: fm music status port (unimplemented)",8388608),255},G.prototype.port2x1_read=function(){return _("221 read: fm music data port (write only)",8388608),255},G.prototype.port2x2_read=function(){return _("222 read: advanced fm music status port (unimplemented)",8388608),255},G.prototype.port2x3_read=function(){return _("223 read: advanced music data port (write only)",8388608),255},G.prototype.port2x4_read=function(){return _("224 read: mixer address port",8388608),this.mixer_current_address},G.prototype.port2x5_read=function(){return _("225 read: mixer data port",8388608),this.mixer_read(this.mixer_current_address)},G.prototype.port2x6_read=function(){return _("226 read: (write only)",8388608),255},G.prototype.port2x7_read=function(){return _("227 read: undocumented",8388608),255},G.prototype.port2x8_read=function(){return _("228 read: fm music status port (unimplemented)",8388608),255},G.prototype.port2x9_read=function(){return _("229 read: fm music data port (write only)",8388608),255},G.prototype.port2xA_read=function(){return _("22A read: read data",8388608),this.read_buffer.length&&(this.read_buffer_lastvalue=this.read_buffer.shift()),_(" <- "+this.read_buffer_lastvalue+" "+p(this.read_buffer_lastvalue)+" '"+String.fromCharCode(this.read_buffer_lastvalue)+"'",8388608),this.read_buffer_lastvalue},G.prototype.port2xB_read=function(){return _("22B read: undocumented",8388608),255},G.prototype.port2xC_read=function(){return _("22C read: write-buffer status",8388608),127},G.prototype.port2xD_read=function(){return _("22D read: undocumented",8388608),255},G.prototype.port2xE_read=function(){return _("22E read: read-buffer status / irq 8bit ack.",8388608),this.irq_triggered[1]&&this.lower_irq(1),(this.read_buffer.length&&!this.dsp_highspeed)<<7|127},G.prototype.port2xF_read=function(){return _("22F read: irq 16bit ack",8388608),this.lower_irq(2),0},G.prototype.port2x0_write=function(t){_("220 write: (unimplemented) fm register 0 address = "+p(t),8388608),this.fm_current_address0=0},G.prototype.port2x1_write=function(t){_("221 write: (unimplemented) fm register 0 data = "+p(t),8388608);var e=U[this.fm_current_address0];e||(e=this.fm_default_write),e.call(this,t,0,this.fm_current_address0)},G.prototype.port2x2_write=function(t){_("222 write: (unimplemented) fm register 1 address = "+p(t),8388608),this.fm_current_address1=0},G.prototype.port2x3_write=function(t){_("223 write: (unimplemented) fm register 1 data ="+p(t),8388608);var e=U[this.fm_current_address1];e||(e=this.fm_default_write),e.call(this,t,1,this.fm_current_address1)},G.prototype.port2x4_write=function(t){_("224 write: mixer address = "+p(t),8388608),this.mixer_current_address=t},G.prototype.port2x5_write=function(t){_("225 write: mixer data = "+p(t),8388608),this.mixer_write(this.mixer_current_address,t)},G.prototype.port2x6_write=function(t){_("226 write: reset = "+p(t),8388608),this.dsp_highspeed?(_(" -> exit highspeed",8388608),this.dsp_highspeed=!1):t&&(_(" -> reset",8388608),this.dsp_reset()),this.read_buffer.clear(),this.read_buffer.push(170)},G.prototype.port2x7_write=function(t){_("227 write: undocumented",8388608)},G.prototype.port2x8_write=function(t){_("228 write: fm music register port (unimplemented)",8388608)},G.prototype.port2x9_write=function(t){_("229 write: fm music data port (unimplemented)",8388608)},G.prototype.port2xA_write=function(t){_("22A write: dsp read data port (read only)",8388608)},G.prototype.port2xB_write=function(t){_("22B write: undocumented",8388608)},G.prototype.port2xC_write=function(t){_("22C write: write command/data",8388608),0===this.command?(_("22C write: command = "+p(t),8388608),this.command=t,this.write_buffer.clear(),this.command_size=S[t]):(_("22C write: data: "+p(t),8388608),this.write_buffer.push(t)),this.write_buffer.length>=this.command_size&&this.command_do()},G.prototype.port2xD_write=function(t){_("22D write: undocumented",8388608)},G.prototype.port2xE_write=function(t){_("22E write: dsp read buffer status (read only)",8388608)},G.prototype.port2xF_write=function(t){_("22F write: undocumented",8388608)},G.prototype.port3x0_read=function(){return _("330 read: mpu data",8388608),this.mpu_read_buffer.length&&(this.mpu_read_buffer_lastvalue=this.mpu_read_buffer.shift()),_(" <- "+p(this.mpu_read_buffer_lastvalue),8388608),this.mpu_read_buffer_lastvalue},G.prototype.port3x0_write=function(t){_("330 write: mpu data (unimplemented) : "+p(t),8388608)},G.prototype.port3x1_read=function(){return _("331 read: mpu status",8388608),0|128*!this.mpu_read_buffer.length},G.prototype.port3x1_write=function(t){_("331 write: mpu command: "+p(t),8388608),255==t&&(this.mpu_read_buffer.clear(),this.mpu_read_buffer.push(254))},G.prototype.command_do=function(){var t=D[this.command];t||(t=this.dsp_default_handler),t.call(this),this.command=0,this.command_size=0,this.write_buffer.clear()},G.prototype.dsp_default_handler=function(){_("Unhandled command: "+p(this.command),8388608)},N([14],2,(function(){this.asp_registers[this.write_buffer.shift()]=this.write_buffer.shift()})),N([15],1,(function(){this.read_buffer.clear(),this.read_buffer.push(this.asp_registers[this.write_buffer.shift()])})),N([16],1,(function(){var t=Z(this.write_buffer.shift(),127.5,-1);this.dac_buffers[0].push(t),this.dac_buffers[1].push(t),this.bus.send("dac-enable")})),N([20,21],2,(function(){this.dma_irq=1,this.dma_channel=this.dma_channel_8bit,this.dsp_highspeed=this.dsp_16bit=this.dsp_signed=this.dma_autoinit=!1,this.dma_transfer_size_set(),this.dma_transfer_start()})),N([22],2),N([23],2),N([28],0,(function(){this.dma_irq=1,this.dma_channel=this.dma_channel_8bit,this.dma_autoinit=!0,this.dsp_highspeed=this.dsp_16bit=this.dsp_signed=!1,this.dma_transfer_start()})),N([31],0),N([32],0,(function(){this.read_buffer.clear(),this.read_buffer.push(127)})),N([36],2),N([44],0),N([48],0),N([49],0),N([52],0),N([53],0),N([54],0),N([55],0),N([56],0),N([64],1,(function(){this.sampling_rate_change(1e6/(256-this.write_buffer.shift())/this.get_channel_count())})),N([65,66],2,(function(){this.sampling_rate_change(this.write_buffer.shift()<<8|this.write_buffer.shift())})),N([72],2,(function(){this.dma_transfer_size_set()})),N([116],2),N([117],2),N([118],2),N([119],2),N([125],0),N([127],0),N([128],2),N([144],0,(function(){this.dma_irq=1,this.dma_channel=this.dma_channel_8bit,this.dma_autoinit=!0,this.dsp_signed=!1,this.dsp_highspeed=!0,this.dsp_16bit=!1,this.dma_transfer_start()})),N([145],0),N([152],0),N([153],0),N([160],0),N([168],0),N(F(176),3,(function(){if(8&this.command)this.dsp_default_handler();else{var t=this.write_buffer.shift();this.dma_irq=2,this.dma_channel=this.dma_channel_16bit,this.dma_autoinit=!!(4&this.command),this.dsp_signed=!!(16&t),this.dsp_stereo=!!(32&t),this.dsp_16bit=!0,this.dma_transfer_size_set(),this.dma_transfer_start()}})),N(F(192),3,(function(){if(8&this.command)this.dsp_default_handler();else{var t=this.write_buffer.shift();this.dma_irq=1,this.dma_channel=this.dma_channel_8bit,this.dma_autoinit=!!(4&this.command),this.dsp_signed=!!(16&t),this.dsp_stereo=!!(32&t),this.dsp_16bit=!1,this.dma_transfer_size_set(),this.dma_transfer_start()}})),N([208],0,(function(){this.dma_paused=!0,this.bus.send("dac-disable")})),N([209],0,(function(){this.dummy_speaker_enabled=!0})),N([211],0,(function(){this.dummy_speaker_enabled=!1})),N([212],0,(function(){this.dma_paused=!1,this.bus.send("dac-enable")})),N([213],0,(function(){this.dma_paused=!0,this.bus.send("dac-disable")})),N([214],0,(function(){this.dma_paused=!1,this.bus.send("dac-enable")})),N([216],0,(function(){this.read_buffer.clear(),this.read_buffer.push(255*this.dummy_speaker_enabled)})),N([217,218],0,(function(){this.dma_autoinit=!1})),N([224],1,(function(){this.read_buffer.clear(),this.read_buffer.push(~this.write_buffer.shift())})),N([225],0,(function(){this.read_buffer.clear(),this.read_buffer.push(4),this.read_buffer.push(5)})),N([226],1),N([227],0,(function(){this.read_buffer.clear();for(var t=0;t<L.length;t++)this.read_buffer.push(L.charCodeAt(t));this.read_buffer.push(0)})),N([228],1,(function(){this.test_register=this.write_buffer.shift()})),N([232],0,(function(){this.read_buffer.clear(),this.read_buffer.push(this.test_register)})),N([242,243],0,(function(){this.raise_irq()}));var B=new Uint8Array(256);function V(t,e){e||(e=G.prototype.mixer_default_read),P[t]=e}function W(t,e){e||(e=G.prototype.mixer_default_write),z[t]=e}function H(t,e,i){M[t]=1,P[t]=function(){return 240&this.mixer_registers[e]|this.mixer_registers[i]>>>4},z[t]=function(s){this.mixer_registers[t]=s;var r=s<<4&240|15&this.mixer_registers[i];this.mixer_write(e,240&s|15&this.mixer_registers[e]),this.mixer_write(i,r)}}function X(t,e,i){P[t]=G.prototype.mixer_default_read,z[t]=function(s){this.mixer_registers[t]=s,this.bus.send("mixer-volume",[e,i,(s>>>2)-62])}}function j(t,e){e||(e=G.prototype.fm_default_write);for(var i=0;i<t.length;i++)U[t[i]]=e}function K(t,e){for(var i=[];t<=e;t++)i.push(t);return i}B[14]=255,B[15]=7,B[55]=56,N([249],1,(function(){var t=this.write_buffer.shift();_("dsp 0xf9: unknown function. input: "+t,8388608),this.read_buffer.clear(),this.read_buffer.push(B[t])})),G.prototype.mixer_read=function(t){var e=P[t];return e?e=e.call(this):(e=this.mixer_registers[t],_("unhandled mixer register read. addr:"+p(t)+" data:"+p(e),8388608)),e},G.prototype.mixer_write=function(t,e){var i=z[t];i?i.call(this,e):_("unhandled mixer register write. addr:"+p(t)+" data:"+p(e),8388608)},G.prototype.mixer_default_read=function(){return _("mixer register read. addr:"+p(this.mixer_current_address),8388608),this.mixer_registers[this.mixer_current_address]},G.prototype.mixer_default_write=function(t){_("mixer register write. addr:"+p(this.mixer_current_address)+" data:"+p(t),8388608),this.mixer_registers[this.mixer_current_address]=t},G.prototype.mixer_reset=function(){this.mixer_registers[4]=204,this.mixer_registers[34]=204,this.mixer_registers[38]=204,this.mixer_registers[40]=0,this.mixer_registers[46]=0,this.mixer_registers[10]=0,this.mixer_registers[48]=192,this.mixer_registers[49]=192,this.mixer_registers[50]=192,this.mixer_registers[51]=192,this.mixer_registers[52]=192,this.mixer_registers[53]=192,this.mixer_registers[54]=0,this.mixer_registers[55]=0,this.mixer_registers[56]=0,this.mixer_registers[57]=0,this.mixer_registers[59]=0,this.mixer_registers[60]=31,this.mixer_registers[61]=21,this.mixer_registers[62]=11,this.mixer_registers[63]=0,this.mixer_registers[64]=0,this.mixer_registers[65]=0,this.mixer_registers[66]=0,this.mixer_registers[67]=0,this.mixer_registers[68]=128,this.mixer_registers[69]=128,this.mixer_registers[70]=128,this.mixer_registers[71]=128,this.mixer_full_update()},G.prototype.mixer_full_update=function(){for(var t=1;t<this.mixer_registers.length;t++)M[t]||this.mixer_write(t,this.mixer_registers[t])},V(0,(function(){return this.mixer_reset(),0})),W(0),H(4,50,51),H(34,48,49),H(38,52,53),H(40,54,55),H(46,56,57),X(48,0,0),X(49,0,1),X(50,2,0),X(51,2,1),V(59),W(59,(function(t){this.mixer_registers[59]=t,this.bus.send("mixer-volume",[1,2,6*(t>>>6)-18])})),V(65),W(65,(function(t){this.mixer_registers[65]=t,this.bus.send("mixer-gain-left",6*(t>>>6))})),V(66),W(66,(function(t){this.mixer_registers[66]=t,this.bus.send("mixer-gain-right",6*(t>>>6))})),V(68),W(68,(function(t){this.mixer_registers[68]=t,t>>>=3,this.bus.send("mixer-treble-left",t-(16>t?14:16))})),V(69),W(69,(function(t){this.mixer_registers[69]=t,t>>>=3,this.bus.send("mixer-treble-right",t-(16>t?14:16))})),V(70),W(70,(function(t){this.mixer_registers[70]=t,t>>>=3,this.bus.send("mixer-bass-right",t-(16>t?14:16))})),V(71),W(71,(function(t){this.mixer_registers[71]=t,t>>>=3,this.bus.send("mixer-bass-right",t-(16>t?14:16))})),V(128,(function(){switch(this.irq){case 2:return 1;case 5:return 2;case 7:return 4;case 10:return 8;default:return 0}})),W(128,(function(t){1&t&&(this.irq=2),2&t&&(this.irq=5),4&t&&(this.irq=7),8&t&&(this.irq=10)})),V(129,(function(){var t=0;switch(this.dma_channel_8bit){case 0:t|=1;break;case 1:t|=2;break;case 3:t|=8}switch(this.dma_channel_16bit){case 5:t|=32;break;case 6:t|=64;break;case 7:t|=128}return t})),W(129,(function(t){1&t&&(this.dma_channel_8bit=0),2&t&&(this.dma_channel_8bit=1),8&t&&(this.dma_channel_8bit=3),32&t&&(this.dma_channel_16bit=5),64&t&&(this.dma_channel_16bit=6),128&t&&(this.dma_channel_16bit=7)})),V(130,(function(){for(var t=32,e=0;16>e;e++)t|=e*this.irq_triggered[e];return t})),G.prototype.fm_default_write=function(t,e,i){_("unhandled fm register write. addr:"+e+"|"+p(i)+" data:"+p(t),8388608)};var Y=new Uint8Array(32);function Q(t,e){return 18*t+Y[e]}function Z(t,e,i){return function(t,e,i){return(t<e)*e+(t>i)*i+(e<=t&&t<=i)*t}(t/e+i,-1,1)}Y[0]=0,Y[1]=1,Y[2]=2,Y[3]=3,Y[4]=4,Y[5]=5,Y[8]=6,Y[9]=7,Y[10]=8,Y[11]=9,Y[12]=10,Y[13]=11,Y[16]=12,Y[17]=13,Y[18]=14,Y[19]=15,Y[20]=16,Y[21]=17,j([1],(function(t,e,i){this.fm_waveform_select_enable[e]=1&t,this.fm_update_waveforms()})),j([2]),j([3]),j([4],(function(t,e,i){})),j([5],(function(t,e,i){0===e&&this.fm_default_write(t,e,i)})),j([8],(function(t,e,i){})),j(K(32,53),(function(t,e,i){Q(e,i-32)})),j(K(64,85),(function(t,e,i){Q(e,i-64)})),j(K(96,117),(function(t,e,i){Q(e,i-96)})),j(K(128,149),(function(t,e,i){Q(e,i-128)})),j(K(160,168),(function(t,e,i){})),j(K(176,184),(function(t,e,i){})),j([189],(function(t,e,i){})),j(K(192,200),(function(t,e,i){})),j(K(224,245),(function(t,e,i){Q(e,i-224)})),G.prototype.fm_update_waveforms=function(){},G.prototype.sampling_rate_change=function(t){this.sampling_rate=t,this.bus.send("dac-tell-sampling-rate",t)},G.prototype.get_channel_count=function(){return this.dsp_stereo?2:1},G.prototype.dma_transfer_size_set=function(){this.dma_sample_count=1+(this.write_buffer.shift()<<0)+(this.write_buffer.shift()<<8)},G.prototype.dma_transfer_start=function(){_("begin dma transfer",8388608),this.bytes_per_sample=1,this.dsp_16bit&&(this.bytes_per_sample*=2),this.dma_bytes_count=this.dma_sample_count*this.bytes_per_sample,this.dma_bytes_block=1024*this.bytes_per_sample,this.dma_bytes_block=Math.min(Math.max(this.dma_bytes_count>>2&-4,32),this.dma_bytes_block),this.dma_waiting_transfer=!0,this.dma.channel_mask[this.dma_channel]||this.dma_on_unmask(this.dma_channel)},G.prototype.dma_on_unmask=function(t){t===this.dma_channel&&this.dma_waiting_transfer&&(this.dma_waiting_transfer=!1,this.dma_bytes_left=this.dma_bytes_count,this.dma_paused=!1,this.bus.send("dac-enable"))},G.prototype.dma_transfer_next=function(){_("dma transfering next block",8388608);var t=Math.min(this.dma_bytes_left,this.dma_bytes_block),e=Math.floor(t/this.bytes_per_sample);this.dma.do_write(this.dma_syncbuffer,0,t,this.dma_channel,(i=>{_("dma block transfer "+(i?"unsuccessful":"successful"),8388608),i||(this.dma_to_dac(e),this.dma_bytes_left-=t,this.dma_bytes_left||(this.raise_irq(this.dma_irq),this.dma_autoinit&&(this.dma_bytes_left=this.dma_bytes_count)))}))},G.prototype.dma_to_dac=function(t){for(var e=this.dsp_16bit?32767.5:127.5,i=this.dsp_signed?0:-1,s=this.dsp_stereo?1:2,r=this.dsp_16bit?this.dsp_signed?this.dma_buffer_int16:this.dma_buffer_uint16:this.dsp_signed?this.dma_buffer_int8:this.dma_buffer_uint8,a=0,o=0;o<t;o++)for(var _=Z(r[o],e,i),n=0;n<s;n++)this.dac_buffers[a].push(_),a^=1;this.dac_send()},G.prototype.dac_handle_request=function(){!this.dma_bytes_left||this.dma_paused?this.dac_send():this.dma_transfer_next()},G.prototype.dac_send=function(){if(this.dac_buffers[0].length){var t=this.dac_buffers[0].shift_block(this.dac_buffers[0].length),e=this.dac_buffers[1].shift_block(this.dac_buffers[1].length);this.bus.send("dac-send-data",[t,e],[t.buffer,e.buffer])}},G.prototype.raise_irq=function(t){_("raise irq",8388608),this.irq_triggered[t]=1,this.cpu.device_raise_irq(this.irq)},G.prototype.lower_irq=function(t){_("lower irq",8388608),this.irq_triggered[t]=0,this.cpu.device_lower_irq(this.irq)};function J(t,e,i){switch(this.bus=i,this.cpu=t,this.ints=4,this.line_control=this.baud_rate=0,this.lsr=96,this.ier=this.fifo_control=0,this.iir=1,this.irq=this.scratch_register=this.modem_status=this.modem_control=0,this.input=new l(4096),this.current_line=[],e){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:_("Invalid serial port: "+p(e),16384),this.com=0,this.irq=4}this.bus.register("serial"+this.com+"-input",(function(t){this.data_received(t)}),this),(t=t.io).register_write(e,this,(function(t){this.write_data(t)}),(function(t){this.write_data(255&t),this.write_data(t>>8)})),t.register_write(1|e,this,(function(t){128&this.line_control?(this.baud_rate=255&this.baud_rate|t<<8,_("baud rate: "+p(this.baud_rate),16384)):(this.ier=15&t,_("interrupt enable: "+p(t),16384),this.CheckInterrupt())})),t.register_read(e,this,(function(){if(128&this.line_control)return 255&this.baud_rate;var t=this.input.shift();return _(-1===t?"Read input empty":"Read input: "+p(t),16384),0===this.input.length&&(this.lsr&=-2,this.ClearInterrupt(12)),t})),t.register_read(1|e,this,(function(){return 128&this.line_control?this.baud_rate>>8:15&this.ier})),t.register_read(2|e,this,(function(){var t=15&this.iir|192;return _("read interrupt identification: "+p(this.iir),16384),t})),t.register_write(2|e,this,(function(t){_("fifo control: "+p(t),16384),this.fifo_control=t})),t.register_read(3|e,this,(function(){return _("read line control: "+p(this.line_control),16384),this.line_control})),t.register_write(3|e,this,(function(t){_("line control: "+p(t),16384),this.line_control=t})),t.register_read(4|e,this,(function(){return this.modem_control})),t.register_write(4|e,this,(function(t){_("modem control: "+p(t),16384),this.modem_control=t})),t.register_read(5|e,this,(function(){return _("read line status: "+p(this.lsr),16384),this.lsr})),t.register_write(5|e,this,(function(t){_("Factory test write",16384)})),t.register_read(6|e,this,(function(){return _("read modem status: "+p(this.modem_status),16384),this.modem_status})),t.register_write(6|e,this,(function(t){_("Unkown register write (base+6)",16384)})),t.register_read(7|e,this,(function(){return this.scratch_register})),t.register_write(7|e,this,(function(t){this.scratch_register=t}))}J.prototype.get_state=function(){var t=[];return t[0]=this.ints,t[1]=this.baud_rate,t[2]=this.line_control,t[3]=this.lsr,t[4]=this.fifo_control,t[5]=this.ier,t[6]=this.iir,t[7]=this.modem_control,t[8]=this.modem_status,t[9]=this.scratch_register,t[10]=this.irq,t},J.prototype.set_state=function(t){this.ints=t[0],this.baud_rate=t[1],this.line_control=t[2],this.lsr=t[3],this.fifo_control=t[4],this.ier=t[5],this.iir=t[6],this.modem_control=t[7],this.modem_status=t[8],this.scratch_register=t[9],this.irq=t[10]},J.prototype.CheckInterrupt=function(){4096&this.ints&&1&this.ier?(this.iir=12,this.cpu.device_raise_irq(this.irq)):4&this.ints&&2&this.ier?(this.iir=2,this.cpu.device_raise_irq(this.irq)):1&this.ints&&8&this.ier?(this.iir=0,this.cpu.device_raise_irq(this.irq)):(this.iir=1,this.cpu.device_lower_irq(this.irq))},J.prototype.ThrowInterrupt=function(t){this.ints|=1<<t,this.CheckInterrupt()},J.prototype.ClearInterrupt=function(t){this.ints&=~(1<<t),this.CheckInterrupt()},J.prototype.data_received=function(t){_("input: "+p(t),16384),this.input.push(t),this.lsr|=1,this.ThrowInterrupt(12)},J.prototype.write_data=function(t){if(128&this.line_control)this.baud_rate=-256&this.baud_rate|t;else if(_("data: "+p(t),16384),this.ThrowInterrupt(2),255!==t){var e=String.fromCharCode(t);this.bus.send("serial"+this.com+"-output-char",e),this.current_line.push(t),"\n"===e&&(t=String.fromCharCode.apply("",this.current_line).trimRight().replace(/[\x00-\x08\x0b-\x1f\x7f\x80-\xff]/g,""),_("SERIAL: "+t),this.bus.send("serial"+this.com+"-output-line",String.fromCharCode.apply("",this.current_line)),this.current_line=[])}};var $=Uint32Array.from([655360,655360,720896,753664]),tt=Uint32Array.from([131072,65536,32768,32768]);function et(t,e,i){this.cpu=t,this.bus=e,this.vga_memory_size=i,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((()=>{e.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,14680064,57344,224,0,0,0,0,0,0,191,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,0,17,0,0,190,254,0,0,0,0,0,0,0,0,0,0,0,0],this.pci_id=144,this.pci_bars=[{size:i}],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,(i=t.io).register_write(960,this,this.port3C0_write),i.register_read(960,this,this.port3C0_read,this.port3C0_read16),i.register_read(961,this,this.port3C1_read),i.register_write(962,this,this.port3C2_write),i.register_write_consecutive(964,this,this.port3C4_write,this.port3C5_write),i.register_read(964,this,this.port3C4_read),i.register_read(965,this,this.port3C5_read),i.register_write_consecutive(974,this,this.port3CE_write,this.port3CF_write),i.register_read(974,this,this.port3CE_read),i.register_read(975,this,this.port3CF_read),i.register_read(966,this,this.port3C6_read),i.register_write(966,this,this.port3C6_write),i.register_write(967,this,this.port3C7_write),i.register_read(967,this,this.port3C7_read),i.register_write(968,this,this.port3C8_write),i.register_read(968,this,this.port3C8_read),i.register_write(969,this,this.port3C9_write),i.register_read(969,this,this.port3C9_read),i.register_read(972,this,this.port3CC_read),i.register_write_consecutive(980,this,this.port3D4_write,this.port3D5_write),i.register_read(980,this,this.port3D4_read),i.register_read(981,this,this.port3D5_read,(()=>(_("Warning: 16-bit read from 3D5",256),this.port3D5_read()))),i.register_read(970,this,(function(){return _("3CA read",256),0})),i.register_read(986,this,this.port3DA_read),i.register_read(954,this,this.port3DA_read),this.dispi_index=-1,this.dispi_enable_value=0,i.register_write(462,this,void 0,this.port1CE_write),i.register_write(463,this,void 0,this.port1CF_write),i.register_read(463,this,void 0,this.port1CF_read),void 0===this.vga_memory_size||this.vga_memory_size<262144?(this.vga_memory_size=262144,_("vga memory size rounded up to "+this.vga_memory_size,256)):65535&this.vga_memory_size&&(this.vga_memory_size|=65535,this.vga_memory_size++);let s=t.svga_allocate_memory(this.vga_memory_size);this.svga_memory=c.view(Uint8Array,t.wasm_memory,s,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,e.register("screen-fill-buffer",(function(){this.screen_fill_buffer()}),this),this.vga_memory=new Uint8Array(262144),this.plane0=new Uint8Array(this.vga_memory.buffer,0,65536),this.plane1=new Uint8Array(this.vga_memory.buffer,65536,65536),this.plane2=new Uint8Array(this.vga_memory.buffer,131072,65536),this.plane3=new Uint8Array(this.vga_memory.buffer,196608,65536),this.pixel_buffer=new Uint8Array(524288);var r=this;i.mmap_register(655360,131072,(function(t){return r.vga_memory_read(t)}),(function(t,e){r.vga_memory_write(t,e)})),t.devices.pci.register_device(this)}et.prototype.get_state=function(){var t=[];return t[0]=this.vga_memory_size,t[1]=this.cursor_address,t[2]=this.cursor_scanline_start,t[3]=this.cursor_scanline_end,t[4]=this.max_cols,t[5]=this.max_rows,t[6]=this.vga_memory,t[7]=this.dac_state,t[8]=this.start_address,t[9]=this.graphical_mode,t[10]=this.vga256_palette,t[11]=this.latch_dword,t[12]=this.color_compare,t[13]=this.color_dont_care,t[14]=this.miscellaneous_graphics_register,t[15]=this.svga_width,t[16]=this.svga_height,t[17]=this.crtc_mode,t[18]=this.svga_enabled,t[19]=this.svga_bpp,t[20]=this.svga_bank_offset,t[21]=this.svga_offset,t[22]=this.index_crtc,t[23]=this.dac_color_index_write,t[24]=this.dac_color_index_read,t[25]=this.dac_map,t[26]=this.sequencer_index,t[27]=this.plane_write_bm,t[28]=this.sequencer_memory_mode,t[29]=this.graphics_index,t[30]=this.plane_read,t[31]=this.planar_mode,t[32]=this.planar_rotate_reg,t[33]=this.planar_bitmap,t[34]=this.max_scan_line,t[35]=this.miscellaneous_output_register,t[36]=this.port_3DA_value,t[37]=this.dispi_index,t[38]=this.dispi_enable_value,t[39]=this.svga_memory,t[40]=this.graphical_mode_is_linear,t[41]=this.attribute_controller_index,t[42]=this.offset_register,t[43]=this.planar_setreset,t[44]=this.planar_setreset_enable,t[45]=this.start_address_latched,t[46]=this.crtc,t[47]=this.horizontal_display_enable_end,t[48]=this.horizontal_blank_start,t[49]=this.vertical_display_enable_end,t[50]=this.vertical_blank_start,t[51]=this.underline_location_register,t[52]=this.preset_row_scan,t[53]=this.offset_register,t[54]=this.palette_source,t[55]=this.attribute_mode,t[56]=this.color_plane_enable,t[57]=this.horizontal_panning,t[58]=this.color_select,t[59]=this.clocking_mode,t[60]=this.line_compare,t[61]=this.pixel_buffer,t[62]=this.dac_mask,t},et.prototype.set_state=function(t){this.vga_memory_size=t[0],this.cursor_address=t[1],this.cursor_scanline_start=t[2],this.cursor_scanline_end=t[3],this.max_cols=t[4],this.max_rows=t[5],t[6]&&this.vga_memory.set(t[6]),this.dac_state=t[7],this.start_address=t[8],this.graphical_mode=t[9],this.vga256_palette=t[10],this.latch_dword=t[11],this.color_compare=t[12],this.color_dont_care=t[13],this.miscellaneous_graphics_register=t[14],this.svga_width=t[15],this.svga_height=t[16],this.crtc_mode=t[17],this.svga_enabled=t[18],this.svga_bpp=t[19],this.svga_bank_offset=t[20],this.svga_offset=t[21],this.index_crtc=t[22],this.dac_color_index_write=t[23],this.dac_color_index_read=t[24],this.dac_map=t[25],this.sequencer_index=t[26],this.plane_write_bm=t[27],this.sequencer_memory_mode=t[28],this.graphics_index=t[29],this.plane_read=t[30],this.planar_mode=t[31],this.planar_rotate_reg=t[32],this.planar_bitmap=t[33],this.max_scan_line=t[34],this.miscellaneous_output_register=t[35],this.port_3DA_value=t[36],this.dispi_index=t[37],this.dispi_enable_value=t[38],this.svga_memory.set(t[39]),this.graphical_mode_is_linear=t[40],this.attribute_controller_index=t[41],this.offset_register=t[42],this.planar_setreset=t[43],this.planar_setreset_enable=t[44],this.start_address_latched=t[45],this.crtc.set(t[46]),this.horizontal_display_enable_end=t[47],this.horizontal_blank_start=t[48],this.vertical_display_enable_end=t[49],this.vertical_blank_start=t[50],this.underline_location_register=t[51],this.preset_row_scan=t[52],this.offset_register=t[53],this.palette_source=t[54],this.attribute_mode=t[55],this.color_plane_enable=t[56],this.horizontal_panning=t[57],this.color_select=t[58],this.clocking_mode=t[59],this.line_compare=t[60],t[61]&&this.pixel_buffer.set(t[61]),this.dac_mask=void 0===t[62]?255:t[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()},et.prototype.vga_memory_read=function(t){if(this.svga_enabled&&this.graphical_mode_is_linear)return this.cpu.read8(3758096384+(t-655360|this.svga_bank_offset)|0);var e=this.miscellaneous_graphics_register>>2&3;return 0>(t-=$[e])||t>=tt[e]?(_("vga read outside memory space: addr:"+p(t),256),0):(this.latch_dword=this.plane0[t],this.latch_dword|=this.plane1[t]<<8,this.latch_dword|=this.plane2[t]<<16,this.latch_dword|=this.plane3[t]<<24,8&this.planar_mode?(e=255,1&this.color_dont_care&&(e&=this.plane0[t]^~(1&this.color_compare?255:0)),2&this.color_dont_care&&(e&=this.plane1[t]^~(2&this.color_compare?255:0)),4&this.color_dont_care&&(e&=this.plane2[t]^~(4&this.color_compare?255:0)),8&this.color_dont_care&&(e&=this.plane3[t]^~(8&this.color_compare?255:0)),e):(e=this.plane_read,this.graphical_mode?8&this.sequencer_memory_mode?(e=3&t,t&=-4):16&this.planar_mode&&(e=1&t,t&=-2):e=0,this.vga_memory[e<<16|t]))},et.prototype.vga_memory_write=function(t,e){if(this.svga_enabled&&this.graphical_mode&&this.graphical_mode_is_linear)this.cpu.write8(3758096384+(t-655360|this.svga_bank_offset)|0,e);else{var i=this.miscellaneous_graphics_register>>2&3;0>(t-=$[i])||t>=tt[i]?_("vga write outside memory space: addr:"+p(t)+", value:"+p(e),256):this.graphical_mode?this.vga_memory_write_graphical(t,e):3&this.plane_write_bm&&this.vga_memory_write_text_mode(t,e)}},et.prototype.vga_memory_write_graphical=function(t,e){var i=3&this.planar_mode,s=this.apply_feed(this.planar_bitmap),r=this.apply_expand(this.planar_setreset),a=this.apply_expand(this.planar_setreset_enable);switch(i){case 0:e=this.apply_rotate(e);var o=this.apply_feed(e);o=this.apply_setreset(o,a),o=this.apply_logical(o,this.latch_dword),o=this.apply_bitmask(o,s);break;case 1:o=this.latch_dword;break;case 2:o=this.apply_expand(e),o=this.apply_logical(o,this.latch_dword),o=this.apply_bitmask(o,s);break;case 3:e=this.apply_rotate(e),s&=this.apply_feed(e),o=this.apply_bitmask(r,s)}switch(e=15,12&this.sequencer_memory_mode){case 0:e=5<<(1&t),t&=-2;break;case 8:case 12:e=1<<(3&t),t&=-4}1&(e&=this.plane_write_bm)&&(this.plane0[t]=o>>0&255),2&e&&(this.plane1[t]=o>>8&255),4&e&&(this.plane2[t]=o>>16&255),8&e&&(this.plane3[t]=o>>24&255),t=this.vga_addr_to_pixel(t),this.partial_replot(t,t+7)},et.prototype.apply_feed=function(t){return t|t<<8|t<<16|t<<24},et.prototype.apply_expand=function(t){return(1&t?255:0)|(2&t?255:0)<<8|(4&t?255:0)<<16|(8&t?255:0)<<24},et.prototype.apply_rotate=function(t){return(t|t<<8)>>>(7&this.planar_rotate_reg)&255},et.prototype.apply_setreset=function(t,e){var i=this.apply_expand(this.planar_setreset);return(t|e&i)&(~e|i)},et.prototype.apply_logical=function(t,e){switch(24&this.planar_rotate_reg){case 8:return t&e;case 16:return t|e;case 24:return t^e}return t},et.prototype.apply_bitmask=function(t,e){return e&t|~e&this.latch_dword},et.prototype.text_mode_redraw=function(){for(var t,e,i=this.start_address<<1,s=0;s<this.max_rows;s++)for(var r=0;r<this.max_cols;r++)t=this.vga_memory[i],e=this.vga_memory[1|i],this.bus.send("screen-put-char",[s,r,t,this.vga256_palette[this.dac_mask&this.dac_map[e>>4&15]],this.vga256_palette[this.dac_mask&this.dac_map[15&e]]]),i+=2},et.prototype.vga_memory_write_text_mode=function(t,e){var i=(t>>1)-this.start_address,s=i/this.max_cols|0;if(i%=this.max_cols,1&t)var r=e,a=this.vga_memory[-2&t];else a=e,r=this.vga_memory[1|t];this.bus.send("screen-put-char",[s,i,a,this.vga256_palette[this.dac_mask&this.dac_map[r>>4&15]],this.vga256_palette[this.dac_mask&this.dac_map[15&r]]]),this.vga_memory[t]=e},et.prototype.update_cursor=function(){var t=(this.cursor_address-this.start_address)/this.max_cols|0,e=(this.cursor_address-this.start_address)%this.max_cols;t=Math.min(this.max_rows-1,t),this.bus.send("screen-update-cursor",[t,e])},et.prototype.complete_redraw=function(){_("complete redraw",256),this.graphical_mode?this.svga_enabled?this.cpu.svga_mark_dirty():(this.diff_addr_min=0,this.diff_addr_max=524288):this.text_mode_redraw()},et.prototype.complete_replot=function(){_("complete replot",256),this.graphical_mode&&!this.svga_enabled&&(this.diff_plot_min=0,this.diff_plot_max=524288,this.complete_redraw())},et.prototype.partial_redraw=function(t,e){t<this.diff_addr_min&&(this.diff_addr_min=t),e>this.diff_addr_max&&(this.diff_addr_max=e)},et.prototype.partial_replot=function(t,e){t<this.diff_plot_min&&(this.diff_plot_min=t),e>this.diff_plot_max&&(this.diff_plot_max=e),this.partial_redraw(t,e)},et.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},et.prototype.destroy=function(){},et.prototype.vga_bytes_per_line=function(){var t=this.offset_register<<2;return 64&this.underline_location_register?t<<=1:64&this.crtc_mode&&(t>>>=1),t},et.prototype.vga_addr_shift_count=function(){var t=128+(~this.underline_location_register&this.crtc_mode&64);return t-=64&this.underline_location_register,(t-=64&this.attribute_mode)>>>6},et.prototype.vga_addr_to_pixel=function(t){var e=this.vga_addr_shift_count();if(3&~this.crtc_mode){var i=t-this.start_address;i&=this.crtc_mode<<13|-24577;var s=(i<<=e)/this.virtual_width|0;switch(i%=this.virtual_width,3&this.crtc_mode){case 2:s=s<<1|t>>13&1;break;case 1:s=s<<1|t>>14&1;break;case 0:s=s<<2|t>>13&3}return s*this.virtual_width+i+(this.start_address<<e)}return t<<e},et.prototype.scan_line_to_screen_row=function(t){return 128&this.max_scan_line&&(t>>>=1),t=Math.ceil(t/(1+(31&this.max_scan_line))),1&this.crtc_mode||(t<<=1),2&this.crtc_mode||(t<<=1),t},et.prototype.set_size_text=function(t,e){this.max_cols=t,this.max_rows=e,this.bus.send("screen-set-size-text",[t,e])},et.prototype.set_size_graphical=function(t,e,i,s,r){if(!this.stats.is_graphical||this.stats.bpp!==i||this.screen_width!==t||this.screen_height!==e||this.virtual_width!==s||this.virtual_height!==r){if(this.screen_width=t,this.screen_height=e,this.virtual_width=s,this.virtual_height=r,this.stats.bpp=i,this.stats.is_graphical=!0,this.stats.res_x=t,this.stats.res_y=e,"undefined"!=typeof ImageData){let t=s*r,e=this.cpu.svga_allocate_dest_buffer(t)>>>0;this.dest_buffet_offset=e,this.image_data=new ImageData(new Uint8ClampedArray(this.cpu.wasm_memory.buffer,e,4*t),s,r),this.cpu.svga_mark_dirty()}this.bus.send("screen-set-size-graphical",[t,e,s,r,i])}},et.prototype.update_vga_size=function(){if(!this.svga_enabled){var t=Math.min(1+this.horizontal_display_enable_end,this.horizontal_blank_start),e=Math.min(1+this.vertical_display_enable_end,this.vertical_blank_start);if(t&&e)if(this.graphical_mode){t<<=3;var i=this.offset_register<<4;64&this.attribute_mode&&(t>>>=1,i>>>=1),e=this.scan_line_to_screen_row(e);var s=Math.ceil(tt[0]/this.vga_bytes_per_line());this.set_size_graphical(t,e,8,i,s),this.update_vertical_retrace(),this.update_layers()}else 128&this.max_scan_line&&(e>>>=1),i=e/(1+(31&this.max_scan_line))|0,t&&i&&this.set_size_text(t,i)}},et.prototype.update_layers=function(){if(this.graphical_mode||this.text_mode_redraw(),this.svga_enabled)this.layers=[];else if(this.virtual_width&&this.screen_width)if(!this.palette_source||32&this.clocking_mode)this.layers=[],this.bus.send("screen-clear");else{var t=this.start_address_latched,e=this.horizontal_panning;64&this.attribute_mode&&(e>>>=1);var i=this.preset_row_scan>>5&3,s=this.vga_addr_to_pixel(t+i);t=s/this.virtual_width|0;var r=s%this.virtual_width+e;s=this.scan_line_to_screen_row(1+this.line_compare),s=Math.min(s,this.screen_height);var a=this.screen_height-s;this.layers=[],r=-r;for(var o=0;r<this.screen_width;r+=this.virtual_width,o++)this.layers.push({image_data:this.image_data,screen_x:r,screen_y:0,buffer_x:0,buffer_y:t+o,buffer_width:this.virtual_width,buffer_height:s});for(t=0,32&this.attribute_mode||(t=this.vga_addr_to_pixel(i)+e),r=-t,o=0;r<this.screen_width;r+=this.virtual_width,o++)this.layers.push({image_data:this.image_data,screen_x:r,screen_y:s,buffer_x:0,buffer_y:o,buffer_width:this.virtual_width,buffer_height:a})}},et.prototype.update_vertical_retrace=function(){this.port_3DA_value|=8,this.start_address_latched!==this.start_address&&(this.start_address_latched=this.start_address,this.update_layers())},et.prototype.update_cursor_scanline=function(){this.bus.send("screen-update-cursor-scanline",[this.cursor_scanline_start,this.cursor_scanline_end])},et.prototype.port3C0_write=function(t){if(-1===this.attribute_controller_index)_("attribute controller index register: "+p(t),256),this.attribute_controller_index=31&t,_("attribute actual index: "+p(this.attribute_controller_index),256),this.palette_source!==(32&t)&&(this.palette_source=32&t,this.update_layers());else{if(16>this.attribute_controller_index)_("internal palette: "+p(this.attribute_controller_index)+" -> "+p(t),256),this.dac_map[this.attribute_controller_index]=t,64&this.attribute_mode||this.complete_redraw();else switch(this.attribute_controller_index){case 16:if(_("3C0 / attribute mode control: "+p(t),256),this.attribute_mode!==t){var e=this.attribute_mode;this.attribute_mode=t;var i=0<(1&t);this.svga_enabled||this.graphical_mode===i||(this.graphical_mode=i,this.bus.send("screen-set-mode",this.graphical_mode)),64&(e^t)&&this.complete_replot(),this.update_vga_size(),this.complete_redraw()}break;case 18:_("3C0 / color plane enable: "+p(t),256),this.color_plane_enable!==t&&(this.color_plane_enable=t,this.complete_redraw());break;case 19:_("3C0 / horizontal panning: "+p(t),256),this.horizontal_panning!==t&&(this.horizontal_panning=15&t,this.update_layers());break;case 20:_("3C0 / color select: "+p(t),256),this.color_select!==t&&(this.color_select=t,this.complete_redraw());break;default:_("3C0 / attribute controller write "+p(this.attribute_controller_index)+": "+p(t),256)}this.attribute_controller_index=-1}},et.prototype.port3C0_read=function(){return _("3C0 read",256),this.attribute_controller_index|this.palette_source},et.prototype.port3C0_read16=function(){return _("3C0 read16",256),255&this.port3C0_read()|this.port3C1_read()<<8&65280},et.prototype.port3C1_read=function(){if(16>this.attribute_controller_index)return _("3C1 / internal palette read: "+p(this.attribute_controller_index)+" -> "+p(this.dac_map[this.attribute_controller_index]),256),255&this.dac_map[this.attribute_controller_index];switch(this.attribute_controller_index){case 16:return _("3C1 / attribute mode read: "+p(this.attribute_mode),256),this.attribute_mode;case 18:return _("3C1 / color plane enable read: "+p(this.color_plane_enable),256),this.color_plane_enable;case 19:return _("3C1 / horizontal panning read: "+p(this.horizontal_panning),256),this.horizontal_panning;case 20:return _("3C1 / color select read: "+p(this.color_select),256),this.color_select;default:_("3C1 / attribute controller read "+p(this.attribute_controller_index),256)}return 255},et.prototype.port3C2_write=function(t){_("3C2 / miscellaneous output register = "+p(t),256),this.miscellaneous_output_register=t},et.prototype.port3C4_write=function(t){this.sequencer_index=t},et.prototype.port3C4_read=function(){return this.sequencer_index},et.prototype.port3C5_write=function(t){switch(this.sequencer_index){case 1:_("clocking mode: "+p(t),256);var e=this.clocking_mode;this.clocking_mode=t,32&(e^t)&&this.update_layers();break;case 2:_("plane write mask: "+p(t),256),this.plane_write_bm=t;break;case 4:_("sequencer memory mode: "+p(t),256),this.sequencer_memory_mode=t;break;default:_("3C5 / sequencer write "+p(this.sequencer_index)+": "+p(t),256)}},et.prototype.port3C5_read=function(){switch(_("3C5 / sequencer read "+p(this.sequencer_index),256),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},et.prototype.port3C6_write=function(t){this.dac_mask=t},et.prototype.port3C6_read=function(){return this.dac_mask},et.prototype.port3C7_write=function(t){_("3C7 write: "+p(t),256),this.dac_color_index_read=3*t,this.dac_state&=0},et.prototype.port3C7_read=function(){return this.dac_state},et.prototype.port3C8_write=function(t){this.dac_color_index_write=3*t,this.dac_state|=3},et.prototype.port3C8_read=function(){return this.dac_color_index_write/3&255},et.prototype.port3C9_write=function(t){var e=this.dac_color_index_write/3|0,i=this.dac_color_index_write%3,s=this.vga256_palette[e];if(0==(32&this.dispi_enable_value)){let e=1&(t&=63);t=t<<2|e<<1|e}0===i?s=-16711681&s|t<<16:1===i?s=-65281&s|t<<8:(s=-256&s|t,_("dac set color, index="+p(e)+" value="+p(s),256)),this.vga256_palette[e]!==s&&(this.vga256_palette[e]=s,this.complete_redraw()),this.dac_color_index_write++},et.prototype.port3C9_read=function(){_("3C9 read",256);var t=this.vga256_palette[this.dac_color_index_read/3|0]>>8*(2-this.dac_color_index_read%3)&255;return this.dac_color_index_read++,32&this.dispi_enable_value?t:t>>2},et.prototype.port3CC_read=function(){return _("3CC read",256),this.miscellaneous_output_register},et.prototype.port3CE_write=function(t){this.graphics_index=t},et.prototype.port3CE_read=function(){return this.graphics_index},et.prototype.port3CF_write=function(t){switch(this.graphics_index){case 0:this.planar_setreset=t,_("plane set/reset: "+p(t),256);break;case 1:this.planar_setreset_enable=t,_("plane set/reset enable: "+p(t),256);break;case 2:this.color_compare=t,_("color compare: "+p(t),256);break;case 3:this.planar_rotate_reg=t,_("plane rotate: "+p(t),256);break;case 4:this.plane_read=t,_("plane read: "+p(t),256);break;case 5:var e=this.planar_mode;this.planar_mode=t,_("planar mode: "+p(t),256),96&(e^t)&&this.complete_replot();break;case 6:_("miscellaneous graphics register: "+p(t),256),this.miscellaneous_graphics_register!==t&&(this.miscellaneous_graphics_register=t,this.update_vga_size());break;case 7:this.color_dont_care=t,_("color don't care: "+p(t),256);break;case 8:this.planar_bitmap=t,_("planar bitmap: "+p(t),256);break;default:_("3CF / graphics write "+p(this.graphics_index)+": "+p(t),256)}},et.prototype.port3CF_read=function(){switch(_("3CF / graphics read "+p(this.graphics_index),256),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},et.prototype.port3D4_write=function(t){_("3D4 / crtc index: "+t,256),this.index_crtc=t},et.prototype.port3D4_read=function(){return _("3D4 read / crtc index: "+this.index_crtc,256),this.index_crtc},et.prototype.port3D5_write=function(t){switch(this.index_crtc){case 1:_("3D5 / hdisp enable end write: "+p(t),256),this.horizontal_display_enable_end!==t&&(this.horizontal_display_enable_end=t,this.update_vga_size());break;case 2:this.horizontal_blank_start!==t&&(this.horizontal_blank_start=t,this.update_vga_size());break;case 7:_("3D5 / overflow register write: "+p(t),256);var e=this.vertical_display_enable_end;this.vertical_display_enable_end&=255,this.vertical_display_enable_end=this.vertical_display_enable_end|t<<3&512|t<<7&256,e!=this.vertical_display_enable_end&&this.update_vga_size(),this.line_compare=767&this.line_compare|t<<4&256,e=this.vertical_blank_start,this.vertical_blank_start=767&this.vertical_blank_start|t<<5&256,e!==this.vertical_blank_start&&this.update_vga_size(),this.update_layers();break;case 8:_("3D5 / preset row scan write: "+p(t),256),this.preset_row_scan=t,this.update_layers();break;case 9:_("3D5 / max scan line write: "+p(t),256),this.max_scan_line=t,this.line_compare=511&this.line_compare|t<<3&512,e=this.vertical_blank_start,this.vertical_blank_start=511&this.vertical_blank_start|t<<4&512,e!==this.vertical_blank_start&&this.update_vga_size(),this.update_layers();break;case 10:_("3D5 / cursor scanline start write: "+p(t),256),this.cursor_scanline_start=t,this.update_cursor_scanline();break;case 11:_("3D5 / cursor scanline end write: "+p(t),256),this.cursor_scanline_end=t,this.update_cursor_scanline();break;case 12:(this.start_address>>8&255)!==t&&(this.start_address=255&this.start_address|t<<8,this.update_layers(),3&~this.crtc_mode&&this.complete_replot()),_("3D5 / start addr hi write: "+p(t)+" -> "+p(this.start_address,4),256);break;case 13:(255&this.start_address)!==t&&(this.start_address=65280&this.start_address|t,this.update_layers(),3&~this.crtc_mode&&this.complete_replot()),_("3D5 / start addr lo write: "+p(t)+" -> "+p(this.start_address,4),256);break;case 14:_("3D5 / cursor address hi write: "+p(t),256),this.cursor_address=255&this.cursor_address|t<<8,this.update_cursor();break;case 15:_("3D5 / cursor address lo write: "+p(t),256),this.cursor_address=65280&this.cursor_address|t,this.update_cursor();break;case 18:_("3D5 / vdisp enable end write: "+p(t),256),(255&this.vertical_display_enable_end)!==t&&(this.vertical_display_enable_end=768&this.vertical_display_enable_end|t,this.update_vga_size());break;case 19:_("3D5 / offset register write: "+p(t),256),this.offset_register!==t&&(this.offset_register=t,this.update_vga_size(),3&~this.crtc_mode&&this.complete_replot());break;case 20:_("3D5 / underline location write: "+p(t),256),this.underline_location_register!==t&&(e=this.underline_location_register,this.underline_location_register=t,this.update_vga_size(),64&(e^t)&&this.complete_replot());break;case 21:_("3D5 / vertical blank start write: "+p(t),256),(255&this.vertical_blank_start)!==t&&(this.vertical_blank_start=768&this.vertical_blank_start|t,this.update_vga_size());break;case 23:_("3D5 / crtc mode write: "+p(t),256),this.crtc_mode!==t&&(e=this.crtc_mode,this.crtc_mode=t,this.update_vga_size(),67&(e^t)&&this.complete_replot());break;case 24:_("3D5 / line compare write: "+p(t),256),this.line_compare=768&this.line_compare|t,this.update_layers();break;default:this.index_crtc<this.crtc.length&&(this.crtc[this.index_crtc]=t),_("3D5 / CRTC write "+p(this.index_crtc)+": "+p(t),256)}},et.prototype.port3D5_read=function(){switch(_("3D5 read "+p(this.index_crtc),256),this.index_crtc){case 1:return this.horizontal_display_enable_end;case 2:return this.horizontal_blank_start;case 7:return this.vertical_display_enable_end>>7&2|this.vertical_blank_start>>5&8|this.line_compare>>4&16|this.vertical_display_enable_end>>3&64;case 8:return this.preset_row_scan;case 9:return this.max_scan_line;case 10:return this.cursor_scanline_start;case 11:return this.cursor_scanline_end;case 12:return 255&this.start_address;case 13:return this.start_address>>8;case 14:return this.cursor_address>>8;case 15:return 255&this.cursor_address;case 18:return 255&this.vertical_display_enable_end;case 19:return this.offset_register;case 20:return this.underline_location_register;case 21:return 255&this.vertical_blank_start;case 23:return this.crtc_mode;case 24:return 255&this.line_compare}return this.index_crtc<this.crtc.length?this.crtc[this.index_crtc]:0},et.prototype.port3DA_read=function(){_("3DA read - status 1 and clear attr index",256);var t=this.port_3DA_value;return this.graphical_mode?(this.port_3DA_value^=1,this.port_3DA_value&=1):(1&this.port_3DA_value&&(this.port_3DA_value^=8),this.port_3DA_value^=1),this.attribute_controller_index=-1,t},et.prototype.port1CE_write=function(t){this.dispi_index=t},et.prototype.port1CF_write=function(t){switch(_("1CF / dispi write "+p(this.dispi_index)+": "+p(t),256),this.dispi_index){case 1:this.svga_width=t,this.svga_width>2560&&(_("svga_width reduced from "+this.svga_width+" to 2560",256),this.svga_width=2560);break;case 2:this.svga_height=t,this.svga_height>1600&&(_("svga_height reduced from "+this.svga_height+" to 1600",256),this.svga_height=1600);break;case 3:this.svga_bpp=t;break;case 4:this.svga_enabled=1==(1&t),this.dispi_enable_value=t;break;case 5:_("SVGA bank offset: "+p(t<<16)),this.svga_bank_offset=t<<16;break;case 9:let e=t*this.svga_width;_("SVGA offset: "+p(e)+" y="+p(t),256),this.svga_offset!==e&&(this.svga_offset=e,this.complete_redraw())}!this.svga_enabled||this.svga_width&&this.svga_height||(_("SVGA: disabled because of invalid width/height: "+this.svga_width+"x"+this.svga_height,256),this.svga_enabled=!1),this.svga_bpp,4===this.svga_bpp||8===this.svga_bpp||15===this.svga_bpp||16===this.svga_bpp||24===this.svga_bpp||this.svga_bpp,this.svga_bpp,_("SVGA: enabled="+this.svga_enabled+", "+this.svga_width+"x"+this.svga_height+"x"+this.svga_bpp,256),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()},et.prototype.port1CF_read=function(){return _("1CF / dispi read "+p(this.dispi_index),256),this.svga_register_read(this.dispi_index)},et.prototype.svga_register_read=function(t){switch(t){case 0:return 45248;case 1:return 2&this.dispi_enable_value?2560:this.svga_width;case 2:return 2&this.dispi_enable_value?1600:this.svga_height;case 3:return 2&this.dispi_enable_value?32:this.svga_bpp;case 4:return this.dispi_enable_value;case 5:return this.svga_bank_offset>>>16;case 6:return this.screen_width?this.screen_width:1;case 8:return 0;case 10:return this.vga_memory_size/65536|0}return 255},et.prototype.vga_replot=function(){for(var t=-16&this.diff_plot_min,e=Math.min(15|this.diff_plot_max,524287),i=this.vga_addr_shift_count(),s=3&~this.crtc_mode,r=96&this.planar_mode,a=64&this.attribute_mode;t<=e;){var o=t>>>i;if(s){var _=t/this.virtual_width|0,n=t-this.virtual_width*_;switch(s){case 1:o=(1&_)<<13,_>>>=1;break;case 2:o=(1&_)<<14,_>>>=1;break;case 3:o=(3&_)<<13,_>>>=2}o|=(_*this.virtual_width+n>>>i)+this.start_address}_=this.plane0[o],n=this.plane1[o];var h=this.plane2[o],d=this.plane3[o];switch(o=new Uint8Array(8),r){case 0:_<<=0,n<<=1,h<<=2,d<<=3;for(var c=7;0<=c;c--)o[7-c]=_>>c&1|n>>c&2|h>>c&4|d>>c&8;break;case 32:o[0]=_>>6&3|h>>4&12,o[1]=_>>4&3|h>>2&12,o[2]=_>>2&3|h>>0&12,o[3]=_>>0&3|h<<2&12,o[4]=n>>6&3|d>>4&12,o[5]=n>>4&3|d>>2&12,o[6]=n>>2&3|d>>0&12,o[7]=n>>0&3|d<<2&12;break;case 64:case 96:o[0]=_>>4&15,o[1]=_>>0&15,o[2]=n>>4&15,o[3]=n>>0&15,o[4]=h>>4&15,o[5]=h>>0&15,o[6]=d>>4&15,o[7]=d>>0&15}if(a)for(_=c=0;4>c;c++,t++,_+=2)this.pixel_buffer[t]=o[_]<<4|o[_+1];else for(c=0;8>c;c++,t++)this.pixel_buffer[t]=o[c]}},et.prototype.vga_redraw=function(){var t=this.diff_addr_min,e=Math.min(this.diff_addr_max,524287);let i=new Int32Array(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,this.virtual_width*this.virtual_height);var s=255,r=0;if(128&this.attribute_mode&&(s&=207,r|=this.color_select<<4&48),64&this.attribute_mode)for(;t<=e;t++){var a=this.pixel_buffer[t]&s|r;a=this.vga256_palette[a],i[t]=65280&a|a<<16|a>>16|4278190080}else for(s&=63,r|=this.color_select<<4&192;t<=e;t++)a=this.dac_map[this.pixel_buffer[t]&this.color_plane_enable]&s|r,a=this.vga256_palette[a],i[t]=65280&a|a<<16|a>>16|4278190080},et.prototype.screen_fill_buffer=function(){if(this.graphical_mode){if(0===this.image_data.data.byteLength){var t=new Uint8ClampedArray(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,4*this.virtual_width*this.virtual_height);this.image_data=new ImageData(t,this.virtual_width,this.virtual_height),this.update_layers()}if(this.svga_enabled){t=0;let s=this.svga_height;if(8===this.svga_bpp){let t=new Int32Array(this.cpu.wasm_memory.buffer,this.dest_buffet_offset,this.screen_width*this.screen_height),s=new Uint8Array(this.cpu.wasm_memory.buffer,this.svga_memory.byteOffset,this.vga_memory_size);for(var e=0;e<t.length;e++){var i=this.vga256_palette[s[e]];t[e]=65280&i|i<<16|i>>16|4278190080}}else this.cpu.svga_fill_pixel_buffer(this.svga_bpp,this.svga_offset),e=15===this.svga_bpp?2:this.svga_bpp/8,t=((this.cpu.svga_dirty_bitmap_min_offset[0]/e|0)-this.svga_offset)/this.svga_width|0,s=1+(((this.cpu.svga_dirty_bitmap_max_offset[0]/e|0)-this.svga_offset)/this.svga_width|0);t<s&&(t=Math.max(t,0),s=Math.min(s,this.svga_height),this.bus.send("screen-fill-buffer-end",[{image_data:this.image_data,screen_x:0,screen_y:t,buffer_x:0,buffer_y:t,buffer_width:this.svga_width,buffer_height:s-t}]))}else this.vga_replot(),this.vga_redraw(),this.bus.send("screen-fill-buffer-end",this.layers);this.reset_diffs()}this.update_vertical_retrace()};function it(t,e,i){this.next_tick_immediately=i,this.wm=e,this.wasm_patch(),this.create_jit_imports(),this.wasm_memory=e=this.wm.exports.memory,this.memory_size=c.view(Uint32Array,e,812,1),this.mem8=new Uint8Array(0),this.mem32s=new Int32Array(this.mem8.buffer),this.segment_is_null=c.view(Uint8Array,e,724,8),this.segment_offsets=c.view(Int32Array,e,736,8),this.segment_limits=c.view(Uint32Array,e,768,8),this.protected_mode=c.view(Int32Array,e,800,1),this.idtr_size=c.view(Int32Array,e,564,1),this.idtr_offset=c.view(Int32Array,e,568,1),this.gdtr_size=c.view(Int32Array,e,572,1),this.gdtr_offset=c.view(Int32Array,e,576,1),this.tss_size_32=c.view(Int32Array,e,1128,1),this.page_fault=c.view(Uint32Array,e,540,8),this.cr=c.view(Int32Array,e,580,8),this.cpl=c.view(Uint8Array,e,612,1),this.is_32=c.view(Int32Array,e,804,1),this.stack_size_32=c.view(Int32Array,e,808,1),this.in_hlt=c.view(Uint8Array,e,616,1),this.last_virt_eip=c.view(Int32Array,e,620,1),this.eip_phys=c.view(Int32Array,e,624,1),this.sysenter_cs=c.view(Int32Array,e,636,1),this.sysenter_esp=c.view(Int32Array,e,640,1),this.sysenter_eip=c.view(Int32Array,e,644,1),this.prefixes=c.view(Int32Array,e,648,1),this.flags=c.view(Int32Array,e,120,1),this.flags_changed=c.view(Int32Array,e,116,1),this.last_op1=c.view(Int32Array,e,96,1),this.last_op_size=c.view(Int32Array,e,104,1),this.last_result=c.view(Int32Array,e,112,1),this.current_tsc=c.view(Uint32Array,e,960,2),this.devices={},this.instruction_pointer=c.view(Int32Array,e,556,1),this.previous_ip=c.view(Int32Array,e,560,1),this.apic_enabled=c.view(Uint8Array,e,548,1),this.acpi_enabled=c.view(Uint8Array,e,552,1),this.memory_map_read8=[],this.memory_map_write8=[],this.memory_map_read32=[],this.memory_map_write32=[],this.bios={main:null,vga:null},this.instruction_counter=c.view(Uint32Array,e,664,1),this.reg32=c.view(Int32Array,e,64,8),this.fpu_st=c.view(Int32Array,e,1152,32),this.fpu_stack_empty=c.view(Uint8Array,e,816,1),this.fpu_stack_empty[0]=255,this.fpu_stack_ptr=c.view(Uint8Array,e,1032,1),this.fpu_stack_ptr[0]=0,this.fpu_control_word=c.view(Uint16Array,e,1036,1),this.fpu_control_word[0]=895,this.fpu_status_word=c.view(Uint16Array,e,1040,1),this.fpu_status_word[0]=0,this.fpu_ip=c.view(Int32Array,e,1048,1),this.fpu_ip[0]=0,this.fpu_ip_selector=c.view(Int32Array,e,1052,1),this.fpu_ip_selector[0]=0,this.fpu_opcode=c.view(Int32Array,e,1044,1),this.fpu_opcode[0]=0,this.fpu_dp=c.view(Int32Array,e,1056,1),this.fpu_dp[0]=0,this.fpu_dp_selector=c.view(Int32Array,e,1060,1),this.fpu_dp_selector[0]=0,this.reg_xmm32s=c.view(Int32Array,e,832,32),this.mxcsr=c.view(Int32Array,e,824,1),this.sreg=c.view(Uint16Array,e,668,8),this.dreg=c.view(Int32Array,e,684,8),this.reg_pdpte=c.view(Int32Array,e,968,8),this.svga_dirty_bitmap_min_offset=c.view(Uint32Array,e,716,1),this.svga_dirty_bitmap_max_offset=c.view(Uint32Array,e,720,1),this.fw_value=[],this.fw_pointer=0,this.option_roms=[],this.io=void 0,this.bus=t,this.set_tsc(0,0),this.debug_init()}it.prototype.clear_opstats=function(){new Uint8Array(this.wasm_memory.buffer,32768,131072).fill(0),this.wm.exports.profiler_init()},it.prototype.create_jit_imports=function(){let t=Object.create(null);t.m=this.wm.exports.memory;for(let e of Object.keys(this.wm.exports))e.startsWith("_")||e.startsWith("zstd")||e.endsWith("_js")||(t[e]=this.wm.exports[e]);this.jit_imports=t},it.prototype.wasm_patch=function(){let t=t=>this.wm.exports[t],e=e=>{const i=t(e);return console.assert(i,"Missing import: "+e),i};this.reset_cpu=e("reset_cpu"),this.getiopl=e("getiopl"),this.get_eflags=e("get_eflags"),this.get_eflags_no_arith=e("get_eflags_no_arith"),this.pic_call_irq=e("pic_call_irq"),this.do_many_cycles_native=e("do_many_cycles_native"),this.cycle_internal=e("cycle_internal"),this.read8=e("read8"),this.read16=e("read16"),this.read32s=e("read32s"),this.write8=e("write8"),this.write16=e("write16"),this.write32=e("write32"),this.in_mapped_range=e("in_mapped_range"),this.fpu_load_tag_word=e("fpu_load_tag_word"),this.fpu_load_status_word=e("fpu_load_status_word"),this.fpu_get_sti_f64=e("fpu_get_sti_f64"),this.translate_address_system_read=e("translate_address_system_read_js"),this.get_seg_cs=e("get_seg_cs"),this.get_real_eip=e("get_real_eip"),this.clear_tlb=e("clear_tlb"),this.full_clear_tlb=e("full_clear_tlb"),this.set_tsc=e("set_tsc"),this.store_current_tsc=e("store_current_tsc"),this.jit_clear_cache=e("jit_clear_cache_js"),this.jit_dirty_cache=e("jit_dirty_cache"),this.codegen_finalize_finished=e("codegen_finalize_finished"),this.allocate_memory=e("allocate_memory"),this.zero_memory=e("zero_memory"),this.svga_allocate_memory=e("svga_allocate_memory"),this.svga_allocate_dest_buffer=e("svga_allocate_dest_buffer"),this.svga_fill_pixel_buffer=e("svga_fill_pixel_buffer"),this.svga_mark_dirty=e("svga_mark_dirty"),this.zstd_create_ctx=e("zstd_create_ctx"),this.zstd_get_src_ptr=e("zstd_get_src_ptr"),this.zstd_free_ctx=e("zstd_free_ctx"),this.zstd_read=e("zstd_read"),this.zstd_read_free=e("zstd_read_free")},it.prototype.jit_force_generate=function(t){this.jit_force_generate_unsafe&&this.jit_force_generate_unsafe(t)},it.prototype.jit_clear_func=function(t){this.wm.wasm_table.set(t+1024,null)},it.prototype.jit_clear_all_funcs=function(){let t=this.wm.wasm_table;for(let e=0;e<900;e++)t.set(1024+e,null)},it.prototype.get_state=function(){var t=[];t[0]=this.memory_size[0],t[1]=this.segment_is_null,t[2]=this.segment_offsets,t[3]=this.segment_limits,t[4]=this.protected_mode[0],t[5]=this.idtr_offset[0],t[6]=this.idtr_size[0],t[7]=this.gdtr_offset[0],t[8]=this.gdtr_size[0],t[9]=this.page_fault[0],t[10]=this.cr,t[11]=this.cpl[0],t[13]=this.is_32[0],t[16]=this.stack_size_32[0],t[17]=this.in_hlt[0],t[18]=this.last_virt_eip[0],t[19]=this.eip_phys[0],t[22]=this.sysenter_cs[0],t[23]=this.sysenter_eip[0],t[24]=this.sysenter_esp[0],t[25]=this.prefixes[0],t[26]=this.flags[0],t[27]=this.flags_changed[0],t[28]=this.last_op1[0],t[30]=this.last_op_size[0],t[37]=this.instruction_pointer[0],t[38]=this.previous_ip[0],t[39]=this.reg32,t[40]=this.sreg,t[41]=this.dreg,t[42]=this.reg_pdpte,this.store_current_tsc(),t[43]=this.current_tsc,t[45]=this.devices.virtio_9p,t[46]=this.devices.apic,t[47]=this.devices.rtc,t[48]=this.devices.pci,t[49]=this.devices.dma,t[50]=this.devices.acpi,t[51]=this.devices.hpet,t[52]=this.devices.vga,t[53]=this.devices.ps2,t[54]=this.devices.uart0,t[55]=this.devices.fdc,t[56]=this.devices.cdrom,t[57]=this.devices.hda,t[58]=this.devices.pit,t[59]=this.devices.net,t[60]=this.devices.pic,t[61]=this.devices.sb16,t[62]=this.fw_value,t[63]=this.devices.ioapic,t[64]=this.tss_size_32[0],t[66]=this.reg_xmm32s,t[67]=this.fpu_st,t[68]=this.fpu_stack_empty[0],t[69]=this.fpu_stack_ptr[0],t[70]=this.fpu_control_word[0],t[71]=this.fpu_ip[0],t[72]=this.fpu_ip_selector[0],t[73]=this.fpu_dp[0],t[74]=this.fpu_dp_selector[0],t[75]=this.fpu_opcode[0];let{packed_memory:e,bitmap:i}=this.pack_memory();return t[77]=e,t[78]=new Uint8Array(i.get_buffer()),t[79]=this.devices.uart1,t[80]=this.devices.uart2,t[81]=this.devices.uart3,t},it.prototype.set_state=function(t){this.memory_size[0]=t[0],this.mem8.length!==this.memory_size[0]&&console.warn("Note: Memory size mismatch. we="+this.mem8.length+" state="+this.memory_size[0]),this.segment_is_null.set(t[1]),this.segment_offsets.set(t[2]),this.segment_limits.set(t[3]),this.protected_mode[0]=t[4],this.idtr_offset[0]=t[5],this.idtr_size[0]=t[6],this.gdtr_offset[0]=t[7],this.gdtr_size[0]=t[8],this.page_fault[0]=t[9],this.cr.set(t[10]),this.cpl[0]=t[11],this.is_32[0]=t[13],this.stack_size_32[0]=t[16],this.in_hlt[0]=t[17],this.last_virt_eip[0]=t[18],this.eip_phys[0]=t[19],this.sysenter_cs[0]=t[22],this.sysenter_eip[0]=t[23],this.sysenter_esp[0]=t[24],this.prefixes[0]=t[25],this.flags[0]=t[26],this.flags_changed[0]=t[27],this.last_op1[0]=t[28],this.last_op_size[0]=t[30],this.instruction_pointer[0]=t[37],this.previous_ip[0]=t[38],this.reg32.set(t[39]),this.sreg.set(t[40]),this.dreg.set(t[41]),t[42]&&this.reg_pdpte.set(t[42]),this.set_tsc(t[43][0],t[43][1]),this.devices.virtio_9p&&this.devices.virtio_9p.set_state(t[45]),this.devices.apic&&this.devices.apic.set_state(t[46]),this.devices.rtc&&this.devices.rtc.set_state(t[47]),this.devices.pci&&this.devices.pci.set_state(t[48]),this.devices.dma&&this.devices.dma.set_state(t[49]),this.devices.acpi&&this.devices.acpi.set_state(t[50]),this.devices.hpet&&this.devices.hpet.set_state(t[51]),this.devices.vga&&this.devices.vga.set_state(t[52]),this.devices.ps2&&this.devices.ps2.set_state(t[53]),this.devices.uart0&&this.devices.uart0.set_state(t[54]),this.devices.fdc&&this.devices.fdc.set_state(t[55]),this.devices.cdrom&&this.devices.cdrom.set_state(t[56]),this.devices.hda&&this.devices.hda.set_state(t[57]),this.devices.pit&&this.devices.pit.set_state(t[58]),this.devices.net&&this.devices.net.set_state(t[59]),this.devices.pic&&this.devices.pic.set_state(t[60]),this.devices.sb16&&this.devices.sb16.set_state(t[61]),this.devices.uart1&&this.devices.uart1.set_state(t[79]),this.devices.uart2&&this.devices.uart2.set_state(t[80]),this.devices.uart3&&this.devices.uart3.set_state(t[81]),this.fw_value=t[62],this.devices.ioapic&&this.devices.ioapic.set_state(t[63]),this.tss_size_32[0]=t[64],this.reg_xmm32s.set(t[66]),this.fpu_st.set(t[67]),this.fpu_stack_empty[0]=t[68],this.fpu_stack_ptr[0]=t[69],this.fpu_control_word[0]=t[70],this.fpu_ip[0]=t[71],this.fpu_ip_selector[0]=t[72],this.fpu_dp[0]=t[73],this.fpu_dp_selector[0]=t[74],this.fpu_opcode[0]=t[75];let e=new c.Bitmap(t[78].buffer);this.unpack_memory(e,t[77]),this.full_clear_tlb(),this.jit_clear_cache()},it.prototype.pack_memory=function(){this.mem8.length;for(var t=this.mem8.length>>12,e=[],i=0;i<t;i++){var s=i<<12;s=this.mem32s.subarray(s>>2,s+4096>>2);let t=!0;for(let e=0;e<s.length;e++)if(0!==s[e]){t=!1;break}t||e.push(i)}t=new c.Bitmap(t),i=new Uint8Array(e.length<<12);for(let[s,r]of e.entries())t.set(r,1),e=r<<12,e=this.mem8.subarray(e,e+4096),i.set(e,s<<12);return{bitmap:t,packed_memory:i}},it.prototype.unpack_memory=function(t,e){this.zero_memory(this.memory_size[0]);let i=this.memory_size[0]>>12,s=0;for(let a=0;a<i;a++)if(t.get(a)){var r=s<<12;r=e.subarray(r,r+4096),this.mem8.set(r,a<<12),s++}},it.prototype.main_run=function(){if(this.in_hlt[0]){var t=this.hlt_loop();if(this.in_hlt[0])return t}let e=t=O.microtick();for(;e-t<1;){this.do_many_cycles(),e=O.microtick();let t=this.run_hardware_timers(e);if(this.handle_irqs(),this.in_hlt[0])return t}return 0},it.prototype.reboot_internal=function(){this.reset_cpu(),this.fw_value=[],this.devices.virtio&&this.devices.virtio.reset(),this.load_bios()},it.prototype.reset_memory=function(){this.mem8.fill(0)},it.prototype.create_memory=function(t){1048576>t?t=1048576:0>(0|t)&&(t=Math.pow(2,31)-131072),t=1+(t-1|131071)|0,console.assert(0===this.memory_size[0],"Expected uninitialised memory"),this.memory_size[0]=t;let e=this.allocate_memory(t);this.mem8=c.view(Uint8Array,this.wasm_memory,e,t),this.mem32s=c.view(Uint32Array,this.wasm_memory,e,t>>2)},d.exportProperty(it.prototype,"create_memory",it.prototype.create_memory),it.prototype.init=function(e,i){"number"==typeof e.log_level&&(LOG_LEVEL=e.log_level),this.create_memory("number"==typeof e.memory_size?e.memory_size:67108864),this.acpi_enabled[0]=+e.acpi,this.reset_cpu();var r=new k(this);if(this.io=r,this.bios.main=e.bios,this.bios.vga=e.vga_bios,this.load_bios(),e.bzimage){let{option_rom:t}=load_kernel(this.mem8,e.bzimage,e.initrd,e.cmdline||"");t&&this.option_roms.push(t)}r.register_read(179,this,(function(){return _("port 0xB3 read"),0}));var a=0;r.register_read(146,this,(function(){return a})),r.register_write(146,this,(function(t){a=t})),r.register_read(1297,this,(function(){return this.fw_pointer<this.fw_value.length?this.fw_value[this.fw_pointer++]:0})),r.register_write(1296,this,void 0,(function(t){function e(t){return new Uint8Array(new Int32Array([t]).buffer)}function i(t){return t>>8|t<<8&65280}function s(t){return t<<24|t<<8&16711680|t>>8&65280|t>>>24}if(_("bios config port, index="+p(t)),this.fw_pointer=0,0===t)this.fw_value=e(1431127377);else if(1===t)this.fw_value=e(0);else if(3===t)this.fw_value=e(this.memory_size[0]);else if(5===t)this.fw_value=e(1);else if(15===t)this.fw_value=e(1);else if(13===t)this.fw_value=new Uint8Array(16);else if(25===t){t=new Int32Array(4+64*this.option_roms.length);let e=new Uint8Array(t.buffer);t[0]=s(this.option_roms.length);for(let r=0;r<this.option_roms.length;r++){let{name:a,data:o}=this.option_roms[r],_=4+64*r;t[_+0>>2]=s(o.length),t[_+4>>2]=i(49152+r),a.length;for(let t=0;t<a.length;t++)e[_+8+t]=a.charCodeAt(t)}this.fw_value=e}else t>=32768&&t<49152?this.fw_value=e(0):t>=49152&&t-49152<this.option_roms.length?this.fw_value=this.option_roms[t-49152].data:(_("Warning: Unimplemented fw index: "+p(t)),this.fw_value=e(0))})),this.devices={},e.load_devices&&(this.devices.pic=new C(this),this.devices.pci=new E(this),this.acpi_enabled[0]&&(this.devices.ioapic=new I(this),this.devices.apic=new s(this),this.devices.acpi=new t(this)),this.devices.rtc=new y(this),this.fill_cmos(this.devices.rtc,e),this.devices.dma=new b(this),this.devices.vga=new et(this,i,e.vga_memory_size||8388608),this.devices.ps2=new q(this,i),this.devices.uart0=new J(this,1016,i),e.uart1&&(this.devices.uart1=new J(this,760,i)),e.uart2&&(this.devices.uart2=new J(this,1e3,i)),e.uart3&&(this.devices.uart3=new J(this,744,i)),this.devices.fdc=new v(this,e.fda,e.fdb),r=0,e.hda&&(this.devices.hda=new w(this,e.hda,e.hdb,!1,r++,i)),e.cdrom&&(this.devices.cdrom=new w(this,e.cdrom,void 0,!0,r++,i)),this.devices.pit=new T(this,i),e.enable_ne2k&&(this.devices.net=new R(this,i,e.preserve_mac_from_state_image)),e.fs9p&&(this.devices.virtio_9p=new Virtio9p(e.fs9p,this,i)),this.devices.sb16=new G(this,i)),e.multiboot&&this.load_multiboot(e.multiboot)},it.prototype.load_multiboot=function(t){if(_("Trying multiboot from buffer of size "+t.byteLength,LOG_CPU),8192>t.byteLength){var e=new Int32Array(2048);new Uint8Array(e.buffer).set(new Uint8Array(t))}else e=new Int32Array(t,0,2048);for(var i=0;8192>i;i+=4)if(464367618===e[i>>2]){var s=e[i+4>>2];if(!(464367618+s+e[i+8>>2]|0)){_("Multiboot magic found, flags: "+p(s>>>0,8),LOG_CPU),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 r=0;6>r;r++)this.segment_is_null[r]=0,this.segment_offsets[r]=0,this.segment_limits[r]=4294967295,this.sreg[r]=45058;if(65536&s){_("Multiboot specifies its own address table",LOG_CPU),s=e[i+12>>2];var a=e[i+16>>2];r=e[i+20>>2];var o=e[i+24>>2];e=e[i+28>>2],_("header="+p(s,8)+" load="+p(a,8)+" load_end="+p(r,8)+" bss_end="+p(o,8)+" entry="+p(e,8)),t=new Uint8Array(t,i-(s-a),0===r?void 0:r-a),this.write_blob(t,a),this.instruction_pointer[0]=this.get_seg_cs()+e|0}else if(1179403647===e[0])for(a of(_("Multiboot image is in elf format",LOG_CPU),i=read_elf(t),this.instruction_pointer[0]=this.get_seg_cs()+i.header.entry|0,i.program_headers))0!==a.type&&(1===a.type?(a.paddr,a.vaddr,a.filesz,a.memsz,a.paddr+a.memsz<this.memory_size[0]?a.filesz&&(i=new Uint8Array(t,a.offset,a.filesz),this.write_blob(i,a.paddr)):_("Warning: Skipped loading section, paddr="+p(a.paddr)+" memsz="+a.memsz,LOG_CPU)):2!==a.type&&3!==a.type&&4!==a.type&&6!==a.type&&1685382480!==a.type&&1685382481!==a.type&&1685382483!==a.type&&p(a.type));this.io.register_write_consecutive(244,this,(function(t){throw console.log("Test exited with code "+p(t,2)),"HALT"}),(function(){}),(function(){}),(function(){}));for(let t=14;15>=t;t++)this.io.register_write(8192+t,this,(function(e){_("kvm-unit-test: Set irq "+p(t)+" to "+p(e,2)),e?this.device_raise_irq(t):this.device_lower_irq(t)}));_("Starting multiboot kernel at:",LOG_CPU),this.debug.dump_state(),this.debug.dump_regs();break}_("Multiboot checksum check failed",LOG_CPU)}},it.prototype.fill_cmos=function(t,e){var i=e.boot_order||531;t.cmos_write(56,1|i>>4&240),t.cmos_write(61,255&i),t.cmos_write(21,128),t.cmos_write(22,2),i=0,1048576<=this.memory_size[0]&&(i=this.memory_size[0]-1048576>>10,i=Math.min(i,65535)),t.cmos_write(23,255&i),t.cmos_write(24,i>>8&255),t.cmos_write(48,255&i),t.cmos_write(49,i>>8&255),i=0,16777216<=this.memory_size[0]&&(i=this.memory_size[0]-16777216>>16,i=Math.min(i,65535)),t.cmos_write(52,255&i),t.cmos_write(53,i>>8&255),t.cmos_write(91,0),t.cmos_write(92,0),t.cmos_write(93,0),t.cmos_write(20,47),t.cmos_write(95,0),e.fastboot&&t.cmos_write(63,1)},it.prototype.load_bios=function(){var t=this.bios.main,e=this.bios.vga;if(t){var i=new Uint8Array(t);if(this.write_blob(i,1048576-t.byteLength),e){var s=new Uint8Array(e);this.write_blob(s,786432),this.io.mmap_register(4272947200,1048576,(function(t){return(t=t-4272947200|0)<s.length?s[t]:0}),(function(t,e){}))}else _("Warning: No VGA BIOS");this.io.mmap_register(4293918720,1048576,function(t){return this.mem8[1048575&t]}.bind(this),function(t,e){this.mem8[1048575&t]=e}.bind(this))}else _("Warning: No BIOS")},it.prototype.do_many_cycles=function(){this.do_many_cycles_native()},it.prototype.cycle=function(){this.cycle_internal()},d.exportProperty(it.prototype,"cycle",it.prototype.cycle),it.prototype.codegen_finalize=function(t,e,i,s,r){let a=new Uint8Array(this.wasm_memory.buffer,s>>>0,r>>>0);WebAssembly.instantiate(a,{e:this.jit_imports}).then((s=>{s=s.instance.exports.f,this.codegen_finalize_finished(t,e,i),this.wm.wasm_table.set(t+1024,s),this.test_hook_did_finalize_wasm&&this.test_hook_did_finalize_wasm(a)}))},it.prototype.log_uncompiled_code=function(t,e){},it.prototype.dump_function_code=function(t,e){},it.prototype.hlt_loop=function(){if(512&this.get_eflags_no_arith()){let t=this.run_hardware_timers(O.microtick());return this.handle_irqs(),t}return 100},it.prototype.run_hardware_timers=function(t){var e=this.devices.pit.timer(t,!1),i=this.devices.rtc.timer(t,!1);let s=100,r=100;return this.acpi_enabled[0]&&(s=this.devices.acpi.timer(t),r=this.devices.apic.timer(t)),Math.min(e,i,100,s,r)},it.prototype.hlt_op=function(){0==(512&this.get_eflags_no_arith())&&this.bus.send("cpu-event-halt"),this.in_hlt[0]=1,this.hlt_loop()},it.prototype.handle_irqs=function(){512&this.get_eflags_no_arith()&&(this.pic_acknowledge(),this.next_tick_immediately())},it.prototype.pic_acknowledge=function(){this.get_eflags_no_arith(),this.devices.pic&&this.devices.pic.acknowledge_irq(),this.devices.apic&&this.devices.apic.acknowledge_irq()},it.prototype.device_raise_irq=function(t){this.devices.pic&&this.devices.pic.set_irq(t),this.devices.ioapic&&this.devices.ioapic.set_irq(t)},it.prototype.device_lower_irq=function(t){this.devices.pic&&this.devices.pic.clear_irq(t),this.devices.ioapic&&this.devices.ioapic.clear_irq(t)},it.prototype.debug_init=function(){var t=this,e={};let i,s,r;this.debug=e,e.init=function(){},e.get_regs_short=function(){var e={eax:REG_EAX,ecx:REG_ECX,edx:REG_EDX,ebx:REG_EBX,esp:REG_ESP,ebp:REG_EBP,esi:REG_ESI,edi:REG_EDI},i="eax ecx edx ebx esp ebp esi edi".split(" ");REG_CS,REG_DS,REG_ES,REG_FS,REG_GS,REG_SS;for(var s="",r="",a=0;4>a;a++)s+=i[a]+"="+h(t.reg32[e[i[a]]]>>>0,8)+" ",r+=i[a+4]+"="+h(t.reg32[e[i[a+4]]]>>>0,8)+" ";return[s+=" ds="+h(t.sreg[REG_DS],4)+" es="+h(t.sreg[REG_ES],4)+" fs="+h(t.sreg[REG_FS],4),r+=" gs="+h(t.sreg[REG_GS],4)+" cs="+h(t.sreg[REG_CS],4)+" ss="+h(t.sreg[REG_SS],4)]},e.dump_regs=function(){},e.get_state=function(t){},e.dump_state=function(t){},e.dump_stack=function(t,e){},e.dump_page_structures=function(){if(t.cr[4]&CR4_PAE){dbg_log("PAE enabled");for(var e=0;4>e;e++)t.read32s(t.cr[3]+8*e)}else dbg_log("PAE disabled")},e.dump_gdt_ldt=function(){},e.dump_idt=function(){},e.get_memory_dump=function(t,e){},e.memory_hex_dump=function(t,e){},e.used_memory_dump=function(){},e.debug_interrupt=function(t){},e.dump_code=function(t,e,r){if(!s){if(void 0===i&&("function"!=typeof require&&(i=window.cs),void 0===i))return void dbg_log("Warning: Missing capstone library, disassembly not available");s=[new i.Capstone(i.ARCH_X86,i.MODE_16),new i.Capstone(i.ARCH_X86,i.MODE_32)]}try{s[t].disasm(e,r).forEach((function(t){dbg_log(h(t.address>>>0)+": "+v86util.pads(t.bytes.map((t=>h(t,2).slice(-2))).join(" "),20)+" "+t.mnemonic+" "+t.op_str)})),dbg_log("")}catch(t){dbg_log("Could not disassemble: "+Array.from(e).map((t=>h(t,2))).join(" "))}},e.dump_wasm=function(t){if(void 0!==r||("function"!=typeof require&&(r=new window.WabtModule),void 0!==r)){t=t.slice();try{var e=r.readWasm(t,{readDebugNames:!1});e.generateNames(),e.applyNames();let i=e.toText({foldExprs:!0,inlineExport:!0});dbg_log(i)}catch(e){var i=new Blob([t]),s=document.createElement("a");s.download="failed.wasm",s.href=window.URL.createObjectURL(i),s.dataset.downloadurl=["application/octet-stream",s.download,s.href].join(":"),s.click(),window.URL.revokeObjectURL(s.src),console.log(e.toString())}finally{e&&e.destroy()}}else dbg_log("Warning: Missing libwabt, wasm dump not available")}};let st=DataView.prototype,rt={size:1,get:st.getUint8,set:st.setUint8},at={size:2,get:st.getUint16,set:st.setUint16},ot={size:4,get:st.getUint32,set:st.setUint32},_t=ct([{magic:ot},{class:rt},{data:rt},{version0:rt},{osabi:rt},{abiversion:rt},{pad0:(nt=7,{size:nt,get:t=>-1})},{type:at},{machine:at},{version1:ot},{entry:ot},{phoff:ot},{shoff:ot},{flags:ot},{ehsize:at},{phentsize:at},{phnum:at},{shentsize:at},{shnum:at},{shstrndx:at}]);var nt;console.assert(52===_t.reduce(((t,e)=>t+e.size),0));let ht=ct([{type:ot},{offset:ot},{vaddr:ot},{paddr:ot},{filesz:ot},{memsz:ot},{flags:ot},{align:ot}]);console.assert(32===ht.reduce(((t,e)=>t+e.size),0));let dt=ct([{name:ot},{type:ot},{flags:ot},{addr:ot},{offset:ot},{size:ot},{link:ot},{info:ot},{addralign:ot},{entsize:ot}]);function ct(t){return t.map((function(t){var e=Object.keys(t);return console.assert(1===e.length),t=t[e=e[0]],console.assert(0<t.size),{name:e,type:t,size:t.size,get:t.get,set:t.set}}))}function pt(t,e){let i={},s=0;for(let r of e)e=r.get.call(t,s,!0),console.assert(void 0===i[r.name]),i[r.name]=e,s+=r.size;return[i,s]}function ut(t,e,i){let s=[],r=0;for(var a=0;a<i;a++){let[i,a]=pt(lt(t,r),e);s.push(i),r+=a}return[s,r]}function lt(t,e,i){return new DataView(t.buffer,t.byteOffset+e,i)}console.assert(40===dt.reduce(((t,e)=>t+e.size),0));function ft(t,e){let i=new Uint8Array(256);new Uint16Array(i.buffer)[0]=43605,i[2]=1;var s=3;for(i[s++]=250,i[s++]=184,i[s++]=t>>0,i[s++]=t>>8,i[s++]=142,i[s++]=192,i[s++]=142,i[s++]=216,i[s++]=142,i[s++]=224,i[s++]=142,i[s++]=232,i[s++]=142,i[s++]=208,i[s++]=188,i[s++]=e>>0,i[s++]=e>>8,i[s++]=234,i[s++]=0,i[s++]=0,i[s++]=t+32>>0,i[s++]=t+32>>8,dbg_assert(512>s),e=i[t=s]=0,s=0;s<i.length;s++)e+=i[s];return i[t]=-e,i}it.prototype.mmap_read8=function(t){return this.memory_map_read8[t>>>17](t)},it.prototype.mmap_write8=function(t,e){this.memory_map_write8[t>>>17](t,e)},it.prototype.mmap_read16=function(t){var e=this.memory_map_read8[t>>>17];return e(t)|e(t+1|0)<<8},it.prototype.mmap_write16=function(t,e){var i=this.memory_map_write8[t>>>17];i(t,255&e),i(t+1|0,e>>8)},it.prototype.mmap_read32=function(t){return this.memory_map_read32[t>>>17](t)},it.prototype.mmap_write32=function(t,e){this.memory_map_write32[t>>>17](t,e)},it.prototype.mmap_write64=function(t,e,i){var s=this.memory_map_write32[t>>>17];s(t,e),s(t+4,i)},it.prototype.mmap_write128=function(t,e,i,s,r){var a=this.memory_map_write32[t>>>17];a(t,e),a(t+4,i),a(t+8,s),a(t+12,r)},it.prototype.write_blob=function(t,e){t&&t.length,t.length&&(this.in_mapped_range(e),this.in_mapped_range(e+t.length-1),this.jit_dirty_cache(e,e+t.length),this.mem8.set(t,e))},it.prototype.read_blob=function(t,e){return e&&(this.in_mapped_range(t),this.in_mapped_range(t+e-1)),this.mem8.subarray(t,t+e)};function mt(t){this.message=t}mt.prototype=Error();let gt={Uint8Array:Uint8Array,Int8Array:Int8Array,Uint16Array:Uint16Array,Int16Array:Int16Array,Uint32Array:Uint32Array,Int32Array:Int32Array,Float32Array:Float32Array,Float64Array:Float64Array};function bt(t,e){if("object"!=typeof t||null===t)return dbg_assert("function"!=typeof t),t;if(t instanceof Array)return t.map((t=>bt(t,e)));if(t.constructor===Object&&(console.log(t),dbg_assert(t.constructor!==Object,"Expected non-object")),t.BYTES_PER_ELEMENT){var i=new Uint8Array(t.buffer,t.byteOffset,t.length*t.BYTES_PER_ELEMENT);return t=t.constructor.name.replace("bound ",""),dbg_assert(gt[t]),{__state_type__:t,buffer_id:e.push(i)-1}}i=t.get_state(),t=[];for(var s=0;s<i.length;s++){var r=i[s];dbg_assert("function"!=typeof r),t[s]=bt(r,e)}return t}function yt(t,e){if("object"!=typeof t||null===t)return dbg_assert("function"!=typeof t),t;if(t instanceof Array){for(var i=0;i<t.length;i++)t[i]=yt(t[i],e);return t}i=t.__state_type__,dbg_assert(void 0!==i);let s=gt[i];return dbg_assert(s,"Unkown type: "+i),new s(e[t.buffer_id])}it.prototype.save_state=function(){for(var t=[],e=bt(this,t),i=[],s=0,r=0;r<t.length;r++){var a=t[r].byteLength;i[r]={offset:s,length:a},s=(s+=a)+3&-4}e=JSON.stringify({buffer_infos:i,state:e});var o=(r=(r=16+(e=(new TextEncoder).encode(e)).length)+3&-4)+s;s=new ArrayBuffer(o);var _=new Int32Array(s,0,4);for(new Uint8Array(s,16,e.length).set(e),a=new Uint8Array(s,r),_[0]=-2039052682,_[1]=6,_[2]=o,_[3]=e.length,r=0;r<t.length;r++)o=t[r],dbg_assert(o.constructor===Uint8Array),a.set(o,i[r].offset);return dbg_log("State: json size "+(e.byteLength>>10)+"k"),dbg_log("State: Total buffers size "+(a.byteLength>>10)+"k"),s},it.prototype.restore_state=function(t){function e(t,e){let i=t.length;if(i<16)throw new mt("Invalid length: "+i);if(-2039052682!==(t=new Int32Array(t.buffer,t.byteOffset,4))[0])throw new mt("Invalid header: "+h(t[0]>>>0));if(6!==t[1])throw new mt("Version mismatch: dump="+t[1]+" we=6");if(e&&t[2]!==i)throw new mt("Length doesn't match header: real="+i+" header="+t[2]);return t[3]}function i(t){return t=(new TextDecoder).decode(t),JSON.parse(t)}if(t=new Uint8Array(t),4247762216===new Uint32Array(t.buffer,0,1)[0]){var s=this.zstd_create_ctx(t.length);new Uint8Array(this.wasm_memory.buffer,this.zstd_get_src_ptr(s),t.length).set(t);var r=this.zstd_read(s,16),a=new Uint8Array(this.wasm_memory.buffer,r,16),o=e(a,!1);this.zstd_read_free(r,16),r=this.zstd_read(s,o),a=i(a=new Uint8Array(this.wasm_memory.buffer,r,o)),this.zstd_read_free(r,o),r=a.state;var _=a.buffer_infos;for(var n of(a=[],o=16+o,_)){if(_=(o+3&-4)-o,1048576<n.length){var d=this.zstd_read(s,_);this.zstd_read_free(d,_),d=new Uint8Array(n.length),a.push(d.buffer);for(var c=0;c<n.length;){var p=n.length-c;dbg_assert(0<=p),p=Math.min(p,1048576);let t=this.zstd_read(s,p);d.set(new Uint8Array(this.wasm_memory.buffer,t,p),c),this.zstd_read_free(t,p),c+=p}}else c=(d=this.zstd_read(s,_+n.length))+_,a.push(this.wasm_memory.buffer.slice(c,c+n.length)),this.zstd_read_free(d,_+n.length);o+=_+n.length}r=yt(r,a),this.set_state(r),this.zstd_free_ctx(s)}else{if(0>(s=e(t,!0))||s+12>=t.length)throw new mt("Invalid info block length: "+s);n=(r=i(n=t.subarray(16,16+s))).state,r=r.buffer_infos;let a=16+s;a=a+3&-4,n=yt(n,s=r.map((e=>{const i=a+e.offset;return t.buffer.slice(i,i+e.length)}))),this.set_state(n)}};function vt(t,e){for(var i of(t.io,this.cpu=t,this.pci=t.devices.pci,this.device_id=e.device_id,this.pci_space=[244,26,255&e.device_id,e.device_id>>8,7,5,16,0,1,0,2,0,0,0,0,0,1,168,0,0,0,16,191,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,255&e.subsystem_device_id,e.subsystem_device_id>>8,0,0,0,0,64,0,0,0,0,0,0,0,0,1,0,0],this.pci_space=this.pci_space.concat(v86util.zeros(256-this.pci_space.length)),this.pci_id=e.pci_id,this.pci_bars=[],this.name=e.name,this.driver_feature_select=this.device_feature_select=0,this.device_feature=new Uint32Array(4),this.driver_feature=new Uint32Array(4),e.common.features))dbg_assert(0<=i,"VirtIO device<"+this.name+"> feature bit numbers must be non-negative"),dbg_assert(128>i,"VirtIO device<"+this.name+"> feature bit numbers assumed less than 128 in implementation"),this.device_feature[i>>>5]|=1<<(31&i),this.driver_feature[i>>>5]|=1<<(31&i);for(var s of(dbg_assert(e.common.features.includes(32),"VirtIO device<"+this.name+"> only non-transitional devices are supported"),this.features_ok=!0,this.device_status=0,this.config_has_changed=!1,this.config_generation=0,this.queues=[],e.common.queues))this.queues.push(new wt(t,this,s));if(this.queue_select=0,this.queue_selected=this.queues[0],this.isr_status=0,DEBUG){for(var r of(i=new Set,this.queues.map((t=>t.notify_offset))))s=e.notification.single_handler?0:r,i.add(s),dbg_assert(e.notification.handlers[s],"VirtIO device<"+this.name+"> every queue's notifier must exist");for(let[t,s]of e.notification.handlers.entries())dbg_assert(!s||i.has(t),"VirtIO device<"+this.name+"> no defined notify handler should be unused")}(r=[]).push(this.create_common_capability(e.common)),r.push(this.create_notification_capability(e.notification)),r.push(this.create_isr_capability(e.isr_status)),e.device_specific&&r.push(this.create_device_specific_capability(e.device_specific)),this.init_capabilities(r),t.devices.pci.register_device(this),this.reset()}function wt(t,e,i){this.cpu=t,this.virtio=e,this.size_supported=this.size=i.size_supported,this.mask=this.size-1,this.enabled=!1,this.notify_offset=i.notify_offset,this.num_staged_replies=this.used_addr=this.avail_last_idx=this.avail_addr=this.desc_addr=0,this.reset()}function xt(t,e){this.cpu=t.cpu,this.virtio=t.virtio,this.head_idx=e,this.read_buffers=[],this.length_readable=this.read_buffer_offset=this.read_buffer_idx=0,this.write_buffers=[],this.length_writable=this.length_written=this.write_buffer_offset=this.write_buffer_idx=0;let i=t.desc_addr,s=0,r=t.size,a=!1,o=this.virtio.is_feature_negotiated(28);for(dbg_log("<<< Descriptor chain start",LOG_VIRTIO);;){let _=t.get_descriptor(i,e);if(dbg_log("descriptor: idx="+e+" addr="+h(_.addr_high,8)+":"+h(_.addr_low,8)+" len="+h(_.len,8)+" flags="+h(_.flags,4)+" next="+h(_.next,4),LOG_VIRTIO),o&&4&_.flags)DEBUG&&1&_.flags&&dbg_log("Driver bug: has set VIRTQ_DESC_F_NEXT flag in an indirect table descriptor",LOG_VIRTIO),i=_.addr_low,s=e=0,r=_.len/16,dbg_log("start indirect",LOG_VIRTIO);else{if(2&_.flags)a=!0,this.write_buffers.push(_),this.length_writable+=_.len;else{if(a){dbg_log("Driver bug: readonly buffer after writeonly buffer within chain",LOG_VIRTIO);break}this.read_buffers.push(_),this.length_readable+=_.len}if(s++,s>r){dbg_log("Driver bug: descriptor chain cycle detected",LOG_VIRTIO);break}if(!(1&_.flags))break;e=_.next}}dbg_log("Descriptor chain end >>>",LOG_VIRTIO)}function kt(t,e){this.bus=e,this.socket=void 0,this.send_queue=[],this.url=t,this.reconnect_interval=1e4,this.last_connect_attempt=Date.now()-this.reconnect_interval,this.send_queue_limit=64,this.bus.register("net0-send",(function(t){this.send(t)}),this)}vt.prototype.create_common_capability=function(t){return{type:1,bar:0,port:t.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array(0),struct:[{bytes:4,name:"device_feature_select",read:()=>this.device_feature_select,write:t=>{this.device_feature_select=t}},{bytes:4,name:"device_feature",read:()=>this.device_feature[this.device_feature_select]||0,write:t=>{}},{bytes:4,name:"driver_feature_select",read:()=>this.driver_feature_select,write:t=>{this.driver_feature_select=t}},{bytes:4,name:"driver_feature",read:()=>this.driver_feature[this.driver_feature_select]||0,write:t=>{let e=this.device_feature[this.driver_feature_select];this.driver_feature_select<this.driver_feature.length&&(this.driver_feature[this.driver_feature_select]=t&e),this.features_ok=this.features_ok&&!(t&~e)}},{bytes:2,name:"msix_config",read:()=>(dbg_log("No msi-x capability supported.",LOG_VIRTIO),65535),write:t=>{dbg_log("No msi-x capability supported.",LOG_VIRTIO)}},{bytes:2,name:"num_queues",read:()=>this.queues.length,write:t=>{}},{bytes:1,name:"device_status",read:()=>this.device_status,write:e=>{0===e?(dbg_log("Reset device<"+this.name+">",LOG_VIRTIO),this.reset()):128&e?dbg_log("Warning: Device<"+this.name+"> status failed",LOG_VIRTIO):dbg_log("Device<"+this.name+"> status: "+(1&e?"ACKNOWLEDGE ":"")+(2&e?"DRIVER ":"")+(4&e?"DRIVER_OK":"")+(8&e?"FEATURES_OK ":"")+(64&e?"DEVICE_NEEDS_RESET":""),LOG_VIRTIO),e&~this.device_status&4&&64&this.device_status&&this.notify_config_changes(),this.features_ok||(DEBUG&&8&e&&dbg_log("Removing FEATURES_OK",LOG_VIRTIO),e&=-9),this.device_status=e,e&~this.device_status&4&&t.on_driver_ok()}},{bytes:1,name:"config_generation",read:()=>this.config_generation,write:t=>{}},{bytes:2,name:"queue_select",read:()=>this.queue_select,write:t=>{this.queue_select=t,this.queue_select<this.queues.length?this.queues_selected=this.queues[this.queue_select]:this.queue_selected=null}},{bytes:2,name:"queue_size",read:()=>this.queue_selected?this.queue_selected.size:0,write:t=>{this.queue_selected&&(t&t-1&&(dbg_log("Warning: dev<"+this.name+"> Given queue size was not a power of 2. Rounding up to next power of 2.",LOG_VIRTIO),t=1<<v86util.int_log2(t-1)+1),t>this.queue_selected.size_supported&&(dbg_log("Warning: dev<"+this.name+"> Trying to set queue size greater than supported. Clamping to supported size.",LOG_VIRTIO),t=this.queue_selected.size_supported),this.queue_selected.set_size(t))}},{bytes:2,name:"queue_msix_vector",read:()=>(dbg_log("No msi-x capability supported.",LOG_VIRTIO),65535),write:t=>{dbg_log("No msi-x capability supported.",LOG_VIRTIO)}},{bytes:2,name:"queue_enable",read:()=>this.queue_selected?0|this.queue_selected.enabled:0,write:t=>{this.queue_selected&&(1===t?this.queue_selected.is_configured()?this.queue_selected.enable():dbg_log("Driver bug: tried enabling unconfigured queue",LOG_VIRTIO):0===t&&dbg_log("Driver bug: tried writing 0 to queue_enable",LOG_VIRTIO))}},{bytes:2,name:"queue_notify_off",read:()=>this.queue_selected?this.queue_selected.notify_offset:0,write:t=>{}},{bytes:4,name:"queue_desc (low dword)",read:()=>this.queue_selected?this.queue_selected.desc_addr:0,write:t=>{this.queue_selected&&(this.queue_selected.desc_addr=t)}},{bytes:4,name:"queue_desc (high dword)",read:()=>0,write:t=>{dbg_log("Warning: High dword of 64 bit queue_desc ignored",LOG_VIRTIO)}},{bytes:4,name:"queue_avail (low dword)",read:()=>this.queue_selected?this.queue_selected.avail_addr:0,write:t=>{this.queue_selected&&(this.queue_selected.avail_addr=t)}},{bytes:4,name:"queue_avail (high dword)",read:()=>0,write:t=>{dbg_log("Warning: High dword of 64 bit queue_avail ignored",LOG_VIRTIO)}},{bytes:4,name:"queue_used (low dword)",read:()=>this.queue_selected?this.queue_selected.used_addr:0,write:t=>{this.queue_selected&&(this.queue_selected.used_addr=t)}},{bytes:4,name:"queue_used (high dword)",read:()=>0,write:t=>{dbg_log("Warning: High dword of 64 bit queue_used ignored",LOG_VIRTIO)}}]}},vt.prototype.create_notification_capability=function(t){let e,i=[];t.single_handler?(dbg_assert(1===t.handlers.length,"VirtIO device<"+this.name+"> too many notify handlers specified: expected single handler"),e=0):e=2;for(let[e,s]of t.handlers.entries())i.push({bytes:2,name:"notify"+e,read:()=>65535,write:s||(t=>{})});return{type:2,bar:1,port:t.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array([255&e,e>>8&255,e>>16&255,e>>24]),struct:i}},vt.prototype.create_isr_capability=function(t){return{type:3,bar:2,port:t.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array(0),struct:[{bytes:1,name:"isr_status",read:()=>{let t=this.isr_status;return this.lower_irq(),t},write:t=>{}}]}},vt.prototype.create_device_specific_capability=function(t){return dbg_assert(3&~t.offset,"VirtIO device<"+this.name+"> device specific cap offset must be 4-byte aligned"),{type:4,bar:3,port:t.initial_port,use_mmio:!1,offset:0,extra:new Uint8Array(0),struct:t.struct}},vt.prototype.init_capabilities=function(t){let e=this.pci_space[52]=64;var i=e;for(let r of t){e=(i=e)+(t=16+r.extra.length),dbg_assert(256>=e,"VirtIO device<"+this.name+"> can't fit all capabilities into 256byte configspace"),dbg_assert(0<=r.bar&&6>r.bar,"VirtIO device<"+this.name+"> capability invalid bar number");var s=r.struct.reduce(((t,e)=>t+e.bytes),0);s=16>(s+=r.offset)?16:1<<v86util.int_log2(s-1)+1,dbg_assert(0==(r.port&s-1),"VirtIO device<"+this.name+"> capability port should be aligned to pci bar size"),this.pci_bars[r.bar]={size:s},this.pci_space[i]=9,this.pci_space[i+1]=e,this.pci_space[i+2]=t,this.pci_space[i+3]=r.type,this.pci_space[i+4]=r.bar,this.pci_space[i+5]=0,this.pci_space[i+6]=0,this.pci_space[i+7]=0,this.pci_space[i+8]=255&r.offset,this.pci_space[i+9]=r.offset>>>8&255,this.pci_space[i+10]=r.offset>>>16&255,this.pci_space[i+11]=r.offset>>>24,this.pci_space[i+12]=255&s,this.pci_space[i+13]=s>>>8&255,this.pci_space[i+14]=s>>>16&255,this.pci_space[i+15]=s>>>24;for(let[t,e]of r.extra.entries())this.pci_space[i+16+t]=e;i=16+4*r.bar,this.pci_space[i]=254&r.port|!r.use_mmio,this.pci_space[i+1]=r.port>>>8&255,this.pci_space[i+2]=r.port>>>16&255,this.pci_space[i+3]=r.port>>>24&255,i=r.port+r.offset;for(let e of r.struct){let a=e.read;if(t=e.write,DEBUG&&(a=()=>{let t=e.read();return dbg_log("Device<"+this.name+"> cap["+r.type+"] read["+e.name+"] => "+h(t,8*e.bytes),LOG_VIRTIO),t},t=t=>{dbg_log("Device<"+this.name+"> cap["+r.type+"] write["+e.name+"] <= "+h(t,8*e.bytes),LOG_VIRTIO),e.write(t)}),r.use_mmio)dbg_assert(!1,"VirtIO device <"+this.name+"> mmio capability not implemented.");else{s=function(t){return dbg_log("Warning: 8-bit read from 16-bit virtio port",LOG_VIRTIO),a(-2&t)>>((1&t)<<3)&255};let r=function(t){return dbg_log("Warning: 8-bit read from 32-bit virtio port",LOG_VIRTIO),a(-4&t)>>((3&t)<<3)&255};switch(e.bytes){case 4:this.cpu.io.register_read(i,this,r,void 0,a),this.cpu.io.register_write(i,this,void 0,void 0,t);break;case 2:this.cpu.io.register_read(i,this,s,a),this.cpu.io.register_write(i,this,void 0,t);break;case 1:this.cpu.io.register_read(i,this,a),this.cpu.io.register_write(i,this,t);break;default:dbg_assert(!1,"VirtIO device <"+this.name+"> invalid capability field width of "+e.bytes+" bytes")}}i+=e.bytes}}i=20,dbg_assert(256>=e+i,"VirtIO device<"+this.name+"> can't fit all capabilities into 256byte configspace"),this.pci_space[e]=9,this.pci_space[e+1]=0,this.pci_space[e+2]=i,this.pci_space[e+3]=5,this.pci_space[e+4]=0,this.pci_space[e+5]=0,this.pci_space[e+6]=0,this.pci_space[e+7]=0,this.pci_space[e+8]=0,this.pci_space[e+9]=0,this.pci_space[e+10]=0,this.pci_space[e+11]=0,this.pci_space[e+12]=0,this.pci_space[e+13]=0,this.pci_space[e+14]=0,this.pci_space[e+15]=0,this.pci_space[e+16]=0,this.pci_space[e+17]=0,this.pci_space[e+18]=0,this.pci_space[e+19]=0},vt.prototype.get_state=function(){let t=[];return t[0]=this.device_feature_select,t[1]=this.driver_feature_select,t[2]=this.device_feature,t[3]=this.driver_feature,t[4]=this.features_ok,t[5]=this.device_status,t[6]=this.config_has_changed,t[7]=this.config_generation,t[8]=this.isr_status,t[9]=this.queue_select,t.concat(this.queues)},vt.prototype.set_state=function(t){this.device_feature_select=t[0],this.driver_feature_select=t[1],this.device_feature=t[2],this.driver_feature=t[3],this.features_ok=t[4],this.device_status=t[5],this.config_has_changed=t[6],this.config_generation=t[7],this.isr_status=t[8],this.queue_select=t[9];let e=0;for(let i of t.slice(10))this.queues[e].set_state(i),e++;this.queue_selected=this.queues[this.queue_select]||null},vt.prototype.reset=function(){this.driver_feature_select=this.device_feature_select=0,this.driver_feature.set(this.device_feature),this.features_ok=!0,this.queue_select=this.device_status=0,this.queue_selected=this.queues[0];for(let t of this.queues)t.reset();this.config_has_changed=!1,this.config_generation=0,this.lower_irq()},vt.prototype.notify_config_changes=function(){this.config_has_changed=!0,4&this.device_status?this.raise_irq(2):dbg_assert(!1,"VirtIO device<"+this.name+"> attempted to notify driver before DRIVER_OK")},vt.prototype.update_config_generation=function(){this.config_has_changed&&(this.config_generation++,this.config_generation&=255,this.config_has_changed=!1)},vt.prototype.is_feature_negotiated=function(t){return 0<(this.driver_feature[t>>>5]&1<<(31&t))},vt.prototype.needs_reset=function(){dbg_log("Device<"+this.name+"> experienced error - requires reset",LOG_VIRTIO),this.device_status|=64,4&this.device_status&&this.notify_config_changes()},vt.prototype.raise_irq=function(t){dbg_log("Raise irq "+h(t),LOG_VIRTIO),this.isr_status|=t,this.pci.raise_irq(this.pci_id)},vt.prototype.lower_irq=function(){dbg_log("Lower irq ",LOG_VIRTIO),this.isr_status=0,this.pci.lower_irq(this.pci_id)},wt.prototype.get_state=function(){let t=[];return t[0]=this.size,t[1]=this.size_supported,t[2]=this.enabled,t[3]=this.notify_offset,t[4]=this.desc_addr,t[5]=this.avail_addr,t[6]=this.avail_last_idx,t[7]=this.used_addr,t[8]=this.num_staged_replies,t},wt.prototype.set_state=function(t){this.size=t[0],this.size_supported=t[1],this.enabled=t[2],this.notify_offset=t[3],this.desc_addr=t[4],this.avail_addr=t[5],this.avail_last_idx=t[6],this.used_addr=t[7],this.num_staged_replies=t[8],this.mask=this.size-1},wt.prototype.reset=function(){this.enabled=!1,this.num_staged_replies=this.used_addr=this.avail_last_idx=this.avail_addr=this.desc_addr=0,this.set_size(this.size_supported)},wt.prototype.is_configured=function(){return this.desc_addr&&this.avail_addr&&this.used_addr},wt.prototype.enable=function(){dbg_assert(this.is_configured(),"VirtQueue must be configured before enabled"),this.enabled=!0},wt.prototype.set_size=function(t){dbg_assert(0==(t&t-1),"VirtQueue size must be power of 2 or zero"),dbg_assert(t<=this.size_supported,"VirtQueue size must be within supported size"),this.size=t,this.mask=t-1},wt.prototype.count_requests=function(){return dbg_assert(this.avail_addr,"VirtQueue addresses must be configured before use"),this.avail_get_idx()-this.avail_last_idx&this.mask},wt.prototype.has_request=function(){return dbg_assert(this.avail_addr,"VirtQueue addresses must be configured before use"),(this.avail_get_idx()&this.mask)!==this.avail_last_idx},wt.prototype.pop_request=function(){dbg_assert(this.avail_addr,"VirtQueue addresses must be configured before use"),dbg_assert(this.has_request(),"VirtQueue must not pop nonexistent request");var t=this.avail_get_entry(this.avail_last_idx);return dbg_log("Pop request: avail_last_idx="+this.avail_last_idx+" desc_idx="+t,LOG_VIRTIO),t=new xt(this,t),this.avail_last_idx=this.avail_last_idx+1&this.mask,t},wt.prototype.push_reply=function(t){dbg_assert(this.used_addr,"VirtQueue addresses must be configured before use"),dbg_assert(this.num_staged_replies<this.size,"VirtQueue replies must not exceed queue size");let e=this.used_get_idx()+this.num_staged_replies&this.mask;dbg_log("Push reply: used_idx="+e+" desc_idx="+t.head_idx,LOG_VIRTIO),this.used_set_entry(e,t.head_idx,t.length_written),this.num_staged_replies++},wt.prototype.flush_replies=function(){if(dbg_assert(this.used_addr,"VirtQueue addresses must be configured before use"),0===this.num_staged_replies)dbg_log("flush_replies: Nothing to flush",LOG_VIRTIO);else{dbg_log("Flushing "+this.num_staged_replies+" replies",LOG_VIRTIO);var t=this.used_get_idx()+this.num_staged_replies&65535;this.used_set_idx(t),this.num_staged_replies=0,this.virtio.is_feature_negotiated(29)?(this.avail_get_used_event(),this.virtio.raise_irq(1)):1&~this.avail_get_flags()&&this.virtio.raise_irq(1)}},wt.prototype.notify_me_after=function(t){dbg_assert(0<=t,"Must skip a non-negative number of requests"),t=this.avail_get_idx()+t&65535,this.used_set_avail_event(t)},wt.prototype.get_descriptor=function(t,e){return{addr_low:this.cpu.read32s(t+16*e),addr_high:this.cpu.read32s(t+16*e+4),len:this.cpu.read32s(t+16*e+8),flags:this.cpu.read16(t+16*e+12),next:this.cpu.read16(t+16*e+14)}},wt.prototype.avail_get_flags=function(){return this.cpu.read16(this.avail_addr)},wt.prototype.avail_get_idx=function(){return this.cpu.read16(this.avail_addr+2)},wt.prototype.avail_get_entry=function(t){return this.cpu.read16(this.avail_addr+4+2*t)},wt.prototype.avail_get_used_event=function(){return this.cpu.read16(this.avail_addr+4+2*this.size)},wt.prototype.used_get_flags=function(){return this.cpu.read16(this.used_addr)},wt.prototype.used_set_flags=function(t){this.cpu.write16(this.used_addr,t)},wt.prototype.used_get_idx=function(){return this.cpu.read16(this.used_addr+2)},wt.prototype.used_set_idx=function(t){this.cpu.write16(this.used_addr+2,t)},wt.prototype.used_set_entry=function(t,e,i){this.cpu.write32(this.used_addr+4+8*t,e),this.cpu.write32(this.used_addr+8+8*t,i)},wt.prototype.used_set_avail_event=function(t){this.cpu.write16(this.used_addr+4+8*this.size,t)},xt.prototype.get_next_blob=function(t){let e=0,i=t.length;for(;i;){if(this.read_buffer_idx===this.read_buffers.length){dbg_log("Device<"+this.virtio.name+"> Read more than device-readable buffers has",LOG_VIRTIO);break}var s=this.read_buffers[this.read_buffer_idx];let r=s.addr_low+this.read_buffer_offset;(s=s.len-this.read_buffer_offset)>i?(s=i,this.read_buffer_offset+=i):(this.read_buffer_idx++,this.read_buffer_offset=0),t.set(this.cpu.read_blob(r,s),e),e+=s,i-=s}return e},xt.prototype.set_next_blob=function(t){let e=0,i=t.length;for(;i;){if(this.write_buffer_idx===this.write_buffers.length){dbg_log("Device<"+this.virtio.name+"> Write more than device-writable capacity",LOG_VIRTIO);break}var s=this.write_buffers[this.write_buffer_idx];let r=s.addr_low+this.write_buffer_offset;(s=s.len-this.write_buffer_offset)>i?(s=i,this.write_buffer_offset+=i):(this.write_buffer_idx++,this.write_buffer_offset=0),this.cpu.write_blob(t.subarray(e,e+s),r),e+=s,i-=s}return this.length_written+=e,e},kt.prototype.handle_message=function(t){this.bus&&this.bus.send("net0-receive",new Uint8Array(t.data))},kt.prototype.handle_close=function(t){this.connect(),setTimeout(this.connect.bind(this),this.reconnect_interval)},kt.prototype.handle_open=function(t){for(t=0;t<this.send_queue.length;t++)this.send(this.send_queue[t]);this.send_queue=[]},kt.prototype.handle_error=function(t){},kt.prototype.destroy=function(){this.socket&&this.socket.close()},kt.prototype.connect=function(){if("undefined"!=typeof WebSocket){if(this.socket){var t=this.socket.readyState;if(0===t||1===t)return}t=Date.now(),this.last_connect_attempt+this.reconnect_interval>t||(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))}},kt.prototype.send=function(t){this.socket&&1===this.socket.readyState?this.socket.send(t):(this.send_queue.push(t),this.send_queue.length>2*this.send_queue_limit&&(this.send_queue=this.send_queue.slice(-this.send_queue_limit)),this.connect())},kt.prototype.change_proxy=function(t){this.url=t,this.socket&&(this.socket.onclose=function(){},this.socket.onerror=function(){},this.socket.close(),this.socket=void 0)};function At(t){function e(t){return!t.altKey&&_[56]&&a(56,!1),r(t,!1)}function i(t){return!t.altKey&&_[56]&&a(56,!1),r(t,!0)}function s(t){t=Object.keys(_);for(var e,i=0;i<t.length;i++)e=+t[i],_[e]&&a(e,!1);_={}}function r(t,e){var i;if((i=n.bus)&&(i=!(t.shiftKey&&t.ctrlKey&&(73===t.keyCode||74===t.keyCode||75===t.keyCode)||!n.emu_enabled)&&(!t.target||(t.target.classList.contains("phone_keyboard")||"INPUT"!==t.target.nodeName&&"TEXTAREA"!==t.target.nodeName))),i){if(void 0!==t.code&&void 0!==(i=p[t.code])||(i=h[t.keyCode]),i)return a(i,e,t.repeat),t.preventDefault&&t.preventDefault(),!1;console.log("Missing char in map: keyCode="+(t.keyCode||-1).toString(16)+" code="+t.code)}}function a(t,e,i){if(e)_[t]&&!i&&a(t,!1);else if(!_[t])return;(_[t]=e)||(t|=128),255<t?(o(t>>8),o(255&t)):o(t)}function o(t){n.bus.send("keyboard-code",t)}var _={},n=this;this.emu_enabled=!0;var h=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]),d={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},c={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},p={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=t,this.destroy=function(){"undefined"!=typeof window&&(window.removeEventListener("keyup",e,!1),window.removeEventListener("keydown",i,!1),window.removeEventListener("blur",s,!1))},this.init=function(){"undefined"!=typeof window&&(this.destroy(),window.addEventListener("keyup",e,!1),window.addEventListener("keydown",i,!1),window.addEventListener("blur",s,!1))},this.init(),this.simulate_press=function(t){r(t={keyCode:t},!0),r(t,!1)},this.simulate_char=function(t){var e=t.charCodeAt(0);e in d?this.simulate_press(d[e]):e in c?(o(42),this.simulate_press(c[e]),o(170)):console.log("ascii -> keyCode not found: ",e,t)}}function It(t,e){function i(t){if(!f.enabled||!f.emu_enabled)return!1;var i,s=e||document.body;if(!(i=document.pointerLockElement))t:{for(t=t.target;t.parentNode;){if(t===s){i=!0;break t}t=t.parentNode}i=!1}return i}function s(t){i(t)&&(t=t.changedTouches)&&t.length&&(t=t[t.length-1],u=t.clientX,l=t.clientY)}function r(t){(d||p||c)&&(f.bus.send("mouse-click",[!1,!1,!1]),d=p=c=!1)}function a(t){if(f.bus&&i(t)){var s=0,r=0,a=t.changedTouches;a?a.length&&(s=(a=a[a.length-1]).clientX-u,r=a.clientY-l,u=a.clientX,l=a.clientY,t.preventDefault()):"number"==typeof t.movementX?(s=t.movementX,r=t.movementY):"number"==typeof t.webkitMovementX?(s=t.webkitMovementX,r=t.webkitMovementY):"number"==typeof t.mozMovementX?(s=t.mozMovementX,r=t.mozMovementY):(s=t.clientX-u,r=t.clientY-l,u=t.clientX,l=t.clientY),f.bus.send("mouse-delta",[.15*s,-.15*r]),e&&f.bus.send("mouse-absolute",[t.pageX-e.offsetLeft,t.pageY-e.offsetTop,e.offsetWidth,e.offsetHeight])}}function o(t){i(t)&&n(t,!0)}function _(t){i(t)&&n(t,!1)}function n(t,e){f.bus&&(1===t.which?d=e:2===t.which?p=e:3===t.which?c=e:dbg_log("Unknown event.which: "+t.which),f.bus.send("mouse-click",[d,p,c]),t.preventDefault())}function h(t){if(i(t)){var e=t.wheelDelta||-t.detail;0>e?e=-1:0<e&&(e=1),f.bus.send("mouse-wheel",[e,0]),t.preventDefault()}}var d=!1,c=!1,p=!1,u=0,l=0,f=this;this.enabled=!1,this.emu_enabled=!0,this.bus=t,this.bus.register("mouse-enable",(function(t){this.enabled=t}),this),this.destroy=function(){"undefined"!=typeof window&&(window.removeEventListener("touchstart",s,!1),window.removeEventListener("touchend",r,!1),window.removeEventListener("touchmove",a,!1),window.removeEventListener("mousemove",a,!1),window.removeEventListener("mousedown",o,!1),window.removeEventListener("mouseup",_,!1),window.removeEventListener("wheel",h,{passive:!1}))},this.init=function(){"undefined"!=typeof window&&(this.destroy(),window.addEventListener("touchstart",s,!1),window.addEventListener("touchend",r,!1),window.addEventListener("touchmove",a,!1),window.addEventListener("mousemove",a,!1),window.addEventListener("mousedown",o,!1),window.addEventListener("mouseup",_,!1),window.addEventListener("wheel",h,{passive:!1}))},this.init()}function Ot(t,e){function i(t){return t=t.toString(16),"#"+"0".repeat(6-t.length)+t}function s(t,e,i,s){t.style.width="",t.style.height="",s&&(t.style.transform="");var r=t.getBoundingClientRect();s?t.style.transform=(1===e?"":" scaleX("+e+")")+(1===i?"":" scaleY("+i+")"):(0==e%1&&0==i%1?(d.style.imageRendering="crisp-edges",d.style.imageRendering="pixelated",d.style["-ms-interpolation-mode"]="nearest-neighbor"):(d.style.imageRendering="",d.style["-ms-interpolation-mode"]=""),0!=(s=window.devicePixelRatio||1)%1&&(e/=s,i/=s)),1!==e&&(t.style.width=r.width*e+"px"),1!==i&&(t.style.height=r.height*i+"px")}console.assert(t,"1st argument must be a DOM container");var r,a,o,_,n,h,d=t.getElementsByTagName("canvas")[0],c=d.getContext("2d",{alpha:!1}),p=t.getElementsByTagName("div")[0],u=document.createElement("div"),l=1,f=1,m=1,g=!1,b=!1,y=this;t=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 v,w=new Uint16Array([32,9786,9787,9829,9830,9827,9824,8226,9688,9675,9689,9794,9792,9834,9835,9788,9658,9668,8597,8252,182,167,9644,8616,8593,8595,8594,8592,8735,8596,9650,9660]),x=[],k=0;256>k;k++)v=127<k?t[k-128]:32>k?w[k]:k,x[k]=String.fromCharCode(v);c.imageSmoothingEnabled=!1,u.style.position="absolute",u.style.backgroundColor="#ccc",u.style.width="7px",u.style.display="inline-block",p.style.display="block",d.style.display="none",this.bus=e,e.register("screen-set-mode",(function(t){this.set_mode(t)}),this),e.register("screen-fill-buffer-end",(function(t){this.update_buffer(t)}),this),e.register("screen-put-char",(function(t){this.put_char(t[0],t[1],t[2],t[3],t[4])}),this),e.register("screen-update-cursor",(function(t){this.update_cursor(t[0],t[1])}),this),e.register("screen-update-cursor-scanline",(function(t){this.update_cursor_scanline(t[0],t[1])}),this),e.register("screen-clear",(function(){this.clear_screen()}),this),e.register("screen-set-size-text",(function(t){this.set_size_text(t[0],t[1])}),this),e.register("screen-set-size-graphical",(function(t){this.set_size_graphical(t[0],t[1],t[2],t[3])}),this),this.init=function(){this.set_size_text(80,25),this.timer()},this.make_screenshot=function(){let t=new Image;if(g)t.src=d.toDataURL("image/png");else{let e=[9,16],s=document.createElement("canvas");s.width=n*e[0],s.height=h*e[1];let o=s.getContext("2d");o.imageSmoothingEnabled=!1,o.font=window.getComputedStyle(p).font,o.textBaseline="top";for(let t=0;t<n;t++)for(let s=0;s<h;s++){let r=3*(s*n+t);o.fillStyle=i(_[r+1]),o.fillRect(t*e[0],s*e[1],e[0],e[1]),o.fillStyle=i(_[r+2]),o.fillText(x[_[r]],t*e[0],s*e[1])}"none"!==u.style.display&&(o.fillStyle=u.style.backgroundColor,o.fillRect(a*e[0],r*e[1]+parseInt(u.style.marginTop,10)-1,parseInt(u.style.width,10),parseInt(u.style.height,10))),t.src=s.toDataURL("image/png")}try{window.open("").document.write(t.outerHTML)}catch(t){}},this.put_char=function(t,e,i,s,r){t<h&&e<n&&(_[e=3*(t*n+e)]=i,_[e+1]=s,_[e+2]=r,o[t]=1)},this.timer=function(){b||requestAnimationFrame(g?I:A)};var A=function(){for(var t=0;t<h;t++)o[t]&&(y.text_update_row(t),o[t]=0);this.timer()}.bind(this),I=function(){this.bus.send("screen-fill-buffer"),this.timer()}.bind(this);this.destroy=function(){b=!0},this.set_mode=function(t){(g=t)?(p.style.display="none",d.style.display="block"):(p.style.display="block",d.style.display="none")},this.clear_screen=function(){c.fillStyle="#000",c.fillRect(0,0,d.width,d.height)},this.set_size_text=function(t,e){if(t!==n||e!==h){for(o=new Int8Array(e),_=new Int32Array(t*e*3),n=t,h=e;p.childNodes.length>e;)p.removeChild(p.firstChild);for(;p.childNodes.length<e;)p.appendChild(document.createElement("div"));for(t=0;t<e;t++)this.text_update_row(t);s(p,l,f,!0)}},this.set_size_graphical=function(t,e,i,r){d.style.display="block",d.width=t,d.height=e,m=640>=t&&2*t<window.innerWidth&&2*t<window.innerHeight?2:1,s(d,l*m,f*m,!1)},this.set_scale=function(t,e){s(p,l=t,f=e,!0),s(d,l*m,f*m,!1)},this.set_scale(l,f),this.update_cursor_scanline=function(t,e){32&t?u.style.display="none":(u.style.display="inline",u.style.height=Math.min(15,e-t)+"px",u.style.marginTop=Math.min(15,t)+"px")},this.update_cursor=function(t,e){t===r&&e===a||(o[t]=1,o[r]=1,r=t,a=e)},this.text_update_row=function(t){for(var e,s=3*t*n,o=p.childNodes[t],h=document.createElement("div"),d=0;d<n;){var c=document.createElement("span"),l=_[s+1],f=_[s+2];for(c.style.backgroundColor=i(l),c.style.color=i(f),e="";d<n&&_[s+1]===l&&_[s+2]===f;){var m=_[s];if(e+=x[m],x[m],d++,s+=3,t===r){if(d===a)break;if(d===a+1){h.appendChild(u);break}}}c.textContent=e,h.appendChild(c)}o.parentNode.replaceChild(h,o)},this.update_buffer=function(t){t.forEach((t=>{c.putImageData(t.image_data,t.screen_x-t.buffer_x,t.screen_y-t.buffer_y,t.buffer_x,t.buffer_y,t.buffer_width,t.buffer_height)}))},this.init()}function Rt(t){var e,i,s;this.bus=t,t.register("screen-set-mode",(function(t){this.set_mode(t)}),this),t.register("screen-fill-buffer-end",(function(t){this.update_buffer(t[0],t[1])}),this),t.register("screen-put-char",(function(t){this.put_char(t[0],t[1],t[2],t[3],t[4])}),this),t.register("screen-text-scroll",(function(t){console.log("scroll",t)}),this),t.register("screen-update-cursor",(function(t){this.update_cursor(t[0],t[1])}),this),t.register("screen-update-cursor-scanline",(function(t){this.update_cursor_scanline(t[0],t[1])}),this),t.register("screen-set-size-text",(function(t){this.set_size_text(t[0],t[1])}),this),t.register("screen-set-size-graphical",(function(t){this.set_size_graphical(t[0],t[1])}),this),this.put_char=function(t,r,a,o,_){t<s&&r<i&&(e[t=3*(t*i+r)]=a,e[t+1]=o,e[t+2]=_)},this.destroy=function(){},this.set_mode=function(t){},this.clear_screen=function(){},this.set_size_text=function(t,r){t===i&&r===s||(e=new Int32Array(t*r*3),i=t,s=r)},this.set_size_graphical=function(t,e){},this.set_scale=function(t,e){},this.update_cursor_scanline=function(t,e){},this.update_cursor=function(t,e){},this.update_buffer=function(t,e){},this.get_text_screen=function(){for(var t=[],e=0;e<s;e++)t.push(this.get_text_row(e));return t},this.get_text_row=function(t){var s="";t=3*t*i;for(var r=0;r<i;r++)s+=String.fromCharCode(e[t+3*r]);return s}}function Et(t,e){function i(t){o.bus&&o.enabled&&(o.send_char(t.which),t.preventDefault())}function s(t){var e=t.which;8===e?(o.send_char(127),t.preventDefault()):9===e&&(o.send_char(9),t.preventDefault())}function r(t){if(o.enabled){for(var e=t.clipboardData.getData("text/plain"),i=0;i<e.length;i++)o.send_char(e.charCodeAt(i));t.preventDefault()}}function a(e){e.target!==t&&t.blur()}var o=this;this.enabled=!0,this.bus=e,this.text="",this.text_new_line=!1,this.last_update=0,this.bus.register("serial0-output-char",(function(t){this.show_char(t)}),this),this.destroy=function(){t.removeEventListener("keypress",i,!1),t.removeEventListener("keydown",s,!1),t.removeEventListener("paste",r,!1),window.removeEventListener("mousedown",a,!1)},this.init=function(){this.destroy(),t.style.display="block",t.addEventListener("keypress",i,!1),t.addEventListener("keydown",s,!1),t.addEventListener("paste",r,!1),window.addEventListener("mousedown",a,!1)},this.init(),this.show_char=function(t){"\b"===t?(this.text=this.text.slice(0,-1),this.update()):"\r"!==t&&(this.text+=t,"\n"===t&&(this.text_new_line=!0),this.update())},this.update=function(){var t=Date.now(),e=t-this.last_update;16>e?void 0===this.update_timer&&(this.update_timer=setTimeout((()=>{this.update_timer=void 0;var t=Date.now();dbg_assert(15<=t-this.last_update),this.last_update=t,this.render()}),16-e)):(void 0!==this.update_timer&&(clearTimeout(this.update_timer),this.update_timer=void 0),this.last_update=t,this.render())},this.render=function(){t.value=this.text,this.text_new_line&&(this.text_new_line=!1,t.scrollTop=1e9)},this.send_char=function(t){o.bus&&o.bus.send("serial0-input",t)}}function Ct(t,e){if(this.element=t,window.Terminal){var i=this.term=new window.Terminal;i.setOption("logLevel","off"),i.write("This is the serial console. Whatever you type or paste here will be sent to COM1"),i.onData((function(t){for(let i=0;i<t.length;i++)e.send("serial0-input",t.charCodeAt(i))})),e.register("serial0-output-char",(function(t){i.write(t)}),this)}}Ct.prototype.show=function(){this.term&&this.term.open(this.element)};function Tt(t){if("undefined"!=typeof window)if(window.AudioContext||window.webkitAudioContext){var e=window.AudioWorklet?Dt:Pt;this.bus=t,this.audio_context=window.AudioContext?new AudioContext:new webkitAudioContext,this.mixer=new qt(t,this.audio_context),this.pcspeaker=new St(t,this.audio_context,this.mixer),this.dac=new e(t,this.audio_context,this.mixer),this.pcspeaker.start(),t.register("emulator-stopped",(function(){this.audio_context.suspend()}),this),t.register("emulator-started",(function(){this.audio_context.resume()}),this),t.register("speaker-confirm-initialized",(function(){t.send("speaker-has-initialized")}),this),t.send("speaker-has-initialized")}else console.warn("Web browser doesn't support Web Audio API")}function qt(t,e){function i(t){return function(e){t.gain.setValueAtTime(e,this.audio_context.currentTime)}}this.audio_context=e,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),t.register("mixer-connect",(function(t){this.connect_source(t[0],t[1])}),this),t.register("mixer-disconnect",(function(t){this.disconnect_source(t[0],t[1])}),this),t.register("mixer-volume",(function(t){var e=t[0],i=t[1];t=Math.pow(10,t[2]/20),void 0!==(e=0===e?this:this.sources.get(e))&&e.set_volume(t,i)}),this),t.register("mixer-gain-left",(function(t){this.gain_left=Math.pow(10,t/20),this.update()}),this),t.register("mixer-gain-right",(function(t){this.gain_right=Math.pow(10,t/20),this.update()}),this),t.register("mixer-treble-left",i(this.node_treble_left),this),t.register("mixer-treble-right",i(this.node_treble_right),this),t.register("mixer-bass-left",i(this.node_bass_left),this),t.register("mixer-bass-right",i(this.node_bass_right),this)}function Lt(t,e,i,s){this.audio_context=t,this.connected_right=this.connected_left=!0,this.volume_right=this.volume_left=this.volume_both=this.gain_hidden=1,this.node_splitter=t.createChannelSplitter(2),this.node_gain_left=t.createGain(),this.node_gain_right=t.createGain(),e.connect(this.node_splitter),this.node_splitter.connect(this.node_gain_left,0),this.node_gain_left.connect(i),this.node_splitter.connect(this.node_gain_right,1),this.node_gain_right.connect(s)}function St(t,e,i){this.node_oscillator=e.createOscillator(),this.node_oscillator.type="square",this.node_oscillator.frequency.setValueAtTime(440,e.currentTime),this.mixer_connection=i.add_source(this.node_oscillator,1),this.mixer_connection.disconnect(),t.register("pcspeaker-enable",(function(){i.connect_source(1)}),this),t.register("pcspeaker-disable",(function(){i.disconnect_source(1)}),this),t.register("pcspeaker-update",(function(t){var i=t[1],s=0;3===t[0]&&(s=1e3*1193.1816666/i,s=Math.min(s,this.node_oscillator.frequency.maxValue),s=Math.max(s,0)),this.node_oscillator.frequency.setValueAtTime(s,e.currentTime)}),this)}function Dt(t,e,i){this.bus=t,this.audio_context=e,this.enabled=!1,this.sampling_rate=48e3;var s=(e=function(){function t(t){return 0===t?1:(t*=Math.PI,Math.sin(t)/t)}function e(){var t=Reflect.construct(AudioWorkletProcessor,[],e);return t.kernel_size=3,t.queue_data=Array(1024),t.queue_start=0,t.queue_end=0,t.queue_length=0,t.queue_size=t.queue_data.length,t.queued_samples=0,t.source_buffer_previous=i,t.source_buffer_current=i,t.source_samples_per_destination=1,t.source_block_start=0,t.source_time=0,t.source_offset=0,t.port.onmessage=e=>{switch(e.data.type){case"queue":t.queue_push(e.data.value);break;case"sampling-rate":t.source_samples_per_destination=e.data.value/sampleRate}},t}var i=[new Float32Array(256),new Float32Array(256)];Reflect.setPrototypeOf(e.prototype,AudioWorkletProcessor.prototype),Reflect.setPrototypeOf(e,AudioWorkletProcessor),e.prototype.process=e.prototype.process=function(t,e,i){for(t=0;t<e[0][0].length;t++){for(var s=i=0,r=this.source_offset+this.kernel_size,a=this.source_offset-this.kernel_size+1;a<=r;a++){var o=this.source_block_start+a;i+=this.get_sample(o,0)*this.kernel(this.source_time-a),s+=this.get_sample(o,1)*this.kernel(this.source_time-a)}(isNaN(i)||isNaN(s))&&(i=s=0,this.dbg_log("ERROR: NaN values! Ignoring for now.")),e[0][0][t]=i,e[0][1][t]=s,this.source_time+=this.source_samples_per_destination,this.source_offset=Math.floor(this.source_time)}return e=this.source_offset,e+=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(e),!0},e.prototype.kernel=function(e){return t(e)*t(e/this.kernel_size)},e.prototype.get_sample=function(t,e){return 0>t?(t+=this.source_buffer_previous[0].length,this.source_buffer_previous[e][t]):this.source_buffer_current[e][t]},e.prototype.ensure_enough_data=function(t){var e=this.source_buffer_current[0].length;e-this.source_block_start<t&&(this.prepare_next_buffer(),this.source_block_start-=e)},e.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 t=this.source_buffer_current[0].length;if(256>t){for(var e=this.queue_start,i=0;256>t&&i<this.queue_length;)t+=this.queue_data[e][0].length,e=e+1&this.queue_size-1,i++;t=Math.max(t,256),(t=[new Float32Array(t),new Float32Array(t)])[0].set(this.source_buffer_current[0]),t[1].set(this.source_buffer_current[1]),e=this.source_buffer_current[0].length;for(var s=0;s<i;s++){var r=this.queue_shift();t[0].set(r[0],e),t[1].set(r[1],e),e+=r[0].length}this.source_buffer_current=t}this.pump()},e.prototype.pump=function(){1024>this.queued_samples/this.source_samples_per_destination&&this.port.postMessage({type:"pump"})},e.prototype.queue_push=function(t){this.queue_length<this.queue_size&&(this.queue_data[this.queue_end]=t,this.queue_end=this.queue_end+1&this.queue_size-1,this.queue_length++,this.queued_samples+=t[0].length,this.pump())},e.prototype.queue_shift=function(){if(!this.queue_length)return i;var t=this.queue_data[this.queue_start];return this.queue_data[this.queue_start]=null,this.queue_start=this.queue_start+1&this.queue_size-1,this.queue_length--,this.queued_samples-=t[0].length,t},e.prototype.dbg_log=function(t){},registerProcessor("dac-processor",e)}.toString()).indexOf("{")+1,r=e.lastIndexOf("}");e=e.substring(s,r),e=new Blob([e],{type:"application/javascript"});var a=URL.createObjectURL(e);this.node_processor=null,this.node_output=this.audio_context.createGain(),this.audio_context.audioWorklet.addModule(a).then((()=>{URL.revokeObjectURL(a),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=t=>{switch(t.data.type){case"pump":this.pump();break;case"debug-log":_("SpeakerWorkletDAC - Worklet: "+t.data.value)}},this.node_processor.connect(this.node_output)})),this.mixer_connection=i.add_source(this.node_output,2),this.mixer_connection.set_gain_hidden(3),t.register("dac-send-data",(function(t){this.queue(t)}),this),t.register("dac-enable",(function(t){this.enabled=!0}),this),t.register("dac-disable",(function(){this.enabled=!1}),this),t.register("dac-tell-sampling-rate",(function(t){this.sampling_rate=t,this.node_processor&&this.node_processor.port.postMessage({type:"sampling-rate",value:t})}),this)}function Pt(t,e,i){this.bus=t,this.audio_context=e,this.enabled=!1,this.sampling_rate=22050,this.buffered_time=0,this.rate_ratio=1,this.node_lowpass=this.audio_context.createBiquadFilter(),this.node_lowpass.type="lowpass",this.node_output=this.node_lowpass,this.mixer_connection=i.add_source(this.node_output,2),this.mixer_connection.set_gain_hidden(3),t.register("dac-send-data",(function(t){this.queue(t)}),this),t.register("dac-enable",(function(t){this.enabled=!0,this.pump()}),this),t.register("dac-disable",(function(){this.enabled=!1}),this),t.register("dac-tell-sampling-rate",(function(t){this.sampling_rate=t,this.rate_ratio=Math.ceil(8e3/t),this.node_lowpass.frequency.setValueAtTime(t/2,this.audio_context.currentTime)}),this)}function zt(t){this.cpu_is_running=!1;var e,i,s=r.create();this.bus=s[0],this.emulator_bus=s[1];let a=new WebAssembly.Table({element:"anyfunc",initial:1924});s={cpu_exception_hook:t=>this.cpu_exception_hook&&this.cpu_exception_hook(t),hlt_op:function(){return e.hlt_op()},abort:function(){},microtick:O.microtick,get_rand_int:function(){return c.get_rand_int()},pic_acknowledge:function(){e.pic_acknowledge()},io_port_read8:function(t){return e.io.port_read8(t)},io_port_read16:function(t){return e.io.port_read16(t)},io_port_read32:function(t){return e.io.port_read32(t)},io_port_write8:function(t,i){e.io.port_write8(t,i)},io_port_write16:function(t,i){e.io.port_write16(t,i)},io_port_write32:function(t,i){e.io.port_write32(t,i)},mmap_read8:function(t){return e.mmap_read8(t)},mmap_read16:function(t){return e.mmap_read16(t)},mmap_read32:function(t){return e.mmap_read32(t)},mmap_write8:function(t,i){e.mmap_write8(t,i)},mmap_write16:function(t,i){e.mmap_write16(t,i)},mmap_write32:function(t,i){e.mmap_write32(t,i)},mmap_write64:function(t,i,s){e.mmap_write64(t,i,s)},mmap_write128:function(t,i,s,r,a){e.mmap_write128(t,i,s,r,a)},log_from_wasm:function(t,e){t=c.read_sized_string_from_mem(i,t,e),_(t,LOG_CPU)},console_log_from_wasm:function(t,e){t=c.read_sized_string_from_mem(i,t,e),console.error(t)},dbg_trace_from_wasm:function(){},codegen_finalize:(t,i,s,r,a)=>{e.codegen_finalize(t,i,s,r,a)},jit_clear_func:t=>e.jit_clear_func(t),jit_clear_all_funcs:()=>e.jit_clear_all_funcs(),__indirect_function_table:a};let o=t.wasm_fn;o||(o=e=>new Promise((i=>{let s="v86.wasm",r="v86-fallback.wasm";if(t.wasm_path){s=t.wasm_path;let e=s.lastIndexOf("/");r=(-1===e?"":s.substr(0,e))+"/"+r}else"undefined"==typeof window&&"string"==typeof __dirname?(s=__dirname+"/"+s,r=__dirname+"/"+r):(s="build/"+s,r="build/"+r);c.load_file(s,{done:t=>{WebAssembly.instantiate(t,e).then((({instance:t})=>{i(t.exports)}),(t=>{c.load_file(r,{done:t=>{WebAssembly.instantiate(t,e).then((({instance:t})=>{i(t.exports)}))}})}))},progress:t=>{this.emulator_bus.send("download-progress",{file_index:0,file_count:1,file_name:s,lengthComputable:t.lengthComputable,total:t.total,loaded:t.loaded})}})}))),o({env:s}).then((s=>{i=s.memory,s.rust_init(),s=this.v86=new O(this.emulator_bus,{exports:s,wasm_table:a}),e=s.cpu,this.continue_init(s,t)}))}function Mt(t){this.message=t||"File already exists"}function Ut(t){this.message=t||"File not found"}qt.prototype.add_source=function(t,e){return t=new Lt(this.audio_context,t,this.input_left,this.input_right),this.sources.has(e),this.sources.set(e,t),t},qt.prototype.connect_source=function(t,e){void 0!==(t=this.sources.get(t))&&t.connect(e)},qt.prototype.disconnect_source=function(t,e){void 0!==(t=this.sources.get(t))&&t.disconnect(e)},qt.prototype.set_volume=function(t,e){switch(void 0===e&&(e=2),e){case 0:this.volume_left=t;break;case 1:this.volume_right=t;break;case 2:this.volume_both=t;break;default:return}this.update()},qt.prototype.update=function(){var t=this.volume_both*this.volume_right*this.gain_right;this.node_gain_left.gain.setValueAtTime(this.volume_both*this.volume_left*this.gain_left,this.audio_context.currentTime),this.node_gain_right.gain.setValueAtTime(t,this.audio_context.currentTime)},Lt.prototype.update=function(){var t=this.connected_right*this.gain_hidden*this.volume_both*this.volume_right;this.node_gain_left.gain.setValueAtTime(this.connected_left*this.gain_hidden*this.volume_both*this.volume_left,this.audio_context.currentTime),this.node_gain_right.gain.setValueAtTime(t,this.audio_context.currentTime)},Lt.prototype.connect=function(t){var e=!t||2===t;(e||0===t)&&(this.connected_left=!0),(e||1===t)&&(this.connected_right=!0),this.update()},Lt.prototype.disconnect=function(t){var e=!t||2===t;(e||0===t)&&(this.connected_left=!1),(e||1===t)&&(this.connected_right=!1),this.update()},Lt.prototype.set_volume=function(t,e){switch(void 0===e&&(e=2),e){case 0:this.volume_left=t;break;case 1:this.volume_right=t;break;case 2:this.volume_both=t;break;default:return}this.update()},Lt.prototype.set_gain_hidden=function(t){this.gain_hidden=t},St.prototype.start=function(){this.node_oscillator.start()},Dt.prototype.queue=function(t){this.node_processor&&this.node_processor.port.postMessage({type:"queue",value:t},[t[0].buffer,t[1].buffer])},Dt.prototype.pump=function(){this.enabled&&this.bus.send("dac-request-data")},Pt.prototype.queue=function(t){var e=t[0].length,i=e/this.sampling_rate;if(1<this.rate_ratio)for(var s=this.audio_context.createBuffer(2,e*this.rate_ratio,this.sampling_rate*this.rate_ratio),r=s.getChannelData(0),a=s.getChannelData(1),o=0,n=0;n<e;n++)for(var h=0;h<this.rate_ratio;h++,o++)r[o]=t[0][n],a[o]=t[1][n];else(s=this.audio_context.createBuffer(2,e,this.sampling_rate)).copyToChannel?(s.copyToChannel(t[0],0),s.copyToChannel(t[1],1)):(s.getChannelData(0).set(t[0]),s.getChannelData(1).set(t[1]));if((t=this.audio_context.createBufferSource()).buffer=s,t.connect(this.node_lowpass),t.addEventListener("ended",this.pump.bind(this)),s=this.audio_context.currentTime,this.buffered_time<s)for(_("Speaker DAC - Creating/Recreating reserve - shouldn't occur frequently during playback"),this.buffered_time=s,s=.2-i,e=0;e<=s;)e+=i,this.buffered_time+=i,setTimeout((()=>this.pump()),1e3*e);t.start(this.buffered_time),this.buffered_time+=i,setTimeout((()=>this.pump()),0)},Pt.prototype.pump=function(){this.enabled&&(this.buffered_time-this.audio_context.currentTime>.2||this.bus.send("dac-request-data"))},zt.prototype.continue_init=async function(t,e){function i(t,e){switch(t){case"hda":r.hda=this.disk_images.hda=e;break;case"hdb":r.hdb=this.disk_images.hdb=e;break;case"cdrom":r.cdrom=this.disk_images.cdrom=e;break;case"fda":r.fda=this.disk_images.fda=e;break;case"fdb":r.fdb=this.disk_images.fdb=e;break;case"multiboot":r.multiboot=this.disk_images.multiboot=e.buffer;break;case"bzimage":r.bzimage=this.disk_images.bzimage=e.buffer;break;case"initrd":r.initrd=this.disk_images.initrd=e.buffer;break;case"bios":r.bios=e.buffer;break;case"vga_bios":r.vga_bios=e.buffer;break;case"initial_state":r.initial_state=e.buffer;break;case"fs9p_json":r.fs9p_json=e}}function s(){function s(){this.serial_adapter&&this.serial_adapter.show&&this.serial_adapter.show(),this.bus.send("cpu-init",r),r.initial_state&&(t.restore_state(r.initial_state),r.initial_state=void 0),e.autostart&&this.bus.send("cpu-run"),this.emulator_bus.send("emulator-loaded")}if(r.fs9p&&r.fs9p_json)if(r.initial_state?_("Filesystem basefs ignored: Overridden by state image"):r.fs9p.load_from_json(r.fs9p_json),e.bzimage_initrd_from_filesystem){let{bzimage:t,initrd:e}=this.get_bzimage_initrd_from_filesystem(r.fs9p);_("Found bzimage: "+t+" and initrd: "+e),Promise.all([r.fs9p.read_file(e),r.fs9p.read_file(t)]).then((([t,e])=>{i.call(this,"initrd",new u(t.buffer)),i.call(this,"bzimage",new u(e.buffer)),s.call(this)}))}else s.call(this);else console.assert(!e.bzimage_initrd_from_filesystem,"bzimage_initrd_from_filesystem: Requires a filesystem"),s.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 r={};this.disk_images={fda:void 0,fdb:void 0,hda:void 0,hdb:void 0,cdrom:void 0},r.acpi=e.acpi,r.load_devices=!0,r.log_level=e.log_level,r.memory_size=e.memory_size||67108864,r.vga_memory_size=e.vga_memory_size||8388608,r.boot_order=e.boot_order||531,r.fastboot=e.fastboot||!1,r.fda=void 0,r.fdb=void 0,r.uart1=e.uart1,r.uart2=e.uart2,r.uart3=e.uart3,r.cmdline=e.cmdline,r.preserve_mac_from_state_image=e.preserve_mac_from_state_image,e.network_adapter?this.network_adapter=e.network_adapter(this.bus):e.network_relay_url&&(this.network_adapter=new kt(e.network_relay_url,this.bus)),r.enable_ne2k=!0,e.disable_keyboard||(this.keyboard_adapter=new At(this.bus)),e.disable_mouse||(this.mouse_adapter=new It(this.bus,e.screen_container)),e.screen_container?this.screen_adapter=new Ot(e.screen_container,this.bus):e.screen_dummy&&(this.screen_adapter=new Rt(this.bus)),e.serial_container&&(this.serial_adapter=new Et(e.serial_container,this.bus)),e.serial_container_xtermjs&&(this.serial_adapter=new Ct(e.serial_container_xtermjs,this.bus)),e.disable_speaker||(this.speaker_adapter=new Tt(this.bus));var a,o,n=[];e.state&&console.warn("Warning: Unknown option 'state'. Did you mean 'initial_state'?");for(var h="bios vga_bios cdrom hda hdb fda fdb initial_state multiboot bzimage initrd".split(" "),d=0;d<h.length;d++)a=h[d],(o=e[h[d]])&&(o.get&&o.set&&o.load?n.push({name:a,loadable:o}):(o={buffer:o.buffer,async:o.async,url:o.url,size:o.size,fixed_chunk_size:o.fixed_chunk_size,use_parts:o.use_parts},"bios"!==a&&"vga_bios"!==a&&"initial_state"!==a&&"multiboot"!==a&&"bzimage"!==a&&"initrd"!==a||(o.async=!1),o.buffer instanceof ArrayBuffer?(o=new u(o.buffer),n.push({name:a,loadable:o})):"undefined"!=typeof File&&o.buffer instanceof File?(void 0===o.async&&(o.async=268435456<=o.buffer.size),o=o.async?new c.AsyncFileBuffer(o.buffer):new c.SyncFileBuffer(o.buffer),n.push({name:a,loadable:o})):o.url?o.async?(o=o.use_parts?new c.AsyncXHRPartfileBuffer(o.url,o.size,o.fixed_chunk_size):new c.AsyncXHRBuffer(o.url,o.size),n.push({name:a,loadable:o})):n.push({name:a,url:o.url,size:o.size}):_("Ignored file: url="+o.url+" buffer="+o.buffer)));if(e.filesystem){h=e.filesystem.basefs,d=e.filesystem.baseurl;let t=new MemoryFileStorage;if(d&&(t=new ServerFileStorageWrapper(t,d)),r.fs9p=this.fs9p=new FS(t),h){if(console.assert(d,"Filesystem: baseurl must be specified"),"object"==typeof h){var p=h.size;h=h.url}n.push({name:"fs9p_json",url:h,size:p,as_json:!0})}}var l=this,f=n.length,m=function(t){if(t===f)setTimeout(s.bind(this),0);else{var e=n[t];e.loadable?(e.loadable.onload=function(s){i.call(this,e.name,e.loadable),m(t+1)}.bind(this),e.loadable.load()):c.load_file(e.url,{done:function(s){i.call(this,e.name,e.as_json?s:new u(s)),m(t+1)}.bind(this),progress:function(i){200===i.target.status?l.emulator_bus.send("download-progress",{file_index:t,file_count:f,file_name:e.url,lengthComputable:i.lengthComputable,total:i.total||e.size,loaded:i.loaded}):l.emulator_bus.send("download-error",{file_index:t,file_count:f,file_name:e.url,request:i.target})},as_json:e.as_json})}}.bind(this);m(0)},zt.prototype.get_bzimage_initrd_from_filesystem=function(t){let e,i,s=(t.read_dir("/")||[]).map((t=>"/"+t));t=(t.read_dir("/boot/")||[]).map((t=>"/boot/"+t));for(let r of[].concat(s,t)){let t=/old/i.test(r)||/fallback/i.test(r),s=/vmlinuz/i.test(r)||/bzimage/i.test(r),a=/initrd/i.test(r)||/initramfs/i.test(r);!s||i&&t||(i=r),!a||e&&t||(e=r)}return e&&i||(console.log("Failed to find bzimage or initrd in filesystem. Files:"),console.log(s.join(" ")),console.log(t.join(" "))),{initrd:e,bzimage:i}},zt.prototype.run=function(){this.bus.send("cpu-run")},d.exportProperty(zt.prototype,"run",zt.prototype.run),zt.prototype.stop=function(){this.bus.send("cpu-stop")},d.exportProperty(zt.prototype,"stop",zt.prototype.stop),zt.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()},d.exportProperty(zt.prototype,"destroy",zt.prototype.destroy),zt.prototype.restart=function(){this.bus.send("cpu-restart")},d.exportProperty(zt.prototype,"restart",zt.prototype.restart),zt.prototype.add_listener=function(t,e){this.bus.register(t,e,this)},d.exportProperty(zt.prototype,"add_listener",zt.prototype.add_listener),zt.prototype.remove_listener=function(t,e){this.bus.unregister(t,e)},d.exportProperty(zt.prototype,"remove_listener",zt.prototype.remove_listener),zt.prototype.restore_state=function(t){this.v86.restore_state(t)},d.exportProperty(zt.prototype,"restore_state",zt.prototype.restore_state),zt.prototype.save_state=function(t){setTimeout(function(){try{t(null,this.v86.save_state())}catch(e){t(e,null)}}.bind(this),0)},d.exportProperty(zt.prototype,"save_state",zt.prototype.save_state),zt.prototype.get_statistics=function(){console.warn("V86Starter.prototype.get_statistics is deprecated. Use events instead.");var t={cpu:{instruction_counter:this.get_instruction_counter()}};if(!this.v86)return t;var e=this.v86.cpu.devices;return e.hda&&(t.hda=e.hda.stats),e.cdrom&&(t.cdrom=e.cdrom.stats),e.ps2&&(t.mouse={enabled:e.ps2.use_mouse}),e.vga&&(t.vga={is_graphical:e.vga.stats.is_graphical}),t},d.exportProperty(zt.prototype,"get_statistics",zt.prototype.get_statistics),zt.prototype.get_instruction_counter=function(){return this.v86?this.v86.cpu.instruction_counter[0]>>>0:0},d.exportProperty(zt.prototype,"get_instruction_counter",zt.prototype.get_instruction_counter),zt.prototype.is_running=function(){return this.cpu_is_running},d.exportProperty(zt.prototype,"is_running",zt.prototype.is_running),zt.prototype.keyboard_send_scancodes=function(t){for(var e=0;e<t.length;e++)this.bus.send("keyboard-code",t[e])},d.exportProperty(zt.prototype,"keyboard_send_scancodes",zt.prototype.keyboard_send_scancodes),zt.prototype.keyboard_send_keys=function(t){for(var e=0;e<t.length;e++)this.keyboard_adapter.simulate_press(t[e])},d.exportProperty(zt.prototype,"keyboard_send_keys",zt.prototype.keyboard_send_keys),zt.prototype.keyboard_send_text=function(t){for(var e=0;e<t.length;e++)this.keyboard_adapter.simulate_char(t[e])},d.exportProperty(zt.prototype,"keyboard_send_text",zt.prototype.keyboard_send_text),zt.prototype.screen_make_screenshot=function(){this.screen_adapter&&this.screen_adapter.make_screenshot()},d.exportProperty(zt.prototype,"screen_make_screenshot",zt.prototype.screen_make_screenshot),zt.prototype.screen_set_scale=function(t,e){this.screen_adapter&&this.screen_adapter.set_scale(t,e)},d.exportProperty(zt.prototype,"screen_set_scale",zt.prototype.screen_set_scale),zt.prototype.screen_go_fullscreen=function(){if(this.screen_adapter){var t=document.getElementById("screen_container");if(t){var e=t.requestFullScreen||t.webkitRequestFullscreen||t.mozRequestFullScreen||t.msRequestFullScreen;e&&(e.call(t),(t=document.getElementsByClassName("phone_keyboard")[0])&&t.focus());try{navigator.keyboard.lock()}catch(t){}this.lock_mouse()}}},d.exportProperty(zt.prototype,"screen_go_fullscreen",zt.prototype.screen_go_fullscreen),zt.prototype.lock_mouse=function(){var t=document.body,e=t.requestPointerLock||t.mozRequestPointerLock||t.webkitRequestPointerLock;e&&e.call(t)},d.exportProperty(zt.prototype,"lock_mouse",zt.prototype.lock_mouse),zt.prototype.mouse_set_status=function(t){this.mouse_adapter&&(this.mouse_adapter.emu_enabled=t)},zt.prototype.keyboard_set_status=function(t){this.keyboard_adapter&&(this.keyboard_adapter.emu_enabled=t)},d.exportProperty(zt.prototype,"keyboard_set_status",zt.prototype.keyboard_set_status),zt.prototype.serial0_send=function(t){for(var e=0;e<t.length;e++)this.bus.send("serial0-input",t.charCodeAt(e))},d.exportProperty(zt.prototype,"serial0_send",zt.prototype.serial0_send),zt.prototype.serial_send_bytes=function(t,e){for(var i=0;i<e.length;i++)this.bus.send("serial"+t+"-input",e[i])},d.exportProperty(zt.prototype,"serial_send_bytes",zt.prototype.serial_send_bytes),zt.prototype.mount_fs=async function(t,e,i,s){let r=new MemoryFileStorage;e&&(r=new ServerFileStorageWrapper(r,e));let a=new FS(r,this.fs9p.qidcounter),o=()=>{const e=this.fs9p.Mount(t,a);s&&(e===-ENOENT?s(new Ut):e===-EEXIST?s(new Mt):s(0>e?Error("Failed to mount. Error number: "+-e):null))};e?a.load_from_json(i,(()=>o())):o()},d.exportProperty(zt.prototype,"mount_fs",zt.prototype.mount_fs),zt.prototype.create_file=function(t,e,i){i=i||function(){};var s=this.fs9p;if(s){var r=t.split("/");r=r[r.length-1],t=s.SearchPath(t).parentid,""!==r&&-1!==t?s.CreateBinaryFile(r,t,e).then((()=>i(null))):setTimeout((function(){i(new Ut)}),0)}},d.exportProperty(zt.prototype,"create_file",zt.prototype.create_file),zt.prototype.read_file=function(t,e){var i=this.fs9p;i&&i.read_file(t).then((t=>{t?e(null,t):e(new Ut,null)}))},d.exportProperty(zt.prototype,"read_file",zt.prototype.read_file),zt.prototype.automatically=function(t){let e=t=>{const i=t[0];if(i){var s=t.slice(1);if(i.sleep)setTimeout((()=>e(s)),1e3*i.sleep);else if(i.vga_text){const r=this.screen_adapter.get_text_screen();for(let t of r)if(t.includes(i.vga_text))return void e(s);setTimeout((()=>e(t)),1e3)}else i.keyboard_send?(i.keyboard_send instanceof Array?this.keyboard_send_scancodes(i.keyboard_send):(i.keyboard_send,this.keyboard_send_text(i.keyboard_send)),e(s)):i.call?(i.call(),e(s)):console.assert(!1,i)}};e(t)},zt.prototype.read_memory=function(t,e){return this.v86.cpu.read_blob(t,e)},zt.prototype.write_memory=function(t,e){this.v86.cpu.write_blob(t,e)},Mt.prototype=Error.prototype,Ut.prototype=Error.prototype,"undefined"!=typeof window?(window.V86Starter=zt,window.V86=zt):"undefined"!=typeof module&&void 0!==module.exports?(module.exports.V86Starter=zt,module.exports.V86=zt):"function"==typeof importScripts&&(self.V86Starter=zt,self.V86=zt),function(){function t(t,e){this.filename=t,this.block_size=256,this.byteLength=e,this.loaded_blocks=Object.create(null),this.onprogress=this.onload=void 0}function e(t,e,i){let s=t.match(/(.*)(\..*)/);s?(this.basename=s[1],this.extension=s[2]):(this.basename=t,this.extension=""),this.block_size=256,this.byteLength=e,this.use_fixed_chunk_size="number"==typeof i,this.fixed_chunk_size=i,this.loaded_blocks=Object.create(null),this.onprogress=this.onload=void 0}function i(t){this.file=t,this.byteLength=t.size,1073741824<t.size&&console.warn("SyncFileBuffer: Allocating buffer of "+(t.size>>20)+" MB ..."),this.buffer=new ArrayBuffer(t.size),this.onprogress=this.onload=void 0}function s(t){this.file=t,this.byteLength=t.size,this.block_size=256,this.loaded_blocks=Object.create(null),this.onprogress=this.onload=void 0}c.load_file="undefined"==typeof XMLHttpRequest?load_file_nodejs:function t(e,i,s){function r(){let r=s||0;setTimeout((()=>{t(e,i,r+1)}),1e3*([1,1,2,3,5,8,13,21][r]||34))}var a=new XMLHttpRequest;if(a.open(i.method||"get",e,!0),a.responseType=i.as_json?"json":"arraybuffer",i.headers)for(var o=Object.keys(i.headers),_=0;_<o.length;_++){var n=o[_];a.setRequestHeader(n,i.headers[n])}i.range&&(o=i.range.start,a.setRequestHeader("Range","bytes="+o+"-"+(o+i.range.length-1)),a.onreadystatechange=function(){200===a.status&&a.abort()}),a.onload=function(t){4===a.readyState&&(200!==a.status&&206!==a.status?(console.error("Loading the image "+e+" failed (status %d)",a.status),500<=a.status&&600>a.status&&r()):a.response&&i.done&&i.done(a.response,a))},a.onerror=function(t){console.error("Loading the image "+e+" failed",t),r()},i.progress&&(a.onprogress=function(t){i.progress(t)}),a.send(null)},c.AsyncXHRBuffer=t,c.AsyncXHRPartfileBuffer=e,c.AsyncFileBuffer=s,c.SyncFileBuffer=i,c.read_sized_string_from_mem=function(t,e,i){return String.fromCharCode(...new Uint8Array(t.buffer,e>>>0,i>>>0))};t.prototype.load=function(){var t,e;void 0!==this.byteLength?this.onload&&this.onload(Object.create(null)):(t=this.filename,e=(t,e)=>{if(t)throw Error("Cannot use: "+this.filename+". "+t);dbg_assert(0<=e),this.byteLength=e,this.onload&&this.onload(Object.create(null))},c.load_file(t,{done:(t,i)=>{t=i.getResponseHeader("Content-Range")||"",(i=t.match(/\/(\d+)\s*$/))?e(null,+i[1]):e("`Range: bytes=...` header not supported (Got `"+t+"`)")},headers:{Range:"bytes=0-0"}}))},t.prototype.get_from_cache=function(t,e,i){i=e/this.block_size,t/=this.block_size;for(var s=0;s<i;s++)if(!this.loaded_blocks[t+s])return;if(1===i)return this.loaded_blocks[t];for(e=new Uint8Array(e),s=0;s<i;s++)e.set(this.loaded_blocks[t+s],s*this.block_size);return e},t.prototype.get=function(t,e,i){console.assert(t+e<=this.byteLength),console.assert(0==t%this.block_size),console.assert(0==e%this.block_size),console.assert(e);var s=this.get_from_cache(t,e,i);s?i(s):c.load_file(this.filename,{done:function(s){s=new Uint8Array(s),this.handle_read(t,e,s),i(s)}.bind(this),range:{start:t,length:e}})},t.prototype.set=function(t,e,i){console.assert(t+e.byteLength<=this.byteLength);var s=e.length;console.assert(0==t%this.block_size),console.assert(0==s%this.block_size),console.assert(s),t/=this.block_size,s/=this.block_size;for(var r=0;r<s;r++){var a=this.loaded_blocks[t+r];void 0===a&&(a=this.loaded_blocks[t+r]=new Uint8Array(this.block_size));var o=e.subarray(r*this.block_size,(r+1)*this.block_size);a.set(o),console.assert(a.byteLength===o.length)}i()},t.prototype.handle_read=function(t,e,i){t/=this.block_size,e/=this.block_size;for(var s=0;s<e;s++){var r=this.loaded_blocks[t+s];r&&i.set(r,s*this.block_size)}},t.prototype.get_buffer=function(t){t()},t.prototype.get_written_blocks=function(){var t=Object.keys(this.loaded_blocks).length;t=new Uint8Array(t*this.block_size);var e=[],i=0;for(r of Object.keys(this.loaded_blocks)){var s=this.loaded_blocks[r];dbg_assert(s.length===this.block_size);var r=+r;e.push(r),t.set(s,i*this.block_size),i++}return{buffer:t,indices:e,block_size:this.block_size}},t.prototype.get_state=function(){let t=[],e=[];for(let[t,i]of Object.entries(this.loaded_blocks))dbg_assert(isFinite(+t)),e.push([+t,i]);return t[0]=e,t},t.prototype.set_state=function(t){t=t[0],this.loaded_blocks=Object.create(null);for(let[e,i]of Object.values(t))this.loaded_blocks[e]=i},e.prototype.load=function(){void 0===this.byteLength&&dbg_assert(!1),this.onload&&this.onload(Object.create(null))},e.prototype.get_from_cache=t.prototype.get_from_cache,e.prototype.get=function(t,e,i){console.assert(t+e<=this.byteLength),console.assert(0==t%this.block_size),console.assert(0==e%this.block_size),console.assert(e);var s=this.get_from_cache(t,e,i);if(s)i(s);else if(this.use_fixed_chunk_size){s=Math.floor(t/this.fixed_chunk_size);let r=t-s*this.fixed_chunk_size;dbg_assert(0<=r);let a=Math.floor(e/this.fixed_chunk_size)+(0===r?1:2),o=new Uint8Array(r+a*this.fixed_chunk_size),_=0;for(let n=0;n<a;n++){let h=this.basename+"-"+(s+n+"").padStart(8,"0")+this.extension;c.load_file(h,{done:function(s){var h=n*this.fixed_chunk_size;s=new Uint8Array(s),o.set(s,h),_++,_===a&&(h=o.subarray(r,r+e),this.handle_read(t,e,h),i(h))}.bind(this)})}}else c.load_file(this.basename+"-"+t+"-"+(t+e)+this.extension,{done:function(s){dbg_assert(s.byteLength===e),s=new Uint8Array(s),this.handle_read(t,e,s),i(s)}.bind(this)})},e.prototype.set=t.prototype.set,e.prototype.handle_read=t.prototype.handle_read,e.prototype.get_written_blocks=t.prototype.get_written_blocks,e.prototype.get_state=t.prototype.get_state,e.prototype.set_state=t.prototype.set_state,i.prototype.load=function(){this.load_next(0)},i.prototype.load_next=function(t){var e=new FileReader;if(e.onload=function(e){e=new Uint8Array(e.target.result),new Uint8Array(this.buffer,t).set(e),this.load_next(t+4194304)}.bind(this),this.onprogress&&this.onprogress({loaded:t,total:this.byteLength,lengthComputable:!0}),t<this.byteLength){var i=this.file.slice(t,Math.min(t+4194304,this.byteLength));e.readAsArrayBuffer(i)}else this.file=void 0,this.onload&&this.onload({buffer:this.buffer})},i.prototype.get=function(t,e,i){console.assert(t+e<=this.byteLength),i(new Uint8Array(this.buffer,t,e))},i.prototype.set=function(t,e,i){console.assert(t+e.byteLength<=this.byteLength),new Uint8Array(this.buffer,t,e.byteLength).set(e),i()},i.prototype.get_buffer=function(t){t(this.buffer)},i.prototype.get_state=function(){let t=[];return t[0]=this.byteLength,t[1]=new Uint8Array(this.buffer),t},i.prototype.set_state=function(t){this.byteLength=t[0],this.buffer=t[1].slice().buffer},s.prototype.load=function(){this.onload&&this.onload(Object.create(null))},s.prototype.get=function(t,e,i){console.assert(0==t%this.block_size),console.assert(0==e%this.block_size),console.assert(e);var s=this.get_from_cache(t,e,i);s?i(s):((s=new FileReader).onload=function(s){s=new Uint8Array(s.target.result),this.handle_read(t,e,s),i(s)}.bind(this),s.readAsArrayBuffer(this.file.slice(t,t+e)))},s.prototype.get_from_cache=t.prototype.get_from_cache,s.prototype.set=t.prototype.set,s.prototype.handle_read=t.prototype.handle_read,s.prototype.get_state=t.prototype.get_state,s.prototype.get_buffer=function(t){t()},s.prototype.get_as_file=function(t){for(var e=[],i=Object.keys(this.loaded_blocks).map(Number).sort((function(t,e){return t-e})),s=0,r=0;r<i.length;r++){var a=i[r],o=this.loaded_blocks[a];a*=this.block_size,console.assert(a>=s),a!==s&&(e.push(this.file.slice(s,a)),s=a),e.push(o),s+=o.length}return s!==this.file.size&&e.push(this.file.slice(s)),t=new File(e,t),console.assert(t.size===this.file.size),t}}(),exports.ACPI=t,exports.APIC=s,exports.APIC_ADDRESS=4276092928,exports.APIC_LOG_VERBOSE=!1,exports.APIC_TIMER_FREQ=1e6,exports.APIC_TIMER_MODE_MASK=393216,exports.APIC_TIMER_MODE_ONE_SHOT=0,exports.APIC_TIMER_MODE_PERIODIC=131072,exports.APIC_TIMER_MODE_TSC=262144,exports.Bus=r,exports.BusConnector=a,exports.ByteQueue=l,exports.CDROM_SECTOR_SIZE=2048,exports.CMOS_BIOS_BOOTFLAG1=56,exports.CMOS_BIOS_BOOTFLAG2=61,exports.CMOS_BIOS_DISKTRANSFLAG=57,exports.CMOS_BIOS_SMP_COUNT=95,exports.CMOS_CENTURY=50,exports.CMOS_DISK_DATA=18,exports.CMOS_DISK_DRIVE1_CYL=27,exports.CMOS_DISK_DRIVE1_TYPE=25,exports.CMOS_DISK_DRIVE2_CYL=36,exports.CMOS_DISK_DRIVE2_TYPE=26,exports.CMOS_EQUIPMENT_INFO=20,exports.CMOS_FLOPPY_DRIVE_TYPE=16,exports.CMOS_MEM_BASE_HIGH=22,exports.CMOS_MEM_BASE_LOW=21,exports.CMOS_MEM_EXTMEM2_HIGH=53,exports.CMOS_MEM_EXTMEM2_LOW=52,exports.CMOS_MEM_EXTMEM_HIGH=49,exports.CMOS_MEM_EXTMEM_LOW=48,exports.CMOS_MEM_HIGHMEM_HIGH=93,exports.CMOS_MEM_HIGHMEM_LOW=91,exports.CMOS_MEM_HIGHMEM_MID=92,exports.CMOS_MEM_OLD_EXT_HIGH=24,exports.CMOS_MEM_OLD_EXT_LOW=23,exports.CMOS_RESET_CODE=15,exports.CMOS_RTC_DAY_MONTH=7,exports.CMOS_RTC_DAY_WEEK=6,exports.CMOS_RTC_HOURS=4,exports.CMOS_RTC_HOURS_ALARM=5,exports.CMOS_RTC_MINUTES=2,exports.CMOS_RTC_MINUTES_ALARM=3,exports.CMOS_RTC_MONTH=8,exports.CMOS_RTC_SECONDS=0,exports.CMOS_RTC_SECONDS_ALARM=1,exports.CMOS_RTC_YEAR=9,exports.CMOS_STATUS_A=10,exports.CMOS_STATUS_B=11,exports.CMOS_STATUS_C=12,exports.CMOS_STATUS_D=13,exports.CPU=it,exports.CPU_LOG_VERBOSE=!1,exports.CR0_PG=-2147483648,exports.CR4_PAE=32,exports.CircularQueue=m,exports.DEBUG=false,exports.DEBUG_SCREEN_LAYERS=false,exports.DELIVERY_MODES=e,exports.DESTINATION_MODES=i,exports.DLAB=128,exports.DMA=b,exports.DSP_BUFSIZE=64,exports.DSP_COMMAND_HANDLERS=D,exports.DSP_COMMAND_SIZES=S,exports.DSP_COPYRIGHT=L,exports.DSP_DACSIZE=65536,exports.DSP_NO_COMMAND=0,exports.DUMP_GENERATED_WASM=!1,exports.DUMP_UNCOMPILED_ASSEMBLY=!1,exports.E8390_CMD=0,exports.ELF_MAGIC=1179403647,exports.EN0_BOUNDARY=3,exports.EN0_CLDAHI=2,exports.EN0_CLDALO=1,exports.EN0_COUNTER0=13,exports.EN0_COUNTER1=14,exports.EN0_COUNTER2=15,exports.EN0_CRDAHI=9,exports.EN0_CRDALO=8,exports.EN0_DCFG=14,exports.EN0_FIFO=6,exports.EN0_IMR=15,exports.EN0_ISR=7,exports.EN0_NCR=5,exports.EN0_RCNTHI=11,exports.EN0_RCNTLO=10,exports.EN0_RSARHI=9,exports.EN0_RSARLO=8,exports.EN0_RSR=12,exports.EN0_RXCR=12,exports.EN0_STARTPG=1,exports.EN0_STOPPG=2,exports.EN0_TCNTHI=6,exports.EN0_TCNTLO=5,exports.EN0_TPSR=4,exports.EN0_TSR=4,exports.EN0_TXCR=13,exports.ENABLE_HPET=false,exports.ENISR_ALL=63,exports.ENISR_COUNTERS=32,exports.ENISR_OVER=16,exports.ENISR_RDC=64,exports.ENISR_RESET=128,exports.ENISR_RX=1,exports.ENISR_RX_ERR=4,exports.ENISR_TX=2,exports.ENISR_TX_ERR=8,exports.ENRSR_RXOK=1,exports.FLAGS_DEFAULT=2,exports.FLAG_AC=262144,exports.FLAG_ADJUST=16,exports.FLAG_CARRY=1,exports.FLAG_DIRECTION=1024,exports.FLAG_ID=2097152,exports.FLAG_INTERRUPT=512,exports.FLAG_IOPL=12288,exports.FLAG_NT=16384,exports.FLAG_OVERFLOW=2048,exports.FLAG_PARITY=4,exports.FLAG_RF=65536,exports.FLAG_SIGN=128,exports.FLAG_TRAP=256,exports.FLAG_VIF=524288,exports.FLAG_VIP=1048576,exports.FLAG_VM=131072,exports.FLAG_ZERO=64,exports.FM_HANDLERS=U,exports.FW_CFG_CUSTOM_START=32768,exports.FW_CFG_FILE_DIR=25,exports.FW_CFG_FILE_START=49152,exports.FW_CFG_ID=1,exports.FW_CFG_MAX_CPUS=15,exports.FW_CFG_NB_CPUS=5,exports.FW_CFG_NUMA=13,exports.FW_CFG_RAM_SIZE=3,exports.FW_CFG_SIGNATURE=0,exports.FW_CFG_SIGNATURE_QEMU=1431127377,exports.FloatQueue=f,exports.FloppyController=v,exports.HD_SECTOR_SIZE=512,exports.HPET=function(t){function e(){return s?1e4*(Date.now()-r)+a|0:a}var i=this,s=!1,r=Date.now(),a=0,o=!1,_=0,n=new Int32Array(8),d=new Int32Array(8),c=new Int32Array(8),p=0;this.legacy_mode=!1,this.timer=function(i){if(!s)return 100;i=e()>>>0;for(var r,a,o=0;o<4;o++)r=n[o<<1],a=d[o<<1]>>>0,(p<=i?a>p&&a<=i:a>p||a<=i)&&(a=4&r,2&r?(a=a&&!(_&1<<o),_|=1<<o):_&=~(1<<o),8&r&&(d[o<<1]+=c[o<<1]),a&&t.device_raise_irq(0));return p=i,100},t.io.mmap_register(4275044352,16384,(function(t){switch(dbg_log("Read "+h(t,4)+" (ctr="+h(e()>>>0)+")",LOG_HPET),t){case 0:return 99073;case 4:return 1e8;case 16:return i.legacy_mode<<1|s;case 240:return e();case 244:return 0}var r=t>>2&7,a=t-256>>5;if(256>t||a>=4||5<r)return dbg_log("Read reserved address: "+h(t),LOG_HPET),0;switch(dbg_log("Read counter: addr="+h(t)+" counter="+h(a,2)+" reg="+h(r),LOG_HPET),r){case 0:return-32817&n[a<<1]|16;case 1:return n[a<<1|1];case 2:return d[a<<1];case 3:return d[a<<1|1];case 4:case 5:return 0}}),(function(t,p){switch(dbg_log("Write "+h(t,4)+": "+h(p,2),LOG_HPET),t){case 16:return dbg_log("conf: enabled="+(1&p)+" legacy="+(p>>1&1),LOG_HPET),1&(s^p)&&(1&p?r=Date.now():a=e()),s=1==(1&p),void(i.legacy_mode=2==(2&p));case 32:return void(_&=~p);case 240:return void(a=p);case 244:return}var u=t>>2&7,l=t-256>>5;if(256>t||l>=4||2<u)dbg_log("Write reserved address: "+h(t)+" data="+h(p),LOG_HPET);else switch(dbg_log("Write counter: addr="+h(t)+" counter="+h(l,2)+" reg="+h(u)+" data="+h(p,2),LOG_HPET),u){case 0:n[l<<1]=p;break;case 2:o?(c[l<<1]=p,o=!1,dbg_log("Accumulator acc="+h(p>>>0,8)+" ctr="+h(l,2),LOG_HPET)):(d[l<<1]=p,64&n[l<<1]&&(o=!0,n[l<<1]&=-65));break;case 3:d[l<<1|1]=p}}))},exports.HPET_ADDR=4275044352,exports.HPET_COUNTER_CONFIG=16,exports.HPET_COUNTER_CONFIG_MASK=32816,exports.HPET_FREQ_MS=1e4,exports.HPET_NUM_COUNTERS=4,exports.HPET_PERIOD=1e8,exports.HPET_SUPPORT_64=0,exports.IDEDevice=w,exports.IDEInterface=x,exports.IO=k,exports.IOAPIC=I,exports.IOAPIC_ADDRESS=4273995776,exports.IOAPIC_CONFIG_DELIVS=4096,exports.IOAPIC_CONFIG_MASKED=65536,exports.IOAPIC_CONFIG_READONLY_MASK=-110592,exports.IOAPIC_CONFIG_REMOTE_IRR=16384,exports.IOAPIC_CONFIG_TRIGGER_MODE_LEVEL=32768,exports.IOAPIC_DELIVERY_FIXED=0,exports.IOAPIC_DELIVERY_INIT=5,exports.IOAPIC_DELIVERY_LOWEST_PRIORITY=1,exports.IOAPIC_DELIVERY_NMI=4,exports.IOAPIC_ID=0,exports.IOAPIC_IRQ_COUNT=24,exports.IOREGSEL=0,exports.IOWIN=16,exports.LINUX_BOOT_HDR_BOOT_FLAG=510,exports.LINUX_BOOT_HDR_CHECKSUM1=43605,exports.LINUX_BOOT_HDR_CHECKSUM2=1400005704,exports.LINUX_BOOT_HDR_CMDLINE_SIZE=568,exports.LINUX_BOOT_HDR_CMD_LINE_PTR=552,exports.LINUX_BOOT_HDR_CODE32_START=532,exports.LINUX_BOOT_HDR_HEADER=514,exports.LINUX_BOOT_HDR_HEAP_END_PTR=548,exports.LINUX_BOOT_HDR_INITRD_ADDR_MAX=556,exports.LINUX_BOOT_HDR_INIT_SIZE=608,exports.LINUX_BOOT_HDR_KERNEL_ALIGNMENT=560,exports.LINUX_BOOT_HDR_LOADFLAGS=529,exports.LINUX_BOOT_HDR_LOADFLAGS_CAN_USE_HEAPS=128,exports.LINUX_BOOT_HDR_LOADFLAGS_KEEP_SEGMENTS=64,exports.LINUX_BOOT_HDR_LOADFLAGS_LOADED_HIGH=1,exports.LINUX_BOOT_HDR_LOADFLAGS_QUIET_FLAG=32,exports.LINUX_BOOT_HDR_MIN_ALIGNMENT=565,exports.LINUX_BOOT_HDR_PAYLOAD_LENGTH=588,exports.LINUX_BOOT_HDR_PAYLOAD_OFFSET=584,exports.LINUX_BOOT_HDR_PREF_ADDRESS=600,exports.LINUX_BOOT_HDR_RAMDISK_IMAGE=536,exports.LINUX_BOOT_HDR_RAMDISK_SIZE=540,exports.LINUX_BOOT_HDR_RELOCATABLE_KERNEL=564,exports.LINUX_BOOT_HDR_SETUP_SECTS=497,exports.LINUX_BOOT_HDR_SYSSIZE=500,exports.LINUX_BOOT_HDR_TYPE_OF_LOADER=528,exports.LINUX_BOOT_HDR_TYPE_OF_LOADER_NOT_ASSIGNED=255,exports.LINUX_BOOT_HDR_VERSION=518,exports.LINUX_BOOT_HDR_VIDMODE=506,exports.LINUX_BOOT_HDR_XLOADFLAGS=566,exports.LOG_9P=4194304,exports.LOG_ACPI=262144,exports.LOG_ALL=-1,exports.LOG_ALL_IO=false,exports.LOG_APIC=524288,exports.LOG_BIOS=4096,exports.LOG_CPU=2,exports.LOG_DISK=32768,exports.LOG_DMA=16,exports.LOG_FLOPPY=8192,exports.LOG_FPU=4,exports.LOG_HPET=131072,exports.LOG_IO=32,exports.LOG_LEVEL=-15786961,exports.LOG_MEM=8,exports.LOG_MOUSE=1024,exports.LOG_NAMES=o,exports.LOG_NET=1048576,exports.LOG_NONE=0,exports.LOG_OTHER=1,exports.LOG_PCI=2048,exports.LOG_PIC=128,exports.LOG_PIT=512,exports.LOG_PS2=64,exports.LOG_RTC=65536,exports.LOG_SB16=8388608,exports.LOG_SERIAL=16384,exports.LOG_TO_FILE=!1,exports.LOG_VGA=256,exports.LOG_VIRTIO=2097152,exports.MAX_BPP=32,exports.MAX_XRES=2560,exports.MAX_YRES=1600,exports.MIXER_CHANNEL_BOTH=2,exports.MIXER_CHANNEL_LEFT=0,exports.MIXER_CHANNEL_RIGHT=1,exports.MIXER_READ_HANDLERS=P,exports.MIXER_REGISTER_IS_LEGACY=M,exports.MIXER_SRC_DAC=2,exports.MIXER_SRC_MASTER=0,exports.MIXER_SRC_PCSPEAKER=1,exports.MIXER_WRITE_HANDLERS=z,exports.MMAP_BLOCK_BITS=17,exports.MMAP_BLOCK_SIZE=131072,exports.NE2K_LOG_VERBOSE=!1,exports.NE_DATAPORT=16,exports.NE_RESET=31,exports.Ne2k=R,exports.OSCILLATOR_FREQ=1193.1816666,exports.PCI=E,exports.PCI_CONFIG_ADDRESS=3320,exports.PCI_CONFIG_DATA=3324,exports.PIC=C,exports.PIC_LOG_VERBOSE=!1,exports.PIT=T,exports.PMTIMER_FREQ_SECONDS=3579545,exports.PS2=q,exports.PS2_LOG_VERBOSE=!1,exports.REG_CS=1,exports.REG_DS=3,exports.REG_EAX=0,exports.REG_EBP=5,exports.REG_EBX=3,exports.REG_ECX=1,exports.REG_EDI=7,exports.REG_EDX=2,exports.REG_ES=0,exports.REG_ESI=6,exports.REG_ESP=4,exports.REG_FS=4,exports.REG_GS=5,exports.REG_LDTR=7,exports.REG_SS=2,exports.RTC=y,exports.SB16=G,exports.SB_DMA0=0,exports.SB_DMA1=1,exports.SB_DMA3=3,exports.SB_DMA5=5,exports.SB_DMA6=6,exports.SB_DMA7=7,exports.SB_DMA_BLOCK_SAMPLES=1024,exports.SB_DMA_BUFSIZE=65536,exports.SB_DMA_CHANNEL_16BIT=5,exports.SB_DMA_CHANNEL_8BIT=1,exports.SB_IRQ=5,exports.SB_IRQ10=10,exports.SB_IRQ2=2,exports.SB_IRQ5=5,exports.SB_IRQ7=7,exports.SB_IRQ_16BIT=2,exports.SB_IRQ_8BIT=1,exports.SB_IRQ_MIDI=1,exports.SB_IRQ_MPU=4,exports.START_PAGE=64,exports.START_RX_PAGE=76,exports.STATE_INDEX_INFO_LEN=3,exports.STATE_INDEX_MAGIC=0,exports.STATE_INDEX_TOTAL_LEN=2,exports.STATE_INDEX_VERSION=1,exports.STATE_INFO_BLOCK_START=16,exports.STATE_MAGIC=-2039052682,exports.STATE_VERSION=6,exports.STOP_PAGE=128,exports.SyncBuffer=u,exports.TIME_PER_FRAME=1,exports.TRACK_FILENAMES=!1,exports.TSC_RATE=1e6,exports.UART=J,exports.UART_IER_MSI=8,exports.UART_IER_RDI=1,exports.UART_IER_THRI=2,exports.UART_IIR_CTI=12,exports.UART_IIR_MSI=0,exports.UART_IIR_NO_INT=1,exports.UART_IIR_RDI=4,exports.UART_IIR_RLSI=6,exports.UART_IIR_THRI=2,exports.UART_LSR_DATA_READY=1,exports.UART_LSR_TRANSMITTER_EMPTY=64,exports.UART_LSR_TX_EMPTY=32,exports.V86Starter=zt,exports.VGAScreen=et,exports.VGA_BANK_SIZE=65536,exports.VGA_HOST_MEMORY_SPACE_SIZE=tt,exports.VGA_HOST_MEMORY_SPACE_START=$,exports.VGA_LFB_ADDRESS=3758096384,exports.VGA_MIN_MEMORY_SIZE=262144,exports.VGA_PIXEL_BUFFER_SIZE=524288,exports.VIRTIO_F_RING_EVENT_IDX=29,exports.VIRTIO_F_RING_INDIRECT_DESC=28,exports.VIRTIO_F_VERSION_1=32,exports.VIRTIO_ISR_DEVICE_CFG=2,exports.VIRTIO_ISR_QUEUE=1,exports.VIRTIO_PCI_CAP_COMMON_CFG=1,exports.VIRTIO_PCI_CAP_DEVICE_CFG=4,exports.VIRTIO_PCI_CAP_ISR_CFG=3,exports.VIRTIO_PCI_CAP_LENGTH=16,exports.VIRTIO_PCI_CAP_NOTIFY_CFG=2,exports.VIRTIO_PCI_CAP_PCI_CFG=5,exports.VIRTIO_PCI_CAP_VENDOR=9,exports.VIRTIO_PCI_VENDOR_ID=6900,exports.VIRTIO_STATUS_ACKNOWLEDGE=1,exports.VIRTIO_STATUS_DEVICE_NEEDS_RESET=64,exports.VIRTIO_STATUS_DRIVER=2,exports.VIRTIO_STATUS_DRIVER_OK=4,exports.VIRTIO_STATUS_FAILED=128,exports.VIRTIO_STATUS_FEATURES_OK=8,exports.VIRTQ_AVAIL_BASESIZE=6,exports.VIRTQ_AVAIL_ENTRYSIZE=2,exports.VIRTQ_AVAIL_F_NO_INTERRUPT=1,exports.VIRTQ_DESC_ENTRYSIZE=16,exports.VIRTQ_DESC_F_INDIRECT=4,exports.VIRTQ_DESC_F_NEXT=1,exports.VIRTQ_DESC_F_WRITE=2,exports.VIRTQ_IDX_MASK=65535,exports.VIRTQ_USED_BASESIZE=6,exports.VIRTQ_USED_ENTRYSIZE=8,exports.VIRTQ_USED_F_NO_NOTIFY=1,exports.VirtIO=vt,exports.WASM_TABLE_OFFSET=1024,exports.WASM_TABLE_SIZE=900,exports.ZSTD_MAGIC=4247762216,exports.dbg_assert=n,exports.dbg_assert_failed=function(t){if(console.trace(),t)throw"Assert failed: "+t;throw"Assert failed"},exports.dbg_log=_,exports.dbg_trace=function(t){},exports.download=g,exports.dump_file=function(t,e){t instanceof Array||(t=[t]),g(t=new Blob(t),e)},exports.h=p,exports.load_kernel=function(t,e,i,s){dbg_log("Trying to load kernel of size "+e.byteLength);var r=new Uint8Array(e);let a=new Uint16Array(e),o=new Uint32Array(e);var _=r[497]||4;o[125],a[253];var n=a[255];if(43605!==n)dbg_log("Bad checksum1: "+h(n));else{if(1400005704==(n=a[257]|a[258]<<16)){n=a[259],dbg_assert(514<=n);var d=r[529];dbg_assert(1&d);var c=a[283],p=o[139],u=o[140],l=r[564],f=r[565],m=o[142],g=o[146],b=o[147],y=o[150],v=o[151],w=o[152];for(dbg_log("kernel boot protocol version: "+h(n)),dbg_log("flags="+h(d)+" xflags="+h(c)),dbg_log("code32_start="+h(o[133])),dbg_log("initrd_addr_max="+h(p)),dbg_log("kernel_alignment="+h(u)),dbg_log("relocatable="+l),dbg_log("min_alignment="+h(f)),dbg_log("cmdline max="+h(m)),dbg_log("payload offset="+h(g)+" size="+h(b)),dbg_log("pref_address="+h(v)+":"+h(y)),dbg_log("init_size="+h(w)),r[528]=255,r[529]=-33&d&-65|128,a[274]=56832,a[253]=65535,dbg_log("heap_end_ptr="+h(56832)),s+="\0",dbg_assert(s.length<m),dbg_log("cmd_line_ptr="+h(581632)),o[138]=581632,r=0;r<s.length;r++)t[581632+r]=s.charCodeAt(r);return _=512*(_+1),dbg_log("prot_mode_kernel_start="+h(_)),s=new Uint8Array(e,0,_),e=new Uint8Array(e,_),r=_=0,i&&(_=67108864,r=i.byteLength,dbg_assert(1048576+e.length<_),t.set(new Uint8Array(i),_)),o[134]=_,o[135]=r,dbg_assert(655360>524288+s.length),t.set(s,524288),t.set(e,1048576),{option_rom:{name:"genroms/kernel.bin",data:ft(32768,57344)}}}dbg_log("Bad checksum2: "+h(n))}},exports.read_elf=function(t){t=new DataView(t);let[e,i]=pt(t,_t);if(console.assert(52===i),DEBUG){for(var s of Object.keys(e))dbg_log(s+": 0x"+e[s].toString(16));dbg_log(e)}if(console.assert(1179403647===e.magic,"Bad magic"),console.assert(1===e.class,"Unimplemented: 64 bit elf"),console.assert(1===e.data,"Unimplemented: big endian"),console.assert(1===e.version0,"Bad version0"),console.assert(2===e.type,"Unimplemented type"),console.assert(1===e.version1,"Bad version1"),console.assert(52===e.ehsize,"Bad header size"),console.assert(32===e.phentsize,"Bad program header size"),console.assert(40===e.shentsize,"Bad section header size"),[s]=ut(lt(t,e.phoff,e.phentsize*e.phnum),ht,e.phnum),[t]=ut(lt(t,e.shoff,e.shentsize*e.shnum),dt,e.shnum),DEBUG&&LOG_LEVEL){console.log("%d program headers:",s.length);for(let t of s)console.log("type=%s offset=%s vaddr=%s paddr=%s filesz=%s memsz=%s flags=%s align=%s",t.type.toString(16),t.offset.toString(16),t.vaddr.toString(16),t.paddr.toString(16),t.filesz.toString(16),t.memsz.toString(16),t.flags.toString(16),t.align.toString(16));console.log("%d program headers:",t.length);for(let e of t)console.log("name=%s type=%s flags=%s addr=%s offset=%s size=%s link=%s info=%s addralign=%s entsize=%s",e.name.toString(16),e.type.toString(16),e.flags.toString(16),e.addr.toString(16),e.offset.toString(16),e.size.toString(16),e.link.toString(16),e.info.toString(16),e.addralign.toString(16),e.entsize.toString(16))}return{header:e,program_headers:s,sections_headers:t}},exports.v86=O,exports.v86util=c;