Repository navigation
Expand file tree
/
Copy pathCU.vhd
More file actions
181 lines (139 loc) · 6.04 KB
/
Copy pathCU.vhd
File metadata and controls
181 lines (139 loc) · 6.04 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
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use WORK.constants.all;
use WORK.functions.all;
entity CU is
port(
clk: IN std_logic;
rst: IN std_logic;
opcode: IN std_logic_vector(OP_CODE_SIZE-1 downto 0); -- opcode
func: IN std_logic_vector(FUNC_SIZE-1 downto 0); -- concat between func3 and func7 (7&3)
--stage1(DEC)
RF1: OUT std_logic; -- enable read from port_1 of RF MSB of CW
RF2: OUT std_logic; -- enable read from port_2 of RF
EN1: OUT std_logic; -- enable RF
--stage2(EXE)
S1: OUT std_logic; -- mux_1 selection signal (between IMM(0) and operand_1(1))
S2: OUT std_logic; -- mux_2 selection signal (between IMM(1) and operand_2(0))
ALU3: OUT std_logic; -- alu func sel bit 3
ALU2: OUT std_logic; -- alu func sel bit 2
ALU1: OUT std_logic; -- alu func sel bit 1
ALU0: OUT std_logic; -- alu func sel bit 0
--stage3(MEM)
LnS: OUT std_logic; -- Load notStore from main memory
EN3: OUT std_logic; -- dram enable
--stage4(WB)
S3: OUT std_logic; -- write back mux selection signal
WF1: OUT std_logic; -- enable write for RF
--others
Ld: OUT std_logic -- THIS IS EXTRA and indicates that the instr in ALU is a load. Used for the hazard detection.
);
end CU;
architecture Behavioral of CU is
signal cw1: std_logic_vector(CW_SIZE-1 downto 0); -- this is the cw that will be used in the stage 1(DEC)
signal cw2: std_logic_vector(CW_SIZE-1 downto 0); -- this is the cw that will be used in the stage 2(EXE)
signal cw3: std_logic_vector(CW_SIZE-1 downto 0); -- this is the cw that will be used in the stage 3(MEM)
signal cw4: std_logic_vector(CW_SIZE-1 downto 0); -- this is the cw that will be used in the stage 3(WB)
begin
pipelineProc: Process(clk,rst)
begin
if(rst = '0') then
cw2<=(others=>'0');
cw3<=(others=>'0');
cw4<=(others=>'0');
elsif (rising_edge(clk)) then
--shifting of the instructions in the pipeline
cw2<=cw1;
cw3<=cw2;
cw4<=cw3;
end if;
end Process pipelineProc;
-- concurrent assignement of the output signals considering for each stage the correct cw
--stage1
RF1 <= cw1(12);
RF2 <= cw1(11);
EN1 <= cw1(10);
--stage2
S1 <= cw2(9);
S2 <= cw2(8);
ALU3 <= cw2(7);
ALU2 <= cw2(6);
ALU1 <= cw2(5);
ALU0 <= cw2(4);
--stage3
LnS <= cw3(3);
EN3 <= cw3(2);
--stage4
S3 <= cw4(1);
WF1 <= cw4(0);
LD<=cw2(3); -- the load signal is given in the MEM stage but for the hazard detection we need to know if we have a load instr. in EXE so we get the load info from the second cw that usually is not yet given
cw_proc:Process(opcode, func, rst)
begin
if (rst ='0') then
cw1 <= "0000000000000";
elsif(unsigned(opcode) = unsigned(LTYPE)) then
case (to_integer(unsigned(func))) is
when 2 => cw1 <= "1011100001101"; --lw
when others => cw1<=(others=>'0');
end case;
elsif(unsigned(opcode) = unsigned(ITYPE)) then
case (to_integer(unsigned(func))) is
when 0 => cw1 <= "1011100000011"; --ADDI **
when 1 => cw1 <= "1011101010011"; --SLLI
when 2 => cw1 <= "1011111010011"; --SLTI
when 4 => cw1 <= "1011101000011"; --xori
when 5 => cw1 <= "1011101110011"; --SRAI
when 6 => cw1 <= "1011100110011"; --ori
when 7 => cw1 <= "1011100100011"; --andi
when others => cw1<=(others=>'0');
end case;
elsif(unsigned(opcode) = unsigned(PCTYPE)) then --auipc instr
case (to_integer(unsigned(func))) is
when 0 => cw1 <= "1011100000011"; --ADDI
when others => cw1<=(others=>'0');
end case;
elsif(unsigned(opcode) = unsigned(STYPE)) then -- store instr
case (to_integer(unsigned(func))) is
when 2 => cw1 <= "1111100000000"; --SW
when others => cw1<=(others=>'0');
end case;
elsif(unsigned(opcode) = unsigned(RTYPE)) then --R-Type instr 33
case (to_integer(unsigned(func))) is
when 0 => cw1<="11110" & ADDS & "0011"; --add **
when 1 => cw1<="11110" & LSL & "0011"; --lsl
when 4 => cw1<="11100" & BITXOR & "0011"; --xor **
when 5 => cw1<="11110" & LSR & "0011"; --lsr
when 6 => cw1<="11110" & BITOR & "0011"; --or
when 7 => cw1<="11110" & BITAND & "0011"; --and
when 261 => cw1<="11110" & ASR & "0011"; --asr
when 256 => cw1<="11110" & SUBS & "0011"; --sub **
when others => cw1<=(others=>'0');
end case;
elsif(unsigned(opcode) = unsigned(UTYPE)) then --lui instr
case (to_integer(unsigned(func))) is
when 0 => cw1 <= "1011100000011"; --ADDI
when others => cw1<=(others=>'0');
end case;
elsif(unsigned(opcode) = unsigned(SBTYPE)) then --branch type
case (to_integer(unsigned(func))) is
when 0 => cw1 <= "1110000000011"; --BEQ
when 1 => cw1 <= "1110000000011"; --BNE
when 5 => cw1 <= "1110000000011"; --BGE
when others => cw1<=(others=>'0');
end case;
elsif(unsigned(opcode) = unsigned(JTYPE)) then
case (to_integer(unsigned(func))) is
when 0 => cw1 <= "0110000000011"; --Jalr
when others => cw1<=(others=>'0');
end case;
elsif(unsigned(opcode) = unsigned(UJTYPE)) then
case (to_integer(unsigned(func))) is
when 0 => cw1 <= "1010100100011"; --Jal
when others => cw1<=(others=>'0');
end case;
else
cw1<=(others=>'0');
end if;
end process cw_proc;
end Behavioral;