|
@@ -30,25 +30,24 @@ module.exports = class Computer {
|
|
|
*/
|
|
|
Run() {
|
|
|
// eslint-disable-next-line no-empty
|
|
|
- while (this.Execute(this.stack.Get()) === true) { }
|
|
|
+ while (this.Execute(this.stack.Get(ComputerParameterMode.IMMEDIATE_MODE)) === true) { }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Execute a call using the provided opcode
|
|
|
*
|
|
|
- * @param {number} opcode A opcode to execute
|
|
|
+ * @param {number} rawOpcode A opcode to execute
|
|
|
* @returns {boolean} False if the opcode was HALT, otherwise true
|
|
|
*/
|
|
|
- Execute(opcode) {
|
|
|
- // console.log(`DEBUG: opcode: ${opcode}`)
|
|
|
+ Execute(rawOpcode) {
|
|
|
let status = true;
|
|
|
+
|
|
|
+ const opcode = rawOpcode % 100;
|
|
|
+
|
|
|
+ // console.log(`DEBUG: opcode: ${opcode}`);
|
|
|
switch (opcode) {
|
|
|
case this.OPCODES.ADD: {
|
|
|
- const operandLeft = this.stack.Next().Get();
|
|
|
- const operandRight = this.stack.Next().Get();
|
|
|
- const position = this.stack.Next().Get();
|
|
|
-
|
|
|
- this.Operation_Add(operandLeft, operandRight, position);
|
|
|
+ this.Operation_Add(rawOpcode);
|
|
|
break;
|
|
|
}
|
|
|
case this.OPCODES.MULTIPLY: {
|
|
@@ -69,7 +68,7 @@ module.exports = class Computer {
|
|
|
status = false;
|
|
|
break;
|
|
|
default:
|
|
|
- throw Error(`Opcode ${opcode} not found`);
|
|
|
+ throw Error(`Opcode ${opcode} not found\nMemdump: ${JSON.stringify(this.stack.Dump())}`);
|
|
|
}
|
|
|
|
|
|
this.stack.Next();
|
|
@@ -97,13 +96,20 @@ module.exports = class Computer {
|
|
|
* Adds two numbers and stores the result at the provided position
|
|
|
* on the stack.
|
|
|
*
|
|
|
- * @param {number} operandLeft The first operand
|
|
|
- * @param {number} operandRight The second operand
|
|
|
- * @param {number} outputPosition The position on the stack to place the result
|
|
|
+ * Parses the operand Parameter Mode out of the opcode used to make
|
|
|
+ * this call.
|
|
|
+ *
|
|
|
+ * @param {number} rawOpcode The opcode in memory used to make this call
|
|
|
* @returns {void}
|
|
|
*/
|
|
|
- Operation_Add(operandLeft, operandRight, outputPosition) {
|
|
|
- [operandLeft, operandRight] = this._ParseOperands(operandLeft, operandRight);
|
|
|
+ Operation_Add(rawOpcode) {
|
|
|
+ const operandLeftMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 1);
|
|
|
+ const operandRightMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 2);
|
|
|
+
|
|
|
+ const operandLeft = this.stack.Next().Get(operandLeftMode);
|
|
|
+ const operandRight = this.stack.Next().Get(operandRightMode);
|
|
|
+ const outputPosition = this.stack.Next().Get(ComputerParameterMode.IMMEDIATE_MODE);
|
|
|
+
|
|
|
const newValue = operandLeft + operandRight;
|
|
|
this.stack.Put(outputPosition, newValue);
|
|
|
}
|