nodejs_state.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env node
  2. "use strict";
  3. var fs = require("fs");
  4. var V86Starter = require("../build/libv86.js").V86Starter;
  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/linux.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 V86Starter({
  17. bios: { buffer: bios },
  18. cdrom: { buffer: linux },
  19. autostart: true,
  20. });
  21. emulator.add_listener("serial0-output-char", function(chr)
  22. {
  23. process.stdout.write(chr);
  24. });
  25. var state;
  26. process.stdin.on("data", function(c)
  27. {
  28. if(c === "\u0003")
  29. {
  30. // ctrl c
  31. emulator.stop();
  32. process.stdin.pause();
  33. }
  34. else if(c === "\x1b\x4f\x51")
  35. {
  36. // f2
  37. emulator.save_state(function(err, s)
  38. {
  39. console.log("--- Saved ---");
  40. if(err)
  41. {
  42. throw err;
  43. }
  44. state = s;
  45. });
  46. }
  47. else if(c === "\x1b\x4f\x52")
  48. {
  49. // f3
  50. if(state)
  51. {
  52. console.log("--- Restored ---");
  53. emulator.restore_state(state);
  54. }
  55. }
  56. else
  57. {
  58. emulator.serial0_send(c);
  59. }
  60. });