Tidy up formatting + re-order code for clarity

This commit is contained in:
n loewen 2023-07-25 15:29:36 +01:00
parent d674394afb
commit 9aed9e5e8b
1 changed files with 28 additions and 20 deletions

View File

@ -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