```       Name: AddTimeToTimer                                          [Show more]
Type: Subroutine
Category: Drivers
Summary: Add time to the specified timer
Deep dive: Scheduling tasks in the main loop
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:

Arguments:

X                    The timer to increment:

* 0 = the clock timer
(clockMinutes clockSeconds clockTenths)

* 1 = the lap timer
(lapMinutes lapSeconds lapTenths)

Returns:

C flag               Denotes whether the number of seconds has changed:

* Set if the time just ticked on to the next second

* Clear if the time is unchanged

SED                    \ Set the D flag to switch arithmetic to Binary Coded
\ Decimal (BCD)

LDA #&09               \ Set A = &09, so we add 9/100 of a second below

BNE time1              \ following

\ we need to apply the clock adjustment, as this gets
\ applied every trackTimerAdjust iterations around the
\ main driving loop
\
\ the clock twice as fast as usual, for this tick only

LDA #&18               \ Set A = &18, so we add 18/100 of a second below

.time1

CLC                    \ Add A to the tenths for the timer
STA clockTenths,X      \ starting with the tenths of a second

PHP                    \ Store the C flag on the stack, so we can return it
\ from the subroutine below (the C flag will be set
\ if the lap time just ticked on to the next second)

LDA clockSeconds,X     \ Then we add the seconds into A

CMP #&60               \ If A < &60, then the number of seconds is still valid,
BCC time2              \ so jump to time2 to skip the following instruction

LDA #0                 \ Otherwise set A = 0, so 60 seconds on the timer
\ increments back round to 0 seconds

.time2

STA clockSeconds,X     \ Update the seconds value for the timer

LDA clockMinutes,X     \ Finally, we add the minutes
STA clockMinutes,X

BPL time3              \ If the updates minutes value for the timer is

JSR ZeroTimer          \ Otherwise the timer just reached the maximum possible
\ value, so wrap it back round to zero

LDY currentPlayer      \ Set the total race time for the current player to &80,
LDA #&80               \ or 80 minutes (as totalRaceMinutes is BCD), otherwise
STA totalRaceMinutes,Y \ the driver might finish with a very low time just
\ because the race timer looped back to zero

.time3

PLP                    \ Retrieve the C flag from the stack, which will be set
\ if the addition of tenths overflowed (in other words,
\ if the lap time just ticked on to the next second)

CLD                    \ Clear the D flag to switch arithmetic to normal

RTS                    \ Return from the subroutine
```