I’ve successfully simulated a cos function with input x0 being the angle 0…2**n. Sector is 0 for 0…pi/2 up to 3 for 3pi/2…2pi
Implementing in hw i found that already my first multiplication on line 31 or 34 did return zero instead of the expected values which i get when simulating:
31 X.next=(x0*piquart)>>n #this results in X=0 for all x0
(I read back internal fpga data via a spi interface)
x0 indeed does a triangle wave 0…2**n
Tried with always_comb and always(‘angle or sector i.e. x0,sector’)
also with Signal or not Signal for the constants but with no success.
Any suggestions?
I can publish the complete set of 4 files if needed, here is just the cos generator:
#unit_cos4.py 10.02.21 sk
#taylor series for cos: 1- 1/2*x**2 + 1/24*x**4 -1/720*x**6
#tested ok with testcase_unit.py
#inputs sector (0..3), angle x0 (0..2**n)
#output y0 = cos(x0)
from myhdl import *
from math import pi
@block
def unit_cos4(sector,x0,y0,n,y10):
One=Signal(intbv(2**n-1,min=0,max=2**n))
piquart=Signal(intbv(int(round((pi/2.0)*2**(n-1))),min=0,max=2**n))
Inv24=Signal(intbv(int(round((1/24.0)*2**n)),min=0,max=2**n))
Inv720=Signal(intbv(int(round((1/720.0)*2**n)),min=0,max=2**n))
X=Signal(intbv(0,min=0,max=2**n))
X2=Signal(intbv(0,min=0,max=2**n))
X4=Signal(intbv(0,min=0,max=2**n))
X6=Signal(intbv(0,min=0,max=2**n))
t1=Signal(intbv(0,min=0,max=2**n))
t2=Signal(intbv(0,min=0,max=2**n))
t3=Signal(intbv(0,min=0,max=2**n))
#y10=Signal(intbv(0,min=-2**n,max=2**n)) #probe for debug only
@always_comb
def probe():
y10.next=X[:1] #X is unsigned, y10 is signed. both n bits
@always(x0,sector)
def normalize():
if sector==0 or sector==2:
X.next=(x0*piquart)>>n #this results in X=0 for all x0
#X.next=x0
else:
X.next=((One-x0)*piquart)>>n
#X.next=(One-x0) #this results in X=0 for all x0
@always_comb
def calc0():
X2.next=(X*X)>>(n+1) #half x**2
@always_comb
def calc1():
t1.next= X2 #half x**2
X4.next=(X2*X2)>>(n) #quart x**4
@always_comb
def calc2():
#t2.next=((X4*X4)>>(n))*Inv24)>>(n)
t2.next=(X4*Inv24)>>(n-2) # *4 now to compensate
X6.next=(X4*X2)>>(n) #eighth of x**6
@always_comb
def calc3():
t3.next=(X6*Inv720)>>(n-3) # *8 now to compensate
''' clipping output for debug purpose
@always_comb
def clipsin():
if Y10temp>=2**(n-1):
y0.next=2**(n-1)-1
elif Y10temp<-2**(n-1):
y0.next=-2**(n-1)
else:
y0.next=Y10temp
'''
@always_comb
def calcsum():
if sector==0 or sector==3:
#y0.next = (One-t1+t2-t3)>>1
y0.next = (One-t1+t2-t3)>>1 #1
else:
#y0.next = (t1-One-t2+t3)>>1
y0.next = (t1-One-t2+t3)>>1 #1
return instances()
indent preformatted text by 4 spaces