Rename `byteCursor` to `IP` + update debugging setup
This commit is contained in:
parent
f7c2203450
commit
dab111b6ad
|
|
@ -10,11 +10,11 @@
|
|||
|
||||
const printMemory = require('./print-memory.js');
|
||||
|
||||
// 0 = silent
|
||||
// 1 = verbose
|
||||
// 2 = what i'm currently focusing on
|
||||
// 3 = always print
|
||||
const DEBUG = 2;
|
||||
// 4 = silent
|
||||
const DEBUG = 4;
|
||||
const INITIAL_IP_ADDRESS = 32;
|
||||
|
||||
const mnemonicsWithOptionalArgs = ['end', 'cfc', 'chp'];
|
||||
|
|
@ -34,18 +34,18 @@ function decodeInstructions(str) {
|
|||
let lines = str.split(/\n/); // returns an array of lines
|
||||
let machineCode = new Array(INITIAL_IP_ADDRESS).fill(0);
|
||||
let labels = {};
|
||||
let byteCursor = INITIAL_IP_ADDRESS;
|
||||
let IP = INITIAL_IP_ADDRESS;
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
console.log();
|
||||
console.group(`Input line ${i}, cursor ${byteCursor}`);
|
||||
dbg(1, '');
|
||||
dbgGroup(1, `Input line ${i}, IP ${IP}`);
|
||||
dbg(3, `> ${lines[i]}`);
|
||||
let line = stripWhitespaceFromEnds(stripComments(lines[i]));
|
||||
|
||||
// Handle blank lines
|
||||
if (line.length === 0) {
|
||||
dbg(3, `cursor: ${byteCursor}, new code: none`);
|
||||
dbg(3, `IP: ${IP}, new code: none`);
|
||||
dbg(1, 'blank');
|
||||
console.groupEnd('Input line');
|
||||
dbgGroupEnd(1, 'Input line');
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -56,17 +56,17 @@ function decodeInstructions(str) {
|
|||
label = line.substring(1); // strip '@'
|
||||
|
||||
if (label in labels) {
|
||||
labels[label].pointsToByte = byteCursor;
|
||||
labels[label].pointsToByte = IP;
|
||||
} else {
|
||||
labels[label] = {
|
||||
pointsToByte: byteCursor,
|
||||
pointsToByte: IP,
|
||||
bytesToReplace: [],
|
||||
};
|
||||
}
|
||||
dbg(2, `pointsToByte: ${labels[label].pointsToByte}`);
|
||||
dbg(2, `bytesToReplace: ${labels[label].bytesToReplace}`);
|
||||
dbg(3, `cursor: ${byteCursor}, new code: none`);
|
||||
console.groupEnd('Input line');
|
||||
dbg(3, `IP: ${IP}, new code: none`);
|
||||
dbgGroupEnd(1, 'Input line');
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -91,38 +91,45 @@ function decodeInstructions(str) {
|
|||
|
||||
if (label in labels) {
|
||||
dbg(1, `'${label}' already in labels object`);
|
||||
labels[label].bytesToReplace.push(byteCursor + 1);
|
||||
labels[label].bytesToReplace.push(IP + 1);
|
||||
} else {
|
||||
dbg(1, `'${label}' NOT in labels object`);
|
||||
labels[label] = {
|
||||
bytesToReplace: [byteCursor + 1],
|
||||
bytesToReplace: [IP + 1],
|
||||
};
|
||||
}
|
||||
dbg(2, `pointsToByte: ${labels[label].pointsToByte}`);
|
||||
dbg(2, `bytesToReplace: ${labels[label].bytesToReplace}`);
|
||||
} else if (arg_str.startsWith("(")) {
|
||||
// Handle indirect expressions
|
||||
dbg(2, "INDIRECT");
|
||||
addressingMode = "indirect";
|
||||
arg_str = arg_str.replace("(", "");
|
||||
arg_str = arg_str.replace(")", "");
|
||||
arg_num = parseInt(arg_str);
|
||||
} else if (arg_str.startsWith("$")) {
|
||||
// Handle direct expressions
|
||||
}
|
||||
|
||||
// Handle numeric operands
|
||||
dbg(2, `ARG NUM ${arg_num}`);
|
||||
if (arg_num === null) {
|
||||
dbg(2, `ARG STR ${arg_str}`);
|
||||
if (arg_str.startsWith("$")) {
|
||||
// Handle hex
|
||||
arg_str = arg_str.replace("$", "");
|
||||
arg_num = hex2num(arg_str);
|
||||
} else {
|
||||
// Accept decimal i guess
|
||||
arg_num = parseInt(arg_str);
|
||||
}
|
||||
}
|
||||
|
||||
// Decode!
|
||||
op = mnemonics2opcodes[opName][addressingMode];
|
||||
|
||||
machineCode.push(op);
|
||||
machineCode.push(arg_num);
|
||||
byteCursor += 2;
|
||||
dbg(3, `cursor: ${byteCursor}, new code: ${op}, ${arg_num}`);
|
||||
console.groupEnd('Input line');
|
||||
IP += 2;
|
||||
dbg(3, `IP: ${IP}, new code: ${op}, ${arg_num}`);
|
||||
dbgGroupEnd(1, 'Input line');
|
||||
};
|
||||
|
||||
dbg(1, '');
|
||||
|
|
|
|||
Loading…
Reference in New Issue