Rename `byteCursor` to `IP` + update debugging setup

This commit is contained in:
n loewen 2023-08-01 15:38:31 +01:00
parent f7c2203450
commit dab111b6ad
1 changed files with 31 additions and 24 deletions

View File

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