diff --git a/simulator-sketch.js b/simulator-sketch.js index 3a8b91e..f646bc8 100644 --- a/simulator-sketch.js +++ b/simulator-sketch.js @@ -33,6 +33,28 @@ function CPU(mem) { this.acc = this.memory[addr]; this.instructionPointer = this.instructionPointer += 1; }, + add_lit: (lit) => { + console.log("ADD lit"); + if ( (this.acc + lit) > 15 ) { this.carryFlag = 1; } + this.acc = (this.acc + lit % 15); + this.instructionPointer = this.instructionPointer += 1; + }, + add_addr: (addr) => { + console.log("ADD addr"); + if ( (this.acc + this.memory[addr]) > 15 ) { this.carryFlag = 1; } + this.acc = (this.acc + this.memory[addr] % 15); + this.instructionPointer = this.instructionPointer += 1; + }, + sub_lit: (lit) => { // TODO: carry flag + console.log("SUB lit"); + this.acc = this.acc - lit; + this.instructionPointer = this.instructionPointer += 1; + }, + sub_addr: (addr) => { // TODO: carry flag + console.log("SUB addr"); + this.acc = this.acc - this.memory[addr]; + this.instructionPointer = this.instructionPointer += 1; + }, }; log_debug_state = () => { @@ -71,6 +93,22 @@ function CPU(mem) { case 4: this.instructions.load_addr(arg); break; + + case 5: + this.instructions.add_lit(arg); + break; + + case 6: + this.instructions.add_addr(arg); + break; + + case 7: + this.instructions.sub_lit(arg); + break; + + case 8: + this.instructions.sub_addr(arg); + break; default: console.error( `Invalid opcode: ${opcode} with argument ${arg}` ); @@ -100,10 +138,6 @@ function CPU(mem) { }; // UNIMPLEMENTED - let add_lit = function(lit) { return; } - let add_addr = function(addr) { return; } - let subtract_lit = function(lit) { return; } - let subtract_addr = function(addr) { return; } let hop_lit = function(lit) { return; } let hop_addr = function(addr) { return; } let jump_lit = function(lit) { return; } @@ -119,13 +153,29 @@ let halt_and_catch_fire = [ [1, 0], ]; -let basic_test = [ +let test_lda_sto = [ [3, 8], // LDA lit [1, 5], // STO lit [4, 5], // LDA addr [2, 6], // STO addr [0, 0], // END ]; +// let comp = new CPU(test_lda_sto); +// comp.run_program(); -let comp = new CPU(basic_test); +let test_add_sub_nocarry = [ + [5, 6], // ADD lit ... acc = 6 + [7, 1], // SUB lit ... acc = 5 + [1, 8], // STO lit ... mem[8] = 5 + [6, 8], // ADD addr ... acc = 10 + [8, 8], // SUB addr ... acc = 5 + [0, 0], // END +] + +let test_add_sub = [ + [5, 26], // ADD lit + [0, 0], // END +] + +let comp = new CPU(test_add_sub); comp.run_program(); \ No newline at end of file