2012-02-09 03:37:04 +00:00
|
|
|
|
|
|
|
window.onload = function () {
|
|
|
|
|
|
|
|
var $ = function (s) { return document.querySelector(s); };
|
|
|
|
|
|
|
|
$('#lb_files').selectedIndex = 0;
|
|
|
|
|
|
|
|
var bell = (function () {
|
|
|
|
var b = new Bell(/^.*\/|/.exec(window.location)[0]);
|
|
|
|
return function () { b.play(); };
|
|
|
|
} ());
|
|
|
|
|
|
|
|
|
2013-08-17 15:24:56 +00:00
|
|
|
var frame = $('#frame');
|
|
|
|
var tty = new TTY($('#screen'), frame, bell);
|
2012-02-09 03:37:04 +00:00
|
|
|
var dos = new DOS(tty);
|
|
|
|
|
|
|
|
var lores = new LoRes($('#lores'), 40, 48);
|
|
|
|
var hires = new HiRes($('#hires'), 280, 192);
|
|
|
|
var hires2 = new HiRes($('#hires2'), 280, 192);
|
|
|
|
var display = {
|
|
|
|
state: { graphics: false, full: true, page1: true, lores: true },
|
|
|
|
setState: function (state, value /* ... */) {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
while (args.length) {
|
|
|
|
state = args.shift();
|
|
|
|
value = args.shift();
|
|
|
|
this.state[state] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.graphics) {
|
|
|
|
lores.show(this.state.lores);
|
|
|
|
hires.show(!this.state.lores && this.state.page1);
|
|
|
|
hires2.show(!this.state.lores && !this.state.page1);
|
|
|
|
tty.splitScreen(tty.getScreenSize().height - (this.state.full ? 0 : 4));
|
|
|
|
} else {
|
|
|
|
lores.show(false);
|
|
|
|
hires.show(false);
|
|
|
|
hires2.show(false);
|
|
|
|
tty.splitScreen(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var pdl = [0, 0, 0, 0];
|
|
|
|
|
2012-06-20 16:13:06 +00:00
|
|
|
// Lexical highlighting, if available
|
|
|
|
var editor;
|
|
|
|
if (typeof CodeMirror === 'function') {
|
|
|
|
editor = new CodeMirror($('#editorframe'), {
|
|
|
|
mode: 'basic',
|
|
|
|
tabMode: 'default',
|
|
|
|
content: $('#source').value,
|
|
|
|
height: '100%'
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
editor = (function () {
|
|
|
|
var textArea = document.createElement('textarea');
|
|
|
|
$('#editorframe').appendChild(textArea);
|
|
|
|
textArea.style.width = '598px';
|
|
|
|
textArea.style.height = '384px';
|
|
|
|
return {
|
|
|
|
getValue: function () {
|
|
|
|
return textArea.value;
|
|
|
|
},
|
|
|
|
setValue: function (value) {
|
|
|
|
textArea.value = value;
|
|
|
|
},
|
|
|
|
setCursor: function (line, column) {
|
|
|
|
// TODO: Implement me!
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
}
|
2012-02-09 03:37:04 +00:00
|
|
|
|
|
|
|
$('#btn_share').onclick = function () {
|
|
|
|
// Load up the hidden text area with the current source
|
|
|
|
$('#source').value = getSource();
|
|
|
|
};
|
|
|
|
|
|
|
|
function getSource() {
|
|
|
|
return editor.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
function setSource(source) {
|
|
|
|
editor.setValue(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
var program;
|
|
|
|
addEvent($('#btn_run'), 'click', function () {
|
|
|
|
dos.reset();
|
|
|
|
tty.reset();
|
|
|
|
tty.autoScroll = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
program = basic.compile(getSource());
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof basic.ParseError) {
|
|
|
|
editor.setCursor({ line: e.line - 1, ch: e.column - 1 });
|
|
|
|
console.log(e.message +
|
|
|
|
' (source line:' + e.line + ', column:' + e.column + ')');
|
|
|
|
}
|
|
|
|
alert(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stopped = false;
|
|
|
|
updateUI();
|
|
|
|
|
|
|
|
program.init({
|
|
|
|
tty: tty,
|
|
|
|
hires: hires,
|
|
|
|
hires2: hires2,
|
|
|
|
lores: lores,
|
|
|
|
display: display,
|
|
|
|
paddle: function (n) { return pdl[n]; }
|
|
|
|
});
|
|
|
|
setTimeout(driver, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
addEvent($('#btn_stop'), 'click', function () {
|
|
|
|
tty.reset(); // cancel any blocking input
|
|
|
|
stopped = true;
|
|
|
|
updateUI();
|
|
|
|
});
|
|
|
|
|
|
|
|
addEvent($('#lb_files'), 'change', function () {
|
|
|
|
var sel = $('#lb_files');
|
|
|
|
loadFile('samples/' + sel.value + ".txt", setSource);
|
|
|
|
});
|
|
|
|
|
|
|
|
addEvent($('#btn_save'), 'click', function () {
|
|
|
|
window.localStorage.setItem("save_program", getSource());
|
2012-06-20 15:54:40 +00:00
|
|
|
$('#btn_load').disabled = false;
|
2012-02-09 03:37:04 +00:00
|
|
|
});
|
|
|
|
addEvent($('#btn_load'), 'click', function () {
|
|
|
|
setSource(window.localStorage.getItem("save_program"));
|
|
|
|
});
|
2012-06-20 15:54:40 +00:00
|
|
|
$('#btn_load').disabled = (window.localStorage.getItem("save_program") === null);
|
2012-02-09 03:37:04 +00:00
|
|
|
|
|
|
|
// Add a "printer" on demand
|
2013-08-16 08:37:54 +00:00
|
|
|
var printer = null;
|
|
|
|
var paper = $('#paper');
|
|
|
|
addEvent($('#show_paper'), 'click', function () {
|
|
|
|
window.getClassList(document.body).add('printout');
|
|
|
|
printer = new Printer(tty, paper);
|
|
|
|
});
|
|
|
|
addEvent($('#hide_paper'), 'click', function () {
|
|
|
|
window.getClassList(document.body).remove('printout');
|
|
|
|
printer.close();
|
|
|
|
printer = null;
|
2012-02-09 03:37:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Mouse-as-Joystick
|
|
|
|
var wrapper = $('#screen-wrapper');
|
|
|
|
addEvent(wrapper, 'mousemove', function (e) {
|
2013-08-17 22:15:54 +00:00
|
|
|
var rect = wrapper.getBoundingClientRect(),
|
|
|
|
x = e.clientX - rect.left, y = e.clientY - rect.top;
|
|
|
|
function clamp(n, min, max) { return n < min ? min : n > max ? max : n; }
|
|
|
|
pdl[0] = clamp(x / (rect.width - 1), 0, 1);
|
|
|
|
pdl[1] = clamp(y / (rect.height - 1), 0, 1);
|
2012-02-09 03:37:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var stopped = true;
|
|
|
|
function updateUI() {
|
|
|
|
$("#btn_stop").disabled = stopped ? "disabled" : "";
|
|
|
|
$("#btn_run").disabled = stopped ? "" : "disabled";
|
|
|
|
$("#lb_files").disabled = stopped ? "" : "disabled";
|
|
|
|
|
|
|
|
if (stopped) {
|
|
|
|
$("#btn_run").focus();
|
|
|
|
} else {
|
|
|
|
tty.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Expose a RESPONSIVE |---+--------| FAST slider in the UI
|
|
|
|
// Number of steps to execute before yielding execution
|
|
|
|
// (Use a prime to minimize risk of phasing with loops)
|
|
|
|
var NUM_SYNCHRONOUS_STEPS = 37;
|
|
|
|
|
|
|
|
function driver() {
|
|
|
|
var state = basic.STATE_RUNNING;
|
|
|
|
var statements = NUM_SYNCHRONOUS_STEPS;
|
|
|
|
|
|
|
|
while (!stopped && state === basic.STATE_RUNNING && statements > 0) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
state = program.step(driver);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
alert(e.message ? e.message : e);
|
|
|
|
stopped = true;
|
|
|
|
updateUI();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
statements -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state === basic.STATE_STOPPED || stopped) {
|
|
|
|
stopped = true;
|
|
|
|
updateUI();
|
|
|
|
} else if (state === basic.STATE_BLOCKED) {
|
|
|
|
// Fall out
|
|
|
|
} else { // state === basic.STATE_RUNNING
|
|
|
|
setTimeout(driver, 0); // Keep going
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 02:53:08 +00:00
|
|
|
function parseQueryParams() {
|
|
|
|
var params = {};
|
|
|
|
var query = document.location.search.substring(1);
|
|
|
|
query.split(/&/g).forEach(function(pair) {
|
|
|
|
pair = pair.replace(/\+/g, " ").split(/=/).map(decodeURIComponent);
|
|
|
|
params[pair[0]] = pair.length === 1 ? pair[0] : pair[1];
|
|
|
|
});
|
|
|
|
return params;
|
|
|
|
}
|
2012-02-09 03:37:04 +00:00
|
|
|
|
|
|
|
function loadFile(filename, callback) {
|
|
|
|
var req = new XMLHttpRequest();
|
|
|
|
var url = encodeURI(filename); // not encodeURIComponent, so paths can be specified
|
|
|
|
var async = true;
|
|
|
|
req.open("GET", url, async);
|
|
|
|
req.onreadystatechange = function () {
|
|
|
|
if (req.readyState === XMLHttpRequest.DONE) {
|
|
|
|
if (req.status === 200 || req.status === 0) {
|
|
|
|
callback(req.responseText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
req.send(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// load default
|
2013-05-03 02:53:08 +00:00
|
|
|
var params = parseQueryParams();
|
|
|
|
if ('source' in params) {
|
|
|
|
setSource(params.source);
|
|
|
|
} else {
|
|
|
|
loadFile('samples/sample.default.txt', setSource);
|
|
|
|
}
|
2012-02-09 03:37:04 +00:00
|
|
|
};
|