c61e6bf2648bfe530855e54fb8ae110e4eb7fb72
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
13 class CordicState(Enum
):
19 class CordicROM(Elaboratable
):
20 def __init__(self
, fracbits
, iterations
):
21 self
.fracbits
= fracbits
22 self
.iterations
= iterations
25 self
.addr
= Signal(range(iterations
))
26 self
.data
= Signal(range(-M
, M
-1))
28 angles
= [int(round(M
*math
.atan(2**(-i
))/(math
.pi
/2)))
29 for i
in range(self
.iterations
)]
31 self
.mem
= Memory(width
=self
.data
.width
,
32 depth
=self
.iterations
,
35 def elaborate(self
, platform
):
37 m
.submodules
.rdport
= rdport
= self
.mem
.read_port()
38 m
.d
.comb
+= rdport
.addr
.eq(self
.addr
)
39 m
.d
.comb
+= self
.data
.eq(rdport
.data
)
43 class CORDIC(Elaboratable
):
44 def __init__(self
, width
):
46 self
.z0
= Signal(width
, name
="z0")
47 self
.z_record
= FPNumBaseRecord(self
.z0
.width
, False, name
="z_record")
48 self
.fracbits
= 2 * self
.z_record
.m_width
49 self
.M
= M
= (1 << self
.fracbits
)
50 self
.ZMAX
= int(round(self
.M
* math
.pi
/2))
51 self
.z_out
= Signal(range(-self
.ZMAX
, self
.ZMAX
-1))
53 # sin/cos output in 0.ffffff format
54 self
.cos
= Signal(range(-M
, M
+1), reset
=0)
55 self
.sin
= Signal(range(-M
, M
+1), reset
=0)
59 self
.start
= Signal(reset_less
=True)
60 # cordic done/ready for input
61 self
.ready
= Signal(reset
=True)
63 self
.width
= self
.z0
.width
64 self
.iterations
= self
.fracbits
- 1
66 def elaborate(self
, platform
):
71 m
.submodules
.z_in
= z_in
= FPNumDecode(None, self
.z_record
)
72 comb
+= z_in
.v
.eq(self
.z0
)
74 z_fixed
= Signal(range(-self
.ZMAX
, self
.ZMAX
-1),
77 # Calculate initial amplitude?
79 for i
in range(self
.iterations
):
80 An
*= math
.sqrt(1 + 2**(-2*i
))
82 X0
= int(round(self
.M
*1/An
))
83 x
= Signal(self
.sin
.shape())
84 y
= Signal(self
.sin
.shape())
85 z
= Signal(z_fixed
.shape())
86 dx
= Signal(self
.sin
.shape())
87 dy
= Signal(self
.sin
.shape())
88 dz
= Signal(z_fixed
.shape())
89 i
= Signal(range(self
.iterations
))
90 state
= Signal(CordicState
, reset
=CordicState
.WAITING
)
92 m
.submodules
.anglerom
= anglerom
= \
93 CordicROM(self
.fracbits
, self
.iterations
)
97 comb
+= dz
.eq(anglerom
.data
)
98 comb
+= self
.cos
.eq(x
)
99 comb
+= self
.sin
.eq(y
)
100 with m
.If(state
== CordicState
.WAITING
):
101 with m
.If(self
.start
):
102 z_intermed
= Signal(z_fixed
.shape())
103 shifter
= Signal(z_in
.e
.width
)
104 comb
+= shifter
.eq(-z_in
.e
)
105 # This converts z_in.m to a large fixed point
106 # integer. Right now, I'm ignoring denormals but they
107 # will be added back in when I convert this to the
108 # pipelined implementation (and I can use FPAddDenormMod)
109 comb
+= z_intermed
.eq(Cat(Repl(0, self
.fracbits
- z_in
.rmw
),
111 sync
+= z_fixed
.eq(z_intermed
>> shifter
)
112 sync
+= state
.eq(CordicState
.INIT
)
113 sync
+= self
.ready
.eq(0)
114 with m
.If(state
== CordicState
.INIT
):
115 z_temp
= Signal(z
.shape(), reset_less
=True)
116 comb
+= z_temp
.eq(Mux(z_in
.s
, ~z_fixed
+ 1, z_fixed
))
118 sync
+= self
.z_out
.eq(z_temp
)
122 sync
+= state
.eq(CordicState
.RUNNING
)
123 sync
+= anglerom
.addr
.eq(1)
124 with m
.If(state
== CordicState
.RUNNING
):
133 with m
.If(i
== self
.iterations
- 1):
134 sync
+= state
.eq(CordicState
.WAITING
)
135 sync
+= self
.ready
.eq(1)
136 sync
+= anglerom
.addr
.eq(0)
139 sync
+= anglerom
.addr
.eq(i
+2)
143 lst
= [self
.cos
, self
.sin
,
144 self
.ready
, self
.start
]
149 if __name__
== '__main__':
151 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
152 with
open("cordic.il", "w") as f
: