OpenCores
URL https://opencores.org/ocsvn/cordic/cordic/trunk

Subversion Repositories cordic

[/] [cordic/] [trunk/] [rect2polar/] [r2p_post.vhd] - Blame information for rev 13

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 rherveille
--
2
--      post.vhd
3
--
4
--      Cordic post-processing block
5
--
6
-- Compensate cordic algorithm K-factor; divide Radius by 1.6467, or multiply by 0.60725. 
7
-- Approximation:  Ra = Ri/2 + Ri/8 - Ri/64 - Ri/512
8
--                 Radius = Ra - Ra/4096 = Ri * 0.60727. This is a 0.0034% error.
9
-- Implementation: Ra = (Ri/2 + Ri/8) - (Ri/64 + Ri/512)
10
--                 Radius = Ra - Ra/4096
11
--      Position calculated angle in correct quadrant.
12
--
13
 
14
library ieee;
15
use ieee.std_logic_1164.all;
16
use ieee.std_logic_arith.all;
17
 
18
entity r2p_post is
19
        port(
20
                clk     : in std_logic;
21
                ena     : in std_logic;
22
 
23
                Ai      : in signed(19 downto 0);
24
                Ri      : in unsigned(19 downto 0);
25
                Q       : in std_logic_vector(2 downto 0);
26
 
27
                Ao      : out signed(19 downto 0);
28
                Ro      : out unsigned(19 downto 0));
29
end entity r2p_post;
30
 
31
architecture dataflow of r2p_post is
32
begin
33
        radius: block
34
                signal RadA, RadB, RadC : unsigned(19 downto 0);
35
        begin
36
                process(clk)
37
                begin
38
                        if (clk'event and clk = '1') then
39
                                if (ena = '1') then
40
                                        RadA <= ('0' & Ri(19 downto 1)) + ("000" & Ri(19 downto 3));
41
                                        RadB <= ("000000" & Ri(19 downto 6)) + ("000000000" & Ri(19 downto 9));
42
                                        RadC <= RadA - RadB;
43
 
44
                                        Ro <= RadC - RadC(19 downto 12);
45
                                end if;
46
                        end if;
47
                end process;
48
        end block radius;
49
 
50
        angle: block
51
                constant const_PI2 : signed(19 downto 0) := conv_signed(16#40000#, 20); -- PI / 2
52
                constant const_PI : signed(19 downto 0) := conv_signed(16#80000#, 20);  -- PI
53
                constant const_2PI : signed(19 downto 0) := (others => '0');            -- 2PI
54
 
55
                signal dQ : std_logic_vector(2 downto 1);
56
                signal ddQ : std_logic;
57
                signal AngStep1 : signed(19 downto 0);
58
                signal AngStep2 : signed(19 downto 0);
59
        begin
60
                angle_step1: process(clk, Ai, Q)
61
                        variable overflow : std_logic;
62
                        variable AngA, AngB, Ang : signed(19 downto 0);
63
                begin
64
                        -- check if angle is negative, if so set it to zero
65
                        overflow := Ai(19); --and Ai(18);
66
 
67
                        if (overflow = '1') then
68
                                AngA := (others => '0');
69
                        else
70
                                AngA := Ai;
71
                        end if;
72
 
73
                        -- step 1: Xabs and Yabs are swapped
74
                        -- Calculated angle is the angle between vector and Y-axis.
75
                        -- ActualAngle = PI/2 - CalculatedAngle
76
                        AngB := const_PI2 - AngA;
77
 
78
                        if (Q(0) = '1') then
79
                                Ang := AngB;
80
                        else
81
                                Ang := AngA;
82
                        end if;
83
 
84
                        if (clk'event and clk = '1') then
85
                                if (ena = '1') then
86
                                        AngStep1 <= Ang;
87
                                        dQ <= q(2 downto 1);
88
                                end if;
89
                        end if;
90
                end process angle_step1;
91
 
92
 
93
                angle_step2: process(clk, AngStep1, dQ)
94
                        variable AngA, AngB, Ang : signed(19 downto 0);
95
                begin
96
                        AngA := AngStep1;
97
 
98
                        -- step 2: Xvalue is negative
99
                        -- Actual angle is in the second or third quadrant
100
                        -- ActualAngle = PI - CalculatedAngle
101
                        AngB := const_PI - AngA;
102
 
103
                        if (dQ(1) = '1') then
104
                                Ang := AngB;
105
                        else
106
                                Ang := AngA;
107
                        end if;
108
 
109
                        if (clk'event and clk = '1') then
110
                                if (ena = '1') then
111
                                        AngStep2 <= Ang;
112
                                        ddQ <= dQ(2);
113
                                end if;
114
                        end if;
115
                end process angle_step2;
116
 
117
 
118
                angle_step3: process(clk, AngStep2, ddQ)
119
                        variable AngA, AngB, Ang : signed(19 downto 0);
120
                begin
121
                        AngA := AngStep2;
122
 
123
                        -- step 3: Yvalue is negative
124
                        -- Actual angle is in the third or fourth quadrant
125
                        -- ActualAngle = 2PI - CalculatedAngle
126
                        AngB := const_2PI - AngA;
127
 
128
                        if (ddQ = '1') then
129
                                Ang := AngB;
130
                        else
131
                                Ang := AngA;
132
                        end if;
133
 
134
                        if (clk'event and clk = '1') then
135
                                if (ena = '1') then
136
                                        Ao <= Ang;
137
                                end if;
138
                        end if;
139
                end process angle_step3;
140
        end block angle;
141
end;
142
 
143
 
144
 
145
 
146
 
147
 
148
 
149
 
150
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.