Debian Codegraph @

hdl/fpga/ip/nuand/synthesis/cordic.vhd

raw
1 -- Copyright (c) 2013 Nuand LLC
2 --
3 -- Permission is hereby granted, free of charge, to any person obtaining a copy
4 -- of this software and associated documentation files (the "Software"), to deal
5 -- in the Software without restriction, including without limitation the rights
6 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 -- copies of the Software, and to permit persons to whom the Software is
8 -- furnished to do so, subject to the following conditions:
9 --
10 -- The above copyright notice and this permission notice shall be included in
11 -- all copies or substantial portions of the Software.
12 --
13 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 -- THE SOFTWARE.
20
21 library ieee ;
22 use ieee.std_logic_1164.all ;
23 use ieee.numeric_std.all ;
24
25 package cordic_p is
26
27 -- Vectoring mode forces outputs.y to 0
28 -- Rotation mode forces outputs.z to 0
29 type cordic_mode_t is (CORDIC_ROTATION, CORDIC_VECTORING) ;
30
31 -- TODO: Make this a generic package
32 type cordic_xyz_t is record
33 x : signed(15 downto 0) ;
34 y : signed(15 downto 0) ;
35 z : signed(15 downto 0) ;
36 valid : std_logic ;
37 end record ;
38
39 end package ; -- cordic_p
40
41 library ieee ;
42 use ieee.std_logic_1164.all ;
43 use ieee.numeric_std.all ;
44 use ieee.math_real.all ;
45
46 library work ;
47 use work.cordic_p.all ;
48
49 entity cordic is
50 port (
51 clock : in std_logic ;
52 reset : in std_logic ;
53 mode : in cordic_mode_t ;
54 inputs : in cordic_xyz_t ;
55 outputs : out cordic_xyz_t
56 ) ;
57 end entity ; -- cordic
58
59 architecture arch of cordic is
60
61 -- TODO: Make this generic
62 constant NUM_STAGES : natural := 12 ;
63
64 -- TODO: Use the VHDL-2008 integer_vector
65 type integer_array_t is array(natural range <>) of integer ;
66
67 -- Each stage of the CORDIC is atan(2^(-i))
68 function calculate_cordic_table return integer_array_t is
69 variable rv : integer_array_t(0 to NUM_STAGES-1) := (others => 0) ;
70 begin
71 for i in 0 to rv'high loop
72 rv(i) := integer(round((2.0**NUM_STAGES)*arctan(2.0**(-i))/MATH_PI)) ;
73 end loop ;
74 return rv ;
75 end function ;
76
77 -- Constant array
78 constant K : integer_array_t := calculate_cordic_table ;
79
80 -- The array for CORDIC stages
81 type xyzs_t is array(0 to NUM_STAGES-1) of cordic_xyz_t ;
82
83 -- ALl the XYZ's for the CORDIC stages
84 signal xyzs : xyzs_t ;
85
86 begin
87
88 rotate : process(clock, reset)
89 begin
90 if( reset = '1' ) then
91 xyzs <= (others =>(x => (others =>'0'), y => (others =>'0'), z => (others =>'0'), valid => '0')) ;
92 elsif( rising_edge( clock ) ) then
93 -- First stage will rotate the vector to be within -pi/2 to pi/2
94 xyzs(0).valid <= inputs.valid ;
95 if( inputs.valid = '1' ) then
96 case mode is
97 when CORDIC_ROTATION =>
98 -- Make sure we're only rotating -pi/2 to pi/2 and adjust accordingly
99 if( inputs.z > 2**(NUM_STAGES-1) ) then
100 xyzs(0).x <= -inputs.x ;
101 xyzs(0).y <= -inputs.y ;
102 xyzs(0).z <= inputs.z - 2**NUM_STAGES ;
103 elsif( inputs.z < -(2**(NUM_STAGES-1)) ) then
104 xyzs(0).x <= -inputs.x ;
105 xyzs(0).y <= -inputs.y ;
106 xyzs(0).z <= inputs.z + 2**NUM_STAGES ;
107 else
108 xyzs(0).x <= inputs.x ;
109 xyzs(0).y <= inputs.y ;
110 xyzs(0).z <= inputs.z ;
111 end if ;
112 when CORDIC_VECTORING =>
113 -- Make sure we're in the 1st or 4th quadrant only
114 if( inputs.x < 0 and inputs.y < 0 ) then
115 xyzs(0).x <= -inputs.x ;
116 xyzs(0).y <= -inputs.y ;
117 xyzs(0).z <= inputs.z - 2**(NUM_STAGES) ;
118 elsif( inputs.x < 0 ) then
119 xyzs(0).x <= -inputs.x ;
120 xyzs(0).y <= -inputs.y ;
121 xyzs(0).z <= inputs.z + 2**(NUM_STAGES) ;
122 else
123 xyzs(0).x <= inputs.x ;
124 xyzs(0).y <= inputs.y ;
125 xyzs(0).z <= inputs.z ;
126 end if ;
127 end case ;
128
129 end if ;
130
131 -- Run through all the other stages
132 for i in 0 to xyzs'high-1 loop
133 xyzs(i+1).valid <= xyzs(i).valid ;
134 if( xyzs(i).valid = '1' ) then
135 case mode is
136 when CORDIC_ROTATION =>
137 if( xyzs(i).z < 0 ) then
138 xyzs(i+1).x <= xyzs(i).x + shift_right(xyzs(i).y, i) ;
139 xyzs(i+1).y <= xyzs(i).y - shift_right(xyzs(i).x, i) ;
140 xyzs(i+1).z <= xyzs(i).z + K(i) ;
141 else
142 xyzs(i+1).x <= xyzs(i).x - shift_right(xyzs(i).y, i) ;
143 xyzs(i+1).y <= xyzs(i).y + shift_right(xyzs(i).x, i) ;
144 xyzs(i+1).z <= xyzs(i).z - K(i) ;
145 end if ;
146 when CORDIC_VECTORING =>
147 if( xyzs(i).y < 0 ) then
148 xyzs(i+1).x <= xyzs(i).x - shift_right(xyzs(i).y, i) ;
149 xyzs(i+1).y <= xyzs(i).y + shift_right(xyzs(i).x, i) ;
150 xyzs(i+1).z <= xyzs(i).z - K(i) ;
151 else
152 xyzs(i+1).x <= xyzs(i).x + shift_right(xyzs(i).y, i) ;
153 xyzs(i+1).y <= xyzs(i).y - shift_right(xyzs(i).x, i) ;
154 xyzs(i+1).z <= xyzs(i).z + K(i) ;
155 end if ;
156 end case ;
157 end if ;
158 end loop ;
159
160 -- Output stage
161 outputs <= xyzs(xyzs'high) ;
162 end if ;
163 end process ;
164
165 end architecture ; -- arch