Main Menu
Main Page
Forums
Recent changes
Random page
Help

Databases
GlitchDex
AttackDex
ItemDex
TypeDex
UnownDex
More

Major Glitches
Trainer escape glitch
Old man trick
Celebi trick
Select glitches (Japan)
SRAM glitch
CoolTrainer♀ corruption
LOL glitch
Rival LOL glitch
Super Glitch
ZZAZZ glitch
Pomeg corruption glitch (Glitzer Popping)
Tweaking
Elite Four door glitch (Japan)
Pokémon merge glitch
Pokémon cloning
Time Capsule exploit
Arbitrary code execution
Coin Case glitch
More

Other Glitch Categories
Glitches by generation
Glitches between two generations
Japan-only/language specific glitches
Music glitches
Natural glitches
Non-core series glitches
Non-Pokémon glitches
Officially acknowledged glitches
Recurring glitches
Dead glitches

References
Pokémon GameShark codes
The Big HEX List
GB programming
Curiosities
Debugging features
Easter eggs
Error traps
Glitch areas
Glitch myths
Non-glitch exploits
Placeholder texts
Pokémon glitch terminology
Unused content and prerelease information

Useful Tools
8F Helper
GBz80 to Items
Old man trick name generator
PATH (Prama's Advanced Tweaking Heaven)
Save file editors
Special stat/Pokémon converter
Trainer escape Trainer Pokémon finder

Affiliates
Legendary Star Blob 2 (Hakuda)
Pokémon Speedruns wiki
PRAMA Initiative
Become an affiliate!

Search Wiki

 

Search Forums

 

Author Topic: What the Berry glitch is and how it was fixed  (Read 7486 times)

0 Members and 1 Guest are viewing this topic.

Háčky

  • Distinguished Member
  • *
  • Offline Offline
  • Pick which packet as an error?
    • View Profile
What the Berry glitch is and how it was fixed
« on: February 08, 2015, 09:30:22 am »
Bulbapedia currently describes the Berry glitch like this:
Quote
The Berry glitch is a glitch only found in Pokémon Ruby and Sapphire which stops the game's internal clock…The glitch typically occurs after about a year of owning the game, or to games that have been played for over 100 hours…
Nintendo described it like this:
Quote
In Pokémon Ruby and Pokémon Sapphire, certain berry trees will stop producing berries after you've played your game for one year.

One of these turns out to be fairly accurate.

The real-time clock (RTC) in the cartridges of Pokémon Ruby, Sapphire, and Emerald reports the time as a series of binary-coded decimal bytes for the year (0–99, which represent the years 2000–2099), month (1–12), day (1–31), hour (0–23), minute (0–59), and second (0–59). (It also gives the day of the week, but the game doesn’t use it.) Because the game doesn’t care about the calendar, and maybe because Game Freak can’t do BCD math properly, the RTC value is converted to a standard integer format for the timestamp stored in the save file: a 16-bit day number followed by 8-bit hours, minutes, and seconds. In version 1.0 of Ruby and Sapphire, and English version 1.1, the conversion from the RTC’s year-month-day to the save file’s day number looks something like this:

Code: [Select]
uint16_t rtcDateToDayNumber(uint8_t * rtcDate)
{ // JP Ruby/Sapphire v1.0: $8006818
        return ymdToDayNumber(bcdToInt(*rtcDate),
                              bcdToInt(*(rtcDate + 1)),
                              bcdToInt(*(rtcDate + 2));
}

uint16_t ymdToDayNumber(uint8_t year, uint8_t month, uint8_t day)
{ // JP Ruby/Sapphire v1.0: $800678C
        uint16_t dayNumber;

        // add 365 or 366 days for each elapsed year
        for(int i = year - 1; i > 0; --i)
        {
                dayNumber += 365;
                if(isLeapYear(i))
                        dayNumber += 1;
        }

        // add days for each elapsed month this year
        for(int i = month - 1; i > 0; --i)
                dayNumber += daysInMonth[i]; // array of {31, 28, 31, 30, …}
        if(month > 2 && isLeapYear(year))
                dayNumber += 1;

        // add the elapsed days
        dayNumber += day;

        return dayNumber;
}

When you start a new game, and the RTC is set to 2000-01-01, this function will happily start counting the days in 2000 from 1 to 366. But when the RTC ticks over to the year 2001, suddenly the day number is back to 1, because the condition on the year loop is wrong—since the RTC’s years start from 0, the loop ought to run while i >= 0.

If the save file says your planted Berries are going to grow on day 367, and the RTC says it’s currently day 1, you’re going to be waiting a while. But if you have the patience to wait 366 days until the RTC reached 2002-01-02, you could finally pick your Berries then, and the system would work correctly from that point on.

Basically, all that needs to be done to fix the Berry glitch is to add a year to the RTC to make up for the year that is lost when the date rolls over from 2000-12-31 to 2001-01-01. Less basically,
  • If the date is between 2000-01-01 and 2000-02-29, add one year, so that the day number remains the same and the glitch will not be triggered at the end of the year. (2000-02-29 is changed to 2001-03-01.)
  • If the date is between 2000-03-01 and 2000-12-31, add one year and one day, so that the day number remains the same (taking into account that 2001 is not a leap year) and the glitch will not be triggered at the end of the year.
  • If the date is in the year 2001, and the save file timestamp is greater than the current RTC value, then change the date to 2002-01-02, so that time can resume from that point.
  • If the date is in 2002 or later, there’s nothing to be done.
In later versions of Ruby and Sapphire, the ymdToDayNumber function was corrected so that the loop accounts for the days elapsed in the year 2000 (you can find it at $8009180 in English Ruby v1.2). The Berry Program Update checks the version number of the ROM and says that “There is no need to update your Berry Program” if it is a fixed version.

The Berry Program Update does nothing to help with cartridges in which the battery has been replaced. These games exhibit the same symptoms as the Berry glitch, but it is caused by the RTC being reset rather than the date conversion bug. If the save file timestamp is greater than the RTC value and the date is not in 2001, then the Berry Program Update will be “Unable to update the Berry Program.” If the RTC date is in 2001 (the battery was replaced between 1 and 2 years ago), the update will only change the date to 2002-01-02, which will still not fix the issue. For time-based events to resume after the cartridge battery is replaced, another method is needed to adjust the RTC, such as the tool on Furlock’s Forest. The in-game clock adjustment function which can be activated through Mystery Events might also be able to help, but I haven’t investigated that possibility.

Wack0

  • Coder, reverser, beta collector [BetaArchive staff]
  • Staff
  • *****
  • Offline Offline
  • Gender: Male
  • cBRH - Doing nothing since 2k7
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #1 on: February 08, 2015, 10:29:51 am »
That's some nice reversing there.
C H E C K E D . B U I L D S . A R E . A W E S O M E N E S S

BetaArchiveSoftHistory Forumsirc.rol.im #galaxy,#softhistory

Also known as The Distractor.

Shane, please stop telling children that there's a Mew outside under the delivery trucks. - Management

Pokémon: arbitrary code execution 1996-2016

SatoMew

  • Member+
  • *
  • Offline Offline
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #2 on: February 08, 2015, 01:59:06 pm »
Wait, so which Japanese and English versions of Ruby and Sapphire have the bug already fixed? What about the European localizations?

Bert

  • binthiswayforyears
  • Member+
  • *
  • Offline Offline
  • Gender: Male
  • Hindsight - it's still stinging.
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #3 on: February 08, 2015, 03:49:52 pm »
The glitch exists in all versions, from what I understand. You could also connect it to Colosseum's bonus disc, FR/LG, or Emerald version. GameFreak also let players send in the cart to fix the glitch, but they don't offer that service anymore.
"Johnny’s got a gun Johnny’s got some ammo
He lost his firing pin a long time ago
Johnny’s got to push Johnny’s got that stroke
Johnny lost it all and now nobody knows
Ain’t it funny how he walks through everything
Ain’t it funny how he listens to everything
Well he’s got the gears well he’s lost the fear
Well he’s done the years so near and dear
Well he’s learned the ropes learned to cope
Lost the hope so near and dear
Well he’s got it all learned to fall
Dropped the ball so near and dear"
-Headstones, 'Firing Pin'

The Story of Glitchy, the 4. . | Meganium Man! | MEGANIUM ADVENTURES! (MSPA)

Háčky

  • Distinguished Member
  • *
  • Offline Offline
  • Pick which packet as an error?
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #4 on: February 08, 2015, 05:47:30 pm »
Wait, so which Japanese and English versions of Ruby and Sapphire have the bug already fixed? What about the European localizations?
Japanese 1.1, English 1.2, and European 1.1.

Wack0

  • Coder, reverser, beta collector [BetaArchive staff]
  • Staff
  • *****
  • Offline Offline
  • Gender: Male
  • cBRH - Doing nothing since 2k7
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #5 on: February 09, 2015, 05:35:06 am »
BTW, you mentioned an "in-game clock adjustment function", but I don't think I've heard of that. I assume it's an e-Reader card because you said it's to do with Mystery Events?
C H E C K E D . B U I L D S . A R E . A W E S O M E N E S S

BetaArchiveSoftHistory Forumsirc.rol.im #galaxy,#softhistory

Also known as The Distractor.

Shane, please stop telling children that there's a Mew outside under the delivery trucks. - Management

Pokémon: arbitrary code execution 1996-2016

Háčky

  • Distinguished Member
  • *
  • Offline Offline
  • Pick which packet as an error?
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #6 on: February 09, 2015, 07:09:53 am »
BTW, you mentioned an "in-game clock adjustment function", but I don't think I've heard of that. I assume it's an e-Reader card because you said it's to do with Mystery Events?
There’s an unreleased Mystery Event that causes the message “The in-game clock adjustment function is now useable.” to be displayed. After it’s activated, pressing Left + Select + B on the title screen will open a screen which asks “Reset RTC?” and then invites you to “Please reset the time” with options to adjust the day number, hours, minutes, and seconds. For obvious reasons, it was never actually released in the form of an e-Reader card, but one could be made. From my limited experimentation, I think it only adjusts the save file timestamp rather than the RTC itself.

SatoMew

  • Member+
  • *
  • Offline Offline
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #7 on: February 09, 2015, 08:37:51 am »
Japanese 1.1, English 1.2, and European 1.1.

Interesting. English Ruby v1.1 also fixed the typo in the Pokédex entry of Mawile (they had misspelled it as MAWHILE) but since there's an English Sapphire v1.1, I don't know what else was changed.

DAT-O-MATIC considers English v1.1 of both games as being the UK release while English v1.2 is considered a global release. And it seems like it doesn't have an entry for Japanese Ruby/Sapphire v1.1. It does have an entry for Japanese FireRed v1.1 but not for Japanese LeafGreen v1.1, which is odd.

There’s an unreleased Mystery Event that causes the message “The in-game clock adjustment function is now useable.” to be displayed. After it’s activated, pressing Left + Select + B on the title screen will open a screen which asks “Reset RTC?” and then invites you to “Please reset the time” with options to adjust the day number, hours, minutes, and seconds. For obvious reasons, it was never actually released in the form of an e-Reader card, but one could be made. From my limited experimentation, I think it only adjusts the save file timestamp rather than the RTC itself.

Is this the screen you're referring to? KazoWAR's A-Save has an utility which reads the same data, but from what I understand, the in-game function is more limited in scope, correct?



After clicking "Reset RTC", it resets all values to 0 except for the day under "initial time", which is set to 1.
« Last Edit: February 24, 2015, 01:02:36 pm by SatoMew »

Háčky

  • Distinguished Member
  • *
  • Offline Offline
  • Pick which packet as an error?
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #8 on: February 09, 2015, 11:31:35 am »
Interesting. English Ruby v1.1 also fixed the typo in the Pokédex entry of Mawile (they had misspelled it as MAWHILE) but since there's an English Sapphire v1.1, I don't know what else was changed.
Neither do I.

DAT-O-MATIC considers English v1.1 of both games as being the UK release while English v1.2 is considered a global release. And it seems like it doesn't have an entry for Japanese Ruby/Sapphire v1.1. It does have an entry for Japanese FireRed v1.1 but not for Japanese LeafGreen v1.1, which is odd.
They have two v1.1 “bad dumps” listed under the entries for Japanese v1.0. I just looked into those and am fairly certain that the “PROPER GBA-COS” releases (MD5 Ruby 20449ADAB693BF966BF0F4A907102FB9 and Sapphire ABF042BE32E2AEAB1B926EFE6D4B8B8B) are accurate dumps. Only four bytes are changed between Japanese v1.0 and v1.1: two bytes in the cartridge header to update the version number and two bytes in the ymdToDateNumber function to fix the Berry glitch. The difference between the verified dumps of English v1.1 and v1.2 is these same four bytes. (I wish I’d known that earlier; it would’ve made it easy to spot the bug ;D)

There’s an unreleased Mystery Event that causes the message “The in-game clock adjustment function is now useable.” to be displayed. After it’s activated, pressing Left + Select + B on the title screen will open a screen which asks “Reset RTC?” and then invites you to “Please reset the time” with options to adjust the day number, hours, minutes, and seconds. For obvious reasons, it was never actually released in the form of an e-Reader card, but one could be made. From my limited experimentation, I think it only adjusts the save file timestamp rather than the RTC itself.
Is this the screen you're referring to?
That’s the one. If you say yes, you get this screen. (In English, it’s “Please reset the time.”)

KazoWAR's A-Save has an utility which reads the same data, but from what I understand, the in-game function is more limited in scope, correct?
The in-game function only allows a single value to be changed, and I did some more experiments to figure out exactly what it does with that value. The “Please reset the time” screen initially shows the value of the “Previous time in game” from the prior screen, or “Time Passed” as A-Save calls it. After you choose a new value, the Time Passed is changed to that value, and the Initial Time is also changed to the current RTC value minus the Time Passed, so that the “Present time in game” is the same as the value you set. I believe the actual RTC date is completely unchanged, but it would be best to confirm this on an actual cartridge.

If you use this screen to change the time to something greater than the actual RTC value, then the Initial Time will become negative, because you’re saying that the save file was created before the RTC was (re)started. From what I can tell, the game still calculates the present time correctly when Initial Time is negative. Therefore, I don’t think it’s necessary to actually change the RTC in order to fix a new-battery save file; just running the in-game clock adjustment ought to cause time-based events to resume.

Activating the in-game clock adjustment function on a real cartridge could potentially be done either by save manipulation or by printing a custom e-Reader card. After replacing the battery and adjusting the clock, a v1.0 or EN v1.1 Ruby/Sapphire cartridge would also need the Berry Program Update to prevent the Berry glitch from happening when the RTC reaches 2001.
« Last Edit: February 09, 2015, 11:35:01 am by Háčky »

SatoMew

  • Member+
  • *
  • Offline Offline
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #9 on: February 09, 2015, 03:26:55 pm »
They have two v1.1 “bad dumps” listed under the entries for Japanese v1.0. I just looked into those and am fairly certain that the “PROPER GBA-COS” releases (MD5 Ruby 20449ADAB693BF966BF0F4A907102FB9 and Sapphire ABF042BE32E2AEAB1B926EFE6D4B8B8B) are accurate dumps. Only four bytes are changed between Japanese v1.0 and v1.1: two bytes in the cartridge header to update the version number and two bytes in the ymdToDateNumber function to fix the Berry glitch. The difference between the verified dumps of English v1.1 and v1.2 is these same four bytes. (I wish I’d known that earlier; it would’ve made it easy to spot the bug ;D)

The SHA-1 hashes I got are 971e0d670a95e5b32240b2deed20405b8daddf47 (Ruby JP v1.1) and 01f509671445965236ac4c6b5a354fe2f1e69f13 (Sapphire JP v1.1). Do they match yours? The MD5 and CRC32 hashes are the same here.

The in-game function only allows a single value to be changed, and I did some more experiments to figure out exactly what it does with that value. The “Please reset the time” screen initially shows the value of the “Previous time in game” from the prior screen, or “Time Passed” as A-Save calls it. After you choose a new value, the Time Passed is changed to that value, and the Initial Time is also changed to the current RTC value minus the Time Passed, so that the “Present time in game” is the same as the value you set. I believe the actual RTC date is completely unchanged, but it would be best to confirm this on an actual cartridge.

If you use this screen to change the time to something greater than the actual RTC value, then the Initial Time will become negative, because you’re saying that the save file was created before the RTC was (re)started. From what I can tell, the game still calculates the present time correctly when Initial Time is negative. Therefore, I don’t think it’s necessary to actually change the RTC in order to fix a new-battery save file; just running the in-game clock adjustment ought to cause time-based events to resume.

Activating the in-game clock adjustment function on a real cartridge could potentially be done either by save manipulation or by printing a custom e-Reader card. After replacing the battery and adjusting the clock, a v1.0 or EN v1.1 Ruby/Sapphire cartridge would also need the Berry Program Update to prevent the Berry glitch from happening when the RTC reaches 2001.

Thank you for the detailed post. Is A-Save's "Reset RTC" option recommended? I wish I could try this out with my Ruby (AGB-AXVP-EUR), Sapphire (AGB-AXPP-EUR), and Emerald (AGB-BPEP-EUR) cartridges since they all have dead batteries. Thing is, I don't have the hardware to extract and send save data from and to retail cartridges.

Wack0

  • Coder, reverser, beta collector [BetaArchive staff]
  • Staff
  • *****
  • Offline Offline
  • Gender: Male
  • cBRH - Doing nothing since 2k7
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #10 on: February 09, 2015, 04:26:00 pm »
Thing is, I don't have the hardware to extract and send save data from and to retail cartridges.

You can use a phat DS/DS lite with a flashcart and the GBA Backup Tool homebrew.
C H E C K E D . B U I L D S . A R E . A W E S O M E N E S S

BetaArchiveSoftHistory Forumsirc.rol.im #galaxy,#softhistory

Also known as The Distractor.

Shane, please stop telling children that there's a Mew outside under the delivery trucks. - Management

Pokémon: arbitrary code execution 1996-2016

SatoMew

  • Member+
  • *
  • Offline Offline
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #11 on: February 09, 2015, 06:45:17 pm »
You can use a phat DS/DS lite with a flashcart and the GBA Backup Tool homebrew.

Oh, neat! I tried the tool and it's awesome since it can back up the ROM as well.

Háčky

  • Distinguished Member
  • *
  • Offline Offline
  • Pick which packet as an error?
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #12 on: February 09, 2015, 08:40:36 pm »
The SHA-1 hashes I got are 971e0d670a95e5b32240b2deed20405b8daddf47 (Ruby JP v1.1) and 01f509671445965236ac4c6b5a354fe2f1e69f13 (Sapphire JP v1.1). Do they match yours? The MD5 and CRC32 hashes are the same here.
Yes.

Thank you for the detailed post. Is A-Save's "Reset RTC" option recommended? I wish I could try this out with my Ruby (AGB-AXVP-EUR), Sapphire (AGB-AXPP-EUR), and Emerald (AGB-BPEP-EUR) cartridges since they all have dead batteries.
I don’t think changing Time Passed, as A-Save’s “Reset RTC” does, is a good idea. After you replace the battery, I think you should change the Initial Time to 1 minus Time Passed. (In A-Save, the negative day number needs to be entered as 65536 minus the number.) For a battery that was replaced a while ago, it would be best to use the in-game clock adjustment, because it takes the current RTC value into account.

SatoMew

  • Member+
  • *
  • Offline Offline
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #13 on: February 10, 2015, 04:26:09 pm »
I don’t think changing Time Passed, as A-Save’s “Reset RTC” does, is a good idea. After you replace the battery, I think you should change the Initial Time to 1 minus Time Passed. (In A-Save, the negative day number needs to be entered as 65536 minus the number.) For a battery that was replaced a while ago, it would be best to use the in-game clock adjustment, because it takes the current RTC value into account.

I see, thanks :)

xparasite9

  • resigned long after being stripped of title
  • Member+
  • *
  • Offline Offline
  • FALCON...PAAAAUUUUNNNNNCH!
    • View Profile
Re: What the Berry glitch is and how it was fixed
« Reply #14 on: April 14, 2016, 08:34:52 pm »
BTW, you mentioned an "in-game clock adjustment function", but I don't think I've heard of that. I assume it's an e-Reader card because you said it's to do with Mystery Events?
There’s an unreleased Mystery Event that causes the message “The in-game clock adjustment function is now useable.” to be displayed. After it’s activated, pressing Left + Select + B on the title screen will open a screen which asks “Reset RTC?” and then invites you to “Please reset the time” with options to adjust the day number, hours, minutes, and seconds. For obvious reasons, it was never actually released in the form of an e-Reader card, but one could be made. From my limited experimentation, I think it only adjusts the save file timestamp rather than the RTC itself.
It would be greatly useful if someone could make these cards.