nodejs_state.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env node
  2. "use strict";
  3. var fs = require("fs");
  4. var V86 = require("../build/libv86.js").V86;
  5. function readfile(path)
  6. {
  7. return new Uint8Array(fs.readFileSync(path)).buffer;
  8. }
  9. console.log("Use F2 to save the state and F3 to restore.");
  10. var bios = readfile(__dirname + "/../bios/seabios.bin");
  11. var linux = readfile(__dirname + "/../images/linux4.iso");
  12. process.stdin.setRawMode(true);
  13. process.stdin.resume();
  14. process.stdin.setEncoding("utf8");
  15. console.log("Now booting, please stand by ...");
  16. var emulator = new V86({
  17. bios: { buffer: bios },
  18. cdrom: { buffer: linux },
  19. autostart: true,
  20. });
  21. emulator.add_listener("serial0-output-byte", function(byte)
  22. {
  23. var chr = String.fromCharCode(byte);
  24. if(chr <= "~")
  25. {
  26. process.stdout.write(chr);
  27. }
  28. });
  29. var state;
  30. process.stdin.on("data", async function(c)
  31. {
  32. if(c === "\u0003")
  33. {
  34. // ctrl c
  35. emulator.stop();
  36. process.stdin.pause();
  37. }
  38. else if(c === "\x1b\x4f\x51")
  39. {
  40. // f2
  41. state = await emulator.save_state();
  42. console.log("--- Saved ---");
  43. }
  44. else if(c === "\x1b\x4f\x52")
  45. {
  46. // f3
  47. if(state)
  48. {
  49. console.log("--- Restored ---");
  50. await emulator.restore_state(state);
  51. }
  52. }
  53. else
  54. {
  55. emulator.serial0_send(c);
  56. }
  57. });