Advertisement
_PoY

Pokémon Crystal - On general RTC manipulation

May 1st, 2017
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. Pokémon Crystal - On general RTC manipulation
  2. ------
  3.  
  4. 0. Introduction
  5.  
  6. RTC (Real-Time Clock) is an hardware clock that runs whenever the console is alimented (ON or OFF). It features several memory addresses that track frames, seconds, minutes, hours, days. Some games use this available data for different in-game purposes, such as Pokémon Crystal, which allows with RTC usage to update days of the week for example. It appears to also have an impact on RNG (randomness in the game).
  7.  
  8. --
  9. 1. Software variables
  10.  
  11. Pokémon Crystal uses different timers for different uses. Here are some :
  12. > IGT (In-Game Time) : used to display player's progression time through the Trainer Card / save menu / Hall of Fame
  13. > RTC (Real Time Clock) : stored in the RAM of the game for some uses.
  14.  
  15. The game only stores some RTC values from the seconds : the number of RTC frames is not stored, which decreases the execution accuracy in current RTC manipulation attempts.
  16.  
  17. Some useful values are split in the game between the WRAM and the HRAM as follows.
  18.  
  19. 1.1 WRAM variables
  20.  
  21. In WRAM1, you can find values initialized during the ingame clock setup when confirming the number of minutes :
  22. > StartDay [0xD4B6]
  23. > StartHour [0xD4B7]
  24. > StartMinute [0xD4B8]
  25. > StartSecond [0xD4B9]
  26.  
  27. You can easily manipulate these values by waiting a certain amount of time from the Hard Reset.
  28. Note that having a savefile breaks any manipulation of these values
  29.  
  30. 1.2 HRAM variables
  31.  
  32. In HRAM, you can find values that act like an actual RTC clock.
  33. They are nearly always displaying the RTC values provided by the hardware (the whole game introduction freezes them)
  34. > hRTCHours [0xFF8F]
  35. > hRTCMinutes [0xFF90]
  36. > hRTCSeconds [0xFF91]
  37.  
  38. --
  39. 2. Code behaviour linked with RTC
  40.  
  41. _InitTime is a function called twice after New Game is selected.
  42. 1) When confirming the ingame clock minute during the game introduction, the function sets StartSecond, StartMinute, StartHour & StartDay.
  43. 2) When confirming the ingame day during Mom's speech, the function sets StartHour and StartDay. This lets StartSecond and StartMinute unchanged from the 1st _InitTime call.
  44.  
  45. The reason behind manipulating the RTC in order to lead to RNG manipulation comes from the funtion FixTime, called every frame (source code in Postface).
  46. This function takes hRTCSeconds and adds it to StartSecond (and does the same for hRTCMinutes and StartMinute, etc.). By doing so, the game "fixes" the ingame time by aligning the start of the game clock with the running RTC.
  47. In case these sums overflow (>=60 for seconds and minutes, >= 24 for hours), the code will execute an extra instruction. Since hRTCSeconds is updated once per second, this sum will overflow at some point and execute this instruction for every following hRTCSeconds until it reaches 0 again. This behaviour also applies to the sum hRTCMinutes + StartMinute, etc.
  48. By setting StartSecond and StartMinute to 0, the previously described sums will never overflow and thus the extra instructions will never be executed.
  49.  
  50. --
  51. 3. Current RTC manipulations
  52.  
  53. Current RTC setup sets both StartSecond and StartMinute to 0 (refered from now as "RTC0"), and is performed as follows :
  54. 0) If any save was done, clear it (doing the LID setup doesn't interfere with RTC0 setup).
  55. 1) Hard Reset and start a timer set at 63.30s for BGB and 65.30s for GameBoy Player.
  56. 2) Set ingame clock to 6:01PM.
  57. 2) Confirm ingame minute ("YES" textbox) when the timer reaches 0.
  58.  
  59. This setup sets Start variables like so :
  60. > StartDay = 0x00
  61. > StartHour = 0x12
  62. > StartMinute = 0x00
  63. > StartSecond = 0x00
  64.  
  65. Runners are currently investigating how this setup affects RNG through potential RNG manipulation attempts.
  66.  
  67. --
  68. 4. Postface
  69.  
  70. > FixTime source code
  71. FixTime:: ; 61d
  72. ; add ingame time (set at newgame) to current time
  73. ; day hr min sec
  74. ; store time in CurDay, hHours, hMinutes, hSeconds
  75.  
  76. ; second
  77. ld a, [hRTCSeconds] ; S
  78. ld c, a
  79. ld a, [StartSecond]
  80. add c
  81. sub 60
  82. jr nc, .updatesec
  83. add 60
  84. .updatesec
  85. ld [hSeconds], a
  86.  
  87. ; minute
  88. ccf ; carry is set, so turn it off
  89. ld a, [hRTCMinutes] ; M
  90. ld c, a
  91. ld a, [StartMinute]
  92. adc c
  93. sub 60
  94. jr nc, .updatemin
  95. add 60
  96. .updatemin
  97. ld [hMinutes], a
  98.  
  99. ; hour
  100. ccf ; carry is set, so turn it off
  101. ld a, [hRTCHours] ; H
  102. ld c, a
  103. ld a, [StartHour]
  104. adc c
  105. sub 24
  106. jr nc, .updatehr
  107. add 24
  108. .updatehr
  109. ld [hHours], a
  110.  
  111. ; day
  112. ccf ; carry is set, so turn it off
  113. ld a, [hRTCDayLo] ; DL
  114. ld c, a
  115. ld a, [StartDay]
  116. adc c
  117. ld [CurDay], a
  118. ret
  119. ; 658
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement