Revs on the BBC Micro

# Extra tracks: HookJoystick (Part 2 of 2)

```       Name: HookJoystick (Part 2 of 2)                              [Show more]
Type: Subroutine
Category: Extra tracks
Summary: Apply enhanced joystick steering to specific track sections
Deep dive: Secrets of the extra tracks
Code hooks in the extra tracks
Context: See this subroutine in context in the source code
References: No direct references to this subroutine in this source file

This part of the routine scales the steering, but if the joystick x-axis high
byte is zero, then the scaling is applied in a different way. Instad of this:

2 * (scale * x-axis) ^ 2
= 2 * scale ^2 * x-axis ^2

we return this:

scale ^2 * x-axis

in other words, we divide the scale factor by 2 * x-axis.

.joys4

\ By this point, A contains the scale factor to apply to
\ the steering, which is one of the following values:
\
\   * 181 for a scale factor of 1.00
\
\   * 202 for a scale factor of 1.25
\
\   * 205 for a scale factor of 1.28
\
\   * 212 for a scale factor of 1.37

STA U                  \ Set U= A
\
\ So U= 181, 202, 205 or 212

PLA                    \ Set A= joystick x-axis high byte, which we stored on
\ the stack at the start of the routine

JSR Multiply8x8        \ Set (A T)= A * U
\          = x-axis * A

PLP                    \ If the joystick x-axis high byte is zero, which we
BEQ joys5              \ stored on the stack at the start of the routine, jump
\ to joys5 to return x-axis * scale ^ 2 instead

STA U                  \ Set U= A
\      = high byte of A * x-axis

JSR Multiply8x8        \ Set (A T)= A * U
\          = A * A
\          = (A * x-axis) ^ 2

ASL T                  \ Set (A T)= (A T) * 2
ROL A                  \          = 2 * (A * x-axis) ^ 2

\ So for A= 212 we have:
\
\   (A T)= 2 * (212/256 * x-axis) ^ 2
\        = 2 * (0.828 * x-axis) ^ 2
\        = 1.37 * x-axis ^ 2
\
\ and for A= 205 we have:
\
\   (A T)= 2 * (205/256 * x-axis) ^ 2
\        = 2 * (0.801 * x-axis) ^ 2
\        = 1.28 * x-axis ^ 2
\
\ and for A= 202 we have:
\
\   (A T)= 2 * (202/256 * x-axis) ^ 2
\        = 2 * (0.789 * x-axis) ^ 2
\        = 1.25 * x-axis ^ 2
\
\ and for A= 181 we have:
\
\   (A T)= 2 * (181/256 * x-axis) ^ 2
\        = 2 * (0.707 * x-axis) ^ 2
\        = 1.00 * x-axis ^ 2

RTS                    \ Return from the subroutine

.joys5

\ If we get here then the joystick x-axis high byte is
\ zero, U contains the scale factor and A contains the
\ top byte of scale * x-axis

JMP Multiply8x8        \ Calculate:
\
\   (A T)= A * U
\        = scale * scale * x-axis
\
\ and return the high byte in A, returning from the
\ subroutine using a tail call
```