1 # This is an unpipelined version of an sin/cos cordic, which will
2 # later be used to verify the operation of a pipelined version
4 # see http://bugs.libre-riscv.org/show_bug.cgi?id=208
5 from nmigen
import Module
, Elaboratable
, Signal
, Memory
, Cat
, Repl
, Mux
6 from nmigen
.cli
import rtlil
8 from enum
import Enum
, unique
9 from ieee754
.fpcommon
.fpbase
import FPNumBaseRecord
, FPNumDecode
12 from gmpy2
import mpfr
17 class CordicState(Enum
):
23 class CordicROM(Elaboratable
):
24 def __init__(self
, fracbits
, iterations
):
25 self
.fracbits
= fracbits
26 self
.iterations
= iterations
29 self
.addr
= Signal(range(iterations
))
30 self
.data
= Signal(range(-M
, M
-1))
33 gmpy2
.get_context().precision
= 150
34 for i
in range(self
.iterations
):
37 x
= x
/(gmpy2
.const_pi()/mpfr(2))
39 angles
.append(int(round(x
)))
41 self
.mem
= Memory(width
=self
.data
.width
,
42 depth
=self
.iterations
,
45 def elaborate(self
, platform
):
47 m
.submodules
.rdport
= rdport
= self
.mem
.read_port()
48 m
.d
.comb
+= rdport
.addr
.eq(self
.addr
)
49 m
.d
.comb
+= self
.data
.eq(rdport
.data
)
53 class CORDIC(Elaboratable
):
54 def __init__(self
, width
):
56 self
.z0
= Signal(width
, name
="z0")
57 self
.z_record
= FPNumBaseRecord(self
.z0
.width
, False, name
="z_record")
58 self
.fracbits
= 2 * self
.z_record
.m_width
59 self
.M
= M
= (1 << self
.fracbits
)
60 self
.ZMAX
= int(round(self
.M
* math
.pi
/2))
61 self
.z_out
= Signal(range(-self
.ZMAX
, self
.ZMAX
-1))
63 # sin/cos output in 0.ffffff format
64 self
.cos
= Signal(range(-M
, M
+1), reset
=0)
65 self
.sin
= Signal(range(-M
, M
+1), reset
=0)
69 self
.start
= Signal(reset_less
=True)
70 # cordic done/ready for input
71 self
.ready
= Signal(reset
=True)
73 self
.width
= self
.z0
.width
74 self
.iterations
= self
.fracbits
- 1
76 def elaborate(self
, platform
):
81 m
.submodules
.z_in
= z_in
= FPNumDecode(None, self
.z_record
)
82 comb
+= z_in
.v
.eq(self
.z0
)
84 z_fixed
= Signal(range(-self
.ZMAX
, self
.ZMAX
-1),
87 # Calculate initial amplitude?
89 for i
in range(self
.iterations
):
90 An
*= math
.sqrt(1 + 2**(-2*i
))
92 X0
= int(round(self
.M
*1/An
))
93 x
= Signal(self
.sin
.shape())
94 y
= Signal(self
.sin
.shape())
95 z
= Signal(z_fixed
.shape())
96 dx
= Signal(self
.sin
.shape())
97 dy
= Signal(self
.sin
.shape())
98 dz
= Signal(z_fixed
.shape())
99 i
= Signal(range(self
.iterations
))
100 state
= Signal(CordicState
, reset
=CordicState
.WAITING
)
102 m
.submodules
.anglerom
= anglerom
= \
103 CordicROM(self
.fracbits
, self
.iterations
)
105 comb
+= dx
.eq(y
>> i
)
106 comb
+= dy
.eq(x
>> i
)
107 comb
+= dz
.eq(anglerom
.data
)
108 comb
+= self
.cos
.eq(x
)
109 comb
+= self
.sin
.eq(y
)
110 with m
.If(state
== CordicState
.WAITING
):
111 with m
.If(self
.start
):
112 z_intermed
= Signal(z_fixed
.shape())
113 shifter
= Signal(z_in
.e
.width
)
114 comb
+= shifter
.eq(-z_in
.e
)
115 # This converts z_in.m to a large fixed point
116 # integer. Right now, I'm ignoring denormals but they
117 # will be added back in when I convert this to the
118 # pipelined implementation (and I can use FPAddDenormMod)
119 comb
+= z_intermed
.eq(Cat(Repl(0, self
.fracbits
- z_in
.rmw
),
121 sync
+= z_fixed
.eq(z_intermed
>> shifter
)
122 sync
+= state
.eq(CordicState
.INIT
)
123 sync
+= self
.ready
.eq(0)
124 with m
.If(state
== CordicState
.INIT
):
125 z_temp
= Signal(z
.shape(), reset_less
=True)
126 comb
+= z_temp
.eq(Mux(z_in
.s
, ~z_fixed
+ 1, z_fixed
))
128 sync
+= self
.z_out
.eq(z_temp
)
132 sync
+= state
.eq(CordicState
.RUNNING
)
133 sync
+= anglerom
.addr
.eq(1)
134 with m
.If(state
== CordicState
.RUNNING
):
143 with m
.If(i
== self
.iterations
- 1):
144 sync
+= state
.eq(CordicState
.WAITING
)
145 sync
+= self
.ready
.eq(1)
146 sync
+= anglerom
.addr
.eq(0)
149 sync
+= anglerom
.addr
.eq(i
+2)
153 lst
= [self
.cos
, self
.sin
,
154 self
.ready
, self
.start
]
159 if __name__
== '__main__':
161 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
162 with
open("cordic.il", "w") as f
: