1 |
2 |
amulder |
----------------------------------------------------------------------------------
|
2 |
|
|
-- Company:
|
3 |
|
|
-- Engineer: Aart Mulder
|
4 |
|
|
--
|
5 |
|
|
-- Create Date: 15:02:11 12/13/2012
|
6 |
|
|
-- Design Name: Tiff compression and transmission
|
7 |
|
|
-- Module Name: var_width_RAM - Behavioral
|
8 |
|
|
-- Project Name: CCITT4
|
9 |
|
|
--
|
10 |
|
|
-- Revision:
|
11 |
|
|
-- Revision 0.01 - File Created
|
12 |
|
|
--
|
13 |
|
|
----------------------------------------------------------------------------------
|
14 |
|
|
library IEEE;
|
15 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
16 |
|
|
use IEEE.NUMERIC_STD.ALL;
|
17 |
|
|
|
18 |
|
|
entity var_width_RAM is
|
19 |
|
|
Generic (
|
20 |
|
|
MEM_SIZE_G : integer := 1024;
|
21 |
|
|
MEM_INDEX_WIDTH_G : integer := 10;
|
22 |
|
|
DATA_WIDTH_G : integer := 8
|
23 |
|
|
);
|
24 |
|
|
Port (
|
25 |
|
|
reset_i : in STD_LOGIC;
|
26 |
|
|
clk_i : in STD_LOGIC;
|
27 |
|
|
d1_i : in STD_LOGIC_VECTOR (DATA_WIDTH_G-1 downto 0);
|
28 |
|
|
wr1_i : in STD_LOGIC;
|
29 |
|
|
d2_i : in STD_LOGIC_VECTOR (DATA_WIDTH_G-1 downto 0);
|
30 |
|
|
wr2_i : in STD_LOGIC;
|
31 |
|
|
d3_i : in STD_LOGIC_VECTOR (DATA_WIDTH_G-1 downto 0);
|
32 |
|
|
wr3_i : in STD_LOGIC;
|
33 |
|
|
d4_i : in STD_LOGIC_VECTOR (DATA_WIDTH_G-1 downto 0);
|
34 |
|
|
wr4_i : in STD_LOGIC;
|
35 |
|
|
rd_addr_i : in STD_LOGIC_VECTOR (MEM_INDEX_WIDTH_G-1 downto 0);
|
36 |
|
|
d_o : out STD_LOGIC_VECTOR (DATA_WIDTH_G-1 downto 0);
|
37 |
|
|
used_o : out unsigned (MEM_INDEX_WIDTH_G-1 downto 0)
|
38 |
|
|
);
|
39 |
|
|
end var_width_RAM;
|
40 |
|
|
|
41 |
|
|
architecture Behavioral of var_width_RAM is
|
42 |
|
|
signal rd1, rd2, rd3, rd4, wr1, wr2, wr3, wr4 : std_logic := '0';
|
43 |
|
|
signal rd_addr1, rd_addr2, rd_addr3, rd_addr4, wr_addr1, wr_addr2, wr_addr3, wr_addr4 : std_logic_vector(MEM_INDEX_WIDTH_G-3 downto 0) := (others => '0');
|
44 |
|
|
signal d1_in, d2_in, d3_in, d4_in, d1_out, d2_out, d3_out, d4_out : std_logic_vector(DATA_WIDTH_G-1 downto 0) := (others => '0');
|
45 |
|
|
signal mux_sel : unsigned(1 downto 0) := (others => '0');
|
46 |
|
|
begin
|
47 |
|
|
RAM_ins1 : entity work.MyRAM
|
48 |
|
|
generic map(
|
49 |
|
|
DATA_WIDTH_G => DATA_WIDTH_G,
|
50 |
|
|
MEMORY_SIZE_G => MEM_SIZE_G/4,
|
51 |
|
|
MEMORY_ADDRESS_WIDTH_G => MEM_INDEX_WIDTH_G-2,
|
52 |
|
|
BUFFER_OUTPUT_G => true
|
53 |
|
|
)
|
54 |
|
|
port map(
|
55 |
|
|
clk => clk_i,
|
56 |
|
|
en => '1',
|
57 |
|
|
rd => rd1,
|
58 |
|
|
wr => wr1,
|
59 |
|
|
rd_addr => rd_addr1,
|
60 |
|
|
wr_addr => wr_addr1,
|
61 |
|
|
Data_in => d1_in,
|
62 |
|
|
Data_out => d1_out
|
63 |
|
|
);
|
64 |
|
|
rd_addr1 <= rd_addr_i(MEM_INDEX_WIDTH_G-1 downto 2);
|
65 |
|
|
rd1 <= (not rd_addr_i(1)) and (not rd_addr_i(0));
|
66 |
|
|
|
67 |
|
|
RAM_ins2 : entity work.MyRAM
|
68 |
|
|
generic map(
|
69 |
|
|
DATA_WIDTH_G => DATA_WIDTH_G,
|
70 |
|
|
MEMORY_SIZE_G => MEM_SIZE_G/4,
|
71 |
|
|
MEMORY_ADDRESS_WIDTH_G => MEM_INDEX_WIDTH_G-2,
|
72 |
|
|
BUFFER_OUTPUT_G => true
|
73 |
|
|
)
|
74 |
|
|
port map(
|
75 |
|
|
clk => clk_i,
|
76 |
|
|
en => '1',
|
77 |
|
|
rd => rd2,
|
78 |
|
|
wr => wr2,
|
79 |
|
|
rd_addr => rd_addr2,
|
80 |
|
|
wr_addr => wr_addr2,
|
81 |
|
|
Data_in => d2_in,
|
82 |
|
|
Data_out => d2_out
|
83 |
|
|
);
|
84 |
|
|
rd_addr2 <= rd_addr_i(MEM_INDEX_WIDTH_G-1 downto 2);
|
85 |
|
|
rd2 <= (not rd_addr_i(1)) and (rd_addr_i(0));
|
86 |
|
|
|
87 |
|
|
RAM_ins3 : entity work.MyRAM
|
88 |
|
|
generic map(
|
89 |
|
|
DATA_WIDTH_G => DATA_WIDTH_G,
|
90 |
|
|
MEMORY_SIZE_G => MEM_SIZE_G/4,
|
91 |
|
|
MEMORY_ADDRESS_WIDTH_G => MEM_INDEX_WIDTH_G-2,
|
92 |
|
|
BUFFER_OUTPUT_G => true
|
93 |
|
|
)
|
94 |
|
|
port map(
|
95 |
|
|
clk => clk_i,
|
96 |
|
|
en => '1',
|
97 |
|
|
rd => rd3,
|
98 |
|
|
wr => wr3,
|
99 |
|
|
rd_addr => rd_addr3,
|
100 |
|
|
wr_addr => wr_addr3,
|
101 |
|
|
Data_in => d3_in,
|
102 |
|
|
Data_out => d3_out
|
103 |
|
|
);
|
104 |
|
|
rd_addr3 <= rd_addr_i(MEM_INDEX_WIDTH_G-1 downto 2);
|
105 |
|
|
rd3 <= (rd_addr_i(1)) and (not rd_addr_i(0));
|
106 |
|
|
|
107 |
|
|
RAM_ins4 : entity work.MyRAM
|
108 |
|
|
generic map(
|
109 |
|
|
DATA_WIDTH_G => DATA_WIDTH_G,
|
110 |
|
|
MEMORY_SIZE_G => MEM_SIZE_G/4,
|
111 |
|
|
MEMORY_ADDRESS_WIDTH_G => MEM_INDEX_WIDTH_G-2,
|
112 |
|
|
BUFFER_OUTPUT_G => true
|
113 |
|
|
)
|
114 |
|
|
port map(
|
115 |
|
|
clk => clk_i,
|
116 |
|
|
en => '1',
|
117 |
|
|
rd => rd4,
|
118 |
|
|
wr => wr4,
|
119 |
|
|
rd_addr => rd_addr4,
|
120 |
|
|
wr_addr => wr_addr4,
|
121 |
|
|
Data_in => d4_in,
|
122 |
|
|
Data_out => d4_out
|
123 |
|
|
);
|
124 |
|
|
rd_addr4 <= rd_addr_i(MEM_INDEX_WIDTH_G-1 downto 2);
|
125 |
|
|
rd4 <= (rd_addr_i(1)) and (rd_addr_i(0));
|
126 |
|
|
|
127 |
|
|
--Multiplexer that selects the RAM output data
|
128 |
|
|
d_o <= d1_out when rd_addr_i(1 downto 0) = "00"
|
129 |
|
|
else d2_out when rd_addr_i(1 downto 0) = "01"
|
130 |
|
|
else d3_out when rd_addr_i(1 downto 0) = "10"
|
131 |
|
|
else d4_out when rd_addr_i(1 downto 0) = "11";
|
132 |
|
|
|
133 |
|
|
--The multiplexer selection counter
|
134 |
|
|
mux_sel_cnt_process : process(clk_i)
|
135 |
|
|
begin
|
136 |
|
|
if clk_i'event and clk_i = '0' then
|
137 |
|
|
-- "0000" => 0
|
138 |
|
|
-- "0001" => 1
|
139 |
|
|
-- "0011" => 2
|
140 |
|
|
-- "0111" => 3
|
141 |
|
|
-- "1111" => 4 = 0
|
142 |
|
|
|
143 |
|
|
if reset_i = '1' then
|
144 |
|
|
mux_sel <= (others => '0');
|
145 |
|
|
-- if wr4_i = '0' and wr3_i = '0' and wr2_i = '0' and wr1_i = '0' then
|
146 |
|
|
-- mux_sel <= mux_sel - to_unsigned(0,2);
|
147 |
|
|
elsif wr4_i = '0' and wr3_i = '0' and wr2_i = '0' and wr1_i = '1' then
|
148 |
|
|
mux_sel <= mux_sel - to_unsigned(1,2);
|
149 |
|
|
elsif wr4_i = '0' and wr3_i = '0' and wr2_i = '1' and wr1_i = '1' then
|
150 |
|
|
mux_sel <= mux_sel - to_unsigned(2,2);
|
151 |
|
|
elsif wr4_i = '0' and wr3_i = '1' and wr2_i = '1' and wr1_i = '1' then
|
152 |
|
|
mux_sel <= mux_sel - to_unsigned(3,2);
|
153 |
|
|
-- elsif wr4_i = '1' and wr3_i = '1' and wr2_i = '1' and wr1_i = '1' then
|
154 |
|
|
-- mux_sel <= mux_sel - to_unsigned(4,2);
|
155 |
|
|
else
|
156 |
|
|
mux_sel <= mux_sel - to_unsigned(0,2);
|
157 |
|
|
end if;
|
158 |
|
|
end if;
|
159 |
|
|
end process mux_sel_cnt_process;
|
160 |
|
|
|
161 |
|
|
--This process controls the read addresses, i.e. counting up and reseting.
|
162 |
|
|
rd_addr_cnt_process : process(clk_i)
|
163 |
|
|
begin
|
164 |
|
|
if clk_i'event and clk_i = '1' then
|
165 |
|
|
if reset_i = '1' then
|
166 |
|
|
wr_addr1 <= (others => '0');
|
167 |
|
|
wr_addr2 <= (others => '0');
|
168 |
|
|
wr_addr3 <= (others => '0');
|
169 |
|
|
wr_addr4 <= (others => '0');
|
170 |
|
|
else
|
171 |
|
|
if wr1 = '1' then
|
172 |
|
|
wr_addr1 <= std_logic_vector(unsigned(wr_addr1) + to_unsigned(1, MEM_INDEX_WIDTH_G - 3));
|
173 |
|
|
end if;
|
174 |
|
|
if wr2 = '1' then
|
175 |
|
|
wr_addr2 <= std_logic_vector(unsigned(wr_addr2) + to_unsigned(1, MEM_INDEX_WIDTH_G - 3));
|
176 |
|
|
end if;
|
177 |
|
|
if wr3 = '1' then
|
178 |
|
|
wr_addr3 <= std_logic_vector(unsigned(wr_addr3) + to_unsigned(1, MEM_INDEX_WIDTH_G - 3));
|
179 |
|
|
end if;
|
180 |
|
|
if wr4 = '1' then
|
181 |
|
|
wr_addr4 <= std_logic_vector(unsigned(wr_addr4) + to_unsigned(1, MEM_INDEX_WIDTH_G - 3));
|
182 |
|
|
end if;
|
183 |
|
|
end if;
|
184 |
|
|
end if;
|
185 |
|
|
end process rd_addr_cnt_process;
|
186 |
|
|
|
187 |
|
|
--Multiplexer 1(input to RAM 1)
|
188 |
|
|
-- wr1 <= wr1_i when (mux_sel + to_unsigned(0,2)) = to_unsigned(0,2)
|
189 |
|
|
-- else wr2_i when (mux_sel + to_unsigned(0,2)) = to_unsigned(1,2)
|
190 |
|
|
-- else wr3_i when (mux_sel + to_unsigned(0,2)) = to_unsigned(2,2)
|
191 |
|
|
-- else wr4_i when (mux_sel + to_unsigned(0,2)) = to_unsigned(3,2);
|
192 |
|
|
-- d1_in <= d1_i when (mux_sel + to_unsigned(0,2)) = to_unsigned(0,2)
|
193 |
|
|
-- else d2_i when (mux_sel + to_unsigned(0,2)) = to_unsigned(1,2)
|
194 |
|
|
-- else d3_i when (mux_sel + to_unsigned(0,2)) = to_unsigned(2,2)
|
195 |
|
|
-- else d4_i when (mux_sel + to_unsigned(0,2)) = to_unsigned(3,2);
|
196 |
|
|
mux1_process : process(clk_i)
|
197 |
|
|
begin
|
198 |
|
|
if clk_i'event and clk_i = '0' then
|
199 |
|
|
if (mux_sel + to_unsigned(0,2)) = to_unsigned(0,2) then
|
200 |
|
|
wr1 <= wr1_i;
|
201 |
|
|
d1_in <= d1_i;
|
202 |
|
|
elsif (mux_sel + to_unsigned(0,2)) = to_unsigned(1,2) then
|
203 |
|
|
wr1 <= wr2_i;
|
204 |
|
|
d1_in <= d2_i;
|
205 |
|
|
elsif (mux_sel + to_unsigned(0,2)) = to_unsigned(2,2) then
|
206 |
|
|
wr1 <= wr3_i;
|
207 |
|
|
d1_in <= d3_i;
|
208 |
|
|
elsif (mux_sel + to_unsigned(0,2)) = to_unsigned(3,2) then
|
209 |
|
|
wr1 <= wr4_i;
|
210 |
|
|
d1_in <= d4_i;
|
211 |
|
|
end if;
|
212 |
|
|
end if;
|
213 |
|
|
end process mux1_process;
|
214 |
|
|
|
215 |
|
|
--Multiplexer 2(input to RAM 2)
|
216 |
|
|
-- wr2 <= wr1_i when (mux_sel + to_unsigned(1,2)) = to_unsigned(0,2)
|
217 |
|
|
-- else wr2_i when (mux_sel + to_unsigned(1,2)) = to_unsigned(1,2)
|
218 |
|
|
-- else wr3_i when (mux_sel + to_unsigned(1,2)) = to_unsigned(2,2)
|
219 |
|
|
-- else wr4_i when (mux_sel + to_unsigned(1,2)) = to_unsigned(3,2);
|
220 |
|
|
-- d2_in <= d1_i when (mux_sel + to_unsigned(1,2)) = to_unsigned(0,2)
|
221 |
|
|
-- else d2_i when (mux_sel + to_unsigned(1,2)) = to_unsigned(1,2)
|
222 |
|
|
-- else d3_i when (mux_sel + to_unsigned(1,2)) = to_unsigned(2,2)
|
223 |
|
|
-- else d4_i when (mux_sel + to_unsigned(1,2)) = to_unsigned(3,2);
|
224 |
|
|
mux2_process : process(clk_i)
|
225 |
|
|
begin
|
226 |
|
|
if clk_i'event and clk_i = '0' then
|
227 |
|
|
if (mux_sel + to_unsigned(1,2)) = to_unsigned(0,2) then
|
228 |
|
|
wr2 <= wr1_i;
|
229 |
|
|
d2_in <= d1_i;
|
230 |
|
|
elsif (mux_sel + to_unsigned(1,2)) = to_unsigned(1,2) then
|
231 |
|
|
wr2 <= wr2_i;
|
232 |
|
|
d2_in <= d2_i;
|
233 |
|
|
elsif (mux_sel + to_unsigned(1,2)) = to_unsigned(2,2) then
|
234 |
|
|
wr2 <= wr3_i;
|
235 |
|
|
d2_in <= d3_i;
|
236 |
|
|
elsif (mux_sel + to_unsigned(1,2)) = to_unsigned(3,2) then
|
237 |
|
|
wr2 <= wr4_i;
|
238 |
|
|
d2_in <= d4_i;
|
239 |
|
|
end if;
|
240 |
|
|
end if;
|
241 |
|
|
end process mux2_process;
|
242 |
|
|
|
243 |
|
|
--Multiplexer 3(input to RAM 3)
|
244 |
|
|
-- wr3 <= wr1_i when (mux_sel + to_unsigned(2,2)) = to_unsigned(0,2)
|
245 |
|
|
-- else wr2_i when (mux_sel + to_unsigned(2,2)) = to_unsigned(1,2)
|
246 |
|
|
-- else wr3_i when (mux_sel + to_unsigned(2,2)) = to_unsigned(2,2)
|
247 |
|
|
-- else wr4_i when (mux_sel + to_unsigned(2,2)) = to_unsigned(3,2);
|
248 |
|
|
-- d3_in <= d1_i when (mux_sel + to_unsigned(2,2)) = to_unsigned(0,2)
|
249 |
|
|
-- else d2_i when (mux_sel + to_unsigned(2,2)) = to_unsigned(1,2)
|
250 |
|
|
-- else d3_i when (mux_sel + to_unsigned(2,2)) = to_unsigned(2,2)
|
251 |
|
|
-- else d4_i when (mux_sel + to_unsigned(2,2)) = to_unsigned(3,2);
|
252 |
|
|
mux3_process : process(clk_i)
|
253 |
|
|
begin
|
254 |
|
|
if clk_i'event and clk_i = '0' then
|
255 |
|
|
if (mux_sel + to_unsigned(2,2)) = to_unsigned(0,2) then
|
256 |
|
|
wr3 <= wr1_i;
|
257 |
|
|
d3_in <= d1_i;
|
258 |
|
|
elsif (mux_sel + to_unsigned(2,2)) = to_unsigned(1,2) then
|
259 |
|
|
wr3 <= wr2_i;
|
260 |
|
|
d3_in <= d2_i;
|
261 |
|
|
elsif (mux_sel + to_unsigned(2,2)) = to_unsigned(2,2) then
|
262 |
|
|
wr3 <= wr3_i;
|
263 |
|
|
d3_in <= d3_i;
|
264 |
|
|
elsif (mux_sel + to_unsigned(2,2)) = to_unsigned(3,2) then
|
265 |
|
|
wr3 <= wr4_i;
|
266 |
|
|
d3_in <= d4_i;
|
267 |
|
|
end if;
|
268 |
|
|
end if;
|
269 |
|
|
end process mux3_process;
|
270 |
|
|
|
271 |
|
|
--Multiplexer 4(input to RAM 4)
|
272 |
|
|
-- wr4 <= wr1_i when (mux_sel + to_unsigned(3,2)) = to_unsigned(0,2)
|
273 |
|
|
-- else wr2_i when (mux_sel + to_unsigned(3,2)) = to_unsigned(1,2)
|
274 |
|
|
-- else wr3_i when (mux_sel + to_unsigned(3,2)) = to_unsigned(2,2)
|
275 |
|
|
-- else wr4_i when (mux_sel + to_unsigned(3,2)) = to_unsigned(3,2);
|
276 |
|
|
-- d4_in <= d1_i when (mux_sel + to_unsigned(3,2)) = to_unsigned(0,2)
|
277 |
|
|
-- else d2_i when (mux_sel + to_unsigned(3,2)) = to_unsigned(1,2)
|
278 |
|
|
-- else d3_i when (mux_sel + to_unsigned(3,2)) = to_unsigned(2,2)
|
279 |
|
|
-- else d4_i when (mux_sel + to_unsigned(3,2)) = to_unsigned(3,2);
|
280 |
|
|
mux4_process : process(clk_i)
|
281 |
|
|
begin
|
282 |
|
|
if clk_i'event and clk_i = '0' then
|
283 |
|
|
if (mux_sel + to_unsigned(3,2)) = to_unsigned(0,2) then
|
284 |
|
|
wr4 <= wr1_i;
|
285 |
|
|
d4_in <= d1_i;
|
286 |
|
|
elsif (mux_sel + to_unsigned(3,2)) = to_unsigned(1,2) then
|
287 |
|
|
wr4 <= wr2_i;
|
288 |
|
|
d4_in <= d2_i;
|
289 |
|
|
elsif (mux_sel + to_unsigned(3,2)) = to_unsigned(2,2) then
|
290 |
|
|
wr4 <= wr3_i;
|
291 |
|
|
d4_in <= d3_i;
|
292 |
|
|
elsif (mux_sel + to_unsigned(3,2)) = to_unsigned(3,2) then
|
293 |
|
|
wr4 <= wr4_i;
|
294 |
|
|
d4_in <= d4_i;
|
295 |
|
|
end if;
|
296 |
|
|
end if;
|
297 |
|
|
end process mux4_process;
|
298 |
|
|
|
299 |
|
|
used_o <= ("00" & unsigned(wr_addr1)) + ("00" & unsigned(wr_addr2)) + ("00" & unsigned(wr_addr3)) + ("00" & unsigned(wr_addr4));
|
300 |
|
|
|
301 |
|
|
end Behavioral;
|