Elite on the BBC Micro and NES

Version analysis of tnpr

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

Code variations between these versions are shown below.

```       Name: tnpr
Type: Subroutine
Category: Market
Summary: Work out if we have space for a specific amount of cargo

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

.tnpr

PHA                    \ Store A on the stack

LDX #12                \ If QQ29 > 12 then jump to kg below, as this cargo
CPX QQ29               \ type is gold, platinum, gem-stones or alien items,
BCC kg                 \ and they have different cargo limits to the standard
\ tonne canisters

.Tml

\ Here we count the tonne canisters we have in the hold
\ and add to A to see if we have enough room for A more
\ tonnes of cargo, using X as the loop counter, starting
\ with X = 12

ADC QQ20,X             \ Set A = A + the number of tonnes we have in the hold
\ of market item number X. Note that the first time we
\ go round this loop, the C flag is set (as we didn't
\ branch with the BCC above, so the effect of this loop
\ is to count the number of tonne canisters in the hold,

DEX                    \ Decrement the loop counter

BPL Tml                \ Loop back to add in the next market item in the hold,
\ until we have added up all market items from 12
\ (minerals) down to 0 (food)

```

Code variation 1 of 3Related to the Master version

The Master version contains the code for Trumbles to take up cargo space, though as we never actually get given any Trumbles, the value is always zero.

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

```Master ADC TRIBBLE+1          \ Add the high byte of the number of Trumbles in the
\ hold, as 256 Trumbles take up one tonne of cargo space
```
``` CMP CRGO               \ If A < CRGO then the C flag will be clear (we have
\ room in the hold)
\
\ If A >= CRGO then the C flag will be set (we do not
\ have room in the hold)
\
\ This works because A contains the number of canisters
\ plus 1, while CRGO contains our cargo capacity plus 2,
\ so if we actually have "a" canisters and a capacity
\ of "c", then:
\
\ A < CRGO means: a+1 <  c+2
\                 a   <  c+1
\                 a   <= c
\
\ So this is why the value in CRGO is 2 higher than the
\ actual cargo bay size, i.e. it's 22 for the standard
\ 20-tonne bay, and 37 for the large 35-tonne bay

PLA                    \ Restore A from the stack

RTS                    \ Return from the subroutine

.kg

\ Here we count the number of items of this type that
\ we already have in the hold, and add to A to see if
\ we have enough room for A more units

LDY QQ29               \ Set Y to the item number we want to add

```

Code variation 2 of 3Related to the BBC Micro disc version

When scooping gold, platinum, gem-stones or alien items in the disc version, we are allowed to scoop what we want as long as our existing hold containss 200 units or less (irrespecitve of what we're trying to scoop); in the other versions, we can only scoop more units if it would result in a total haul of 200 units or less.

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

```Cassette, Docked, 6502SP, Master, ElectronFlight ADC QQ20,Y             \ Set A = A + the number of units of this item that we
\ already have in the hold
LDA QQ20,Y             \ Set A to the number of units of this item that we
\ already have in the hold
```
``` CMP #200               \ Is the result greater than 200 (the limit on
\ individual stocks of gold, platinum, gem-stones and
\ alien items)?
\
\ If so, this sets the C flag (no room)
\
\ Otherwise it is clear (we have room)

PLA                    \ Restore A from the stack

RTS                    \ Return from the subroutine

```

Code variation 3 of 3Minor and very low-impact

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

```6502SP NOP                    \ This instruction appears to have no effect
```