Debian Codegraph @

hdl/fpga/ip/nuand/vctcxo_tamer/vhdl/tb/mm_driver.vhd

raw
1 -- Copyright (c) 2015 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 library ieee;
21 use ieee.std_logic_1164.all;
22 use ieee.numeric_std.all;
23
24 library std;
25 use std.env.all;
26
27 entity mm_driver is
28 port(
29 mm_clock : in std_logic;
30 mm_reset : in std_logic;
31 mm_rd_req : out std_logic;
32 mm_wr_req : out std_logic;
33 mm_addr : out std_logic_vector(7 downto 0);
34 mm_wr_data : out std_logic_vector(7 downto 0);
35 mm_rd_data : in std_logic_vector(7 downto 0);
36 mm_rd_datav : in std_logic;
37 mm_wait_req : in std_logic;
38
39 mm_irq : in std_logic;
40
41 dac_count : out unsigned(15 downto 0);
42 tcxo_clock : out std_logic
43 );
44 end entity;
45
46 architecture arch of mm_driver is
47
48 function half_clk_per( freq : real ) return time is
49 begin
50 return ( (0.5 sec) / real(freq) );
51 end function;
52
53 constant TCXO_START_FREQ : real := 20.4e6;
54 constant TCXO_TARGET_FREQ : real := 38.4e6;
55 constant TCXO_INCR_FREQ : real := 1.0e6;
56
57 --constant PPS_1S_TARGET : signed(63 downto 0) := to_signed(7, 64);
58 --constant PPS_10S_TARGET : signed(63 downto 0) := to_signed(77, 64);
59 --constant PPS_100S_TARGET : signed(63 downto 0) := to_signed(768, 64);
60 --
61 --constant PPS_1S_MAX_ERROR : signed(63 downto 0) := to_signed(1, 64);
62 --constant PPS_10S_MAX_ERROR : signed(63 downto 0) := to_signed(5, 64);
63 --constant PPS_100S_MAX_ERROR : signed(63 downto 0) := to_signed(10, 64);
64
65 constant PPS_1S_TARGET : signed(63 downto 0) := x"0000_0000_0000_9600";--12_2C00";
66 constant PPS_10S_TARGET : signed(63 downto 0) := x"0000_0000_0005_DC00";--B_B800";
67 constant PPS_100S_TARGET : signed(63 downto 0) := x"0000_0000_003A_9800";--75_3000";
68
69 constant PPS_1S_MAX_ERROR : signed(63 downto 0) := to_signed(1, 64);
70 constant PPS_10S_MAX_ERROR : signed(63 downto 0) := to_signed(3, 64);
71 constant PPS_100S_MAX_ERROR : signed(63 downto 0) := to_signed(38, 64);
72
73 -- FSM States
74 type fsm_t is (
75 RESET_COUNTERS,
76 START_COUNTERS,
77 ENABLE_IRQS,
78 WAIT_FOR_IRQ,
79 READ_COUNTS,
80 FREQ_ADJUST,
81 HOLDOFF,
82 TERMINATE_SIMULATION
83 );
84
85 type error_t is record
86 error : signed(63 downto 0);
87 end record;
88
89
90 -- State of internal signals
91 type state_t is record
92 state : fsm_t;
93 holdoff_count : natural;
94 pps_1s_count : std_logic_vector(63 downto 0);
95 pps_10s_count : std_logic_vector(63 downto 0);
96 pps_100s_count : std_logic_vector(63 downto 0);
97 tcxo_half_per : time;
98 tcxo_freq : real;
99 pid_1s : error_t;
100 pid_10s : error_t;
101 pid_100s : error_t;
102 dac : unsigned(dac_count'range);
103 end record;
104
105 constant RESET_VALUE : state_t := (
106 state => RESET_COUNTERS,
107 holdoff_count => 0,
108 pps_1s_count => (others => '-'),
109 pps_10s_count => (others => '-'),
110 pps_100s_count => (others => '-'),
111 tcxo_half_per => half_clk_per( TCXO_START_FREQ ),
112 tcxo_freq => TCXO_START_FREQ,
113 pid_1s => ( error => (others => '0') ),
114 pid_10s => ( error => (others => '0') ),
115 pid_100s => ( error => (others => '0') ),
116 dac => x"7FFF"
117 );
118
119 constant RESET_TIME : natural := 50;
120 constant DEAD_TIME : natural := 1000;
121 constant IRQ_TIMEOUT : natural := 2**30; --40000;
122
123 constant PPS_CNT_1S_ADDR : natural := 0*8;
124 constant PPS_CNT_10S_ADDR : natural := 1*8;
125 constant PPS_CNT_100S_ADDR : natural := 2*8;
126 constant CONTROL_ADDR : natural := 4*8;
127 constant INTERRUPT_ADDR : natural := 5*8;
128
129 signal tcxo_clock_i : std_logic := '1';
130
131 signal current : state_t := RESET_VALUE;
132 signal future : state_t := RESET_VALUE;
133
134 begin
135
136 tcxo_clock_i <= not tcxo_clock_i after current.tcxo_half_per;
137 tcxo_clock <= tcxo_clock_i;
138
139 sync_proc : process( mm_clock, mm_reset )
140 begin
141 if( mm_reset = '1' ) then
142 current <= RESET_VALUE;
143 elsif( rising_edge(mm_clock) ) then
144 current <= future;
145 end if;
146 end process;
147
148 comb_proc : process( all )
149 variable pps_1s_error : signed(63 downto 0) := (others => '0');
150 variable pps_10s_error : signed(63 downto 0) := (others => '0');
151 variable pps_100s_error : signed(63 downto 0) := (others => '0');
152 begin
153 mm_rd_req <= '0';
154 mm_wr_req <= '0';
155 mm_addr <= (others => '0');
156 mm_wr_data <= (others => '0');
157
158 dac_count <= current.dac;
159
160 case (current.state) is
161 when RESET_COUNTERS =>
162 mm_wr_req <= '1';
163 mm_wr_data <= x"07";
164 mm_addr <= std_logic_vector(to_unsigned(CONTROL_ADDR,mm_addr'length));
165 future.holdoff_count <= 0;
166 future.state <= START_COUNTERS;
167
168 when START_COUNTERS =>
169 future.holdoff_count <= current.holdoff_count + 1;
170 if( current.holdoff_count = RESET_TIME ) then
171 future.holdoff_count <= 0;
172 mm_wr_req <= '1';
173 mm_wr_data <= x"00";
174 mm_addr <= std_logic_vector(to_unsigned(CONTROL_ADDR,mm_addr'length));
175 future.state <= HOLDOFF;
176 end if;
177
178 when ENABLE_IRQS =>
179 mm_wr_req <= '1';
180 mm_wr_data <= x"01";
181 mm_addr <= std_logic_vector(to_unsigned(INTERRUPT_ADDR,mm_addr'length));
182 future.state <= WAIT_FOR_IRQ;
183
184 when WAIT_FOR_IRQ =>
185 if( mm_irq = '1' ) then
186 future.holdoff_count <= 0;
187 mm_wr_req <= '1';
188 mm_wr_data <= x"11"; -- clear the interrupt
189 mm_addr <= std_logic_vector(to_unsigned(INTERRUPT_ADDR,mm_addr'length));
190 future.state <= READ_COUNTS;
191 else
192 future.holdoff_count <= current.holdoff_count + 1;
193 if( current.holdoff_count = IRQ_TIMEOUT ) then
194 future.holdoff_count <= 0;
195 future.state <= TERMINATE_SIMULATION;
196 end if;
197 end if;
198
199 when READ_COUNTS =>
200 future.holdoff_count <= current.holdoff_count + 1;
201 mm_rd_req <= '1';
202 if( current.holdoff_count < 8 ) then
203 mm_addr <= std_logic_vector(to_unsigned(PPS_CNT_1S_ADDR+(current.holdoff_count mod 8),mm_addr'length));
204 future.pps_1s_count <= mm_rd_data &
205 current.pps_1s_count(current.pps_1s_count'left downto
206 current.pps_1s_count'right+mm_rd_data'length);
207 elsif( current.holdoff_count < 16 ) then
208 -- Need to shift in the last byte of data from previous count
209 if( current.holdoff_count = 8 ) then
210 future.pps_1s_count <= mm_rd_data &
211 current.pps_1s_count(current.pps_1s_count'left downto
212 current.pps_1s_count'right+mm_rd_data'length);
213 end if;
214 -- Also start capturing the next counter value
215 mm_addr <= std_logic_vector(to_unsigned(PPS_CNT_10S_ADDR+(current.holdoff_count mod 8),mm_addr'length));
216 future.pps_10s_count <= mm_rd_data &
217 current.pps_10s_count(current.pps_10s_count'left downto
218 current.pps_10s_count'right+mm_rd_data'length);
219 elsif( current.holdoff_count < 24 ) then
220 -- Need to shift in the last byte of data from previous count
221 if( current.holdoff_count = 16 ) then
222 future.pps_10s_count <= mm_rd_data &
223 current.pps_10s_count(current.pps_10s_count'left downto
224 current.pps_10s_count'right+mm_rd_data'length);
225 end if;
226 -- Also start capturing the next counter value
227 mm_addr <= std_logic_vector(to_unsigned(PPS_CNT_100S_ADDR+(current.holdoff_count mod 8),mm_addr'length));
228 future.pps_100s_count <= mm_rd_data &
229 current.pps_100s_count(current.pps_100s_count'left downto
230 current.pps_100s_count'right+mm_rd_data'length);
231 else
232 -- Need to shift in the last byte of data from previous count
233 if( current.holdoff_count = 24 ) then
234 future.pps_100s_count <= mm_rd_data &
235 current.pps_100s_count(current.pps_100s_count'left downto
236 current.pps_100s_count'right+mm_rd_data'length);
237 end if;
238 mm_rd_req <= '0';
239 future.holdoff_count <= 0;
240 future.state <= FREQ_ADJUST;
241 end if;
242
243 when FREQ_ADJUST =>
244
245 -- Current error (only valid if the count is > 0)
246 if( unsigned(current.pps_1s_count) /= to_unsigned(0,current.pps_1s_count'length) ) then
247 pps_1s_error := signed(current.pps_1s_count)-PPS_1S_TARGET;
248 end if;
249
250 if( unsigned(current.pps_10s_count) /= to_unsigned(0,current.pps_10s_count'length) ) then
251 pps_10s_error := signed(current.pps_10s_count)-PPS_10S_TARGET;
252 end if;
253
254 if( unsigned(current.pps_100s_count) /= to_unsigned(0,current.pps_100s_count'length) ) then
255 pps_100s_error := signed(current.pps_100s_count)-PPS_100S_TARGET;
256 end if;
257
258 -- Save current error off for next time "previous error"
259 future.pid_1s.error <= pps_1s_error;
260 future.pid_10s.error <= pps_10s_error;
261 future.pid_100s.error <= pps_100s_error;
262
263 if( abs(pps_1s_error) > PPS_1S_MAX_ERROR ) then
264 if( pps_1s_error < 0 ) then -- too slow, increase DAC
265 future.dac <= current.dac + to_unsigned(1024,current.dac'length);
266 else -- too fast, lower the DAC
267 future.dac <= current.dac - to_unsigned(1024,current.dac'length);
268 end if;
269 elsif( abs(pps_10s_error) > PPS_10S_MAX_ERROR ) then
270 if( pps_10s_error < 0 ) then -- too slow, increase DAC
271 future.dac <= current.dac + to_unsigned(512,current.dac'length);
272 else -- too fast, lower the DAC
273 future.dac <= current.dac - to_unsigned(512,current.dac'length);
274 end if;
275 elsif( abs(pps_100s_error) > PPS_100S_MAX_ERROR ) then
276 if( pps_100s_error < 0 ) then -- too slow, increase DAC
277 future.dac <= current.dac + to_unsigned(128,current.dac'length);
278 else -- too fast, lower the DAC
279 future.dac <= current.dac - to_unsigned(128,current.dac'length);
280 end if;
281 end if;
282
283
284
285 future.tcxo_half_per <= half_clk_per(current.tcxo_freq+TCXO_INCR_FREQ);
286 future.tcxo_freq <= current.tcxo_freq + TCXO_INCR_FREQ;
287 future.state <= HOLDOFF;
288
289 when HOLDOFF =>
290 future.holdoff_count <= current.holdoff_count + 1;
291 if( current.holdoff_count = DEAD_TIME ) then
292 future.holdoff_count <= 0;
293 future.state <= ENABLE_IRQS;
294 end if;
295
296 when TERMINATE_SIMULATION =>
297 stop(0);
298 null;
299
300 end case;
301 end process;
302
303 end architecture;