Improve simulator debugging output + refactor debugging code

This commit is contained in:
n loewen 2023-08-02 15:08:33 +01:00
parent 9f04c4d314
commit 4183b7c38e
1 changed files with 30 additions and 33 deletions

View File

@ -25,41 +25,41 @@ const CPU = {
const Instructions = {
end: () => {
console.log('END');
CPU.currentInstruction.mnemonic = 'END';
CPU.running = false;
},
store_lit: (lit) => {
console.log('STO lit#');
CPU.currentInstruction.mnemonic = 'STO lit';
CPU.memory[lit] = CPU.Acc;
CPU.IP = CPU.IP += 2;
},
store_addr: (addr) => {
console.log('STO addr');
CPU.currentInstruction.mnemonic = 'STO addr';
CPU.memory[CPU.memory[addr]] = CPU.Acc;
CPU.IP = CPU.IP += 2;
},
load_lit: (lit) => {
console.log('LDA lit#');
CPU.currentInstruction.mnemonic = 'LDA lit';
CPU.Acc = lit;
CPU.IP = CPU.IP += 2;
},
load_addr: (addr) => {
console.log('LDA addr');
CPU.currentInstruction.mnemonic = 'LDA addr';
console.log('mem at addr: ', CPU.memory[addr]);
CPU.Acc = CPU.memory[addr];
CPU.IP = CPU.IP += 2;
},
add_lit: (lit) => {
console.log("ADD lit");
CPU.currentInstruction.mnemonic = 'ADD lit';
let sum = CPU.Acc + lit;
if (sum > 15) {
if (sum > 255) {
CPU.CF = 1;
CPU.Acc = (sum % 15) - 1;
CPU.Acc = (sum % 255) - 1;
} else {
CPU.CF = 0;
CPU.Acc = sum;
@ -68,7 +68,7 @@ const Instructions = {
},
add_addr: (addr) => {
console.log("ADD addr");
CPU.currentInstruction.mnemonic = 'ADD addr';
let sum = CPU.Acc + CPU.memory[addr];
if (sum > 15) {
CPU.CF = 1;
@ -80,8 +80,8 @@ const Instructions = {
CPU.IP = CPU.IP += 2;
},
sub_lit: (lit) => { // TODO: carry flag
console.log("SUB lit");
sub_lit: (lit) => {
CPU.currentInstruction.mnemonic = 'SUB lit';
let sum = CPU.Acc - lit;
if (sum < 0) {
CPU.CF = 1;
@ -100,8 +100,7 @@ const Instructions = {
},
hop_lit: (lit) => {
console.log("HOP lit");
console.log(` ↳ Memory at IP+2 and +3: ${CPU.memory[CPU.IP+2]}, ${CPU.memory[CPU.IP+3]}`);
CPU.currentInstruction.mnemonic = `HOP lit \n ↳ Memory at IP+2 and +3: ${CPU.memory[CPU.IP+2]}, ${CPU.memory[CPU.IP+3]}`;
if (CPU.Acc === lit) {
CPU.IP += 4;
} else {
@ -110,7 +109,7 @@ const Instructions = {
},
hop_addr: (addr) => {
console.log("HOP addr");
CPU.currentInstruction.mnemonic = 'HOP addr';
if (CPU.Acc === CPU.memory[addr]) {
CPU.IP += 4;
} else {
@ -119,24 +118,23 @@ const Instructions = {
},
jump_lit: (lit) => {
console.log("JMP lit");
CPU.currentInstruction.mnemonic = 'JMP lit';
CPU.IP = lit;
},
jump_addr: (addr) => {
console.log("JMP addr");
CPU.currentInstruction.mnemonic = 'JMP addr';
CPU.IP = CPU.memory[addr];
},
carry_clear: () => {
console.log("CFC");
CPU.currentInstruction.mnemonic = 'CFC';
CPU.CF = 0;
CPU.IP += 2;
},
carry_hop: () => {
console.log("CHP");
console.log(` ↳ Memory at IP+2 and +3: ${CPU.memory[CPU.IP+2]}, ${CPU.memory[CPU.IP+3]}`);
CPU.currentInstruction.mnemonic = `CHP \n ↳ Memory at IP+2 and +3: ${CPU.memory[CPU.IP+2]}, ${CPU.memory[CPU.IP+3]}`;
if (CPU.CF != 0) {
CPU.IP += 4;
} else {
@ -164,23 +162,16 @@ const opcodes2mnemonics = {
};
function stepCPU() {
console.group(`IP: ${CPU.IP}`);
let opcode = CPU.memory[CPU.IP];
let argument = CPU.memory[CPU.IP+1];
console.log(`OP: ${opcode} ARG: ${argument}`);
let instruction = opcodes2mnemonics[opcode];
instruction(argument);
logCPUState();
console.groupEnd(`IP:`);
CPU.currentInstruction.opcode = CPU.memory[CPU.IP];
CPU.currentInstruction.argument = CPU.memory[CPU.IP+1];
let executeInstruction = opcodes2mnemonics[CPU.currentInstruction.opcode];
executeInstruction(CPU.currentInstruction.argument);
}
exports.runProgram = async (code, debug = false) => {
if (debug) logRunningHeader();
CPU.loadMemory(code);
if (debug) logCPUState();
CPU.running = true;
let step = 0;
while (true) {
@ -189,12 +180,15 @@ exports.runProgram = async (code, debug = false) => {
if (!CPU.running) break;
if (CPU.IP >= CPU.memory.length) break;
stepCPU();
console.group(`Step`);
if (!debug) console.clear();
console.group('Display')
display.printDisplay(CPU.memory);
console.groupEnd('Display');
logCPUState();
if (!debug) await new Promise(resolve => setTimeout(resolve, 75));
console.groupEnd('Step');
};
}
@ -202,6 +196,9 @@ exports.runProgram = async (code, debug = false) => {
// FUNCTIONS THAT PULL INFO FROM STATE TO DISPLAY
function logCPUState() {
console.log();
console.log('Mnemonic:', CPU.currentInstruction.mnemonic);
console.log(`Machine: $${num2hex(CPU.currentInstruction.opcode)} $${num2hex(CPU.currentInstruction.argument)}`);
console.log();
console.log( `IP: $${num2hex(CPU.IP)} Acc: $${num2hex(CPU.Acc)} CF: ${CPU.CF}  ${CPU.running ? "running" : "halted" }` );
console.log();