v86/src/main.js

220 lines
4.4 KiB
JavaScript
Raw Normal View History

2013-11-06 01:12:55 +01:00
"use strict";
Big squash e72eac2fc A few more sse instructions bdf83e0f0 temp rebase to resolve conflicts 58aa08353 replace all usage of modrm_byte with the set/get fns 7f1dbadf4 move registers to wasm memory 9a0432708 remove stdio inclusion 60f49f735 partial branch port of read_e8 747c51979 partial port of do_div32 8700c3042 remove wasm file from repo c69bc5c91 revert div32 to js ead0195b9 move last_op1 to wasm 16e9a2801 move last_op2 to wasm 4b5e749c8 move last_op_size to wasm 3a02ec98c move last_add_result, last_result, flags_changed to wasm 882b5476b implement add in C 8a8b15e11 move flags to wasm 4667917a9 implement getcf in C 1199537a1 implement adc in C a8a2f81ba implement sub in C 697cb4070 implement sbb in C 1e4e08079 implement inc in C 35259ddc3 implement dec in C 82498a632 implement neg in C 47fe549af copy opsize and reg constants 39c0f07aa implement mul8 in C 443aef4b2 implement imul8 in C afd2ec864 refactor memory accessing from C f937617bc implement mul16 in C af430b023 rewrite reg_* macros 6639f23a7 implement imul16 in C 6c490e88d add wasm build to makefile ea698b8bc refactor modrm_byte access 71899e821 split cpu.c into individual files 00dc878be capitalize flag constants e1ccf9477 remove unused buffer view f0175e480 refactor JS to wasm replacement f52c5f3a6 add assert.h ccf73539e implement imul_reg16 in C 8ae41facd implement do_mul32 in C and port mul32_result to wasm memory 77d3875eb implement do_imul32 in C af1cdc85d implement mul32 in C 820f514e3 implement imul32 in C e165f02e0 implement imul_reg32 in C fdee993e9 implement xadd8 in C 4e5d6f1c2 implement xadd16 in C 1c903565f implement xadd32 in C 1be111a93 implement getaf in C 4c2bc9f43 implement bcd_daa in C 3852cee61 implement bcd_das in C ed68e2777 implement bcd_aad in C b56c17640 implement bcd_aaa in C 5650e48c6 implement bcd_aas in C 99aeb5682 implement and in C 39d2308aa implement or in C c76e59a0b implement xor in C b14b87b95 async wasm instantiation 7f11605bc implement rol8 in C 161c662cf implement rol16 in C 37bf420f6 implement rol32 in C 921f06821 implement rcl8 in C 2f1bf0c2d implement rcl16 in C ad1d56ebe implement rcl32 in C ebcded69e implement ror8 in C 9bc225ff0 implement ror16 in C 403125717 implement ror32 in C 2a21a0896 implement rcr8 in C 80bf2667e implement rcr16 in C f8fe46c47 implement rcr32 in C 95493a272 split error_code flag in call_interrupt_vector a6f19110e export throw_cpu_exception to C dd5d8ce0d export call_interrupt_vector to C 537c08f65 implement raise_exception_with_code to C 8347e787e port instruction_pointer to wasm memory (offset 556) 0e62a7046 port previous_ip to wasm memory (offset 560) c09089e40 implement trigger_gp in C a4f0fed1b implement raise_exception in C 0b41949f4 implement trigger_de in C fc2a568a6 implement div8 in C acc6bbb54 implement idiv8 in C 5a3db2d09 implement div16 in C d77f0d151 Move wasm replacements to a dedicated function 135b19af0 port idtr_size and idtr_offset to wasm (offset 564, 568) 5f0ad7257 port gdtr_size to wasm (offset 572) dc17f7b7a port gdtr_offset to wasm (offset 576) 07876e86f port cr to wasm (offsets 580-608) 50b0213c2 port cpl to wasm (offset 612) 5cea3efcc port page_size_extensions to wasm (offset 616) 89d6fd6e8 port last_virt_eip to wasm (offset 620) 68f4111fd port eip_phys to wasm (offset 624) 73b146319 port last_virt_esp to wasm (offset 628) dbf920ff7 port esp_phys to wasm (offset 632) c05177c5a port sysenter_cs to wasm (offset 636) 63dd72194 port sysenter_esp to wasm (offset 640) 59d8aa686 port sysenter_eip to wasm (offset 644) c5e02dd8b port prefixes to wasm (offset 648) 259ca5d72 port tsc_offset to wasm (offset 652) 62ecc8825 port phys_addr to wasm (offset 656) 188ea275e port phys_addr_high to wasm (offset 660) f458cb2bb port timestamp_counter to wasm (offset 664) 890449fa3 port sreg to wasm (offsets 668-682) ebe65e6f3 port dreg to wasm (offsets 684-716) df6553220 port fw_value to wasm (offset 720) 0cd3b78ea port segment isnull, offsets and limits to wasm (offset 724-796) 4fb0ecebc Turn off verbose logging b55bc2cae fix unzip command in readme f5b897bc3 fix docs links to api.md ba03a7b4e Remove outdated test 930483ac4 Add elf parser 6597973c8 Shift addresses correctly 189903a7d Add comment to lss #ud 0a46ec4a9 Fix iret #gp error code (Windows XP) 28986b7ee Lower TIME_PER_FRAME 4ce987d71 Add acpi PM timer 43c31ea99 Handle access to debug registers when cr4.DE is set 283305215 Add run_hardware_timers c617846f2 A few more assertions for IO ports 32e7444f4 Make Closure Compiler happy a661c408c Buffer get written blocks 9306fd080 Add debug panel (#108) 795ff1a50 More complete APIC and IOAPIC implementation (#86) 4a4253956 Makefile: Refactor using addprefix ac1009331 More VGA logging 18ee4ecde Only check irqs when irr changed cdbc55e03 Add multiboot image loading, currently only supporting kvm-unit-test (#95, #129) 3ee9a1c85 Disable advanced optimisations for libv86. Fixes tests 12df5c76d Import kvm-unit-test into this repo 249f9ba37 Add kvm-unit-test (#129) 99b824f49 Mention than FreeBSD works 91fe8a3a1 ATA flush cache 0af630d99 PCI: Fix command/status 30fc01491 Rename function 569d1f3d6 APIC version write df376842c Split movsx/movzx into 16/32 bit versions dda21c5e2 CR3 ignored bits b1f039211 Implement #NP (kvm-unit-test) 57dd8358f Minor (style and logging) 17fd08e29 Less incomplete task switch implementation 797fd5bd8 cpuid level 5 24e66a43f Missing parameter a679829f0 PIC logging e68ea4573 Don't create new arrays in reset(), set to 0 instead 550e55186 Update readme regarding Arch Linux 3b565e54e Use local kolibriOS image on https. Temporary workaround for #141 1a0f804d6 Fix restoring state b397f1934 Leave ACPI disabled, fixes #151 83db7e871 add note about range header in SimpleHTTPServer a20794873 update linux 9p image mount hook doc 88e65312e Improve fprem instruction implementation to update FPU status word's condition codes 9844ef433 Remove comments from fprem instruction a429bbe73 Disable Oberon tests ba3092060 Add kvm-unit-test to travis CI (#129) 6a8813567 Travis fix 46da581de Travis fix (gcc-multilib) c78dcb105 Travis fix (switch to Trusty) dbc2ab704 Reduce memory for kvm-unit-test 2c6bd1889 Fix multiboot images smaller than 8192 bytes f5dead1f5 expand Archlinux doc with scripted install example a34cd08d9 fix raw RangeHTTPServer link to wget 35f9923fb change state to initial state in archlinux doc 2aed2636f extend archlinux doc with networking example 2aa8f3722 First entry in LDT is valid (#145) 7492c8d3c Basic 16-bit tss implementation. Fixes #127. Fixes #145. Fixes windows 3.0 098d84a02 Minor b9c93f8c6 Proper CD IDE DMA implementation, fixes #147, fixes #107 9b9123d6e Improved file loading in nodejs, simplify tests 40ee13f9b Add dummy screen a58bd7419 Fix test: Check if disk image is missing c8a7fd8dc Reenable Oberon tests bd63f49ad Minor: Semicolons d10f0b7ff Implement elcr in legacy pic aed5b441a Change virtio irq to 10, old one collides with ps2 mouse b93519c8a PS2 minor changes 25e64afa6 Update build command 5377f9457 Add maximum debug to emscripten build dac6ce6a4 Reduce logging f44f3b467 Convert argument for call from C e1808f222 Raise when C assertion fails bb6d38e6e Add cpu imports, in order to be called from C f3f909d5d Port constants 7bab32657 Port protected_mode, is_32 and stack_size_32 ee02cdd86 Port the main instruction table fe15a6cc0 fixes for prop mangling and closure type checks 11935fcc0 remove redundant pointer timestamp_counter a7021eb20 port read_imm8 to wasm f7a432691 strip useless brackets 90c7435f8 Reduce logging 047f0f405 Port a few small functions df293f8d4 Port get_seg* 37774b0d1 Remove useless comparison 47f7a89b9 Port instructions with 0F prefix 8a822bae4 Remove useless brackets again (lost in rebase) and use correct quote style 2f6e90023 port read_imm{8s,16,32s} 8f5ce2dbf remove redundant declaration 5785195ce port read_write_e8 and write_e8 5fa1a15af Make instruction functions static 4a2336d12 Port jmp_rel16, jmpcc8, jmpcc16, jmpcc32 8633cfad9 Port set_e* f024aec98 Port read_imm* aliases d39cb4d7b Port read_g* fa838388d Port read_e* 2f1c41d93 Port write_g* 43157269f Port logging and assertions ac2c937d5 Port read_write_e{16,32} and write_e{16,32} eeb7340c2 Port safe_read* 54d89b3d1 Port safe_write* c5557a227 Port diverged, branch_taken, branch_taken e3bb35939 Fix loading in node 00f6b2638 Remove useless `| 0` and `>> 0`. Use unsigned comparison 70fa762e3 Optimise read_imm8 4487917fd Port a20_enabled to wasm a8f16cb20 Fix a20_enabled booleans for closure compiler 95145ec53 Move mul32 result so offsets are reserved in order (and mention length) a3264e132 Move global pointer reservations to its own header file and make sure all .c files include all headers 78c779cbd Port FPU's stack_empty to wasm (offset 816) 4f25fc4b7 Port FPU's safe_tag_word function to wasm 8589a1c21 Port emms instruction to wasm feb5079c7 port memory_size to wasm (offset 812) 262559291 port in_mapped_range to C 8a72ad56c Port a20_enabled to wasm ce393e211 [wip] port read8 e936b49d1 port read8 to wasm 80cf98a3a port read16 to wasm 3a423372b port read_aligned16 to wasm 4725e1bac port read32s to wasm 60815b748 port write8 to wasm 5d64b295b port write16 to wasm 5fc83a791 port write32 to wasm 7148b6804 port push{16,32} to wasm 697222408 port pusha{16,32} to wasm 6adb5be15 Port paging, tlb_info, tlb_info_global and tlb_data c8ab560af Port translate_address_* 8bb3e4285 Port get_eflags and getzf c7a70a79b Port getsf 0d35dd621 Port getof e4d3ec252 Port getpf 9aacf2cfe Port test_* (branch conditions) 9cb64f970 Add v86-debug.wasm build target 37012099d Call mmap_* functions statically 8fb9da298 Add missing voids 0bcbe7ef4 Silence warning 6fb9db2fd Simplify translate_address_read and translate_address_write 28b66ca6b Optimized versions of resolve_modrm32 and resolve_sib cf4aea666 Silence warning b4915a2d2 Port adjust_stack_reg, get_stack_pointer and pop32s 7ddc14948 Optimised memory reads and writes
2017-07-06 01:59:48 +02:00
/**
* @constructor
2021-01-01 02:14:29 +01:00
* @param {Object=} wasm
Big squash e72eac2fc A few more sse instructions bdf83e0f0 temp rebase to resolve conflicts 58aa08353 replace all usage of modrm_byte with the set/get fns 7f1dbadf4 move registers to wasm memory 9a0432708 remove stdio inclusion 60f49f735 partial branch port of read_e8 747c51979 partial port of do_div32 8700c3042 remove wasm file from repo c69bc5c91 revert div32 to js ead0195b9 move last_op1 to wasm 16e9a2801 move last_op2 to wasm 4b5e749c8 move last_op_size to wasm 3a02ec98c move last_add_result, last_result, flags_changed to wasm 882b5476b implement add in C 8a8b15e11 move flags to wasm 4667917a9 implement getcf in C 1199537a1 implement adc in C a8a2f81ba implement sub in C 697cb4070 implement sbb in C 1e4e08079 implement inc in C 35259ddc3 implement dec in C 82498a632 implement neg in C 47fe549af copy opsize and reg constants 39c0f07aa implement mul8 in C 443aef4b2 implement imul8 in C afd2ec864 refactor memory accessing from C f937617bc implement mul16 in C af430b023 rewrite reg_* macros 6639f23a7 implement imul16 in C 6c490e88d add wasm build to makefile ea698b8bc refactor modrm_byte access 71899e821 split cpu.c into individual files 00dc878be capitalize flag constants e1ccf9477 remove unused buffer view f0175e480 refactor JS to wasm replacement f52c5f3a6 add assert.h ccf73539e implement imul_reg16 in C 8ae41facd implement do_mul32 in C and port mul32_result to wasm memory 77d3875eb implement do_imul32 in C af1cdc85d implement mul32 in C 820f514e3 implement imul32 in C e165f02e0 implement imul_reg32 in C fdee993e9 implement xadd8 in C 4e5d6f1c2 implement xadd16 in C 1c903565f implement xadd32 in C 1be111a93 implement getaf in C 4c2bc9f43 implement bcd_daa in C 3852cee61 implement bcd_das in C ed68e2777 implement bcd_aad in C b56c17640 implement bcd_aaa in C 5650e48c6 implement bcd_aas in C 99aeb5682 implement and in C 39d2308aa implement or in C c76e59a0b implement xor in C b14b87b95 async wasm instantiation 7f11605bc implement rol8 in C 161c662cf implement rol16 in C 37bf420f6 implement rol32 in C 921f06821 implement rcl8 in C 2f1bf0c2d implement rcl16 in C ad1d56ebe implement rcl32 in C ebcded69e implement ror8 in C 9bc225ff0 implement ror16 in C 403125717 implement ror32 in C 2a21a0896 implement rcr8 in C 80bf2667e implement rcr16 in C f8fe46c47 implement rcr32 in C 95493a272 split error_code flag in call_interrupt_vector a6f19110e export throw_cpu_exception to C dd5d8ce0d export call_interrupt_vector to C 537c08f65 implement raise_exception_with_code to C 8347e787e port instruction_pointer to wasm memory (offset 556) 0e62a7046 port previous_ip to wasm memory (offset 560) c09089e40 implement trigger_gp in C a4f0fed1b implement raise_exception in C 0b41949f4 implement trigger_de in C fc2a568a6 implement div8 in C acc6bbb54 implement idiv8 in C 5a3db2d09 implement div16 in C d77f0d151 Move wasm replacements to a dedicated function 135b19af0 port idtr_size and idtr_offset to wasm (offset 564, 568) 5f0ad7257 port gdtr_size to wasm (offset 572) dc17f7b7a port gdtr_offset to wasm (offset 576) 07876e86f port cr to wasm (offsets 580-608) 50b0213c2 port cpl to wasm (offset 612) 5cea3efcc port page_size_extensions to wasm (offset 616) 89d6fd6e8 port last_virt_eip to wasm (offset 620) 68f4111fd port eip_phys to wasm (offset 624) 73b146319 port last_virt_esp to wasm (offset 628) dbf920ff7 port esp_phys to wasm (offset 632) c05177c5a port sysenter_cs to wasm (offset 636) 63dd72194 port sysenter_esp to wasm (offset 640) 59d8aa686 port sysenter_eip to wasm (offset 644) c5e02dd8b port prefixes to wasm (offset 648) 259ca5d72 port tsc_offset to wasm (offset 652) 62ecc8825 port phys_addr to wasm (offset 656) 188ea275e port phys_addr_high to wasm (offset 660) f458cb2bb port timestamp_counter to wasm (offset 664) 890449fa3 port sreg to wasm (offsets 668-682) ebe65e6f3 port dreg to wasm (offsets 684-716) df6553220 port fw_value to wasm (offset 720) 0cd3b78ea port segment isnull, offsets and limits to wasm (offset 724-796) 4fb0ecebc Turn off verbose logging b55bc2cae fix unzip command in readme f5b897bc3 fix docs links to api.md ba03a7b4e Remove outdated test 930483ac4 Add elf parser 6597973c8 Shift addresses correctly 189903a7d Add comment to lss #ud 0a46ec4a9 Fix iret #gp error code (Windows XP) 28986b7ee Lower TIME_PER_FRAME 4ce987d71 Add acpi PM timer 43c31ea99 Handle access to debug registers when cr4.DE is set 283305215 Add run_hardware_timers c617846f2 A few more assertions for IO ports 32e7444f4 Make Closure Compiler happy a661c408c Buffer get written blocks 9306fd080 Add debug panel (#108) 795ff1a50 More complete APIC and IOAPIC implementation (#86) 4a4253956 Makefile: Refactor using addprefix ac1009331 More VGA logging 18ee4ecde Only check irqs when irr changed cdbc55e03 Add multiboot image loading, currently only supporting kvm-unit-test (#95, #129) 3ee9a1c85 Disable advanced optimisations for libv86. Fixes tests 12df5c76d Import kvm-unit-test into this repo 249f9ba37 Add kvm-unit-test (#129) 99b824f49 Mention than FreeBSD works 91fe8a3a1 ATA flush cache 0af630d99 PCI: Fix command/status 30fc01491 Rename function 569d1f3d6 APIC version write df376842c Split movsx/movzx into 16/32 bit versions dda21c5e2 CR3 ignored bits b1f039211 Implement #NP (kvm-unit-test) 57dd8358f Minor (style and logging) 17fd08e29 Less incomplete task switch implementation 797fd5bd8 cpuid level 5 24e66a43f Missing parameter a679829f0 PIC logging e68ea4573 Don't create new arrays in reset(), set to 0 instead 550e55186 Update readme regarding Arch Linux 3b565e54e Use local kolibriOS image on https. Temporary workaround for #141 1a0f804d6 Fix restoring state b397f1934 Leave ACPI disabled, fixes #151 83db7e871 add note about range header in SimpleHTTPServer a20794873 update linux 9p image mount hook doc 88e65312e Improve fprem instruction implementation to update FPU status word's condition codes 9844ef433 Remove comments from fprem instruction a429bbe73 Disable Oberon tests ba3092060 Add kvm-unit-test to travis CI (#129) 6a8813567 Travis fix 46da581de Travis fix (gcc-multilib) c78dcb105 Travis fix (switch to Trusty) dbc2ab704 Reduce memory for kvm-unit-test 2c6bd1889 Fix multiboot images smaller than 8192 bytes f5dead1f5 expand Archlinux doc with scripted install example a34cd08d9 fix raw RangeHTTPServer link to wget 35f9923fb change state to initial state in archlinux doc 2aed2636f extend archlinux doc with networking example 2aa8f3722 First entry in LDT is valid (#145) 7492c8d3c Basic 16-bit tss implementation. Fixes #127. Fixes #145. Fixes windows 3.0 098d84a02 Minor b9c93f8c6 Proper CD IDE DMA implementation, fixes #147, fixes #107 9b9123d6e Improved file loading in nodejs, simplify tests 40ee13f9b Add dummy screen a58bd7419 Fix test: Check if disk image is missing c8a7fd8dc Reenable Oberon tests bd63f49ad Minor: Semicolons d10f0b7ff Implement elcr in legacy pic aed5b441a Change virtio irq to 10, old one collides with ps2 mouse b93519c8a PS2 minor changes 25e64afa6 Update build command 5377f9457 Add maximum debug to emscripten build dac6ce6a4 Reduce logging f44f3b467 Convert argument for call from C e1808f222 Raise when C assertion fails bb6d38e6e Add cpu imports, in order to be called from C f3f909d5d Port constants 7bab32657 Port protected_mode, is_32 and stack_size_32 ee02cdd86 Port the main instruction table fe15a6cc0 fixes for prop mangling and closure type checks 11935fcc0 remove redundant pointer timestamp_counter a7021eb20 port read_imm8 to wasm f7a432691 strip useless brackets 90c7435f8 Reduce logging 047f0f405 Port a few small functions df293f8d4 Port get_seg* 37774b0d1 Remove useless comparison 47f7a89b9 Port instructions with 0F prefix 8a822bae4 Remove useless brackets again (lost in rebase) and use correct quote style 2f6e90023 port read_imm{8s,16,32s} 8f5ce2dbf remove redundant declaration 5785195ce port read_write_e8 and write_e8 5fa1a15af Make instruction functions static 4a2336d12 Port jmp_rel16, jmpcc8, jmpcc16, jmpcc32 8633cfad9 Port set_e* f024aec98 Port read_imm* aliases d39cb4d7b Port read_g* fa838388d Port read_e* 2f1c41d93 Port write_g* 43157269f Port logging and assertions ac2c937d5 Port read_write_e{16,32} and write_e{16,32} eeb7340c2 Port safe_read* 54d89b3d1 Port safe_write* c5557a227 Port diverged, branch_taken, branch_taken e3bb35939 Fix loading in node 00f6b2638 Remove useless `| 0` and `>> 0`. Use unsigned comparison 70fa762e3 Optimise read_imm8 4487917fd Port a20_enabled to wasm a8f16cb20 Fix a20_enabled booleans for closure compiler 95145ec53 Move mul32 result so offsets are reserved in order (and mention length) a3264e132 Move global pointer reservations to its own header file and make sure all .c files include all headers 78c779cbd Port FPU's stack_empty to wasm (offset 816) 4f25fc4b7 Port FPU's safe_tag_word function to wasm 8589a1c21 Port emms instruction to wasm feb5079c7 port memory_size to wasm (offset 812) 262559291 port in_mapped_range to C 8a72ad56c Port a20_enabled to wasm ce393e211 [wip] port read8 e936b49d1 port read8 to wasm 80cf98a3a port read16 to wasm 3a423372b port read_aligned16 to wasm 4725e1bac port read32s to wasm 60815b748 port write8 to wasm 5d64b295b port write16 to wasm 5fc83a791 port write32 to wasm 7148b6804 port push{16,32} to wasm 697222408 port pusha{16,32} to wasm 6adb5be15 Port paging, tlb_info, tlb_info_global and tlb_data c8ab560af Port translate_address_* 8bb3e4285 Port get_eflags and getzf c7a70a79b Port getsf 0d35dd621 Port getof e4d3ec252 Port getpf 9aacf2cfe Port test_* (branch conditions) 9cb64f970 Add v86-debug.wasm build target 37012099d Call mmap_* functions statically 8fb9da298 Add missing voids 0bcbe7ef4 Silence warning 6fb9db2fd Simplify translate_address_read and translate_address_write 28b66ca6b Optimized versions of resolve_modrm32 and resolve_sib cf4aea666 Silence warning b4915a2d2 Port adjust_stack_reg, get_stack_pointer and pop32s 7ddc14948 Optimised memory reads and writes
2017-07-06 01:59:48 +02:00
*/
2021-01-01 02:14:29 +01:00
function v86(bus, wasm)
2014-12-21 18:11:22 +01:00
{
/** @type {boolean} */
this.running = false;
/** @type {boolean} */
this.stopped = false;
/** @type {CPU} */
2021-01-01 02:14:29 +01:00
this.cpu = new CPU(bus, wasm);
2014-12-21 18:11:22 +01:00
this.bus = bus;
bus.register("cpu-init", this.init, this);
bus.register("cpu-run", this.run, this);
bus.register("cpu-stop", this.stop, this);
bus.register("cpu-restart", this.restart, this);
this.register_tick();
2014-12-21 18:11:22 +01:00
}
2015-09-12 01:41:31 +02:00
v86.prototype.run = function()
2014-12-21 18:11:22 +01:00
{
this.stopped = false;
2014-12-21 18:11:22 +01:00
if(!this.running)
{
this.bus.send("emulator-started");
2015-01-06 04:26:39 +01:00
this.fast_next_tick();
2014-12-21 18:11:22 +01:00
}
};
2015-09-12 01:41:31 +02:00
v86.prototype.do_tick = function()
2014-12-21 18:11:22 +01:00
{
if(this.stopped)
{
this.stopped = this.running = false;
this.bus.send("emulator-stopped");
2014-12-21 18:11:22 +01:00
return;
}
this.running = true;
2015-01-06 04:26:39 +01:00
var dt = this.cpu.main_run();
2014-12-21 18:11:22 +01:00
2015-09-12 01:41:31 +02:00
if(dt <= 0)
{
this.fast_next_tick();
}
else
{
this.next_tick(dt);
}
2014-12-21 18:11:22 +01:00
};
v86.prototype.stop = function()
{
if(this.running)
{
this.stopped = true;
}
};
2019-07-11 22:13:54 +02:00
v86.prototype.destroy = function()
{
this.unregister_tick();
};
2014-12-21 18:11:22 +01:00
v86.prototype.restart = function()
{
2014-12-21 19:19:04 +01:00
this.cpu.reset();
this.cpu.load_bios();
2014-12-21 18:11:22 +01:00
};
v86.prototype.init = function(settings)
{
this.cpu.init(settings, this.bus);
2015-01-19 21:58:58 +01:00
this.bus.send("emulator-ready");
2014-12-21 18:11:22 +01:00
};
if(typeof setImmediate !== "undefined")
2014-12-21 18:11:22 +01:00
{
2017-04-02 17:51:36 +02:00
/** @this {v86} */
var fast_next_tick = function()
2014-12-21 18:11:22 +01:00
{
setImmediate(() => { this.do_tick(); });
};
2017-04-02 17:51:36 +02:00
/** @this {v86} */
var register_tick = function() {};
2019-07-11 22:13:54 +02:00
/** @this {v86} */
var unregister_tick = function() {};
}
else if(typeof window !== "undefined" && typeof postMessage !== "undefined")
{
// setImmediate shim for the browser.
// TODO: Make this deactivatable, for other applications
// using postMessage
2014-12-21 18:11:22 +01:00
/** @const */
let MAGIC_POST_MESSAGE = 0xAA55;
2014-12-21 18:11:22 +01:00
2017-04-02 17:51:36 +02:00
/** @this {v86} */
fast_next_tick = function()
{
window.postMessage(MAGIC_POST_MESSAGE, "*");
};
2019-07-11 22:13:54 +02:00
let tick;
2017-04-02 17:51:36 +02:00
/** @this {v86} */
register_tick = function()
{
2019-07-11 22:13:54 +02:00
tick = e =>
2014-12-21 18:11:22 +01:00
{
if(e.source === window && e.data === MAGIC_POST_MESSAGE)
{
this.do_tick();
2014-12-21 18:11:22 +01:00
}
2019-07-11 22:13:54 +02:00
};
window.addEventListener("message", tick, false);
};
/** @this {v86} */
unregister_tick = function()
{
window.removeEventListener("message", tick);
tick = null;
};
}
else
{
2017-04-02 17:51:36 +02:00
/** @this {v86} */
fast_next_tick = function()
2014-12-21 18:11:22 +01:00
{
setTimeout(() => { this.do_tick(); }, 0);
};
2017-04-02 17:51:36 +02:00
/** @this {v86} */
register_tick = function() {};
2019-07-11 22:13:54 +02:00
/** @this {v86} */
unregister_tick = function() {};
}
2014-12-21 18:11:22 +01:00
2017-04-02 17:51:36 +02:00
v86.prototype.fast_next_tick = fast_next_tick;
v86.prototype.register_tick = register_tick;
2019-07-11 22:13:54 +02:00
v86.prototype.unregister_tick = unregister_tick;
2017-04-02 17:51:36 +02:00
if(typeof document !== "undefined" && typeof document.hidden === "boolean")
{
2017-04-02 17:51:36 +02:00
/** @this {v86} */
var next_tick = function(t)
2015-01-06 04:26:39 +01:00
{
if(t < 4 || document.hidden)
2015-01-06 04:26:39 +01:00
{
// Avoid sleeping for 1 second (happens if page is not
// visible), it can break boot processes. Also don't try to
// sleep for less than 4ms, since the value is clamped up
this.fast_next_tick();
}
else
2015-01-06 04:26:39 +01:00
{
setTimeout(() => { this.do_tick(); }, t);
}
};
}
else
{
// In environments that aren't browsers, we might as well use setTimeout
2017-04-02 17:51:36 +02:00
/** @this {v86} */
next_tick = function(t)
{
setTimeout(() => { this.do_tick(); }, t);
};
}
2014-12-21 18:11:22 +01:00
2017-04-02 17:51:36 +02:00
v86.prototype.next_tick = next_tick;
2014-12-22 03:10:03 +01:00
v86.prototype.save_state = function()
{
// TODO: Should be implemented here, not on cpu
2014-12-22 03:10:03 +01:00
return this.cpu.save_state();
};
v86.prototype.restore_state = function(state)
{
// TODO: Should be implemented here, not on cpu
2014-12-22 03:10:03 +01:00
return this.cpu.restore_state(state);
};
2014-12-21 18:11:22 +01:00
if(typeof performance === "object" && performance.now)
{
2018-01-28 01:12:27 +01:00
v86.microtick = performance.now.bind(performance);
2014-12-21 18:11:22 +01:00
}
else if(typeof require === "function")
{
const { performance } = require("perf_hooks");
v86.microtick = performance.now.bind(performance);
}
else if(typeof process === "object" && process.hrtime)
2014-12-21 18:11:22 +01:00
{
v86.microtick = function()
{
var t = process.hrtime();
return t[0] * 1000 + t[1] / 1e6;
2014-12-21 18:11:22 +01:00
};
}
else
{
v86.microtick = Date.now;
}