VM_RTC_LATCH
is a GBVM instruction used to latch (capture) the current time values from the Game Boy Color’s (GBC) Real-Time Clock (RTC) into internal registers, making them available for reading.
Purpose:
The RTC is a continuously running clock. To ensure that you get a consistent reading of the time (e.g., all components like seconds, minutes, hours, and days correspond to the exact same moment), you need to “latch” the values. VM_RTC_LATCH
performs this operation. It is essential for:
When VM_RTC_LATCH
is called, the current values of the RTC’s time components are copied into a set of internal, accessible registers. Subsequent VM_RTC_GET
calls will read from these latched registers until VM_RTC_LATCH
is called again.
Syntax:
VM_RTC_LATCH
VM_RTC_LATCH
takes no arguments. It simply performs the latching operation.
Usage Example: Reading a Consistent Timestamp
Imagine you want to record the exact time a player saves their game, including seconds, minutes, and hours. To ensure these values are all from the same moment, you would latch the RTC before reading each component.
; In your game save routine:
; Variables to store the saved time components
VAR_SAVE_SECONDS:
.R_INT8 0
VAR_SAVE_MINUTES:
.R_INT8 0
VAR_SAVE_HOURS:
.R_INT8 0
SAVE_GAME_ROUTINE:
; Latch the RTC to get a consistent snapshot of the current time
VM_RTC_LATCH
; Read each time component from the latched RTC values
VM_RTC_GET VAR_SAVE_SECONDS, .RTC_SECONDS
VM_RTC_GET VAR_SAVE_MINUTES, .RTC_MINUTES
VM_RTC_GET VAR_SAVE_HOURS, .RTC_HOURS
; Now, VAR_SAVE_SECONDS, VAR_SAVE_MINUTES, and VAR_SAVE_HOURS
; all represent the time at which VM_RTC_LATCH was called.
; ... proceed to save these time values along with other game data ...
VM_LOAD_TEXT TEXT_GAME_SAVED_TIME
VM_DISPLAY_TEXT
VM_IDLE 60
VM_RET
TEXT_GAME_SAVED_TIME:
.TEXT "Game saved at: "
.TEXT_END
; RTC component constants
.RTC_SECONDS:
.R_INT8 0
.RTC_MINUTES:
.R_INT8 1
.RTC_HOURS:
.R_INT8 2
In this example, VM_RTC_LATCH
is called once at the beginning of the save routine. This ensures that all subsequent VM_RTC_GET
calls retrieve time components from the same moment in time, providing an accurate timestamp for the save file. Without VM_RTC_LATCH
, there’s a small chance that the RTC could tick over between reading seconds and minutes, leading to an incorrect timestamp.
Analogy to Game Boy Development: This is a direct interface with the Game Boy Color’s Real-Time Clock hardware. In modern programming, it’s analogous to taking a snapshot of the system time or freezing a timestamp to ensure consistency when reading multiple time components. It’s a critical step for accurate time-based operations on hardware where the clock is continuously running and might update between successive reads.