core: Implement a simple branch predictor
[microwatt.git] / decode2.vhdl
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4
5 library work;
6 use work.decode_types.all;
7 use work.common.all;
8 use work.helpers.all;
9 use work.insn_helpers.all;
10
11 entity decode2 is
12 generic (
13 EX1_BYPASS : boolean := true
14 );
15 port (
16 clk : in std_ulogic;
17 rst : in std_ulogic;
18
19 complete_in : in std_ulogic;
20 busy_in : in std_ulogic;
21 stall_out : out std_ulogic;
22
23 stopped_out : out std_ulogic;
24
25 flush_in: in std_ulogic;
26
27 d_in : in Decode1ToDecode2Type;
28
29 e_out : out Decode2ToExecute1Type;
30
31 r_in : in RegisterFileToDecode2Type;
32 r_out : out Decode2ToRegisterFileType;
33
34 c_in : in CrFileToDecode2Type;
35 c_out : out Decode2ToCrFileType;
36
37 log_out : out std_ulogic_vector(9 downto 0)
38 );
39 end entity decode2;
40
41 architecture behaviour of decode2 is
42 type reg_type is record
43 e : Decode2ToExecute1Type;
44 end record;
45
46 signal r, rin : reg_type;
47
48 signal deferred : std_ulogic;
49
50 signal log_data : std_ulogic_vector(9 downto 0);
51
52 type decode_input_reg_t is record
53 reg_valid : std_ulogic;
54 reg : gspr_index_t;
55 data : std_ulogic_vector(63 downto 0);
56 end record;
57
58 type decode_output_reg_t is record
59 reg_valid : std_ulogic;
60 reg : gspr_index_t;
61 end record;
62
63 function decode_input_reg_a (t : input_reg_a_t; insn_in : std_ulogic_vector(31 downto 0);
64 reg_data : std_ulogic_vector(63 downto 0);
65 ispr : gspr_index_t;
66 instr_addr : std_ulogic_vector(63 downto 0))
67 return decode_input_reg_t is
68 begin
69 if t = RA or (t = RA_OR_ZERO and insn_ra(insn_in) /= "00000") then
70 return ('1', gpr_to_gspr(insn_ra(insn_in)), reg_data);
71 elsif t = SPR then
72 -- ISPR must be either a valid fast SPR number or all 0 for a slow SPR.
73 -- If it's all 0, we don't treat it as a dependency as slow SPRs
74 -- operations are single issue.
75 --
76 assert is_fast_spr(ispr) = '1' or ispr = "000000"
77 report "Decode A says SPR but ISPR is invalid:" &
78 to_hstring(ispr) severity failure;
79 return (is_fast_spr(ispr), ispr, reg_data);
80 elsif t = CIA then
81 return ('0', (others => '0'), instr_addr);
82 else
83 return ('0', (others => '0'), (others => '0'));
84 end if;
85 end;
86
87 function decode_input_reg_b (t : input_reg_b_t; insn_in : std_ulogic_vector(31 downto 0);
88 reg_data : std_ulogic_vector(63 downto 0);
89 ispr : gspr_index_t) return decode_input_reg_t is
90 variable ret : decode_input_reg_t;
91 begin
92 case t is
93 when RB =>
94 ret := ('1', gpr_to_gspr(insn_rb(insn_in)), reg_data);
95 when CONST_UI =>
96 ret := ('0', (others => '0'), std_ulogic_vector(resize(unsigned(insn_ui(insn_in)), 64)));
97 when CONST_SI =>
98 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_si(insn_in)), 64)));
99 when CONST_SI_HI =>
100 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_si(insn_in)) & x"0000", 64)));
101 when CONST_UI_HI =>
102 ret := ('0', (others => '0'), std_ulogic_vector(resize(unsigned(insn_si(insn_in)) & x"0000", 64)));
103 when CONST_LI =>
104 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_li(insn_in)) & "00", 64)));
105 when CONST_BD =>
106 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_bd(insn_in)) & "00", 64)));
107 when CONST_DS =>
108 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_ds(insn_in)) & "00", 64)));
109 when CONST_DXHI4 =>
110 ret := ('0', (others => '0'), std_ulogic_vector(resize(signed(insn_dx(insn_in)) & x"0004", 64)));
111 when CONST_M1 =>
112 ret := ('0', (others => '0'), x"FFFFFFFFFFFFFFFF");
113 when CONST_SH =>
114 ret := ('0', (others => '0'), x"00000000000000" & "00" & insn_in(1) & insn_in(15 downto 11));
115 when CONST_SH32 =>
116 ret := ('0', (others => '0'), x"00000000000000" & "000" & insn_in(15 downto 11));
117 when SPR =>
118 -- ISPR must be either a valid fast SPR number or all 0 for a slow SPR.
119 -- If it's all 0, we don't treat it as a dependency as slow SPRs
120 -- operations are single issue.
121 assert is_fast_spr(ispr) = '1' or ispr = "000000"
122 report "Decode B says SPR but ISPR is invalid:" &
123 to_hstring(ispr) severity failure;
124 ret := (is_fast_spr(ispr), ispr, reg_data);
125 when NONE =>
126 ret := ('0', (others => '0'), (others => '0'));
127 end case;
128
129 return ret;
130 end;
131
132 function decode_input_reg_c (t : input_reg_c_t; insn_in : std_ulogic_vector(31 downto 0);
133 reg_data : std_ulogic_vector(63 downto 0)) return decode_input_reg_t is
134 begin
135 case t is
136 when RS =>
137 return ('1', gpr_to_gspr(insn_rs(insn_in)), reg_data);
138 when NONE =>
139 return ('0', (others => '0'), (others => '0'));
140 end case;
141 end;
142
143 function decode_output_reg (t : output_reg_a_t; insn_in : std_ulogic_vector(31 downto 0);
144 ispr : gspr_index_t) return decode_output_reg_t is
145 begin
146 case t is
147 when RT =>
148 return ('1', gpr_to_gspr(insn_rt(insn_in)));
149 when RA =>
150 return ('1', gpr_to_gspr(insn_ra(insn_in)));
151 when SPR =>
152 -- ISPR must be either a valid fast SPR number or all 0 for a slow SPR.
153 -- If it's all 0, we don't treat it as a dependency as slow SPRs
154 -- operations are single issue.
155 assert is_fast_spr(ispr) = '1' or ispr = "000000"
156 report "Decode B says SPR but ISPR is invalid:" &
157 to_hstring(ispr) severity failure;
158 return (is_fast_spr(ispr), ispr);
159 when NONE =>
160 return ('0', "000000");
161 end case;
162 end;
163
164 function decode_rc (t : rc_t; insn_in : std_ulogic_vector(31 downto 0)) return std_ulogic is
165 begin
166 case t is
167 when RC =>
168 return insn_rc(insn_in);
169 when ONE =>
170 return '1';
171 when NONE =>
172 return '0';
173 end case;
174 end;
175
176 -- For now, use "rc" in the decode table to decide whether oe exists.
177 -- This is not entirely correct architecturally: For mulhd and
178 -- mulhdu, the OE field is reserved. It remains to be seen what an
179 -- actual POWER9 does if we set it on those instructions, for now we
180 -- test that further down when assigning to the multiplier oe input.
181 --
182 function decode_oe (t : rc_t; insn_in : std_ulogic_vector(31 downto 0)) return std_ulogic is
183 begin
184 case t is
185 when RC =>
186 return insn_oe(insn_in);
187 when OTHERS =>
188 return '0';
189 end case;
190 end;
191
192 -- issue control signals
193 signal control_valid_in : std_ulogic;
194 signal control_valid_out : std_ulogic;
195 signal control_sgl_pipe : std_logic;
196
197 signal gpr_write_valid : std_ulogic;
198 signal gpr_write : gspr_index_t;
199 signal gpr_bypassable : std_ulogic;
200
201 signal update_gpr_write_valid : std_ulogic;
202 signal update_gpr_write_reg : gspr_index_t;
203
204 signal gpr_a_read_valid : std_ulogic;
205 signal gpr_a_read :gspr_index_t;
206 signal gpr_a_bypass : std_ulogic;
207
208 signal gpr_b_read_valid : std_ulogic;
209 signal gpr_b_read : gspr_index_t;
210 signal gpr_b_bypass : std_ulogic;
211
212 signal gpr_c_read_valid : std_ulogic;
213 signal gpr_c_read : gpr_index_t;
214 signal gpr_c_bypass : std_ulogic;
215
216 signal cr_write_valid : std_ulogic;
217 begin
218 control_0: entity work.control
219 generic map (
220 PIPELINE_DEPTH => 1
221 )
222 port map (
223 clk => clk,
224 rst => rst,
225
226 complete_in => complete_in,
227 valid_in => control_valid_in,
228 busy_in => busy_in,
229 deferred => deferred,
230 flush_in => flush_in,
231 sgl_pipe_in => control_sgl_pipe,
232 stop_mark_in => d_in.stop_mark,
233
234 gpr_write_valid_in => gpr_write_valid,
235 gpr_write_in => gpr_write,
236 gpr_bypassable => gpr_bypassable,
237
238 update_gpr_write_valid => update_gpr_write_valid,
239 update_gpr_write_reg => update_gpr_write_reg,
240
241 gpr_a_read_valid_in => gpr_a_read_valid,
242 gpr_a_read_in => gpr_a_read,
243
244 gpr_b_read_valid_in => gpr_b_read_valid,
245 gpr_b_read_in => gpr_b_read,
246
247 gpr_c_read_valid_in => gpr_c_read_valid,
248 gpr_c_read_in => gpr_c_read,
249
250 cr_read_in => d_in.decode.input_cr,
251 cr_write_in => cr_write_valid,
252
253 valid_out => control_valid_out,
254 stall_out => stall_out,
255 stopped_out => stopped_out,
256
257 gpr_bypass_a => gpr_a_bypass,
258 gpr_bypass_b => gpr_b_bypass,
259 gpr_bypass_c => gpr_c_bypass
260 );
261
262 deferred <= r.e.valid and busy_in;
263
264 decode2_0: process(clk)
265 begin
266 if rising_edge(clk) then
267 if rst = '1' or flush_in = '1' or deferred = '0' then
268 if rin.e.valid = '1' then
269 report "execute " & to_hstring(rin.e.nia);
270 end if;
271 r <= rin;
272 end if;
273 end if;
274 end process;
275
276 r_out.read1_reg <= d_in.ispr1 when d_in.decode.input_reg_a = SPR
277 else gpr_to_gspr(insn_ra(d_in.insn));
278 r_out.read2_reg <= d_in.ispr2 when d_in.decode.input_reg_b = SPR
279 else gpr_to_gspr(insn_rb(d_in.insn));
280 r_out.read3_reg <= insn_rs(d_in.insn);
281
282 c_out.read <= d_in.decode.input_cr;
283
284 decode2_1: process(all)
285 variable v : reg_type;
286 variable mul_a : std_ulogic_vector(63 downto 0);
287 variable mul_b : std_ulogic_vector(63 downto 0);
288 variable decoded_reg_a : decode_input_reg_t;
289 variable decoded_reg_b : decode_input_reg_t;
290 variable decoded_reg_c : decode_input_reg_t;
291 variable decoded_reg_o : decode_output_reg_t;
292 variable length : std_ulogic_vector(3 downto 0);
293 begin
294 v := r;
295
296 v.e := Decode2ToExecute1Init;
297
298 mul_a := (others => '0');
299 mul_b := (others => '0');
300
301 --v.e.input_cr := d_in.decode.input_cr;
302 --v.e.output_cr := d_in.decode.output_cr;
303
304 decoded_reg_a := decode_input_reg_a (d_in.decode.input_reg_a, d_in.insn, r_in.read1_data, d_in.ispr1,
305 d_in.nia);
306 decoded_reg_b := decode_input_reg_b (d_in.decode.input_reg_b, d_in.insn, r_in.read2_data, d_in.ispr2);
307 decoded_reg_c := decode_input_reg_c (d_in.decode.input_reg_c, d_in.insn, r_in.read3_data);
308 decoded_reg_o := decode_output_reg (d_in.decode.output_reg_a, d_in.insn, d_in.ispr1);
309
310 r_out.read1_enable <= decoded_reg_a.reg_valid and d_in.valid;
311 r_out.read2_enable <= decoded_reg_b.reg_valid and d_in.valid;
312 r_out.read3_enable <= decoded_reg_c.reg_valid and d_in.valid;
313
314 case d_in.decode.length is
315 when is1B =>
316 length := "0001";
317 when is2B =>
318 length := "0010";
319 when is4B =>
320 length := "0100";
321 when is8B =>
322 length := "1000";
323 when NONE =>
324 length := "0000";
325 end case;
326
327 -- execute unit
328 v.e.nia := d_in.nia;
329 v.e.unit := d_in.decode.unit;
330 v.e.insn_type := d_in.decode.insn_type;
331 v.e.read_reg1 := decoded_reg_a.reg;
332 v.e.read_data1 := decoded_reg_a.data;
333 v.e.bypass_data1 := gpr_a_bypass;
334 v.e.read_reg2 := decoded_reg_b.reg;
335 v.e.read_data2 := decoded_reg_b.data;
336 v.e.bypass_data2 := gpr_b_bypass;
337 v.e.read_data3 := decoded_reg_c.data;
338 v.e.bypass_data3 := gpr_c_bypass;
339 v.e.write_reg := decoded_reg_o.reg;
340 v.e.rc := decode_rc(d_in.decode.rc, d_in.insn);
341 if not (d_in.decode.insn_type = OP_MUL_H32 or d_in.decode.insn_type = OP_MUL_H64) then
342 v.e.oe := decode_oe(d_in.decode.rc, d_in.insn);
343 end if;
344 v.e.cr := c_in.read_cr_data;
345 v.e.xerc := c_in.read_xerc_data;
346 v.e.invert_a := d_in.decode.invert_a;
347 v.e.invert_out := d_in.decode.invert_out;
348 v.e.input_carry := d_in.decode.input_carry;
349 v.e.output_carry := d_in.decode.output_carry;
350 v.e.is_32bit := d_in.decode.is_32bit;
351 v.e.is_signed := d_in.decode.is_signed;
352 if d_in.decode.lr = '1' then
353 v.e.lr := insn_lk(d_in.insn);
354 end if;
355 v.e.insn := d_in.insn;
356 v.e.data_len := length;
357 v.e.byte_reverse := d_in.decode.byte_reverse;
358 v.e.sign_extend := d_in.decode.sign_extend;
359 v.e.update := d_in.decode.update;
360 v.e.reserve := d_in.decode.reserve;
361 v.e.br_pred := d_in.br_pred;
362
363 -- issue control
364 control_valid_in <= d_in.valid;
365 control_sgl_pipe <= d_in.decode.sgl_pipe;
366
367 gpr_write_valid <= decoded_reg_o.reg_valid;
368 gpr_write <= decoded_reg_o.reg;
369 gpr_bypassable <= '0';
370 if EX1_BYPASS and d_in.decode.unit = ALU then
371 gpr_bypassable <= '1';
372 end if;
373 update_gpr_write_valid <= d_in.decode.update;
374 update_gpr_write_reg <= decoded_reg_a.reg;
375 if v.e.lr = '1' then
376 -- there are no instructions that have both update=1 and lr=1
377 update_gpr_write_valid <= '1';
378 update_gpr_write_reg <= fast_spr_num(SPR_LR);
379 end if;
380
381 gpr_a_read_valid <= decoded_reg_a.reg_valid;
382 gpr_a_read <= decoded_reg_a.reg;
383
384 gpr_b_read_valid <= decoded_reg_b.reg_valid;
385 gpr_b_read <= decoded_reg_b.reg;
386
387 gpr_c_read_valid <= decoded_reg_c.reg_valid;
388 gpr_c_read <= gspr_to_gpr(decoded_reg_c.reg);
389
390 cr_write_valid <= d_in.decode.output_cr or decode_rc(d_in.decode.rc, d_in.insn);
391
392 v.e.valid := control_valid_out;
393 if d_in.decode.unit = NONE then
394 v.e.insn_type := OP_ILLEGAL;
395 end if;
396
397 if rst = '1' or flush_in = '1' then
398 v.e := Decode2ToExecute1Init;
399 end if;
400
401 -- Update registers
402 rin <= v;
403
404 -- Update outputs
405 e_out <= r.e;
406 end process;
407
408 dec2_log : process(clk)
409 begin
410 if rising_edge(clk) then
411 log_data <= r.e.nia(5 downto 2) &
412 r.e.valid &
413 stopped_out &
414 stall_out &
415 r.e.bypass_data3 &
416 r.e.bypass_data2 &
417 r.e.bypass_data1;
418 end if;
419 end process;
420 log_out <= log_data;
421
422 end architecture behaviour;