Sine lookup table question
Rumple
Posts: 38
Many years ago I captured a handy-looking snippet from this forum that illustrated a method of retrieving sine values from the Prop's internal lookup table. It looked a lot like this:
Well, now I have a use for it so I'm using it. And it works superbly. But it bothers me that I can't figure out HOW it works superbly. There are gaping holes in my bit-twiddling knowledge. Could somebody gently explain to me how that shifting and masking makes an address out of an an angle (if that's what it's doing), and what problem that IF solves?
Thanks.
PUB Sin(angle) 'Sin angle is 13-bit, returns 16-bits Result := angle << 1 & $FFE if angle & $800 Result := word[$F000 - Result] else Result := word[$E000 + Result]
Well, now I have a use for it so I'm using it. And it works superbly. But it bothers me that I can't figure out HOW it works superbly. There are gaping holes in my bit-twiddling knowledge. Could somebody gently explain to me how that shifting and masking makes an address out of an an angle (if that's what it's doing), and what problem that IF solves?
Thanks.
Comments
Rumple, I think the spin code you posted above works for angles in the range of 0 to 180 degrees, those for which the result is a positive number. It takes a bit more code to complete the circle.
The IF statement you asked about looks at bit 12 of the incoming angle. If that is a 1, that means the angle is in the second quadrant and the values in the table have to be read "backwards", that is subtracting from 90 degrees and working down to 0+ degrees. Thus The 90 degree value for the sine table is located at word address $F000 in the HUB, and the 0 degree address is at $E000.
Note that $F000 - $E000 = $1000 or 4096 table bytes, 2048 table words, 11 bits worth, but note also that the table is $E000 to $F000 inclusive, so that is 2049 words. The incoming angle is shifted left by one to turn it into a word address, then the $FFE makes it 11 bits, and that is what goes into the table lookup. In the first quadrant it starts at $E000 and goes up to $EFFE. In the second quadrant it starts at $F000 and goes down to $E002.
And thanks to Brian and Max for turning me on to 812 pages of Prop stuff I've never read before.