save_restore.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!doctype html>
  2. <title>Save and restore</title>
  3. <script src="../build/libv86.js"></script>
  4. <script>
  5. "use strict";
  6. window.onload = function()
  7. {
  8. var emulator = new V86Starter({
  9. wasm_path: "../build/v86.wasm",
  10. memory_size: 32 * 1024 * 1024,
  11. vga_memory_size: 2 * 1024 * 1024,
  12. screen_container: document.getElementById("screen_container"),
  13. bios: {
  14. url: "../bios/seabios.bin",
  15. },
  16. vga_bios: {
  17. url: "../bios/vgabios.bin",
  18. },
  19. cdrom: {
  20. url: "../images/linux.iso",
  21. },
  22. autostart: true,
  23. });
  24. var state;
  25. document.getElementById("save_restore").onclick = function()
  26. {
  27. var button = this;
  28. if(state)
  29. {
  30. button.value = "Save state";
  31. emulator.restore_state(state);
  32. state = undefined;
  33. }
  34. else
  35. {
  36. emulator.save_state(function(error, new_state)
  37. {
  38. if(error)
  39. {
  40. throw error;
  41. }
  42. console.log("Saved state of " + new_state.byteLength + " bytes");
  43. button.value = "Restore state";
  44. state = new_state;
  45. });
  46. }
  47. button.blur();
  48. };
  49. document.getElementById("save_file").onclick = function()
  50. {
  51. emulator.save_state(function(error, new_state)
  52. {
  53. if(error)
  54. {
  55. throw error;
  56. }
  57. var a = document.createElement("a");
  58. a.download = "v86state.bin";
  59. a.href = window.URL.createObjectURL(new Blob([new_state]));
  60. a.dataset.downloadurl = "application/octet-stream:" + a.download + ":" + a.href;
  61. a.click();
  62. });
  63. this.blur();
  64. };
  65. document.getElementById("restore_file").onchange = function()
  66. {
  67. if(this.files.length)
  68. {
  69. var filereader = new FileReader();
  70. emulator.stop();
  71. filereader.onload = function(e)
  72. {
  73. emulator.restore_state(e.target.result);
  74. emulator.run();
  75. };
  76. filereader.readAsArrayBuffer(this.files[0]);
  77. this.value = "";
  78. }
  79. this.blur();
  80. };
  81. };
  82. </script>
  83. <input id="save_restore" type="button" value="Save state">
  84. <input id="save_file" type="button" value="Save state to file">
  85. Restore from file: <input id="restore_file" type="file">
  86. <hr>
  87. <!-- A minimal structure for the ScreenAdapter defined in browser/screen.js -->
  88. <div id="screen_container">
  89. <div style="white-space: pre; font: 14px monospace; line-height: 14px"></div>
  90. <canvas style="display: none"></canvas>
  91. </div>