private static int runToInputHiOrLoLimit(int joypadInputAdd, int limit) {
    int initialSteps = curGb.stepCount;
    int lastVframe;

    int add = runToAddressLimit(0, 0, limit, joypadInputAdd);
    while (true) {
      if (add == 0 || curGb.stepCount - initialSteps > limit)
        return 0;
      lastVframe = curGb.stepCount - 1; // subtract started frame
      add = curGb.step(0, curGb.rom.readJoypadAddress);
      if (add != 0)
        return lastVframe;
      add = runToAddressLimit(0, 0, limit, joypadInputAdd, curGb.rom.readJoypadAddress);
      if (add == curGb.rom.readJoypadAddress)
        return lastVframe;
    }
  }