private static int runToAddressOrNextInputFrameHiOrLoLimit(State initial, int joypadInputAdd, int limit, int... addresses) {
    if (!curGb.onFrameBoundaries) {
      int add = curGb.step(0, addresses); // end unfinished frame
      if (add != 0)
        return add;
    }

    if (initial == null)
      initial = curGb.newState();

    int initialSteps = curGb.stepCount;
    int lastVframe = runToAddressOrInputHiOrLoLimit(joypadInputAdd, limit, addresses);
//    System.out.println("lastVframe=" + lastVframe);
    if (lastVframe != 0) {
      curGb.restore(initial);
      runFor(lastVframe - initialSteps, 0, 0);
      return 0;
    }
    curGb.restore(initial);
    int ret = runToAddressLimit(0, 0, limit, addresses);
//    System.out.println("ret=" + ret);
    return ret == 0 ? -1 : ret;
  }