-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatapath.vhd
executable file
·179 lines (162 loc) · 5.15 KB
/
datapath.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
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_signed.all;
entity datapath is
port(
clk : in std_logic;
reset : in std_logic;
data_write : in std_logic;
pc_sel : in std_logic_vector(1 downto 0);
branch_type : in std_logic_vector(1 downto 0);
func : in std_logic_vector(1 downto 0);
reg_dst : in std_logic;
reg_write : in std_logic;
add_sub : in std_logic;
logic_func : in std_logic_vector(1 downto 0);
alu_src : in std_logic;
reg_in_src : in std_logic;
overflow : out std_logic;
zero : out std_logic;
opcode : out std_logic_vector(5 downto 0);
op_function : out std_logic_vector(5 downto 0);
rs_out, rt_out, pc_out : out std_logic_vector(3 downto 0)
);
end datapath;
architecture arch_datapath of datapath is
component instr_cache is
port(
address : in std_logic_vector(4 downto 0);
data : out std_logic_vector(31 downto 0)
);
end component;
component d_cache is
port(
address : in std_logic_vector(4 downto 0);
reset : in std_logic;
din : in std_logic_vector(31 downto 0);
clk : in std_logic;
data_write : in std_logic;
data : out std_logic_vector(31 downto 0)
);
end component;
component next_address is
port(
rt, rs : in std_logic_vector(31 downto 0);
pc : in std_logic_vector(31 downto 0);
target_address : in std_logic_vector(25 downto 0);
branch_type : in std_logic_vector(1 downto 0);
pc_sel : in std_logic_vector(1 downto 0);
next_pc : out std_logic_vector(31 downto 0)
);
end component;
component alu is
port(
x, y : in std_logic_vector(31 downto 0);
add_sub : in std_logic;
logic_func : in std_logic_vector(1 downto 0);
func : in std_logic_vector(1 downto 0);
output : out std_logic_vector(31 downto 0);
overflow : out std_logic;
zero : out std_logic
);
end component;
component regfile is
port(
din : in std_logic_vector(31 downto 0);
reset : in std_logic;
clk : in std_logic;
write : in std_logic;
read_a : in std_logic_vector(4 downto 0);
read_b : in std_logic_vector(4 downto 0);
write_address : in std_logic_vector(4 downto 0);
out_a : out std_logic_vector(31 downto 0);
out_b : out std_logic_vector(31 downto 0)
);
end component;
signal immediate_sign_extended : std_logic_vector(31 downto 0);
signal pc_reg : std_logic_vector(31 downto 0) := "00000000000000000000000000000000";
signal pc_reg_next : std_logic_vector(31 downto 0);
signal instruction : std_logic_vector(31 downto 0);
signal regfile_mux : std_logic_vector(4 downto 0);
signal rs : std_logic_vector(4 downto 0);
signal rt : std_logic_vector(4 downto 0);
signal rd : std_logic_vector(4 downto 0);
--signal opcode : std_logic_vector(5 downto 0);
signal target_address : std_logic_vector(25 downto 0);
signal immediate_value : std_logic_vector(15 downto 0);
--signal op_function : std_logic_vector(5 downto 0);
signal out_a : std_logic_vector(31 downto 0);
signal out_b : std_logic_vector(31 downto 0);
signal alu_mux : std_logic_vector(31 downto 0);
signal alu_output : std_logic_vector(31 downto 0);
signal dcache_mux : std_logic_vector(31 downto 0);
signal dcache_output : std_logic_vector(31 downto 0);
begin
rs_out <= out_a(3 downto 0);
rt_out <= out_b(3 downto 0);
pc_out <= pc_reg(3 downto 0);
immediate_value <= instruction(15 downto 0);
rs <= instruction(25 downto 21);
rt <= instruction(20 downto 16);
rd <= instruction(15 downto 11);
opcode <= instruction(31 downto 26);
target_address <= instruction(25 downto 0);
immediate_value <= instruction(15 downto 0);
op_function <= instruction(5 downto 0);
regfile_mux <= rt when reg_dst = '0' else rd;
alu_mux <= out_b when alu_src = '0' else immediate_sign_extended;
dcache_mux <= dcache_output when reg_in_src = '0' else alu_output;
immediate_sign_extended <= immediate_value & "0000000000000000" when func = "00" else
"1111111111111111" & immediate_value when (func = "01" or func = "10") and immediate_value(15) = '1' else
"0000000000000000" & immediate_value;
process(pc_reg, pc_reg_next, clk)
begin
if(reset = '1') then
pc_reg <= (others => '0');
elsif(clk = '1' and clk'event) then
pc_reg <= pc_reg_next;
end if;
end process;
next_address_inst : next_address port map(
rt => out_b,
rs => out_a,
pc => pc_reg,
target_address => target_address,
branch_type => branch_type,
pc_sel => pc_sel,
next_pc => pc_reg_next
);
instr_cache_inst: instr_cache port map(
address => pc_reg(4 downto 0),
data => instruction
);
regfile_inst: regfile port map(
din => dcache_mux,
reset => reset,
clk => clk,
write => reg_write,
read_a => rs,
read_b => rt,
write_address => regfile_mux,
out_a => out_a,
out_b => out_b
);
alu_inst: alu port map(
x => out_a,
y => alu_mux,
add_sub => add_sub,
logic_func => logic_func,
func => func,
output => alu_output,
overflow => overflow,
zero => zero
);
d_cache_inst: d_cache port map(
address => alu_output(4 downto 0),
reset => reset,
din => out_b,
clk => clk,
data_write => data_write,
data => dcache_output
);
end arch_datapath;