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
11 from bigfloat
import BigFloat
15 class CordicState(Enum
):
21 class CordicROM(Elaboratable
):
22 def __init__(self
, fracbits
, iterations
):
23 self
.fracbits
= fracbits
24 self
.iterations
= iterations
27 self
.addr
= Signal(range(iterations
))
28 self
.data
= Signal(range(-M
, M
-1))
31 with bf
.quadruple_precision
:
32 for i
in range(self
.iterations
):
33 x
= bf
.atan(BigFloat(2) ** BigFloat(-i
))
34 x
= x
/(bf
.const_pi()/2)
36 angles
.append(int(round(x
)))
38 self
.mem
= Memory(width
=self
.data
.width
,
39 depth
=self
.iterations
,
42 def elaborate(self
, platform
):
44 m
.submodules
.rdport
= rdport
= self
.mem
.read_port()
45 m
.d
.comb
+= rdport
.addr
.eq(self
.addr
)
46 m
.d
.comb
+= self
.data
.eq(rdport
.data
)
50 class CORDIC(Elaboratable
):
51 def __init__(self
, width
):
53 self
.z0
= Signal(width
, name
="z0")
54 self
.z_record
= FPNumBaseRecord(self
.z0
.width
, False, name
="z_record")
55 self
.fracbits
= 2 * self
.z_record
.m_width
56 self
.M
= M
= (1 << self
.fracbits
)
57 self
.ZMAX
= int(round(self
.M
* math
.pi
/2))
58 self
.z_out
= Signal(range(-self
.ZMAX
, self
.ZMAX
-1))
60 # sin/cos output in 0.ffffff format
61 self
.cos
= Signal(range(-M
, M
+1), reset
=0)
62 self
.sin
= Signal(range(-M
, M
+1), reset
=0)
66 self
.start
= Signal(reset_less
=True)
67 # cordic done/ready for input
68 self
.ready
= Signal(reset
=True)
70 self
.width
= self
.z0
.width
71 self
.iterations
= self
.fracbits
- 1
73 def elaborate(self
, platform
):
78 m
.submodules
.z_in
= z_in
= FPNumDecode(None, self
.z_record
)
79 comb
+= z_in
.v
.eq(self
.z0
)
81 z_fixed
= Signal(range(-self
.ZMAX
, self
.ZMAX
-1),
84 # Calculate initial amplitude?
86 for i
in range(self
.iterations
):
87 An
*= math
.sqrt(1 + 2**(-2*i
))
89 X0
= int(round(self
.M
*1/An
))
90 x
= Signal(self
.sin
.shape())
91 y
= Signal(self
.sin
.shape())
92 z
= Signal(z_fixed
.shape())
93 dx
= Signal(self
.sin
.shape())
94 dy
= Signal(self
.sin
.shape())
95 dz
= Signal(z_fixed
.shape())
96 i
= Signal(range(self
.iterations
))
97 state
= Signal(CordicState
, reset
=CordicState
.WAITING
)
99 m
.submodules
.anglerom
= anglerom
= \
100 CordicROM(self
.fracbits
, self
.iterations
)
102 comb
+= dx
.eq(y
>> i
)
103 comb
+= dy
.eq(x
>> i
)
104 comb
+= dz
.eq(anglerom
.data
)
105 comb
+= self
.cos
.eq(x
)
106 comb
+= self
.sin
.eq(y
)
107 with m
.If(state
== CordicState
.WAITING
):
108 with m
.If(self
.start
):
109 z_intermed
= Signal(z_fixed
.shape())
110 shifter
= Signal(z_in
.e
.width
)
111 comb
+= shifter
.eq(-z_in
.e
)
112 # This converts z_in.m to a large fixed point
113 # integer. Right now, I'm ignoring denormals but they
114 # will be added back in when I convert this to the
115 # pipelined implementation (and I can use FPAddDenormMod)
116 comb
+= z_intermed
.eq(Cat(Repl(0, self
.fracbits
- z_in
.rmw
),
118 sync
+= z_fixed
.eq(z_intermed
>> shifter
)
119 sync
+= state
.eq(CordicState
.INIT
)
120 sync
+= self
.ready
.eq(0)
121 with m
.If(state
== CordicState
.INIT
):
122 z_temp
= Signal(z
.shape(), reset_less
=True)
123 comb
+= z_temp
.eq(Mux(z_in
.s
, ~z_fixed
+ 1, z_fixed
))
125 sync
+= self
.z_out
.eq(z_temp
)
129 sync
+= state
.eq(CordicState
.RUNNING
)
130 sync
+= anglerom
.addr
.eq(1)
131 with m
.If(state
== CordicState
.RUNNING
):
140 with m
.If(i
== self
.iterations
- 1):
141 sync
+= state
.eq(CordicState
.WAITING
)
142 sync
+= self
.ready
.eq(1)
143 sync
+= anglerom
.addr
.eq(0)
146 sync
+= anglerom
.addr
.eq(i
+2)
150 lst
= [self
.cos
, self
.sin
,
151 self
.ready
, self
.start
]
156 if __name__
== '__main__':
158 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
159 with
open("cordic.il", "w") as f
: