Browse Source

Add Relative Base Offset tracking

ApisNecros 1 year ago
parent
commit
03d7fcef34
1 changed files with 30 additions and 9 deletions
  1. 30 9
      IntComp/Stack.js

+ 30 - 9
IntComp/Stack.js

@@ -9,6 +9,15 @@ module.exports = class Stack {
     constructor(stack) {
         this.pointer = 0;
         this._stack = stack;
+
+        /**
+         * The relative base offset for the stack
+         *
+         * This value is used when finding a parameter in Relative Parameter Mode.
+         * It is added to an address to find the needed value at the calculated
+         * address.
+         */
+        this.relativeBaseOffset = 0;
     }
 
     /**
@@ -39,8 +48,11 @@ module.exports = class Stack {
         if (parameterMode == ComputerParameterMode.POSITION_MODE) {
             value = this._stack[value];
         }
+        else if (parameterMode == ComputerParameterMode.RELATIVE_MODE) {
+            value = this._stack[value + this.relativeBaseOffset];
+        }
 
-        return value;
+        return value ?? 0;
     }
 
     /**
@@ -55,16 +67,15 @@ module.exports = class Stack {
             throw new TypeError("index must be an integer");
         }
 
-        try {
-            let value = this._stack[index];
-            if (parameterMode == ComputerParameterMode.POSITION_MODE) {
-                value = this._stack[value];
-            }
-            return value;
+        let value = this._stack[index];
+        if (parameterMode == ComputerParameterMode.POSITION_MODE) {
+            value = this._stack[value];
         }
-        catch (e) {
-            return 0;
+        else if (parameterMode == ComputerParameterMode.RELATIVE_MODE) {
+            value = this._stack[value + this.relativeBaseOffset];
         }
+
+        return value ?? 0;
     }
 
     /**
@@ -131,6 +142,16 @@ module.exports = class Stack {
         this.pointer = newAddress;
     }
 
+    /**
+     * Adjust the Relative Base Offset by an amount
+     *
+     * @param {number} adjustment A positive or negative integer to add to the Relative Base Offset
+     * @returns {void}
+     */
+    AdjustRelativeBaseOffset(adjustment) {
+        this.relativeBaseOffset += adjustment;
+    }
+
     /**
      * Return the whole stack
      *