assembler - Fix bug where the assembler attempted to handle indirect references to constants twice / Remove unused "assemble with optional args" function / Tidy up debugging printouts
This commit is contained in:
parent
ee1e899108
commit
78ac43bead
71
assembler.js
71
assembler.js
|
|
@ -74,7 +74,7 @@ function preparseSourceCode(source) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return lines.map((line, index) => {
|
return lines.map((line, index) => {
|
||||||
dbg('2', ` ${line}`);
|
dbg(1, ` ${line}`);
|
||||||
let info = {
|
let info = {
|
||||||
number: index,
|
number: index,
|
||||||
source: line,
|
source: line,
|
||||||
|
|
@ -109,7 +109,7 @@ function decodeNumericOp(arg) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} op
|
* @param {string} op
|
||||||
* @param {object} labels // TODO
|
* @param {object} labels // TODO document better
|
||||||
* @param {number} IP
|
* @param {number} IP
|
||||||
* @returns {Array<string>} - array of labels
|
* @returns {Array<string>} - array of labels
|
||||||
**/
|
**/
|
||||||
|
|
@ -124,10 +124,10 @@ function handleLabelDefinition(op, IP, labels) {
|
||||||
bytesToReplace: [],
|
bytesToReplace: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
dbg(2, ` Label definition:`);
|
dbg(1, ` Label definition:`);
|
||||||
dbg(2, ` Points to byte: ${labels[label].pointsToByte}`);
|
dbg(1, ` Points to byte: ${labels[label].pointsToByte}`);
|
||||||
dbg(2, ` Bytes to replace: ${labels[label].bytesToReplace}`);
|
dbg(1, ` Bytes to replace: ${labels[label].bytesToReplace}`);
|
||||||
dbg(3, ` IP: $${num2hex(IP)}, new code: none`);
|
dbg(1, ` IP: $${num2hex(IP)}, new code: none`);
|
||||||
dbgGroupEnd(1, 'Input line');
|
dbgGroupEnd(1, 'Input line');
|
||||||
return labels;
|
return labels;
|
||||||
}
|
}
|
||||||
|
|
@ -151,21 +151,6 @@ function handleConstantDefinitions(op, arg, IP, constants) {
|
||||||
return constants;
|
return constants;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {SourceLineInfo} line
|
|
||||||
* @returns {Object}
|
|
||||||
**/
|
|
||||||
function assembleMnemonicsWithOptionalArgs(line) {
|
|
||||||
if (mnemonicsWithOptionalArgs.indexOf(line.operation) < 0) {
|
|
||||||
console.error(`Missing opcode for line ${line.number}: ${line.source}`);
|
|
||||||
throw new Error("Missing opcode");
|
|
||||||
}
|
|
||||||
let opcode = decodeNumericOp(line.operation);
|
|
||||||
console.log('decoding op without operand', line);
|
|
||||||
let operand = line.argument !== null ? decodeNumericOp(line.argument) : 0;
|
|
||||||
return [opcode, operand];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assemble source code.
|
* Assemble source code.
|
||||||
|
|
@ -177,12 +162,12 @@ function assembleMnemonicsWithOptionalArgs(line) {
|
||||||
* @return TODO
|
* @return TODO
|
||||||
**/
|
**/
|
||||||
function decodeInstructions(source) {
|
function decodeInstructions(source) {
|
||||||
dbg(2, 'Pre-parsing...');
|
dbg(1, 'Pre-parsing...');
|
||||||
let lines = preparseSourceCode(source);
|
let lines = preparseSourceCode(source);
|
||||||
dbg(2, '');
|
dbg(1, '');
|
||||||
dbg(2, 'Done pre-parsing.');
|
dbg(1, 'Done pre-parsing.');
|
||||||
dbg(2, '');
|
dbg(1, '');
|
||||||
dbg(2, 'Assembling...');
|
dbg(1, 'Assembling...');
|
||||||
|
|
||||||
// Figure out where to start assembly...
|
// Figure out where to start assembly...
|
||||||
|
|
||||||
|
|
@ -280,34 +265,35 @@ function decodeInstructions(source) {
|
||||||
bytesToReplace: [IP + 1],
|
bytesToReplace: [IP + 1],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
dbg(2, ` Label reference:`);
|
dbg(1, `Label reference:`);
|
||||||
dbg(2, ` Points to byte: ${labels[label].pointsToByte}`);
|
dbg(1, ` Points to byte: ${labels[label].pointsToByte}`);
|
||||||
dbg(2, ` Bytes to replace: ${labels[label].bytesToReplace}`);
|
dbg(1, ` Bytes to replace: ${labels[label].bytesToReplace}`);
|
||||||
decodedArg = 0; // Return 0 for operand for now -- we'll replace it later
|
decodedArg = 0; // Return 0 for operand for now -- we'll replace it later
|
||||||
}
|
}
|
||||||
|
|
||||||
// Operands - Handle references to the Instruction Pointer
|
// Operands - Handle references to the Instruction Pointer
|
||||||
if (arg !== null && arg === ASM_IP_LABEL) {
|
if (arg !== null && arg === ASM_IP_LABEL) {
|
||||||
dbg(2, ` References current IP - ${IP}`);
|
dbg(1, ` References current IP - ${IP}`);
|
||||||
decodedArg = IP;
|
decodedArg = IP;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Operands - Handle references to constants
|
// Operands - Handle references to constants
|
||||||
if (arg !== null && arg.startsWith(ASM_CONSTANT_PREFIX)) {
|
if (arg !== null && arg.startsWith(ASM_CONSTANT_PREFIX)) {
|
||||||
dbg(2, ` References '${arg}'`);
|
dbg(1, `References '${arg}'`);
|
||||||
decodedArg = constants[arg.substring(1)]; // substring(1) strips '>'
|
decodedArg = decodeNumericOp(constants[arg.substring(1)]); // substring(1) strips '>'
|
||||||
}
|
}
|
||||||
|
|
||||||
// Operands - Handle references to constants in indirect mode
|
// Operands - Handle references to constants in indirect mode
|
||||||
if (arg !== null && arg.startsWith(`(${ASM_CONSTANT_PREFIX}`)) {
|
if (arg !== null && arg.startsWith(`(${ASM_CONSTANT_PREFIX}`)) {
|
||||||
addressingMode = "indirect";
|
addressingMode = "indirect";
|
||||||
dbg(2, ` (Indirectly) References '${arg}'`);
|
dbg(1, `(Indirectly) References '${arg}'`);
|
||||||
let constTemp = arg.replace(`(${ASM_CONSTANT_PREFIX}`, "").replace(")", "");
|
let constName = arg.replace(`(${ASM_CONSTANT_PREFIX}`, "");
|
||||||
decodedArg = constants[constTemp];
|
constName = constName.replace(")", "");
|
||||||
|
decodedArg = decodeNumericOp(constants[constName]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Operands - Handle indirect expressions
|
// Operands - Handle indirect expressions
|
||||||
if (arg !== null && arg.startsWith("(")) {
|
if (arg !== null && decodedArg === null && arg.startsWith("(")) {
|
||||||
addressingMode = "indirect";
|
addressingMode = "indirect";
|
||||||
let indyTemp = arg.replace("(", "").replace(")", "");
|
let indyTemp = arg.replace("(", "").replace(")", "");
|
||||||
decodedArg = decodeNumericOp(indyTemp);
|
decodedArg = decodeNumericOp(indyTemp);
|
||||||
|
|
@ -318,11 +304,13 @@ function decodeInstructions(source) {
|
||||||
decodedOp = mnemonics2opcodes[line.operation][addressingMode];
|
decodedOp = mnemonics2opcodes[line.operation][addressingMode];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Decode regular operands
|
// Decode regular operands
|
||||||
if (decodedArg === null) {
|
if (decodedArg === null) {
|
||||||
decodedArg = decodeNumericOp(line.argument);
|
decodedArg = decodeNumericOp(line.argument);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
machineCode[IP] = decodedOp;
|
machineCode[IP] = decodedOp;
|
||||||
machineCode[IP + 1] = decodedArg;
|
machineCode[IP + 1] = decodedArg;
|
||||||
|
|
||||||
|
|
@ -336,7 +324,6 @@ function decodeInstructions(source) {
|
||||||
|
|
||||||
dbg(3, ` Machine code: $${num2hex(decodedOp)} $${num2hex(decodedArg)}`);
|
dbg(3, ` Machine code: $${num2hex(decodedOp)} $${num2hex(decodedArg)}`);
|
||||||
dbg(3, ` IP: $${num2hex(IP)}`);
|
dbg(3, ` IP: $${num2hex(IP)}`);
|
||||||
dbg(3, '');
|
|
||||||
IP += 2;
|
IP += 2;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -347,11 +334,11 @@ function decodeInstructions(source) {
|
||||||
|
|
||||||
// Backfill label references
|
// Backfill label references
|
||||||
for (let k of Object.keys(labels)) {
|
for (let k of Object.keys(labels)) {
|
||||||
dbgGroup(2, `${ASM_LABEL_PREFIX}${k}`);
|
dbgGroup(1, `${ASM_LABEL_PREFIX}${k}`);
|
||||||
let label = labels[k];
|
let label = labels[k];
|
||||||
dbg(2, `Points to byte: ${label.pointsToByte}`);
|
dbg(1, `Points to byte: ${label.pointsToByte}`);
|
||||||
dbg(2, `Bytes to replace: ${label.bytesToReplace}`);
|
dbg(1, `Bytes to replace: ${label.bytesToReplace}`);
|
||||||
dbgGroupEnd(2, `label`);
|
dbgGroupEnd(1);
|
||||||
for (let j = 0; j < label.bytesToReplace.length; j++) {
|
for (let j = 0; j < label.bytesToReplace.length; j++) {
|
||||||
machineCode[label.bytesToReplace[j]] = label.pointsToByte;
|
machineCode[label.bytesToReplace[j]] = label.pointsToByte;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue