This commit is contained in:
copy 2015-01-12 06:16:01 +01:00
parent 0832f75ad4
commit 2940ef12c4
4 changed files with 397 additions and 0 deletions

127
docs/samples/lua.html Normal file
View file

@ -0,0 +1,127 @@
<!doctype html>
<title>Lua interpreter</title>
<script src="../../build/libv86.js"></script>
<script>
"use strict";
window.onload = function()
{
var emulator = new V86Starter({
memory_size: 32 * 1024 * 1024,
vga_memory_size: 2 * 1024 * 1024,
// Uncomment to see what's going on
//screen_container: document.getElementById("screen_container"),
bios: {
url: "../../bios/seabios.bin",
},
vga_bios: {
url: "../../bios/vgabios.bin",
},
cdrom: {
url: "../../images/linux.iso",
},
autostart: true,
disable_keyboard: true,
});
var data = "";
emulator.add_listener("serial0-output-char", function(char)
{
if(char !== "\r")
{
data += char;
}
if(data.endsWith("login: "))
{
console.log("Do login");
emulator.serial0_send("root\n");
}
else if(data.endsWith("/root% "))
{
console.log("Now ready");
document.getElementById("status").textContent = "Ready.\n";
document.getElementById("run").disabled = false;
}
});
emulator.add_listener("serial0-output-line", function(line)
{
// filter noise
if(!line.startsWith("/root% lua -e") &&
!line.startsWith("> ") &&
line.indexOf("Welcome to Buildroot") === -1 &&
line.indexOf("login:") === -1 &&
line.trim() !== "")
{
document.getElementById("result").textContent += line;
}
});
document.getElementById("source").onkeydown = function(e)
{
if(e.which == 13 && e.ctrlKey)
{
document.getElementById("run").onclick();
}
};
document.getElementById("run").onclick = function()
{
var code = document.getElementById("source").value;
emulator.serial0_send("lua -e " + bashEscape(code) + "\n");
document.getElementById("result").textContent = "";
document.getElementById("status").textContent = "Running ...\n";
this.disabled = true;
};
};
// https://gist.github.com/creationix/2502704
// Implement bash string escaping.
function bashEscape(arg)
{
return "'" + arg.replace(/'+/g, function (val) {
return "'" + val.replace(/'/g, "\\'") + "'";
}) + "'";
}
</script>
<textarea id=source rows=20 cols=80>
k = 1
x = 0
while k &lt; 1000 do
x = x + 1 / (k * k)
k = k + 2
end
print(math.sqrt(x*8))
function factorial(n)
if n == 0 then
return 1
else
return n * factorial(n - 1)
end
end
print("factorial(10):", factorial(10))
</textarea>
<button disabled id=run>run (ctrl-enter)</button>
<br>
<hr>
<pre id=status>Wait for boot ...</pre>
<pre id=result></pre>
<hr>
<div id="screen_container">
<div style="white-space: pre; font: 14px monospace; line-height: 14px"></div>
<canvas style="display: none"></canvas>
</div>

View file

@ -0,0 +1,107 @@
<!doctype html>
<title>Save and restore</title>
<script src="../../build/libv86.js"></script>
<script>
"use strict";
window.onload = function()
{
var emulator = new V86Starter({
memory_size: 32 * 1024 * 1024,
vga_memory_size: 2 * 1024 * 1024,
screen_container: document.getElementById("screen_container"),
bios: {
url: "../../bios/seabios.bin",
},
vga_bios: {
url: "../../bios/vgabios.bin",
},
cdrom: {
url: "../../images/linux.iso",
},
autostart: true,
});
var state;
document.getElementById("save_restore").onclick = function()
{
var button = this;
if(state)
{
button.value = "Save state";
emulator.restore_state(state);
state = undefined;
}
else
{
emulator.save_state(function(error, new_state)
{
if(error)
{
throw error;
}
button.value = "Restore state";
state = new_state;
});
}
button.blur();
};
document.getElementById("save_file").onclick = function()
{
emulator.save_state(function(error, new_state)
{
if(error)
{
throw error;
}
var a = document.createElement("a");
a.download = "v86-state.bin";
a.href = window.URL.createObjectURL(new Blob([new_state]));
a.dataset.downloadurl = "application/octet-stream:" + a.download + ":" + a.href;
a.click();
});
this.blur();
};
document.getElementById("restore_file").onchange = function()
{
if(this.files.length)
{
var filereader = new FileReader();
emulator.stop();
filereader.onload = function(e)
{
emulator.restore_state(e.target.result);
emulator.run();
};
filereader.readAsArrayBuffer(this.files[0]);
this.value = "";
}
this.blur();
};
};
</script>
<input id="save_restore" type="button" value="Save state">
<input id="save_file" type="button" value="Save state to file">
Restore from file: <input id="restore_file" type="file">
<hr>
<!-- A minimal structure for the ScreenAdapter defined in browser/screen.js -->
<div id="screen_container">
<div style="white-space: pre; font: 14px monospace; line-height: 14px"></div>
<canvas style="display: none"></canvas>
</div>

