Belle II KLM Scint Firmware
1
MeasurePeds.vhd
1
-- Module Name: MeasurePeds - Behavioral
2
-- Create Date: 09/29/2019
3
library
IEEE
;
4
use
IEEE.STD_LOGIC_1164.
ALL
;
5
use
IEEE.NUMERIC_STD.
ALL
;
6
use
IEEE.STD_LOGIC_MISC.
ALL
;
7
use
IEEE.STD_LOGIC_UNSIGNED.
ALL
;
8
Library
work
;
9
use
work.
klm_scint_pkg
.
all
;
10
28
29
30
entity
MeasurePeds
is
31
generic
(
32
LAST_WINDOW_ADDRESS
:
std_logic_vector
(
8
downto
0
)
:=
"111111111"
;
33
N_BITS_AVG_g
:
integer
;
34
ANALOG_STORE_TIME
:
std_logic_vector
(
8
downto
0
)
:=
"000001000"
-- in 32ns (1 win) increments
35
36
)
;
37
Port
(
38
clk
:
in
STD_logic
;
39
ena
:
in
std_logic
:=
'
0
'
;
40
busy
:
out
std_logic
:=
'
0
'
;
41
42
force_trig
:
out
std_logic
:=
'
0
'
;
43
trig_win
:
out
std_logic_vector
(
8
downto
0
)
:=
(
others
=
>
'
0
'
)
;
44
prime_fifos
:
out
std_logic
:=
'
0
'
;
45
summing_ena
:
out
std_logic
:=
'
0
'
;
46
47
cur_win
:
in
std_logic_vector
(
8
downto
0
)
;
48
either_bus_busy
:
in
std_logic
:=
'
0
'
;
49
avg_peds_ena
:
out
std_logic
:=
'
0
'
;
50
avg_peds_busy
:
in
std_logic_vector
(
1
downto
0
)
:=
(
others
=
>
'
0
'
)
;
51
dig_busy
:
in
std_logic
52
-- bus_mask : out std_logic_vector(13 downto 0)
53
54
)
;
55
end
MeasurePeds
;
56
57
architecture
Behavioral
of
MeasurePeds
is
58
59
60
type
MeasurePeds_sm
is
(
61
IDLE
,
62
WINDOW_LOOP
,
63
N_AVG_LOOP
,
64
WAIT_WIN
,
65
WAIT_DIG
,
66
WAIT_SHIFT
,
67
WAIT_FINISH_BEFORE_AVERAGING
,
68
WAIT_AVG_AND_WRITE_PEDS
69
)
;
70
signal
ped_meas_state
:
MeasurePeds_sm
:=
IDLE
;
71
72
signal
ena_i
:
std_logic_vector
(
1
downto
0
)
:=
"00"
;
73
74
constant
T_wait_busy_to_come_up
:
integer
:=
31
;
75
-- constant LAST_WINDOW_ADDRESS : std_logic_vector(8 downto 0) := "111111111";
76
-- constant LAST_WINDOW_ADDRESS : std_logic_vector(8 downto 0) := "000000011";
77
78
begin
79
80
process
(clk, ena)
81
begin
82
if
rising_edge
(
clk
)
then
83
ena_i
(
0
)
<=
ena
;
84
end
if
;
85
end
process
;
86
87
88
process
(clk, ena_i, either_bus_busy, avg_peds_busy, dig_busy)
89
variable
count
:
integer
range
0
to
T_wait_busy_to_come_up
:=
0
;
90
-- variable count16bit : std_logic_vector(16 downto 0);
91
variable
N_avg_counter
:
integer
range
0
to
2
*
*
(
1
+
N_BITS_AVG_g
)
:=
0
;
92
variable
trig_win_i
:
std_logic_vector
(
8
downto
0
)
:=
(
others
=
>
'
0
'
)
;
93
begin
94
if
(
rising_edge
(
clk
)
)
then
95
96
ena_i
(
1
)
<=
ena_i
(
0
)
;
97
trig_win
<=
trig_win_i
;
98
99
case
ped_meas_state
is
100
101
When
IDLE
=
>
102
-- bus_mask <= (others=>'0');
103
if
(
ena_i
=
"01"
)
then
104
busy
<=
'
1
'
;
105
trig_win_i
:=
(
others
=
>
'
0
'
)
;
106
N_avg_counter
:=
0
;
107
-- force_trig <= '1';
108
summing_ena
<=
'
1
'
;
109
prime_fifos
<=
'
1
'
;
-- prime all wave and ped fifos with 32 null vectors
110
ped_meas_state
<=
N_AVG_LOOP
;
111
else
112
busy
<=
'
0
'
;
113
ped_meas_state
<=
IDLE
;
114
end
if
;
115
116
117
When
WINDOW_LOOP
=
>
118
if
trig_win_i
/=
LAST_WINDOW_ADDRESS
then
119
trig_win_i
:=
trig_win_i
+
"000000001"
;
120
summing_ena
<=
'
1
'
;
121
prime_fifos
<=
'
1
'
;
-- prime all wave and ped fifos with 32 null vectors
122
ped_meas_state
<=
N_AVG_LOOP
;
123
else
124
ped_meas_state
<=
IDLE
;
125
end
if
;
126
127
128
When
N_AVG_LOOP
=
>
129
prime_fifos
<=
'
0
'
;
130
if
N_avg_counter
<
2
*
*
N_BITS_AVG_g
then
131
N_avg_counter
:=
N_avg_counter
+
1
;
132
ped_meas_state
<=
WAIT_WIN
;
133
else
134
N_avg_counter
:=
0
;
135
count
:=
0
;
136
ped_meas_state
<=
WAIT_FINISH_BEFORE_AVERAGING
;
137
end
if
;
138
139
140
When
WAIT_WIN
=
>
141
if
(
cur_win
-
trig_win_i
)
=
ANALOG_STORE_TIME
then
142
force_trig
<=
'
1
'
;
143
-- bus_mask <= "10" & trig_win_i & "001"; -- mask(10) | win start | num windows -- wired to SamplingLgc
144
ped_meas_state
<=
WAIT_DIG
;
145
else
146
ped_meas_state
<=
WAIT_WIN
;
147
end
if
;
148
149
150
When
WAIT_DIG
=
>
151
force_trig
<=
'
0
'
;
152
-- bus_mask <= (others=>'0');
153
if
dig_busy
=
'
1
'
or
count
<
T_wait_busy_to_come_up
then
154
count
:=
count
+
1
;
155
ped_meas_state
<=
WAIT_DIG
;
156
else
157
count
:=
0
;
158
-- count16bit := (others=>'0');
159
-- bus_mask <= "11" & trig_win_i & "001"; -- unmask(11) | win start | num windows -- now sampling will continue while we shift out data
160
ped_meas_state
<=
WAIT_SHIFT
;
161
end
if
;
162
163
164
When
WAIT_SHIFT
=
>
165
-- bus_mask <= (others=>'0');
166
if
either_bus_busy
=
'
1
'
or
count
<
T_wait_busy_to_come_up
then
167
-- if count16bit(16) /= '1' then -- can maybe use cur_win from SamplingLgc_i in conjunction with above line instead of larger counter
168
-- count16bit := count16bit + '1';
169
count
:=
count
+
1
;
170
ped_meas_state
<=
WAIT_SHIFT
;
171
else
172
-- count16bit := (others=>'0');
173
count
:=
0
;
174
ped_meas_state
<=
N_AVG_LOOP
;
175
end
if
;
176
177
178
When
WAIT_FINISH_BEFORE_AVERAGING
=
>
179
if
count
<
T_wait_busy_to_come_up
then
180
count
:=
count
+
1
;
181
ped_meas_state
<=
WAIT_FINISH_BEFORE_AVERAGING
;
182
else
183
count
:=
0
;
184
summing_ena
<=
'
0
'
;
185
avg_peds_ena
<=
'
1
'
;
186
ped_meas_state
<=
WAIT_AVG_AND_WRITE_PEDS
;
187
end
if
;
188
189
190
When
WAIT_AVG_AND_WRITE_PEDS
=
>
191
avg_peds_ena
<=
'
0
'
;
192
if
or_reduce
(
avg_peds_busy
)
=
'
1
'
or
count
<
T_wait_busy_to_come_up
then
193
count
:=
count
+
1
;
194
ped_meas_state
<=
WAIT_AVG_AND_WRITE_PEDS
;
195
else
196
count
:=
0
;
197
ped_meas_state
<=
WINDOW_LOOP
;
198
end
if
;
199
200
201
202
end
case
;
203
end
if
;
204
end
process
;
205
206
207
208
209
210
end
Behavioral
;
klm_scint_pkg
Definition:
klm_scint_pkg.vhd:7
MeasurePeds
Definition:
MeasurePeds.vhd:30
klm_scint
source
MeasurePeds.vhd
Generated by
1.8.13