Skip to navigation

Elite on the BBC Micro

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 4A variation in the comments only

Tap on a version to expand it, and tap it again to show to all variations.

Cassette, Flight, Docked, 6502SP, Electron

Master

Name: DVID4
Name: DVID4_DUPLICATE
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 4A variation in the labels only

Tap on a version to expand it, and tap it again to show to all variations.

Cassette, Flight, Docked, 6502SP, Electron

Master

.DVID4
.DVID4_DUPLICATE \ 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

.DVL4

 ROL A                  \ Shift A to the left

 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

 SBC Q                  \ A >= Q, so set A = A - Q

Code variation 3 of 4Other (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
.DV5

 ROL P                  \ Shift P to the left, pulling the C flag into bit 0

 DEX                    \ Decrement the loop counter

 BNE DVL4               \ Loop back for the next bit until we have done all 8
                        \ bits of P

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

See variation 3 above for details.

Tap on a version to expand it, and tap it again to show to all variations.

Cassette, Flight, Docked, Electron

6502SP, 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
RSS Feed