Debian Codegraph @

hdl/fpga/ip/nuand/pll_reset/vhdl/tb/pll_reset_tb.vhd

raw
1 -- PLL Reset Controller Simulation Testbench
2 -- Copyright (c) 2017 Nuand LLC
3 --
4 -- Permission is hereby granted, free of charge, to any person obtaining a copy
5 -- of this software and associated documentation files (the "Software"), to deal
6 -- in the Software without restriction, including without limitation the rights
7 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 -- copies of the Software, and to permit persons to whom the Software is
9 -- furnished to do so, subject to the following conditions:
10 --
11 -- The above copyright notice and this permission notice shall be included in
12 -- all copies or substantial portions of the Software.
13 --
14 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 -- THE SOFTWARE.
21
22 library ieee;
23 use ieee.std_logic_1164.all;
24 use ieee.numeric_std.all;
25
26 library nuand;
27
28 entity pll_reset_tb is
29 end entity; -- pll_reset_tb
30
31 architecture tb of pll_reset_tb is
32
33 signal done : boolean := false;
34
35 signal clock : std_logic := '1';
36 signal pll_locked : std_logic := '0';
37
38 signal pll_locked_out : std_logic;
39 signal pll_reset : std_logic;
40
41 constant DEVICE_FAMILY : string := "Cyclone V";
42 constant CLOCK_HZ : natural := 10_000_000;
43
44 constant EXPECTED_RESET_TIME : time := 1.1 ms;
45 constant ALLOWED_MARGIN : time := 0.1 ms;
46
47 type stage_t is (
48 TEST_START,
49 TEST_PAUSE,
50 WAIT_FOR_RESET_0,
51 WAIT_FOR_RESET_1,
52 WAIT_FOR_PLL_LOCKED_OUT_0,
53 WAIT_FOR_PLL_LOCKED_OUT_1,
54 TEST_END
55 );
56
57 signal stage : stage_t := TEST_START;
58
59 -- freq_to_period: convert from frequency in Hz to period in seconds
60 function freq_to_period(freq : natural) return time is
61 begin
62 return 1 sec / freq;
63 end function freq_to_period;
64
65 begin
66
67 clock <= not clock after freq_to_period(CLOCK_HZ)/2 when not done else '0';
68
69 U_pll_reset : entity nuand.pll_reset
70 generic map (
71 SYS_CLOCK_FREQ_HZ => CLOCK_HZ,
72 DEVICE_FAMILY => DEVICE_FAMILY
73 )
74 port map (
75 sys_clock => clock,
76
77 pll_locked => pll_locked,
78 pll_locked_out => pll_locked_out,
79 pll_reset => pll_reset
80 );
81
82 tb : process
83 variable start_time : time;
84 begin
85 -- Make sure we're in reset
86 report "TEST: START";
87 stage <= TEST_START;
88 pll_locked <= '0';
89 start_time := now;
90
91 wait for 1 us;
92
93 assert (pll_reset = '1')
94 report "pll_reset not asserted"
95 severity failure;
96
97 -- PLL Reset state machine is holding the PLL in reset
98 -- Wait for reset to drop
99 stage <= WAIT_FOR_RESET_0;
100
101 wait until pll_reset = '0';
102
103 assert (now - start_time) > (EXPECTED_RESET_TIME - ALLOWED_MARGIN)
104 report "FAIL: pll_reset = 0 early " & to_string(now - start_time)
105 severity failure;
106 assert (now - start_time) < (EXPECTED_RESET_TIME + ALLOWED_MARGIN)
107 report "FAIL: pll_reset = 0 late " & to_string(now - start_time)
108 severity failure;
109
110 report "GOOD: pll_reset = 0 after " & to_string(now - start_time);
111
112 -- Simulate a reasonable length of time to lock
113 wait for 100 us;
114
115 -- Lock the PLL
116 report "TEST: simulating PLL lock";
117 stage <= WAIT_FOR_PLL_LOCKED_OUT_1;
118 pll_locked <= '1';
119 start_time := now;
120
121 wait until pll_locked_out = '1';
122
123 report "INFO: pll_locked_out = 1 after " & to_string(now - start_time);
124
125 -- maintain lock for awhile
126 report "TEST: pausing with PLL locked";
127 stage <= TEST_PAUSE;
128
129 while (now - start_time < 5 ms) loop
130 assert (pll_reset = '0')
131 report "FAIL: pll_reset unexpectedly asserted"
132 severity failure;
133 wait for 1 us;
134 end loop;
135
136 -- simulate lock failure
137 report "TEST: simulating lock failure";
138 stage <= WAIT_FOR_RESET_1;
139 pll_locked <= '0';
140 start_time := now;
141
142 wait until pll_reset = '1';
143
144 report "INFO: pll_reset = 1 after " & to_string(now - start_time);
145
146 -- Wait for reset to drop
147 stage <= WAIT_FOR_RESET_0;
148
149 wait until pll_reset = '0';
150
151 assert (now - start_time) > (EXPECTED_RESET_TIME - ALLOWED_MARGIN)
152 report "FAIL: pll_reset = 0 early " & to_string(now - start_time)
153 severity failure;
154 assert (now - start_time) < (EXPECTED_RESET_TIME + ALLOWED_MARGIN)
155 report "FAIL: pll_reset = 0 late " & to_string(now - start_time)
156 severity failure;
157
158 report "GOOD: pll_reset = 0 after " & to_string(now - start_time);
159
160 -- Simulate a reasonable length of time to lock
161 wait for 100 us;
162
163 -- Lock the PLL
164 report "TEST: simulating PLL lock";
165 stage <= WAIT_FOR_PLL_LOCKED_OUT_1;
166 pll_locked <= '1';
167 start_time := now;
168
169 wait until pll_locked_out = '1';
170
171 report "INFO: pll_locked_out = 1 after " & to_string(now - start_time);
172
173 -- maintain lock for awhile
174 report "TEST: pausing with PLL locked";
175 stage <= TEST_PAUSE;
176
177 while (now - start_time < 5 ms) loop
178 assert (pll_reset = '0')
179 report "FAIL: pll_reset unexpectedly asserted"
180 severity failure;
181 wait for 1 us;
182 end loop;
183
184 -- simulate lock failure
185 report "TEST: simulating lock failure";
186 stage <= WAIT_FOR_RESET_1;
187 pll_locked <= '0';
188 start_time := now;
189
190 wait until pll_reset = '1';
191
192 report "INFO: pll_reset = 1 after " & to_string(now - start_time);
193
194 -- Wait for reset to drop
195 stage <= WAIT_FOR_RESET_0;
196
197 wait until pll_reset = '0';
198
199 assert (now - start_time) > (EXPECTED_RESET_TIME - ALLOWED_MARGIN)
200 report "FAIL: pll_reset = 0 early " & to_string(now - start_time)
201 severity failure;
202 assert (now - start_time) < (EXPECTED_RESET_TIME + ALLOWED_MARGIN)
203 report "FAIL: pll_reset = 0 late " & to_string(now - start_time)
204 severity failure;
205
206 report "GOOD: pll_reset = 0 after " & to_string(now - start_time);
207
208 -- simulate an unreasonable length of time to lock
209 report "TEST: simulating failure to lock PLL";
210 stage <= WAIT_FOR_RESET_1;
211 start_time := now;
212
213 wait until pll_reset = '1';
214
215 assert (now - start_time) > (EXPECTED_RESET_TIME - ALLOWED_MARGIN)
216 report "FAIL: pll_reset = 1 early " & to_string(now - start_time)
217 severity failure;
218 assert (now - start_time) < (EXPECTED_RESET_TIME + ALLOWED_MARGIN)
219 report "FAIL: pll_reset = 1 late " & to_string(now - start_time)
220 severity failure;
221
222 report "GOOD: pll_reset = 1 after " & to_string(now - start_time);
223
224 start_time := now;
225 stage <= WAIT_FOR_RESET_0;
226
227 wait until pll_reset = '0';
228
229 assert (now - start_time) > (EXPECTED_RESET_TIME - ALLOWED_MARGIN)
230 report "FAIL: pll_reset = 0 early " & to_string(now - start_time)
231 severity failure;
232 assert (now - start_time) < (EXPECTED_RESET_TIME + ALLOWED_MARGIN)
233 report "FAIL: pll_reset = 0 late " & to_string(now - start_time)
234 severity failure;
235
236 report "GOOD: pll_reset = 0 after " & to_string(now - start_time);
237
238 -- re-lock and finish
239 wait for 100 us;
240 report "TEST: relocking to clean up";
241 pll_locked <= '1';
242 start_time := now;
243 stage <= WAIT_FOR_PLL_LOCKED_OUT_1;
244
245 wait until pll_locked_out = '1';
246
247 report "INFO: pll_locked_out = 1 after " & to_string(now - start_time);
248
249 -- done
250 stage <= TEST_END;
251 wait for 100 us;
252
253 done <= true;
254 report "TEST: completed";
255 wait;
256
257 end process;
258
259 end architecture tb;