Skip to navigation


Drivers: UpdateLaps

Name: UpdateLaps [Show more] Type: Subroutine Category: Drivers Summary: Increment the lap number and lap times for a specific driver
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * MoveObjectForward calls UpdateLaps

Update the lap number and lap times for driver X, but only if the following are true: * Driver number is in the range 0 to 19 * Bit 7 of updateLapTimes is clear * Bit 6 of the driver's car's object status byte is clear (so the car is still racing) * If this is the current player, pastHalfway must be 1 (so the player is in the second half of the track) If these conditions are met, then the lap is incremented.
Arguments: X Driver number (only drivers 0 to 19 have lap times) updateLapTimes If bit 7 is set, this routine does nothing
.UpdateLaps BIT updateLapTimes \ If bit 7 of updateLapTimes is set, jump to ulap1 to BMI ulap1 \ return from the subroutine without doing anything CPX #20 \ If X >= 20 then this is not a driver number, so jump BCS ulap1 \ to ulap1 to return from the subroutine LDA objectStatus,X \ If bit 6 of the driver's car object status byte is ASL A \ set, then the car has finished racing, so jump to BMI ulap1 \ ulap1 to return from the subroutine without updating \ the lap number CPX currentPlayer \ If driver X is not the current player, jump to ulap3 BNE ulap3 \ to skip updating the lap number top of the screen \ If we get here then this is the current player, so now \ we check the value of pastHalfway DEC pastHalfway \ If pastHalfway = 1, then the player is in the second BEQ ulap2 \ half of the track, so set pastHalfway to 0 as the INC pastHalfway \ player has just passwd the starting line into the \ first half of the track, and jump to ulap2 to update \ the lap details .ulap1 RTS \ Return from the subroutine .ulap2 \ If we get here then this is the current player, and \ pastHalfway has just been changed from 1 to 0, to \ denote a new lap LDA #%10000000 \ Set bit 7 and clear bit 6 of updateDrivingInfo so the STA updateDrivingInfo \ lap number gets updated at the top of the screen .ulap3 \ If we get here then we have passed all the checks, so \ it's time to increment the lap number and times, \ starting with the lap number LDA driverLapNumber,X \ Set A to the current lap number for driver X BMI ulap4 \ If A is negative, skip the following instruction INC driverLapNumber,X \ Increment the current lap number for driver X .ulap4 \ We now decide whether to increment the lap times BIT raceStarted \ If bit 7 of raceStarted is clear then this is practice BPL ulap5 \ or qualifying, so jump to ulap5 \ If we get here then this is a race CMP numberOfLaps \ If the current lap number for driver X < the number BCC ulap7 \ of laps in the race, then this is not the last lap in \ the race, so jump to ulap7 BEQ ulap6 \ If the current lap number for driver X = the number \ of laps in the race, then this is the last lap in \ the race, so jump to ulap6 \ If we get here then the current lap number is bigger \ than the number of laps in the race, which means the \ driver has already finished the race and is still \ driving, so we don't increment the lap times RTS \ Return from the subroutine .ulap5 \ If we get here then this is practice or qualifying CPX currentPlayer \ If X <= the driver number of the current player, jump BEQ ulap7 \ to ulap7 BCC ulap7 \ If we get here then driver X has a bigger number than \ the current player, so we ignore it (is this because \ the only players with numbers higher than the current \ player are other players, rather than drivers) RTS \ Return from the subroutine .ulap6 \ If we get here then this is a race and this is the \ last lap in the race, so this driver just finished the \ race (as this routine is all about incrementing the \ lap number) CPX currentPlayer \ If X <> the driver number of the current player, jump BNE ulap7 \ to ulap7 \ If we get here then driver X is the current player, so \ the current player just finished the race LDA #80 \ Set leaveTrackTimer = 80, so we leave the track in 80 STA leaveTrackTimer \ main loop iterations and return to the game menu .ulap7 \ If we get here, then it's time to increment the lap \ times SED \ Set the D flag to switch arithmetic to Binary Coded \ Decimal (BCD) \ We now subtract driver X's total race time from the \ current clock time, to see whether this is a new best \ time SEC \ Set T = clockTenths - totalRaceTenths for driver X LDA clockTenths SBC totalRaceTenths,X STA T LDA clockSeconds \ Set A = clockSeconds - totalRaceSeconds for driver X SBC totalRaceSeconds,X BCS ulap8 \ If the subtraction underflowed, add 60 seconds to the ADC #&60 \ result CLC .ulap8 STA U \ Set U = A \ = clockSeconds - totalRaceSeconds for driver X LDA clockMinutes \ Set H = clockMinutes - totalRaceMinutes for driver X SBC totalRaceMinutes,X STA H \ So by this point, (H U T) contains the time difference \ between the clock time and driver X's total race time, \ which is the current lap time (as we last updated the \ driver's total time at the end of the last lap) BCC ulap9 \ If the subtraction underflowed, then somehow the clock \ timer is showing a smaller time than driver X's total \ race time, so this isn't a new best lap time, and we \ jump to ulap9 SEC \ Subtract (H U T) - driver X's best lap time LDA T SBC bestLapTenths,X LDA U SBC bestLapSeconds,X LDA H SBC bestLapMinutes,X BCS ulap9 \ If the subtraction didn't underflow, then (H U T) is \ bigger than driver X's best lap time, so this isn't a \ new best lap time, so jump to ulap9 \ (H U T) is lower than driver X's best lap time, so we \ have a new best lap time LDA T \ Set driver X's best lap time to (H U T), setting the AND #&F0 \ second digit of the tenths figure to 0 STA bestLapTenths,X LDA U STA bestLapSeconds,X LDA H STA bestLapMinutes,X .ulap9 LDA clockTenths \ Set the total race time for driver X to the clock time STA totalRaceTenths,X \ so we can use it to work out the lap time for the next LDA clockSeconds \ lap STA totalRaceSeconds,X LDA clockMinutes STA totalRaceMinutes,X CLD \ Clear the D flag to switch arithmetic to normal RTS \ Return from the subroutine NOP \ These instructions have no effect - presumably they NOP \ are left over from changes during development