From 63637dac2ffb51824b307349d2b2a0feac241eec Mon Sep 17 00:00:00 2001 From: n loewen Date: Tue, 25 Jul 2023 15:57:24 +0100 Subject: [PATCH] Implement HOP and JMP --- simulator-sketch.js | 87 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 75 insertions(+), 12 deletions(-) diff --git a/simulator-sketch.js b/simulator-sketch.js index a79d2b8..08d406d 100644 --- a/simulator-sketch.js +++ b/simulator-sketch.js @@ -73,6 +73,39 @@ function CPU(mem) { this.acc = this.acc - this.memory[addr]; this.instructionPointer = this.instructionPointer += 1; }, + + hop_lit: (lit) => { + console.log("HOP lit"); + console.log(" ↳ Memory at IP+1:", this.memory[this.instructionPointer+1]); + if (this.acc === lit) { + this.instructionPointer += 2; + } else { + this.instructionPointer += 1; + } + }, + + hop_addr: (addr) => { + console.log("HOP addr"); + if (this.acc === this.memory[addr]) { + this.instructionPointer += 2; + } else { + this.instructionPointer += 1; + } + }, + + jump_lit: (lit) => { + console.log("JMP lit"); + this.instructionPointer = lit; + }, + + jump_addr: (addr) => { + console.log("JMP addr"); + this.instructionPointer = this.memory[addr]; + }, + + // UNIMPLEMENTED + carry_toggle: () => { return; }, + carry_hop: () => { return; }, }; this.perform_operation = (opcode, arg) => { @@ -112,6 +145,22 @@ function CPU(mem) { case 8: this.instructions.sub_addr(arg); break; + + case 9: + this.instructions.hop_lit(arg); + break; + + case 10: + this.instructions.hop_addr(arg); + break; + + case 11: + this.instructions.jump_lit(arg); + break; + + case 12: + this.instructions.jump_addr(arg); + break; default: console.error( `Invalid opcode: ${opcode} with argument ${arg}` ); @@ -142,7 +191,7 @@ function CPU(mem) { log_debug_state = () => { console.log(); console.group('CPU state'); - console.log( `IP: ${this.instructionPointer} Acc: ${this.acc}  CF: ${this.carryFlag}  ${this.running ? "running" : "halted" }` ); + console.log( `Acc: ${this.acc} IP: ${this.instructionPointer} CF: ${this.carryFlag}  ${this.running ? "running" : "halted" }` ); console.log(); console.groupEnd('CPU state'); }; @@ -155,14 +204,6 @@ function CPU(mem) { }; }; -// UNIMPLEMENTED - let hop_lit = function(lit) { return; } - let hop_addr = function(addr) { return; } - let jump_lit = function(lit) { return; } - let jump_addr = function(addr) { return; } - let carry_toggle = function() { return; } - let carry_hop = function() { return; } - // TEST @@ -189,11 +230,33 @@ let test_add_sub_nocarry = [ [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(); -let comp = new CPU(test_add_sub); -comp.run_program(); \ No newline at end of file +let test_hop = [ + [5, 8], // ADD lit ... acc = 8 + [9, 8], // HOP lit ... hop over next op if acc = 8 + [0, 0], // END ... (hopped over) + [7, 8], // SUB lit ... acc = 0 + [0, 0] +] +//let comp = new CPU(test_hop); +//comp.run_program(); + +// TODO: TEST HOP_addr + +let test_jmp = [ + [11, 4], // 0 + [0, 0], // 1 ... END ... JMP'd over + [0, 0], // 2 + [0, 0], // 3 + [5, 8], // 4 ... ADD lit ... acc = 8 + [0, 0], // 5 ... END +] +let comp = new CPU(test_jmp); +comp.run_program(); +console.table(comp.memory); \ No newline at end of file