forked from filipamator/vu_meter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_avg.vhd
More file actions
148 lines (124 loc) · 4.18 KB
/
sample_avg.vhd
File metadata and controls
148 lines (124 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
USE IEEE.STD_LOGIC_SIGNED.all;
entity sample_avg is
GENERIC (
d_width : natural := 16;
stage : natural := 4
);
PORT (
clk : IN STD_LOGIC;
data_in_en : IN STD_LOGIC;
reset_n : IN STD_LOGIC;
data_in : IN STD_LOGIC_VECTOR(d_width-1 DOWNTO 0);
data_out : OUT STD_LOGIC_VECTOR(d_width-1 DOWNTO 0);
ce : OUT STD_LOGIC
);
end sample_avg;
architecture Behavioral of sample_avg is
component average2 is
GENERIC (
d_width : natural := 16
);
PORT (
clk : IN STD_LOGIC;
data_in_en : IN STD_LOGIC;
reset_n : IN STD_LOGIC;
data_in : IN STD_LOGIC_VECTOR(d_width-1 DOWNTO 0);
data_out : OUT STD_LOGIC_VECTOR(d_width-1 DOWNTO 0);
ce : OUT STD_LOGIC
);
end component average2;
type vector16 is array (natural range <>) of std_logic_vector(d_width-1 downto 0);
signal s_signal : vector16(stage downto 0);
signal s_ce : std_logic_vector(stage downto 0);
begin
s_signal(0) <= data_in;
s_ce(0) <= data_in_en;
data_out <= s_signal(stage);
ce <= s_ce(stage);
GEN_REG:
for I in 0 to stage-1 generate
REG0: average2
generic map (d_width => d_width)
port map (
clk => clk,
reset_n => reset_n,
data_in_en => s_ce(I),
data_in => s_signal(I),
data_out => s_signal(I+1),
ce => s_ce(I+1)
);
end generate;
end Behavioral;
------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
USE IEEE.STD_LOGIC_SIGNED.all;
entity average2 is
GENERIC (
d_width : natural := 16
);
PORT (
clk : IN STD_LOGIC;
data_in_en : IN STD_LOGIC;
reset_n : IN STD_LOGIC;
data_in : IN STD_LOGIC_VECTOR(d_width-1 DOWNTO 0);
data_out : OUT STD_LOGIC_VECTOR(d_width-1 DOWNTO 0);
ce : OUT STD_LOGIC
);
end average2;
architecture Behavioral of average2 is
SIGNAL sample : STD_LOGIC_VECTOR(d_width-1 DOWNTO 0);
SIGNAL counter : INTEGER RANGE 0 TO 3 := 0;
SIGNAL temp : STD_LOGIC_VECTOR(d_width-1 DOWNTO 0);
begin
data_out <= temp;
PROCESS (clk)
BEGIN
IF (reset_N='0') THEN
counter <= 0;
ELSE
IF (clk'event and clk='1') THEN
IF (data_in_en='1') THEN
cASE (counter) IS
WHEN 1 => counter <= 0;
--ce <= '1';
WHEN OTHERS => counter <= counter + 1;
--ce <= '0';
END CASE;
END IF;
END IF;
END IF;
END PROCESS;
PROCESS (clk)
BEGIN
IF (reset_N='0') THEN
sample <= (others => '0');
temp <= (others => '0');
ce <= '0';
ELSE
IF (clk'event and clk='1') THEN
ce <= '0';
IF (data_in_en='1') THEN
cASE (counter) IS
WHEN 0 => sample <= data_in;
ce <= '0';
WHEN 1 => temp <= std_logic_vector(
resize(
shift_right(
resize(signed(sample),sample'length+2) +
resize(signed(data_in),data_in'length+2)
,1)
,data_in'length)
);
ce <= '1';
WHEN OTHERS => NULL;
END CASE;
END IF;
END IF;
END IF;
END PROCESS;
end Behavioral;