diff options
Diffstat (limited to 'engine/rtc')
-rw-r--r-- | engine/rtc/rtc.asm | 210 | ||||
-rw-r--r-- | engine/rtc/timeset.asm | 722 |
2 files changed, 932 insertions, 0 deletions
diff --git a/engine/rtc/rtc.asm b/engine/rtc/rtc.asm new file mode 100644 index 00000000..e56bed45 --- /dev/null +++ b/engine/rtc/rtc.asm @@ -0,0 +1,210 @@ +Unreferenced_StopRTC: + ld a, SRAM_ENABLE + ld [MBC3SRamEnable], a + call LatchClock + ld a, RTC_DH + ld [MBC3SRamBank], a + ld a, [MBC3RTC] + set 6, a ; halt + 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 6, a ; halt + 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 + +Unreferenced_1404e: + 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 7, [hl] + ld a, BANK(sRTCStatusFlags) + ld [MBC3SRamBank], a + xor a + ld [sRTCStatusFlags], a + call CloseSRAM + ret + +StartClock:: + call _GetClock + call GetClock + call _FixDays + call FixDays + jr nc, .skip_set + ; bit 5: Day count exceeds 139 + ; bit 6: Day count exceeds 255 + call RecordRTCStatus ; set flag on sRTCStatusFlags + +.skip_set + call StartRTC + ret + +_FixDays: + ld hl, hRTCDayHi + bit 7, [hl] + jr nz, .set_bit_7 + bit 6, [hl] + jr nz, .set_bit_7 + xor a + ret + +.set_bit_7 + ; Day count exceeds 16383 + ld a, %10000000 + call RecordRTCStatus ; set bit 7 on sRTCStatusFlags + ret + +_GetClock: + ld a, SRAM_ENABLE + ld [MBC3SRamEnable], a + call LatchClock + ld a, RTC_DH + ld [MBC3SRamBank], a + ld a, [MBC3RTC] + push af + call CloseSRAM + pop af + bit 6, a ; halt + ret z + + ld a, BANK(sRTCStatusFlags) + call OpenSRAM + ld a, $34 + ld [sUnusedRTCMinutes], a + ld a, $12 + ld [sUnusedRTCHours], a + call CloseSRAM + ret + +ClockContinue: + call CheckRTCStatus + ld c, a + and %11000000 ; Day count exceeded 255 or 16383 + jr nz, .time_overflow + + ld a, c + and %00100000 ; Day count exceeded 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 + 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 diff --git a/engine/rtc/timeset.asm b/engine/rtc/timeset.asm new file mode 100644 index 00000000..b89d1af7 --- /dev/null +++ b/engine/rtc/timeset.asm @@ -0,0 +1,722 @@ +TIMESET_UP_ARROW EQU "♂" ; $ef +TIMESET_DOWN_ARROW EQU "♀" ; $f5 + +InitClock: +; Ask the player to set the time. + ldh a, [hInMenu] + push af + ld a, $1 + ldh [hInMenu], a + + ld a, $0 + ld [wSpriteUpdatesEnabled], a + ld a, $10 + ld [wMusicFade], a + ld a, LOW(MUSIC_NONE) + ld [wMusicFadeID], a + ld a, HIGH(MUSIC_NONE) + ld [wMusicFadeID + 1], a + ld c, 8 + call DelayFrames + call RotateFourPalettesLeft + call ClearTilemap + call ClearSprites + xor a + ldh [hBGMapMode], a + call LoadStandardFont + ld de, TimeSetBackgroundGFX + ld hl, vTiles2 tile $00 + lb bc, BANK(TimeSetBackgroundGFX), 1 + call Request1bpp + ld de, TimeSetUpArrowGFX + ld hl, vTiles2 tile $01 + lb bc, BANK(TimeSetUpArrowGFX), 1 + call Request1bpp + ld de, TimeSetDownArrowGFX + ld hl, vTiles2 tile $02 + lb bc, BANK(TimeSetDownArrowGFX), 1 + call Request1bpp + call .ClearScreen + call WaitBGMap + call RotateFourPalettesRight + ld hl, OakTimeWokeUpText + call PrintText + ld hl, wTimeSetBuffer + ld bc, wTimeSetBufferEnd - wTimeSetBuffer + xor a + call ByteFill + ld a, 10 ; default hour = 10 AM + ld [wInitHourBuffer], a + +.loop + ld hl, OakTimeWhatTimeIsItText + call PrintText + hlcoord 3, 7 + lb bc, 2, 15 + call Textbox + hlcoord 11, 7 + ld [hl], $1 + hlcoord 11, 10 + ld [hl], $2 + hlcoord 4, 9 + call DisplayHourOClock + ld c, 10 + call DelayFrames + +.SetHourLoop: + call JoyTextDelay + call SetHour + jr nc, .SetHourLoop + + ld a, [wInitHourBuffer] + ld [wStringBuffer2 + 1], a + call .ClearScreen + ld hl, OakTimeWhatHoursText + call PrintText + call YesNoBox + jr nc, .HourIsSet + call .ClearScreen + jr .loop + +.HourIsSet: + ld hl, OakTimeHowManyMinutesText + call PrintText + hlcoord 11, 7 + lb bc, 2, 7 + call Textbox + hlcoord 15, 7 + ld [hl], $1 + hlcoord 15, 10 + ld [hl], $2 + hlcoord 12, 9 + call DisplayMinutesWithMinString + ld c, 10 + call DelayFrames + +.SetMinutesLoop: + call JoyTextDelay + call SetMinutes + jr nc, .SetMinutesLoop + + ld a, [wInitMinuteBuffer] + ld [wStringBuffer2 + 2], a + call .ClearScreen + ld hl, OakTimeWhoaMinutesText + call PrintText + call YesNoBox + jr nc, .MinutesAreSet + call .ClearScreen + jr .HourIsSet + +.MinutesAreSet: + call InitTimeOfDay + ld hl, OakText_ResponseToSetTime + call PrintText + call WaitPressAorB_BlinkCursor + pop af + ldh [hInMenu], a + ret + +.ClearScreen: + xor a + ldh [hBGMapMode], a + hlcoord 0, 0 + ld bc, SCREEN_HEIGHT * SCREEN_WIDTH + xor a + call ByteFill + ld a, $1 + ldh [hBGMapMode], a + ret + +SetHour: + ldh a, [hJoyPressed] + and A_BUTTON + jr nz, .Confirm + + ld hl, hJoyLast + ld a, [hl] + and D_UP + jr nz, .up + ld a, [hl] + and D_DOWN + jr nz, .down + call DelayFrame + and a + ret + +.down + ld hl, wInitHourBuffer + ld a, [hl] + and a + jr nz, .DecreaseThroughMidnight + ld a, 23 + 1 +.DecreaseThroughMidnight: + dec a + ld [hl], a + jr .okay + +.up + ld hl, wInitHourBuffer + ld a, [hl] + cp 23 + jr c, .AdvanceThroughMidnight + ld a, -1 +.AdvanceThroughMidnight: + inc a + ld [hl], a + +.okay + hlcoord 4, 9 + ld a, " " + ld bc, 15 + call ByteFill + hlcoord 4, 9 + call DisplayHourOClock + call WaitBGMap + and a + ret + +.Confirm: + scf + ret + +DisplayHourOClock: + push hl + ld a, [wInitHourBuffer] + ld c, a + ld e, l + ld d, h + call PrintHour + inc hl + ld de, String_oclock + call PlaceString + pop hl + ret + +UnreferencedFunction907ba: + ld h, d + ld l, e + push hl + call DisplayHourOClock + pop de + inc de + inc de + ld a, ":" + ld [de], a + inc de + push de + ld hl, 3 + add hl, de + ld a, [de] + inc de + ld [hli], a + ld a, [de] + ld [hl], a + pop hl + call DisplayMinutesWithMinString + inc hl + inc hl + inc hl + ret + +SetMinutes: + ldh a, [hJoyPressed] + and A_BUTTON + jr nz, .a_button + ld hl, hJoyLast + ld a, [hl] + and D_UP + jr nz, .d_up + ld a, [hl] + and D_DOWN + jr nz, .d_down + call DelayFrame + and a + ret + +.d_down + ld hl, wInitMinuteBuffer + ld a, [hl] + and a + jr nz, .decrease + ld a, 59 + 1 +.decrease + dec a + ld [hl], a + jr .finish_dpad + +.d_up + ld hl, wInitMinuteBuffer + ld a, [hl] + cp 59 + jr c, .increase + ld a, -1 +.increase + inc a + ld [hl], a +.finish_dpad + hlcoord 12, 9 + ld a, " " + ld bc, 7 + call ByteFill + hlcoord 12, 9 + call DisplayMinutesWithMinString + call WaitBGMap + and a + ret +.a_button + scf + ret + +DisplayMinutesWithMinString: + ld de, wInitMinuteBuffer + call PrintTwoDigitNumberLeftAlign + inc hl + ld de, String_min + call PlaceString + ret + +PrintTwoDigitNumberLeftAlign: + push hl + ld a, " " + ld [hli], a + ld [hl], a + pop hl + lb bc, PRINTNUM_LEFTALIGN | 1, 2 + call PrintNum + ret + +OakTimeWokeUpText: + text_far _OakTimeWokeUpText + text_end + +OakTimeWhatTimeIsItText: + text_far _OakTimeWhatTimeIsItText + text_end + +String_oclock: + db "o'clock@" + +OakTimeWhatHoursText: + ; What?@ @ + text_far _OakTimeWhatHoursText + text_asm + hlcoord 1, 16 + call DisplayHourOClock + ld hl, .OakTimeHoursQuestionMarkText + ret + +.OakTimeHoursQuestionMarkText: + text_far _OakTimeHoursQuestionMarkText + text_end + +OakTimeHowManyMinutesText: + text_far _OakTimeHowManyMinutesText + text_end + +String_min: + db "min.@" + +OakTimeWhoaMinutesText: + ; Whoa!@ @ + text_far _OakTimeWhoaMinutesText + text_asm + hlcoord 7, 14 + call DisplayMinutesWithMinString + ld hl, .OakTimeMinutesQuestionMarkText + ret + +.OakTimeMinutesQuestionMarkText: + text_far _OakTimeMinutesQuestionMarkText + text_end + +OakText_ResponseToSetTime: + text_asm + decoord 1, 14 + ld a, [wInitHourBuffer] + ld c, a + call PrintHour + ld [hl], ":" + inc hl + ld de, wInitMinuteBuffer + lb bc, PRINTNUM_LEADINGZEROS | 1, 2 + call PrintNum + ld b, h + ld c, l + ld a, [wInitHourBuffer] + cp MORN_HOUR + jr c, .nite + cp DAY_HOUR + 1 + jr c, .morn + cp NITE_HOUR + jr c, .day +.nite + ld hl, .OakTimeSoDarkText + ret +.morn + ld hl, .OakTimeOversleptText + ret +.day + ld hl, .OakTimeYikesText + ret + +.OakTimeOversleptText: + text_far _OakTimeOversleptText + text_end + +.OakTimeYikesText: + text_far _OakTimeYikesText + text_end + +.OakTimeSoDarkText: + text_far _OakTimeSoDarkText + text_end + +TimeSetBackgroundGFX: +INCBIN "gfx/new_game/timeset_bg.1bpp" +TimeSetUpArrowGFX: +INCBIN "gfx/new_game/up_arrow.1bpp" +TimeSetDownArrowGFX: +INCBIN "gfx/new_game/down_arrow.1bpp" + +SetDayOfWeek: + ldh a, [hInMenu] + push af + ld a, $1 + ldh [hInMenu], a + ld de, TimeSetUpArrowGFX + ld hl, vTiles0 tile TIMESET_UP_ARROW + lb bc, BANK(TimeSetUpArrowGFX), 1 + call Request1bpp + ld de, TimeSetDownArrowGFX + ld hl, vTiles0 tile TIMESET_DOWN_ARROW + lb bc, BANK(TimeSetDownArrowGFX), 1 + call Request1bpp + xor a + ld [wTempDayOfWeek], a +.loop + hlcoord 0, 12 + lb bc, 4, 18 + call Textbox + call LoadStandardMenuHeader + ld hl, .OakTimeWhatDayIsItText + call PrintText + hlcoord 9, 3 + lb bc, 2, 9 + call Textbox + hlcoord 14, 3 + ld [hl], TIMESET_UP_ARROW + hlcoord 14, 6 + ld [hl], TIMESET_DOWN_ARROW + hlcoord 10, 5 + call .PlaceWeekdayString + call ApplyTilemap + ld c, 10 + call DelayFrames +.loop2 + call JoyTextDelay + call .GetJoypadAction + jr nc, .loop2 + call ExitMenu + call UpdateSprites + ld hl, .ConfirmWeekdayText + call PrintText + call YesNoBox + jr c, .loop + ld a, [wTempDayOfWeek] + ld [wStringBuffer2], a + call InitDayOfWeek + call LoadStandardFont + pop af + ldh [hInMenu], a + ret + +.GetJoypadAction: + ldh a, [hJoyPressed] + and A_BUTTON + jr z, .not_A + scf + ret + +.not_A + ld hl, hJoyLast + ld a, [hl] + and D_UP + jr nz, .d_up + ld a, [hl] + and D_DOWN + jr nz, .d_down + call DelayFrame + and a + ret + +.d_down + ld hl, wTempDayOfWeek + ld a, [hl] + and a + jr nz, .decrease + ld a, SATURDAY + 1 + +.decrease + dec a + ld [hl], a + jr .finish_dpad + +.d_up + ld hl, wTempDayOfWeek + ld a, [hl] + cp 6 + jr c, .increase + ld a, SUNDAY - 1 + +.increase + inc a + ld [hl], a + +.finish_dpad + xor a + ldh [hBGMapMode], a + hlcoord 10, 4 + lb bc, 2, 9 + call ClearBox + hlcoord 10, 5 + call .PlaceWeekdayString + call WaitBGMap + and a + ret + +.PlaceWeekdayString: + push hl + ld a, [wTempDayOfWeek] + ld e, a + ld d, 0 + ld hl, .WeekdayStrings + add hl, de + add hl, de + ld a, [hli] + ld d, [hl] + ld e, a + pop hl + call PlaceString + ret + +.WeekdayStrings: +; entries correspond to wCurDay constants (see constants/wram_constants.asm) + dw .Sunday + dw .Monday + dw .Tuesday + dw .Wednesday + dw .Thursday + dw .Friday + dw .Saturday + dw .Sunday + +.Sunday: db " SUNDAY@" +.Monday: db " MONDAY@" +.Tuesday: db " TUESDAY@" +.Wednesday: db "WEDNESDAY@" +.Thursday: db "THURSDAY@" +.Friday: db " FRIDAY@" +.Saturday: db "SATURDAY@" + +.OakTimeWhatDayIsItText: + text_far _OakTimeWhatDayIsItText + text_end + +.ConfirmWeekdayText: + text_asm + hlcoord 1, 14 + call .PlaceWeekdayString + ld hl, .OakTimeIsItText + ret + +.OakTimeIsItText: + text_far _OakTimeIsItText + text_end + +InitialSetDSTFlag: + ld a, [wDST] + set 7, a + ld [wDST], a + predef UpdateTimePredef + hlcoord 1, 14 + lb bc, 3, 18 + call ClearBox + ld hl, .Text + call PlaceHLTextAtBC + ret + +.Text: + text_asm + call UpdateTime + ldh a, [hHours] + ld b, a + ldh a, [hMinutes] + ld c, a + decoord 1, 14 + farcall PrintHoursMins + ld hl, .DSTIsThatOKText + ret + +.DSTIsThatOKText: + text_far _DSTIsThatOKText + text_end + +InitialClearDSTFlag: + ld a, [wDST] + res 7, a + ld [wDST], a + predef UpdateTimePredef + hlcoord 1, 14 + lb bc, 3, 18 + call ClearBox + ld hl, .Text + call PlaceHLTextAtBC + ret + +.Text: + text_asm + call UpdateTime + ldh a, [hHours] + ld b, a + ldh a, [hMinutes] + ld c, a + decoord 1, 14 + farcall PrintHoursMins + ld hl, .TimeAskOkayText + ret + +.TimeAskOkayText: + text_far _TimeAskOkayText + text_end + +MrChrono: + hlcoord 1, 14 + lb bc, 3, SCREEN_WIDTH - 2 + call ClearBox + ld hl, .Text + call PlaceHLTextAtBC + ret + +.Text: + text_asm + call UpdateTime + + hlcoord 1, 14 + ld [hl], "R" + inc hl + ld [hl], "T" + inc hl + ld [hl], " " + inc hl + + ld de, hRTCDayLo + call .PrintTime + + hlcoord 1, 16 + ld [hl], "D" + inc hl + ld [hl], "F" + inc hl + ld [hl], " " + inc hl + + ld de, wStartDay + call .PrintTime + + ld [hl], " " + inc hl + + ld a, [wDST] + bit 7, a + jr z, .off + + ld [hl], "O" + inc hl + ld [hl], "N" + inc hl + jr .done + +.off + ld [hl], "O" + inc hl + ld [hl], "F" + inc hl + ld [hl], "F" + inc hl + +.done + ld hl, .NowOnDebug + ret + +.NowOnDebug: + text "<PARA>Now on DEBUG…" + prompt + +.PrintTime: + lb bc, 1, 3 + call PrintNum + ld [hl], "." + inc hl + inc de + lb bc, PRINTNUM_LEADINGZEROS | 1, 2 + call PrintNum + ld [hl], ":" + inc hl + inc de + lb bc, PRINTNUM_LEADINGZEROS | 1, 2 + call PrintNum + ret + +PrintHour: + ld l, e + ld h, d + push bc + call GetTimeOfDayString + call PlaceString + ld l, c + ld h, b + inc hl + pop bc + call AdjustHourForAMorPM + ld [wDeciramBuffer], a + ld de, wDeciramBuffer + call PrintTwoDigitNumberLeftAlign + ret + +GetTimeOfDayString: + ld a, c + cp MORN_HOUR + jr c, .nite + cp DAY_HOUR + jr c, .morn + cp NITE_HOUR + jr c, .day +.nite + ld de, .nite_string + ret +.morn + ld de, .morn_string + ret +.day + ld de, .day_string + ret + +.nite_string: db "NITE@" +.morn_string: db "MORN@" +.day_string: db "DAY@" + +AdjustHourForAMorPM: +; Convert the hour stored in c (0-23) to a 1-12 value + ld a, c + or a + jr z, .midnight + cp NOON_HOUR + ret c + ret z + sub NOON_HOUR + ret + +.midnight + ld a, NOON_HOUR + ret |