Skip to content

Instantly share code, notes, and snippets.

@chrismwendt
Last active August 29, 2015 14:22
Show Gist options
  • Save chrismwendt/af939e82cdb54814de82 to your computer and use it in GitHub Desktop.
Save chrismwendt/af939e82cdb54814de82 to your computer and use it in GitHub Desktop.
simulator for a particular machine with a single operation
// http://jsfiddle.net/hfn5w1je/1/
// TODO step through instructions on key press
// TODO step through program on timer
// TODO highlight the next instruction and components (a, b, c, true, false)
// TODO highlight jumps
// TODO dynamically resize memory
// TODO compress memory representation via run-length encoding
// TODO add I/O capabilities (e.g. keyboard and terminal view)
window.onload = function() {
stringToMem = function(s) {
return s.split("")
.map(function(v) {
return v !== "0";
});
};
overwrite = function(arr, i, s) {
var j = 0;
var sp = stringToMem(s);
for (; j < sp.length; j += 1) {
arr[i + j] = sp[j];
}
};
bin = function(a) {
v = a.toString(2);
return (new Array(v.length + 1).join("1")) + "0" + v;
};
var n = 500;
var arr = [];
for (var i=0, t=n; i<t; i++) {
// arr.push(Math.random() < 0.5 ? false : true);
arr.push(false);
}
overwrite(arr, 0, [100, 100, 100, 0, 200].map(bin).join(""));
overwrite(arr, 100, "0");
overwrite(arr, 200, [100, 100, 100, 0, 200].map(bin).join(""));
mem = arr;
pointer = 0;
pa = null;
pb = null;
pr = null;
pt = null;
pe = null;
width = 1000;
height = 1000;
margin = 5;
size = 20;
columns = 30;
bufferLeft = 10;
bufferTop = 10;
trueColor = "lightgray";
falseColor = "darkslategray";
pointerColor = "greenyellow";
fill = {
"true": {
"true": "plum",
"false": "lightgray"
},
"false": {
"true": "slateblue",
"false": "darkslategray"
}
}
addressToColumn = function(a, columns) {
return a % columns;
}
addressToRow = function(a) {
return Math.floor(a / columns);
}
readNat = function(arr, i) {
if (arr[i] == false) {
return null;
} else {
var j = i;
for (; arr[j]; j += 1) { }
var a = arr.slice(j + 1, j + 1 + (j - i));
var n = 0;
a.map(function(v) { n *= 2; n += v ? 1 : 0; });
return [n, j + 1 + (j - i)];
}
};
readInst = function(arr, i) {
var ra = readNat(arr, i);
if (ra !== null) {
pa = ra[0];
var rb = readNat(arr, i + ra[1]);
if (rb !== null) {
pb = rb[0];
var rr = readNat(arr, i + rb[1]);
if (rr !== null) {
pr = rr[0];
var rt = readNat(arr, i + rr[1]);
if (rt !== null) {
pt = rt[0];
var re = readNat(arr, i + rt[1]);
if (re !== null) {
pe = re[0];
}
}
}
}
}
}
var stepButton = d3
.select("body")
.append("input")
.attr("type", "button")
.attr("value", "Step");
svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("display", "block");
update = function() {
readInst(mem, pointer);
console.log(pa, pb, pr, pt, pe);
stepButton.attr("disabled",
(pa === null ||
pb === null ||
pr === null ||
pt === null ||
pe === null) ?
true : null);
svg
.selectAll(".bit")
.data(mem)
.style("fill", function(v, i) { return fill[v][i == pointer]; });
};
svg
.selectAll("rect")
.data(mem)
.enter()
.append("rect")
.attr("class", "bit")
.attr("x", function(_, i) { return bufferLeft + (size + margin) * addressToColumn(i, columns); })
.attr("y", function(_, i) { return bufferTop + (size + margin) * addressToRow(i); })
.attr("width", size)
.attr("height", size)
.on("click", function(v, i) { mem[i] = !v; update(); });
var step = function(a, b, c) {
tf = !(arr[pa] || arr[pb]);
mem[pr] = tf;
pointer = tf ? pt : pe;
console.log(arr[pa], arr[pb], tf);
update();
};
stepButton.on("click", step);
update();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment