arch-python.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env node
  2. "use strict";
  3. const BENCH_COLLECT_STATS = +process.env.BENCH_COLLECT_STATS;
  4. const { V86, print_stats } = require(`../../build/${BENCH_COLLECT_STATS ? "libv86-debug" : "libv86"}.js`);
  5. const path = require("path");
  6. const V86_ROOT = path.join(__dirname, "../..");
  7. const emulator = new V86({
  8. bios: { url: path.join(V86_ROOT, "/bios/seabios.bin") },
  9. vga_bios: { url: path.join(V86_ROOT, "/bios/vgabios.bin") },
  10. autostart: true,
  11. memory_size: 512 * 1024 * 1024,
  12. vga_memory_size: 8 * 1024 * 1024,
  13. network_relay_url: "<UNUSED>",
  14. initial_state: { url: path.join(V86_ROOT, "/images/arch_state.bin") },
  15. filesystem: { baseurl: path.join(V86_ROOT, "/images/arch/") },
  16. disable_jit: +process.env.DISABLE_JIT,
  17. log_level: 0,
  18. });
  19. emulator.bus.register("emulator-started", function()
  20. {
  21. emulator.create_file("/bench.py", Buffer.from(`
  22. def fib(n):
  23. if n < 2:
  24. return n
  25. return fib(n-2) + fib(n-1)
  26. n = 30
  27. print("fib(", n, ")= ", fib(n))
  28. `));
  29. setTimeout(() => {
  30. emulator.serial0_send(`python3 /bench.py > /dev/null && python /bench.py > /dev/null && time python /bench.py\n`);
  31. }, 1000);
  32. });
  33. var line = "";
  34. emulator.add_listener("serial0-output-byte", function(byte)
  35. {
  36. var chr = String.fromCharCode(byte);
  37. if(chr < " " && chr !== "\n" && chr !== "\t" || chr > "~")
  38. {
  39. return;
  40. }
  41. if(chr === "\n")
  42. {
  43. console.log("%s", line);
  44. if(line.startsWith("sys"))
  45. {
  46. emulator.stop();
  47. if(BENCH_COLLECT_STATS)
  48. {
  49. const cpu = emulator.v86.cpu;
  50. console.log(print_stats.stats_to_string(cpu));
  51. }
  52. }
  53. line = "";
  54. }
  55. else
  56. {
  57. line += chr;
  58. }
  59. });