1 |
13 |
serginhofr |
library ieee;
|
2 |
|
|
use ieee.std_logic_1164.all;
|
3 |
|
|
use ieee.std_logic_unsigned.all;
|
4 |
|
|
use ieee.std_logic_arith.all;
|
5 |
|
|
|
6 |
|
|
entity datapath is
|
7 |
|
|
port ( clock: in std_logic;
|
8 |
|
|
reset: in std_logic;
|
9 |
|
|
|
10 |
|
|
stall: in std_logic;
|
11 |
|
|
busy: in std_logic;
|
12 |
|
|
|
13 |
|
|
irq_vector: in std_logic_vector(31 downto 0);
|
14 |
|
|
irq: in std_logic;
|
15 |
|
|
irq_ack: out std_logic;
|
16 |
|
|
|
17 |
|
|
inst_addr: out std_logic_vector(31 downto 0);
|
18 |
|
|
inst_in: in std_logic_vector(31 downto 0);
|
19 |
|
|
|
20 |
|
|
data_addr: out std_logic_vector(31 downto 0);
|
21 |
|
|
data_in: in std_logic_vector(31 downto 0);
|
22 |
|
|
data_out: out std_logic_vector(31 downto 0);
|
23 |
|
|
data_w: out std_logic_vector(3 downto 0);
|
24 |
|
|
data_access: out std_logic
|
25 |
|
|
);
|
26 |
|
|
end datapath;
|
27 |
|
|
|
28 |
|
|
architecture arch_datapath of datapath is
|
29 |
|
|
-- datapath signals
|
30 |
|
|
signal data_in_s, pc, pc_last, pc_plus4, pc_next, result, branch, jump, ext32, ext32b, ext32h, alu_src: std_logic_vector(31 downto 0);
|
31 |
|
|
signal opcode, funct: std_logic_vector(5 downto 0);
|
32 |
|
|
signal read_reg1, read_reg2, write_reg, rs, rt, rd, target: std_logic_vector(4 downto 0);
|
33 |
|
|
signal write_data, read_data1, read_data2: std_logic_vector(31 downto 0);
|
34 |
|
|
signal imm: std_logic_vector(15 downto 0);
|
35 |
|
|
signal wreg, zero, less_than, br_link_ctl, branch_taken, branch_taken_dly, jump_taken, jump_taken_dly, stall_reg: std_logic;
|
36 |
|
|
signal irq_ack_s, irq_ack_s_dly, bds: std_logic;
|
37 |
|
|
|
38 |
|
|
-- control signals
|
39 |
|
|
signal reg_dst_ctl, reg_write_ctl, alu_src_ctl, reg_to_mem_ctl, mem_to_reg_ctl, mem_to_reg_ctl_dly, signed_imm_ctl, signed_rd_ctl, shift_ctl: std_logic;
|
40 |
|
|
signal jump_ctl, mem_read_ctl, mem_write_ctl: std_logic_vector(1 downto 0);
|
41 |
|
|
signal branch_ctl: std_logic_vector(2 downto 0);
|
42 |
|
|
signal alu_op_ctl: std_logic_vector(3 downto 0);
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
signal reg_dst_ctl_r, reg_write_ctl_r, alu_src_ctl_r, reg_to_mem_ctl_r, mem_to_reg_ctl_r, signed_imm_ctl_r, signed_rd_ctl_r, shift_ctl_r, br_link_ctl_r: std_logic;
|
46 |
|
|
signal jump_ctl_r, mem_read_ctl_r, mem_write_ctl_r: std_logic_vector(1 downto 0);
|
47 |
|
|
signal branch_ctl_r: std_logic_vector(2 downto 0);
|
48 |
|
|
signal alu_op_ctl_r: std_logic_vector(3 downto 0);
|
49 |
|
|
signal rs_r, rt_r, rd_r: std_logic_vector(4 downto 0);
|
50 |
|
|
signal imm_r: std_logic_vector(15 downto 0);
|
51 |
|
|
begin
|
52 |
|
|
|
53 |
|
|
--
|
54 |
|
|
-- FETCH STAGE
|
55 |
|
|
--
|
56 |
|
|
-- 1st stage, instruction memory access, PC update, interrupt acknowledge logic
|
57 |
|
|
|
58 |
|
|
-- program counter logic
|
59 |
|
|
process(clock, reset, reg_to_mem_ctl_r, mem_to_reg_ctl_r, busy, stall)
|
60 |
|
|
begin
|
61 |
|
|
if reset = '1' then
|
62 |
|
|
pc <= (others => '0');
|
63 |
|
|
pc_last <= (others => '0');
|
64 |
|
|
elsif clock'event and clock = '1' then
|
65 |
|
|
if stall = '0' then
|
66 |
|
|
if busy = '0' then
|
67 |
|
|
pc <= pc_next;
|
68 |
|
|
pc_last <= pc;
|
69 |
|
|
else
|
70 |
|
|
if (reg_to_mem_ctl_r = '1' or mem_to_reg_ctl_r = '1') and branch_taken_dly = '0' and jump_taken_dly = '0' then
|
71 |
|
|
pc <= pc_last;
|
72 |
|
|
end if;
|
73 |
|
|
end if;
|
74 |
|
|
end if;
|
75 |
|
|
end if;
|
76 |
|
|
end process;
|
77 |
|
|
|
78 |
|
|
pc_plus4 <= pc + 4;
|
79 |
|
|
|
80 |
|
|
pc_next <= irq_vector when irq = '1' and irq_ack_s = '1' else
|
81 |
|
|
branch when branch_taken = '1' else
|
82 |
|
|
jump when jump_taken = '1' else
|
83 |
|
|
pc_plus4;
|
84 |
|
|
|
85 |
|
|
-- interrupt acknowledge logic
|
86 |
|
|
irq_ack_s <= '1' when irq = '1' and
|
87 |
|
|
bds = '0' and branch_taken = '0' and jump_taken = '0' and
|
88 |
|
|
reg_to_mem_ctl_r = '0' and mem_to_reg_ctl_r = '0' else '0';
|
89 |
|
|
|
90 |
|
|
irq_ack <= irq_ack_s_dly;
|
91 |
|
|
|
92 |
|
|
process(clock, reset, irq, irq_ack_s, mem_to_reg_ctl_r, busy, stall)
|
93 |
|
|
begin
|
94 |
|
|
if reset = '1' then
|
95 |
|
|
irq_ack_s_dly <= '0';
|
96 |
|
|
bds <= '0';
|
97 |
|
|
branch_taken_dly <= '0';
|
98 |
|
|
jump_taken_dly <= '0';
|
99 |
|
|
mem_to_reg_ctl_dly <= '0';
|
100 |
|
|
stall_reg <= '0';
|
101 |
|
|
elsif clock'event and clock = '1' then
|
102 |
|
|
stall_reg <= stall;
|
103 |
|
|
if stall = '0' then
|
104 |
|
|
mem_to_reg_ctl_dly <= mem_to_reg_ctl_r;
|
105 |
|
|
if busy = '0' then
|
106 |
|
|
irq_ack_s_dly <= irq_ack_s;
|
107 |
|
|
if branch_taken = '1' or jump_taken = '1' then
|
108 |
|
|
bds <= '1';
|
109 |
|
|
else
|
110 |
|
|
bds <= '0';
|
111 |
|
|
end if;
|
112 |
|
|
branch_taken_dly <= branch_taken;
|
113 |
|
|
jump_taken_dly <= jump_taken;
|
114 |
|
|
end if;
|
115 |
|
|
end if;
|
116 |
|
|
end if;
|
117 |
|
|
end process;
|
118 |
|
|
|
119 |
|
|
--
|
120 |
|
|
-- DECODE STAGE
|
121 |
|
|
--
|
122 |
|
|
-- 2nd stage, instruction decode, control unit operation, pipeline bubble insertion logic on load/store and 2nd branch delay slot
|
123 |
|
|
|
124 |
|
|
-- instruction decode
|
125 |
|
|
opcode <= inst_in(31 downto 26);
|
126 |
|
|
rs <= inst_in(25 downto 21);
|
127 |
|
|
rt <= inst_in(20 downto 16);
|
128 |
|
|
rd <= "11111" when br_link_ctl = '1' else inst_in(15 downto 11); -- FIXME: this will not work for the 'jalr rd, rs' format
|
129 |
|
|
funct <= inst_in(5 downto 0);
|
130 |
|
|
imm <= inst_in(15 downto 0);
|
131 |
|
|
|
132 |
|
|
-- control unit
|
133 |
|
|
control_hellfire: entity work.control
|
134 |
|
|
port map( opcode => opcode,
|
135 |
|
|
funct => funct,
|
136 |
|
|
rtx => rt,
|
137 |
|
|
reg_dst => reg_dst_ctl,
|
138 |
|
|
reg_write => reg_write_ctl,
|
139 |
|
|
alu_src => alu_src_ctl,
|
140 |
|
|
alu_op => alu_op_ctl,
|
141 |
|
|
jump => jump_ctl,
|
142 |
|
|
branch => branch_ctl,
|
143 |
|
|
br_link => br_link_ctl,
|
144 |
|
|
reg_to_mem => reg_to_mem_ctl,
|
145 |
|
|
mem_to_reg => mem_to_reg_ctl,
|
146 |
|
|
signed_imm => signed_imm_ctl,
|
147 |
|
|
mem_write => mem_write_ctl,
|
148 |
|
|
mem_read => mem_read_ctl,
|
149 |
|
|
signed_rd => signed_rd_ctl,
|
150 |
|
|
shift => shift_ctl
|
151 |
|
|
);
|
152 |
|
|
|
153 |
|
|
process(clock, reset, busy, stall)
|
154 |
|
|
begin
|
155 |
|
|
if reset = '1' then
|
156 |
|
|
rs_r <= (others => '0');
|
157 |
|
|
rt_r <= (others => '0');
|
158 |
|
|
rd_r <= (others => '0');
|
159 |
|
|
imm_r <= (others => '0');
|
160 |
|
|
reg_dst_ctl_r <= '0';
|
161 |
|
|
reg_write_ctl_r <= '0';
|
162 |
|
|
alu_src_ctl_r <= '0';
|
163 |
|
|
alu_op_ctl_r <= (others => '0');
|
164 |
|
|
jump_ctl_r <= (others => '0');
|
165 |
|
|
branch_ctl_r <= (others => '0');
|
166 |
|
|
br_link_ctl_r <= '0';
|
167 |
|
|
reg_to_mem_ctl_r <= '0';
|
168 |
|
|
mem_to_reg_ctl_r <= '0';
|
169 |
|
|
signed_imm_ctl_r <= '0';
|
170 |
|
|
mem_write_ctl_r <= "00";
|
171 |
|
|
mem_read_ctl_r <= "00";
|
172 |
|
|
signed_rd_ctl_r <= '0';
|
173 |
|
|
shift_ctl_r <= '0';
|
174 |
|
|
elsif clock'event and clock = '1' then
|
175 |
|
|
if stall = '0' then
|
176 |
|
|
if irq_ack_s = '1' then
|
177 |
|
|
rs_r <= (others => '0');
|
178 |
|
|
rt_r <= (others => '0');
|
179 |
|
|
rd_r <= (others => '0');
|
180 |
|
|
imm_r <= (others => '0');
|
181 |
|
|
reg_dst_ctl_r <= '0';
|
182 |
|
|
reg_write_ctl_r <= '0';
|
183 |
|
|
alu_src_ctl_r <= '0';
|
184 |
|
|
alu_op_ctl_r <= (others => '0');
|
185 |
|
|
jump_ctl_r <= (others => '0');
|
186 |
|
|
branch_ctl_r <= (others => '0');
|
187 |
|
|
br_link_ctl_r <= '0';
|
188 |
|
|
reg_to_mem_ctl_r <= '0';
|
189 |
|
|
mem_to_reg_ctl_r <= '0';
|
190 |
|
|
signed_imm_ctl_r <= '0';
|
191 |
|
|
mem_write_ctl_r <= "00";
|
192 |
|
|
mem_read_ctl_r <= "00";
|
193 |
|
|
signed_rd_ctl_r <= '0';
|
194 |
|
|
shift_ctl_r <= '0';
|
195 |
|
|
else
|
196 |
|
|
if busy = '0' then
|
197 |
|
|
if reg_to_mem_ctl_r = '1' or mem_to_reg_ctl_r = '1' or branch_taken_dly = '1' or jump_taken_dly = '1' then
|
198 |
|
|
rs_r <= (others => '0');
|
199 |
|
|
rt_r <= (others => '0');
|
200 |
|
|
rd_r <= (others => '0');
|
201 |
|
|
imm_r <= (others => '0');
|
202 |
|
|
reg_dst_ctl_r <= '0';
|
203 |
|
|
reg_write_ctl_r <= '0';
|
204 |
|
|
alu_src_ctl_r <= '0';
|
205 |
|
|
alu_op_ctl_r <= (others => '0');
|
206 |
|
|
jump_ctl_r <= (others => '0');
|
207 |
|
|
branch_ctl_r <= (others => '0');
|
208 |
|
|
br_link_ctl_r <= '0';
|
209 |
|
|
reg_to_mem_ctl_r <= '0';
|
210 |
|
|
mem_to_reg_ctl_r <= '0';
|
211 |
|
|
signed_imm_ctl_r <= '0';
|
212 |
|
|
mem_write_ctl_r <= "00";
|
213 |
|
|
mem_read_ctl_r <= "00";
|
214 |
|
|
signed_rd_ctl_r <= '0';
|
215 |
|
|
shift_ctl_r <= '0';
|
216 |
|
|
else
|
217 |
|
|
rs_r <= rs;
|
218 |
|
|
rt_r <= rt;
|
219 |
|
|
rd_r <= rd;
|
220 |
|
|
imm_r <= imm;
|
221 |
|
|
reg_dst_ctl_r <= reg_dst_ctl;
|
222 |
|
|
reg_write_ctl_r <= reg_write_ctl;
|
223 |
|
|
alu_src_ctl_r <= alu_src_ctl;
|
224 |
|
|
alu_op_ctl_r <= alu_op_ctl;
|
225 |
|
|
jump_ctl_r <= jump_ctl;
|
226 |
|
|
branch_ctl_r <= branch_ctl;
|
227 |
|
|
br_link_ctl_r <= br_link_ctl;
|
228 |
|
|
reg_to_mem_ctl_r <= reg_to_mem_ctl;
|
229 |
|
|
mem_to_reg_ctl_r <= mem_to_reg_ctl;
|
230 |
|
|
signed_imm_ctl_r <= signed_imm_ctl;
|
231 |
|
|
mem_write_ctl_r <= mem_write_ctl;
|
232 |
|
|
mem_read_ctl_r <= mem_read_ctl;
|
233 |
|
|
signed_rd_ctl_r <= signed_rd_ctl;
|
234 |
|
|
shift_ctl_r <= shift_ctl;
|
235 |
|
|
end if;
|
236 |
|
|
end if;
|
237 |
|
|
end if;
|
238 |
|
|
end if;
|
239 |
|
|
end if;
|
240 |
|
|
end process;
|
241 |
|
|
|
242 |
|
|
--
|
243 |
|
|
-- EXECUTE STAGE
|
244 |
|
|
--
|
245 |
|
|
|
246 |
|
|
-- 3rd stage (a) register file access (read)
|
247 |
|
|
-- the register file
|
248 |
|
|
register_bank: entity work.reg_bank
|
249 |
|
|
port map( clock => clock,
|
250 |
|
|
read_reg1 => read_reg1,
|
251 |
|
|
read_reg2 => read_reg2,
|
252 |
|
|
write_reg => write_reg,
|
253 |
|
|
wreg => wreg,
|
254 |
|
|
write_data => write_data,
|
255 |
|
|
read_data1 => read_data1,
|
256 |
|
|
read_data2 => read_data2
|
257 |
|
|
);
|
258 |
|
|
|
259 |
|
|
-- register file read/write selection and write enable
|
260 |
|
|
read_reg1 <= rs_r when shift_ctl_r = '0' else rt_r; -- source for shifts or normal operations
|
261 |
|
|
read_reg2 <= "00000" when branch_ctl_r > "010" else -- source for branch and link (for zero operations)
|
262 |
|
|
rs_r when shift_ctl_r = '1' else rt_r; -- source for register based shifts or normal operations
|
263 |
|
|
write_reg <= target when mem_to_reg_ctl_r = '0' else rt_r;
|
264 |
|
|
ext32 <= x"0000" & imm_r when (imm_r(15) = '0' or signed_imm_ctl_r = '0') else x"ffff" & imm_r;
|
265 |
|
|
target <= rt_r when reg_dst_ctl_r = '0' else rd_r; -- target register selection
|
266 |
|
|
wreg <= (reg_write_ctl_r or mem_to_reg_ctl_dly) and not busy and not stall_reg; -- enable the register bank for write back also
|
267 |
|
|
|
268 |
|
|
-- 3rd stage (b) ALU operation
|
269 |
|
|
alu: entity work.alu
|
270 |
|
|
port map( op1 => read_data1,
|
271 |
|
|
op2 => alu_src,
|
272 |
|
|
alu_op => alu_op_ctl_r,
|
273 |
|
|
result => result,
|
274 |
|
|
zero => zero,
|
275 |
|
|
less_than => less_than
|
276 |
|
|
);
|
277 |
|
|
|
278 |
|
|
alu_src <= read_data2 when alu_src_ctl_r = '0' else ext32;
|
279 |
|
|
|
280 |
|
|
branch <= (ext32(29 downto 0) & "00") + pc_last;
|
281 |
|
|
jump <= read_data1 when jump_ctl_r = "10" else pc_last(31 downto 28) & rs_r & rt_r & imm_r & "00";
|
282 |
|
|
|
283 |
|
|
branch_taken <= '1' when (zero = '1' and branch_ctl_r = "001") or -- BEQ
|
284 |
|
|
(zero = '0' and branch_ctl_r = "010") or -- BNE
|
285 |
|
|
((zero = '1' or less_than = '1') and branch_ctl_r = "011") or -- BLEZ
|
286 |
|
|
((zero = '0' and less_than = '0') and branch_ctl_r = "100") or -- BGTZ
|
287 |
|
|
((zero = '0' and less_than = '1') and branch_ctl_r = "101") or -- BLTZ, BLTZAL
|
288 |
|
|
((zero = '1' or less_than = '0') and branch_ctl_r = "110") -- BGEZ, BGEZAL
|
289 |
|
|
else '0';
|
290 |
|
|
jump_taken <= '1' when jump_ctl_r /= "00" else '0'; -- J, JAL, JR, JALR
|
291 |
|
|
|
292 |
|
|
inst_addr <= pc;
|
293 |
|
|
data_addr <= result; --result(31 downto 2) & "00";
|
294 |
|
|
data_access <= '1' when reg_to_mem_ctl_r = '1' or mem_to_reg_ctl_r = '1' else '0';
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
-- 3rd stage (c) data memory / write back operation, register file access (write)
|
298 |
|
|
-- memory access, store operations
|
299 |
|
|
process(mem_write_ctl_r, result, read_data2)
|
300 |
|
|
begin
|
301 |
|
|
case mem_write_ctl_r is
|
302 |
|
|
when "11" => -- store word
|
303 |
|
|
data_out <= read_data2;
|
304 |
|
|
data_w <= "1111";
|
305 |
|
|
when "01" => -- store byte
|
306 |
|
|
data_out <= read_data2(7 downto 0) & read_data2(7 downto 0) & read_data2(7 downto 0) & read_data2(7 downto 0);
|
307 |
|
|
case result(1 downto 0) is
|
308 |
|
|
when "11" => data_w <= "0001";
|
309 |
|
|
when "10" => data_w <= "0010";
|
310 |
|
|
when "01" => data_w <= "0100";
|
311 |
|
|
when others => data_w <= "1000";
|
312 |
|
|
end case;
|
313 |
|
|
when "10" => -- store half word
|
314 |
|
|
data_out <= read_data2(15 downto 0) & read_data2(15 downto 0);
|
315 |
|
|
case result(1) is
|
316 |
|
|
when '1' => data_w <= "0011";
|
317 |
|
|
when others => data_w <= "1100";
|
318 |
|
|
end case;
|
319 |
|
|
when others => -- WTF??
|
320 |
|
|
data_out <= read_data2;
|
321 |
|
|
data_w <= "0000";
|
322 |
|
|
end case;
|
323 |
|
|
end process;
|
324 |
|
|
|
325 |
|
|
-- memory access, load operations
|
326 |
|
|
process(mem_read_ctl_r, result, data_in)
|
327 |
|
|
begin
|
328 |
|
|
case mem_read_ctl_r is
|
329 |
|
|
when "01" => -- load byte
|
330 |
|
|
case result(1 downto 0) is
|
331 |
|
|
when "11" => data_in_s <= x"000000" & data_in(7 downto 0);
|
332 |
|
|
when "10" => data_in_s <= x"000000" & data_in(15 downto 8);
|
333 |
|
|
when "01" => data_in_s <= x"000000" & data_in(23 downto 16);
|
334 |
|
|
when others => data_in_s <= x"000000" & data_in(31 downto 24);
|
335 |
|
|
|
336 |
|
|
end case;
|
337 |
|
|
when "10" => -- load half word
|
338 |
|
|
case result(1) is
|
339 |
|
|
when '1' => data_in_s <= x"0000" & data_in(15 downto 0);
|
340 |
|
|
when others => data_in_s <= x"0000" & data_in(31 downto 16);
|
341 |
|
|
end case;
|
342 |
|
|
when others => -- load word
|
343 |
|
|
data_in_s <= data_in;
|
344 |
|
|
end case;
|
345 |
|
|
end process;
|
346 |
|
|
|
347 |
|
|
-- write back
|
348 |
|
|
ext32b <= x"000000" & data_in_s(7 downto 0) when (data_in_s(7) = '0' or signed_rd_ctl_r = '0') else x"ffffff" & data_in_s(7 downto 0);
|
349 |
|
|
ext32h <= x"0000" & data_in_s(15 downto 0) when (data_in_s(15) = '0' or signed_rd_ctl_r = '0') else x"ffff" & data_in_s(15 downto 0);
|
350 |
|
|
|
351 |
|
|
write_data <= data_in_s when mem_read_ctl_r = "11" else
|
352 |
|
|
ext32b when mem_read_ctl_r = "01" else
|
353 |
|
|
ext32h when mem_read_ctl_r = "10" else
|
354 |
|
|
pc when br_link_ctl_r = '1' else result;
|
355 |
|
|
|
356 |
|
|
end arch_datapath;
|
357 |
|
|
|