private static int runToNextInputFrameHiLoLimit(int limit) {
    int joypadInputFirst = Math.min(curGb.rom.readJoypadInputHi, curGb.rom.readJoypadInputLo);
    int joypadInputLast = Math.max(curGb.rom.readJoypadInputHi, curGb.rom.readJoypadInputLo);

    if (!curGb.onFrameBoundaries)
      curGb.step(); // end unfinished frame

    State initial = curGb.newState();
    int initialSteps = curGb.stepCount;
    int lastVframe;
    int ret;

    int add = runToAddressLimit(0, 0, limit, joypadInputFirst);
    while (true) {
      if (add == 0 || curGb.stepCount - initialSteps > limit)
        return 0;
      lastVframe = curGb.stepCount - 1; // subtract started frame
      if (curGb.step(0, joypadInputLast) == 0) { // Assumption: joypadInputLast will come before curGb.rom.readJoypadAddress
        System.out.println("WARNING: joypadInputFirst not is same frame as joypadInputLast! Need two-frame press.");
        ret = 2;
      } else {
        ret = 1;
        add = curGb.step(0, curGb.rom.readJoypadAddress);
        if (add != 0)
          break;
      }
      add = runToAddressLimit(0, 0, limit, joypadInputFirst, curGb.rom.readJoypadAddress);
      if (add == curGb.rom.readJoypadAddress)
        break;
    }

    curGb.restore(initial);
    runFor(lastVframe - initialSteps, 0, 0);
    return ret;
  }