Elite on the BBC Micro and NES

# Elite D flight source

## [BBC Micro disc version]

ELITE D FILE
Name: tnpr1 Type: Subroutine Category: Market Summary: Work out if we have space for one tonne of cargo
Context: See this subroutine on its own page References: This subroutine is called as follows:

Given a market item, work out whether there is room in the cargo hold for one tonne of this item. For standard tonne canisters, the limit is given by the type of cargo hold we have, with a standard cargo hold having a capacity of 20t and an extended cargo bay being 35t. For items measured in kg (gold, platinum), g (gem-stones) and alien items, the individual limit on each of these is 200 units.
Arguments: A The type of market item (see QQ23 for a list of market item numbers)
Returns: A A = 1 C flag Returns the result: * Set if there is no room for this item * Clear if there is room for this item
.tnpr1 STA QQ29 \ Store the type of market item in QQ29 LDA #1 \ Set the number of units of this market item to 1 \ Fall through into tnpr to work out whether there is \ room in the cargo hold for A tonnes of the item of \ type QQ29
Name: tnpr Type: Subroutine Category: Market Summary: Work out if we have space for a specific amount of cargo
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: No direct references to this subroutine in this source file

Given a market item and an amount, work out whether there is room in the cargo hold for this item. For standard tonne canisters, the limit is given by the type of cargo hold we have, with a standard cargo hold having a capacity of 20t and an extended cargo bay being 35t. For items measured in kg (gold, platinum), g (gem-stones) and alien items, the individual limit on each of these is 200 units.
Arguments: A The number of units of this market item QQ29 The type of market item (see QQ23 for a list of market item numbers)
Returns: A A is preserved C flag Returns the result: * Set if there is no room for this item * Clear if there is room for this item
Name: TT20 Type: Subroutine Category: Universe Summary: Twist the selected system's seeds four times Deep dive: Twisting the system seeds Galaxy and system seeds
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT111 calls TT20 * TT22 calls TT20 * TT23 calls TT20

Twist the three 16-bit seeds in QQ15 (selected system) four times, to generate the next system.
.TT20 JSR P%+3 \ This line calls the line below as a subroutine, which \ does two twists before returning here, and then we \ fall through to the line below for another two \ twists, so the net effect of these two consecutive \ JSR calls is four twists, not counting the ones \ inside your head as you try to follow this process JSR P%+3 \ This line calls TT54 as a subroutine to do a twist, \ and then falls through into TT54 to do another twist \ before returning from the subroutine
Name: TT54 Type: Subroutine Category: Universe Summary: Twist the selected system's seeds Deep dive: Twisting the system seeds Galaxy and system seeds
Context: See this subroutine on its own page References: This subroutine is called as follows: * cpl calls TT54

