ComputerParameterMode.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const { DecimalPlaceIsNonZero, GetDecimalInPlace } = require("./common");
  2. module.exports = {
  3. /**
  4. * Position Mode
  5. *
  6. * When the int computer is in Position mode, the value at the pointer
  7. * is interpreted as another address in memory.
  8. */
  9. POSITION_MODE: 0,
  10. /**
  11. * Immediate Mode
  12. *
  13. * When the int computer is in Immediate Mode, the value at the pointer
  14. * is interpreted as its literal value.
  15. */
  16. IMMEDIATE_MODE: 1,
  17. /**
  18. * Relative Mode
  19. *
  20. * Unknown.
  21. */
  22. RELATIVE_MODE: 2,
  23. /**
  24. * Parse an opcode to get the Parameter Mode for an operation's parameter
  25. *
  26. * Note: `parameterIndex` is a 1-based index, not 0-based.
  27. *
  28. * @example
  29. * // POSITION_MODE
  30. * const firstParamMode = ComputerParameterMode.ParseParameterMode(1002, 1);
  31. * // IMMEDIATE_MODE
  32. * const secondParamMode = ComputerParameterMode.ParseParameterMode(1002, 2);
  33. *
  34. * @param {number} rawOpcode The opcode in memory
  35. * @param {number} parameterIndex The index of the parameter in the function arguments
  36. * @returns {number} Either POSITION_MODE or IMMEDIATE_MODE
  37. */
  38. ParseParameterMode: function (rawOpcode, parameterIndex) {
  39. const paramModeFromOpcode = GetDecimalInPlace(rawOpcode, parameterIndex + 1);
  40. switch (paramModeFromOpcode) {
  41. case 0: return this.POSITION_MODE;
  42. case 1: return this.IMMEDIATE_MODE;
  43. case 2: return this.RELATIVE_MODE;
  44. default: return this.POSITION_MODE;
  45. }
  46. },
  47. };