cpu - Fix debugging output on JMP / Add previous IP to state + add functions that abstract setting and incrementing the IP
This commit is contained in:
parent
d56eb34a44
commit
dbe630eb5e
49
cpu.js
49
cpu.js
|
|
@ -29,6 +29,14 @@ const CPU = {
|
|||
CPU.memory = new Uint8Array(256);
|
||||
CPU.memory.set(data, 0);
|
||||
},
|
||||
incrementIP: (offset) => {
|
||||
CPU.previousIP = CPU.IP;
|
||||
CPU.IP = CPU.IP + offset;
|
||||
},
|
||||
setIP: (address) => {
|
||||
CPU.previousIP = CPU.IP;
|
||||
CPU.IP = address;
|
||||
},
|
||||
setFlagOverflow: () => { CPU.FLAGS |= 4 },
|
||||
setFlagNegative: () => { CPU.FLAGS |= 4 },
|
||||
setFlagZero: () => { CPU.FLAGS |= 2 },
|
||||
|
|
@ -49,6 +57,7 @@ const CPU = {
|
|||
},
|
||||
|
||||
// Debug info
|
||||
previousIP: 0,
|
||||
currentInstruction: {
|
||||
opcode: null,
|
||||
operand: null,
|
||||
|
|
@ -64,19 +73,19 @@ const Instructions = {
|
|||
end: () => {
|
||||
CPU.currentInstruction.mnemonic = 'END';
|
||||
CPU.running = false;
|
||||
CPU.IP = CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
},
|
||||
|
||||
store_lit: (lit) => {
|
||||
CPU.currentInstruction.mnemonic = 'STO lit';
|
||||
CPU.memory[lit] = CPU.Acc;
|
||||
CPU.IP = CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
},
|
||||
|
||||
store_addr: (addr) => {
|
||||
CPU.currentInstruction.mnemonic = 'STO addr';
|
||||
CPU.memory[CPU.memory[addr]] = CPU.Acc;
|
||||
CPU.IP = CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
},
|
||||
|
||||
load_lit: (lit) => {
|
||||
|
|
@ -84,7 +93,7 @@ const Instructions = {
|
|||
CPU.Acc = lit;
|
||||
CPU.updateFlagNegative();
|
||||
CPU.updateFlagZero();
|
||||
CPU.IP = CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
},
|
||||
|
||||
load_addr: (addr) => {
|
||||
|
|
@ -92,7 +101,7 @@ const Instructions = {
|
|||
CPU.Acc = CPU.memory[addr];
|
||||
CPU.updateFlagNegative();
|
||||
CPU.updateFlagZero();
|
||||
CPU.IP = CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
},
|
||||
|
||||
add_lit: (lit) => {
|
||||
|
|
@ -117,7 +126,7 @@ const Instructions = {
|
|||
CPU.Acc = sum;
|
||||
CPU.updateFlagNegative();
|
||||
CPU.updateFlagZero();
|
||||
CPU.IP = CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
},
|
||||
|
||||
add_addr: (addr) => {
|
||||
|
|
@ -142,7 +151,7 @@ const Instructions = {
|
|||
CPU.Acc = sum;
|
||||
CPU.updateFlagNegative();
|
||||
CPU.updateFlagZero();
|
||||
CPU.IP = CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
},
|
||||
|
||||
sub_lit: (lit) => {
|
||||
|
|
@ -167,7 +176,7 @@ const Instructions = {
|
|||
CPU.Acc = sum;
|
||||
CPU.updateFlagNegative();
|
||||
CPU.updateFlagZero();
|
||||
CPU.IP = CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
},
|
||||
|
||||
sub_addr: (addr) => {
|
||||
|
|
@ -192,35 +201,35 @@ const Instructions = {
|
|||
CPU.Acc = sum;
|
||||
CPU.updateFlagNegative();
|
||||
CPU.updateFlagZero();
|
||||
CPU.IP = CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
},
|
||||
|
||||
hop_lit: (lit) => {
|
||||
CPU.currentInstruction.mnemonic = `HOP lit; IP+2: ${CPU.memory[CPU.IP+2]}, IP+3: ${CPU.memory[CPU.IP+3]}`;
|
||||
if (CPU.Acc === lit) {
|
||||
CPU.IP += 4;
|
||||
CPU.incrementIP(4);
|
||||
} else {
|
||||
CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
}
|
||||
},
|
||||
|
||||
hop_addr: (addr) => {
|
||||
CPU.currentInstruction.mnemonic = 'HOP addr';
|
||||
if (CPU.Acc === CPU.memory[addr]) {
|
||||
CPU.IP += 4;
|
||||
CPU.incrementIP(4);
|
||||
} else {
|
||||
CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
}
|
||||
},
|
||||
|
||||
jump_lit: (lit) => {
|
||||
CPU.currentInstruction.mnemonic = 'JMP lit';
|
||||
CPU.IP = lit;
|
||||
CPU.setIP(lit);
|
||||
},
|
||||
|
||||
jump_addr: (addr) => {
|
||||
CPU.currentInstruction.mnemonic = 'JMP addr';
|
||||
CPU.IP = CPU.memory[addr];
|
||||
CPU.setIP(CPU.memory[addr]);
|
||||
},
|
||||
|
||||
flag_toggle: (flagNum) => {
|
||||
|
|
@ -232,7 +241,7 @@ const Instructions = {
|
|||
if (flagNum === 3) { mask = 8; }
|
||||
if (mask === null) { throw new Error('Invalid flag number'); }
|
||||
CPU.FLAGS = CPU.FLAGS ^= mask;
|
||||
CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
},
|
||||
|
||||
flag_hop: (flagNum) => {
|
||||
|
|
@ -244,15 +253,15 @@ const Instructions = {
|
|||
if (flagNum === 3) { mask = 8; }
|
||||
if (mask === null) { throw new Error('Invalid flag number'); }
|
||||
if (CPU.FLAGS & mask) {
|
||||
CPU.IP += 4;
|
||||
CPU.incrementIP(4);
|
||||
} else {
|
||||
CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
}
|
||||
},
|
||||
|
||||
no_op: () => {
|
||||
CPU.currentInstruction.mnemonic = `NOP`;
|
||||
CPU.IP += 2;
|
||||
CPU.incrementIP(2);
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -385,7 +394,7 @@ function logCPUState(debugInfo, debug = false) {
|
|||
display.prettyPrintDisplay(CPU.memory);
|
||||
}
|
||||
console.log();
|
||||
console.log(`Line ${debugInfo[CPU.IP-2].lineNumber}: ${debugInfo[CPU.IP-2].source}`);
|
||||
console.log(`Line ${debugInfo[CPU.previousIP].lineNumber}: ${debugInfo[CPU.previousIP].source}`);
|
||||
console.log('Mnemonic:', CPU.currentInstruction.mnemonic);
|
||||
console.log(`Machine: $${num2hex(CPU.currentInstruction.opcode)} $${num2hex(CPU.currentInstruction.operand)}`);
|
||||
console.log();
|
||||
|
|
|
|||
Loading…
Reference in New Issue