This routine twists the three 16-bit seeds in QQ15 once. If we start with seeds s0, s1 and s2 and we want to work out their new values after we perform a twist (let's call the new values s0Â´, s1Â´ and s2Â´), then: s0Â´ = s1 s1Â´ = s2 s2Â´ = s0 + s1 + s2 So given an existing set of seeds in s0, s1 and s2, we can get the new values s0Â´, s1Â´ and s2Â´ simply by doing the above sums. And if we want to do the above in-place without creating three new sÂ´ variables, then we can do the following: tmp = s0 + s1 s0 = s1 s1 = s2 s2 = tmp + s1 So this is what we do in this routine, where each seed is a 16-bit number.
.TT54 LDA QQ15 \ X = tmp_lo = s0_lo + s1_lo CLC ADC QQ15+2 TAX LDA QQ15+1 \ Y = tmp_hi = s1_hi + s1_hi + C ADC QQ15+3 TAY LDA QQ15+2 \ s0_lo = s1_lo STA QQ15 LDA QQ15+3 \ s0_hi = s1_hi STA QQ15+1 LDA QQ15+5 \ s1_hi = s2_hi STA QQ15+3 LDA QQ15+4 \ s1_lo = s2_lo STA QQ15+2 CLC \ s2_lo = X + s1_lo TXA ADC QQ15+2 STA QQ15+4 TYA \ s2_hi = Y + s1_hi + C ADC QQ15+3 STA QQ15+5 RTS \ The twist is complete so return from the subroutine
Name: TT146 Type: Subroutine Category: Universe Summary: Print the distance to the selected system in light years
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT102 calls TT146 * TT25 calls TT146

If it is non-zero, print the distance to the selected system in light years. If it is zero, just move the text cursor down a line. Specifically, if the distance in QQ8 is non-zero, print token 31 ("DISTANCE"), then a colon, then the distance to one decimal place, then token 35 ("LIGHT YEARS"). If the distance is zero, move the cursor down one line.
.TT146 LDA QQ8 \ Take the two bytes of the 16-bit value in QQ8 and ORA QQ8+1 \ OR them together to check whether there are any BNE TT63 \ non-zero bits, and if so, jump to TT63 to print the \ distance INC YC \ The distance is zero, so we just move the text cursor RTS \ in YC down by one line and return from the subroutine .TT63 LDA #191 \ Print recursive token 31 ("DISTANCE") followed by JSR TT68 \ a colon LDX QQ8 \ Load (Y X) from QQ8, which contains the 16-bit LDY QQ8+1 \ distance we want to show SEC \ Set the C flag so that the call to pr5 will include a \ decimal point, and display the value as (Y X) / 10 JSR pr5 \ Print (Y X) to 5 digits, including a decimal point LDA #195 \ Set A to the recursive token 35 (" LIGHT YEARS") and \ fall through into TT60 to print the token followed \ by a paragraph break
Name: TT60 Type: Subroutine Category: Text Summary: Print a text token and a paragraph break
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT213 calls TT60 * TT25 calls TT60

Print a text token (i.e. a character, control code, two-letter token or recursive token). Then print a paragraph break (a blank line between paragraphs) by moving the cursor down a line, setting Sentence Case, and then printing a newline.
Arguments: A The text token to be printed
.TT60 JSR TT27 \ Print the text token in A and fall through into TTX69 \ to print the paragraph break
Name: TTX69 Type: Subroutine Category: Text Summary: Print a paragraph break
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT25 calls TTX69

Print a paragraph break (a blank line between paragraphs) by moving the cursor down a line, setting Sentence Case, and then printing a newline.
.TTX69 INC YC \ Move the text cursor down a line \ Fall through into TT69 to set Sentence Case and print \ a newline
Name: TT69 Type: Subroutine Category: Text Summary: Set Sentence Case and print a newline
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT210 calls TT69
.TT69 LDA #%10000000 \ Set bit 7 of QQ17 to switch to Sentence Case STA QQ17 \ Fall through into TT67 to print a newline
Name: TT67 Type: Subroutine Category: Text Summary: Print a newline
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * CLYNS calls TT67 * plf calls TT67
.TT67 LDA #12 \ Load a newline character into A JMP TT27 \ Print the text token in A and return from the \ subroutine using a tail call
Name: TT70 Type: Subroutine Category: Universe Summary: Display "MAINLY " and jump to TT72
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT25 calls TT70

This subroutine is called by TT25 when displaying a system's economy.
.TT70 LDA #173 \ Print recursive token 13 ("MAINLY ") JSR TT27 JMP TT72 \ Jump to TT72 to continue printing system data as part \ of routine TT25
Name: spc Type: Subroutine Category: Text Summary: Print a text token followed by a space
Context: See this subroutine on its own page References: This subroutine is called as follows: * STATUS calls spc * TT25 calls spc

Print a text token (i.e. a character, control code, two-letter token or recursive token) followed by a space.
Arguments: A The text token to be printed
.spc JSR TT27 \ Print the text token in A JMP TT162 \ Print a space and return from the subroutine using a \ tail call
Name: TT25 Type: Subroutine Category: Universe Summary: Show the Data on System screen (red key f6) Deep dive: Generating system data Galaxy and system seeds
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT102 calls TT25 * TT70 calls via TT72

Other entry points: TT72 Used by TT70 to re-enter the routine after displaying "MAINLY" for the economy type
Name: TT24 Type: Subroutine Category: Universe Summary: Calculate system data from the system seeds Deep dive: Generating system data Galaxy and system seeds
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT111 calls TT24

Calculate system data from the seeds in QQ15 and store them in the relevant locations. Specifically, this routine calculates the following from the three 16-bit seeds in QQ15 (using only s0_hi, s1_hi and s1_lo): QQ3 = economy (0-7) QQ4 = government (0-7) QQ5 = technology level (0-14) QQ6 = population * 10 (1-71) QQ7 = productivity (96-62480) The ranges of the various values are shown in brackets. Note that the radius and type of inhabitant are calculated on-the-fly in the TT25 routine when the system data gets displayed, so they aren't calculated here.
Name: TT22 Type: Subroutine Category: Charts Summary: Show the Long-range Chart (red key f4)
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT102 calls TT22 * TT114 calls TT22
.TT22 LDA #64 \ Clear the top part of the screen, draw a white border, JSR TT66 \ and set the current view type in QQ11 to 32 (Long- \ range Chart) LDA #7 \ Move the text cursor to column 7 STA XC JSR TT81 \ Set the seeds in QQ15 to those of system 0 in the \ current galaxy (i.e. copy the seeds from QQ21 to QQ15) LDA #199 \ Print recursive token 39 ("GALACTIC CHART{galaxy JSR TT27 \ number right-aligned to width 3}") JSR NLIN \ Draw a horizontal line at pixel row 23 to box in the \ title and act as the top frame of the chart, and move \ the text cursor down one line LDA #152 \ Draw a screen-wide horizontal line at pixel row 152 JSR NLIN2 \ for the bottom edge of the chart, so the chart itself \ is 128 pixels high, starting on row 24 and ending on \ row 151 JSR TT14 \ Call TT14 to draw a circle with crosshairs at the \ current system's galactic coordinates LDX #0 \ We're now going to plot each of the galaxy's systems, \ so set up a counter in X for each system, starting at \ 0 and looping through to 255 .TT83 STX XSAV \ Store the counter in XSAV LDX QQ15+3 \ Fetch the s1_hi seed into X, which gives us the \ galactic x-coordinate of this system LDY QQ15+4 \ Fetch the s2_lo seed and set bits 4 and 6, storing the TYA \ result in ZZ to give a random number between 80 and ORA #%01010000 \ (but which will always be the same for this system). STA ZZ \ We use this value to determine the size of the point \ for this system on the chart by passing it as the \ distance argument to the PIXEL routine below LDA QQ15+1 \ Fetch the s0_hi seed into A, which gives us the \ galactic y-coordinate of this system LSR A \ We halve the y-coordinate because the galaxy in \ in Elite is rectangular rather than square, and is \ twice as wide (x-axis) as it is high (y-axis), so the \ chart is 256 pixels wide and 128 high CLC \ Add 24 to the halved y-coordinate and store in XX15+1 ADC #24 \ (as the top of the chart is on pixel row 24, just STA XX15+1 \ below the line we drew on row 23 above) JSR PIXEL \ Call PIXEL to draw a point at (X, A), with the size of \ the point dependent on the distance specified in ZZ \ (so a high value of ZZ will produce a 1-pixel point, \ a medium value will produce a 2-pixel dash, and a \ small value will produce a 4-pixel square) JSR TT20 \ We want to move on to the next system, so call TT20 \ to twist the three 16-bit seeds in QQ15 LDX XSAV \ Restore the loop counter from XSAV INX \ Increment the counter BNE TT83 \ If X > 0 then we haven't done all 256 systems yet, so \ loop back up to TT83 LDA QQ9 \ Set QQ19 to the selected system's x-coordinate STA QQ19 LDA QQ10 \ Set QQ19+1 to the selected system's y-coordinate, LSR A \ halved to fit it into the chart STA QQ19+1 LDA #4 \ Set QQ19+2 to size 4 for the crosshairs size STA QQ19+2 \ Fall through into TT15 to draw crosshairs of size 4 at \ the selected system's coordinates
Name: TT15 Type: Subroutine Category: Drawing lines Summary: Draw a set of crosshairs
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * SIGHT calls TT15 * TT103 calls TT15 * TT105 calls TT15 * TT14 calls TT15

For all views except the Short-range Chart, the centre is drawn 24 pixels to the right of the y-coordinate given.
Arguments: QQ19 The pixel x-coordinate of the centre of the crosshairs QQ19+1 The pixel y-coordinate of the centre of the crosshairs QQ19+2 The size of the crosshairs
.TT15 LDA #24 \ Set A to 24, which we will use as the minimum \ screen indent for the crosshairs (i.e. the minimum \ distance from the top-left corner of the screen) LDX QQ11 \ If the current view is not the Short-range Chart, BPL P%+4 \ which is the only view with bit 7 set, then skip the \ following instruction LDA #0 \ This is the Short-range Chart, so set A to 0, so the \ crosshairs can go right up against the screen edges STA QQ19+5 \ Set QQ19+5 to A, which now contains the correct indent \ for this view LDA QQ19 \ Set A = crosshairs x-coordinate - crosshairs size SEC \ to get the x-coordinate of the left edge of the SBC QQ19+2 \ crosshairs BCS TT84 \ If the above subtraction didn't underflow, then A is \ positive, so skip the next instruction LDA #0 \ The subtraction underflowed, so set A to 0 so the \ crosshairs don't spill out of the left of the screen .TT84 \ In the following, the authors have used XX15 for \ temporary storage. XX15 shares location with X1, Y1, \ X2 and Y2, so in the following, you can consider \ the variables like this: \ \ XX15 is the same as X1 \ XX15+1 is the same as Y1 \ XX15+2 is the same as X2 \ XX15+3 is the same as Y2 \ \ Presumably this routine was written at a different \ time to the line-drawing routine, before the two \ workspaces were merged to save space STA XX15 \ Set XX15 (X1) = A (the x-coordinate of the left edge \ of the crosshairs) LDA QQ19 \ Set A = crosshairs x-coordinate + crosshairs size CLC \ to get the x-coordinate of the right edge of the ADC QQ19+2 \ crosshairs BCC P%+4 \ If the above addition didn't overflow, then A is \ correct, so skip the next instruction LDA #255 \ The addition overflowed, so set A to 255 so the \ crosshairs don't spill out of the right of the screen \ (as 255 is the x-coordinate of the rightmost pixel \ on-screen) STA XX15+2 \ Set XX15+2 (X2) = A (the x-coordinate of the right \ edge of the crosshairs) LDA QQ19+1 \ Set XX15+1 (Y1) = crosshairs y-coordinate + indent CLC \ to get the y-coordinate of the centre of the ADC QQ19+5 \ crosshairs STA XX15+1 JSR HLOIN \ Draw a horizontal line from (X1, Y1) to (X2, Y1), \ which will draw from the left edge of the crosshairs \ to the right edge, through the centre of the \ crosshairs LDA QQ19+1 \ Set A = crosshairs y-coordinate - crosshairs size SEC \ to get the y-coordinate of the top edge of the SBC QQ19+2 \ crosshairs BCS TT86 \ If the above subtraction didn't underflow, then A is \ correct, so skip the next instruction LDA #0 \ The subtraction underflowed, so set A to 0 so the \ crosshairs don't spill out of the top of the screen .TT86 CLC \ Set XX15+1 (Y1) = A + indent to get the y-coordinate ADC QQ19+5 \ of the top edge of the indented crosshairs STA XX15+1 LDA QQ19+1 \ Set A = crosshairs y-coordinate + crosshairs size CLC \ + indent to get the y-coordinate of the bottom edge ADC QQ19+2 \ of the indented crosshairs ADC QQ19+5 CMP #152 \ If A < 152 then skip the following, as the crosshairs BCC TT87 \ won't spill out of the bottom of the screen LDX QQ11 \ A >= 152, so we need to check whether this will fit in \ this view, so fetch the view type BMI TT87 \ If this is the Short-range Chart then the y-coordinate \ is fine, so skip to TT87 LDA #151 \ Otherwise this is the Long-range Chart, so we need to \ clip the crosshairs at a maximum y-coordinate of 151 .TT87 STA XX15+3 \ Set XX15+3 (Y2) = A (the y-coordinate of the bottom \ edge of the crosshairs) LDA QQ19 \ Set XX15 (X1) = the x-coordinate of the centre of the STA XX15 \ crosshairs STA XX15+2 \ Set XX15+2 (X2) = the x-coordinate of the centre of \ the crosshairs JMP LL30 \ Draw a vertical line (X1, Y1) to (X2, Y2), which will \ draw from the top edge of the crosshairs to the bottom \ edge, through the centre of the crosshairs, returning \ from the subroutine using a tail call
Name: TT14 Type: Subroutine Category: Drawing circles Summary: Draw a circle with crosshairs on a chart
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT22 calls TT14 * TT23 calls TT14

Draw a circle with crosshairs at the current system's galactic coordinates.
.TT126 LDA #104 \ Set QQ19 = 104, for the x-coordinate of the centre of STA QQ19 \ the fixed circle on the Short-range Chart LDA #90 \ Set QQ19+1 = 90, for the y-coordinate of the centre of STA QQ19+1 \ the fixed circle on the Short-range Chart LDA #16 \ Set QQ19+2 = 16, the size of the crosshairs on the STA QQ19+2 \ Short-range Chart JSR TT15 \ Draw the set of crosshairs defined in QQ19, at the \ exact coordinates as this is the Short-range Chart LDA QQ14 \ Set K to the fuel level from QQ14, so this can act as STA K \ the circle's radius (70 being a full tank) JMP TT128 \ Jump to TT128 to draw a circle with the centre at the \ same coordinates as the crosshairs, (QQ19, QQ19+1), \ and radius K that reflects the current fuel levels, \ returning from the subroutine using a tail call .TT14 LDA QQ11 \ If the current view is the Short-range Chart, which BMI TT126 \ is the only view with bit 7 set, then jump up to TT126 \ to draw the crosshairs and circle for that view \ Otherwise this is the Long-range Chart, so we draw the \ crosshairs and circle for that view instead LDA QQ14 \ Set K to the fuel level from QQ14 divided by 4, so LSR A \ this can act as the circle's radius (70 being a full LSR A \ tank, which divides down to a radius of 17) STA K LDA QQ0 \ Set QQ19 to the x-coordinate of the current system, STA QQ19 \ which will be the centre of the circle and crosshairs \ we draw LDA QQ1 \ Set QQ19+1 to the y-coordinate of the current system, LSR A \ halved because the galactic chart is half as high as STA QQ19+1 \ it is wide, which will again be the centre of the \ circle and crosshairs we draw LDA #7 \ Set QQ19+2 = 7, the size of the crosshairs on the STA QQ19+2 \ Long-range Chart JSR TT15 \ Draw the set of crosshairs defined in QQ19, which will \ be drawn 24 pixels to the right of QQ19+1 LDA QQ19+1 \ Add 24 to the y-coordinate of the crosshairs in QQ19+1 CLC \ so that the centre of the circle matches the centre ADC #24 \ of the crosshairs STA QQ19+1 \ Fall through into TT128 to draw a circle with the \ centre at the same coordinates as the crosshairs, \ (QQ19, QQ19+1), and radius K that reflects the \ current fuel levels
Name: TT128 Type: Subroutine Category: Drawing circles Summary: Draw a circle on a chart Deep dive: Drawing circles
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT14 calls TT128

Draw a circle with the centre at (QQ19, QQ19+1) and radius K.
Arguments: QQ19 The x-coordinate of the centre of the circle QQ19+1 The y-coordinate of the centre of the circle K The radius of the circle
.TT128 LDA QQ19 \ Set K3 = the x-coordinate of the centre STA K3 LDA QQ19+1 \ Set K4 = the y-coordinate of the centre STA K4 LDX #0 \ Set the high bytes of K3(1 0) and K4(1 0) to 0 STX K4+1 STX K3+1 INX \ Set LSP = 1 to reset the ball line heap STX LSP LDX #2 \ Set STP = 2, the step size for the circle STX STP JSR CIRCLE2 \ Call CIRCLE2 to draw a circle with the centre at \ (K3(1 0), K4(1 0)) and radius K RTS \ Return from the subroutine
Name: TT210 Type: Subroutine Category: Market Summary: Show a list of current cargo in our hold, optionally to sell
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT213 calls TT210

Show a list of current cargo in our hold, either with the ability to sell (the Sell Cargo screen) or without (the Inventory screen), depending on the current view.
Arguments: QQ11 The current view: * 4 = Sell Cargo * 8 = Inventory
.TT210 LDY #0 \ We're going to loop through all the available market \ items and check whether we have any in the hold (and, \ if we are in the Sell Cargo screen, whether we want \ to sell any items), so we set up a counter in Y to \ denote the current item and start it at 0 .TT211 STY QQ29 \ Store the current item number in QQ29 LDX QQ20,Y \ Fetch into X the amount of the current item that we BEQ TT212 \ have in our cargo hold, which is stored in QQ20+Y, \ and if there are no items of this type in the hold, \ jump down to TT212 to skip to the next item TYA \ Set Y = Y * 4, so this will act as an index into the ASL A \ market prices table at QQ23 for this item (as there ASL A \ are four bytes per item in the table) TAY LDA QQ23+1,Y \ Fetch byte #1 from the market prices table for the STA QQ19+1 \ current item and store it in QQ19+1, for use by the \ call to TT152 below TXA \ Store the amount of item in the hold (in X) on the PHA \ stack JSR TT69 \ Call TT69 to set Sentence Case and print a newline CLC \ Print recursive token 48 + QQ29, which will be in the LDA QQ29 \ range 48 ("FOOD") to 64 ("ALIEN ITEMS"), so this ADC #208 \ prints the current item's name JSR TT27 LDA #14 \ Move the text cursor to column 14, for the item's STA XC \ quantity PLA \ Restore the amount of item in the hold into X TAX CLC \ Print the 8-bit number in X to 3 digits, without a JSR pr2 \ decimal point JSR TT152 \ Print the unit ("t", "kg" or "g") for the market item \ whose byte #1 from the market prices table is in \ QQ19+1 (which we set up above) .TT212 LDY QQ29 \ Fetch the item number from QQ29 into Y, and increment INY \ Y to point to the next item CPY #17 \ If Y < 17 then loop back to TT211 to print the next BCC TT211 \ item in the hold RTS \ Otherwise return from the subroutine
Name: TT213 Type: Subroutine Category: Market Summary: Show the Inventory screen (red key f9)
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT102 calls TT213
.TT213 LDA #8 \ Clear the top part of the screen, draw a white border, JSR TT66 \ and set the current view type in QQ11 to 8 (Inventory \ screen) LDA #11 \ Move the text cursor to column 11 to print the screen STA XC \ title LDA #164 \ Print recursive token 4 ("INVENTORY{crlf}") followed JSR TT60 \ by a paragraph break and Sentence Case JSR NLIN4 \ Draw a horizontal line at pixel row 19 to box in the \ title. The authors could have used a call to NLIN3 \ instead and saved the above call to TT60, but you \ just can't optimise everything JSR fwl \ Call fwl to print the fuel and cash levels on two \ separate lines LDA CRGO \ If our ship's cargo capacity is < 26 (i.e. we do not CMP #26 \ have a cargo bay extension), skip the following two BCC P%+7 \ instructions LDA #107 \ We do have a cargo bay extension, so print recursive JSR TT27 \ token 107 ("LARGE CARGO{sentence case} BAY") JMP TT210 \ Jump to TT210 to print the contents of our cargo bay \ and return from the subroutine using a tail call
Name: TT16 Type: Subroutine Category: Charts Summary: Move the crosshairs on a chart
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT102 calls TT16

Move the chart crosshairs by the amount in X and Y.
Arguments: X The amount to move the crosshairs in the x-axis Y The amount to move the crosshairs in the y-axis
.TT16 TXA \ Push the change in X onto the stack (let's call this PHA \ the x-delta) DEY \ Negate the change in Y and push it onto the stack TYA \ (let's call this the y-delta) EOR #&FF PHA JSR WSCAN \ Call WSCAN to wait for the vertical sync, so the whole \ screen gets drawn and we can move the crosshairs with \ no screen flicker JSR TT103 \ Draw small crosshairs at coordinates (QQ9, QQ10), \ which will erase the crosshairs currently there PLA \ Store the y-delta in QQ19+3 and fetch the current STA QQ19+3 \ y-coordinate of the crosshairs from QQ10 into A, ready LDA QQ10 \ for the call to TT123 JSR TT123 \ Call TT123 to move the selected system's galactic \ y-coordinate by the y-delta, putting the new value in \ QQ19+4 LDA QQ19+4 \ Store the updated y-coordinate in QQ10 (the current STA QQ10 \ y-coordinate of the crosshairs) STA QQ19+1 \ This instruction has no effect, as QQ19+1 is \ overwritten below, both in TT103 and TT105 PLA \ Store the x-delta in QQ19+3 and fetch the current STA QQ19+3 \ x-coordinate of the crosshairs from QQ10 into A, ready LDA QQ9 \ for the call to TT123 JSR TT123 \ Call TT123 to move the selected system's galactic \ x-coordinate by the x-delta, putting the new value in \ QQ19+4 LDA QQ19+4 \ Store the updated x-coordinate in QQ9 (the current STA QQ9 \ x-coordinate of the crosshairs) STA QQ19 \ This instruction has no effect, as QQ19 is overwritten \ below, both in TT103 and TT105 \ Now we've updated the coordinates of the crosshairs, \ fall through into TT103 to redraw them at their new \ location
Name: TT103 Type: Subroutine Category: Charts Summary: Draw a small set of crosshairs on a chart
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * hm calls TT103 * TT102 calls TT103 * TT16 calls TT103 * TT23 calls TT103

Draw a small set of crosshairs on a galactic chart at the coordinates in (QQ9, QQ10).
.TT103 LDA QQ11 \ Fetch the current view type into A BMI TT105 \ If this is the Short-range Chart screen, jump to TT105 LDA QQ9 \ Store the crosshairs x-coordinate in QQ19 STA QQ19 LDA QQ10 \ Halve the crosshairs y-coordinate and store it in QQ19 LSR A \ (we halve it because the Long-range Chart is half as STA QQ19+1 \ high as it is wide) LDA #4 \ Set QQ19+2 to 4 denote crosshairs of size 4 STA QQ19+2 JMP TT15 \ Jump to TT15 to draw crosshairs of size 4 at the \ crosshairs coordinates, returning from the subroutine \ using a tail call
Name: TT123 Type: Subroutine Category: Charts Summary: Move galactic coordinates by a signed delta
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT16 calls TT123 * TT105 calls via TT180

Move an 8-bit galactic coordinate by a certain distance in either direction (i.e. a signed 8-bit delta), but only if it doesn't cause the coordinate to overflow. The coordinate is in a single axis, so it's either an x-coordinate or a y-coordinate.
Arguments: A The galactic coordinate to update QQ19+3 The delta (can be positive or negative)
Returns: QQ19+4 The updated coordinate after moving by the delta (this will be the same as A if moving by the delta overflows)
Other entry points: TT180 Contains an RTS
.TT123 STA QQ19+4 \ Store the original coordinate in temporary storage at \ QQ19+4 CLC \ Set A = A + QQ19+3, so A now contains the original ADC QQ19+3 \ coordinate, moved by the delta LDX QQ19+3 \ If the delta is negative, jump to TT124 BMI TT124 BCC TT125 \ If the C flag is clear, then the above addition didn't \ overflow, so jump to TT125 to return the updated value RTS \ Otherwise the C flag is set and the above addition \ overflowed, so do not update the return value .TT124 BCC TT180 \ If the C flag is clear, then because the delta is \ negative, this indicates the addition (which is \ effectively a subtraction) underflowed, so jump to \ TT180 to return from the subroutine without updating \ the return value .TT125 STA QQ19+4 \ Store the updated coordinate in QQ19+4 .TT180 RTS \ Return from the subroutine
Name: TT105 Type: Subroutine Category: Charts Summary: Draw crosshairs on the Short-range Chart, with clipping
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT103 calls TT105

Check whether the crosshairs are close enough to the current system to appear on the Short-range Chart, and if so, draw them.
.TT105 LDA QQ9 \ Set A = QQ9 - QQ0, the horizontal distance between the SEC \ crosshairs (QQ9) and the current system (QQ0) SBC QQ0 CMP #38 \ If the horizontal distance in A < 38, then the BCC TT179 \ crosshairs are close enough to the current system to \ appear in the Short-range Chart, so jump to TT179 to \ check the vertical distance CMP #230 \ If the horizontal distance in A < -26, then the BCC TT180 \ crosshairs are too far from the current system to \ appear in the Short-range Chart, so jump to TT180 to \ return from the subroutine (as TT180 contains an RTS) .TT179 ASL A \ Set QQ19 = 104 + A * 4 ASL A \ CLC \ 104 is the x-coordinate of the centre of the chart, ADC #104 \ so this sets QQ19 to the screen pixel x-coordinate STA QQ19 \ of the crosshairs LDA QQ10 \ Set A = QQ10 - QQ1, the vertical distance between the SEC \ crosshairs (QQ10) and the current system (QQ1) SBC QQ1 CMP #38 \ If the vertical distance in A is < 38, then the BCC P%+6 \ crosshairs are close enough to the current system to \ appear in the Short-range Chart, so skip the next two \ instructions CMP #220 \ If the horizontal distance in A is < -36, then the BCC TT180 \ crosshairs are too far from the current system to \ appear in the Short-range Chart, so jump to TT180 to \ return from the subroutine (as TT180 contains an RTS) ASL A \ Set QQ19+1 = 90 + A * 2 CLC \ ADC #90 \ 90 is the y-coordinate of the centre of the chart, STA QQ19+1 \ so this sets QQ19+1 to the screen pixel x-coordinate \ of the crosshairs LDA #8 \ Set QQ19+2 to 8 denote crosshairs of size 8 STA QQ19+2 JMP TT15 \ Jump to TT15 to draw crosshairs of size 8 at the \ crosshairs coordinates, returning from the subroutine \ using a tail call
Name: TT23 Type: Subroutine Category: Charts Summary: Show the Short-range Chart (red key f5)
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT102 calls TT23 * TT114 calls TT23
Name: TT81 Type: Subroutine Category: Universe Summary: Set the selected system's seeds to those of system 0
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT111 calls TT81 * TT22 calls TT81 * TT23 calls TT81

Copy the three 16-bit seeds for the current galaxy's system 0 (QQ21) into the seeds for the selected system (QQ15) - in other words, set the selected system's seeds to those of system 0.
.TT81 LDX #5 \ Set up a counter in X to copy six bytes (for three \ 16-bit numbers) LDA QQ21,X \ Copy the X-th byte in QQ21 to the X-th byte in QQ15 STA QQ15,X DEX \ Decrement the counter BPL TT81+2 \ Loop back up to the LDA instruction if we still have \ more bytes to copy RTS \ Return from the subroutine
Name: TT111 Type: Subroutine Category: Universe Summary: Set the current system to the nearest system to a point
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * Ghy calls TT111 * hm calls TT111 * hyp1 calls TT111 * STATUS calls TT111 * TT102 calls TT111 * TT110 calls TT111 * TTX110 calls TT111 * TT23 calls via TT111-1

Given a set of galactic coordinates in (QQ9, QQ10), find the nearest system to this point in the galaxy, and set this as the currently selected system.
Arguments: QQ9 The x-coordinate near which we want to find a system QQ10 The y-coordinate near which we want to find a system
Returns: QQ8(1 0) The distance from the current system to the nearest system to the original coordinates QQ9 The x-coordinate of the nearest system to the original coordinates QQ10 The y-coordinate of the nearest system to the original coordinates QQ15 to QQ15+5 The three 16-bit seeds of the nearest system to the original coordinates
Other entry points: TT111-1 Contains an RTS
Name: hyp Type: Subroutine Category: Flight Summary: Start the hyperspace process
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT102 calls hyp * TTX110 calls via TTX111

Called when "H" or CTRL-H is pressed during flight. Checks the following: * We are in space * We are not already in a hyperspace countdown If CTRL is being held down, we jump to Ghy to engage the galactic hyperdrive, otherwise we check that: * The selected system is not the current system * We have enough fuel to make the jump and if all the pre-jump checks are passed, we print the destination on-screen and start the countdown.
Other entry points: TTX111 Used to rejoin this routine from the call to TTX110
.hyp LDA QQ22+1 \ Fetch QQ22+1, which contains the number that's shown \ on-screen during hyperspace countdown ORA QQ12 \ If we are docked (QQ12 = &FF) or there is already a BNE zZ+1 \ countdown in progress, then return from the subroutine \ using a tail call (as zZ+1 contains an RTS), as we \ can't hyperspace when docked, or there is already a \ countdown in progress JSR CTRL \ Scan the keyboard to see if CTRL is currently pressed BMI Ghy \ If it is, then the galactic hyperdrive has been \ activated, so jump to Ghy to process it LDA QQ11 \ If the current view is 0 (i.e. the space view) then BNE P%+5 \ jump to TTX110, which calls TT111 to set the current JMP TTX110 \ system to the nearest system to (QQ9, QQ10), and jumps \ back into this routine at TTX111 below JSR hm \ This is a chart view, so call hm to redraw the chart \ crosshairs .TTX111 \ If we get here then the current view is either the \ space view or a chart LDA QQ8 \ If both bytes of the distance to the selected system ORA QQ8+1 \ in QQ8 are zero, return from the subroutine (as zZ+1 BEQ zZ+1 \ contains an RTS), as the selected system is the \ current system LDA #7 \ Move the text cursor to column 7, row 23 (in the STA XC \ middle of the bottom text row) LDA #23 STA YC LDA #0 \ Set QQ17 = 0 to switch to ALL CAPS STA QQ17 LDA #189 \ Print recursive token 29 ("HYPERSPACE ") JSR TT27 LDA QQ8+1 \ If the high byte of the distance to the selected BNE TT147 \ system in QQ8 is > 0, then it is definitely too far to \ jump (as our maximum range is 7.0 light years, or a \ value of 70 in QQ8(1 0)), so jump to TT147 to print \ "RANGE?" and return from the subroutine using a tail \ call LDA QQ14 \ Fetch our current fuel level from Q114 into A CMP QQ8 \ If our fuel reserves are less than the distance to the BCC TT147 \ selected system, then we don't have enough fuel for \ this jump, so jump to TT147 to print "RANGE?" and \ return from the subroutine using a tail call LDA #'-' \ Print a hyphen JSR TT27 JSR cpl \ Call cpl to print the name of the selected system \ Fall through into wW to start the hyperspace countdown
Name: wW Type: Subroutine Category: Flight Summary: Start a hyperspace countdown
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * Ghy calls wW

Start the hyperspace countdown (for both inter-system hyperspace and the galactic hyperdrive).
.wW LDA #15 \ The hyperspace countdown starts from 15, so set A to \ 15 so we can set the two hyperspace counters STA QQ22+1 \ Set the number in QQ22+1 to A, which is the number \ that's shown on-screen during the hyperspace countdown STA QQ22 \ Set the number in QQ22 to 15, which is the internal \ counter that counts down by 1 each iteration of the \ main game loop, and each time it reaches zero, the \ on-screen counter gets decremented, and QQ22 gets set \ to 5, so setting QQ22 to 15 here makes the first tick \ of the hyperspace counter longer than subsequent ticks TAX \ Print the 8-bit number in X (i.e. 15) at text location JMP ee3 \ (0, 1), padded to 5 digits, so it appears in the top \ left corner of the screen, and return from the \ subroutine using a tail call
Name: Ghy Type: Subroutine Category: Flight Summary: Perform a galactic hyperspace jump Deep dive: Twisting the system seeds Galaxy and system seeds
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * hyp calls Ghy * hyp calls via zZ+1

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
.Ghy 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 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") JSR wW \ Call wW to start the hyperspace countdown LDX #5 \ To move galaxy, we rotate the galaxy's seeds left, so \ set a counter in X for the 6 seed bytes INC GCNT \ Increment the current galaxy number in GCNT 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) .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 .zZ 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) JSR TT110 \ Call TT110 to show the front space view 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) LDX #0 \ Set the distance to the selected system in QQ8(1 0) STX QQ8 \ to 0 STX QQ8+1 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
Name: jmp Type: Subroutine Category: Universe Summary: Set the current system to the selected system
Context: See this subroutine on its own page References: This subroutine is called as follows: * hyp1 calls jmp

Returns: (QQ0, QQ1) The galactic coordinates of the new system
Other entry points: hy5 Contains an RTS
.jmp LDA QQ9 \ Set the current system's galactic x-coordinate to the STA QQ0 \ x-coordinate of the selected system LDA QQ10 \ Set the current system's galactic y-coordinate to the STA QQ1 \ y-coordinate of the selected system .hy5 RTS \ Return from the subroutine
Name: ee3 Type: Subroutine Category: Flight Summary: Print the hyperspace countdown in the top-left of the screen
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT102 calls ee3 * TTX66 calls ee3 * wW calls ee3

Print the 8-bit number in X at text location (1, 1). Print the number to 5 digits, left-padding with spaces for numbers with fewer than 3 digits (so numbers < 10000 are right-aligned), with no decimal point.
Arguments: X The number to print
.ee3 LDY #1 \ Move the text cursor to column 1 STY XC STY YC \ Move the text cursor to row 1 DEY \ Decrement Y to 0 for the high byte in pr6 \ Fall through into pr6 to print X to 5 digits, as the \ high byte in Y is 0
Name: pr6 Type: Subroutine Category: Text Summary: Print 16-bit number, left-padded to 5 digits, no point
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT25 calls pr6

Print the 16-bit number in (Y X) to 5 digits, left-padding with spaces for numbers with fewer than 3 digits (so numbers < 10000 are right-aligned), with no decimal point.
Arguments: X The low byte of the number to print Y The high byte of the number to print
.pr6 CLC \ Do not display a decimal point when printing \ Fall through into pr5 to print X to 5 digits
Name: pr5 Type: Subroutine Category: Text Summary: Print a 16-bit number, left-padded to 5 digits, and optional point
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT146 calls pr5 * TT151 calls pr5 * TT25 calls pr5

Print the 16-bit number in (Y X) to 5 digits, left-padding with spaces for numbers with fewer than 3 digits (so numbers < 10000 are right-aligned). Optionally include a decimal point.
Arguments: X The low byte of the number to print Y The high byte of the number to print C flag If set, include a decimal point
.pr5 LDA #5 \ Set the number of digits to print to 5 JMP TT11 \ Call TT11 to print (Y X) to 5 digits and return from \ the subroutine using a tail call
Name: TT147 Type: Subroutine Category: Flight Summary: Print an error when a system is out of hyperspace range
Context: See this subroutine on its own page References: This subroutine is called as follows: * hyp calls TT147

Print "RANGE?" for when the hyperspace distance is too far
.TT147 LDA #202 \ Load A with token 42 ("RANGE") and fall through into \ prq to print it, followed by a question mark
Name: prq Type: Subroutine Category: Text Summary: Print a text token followed by a question mark
Context: See this subroutine on its own page References: No direct references to this subroutine in this source file

Arguments: A The text token to be printed
.prq JSR TT27 \ Print the text token in A LDA #'?' \ Print a question mark and return from the JMP TT27 \ subroutine using a tail call
Name: TTX110 Type: Subroutine Category: Flight Summary: Set the current system to the nearest system and return to hyp
Context: See this subroutine on its own page References: This subroutine is called as follows: * hyp calls TTX110
.TTX110 \ This routine is only called from the hyp routine, and \ it jumps back into hyp at label TTX111 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 JMP TTX111 \ Return to TTX111 in the hyp routine
Name: TT151 Type: Subroutine Category: Market Summary: Print the name, price and availability of a market item Deep dive: Market item prices and availability Galaxy and system seeds
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT167 calls TT151

Arguments: A The number of the market item to print, 0-16 (see QQ23 for details of item numbers)
Returns: QQ19+1 Byte #1 from the market prices table for this item QQ24 The item's price / 4 QQ25 The item's availability
Name: TT152 Type: Subroutine Category: Market Summary: Print the unit ("t", "kg" or "g") for a market item
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT151 calls TT152 * TT210 calls TT152

Print the unit ("t", "kg" or "g") for the market item whose byte #1 from the market prices table is in QQ19+1, right-padded with spaces to a width of two characters (so that's "t ", "kg" or "g ").
.TT152 LDA QQ19+1 \ Fetch the economic_factor from QQ19+1 AND #96 \ If bits 5 and 6 are both clear, jump to TT160 to BEQ TT160 \ print "t" for tonne, followed by a space, and return \ from the subroutine using a tail call CMP #32 \ If bit 5 is set, jump to TT161 to print "kg" for BEQ TT161 \ kilograms, and return from the subroutine using a tail \ call JSR TT16a \ Otherwise call TT16a to print "g" for grams, and fall \ through into TT162 to print a space and return from \ the subroutine
Name: TT162 Type: Subroutine Category: Text Summary: Print a space
Context: See this subroutine on its own page References: This subroutine is called as follows: * spc calls TT162 * TT160 calls TT162 * TT25 calls TT162 * TTX66 calls TT162 * TT151 calls via TT162+2 * TT163 calls via TT162+2

Other entry points: TT162+2 Jump to TT27 to print the text token in A
.TT162 LDA #' ' \ Load a space character into A JMP TT27 \ Print the text token in A and return from the \ subroutine using a tail call
Name: TT160 Type: Subroutine Category: Market Summary: Print "t" (for tonne) and a space
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT152 calls TT160
.TT160 LDA #'t' \ Load a "t" character into A JSR TT26 \ Print the character, using TT216 so that it doesn't \ change the character case BCC TT162 \ Jump to TT162 to print a space and return from the \ subroutine using a tail call (this BCC is effectively \ a JMP as the C flag is cleared by TT26)
Name: TT161 Type: Subroutine Category: Market Summary: Print "kg" (for kilograms)
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT152 calls TT161
.TT161 LDA #'k' \ Load a "k" character into A JSR TT26 \ Print the character, using TT216 so that it doesn't \ change the character case, and fall through into \ TT16a to print a "g" character
Name: TT16a Type: Subroutine Category: Market Summary: Print "g" (for grams)
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT152 calls TT16a
.TT16a LDA #'g' \ Load a "g" character into A JMP TT26 \ Print the character, using TT216 so that it doesn't \ change the character case, and return from the \ subroutine using a tail call
Name: TT163 Type: Subroutine Category: Market Summary: Print the headers for the table of market prices
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT167 calls TT163

Print the column headers for the prices table in the Buy Cargo and Market Price screens.
.TT163 LDA #17 \ Move the text cursor in XC to column 17 STA XC LDA #255 \ Print recursive token 95 token ("UNIT QUANTITY BNE TT162+2 \ {crlf} PRODUCT UNIT PRICE FOR SALE{crlf}{lf}") by \ jumping to TT162+2, which contains JMP TT27 (this BNE \ is effectively a JMP as A will never be zero), and \ return from the subroutine using a tail call
Name: TT167 Type: Subroutine Category: Market Summary: Show the Market Price screen (red key f7)
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT102 calls TT167
.TT167 LDA #16 \ Clear the top part of the screen, draw a white border, JSR TT66 \ and set the current view type in QQ11 to 16 (Market \ Price screen) LDA #5 \ Move the text cursor to column 5 STA XC LDA #167 \ Print recursive token 7 ("{current system name} MARKET JSR NLIN3 \ PRICES") and draw a horizontal line at pixel row 19 \ to box in the title LDA #3 \ Move the text cursor to row 3 STA YC JSR TT163 \ Print the column headers for the prices table LDA #0 \ We're going to loop through all the available market STA QQ29 \ items, so we set up a counter in QQ29 to denote the \ current item and start it at 0 .TT168 LDX #%10000000 \ Set bit 7 of QQ17 to switch to Sentence Case, with the STX QQ17 \ next letter in capitals JSR TT151 \ Call TT151 to print the item name, market price and \ availability of the current item, and set QQ24 to the \ item's price / 4, QQ25 to the quantity available and \ QQ19+1 to byte #1 from the market prices table for \ this item INC YC \ Move the text cursor down one row INC QQ29 \ Increment QQ29 to point to the next item LDA QQ29 \ If QQ29 >= 17 then jump to TT168 as we have done the CMP #17 \ last item BCC TT168 RTS \ Return from the subroutine
Name: var Type: Subroutine Category: Market Summary: Calculate QQ19+3 = economy * |economic_factor|
Context: See this subroutine on its own page References: This subroutine is called as follows: * GVL calls var * TT151 calls var

Set QQ19+3 = economy * |economic_factor|, given byte #1 of the market prices table for an item. Also sets the availability of alien items to 0. This routine forms part of the calculations for market item prices (TT151) and availability (GVL).
Arguments: QQ19+1 Byte #1 of the market prices table for this market item (which contains the economic_factor in bits 0-5, and the sign of the economic_factor in bit 7)
.var LDA QQ19+1 \ Extract bits 0-5 from QQ19+1 into A, to get the AND #31 \ economic_factor without its sign, in other words: \ \ A = |economic_factor| LDY QQ28 \ Set Y to the economy byte of the current system STA QQ19+2 \ Store A in QQ19+2 CLC \ Clear the C flag so we can do additions below LDA #0 \ Set AVL+16 (availability of alien items) to 0, STA AVL+16 \ setting A to 0 in the process .TT153 \ We now do the multiplication by doing a series of \ additions in a loop, building the result in A. Each \ loop adds QQ19+2 (|economic_factor|) to A, and it \ loops the number of times given by the economy byte; \ in other words, because A starts at 0, this sets: \ \ A = economy * |economic_factor| DEY \ Decrement the economy in Y, exiting the loop when it BMI TT154 \ becomes negative ADC QQ19+2 \ Add QQ19+2 to A JMP TT153 \ Loop back to TT153 to do another addition .TT154 STA QQ19+3 \ Store the result in QQ19+3 RTS \ Return from the subroutine
Name: hyp1 Type: Subroutine Category: Universe Summary: Process a jump to the system closest to (QQ9, QQ10)
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT18 calls via hyp1+3

Do a hyperspace jump to the system closest to galactic coordinates (QQ9, QQ10), and set up the current system's state to those of the new system.
Returns: (QQ0, QQ1) The galactic coordinates of the new system QQ2 to QQ2+6 The seeds of the new system EV Set to 0 QQ28 The new system's economy tek The new system's tech level gov The new system's government
Other entry points: hyp1+3 Jump straight to the system at (QQ9, QQ10) without first calculating which system is closest. We do this if we already know that (QQ9, QQ10) points to a system
.hyp1 JSR TT111 \ Select the system closest to galactic coordinates \ (QQ9, QQ10) JSR jmp \ Set the current system to the selected system LDX #5 \ We now want to copy the seeds for the selected system \ in QQ15 into QQ2, where we store the seeds for the \ current system, so set up a counter in X for copying \ 6 bytes (for three 16-bit seeds) .TT112 LDA QQ15,X \ Copy the X-th byte in QQ15 to the X-th byte in QQ2, to STA QQ2,X \ update the selected system to the new one. Note that \ this approach has a minor bug associated with it: if \ your hyperspace counter hits 0 just as you're docking, \ then you will magically appear in the station in your \ hyperspace destination, without having to go to the \ effort of actually flying there. This bug was fixed in \ later versions by saving the destination seeds in a \ separate location called safehouse, and using those \ instead... but that isn't the case in this version DEX \ Decrement the counter BPL TT112 \ Loop back to TT112 if we still have more bytes to \ copy INX \ Set X = 0 (as we ended the above loop with X = &FF) STX EV \ Set EV, the extra vessels spawning counter, to 0, as \ we are entering a new system with no extra vessels \ spawned LDA QQ3 \ Set the current system's economy in QQ28 to the STA QQ28 \ selected system's economy from QQ3 LDA QQ5 \ Set the current system's tech level in tek to the STA tek \ selected system's economy from QQ5 LDA QQ4 \ Set the current system's government in gov to the STA gov \ selected system's government from QQ4 \ Fall through into GVL to calculate the availability of \ market items in the new system
Name: GVL Type: Subroutine Category: Universe Summary: Calculate the availability of market items Deep dive: Market item prices and availability Galaxy and system seeds
Context: See this subroutine on its own page References: No direct references to this subroutine in this source file

Calculate the availability for each market item and store it in AVL. This is called on arrival in a new system.
Other entry points: hyR Contains an RTS
Name: GTHG Type: Subroutine Category: Universe Summary: Spawn a Thargoid ship and a Thargon companion Deep dive: Fixing ship positions
Context: See this subroutine on its own page References: This subroutine is called as follows: * MJP calls GTHG
.GTHG JSR Ze \ Call Ze to initialise INWK \ \ Note that because Ze uses the value of X returned by \ DORND, and X contains the value of A returned by the \ previous call to DORND, this does not set the new ship \ to a totally random location. See the deep dive on \ "Fixing ship positions" for details LDA #%11111111 \ Set the AI flag in byte #32 so that the ship has AI, STA INWK+32 \ is extremely and aggressively hostile, and has E.C.M. LDA #THG \ Call NWSHP to add a new Thargoid ship to our local JSR NWSHP \ bubble of universe LDA #TGL \ Call NWSHP to add a new Thargon ship to our local JMP NWSHP \ bubble of universe, and return from the subroutine \ using a tail call
Name: MJP Type: Subroutine Category: Flight Summary: Process a mis-jump into witchspace
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT18 calls MJP * TT18 calls via ptg

Process a mis-jump into witchspace (which happens very rarely). Witchspace has a strange, almost dust-free aspect to it, and it is populated by hostile Thargoids. Using our escape pod will be fatal, and our position on the galactic chart is in-between systems. It is a scary place... There is a 0.78% chance that this routine is called from TT18 instead of doing a normal hyperspace, or we can manually trigger a mis-jump by holding down CTRL after first enabling the "author display" configuration option ("X") when paused.
Other entry points: ptg Called when the user manually forces a mis-jump
.ptg LSR COK \ Set bit 0 of the competition flags in COK, so that the SEC \ competition code will include the fact that we have ROL COK \ manually forced a mis-jump into witchspace .MJP LDA #3 \ Call SHIPinA to load ship blueprints file D, which is JSR SHIPinA \ one of the two files that contain Thargoids LDA #3 \ Clear the top part of the screen, draw a white border, JSR TT66 \ and set the current view type in QQ11 to 3 JSR LL164 \ Call LL164 to show the hyperspace tunnel and make the \ hyperspace sound for a second time (as we already \ called LL164 in TT18) JSR RES2 \ Reset a number of flight variables and workspaces, as \ well as setting Y to &FF STY MJ \ Set the mis-jump flag in MJ to &FF, to indicate that \ we are now in witchspace .MJP1 JSR GTHG \ Call GTHG to spawn a Thargoid ship and a Thargon \ companion LDA #3 \ Fetch the number of Thargoid ships from MANY+THG, and CMP MANY+THG \ if it is less than or equal to 3, loop back to MJP1 to BCS MJP1 \ spawn another one, until we have four Thargoids STA NOSTM \ Set NOSTM (the maximum number of stardust particles) \ to 3, so there are fewer bits of stardust in \ witchspace (normal space has a maximum of 18) LDX #0 \ Initialise the front space view JSR LOOK1 LDA QQ1 \ Fetch the current system's galactic y-coordinate in EOR #%00011111 \ QQ1 and flip bits 0-5, so we end up somewhere in the STA QQ1 \ vicinity of our original destination, but above or \ below it in the galactic chart RTS \ Return from the subroutine
Name: TT18 Type: Subroutine Category: Flight Summary: Try to initiate a jump into hyperspace
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * TT102 calls TT18

Try to go through hyperspace. Called from TT102 in the main loop when the hyperspace countdown has finished.
Name: TT110 Type: Subroutine Category: Flight Summary: Launch from a station or show the front space view
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * Ghy calls TT110 * TT102 calls TT110

Launch the ship (if we are docked), or show the front space view (if we are already in space). Called when red key f0 is pressed while docked (launch), after we arrive in a new galaxy, or after a hyperspace if the current view is a space view.
.TT110 LDX QQ12 \ If we are not docked (QQ12 = 0) then jump to NLUNCH BEQ NLUNCH \ to skip the launch tunnel and setup process JSR LAUN \ Show the space station launch tunnel JSR RES2 \ Reset a number of flight variables and workspaces JSR TT111 \ Select the system closest to galactic coordinates \ (QQ9, QQ10) INC INWK+8 \ Increment z_sign ready for the call to SOS, so the \ planet appears at a z_sign of 1 in front of us when \ we launch JSR SOS1 \ Call SOS1 to set up the planet's data block and add it \ to FRIN, where it will get put in the first slot as \ it's the first one to be added to our local bubble of \ universe following the call to RES2 above LDA #128 \ For the space station, set z_sign to &80, so it's STA INWK+8 \ behind us (&80 is negative) INC INWK+7 \ And increment z_hi, so it's only just behind us JSR NWSPS \ Add a new space station to our local bubble of \ universe LDA #12 \ Set our launch speed in DELTA to 12 STA DELTA JSR BAD \ Call BAD to work out how much illegal contraband we \ are carrying in our hold (A is up to 40 for a \ standard hold crammed with contraband, up to 70 for \ an extended cargo hold full of narcotics and slaves) ORA FIST \ OR the value in A with our legal status in FIST to \ get a new value that is at least as high as both \ values, to reflect the fact that launching with a \ hold full of contraband can only make matters worse STA FIST \ Update our legal status with the new value LDA #255 \ Set the view type in QQ11 to 255 STA QQ11 JSR HFS1 \ Call HFS1 to draw 8 concentric rings to remove the \ launch tunnel that we drew above .NLUNCH LDX #0 \ Set QQ12 to 0 to indicate we are not docked STX QQ12 JMP LOOK1 \ Jump to LOOK1 to switch to the front view (X = 0), \ returning from the subroutine using a tail call
Name: TT114 Type: Subroutine Category: Charts Summary: Display either the Long-range or Short-range Chart
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT18 calls TT114

Display either the Long-range or Short-range Chart, depending on the current view setting. Called from TT18 once we know the current view is one of the charts.
Arguments: A The current view, loaded from QQ11
.TT114 BMI TT115 \ If bit 7 of the current view is set (i.e. the view is \ the Short-range Chart, 128), skip to TT115 below to \ jump to TT23 to display the chart JMP TT22 \ Otherwise the current view is the Long-range Chart, so \ jump to TT22 to display it .TT115 JMP TT23 \ Jump to TT23 to display the Short-range Chart
Name: MCASH Type: Subroutine Category: Maths (Arithmetic) Summary: Add an amount of cash to the cash pot
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT18 calls via TT113

Add (Y X) cash to the cash pot in CASH. As CASH is a four-byte number, this calculates: CASH(0 1 2 3) = CASH(0 1 2 3) + (Y X)
Other entry points: TT113 Contains an RTS
.MCASH TXA \ Add the least significant bytes: CLC \ ADC CASH+3 \ CASH+3 = CASH+3 + X STA CASH+3 TYA \ Then the second most significant bytes: ADC CASH+2 \ STA CASH+2 \ CASH+2 = CASH+2 + Y LDA CASH+1 \ Then the third most significant bytes (which are 0): ADC #0 \ STA CASH+1 \ CASH+1 = CASH+1 + 0 LDA CASH \ And finally the most significant bytes (which are 0): ADC #0 \ STA CASH \ CASH = CASH + 0 CLC \ Clear the C flag, so if the above was done following \ a failed LCASH call, the C flag correctly indicates \ failure .TT113 RTS \ Return from the subroutine
Name: GCASH Type: Subroutine Category: Maths (Arithmetic) Summary: Calculate (Y X) = P * Q * 4
Context: See this subroutine on its own page References: No direct references to this subroutine in this source file

Calculate the following multiplication of unsigned 8-bit numbers: (Y X) = P * Q * 4
.GCASH JSR MULTU \ Call MULTU to calculate (A P) = P * Q
Name: GC2 Type: Subroutine Category: Maths (Arithmetic) Summary: Calculate (Y X) = (A P) * 4
Context: See this subroutine on its own page References: This subroutine is called as follows: * TT151 calls GC2

Calculate the following multiplication of unsigned 16-bit numbers: (Y X) = (A P) * 4
.GC2 ASL P \ Set (A P) = (A P) * 4 ROL A ASL P ROL A TAY \ Set (Y X) = (A P) LDX P RTS \ Return from the subroutine
Name: hm Type: Subroutine Category: Charts Summary: Select the closest system and redraw the chart crosshairs
Context: See this subroutine on its own page Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * hyp calls hm * TT102 calls hm

Set the system closest to galactic coordinates (QQ9, QQ10) as the selected system, redraw the crosshairs on the chart accordingly (if they are being shown), and, if this is not a space view, clear the bottom three text rows of the screen.
.hm JSR TT103 \ Draw small crosshairs at coordinates (QQ9, QQ10), \ which will erase the crosshairs currently there JSR TT111 \ Select the system closest to galactic coordinates \ (QQ9, QQ10) JSR TT103 \ Draw small crosshairs at coordinates (QQ9, QQ10), \ which will draw the crosshairs at our current home \ system JMP CLYNS \ Clear the bottom three text rows of the upper screen, \ and move the text cursor to column 1 on row 21, i.e. \ the start of the top row of the three bottom rows \ Return from the subroutine using a tail call
Save ELTD.bin
PRINT "ELITE D" PRINT "Assembled at ", ~CODE_D% PRINT "Ends at ", ~P% PRINT "Code size is ", ~(P% - CODE_D%) PRINT "Execute at ", ~LOAD% PRINT "Reload at ", ~LOAD_D% PRINT "S.ELTD ", ~CODE_D%, " ", ~P%, " ", ~LOAD%, " ", ~LOAD_D% \SAVE "3-assembled-output/D.ELTD.bin", CODE_D%, P%, LOAD%