main.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. "use strict";
  2. /**
  3. * @constructor
  4. * @param {Object=} wasm
  5. */
  6. function v86(bus, wasm)
  7. {
  8. /** @type {boolean} */
  9. this.running = false;
  10. /** @type {boolean} */
  11. this.stopped = false;
  12. /** @type {CPU} */
  13. this.cpu = new CPU(bus, wasm);
  14. this.bus = bus;
  15. bus.register("cpu-init", this.init, this);
  16. bus.register("cpu-run", this.run, this);
  17. bus.register("cpu-stop", this.stop, this);
  18. bus.register("cpu-restart", this.restart, this);
  19. this.register_tick();
  20. }
  21. v86.prototype.run = function()
  22. {
  23. this.stopped = false;
  24. if(!this.running)
  25. {
  26. this.bus.send("emulator-started");
  27. this.fast_next_tick();
  28. }
  29. };
  30. v86.prototype.do_tick = function()
  31. {
  32. if(this.stopped)
  33. {
  34. this.stopped = this.running = false;
  35. this.bus.send("emulator-stopped");
  36. return;
  37. }
  38. this.running = true;
  39. var dt = this.cpu.main_run();
  40. if(dt <= 0)
  41. {
  42. this.fast_next_tick();
  43. }
  44. else
  45. {
  46. this.next_tick(dt);
  47. }
  48. };
  49. v86.prototype.stop = function()
  50. {
  51. if(this.running)
  52. {
  53. this.stopped = true;
  54. }
  55. };
  56. v86.prototype.destroy = function()
  57. {
  58. this.unregister_tick();
  59. };
  60. v86.prototype.restart = function()
  61. {
  62. this.cpu.reset();
  63. this.cpu.load_bios();
  64. };
  65. v86.prototype.init = function(settings)
  66. {
  67. this.cpu.init(settings, this.bus);
  68. this.bus.send("emulator-ready");
  69. };
  70. if(typeof setImmediate !== "undefined")
  71. {
  72. /** @this {v86} */
  73. var fast_next_tick = function()
  74. {
  75. setImmediate(() => { this.do_tick(); });
  76. };
  77. /** @this {v86} */
  78. var register_tick = function() {};
  79. /** @this {v86} */
  80. var unregister_tick = function() {};
  81. }
  82. else if(typeof window !== "undefined" && typeof postMessage !== "undefined")
  83. {
  84. // setImmediate shim for the browser.
  85. // TODO: Make this deactivatable, for other applications
  86. // using postMessage
  87. /** @const */
  88. let MAGIC_POST_MESSAGE = 0xAA55;
  89. /** @this {v86} */
  90. fast_next_tick = function()
  91. {
  92. window.postMessage(MAGIC_POST_MESSAGE, "*");
  93. };
  94. let tick;
  95. /** @this {v86} */
  96. register_tick = function()
  97. {
  98. tick = e =>
  99. {
  100. if(e.source === window && e.data === MAGIC_POST_MESSAGE)
  101. {
  102. this.do_tick();
  103. }
  104. };
  105. window.addEventListener("message", tick, false);
  106. };
  107. /** @this {v86} */
  108. unregister_tick = function()
  109. {
  110. window.removeEventListener("message", tick);
  111. tick = null;
  112. };
  113. }
  114. else
  115. {
  116. /** @this {v86} */
  117. fast_next_tick = function()
  118. {
  119. setTimeout(() => { this.do_tick(); }, 0);
  120. };
  121. /** @this {v86} */
  122. register_tick = function() {};
  123. /** @this {v86} */
  124. unregister_tick = function() {};
  125. }
  126. v86.prototype.fast_next_tick = fast_next_tick;
  127. v86.prototype.register_tick = register_tick;
  128. v86.prototype.unregister_tick = unregister_tick;
  129. if(typeof document !== "undefined" && typeof document.hidden === "boolean")
  130. {
  131. /** @this {v86} */
  132. var next_tick = function(t)
  133. {
  134. if(t < 4 || document.hidden)
  135. {
  136. // Avoid sleeping for 1 second (happens if page is not
  137. // visible), it can break boot processes. Also don't try to
  138. // sleep for less than 4ms, since the value is clamped up
  139. this.fast_next_tick();
  140. }
  141. else
  142. {
  143. setTimeout(() => { this.do_tick(); }, t);
  144. }
  145. };
  146. }
  147. else
  148. {
  149. // In environments that aren't browsers, we might as well use setTimeout
  150. /** @this {v86} */
  151. next_tick = function(t)
  152. {
  153. setTimeout(() => { this.do_tick(); }, t);
  154. };
  155. }
  156. v86.prototype.next_tick = next_tick;
  157. v86.prototype.save_state = function()
  158. {
  159. // TODO: Should be implemented here, not on cpu
  160. return this.cpu.save_state();
  161. };
  162. v86.prototype.restore_state = function(state)
  163. {
  164. // TODO: Should be implemented here, not on cpu
  165. return this.cpu.restore_state(state);
  166. };
  167. if(typeof performance === "object" && performance.now)
  168. {
  169. v86.microtick = performance.now.bind(performance);
  170. }
  171. else if(typeof require === "function")
  172. {
  173. const { performance } = require("perf_hooks");
  174. v86.microtick = performance.now.bind(performance);
  175. }
  176. else if(typeof process === "object" && process.hrtime)
  177. {
  178. v86.microtick = function()
  179. {
  180. var t = process.hrtime();
  181. return t[0] * 1000 + t[1] / 1e6;
  182. };
  183. }
  184. else
  185. {
  186. v86.microtick = Date.now;
  187. }