Implement HOP and JMP

This commit is contained in:
n loewen 2023-07-25 15:57:24 +01:00
parent 39b7e9e2ac
commit 63637dac2f
1 changed files with 75 additions and 12 deletions

View File

@ -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();
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);