85
docs/samples/serial.html Normal file
View file

@ -0,0 +1,85 @@
<!doctype html>
<title>Serial example</title>
<script src="../../build/libv86.js"></script>
<script>
"use strict";
window.onload = function()
{
var emulator = new V86Starter({
// Uncomment to see what's going on
//screen_container: document.getElementById("screen_container"),
bios: {
url: "../../bios/seabios.bin",
},
vga_bios: {
url: "../../bios/vgabios.bin",
},
cdrom: {
url: "../../images/linux.iso",
},
autostart: true,
disable_keyboard: true,
});
// In this example we wait for output from the serial terminal, which
// should be running busybox. We log in as soon as a prompt appears and then
// retrieve a directory listing of the root directory
var data = "";
var stages = [
{
test: "login:",
send: "root\n",
},
{
test: "/root% ",
send: "ls -1 --color=never /\n",
},
{
test: "/root% ",
send: "lua -e 'print(3+4)'\n",
},
];
var stage = 0;
emulator.add_listener("serial0-output-char", function(char)
{
if(char === "\r")
{
return;
}
data += char;
document.getElementById("terminal").value += char;
var current = stages[stage];
if(!current)
{
return;
}
if(data.endsWith(current.test))
{
stage++;
emulator.serial0_send(current.send);
var log = "Sending: " + current.send.replace(/\n/g, "\\n") + "\n";
document.getElementById("log").value += log;
}
});
};
</script>
<textarea readonly rows=25 cols=60 id="log">Waiting for boot ...
</textarea>
<textarea readonly rows=25 cols=60 id="terminal"></textarea>
<div id="screen_container">
<div style="white-space: pre; font: 14px monospace; line-height: 14px"></div>
<canvas style="display: none"></canvas>
</div>

View file

@ -0,0 +1,78 @@
<!doctype html>
<title>Two emulators</title>
<script src="../../build/libv86.js"></script>
<script>
"use strict";
window.onload = function()
{
var container1 = document.getElementById("screen_container1");
var container2 = document.getElementById("screen_container2");
var emulator1 = new V86Starter({
screen_container: container1,
bios: {
url: "../../bios/seabios.bin",
},
vga_bios: {
url: "../../bios/vgabios.bin",
},
cdrom: {
url: "../../images/linux.iso",
},
autostart: true,
});
var emulator2 = new V86Starter({
screen_container: container2,
bios: {
url: "../../bios/seabios.bin",
},
vga_bios: {
url: "../../bios/vgabios.bin",
},
cdrom: {
url: "../../images/linux.iso",
},
autostart: true,
});
emulator2.keyboard_set_status(false);
container1.addEventListener("mousedown", function(e)
{
container1.style.borderColor = "yellow";
container2.style.borderColor = "black";
emulator1.keyboard_set_status(true);
emulator2.keyboard_set_status(false);
}, false);
container2.addEventListener("mousedown", function(e)
{
container1.style.borderColor = "black";
container2.style.borderColor = "yellow";
emulator1.keyboard_set_status(false);
emulator2.keyboard_set_status(true);
}, false);
emulator1.add_listener("serial0-output-char", function(char)
{
emulator2.serial0_send(char);
});
};
</script>
Click on a screen to control it.<hr>
<div id="screen_container1" style="float: left; margin: 10px; border: 3px solid yellow;">
<div style="white-space: pre; font: 14px monospace; line-height: 14px"></div>
<canvas style="display: none"></canvas>
</div>
<div id="screen_container2" style="float: left; margin: 10px; border: 3px solid black;">
<div style="white-space: pre; font: 14px monospace; line-height: 14px"></div>
<canvas style="display: none"></canvas>
</div>