Use python bigfloat to calculate atan tables
[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 import bigfloat as bf
11 from bigfloat import BigFloat
12
13
14 @unique
15 class CordicState(Enum):
16 WAITING = 0
17 INIT = 1
18 RUNNING = 2
19
20
21 class CordicROM(Elaboratable):
22 def __init__(self, fracbits, iterations):
23 self.fracbits = fracbits
24 self.iterations = iterations
25
26 M = 1 << fracbits
27 self.addr = Signal(range(iterations))
28 self.data = Signal(range(-M, M-1))
29
30 angles = []
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)
35 x = x * M
36 angles.append(int(round(x)))
37
38 self.mem = Memory(width=self.data.width,
39 depth=self.iterations,
40 init=angles)
41
42 def elaborate(self, platform):
43 m = Module()
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)
47 return m
48
49
50 class CORDIC(Elaboratable):
51 def __init__(self, width):
52
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))
59
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)
63 # angle input
64
65 # cordic start flag
66 self.start = Signal(reset_less=True)
67 # cordic done/ready for input
68 self.ready = Signal(reset=True)
69
70 self.width = self.z0.width
71 self.iterations = self.fracbits - 1
72
73 def elaborate(self, platform):
74 m = Module()
75 comb = m.d.comb
76 sync = m.d.sync
77
78 m.submodules.z_in = z_in = FPNumDecode(None, self.z_record)
79 comb += z_in.v.eq(self.z0)
80
81 z_fixed = Signal(range(-self.ZMAX, self.ZMAX-1),
82 reset_less=True)
83
84 # Calculate initial amplitude?
85 An = 1.0
86 for i in range(self.iterations):
87 An *= math.sqrt(1 + 2**(-2*i))
88
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)
98
99 m.submodules.anglerom = anglerom = \
100 CordicROM(self.fracbits, self.iterations)
101
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),
117 z_in.m[:-1], 1))
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))
124 sync += z.eq(z_temp)
125 sync += self.z_out.eq(z_temp)
126 sync += x.eq(X0)
127 sync += y.eq(0)
128 sync += i.eq(0)
129 sync += state.eq(CordicState.RUNNING)
130 sync += anglerom.addr.eq(1)
131 with m.If(state == CordicState.RUNNING):
132 with m.If(z >= 0):
133 sync += x.eq(x - dx)
134 sync += y.eq(y + dy)
135 sync += z.eq(z - dz)
136 with m.Else():
137 sync += x.eq(x + dx)
138 sync += y.eq(y - dy)
139 sync += z.eq(z + dz)
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)
144 with m.Else():
145 sync += i.eq(i+1)
146 sync += anglerom.addr.eq(i+2)
147 return m
148
149 def ports(self):
150 lst = [self.cos, self.sin,
151 self.ready, self.start]
152 lst.extend(self.z0)
153 return lst
154
155
156 if __name__ == '__main__':
157 dut = CORDIC(8)
158 vl = rtlil.convert(dut, ports=dut.ports())
159 with open("cordic.il", "w") as f:
160 f.write(vl)