Belle II KLM Scint Firmware  1
b2tt_injv.vhd
1 -- b2tt_injv.vhd
2 --
3 
4 library ieee;
5 use ieee.std_logic_1164.all;
6 use ieee.std_logic_unsigned.all;
7 use ieee.numeric_std.all;
8 library work;
9 use work.b2tt_symbols.all;
10 
11 entity b2tt_injv is
12  port (
13  clock : in std_logic;
14  cntpacket : in std_logic_vector (7 downto 0);
15  frameloc : in std_logic_vector (10 downto 0);
16  revoloc : in std_logic_vector (10 downto 0);
17  sigpayload : in std_logic;
18  payload : in std_logic_vector (76 downto 0);
19  injkick : out std_logic;
20  injveto : out std_logic;
21  injvpos : out std_logic_vector (10 downto 0);
22  injvpre : out std_logic_vector (10 downto 0);
23  injvlen : out std_logic_vector (10 downto 0);
24  injvfull : out std_logic_vector (9 downto 0);
25  injvgate : out std_logic_vector (9 downto 0) );
26 end b2tt_injv;
27 
28 architecture implementation of b2tt_injv is
29 
30  subtype plus1_t is std_logic_vector (11 downto 0);
31  subtype bunch_t is std_logic_vector (10 downto 0);
32  subtype frame_t is std_logic_vector (9 downto 0);
33  function add_mod1280
34  ( a: in bunch_t; b: in bunch_t ) return bunch_t is
35  variable aa : plus1_t;
36  variable bb : plus1_t;
37  variable cc : plus1_t;
38  begin
39  aa := '0' & a;
40  bb := '0' & b;
41  cc := aa + bb;
42  if cc >= 1280 then
43  cc := cc - 1280;
44  end if;
45  return cc(10 downto 0);
46  end function add_mod1280;
47 
48  function sub_mod1280
49  ( a: in bunch_t; b: in bunch_t ) return bunch_t is
50  variable aa : plus1_t;
51  variable bb : plus1_t;
52  variable cc : plus1_t;
53  begin
54  aa := '0' & a;
55  bb := '0' & b;
56  if aa < bb then
57  cc := aa + 1280 - bb;
58  else
59  cc := aa - bb;
60  end if;
61  return cc(10 downto 0);
62  end function sub_mod1280;
63 
64  signal buf_ttpkt : std_logic_vector (11 downto 0) := (others => '0');
65  signal buf_bdata : std_logic_vector (63 downto 0) := (others => '0');
66  signal seq_kick : std_logic_vector (3 downto 0) := (others => '0');
67  signal sig_kick : std_logic := '0';
68  signal buf_kick : std_logic := '0';
69  signal sig_injv : std_logic := '0';
70 
71  signal buf_begloc : std_logic_vector (10 downto 0) := (others => '0');
72  signal buf_injloc : std_logic_vector (10 downto 0) := (others => '0');
73  signal buf_endloc : std_logic_vector (10 downto 0) := (others => '0');
74  signal buf_kicksub : std_logic_vector (10 downto 0) := (others => '0');
75  signal buf_kickloc : std_logic_vector (10 downto 0) := (others => '0');
76  signal buf_vpos : std_logic_vector (10 downto 0) := (others => '0');
77  signal buf_vloc : std_logic_vector (10 downto 0) := (others => '0');
78  signal buf_vpre : std_logic_vector (10 downto 0) := (others => '0');
79  signal buf_vlen : std_logic_vector (10 downto 0) := (others => '0');
80  signal buf_vfull : std_logic_vector (9 downto 0) := (others => '0');
81  signal buf_vgate : std_logic_vector (9 downto 0) := (others => '0');
82  signal cnt_frame : std_logic_vector (9 downto 0) := (others => '0');
83  signal buf_state : std_logic_vector (1 downto 0) := (others => '0');
84 begin
85 
86  -- in
87  buf_ttpkt <= payload(75 downto 64);
88  buf_bdata <= payload(63 downto 0);
89 
90  -- proc
91  proc: process(clock)
92  begin
93  if clock'event and clock = '1' then
94  -- start seq_kick
95  if (sigpayload = '1' and cntpacket = 14 and
96  buf_ttpkt = TTPKT_INJV and buf_state = 0) then
97  seq_kick <= seq_kick(seq_kick'left-1 downto 0) & buf_bdata(63);
98  else
99  seq_kick <= seq_kick(seq_kick'left-1 downto 0) & '0';
100  end if;
101 
102  -- sig_kick, buf_kick, all buf_*** from bdata
103  if seq_kick(0) = '1' then
104  buf_kicksub <= '0' & buf_bdata(9 downto 0);
105  buf_vpos <= buf_bdata(20 downto 10);
106  buf_vpre <= buf_bdata(31 downto 21);
107  buf_vlen <= buf_bdata(42 downto 32);
108  buf_vfull <= buf_bdata(52 downto 43);
109  buf_vgate <= buf_bdata(62 downto 53);
110  end if;
111  if seq_kick(1) = '1' then
112  buf_injloc <= add_mod1280(buf_vpos, revoloc);
113  end if;
114  if seq_kick(2) = '1' then
115  buf_begloc <= sub_mod1280(buf_injloc, buf_vpre);
116  buf_endloc <= add_mod1280(buf_injloc, buf_vlen);
117  --buf_kicksub <= add_mod1280(buf_kicksub, "00000000001");
118  end if;
119  if seq_kick(3) = '1' then
120  buf_vloc <= sub_mod1280(buf_begloc, "00000000001");
121  buf_kickloc <= sub_mod1280(buf_begloc, buf_kicksub);
122  buf_kick <= '1';
123  elsif frameloc = buf_kickloc then
124  sig_kick <= buf_kick;
125  buf_kick <= '0';
126  else
127  sig_kick <= '0';
128  end if;
129 
130  -- buf_state, cnt_frame
131  if seq_kick(3) = '1' then
132  buf_state <= "01";
133  cnt_frame <= "0000000011"; -- 3
134  elsif frameloc = buf_vloc then
135  if cnt_frame /= 0 then
136  cnt_frame <= cnt_frame - 1;
137  elsif buf_state = 1 and buf_vfull /= 0 then
138  cnt_frame <= buf_vfull - 1;
139  buf_state <= "10";
140  elsif buf_state = 1 and buf_vgate /= 0 then
141  cnt_frame <= buf_vgate - 1;
142  buf_state <= "11";
143  elsif buf_state = 2 and buf_vgate /= 0 then
144  cnt_frame <= buf_vgate;
145  buf_state <= "11";
146  else
147  buf_state <= "00";
148  end if;
149  end if;
150 
151  -- sig_injv
152  if buf_state(1) = '1' and frameloc = buf_begloc then
153  sig_injv <= '1';
154  elsif buf_state(1) = buf_state(0) and frameloc = buf_endloc then
155  sig_injv <= '0';
156  end if;
157 
158  end if;
159  end process;
160 
161  -- out
162  injkick <= sig_kick;
163  injveto <= sig_injv;
164  injvpos <= buf_vpos;
165  injvpre <= buf_vpre;
166  injvlen <= buf_vlen;
167  injvfull <= buf_vfull;
168  injvgate <= buf_vgate;
169 
170 
171 end implementation;