/* http://esolangs.org/wiki/TwoFiftyFive Usage: new VM255("program", function(){input}, function(){output}).run(); Example usage (Cat program): new VM255("FF02 FFFF FE*FA FA*FE FDFF FF*FC", function() { var s=prompt("INPUT"); s=(s==null?"":s); var out=new Array(s.length); for(var i=0; i256) throw "ROM Overflow Error"; //reset the word word=""; } } } if(word.length!=0) throw "Extraneous Nibbles Error"; } //RAM manip getVal(p) { if(this.RESERVED.indexOf(p)!=-1) console.warn("RAM("+p.toString(16).padStart(2, "0")+") is reserved for future extensions."); switch(p) { case this.STACK_IO: if(this.stack.length>0) return this.stack.pop(); else throw "Empty Stack Error"; case this.STD_IO: //if input buffer is empty, refill it if(this.inputBuffer.length==0) { this.inputBuffer=this.input(); this.inputBuffer.push(this.EOF); } return this.inputBuffer.shift(); default: return this.ram[p]; } } setVal(v, p) { if(this.RESERVED.indexOf(p)!=-1) console.warn("RAM("+p.toString(16).padStart(2, "0")+") is reserved for future extensions."); switch(p) { case this.NAND_A: case this.NAND_B: this.ram[p]=v; this.updateNAND(); break; case this.NAND_OUT: throw "Read Only Error: RAM("+p.toString(16).padStart(2, "0")+") is reserved for NAND Output, and is read only."; break;//not needed, but it was bugging me case this.STACK_IO: this.stack.push(v); break; case this.STD_IO: this.output(v); break; default: this.ram[p]=v; } } //IP Manip getIP() { return this.getVal(this.IP); } setIP(p) { this.setVal(p, this.IP); } incIP() { this.setIP(this.getIP()+1); } //NAND gate updateNAND() { this.ram[this.NAND_OUT]=+((this.getVal(this.NAND_A)==0)||(this.getVal(this.NAND_B)==0)); /* //nand var foo=~(this.getVal(this.NAND_A)&this.getVal(this.NAND_B)); //refit into 0-255 var bar=((foo%256)+256)%256; this.ram[this.NAND_OUT]=bar; */ } run() { this.ram[this.IP]=0; while(this.getIP()