genlib/cordic: cleanup

This commit is contained in:
Sebastien Bourdeauducq 2013-06-26 22:46:04 +02:00
parent 080afdc3f9
commit 48a5b86dcd
1 changed files with 8 additions and 18 deletions

View File

@ -2,7 +2,6 @@ from math import atan, atanh, log, sqrt, pi
from migen.fhdl.std import * from migen.fhdl.std import *
class TwoQuadrantCordic(Module): class TwoQuadrantCordic(Module):
""" """
http://eprints.soton.ac.uk/267873/1/tcas1_cordic_review.pdf http://eprints.soton.ac.uk/267873/1/tcas1_cordic_review.pdf
@ -10,7 +9,7 @@ class TwoQuadrantCordic(Module):
def __init__(self, width=16, stages=None, guard=0, def __init__(self, width=16, stages=None, guard=0,
eval_mode="iterative", cordic_mode="rotate", eval_mode="iterative", cordic_mode="rotate",
func_mode="circular"): func_mode="circular"):
# validate paramters # validate parameters
assert eval_mode in ("combinatorial", "pipelined", "iterative") assert eval_mode in ("combinatorial", "pipelined", "iterative")
assert cordic_mode in ("rotate", "vector") assert cordic_mode in ("rotate", "vector")
self.cordic_mode = cordic_mode self.cordic_mode = cordic_mode
@ -113,9 +112,7 @@ class TwoQuadrantCordic(Module):
a_{m,i}: elemetary rotation angle a_{m,i}: elemetary rotation angle
""" """
dx, dy, dz = xi>>i, yi>>i, a dx, dy, dz = xi>>i, yi>>i, a
# FIXME need 1'sd0 here, "Signal((n, True)) >= 0" is always true direction = {"rotate": zi < 0, "vector": yi >= 0}[self.cordic_mode]
# as 1'd0 makes the comparison unsigned
direction = {"rotate": zi[-1], "vector": ~yi[-1]}[self.cordic_mode]
dy = {"circular": dy, "linear": 0, "hyperbolic": -dy}[self.func_mode] dy = {"circular": dy, "linear": 0, "hyperbolic": -dy}[self.func_mode]
ret = If(direction, ret = If(direction,
xo.eq(xi + dy), xo.eq(xi + dy),
@ -128,8 +125,6 @@ class TwoQuadrantCordic(Module):
) )
return ret return ret
class Cordic(TwoQuadrantCordic): class Cordic(TwoQuadrantCordic):
def __init__(self, **kwargs): def __init__(self, **kwargs):
TwoQuadrantCordic.__init__(self, **kwargs) TwoQuadrantCordic.__init__(self, **kwargs)
@ -165,7 +160,6 @@ class Cordic(TwoQuadrantCordic):
self.yo.eq(-cyo), self.yo.eq(-cyo),
)] )]
class TB(Module): class TB(Module):
def __init__(self, n, **kwargs): def __init__(self, n, **kwargs):
self.submodules.cordic = Cordic(**kwargs) self.submodules.cordic = Cordic(**kwargs)
@ -189,8 +183,7 @@ class TB(Module):
(self.xi, self.yi, self.zi)): (self.xi, self.yi, self.zi)):
s.wr(r, int(v.pop(0)*c)) s.wr(r, int(v.pop(0)*c))
def _main():
def main():
from migen.fhdl import verilog from migen.fhdl import verilog
from migen.sim.generic import Simulator, TopLevel from migen.sim.generic import Simulator, TopLevel
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
@ -211,8 +204,7 @@ def main():
plt.plot(tb.zo) plt.plot(tb.zo)
plt.show() plt.show()
def _rms_err(width, stages, n):
def rms_err(width, stages, n):
from migen.sim.generic import Simulator from migen.sim.generic import Simulator
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
@ -228,8 +220,7 @@ def rms_err(width, stages, n):
dy = tb.yo[1:]-y*2**(width-1) dy = tb.yo[1:]-y*2**(width-1)
return ((dx**2+dy**2)**.5).sum()/n return ((dx**2+dy**2)**.5).sum()/n
def _test_err():
def test_err():
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
import numpy as np import numpy as np
@ -244,8 +235,7 @@ def test_err():
plt.grid("on") plt.grid("on")
plt.show() plt.show()
if __name__ == "__main__": if __name__ == "__main__":
main() _main()
#rms_err(16, 16, 345) #_rms_err(16, 16, 345)
#test_err() #_test_err()