-
Notifications
You must be signed in to change notification settings - Fork 1
/
datapath.vhd
293 lines (241 loc) · 8.23 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
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
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity datapath is -- This datapath will be used with a controller.
generic (
width : positive := 32);
port(
clk : in std_logic;
rst : in std_logic;
alu_reg_en : in std_logic;
alu_a_mux_sel : in std_logic_vector(1 downto 0);
alu_b_mux_sel : in std_logic_vector(1 downto 0);
inst_reg_en : in std_logic;
pc_mux_sel : in std_logic_vector(1 downto 0);
mem_reg_en : in std_logic;
mem_mux_sel : in std_logic;
data_mux_sel : in std_logic;
dest_mux_sel : in std_logic;
a_reg_en : in std_logic;
b_reg_en : in std_logic;
ram_wren : in std_logic; -- Read and Write enable for the RAM.
reg_file_wren : in std_logic; -- Read and Write enable for the register file.
alu_op : in std_logic_vector(7 downto 0); -- Op Codes coming from CONTROLLER.
pc_write_cond : in std_logic; -- Comes from controller.
pc_write : in std_logic; -- Comes from controller.
lo_reg_en : in std_logic;
hi_reg_en : in std_logic;
op_code : out std_logic_vector(5 downto 0)
);
end datapath;
architecture str of datapath is
signal inst_reg_out : std_logic_vector(WIDTH-1 downto 0);
signal alu_reg_out : std_logic_vector(WIDTH-1 downto 0);
signal pc_reg_out : std_logic_vector(WIDTH-1 downto 0);
signal mem_reg_out : std_logic_vector(WIDTH-1 downto 0);
signal a_reg_out : std_logic_vector(WIDTH-1 downto 0);
signal b_reg_out : std_logic_vector(WIDTH-1 downto 0);
signal alu_out : std_logic_vector(WIDTH-1 downto 0);
signal sign_ext_out : std_logic_vector(WIDTH-1 downto 0);
signal sl_branch_out : std_logic_vector(WIDTH-1 downto 0);
signal sl_jump_out : std_logic_vector(WIDTH-1 downto 0);
signal alu_b_mux_out : std_logic_vector(WIDTH-1 downto 0);
signal alu_a_mux_out : std_logic_vector(WIDTH-1 downto 0);
signal pc_mux_out : std_logic_vector(WIDTH-1 downto 0);
signal mem_mux_out : std_logic_vector(WIDTH-1 downto 0);
signal data_mux_out : std_logic_vector(WIDTH-1 downto 0);
signal dest_mux_out : std_logic_vector(4 downto 0);
signal reg_a_file_out : std_logic_vector(WIDTH-1 downto 0);
signal reg_b_file_out : std_logic_vector(WIDTH-1 downto 0);
signal ram_out : std_logic_vector(WIDTH-1 downto 0);
signal alu_sel : std_logic_vector(7 downto 0);
signal z_flag : std_logic;
signal pc_reg_en : std_logic;
signal lo_reg_in : std_logic_vector(WIDTH-1 downto 0);
signal lo_reg_out : std_logic_vector(WIDTH-1 downto 0);
signal hi_reg_in : std_logic_vector(WIDTH-1 downto 0);
signal hi_reg_out : std_logic_vector(WIDTH-1 downto 0);
signal alu_branch_out : std_logic_vector(WIDTH-1 downto 0);
signal inc_number : std_logic_vector(WIDTH-1 downto 0);
signal arb_number : std_logic_vector(WIDTH-1 downto 0);
signal pc_mux_in3 : signed(WIDTH-1 downto 0);
signal sl_in : unsigned(WIDTH-1 downto 0);
begin
ALU : entity work.alu -- The ALU entity.
port map(
input1 => unsigned(alu_a_mux_out),
input2 => unsigned(alu_b_mux_out),
sel => alu_sel,
output => alu_out,
output_lo => lo_reg_in,
output_hi => hi_reg_in,
branch_out=> alu_branch_out,
zout => z_flag
);
ALU_REG : entity work.reg -- The ALU output register.
port map(
clk => clk,
rst => rst,
input => alu_out,
output => alu_reg_out,
en => alu_reg_en
);
ALU_DEC : entity work.alu_decoder -- The ALU Decoder.
port map(
inst_reg_out => inst_reg_out(5 downto 0),
alu_op => alu_op(7 downto 0),
alu_sel => alu_sel
);
INST_REG : entity work.reg -- The instruction register.
port map(
clk => clk,
rst => rst,
input => ram_out,
output => inst_reg_out,
en => inst_reg_en
);
PC_REG : entity work.reg -- The PC register.
port map(
clk => clk,
rst => rst,
input => pc_mux_out,
output => pc_reg_out,
en => pc_reg_en
);
MEM_REG : entity work.reg -- Memory register, holds current memory address. Used to calculate jumps and branches.
port map(
clk => clk,
rst => rst,
input => ram_out,
output => mem_reg_out,
en => mem_reg_en
);
A_REG : entity work.reg -- The output register for output A of register file.
port map(
clk => clk,
rst => rst,
input => reg_a_file_out,
output => a_reg_out,
en => a_reg_en
);
B_REG : entity work.reg -- The output register for output B of register file.
port map(
clk => clk,
rst => rst,
input => reg_b_file_out,
output => b_reg_out,
en => b_reg_en
);
SIGN_EXT : entity work.sign_ext -- This module does sign extension. Could also use ALU for this.
port map(
input => unsigned(inst_reg_out((WIDTH/2)-1 downto 0)),
output => sign_ext_out
);
SHIFT_LEFT_BRANCH : entity work.left_shift -- This module performs a left shift twice.
port map(
input => unsigned(sign_ext_out),
output => sl_branch_out
);
SHIFT_LEFT_JUMP : entity work.left_shift -- This module performs a left shift twice.
port map(
input => sl_in,
output => sl_jump_out
);
MEM_MUX : entity work.mux_2x1 -- The mux that feeds the memory register.
port map(
in1 => pc_reg_out,
in2 => alu_reg_out,
sel => mem_mux_sel,
output => mem_mux_out
);
DEST_MUX : entity work.mux_2x1 -- The mux that feeds the destination register in the register file.
generic map (width => 5)
port map(
in1 => inst_reg_out(20 downto 16),
in2 => inst_reg_out(15 downto 11),
sel => dest_mux_sel,
output => dest_mux_out
);
DATA_MUX : entity work.mux_2x1 -- The mux that feeds the data input to the destination register.
port map(
in1 => alu_reg_out,
in2 => mem_reg_out,
sel => data_mux_sel,
output => data_mux_out
);
ALU_A_MUX : entity work.mux_4x1 -- The mux that feeds the A input to the ALU.
port map(
in1 => pc_reg_out,
in2 => a_reg_out,
in3 => hi_reg_out,
in4 => lo_reg_out,
sel => alu_a_mux_sel,
output => alu_a_mux_out
);
ALU_B_MUX : entity work.mux_4x1 -- The mux that feeds the B input to the ALU.
port map(
in1 => b_reg_out,
in2 => inc_number,
in3 => sign_ext_out,
in4 => sl_branch_out,
sel => alu_b_mux_sel,
output => alu_b_mux_out
);
PC_MUX : entity work.mux_4x1 -- The mux that feeds the PC register.
port map(
in1 => alu_branch_out,
in2 => alu_reg_out,
in3 => std_logic_vector(pc_mux_in3),
in4 => arb_number,
sel => pc_mux_sel,
output => pc_mux_out
);
PC_EN : entity work.pc_write -- Logic that enables the PC register.
port map(
z_flag => z_flag,
pc_write_cond => pc_write_cond,
pc_write => pc_write,
output => pc_reg_en
);
REG_FILE : entity work.register_file -- The register file contains all 32 registers.
port map(
output1 => reg_a_file_out,
output2 => reg_b_file_out,
input => data_mux_out,
wren => reg_file_wren,
reg1_source => inst_reg_out(25 downto 21),
reg2_source => inst_reg_out(20 downto 16),
reg3_dest => dest_mux_out,
clk => clk,
rst => rst
);
HI_REG : entity work.reg
port map(
clk => clk,
rst => rst,
input => hi_reg_in,
output => hi_reg_out,
en => hi_reg_en
);
LO_REG : entity work.reg
port map(
clk => clk,
rst => rst,
input => lo_reg_in,
output => lo_reg_out,
en => lo_reg_en
);
RAM : entity work.ram_1 -- Not sure if the RAM implementation is the correct one but needed to compile datapath.
port map(
address => mem_mux_out(13 downto 0),
clock => clk,
data => b_reg_out,
wren => ram_wren,
q => ram_out
);
op_code <= inst_reg_out(31 downto 26);
inc_number <= x"0000000" & "0100";
arb_number <= x"00000000";
pc_mux_in3 <= signed(sl_jump_out) + signed(pc_reg_out);
sl_in <= "000000" & unsigned(inst_reg_out(25 downto 0));
end str;