Glitch City Laboratories Archives

Glitch City Laboratories closed on 1 September 2020 (announcement). This is an archived copy of a thread from Glitch City Laboratories Forums.

You can join Glitch City Research Institute to ask questions or discuss current developments.

You may also download the archive of this forum in .tar.gz, .sql.gz, or .sqlite.gz formats.

Generation III Glitch Discussion

What the Berry glitch is and how it was fixed - Page 1

What the Berry glitch is and how it was fixed

Posted by: Háčky
Date: 2015-02-08 09:30:22
Bulbapedia currently describes the Berry glitch like this:
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,


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.

Re: What the Berry glitch is and how it was fixed

Posted by: Stackout
Date: 2015-02-08 10:29:51
That's some nice reversing there.

Re: What the Berry glitch is and how it was fixed

Posted by: SatoMew
Date: 2015-02-08 13:59:06
Wait, so which Japanese and English versions of Ruby and Sapphire have the bug already fixed? What about the European localizations?

Re: What the Berry glitch is and how it was fixed

Posted by: Bert
Date: 2015-02-08 15:49:52
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.

Re: What the Berry glitch is and how it was fixed

Posted by: Háčky
Date: 2015-02-08 17:47:30

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.

Re: What the Berry glitch is and how it was fixed

Posted by: Stackout
Date: 2015-02-09 05:35:06
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?

Re: What the Berry glitch is and how it was fixed

Posted by: Háčky
Date: 2015-02-09 07:09:53

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?

Theres an unreleased Mystery Event that causes the message The in-game clock adjustment function is now useable. to be displayed. After its 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.

Re: What the Berry glitch is and how it was fixed

Posted by: SatoMew
Date: 2015-02-09 08:37:51

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.


Theres an unreleased Mystery Event that causes the message The in-game clock adjustment function is now useable. to be displayed. After its 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?

[img]http://i.imgur.com/jaazxNb.png[/img]

After clicking "Reset RTC", it resets all values to 0 except for the day under "initial time", which is set to 1.

Re: What the Berry glitch is and how it was fixed

Posted by: Háčky
Date: 2015-02-09 11:31:35
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 Id known that earlier; it wouldve made it easy to spot the bug ;D)


Theres an unreleased Mystery Event that causes the message The in-game clock adjustment function is now useable. to be displayed. After its 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?

Thats the one. If you say yes, you get this screen. (In English, its 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 youre 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 dont think its 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.

Re: What the Berry glitch is and how it was fixed

Posted by: SatoMew
Date: 2015-02-09 15:26:55

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 Id known that earlier; it wouldve 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 youre 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 dont think its 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.

Re: What the Berry glitch is and how it was fixed

Posted by: Stackout
Date: 2015-02-09 16:26:00

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.

Re: What the Berry glitch is and how it was fixed

Posted by: SatoMew
Date: 2015-02-09 18:45:17

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.

Re: What the Berry glitch is and how it was fixed

Posted by: Háčky
Date: 2015-02-09 20:40:36
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 dont think changing Time Passed, as A-Saves 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.

Re: What the Berry glitch is and how it was fixed

Posted by: SatoMew
Date: 2015-02-10 16:26:09

I dont think changing Time Passed, as A-Saves 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 :)

Re: What the Berry glitch is and how it was fixed

Posted by: xparasite9
Date: 2016-04-14 21:34:52


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?

Theres an unreleased Mystery Event that causes the message The in-game clock adjustment function is now useable. to be displayed. After its 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.