run.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env node
  2. "use strict";
  3. process.on("unhandledRejection", exn => { throw exn; });
  4. var V86 = require("../../build/libv86-debug.js").V86;
  5. var fs = require("fs");
  6. var test_executable = new Uint8Array(fs.readFileSync(__dirname + "/test-i386"));
  7. var emulator = new V86({
  8. bios: { url: __dirname + "/../../bios/seabios.bin" },
  9. vga_bios: { url: __dirname + "/../../bios/vgabios.bin" },
  10. cdrom: { url: __dirname + "/../../images/linux3.iso" },
  11. autostart: true,
  12. memory_size: 32 * 1024 * 1024,
  13. filesystem: {},
  14. log_level: 0,
  15. });
  16. emulator.bus.register("emulator-started", function()
  17. {
  18. console.error("Booting now, please stand by");
  19. emulator.create_file("test-i386", test_executable);
  20. });
  21. var ran_command = false;
  22. var line = "";
  23. emulator.add_listener("serial0-output-char", function(chr)
  24. {
  25. if(chr < " " && chr !== "\n" && chr !== "\t" || chr > "~")
  26. {
  27. return;
  28. }
  29. if(chr === "\n")
  30. {
  31. var new_line = line;
  32. console.error("Serial: %s", line);
  33. line = "";
  34. }
  35. else
  36. {
  37. line += chr;
  38. }
  39. if(!ran_command && line.endsWith("~% "))
  40. {
  41. ran_command = true;
  42. emulator.serial0_send("chmod +x /mnt/test-i386\n");
  43. emulator.serial0_send("/mnt/test-i386 > /mnt/result\n");
  44. emulator.serial0_send("echo test fini''shed\n");
  45. }
  46. if(new_line && new_line.includes("test finished"))
  47. {
  48. console.error("Done. Reading result ...");
  49. emulator.read_file("/result", function(err, data)
  50. {
  51. if(err)
  52. {
  53. console.error("Reading test result failed: " + err);
  54. process.exit(1);
  55. }
  56. console.error("Got result, writing to stdout");
  57. process.stdout.write(new Buffer(data));
  58. emulator.stop();
  59. });
  60. }
  61. });