Elite on the BBC Micro and NES

# Version analysis of DVID4 / dvid4_duplicate

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

Code variations between these versions are shown below.

Code variation 1 of 8A variation in the comments only

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

```Cassette, Flight, Docked, 6502SP, ElectronMaster       Name: DVID4
Name: DVID4K
```
```       Type: Subroutine
Category: Maths (Arithmetic)
Summary: Calculate (P R) = 256 * A / Q
Deep dive: Shift-and-subtract division

Calculate the following division and remainder:

P = A / Q

R = remainder as a fraction of Q, where 1.0 = 255

Another way of saying the above is this:

(P R) = 256 * A / Q

This uses the same shift-and-subtract algorithm as TIS2, but this time we
keep the remainder.

Returns:

C flag               The C flag is cleared

```

Code variation 2 of 8A variation in the labels only

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

```Cassette, Flight, Docked, 6502SP, ElectronMaster.DVID4
.DVID4K

\ This is an exact duplicate of the DVID4 routine, which
\ is also present in this source, so it isn't clear why
\ this duplicate exists (especially as the other version
\ is slightly faster, as it unrolls the loop)
```
``` LDX #8                 \ Set a counter in X to count the 8 bits in A

ASL A                  \ Shift A left and store in P (we will build the result
STA P                  \ in P)

LDA #0                 \ Set A = 0 for us to build a remainder

```

Code variation 3 of 8A variation in the labels only

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

```Cassette, Flight, Docked, 6502SP, ElectronMaster.DVL4
.DVL4K
```
``` ROL A                  \ Shift A to the left

```

Code variation 4 of 8A variation in the labels only

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

```Cassette, Flight, Docked, 6502SP, ElectronMaster BCS DV8                \ If the C flag is set (i.e. bit 7 of A was set) then
\ skip straight to the subtraction

CMP Q                  \ If A < Q skip the following subtraction
BCC DV5

.DV8
BCS DV8K               \ If the C flag is set (i.e. bit 7 of A was set) then
\ skip straight to the subtraction

CMP Q                  \ If A < Q skip the following subtraction
BCC DV5K

.DV8K
```
``` SBC Q                  \ A >= Q, so set A = A - Q

```

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

There are two differences in the DVID4 routine between the original versions and the advanced versions that look like they would affect the result of the division; I haven't yet worked out what this is all about.

See below for more variations related to this code.

This variation is blank in the 6502 Second Processor and Master versions.

```Cassette, Flight, Docked, Electron SEC                    \ Set the C flag, so that P gets a 1 shifted into bit 0
```

Code variation 6 of 8A variation in the labels only

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

```Cassette, Flight, Docked, 6502SP, ElectronMaster.DV5
.DV5K
```
``` ROL P                  \ Shift P to the left, pulling the C flag into bit 0

DEX                    \ Decrement the loop counter

```

Code variation 7 of 8A variation in the labels only

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

```Cassette, Flight, Docked, 6502SP, ElectronMaster BNE DVL4               \ Loop back for the next bit until we have done all 8
\ bits of P
BNE DVL4K              \ Loop back for the next bit until we have done all 8
\ bits of P
```

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

See variation 5 above for details.

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

```Cassette, Flight, Docked, Electron6502SP, Master JMP LL28+4             \ Jump to LL28+4 to convert the remainder in A into an
\ integer representation of the fractional value A / Q,
\ in R, where 1.0 = 255. LL28+4 always returns with the
\ C flag cleared, and we return from the subroutine
\ using a tail call
RTS                    \ Return from the subroutine
```