scanTable method

void scanTable()

Implementation

void scanTable() {
  //v4 expects only 3 operands, x table len
  final operands = visitOperandsVar(4, true);

  final searchWord = operands[0].value;
  final tableAddress = operands[1].value;
  final tableLength = operands[2].value;

  if (operands.length == 3) {
    operands.add(Operand(OperandType.small)..rawValue = 0x82);
  }

  if (operands.length != 4) {
    throw GameException(
        "scan_table() expected 4 operands.  Found: ${operands.length}");
  }

  final form = operands[3].value!;

  log.fine(
      "scan_table operands: search: $searchWord, table: $tableAddress, table-length: $tableLength, form: $form");

  log.fine(BinaryHelper.isSet(form, 7)
      ? "form is set for word scanning"
      : "form is set for byte scanning");

  if (BinaryHelper.isSet(form, 7)) {
    log.fine("..word scan");
    var addr = tableAddress;
    for (var i = 0; i < tableLength!; i++) {
      final value = mem.loadw(addr!);
      if (value == searchWord) {
        log.fine("...found match");
        final resultTo = readb();
        writeVariable(resultTo, addr);
        branch(true);
        return;
      }
      addr += form & 0x7f; // little trick I picked up from Frotz...
    }
  } else {
    log.fine("..byte scan");
    //byte scan
    for (var i = 0; i < tableLength!; i++) {
      var addr = tableAddress;
      for (var i = 0; i < tableLength; i++) {
        final value = mem.loadb(addr!);
        if (value == searchWord) {
          log.fine("...found match");
          final resultTo = readb();
          writeVariable(resultTo, addr);
          branch(true);
          return;
        }
        addr += form & 0x7f;
      }
    }
  }

  doReturn(0);
}