Skip to navigation

Elite on the BBC Micro and NES

Version analysis of Ghy

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

Code variations between these versions are shown below.

Name: Ghy Type: Subroutine Category: Flight Summary: Perform a galactic hyperspace jump Deep dive: Twisting the system seeds Galaxy and system seeds
Engage the galactic hyperdrive. Called from the hyp routine above if CTRL-H is being pressed. This routine also updates the galaxy seeds to point to the next galaxy. Using a galactic hyperdrive rotates each seed byte to the left, rolling each byte left within itself like this: 01234567 -> 12345670 to get the seeds for the next galaxy. So after 8 galactic jumps, the seeds roll round to those of the first galaxy again. We always arrive in a new galaxy at galactic coordinates (96, 96), and then find the nearest system and set that as our location.
Other entry points: zZ+1 Contains an RTS

Code variation 1 of 13A variation in the labels only

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

Ghy-1 Contains an RTS
.Ghy

Code variation 2 of 13Other (e.g. bug fix, optimisation)

Early cassette versions have a bug where performing a galactic hyperspace can drop you in the middle of nowhere in the next galaxy, with no escape. The bug is in the original source disc, and the text sources contain an attempted fix for the bug, which doesn't work, but which was refined in the later Stairway to Hell variant to fix the issue.

See below for more variations related to this code.

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

IF _TEXT_SOURCES JSR TT111 \ Call TT111 to set the current system to the nearest \ system to (QQ9, QQ10), and put the seeds of the \ nearest system into QQ15 to QQ15+5 \ \ This appears to be a failed attempt to fix a bug in \ the cassette version, where the galactic hyperdrive \ will take us to coordinates (96, 96) in the new \ galaxy, even if there isn't actually a system there, \ so if we jump when you are low on fuel, it is \ possible to get stuck in the middle of nowhere when \ changing galaxy \ \ All the other versions contain a fix for this bug that \ involves adding an extra JSR TT111 instruction after \ the coordinates are set to (96, 96) below, which finds \ the nearest system to those coordinates and sets that \ as the current system \ \ The cassette version on the original source disc \ doesn't contain this instruction, and although the \ text sources do, it's in the wrong place at the start \ of the Ghy routine, as the fix only works if it's done \ after the new coordinates are set, not before ENDIF

Code variation 3 of 13A variation in the labels only

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

LDX GHYP \ Fetch GHYP, which tells us whether we own a galactic BEQ hy5 \ hyperdrive, and if it is zero, which means we don't, \ return from the subroutine (as hy5 contains an RTS) INX \ We own a galactic hyperdrive, so X is &FF, so this \ instruction sets X = 0
LDX GHYP \ Fetch GHYP, which tells us whether we own a galactic BEQ zZ+1 \ hyperdrive, and if it is zero, which means we don't, \ return from the subroutine (as zZ+1 contains an RTS) INX \ We own a galactic hyperdrive, so X is &FF, so this \ instruction sets X = 0
IF _IB_SUPERIOR LDX GHYP \ Fetch GHYP, which tells us whether we own a galactic BEQ zZ+1 \ hyperdrive, and if it is zero, which means we don't, \ return from the subroutine (as zZ+1 contains an RTS) ELIF _IB_ACORNSOFT LDX GHYP \ Fetch GHYP, which tells us whether we own a galactic BEQ Ghy-1 \ hyperdrive, and if it is zero, which means we don't, \ return from the subroutine (as Ghy-1 contains an RTS) ENDIF INX \ We own a galactic hyperdrive, so X is &FF, so this \ instruction sets X = 0

Code variation 4 of 13Other (e.g. bug fix, optimisation)

Part of the bug fix for the "hyperspace while docking" bug (see below).

See below for more variations related to this code.

This variation is blank in the Disc (flight) and Disc (docked) versions.

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

IF _SOURCE_DISC OR _TEXT_SOURCES STX QQ8 \ Set the distance to the selected system in QQ8(1 0) STX QQ8+1 \ to 0 ENDIF
\STX QQ8 \ These instructions are commented out in the original \STX QQ8+1 \ source
IF _IB_ACORNSOFT STX QQ8 \ Set the distance to the selected system in QQ8(1 0) STX QQ8+1 \ to 0 ENDIF
 STX GHYP               \ The galactic hyperdrive is a one-use item, so set GHYP
                        \ to 0 so we no longer have one fitted

 STX FIST               \ Changing galaxy also clears our criminal record, so
                        \ set our legal status in FIST to 0 ("clean")

Code variation 5 of 13Related to an advanced feature

The original versions of Elite start the galactic hyperspace countdown from 15, just like the normal hyperspace countdown, but the advanced versions don't muck about and start the galactic hyperspace countdown from 2.

This variation is blank in the Electron version.

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

JSR wW \ Call wW to start the hyperspace countdown
LDA #2 \ Call wW2 with A = 2 to start the hyperspace countdown, JSR wW2 \ but starting the countdown from 2

Code variation 6 of 13Related to the Electron version

The wW routine is not called in the original Acornsoft release, so the length of the first tick of the galactic hyperspace countdown will depend on the current value of QQ22, which could be any value.

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

IF _IB_SUPERIOR JSR wW \ Call wW to start the hyperspace countdown ENDIF
 LDX #5                 \ To move galaxy, we rotate the galaxy's seeds left, so
                        \ set a counter in X for the 6 seed bytes

Code variation 7 of 13Related to the Electron version

Galactic hyperspace does not work in the original Acornsoft variant, but if it did, the hyperspace counter would count down from 5, rather than 15 (this was changed to 15 in the fixed version to bring it in line with the other original versions).

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

