Debian Codegraph @

hdl/fpga/ip/nuand/time_tamer/vhdl/time_tamer.vhd

raw
1 library ieee ;
2 use ieee.std_logic_1164.all ;
3 use ieee.numeric_std.all ;
4
5 entity time_tamer is
6 port (
7 -- Control signals
8 clock : in std_logic ;
9 reset : in std_logic ;
10
11 -- Memory mapped interface
12 addr : in std_logic_vector(4 downto 0) ;
13 din : in std_logic_vector(7 downto 0) ;
14 dout : out std_logic_vector(7 downto 0) ;
15 write : in std_logic ;
16 read : in std_logic ;
17 waitreq : out std_logic ;
18 readack : out std_logic ;
19 intr : out std_logic ;
20
21 -- Sync signals
22 ts_sync_in : in std_logic ;
23 ts_sync_out : out std_logic ;
24
25 -- Exported signals
26 ts_pps : in std_logic ;
27 ts_clock : in std_logic ;
28 ts_reset : in std_logic ;
29 ts_time : out std_logic_vector(63 downto 0)
30 ) ;
31 end entity ;
32
33 architecture arch of time_tamer is
34
35 signal ack : std_logic ;
36
37 signal snap_time : unsigned(63 downto 0);
38
39 signal snap_req : std_logic ;
40
41 signal snap_ack : std_logic ;
42
43 signal hold_time : unsigned(63 downto 0) ;
44
45 signal uaddr : unsigned(addr'range) ;
46
47 signal status : std_logic_vector(7 downto 0) ;
48
49 signal intr_clear : std_logic ;
50 signal intr_set : std_logic ;
51
52 signal timestamp : unsigned(63 downto 0) ;
53
54 signal compare_time : unsigned(63 downto 0) := (others => '0');
55
56 signal mm_clear_compare : std_logic ;
57 signal mm_compare_cleared : std_logic ;
58
59 signal ts_clear_compare : std_logic ;
60 signal ts_compare_cleared : std_logic ;
61
62 signal ts_time_trigger : std_logic ;
63 signal mm_time_trigger : std_logic ;
64
65 signal current_time : unsigned(63 downto 0) ;
66 signal current_req : std_logic ;
67 signal current_ack : std_logic ;
68
69 signal status_past : std_logic ;
70
71 signal mm_compare_load : std_logic ;
72 signal ts_compare_load : std_logic ;
73
74 signal mm_compare_loaded : std_logic ;
75 signal ts_compare_loaded : std_logic ;
76
77 signal mm_ts_reset : std_logic ;
78
79 begin
80
81 uaddr <= unsigned(addr) ;
82
83 readack <= ack ;
84 waitreq <= not(ack or write) ;
85
86 ts_sync_out <= '0' ;
87
88 status <= (0 => status_past, others =>'0') ;
89
90 U_sync_ts_reset : entity work.synchronizer
91 generic map (
92 RESET_LEVEL => '1'
93 ) port map (
94 reset => reset,
95 clock => clock,
96 async => ts_reset,
97 sync => mm_ts_reset
98 ) ;
99
100 U_sync_load : entity work.synchronizer
101 generic map (
102 RESET_LEVEL => '0'
103 ) port map (
104 reset => ts_reset,
105 clock => ts_clock,
106 async => mm_compare_load,
107 sync => ts_compare_load
108 ) ;
109
110 U_sync_loaded : entity work.synchronizer
111 generic map (
112 RESET_LEVEL => '0'
113 ) port map (
114 reset => reset,
115 clock => clock,
116 async => ts_compare_loaded,
117 sync => mm_compare_loaded
118 ) ;
119
120 U_sync_clear : entity work.synchronizer
121 generic map (
122 RESET_LEVEL => '0'
123 ) port map (
124 reset => ts_reset,
125 clock => ts_clock,
126 async => mm_clear_compare,
127 sync => ts_clear_compare
128 ) ;
129
130 U_sync_cleared : entity work.synchronizer
131 generic map (
132 RESET_LEVEL => '0'
133 ) port map (
134 reset => reset,
135 clock => clock,
136 async => ts_compare_cleared,
137 sync => mm_compare_cleared
138 ) ;
139
140 U_sync_trigger : entity work.synchronizer
141 generic map (
142 RESET_LEVEL => '0'
143 ) port map (
144 reset => reset,
145 clock => clock,
146 async => ts_time_trigger,
147 sync => mm_time_trigger
148 ) ;
149
150 ts_time <= std_logic_vector(timestamp) ;
151
152 increment_time : process(ts_clock, ts_reset)
153 variable tick : std_logic := '1' ;
154 begin
155 if( ts_reset = '1' ) then
156 tick := '1' ;
157 timestamp <= (others =>'0') ;
158 elsif( rising_edge(ts_clock) ) then
159 if( tick = '0' ) then
160 timestamp <= timestamp + 1 ;
161 end if ;
162 tick := not tick ;
163 end if ;
164 end process ;
165
166 U_snap : entity work.handshake
167 generic map (
168 DATA_WIDTH => ts_time'length
169 ) port map (
170 source_reset => ts_reset,
171 source_clock => ts_clock,
172 source_data => std_logic_vector(timestamp),
173
174 dest_reset => reset,
175 dest_clock => clock,
176 unsigned(dest_data) => snap_time,
177 dest_req => snap_req,
178 dest_ack => snap_ack
179 ) ;
180
181 U_current : entity work.handshake
182 generic map (
183 DATA_WIDTH => ts_time'length
184 ) port map (
185 source_reset => ts_reset,
186 source_clock => ts_clock,
187 source_data => std_logic_vector(timestamp),
188
189 dest_reset => reset,
190 dest_clock => clock,
191 unsigned(dest_data) => current_time,
192 dest_req => current_req,
193 dest_ack => current_ack
194 ) ;
195
196 request : process(clock, reset)
197 begin
198 if( reset = '1' ) then
199 snap_req <= '0' ;
200 elsif( rising_edge(clock) ) then
201 if( uaddr = 0 and read = '1' ) then
202 snap_req <= '1' ;
203 else
204 snap_req <= '0' ;
205 end if ;
206 end if ;
207 end process ;
208
209 generate_ack : process(clock, reset)
210 begin
211 if( reset = '1' ) then
212 ack <= '0' ;
213 elsif( rising_edge(clock) ) then
214 if( uaddr = 0 and read = '1' and mm_ts_reset = '0' ) then
215 ack <= snap_ack ;
216 else
217 ack <= read ;
218 end if ;
219 end if ;
220 end process ;
221
222 mm_read : process(clock)
223 begin
224 if( rising_edge(clock) ) then
225 case to_integer(uaddr) is
226 when 0 => dout <= std_logic_vector(snap_time(7 downto 0));
227 when 1 => dout <= std_logic_vector(snap_time(15 downto 8));
228 when 2 => dout <= std_logic_vector(snap_time(23 downto 16));
229 when 3 => dout <= std_logic_vector(snap_time(31 downto 24));
230 when 4 => dout <= std_logic_vector(snap_time(39 downto 32));
231 when 5 => dout <= std_logic_vector(snap_time(47 downto 40));
232 when 6 => dout <= std_logic_vector(snap_time(55 downto 48));
233 when 7 => dout <= std_logic_vector(snap_time(63 downto 56));
234
235 when 9 => dout <= status ;
236
237 when others => dout <= x"13";
238 end case;
239 end if ;
240 end process;
241
242 mm_write : process(clock)
243 begin
244 if( rising_edge(clock) ) then
245 intr_set <= '0' ;
246 intr_clear <= '0' ;
247 if( write = '1' ) then
248 case to_integer(uaddr) is
249 when 0 => hold_time( 7 downto 0) <= unsigned(din) ;
250 when 1 => hold_time(15 downto 8) <= unsigned(din) ;
251 when 2 => hold_time(23 downto 16) <= unsigned(din) ;
252 when 3 => hold_time(31 downto 24) <= unsigned(din) ;
253 when 4 => hold_time(39 downto 32) <= unsigned(din) ;
254 when 5 => hold_time(47 downto 40) <= unsigned(din) ;
255 when 6 => hold_time(55 downto 48) <= unsigned(din) ;
256 when 7 => hold_time(63 downto 56) <= unsigned(din) ;
257
258 when 8 =>
259 -- Command coming in!
260 case to_integer(unsigned(din)) is
261 when 0 =>
262 -- Set interrupt
263 intr_set <= '1' ;
264
265 when 1 =>
266 -- Clear interrupt
267 intr_clear <= '1' ;
268
269 when 2 =>
270 -- Latch time manual
271
272 when 3 =>
273 -- Latch time next PPS
274
275 when others =>
276 null ;
277
278 end case ;
279
280 when others => null ;
281 end case ;
282 end if ;
283 end if ;
284 end process ;
285
286 check_compare : process(ts_clock, ts_reset)
287 type fsm_t is (WAIT_FOR_LOAD, WAIT_FOR_COMPARE, WAIT_FOR_CLEAR) ;
288 variable fsm : fsm_t := WAIT_FOR_LOAD ;
289 begin
290 if( ts_reset = '1' ) then
291 fsm := WAIT_FOR_LOAD ;
292 ts_compare_loaded <= '0' ;
293 ts_compare_cleared <= '0' ;
294 ts_time_trigger <= '0' ;
295 elsif(rising_edge(ts_clock)) then
296 case fsm is
297
298 when WAIT_FOR_LOAD =>
299 if( ts_compare_load = '1' ) then
300 ts_compare_loaded <= '1' ;
301 compare_time <= hold_time ;
302 ts_compare_cleared <= '0' ;
303 fsm := WAIT_FOR_COMPARE ;
304 end if ;
305
306 when WAIT_FOR_COMPARE =>
307 if( compare_time = timestamp ) then
308 ts_time_trigger <= '1' ;
309 ts_compare_loaded <= '0' ;
310 fsm := WAIT_FOR_CLEAR ;
311 end if ;
312
313 when WAIT_FOR_CLEAR =>
314 if( ts_clear_compare = '1' ) then
315 ts_time_trigger <= '0' ;
316 ts_compare_cleared <= '1' ;
317 fsm := WAIT_FOR_LOAD ;
318 end if ;
319
320 when others =>
321 null ;
322
323 end case ;
324 end if ;
325 end process ;
326
327 interrupt : process(clock, reset)
328 type fsm_t is (WAIT_FOR_ARM, CHECK_CURRENT_TIME, SET_COMPARE_TIME, PAST_TIME, WAIT_FOR_COMPARE, WAIT_FOR_INTR_CLEAR, WAIT_FOR_COMPARE_CLEAR) ;
329 variable fsm : fsm_t := WAIT_FOR_ARM ;
330 begin
331 if( reset = '1' ) then
332 intr <= '0' ;
333 fsm := WAIT_FOR_ARM ;
334 mm_clear_compare <= '0' ;
335 mm_compare_load <= '0' ;
336 current_req <= '0' ;
337 status_past <= '0' ;
338 elsif( rising_edge(clock) ) then
339 if( mm_ts_reset = '1' ) then
340 intr <= '0' ;
341 fsm := WAIT_FOR_ARM ;
342 mm_clear_compare <= '0' ;
343 mm_compare_load <= '0' ;
344 current_req <= '0' ;
345 status_past <= '0' ;
346 else
347 case fsm is
348 when WAIT_FOR_ARM =>
349 current_req <= '0' ;
350 status_past <= '0' ;
351 mm_clear_compare <= '0' ;
352 mm_compare_load <= '0' ;
353 if( intr_set = '1' ) then
354 fsm := CHECK_CURRENT_TIME ;
355 end if ;
356
357 when CHECK_CURRENT_TIME =>
358 current_req <= '1' ;
359 if( current_ack = '1' ) then
360 current_req <= '0' ;
361 if( hold_time < current_time ) then
362 fsm := PAST_TIME ;
363 status_past <= '1' ;
364 else
365 fsm := SET_COMPARE_TIME ;
366 end if ;
367 end if ;
368
369 when SET_COMPARE_TIME =>
370 mm_compare_load <= '1' ;
371 if( mm_compare_loaded = '1' ) then
372 mm_compare_load <= '0' ;
373 fsm := WAIT_FOR_COMPARE ;
374 end if ;
375
376 when PAST_TIME =>
377 intr <= '1' ;
378 fsm := WAIT_FOR_INTR_CLEAR ;
379
380 when WAIT_FOR_COMPARE =>
381 if( mm_time_trigger = '1' ) then
382 intr <= '1' ;
383 fsm := WAIT_FOR_INTR_CLEAR ;
384 end if ;
385
386 when WAIT_FOR_INTR_CLEAR =>
387 mm_clear_compare <= '1' ;
388 if( intr_clear = '1' ) then
389 intr <= '0' ;
390 if( status_past = '1' ) then
391 fsm := WAIT_FOR_ARM ;
392 else
393 fsm := WAIT_FOR_COMPARE_CLEAR ;
394 end if ;
395 end if ;
396
397 when WAIT_FOR_COMPARE_CLEAR =>
398 if( mm_compare_cleared = '1' ) then
399 mm_clear_compare <= '0' ;
400 fsm := WAIT_FOR_ARM ;
401 end if ;
402
403 when others => null ;
404 end case ;
405 end if ;
406 end if ;
407 end process ;
408
409 end architecture ;
410