Skip to navigation

Revs on the BBC Micro

Main Loop: ResetVariables

Name: ResetVariables [Show more] Type: Subroutine Category: Main Loop Summary: Reset a number of variables for driving, and print the top two text lines
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * MainDrivingLoop (Part 1 of 5) calls ResetVariables
.ResetVariables LDA #0 \ Set A = 0, so we can use it to reset variables to zero \ in the following loops LDX #&68 \ We start by zeroing all zero-page variables from \ playerMoving to processContact, so set up a loop \ counter in X .rese1 STA playerMoving,X \ Zero the X-th byte from playerMoving DEX \ Decrement the loop counter BPL rese1 \ Loop back until we have zeroed all variables from \ playerMoving to processContact LDX #&7F \ We now zero all variables from xPlayerCoordHi to \ zTyreForceBoth, so set up a loop counter in X .rese2 STA xPlayerCoordHi,X \ Zero the X-th byte from xPlayerCoordHi DEX \ Decrement the loop counter BPL rese2 \ Loop back until we have zeroed all variables from \ xPlayerCoordHi to zTyreForceBoth JSR DefineEnvelope \ Define the first (and only) sound envelope LDX #23 \ We now zero the 24-byte blocks at objTrackSection and \ objSectionSegmt, and initialise all 24 bytes in \ (objectSegmentHi objectSegmentLo), so set up a loop \ counter in X STX previousSignNumber \ Set previousSignNumber = 23 .rese3 LDA trackStartLine+1 \ Set the X-th byte of (objectSegmentHi objectSegmentLo) STA objectSegmentHi,X \ to the value of trackStartLine, which is 843 for the LDA trackStartLine \ Silverstone track and contains the segment number of STA objectSegmentLo,X \ the starting line, expressed as the number of segments \ between the starting line and the start of section 0, \ counting forwards round the track LDA #0 \ Zero the X-th byte of objTrackSection STA objTrackSection,X STA objSectionSegmt,X \ Zero the X-th byte of objSectionSegmt DEX \ Decrement the loop counter BPL rese3 \ Loop back until we have zeroed or copied all 24 \ variable bytes JSR SetPlayerPositions \ Set the current player's position in currentPosition, \ plus the number of the position ahead in positionAhead \ and number of the position behind in positionBehind LDA #1 \ Set A = 1, to pass to PlaceCarsOnTrack as the spacing \ for the cars when this is a race BIT raceStarted \ If bit 7 of raceStarted is set then this is a race BMI rese4 \ rather than practice or qualifying, so jump to rese4 \ to skip the following \ This is a practice or qualifying lap, so we set the \ player's position as specified in the track data LDX trackStartPosition \ Set X to trackStartPosition, the starting position for \ practice or qualifying laps, which is 4 for the \ Silverstone track LDY currentPosition \ Set Y to the current player's position JSR SwapDriverPosition \ Swap the positions of drivers in positions X and Y in \ the driversInOrder table, so for Silverstone this sets \ the current player's position to 4 (i.e. fifth place) JSR SetPlayerPositions \ Set the current player's position in currentPosition, \ plus the number of the position ahead in positionAhead \ and number of the position behind in positionBehind LDA trackCarSpacing \ Set A to trackCarSpacing, to pass to PlaceCarsOnTrack \ as the spacing for the cars when this is a qualifying \ lap (this value is 40 for the Silverstone track) .rese4 \ By this point, A = 1 if this is a race, or the value \ of trackCarSpacing if this is practice or qualifying \ (40 for Silverstone) JSR PlaceCarsOnTrack \ Reset the cars on the track, placing them on the \ starting grid if this is a race, or spread out along \ the track if this is a qualifying lap LDX #19 \ We now zero the 20-byte blocks at driverLapNumber, \ carSteering, carProgress, (carSpeedHi carSpeedLo) and \ carStatus, and initialise the 20-byte blocks at \ objectStatus, totalRaceMinutes and carSectionSpeed, \ so set up a loop counter in X .rese5 LDA #%10000000 \ Set the X-th byte of objectStatus to %10000000, so the STA objectStatus,X \ object is hidden STA totalRaceMinutes,X \ Set the X-th byte of totalRaceMinutes to -1 LDA #0 \ Zero the X-th byte of driverLapNumber STA driverLapNumber,X STA carSteering,X \ Zero the X-th byte of carSteering STA carProgress,X \ Zero the X-th byte of carProgress STA carSpeedHi,X \ Zero the X-th byte of carSpeedHi STA carStatus,X \ Zero the X-th byte of carStatus STA carSpeedLo,X \ Zero the X-th byte of carSpeedLo LDA #255 \ Set the X-th byte of carSectionSpeed to 255 STA carSectionSpeed,X DEX \ Decrement the loop counter BPL rese5 \ Loop back until we have zeroed or initialised all 20 \ bytes in each block LDA #1 \ Set gearNumber = 1, to set the gears to neutral STA gearNumber STA pastHalfway \ Set pastHalfway = 1, so the player is in the second \ half of the track (which is true for both practice \ and qualifying, as well as the starting grid) LDX #7 \ Set oddsOfEngineStart = 7 STX oddsOfEngineStart DEX \ Set sectionListValid = 6 STX sectionListValid STX sectionListPointer \ Set sectionListPointer = 6 DEX \ We now zero the six bytes at mirrorContents to clear \ all six segments of the wing mirrors, so set X = 5 to \ use as a loop counter .rese6 STA mirrorContents,X \ Clear the contents of the X-th wing mirror segment DEX \ Decrement the loop counter BPL rese6 \ Loop back until we have zeroed all six mirror segments JSR PrintGearNumber \ Print the new gear number on the gear stick (neutral) LDA raceStarted \ If bit 7 of raceStarted is set then this is a race BMI rese7 \ rather than practice or qualifying, so jump to rese7 \ with bit 7 of A set and bit 6 of A clear, to print the \ race header at the top of the screen LDX #40 \ Blank out the first text line at the top of the screen JSR PrintSecondLineGap \ and print token 40 on the second line, to give: \ \ " " \ "Lap Time : Best Time " LDX #1 \ Zero the lap timer JSR ZeroTimer JSR PrintBestLapTime \ Print the best lap time and the current lap time at \ the top of the screen LDA #&DF \ Set firstLapStarted = -33 STA firstLapStarted RTS \ Return from the subroutine .rese7 STA updateDrivingInfo \ Set bit 7 and clear bit 6 of updateDrivingInfo so the \ lap number gets printed at the top of the screen STA updateDriverInfo \ Set bit 7 of updateDriverInfo so the driver names get \ printed at the top of the screen LDX #43 \ Print token 43 on the first text line at the top of JSR PrintFirstLine \ the screen and token 44 on the second line, to give: LDX #44 \ JSR PrintSecondLine \ "Position In front: " \ "Laps to go Behind: " \ \ Token 44 includes five extra spaces at the end, though \ I'm not sure why LDA currentPosition \ Set A to the current player's position JSR ConvertNumberToBCD \ Convert the number in A into binary coded decimal \ (BCD), adding 1 in the process STA positionChangeBCD \ Set positionChangeBCD to the current player's position \ in BCD RTS \ Return from the subroutine