save_restore.html 2.6 KB

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