diff --git a/assembler.js b/assembler.js index 059f68a..d46cd08 100644 --- a/assembler.js +++ b/assembler.js @@ -74,7 +74,7 @@ function preparseSourceCode(source) { } return lines.map((line, index) => { - dbg('2', ` ${line}`); + dbg(1, ` ${line}`); let info = { number: index, source: line, @@ -109,7 +109,7 @@ function decodeNumericOp(arg) { /** * @param {string} op - * @param {object} labels // TODO + * @param {object} labels // TODO document better * @param {number} IP * @returns {Array} - array of labels **/ @@ -124,10 +124,10 @@ function handleLabelDefinition(op, IP, labels) { bytesToReplace: [], }; } - dbg(2, ` Label definition:`); - dbg(2, ` Points to byte: ${labels[label].pointsToByte}`); - dbg(2, ` Bytes to replace: ${labels[label].bytesToReplace}`); - dbg(3, ` IP: $${num2hex(IP)}, new code: none`); + dbg(1, ` Label definition:`); + dbg(1, ` Points to byte: ${labels[label].pointsToByte}`); + dbg(1, ` Bytes to replace: ${labels[label].bytesToReplace}`); + dbg(1, ` IP: $${num2hex(IP)}, new code: none`); dbgGroupEnd(1, 'Input line'); return labels; } @@ -151,21 +151,6 @@ function handleConstantDefinitions(op, arg, IP, 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. @@ -177,12 +162,12 @@ function assembleMnemonicsWithOptionalArgs(line) { * @return TODO **/ function decodeInstructions(source) { - dbg(2, 'Pre-parsing...'); + dbg(1, 'Pre-parsing...'); let lines = preparseSourceCode(source); - dbg(2, ''); - dbg(2, 'Done pre-parsing.'); - dbg(2, ''); - dbg(2, 'Assembling...'); + dbg(1, ''); + dbg(1, 'Done pre-parsing.'); + dbg(1, ''); + dbg(1, 'Assembling...'); // Figure out where to start assembly... @@ -280,34 +265,35 @@ function decodeInstructions(source) { bytesToReplace: [IP + 1], }; } - dbg(2, ` Label reference:`); - dbg(2, ` Points to byte: ${labels[label].pointsToByte}`); - dbg(2, ` Bytes to replace: ${labels[label].bytesToReplace}`); + dbg(1, `Label reference:`); + dbg(1, ` Points to byte: ${labels[label].pointsToByte}`); + dbg(1, ` Bytes to replace: ${labels[label].bytesToReplace}`); decodedArg = 0; // Return 0 for operand for now -- we'll replace it later } // Operands - Handle references to the Instruction Pointer if (arg !== null && arg === ASM_IP_LABEL) { - dbg(2, ` References current IP - ${IP}`); + dbg(1, ` References current IP - ${IP}`); decodedArg = IP; } // Operands - Handle references to constants if (arg !== null && arg.startsWith(ASM_CONSTANT_PREFIX)) { - dbg(2, ` References '${arg}'`); - decodedArg = constants[arg.substring(1)]; // substring(1) strips '>' + dbg(1, `References '${arg}'`); + decodedArg = decodeNumericOp(constants[arg.substring(1)]); // substring(1) strips '>' } - + // Operands - Handle references to constants in indirect mode if (arg !== null && arg.startsWith(`(${ASM_CONSTANT_PREFIX}`)) { addressingMode = "indirect"; - dbg(2, ` (Indirectly) References '${arg}'`); - let constTemp = arg.replace(`(${ASM_CONSTANT_PREFIX}`, "").replace(")", ""); - decodedArg = constants[constTemp]; + dbg(1, `(Indirectly) References '${arg}'`); + let constName = arg.replace(`(${ASM_CONSTANT_PREFIX}`, ""); + constName = constName.replace(")", ""); + decodedArg = decodeNumericOp(constants[constName]); } // Operands - Handle indirect expressions - if (arg !== null && arg.startsWith("(")) { + if (arg !== null && decodedArg === null && arg.startsWith("(")) { addressingMode = "indirect"; let indyTemp = arg.replace("(", "").replace(")", ""); decodedArg = decodeNumericOp(indyTemp); @@ -318,11 +304,13 @@ function decodeInstructions(source) { decodedOp = mnemonics2opcodes[line.operation][addressingMode]; } + // Decode regular operands if (decodedArg === null) { decodedArg = decodeNumericOp(line.argument); } + machineCode[IP] = decodedOp; machineCode[IP + 1] = decodedArg; @@ -336,7 +324,6 @@ function decodeInstructions(source) { dbg(3, ` Machine code: $${num2hex(decodedOp)} $${num2hex(decodedArg)}`); dbg(3, ` IP: $${num2hex(IP)}`); - dbg(3, ''); IP += 2; }; } @@ -347,11 +334,11 @@ function decodeInstructions(source) { // Backfill label references for (let k of Object.keys(labels)) { - dbgGroup(2, `${ASM_LABEL_PREFIX}${k}`); + dbgGroup(1, `${ASM_LABEL_PREFIX}${k}`); let label = labels[k]; - dbg(2, `Points to byte: ${label.pointsToByte}`); - dbg(2, `Bytes to replace: ${label.bytesToReplace}`); - dbgGroupEnd(2, `label`); + dbg(1, `Points to byte: ${label.pointsToByte}`); + dbg(1, `Bytes to replace: ${label.bytesToReplace}`); + dbgGroupEnd(1); for (let j = 0; j < label.bytesToReplace.length; j++) { machineCode[label.bytesToReplace[j]] = label.pointsToByte; }