Created
February 5, 2018 21:06
-
-
Save MCJack123/c521f03e5ee90e26eebc9f5470e051bd to your computer and use it in GitHub Desktop.
*Simulator* for upcoming Minecraft 8-bit CPU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function C8S1A(outHandler) { | |
var c = {}; | |
c.out = outHandler; | |
c.cache = new Array(256); | |
c.exec = function(f, argv) { | |
if (typeof this[f] != "function") {this.out("Invalid instruction " + typeof this[f]); return;} | |
var nv = new Array(); | |
var iter = 0; | |
var skip = -1; | |
//console.log(argv); | |
for (var arg in argv) { | |
if (arg != skip) { | |
if (argv[parseInt(arg)] == 255) { | |
nv[iter] = this.cache[argv[parseInt(arg)+1]]; | |
//console.log(this.cache[argv[parseInt(arg)+1]]); | |
//console.log(argv[parseInt(arg)+1]); | |
//console.log(parseInt(arg) + 1); | |
skip = parseInt(arg) + 1; | |
} else { | |
nv[iter] = argv[parseInt(arg)]; | |
} | |
iter++; | |
} | |
} | |
//console.log(nv); | |
this[f](nv); | |
} | |
c[0] = function(argv) {this.cache[0] = argv[0] + argv[1];} | |
c[1] = function(argv) {this.cache[0] = argv[0] - argv[1];} | |
c[2] = function(argv) {this.cache[argv[0]] = argv[1];} | |
c[3] = function(argv) {if (argv[0] == argv[1]) this.exec(argv[2], argv.slice(3));} | |
c[4] = function(argv) {if (argv[0] != argv[1]) this.exec(argv[2], argv.slice(3));} | |
c[5] = function(argv) {if (argv[0] > argv[1]) this.exec(argv[2], argv.slice(3));} | |
c[6] = function(argv) {if (argv[0] < argv[1]) this.exec(argv[2], argv.slice(3));} | |
c[7] = function(argv) {this.out(argv[0]);} | |
c.args = [2, 2, 2, -2, -2, -2, -2, 1]; | |
return c; | |
} | |
var fs = require("fs"); | |
var stdin = process.openStdin(); | |
var ascii = false; | |
var cpu = new C8S1A(function(t) {if (ascii && typeof t == "number") console.log(String.fromCharCode(t)); else console.log(t.toString());}); | |
stdin.addListener("data", function(d) { | |
var data = d.toString().trim(); | |
if (data == "q") process.exit(); | |
if (data == "m") { | |
ascii = !ascii; | |
console.log("Switched mode to " + (ascii ? "ascii" : "number")); | |
process.stdout.write("8S1A Input> "); | |
return; | |
} | |
if (data == "h") { | |
console.log("List of commands:\ne\t\tExecute text file (*.8s1)\neb\t\tExecute binary file (*.8b1)\nh\t\tDisplay this help\nhi\t\tDisplay instructions\nm\t\tToggle display of numbers between ascii and number\nq\t\tQuit"); | |
process.stdout.write("8S1A Input> "); | |
return; | |
} | |
if (data == "hi") { | |
console.log("List of instructions:\n0x00 <addend1> <addend2>\t\tAdd addend1 and addend2\n0x01 <first> <second>\t\t\tSubtract second from first\n0x02 <position> <value>\t\t\tSet position to value\n0x03 <num1> <num2> <instructions...>\tRun instructions if num1 equals num2\n0x04 <num1> <num2> <instructions...>\tRun instructions if num1 does not equal num2\n0x05 <num1> <num2> <instructions...>\tRun instructions if num1 is greater than num2\n0x06 <num1> <num2> <instructions...>\tRun instructions if num1 is less than num2\n0x07 <number>\t\t\t\tPrint number as character or number depending on mode\nVariables can be used by replacing the number with 0xFF + memory position"); | |
process.stdout.write("8S1A Input> "); | |
return; | |
} | |
if (data.startsWith("e")) { | |
var argv = data.split(" "); | |
var file; | |
if (data.startsWith("eb")) { | |
file = fs.readFileSync(argv[1]); | |
var ex = -1; | |
var args = new Array(); | |
for (var i = 0; i < file.length; i++) { | |
if (file[i] == 0xFE) { | |
cpu.exec(ex, args); | |
ex = -1; | |
args = new Array(); | |
} else { | |
if (ex == -1) ex = file[i]; | |
else args.push(file[i]); | |
} | |
} | |
if (ex != -1) cpu.exec(ex, args); | |
} else { | |
file = fs.readFileSync(argv[1], 'utf8'); | |
var sp = file.split("\n"); | |
for (var s in sp) { | |
var insts = sp[s].split(" "); | |
for (var i in insts) insts[i] = parseInt(insts[i]); | |
cpu.exec(insts[0], insts.slice(1)); | |
} | |
} | |
process.stdout.write("8S1A Input> "); | |
return; | |
} | |
var insts = data.trim().split(" "); | |
cpu.exec(insts[0], insts.slice(1)); | |
process.stdout.write("8S1A Input> "); | |
//document.getElementById("input").value = ""; | |
return; | |
}); | |
process.stdout.write("8S1A Input> "); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment