-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcordic_mod.v
172 lines (145 loc) · 3.57 KB
/
cordic_mod.v
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
module cordic_mod(
input clk, rst,
input signed [15:0] inp_x, inp_y,
output reg signed [15:0] arctan,
output reg valid
);
localparam [2:0] stStart = 0, stWait = 1, stRotate = 2, stFinal = 3;
reg [2:0] state = stStart;
reg signed [16:0] x, y;
wire signed [16:0] xp, yp;
wire is_x_negative = inp_x[15];
wire is_y_negative = inp_y[15];
wire is_x_zero = ~|inp_x;
wire is_y_zero = ~|inp_y;
reg enable;
wire [2:0] count_val;
counter cntr(clk, rst, enable, count_val);
reg signed [15:0] initial_angle;
wire signed [15:0] updated_angle;
wire is_done;
rotator rttr(enable, x, y, initial_angle, count_val, xp, yp, updated_angle, is_done);
always @(posedge clk) begin
if (rst) begin
initial_angle <= 0;
x <= 0;
y <= 0;
state <= stStart;
end
else begin
case (state)
stStart : begin
if (is_x_zero | is_y_zero) begin
state <= stFinal;
end
else begin
x <= (is_x_negative) ? -inp_x : inp_x;
y <= (is_y_negative) ? -inp_y : inp_y;
state <= stWait;
end
end
stWait : begin
state <= stRotate;
end
stRotate : begin
if (is_done) begin
state <= stFinal;
end
else begin
x <= xp;
y <= yp;
initial_angle <= updated_angle;
state <= stRotate;
end
end
stFinal : begin
state <= stStart;
end
endcase
end
end
always @(posedge clk) begin
if (rst) begin
valid = 0;
enable = 0;
end
else begin
case (state)
stStart : begin
end
stWait : begin
enable = 1;
end
stRotate : begin
if (is_done) begin
enable = 0;
end
end
stFinal : begin
if (is_x_zero) begin
arctan = {is_y_negative, {15{~is_y_negative}}};
end
else if (is_y_zero) begin
arctan = 0;
end
else begin
arctan = updated_angle;
case ({is_x_negative, is_y_negative})
2'b00 : arctan = updated_angle;
2'b01 : arctan = -updated_angle;
2'b10 : arctan = 16'h7fff - updated_angle;
2'b11 : arctan = updated_angle - 16'h7fff;
endcase
end
valid = 1;
end
endcase
end
end
endmodule
// synthesis translate_off
`timescale 1 ps / 1 ps
module tb_cordic_mod;
reg [15:0] x, y;
reg clk, rst;
wire [15:0] arctan;
wire valid;
cordic_mod dut(clk, rst, x, y, arctan, valid);
always #5 clk = ~clk;
initial begin
clk = 0;
rst = 0;
x = 0;
y = 0;
#6 rst = 1; x = 16'h6666; y = 16'h4ccd;
@(posedge clk);
@(posedge clk); #1 rst = 0;
@(posedge valid); #2 rst = 1; x = 16'h0000; y = 16'h9214;
@(posedge clk);
@(posedge clk); #1 rst = 0;
@(posedge valid); #2 rst = 1; x = 16'h0000; y = 16'h5214;
@(posedge clk);
@(posedge clk); #1 rst = 0;
@(posedge valid); #2 rst = 1; x = 16'h9214; y = 16'h0000;
@(posedge clk);
@(posedge clk); #1 rst = 0;
@(posedge valid); #2 rst = 1; x = 16'h5214; y = 16'h0000;
@(posedge clk);
@(posedge clk); #1 rst = 0;
@(posedge valid); #2 rst = 1; x = 16'h5214; y = 16'h3842;
@(posedge clk);
@(posedge clk); #1 rst = 0;
@(posedge valid); #2 rst = 1; x = 16'h5214; y = 16'hA200;
@(posedge clk);
@(posedge clk); #1 rst = 0;
@(posedge valid); #2 rst = 1; x = 16'hB268; y = 16'h7370;
@(posedge clk);
@(posedge clk); #1 rst = 0;
@(posedge valid); #2 rst = 1; x = 16'hB268; y = 16'hD073;
@(posedge clk);
@(posedge clk); #1 rst = 0;
@(posedge valid); #2 rst = 1;
#10 $finish;
end
endmodule
// synthesis translate_on