This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.vhd
558 lines (501 loc) · 16.4 KB
/
code.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
-----------------------------------------
-- Politecnico Di Milano
-- Alessandro Fornara, Donato Fiore
-- Progetto di Reti Logiche AA 2022-2023
-----------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.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_w : in std_logic;
o_z0 : out std_logic_vector(7 downto 0);
o_z1 : out std_logic_vector(7 downto 0);
o_z2 : out std_logic_vector(7 downto 0);
o_z3 : out std_logic_vector(7 downto 0);
o_done : out std_logic;
o_mem_addr : out std_logic_vector(15 downto 0);
i_mem_data : in std_logic_vector(7 downto 0);
o_mem_we : out std_logic;
o_mem_en : out std_logic
);
end project_reti_logiche;
architecture Behavioral of project_reti_logiche is
component datapath is
port(
i_clk : in STD_LOGIC;
i_rst : in STD_LOGIC;
i_w : in STD_LOGIC;
o_z0 : out STD_LOGIC_VECTOR (7 downto 0);
o_z1 : out STD_LOGIC_VECTOR (7 downto 0);
o_z2 : out STD_LOGIC_VECTOR (7 downto 0);
o_z3 : out STD_LOGIC_VECTOR (7 downto 0);
r1_load : in STD_LOGIC;
r2_load : in STD_LOGIC;
r3_load : in STD_LOGIC;
r4_load : in STD_LOGIC;
output_sel : in STD_LOGIC;
sum_sel : in STD_LOGIC;
input_sel : in STD_LOGIC;
save : in STD_LOGIC;
load_addr : in STD_LOGIC;
o_done : out STD_LOGIC;
i_mem_data : in STD_LOGIC_VECTOR(7 downto 0);
o_mem_addr : out STD_LOGIC_VECTOR(15 downto 0));
end component;
--Segnali Macchina a Stati
signal r1_load : STD_LOGIC;
signal r2_load : STD_LOGIC;
signal r3_load : STD_LOGIC;
signal r4_load : STD_LOGIC;
signal sum_sel : STD_LOGIC;
signal output_sel : STD_LOGIC;
signal input_sel : STD_LOGIC;
signal save : STD_LOGIC;
signal load_addr : 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);
signal cur_state, next_state : S;
--Segnali Datapath
signal o_reg1 : STD_LOGIC := '0';
signal o_reg2 : STD_LOGIC := '0';
signal o_reg3 : STD_LOGIC := '0';
signal o_reg4 : STD_LOGIC_VECTOR (15 downto 0) := (others => '0');
signal mux_input : STD_LOGIC;
signal mux_reg4 : STD_LOGIC_VECTOR (15 downto 0);
signal sum : STD_LOGIC_VECTOR (15 downto 0);
signal shift : STD_LOGIC_VECTOR (15 downto 0);
signal o_reg_addr : STD_LOGIC_VECTOR (15 downto 0) := (others => '0');
signal r_z0_load : STD_LOGIC;
signal r_z1_load : STD_LOGIC;
signal r_z2_load : STD_LOGIC;
signal r_z3_load : STD_LOGIC;
signal o_reg_z0 : STD_LOGIC_VECTOR (7 downto 0) := (others => '0');
signal o_reg_z1 : STD_LOGIC_VECTOR (7 downto 0) := (others => '0');
signal o_reg_z2 : STD_LOGIC_VECTOR (7 downto 0) := (others => '0');
signal o_reg_z3 : STD_LOGIC_VECTOR (7 downto 0) := (others => '0');
signal mux_z0 : STD_LOGIC_VECTOR ( 7 downto 0);
signal mux_z1 : STD_LOGIC_VECTOR ( 7 downto 0);
signal mux_z2 : STD_LOGIC_VECTOR ( 7 downto 0);
signal mux_z3 : STD_LOGIC_VECTOR ( 7 downto 0);
signal done_sel : STD_LOGIC_VECTOR (7 downto 0);
begin
process(i_clk, i_rst)
begin
if(i_rst = '1') then
cur_state <= S0;
elsif i_clk'event and i_clk = '1' then
cur_state <= next_state;
end if;
end process;
process(cur_state, i_start)
begin
next_state <= cur_state;
case cur_state is
when S0 =>
if i_start='1' then
next_state <= S1;
end if;
when S1 =>
if i_rst='1' then
next_state <= S0;
elsif i_rst='0' then
next_state <= S2;
end if;
when S2 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S3;
end if;
when S3 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S4;
end if;
when S4 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S5;
end if;
when S5 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S6;
end if;
when S6 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S7;
end if;
when S7 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S8;
end if;
when S8 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S9;
end if;
when S9 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S10;
end if;
when S10 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S11;
end if;
when S11 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S12;
end if;
when S12 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S13;
end if;
when S13 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S14;
end if;
when S14 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S15;
end if;
when S15 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S16;
end if;
when S16 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S17;
end if;
when S17 =>
if i_rst='1' then
next_state <= S0;
elsif i_start='0' then
next_state <= S19;
elsif i_rst='0' then
next_state <= S18;
end if;
when S18 =>
if i_rst='1' then
next_state <= S0;
elsif i_rst='0' then
next_state <= S19;
end if;
when S19 =>
if i_rst='1' then
next_state <= S0;
elsif i_rst='0' then
next_state <= S20;
end if;
when S20 =>
if i_rst='1' then
next_state <= S0;
elsif i_rst='0' then
next_state <= S21;
end if;
when S21 =>
if i_rst='1' then
next_state <= S0;
elsif i_rst='0' then
next_state <= S0;
end if;
end case;
end process;
process(cur_state)
begin
r1_load<='1';
r2_load<='1';
r3_load<='1';
r4_load<='1';
sum_sel<='1';
output_sel<='0';
input_sel<='1';
save<='0';
load_addr<='1';
o_mem_en<='0';
o_mem_we<='0';
case cur_state is
when S0 =>
sum_sel<='0';
input_sel<='0';
when S1 =>
r1_load<='0';
input_sel<='0';
sum_sel<='0';
when S2 =>
r1_load<='0';
r2_load<='0';
input_sel<='0';
sum_sel<='0';
when S3 =>
r1_load<='0';
r2_load<='0';
when S4 =>
r1_load<='0';
r2_load<='0';
when S5 =>
r1_load<='0';
r2_load<='0';
when S6 =>
r1_load<='0';
r2_load<='0';
when S7 =>
r1_load<='0';
r2_load<='0';
when S8 =>
r1_load<='0';
r2_load<='0';
when S9 =>
r1_load<='0';
r2_load<='0';
when S10 =>
r1_load<='0';
r2_load<='0';
when S11 =>
r1_load<='0';
r2_load<='0';
when S12 =>
r1_load<='0';
r2_load<='0';
when S13 =>
r1_load<='0';
r2_load<='0';
when S14 =>
r1_load<='0';
r2_load<='0';
when S15 =>
r1_load<='0';
r2_load<='0';
when S16 =>
r1_load<='0';
r2_load<='0';
when S17 =>
r1_load<='0';
r2_load<='0';
when S18 =>
r1_load<='0';
r2_load<='0';
r3_load<='0';
when S19 =>
r1_load<='0';
r2_load<='0';
r3_load<='0';
r4_load<='0';
o_mem_en<='1';
sum_sel<='0';
when S20 =>
r1_load<='0';
r2_load<='0';
r3_load<='0';
sum_sel<='0';
save<='1';
when S21 =>
r1_load<='0';
r2_load<='0';
r3_load<='0';
sum_sel<='0';
input_sel<='0';
output_sel<='1';
end case;
end process;
--Datapath
process(i_clk, i_rst)
begin
if(i_rst = '1') then
o_reg1 <= '0';
elsif i_clk'event and i_clk = '1' then
if(r1_load = '1') then
o_reg1 <= i_w;
end if;
end if;
end process;
process(i_clk, i_rst)
begin
if(i_rst = '1') then
o_reg2 <= '0';
elsif i_clk'event and i_clk = '1' then
if(r2_load = '1') then
o_reg2 <= i_w;
end if;
end if;
end process;
process(i_clk, i_rst)
begin
if(i_rst = '1') then
o_reg3 <= '0';
elsif i_clk'event and i_clk = '1' then
if(r3_load = '1') then
o_reg3 <= i_w;
end if;
end if;
end process;
with sum_sel select
mux_reg4 <= "0000000000000000" when '0',
shift when '1',
"XXXXXXXXXXXXXXXX" when others;
process(i_clk, i_rst)
begin
if(i_rst = '1') then
o_reg4 <= "0000000000000000";
elsif i_clk'event and i_clk = '1' then
if(r4_load = '1') then
o_reg4 <= mux_reg4;
end if;
end if;
end process;
with input_sel select
mux_input <= '0' when '0',
o_reg3 when '1',
'X' when others;
sum <= ("000000000000000" & mux_input) + o_reg4;
process(i_clk, i_rst)
begin
if(i_rst = '1') then
o_reg_addr <= "0000000000000000";
elsif i_clk'event and i_clk = '1' then
if(load_addr = '1') then
o_reg_addr <= sum;
end if;
end if;
end process;
o_mem_addr <= o_reg_addr;
shift <= sum + sum;
with save select
r_z0_load <= not(o_reg1) and not(o_reg2) when '1',
'0' when '0',
'X' when others;
with save select
r_z1_load <= not(o_reg1) and o_reg2 when '1',
'0' when '0',
'X' when others;
with save select
r_z2_load <= o_reg1 and not(o_reg2) when '1',
'0' when '0',
'X' when others;
with save select
r_z3_load <= o_reg1 and o_reg2 when '1',
'0' when '0',
'X' when others;
process(i_clk, i_rst)
begin
if(i_rst = '1') then
o_reg_z0 <= "00000000";
elsif i_clk'event and i_clk = '1' then
if(r_z0_load = '1' and o_reg1 ='0' and o_reg2='0') then
o_reg_z0 <= i_mem_data;
end if;
end if;
end process;
process(i_clk, i_rst)
begin
if(i_rst = '1') then
o_reg_z1 <= "00000000";
elsif i_clk'event and i_clk = '1' then
if(r_z1_load = '1' and o_reg1 ='0' and o_reg2='1') then
o_reg_z1 <= i_mem_data;
end if;
end if;
end process;
process(i_clk, i_rst)
begin
if(i_rst = '1') then
o_reg_z2 <= "00000000";
elsif i_clk'event and i_clk = '1' then
if(r_z2_load = '1' and o_reg1 ='1' and o_reg2='0') then
o_reg_z2 <= i_mem_data;
end if;
end if;
end process;
process(i_clk, i_rst)
begin
if(i_rst = '1') then
o_reg_z3 <= "00000000";
elsif i_clk'event and i_clk = '1' then
if(r_z3_load = '1' and o_reg1 ='1' and o_reg2='1') then
o_reg_z3 <= i_mem_data;
end if;
end if;
end process;
with output_sel select
mux_z0 <= "00000000" when '0',
o_reg_z0 when '1',
"XXXXXXXX" when others;
o_z0 <= mux_z0;
with output_sel select
mux_z1 <= "00000000" when '0',
o_reg_z1 when '1',
"XXXXXXXX" when others;
o_z1 <= mux_z1;
with output_sel select
mux_z2 <= "00000000" when '0',
o_reg_z2 when '1',
"XXXXXXXX" when others;
o_z2 <= mux_z2;
with output_sel select
mux_z3 <= "00000000" when '0',
o_reg_z3 when '1',
"XXXXXXXX" when others;
o_z3 <= mux_z3;
done_sel <= ("0000000" & output_sel);
with done_sel select
o_done <= '0' when "00000000",
'1' when others;
end Behavioral;