main.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.restart = function()
  57. {
  58. this.cpu.reset();
  59. this.cpu.load_bios();
  60. };
  61. v86.prototype.init = function(settings)
  62. {
  63. this.cpu.init(settings, this.bus);
  64. this.bus.send("emulator-ready");
  65. };
  66. if(typeof setImmediate !== "undefined")
  67. {
  68. /** @this {v86} */
  69. var fast_next_tick = function()
  70. {
  71. setImmediate(() => { this.do_tick(); });
  72. };
  73. /** @this {v86} */
  74. var register_tick = function() {};
  75. }
  76. else if(typeof window !== "undefined" && typeof postMessage !== "undefined")
  77. {
  78. // setImmediate shim for the browser.
  79. // TODO: Make this deactivatable, for other applications
  80. // using postMessage
  81. /** @const */
  82. let MAGIC_POST_MESSAGE = 0xAA55;
  83. /** @this {v86} */
  84. fast_next_tick = function()
  85. {
  86. window.postMessage(MAGIC_POST_MESSAGE, "*");
  87. };
  88. /** @this {v86} */
  89. register_tick = function()
  90. {
  91. window.addEventListener("message", (e) =>
  92. {
  93. if(e.source === window && e.data === MAGIC_POST_MESSAGE)
  94. {
  95. this.do_tick();
  96. }
  97. }, false);
  98. };
  99. }
  100. else
  101. {
  102. /** @this {v86} */
  103. fast_next_tick = function()
  104. {
  105. setTimeout(() => { this.do_tick(); }, 0);
  106. };
  107. /** @this {v86} */
  108. register_tick = function() {};
  109. }
  110. v86.prototype.fast_next_tick = fast_next_tick;
  111. v86.prototype.register_tick = register_tick;
  112. if(typeof document !== "undefined" && typeof document.hidden === "boolean")
  113. {
  114. /** @this {v86} */
  115. var next_tick = function(t)
  116. {
  117. if(t < 4 || document.hidden)
  118. {
  119. // Avoid sleeping for 1 second (happens if page is not
  120. // visible), it can break boot processes. Also don't try to
  121. // sleep for less than 4ms, since the value is clamped up
  122. this.fast_next_tick();
  123. }
  124. else
  125. {
  126. setTimeout(() => { this.do_tick(); }, t);
  127. }
  128. };
  129. }
  130. else
  131. {
  132. // In environments that aren't browsers, we might as well use setTimeout
  133. /** @this {v86} */
  134. next_tick = function(t)
  135. {
  136. setTimeout(() => { this.do_tick(); }, t);
  137. };
  138. }
  139. v86.prototype.next_tick = next_tick;
  140. v86.prototype.save_state = function()
  141. {
  142. // TODO: Should be implemented here, not on cpu
  143. return this.cpu.save_state();
  144. };
  145. v86.prototype.restore_state = function(state)
  146. {
  147. // TODO: Should be implemented here, not on cpu
  148. return this.cpu.restore_state(state);
  149. };
  150. if(typeof performance === "object" && performance.now)
  151. {
  152. v86.microtick = performance.now.bind(performance);
  153. }
  154. else if(typeof require === "function")
  155. {
  156. const { performance } = require("perf_hooks");
  157. v86.microtick = performance.now.bind(performance);
  158. }
  159. else if(typeof process === "object" && process.hrtime)
  160. {
  161. v86.microtick = function()
  162. {
  163. var t = process.hrtime();
  164. return t[0] * 1000 + t[1] / 1e6;
  165. };
  166. }
  167. else
  168. {
  169. v86.microtick = Date.now;
  170. }