Implement HOP and JMP
This commit is contained in:
parent
39b7e9e2ac
commit
63637dac2f
|
|
@ -73,6 +73,39 @@ function CPU(mem) {
|
||||||
this.acc = this.acc - this.memory[addr];
|
this.acc = this.acc - this.memory[addr];
|
||||||
this.instructionPointer = this.instructionPointer += 1;
|
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) => {
|
this.perform_operation = (opcode, arg) => {
|
||||||
|
|
@ -112,6 +145,22 @@ function CPU(mem) {
|
||||||
case 8:
|
case 8:
|
||||||
this.instructions.sub_addr(arg);
|
this.instructions.sub_addr(arg);
|
||||||
break;
|
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:
|
default:
|
||||||
console.error( `Invalid opcode: ${opcode} with argument ${arg}` );
|
console.error( `Invalid opcode: ${opcode} with argument ${arg}` );
|
||||||
|
|
@ -142,7 +191,7 @@ function CPU(mem) {
|
||||||
log_debug_state = () => {
|
log_debug_state = () => {
|
||||||
console.log();
|
console.log();
|
||||||
console.group('CPU state');
|
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.log();
|
||||||
console.groupEnd('CPU state');
|
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
|
// TEST
|
||||||
|
|
||||||
|
|
@ -189,11 +230,33 @@ let test_add_sub_nocarry = [
|
||||||
[8, 8], // SUB addr ... acc = 5
|
[8, 8], // SUB addr ... acc = 5
|
||||||
[0, 0], // END
|
[0, 0], // END
|
||||||
]
|
]
|
||||||
|
|
||||||
let test_add_sub = [
|
let test_add_sub = [
|
||||||
[5, 26], // ADD lit
|
[5, 26], // ADD lit
|
||||||
[0, 0], // END
|
[0, 0], // END
|
||||||
]
|
]
|
||||||
|
// let comp = new CPU(test_add_sub);
|
||||||
|
//comp.run_program();
|
||||||
|
|
||||||
let comp = new CPU(test_add_sub);
|
let test_hop = [
|
||||||
comp.run_program();
|
[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);
|
||||||
Loading…
Reference in New Issue