-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.vhd
1007 lines (866 loc) · 37.4 KB
/
cpu.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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 05:33:54 10/24/2013
-- Design Name:
-- Module Name: cpu - 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;
-- 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 primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
use work.utils.ALL;
entity cpu is
Port ( CLK : in STD_LOGIC;
cpu_op1 : in STD_LOGIC_VECTOR (31 downto 0);
cpu_op2 : in STD_LOGIC_VECTOR (31 downto 0);
-- DHalt : in std_logic;
-- DRegAddr : in std_logic_vector(4 downto 0);
-- DMemAddr : out std_logic_vector(31 downto 0);
DRegOut : out std_logic_vector(31 downto 0);
DOutput : out std_logic_vector(31 downto 0);
DOutput2 : out std_logic_vector(31 downto 0);
-- DMemOut : out std_logic_vector(31 downto 0);
-- DCPUState : out std_logic_vector(31 downto 0);
DCurrentIns : out std_logic_vector(31 downto 0);
DCurrentIns2 : out std_logic_vector(31 downto 0);
DCurrentIns3 : out std_logic_vector(31 downto 0);
DCurrentIns4 : out std_logic_vector(31 downto 0);
DCurrentIns5 : out std_logic_vector(31 downto 0);
DAlu1 : out std_logic_vector(31 downto 0);
DAlu2 : out std_logic_vector(31 downto 0);
DAluR1 : out std_logic_vector(31 downto 0);
DAluR2 : out std_logic_vector(31 downto 0);
DRegOutAddr : out std_logic_vector(4 downto 0)
);
end cpu;
architecture Behavioral of cpu is
component rom is
port (
EN : in std_logic;
ADDR : in std_logic_vector(31 downto 0);
DATA : out std_logic_vector(31 downto 0));
end component;
component decode is
PORT( CLK : in std_logic;
CurrentInstruction : in std_logic_vector(31 downto 0);
WriteAddr : in std_logic_vector(4 downto 0);
WriteData : in std_logic_vector(31 downto 0);
RegWrite : in std_logic;
AluOP1 : out std_logic_vector(31 downto 0);
AluOP2 : out std_logic_vector(31 downto 0);
AluControl : out std_logic_vector(5 downto 0);
ControlSignals : out std_logic_vector(4 downto 0);
RegWBAddr : out std_logic_vector( 4 downto 0);
WaitFor : out std_logic_vector (5 downto 0);
registerOut : out std_logic_vector(31 downto 0);
lreg: out std_logic_vector(31 downto 0);
lregAddr : out std_logic_vector(4 downto 0));
end component;
component alu is
Port ( Clk : in STD_LOGIC;
Control : in STD_LOGIC_VECTOR (5 downto 0);
Operand1 : in STD_LOGIC_VECTOR (31 downto 0);
Operand2 : in STD_LOGIC_VECTOR (31 downto 0);
Result1 : out STD_LOGIC_VECTOR (31 downto 0);
Result2 : out STD_LOGIC_VECTOR (31 downto 0);
Debug : out STD_LOGIC_VECTOR (31 downto 0));
end component;
--component ram is
-- port (CLK : in std_logic;
-- WE : in std_logic;
-- EN : in std_logic;
-- ADDR : in std_logic_vector(31 downto 0);
-- DI : in std_logic_vector(31 downto 0);
-- DO : out std_logic_vector(31 downto 0));
--end component;
type CPUState is (FetchDecode, Execute, MemWR, WriteBack, AluWait, AluWaitWB);
signal rom_EN : std_logic := '0';
signal rom_ADDR : std_logic_vector(31 downto 0) := (others => '0');
signal rom_DATA : std_logic_vector(31 downto 0) := (others => '0');
signal decode_CurrentInstruction : std_logic_vector(31 downto 0) := (others => '0');
signal decode_WriteAddr : std_logic_vector(4 downto 0) := (others => '0');
signal decode_WriteData : std_logic_vector(31 downto 0) := (others => '0');
signal decode_RegWrite : std_logic := '0';
signal decode_AluOP1 : std_logic_vector(31 downto 0) := (others => '0');
signal decode_AluOP2 : std_logic_vector(31 downto 0) := (others => '0');
signal decode_AluControl : std_logic_vector(5 downto 0) := (others => '0');
signal decode_ControlSignals : std_logic_vector(4 downto 0) := (others => '0');
signal decode_waitFor : std_logic_vector(5 downto 0);
signal decode_registerOut : std_logic_vector(31 downto 0);
signal decode_lreg : std_logic_vector(31 downto 0);
signal decode_lregAddr : std_logic_vector(4 downto 0);
signal decode_RegWBAddr : std_logic_vector(4 downto 0);
signal alu_control : std_logic_vector(5 downto 0) := (others => '0');
signal alu_op1 : std_logic_vector(31 downto 0) := (others => '0');
signal alu_op2 : std_logic_vector(31 downto 0) := (others => '0');
signal alu_r1 : std_logic_vector(31 downto 0) := (others => '0');
signal alu_r2 : std_logic_vector(31 downto 0) := (others => '0');
signal alu_debug : std_logic_vector(31 downto 0) := (others => '0');
signal RAM0: RamData := (0 => x"10", 1 => x"ff", 2 => x"01", 3 => x"0d",others => (others => '0'));
signal RAM1: RamData := (0 => x"00",1 => x"ff",others => (others => '0'));
signal RAM2: RamData := (0 => x"00",others => (others => '0'));
signal RAM3: RamData := (0 => x"00",others => (others => '0'));
--signal ram_we : std_logic := '0';
--signal ram_en : std_logic := '1';
--signal ram_addr : std_logic_vector(31 downto 0) := (others => '0');
--signal ram_di : std_logic_vector(31 downto 0) := (others => '0');
--signal ram_do : std_logic_vector(31 downto 0) := (others => '0');
signal EX_currentIns : std_logic_vector(31 downto 0) := (others => '0');
signal ALUW_currentIns : std_logic_vector(31 downto 0) := (others => '0');
signal MEMWR_currentIns : std_logic_vector(31 downto 0) := (others => '0');
signal WB_currentIns : std_logic_vector(31 downto 0) := (others => '0');
signal ALUW_decodeRegOut : std_logic_vector(31 downto 0) := (others => '0');
signal MEMWR_decodeRegOut : std_logic_vector(31 downto 0) := (others => '0');
signal WB_decodeRegOut : std_logic_vector(31 downto 0) := (others => '0');
signal ALUW_decodeControlSignals : std_logic_vector(4 downto 0) := (others => '0');
signal MEMWR_decodeControlSignals : std_logic_vector(4 downto 0) := (others => '0');
signal WB_decodeControlSignals : std_logic_vector(4 downto 0) := (others => '0');
signal MEMWR_alur1: std_logic_vector(31 downto 0) := (others => '0');
signal MEMWR_alur2: std_logic_vector(31 downto 0) := (others => '0');
signal WB_alur1: std_logic_vector(31 downto 0) := (others => '0');
signal WB_alur2: std_logic_vector(31 downto 0) := (others => '0');
signal ALUW_decodeRegWBAddr : std_logic_vector(4 downto 0);
signal MEMWR_decodeRegWBAddr : std_logic_vector(4 downto 0);
signal WB_decodeRegWBAddr : std_logic_vector(4 downto 0);
signal END_currentIns : std_logic_vector(31 downto 0) := (others => '0');
signal END_decodeRegOut : std_logic_vector(31 downto 0) := (others => '0');
signal END_decodeControlSignals : std_logic_vector(4 downto 0) := (others => '0');
signal END_alur1 : std_logic_vector(31 downto 0) := (others => '0');
signal END_alur2 : std_logic_vector(31 downto 0) := (others => '0');
signal END_decodeRegWBAddr : std_logic_vector(4 downto 0);
begin
irom : rom port map (EN => '1',
ADDR => rom_ADDR,
DATA => rom_DATA);
idecode : decode port map (CLK => CLK,
CurrentInstruction => decode_currentInstruction,
WriteAddr => decode_WriteAddr,
WriteData => decode_WriteData,
RegWrite => decode_RegWrite,
AluOP1 => decode_AluOP1,
AluOP2 => decode_AluOP2,
AluControl => decode_AluControl,
ControlSignals => decode_ControlSignals,
RegWBAddr => decode_RegWBAddr,
WaitFor => decode_waitFor,
registerOut => decode_registerOut,
lreg => decode_lreg,
lregAddr => decode_lregAddr);
-- ControlSignals
-- 0 => Branch
-- 1 => MemRead
-- 2 => MemWrite
-- 3 => RegWrite
-- 4 => MemToReg
-- MemWrite => 1
ialu : alu port map (CLK => CLK,
Control => alu_control,
Operand1 => alu_op1,
Operand2 => alu_op2,
Result1 => alu_r1,
Result2 => alu_r2,
Debug => alu_debug);
--
--iram : ram port map (CLK => CLK,
-- WE => ram_WE,
-- EN => ram_EN,
-- ADDR => ram_ADDR,
-- DI => ram_DI,
-- DO => ram_DO);
--
process(CLK)
variable pc : std_logic_vector(31 downto 0) := (others => '0');
variable currentState : CPUState := FetchDecode;
variable FD_currentIns : std_logic_vector(31 downto 0) := (others => '0');
variable waitCounter : std_logic_vector(5 downto 0) := (others => '0');
variable tconcat : std_logic_vector(31 downto 0);
variable ram_WE : std_logic := '0';
variable vlreg : std_logic_vector(31 downto 0) := (others => '0');
variable vlregAddr : std_logic_vector(4 downto 0) := (others => '0');
variable prev_op1 : STD_LOGIC_VECTOR (31 downto 0):= (others => '0');
variable prev_op2 : STD_LOGIC_VECTOR (31 downto 0):= (others => '0');
variable res1Reg : STD_LOGIC_vector (31 downto 0):= (others => '0');
variable res2Reg : STD_LOGIC_vector (31 downto 0):= (others => '0');
variable lo : std_logic_vector(31 downto 0) := (others => '0');
variable hi : std_logic_vector(31 downto 0) := (others => '0');
--Prefix indicates stage where value is fetched
variable EX_decodeRegOut : std_logic_vector(31 downto 0) := (others => '0');
variable EX_decodeControlSignals : std_logic_vector(4 downto 0) := (others => '0');
variable ALUW_alur1 : std_logic_vector(31 downto 0) := (others => '0');
variable ALUW_alur2 : std_logic_vector(31 downto 0) := (others => '0');
variable EX_decodeRegWBAddr : std_logic_vector(4 downto 0);
variable EX_assignO1 : std_logic_vector(32 downto 0) := (others => '0');
variable EX_assignO2 : std_logic_vector(32 downto 0) := (others => '0');
variable EX_assignRegOut : std_logic_vector(32 downto 0) := (others => '0');
variable sig_Branch : std_logic := '0';
variable sig_MemRead : std_logic := '0';
variable sig_MemWrite : std_logic := '0';
variable sig_RegWrite : std_logic := '0';
variable sig_MemToReg : std_logic := '0';
variable old_pc : std_logic_vector(31 downto 0);
variable jal_pc : std_logic_vector(31 downto 0);
begin
if falling_edge(CLK) then
vlreg := decode_lreg;
vlregAddr := decode_lregAddr;
if(prev_op1 /= cpu_op1) then
prev_op1 := cpu_op1;
elsif(prev_op2 /= cpu_op2) then
prev_op2 := cpu_op2;
end if;
if waitCounter = b"000000" then
alu_control <= decode_AluControl;
if currentState = FetchDecode then
FD_currentIns := rom_DATA;
--DCurrentIns <= FD_currentIns;
-- DCPUState <= (others => '0');
-- Check if instruction is a jump
if FD_currentIns(31 downto 26) = b"000010" then
--pc := b"0000" & CurrentIns(25 downto 0) & b"00";
pc := b"000000" & FD_currentIns(25 downto 0);
currentState := Execute;
decode_currentInstruction <= FD_currentIns;
elsif FD_currentIns(31 downto 26) = b"000011" then
-- check for JAL
jal_pc := pc;
-- decode_WriteAddr <= b"11111";
-- decode_WriteData <= std_logic_vector(unsigned(pc) + 1);
-- decode_RegWrite <= '1';
pc := x"000000" & b"00" & FD_currentIns(5 downto 0);
currentState := FetchDecode;
else
pc := std_logic_vector(unsigned(pc) + 1);
-- feed cur Ins to decode. decode will give alu appropriate operands by nnext clk cycle
decode_currentInstruction <= FD_currentIns;
currentState := Execute;
end if;
EX_currentIns <= FD_currentIns;
end if;
if currentState = Execute then
DCurrentIns2 <= EX_currentIns;
--Assignment of propagated values
EX_decodeControlSignals := decode_controlSignals;
EX_decodeRegWBAddr := decode_RegWBAddr;
--Main execution
alu_op1 <= decode_AluOP1;
alu_op2 <= decode_AluOP2;
EX_assignO1 := decode_AluOP1 & b"0";
EX_assignO2 := decode_AluOP2 & b"0";
--change if store word to latest value
--EX_decodeRegOut := decode_registerOut;
EX_assignRegOut := decode_registerOut & b"0";
-- First stage gathers data to be forwaded
-- Dont know where to forward in this stage
-- Hence, gathered data is assigned to temp (EX_assignO1 and EX_assignO2) vars
if END_decodeRegWBAddr = EX_currentIns(25 downto 21) and
END_alur1(0) /= 'U' then
--Checking if END is R-type.
if END_decodeControlSignals(3) = '1' and
END_decodeControlSignals(4) = '0' then
--alu_op2 <= END_alur1;
EX_assignO2 := END_alur1 & b"0";
-- Checking if LW
elsif END_decodeControlSignals(3) = '1' and
END_decodeControlSignals(4) = '1' then
-- Sets value to last value contained at rs + offset
--alu_op2 <= RAM3(to_integer(unsigned(END_alur1(3 downto 0)))) &
-- EX_assignO2 := RAM3(to_integer(unsigned(END_alur1(3 downto 0)))) &
-- RAM2(to_integer(unsigned(END_alur1(3 downto 0)))) &
-- RAM1(to_integer(unsigned(END_alur1(3 downto 0)))) &
-- RAM0(to_integer(unsigned(END_alur1(3 downto 0))));
EX_assignO2 := END_alur1 & b"1";
-- Checking if SW
elsif END_decodeControlSignals(2) = '1' then
-- MEM[$s + offset] = $t; advance_pc (4);
-- Syntax:
---sw $t, offset($s)
-- Don't care for R-type
end if;
end if;
if END_decodeRegWBAddr = EX_currentIns(20 downto 16) and
END_alur1(0) /= 'U' and
EX_currentIns(31 downto 26) /= b"001000" and
EX_currentIns(31 downto 26) /= op_SLTI and
EX_currentIns(31 downto 26) /= op_ORI
then
--Checking if END is R-type.
if END_decodeControlSignals(3) = '1' and
END_decodeControlSignals(4) = '0' then
--alu_op1 <= END_alur1;
EX_assignO1 := END_alur1 & b"0";
EX_assignRegOut := END_alur1 & b"0";
-- Checking if LW
elsif END_decodeControlSignals(3) = '1' and
END_decodeControlSignals(4) = '1' then
DRegOut <= (others => '1');
-- Sets value to last value contained at rs + offset
--alu_op1 <= RAM3(to_integer(unsigned(END_alur1(3 downto 0)))) &
-- EX_assignO1 := RAM3(to_integer(unsigned(END_alur1(3 downto 0)))) &
-- RAM2(to_integer(unsigned(END_alur1(3 downto 0)))) &
-- RAM1(to_integer(unsigned(END_alur1(3 downto 0)))) &
-- RAM0(to_integer(unsigned(END_alur1(3 downto 0))));
EX_assignO1 := END_alur1 & b"1";
EX_assignRegOut := END_alur1 & b"1";
-- Checking if SW
elsif END_decodeControlSignals(2) = '1' then
-- MEM[$s + offset] = $t; advance_pc (4);
-- Syntax:
---sw $t, offset($s)
-- Don't care for R-type
end if;
end if;
-- If value is written towards the end of a WB stage
if WB_decodeRegWBAddr = EX_currentIns(25 downto 21) and
WB_alur1(0) /= 'U' then
--Checking if WB is R-type.
if WB_decodeControlSignals(3) = '1' and
WB_decodeControlSignals(4) = '0' then
--alu_op2 <= WB_alur1;
EX_assignO2 := WB_alur1 & b"0";
-- Checking if LW
elsif WB_decodeControlSignals(3) = '1' and
WB_decodeControlSignals(4) = '1' then
-- Sets value to last value contained at rs + offset
--alu_op2 <= RAM3(to_integer(unsigned(WB_alur1(3 downto 0)))) &
-- EX_assignO2 := RAM3(to_integer(unsigned(WB_alur1(3 downto 0)))) &
-- RAM2(to_integer(unsigned(WB_alur1(3 downto 0)))) &
-- RAM1(to_integer(unsigned(WB_alur1(3 downto 0)))) &
-- RAM0(to_integer(unsigned(WB_alur1(3 downto 0))));
EX_assignO2 := WB_alur1 & b"1";
-- Checking if SW
elsif WB_decodeControlSignals(2) = '1' then
-- MEM[$s + offset] = $t; advance_pc (4);
-- Syntax:
---sw $t, offset($s)
-- Don't care for R-type
end if;
end if;
if WB_decodeRegWBAddr = EX_currentIns(20 downto 16) and
WB_alur1(0) /= 'U' and
EX_currentIns(31 downto 26) /= b"001000" and
EX_currentIns(31 downto 26) /= op_SLTI and
EX_currentIns(31 downto 26) /= op_ORI then
--Checking if END is R-type.
if WB_decodeControlSignals(3) = '1' and
WB_decodeControlSignals(4) = '0' then
--alu_op1 <= WB_alur1;
EX_assignO1 := WB_alur1 & b"0";
EX_assignRegOut := WB_alur1 & b"0";
-- Checking if LW
elsif WB_decodeControlSignals(3) = '1' and
WB_decodeControlSignals(4) = '1' then
-- Sets value to last value contained at rs + offset
--alu_op1 <= RAM3(to_integer(unsigned(WB_alur1(3 downto 0)))) &
-- EX_assignO1 := RAM3(to_integer(unsigned(WB_alur1(3 downto 0)))) &
-- RAM2(to_integer(unsigned(WB_alur1(3 downto 0)))) &
-- RAM1(to_integer(unsigned(WB_alur1(3 downto 0)))) &
-- RAM0(to_integer(unsigned(WB_alur1(3 downto 0))));
EX_assignO1 := WB_alur1 & b"1";
EX_assignRegOut := WB_alur1 & b"1";
-- Checking if SW
elsif WB_decodeControlSignals(2) = '1' then
-- MEM[$s + offset] = $t; advance_pc (4);
-- Syntax:
---sw $t, offset($s)
-- Don't care for R-type
end if;
end if;
--last change
-- If value is written towards the end of MEMWR stage
if MEMWR_decodeRegWBAddr = EX_currentIns(25 downto 21) and
MEMWR_alur1(0) /= 'U' then
--Checking if END is R-type.
if MEMWR_decodeControlSignals(3) = '1' and
MEMWR_decodeControlSignals(4) = '0' then
--alu_op2 <= MEMWR_alur1;
EX_assignO2 := MEMWR_alur1 & b"0";
-- Checking if LW
elsif MEMWR_decodeControlSignals(3) = '1' and
MEMWR_decodeControlSignals(4) = '1' then
-- Sets value to last value contained at rs + offset
--alu_op2 <= RAM3(to_integer(unsigned(MEMWR_alur1(3 downto 0)))) &
-- EX_assignO2 := RAM3(to_integer(unsigned(MEMWR_alur1(3 downto 0)))) &
-- RAM2(to_integer(unsigned(MEMWR_alur1(3 downto 0)))) &
-- RAM1(to_integer(unsigned(MEMWR_alur1(3 downto 0)))) &
-- RAM0(to_integer(unsigned(MEMWR_alur1(3 downto 0))));
EX_assignO2 := MEMWR_alur1 & b"1";
-- Checking if SW
elsif MEMWR_decodeControlSignals(2) = '1' then
-- MEM[$s + offset] = $t; advance_pc (4);
-- Syntax:
---sw $t, offset($s)
-- Don't care for R-type
end if;
end if;
if MEMWR_decodeRegWBAddr = EX_currentIns(20 downto 16) and
MEMWR_alur1(0) /= 'U' and
EX_currentIns(31 downto 26) /= b"001000" and
EX_currentIns(31 downto 26) /= op_SLTI and
EX_currentIns(31 downto 26) /= op_ORI then
--Checking if END is R-type.
if MEMWR_decodeControlSignals(3) = '1' and
MEMWR_decodeControlSignals(4) = '0' then
--alu_op1 <= MEMWR_alur1;
EX_assignO1 := MEMWR_alur1 & b"0";
EX_assignRegOut := MEMWR_alur1 & b"0";
-- Checking if LW
elsif MEMWR_decodeControlSignals(3) = '1' and
MEMWR_decodeControlSignals(4) = '1' then
-- Sets value to last value contained at rs + offset
--alu_op1 <= RAM3(to_integer(unsigned(MEMWR_alur1(3 downto 0)))) &
-- EX_assignO1 := RAM3(to_integer(unsigned(MEMWR_alur1(3 downto 0)))) &
-- RAM2(to_integer(unsigned(MEMWR_alur1(3 downto 0)))) &
-- RAM1(to_integer(unsigned(MEMWR_alur1(3 downto 0)))) &
-- RAM0(to_integer(unsigned(MEMWR_alur1(3 downto 0))));
EX_assignO1 := MEMWR_alur1 & b"1";
EX_assignRegOut := MEMWR_alur1 & b"1";
-- Checking if SW
elsif MEMWR_decodeControlSignals(2) = '1' then
-- MEM[$s + offset] = $t; advance_pc (4);
-- Syntax:
---sw $t, offset($s)
-- Don't care for R-type
end if;
end if;
-- If value is written towards the end of AluW stage
-- Unlike other stages, we check alu_r1 directly here because we use a
-- variable below and variables are sequentially assigned
if ALUW_decodeRegWBAddr = EX_currentIns(25 downto 21) and
ALUW_alur1(0) /= 'U' then
--Checking if END is R-type.
if ALUW_decodeControlSignals(3) = '1' and
ALUW_decodeControlSignals(4) = '0' then
--alu_op2 <= alu_r1;
EX_assignO2 := alu_r1 & b"0";
-- Checking if LW
elsif ALUW_decodeControlSignals(3) = '1' and
ALUW_decodeControlSignals(4) = '1' then
-- Sets value to last value contained at rs + offset
--alu_op2 <= RAM3(to_integer(unsigned(alu_r1(3 downto 0)))) &
-- EX_assignO2 := RAM3(to_integer(unsigned(alu_r1(3 downto 0)))) &
-- RAM2(to_integer(unsigned(alu_r1(3 downto 0)))) &
-- RAM1(to_integer(unsigned(alu_r1(3 downto 0)))) &
-- RAM0(to_integer(unsigned(alu_r1(3 downto 0))));
EX_assignO2 := alu_r1 & b"1";
-- Checking if SW
elsif ALUW_decodeControlSignals(2) = '1' then
-- MEM[$s + offset] = $t; advance_pc (4);
-- Syntax:
---sw $t, offset($s)
-- Don't care for R-type
end if;
end if;
-- AND HERE
if ALUW_decodeRegWBAddr = EX_currentIns(20 downto 16) and
ALUW_alur1(0) /= 'U' and
EX_currentIns(31 downto 26) /= b"001000" and
EX_currentIns(31 downto 26) /= op_SLTI and
EX_currentIns(31 downto 26) /= op_ORI then
--Checking if END is R-type.
if ALUW_decodeControlSignals(3) = '1' and
ALUW_decodeControlSignals(4) = '0' then
--alu_op1 <= alu_r1;
EX_assignO1 := alu_r1 & b"0";
EX_assignRegOut := alu_r1 & b"0";
-- Checking if LW
elsif ALUW_decodeControlSignals(3) = '1' and
ALUW_decodeControlSignals(4) = '1' then
-- Sets value to last value contained at rs + offset
--alu_op1 <= RAM3(to_integer(unsigned(alu_r1(3 downto 0)))) &
-- EX_assignO1 := RAM3(to_integer(unsigned(alu_r1(3 downto 0)))) &
-- RAM2(to_integer(unsigned(alu_r1(3 downto 0)))) &
-- RAM1(to_integer(unsigned(alu_r1(3 downto 0)))) &
-- RAM0(to_integer(unsigned(alu_r1(3 downto 0))));
EX_assignO1 := alu_r1 & b"1";
EX_assignRegOut := alu_r1 & b"1";
-- Checking if SW
elsif ALUW_decodeControlSignals(2) = '1' then
-- MEM[$s + offset] = $t; advance_pc (4);
-- Syntax:
---sw $t, offset($s)
-- Don't care for R-type
end if;
-- DRegOut <= (others => '0');
end if;
-- checking for MFHI
if (EX_currentIns(5 downto 0) = b"010000" and EX_currentIns(31 downto 26) = b"000000") then
-- If ALUW Ins is of the MUL/DIV family then we can't access HI directly as it hasn't been assigned yet
-- therefore we use alu_r1
if (ALUW_currentIns(5 downto 0) = b"011000" and ALUW_currentIns(31 downto 26) = b"000000") or
(ALUW_currentIns(5 downto 0) = b"011001" and ALUW_currentIns(31 downto 26) = b"000000") or
(ALUW_currentIns(5 downto 0) = b"011010" and ALUW_currentIns(31 downto 26) = b"000000") or
(ALUW_currentIns(5 downto 0) = b"011011" and ALUW_currentIns(31 downto 26) = b"000000") then
alu_op1 <= alu_r2;
else
-- Otherwise we just assign HI
alu_op1 <= HI;
end if;
-- checking for MMFLO
elsif (EX_currentIns(5 downto 0) = b"010010" and EX_currentIns(31 downto 26) = b"000000") then
if (ALUW_currentIns(5 downto 0) = b"011000" and ALUW_currentIns(31 downto 26) = b"000000") or
(ALUW_currentIns(5 downto 0) = b"011001" and ALUW_currentIns(31 downto 26) = b"000000") or
(ALUW_currentIns(5 downto 0) = b"011010" and ALUW_currentIns(31 downto 26) = b"000000") or
(ALUW_currentIns(5 downto 0) = b"011011" and ALUW_currentIns(31 downto 26) = b"000000") then
alu_op1 <= alu_r1;
else
alu_op1 <= LO;
end if;
-- checking for R type, or ADDI or SLTI
elsif EX_currentIns(31 downto 26) = b"000000" or
EX_currentIns(31 downto 26) = b"001000" or
EX_currentIns(31 downto 26) = op_SLTI or
EX_currentIns(31 downto 26) = op_ORI
then
case EX_assignO1(0) is
when '1' =>
case EX_assignO1(5 downto 1) is
when b"10010" => alu_op1<= prev_op1;
when b"10011" => alu_op1 <=prev_op2;
when others => alu_op1 <=
RAM3(to_integer(unsigned(EX_assignO1(4 downto 1)))) &
RAM2(to_integer(unsigned(EX_assignO1(4 downto 1)))) &
RAM1(to_integer(unsigned(EX_assignO1(4 downto 1)))) &
RAM0(to_integer(unsigned(EX_assignO1(4 downto 1))));
end case;
-- alu_op1 <=
-- RAM3(to_integer(unsigned(EX_assignO1(4 downto 1)))) &
-- RAM2(to_integer(unsigned(EX_assignO1(4 downto 1)))) &
-- RAM1(to_integer(unsigned(EX_assignO1(4 downto 1)))) &
-- RAM0(to_integer(unsigned(EX_assignO1(4 downto 1))));
when others => alu_op1 <= EX_assignO1(32 downto 1);
end case;
--alu_op1 <= EX_assignO1 ;
case EX_assignO2(0) is
when '1' =>
case EX_assignO2(5 downto 1) is
when b"10010" => alu_op2<= prev_op1;
when b"10011" => alu_op2 <=prev_op2;
when others => alu_op2 <=
RAM3(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
RAM2(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
RAM1(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
RAM0(to_integer(unsigned(EX_assignO2(4 downto 1))));
end case;
-- alu_op2 <= RAM3(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
-- RAM2(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
-- RAM1(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
-- RAM0(to_integer(unsigned(EX_assignO2(4 downto 1))));
when others => alu_op2 <= EX_assignO2(32 downto 1);
end case;
if EX_currentIns(5 downto 0) = b"000000" or
EX_currentIns(5 downto 0) = b"000010" or
EX_currentIns(5 downto 0) = b"000011" then
alu_op2 <= decode_AluOP2;
end if;
--alu_op2 <= EX_assignO2;
elsif EX_currentIns(31 downto 26) = b"100011" then
-- LOAD WORD
-- $t = MEM[$s + offset]; advance_pc (4);
--Syntax:
--lw $t, offset($s)
-- forward ONLY Rs (25 to 21).
case EX_assignO2(0) is
when '1' =>
case EX_assignO2(5 downto 1) is
when b"10010" => alu_op1<= prev_op1;
when b"10011" => alu_op1 <=prev_op2;
when others => alu_op1 <=
RAM3(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
RAM2(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
RAM1(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
RAM0(to_integer(unsigned(EX_assignO2(4 downto 1))));
end case;
-- alu_op1 <= RAM3(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
-- RAM2(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
-- RAM1(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
-- RAM0(to_integer(unsigned(EX_assignO2(4 downto 1))));
when others => alu_op1 <= EX_assignO2(32 downto 1);
end case;
--alu_op1 <= EX_assignO1;
elsif EX_currentIns(31 downto 26) = b"101011" then
--- STORE WORD
-- Both Rs, and Rt to be forwarded
-- Check for 'U' for unintialised valus
-- Priority system. Most recent change is applied
-- if 25-21 i.e , Rs, or the base addr. to be forwarded
-- if 20-16 i.e , rT, or value to be stored, to be forwarded
-- EX_assign2 should contain new val of 25-21, rS
case EX_assignO2(0) is
when '1' =>
case EX_assignO2(5 downto 1) is
when b"10010" => alu_op1<= prev_op1;
when b"10011" => alu_op1 <=prev_op2;
when others => alu_op1 <=
RAM3(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
RAM2(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
RAM1(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
RAM0(to_integer(unsigned(EX_assignO2(4 downto 1))));
end case;
-- alu_op1 <=
-- RAM3(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
-- RAM2(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
-- RAM1(to_integer(unsigned(EX_assignO2(4 downto 1)))) &
-- RAM0(to_integer(unsigned(EX_assignO2(4 downto 1))));
when others => alu_op1 <= EX_assignO2(32 downto 1);
end case;
--alu_op1 <= EX_assignO2;
--rT val being forwarded
-- EX_assign1 should contain new val of 20-16, rT
if EX_assignRegOut(0) = '1' then
if EX_assignRegOut(5 downto 1) = b"10010" then
EX_decodeRegOut := prev_op1;
elsif EX_assignRegOut(5 downto 1) = b"10011" then
EX_decodeRegOut := prev_op2;
else
EX_decodeRegOut :=
RAM3(to_integer(unsigned(EX_assignRegOut(4 downto 1)))) &
RAM2(to_integer(unsigned(EX_assignRegOut(4 downto 1)))) &
RAM1(to_integer(unsigned(EX_assignRegOut(4 downto 1)))) &
RAM0(to_integer(unsigned(EX_assignRegOut(4 downto 1))));
end if;
else
EX_decodeRegOut := EX_assignRegOut(32 downto 1);
end if;
--EX_decodeRegOut := EX_assignO1;
-- If value is written to a register towards the end of a instruction
-- TODO. Check whether ControlSignal checks are required?
-- else
-- alu_op1 <= EX_assignO1(32 downto 1);
-- alu_op2 <= EX_assignO2(32 downto 1);
end if;
waitCounter := decode_WaitFor;
currentState := AluWait;
--Signal propagation
ALUW_currentIns <= EX_currentIns;
ALUW_decodeRegOut <= EX_decodeRegOut;
ALUW_decodeControlSignals <= EX_decodeControlSignals;
ALUW_decodeRegWBAddr <= EX_decodeRegWBAddr;
end if;
if currentState = AluWait then
DAluR1 <= alu_r1;
DAluR2 <= alu_r2;
DCurrentIns3 <= ALUW_currentIns;
ALUW_alur1 := alu_r1;
ALUW_alur2 := alu_r2;
Dregout <= alu_r2;
-- IF ALUW_Currennt Ins is of mul/div family
if (ALUW_currentIns(5 downto 0) = b"011000" and ALUW_currentIns(31 downto 26) = b"000000") or
(ALUW_currentIns(5 downto 0) = b"011001" and ALUW_currentIns(31 downto 26) = b"000000") or
(ALUW_currentIns(5 downto 0) = b"011010" and ALUW_currentIns(31 downto 26) = b"000000") or
(ALUW_currentIns(5 downto 0) = b"011011" and ALUW_currentIns(31 downto 26) = b"000000") then
lo := ALUW_alur1;
hi := ALUW_alur2;
end if;
if (ALUW_currentIns(20 downto 0) = b"000000000000000001000" and
ALUW_CurrentIns(31 downto 26) = b"000000") then
-- JR
pc := ALUW_decodeRegOut;
currentState := MemWR;
elsif (ALUW_CurrentIns(20 downto 0) = b"000001111100000001001" and
ALUW_CurrentIns(31 downto 26) = b"000000") then
-- JALR
-- TODO. JALR Writing back will lead to Hazard!!!!
old_pc := x"000000" & b"00" & pc(5 downto 0);
pc := ALUW_decodeRegOut;
currentState := MemWR;
else
-- DCPUState <= (5 => '1', others => '0');
currentState := MemWR;
end if;
--Signal propagation
MEMWR_currentIns <= ALUW_currentIns;
MEMWR_decodeRegOut <= ALUW_decodeRegOut;
MEMWR_decodeControlSignals <= ALUW_decodeControlSignals;
MEMWR_decodeRegWBAddr <= ALUW_decodeRegWBAddr;
MEMWR_alur1 <= ALUW_alur1;
MEMWR_alur2 <= ALUW_alur2;
end if;
if currentState = MemWR then
-- DCPUState <= (1 => '1', others => '0');
--Assignment of propagated values
DCurrentIns4 <= MEMWR_currentIns;
sig_Branch := MEMWR_decodeControlSignals(0);
sig_MemRead := MEMWR_decodeControlSignals(1);
sig_MemWrite := MEMWR_decodeControlSignals(2);
sig_RegWrite := MEMWR_decodeControlSignals(3);
sig_MemToReg := MEMWR_decodeControlSignals(4);
--Main execution
if sig_Branch = '0' and
sig_MemRead = '0' and
sig_MemWrite = '0' then
-- DO nothing. Update to next stage
-- R Type
currentState := WriteBack;
elsif sig_Branch = '0' and
sig_MemRead = '1' and
sig_MemWrite = '0' then
-- lw
currentState := WriteBack;
elsif sig_Branch = '0' and
sig_MemRead = '0' and
sig_MemWrite = '1' then
-- -- sw
-- -- registerOut sends data from rt
if MEMWR_alur1(5 downto 0) = b"010000" then
res1Reg := MEMWR_decodeRegOut;
elsif MEMWR_alur1(5 downto 0) = b"010001" then
res2Reg := MEMWR_decodeRegOut;
else
RAM0(to_integer(unsigned(MEMWR_alur1(3 downto 0)))) <= MEMWR_decodeRegOut(7 downto 0);
RAM1(to_integer(unsigned(MEMWR_alur1(3 downto 0)))) <= MEMWR_decodeRegOut(15 downto 8);
RAM2(to_integer(unsigned(MEMWR_alur1(3 downto 0)))) <= MEMWR_decodeRegOut(23 downto 16);
RAM3(to_integer(unsigned(MEMWR_alur1(3 downto 0)))) <= MEMWR_decodeRegOut(31 downto 24);
-- DMemOut <= read_ram_at(RAM, alu_r1);
-- DMemAddr <= alu_r1;
end if;
currentState := WriteBack;
elsif sig_Branch = '1' then
-- BEQ
-- TODO ... flushing
if MEMWR_currentIns(31 downto 26) = b"000100" then
if MEMWR_alur1 = x"00000001" then
-- shift branch offset by 2
-- tconcat := CurrentIns(15 downto 0) & b"00";
-- TODO sign extension
tconcat := x"0000" & MEMWR_currentIns(15 downto 0);
-- add offset to pc
pc := std_logic_vector( signed(pc) + signed( tconcat) - 3);
end if;
-- BGEZ
elsif MEMWR_currentIns(31 downto 26) = b"000001" then
if MEMWR_alur1 = X"00000000" then
tconcat := x"0000" & MEMWR_currentIns(15 downto 0);
-- if MEMWR_currentIns(20 downto 16) = b"10001" then
-- -- BGEZAL
-- -- TODO Write issue. May lead to HAZARD!!!
-- -- Possible fix by waiting?
-- decode_WriteAddr <= b"11111";
-- decode_WriteData <= std_logic_vector( signed(pc)- 3);
-- decode_RegWrite <= '1';
--
-- end if;
-- add offset to pc
pc := std_logic_vector( signed(pc) + signed( tconcat) - 3);
end if;
end if;
currentState := WriteBack;
end if;
--Signal propagation
WB_currentIns <= MEMWR_currentIns;
WB_decodeRegOut <= MEMWR_decodeRegOut;
WB_alur1 <= MEMWR_alur1;
WB_alur2 <= MEMWR_alur2;
WB_decodeControlSignals <= MEMWR_decodeControlSignals;
WB_decodeRegWBAddr <= MEMWR_decodeRegWBAddr;
end if;
if currentState = WriteBack then
-- DCPUState <= (2 => '1', others => '0');
--ram_WE <= '0';
-- R Type
DCurrentIns5 <= WB_currentIns;
--Assignment of propagated values
sig_Branch := WB_decodeControlSignals(0);
sig_MemRead := WB_decodeControlSignals(1);
sig_MemWrite := WB_decodeControlSignals(2);
sig_RegWrite := WB_decodeControlSignals(3);
sig_MemToReg := WB_decodeControlSignals(4);
--Main execution
if sig_RegWrite = '1' and
sig_MemToReg = '0' then
-- if instruction is mul/div family then place result in hi/lo
--Dregout <= WB_alur1;
decode_regWrite <= '1';
-- write to rd
-- also goes here with LUI
decode_WriteAddr <= WB_decodeRegWBAddr;
-- send WB_alur1
decode_WriteData <= WB_alur1;
elsif sig_RegWrite = '1' and
sig_MemToReg = '1' then
-- lw
-- RT for I type instructions
decode_regWrite <= '1';
decode_WriteAddr <= WB_decodeRegWBAddr;
--read_ram_at(RAM3,RAM2,RAM1,RAM0,WB_alur1(3 downto 0));
case WB_alur1(4 downto 0) is
when b"10010" => decode_WriteData <= prev_op1;
when b"10011" => decode_WriteData <=prev_op2;
when others => decode_WriteData <=
RAM3(to_integer(unsigned(WB_alur1(3 downto 0)))) &
RAM2(to_integer(unsigned(WB_alur1(3 downto 0)))) &
RAM1(to_integer(unsigned(WB_alur1(3 downto 0)))) &
RAM0(to_integer(unsigned(WB_alur1(3 downto 0))));
end case;
-- DMemOut <= read_ram_at(RAM, WB_alur1);
-- DMemAddr <= WB_alur1;
--
end if;
if WB_currentIns(20 downto 16) = b"10001" and
WB_currentIns(31 downto 26) = b"000001" and
WB_alur1(0) = '0' then
-- BGEZAL
-- TODO Write issue. May lead to HAZARD!!!
-- Possible fix by waiting?
tconcat := x"0000" & WB_currentIns(15 downto 0);
decode_WriteAddr <= b"11111";
decode_WriteData <= std_logic_vector( (signed(pc) - signed(tconcat) - 1) sll 2);
decode_RegWrite <= '1';
end if;
if (WB_CurrentIns(20 downto 0) = b"000001111100000001001" and
WB_CurrentIns(31 downto 26) = b"000000") then
-- JALR
-- TODO. JALR Writing back will lead to Hazard!!!!
decode_WriteAddr <= b"11111";
decode_WriteData <= std_logic_vector((unsigned(old_pc) - 2) sll 2);
decode_RegWrite <= '1';
elsif WB_currentIns(31 downto 26) = b"000011" then
-- check for JAL
decode_WriteAddr <= b"11111";
decode_WriteData <= std_logic_vector((unsigned(jal_pc) + 1) sll 2);
decode_RegWrite <= '1';
end if;
END_currentIns <= WB_currentIns;
END_decodeRegOut <= WB_decodeRegOut;
END_decodeControlSignals <= WB_decodeControlSignals;
END_alur1 <= WB_alur1;
END_alur2 <= WB_alur2;
END_decodeRegWBAddr <= WB_decodeRegWBAddr;
currentState := FetchDecode;
end if;
else
waitCounter := std_logic_vector(unsigned(waitCounter) - 1);
end if;
end if;
rom_ADDR <= pc;
--DRegOutAddr <= vlregAddr;
--DRegOut <= decode_registerOut;-- vlreg;
--DMeMOut <= pc;
DOutput <= res1Reg;
DOutput2 <= res2Reg;
end process;
DAlu1 <= alu_op1;
DAlu2 <= alu_op2;
--DAlu1 <= Decode_ALUOP1;
--DAlu2 <= Decode_ALUOP2;
DRegOutAddr <= alu_control(4 downto 0);