What the Berry glitch is and how it was fixed
Posted by: Háčky
Date: 2015-02-08 09:30:22
The Berry glitch is a glitch only found in Pokémon Ruby and Sapphire which stops the game's internal clockThe 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:
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 (099, which represent the years 20002099), month (112), day (131), hour (023), minute (059), and second (059). (It also gives the day of the week, but the game doesnt use it.) Because the game doesnt care about the calendar, and maybe because Game Freak cant 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 RTCs year-month-day to the save files day number looks something like this:
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 wrongsince the RTCs years start from 0, the loop ought to run while [tt]i >= 0[/tt].
If the save file says your planted Berries are going to grow on day 367, and the RTC says its currently day 1, youre 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, theres 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 Furlocks Forest. The in-game clock adjustment function which can be activated through Mystery Events might also be able to help, but I havent investigated that possibility.