From 9aed9e5e8b10909eedf2214a6e15ff7f0d489fd4 Mon Sep 17 00:00:00 2001 From: n loewen Date: Tue, 25 Jul 2023 15:29:36 +0100 Subject: [PATCH] Tidy up formatting + re-order code for clarity --- simulator-sketch.js | 48 ++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/simulator-sketch.js b/simulator-sketch.js index f646bc8..d0a1989 100644 --- a/simulator-sketch.js +++ b/simulator-sketch.js @@ -6,50 +6,58 @@ function CPU(mem) { this.acc = 0; this.instructions = { - end: () => { + end: () => { console.log('END'); this.running = false }, - store_lit: (lit) => { + + store_lit: (lit) => { console.log('STO lit#'); this.memory[lit] = this.acc; log_memory(); this.instructionPointer = this.instructionPointer += 1; }, + store_addr: (addr) => { console.log('STO addr'); this.memory[this.memory[addr]] = this.acc; log_memory(); this.instructionPointer = this.instructionPointer += 1; }, - load_lit: (lit) => { + + load_lit: (lit) => { console.log('LDA lit#'); this.acc = lit; this.instructionPointer = this.instructionPointer += 1; }, - load_addr: (addr) => { + + load_addr: (addr) => { console.log('LDA addr'); console.log('mem at addr: ', this.memory[addr]); 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]; @@ -57,21 +65,6 @@ 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(); - console.groupEnd('CPU state'); - }; - - log_memory = () => { - console.log(); - console.group('Memory'); - console.table(this.memory); - console.groupEnd('Memory'); - } - this.perform_operation = (opcode, arg) => { switch (opcode) { case 0: @@ -113,7 +106,7 @@ function CPU(mem) { default: console.error( `Invalid opcode: ${opcode} with argument ${arg}` ); } - }; + } this.run_program = () => { console.log(); @@ -135,6 +128,21 @@ 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(); + console.groupEnd('CPU state'); + }; + + log_memory = () => { + console.log(); + console.group('Memory'); + console.table(this.memory); + console.groupEnd('Memory'); + }; }; // UNIMPLEMENTED