-
Notifications
You must be signed in to change notification settings - Fork 817
/
rtc.asm
194 lines (171 loc) · 2.72 KB
/
rtc.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
StopRTC: ; unreferenced
ld a, SRAM_ENABLE
ld [MBC3SRamEnable], a
call LatchClock
ld a, RTC_DH
ld [MBC3SRamBank], a
ld a, [MBC3RTC]
set RTC_DH_HALT, a
ld [MBC3RTC], a
call CloseSRAM
ret
StartRTC:
ld a, SRAM_ENABLE
ld [MBC3SRamEnable], a
call LatchClock
ld a, RTC_DH
ld [MBC3SRamBank], a
ld a, [MBC3RTC]
res RTC_DH_HALT, a
ld [MBC3RTC], a
call CloseSRAM
ret
GetTimeOfDay::
; get time of day based on the current hour
ldh a, [hHours] ; hour
ld hl, TimesOfDay
.check
; if we're within the given time period,
; get the corresponding time of day
cp [hl]
jr c, .match
; else, get the next entry
inc hl
inc hl
; try again
jr .check
.match
; get time of day
inc hl
ld a, [hl]
ld [wTimeOfDay], a
ret
TimesOfDay:
; hours for the time of day
; 0400-0959 morn | 1000-1759 day | 1800-0359 nite
db MORN_HOUR, NITE_F
db DAY_HOUR, MORN_F
db NITE_HOUR, DAY_F
db MAX_HOUR, NITE_F
db -1, MORN_F
BetaTimesOfDay: ; unreferenced
db 20, NITE_F
db 40, MORN_F
db 60, DAY_F
db -1, MORN_F
StageRTCTimeForSave:
call UpdateTime
ld hl, wRTC
ld a, [wCurDay]
ld [hli], a
ldh a, [hHours]
ld [hli], a
ldh a, [hMinutes]
ld [hli], a
ldh a, [hSeconds]
ld [hli], a
ret
SaveRTC:
ld a, SRAM_ENABLE
ld [MBC3SRamEnable], a
call LatchClock
ld hl, MBC3RTC
ld a, RTC_DH
ld [MBC3SRamBank], a
res RTC_DH_OVERFLOW, [hl]
ld a, BANK(sRTCStatusFlags)
ld [MBC3SRamBank], a
xor a
ld [sRTCStatusFlags], a
call CloseSRAM
ret
StartClock::
call GetClock
call _FixDays
call FixDays
jr nc, .skip_set
call RecordRTCStatus
.skip_set
call StartRTC
ret
_FixDays:
ld hl, hRTCDayHi
bit RTC_DH_OVERFLOW, [hl]
jr nz, .reset_rtc
bit RTC_DH_HALT, [hl]
jr nz, .reset_rtc
xor a
ret
.reset_rtc
ld a, RTC_RESET
call RecordRTCStatus
ret
ClockContinue:
call CheckRTCStatus
ld c, a
and RTC_RESET | RTC_DAYS_EXCEED_255
jr nz, .time_overflow
ld a, c
and RTC_DAYS_EXCEED_139
jr z, .dont_update
call UpdateTime
ld a, [wRTC + 0]
ld b, a
ld a, [wCurDay]
cp b
jr c, .dont_update
.time_overflow
farcall ClearDailyTimers
farcall Function170923
ld a, BANK(s5_aa8c) ; aka BANK(s5_b2fa)
call OpenSRAM
ld a, [s5_aa8c]
inc a
ld [s5_aa8c], a
ld a, [s5_b2fa]
inc a
ld [s5_b2fa], a
call CloseSRAM
ret
.dont_update
xor a
ret
_InitTime::
call GetClock
call FixDays
ld hl, hRTCSeconds
ld de, wStartSecond
ld a, [wStringBuffer2 + 3]
sub [hl]
dec hl
jr nc, .okay_secs
add 60
.okay_secs
ld [de], a
dec de
ld a, [wStringBuffer2 + 2]
sbc [hl]
dec hl
jr nc, .okay_mins
add 60
.okay_mins
ld [de], a
dec de
ld a, [wStringBuffer2 + 1]
sbc [hl]
dec hl
jr nc, .okay_hrs
add 24
.okay_hrs
ld [de], a
dec de
ld a, [wStringBuffer2]
sbc [hl]
dec hl
jr nc, .okay_days
add 140
ld c, 7
call SimpleDivide
.okay_days
ld [de], a
ret