Skip to navigation

Elite on the BBC Micro and NES

Version analysis of CHECK

This code appears in the following versions (click to see it in the source code):

Code variations between these versions are shown below.

Name: CHECK Type: Subroutine Category: Save and load Summary: Calculate the checksum for the last saved commander data block Deep dive: Commander save files
The checksum for the last saved commander data block is saved as part of the commander file, in two places (CHK AND CHK2), to protect against file tampering. This routine calculates the checksum and returns it in A. This algorithm is also implemented in elite-checksum.py.
Returns: A The checksum for the last saved commander data block
.CHECK

Code variation 1 of 2Specific to an individual platform

Tap on a block to expand it, and tap it again to revert.

LDX #NT%-2 \ Set X to the size of the commander data block, less \ 2 (to omit the checksum bytes and the save count)
LDX #NT%-3 \ Set X to the size of the commander data block, less \ 3 (as there are two checksum bytes and the save count)
 CLC                    \ Clear the C flag so we can do addition without the
                        \ C flag affecting the result

 TXA                    \ Seed the checksum calculation by setting A to the
                        \ size of the commander data block, less 2

                        \ We now loop through the commander data block,
                        \ starting at the end and looping down to the start
                        \ (so at the start of this loop, the X-th byte is the
                        \ last byte of the commander data block, i.e. the save
                        \ count)

.QUL2

 ADC NA%+7,X            \ Add the X-1-th byte of the data block to A, plus the
                        \ C flag

 EOR NA%+8,X            \ EOR A with the X-th byte of the data block

 DEX                    \ Decrement the loop counter

 BNE QUL2               \ Loop back for the next byte in the calculation, until
                        \ we have added byte #0 and EOR'd with byte #1 of the
                        \ data block

 RTS                    \ Return from the subroutine

Code variation 2 of 2A variation in the comments only

This variation is blank in the Cassette, Disc (docked), 6502 Second Processor and Electron versions.

.CHECK2 \LDX #NT%-3 \ These instructions are commented out in the original \CLC \ source \TXA \.QU2L2 \STX T \EOR T \ROR A \ADC NA%+7,X \EOR NA%+8,X \DEX \BNE QU2L2 \RTS