Revs on the BBC Micro

# Track geometry: ShuffleSectionList

```       Name: ShuffleSectionList                                      [Show more]
Type: Subroutine
Category: Track geometry
Summary: Shuffle the track section list along by one position
Deep dive: Data structures for the track calculations
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* GetSectionAngles (Part 1 of 3) calls ShuffleSectionList

The track section list lives in the first six bytes of the following variable
blocks, and contains data on a number of track sections that we use when
calculating the positions of the track verges:

* xVergeLeftLo
* xVergeLeftHi
* yVergeLeft
* xVergeRightLo
* xVergeRightHi
* yVergeRight

This routine shuffles the list along by one position, so we can insert a new
track section into position 0 while  pushing the data in position 5 off the
end of the list.

The remaining bytes in the blocks are used to store track segment data.

.ShuffleSectionList

LDX #44                \ Set X = 44 so we start by shuffling the first batch:
\
\   * xVergeLeftLo+0-4 to xVergeLeftLo+1-5
\
\   * xVergeLeftHi+0-4 to xVergeLeftHi+1-5
\
\   * yVergeLeft+0-4 to yVergeLeft+1-5
\
\ This works because:
\
\   * xVergeRightLo + 40 = xVergeLeftLo
\
\   * xVergeRightHi + 40 = xVergeLeftHi
\
\   * yVergeRight + 40 = yVergeLeft

.shuf1

LDA xVergeRightLo,X    \ Shuffle the X-th byte of xVergeRightLo up by one
STA xVergeRightLo+1,X

LDA xVergeRightHi,X    \ Shuffle the X-th byte of xVergeRightHi up by one
STA xVergeRightHi+1,X

LDA yVergeRight,X      \ Shuffle the X-th byte of yVergeRight up by one
STA yVergeRight+1,X

CPX #40                \ If X <> 40 then we are either still shuffling the
BNE shuf2              \ first batch and haven't yet done the last shuffle, or
\ we are already shuffling the second batch, so in

LDX #5                 \ We have just done the last shuffle in the first batch,
\ so set X = 5 so we now shuffle the second batch:
\
\   * xVergeRightLo+0-4 to xVergeRightLo+1-5
\
\   * xVergeRightHi+0-4 to xVergeRightHi+1-5
\
\   * yVergeRight+0-4 to yVergeRight+1-5

.shuf2

DEX                    \ Decrement the loop counter

BPL shuf1              \ Loop back until we have shuffled all the bytes in both
\ batches

LDA #6                 \ Set sectionListStart = 6 - sectionListSize
SEC                    \
SBC sectionListSize    \ so sectionListStart is the start index of the track
STA sectionListStart   \ section list, as the list ends at index 6

JSR IncSectionPointers \ Update the list pointer variables so the new entry is
\ not marked as valid

RTS                    \ Return from the subroutine
```