2023-09-29 14:24:44 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
2025-03-18 00:29:11 -07:00
|
|
|
import path from "node:path";
|
|
|
|
|
import url from "node:url";
|
|
|
|
|
|
|
|
|
|
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
|
2023-09-29 14:24:44 -05:00
|
|
|
|
2025-04-07 19:29:47 +07:00
|
|
|
const BENCH_COLLECT_STATS = +process.env.BENCH_COLLECT_STATS;
|
2025-04-09 15:27:56 +07:00
|
|
|
const { V86 } = await import(BENCH_COLLECT_STATS ? "../../src/main.js" : "../../build/libv86.mjs");
|
2023-09-29 14:24:44 -05:00
|
|
|
|
|
|
|
|
const V86_ROOT = path.join(__dirname, "../..");
|
|
|
|
|
|
|
|
|
|
const emulator = new V86({
|
|
|
|
|
bios: { url: path.join(V86_ROOT, "/bios/seabios.bin") },
|
|
|
|
|
vga_bios: { url: path.join(V86_ROOT, "/bios/vgabios.bin") },
|
|
|
|
|
autostart: true,
|
|
|
|
|
memory_size: 512 * 1024 * 1024,
|
|
|
|
|
vga_memory_size: 8 * 1024 * 1024,
|
2025-04-07 18:59:18 +07:00
|
|
|
net_device: {
|
|
|
|
|
type: "virtio",
|
|
|
|
|
relay_url: "<UNUSED>",
|
|
|
|
|
},
|
2025-04-10 16:57:15 +07:00
|
|
|
initial_state: { url: path.join(V86_ROOT, "/images/arch_state-v2.bin.zst") },
|
2024-07-12 12:57:34 +02:00
|
|
|
filesystem: { baseurl: path.join(V86_ROOT, "/images/arch/") },
|
2023-09-30 17:41:10 -05:00
|
|
|
disable_jit: +process.env.DISABLE_JIT,
|
2023-09-29 14:24:44 -05:00
|
|
|
log_level: 0,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
emulator.bus.register("emulator-started", function()
|
|
|
|
|
{
|
|
|
|
|
emulator.create_file("/bench.py", Buffer.from(`
|
|
|
|
|
def fib(n):
|
|
|
|
|
if n < 2:
|
|
|
|
|
return n
|
|
|
|
|
return fib(n-2) + fib(n-1)
|
|
|
|
|
|
|
|
|
|
n = 30
|
|
|
|
|
print("fib(", n, ")= ", fib(n))
|
|
|
|
|
`));
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
emulator.serial0_send(`python3 /bench.py > /dev/null && python /bench.py > /dev/null && time python /bench.py\n`);
|
|
|
|
|
}, 1000);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var line = "";
|
|
|
|
|
|
|
|
|
|
emulator.add_listener("serial0-output-byte", function(byte)
|
|
|
|
|
{
|
|
|
|
|
var chr = String.fromCharCode(byte);
|
|
|
|
|
if(chr < " " && chr !== "\n" && chr !== "\t" || chr > "~")
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(chr === "\n")
|
|
|
|
|
{
|
|
|
|
|
console.log("%s", line);
|
|
|
|
|
|
|
|
|
|
if(line.startsWith("sys"))
|
|
|
|
|
{
|
2024-12-20 10:35:55 -07:00
|
|
|
emulator.destroy();
|
2023-09-29 14:24:44 -05:00
|
|
|
|
|
|
|
|
if(BENCH_COLLECT_STATS)
|
|
|
|
|
{
|
2025-04-09 15:27:56 +07:00
|
|
|
console.log(emulator.get_instruction_stats());
|
2023-09-29 14:24:44 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
line = "";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
line += chr;
|
|
|
|
|
}
|
|
|
|
|
});
|