mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-03-12 14:11:50 +09:00
86 lines
2.7 KiB
Plaintext
86 lines
2.7 KiB
Plaintext
// define TVDOS
|
|
var _TVDOS = {};
|
|
_TVDOS.VERSION = "1.0";
|
|
_TVDOS.DRIVES = {}; // Object where key-value pair is <drive-letter> : [serial-port, drive-number]
|
|
// actually figure out the drive letter association
|
|
// Drive A is always the device we're currently on
|
|
_TVDOS.DRIVES["A"] = _BIOS.FIRST_BOOTABLE_PORT;
|
|
//TODO
|
|
|
|
|
|
_TVDOS.defaults = {
|
|
path: [
|
|
""
|
|
]
|
|
};
|
|
Object.freeze(_TVDOS);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
var filesystem = {};
|
|
filesystem._toPorts = function(driveLetter) {
|
|
let port = _TVDOS.DRIVES[driveLetter.toUpperCase()];
|
|
if (port === undefined) {
|
|
throw Error("Drive letter '" + driveLetter.toUpperCase() + "' does not exist");
|
|
}
|
|
return port
|
|
};
|
|
filesystem._close = function(portNo) {
|
|
com.sendMessage(portNo, "CLOSE");
|
|
};
|
|
filesystem._flush = function(portNo) {
|
|
com.sendMessage(portNo, "FLUSH");
|
|
};
|
|
// @return true if operation committed successfully, false otherwise; throws error
|
|
// if unknown mode or invalid drive letter was given
|
|
filesystem.open = function(driveLetter, path, operationMode) {
|
|
let port = filesystem._toPorts(driveLetter);
|
|
|
|
filesystem._flush(port[0]); filesystem._close(port[0]);
|
|
|
|
let mode = operationMode.toUpperCase();
|
|
if (mode != "R" && mode != "W" && mode != "A") {
|
|
throw Error("Unknown file opening mode: " + mode);
|
|
}
|
|
|
|
com.sendMessage(port[0], "OPEN"+mode+'"'+path+'",'+port[1]);
|
|
let response = com.getStatusCode(port[0]);
|
|
return (response == 0);
|
|
};
|
|
// @return the entire contents of the file in String
|
|
filesystem.readAll = function(driveLetter) {
|
|
let port = filesystem._toPorts(driveLetter);
|
|
com.sendMessage(port[0], "READ");
|
|
let response = com.getStatusCode(port[0]);
|
|
if (response < 0 || response >= 128) {
|
|
let status = com.getDeviceStatus(port[0]);
|
|
throw Error("Reading a file failed with "+status.code+": "+status.message);
|
|
}
|
|
return com.pullMessage(port[0]);
|
|
};
|
|
Object.freeze(filesystem);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// install other stuffs
|
|
filesystem.open("A", "tvdos/gl.js", "R");
|
|
var GL = eval(filesystem.readAll("A"));
|
|
|
|
// @param cmdsrc JS source code
|
|
// @param args arguments for the program, must be Array
|
|
var execApp = function(cmdsrc, args) {
|
|
let prg = eval("let _appStub=function(exec_args){"+cmdsrc+"};_appStub;"); // making 'exec_args' a app-level global
|
|
return prg(args);
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Boot script
|
|
filesystem.open("A", "tvdos/command.js", "R");
|
|
let cmdsrc = filesystem.readAll("A");
|
|
|
|
// app execution stub
|
|
execApp(cmdsrc);
|
|
//let sh = execApp(cmdsrc, [42]);
|
|
//println(sh.test);
|