Run the cordic for the proper number of iterations
[ieee754fpu.git] / src / ieee754 / cordic / fpsin_cos.py
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
3
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
7 import math
8 from enum import Enum, unique
9 from ieee754.fpcommon.fpbase import FPNumBaseRecord, FPNumDecode
10
11
12 @unique
13 class CordicState(Enum):
14 WAITING = 0
15 INIT = 1
16 RUNNING = 2
17
18
19 class CordicROM(Elaboratable):
20 def __init__(self, fracbits, iterations):
21 self.fracbits = fracbits
22 self.iterations = iterations
23
24 M = 1 << fracbits
25 self.addr = Signal(range(iterations))
26 self.data = Signal(range(-M, M-1))
27
28 angles = [int(round(M*math.atan(2**(-i))/(math.pi/2)))
29 for i in range(self.iterations)]
30
31 self.mem = Memory(width=self.data.width,
32 depth=self.iterations,
33 init=angles)
34
35 def elaborate(self, platform):
36 m = Module()
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)
40 return m
41
42
43 class CORDIC(Elaboratable):
44 def __init__(self, width):
45
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))
52
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)
56 # angle input
57
58 # cordic start flag
59 self.start = Signal(reset_less=True)
60 # cordic done/ready for input
61 self.ready = Signal(reset=True)
62
63 self.width = self.z0.width
64 self.iterations = self.fracbits - 1
65
66 def elaborate(self, platform):
67 m = Module()
68 comb = m.d.comb
69 sync = m.d.sync
70
71 m.submodules.z_in = z_in = FPNumDecode(None, self.z_record)
72 comb += z_in.v.eq(self.z0)
73
74 z_fixed = Signal(range(-self.ZMAX, self.ZMAX-1),
75 reset_less=True)
76
77 # Calculate initial amplitude?
78 An = 1.0
79 for i in range(self.iterations):
80 An *= math.sqrt(1 + 2**(-2*i))
81
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)
91
92 m.submodules.anglerom = anglerom = \
93 CordicROM(self.fracbits, self.iterations)
94
95 comb += dx.eq(y >> i)
96 comb += dy.eq(x >> i)
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),
110 z_in.m[:-1], 1))
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))
117 sync += z.eq(z_temp)
118 sync += self.z_out.eq(z_temp)
119 sync += x.eq(X0)
120 sync += y.eq(0)
121 sync += i.eq(0)
122 sync += state.eq(CordicState.RUNNING)
123 sync += anglerom.addr.eq(1)
124 with m.If(state == CordicState.RUNNING):
125 with m.If(z >= 0):
126 sync += x.eq(x - dx)
127 sync += y.eq(y + dy)
128 sync += z.eq(z - dz)
129 with m.Else():
130 sync += x.eq(x + dx)
131 sync += y.eq(y - dy)
132 sync += z.eq(z + dz)
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)
137 with m.Else():
138 sync += i.eq(i+1)
139 sync += anglerom.addr.eq(i+2)
140 return m
141
142 def ports(self):
143 lst = [self.cos, self.sin,
144 self.ready, self.start]
145 lst.extend(self.z0)
146 return lst
147
148
149 if __name__ == '__main__':
150 dut = CORDIC(8)
151 vl = rtlil.convert(dut, ports=dut.ports())
152 with open("cordic.il", "w") as f:
153 f.write(vl)