v86/examples/nodejs_state.js

67 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2015-12-30 18:18:23 +01:00
#!/usr/bin/env node
"use strict";
var fs = require("fs");
var V86Starter = require("../build/libv86.js").V86Starter;
function readfile(path)
{
return new Uint8Array(fs.readFileSync(path)).buffer;
}
console.log("Use F2 to save the state and F3 to restore.");
var bios = readfile(__dirname + "/../bios/seabios.bin");
2021-01-02 03:10:47 +01:00
var linux = readfile(__dirname + "/../images/linux4.iso");
2015-12-30 18:18:23 +01:00
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding("utf8");
console.log("Now booting, please stand by ...");
var emulator = new V86Starter({
bios: { buffer: bios },
cdrom: { buffer: linux },
autostart: true,
});
emulator.add_listener("serial0-output-char", function(chr)
{
2021-01-02 03:10:47 +01:00
if(chr <= "~")
{
process.stdout.write(chr);
}
2015-12-30 18:18:23 +01:00
});
var state;
process.stdin.on("data", async function(c)
2015-12-30 18:18:23 +01:00
{
if(c === "\u0003")
{
// ctrl c
2015-12-30 21:00:37 +01:00
emulator.stop();
process.stdin.pause();
2015-12-30 18:18:23 +01:00
}
else if(c === "\x1b\x4f\x51")
{
// f2
state = await emulator.save_state();
console.log("--- Saved ---");
2015-12-30 18:18:23 +01:00
}
else if(c === "\x1b\x4f\x52")
{
// f3
if(state)
{
console.log("--- Restored ---");
await emulator.restore_state(state);
2015-12-30 18:18:23 +01:00
}
}
else
{
emulator.serial0_send(c);
}
});