-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_reti_logiche.vhd
525 lines (478 loc) · 17.4 KB
/
project_reti_logiche.vhd
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 03/23/2021 10:20:46 PM
-- Design Name:
-- Module Name: project_reti_logiche - Behavioral
-- Project Name:
-- Target Devices:
-- Tool Versions:
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx leaf cells in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity datapath_addr is
port (
in_dim1 : in std_logic_vector (7 downto 0); --uscita del dim1
in_dim2 : in std_logic_vector (7 downto 0); --uscita del dim2
in_reg_counter : in std_logic_vector (15 downto 0); --uscita reg_counter
in_reg_dim : in std_logic_vector (15 downto 0); --uscita reg_dim
in_reg_counter_sel : in std_logic; --ingresso mux counter_sel
in_reg_addr : in std_logic_vector (15 downto 0); --uscita del reg_addr
in_addr_sel : in std_logic; --selettore mux a monte del reg_addr
in_o_addr_sel : in std_logic;
in_fsm_o_addr : in std_logic_vector (15 downto 0);
in_fsm_sel : in std_logic;
--in_fsm_done : in std_logic;
--in_fsm_done_sel : in std_logic;
out_reg_counter : out std_logic_vector (15 downto 0); --ingresso reg_counter
out_end_loop : out std_logic;
out_reg_dim : out std_logic_vector (15 downto 0); --uscita moltiplicatore, ingresso reg_dim
out_reg_addr : out std_logic_vector (15 downto 0);
o_addr : out std_logic_vector (15 downto 0);
out_done : out std_logic
--o_temp : out std_logic
);
end datapath_addr;
architecture Behavioral of datapath_addr is
signal new_o_addr : std_logic_vector (15 downto 0); --segnale uscita sommatore
signal diff_temp : UNSIGNED (15 downto 0); --uscita sottrattore
signal offset : std_logic_vector (15 downto 0);
signal o_addr_temp : std_logic_vector (15 downto 0) := (others => '0');
signal tmp_done : std_logic := '0';
begin
out_reg_dim <= std_logic_vector(UNSIGNED(in_dim1) * UNSIGNED(in_dim2));
out_reg_counter <= in_reg_dim when in_reg_counter_sel = '0' else
std_logic_vector(diff_temp);
diff_temp <= (UNSIGNED(in_reg_counter)-1);
out_end_loop <= '1' when in_reg_counter = "0000000000000000" else
'0';
--------------------------------------------------------------------------------
new_o_addr <= std_logic_vector(UNSIGNED(in_reg_addr) + 1);
out_reg_addr <= new_o_addr when in_addr_sel = '0' else
"0000000000000001";
offset <= std_logic_vector(UNSIGNED(in_reg_dim) + UNSIGNED(new_o_addr));
o_addr_temp <= new_o_addr when in_o_addr_sel = '0' else
offset;
o_addr <= o_addr_temp when in_fsm_sel = '1' else
in_fsm_o_addr;
tmp_done <= '1' when o_addr_temp = std_logic_vector(1+(UNSIGNED(in_reg_dim)+UNSIGNED(in_reg_dim))) else '0';
out_done <= tmp_done;
end Behavioral;
---------------------------------------------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity datapath1 is
port (
in1 : in std_logic_vector (7 downto 0); --uscita del reg1
in_min : in std_logic_vector (7 downto 0); --uscita di reg_min
in_max : in std_logic_vector (7 downto 0); --uscita di reg_max
in_max_min_sel : in std_logic; --selettore multiplexer
out_max : out std_logic_vector (7 downto 0); --ingresso di reg_max
out_min : out std_logic_vector (7 downto 0); --ingresso di reg_min
out_delta : out std_logic_vector (7 downto 0) --ingresso di reg_delta
);
end datapath1;
architecture Behavioral of datapath1 is
signal new_max : UNSIGNED(7 downto 0);
signal new_min : UNSIGNED(7 downto 0);
begin
new_max <= UNSIGNED(in1) when UNSIGNED(in1) > UNSIGNED(in_max) else
UNSIGNED(in_max);
new_min <= UNSIGNED(in1) when UNSIGNED(in1) < UNSIGNED(in_min) else
UNSIGNED(in_min);
out_max <= std_logic_vector (new_max) when in_max_min_sel = '1' else
"00000000";
out_min <= std_logic_vector (new_min) when in_max_min_sel = '1' else
"11111111";
out_delta <= std_logic_vector(UNSIGNED(in_max) - UNSIGNED(in_min));
end Behavioral;
---------------------------------------------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity datapath3 is
port (
in1 : in std_logic_vector (7 downto 0); --uscita del reg1
in_reg_min : in std_logic_vector (7 downto 0); --uscita di reg_min
in_reg_delta : in std_logic_vector (7 downto 0); --uscita del reg_delta
in_reg_shift : in std_logic_vector (7 downto 0); --uscita di reg_shift
out_reg_shift : out std_logic_vector (7 downto 0); --ingresso di reg_shift
o_data : out std_logic_vector (7 downto 0) --o_data
);
end datapath3;
architecture Behavioral of datapath3 is
signal tmp_pixel : UNSIGNED(7 downto 0);
signal overflow : UNSIGNED(7 downto 0);
begin
out_reg_shift <= "00001000" when in_reg_delta = "00000000" else --shift = 8 se delta = 0
"00000111" when in_reg_delta = "00000001" or in_reg_delta = "00000010" else --shift = 7 se delta = 1 o delta = 2
"00000110" when in_reg_delta > "00000010" and in_reg_delta < "00000111" else --shift = 6 se 2 < delta < 7
"00000101" when in_reg_delta > "00000110" and in_reg_delta < "00001111" else --shift = 5 se 6 < delta < 15
"00000100" when in_reg_delta > "00001110" and in_reg_delta < "00011111" else --shift = 4 se 14 < delta < 31
"00000011" when in_reg_delta > "00011110" and in_reg_delta < "00111111" else --shift = 3 se 30 < delta < 63
"00000010" when in_reg_delta > "00111110" and in_reg_delta < "01111111" else --shift = 2 se 62 < delta < 127
"00000001" when in_reg_delta > "01111110" and in_reg_delta < "11111111" else --shift = 1 se 126 < delta < 255
"00000000"; --shift = 0 se delta >=255
tmp_pixel <= (UNSIGNED(in1) - UNSIGNED(in_reg_min));
overflow <= "00000000" when in_reg_shift = "00000000" else
"10000000" when in_reg_shift = "00000001" else
"11000000" when in_reg_shift = "00000010" else
"11100000" when in_reg_shift = "00000011" else
"11110000" when in_reg_shift = "00000100" else
"11111000" when in_reg_shift = "00000101" else
"11111100" when in_reg_shift = "00000110" else
"11111110" when in_reg_shift = "00000111" else
"11111111"; --when in_reg_shift = "00001000" else
o_data <= std_logic_vector (shift_left(tmp_pixel, TO_INTEGER(UNSIGNED(in_reg_shift)))) when (tmp_pixel and overflow) = "00000000" else
"11111111";
end Behavioral;
-----------------------------------------------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity project_reti_logiche is
port (
i_clk : in std_logic;
i_rst : in std_logic;
i_start : in std_logic;
i_data : in std_logic_vector(7 downto 0);
o_address : out std_logic_vector(15 downto 0);
o_done : out std_logic;
o_en : out std_logic;
o_we : out std_logic;
o_data : out std_logic_vector (7 downto 0)
);
end project_reti_logiche;
architecture Behavioral of project_reti_logiche is
--------------------------------------------------FSM states----------------------------------------------------------------------------------
type state_type is (S0, S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14, S15, S16, S1_bis, S2_bis, S4_bis, S5_bis, S12_bis, S13_bis, S14_bis, S15_bis, S16_bis);
--------------------------------------------------signals declaration---------------------------------------------------------------------------
--------------------------------------------------fsm signals--------------------------------------------------------------------------
signal next_state, current_state: state_type;
---------------------------------------------------datahpath signals-------------------------------------------------------------
signal tmp_done, end_loop : std_logic ;
--------------------------------------------------registers' signals------------------------------------
signal reg1_load, reg_delta_load, reg_max_load, reg_min_load, reg_shift_load : std_logic := '0';
signal reg_dim1_load, reg_dim2_load, reg_dim_load, reg_addr_load, reg_counter_load : std_logic := '0';
signal reg1_out, reg_max_out, reg_delta_out, reg_min_out, reg_dim1_out, reg_dim2_out, reg_shift_out : std_logic_vector (7 downto 0);
signal reg_max_in, reg_delta_in, reg_min_in, reg_shift_in : std_logic_vector (7 downto 0);
signal reg_dim_out, reg_counter_out, reg_addr_out : std_logic_vector (15 downto 0);
signal reg_dim_in, reg_counter_in, reg_addr_in : std_logic_vector (15 downto 0);
-----------------------------------------------------multiplexer selection signals--------------------------------------------------------------
signal max_min_sel, reg_counter_sel, addr_sel, o_addr_sel, fsm_sel : std_logic;
------------------------------------------------------fsm out signals---------------------------------------------------------------------------
signal fsm_o_addr : std_logic_vector (15 downto 0);
---------------------------------------------------end of signal declaration---------------------------------------------------------------------
--------------------------------------------------component declaration--------------------------------------------------------------------------
component datapath1 is
port (
in1 : in std_logic_vector (7 downto 0); --uscita del reg1
in_min : in std_logic_vector (7 downto 0); --uscita di reg_min
in_max : in std_logic_vector (7 downto 0); --uscita di reg_max
in_max_min_sel : in std_logic; --selettore multiplexer
out_max : out std_logic_vector (7 downto 0); --ingresso di reg_max
out_min : out std_logic_vector (7 downto 0); --ingresso di reg_min
out_delta : out std_logic_vector (7 downto 0) --ingresso di reg_delta
);
end component;
component datapath_addr is
port (
in_dim1 : in std_logic_vector (7 downto 0); --uscita del dim1
in_dim2 : in std_logic_vector (7 downto 0); --uscita del dim2
in_reg_counter : in std_logic_vector (15 downto 0); --uscita reg_counter
in_reg_dim : in std_logic_vector (15 downto 0); --uscita reg_dim
in_reg_counter_sel : in std_logic; --ingresso mux counter_sel
in_reg_addr : in std_logic_vector (15 downto 0); --uscita del reg_addr
in_addr_sel : in std_logic; --selettore mux a monte del reg_addr
in_o_addr_sel : in std_logic;
in_fsm_o_addr : in std_logic_vector (15 downto 0);
in_fsm_sel : in std_logic;
out_reg_counter : out std_logic_vector (15 downto 0); --ingresso reg_counter
out_end_loop : out std_logic;
out_reg_dim : out std_logic_vector (15 downto 0); --uscita moltiplicatore, ingresso reg_dim
out_reg_addr : out std_logic_vector (15 downto 0);
o_addr : out std_logic_vector (15 downto 0);
out_done : out std_logic
);
end component;
component datapath3 is
port (
in1 : in std_logic_vector (7 downto 0); --uscita del reg1
in_reg_min : in std_logic_vector (7 downto 0); --uscita di reg_min
in_reg_delta : in std_logic_vector (7 downto 0); --uscita del reg_delta
in_reg_shift : in std_logic_vector (7 downto 0); --uscita di reg_shift
out_reg_shift : out std_logic_vector (7 downto 0); --ingresso di reg_shift
o_data : out std_logic_vector (7 downto 0) --o_data
);
end component;
--------------------------------------------------end of component declaration------------------------------------------------------------------
begin
-----------------------------------------------------------dataflow-----------------------------------------------------------------------------
p1: datapath1
port map(reg1_out, reg_min_out, reg_max_out, max_min_sel, reg_max_in, reg_min_in, reg_delta_in);
p2: datapath_addr
port map(reg_dim1_out, reg_dim2_out, reg_counter_out, reg_dim_out, reg_counter_sel, reg_addr_out, addr_sel, o_addr_sel, fsm_o_addr, fsm_sel,
reg_counter_in, end_loop, reg_dim_in, reg_addr_in, o_address, tmp_done);
p3: datapath3
port map(reg1_out, reg_min_out, reg_delta_out, reg_shift_out, reg_shift_in, o_data);
-------------------------------------------------------------------end dataflow----------------------------------------
registri: process(i_clk,i_rst)
begin
if i_rst = '1' then
reg1_out <= (others => '0');
reg_max_out <= (others => '0');
reg_delta_out<= (others => '0');
reg_min_out <= (others => '0');
reg_dim1_out <= (others => '0');
reg_dim2_out <= (others => '0');
reg_shift_out <= (others => '0');
reg_dim_out <= (others => '0');
reg_counter_out <= (others => '0');
reg_addr_out <= (others => '0');
elsif i_clk = '1' and i_clk'event then
if reg1_load = '1' then
reg1_out <= i_data;
end if;
if reg_delta_load = '1' then
reg_delta_out <= reg_delta_in;
end if;
if reg_max_load = '1' then
reg_max_out <= reg_max_in;
end if;
if reg_min_load = '1' then
reg_min_out <= reg_min_in;
end if;
if reg_shift_load = '1' then
reg_shift_out <= reg_shift_in;
end if;
if reg_dim1_load = '1' then
reg_dim1_out <= i_data;
end if;
if reg_dim2_load = '1' then
reg_dim2_out <= i_data;
end if;
if reg_dim_load = '1' then
reg_dim_out <= reg_dim_in;
end if;
if reg_addr_load = '1' then
reg_addr_out <= reg_addr_in;
end if;
if reg_counter_load = '1' then
reg_counter_out <= reg_counter_in;
end if;
end if;
end process;
state_reg: process(i_clk, i_rst)
begin
if i_rst='1' then
current_state <= S0;
elsif i_clk = '1' and i_clk'event then
current_state <= next_state;
end if;
end process;
next_stato: process(current_state, i_start, end_loop, tmp_done, reg_dim_out)
begin
next_state <= current_state;
case current_state is
when S0 =>
if i_start='1' then
next_state <= S1;
end if;
when S1 =>
next_state <= S1_bis;
when S1_bis =>
next_state <= S2;
when S2 =>
next_state <= S2_bis;
when S2_bis =>
next_state <= S3;
when S3 =>
next_state <= S4;
when S4 =>
if reg_dim_out="0000000000000000" then
next_state <= S16;
else
next_state <= S4_bis;
end if;
when S4_bis =>
next_state <= S5;
when S5 =>
next_state <= S5_bis;
when S5_bis =>
next_state <= S6;
when S6 =>
if end_loop = '1' then
next_state <=S8;
else
next_state <= S7;
end if;
when S7 =>
next_state <= S6;
when S8 =>
next_state <= S9;
when S9 =>
next_state <= S10;
when S10 =>
next_state <= S11;
when S11 =>
next_state <= S12;
when S12 =>
next_state <= S12_bis;
when S12_bis =>
next_state <= S13;
when S13 =>
next_state <= S13_bis;
when S13_bis =>
next_state <= S14;
when S14 =>
if tmp_done = '1' then
next_state <= S16;
else
next_state <= S14_bis;
end if;
when S14_bis =>
if tmp_done = '1' then
next_state <= S16;
else
next_state <= S15;
end if;
when S15 =>
next_state <= S15_bis;
when S15_bis =>
next_state <= S13;
when S16 =>
next_state <= S16_bis;
when S16_bis =>
if i_start = '0' then
next_state <= S0;
end if;
end case;
end process;
fsm_out: process(current_state, tmp_done)
begin
reg_dim1_load <= '0';
o_en <= '1';
o_we <= '0';
reg_dim2_load <= '0';
fsm_sel <= '1';
reg_dim_load <= '0';
addr_sel <= '0';
reg_addr_load <= '0';
reg_counter_load <= '0';
reg1_load <= '0';
reg_counter_sel <= '0';
max_min_sel <= '0';
reg_max_load <= '0';
reg_min_load <= '0';
reg_delta_load <= '0';
reg_shift_load <= '0';
o_addr_sel <= '0';
fsm_o_addr <= "0000000000000000";
o_done <= '0';
case current_state is
when S0 =>
o_en <= '0';
when S1 =>
fsm_sel <= '0';
reg_dim1_load <= '1';
fsm_o_addr <= ( others => '0');
when S1_bis =>
fsm_sel <= '0';
reg_dim1_load <= '1';
fsm_o_addr <= ( others => '0');
when S2 =>
fsm_sel <= '0';
reg_dim2_load <= '1';
fsm_o_addr <= ( 15 downto 1 => '0', others => '1');
when S2_bis =>
fsm_sel <= '0';
reg_dim2_load <= '1';
fsm_o_addr <= ( 15 downto 1 => '0', others => '1');
when S3 =>
reg_dim_load <= '1';
when S4 =>
addr_sel <= '1';
reg_addr_load <= '1';
reg_counter_load <= '1';
when S4_bis =>
addr_sel <= '1';
reg_addr_load <= '1';
reg_counter_load <= '1';
when S5 =>
reg1_load <= '1';
reg_max_load <= '1';
reg_min_load <= '1';
reg_counter_sel <= '1';
when S5_bis =>
reg1_load <= '1';
reg_max_load <= '1';
reg_min_load <= '1';
reg_counter_sel <= '1';
when S6 =>
reg_counter_sel <= '1';
reg_max_load <= '1';
reg_min_load <= '1';
reg_addr_load <= '1';
max_min_sel <= '1';
when S7 =>
reg_counter_sel <= '1';
reg_max_load <= '1';
reg_min_load <= '1';
max_min_sel <= '1';
reg_counter_load <= '1';
reg1_load <= '1';
when S8 =>
o_en <= '0';
when S9 =>
reg_delta_load <= '1';
when S10 =>
reg_shift_load <= '1';
when S11 =>
reg_shift_load <= '0';
when S12 =>
addr_sel <= '1';
reg_addr_load <= '1';
when S12_bis =>
addr_sel <= '1';
reg_addr_load <= '1';
when S13 =>
reg1_load <= '1';
when S13_bis =>
reg1_load <= '1';
when S14 =>
o_we <= '1';
o_addr_sel <= '1';
when S14_bis =>
o_we <= '1';
o_addr_sel <= '1';
o_done <= tmp_done;
when S15 =>
reg_addr_load <= '1';
when S15_bis =>
when S16 =>
o_done <= '1';
o_en <= '0';
when S16_bis =>
o_done <= '1';
o_en <= '0';
end case;
end process;
end Behavioral;