IF _IB_ACORNSOFT STX QQ22+1 \ Set the on-screen hyperspace countdown to 5 ENDIF
 INC GCNT               \ Increment the current galaxy number in GCNT

Code variation 8 of 13Related to the Master version

In the Master version, the internal galaxy number can be set to be greater than 16, and it will stay high even if you jump to the next galaxy (though it isn't clear what this is for, as the game doesn't set the galaxy to more than 7 at any point, so perhaps this was for an expansion that never happened).

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

LDA GCNT \ Set GCNT = GCNT mod 8, so we jump from galaxy 7 back AND #7 \ to galaxy 0 (shown in-game as going from galaxy 8 back STA GCNT \ to the starting point in galaxy 1)
LDA GCNT \ Clear bit 3 of GCNT, so we jump from galaxy 7 back AND #%11110111 \ to galaxy 0 (shown in-game as going from galaxy 8 back STA GCNT \ to the starting point in galaxy 1). We also retain any \ set bits in the high nibble, so if the galaxy number \ is manually set to 16 or higher, it will stay high \ (though the high nibble doesn't seem to get set by \ the game at any point, so it isn't clear what this is \ for, though Lave in galaxy 16 does show a unique \ system description override, so something is going on \ here...)
.G1

 LDA QQ21,X             \ Load the X-th seed byte into A

 ASL A                  \ Set the C flag to bit 7 of the seed

 ROL QQ21,X             \ Rotate the seed in memory, which will add bit 7 back
                        \ in as bit 0, so this rolls the seed around on itself

 DEX                    \ Decrement the counter

 BPL G1                 \ Loop back for the next seed byte, until we have
                        \ rotated them all

Code variation 9 of 13A variation in the comments only

This variation is blank in the Disc (flight), Disc (docked) and Electron versions.

\JSR DORND \ This instruction is commented out in the original \ source, and would set A and X to random numbers, so \ perhaps the original plan was to arrive in each new \ galaxy in a random place?
.zZ

Code variation 10 of 13Related to the Electron version

Galactic hyperspace does not work in the original Acornsoft variant, but if it did, it would drop you at a randomly generated point in the new galaxy, rather than the closest system to galactic coordinates (96, 96), which is how all the other versions work.

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

LDA #96 \ Set (QQ9, QQ10) to (96, 96), which is where we always STA QQ9 \ arrive in a new galaxy (the selected system will be STA QQ10 \ set to the nearest actual system later on)
IF _IB_SUPERIOR LDA #96 \ Set (QQ9, QQ10) to (96, 96), which is where we always STA QQ9 \ arrive in a new galaxy (the selected system will be STA QQ10 \ set to the nearest actual system later on) ELIF _IB_ACORNSOFT JSR DORND \ Set A and X to random numbers STA QQ9 \ Set (QQ9, QQ10) to a random point in the new galaxy STX QQ10 \ (the selected system will be set to the nearest actual \ system later on) ENDIF
 JSR TT110              \ Call TT110 to show the front space view

Code variation 11 of 13Other (e.g. bug fix, optimisation)

See variation 2 above for details.

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

IF _STH_CASSETTE JSR TT111 \ Call TT111 to set the current system to the nearest \ system to (QQ9, QQ10), and put the seeds of the \ nearest system into QQ15 to QQ15+5 \ \ This call fixes a bug in the early cassette versions, \ where the galactic hyperdrive will take us to \ coordinates (96, 96) in the new galaxy, even if there \ isn't actually a system there, so if we jump when we \ are low on fuel, it is possible to get stuck in the \ middle of nowhere when changing galaxy \ \ This call sets the current system correctly, so we \ always arrive at the nearest system to (96, 96) ENDIF
JSR TT111 \ Call TT111 to set the current system to the nearest \ system to (QQ9, QQ10), and put the seeds of the \ nearest system into QQ15 to QQ15+5 \ \ This call fixes a bug in the cassette version, where \ the galactic hyperdrive will take us to coordinates \ (96, 96) in the new galaxy, even if there isn't \ actually a system there, so if we jump when we are \ low on fuel, it is possible to get stuck in the \ middle of nowhere when changing galaxy \ \ This call sets the current system correctly, so we \ always arrive at the nearest system to (96, 96)

Code variation 12 of 13Other (e.g. bug fix, optimisation)

See variation 4 above for details.

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

LDX #5 \ We now want to copy those seeds into safehouse, so we \ so set a counter in X to copy 6 bytes .dumdeedum LDA QQ15,X \ Copy the X-th byte of QQ15 into the X-th byte of STA safehouse,X \ safehouse DEX \ Decrement the loop counter BPL dumdeedum \ Loop back to copy the next byte until we have copied \ all six seed bytes

Code variation 13 of 13Other (e.g. bug fix, optimisation)

See variation 4 above for details.

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

IF _STH_CASSETTE LDX #0 \ Set the distance to the selected system in QQ8(1 0) STX QQ8 \ to 0 STX QQ8+1 ENDIF
LDX #0 \ Set the distance to the selected system in QQ8(1 0) STX QQ8 \ to 0 STX QQ8+1
IF _IB_SUPERIOR LDX #0 \ Set the distance to the selected system in QQ8(1 0) STX QQ8 \ to 0 STX QQ8+1 ENDIF
 LDA #116               \ Print recursive token 116 (GALACTIC HYPERSPACE ")
 JSR MESS               \ as an in-flight message

                        \ Fall through into jmp to set the system to the
                        \ current system and return from the subroutine there