-
Notifications
You must be signed in to change notification settings - Fork 0
/
Progetto.vhd
704 lines (592 loc) · 19.8 KB
/
Progetto.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.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 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
component datapathRL is --component è un copia incolla dell'entity di DATAPATH. qua sto istanziando il compnente
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_data : out STD_LOGIC_VECTOR (7 downto 0);
o_addr : out STD_LOGIC_VECTOR (15 downto 0);
r1_load : in STD_LOGIC;
r2_load : in STD_LOGIC;
rsu_load : in STD_LOGIC;
rsu1_load : in STD_LOGIC;
rco_load : in STD_LOGIC;
rma_load : in STD_LOGIC;
rm_load : in STD_LOGIC;
rt_load : in STD_LOGIC;
rTS_load : in STD_LOGIC;
r_sel2 : in STD_LOGIC;
iniz_sel : in STD_LOGIC;
mult_sel : in STD_LOGIC;
addr_sel : in STD_LOGIC;
compmin_sel : in STD_LOGIC;
r_sel : in STD_LOGIC;
o_end : out STD_LOGIC;
finish : out STD_LOGIC;
CZ : out STD_LOGIC);
end component;
signal r1_load : STD_LOGIC;
signal r2_load : STD_LOGIC;
signal rsu_load : STD_LOGIC;
signal rsu1_load : STD_LOGIC;
signal rco_load : STD_LOGIC;
signal rma_load : STD_LOGIC;
signal rm_load : STD_LOGIC;
signal rpt_load : STD_LOGIC;
signal rd_load : STD_LOGIC;
signal rs_load : STD_LOGIC;
signal rt_load : STD_LOGIC;
signal rTS_load : STD_LOGIC;
signal r_sel2 : STD_LOGIC;
signal iniz_sel : STD_LOGIC;
signal mult_sel : STD_LOGIC;
signal addr_sel : STD_LOGIC;
signal compmin_sel : STD_LOGIC;
signal r_sel : STD_LOGIC;
signal o_end : STD_LOGIC;
signal finish : STD_LOGIC;
signal CZ : STD_LOGIC;
type S is (S0,S1,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S12,S13,S14,S15,S16,S17,S18,S19,S20,S21,S22,S23,S24,S25);
signal cur_state, next_state : S;
begin
DATAPATH0: datapathRL port map(
i_clk,
i_rst,
i_start,
i_data,
o_data,
o_address,
r1_load,
r2_load,
rsu_load,
rsu1_load,
rco_load,
rma_load,
rm_load,
rt_load,
rTS_load,
r_sel2,
iniz_sel,
mult_sel,
addr_sel,
compmin_sel,
r_sel,
o_end,
finish,
CZ
);
process(i_clk, i_rst)
begin
if(i_rst = '1') then
cur_state <= S0;
elsif rising_edge(i_clk) then
cur_state <= next_state;
end if;
end process;
process(cur_state, i_start, o_end, CZ, finish) -- FUNZIONE STATO PROSSIMO
begin
next_state <= cur_state;
case cur_state is
when S0 =>
if i_start = '1' then
next_state <= S1;
end if;
when S1 =>
next_state <= S2;
when S2 =>
next_state <= S3;
when S3 =>
next_state <= S4;
when S4 =>
next_state <= S25;
when S25 =>
next_state <= S20;
when S20 =>
if CZ = '1' then
next_state <= S5;
else
next_state <= S23;
end if;
when S23 =>
next_state <= S24;
when S24 =>
next_state <= S20;
when S5 =>
if o_end = '1' then
next_state <= S18;
else if finish = '1' then
next_state <= S16;
else
next_state <= S21;
end if;
end if;
when S21 =>
if o_end = '1' then
next_state <= S22;
else
next_state <= S6;
end if;
when S22 =>
next_state <= S7;
when S6 =>
if o_end = '1' then
next_state <= S7;
else
next_state <= S6;
end if;
when S7 =>
next_state <= S8;
when S8 =>
next_state <= S9;
when S9 =>
next_state <= S10;
when S10 =>
next_state <= S11;
when S11 =>
next_state <= S12;
when S12 =>
if o_end = '1' then
next_state <= S13;
else
next_state <= S9;
end if;
when S13 =>
next_state <= S14;
when S14 =>
next_state <= S15;
when S15 =>
next_state <= S16;
when S16 =>
if i_start = '1' then
next_state <= S16;
else
next_state <= S17;
end if;
when S17 =>
if i_start = '1' then
next_state <= S1;
else
next_state <= S0;
end if;
when S18 =>
next_state <= S19;
when S19 =>
next_state <= S16;
end case;
end process;
process(cur_state) -- FUNZIONE D'USCITA
begin
r1_load <= '0';
r2_load <= '0';
rsu_load <= '0';
rsu1_load <= '0';
rco_load <= '0';
rma_load <= '0';
rm_load <= '0';
rt_load <= '0';
rTS_load <= '0';
r_sel2 <= '0';
iniz_sel <= '0';
mult_sel <= '0';
addr_sel <= '0';
r_sel <= '0';
compmin_sel <= '0';
o_en <= '0';
o_we <= '0';
o_done <= '0';
case cur_state is
when S0 =>
when S1 =>
rco_load <= '1';
when S2 =>
rco_load <= '1';
rma_load <= '1';
rm_load <= '1';
iniz_sel <= '1';
r_sel <= '1';
o_en <= '1';
when S3 =>
r1_load <= '1';
iniz_sel <= '1';
o_en <= '1';
rco_load <= '1';
when S4 =>
r2_load <= '1';
iniz_sel <= '1';
o_en <= '1';
rco_load <= '1';
when S5 =>
rsu1_load <= '1';
rma_load <= '1';
rm_load <= '1';
when S6 =>
rma_load <= '1';
rm_load <= '1';
rco_load <= '1';
iniz_sel <= '1';
o_en <= '1';
when S7 =>
mult_sel <= '1';
rsu_load <= '1';
rco_load <= '1';
rma_load <= '1';
rm_load <= '1';
when S8 =>
o_en <= '1';
when S9 =>
o_en <= '1';
when S10 =>
rt_load <= '1';
when S11 =>
addr_sel <= '1';
rsu_load <= '1';
rco_load <= '1';
iniz_sel <= '1';
o_en <= '1';
o_we <= '1';
when S12 =>
when S13 =>
o_en <= '1';
when S14 =>
rt_load <= '1';
when S15 =>
addr_sel <= '1';
o_en <= '1';
o_we <= '1';
when S16 =>
o_done <= '1';
when S17 =>
o_done <= '0';
when S18 =>
mult_sel <= '1';
rsu_load <= '1';
rco_load <= '1';
when S19 =>
addr_sel <= '1';
o_en <= '1';
o_we <= '1';
compmin_sel <= '1';
when S20 =>
when S21 =>
rco_load <= '1';
iniz_sel <= '1';
o_en <= '1';
when S22 =>
rma_load <= '1';
rm_load <= '1';
when S23 =>
r_sel2 <= '1';
rTS_load <= '1';
r2_load <= '1';
when S25 =>
when S24 =>
r_sel2 <= '1';
end case;
end process;
end Behavioral;
-------------------------------------------------------------------------------------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.NUMERIC_STD.ALL;
entity datapathRL is --datapath è un componente. la entity sono i fili del datapath che porto all'esterno
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_data : out STD_LOGIC_VECTOR (7 downto 0);
o_addr : out STD_LOGIC_VECTOR (15 downto 0);
r1_load : in STD_LOGIC;
r2_load : in STD_LOGIC;
rsu_load : in STD_LOGIC; --forse serve aggiungere o_end
rsu1_load : in STD_LOGIC;
rco_load : in STD_LOGIC;
rma_load : in STD_LOGIC;
rm_load : in STD_LOGIC;
rt_load : in STD_LOGIC;
rTS_load : in STD_LOGIC;
r_sel2 : in STD_LOGIC;
iniz_sel : in STD_LOGIC;
mult_sel : in STD_LOGIC;
addr_sel : in STD_LOGIC;
compmin_sel : in STD_LOGIC;
r_sel : in STD_LOGIC;
o_end : out STD_LOGIC;
finish : out STD_LOGIC;
CZ : out STD_LOGIC);
end datapathRL;
architecture Behavioral of datapathRL is
signal o_reg1 : STD_LOGIC_VECTOR (7 downto 0);
signal o_reg2 : STD_LOGIC_VECTOR (7 downto 0);
signal o_regsum1 : STD_LOGIC_VECTOR (15 downto 0);
signal o_regsum2 : STD_LOGIC_VECTOR (15 downto 0);
signal o_regcount : STD_LOGIC_VECTOR (15 downto 0);
signal o_regmax : STD_LOGIC_VECTOR (7 downto 0);
signal o_regmin : STD_LOGIC_VECTOR (7 downto 0);
--signal o_regdelta : STD_LOGIC_VECTOR (7 downto 0);
--signal o_regshift : STD_LOGIC_VECTOR (7 downto 0);
--signal o_regpretemp : STD_LOGIC_VECTOR (7 downto 0);
signal o_regtemp : STD_LOGIC_VECTOR (15 downto 0);
signal sum1 : STD_LOGIC_VECTOR(15 downto 0);
signal sum2 : STD_LOGIC_VECTOR(15 downto 0);
signal sum3 : STD_LOGIC_VECTOR(15 downto 0);
signal sum4 : STD_LOGIC_VECTOR(8 downto 0); -- ATTENZIONE, è SU 9 BIT
signal sum5 : STD_LOGIC_VECTOR(15 downto 0);
signal sub1 : STD_LOGIC_VECTOR(7 downto 0);
signal sub2 : STD_LOGIC_VECTOR(7 downto 0);
signal sub3 : STD_LOGIC_VECTOR(7 downto 0);
signal sub4 : STD_LOGIC_VECTOR(7 downto 0);
signal o_regTS : STD_LOGIC_VECTOR(15 downto 0);
signal shift : STD_LOGIC_VECTOR(15 downto 0);
signal log : STD_LOGIC_VECTOR(7 downto 0);
signal mux1 : STD_LOGIC_VECTOR(15 downto 0);
signal mux2 : STD_LOGIC_VECTOR(15 downto 0);
signal mux3 : STD_LOGIC_VECTOR(15 downto 0);
signal mux5 : STD_LOGIC_VECTOR(7 downto 0);
signal mux6 : STD_LOGIC_VECTOR(7 downto 0);
signal mux7 : STD_LOGIC_VECTOR(7 downto 0);
signal mux8 : STD_LOGIC_VECTOR(7 downto 0);
signal mux9 : STD_LOGIC_VECTOR(7 downto 0);
signal compmin : STD_LOGIC_VECTOR(7 downto 0);
signal comp1_sel : STD_LOGIC;
signal comp2_sel : STD_LOGIC;
begin
------------------------------------------------------------REGISTRI--------------------------------
process(i_clk, i_rst) --PROCESS CHE DESCIRVE REGISTRO 1
begin
if(i_rst = '1') then
o_reg1 <= "00000000";
elsif rising_edge(i_clk) then
if(r1_load = '1') then
o_reg1 <= i_data;
end if;
end if;
end process;
process(i_clk, i_rst) --PROCESS CHE DESCIRVE REGISTRO 2
begin
if(i_rst = '1') then
o_reg2 <= "00000000";
elsif rising_edge(i_clk) then
if(r2_load = '1') then
o_reg2 <= mux9;
end if;
end if;
end process;
process(i_clk, i_rst) --PROCESS CHE DESCIRVE REGISTRO regsum1
begin
if(i_rst = '1') then
o_regsum1 <= "0000000000000000";
elsif rising_edge(i_clk) then
if(rsu1_load = '1') then
o_regsum1 <= sum1;
end if;
end if;
end process;
process(i_clk, i_rst) --PROCESS CHE DESCIRVE REGISTRO count
begin
if(i_rst = '1') then
o_regcount <= "0000000000000000";
elsif rising_edge(i_clk) then
if(rco_load = '1') then
o_regcount <= sum2;
end if;
end if;
end process;
process(i_clk, i_rst) --PROCESS CHE DESCIRVE REGISTRO sum2
begin
if(i_rst = '1') then
o_regsum2 <= "0000000000000000";
elsif rising_edge(i_clk) then
if(rsu_load = '1') then
o_regsum2 <= sum3;
end if;
end if;
end process;
process(i_clk, i_rst) --PROCESS CHE DESCIRVE REGISTRO max
begin
if(i_rst = '1') then
o_regmax <= "00000000";
elsif rising_edge(i_clk) then
if(rma_load = '1') then
o_regmax <= mux6;
end if;
end if;
end process;
process(i_clk, i_rst) --PROCESS CHE DESCIRVE REGISTRO min
begin
if(i_rst = '1') then
o_regmin <= "00000000";
elsif rising_edge(i_clk) then
if(rm_load = '1') then
o_regmin <= mux8;
end if;
end if;
end process;
process(i_clk, i_rst) --PROCESS CHE DESCIRVE REGISTRO temp
begin
if(i_rst = '1') then
o_regtemp <= "0000000000000000";
elsif rising_edge(i_clk) then
if(rt_load = '1') then
o_regtemp <= shift;
end if;
end if;
end process;
process(i_clk, i_rst, i_start) --PROCESS CHE DESCIRVE REGISTRO TS
begin
if(i_rst = '1' or i_start = '0') then
o_regTS <= "0000000000000000";
elsif rising_edge(i_clk) then
if(rTS_load = '1') then
o_regTS <= sum5;
end if;
end if;
end process;
---------------------------------------------------------REGISTRI---------------------------------
---------------------------------------------------------MULTIPLEXER---------------------------------
with mult_sel select --MULTIPLEXER
mux1 <= "1111111111111111" when '0',
"0000000000000001" when '1',
"XXXXXXXXXXXXXXXX" when others;
with iniz_sel select
mux2 <= mux1 when '0',
o_regcount when '1',
"XXXXXXXXXXXXXXXX" when others;
with iniz_sel select
mux3 <= o_regsum1 when '0',
o_regsum2 when '1',
"XXXXXXXXXXXXXXXX" when others;
with addr_sel select --mux4
o_addr <= o_regcount when '0',
o_regsum2 when '1',
"XXXXXXXXXXXXXXXX" when others;
with comp1_sel select
mux5 <= i_data when '0',
o_regmax when '1',
"XXXXXXXX" when others;
with r_sel select
mux6 <= mux5 when '0',
"00000000" when '1',
"XXXXXXXX" when others;
with comp2_sel select
mux7 <=i_data when '0',
o_regmin when '1',
"XXXXXXXX" when others;
with r_sel select
mux8 <= mux7 when '0',
"11111111" when '1',
"XXXXXXXX" when others;
with compmin_sel select
o_data <= compmin when '0',
"00000000" when '1',
"XXXXXXXX" when others;
with r_sel2 select
mux9 <= i_data when '0',
sub4 when '1',
"XXXXXXXX" when others;
-----------------------------------------------------------MULTIPLEXER------------------------------
---------------------------------------------------------OPERESCIONSz-------------------------------
sum1 <= "0000000000000001" + o_regTS;
sum2 <= "0000000000000001" + mux2;
sum3 <= "0000000000000001" + mux3;
sum4 <= "000000001" + ('0' & sub3); -- ATTENZIONE, è SU 9 BIT LA SOMMA
sum5 <= ("00000000" & o_reg1) + o_regTS;
sub1 <= i_data - o_regmin;
sub2 <= "00001000" - log;
sub3 <= o_regmax - o_regmin;
sub4 <= (o_reg2 - "00000001");-- when (o_reg2 /= "00000000") else o_reg2;
---------------------------------------------------------OPERESCIONSz-------------------------------
----------------------------------------------------------blocchi logici--------------------------
o_end <= '1' when (o_regcount = o_regsum1 or sum1 = "0000000000000010") else '0'; --COMPARAORE
--o_end <= '1' when (sum1 = "0000000000000010") else '0';
comp1_sel <= '1' when (o_regmax > i_data) else '0';
comp2_sel <= '1' when (o_regmin < i_data) else '0';
finish <= '1' when (o_regTS = "0000000000000000") else '0';
CZ <= '1' when (o_reg2 = "00000000") else '0';
process(sum4)
begin
log <= "00000000";
if( sum4 = "00000001") then
log <= "00000000";
end if;
if(sum4>= "00000010" and sum4< "00000100") then
log <= "00000001";
end if;
if(sum4>= "00000100" and sum4< "00001000") then
log <= "00000010";
end if;
if(sum4>= "00001000" and sum4< "00010000") then
log <= "00000011";
end if;
if(sum4>= "00010000" and sum4< "00100000") then
log <= "00000100";
end if;
if(sum4>= "00100000" and sum4< "01000000") then
log <= "00000101";
end if;
if(sum4>= "01000000" and sum4< "10000000") then
log <= "00000110";
end if;
if(sum4 >= "10000000" and sum4<= "11111111") then
log <= "00000111";
end if;
if(sum4 > "11111111") then
log <= "00001000";
end if;
end process;
process(sub1, sub2)
begin
shift <= "0000000000000000";
if( sub2 = "00000000") then
shift <= "00000000" & sub1;
end if;
if( sub2 = "00000001") then
shift <= "0000000" & sub1 & '0';
end if;
if( sub2 = "00000010" ) then
shift <= "000000" & sub1 & "00";
end if;
if( sub2 = "00000011" ) then
shift <= "00000" & sub1 & "000";
end if;
if( sub2 = "00000100" ) then
shift <= "0000" & sub1 & "0000";
end if;
if( sub2 = "00000101" ) then
shift <= "000" & sub1 & "00000";
end if;
if( sub2 = "00000110" ) then
shift <= "00" & sub1 & "000000";
end if;
if( sub2 = "00000111" ) then
shift <= "0" & sub1 & "0000000";
end if;
if( sub2 = "00001000" ) then
shift <= sub1 & "00000000";
end if;
end process;
compmin <= "11111111" when ( "0000000011111111" < o_regtemp) else o_regtemp(7 downto 0);
end Behavioral;