branch method

void branch(
  1. bool testResult
)

Implementation

void branch(bool testResult) {
  assert(testResult is bool);

  //calculates the local jump offset (ref 4.7)

  final jumpByte = readb();
  int offset;

  if (BinaryHelper.isSet(jumpByte, 6)) {
    //single byte offset
    offset = BinaryHelper.bottomBits(jumpByte, 6);
  } else {
    //create the 14-bit offset value with next byte
    final val = (BinaryHelper.bottomBits(jumpByte, 6) << 8) | readb();

    //convert to Dart signed int (14-bit MSB is the sign bit)
    offset = ((val & 0x2000) == 0x2000) ? -(16384 - val) : val;
  }

  //Debugger.verbose('    (branch condition: $branchOn)');

  //compare test result to branchOn (true|false) bit
  if (BinaryHelper.isSet(jumpByte, 7) == testResult) {
    // If the offset is 0 or 1 (FALSE or TRUE), perform a return
    // operation.
    if (offset == Engine.gameFalse || offset == Engine.gameTrue) {
      doReturn(offset);
      return;
    }

    //jump to the offset and continue...
    programCounter += (offset - 2);
    //Debugger.verbose('    (branching to 0x${pc.toRadixString(16)})');
  }

  //otherwise just continue to the next instruction...
  //Debugger.verbose('    (continuing to next instruction)');
}