2017-08-20 22:15:46 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import math
|
|
|
|
|
|
|
|
|
2017-08-21 00:51:12 +00:00
|
|
|
def twosCompliment(value):
|
|
|
|
return (value^65535)+1
|
|
|
|
|
2017-08-25 04:45:05 +00:00
|
|
|
def toFixed124(flt): # Floating point to 12.4 fixed point
|
2017-08-23 03:57:07 +00:00
|
|
|
whole = max(-2048, min(2047, math.trunc(flt))) # Clamp to signed range
|
2017-08-21 00:51:12 +00:00
|
|
|
frac = abs(flt)%1
|
2017-08-23 03:57:07 +00:00
|
|
|
binary = (abs(whole)<<4) + math.trunc(16*frac)
|
2017-08-21 00:51:12 +00:00
|
|
|
|
2017-08-23 03:57:07 +00:00
|
|
|
if (flt<0 and abs(flt)>0.06):
|
2017-08-21 00:51:12 +00:00
|
|
|
binary = twosCompliment(binary)
|
|
|
|
|
|
|
|
return binary
|
|
|
|
|
|
|
|
|
2017-08-25 04:45:05 +00:00
|
|
|
def toFixed88(flt): # Floating point to 8.8 fixed point
|
|
|
|
whole = max(-128, min(127, math.trunc(flt))) # Clamp to signed range
|
|
|
|
frac = abs(flt)%1
|
|
|
|
binary = (abs(whole)<<8) + math.trunc(256*frac)
|
|
|
|
|
|
|
|
if (flt<0 and abs(flt)>0.0037):
|
|
|
|
binary = twosCompliment(binary)
|
|
|
|
|
|
|
|
return binary
|
|
|
|
|
|
|
|
|
2017-08-20 22:15:46 +00:00
|
|
|
def main(argv):
|
|
|
|
|
2017-08-23 03:57:07 +00:00
|
|
|
print ("sineTable:\t\t; 8.8 fixed point",end="")
|
2017-08-20 22:15:46 +00:00
|
|
|
rowCount = 7
|
|
|
|
|
|
|
|
for v in range(0,512):
|
|
|
|
rowCount += 1
|
|
|
|
if (rowCount==8):
|
|
|
|
print ("\n\t.word ", end="")
|
|
|
|
rowCount=0
|
|
|
|
|
2017-08-21 00:51:12 +00:00
|
|
|
print ("$%04x" % (int)(math.sin((2*3.14159)/256 * v%256)*128+128), end="")
|
2017-08-20 22:15:46 +00:00
|
|
|
if (rowCount<7):
|
|
|
|
print (",", end="")
|
|
|
|
|
|
|
|
|
2017-08-25 04:45:05 +00:00
|
|
|
print ("\n\nangleToVectorX:\t\t; 8.8 fixed point, counterclockwise angle, +x=(1,0)",end="")
|
2017-08-21 00:51:12 +00:00
|
|
|
rowCount = 11
|
|
|
|
|
|
|
|
for v in range(0,360):
|
|
|
|
rowCount += 1
|
|
|
|
if (rowCount==12):
|
|
|
|
print ("\n\t.word ", end="")
|
|
|
|
rowCount=0
|
|
|
|
|
2017-08-25 04:45:05 +00:00
|
|
|
print ("$%04x" % toFixed88(math.cos(math.radians(v))), end="")
|
2017-08-21 00:51:12 +00:00
|
|
|
if (rowCount<11):
|
|
|
|
print (",", end="")
|
|
|
|
|
|
|
|
|
2017-08-25 04:45:05 +00:00
|
|
|
print ("\n\nangleToVectorY:\t\t; 8.8 fixed point, counterclockwise angle, +x=(1,0)",end="")
|
2017-08-21 00:51:12 +00:00
|
|
|
rowCount = 11
|
|
|
|
|
|
|
|
for v in range(0,360):
|
|
|
|
rowCount += 1
|
|
|
|
if (rowCount==12):
|
|
|
|
print ("\n\t.word ", end="")
|
|
|
|
rowCount=0
|
|
|
|
|
2017-08-25 04:45:05 +00:00
|
|
|
print ("$%04x" % toFixed88(math.sin(math.radians(v))), end="")
|
2017-08-21 00:51:12 +00:00
|
|
|
if (rowCount<11):
|
|
|
|
print (",", end="")
|
|
|
|
|
|
|
|
print ("\n\n")
|
|
|
|
|
2017-08-20 22:15:46 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv[1:])
|
|
|
|
|