2013-08-29 23:32:03 +00:00
|
|
|
; Functions relating to the timer interrupt and the real-time-clock.
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
AskTimer::
|
2013-08-29 23:32:03 +00:00
|
|
|
push af
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hMobile]
|
2013-08-29 23:32:03 +00:00
|
|
|
and a
|
2015-11-18 04:29:14 +00:00
|
|
|
jr z, .not_mobile
|
2013-08-29 23:32:03 +00:00
|
|
|
call Timer
|
|
|
|
|
2015-11-18 04:29:14 +00:00
|
|
|
.not_mobile
|
2013-08-29 23:32:03 +00:00
|
|
|
pop af
|
|
|
|
reti
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
LatchClock::
|
2013-08-29 23:32:03 +00:00
|
|
|
; latch clock counter data
|
|
|
|
ld a, 0
|
|
|
|
ld [MBC3LatchClock], a
|
|
|
|
ld a, 1
|
|
|
|
ld [MBC3LatchClock], a
|
|
|
|
ret
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
UpdateTime::
|
2013-08-29 23:32:03 +00:00
|
|
|
call GetClock
|
|
|
|
call FixDays
|
|
|
|
call FixTime
|
2017-12-24 17:47:30 +00:00
|
|
|
farcall GetTimeOfDay
|
2013-08-29 23:32:03 +00:00
|
|
|
ret
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
GetClock::
|
2013-08-29 23:32:03 +00:00
|
|
|
; store clock data in hRTCDayHi-hRTCSeconds
|
|
|
|
|
|
|
|
; enable clock r/w
|
|
|
|
ld a, SRAM_ENABLE
|
|
|
|
ld [MBC3SRamEnable], a
|
|
|
|
|
|
|
|
; clock data is 'backwards' in hram
|
|
|
|
|
|
|
|
call LatchClock
|
|
|
|
ld hl, MBC3SRamBank
|
|
|
|
ld de, MBC3RTC
|
|
|
|
|
|
|
|
ld [hl], RTC_S
|
|
|
|
ld a, [de]
|
2016-05-16 01:50:31 +00:00
|
|
|
maskbits 60
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh [hRTCSeconds], a
|
2013-08-29 23:32:03 +00:00
|
|
|
|
|
|
|
ld [hl], RTC_M
|
|
|
|
ld a, [de]
|
2016-05-16 01:50:31 +00:00
|
|
|
maskbits 60
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh [hRTCMinutes], a
|
2013-08-29 23:32:03 +00:00
|
|
|
|
|
|
|
ld [hl], RTC_H
|
|
|
|
ld a, [de]
|
2016-05-16 01:50:31 +00:00
|
|
|
maskbits 24
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh [hRTCHours], a
|
2013-08-29 23:32:03 +00:00
|
|
|
|
|
|
|
ld [hl], RTC_DL
|
|
|
|
ld a, [de]
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh [hRTCDayLo], a
|
2013-08-29 23:32:03 +00:00
|
|
|
|
|
|
|
ld [hl], RTC_DH
|
|
|
|
ld a, [de]
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh [hRTCDayHi], a
|
2013-08-29 23:32:03 +00:00
|
|
|
|
|
|
|
; unlatch clock / disable clock r/w
|
|
|
|
call CloseSRAM
|
|
|
|
ret
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
FixDays::
|
2013-08-29 23:32:03 +00:00
|
|
|
; fix day count
|
|
|
|
; mod by 140
|
|
|
|
|
|
|
|
; check if day count > 255 (bit 8 set)
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hRTCDayHi] ; DH
|
2013-08-29 23:32:03 +00:00
|
|
|
bit 0, a
|
|
|
|
jr z, .daylo
|
|
|
|
; reset dh (bit 8)
|
|
|
|
res 0, a
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh [hRTCDayHi], a ; DH
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
; mod 140
|
|
|
|
; mod twice since bit 8 (DH) was set
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hRTCDayLo] ; DL
|
2013-08-29 23:32:03 +00:00
|
|
|
.modh
|
|
|
|
sub 140
|
|
|
|
jr nc, .modh
|
|
|
|
.modl
|
|
|
|
sub 140
|
|
|
|
jr nc, .modl
|
|
|
|
add 140
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
; update dl
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh [hRTCDayLo], a ; DL
|
2013-08-29 23:32:03 +00:00
|
|
|
|
2015-11-20 16:47:52 +00:00
|
|
|
; flag for sRTCStatusFlags
|
2015-11-10 16:05:26 +00:00
|
|
|
ld a, %01000000
|
2013-08-29 23:32:03 +00:00
|
|
|
jr .set
|
|
|
|
|
|
|
|
.daylo
|
|
|
|
; quit if fewer than 140 days have passed
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hRTCDayLo] ; DL
|
2013-08-29 23:32:03 +00:00
|
|
|
cp 140
|
|
|
|
jr c, .quit
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
; mod 140
|
|
|
|
.mod
|
|
|
|
sub 140
|
|
|
|
jr nc, .mod
|
|
|
|
add 140
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
; update dl
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh [hRTCDayLo], a ; DL
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2015-11-20 16:47:52 +00:00
|
|
|
; flag for sRTCStatusFlags
|
2015-11-10 16:05:26 +00:00
|
|
|
ld a, %00100000
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
.set
|
|
|
|
; update clock with modded day value
|
|
|
|
push af
|
|
|
|
call SetClock
|
|
|
|
pop af
|
|
|
|
scf
|
|
|
|
ret
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
.quit
|
|
|
|
xor a
|
|
|
|
ret
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
FixTime::
|
2013-08-29 23:32:03 +00:00
|
|
|
; add ingame time (set at newgame) to current time
|
|
|
|
; day hr min sec
|
2018-01-23 22:39:09 +00:00
|
|
|
; store time in wCurDay, hHours, hMinutes, hSeconds
|
2013-08-29 23:32:03 +00:00
|
|
|
|
|
|
|
; second
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hRTCSeconds] ; S
|
2013-08-29 23:32:03 +00:00
|
|
|
ld c, a
|
2018-01-23 22:39:09 +00:00
|
|
|
ld a, [wStartSecond]
|
2013-08-29 23:32:03 +00:00
|
|
|
add c
|
|
|
|
sub 60
|
|
|
|
jr nc, .updatesec
|
|
|
|
add 60
|
|
|
|
.updatesec
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh [hSeconds], a
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
; minute
|
|
|
|
ccf ; carry is set, so turn it off
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hRTCMinutes] ; M
|
2013-08-29 23:32:03 +00:00
|
|
|
ld c, a
|
2018-01-23 22:39:09 +00:00
|
|
|
ld a, [wStartMinute]
|
2013-08-29 23:32:03 +00:00
|
|
|
adc c
|
|
|
|
sub 60
|
|
|
|
jr nc, .updatemin
|
|
|
|
add 60
|
|
|
|
.updatemin
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh [hMinutes], a
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
; hour
|
|
|
|
ccf ; carry is set, so turn it off
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hRTCHours] ; H
|
2013-08-29 23:32:03 +00:00
|
|
|
ld c, a
|
2018-01-23 22:39:09 +00:00
|
|
|
ld a, [wStartHour]
|
2013-08-29 23:32:03 +00:00
|
|
|
adc c
|
|
|
|
sub 24
|
|
|
|
jr nc, .updatehr
|
|
|
|
add 24
|
|
|
|
.updatehr
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh [hHours], a
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
; day
|
|
|
|
ccf ; carry is set, so turn it off
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hRTCDayLo] ; DL
|
2013-08-29 23:32:03 +00:00
|
|
|
ld c, a
|
2018-01-23 22:39:09 +00:00
|
|
|
ld a, [wStartDay]
|
2013-08-29 23:32:03 +00:00
|
|
|
adc c
|
2018-01-23 22:39:09 +00:00
|
|
|
ld [wCurDay], a
|
2013-08-29 23:32:03 +00:00
|
|
|
ret
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
InitTimeOfDay::
|
2013-08-29 23:32:03 +00:00
|
|
|
xor a
|
2018-01-23 22:39:09 +00:00
|
|
|
ld [wStringBuffer2], a
|
2015-11-18 04:29:14 +00:00
|
|
|
ld a, $0 ; useless
|
2018-01-23 22:39:09 +00:00
|
|
|
ld [wStringBuffer2 + 3], a
|
2015-12-19 01:07:09 +00:00
|
|
|
jr InitTime
|
2013-08-29 23:32:03 +00:00
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
InitDayOfWeek::
|
2013-08-29 23:32:03 +00:00
|
|
|
call UpdateTime
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hHours]
|
2018-01-23 22:39:09 +00:00
|
|
|
ld [wStringBuffer2 + 1], a
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hMinutes]
|
2018-01-23 22:39:09 +00:00
|
|
|
ld [wStringBuffer2 + 2], a
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hSeconds]
|
2018-01-23 22:39:09 +00:00
|
|
|
ld [wStringBuffer2 + 3], a
|
2015-12-19 01:07:09 +00:00
|
|
|
jr InitTime ; useless
|
2013-08-29 23:32:03 +00:00
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
InitTime::
|
2017-12-24 17:47:30 +00:00
|
|
|
farcall _InitTime
|
2013-08-29 23:32:03 +00:00
|
|
|
ret
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
PanicResetClock::
|
2015-11-18 04:29:14 +00:00
|
|
|
call .ClearhRTC
|
2013-08-29 23:32:03 +00:00
|
|
|
call SetClock
|
|
|
|
ret
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
.ClearhRTC:
|
2013-08-29 23:32:03 +00:00
|
|
|
xor a
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh [hRTCSeconds], a
|
|
|
|
ldh [hRTCMinutes], a
|
|
|
|
ldh [hRTCHours], a
|
|
|
|
ldh [hRTCDayLo], a
|
|
|
|
ldh [hRTCDayHi], a
|
2013-08-29 23:32:03 +00:00
|
|
|
ret
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
SetClock::
|
2013-08-29 23:32:03 +00:00
|
|
|
; set clock data from hram
|
|
|
|
|
|
|
|
; enable clock r/w
|
|
|
|
ld a, SRAM_ENABLE
|
|
|
|
ld [MBC3SRamEnable], a
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
; set clock data
|
|
|
|
; stored 'backwards' in hram
|
|
|
|
|
|
|
|
call LatchClock
|
|
|
|
ld hl, MBC3SRamBank
|
|
|
|
ld de, MBC3RTC
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
; seems to be a halt check that got partially commented out
|
|
|
|
; this block is totally pointless
|
|
|
|
ld [hl], RTC_DH
|
|
|
|
ld a, [de]
|
|
|
|
bit 6, a ; halt
|
|
|
|
ld [de], a
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
; seconds
|
|
|
|
ld [hl], RTC_S
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hRTCSeconds]
|
2013-08-29 23:32:03 +00:00
|
|
|
ld [de], a
|
|
|
|
; minutes
|
|
|
|
ld [hl], RTC_M
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hRTCMinutes]
|
2013-08-29 23:32:03 +00:00
|
|
|
ld [de], a
|
|
|
|
; hours
|
|
|
|
ld [hl], RTC_H
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hRTCHours]
|
2013-08-29 23:32:03 +00:00
|
|
|
ld [de], a
|
|
|
|
; day lo
|
|
|
|
ld [hl], RTC_DL
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hRTCDayLo]
|
2013-08-29 23:32:03 +00:00
|
|
|
ld [de], a
|
|
|
|
; day hi
|
|
|
|
ld [hl], RTC_DH
|
2018-08-25 18:28:22 +00:00
|
|
|
ldh a, [hRTCDayHi]
|
2013-08-29 23:32:03 +00:00
|
|
|
res 6, a ; make sure timer is active
|
|
|
|
ld [de], a
|
2016-04-10 18:42:14 +00:00
|
|
|
|
2013-08-29 23:32:03 +00:00
|
|
|
; cleanup
|
|
|
|
call CloseSRAM ; unlatch clock, disable clock r/w
|
|
|
|
ret
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
ClearRTCStatus::
|
2015-11-20 16:47:52 +00:00
|
|
|
; clear sRTCStatusFlags
|
2013-08-29 23:32:03 +00:00
|
|
|
xor a
|
|
|
|
push af
|
2015-11-20 16:47:52 +00:00
|
|
|
ld a, BANK(sRTCStatusFlags)
|
2013-08-29 23:32:03 +00:00
|
|
|
call GetSRAMBank
|
|
|
|
pop af
|
2015-11-20 16:47:52 +00:00
|
|
|
ld [sRTCStatusFlags], a
|
2013-08-29 23:32:03 +00:00
|
|
|
call CloseSRAM
|
|
|
|
ret
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
RecordRTCStatus::
|
2015-11-20 16:47:52 +00:00
|
|
|
; append flags to sRTCStatusFlags
|
|
|
|
ld hl, sRTCStatusFlags
|
2013-08-29 23:32:03 +00:00
|
|
|
push af
|
2015-11-20 16:47:52 +00:00
|
|
|
ld a, BANK(sRTCStatusFlags)
|
2013-08-29 23:32:03 +00:00
|
|
|
call GetSRAMBank
|
|
|
|
pop af
|
|
|
|
or [hl]
|
|
|
|
ld [hl], a
|
|
|
|
call CloseSRAM
|
|
|
|
ret
|
|
|
|
|
2018-06-24 14:09:41 +00:00
|
|
|
CheckRTCStatus::
|
2015-11-20 16:47:52 +00:00
|
|
|
; check sRTCStatusFlags
|
|
|
|
ld a, BANK(sRTCStatusFlags)
|
2013-08-29 23:32:03 +00:00
|
|
|
call GetSRAMBank
|
2015-11-20 16:47:52 +00:00
|
|
|
ld a, [sRTCStatusFlags]
|
2013-08-29 23:32:03 +00:00
|
|
|
call CloseSRAM
|
|
|
|
ret
|