-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcordic.py
More file actions
149 lines (128 loc) · 5.2 KB
/
Copy pathcordic.py
File metadata and controls
149 lines (128 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from argparse import ArgumentParser
from pathlib import Path
import numpy as np
from amaranth import Module, Signal, signed
from amaranth.back import verilog
from amaranth.lib import wiring
from amaranth.lib.memory import Memory
from amaranth.lib.wiring import In, Out
from amaranth.sim import Simulator, Tick
def cordic(theta: np.ndarray, resolution: int, iterations: int):
if resolution > 64:
raise ValueError('Resolution must be less than 64')
tans = np.power(2, -np.arange(iterations, dtype=np.float64))
phis = np.arctan(tans)
k = np.prod(np.cos(phis))
mul = 2**(resolution - 2)
tan_phis_fixed = (phis / np.pi * mul).astype(np.int64)
theta_fixed = (theta / np.pi * mul).astype(np.int64)
x = np.ones_like(theta_fixed) * int(mul * k)
y = np.zeros_like(theta_fixed)
t = np.zeros_like(theta_fixed)
for i in range(iterations):
sign = (theta_fixed > t) * 2 - 1
xn = x - (y >> i) * sign
yn = y + (x >> i) * sign
x, y = xn, yn
t = t + tan_phis_fixed[i] * sign
return x / mul, y / mul
class CORDIC_SINE(wiring.Component):
def __init__(self, resolution=32, iterations=20):
self.resolution = resolution
self.mul = 2**(self.resolution - 2)
self._iterations = iterations
super().__init__(
{
'enable': In(1),
'valid': Out(1),
'theta': In(signed(resolution)),
'sine': Out(signed(resolution)),
'cosine': Out(signed(resolution)),
}
)
def elaborate(self, platform):
tans = np.power(2, -np.arange(self._iterations, dtype=np.float64))
# radians
phis = np.arctan(tans)
k = np.prod(np.cos(phis))
tan_phis_fixed = (phis / np.pi * self.mul).astype(np.int64)
m = Module()
m.submodules.tan_phi = tan_phi_mem = Memory(
shape=signed(self.resolution),
depth=self._iterations,
init=tan_phis_fixed,
)
x = Signal(signed(self.resolution))
y = Signal(signed(self.resolution))
phi = Signal(signed(self.resolution))
iteration = Signal(range(self._iterations + 1))
tan_phi_i = Signal(signed(self.resolution))
read_port = tan_phi_mem.read_port(domain='comb')
m.d.comb += [read_port.addr.eq(iteration), tan_phi_i.eq(read_port.data)]
with m.FSM() as fsm:
with m.State('idle'):
with m.If(self.enable):
m.d.sync += x.eq(int(self.mul * k))
m.d.sync += y.eq(0)
m.d.sync += phi.eq(0)
m.d.sync += iteration.eq(0)
m.d.sync += self.valid.eq(0)
m.next = 'calculate'
with m.State('calculate'):
with m.If(iteration < self._iterations):
m.d.sync += iteration.eq(iteration + 1)
with m.Else():
m.d.sync += self.sine.eq(y)
m.d.sync += self.cosine.eq(x)
m.d.sync += self.valid.eq(1)
m.next = 'idle'
with m.If(phi < self.theta):
m.d.sync += x.eq(x - (y >> iteration))
m.d.sync += y.eq(y + (x >> iteration))
m.d.sync += phi.eq(phi + tan_phi_i)
with m.Else():
m.d.sync += x.eq(x + (y >> iteration))
m.d.sync += y.eq(y - (x >> iteration))
m.d.sync += phi.eq(phi - tan_phi_i)
return m
def get_args():
parser = ArgumentParser()
parser.add_argument('--resolution', type=int, default=32)
parser.add_argument('--iterations', type=int, default=20)
parser.add_argument('--sim', action='store_true')
parser.add_argument('--verilog', action='store_true')
parser.add_argument('--output', type=Path, default=Path('build/cordic.v'))
return parser.parse_args()
args = get_args()
args.resolution = args.resolution
args.iterations = args.iterations
dut = CORDIC_SINE(resolution=args.resolution, iterations=args.iterations)
if args.sim:
sim = Simulator(dut)
sim.add_clock(1e-6)
def bench():
for i in range(3):
yield Tick()
test_thetas = np.linspace(-np.pi / 2, np.pi / 2, 100)
answers_cos, answers_sin = cordic(
test_thetas,
resolution=args.resolution,
iterations=args.iterations,
)
for theta, ans_x, ans_y in zip(test_thetas, answers_cos, answers_sin):
yield dut.theta.eq(int(theta / np.pi * dut.mul))
yield dut.enable.eq(1)
yield Tick()
while not (yield dut.valid):
yield Tick()
yield dut.enable.eq(0)
# print(f'θ{theta:6.3f}, sin(θ)={ans:6.3f}, cordic={((yield dut.sine) / dut.mul):6.3f}')
assert ((yield dut.sine) / dut.mul) == ans_y
assert ((yield dut.cosine) / dut.mul) == ans_x
sim.add_testbench(bench)
with sim.write_vcd("cordic.vcd"):
sim.run()
if args.verilog:
if not args.output.parent.exists():
args.output.parent.mkdir(parents=True)
args.output.write_text(verilog.convert(dut, name='cordic', strip_internal_attrs=True))