1
0

save_restore.html 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 V86({
  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 = async function()
  26. {
  27. var button = this;
  28. if(state)
  29. {
  30. button.value = "Save state";
  31. await emulator.restore_state(state);
  32. state = undefined;
  33. }
  34. else
  35. {
  36. const new_state = await emulator.save_state();
  37. console.log("Saved state of " + new_state.byteLength + " bytes");
  38. button.value = "Restore state";
  39. state = new_state;
  40. }
  41. button.blur();
  42. };
  43. document.getElementById("save_file").onclick = async function()
  44. {
  45. const new_state = await emulator.save_state();
  46. var a = document.createElement("a");
  47. a.download = "v86state.bin";
  48. a.href = window.URL.createObjectURL(new Blob([new_state]));
  49. a.dataset.downloadurl = "application/octet-stream:" + a.download + ":" + a.href;
  50. a.click();
  51. this.blur();
  52. };
  53. document.getElementById("restore_file").onchange = function()
  54. {
  55. if(this.files.length)
  56. {
  57. var filereader = new FileReader();
  58. emulator.stop();
  59. filereader.onload = async function(e)
  60. {
  61. await emulator.restore_state(e.target.result);
  62. emulator.run();
  63. };
  64. filereader.readAsArrayBuffer(this.files[0]);
  65. this.value = "";
  66. }
  67. this.blur();
  68. };
  69. };
  70. </script>
  71. <input id="save_restore" type="button" value="Save state">
  72. <input id="save_file" type="button" value="Save state to file">
  73. Restore from file: <input id="restore_file" type="file">
  74. <hr>
  75. <!-- A minimal structure for the ScreenAdapter defined in browser/screen.js -->
  76. <div id="screen_container">
  77. <div style="white-space: pre; font: 14px monospace; line-height: 14px"></div>
  78. <canvas style="display: none"></canvas>
  79. </div>