I began a library of 6502 game math subroutines when building the game Thwaite. And with the announcement that

uc65 would likely ship without multiply and divide operators, I'm considering offering it as a self-contained library. Here's what I have so far:

- Multiply A by Y, 8 bits, about 150 cycles
- Divide floor(256 * A / Y), for converting rise and run to an 0.8 fixed-point slope
- Arctangent, producing the angle of the vector from (a, b) to (c, d) in units of 1/32 turn, within 380 cycles
- Square root of a 16-bit number within 520 cycles
- Unrolled binary to decimal conversion, 8 bits to 3 digits, within 80 cycles
- Looping binary to decimal conversion, 16 bits to 5 digits, within 652 cycles
- Percentage calculator, producing the decimal expansion of a/b with 16-bit numerator and denominator to up to five digits at 230 cycles per digit. Useful for accuracy counters like the one on Galaga's debrief screen.

I built it alongside a basic 6502 simulator written in Python so that I could exhaustively verify the subroutines' correctness. This way I make sure that what the subroutine thinks is 23 * 45 matches what Python thinks is 23 * 45. I tested the simulator itself using nestest, including the official instructions and the useful unofficial instructions, to make sure it matches the register values on each line of the Nintendulator golden log.

There are two ways to calculate the length of a vector (a, b). Normally, you'd do sqrt(a * a + b * b). But Thwaite uses a shortcut to skip the square root. If you already have the angle theta, the length is two table lookups and two multiplies: a * cos(theta) + b * sin(theta).

Who is interested in having a look at this library? Are there any additions you think others might use?