-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.vhd
More file actions
606 lines (575 loc) · 14.5 KB
/
cpu.vhd
File metadata and controls
606 lines (575 loc) · 14.5 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
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
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.numeric_std.all;
use work.States.all;
entity cpu is
port(
reset : in STD_LOGIC;
sw0 : in STD_LOGIC;
start : in STD_LOGIC;
clk : in STD_LOGIC;
output1: out STd_logic_vector(6 downto 0);
output2: out STd_logic_vector(6 downto 0);
output3: out STd_logic_vector(6 downto 0);
output4: out STd_logic_vector(6 downto 0);
output5: out STd_logic_vector(6 downto 0);
output6: out STd_logic_vector(6 downto 0);
test0: out STd_logic;
test1: out STd_logic;
Z: out STd_logic
);
end cpu;
architecture behavioral of cpu is
function convSEG (N : std_logic_vector(3 downto 0) ) return std_logic_vector is
variable ans:std_logic_vector(6 downto 0);
begin
Case N is
when "0000" => ans:="1000000";
when "0001" => ans:="1111001";
when "0010" => ans:="0100100";
when "0011" => ans:="0110000";
when "0100" => ans:="0011001";
when "0101" => ans:="0010010";
when "0110" => ans:="0000010";
when "0111" => ans:="1111000";
when "1000" => ans:="0000000";
when "1001" => ans:="0010000";
when "1010" => ans:="0001000";
when "1011" => ans:="0000011";
when "1100" => ans:="1000110";
when "1101" => ans:="0100001";
when "1110" => ans:="0000110";
when "1111" => ans:="0001110";
when others=> ans:="1111111";
end case;
return ans;
end function convSEG;
type MEMtype is array (0 to 2**16-1) of std_logic_vector(7 downto 0);
constant ROM : MEMtype := (
"00001011",
"00000001",
"00000000",
"00100000",
"00000011",
"00000001",
"00000000",
"00100001",
"00001000",
"00000111",
"00000000",
"00010000",
"00001100",
"00000101",
"00000000",
"00010010",
"00001001",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000101",
"00001010",
others => "00000000" );
signal P_S : myState;
signal N_S : myState;
signal AR_REGISTER :STd_logic_vector(15 downto 0);
signal AC_REGISTER :STd_logic_vector(7 downto 0);
signal IR_REGISTER :STd_logic_vector(7 downto 0);
signal PC_REGISTER :STd_logic_vector(15 downto 0);
signal R_REGISTER :STd_logic_vector(7 downto 0);
signal TR_REGISTER :STd_logic_vector(7 downto 0);
signal Z_FLAG :STd_logic;
signal DR_REGISTER :STd_logic_vector(7 downto 0);
signal RAM :MEMtype;
signal Delay_count : integer := 0;
signal en : STd_logic := '0';
signal temp : STd_logic_vector(1 downto 0);
signal Mem_count : integer := 0;
begin
--FSM structure process
process(clk , reset)
begin
if (reset = '0') then
P_S <= RESET1;
elsif (rising_edge(clk)) then
P_S <= N_S;
end if;
end process;
--FSM transport process
process(P_S , IR_REGISTER, Z_FLAG, start, en, Mem_count)
begin
N_S <= P_S;
case P_S is
when RESET1 =>
N_S <= MEM_INIT;
temp <= "00";
when MEM_INIT =>
if (Mem_count = 2**16-1) then
N_S <= FINISH;
end if;
temp <= "01";
when FINISH =>
if (start = '0') then
N_S <= FETCH1;
end if;
temp <= "10";
when FETCH1 =>
if( en = '1' )then
N_S <= FETCH2;
end if;
temp <= "11";
when FETCH2 =>
if( en = '1' )then
N_S <= FETCH3;
end if;
temp <= "00";
when FETCH3 =>
if( en = '1' )then
if (IR_REGISTER="00000000") then
N_S <= NOP1;
elsif (IR_REGISTER="00000001") then
N_S <= LDAC1;
elsif (IR_REGISTER="00000010") then
N_S <= STAC1;
elsif (IR_REGISTER="00000011") then
N_S <= MVAC1;
elsif (IR_REGISTER="00000100") then
N_S <= MOVR1;
elsif (IR_REGISTER="00000101") then
N_S <= JUMP1;
elsif (IR_REGISTER="00000110" AND Z_FLAG='1') then
N_S <= JMPZY1;
elsif (IR_REGISTER="00000110" AND Z_FLAG='0') then
N_S <= JMPZN1;
elsif (IR_REGISTER="00000111" AND Z_FLAG='0') then
N_S <= JPNZY1;
elsif (IR_REGISTER="00000111" AND Z_FLAG='1') then
N_S <= JPNZN1;
elsif (IR_REGISTER="00001000") then
N_S <= ADD1;
elsif (IR_REGISTER="00001001") then
N_S <= SUB1;
elsif (IR_REGISTER="00001010") then
N_S <= INAC1;
elsif (IR_REGISTER="00001011") then
N_S <= CLAC1;
elsif (IR_REGISTER="00001100") then
N_S <= AND1;
elsif (IR_REGISTER="00001101") then
N_S <= OR1;
elsif (IR_REGISTER="00001110") then
N_S <= XOR1;
elsif (IR_REGISTER="00001111") then
N_S <= NOT1;
end if;
end if;
when NOP1 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when LDAC1 =>
if( en = '1' )then
N_S <= LDAC2;
end if;
when LDAC2 =>
if( en = '1' )then
N_S <= LDAC3;
end if;
when LDAC3 =>
if( en = '1' )then
N_S <= LDAC4;
end if;
when LDAC4 =>
if( en = '1' )then
N_S <= LDAC5;
end if;
when LDAC5 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when STAC1 =>
if( en = '1' )then
N_S <= STAC2;
end if;
when STAC2 =>
if( en = '1' )then
N_S <= STAC3;
end if;
when STAC3 =>
if( en = '1' )then
N_S <= STAC4;
end if;
when STAC4 =>
if( en = '1' )then
N_S <= STAC5;
end if;
when STAC5 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when MVAC1 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when MOVR1 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when JUMP1 =>
if( en = '1' )then
N_S <= JUMP2;
end if;
when JUMP2 =>
if( en = '1' )then
N_S <= JUMP3;
end if;
when JUMP3 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when JMPZY1 =>
if( en = '1' )then
N_S <= JMPZY2;
end if;
when JMPZY2 =>
if( en = '1' )then
N_S <= JMPZY3;
end if;
when JMPZY3 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when JMPZN1 =>
if( en = '1' )then
N_S <= JMPZN2;
end if;
when JMPZN2 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when JPNZY1 =>
if( en = '1' )then
N_S <= JPNZY2;
end if;
when JPNZY2 =>
if( en = '1' )then
N_S <= JPNZY3;
end if;
when JPNZY3 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when JPNZN1 =>
if( en = '1' )then
N_S <= JPNZN2;
end if;
when JPNZN2 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when ADD1 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when SUB1 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when INAC1 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when CLAC1 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when AND1 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when OR1 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when XOR1 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
when NOT1 =>
if( en = '1' )then
N_S <= FETCH1;
end if;
end case;
end process;
--manage AC_REGISTER
process(clk, reset)
begin
if(reset = '0')then
AC_REGISTER <= (others => '0');
elsif (falling_edge(clk) and en = '1')then
AC_REGISTER <= AC_REGISTER ;
case P_S is
when MEM_INIT =>
AC_REGISTER(7 downto 4) <= "0011";
AC_REGISTER(3 downto 0) <= "0011";
when LDAC5 =>
AC_REGISTER <= DR_REGISTER ;
when MOVR1 =>
AC_REGISTER <= R_REGISTER ;
when ADD1 =>
AC_REGISTER <= std_logic_vector(unsigned(AC_REGISTER) + unsigned(R_REGISTER));
when SUB1 =>
AC_REGISTER <= std_logic_vector(unsigned(AC_REGISTER) - unsigned(R_REGISTER));
when INAC1 =>
AC_REGISTER <= std_logic_vector(unsigned(AC_REGISTER) + 1);
when CLAC1 =>
AC_REGISTER <= (others => '0');
when AND1 =>
AC_REGISTER <= AC_REGISTER AND R_REGISTER ;
when OR1 =>
AC_REGISTER <= AC_REGISTER OR R_REGISTER ;
when XOR1 =>
AC_REGISTER <= AC_REGISTER XOR R_REGISTER ;
when NOT1 =>
AC_REGISTER <= NOT AC_REGISTER ;
when others =>
end case;
end if;
end process;
--manage PC_REGISTER
process(clk, reset)
begin
if(reset = '0')then
PC_REGISTER <= (others => '0');
elsif (falling_edge(clk) and en = '1')then
PC_REGISTER <= PC_REGISTER ;
case P_S is
when FETCH2 =>
PC_REGISTER <= std_logic_vector(unsigned(PC_REGISTER) +1);
when LDAC1 =>
PC_REGISTER <= std_logic_vector(unsigned(PC_REGISTER) +1);
when LDAC2 =>
PC_REGISTER <= std_logic_vector(unsigned(PC_REGISTER) +1);
when STAC1 =>
PC_REGISTER <= std_logic_vector(unsigned(PC_REGISTER) +1);
when STAC2 =>
PC_REGISTER <= std_logic_vector(unsigned(PC_REGISTER) +1);
when JUMP3 =>
PC_REGISTER(15 downto 8) <= TR_REGISTER;
PC_REGISTER(7 downto 0) <= DR_REGISTER;
when JMPZY3 =>
PC_REGISTER(15 downto 8) <= TR_REGISTER;
PC_REGISTER(7 downto 0) <= DR_REGISTER;
when JMPZN1 =>
PC_REGISTER <= std_logic_vector(unsigned(PC_REGISTER) +1);
when JMPZN2 =>
PC_REGISTER <= std_logic_vector(unsigned(PC_REGISTER) +1);
when JPNZY3 =>
PC_REGISTER(15 downto 8) <= TR_REGISTER;
PC_REGISTER(7 downto 0) <= DR_REGISTER;
when JPNZN1 =>
PC_REGISTER <= std_logic_vector(unsigned(PC_REGISTER) +1);
when JPNZN2 =>
PC_REGISTER <= std_logic_vector(unsigned(PC_REGISTER) +1);
when others =>
end case;
end if;
end process;
--manage AR_REGISTER
process(clk, reset)
begin
if(reset = '0')then
AR_REGISTER <= (others => '0');
elsif (falling_edge(clk) and en = '1')then
AR_REGISTER <= AR_REGISTER ;
case P_S is
when FETCH1 =>
AR_REGISTER <= PC_REGISTER ;
when FETCH3 =>
AR_REGISTER <= PC_REGISTER ;
when LDAC1 =>
AR_REGISTER <= std_logic_vector(unsigned(AR_REGISTER) +1);
when LDAC3 =>
AR_REGISTER(15 downto 8) <= TR_REGISTER;
AR_REGISTER(7 downto 0) <= DR_REGISTER;
when STAC1 =>
AR_REGISTER <= std_logic_vector(unsigned(AR_REGISTER) +1);
when STAC3 =>
AR_REGISTER(15 downto 8) <= TR_REGISTER;
AR_REGISTER(7 downto 0) <= DR_REGISTER;
when JUMP1 =>
AR_REGISTER <= std_logic_vector(unsigned(AR_REGISTER) +1);
when JMPZY1 =>
AR_REGISTER <= std_logic_vector(unsigned(AR_REGISTER) +1);
when JPNZY1 =>
AR_REGISTER <= std_logic_vector(unsigned(AR_REGISTER) +1);
when others =>
end case;
end if;
end process;
--manage DR_REGISTER
process(clk, reset)
begin
if(reset = '0')then
DR_REGISTER <= (others => '0');
elsif (falling_edge(clk))then
DR_REGISTER <= DR_REGISTER ;
case P_S is
when FETCH2 =>
DR_REGISTER <= RAM(to_integer(unsigned(AR_REGISTER)));
when LDAC1 =>
DR_REGISTER <= RAM(to_integer(unsigned(AR_REGISTER)));
when LDAC2 =>
DR_REGISTER <= RAM(to_integer(unsigned(AR_REGISTER)));
when LDAC4 =>
DR_REGISTER <= RAM(to_integer(unsigned(AR_REGISTER)));
when STAC1 =>
DR_REGISTER <= RAM(to_integer(unsigned(AR_REGISTER)));
when STAC2 =>
DR_REGISTER <= RAM(to_integer(unsigned(AR_REGISTER)));
when STAC4 =>
DR_REGISTER <= AC_REGISTER ;
when JUMP1 =>
DR_REGISTER <= RAM(to_integer(unsigned(AR_REGISTER)));
when JUMP2 =>
DR_REGISTER <= RAM(to_integer(unsigned(AR_REGISTER)));
when JMPZY1 =>
DR_REGISTER <= RAM(to_integer(unsigned(AR_REGISTER)));
when JMPZY2 =>
DR_REGISTER <= RAM(to_integer(unsigned(AR_REGISTER)));
when JPNZY1 =>
DR_REGISTER <= RAM(to_integer(unsigned(AR_REGISTER)));
when JPNZY2 =>
DR_REGISTER <= RAM(to_integer(unsigned(AR_REGISTER)));
when others =>
end case;
end if;
end process;
--manage TR_REGISTER
process(clk, reset)
begin
if(reset = '0')then
TR_REGISTER <= (others => '0');
elsif (falling_edge(clk))then
TR_REGISTER <= TR_REGISTER ;
case P_S is
when LDAC2 =>
TR_REGISTER <= DR_REGISTER ;
when STAC2 =>
TR_REGISTER <= DR_REGISTER ;
when JUMP2 =>
TR_REGISTER <= DR_REGISTER ;
when JMPZY2 =>
TR_REGISTER <= DR_REGISTER ;
when JPNZY2 =>
TR_REGISTER <= DR_REGISTER ;
when others =>
end case;
end if;
end process;
--manage IR_REGISTER
process(clk, reset)
begin
if(reset = '0')then
IR_REGISTER <= (others => '0');
elsif (falling_edge(clk))then
IR_REGISTER <= IR_REGISTER ;
case P_S is
when FETCH3 =>
IR_REGISTER <= DR_REGISTER ;
when others =>
end case;
end if;
end process;
--manage R_REGISTER
process(clk, reset)
begin
if(reset = '0')then
R_REGISTER <= (others => '0');
elsif (falling_edge(clk))then
R_REGISTER <= R_REGISTER ;
case P_S is
when MEM_INIT =>
R_REGISTER(7 downto 4) <= "0000";
R_REGISTER(3 downto 0) <= "0010";
when MVAC1 =>
R_REGISTER <= AC_REGISTER ;
when others =>
end case;
end if;
end process;
--manage Z_FLAG
process(clk, reset)
begin
if(reset = '0')then
Z_FLAG <= '0';
elsif (rising_edge(clk))then
if (AC_REGISTER = "00000000" ) then
Z_FLAG <= '1';
else
Z_FLAG <= '0';
end if;
end if;
end process;
--manage memory
process(clk)
begin
if (falling_edge(clk))then
case P_S is
when MEM_INIT =>
RAM(Mem_count) <= ROM(Mem_count);
Mem_count <= Mem_count + 1;
when STAC5 =>
RAM(to_integer(unsigned(AR_REGISTER))) <= DR_REGISTER ;
when others =>
end case;
end if;
end process;
--Send value of register to output
process(clk)
variable tmp: STd_logic_vector(7 downto 0);
begin
tmp := RAM(to_integer(unsigned(AR_REGISTER)));
if(sw0 = '0')then
output1 <= convSEG(AC_REGISTER(3 downto 0));
output2 <= convSEG(AC_REGISTER(7 downto 4));
output3 <= convSEG(R_REGISTER(3 downto 0));
output4 <= convSEG(R_REGISTER(7 downto 4));
output5 <= convSEG(tmp(3 downto 0));
output6 <= convSEG(tmp(7 downto 4));
else
output1 <= convSEG(DR_REGISTER(3 downto 0));
output2 <= convSEG(DR_REGISTER(7 downto 4));
output3 <= convSEG(AR_REGISTER(3 downto 0));
output4 <= convSEG(AR_REGISTER(7 downto 4));
output5 <= convSEG(PC_REGISTER(3 downto 0));
output6 <= convSEG(PC_REGISTER(7 downto 4));
end if;
end process;
process(clk)
begin
if (falling_edge(clk))then
if(Delay_count = 100000000)then
en <= '1';
Delay_count <= 0;
else
en <= '0';
Delay_count <= Delay_count + 1;
end if;
end if;
end process;
test0 <= temp(0);
test1 <= temp(1);
Z <= Z_FLAG;
end behavioral;