15
this.tick_counter = 0;
19
this.cpu = new CPU(bus, wasm, () => { this.idle && this.next_tick(0); });
22
bus.register("cpu-init", this.init, this);
23
bus.register("cpu-run", this.run, this);
24
bus.register("cpu-stop", this.stop, this);
25
bus.register("cpu-restart", this.restart, this);
27
this.register_yield();
30
v86.prototype.run = function()
37
this.bus.send("emulator-started");
43
v86.prototype.do_tick = function()
45
if(this.stopped || !this.running)
47
this.stopped = this.running = false;
48
this.bus.send("emulator-stopped");
53
const t = this.cpu.main_run();
58
v86.prototype.next_tick = function(t)
60
const tick = ++this.tick_counter;
65
v86.prototype.yield_callback = function(tick)
67
if(tick === this.tick_counter)
73
v86.prototype.stop = function()
81
v86.prototype.destroy = function()
83
this.unregister_yield();
86
v86.prototype.restart = function()
92
v86.prototype.init = function(settings)
94
this.cpu.init(settings, this.bus);
95
this.bus.send("emulator-ready");
98
if(typeof process !== "undefined")
100
v86.prototype.yield = function(t, tick)
104
global.setImmediate(tick => this.yield_callback(tick), tick);
108
setTimeout(tick => this.yield_callback(tick), t, tick);
112
v86.prototype.register_yield = function() {};
113
v86.prototype.unregister_yield = function() {};
115
else if(typeof Worker !== "undefined")
119
function the_worker()
121
globalThis.onmessage = function(e)
124
if(t < 1) postMessage(e.data.tick);
125
else setTimeout(() => postMessage(e.data.tick), t);
129
v86.prototype.register_yield = function()
131
const url = URL.createObjectURL(new Blob(["(" + the_worker.toString() + ")()"], { type: "text/javascript" }));
132
this.worker = new Worker(url);
133
this.worker.onmessage = e => this.yield_callback(e.data);
134
URL.revokeObjectURL(url);
137
v86.prototype.yield = function(t, tick)
139
this.worker.postMessage({ t, tick });
142
v86.prototype.unregister_yield = function()
144
this.worker.terminate();
186
v86.prototype.yield = function(t)
188
setTimeout(() => { this.do_tick(); }, t);
191
v86.prototype.register_yield = function() {};
192
v86.prototype.unregister_yield = function() {};
195
v86.prototype.save_state = function()
198
return this.cpu.save_state();
201
v86.prototype.restore_state = function(state)
204
return this.cpu.restore_state(state);
208
if(typeof performance === "object" && performance.now)
210
v86.microtick = performance.now.bind(performance);
212
else if(typeof require === "function")
214
const { performance } = require("perf_hooks");
215
v86.microtick = performance.now.bind(performance);
217
else if(typeof process === "object" && process.hrtime)
219
v86.microtick = function()
221
var t = process.hrtime();
222
return t[0] * 1000 + t[1] / 1e6;
227
v86.microtick = Date.now;