AMC13
Firmwares for the different applications of the AMC13 uTCA board made at Boston University
 All Classes Variables
event_generator.vhd
1 ------------------------------------------------------
2 -- remove CMC slink status word until the first Header on L0 & L1 before each event
3 --
4 -- Ver 2.00
5 --
6 -- Dominique Gigi Jan 2011
7 ------------------------------------------------------
8 -- func(0) bit 0 PCI trigger
9 -- bit 1 Start loop
10 -- bit 2 stop loop
11 -- bit 3 Ena PCI trigger
12 -- bit 4 Start
13 -- bit 5 soft_reset
14 -- func(1) bit23.0 event #
15 -- func(2) bit 0 CRC error gen
16 -- bit 1 BIT error gen
17 -- func(3) bit11.0 Source #
18 -- bit27.11 BX#
19 -- func(4) bit23.0 event lenght
20 -- func(5) bit15..0 wc -- for random distribution of triggers
21 -- bit 31..16 timer between two trigger
22 --
23 -- correct some variable to be compliant the the old graphical version
24 ------------------------------------------------------
25 LIBRARY ieee;
26 --LIBRARY altera_mf;
27 --LIBRARY altera;
28 
29 
30 USE ieee.std_logic_1164.all;
31 use ieee.numeric_std.all;
32 use ieee.std_logic_unsigned.all;
33 --LIBRARY lpm;
34 --USE lpm.lpm_components.all;
35 --USE altera_mf.altera_mf_components.all;
36 --USE altera.altera_primitives_components.all;
37 
38 entity event_generator is
39 
40 port (
41  reset : IN std_logic;
42  low_clk : IN std_logic; -- frequency of 50 Mhz
43  PCIe_clk : IN std_logic;
44  PCIe_func : IN std_logic_vector(15 downto 0);
45  PCIe_wen : IN std_logic;
46  PCIe_dti : IN std_logic_vector(31 downto 0);
47  PCIe_dto : out std_logic_vector(31 downto 0);
48  PCIe_cs : IN std_logic;
49 
50  evt_clk : IN std_logic;
51 
52  wen : OUT std_logic;
53  data : OUT std_logic_vector(63 downto 0);
54  uctrl : OUT std_logic;
55  Back_p : IN std_logic -- Back_p when '0'
56  );
57 end event_generator;
58 
59 architecture behavioral of event_generator is
60 
61 component resync
62 port (
63  reset : in std_logic;
64  Free_clki : in std_logic;
65  clocki : in std_logic;
66  clocko : in std_logic;
67  input : in std_logic;
68  output : out std_logic
69  );
70 end component;
71 
72 component trigger_gen
73 port (
74  Rst_Pciclk : IN std_logic;
75  PCIe_clk : IN std_logic;
76  PCIe_func : IN std_logic_vector(15 downto 0);
77  PCIe_wen : IN std_logic;
78  PCIe_dti : IN std_logic_vector(31 downto 0);
79  PCIe_dto : OUT std_logic_vector(31 downto 0);
80  PCIe_cs : IN std_logic;
81  ttc_trigger : IN std_logic;
82 
83  Rst_Evtclk : IN std_logic;
84  evt_clk : IN std_logic;
85  ena_PCIe : OUT std_logic;
86  run_mode : OUT std_logic;
87  trig_nb : OUT std_logic_vector(23 downto 0);
88  trigger : OUT std_logic;
89  end_evt : IN std_logic;
90  Back_p : IN std_logic );
91 end component;
92 
93 component CRC_generator
94  Port (
95  D : in std_logic_vector(63 downto 0);
96  CRC_out : out std_logic_vector(15 downto 0);
97  clk : in std_logic;
98  clear : in std_logic;
99  enable : in std_logic);
100 end component;
101 --memory used to store WC and trigger distribution (31..16) WC (words) // (15..0) time between triggers (bit= 20 ns)
102 component memory_rnd
103 port (
104  RST_lowClk : IN std_logic;
105  low_clk : IN Std_logic;
106  RST_PCIClk : IN Std_logic;
107  PCIe_clk : IN std_logic;
108 
109  PCIe_dt : IN std_logic_vector(31 downto 0);
110  PCIe_func : IN std_logic_vector(15 downto 0);
111  PCIe_cs : IN std_logic;
112  PCIe_wen : IN std_logic;
113  start : IN std_logic;
114  wc : OUT std_logic_vector(15 downto 0);
115  RST_EvtClk : IN std_logic;
116  evt_clk : IN std_logic;
117  trigger : OUT std_logic;
118  end_evt : IN std_logic
119  );
120 end component;
121 
122 component generate_3
123  PORT
124  (
125  clock : IN STD_LOGIC;
126  START : IN STD_LOGIC;
127  LOAD_SEED : IN STD_LOGIC;
128  SEED : IN STD_LOGIC_VECTOR(7 DOWNTO 0);
129  rnd : OUT STD_LOGIC_VECTOR(7 DOWNTO 0)
130  );
131 END component;
132 
133 component reset_resync is
134 port (
135  reset : in std_logic;
136  clock : in std_logic;
137 
138  Reset_sync : out std_logic
139  );
140 end component;
141 
142 signal rnd_dt_O : std_logic_vector(63 downto 0);
143 signal rnd_dt_L : std_logic_vector(63 downto 0);
144 signal ena_rnd_dt : std_logic;
145 signal LFF_D : std_logic;
146 signal BX : std_logic_vector(11 downto 0);
147 signal SOURCE : std_logic_vector(11 downto 0);
148 signal evt_lenght : std_logic_vector(23 downto 0); --(byte--)
149 signal crc_error : std_logic_vector(2 downto 0);
150 signal bit_error : std_logic_vector(2 downto 0);
151 signal hd_error : std_logic_vector(2 downto 0);
152 signal tr_error : std_logic_vector(2 downto 0);
153 signal HD_ena : std_logic;
154 signal TR_ena : std_logic_vector(3 downto 0);
155 signal PL_ena : std_logic_vector(1 downto 0);
156 signal HD_dt : std_logic_vector(63 downto 0);
157 signal TR_dt : std_logic_vector(63 downto 0);
158 signal PL_dt : std_logic_vector(63 downto 0);
159 signal evt_run : std_logic_vector(3 downto 0);
160 signal pre_TR : std_logic_vector(63 downto 0);
161 signal WC_size : std_logic_vector(20 downto 0);
162 signal end_WC : std_logic_vector(1 downto 0);
163 
164 signal run_mode : std_logic;
165 signal ena_PCIe : std_logic;
166 signal trig_nb : std_logic_vector(23 downto 0);
167 signal trigger : std_logic;
168 signal end_evt : std_logic;
169 signal soft_rst : std_logic;
170 
171 signal wc_rnd : std_logic_vector(15 downto 0);
172 signal trigger_rnd : std_logic;
173 signal pseudo_reset : std_logic_vector(0 downto 0);
174 signal CRC_OUT : std_logic_vector(15 downto 0);
175 signal CRC_val : std_logic_vector(15 downto 0);
176 signal CRC_cmp : std_logic;
177 signal CRC_reg : std_logic_vector(63 downto 0);
178 
179 signal wen_reg : std_logic;
180 signal data_reg : std_logic_vector(63 downto 0);
181 signal uctrl_reg : std_logic;
182 signal PCIe_dto_trg : std_logic_vector(31 downto 0);
183 signal PCIe_dto_local: std_logic_vector(31 downto 0);
184 
185 signal Byte_rnd : std_logic_vector(7 downto 0);
186 signal Rst_PCIclk : std_logic;
187 signal Rst_Evtclk : std_logic;
188 signal Rst_lowclk : std_logic;
189 --***********************************************************************
190 --**********************<< BEGIN >>*************************
191 --***********************************************************************
192 begin
193 --/***************** resync reset to clocks domain******************
194 rst_pci_i1:reset_resync
195 port map(
196  reset => reset,
197  clock => PCIe_clk,
198  Reset_sync => Rst_PCIclk
199  );
200 
201 rst_evt_i1:reset_resync
202 port map(
203  reset => reset,
204  clock => evt_clk,
205  Reset_sync => Rst_Evtclk
206  );
207 
208 rst_low_i1:reset_resync
209 port map(
210  reset => reset,
211  clock => low_clk,
212  Reset_sync => Rst_lowclk
213  );
214 
215 
216 --*************** Sync Back_pressure ************************
217 process(Rst_Evtclk,evt_clk)
218 begin
219  if Rst_Evtclk = '0' then
220  LFF_D <= '1';
221  elsif rising_edge(evt_clk) then
222  LFF_D <= Back_p; -- Back_p when '0'
223  end if;
224 end process;
225 
226 
227 ena_rnd_dt <= '1' when LFF_D = '1' and evt_run(2) = '1' else '0';
228 
229 --################## Pseudo random data with 8 times same bytes ###################
230 i3:generate_3
231 PORT MAP
232 (
233  clock => evt_clk,
234  START => ena_rnd_dt,
235  LOAD_SEED => evt_run(0),
236  SEED => x"00",
237  rnd => Byte_rnd
238 );
239 
240 rnd_dt_L(63 downto 56) <= Byte_rnd;
241 rnd_dt_L(55 downto 48) <= Byte_rnd;
242 rnd_dt_L(47 downto 40) <= Byte_rnd;
243 rnd_dt_L(39 downto 32) <= Byte_rnd;
244 rnd_dt_L(31 downto 24) <= not(Byte_rnd);
245 rnd_dt_L(23 downto 16) <= not(Byte_rnd);
246 rnd_dt_L(15 downto 08) <= not(Byte_rnd);
247 rnd_dt_L(07 downto 00) <= not(Byte_rnd);
248 
249 
250 --*********************************************************************************
251 --***********************<< error generator >> ***********************************
252 --******** decoding PCIe functions
253 process(Rst_PCIclk,PCIe_clk)
254 begin
255 if Rst_PCIclk = '0' then
256  crc_error(0) <= '0';
257  bit_error(0) <= '0';
258  hd_error(0) <= '0';
259  tr_error(0) <= '0';
260 elsif rising_edge(PCIe_clk) then
261  crc_error(0) <= '0';
262  bit_error(0) <= '0';
263  hd_error(0) <= '0';
264  tr_error(0) <= '0';
265  if PCIe_cs = '1' AND PCIe_func(6) = '1' AND PCIe_wen = '1' then
266  crc_error(0) <= PCIe_dti(0);
267  bit_error(0) <= PCIe_dti(1);
268  hd_error(0) <= PCIe_dti(2);
269  tr_error(0) <= PCIe_dti(3);
270  end if;
271 end if;
272 end process;
273 
274 crc_err_resync:resync
275 port map(
276  reset => Rst_PCIclk,
277  Free_clki => '1',
278  clocki => PCIe_clk,
279  clocko => evt_clk,
280  input => crc_error(0),
281  output => crc_error(1) );
282 bit_err_resync:resync
283 port map(
284  reset => Rst_PCIclk,
285  Free_clki => '1',
286  clocki => PCIe_clk,
287  clocko => evt_clk,
288  input => bit_error(0),
289  output => bit_error(1) );
290 
291 hd_err_resync:resync
292 port map(
293  reset => Rst_PCIclk,
294  Free_clki => '1',
295  clocki => PCIe_clk,
296  clocko => evt_clk,
297  input => hd_error(0),
298  output => hd_error(1) );
299 
300 tr_err_resync:resync
301 port map(
302  reset => Rst_PCIclk,
303  Free_clki => '1',
304  clocki => PCIe_clk,
305  clocko => evt_clk,
306  input => tr_error(0),
307  output => tr_error(1) );
308 
309 process(Rst_Evtclk,evt_clk)
310 begin
311  if Rst_Evtclk = '0' then
312  crc_error(2) <= '0';
313  bit_error(2) <= '0';
314  hd_error(2) <= '0';
315  tr_error(2) <= '0';
316  elsif rising_edge(evt_clk) then
317  if crc_error(1) = '1' then
318  crc_error(2) <= '1';
319  elsif TR_ena(3) = '1' then
320  crc_error(2) <= '0';
321  end if;
322 
323  if bit_error(1) = '1' then
324  bit_error(2) <= '1';
325  elsif evt_run(3) = '1' and LFF_D = '1' then
326  bit_error(2) <= '0';
327  end if;
328 
329  if hd_error(1) = '1' then
330  hd_error(2) <= '1';
331  elsif TR_ena(3) = '1' and LFF_D = '1' then
332  hd_error(2) <= '0';
333  end if;
334 
335  if tr_error(1) = '1' then
336  tr_error(2) <= '1';
337  elsif TR_ena(3) = '1' and LFF_D = '1' then
338  tr_error(2) <= '0';
339  end if;
340  end if;
341 end process;
342 
343 end_evt <= TR_ena(3);
344 
345 --*******************************************************************************
346 --**************************<< event gener. >>***********************************
347 
348 trig:trigger_gen
349 port map(
350  Rst_Pciclk => Rst_Pciclk,
351  PCIe_clk => PCIe_clk,
352  PCIe_func => PCIe_func,
353  PCIe_wen => PCIe_wen,
354  PCIe_dti => PCIe_dti,
355  PCIe_dto => PCIe_dto_trg,
356  PCIe_cs => PCIe_cs,
357  ttc_trigger => trigger_rnd,
358  Rst_Evtclk => Rst_Evtclk,
359  evt_clk => evt_clk,
360  ena_PCIe => ena_PCIe,
361  run_mode => run_mode,
362  trig_nb => trig_nb,
363  trigger => trigger,
364  end_evt => end_evt,
365  Back_p => LFF_D );
366 
367 process(Rst_Evtclk,evt_clk)
368 begin
369  if Rst_Evtclk = '0' then
370  evt_run <= (others => '0');
371  elsif rising_edge(evt_clk) then
372  if evt_run(1) = '1' then
373  evt_run(2) <= '1';
374  elsif LFF_D = '1' AND end_WC(0) = '1' then
375  evt_run(2) <= '0';
376  end if;
377 
378  if evt_run(2) = '1' then
379  evt_run(3) <= '1';
380  elsif end_WC(1) = '1' then
381  evt_run(3) <= '0';
382  end if;
383 
384  evt_run(1) <= evt_run(0);
385  evt_run(0) <= trigger;
386  end if;
387 end process;
388 
389 rnd_mem_trig:memory_rnd
390 port map(
391  RST_lowClk => RST_lowClk,
392  low_clk => low_clk,
393  RST_PCIClk => RST_PCIClk,
394  PCIe_clk => PCIe_clk,
395  PCIe_dt => PCIe_dti,
396  PCIe_func => PCIe_func,
397  PCIe_cs => PCIe_cs,
398  PCIe_wen => PCIe_wen,
399  start => run_mode,
400  wc => wc_rnd ,
401  RST_EvtClk => RST_EvtClk,
402  evt_clk => evt_clk,
403  trigger => trigger_rnd,
404  end_evt => end_evt
405  );
406 
407 --*******************************************************************************
408 --************************** << Header >> ***********************************
409 process(Rst_PCIclk,PCIe_clk)
410 begin
411  if Rst_PCIclk = '0' then
412  BX <= (others => '0');
413  SOURCE <= (others => '0');
414  evt_lenght <= (others => '0');
415  elsif rising_edge(PCIe_clk) then
416  if PCIe_cs = '1' and PCIe_func(3) = '1' AND PCIe_wen = '1' then
417  BX <= PCIe_dti(27 downto 16);
418  SOURCE <= PCIe_dti(11 downto 00);
419  end if;
420  if PCIe_cs = '1' and PCIe_func(4) = '1' AND PCIe_wen = '1' then
421  evt_lenght <= PCIe_dti(23 downto 00);
422  end if;
423 
424  PCIe_dto_local <= (others => '0');
425  if PCIe_func(3) = '1' then
426  PCIe_dto_local(11 downto 0) <= BX;
427  PCIe_dto_local(27 downto 16) <= SOURCE;
428  elsif PCIe_func(4) = '1' then
429  PCIe_dto_local(23 downto 00) <= evt_lenght;
430  end if;
431  end if;
432 end process;
433 
434 PCIe_dto <= PCIe_dto_local when PCIe_func(4) = '1' or PCIe_func(3) = '1' else PCIe_dto_trg;
435 
436 process(Rst_Evtclk,evt_clk)
437 begin
438  if Rst_Evtclk = '0' then
439  HD_ena <= '0';
440  elsif rising_edge(evt_clk) then
441  HD_ena <= evt_run(0);
442 
443  HD_dt(63 downto 56) <= x"50";
444  HD_dt(55 downto 32) <= trig_nb;
445  HD_dt(31 downto 20) <= BX;
446  HD_dt(19 downto 08) <= SOURCE;
447  HD_dt(07 downto 00) <= x"00";
448  end if;
449 end process;
450 
451 
452 --*******************************************************************************
453 --************************** << PayLoad >> ***********************************
454 pseudo_reset(0) <= '0' when (end_WC(0) = '1' AND LFF_D = '1') else '1';
455 
456 process(pseudo_reset(0),evt_clk)
457 begin
458  if pseudo_reset(0) = '0' then
459  PL_ena(0) <= '0';
460  elsif rising_edge(evt_clk) then
461  if reset = '0' then
462  PL_ena(0) <= '0';
463  elsif evt_run(2) = '1' and LFF_D = '1' then
464  PL_ena(0) <= '1';
465  end if;
466  end if;
467 end process;
468 
469 process(Rst_Evtclk,evt_clk)
470 begin
471  if Rst_Evtclk = '0' then
472  PL_ena(1) <= '0';
473  elsif rising_edge(evt_clk) then
474  if PL_ena(0) = '1' and LFF_D = '1' and end_WC(0) = '0' then
475  PL_ena(1) <= '1';
476  elsif end_WC(0) = '1' and LFF_D = '1' then
477  PL_ena(1) <= '0';
478  end if;
479  end if;
480 end process;
481 
482 process(evt_run(0),evt_clk)
483 begin
484 if evt_run(0) = '1' then
485  PL_dt <= (others => '0');
486 elsif rising_edge(evt_clk) then
487  if evt_run(2) = '1' and LFF_D = '1' then
488  PL_dt <= rnd_dt_L;
489  end if;
490 end if;
491 end process;
492 
493 --*******************************************************************************
494 --************************** << Trailer >> ***********************************
495 process(Rst_Evtclk,evt_clk)
496 begin
497  if Rst_Evtclk = '0' then
498  pre_TR <= (others => '0');
499  TR_ena <= (others => '0');
500  elsif rising_edge(evt_clk) then
501  if trigger = '1' then
502  pre_TR(63 downto 56) <= x"A0";
503  if ena_PCIe = '1' then
504  pre_TR(55 downto 53) <= (others => '0');
505  pre_TR(52 downto 32) <= evt_lenght(23 downto 3); -- value send by PCIe (in bytes) wc used in 63-bit words , it's why we start by bit '3'
506  else
507  pre_TR(55 downto 48) <= "00000000";
508  pre_TR(47 downto 32) <= wc_rnd;
509  end if;
510  pre_TR(31 downto 00) <= x"00000000";
511  end if;
512 
513 
514 
515  TR_ena(3 downto 1) <= TR_ena(2 downto 0);
516 
517  TR_ena(0) <= '0';
518  if evt_run(2) = '1' and end_WC(0) = '1' and LFF_D = '1' then
519  TR_ena(0) <= '1' ;
520  end if;
521  end if;
522 end process;
523 
524 TR_dt(63 downto 32) <= pre_TR(63 downto 32);
525 TR_dt(31 downto 16) <= CRC_val;
526 TR_dt(15 downto 00) <= pre_TR(15 downto 00);
527 
528 
529 --*******************************************************************************
530 --**************************<< Event size >>***********************************
531 process(Rst_Evtclk,evt_clk)
532 begin
533  if Rst_Evtclk = '0' then
534  WC_size <= (others => '0');
535  elsif rising_edge(evt_clk) then
536  if evt_run(0) = '1' then
537  WC_size(20 downto 0) <= pre_TR(52 downto 32);
538  elsif evt_run(2) = '1' and LFF_D = '1' then
539  WC_size <= WC_size - "1";
540  end if;
541  end if;
542  end process;
543 
544  process(trigger,evt_clk)
545  begin
546  if trigger = '1' then
547  end_WC <=(others => '0');
548  elsif rising_edge(evt_clk) then
549  end_WC(1) <= '0';
550  if end_WC(0) = '1' AND LFF_D = '1' AND evt_run(1) = '1' then
551  end_WC(1) <= '1';
552  end if;
553 
554  if WC_size = "000000000000000000010" AND evt_run(2) = '1' AND LFF_D = '1' then -- WC include Header and trailer (in 64-bit words)
555  end_WC(0) <= '1';
556  end if;
557  end if;
558 end process;
559 
560 --*******************************************************************************
561 --**************************<< CRC compute >>***********************************
562 process(Rst_Evtclk,evt_clk)
563 begin
564  if Rst_Evtclk = '0' then
565  CRC_cmp <= '0';
566  elsif rising_edge(evt_clk) then
567  if HD_ena = '1' then
568  CRC_reg <= HD_dt;
569  elsif PL_ena(1) = '1' then
570  CRC_reg <= PL_dt;
571  elsif TR_ena(0) = '1' then
572  CRC_reg <= pre_TR;
573  end if;
574 
575  CRC_cmp <= '0';
576  if HD_ena = '1' OR (PL_ena(1) = '1' AND LFF_D = '1') OR TR_ena(0) = '1' then
577  CRC_cmp <= '1';
578  end if;
579  end if;
580 end process;
581 
582 crc_cmp_comp:CRC_generator
583 Port map (
584  D => CRC_reg,
585  CRC_out => CRC_OUT,
586  clk => evt_clk,
587  clear => evt_run(0),
588  enable => CRC_cmp );
589 
590 CRC_val <= not(CRC_OUT) when crc_error(2) = '1' else CRC_OUT;
591 
592 
593 --**********************************************************************************
594 --******************** << registers output >> ************************************
595 
596 process(Rst_Evtclk,evt_clk)
597 begin
598  if Rst_Evtclk = '0' then
599  wen_reg <= '0';
600  uctrl_reg <= '0';
601  elsif rising_edge(evt_clk) then
602  uctrl_reg <= '0';
603  if (HD_ena = '1' and hd_error(2) = '0') OR (TR_ena(2) = '1' and tr_error(2) = '0') then
604  uctrl_reg <= '1';
605  end if;
606 
607  wen_reg <= '0';
608  if HD_ena = '1' OR (PL_ena(1) = '1' AND LFF_D = '1') OR TR_ena(2) = '1' then
609  wen_reg <= '1';
610  end if;
611 
612  if HD_ena = '1' then
613  data_reg <= HD_dt;
614  elsif TR_ena(2) = '1' then
615  data_reg <= TR_dt;
616  elsif LFF_D = '1' and evt_run(2) = '1' then
617  data_reg(63 downto 43) <= PL_dt(63 downto 43);
618  if bit_error(2) = '1' then
619  data_reg(42) <= not(PL_dt(42));
620  else
621  data_reg(42) <= PL_dt(42);
622  end if;
623  data_reg(41 downto 00) <= PL_dt(41 downto 00);
624  end if;
625  end if;
626 end process;
627 
628 wen <= wen_reg;
629 data <= data_reg;
630 uctrl <= not(uctrl_reg);
631 
632 end behavioral;