Skip to navigation

Revs on the BBC Micro

Maths (Arithmetic): Scale16Bit

Name: Scale16Bit [Show more] Type: Subroutine Category: Maths (Arithmetic) Summary: Scale up a 16-bit value by 2^5
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * ApplySkidForces calls Scale16Bit * GetTyreForces calls Scale16Bit

Scale up a 16-bit value by 2^5, capping the result at the maximum possible positive value of &7Fxx, and ensuring that the result is positive. Arguments: Y The offset of the variable to scale * 8 = xVelocity * 9 = zVelocity Returns: (NN MM) The scaled value
.Scale16Bit LDA xPlayerSpeedHi,Y \ Set (A MM) to the variable pointed to by Y, which we STA MM \ call variableY LDA xPlayerSpeedTop,Y BPL scal1 \ If the top byte in A is positive, jump to scal1 to \ skip the following LDA #0 \ Negate (A MM), starting with the low bytes SEC SBC MM STA MM LDA #0 \ And then the high bytes, so we now have: SBC xPlayerSpeedTop,Y \ \ (A MM) = |variableY| .scal1 LDY #5 \ Set Y = 5, to act as a shift counter in the following \ loop .scal2 ASL MM \ Set (A MM) = (A MM) << 1 ROL A BMI scal4 \ If bit 7 of the top byte in A is set, jump to scal4 \ to stop shifting and set the top byte to the largest \ possible positive top byte DEY \ Decrement the shift counter BNE scal2 \ Loop back until we have left-shifted five times .scal3 STA NN \ Set (NN MM) = (A MM) to return as the result RTS \ Return from the subroutine .scal4 LDA #%01111111 \ Set A = %01111111 to act as the largest possible \ positive top byte BNE scal3 \ Jump to scal3 to return (NN MM)