begin working on linux verilator simulation
[microwatt.git] / execute1.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.crhelpers.all;
10 use work.insn_helpers.all;
11 use work.ppc_fx_insns.all;
12
13 entity execute1 is
14 generic (
15 EX1_BYPASS : boolean := true;
16 HAS_FPU : boolean := true;
17 HAS_SHORT_MULT : boolean := false;
18 -- Non-zero to enable log data collection
19 LOG_LENGTH : natural := 0
20 );
21 port (
22 clk : in std_ulogic;
23 rst : in std_ulogic;
24
25 -- asynchronous
26 flush_in : in std_ulogic;
27 busy_out : out std_ulogic;
28
29 e_in : in Decode2ToExecute1Type;
30 l_in : in Loadstore1ToExecute1Type;
31 fp_in : in FPUToExecute1Type;
32
33 ext_irq_in : std_ulogic;
34 interrupt_in : std_ulogic;
35
36 -- asynchronous
37 l_out : out Execute1ToLoadstore1Type;
38 fp_out : out Execute1ToFPUType;
39
40 e_out : out Execute1ToWritebackType;
41 bypass_data : out bypass_data_t;
42 bypass_cr_data : out cr_bypass_data_t;
43
44 dbg_msr_out : out std_ulogic_vector(63 downto 0);
45
46 icache_inval : out std_ulogic;
47 terminate_out : out std_ulogic;
48
49 -- PMU event buses
50 wb_events : in WritebackEventType;
51 ls_events : in Loadstore1EventType;
52 dc_events : in DcacheEventType;
53 ic_events : in IcacheEventType;
54
55 log_out : out std_ulogic_vector(14 downto 0);
56 log_rd_addr : out std_ulogic_vector(31 downto 0);
57 log_rd_data : in std_ulogic_vector(63 downto 0);
58 log_wr_addr : in std_ulogic_vector(31 downto 0)
59 );
60 end entity execute1;
61
62 architecture behaviour of execute1 is
63 type reg_type is record
64 e : Execute1ToWritebackType;
65 cur_instr : Decode2ToExecute1Type;
66 busy: std_ulogic;
67 terminate: std_ulogic;
68 intr_pending : std_ulogic;
69 fp_exception_next : std_ulogic;
70 trace_next : std_ulogic;
71 prev_op : insn_type_t;
72 br_taken : std_ulogic;
73 mul_in_progress : std_ulogic;
74 mul_finish : std_ulogic;
75 div_in_progress : std_ulogic;
76 cntz_in_progress : std_ulogic;
77 no_instr_avail : std_ulogic;
78 instr_dispatch : std_ulogic;
79 ext_interrupt : std_ulogic;
80 taken_branch_event : std_ulogic;
81 br_mispredict : std_ulogic;
82 log_addr_spr : std_ulogic_vector(31 downto 0);
83 end record;
84 constant reg_type_init : reg_type :=
85 (e => Execute1ToWritebackInit,
86 cur_instr => Decode2ToExecute1Init,
87 busy => '0', terminate => '0', intr_pending => '0',
88 fp_exception_next => '0', trace_next => '0', prev_op => OP_ILLEGAL, br_taken => '0',
89 mul_in_progress => '0', mul_finish => '0', div_in_progress => '0', cntz_in_progress => '0',
90 no_instr_avail => '0', instr_dispatch => '0', ext_interrupt => '0',
91 taken_branch_event => '0', br_mispredict => '0',
92 others => (others => '0'));
93
94 signal r, rin : reg_type;
95
96 signal a_in, b_in, c_in : std_ulogic_vector(63 downto 0);
97 signal cr_in : std_ulogic_vector(31 downto 0);
98 signal xerc_in : xer_common_t;
99 signal mshort_p : std_ulogic_vector(31 downto 0) := (others => '0');
100
101 signal valid_in : std_ulogic;
102 signal ctrl: ctrl_t := (others => (others => '0'));
103 signal ctrl_tmp: ctrl_t := (others => (others => '0'));
104 signal right_shift, rot_clear_left, rot_clear_right: std_ulogic;
105 signal rot_sign_ext: std_ulogic;
106 signal rotator_result: std_ulogic_vector(63 downto 0);
107 signal rotator_carry: std_ulogic;
108 signal logical_result: std_ulogic_vector(63 downto 0);
109 signal do_popcnt: std_ulogic;
110 signal countbits_result: std_ulogic_vector(63 downto 0);
111 signal alu_result: std_ulogic_vector(63 downto 0);
112 signal adder_result: std_ulogic_vector(63 downto 0);
113 signal misc_result: std_ulogic_vector(63 downto 0);
114 signal muldiv_result: std_ulogic_vector(63 downto 0);
115 signal spr_result: std_ulogic_vector(63 downto 0);
116 signal result_mux_sel: std_ulogic_vector(2 downto 0);
117 signal sub_mux_sel: std_ulogic_vector(2 downto 0);
118 signal next_nia : std_ulogic_vector(63 downto 0);
119 signal current: Decode2ToExecute1Type;
120
121 signal carry_32 : std_ulogic;
122 signal carry_64 : std_ulogic;
123 signal overflow_32 : std_ulogic;
124 signal overflow_64 : std_ulogic;
125
126 signal trapval : std_ulogic_vector(4 downto 0);
127
128 signal write_cr_mask : std_ulogic_vector(7 downto 0);
129 signal write_cr_data : std_ulogic_vector(31 downto 0);
130
131 -- multiply signals
132 signal x_to_multiply: MultiplyInputType;
133 signal multiply_to_x: MultiplyOutputType;
134
135 -- divider signals
136 signal x_to_divider: Execute1ToDividerType;
137 signal divider_to_x: DividerToExecute1Type;
138
139 -- random number generator signals
140 signal random_raw : std_ulogic_vector(63 downto 0);
141 signal random_cond : std_ulogic_vector(63 downto 0);
142 signal random_err : std_ulogic;
143
144 -- PMU signals
145 signal x_to_pmu : Execute1ToPMUType;
146 signal pmu_to_x : PMUToExecute1Type;
147
148 -- signals for logging
149 signal exception_log : std_ulogic;
150 signal irq_valid_log : std_ulogic;
151
152 type privilege_level is (USER, SUPER);
153 type op_privilege_array is array(insn_type_t) of privilege_level;
154 constant op_privilege: op_privilege_array := (
155 OP_ATTN => SUPER,
156 OP_MFMSR => SUPER,
157 OP_MTMSRD => SUPER,
158 OP_RFID => SUPER,
159 OP_TLBIE => SUPER,
160 others => USER
161 );
162
163 function instr_is_privileged(op: insn_type_t; insn: std_ulogic_vector(31 downto 0))
164 return boolean is
165 begin
166 if op_privilege(op) = SUPER then
167 return true;
168 elsif op = OP_MFSPR or op = OP_MTSPR then
169 return insn(20) = '1';
170 else
171 return false;
172 end if;
173 end;
174
175 procedure set_carry(e: inout Execute1ToWritebackType;
176 carry32 : in std_ulogic;
177 carry : in std_ulogic) is
178 begin
179 e.xerc.ca32 := carry32;
180 e.xerc.ca := carry;
181 end;
182
183 procedure set_ov(e: inout Execute1ToWritebackType;
184 ov : in std_ulogic;
185 ov32 : in std_ulogic) is
186 begin
187 e.xerc.ov32 := ov32;
188 e.xerc.ov := ov;
189 if ov = '1' then
190 e.xerc.so := '1';
191 end if;
192 end;
193
194 function calc_ov(msb_a : std_ulogic; msb_b: std_ulogic;
195 ca: std_ulogic; msb_r: std_ulogic) return std_ulogic is
196 begin
197 return (ca xor msb_r) and not (msb_a xor msb_b);
198 end;
199
200 function decode_input_carry(ic : carry_in_t;
201 xerc : xer_common_t) return std_ulogic is
202 begin
203 case ic is
204 when ZERO =>
205 return '0';
206 when CA =>
207 return xerc.ca;
208 when OV =>
209 return xerc.ov;
210 when ONE =>
211 return '1';
212 end case;
213 end;
214
215 function msr_copy(msr: std_ulogic_vector(63 downto 0))
216 return std_ulogic_vector is
217 variable msr_out: std_ulogic_vector(63 downto 0);
218 begin
219 -- ISA says this:
220 -- Defined MSR bits are classified as either full func-
221 -- tion or partial function. Full function MSR bits are
222 -- saved in SRR1 or HSRR1 when an interrupt other
223 -- than a System Call Vectored interrupt occurs and
224 -- restored by rfscv, rfid, or hrfid, while partial func-
225 -- tion MSR bits are not saved or restored.
226 -- Full function MSR bits lie in the range 0:32, 37:41, and
227 -- 48:63, and partial function MSR bits lie in the range
228 -- 33:36 and 42:47. (Note this is IBM bit numbering).
229 msr_out := (others => '0');
230 msr_out(63 downto 31) := msr(63 downto 31);
231 msr_out(26 downto 22) := msr(26 downto 22);
232 msr_out(15 downto 0) := msr(15 downto 0);
233 return msr_out;
234 end;
235
236 -- Work out whether a signed value fits into n bits,
237 -- that is, see if it is in the range -2^(n-1) .. 2^(n-1) - 1
238 function fits_in_n_bits(val: std_ulogic_vector; n: integer) return boolean is
239 variable x, xp1: std_ulogic_vector(val'left downto val'right);
240 begin
241 x := val;
242 if val(val'left) = '0' then
243 x := not val;
244 end if;
245 xp1 := bit_reverse(std_ulogic_vector(unsigned(bit_reverse(x)) + 1));
246 x := x and not xp1;
247 -- For positive inputs, x has ones at the positions
248 -- to the left of the leftmost 1 bit in val.
249 -- For negative inputs, x has ones to the left of
250 -- the leftmost 0 bit in val.
251 return x(n - 1) = '1';
252 end;
253
254 -- Tell vivado to keep the hierarchy for the random module so that the
255 -- net names in the xdc file match.
256 attribute keep_hierarchy : string;
257 attribute keep_hierarchy of random_0 : label is "yes";
258
259 begin
260
261 rotator_0: entity work.rotator
262 port map (
263 rs => c_in,
264 ra => a_in,
265 shift => b_in(6 downto 0),
266 insn => e_in.insn,
267 is_32bit => e_in.is_32bit,
268 right_shift => right_shift,
269 arith => e_in.is_signed,
270 clear_left => rot_clear_left,
271 clear_right => rot_clear_right,
272 sign_ext_rs => rot_sign_ext,
273 result => rotator_result,
274 carry_out => rotator_carry
275 );
276
277 logical_0: entity work.logical
278 port map (
279 rs => c_in,
280 rb => b_in,
281 op => e_in.insn_type,
282 invert_in => e_in.invert_a,
283 invert_out => e_in.invert_out,
284 result => logical_result,
285 datalen => e_in.data_len
286 );
287
288 countbits_0: entity work.bit_counter
289 port map (
290 clk => clk,
291 rs => c_in,
292 count_right => e_in.insn(10),
293 is_32bit => e_in.is_32bit,
294 do_popcnt => do_popcnt,
295 datalen => e_in.data_len,
296 result => countbits_result
297 );
298
299 multiply_0: entity work.multiply
300 port map (
301 clk => clk,
302 m_in => x_to_multiply,
303 m_out => multiply_to_x
304 );
305
306 divider_0: entity work.divider
307 port map (
308 clk => clk,
309 rst => rst,
310 d_in => x_to_divider,
311 d_out => divider_to_x
312 );
313
314 random_0: entity work.random
315 port map (
316 clk => clk,
317 data => random_cond,
318 raw => random_raw,
319 err => random_err
320 );
321
322 pmu_0: entity work.pmu
323 port map (
324 clk => clk,
325 rst => rst,
326 p_in => x_to_pmu,
327 p_out => pmu_to_x
328 );
329
330 short_mult_0: if HAS_SHORT_MULT generate
331 begin
332 short_mult: entity work.short_multiply
333 port map (
334 clk => clk,
335 a_in => a_in(15 downto 0),
336 b_in => b_in(15 downto 0),
337 m_out => mshort_p
338 );
339 end generate;
340
341 dbg_msr_out <= ctrl.msr;
342 log_rd_addr <= r.log_addr_spr;
343
344 a_in <= e_in.read_data1;
345 b_in <= e_in.read_data2;
346 c_in <= e_in.read_data3;
347 cr_in <= e_in.cr;
348
349 x_to_pmu.occur <= (instr_complete => wb_events.instr_complete,
350 fp_complete => wb_events.fp_complete,
351 ld_complete => ls_events.load_complete,
352 st_complete => ls_events.store_complete,
353 itlb_miss => ls_events.itlb_miss,
354 dc_load_miss => dc_events.load_miss,
355 dc_ld_miss_resolved => dc_events.dcache_refill,
356 dc_store_miss => dc_events.store_miss,
357 dtlb_miss => dc_events.dtlb_miss,
358 dtlb_miss_resolved => dc_events.dtlb_miss_resolved,
359 icache_miss => ic_events.icache_miss,
360 itlb_miss_resolved => ic_events.itlb_miss_resolved,
361 no_instr_avail => r.no_instr_avail,
362 dispatch => r.instr_dispatch,
363 ext_interrupt => r.ext_interrupt,
364 br_taken_complete => r.taken_branch_event,
365 br_mispredict => r.br_mispredict,
366 others => '0');
367 x_to_pmu.nia <= current.nia;
368 x_to_pmu.addr <= (others => '0');
369 x_to_pmu.addr_v <= '0';
370 x_to_pmu.spr_num <= e_in.insn(20 downto 16);
371 x_to_pmu.spr_val <= c_in;
372 x_to_pmu.run <= '1';
373
374 -- XER forwarding. To avoid having to track XER hazards, we use
375 -- the previously latched value. Since the XER common bits
376 -- (SO, OV[32] and CA[32]) are only modified by instructions that are
377 -- handled here, we can just forward the result being sent to
378 -- writeback.
379 xerc_in <= r.e.xerc when r.e.write_xerc_enable = '1' or r.busy = '1' else e_in.xerc;
380
381 with e_in.unit select busy_out <=
382 l_in.busy or r.busy or fp_in.busy when LDST,
383 l_in.busy or l_in.in_progress or r.busy or fp_in.busy when others;
384
385 valid_in <= e_in.valid and not busy_out and not flush_in;
386
387 terminate_out <= r.terminate;
388
389 current <= e_in when r.busy = '0' else r.cur_instr;
390
391 -- Result mux
392 with current.result_sel select alu_result <=
393 adder_result when "000",
394 logical_result when "001",
395 rotator_result when "010",
396 muldiv_result when "011",
397 countbits_result when "100",
398 spr_result when "101",
399 next_nia when "110",
400 misc_result when others;
401
402 execute1_0: process(clk)
403 begin
404 if rising_edge(clk) then
405 if rst = '1' then
406 r <= reg_type_init;
407 ctrl.tb <= (others => '0');
408 ctrl.dec <= (others => '0');
409 ctrl.msr <= (MSR_SF => '1', MSR_LE => '1', others => '0');
410 else
411 r <= rin;
412 ctrl <= ctrl_tmp;
413 if valid_in = '1' then
414 report "execute " & to_hstring(e_in.nia) & " op=" & insn_type_t'image(e_in.insn_type) &
415 " wr=" & to_hstring(rin.e.write_reg) & " we=" & std_ulogic'image(rin.e.write_enable) &
416 " tag=" & integer'image(rin.e.instr_tag.tag) & std_ulogic'image(rin.e.instr_tag.valid);
417 end if;
418 end if;
419 end if;
420 end process;
421
422 -- Data path for integer instructions
423 execute1_dp: process(all)
424 variable a_inv : std_ulogic_vector(63 downto 0);
425 variable b_or_m1 : std_ulogic_vector(63 downto 0);
426 variable sum_with_carry : std_ulogic_vector(64 downto 0);
427 variable sign1, sign2 : std_ulogic;
428 variable abs1, abs2 : signed(63 downto 0);
429 variable addend : std_ulogic_vector(127 downto 0);
430 variable addg6s : std_ulogic_vector(63 downto 0);
431 variable crbit : integer range 0 to 31;
432 variable isel_result : std_ulogic_vector(63 downto 0);
433 variable darn : std_ulogic_vector(63 downto 0);
434 variable setb_result : std_ulogic_vector(63 downto 0);
435 variable mfcr_result : std_ulogic_vector(63 downto 0);
436 variable lo, hi : integer;
437 variable l : std_ulogic;
438 variable zerohi, zerolo : std_ulogic;
439 variable msb_a, msb_b : std_ulogic;
440 variable a_lt : std_ulogic;
441 variable a_lt_lo : std_ulogic;
442 variable a_lt_hi : std_ulogic;
443 variable newcrf : std_ulogic_vector(3 downto 0);
444 variable bf, bfa : std_ulogic_vector(2 downto 0);
445 variable crnum : crnum_t;
446 variable scrnum : crnum_t;
447 variable cr_operands : std_ulogic_vector(1 downto 0);
448 variable crresult : std_ulogic;
449 variable bt, ba, bb : std_ulogic_vector(4 downto 0);
450 variable btnum : integer range 0 to 3;
451 variable banum, bbnum : integer range 0 to 31;
452 variable j : integer;
453 begin
454 -- Main adder
455 if e_in.invert_a = '0' then
456 a_inv := a_in;
457 else
458 a_inv := not a_in;
459 end if;
460 if e_in.addm1 = '0' then
461 b_or_m1 := b_in;
462 else
463 b_or_m1 := (others => '1');
464 end if;
465 sum_with_carry := ppc_adde(a_inv, b_or_m1,
466 decode_input_carry(e_in.input_carry, xerc_in));
467 adder_result <= sum_with_carry(63 downto 0);
468 carry_32 <= sum_with_carry(32) xor a_inv(32) xor b_in(32);
469 carry_64 <= sum_with_carry(64);
470 overflow_32 <= calc_ov(a_inv(31), b_in(31), carry_32, sum_with_carry(31));
471 overflow_64 <= calc_ov(a_inv(63), b_in(63), carry_64, sum_with_carry(63));
472
473 -- signals to multiply and divide units
474 sign1 := '0';
475 sign2 := '0';
476 if e_in.is_signed = '1' then
477 if e_in.is_32bit = '1' then
478 sign1 := a_in(31);
479 sign2 := b_in(31);
480 else
481 sign1 := a_in(63);
482 sign2 := b_in(63);
483 end if;
484 end if;
485 -- take absolute values
486 if sign1 = '0' then
487 abs1 := signed(a_in);
488 else
489 abs1 := - signed(a_in);
490 end if;
491 if sign2 = '0' then
492 abs2 := signed(b_in);
493 else
494 abs2 := - signed(b_in);
495 end if;
496
497 -- Interface to multiply and divide units
498 x_to_divider.is_signed <= e_in.is_signed;
499 x_to_divider.is_32bit <= e_in.is_32bit;
500 x_to_divider.is_extended <= '0';
501 x_to_divider.is_modulus <= '0';
502 if e_in.insn_type = OP_MOD then
503 x_to_divider.is_modulus <= '1';
504 end if;
505
506 addend := (others => '0');
507 if e_in.insn(26) = '0' then
508 -- integer multiply-add, major op 4 (if it is a multiply)
509 addend(63 downto 0) := c_in;
510 if e_in.is_signed = '1' then
511 addend(127 downto 64) := (others => c_in(63));
512 end if;
513 end if;
514 if (sign1 xor sign2) = '1' then
515 addend := not addend;
516 end if;
517
518 x_to_multiply.is_32bit <= e_in.is_32bit;
519 x_to_multiply.not_result <= sign1 xor sign2;
520 x_to_multiply.addend <= addend;
521 x_to_divider.neg_result <= sign1 xor (sign2 and not x_to_divider.is_modulus);
522 if e_in.is_32bit = '0' then
523 -- 64-bit forms
524 x_to_multiply.data1 <= std_ulogic_vector(abs1);
525 x_to_multiply.data2 <= std_ulogic_vector(abs2);
526 if e_in.insn_type = OP_DIVE then
527 x_to_divider.is_extended <= '1';
528 end if;
529 x_to_divider.dividend <= std_ulogic_vector(abs1);
530 x_to_divider.divisor <= std_ulogic_vector(abs2);
531 else
532 -- 32-bit forms
533 x_to_multiply.data1 <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
534 x_to_multiply.data2 <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
535 x_to_divider.is_extended <= '0';
536 if e_in.insn_type = OP_DIVE then -- extended forms
537 x_to_divider.dividend <= std_ulogic_vector(abs1(31 downto 0)) & x"00000000";
538 else
539 x_to_divider.dividend <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
540 end if;
541 x_to_divider.divisor <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
542 end if;
543
544 case current.sub_select(1 downto 0) is
545 when "00" =>
546 if HAS_SHORT_MULT and r.mul_in_progress = '0' then
547 muldiv_result <= std_ulogic_vector(resize(signed(mshort_p), 64));
548 else
549 muldiv_result <= multiply_to_x.result(63 downto 0);
550 end if;
551 when "01" =>
552 muldiv_result <= multiply_to_x.result(127 downto 64);
553 when "10" =>
554 muldiv_result <= multiply_to_x.result(63 downto 32) &
555 multiply_to_x.result(63 downto 32);
556 when others =>
557 muldiv_result <= divider_to_x.write_reg_data;
558 end case;
559
560 -- Compute misc_result
561 case current.sub_select is
562 when "000" =>
563 misc_result <= (others => '0');
564 when "001" =>
565 -- addg6s
566 addg6s := (others => '0');
567 for i in 0 to 14 loop
568 lo := i * 4;
569 hi := (i + 1) * 4;
570 if (a_in(hi) xor b_in(hi) xor sum_with_carry(hi)) = '0' then
571 addg6s(lo + 3 downto lo) := "0110";
572 end if;
573 end loop;
574 if sum_with_carry(64) = '0' then
575 addg6s(63 downto 60) := "0110";
576 end if;
577 misc_result <= addg6s;
578 when "010" =>
579 -- isel
580 crbit := to_integer(unsigned(insn_bc(e_in.insn)));
581 if cr_in(31-crbit) = '1' then
582 isel_result := a_in;
583 else
584 isel_result := b_in;
585 end if;
586 misc_result <= isel_result;
587 when "011" =>
588 -- darn
589 darn := (others => '1');
590 if random_err = '0' then
591 case e_in.insn(17 downto 16) is
592 when "00" =>
593 darn := x"00000000" & random_cond(31 downto 0);
594 when "10" =>
595 darn := random_raw;
596 when others =>
597 darn := random_cond;
598 end case;
599 end if;
600 misc_result <= darn;
601 when "100" =>
602 -- mfmsr
603 misc_result <= ctrl.msr;
604 when "101" =>
605 if e_in.insn(20) = '0' then
606 -- mfcr
607 mfcr_result := x"00000000" & cr_in;
608 else
609 -- mfocrf
610 crnum := fxm_to_num(insn_fxm(e_in.insn));
611 mfcr_result := (others => '0');
612 for i in 0 to 7 loop
613 lo := (7-i)*4;
614 hi := lo + 3;
615 if crnum = i then
616 mfcr_result(hi downto lo) := cr_in(hi downto lo);
617 end if;
618 end loop;
619 end if;
620 misc_result <= mfcr_result;
621 when "110" =>
622 -- setb
623 bfa := insn_bfa(e_in.insn);
624 crbit := to_integer(unsigned(bfa)) * 4;
625 setb_result := (others => '0');
626 if cr_in(31 - crbit) = '1' then
627 setb_result := (others => '1');
628 elsif cr_in(30 - crbit) = '1' then
629 setb_result(0) := '1';
630 end if;
631 misc_result <= setb_result;
632 when others =>
633 misc_result <= (others => '0');
634 end case;
635
636 -- compute comparison results
637 -- Note, we have done RB - RA, not RA - RB
638 if e_in.insn_type = OP_CMP then
639 l := insn_l(e_in.insn);
640 else
641 l := not e_in.is_32bit;
642 end if;
643 zerolo := not (or (a_in(31 downto 0) xor b_in(31 downto 0)));
644 zerohi := not (or (a_in(63 downto 32) xor b_in(63 downto 32)));
645 if zerolo = '1' and (l = '0' or zerohi = '1') then
646 -- values are equal
647 trapval <= "00100";
648 else
649 a_lt_lo := '0';
650 a_lt_hi := '0';
651 if unsigned(a_in(30 downto 0)) < unsigned(b_in(30 downto 0)) then
652 a_lt_lo := '1';
653 end if;
654 if unsigned(a_in(62 downto 31)) < unsigned(b_in(62 downto 31)) then
655 a_lt_hi := '1';
656 end if;
657 if l = '1' then
658 -- 64-bit comparison
659 msb_a := a_in(63);
660 msb_b := b_in(63);
661 a_lt := a_lt_hi or (zerohi and (a_in(31) xnor b_in(31)) and a_lt_lo);
662 else
663 -- 32-bit comparison
664 msb_a := a_in(31);
665 msb_b := b_in(31);
666 a_lt := a_lt_lo;
667 end if;
668 if msb_a /= msb_b then
669 -- Comparison is clear from MSB difference.
670 -- for signed, 0 is greater; for unsigned, 1 is greater
671 trapval <= msb_a & msb_b & '0' & msb_b & msb_a;
672 else
673 -- MSBs are equal, so signed and unsigned comparisons give the
674 -- same answer.
675 trapval <= a_lt & not a_lt & '0' & a_lt & not a_lt;
676 end if;
677 end if;
678
679 -- CR result mux
680 bf := insn_bf(e_in.insn);
681 crnum := to_integer(unsigned(bf));
682 newcrf := (others => '0');
683 case current.sub_select is
684 when "000" =>
685 -- CMP and CMPL instructions
686 if e_in.is_signed = '1' then
687 newcrf := trapval(4 downto 2) & xerc_in.so;
688 else
689 newcrf := trapval(1 downto 0) & trapval(2) & xerc_in.so;
690 end if;
691 when "001" =>
692 newcrf := ppc_cmprb(a_in, b_in, insn_l(e_in.insn));
693 when "010" =>
694 newcrf := ppc_cmpeqb(a_in, b_in);
695 when "011" =>
696 if current.insn(1) = '1' then
697 -- CR logical instructions
698 j := (7 - crnum) * 4;
699 newcrf := cr_in(j + 3 downto j);
700 bt := insn_bt(e_in.insn);
701 ba := insn_ba(e_in.insn);
702 bb := insn_bb(e_in.insn);
703 btnum := 3 - to_integer(unsigned(bt(1 downto 0)));
704 banum := 31 - to_integer(unsigned(ba));
705 bbnum := 31 - to_integer(unsigned(bb));
706 -- Bits 6-9 of the instruction word give the truth table
707 -- of the requested logical operation
708 cr_operands := cr_in(banum) & cr_in(bbnum);
709 crresult := e_in.insn(6 + to_integer(unsigned(cr_operands)));
710 for i in 0 to 3 loop
711 if i = btnum then
712 newcrf(i) := crresult;
713 end if;
714 end loop;
715 else
716 -- MCRF
717 bfa := insn_bfa(e_in.insn);
718 scrnum := to_integer(unsigned(bfa));
719 j := (7 - scrnum) * 4;
720 newcrf := cr_in(j + 3 downto j);
721 end if;
722 when "100" =>
723 -- MCRXRX
724 newcrf := xerc_in.ov & xerc_in.ov32 & xerc_in.ca & xerc_in.ca32;
725 when others =>
726 end case;
727 if current.insn_type = OP_MTCRF then
728 if e_in.insn(20) = '0' then
729 -- mtcrf
730 write_cr_mask <= insn_fxm(e_in.insn);
731 else
732 -- mtocrf: We require one hot priority encoding here
733 crnum := fxm_to_num(insn_fxm(e_in.insn));
734 write_cr_mask <= num_to_fxm(crnum);
735 end if;
736 write_cr_data <= c_in(31 downto 0);
737 else
738 write_cr_mask <= num_to_fxm(crnum);
739 write_cr_data <= newcrf & newcrf & newcrf & newcrf &
740 newcrf & newcrf & newcrf & newcrf;
741 end if;
742
743 end process;
744
745 execute1_1: process(all)
746 variable v : reg_type;
747 variable bo, bi : std_ulogic_vector(4 downto 0);
748 variable overflow : std_ulogic;
749 variable lv : Execute1ToLoadstore1Type;
750 variable irq_valid : std_ulogic;
751 variable exception : std_ulogic;
752 variable illegal : std_ulogic;
753 variable is_branch : std_ulogic;
754 variable is_direct_branch : std_ulogic;
755 variable taken_branch : std_ulogic;
756 variable abs_branch : std_ulogic;
757 variable spr_val : std_ulogic_vector(63 downto 0);
758 variable do_trace : std_ulogic;
759 variable hold_wr_data : std_ulogic;
760 variable fv : Execute1ToFPUType;
761 begin
762 is_branch := '0';
763 is_direct_branch := '0';
764 taken_branch := '0';
765 abs_branch := '0';
766 hold_wr_data := '0';
767
768 v := r;
769 v.e := Execute1ToWritebackInit;
770 v.e.redir_mode := ctrl.msr(MSR_IR) & not ctrl.msr(MSR_PR) &
771 not ctrl.msr(MSR_LE) & not ctrl.msr(MSR_SF);
772 v.e.xerc := xerc_in;
773
774 lv := Execute1ToLoadstore1Init;
775 fv := Execute1ToFPUInit;
776
777 x_to_multiply.valid <= '0';
778 x_to_divider.valid <= '0';
779 v.mul_in_progress := '0';
780 v.div_in_progress := '0';
781 v.cntz_in_progress := '0';
782 v.mul_finish := '0';
783 v.ext_interrupt := '0';
784 v.taken_branch_event := '0';
785 v.br_mispredict := '0';
786
787 x_to_pmu.mfspr <= '0';
788 x_to_pmu.mtspr <= '0';
789 x_to_pmu.tbbits(3) <= ctrl.tb(63 - 47);
790 x_to_pmu.tbbits(2) <= ctrl.tb(63 - 51);
791 x_to_pmu.tbbits(1) <= ctrl.tb(63 - 55);
792 x_to_pmu.tbbits(0) <= ctrl.tb(63 - 63);
793 x_to_pmu.pmm_msr <= ctrl.msr(MSR_PMM);
794 x_to_pmu.pr_msr <= ctrl.msr(MSR_PR);
795
796 spr_result <= (others => '0');
797 spr_val := (others => '0');
798
799 ctrl_tmp <= ctrl;
800 -- FIXME: run at 512MHz not core freq
801 ctrl_tmp.tb <= std_ulogic_vector(unsigned(ctrl.tb) + 1);
802 ctrl_tmp.dec <= std_ulogic_vector(unsigned(ctrl.dec) - 1);
803
804 irq_valid := ctrl.msr(MSR_EE) and (pmu_to_x.intr or ctrl.dec(63) or ext_irq_in);
805
806 v.terminate := '0';
807 icache_inval <= '0';
808 v.busy := '0';
809
810 -- Next insn adder used in a couple of places
811 next_nia <= std_ulogic_vector(unsigned(e_in.nia) + 4);
812
813 -- rotator control signals
814 right_shift <= '1' when e_in.insn_type = OP_SHR else '0';
815 rot_clear_left <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCL else '0';
816 rot_clear_right <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCR else '0';
817 rot_sign_ext <= '1' when e_in.insn_type = OP_EXTSWSLI else '0';
818
819 do_popcnt <= '1' when e_in.insn_type = OP_POPCNT else '0';
820
821 illegal := '0';
822 if r.intr_pending = '1' then
823 v.e.srr1 := r.e.srr1;
824 v.e.intr_vec := r.e.intr_vec;
825 end if;
826 if valid_in = '1' then
827 v.e.last_nia := e_in.nia;
828 else
829 v.e.last_nia := r.e.last_nia;
830 end if;
831
832 v.e.mode_32bit := not ctrl.msr(MSR_SF);
833 v.e.instr_tag := current.instr_tag;
834
835 do_trace := valid_in and ctrl.msr(MSR_SE);
836 if valid_in = '1' then
837 v.cur_instr := e_in;
838 v.prev_op := e_in.insn_type;
839 end if;
840
841 -- Determine if there is any interrupt to be taken
842 -- before/instead of executing this instruction
843 exception := r.intr_pending;
844 if valid_in = '1' and e_in.second = '0' and r.intr_pending = '0' then
845 if HAS_FPU and r.fp_exception_next = '1' then
846 -- This is used for FP-type program interrupts that
847 -- become pending due to MSR[FE0,FE1] changing from 00 to non-zero.
848 exception := '1';
849 v.e.intr_vec := 16#700#;
850 v.e.srr1(47 - 43) := '1';
851 v.e.srr1(47 - 47) := '1';
852 elsif r.trace_next = '1' then
853 -- Generate a trace interrupt rather than executing the next instruction
854 -- or taking any asynchronous interrupt
855 exception := '1';
856 v.e.intr_vec := 16#d00#;
857 v.e.srr1(47 - 33) := '1';
858 if r.prev_op = OP_LOAD or r.prev_op = OP_ICBI or r.prev_op = OP_ICBT or
859 r.prev_op = OP_DCBT or r.prev_op = OP_DCBST or r.prev_op = OP_DCBF then
860 v.e.srr1(47 - 35) := '1';
861 elsif r.prev_op = OP_STORE or r.prev_op = OP_DCBZ or r.prev_op = OP_DCBTST then
862 v.e.srr1(47 - 36) := '1';
863 end if;
864
865 elsif irq_valid = '1' then
866 -- Don't deliver the interrupt until we have a valid instruction
867 -- coming in, so we have a valid NIA to put in SRR0.
868 if pmu_to_x.intr = '1' then
869 v.e.intr_vec := 16#f00#;
870 report "IRQ valid: PMU";
871 elsif ctrl.dec(63) = '1' then
872 v.e.intr_vec := 16#900#;
873 report "IRQ valid: DEC";
874 elsif ext_irq_in = '1' then
875 v.e.intr_vec := 16#500#;
876 report "IRQ valid: External";
877 v.ext_interrupt := '1';
878 end if;
879 exception := '1';
880
881 elsif ctrl.msr(MSR_PR) = '1' and instr_is_privileged(e_in.insn_type, e_in.insn) then
882 -- generate a program interrupt
883 exception := '1';
884 v.e.intr_vec := 16#700#;
885 -- set bit 45 to indicate privileged instruction type interrupt
886 v.e.srr1(47 - 45) := '1';
887 report "privileged instruction";
888
889 elsif not HAS_FPU and e_in.fac = FPU then
890 -- make lfd/stfd/lfs/stfs etc. illegal in no-FPU implementations
891 illegal := '1';
892
893 elsif HAS_FPU and ctrl.msr(MSR_FP) = '0' and e_in.fac = FPU then
894 -- generate a floating-point unavailable interrupt
895 exception := '1';
896 v.e.intr_vec := 16#800#;
897 report "FP unavailable interrupt";
898 end if;
899 end if;
900 if exception = '1' and l_in.in_progress = '1' then
901 -- We can't send this interrupt to writeback yet because there are
902 -- still instructions in loadstore1 that haven't completed.
903 v.intr_pending := '1';
904 v.busy := '1';
905 end if;
906 if l_in.interrupt = '1' then
907 v.intr_pending := '0';
908 end if;
909
910 v.no_instr_avail := not (e_in.valid or l_in.busy or l_in.in_progress or r.busy or fp_in.busy);
911 v.instr_dispatch := valid_in and not exception and not illegal;
912
913 if valid_in = '1' and exception = '0' and illegal = '0' and e_in.unit = ALU then
914 v.e.valid := '1';
915
916 case_0: case e_in.insn_type is
917
918 when OP_ILLEGAL =>
919 -- we need two cycles to write srr0 and 1
920 -- will need more when we have to write HEIR
921 illegal := '1';
922 when OP_SC =>
923 -- check bit 1 of the instruction is 1 so we know this is sc;
924 -- 0 would mean scv, so generate an illegal instruction interrupt
925 -- we need two cycles to write srr0 and 1
926 if e_in.insn(1) = '1' then
927 exception := '1';
928 v.e.intr_vec := 16#C00#;
929 v.e.last_nia := next_nia;
930 report "sc";
931 else
932 illegal := '1';
933 end if;
934 when OP_ATTN =>
935 -- check bits 1-10 of the instruction to make sure it's attn
936 -- if not then it is illegal
937 if e_in.insn(10 downto 1) = "0100000000" then
938 v.terminate := '1';
939 report "ATTN";
940 else
941 illegal := '1';
942 end if;
943 when OP_NOP | OP_DCBF | OP_DCBST | OP_DCBT | OP_DCBTST | OP_ICBT =>
944 -- Do nothing
945 when OP_ADD =>
946 if e_in.output_carry = '1' then
947 if e_in.input_carry /= OV then
948 set_carry(v.e, carry_32, carry_64);
949 else
950 v.e.xerc.ov := carry_64;
951 v.e.xerc.ov32 := carry_32;
952 end if;
953 end if;
954 if e_in.oe = '1' then
955 set_ov(v.e, overflow_64, overflow_32);
956 end if;
957 when OP_CMP =>
958 when OP_TRAP =>
959 -- trap instructions (tw, twi, td, tdi)
960 v.e.intr_vec := 16#700#;
961 -- set bit 46 to say trap occurred
962 v.e.srr1(47 - 46) := '1';
963 if or (trapval and insn_to(e_in.insn)) = '1' then
964 -- generate trap-type program interrupt
965 exception := '1';
966 report "trap";
967 end if;
968 when OP_ADDG6S =>
969 when OP_CMPRB =>
970 when OP_CMPEQB =>
971 when OP_AND | OP_OR | OP_XOR | OP_PRTY | OP_CMPB | OP_EXTS |
972 OP_BPERM | OP_BCD =>
973
974 when OP_B =>
975 is_branch := '1';
976 taken_branch := '1';
977 is_direct_branch := '1';
978 abs_branch := e_in.br_abs;
979 if ctrl.msr(MSR_BE) = '1' then
980 do_trace := '1';
981 end if;
982 v.taken_branch_event := '1';
983 when OP_BC | OP_BCREG =>
984 -- read_data1 is CTR
985 -- for OP_BCREG, read_data2 is target register (CTR, LR or TAR)
986 -- If this instruction updates both CTR and LR, then it is
987 -- doubled; the first instruction decrements CTR and determines
988 -- whether the branch is taken, and the second does the
989 -- redirect and the LR update.
990 bo := insn_bo(e_in.insn);
991 bi := insn_bi(e_in.insn);
992 if e_in.second = '0' then
993 taken_branch := ppc_bc_taken(bo, bi, cr_in, a_in);
994 else
995 taken_branch := r.br_taken;
996 end if;
997 v.br_taken := taken_branch;
998 v.taken_branch_event := taken_branch;
999 abs_branch := e_in.br_abs;
1000 if e_in.repeat = '0' or e_in.second = '1' then
1001 is_branch := '1';
1002 if e_in.insn_type = OP_BC then
1003 is_direct_branch := '1';
1004 end if;
1005 if ctrl.msr(MSR_BE) = '1' then
1006 do_trace := '1';
1007 end if;
1008 end if;
1009
1010 when OP_RFID =>
1011 v.e.redir_mode := (a_in(MSR_IR) or a_in(MSR_PR)) & not a_in(MSR_PR) &
1012 not a_in(MSR_LE) & not a_in(MSR_SF);
1013 -- Can't use msr_copy here because the partial function MSR
1014 -- bits should be left unchanged, not zeroed.
1015 ctrl_tmp.msr(63 downto 31) <= a_in(63 downto 31);
1016 ctrl_tmp.msr(26 downto 22) <= a_in(26 downto 22);
1017 ctrl_tmp.msr(15 downto 0) <= a_in(15 downto 0);
1018 if a_in(MSR_PR) = '1' then
1019 ctrl_tmp.msr(MSR_EE) <= '1';
1020 ctrl_tmp.msr(MSR_IR) <= '1';
1021 ctrl_tmp.msr(MSR_DR) <= '1';
1022 end if;
1023 -- mark this as a branch so CFAR gets updated
1024 is_branch := '1';
1025 taken_branch := '1';
1026 abs_branch := '1';
1027 if HAS_FPU then
1028 v.fp_exception_next := fp_in.exception and
1029 (a_in(MSR_FE0) or a_in(MSR_FE1));
1030 end if;
1031 do_trace := '0';
1032
1033 when OP_CNTZ | OP_POPCNT =>
1034 v.e.valid := '0';
1035 v.cntz_in_progress := '1';
1036 v.busy := '1';
1037 when OP_ISEL =>
1038 when OP_CROP =>
1039 when OP_MCRXRX =>
1040 when OP_DARN =>
1041 when OP_MFMSR =>
1042 when OP_MFSPR =>
1043 report "MFSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
1044 "=" & to_hstring(a_in);
1045 if is_fast_spr(e_in.read_reg1) = '1' then
1046 spr_val := a_in;
1047 if decode_spr_num(e_in.insn) = SPR_XER then
1048 -- bits 0:31 and 35:43 are treated as reserved and return 0s when read using mfxer
1049 spr_val(63 downto 32) := (others => '0');
1050 spr_val(63-32) := xerc_in.so;
1051 spr_val(63-33) := xerc_in.ov;
1052 spr_val(63-34) := xerc_in.ca;
1053 spr_val(63-35 downto 63-43) := "000000000";
1054 spr_val(63-44) := xerc_in.ov32;
1055 spr_val(63-45) := xerc_in.ca32;
1056 end if;
1057 else
1058 spr_val := c_in;
1059 case decode_spr_num(e_in.insn) is
1060 when SPR_TB =>
1061 spr_val := ctrl.tb;
1062 when SPR_TBU =>
1063 spr_val(63 downto 32) := (others => '0');
1064 spr_val(31 downto 0) := ctrl.tb(63 downto 32);
1065 when SPR_DEC =>
1066 spr_val := ctrl.dec;
1067 when SPR_CFAR =>
1068 spr_val := ctrl.cfar;
1069 when SPR_PVR =>
1070 spr_val(63 downto 32) := (others => '0');
1071 spr_val(31 downto 0) := PVR_MICROWATT;
1072 when 724 => -- LOG_ADDR SPR
1073 spr_val := log_wr_addr & r.log_addr_spr;
1074 when 725 => -- LOG_DATA SPR
1075 spr_val := log_rd_data;
1076 v.log_addr_spr := std_ulogic_vector(unsigned(r.log_addr_spr) + 1);
1077 when SPR_UPMC1 | SPR_UPMC2 | SPR_UPMC3 | SPR_UPMC4 | SPR_UPMC5 | SPR_UPMC6 |
1078 SPR_UMMCR0 | SPR_UMMCR1 | SPR_UMMCR2 | SPR_UMMCRA | SPR_USIER | SPR_USIAR | SPR_USDAR |
1079 SPR_PMC1 | SPR_PMC2 | SPR_PMC3 | SPR_PMC4 | SPR_PMC5 | SPR_PMC6 |
1080 SPR_MMCR0 | SPR_MMCR1 | SPR_MMCR2 | SPR_MMCRA | SPR_SIER | SPR_SIAR | SPR_SDAR =>
1081 x_to_pmu.mfspr <= '1';
1082 spr_val := pmu_to_x.spr_val;
1083 when others =>
1084 -- mfspr from unimplemented SPRs should be a nop in
1085 -- supervisor mode and a program interrupt for user mode
1086 if is_fast_spr(e_in.read_reg1) = '0' and ctrl.msr(MSR_PR) = '1' then
1087 illegal := '1';
1088 end if;
1089 end case;
1090 end if;
1091 spr_result <= spr_val;
1092
1093 when OP_MFCR =>
1094 when OP_MTCRF =>
1095 when OP_MTMSRD =>
1096 if e_in.insn(16) = '1' then
1097 -- just update EE and RI
1098 ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
1099 ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
1100 else
1101 -- Architecture says to leave out bits 3 (HV), 51 (ME)
1102 -- and 63 (LE) (IBM bit numbering)
1103 if e_in.is_32bit = '0' then
1104 ctrl_tmp.msr(63 downto 61) <= c_in(63 downto 61);
1105 ctrl_tmp.msr(59 downto 32) <= c_in(59 downto 32);
1106 end if;
1107 ctrl_tmp.msr(31 downto 13) <= c_in(31 downto 13);
1108 ctrl_tmp.msr(11 downto 1) <= c_in(11 downto 1);
1109 if c_in(MSR_PR) = '1' then
1110 ctrl_tmp.msr(MSR_EE) <= '1';
1111 ctrl_tmp.msr(MSR_IR) <= '1';
1112 ctrl_tmp.msr(MSR_DR) <= '1';
1113 end if;
1114 if HAS_FPU then
1115 v.fp_exception_next := fp_in.exception and
1116 (c_in(MSR_FE0) or c_in(MSR_FE1));
1117 end if;
1118 end if;
1119 when OP_MTSPR =>
1120 report "MTSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
1121 "=" & to_hstring(c_in);
1122 if is_fast_spr(e_in.write_reg) then
1123 if decode_spr_num(e_in.insn) = SPR_XER then
1124 v.e.xerc.so := c_in(63-32);
1125 v.e.xerc.ov := c_in(63-33);
1126 v.e.xerc.ca := c_in(63-34);
1127 v.e.xerc.ov32 := c_in(63-44);
1128 v.e.xerc.ca32 := c_in(63-45);
1129 end if;
1130 else
1131 -- slow spr
1132 case decode_spr_num(e_in.insn) is
1133 when SPR_DEC =>
1134 ctrl_tmp.dec <= c_in;
1135 when 724 => -- LOG_ADDR SPR
1136 v.log_addr_spr := c_in(31 downto 0);
1137 when SPR_UPMC1 | SPR_UPMC2 | SPR_UPMC3 | SPR_UPMC4 | SPR_UPMC5 | SPR_UPMC6 |
1138 SPR_UMMCR0 | SPR_UMMCR2 | SPR_UMMCRA |
1139 SPR_PMC1 | SPR_PMC2 | SPR_PMC3 | SPR_PMC4 | SPR_PMC5 | SPR_PMC6 |
1140 SPR_MMCR0 | SPR_MMCR1 | SPR_MMCR2 | SPR_MMCRA | SPR_SIER | SPR_SIAR | SPR_SDAR =>
1141 x_to_pmu.mtspr <= '1';
1142 when others =>
1143 -- mtspr to unimplemented SPRs should be a nop in
1144 -- supervisor mode and a program interrupt for user mode
1145 if ctrl.msr(MSR_PR) = '1' then
1146 illegal := '1';
1147 end if;
1148 end case;
1149 end if;
1150 when OP_RLC | OP_RLCL | OP_RLCR | OP_SHL | OP_SHR | OP_EXTSWSLI =>
1151 if e_in.output_carry = '1' then
1152 set_carry(v.e, rotator_carry, rotator_carry);
1153 end if;
1154 when OP_SETB =>
1155
1156 when OP_ISYNC =>
1157 v.e.redirect := '1';
1158 v.e.br_offset := std_ulogic_vector(to_unsigned(4, 64));
1159
1160 when OP_ICBI =>
1161 icache_inval <= '1';
1162
1163 when OP_MUL_L64 | OP_MUL_H64 | OP_MUL_H32 =>
1164 if HAS_SHORT_MULT and e_in.insn_type = OP_MUL_L64 and e_in.insn(26) = '1' and
1165 fits_in_n_bits(a_in, 16) and fits_in_n_bits(b_in, 16) then
1166 -- Operands fit into 16 bits, so use short multiplier
1167 if e_in.oe = '1' then
1168 -- Note 16x16 multiply can't overflow, even for mullwo
1169 set_ov(v.e, '0', '0');
1170 end if;
1171 else
1172 -- Use standard multiplier
1173 v.e.valid := '0';
1174 v.mul_in_progress := '1';
1175 v.busy := '1';
1176 x_to_multiply.valid <= '1';
1177 end if;
1178
1179 when OP_DIV | OP_DIVE | OP_MOD =>
1180 v.e.valid := '0';
1181 v.div_in_progress := '1';
1182 v.busy := '1';
1183 x_to_divider.valid <= '1';
1184
1185 when others =>
1186 v.terminate := '1';
1187 report "illegal";
1188 end case;
1189
1190 -- Mispredicted branches cause a redirect
1191 if is_branch = '1' then
1192 if taken_branch = '1' then
1193 ctrl_tmp.cfar <= e_in.nia;
1194 end if;
1195 if taken_branch = '1' then
1196 v.e.br_offset := b_in;
1197 v.e.abs_br := abs_branch;
1198 else
1199 v.e.br_offset := std_ulogic_vector(to_unsigned(4, 64));
1200 end if;
1201 if taken_branch /= e_in.br_pred then
1202 v.e.redirect := '1';
1203 v.br_mispredict := is_direct_branch;
1204 end if;
1205 v.e.br_last := is_direct_branch;
1206 v.e.br_taken := taken_branch;
1207 end if;
1208
1209 elsif valid_in = '1' and exception = '0' and illegal = '0' then
1210 -- instruction for other units, i.e. LDST
1211 if e_in.unit = LDST then
1212 lv.valid := '1';
1213 elsif e_in.unit = NONE then
1214 illegal := '1';
1215 elsif HAS_FPU and e_in.unit = FPU then
1216 fv.valid := '1';
1217 end if;
1218 -- Handling an ITLB miss doesn't count as having executed an instruction
1219 if e_in.insn_type = OP_FETCH_FAILED then
1220 do_trace := '0';
1221 end if;
1222 end if;
1223
1224 -- The following cases all occur when r.busy = 1 and therefore
1225 -- valid_in = 0. Hence they don't happen in the same cycle as any of
1226 -- the cases above which depend on valid_in = 1.
1227 if r.cntz_in_progress = '1' then
1228 -- cnt[lt]z and popcnt* always take two cycles
1229 v.e.valid := '1';
1230 elsif r.mul_in_progress = '1' or r.div_in_progress = '1' then
1231 if (r.mul_in_progress = '1' and multiply_to_x.valid = '1') or
1232 (r.div_in_progress = '1' and divider_to_x.valid = '1') then
1233 if r.mul_in_progress = '1' then
1234 overflow := '0';
1235 else
1236 overflow := divider_to_x.overflow;
1237 end if;
1238 if r.mul_in_progress = '1' and current.oe = '1' then
1239 -- have to wait until next cycle for overflow indication
1240 v.mul_finish := '1';
1241 v.busy := '1';
1242 else
1243 -- We must test oe because the RC update code in writeback
1244 -- will use the xerc value to set CR0:SO so we must not clobber
1245 -- xerc if OE wasn't set.
1246 if current.oe = '1' then
1247 v.e.xerc.ov := overflow;
1248 v.e.xerc.ov32 := overflow;
1249 if overflow = '1' then
1250 v.e.xerc.so := '1';
1251 end if;
1252 end if;
1253 v.e.valid := '1';
1254 end if;
1255 else
1256 v.busy := '1';
1257 v.mul_in_progress := r.mul_in_progress;
1258 v.div_in_progress := r.div_in_progress;
1259 end if;
1260 elsif r.mul_finish = '1' then
1261 hold_wr_data := '1';
1262 v.e.xerc.ov := multiply_to_x.overflow;
1263 v.e.xerc.ov32 := multiply_to_x.overflow;
1264 if multiply_to_x.overflow = '1' then
1265 v.e.xerc.so := '1';
1266 end if;
1267 v.e.valid := '1';
1268 end if;
1269
1270 if illegal = '1' then
1271 exception := '1';
1272 v.e.intr_vec := 16#700#;
1273 -- Since we aren't doing Hypervisor emulation assist (0xe40) we
1274 -- set bit 44 to indicate we have an illegal
1275 v.e.srr1(47 - 44) := '1';
1276 report "illegal";
1277 end if;
1278
1279 v.e.interrupt := exception and not (l_in.in_progress or l_in.interrupt);
1280 if v.e.interrupt = '1' then
1281 v.intr_pending := '0';
1282 end if;
1283
1284 if do_trace = '1' then
1285 v.trace_next := '1';
1286 end if;
1287
1288 if interrupt_in = '1' then
1289 ctrl_tmp.msr(MSR_SF) <= '1';
1290 ctrl_tmp.msr(MSR_EE) <= '0';
1291 ctrl_tmp.msr(MSR_PR) <= '0';
1292 ctrl_tmp.msr(MSR_SE) <= '0';
1293 ctrl_tmp.msr(MSR_BE) <= '0';
1294 ctrl_tmp.msr(MSR_FP) <= '0';
1295 ctrl_tmp.msr(MSR_FE0) <= '0';
1296 ctrl_tmp.msr(MSR_FE1) <= '0';
1297 ctrl_tmp.msr(MSR_IR) <= '0';
1298 ctrl_tmp.msr(MSR_DR) <= '0';
1299 ctrl_tmp.msr(MSR_RI) <= '0';
1300 ctrl_tmp.msr(MSR_LE) <= '1';
1301 v.trace_next := '0';
1302 v.fp_exception_next := '0';
1303 v.intr_pending := '0';
1304 end if;
1305
1306 if hold_wr_data = '0' then
1307 v.e.write_data := alu_result;
1308 else
1309 v.e.write_data := r.e.write_data;
1310 end if;
1311 v.e.write_reg := current.write_reg;
1312 v.e.write_enable := current.write_reg_enable and v.e.valid and not exception;
1313 v.e.rc := current.rc and v.e.valid and not exception;
1314 v.e.write_cr_data := write_cr_data;
1315 v.e.write_cr_mask := write_cr_mask;
1316 v.e.write_cr_enable := current.output_cr and v.e.valid and not exception;
1317 v.e.write_xerc_enable := current.output_xer and v.e.valid and not exception;
1318
1319 bypass_data.tag.valid <= current.instr_tag.valid and current.write_reg_enable and v.e.valid;
1320 bypass_data.tag.tag <= current.instr_tag.tag;
1321 bypass_data.data <= v.e.write_data;
1322
1323 bypass_cr_data.tag.valid <= current.instr_tag.valid and current.output_cr and v.e.valid;
1324 bypass_cr_data.tag.tag <= current.instr_tag.tag;
1325 for i in 0 to 7 loop
1326 if v.e.write_cr_mask(i) = '1' then
1327 bypass_cr_data.data(i*4 + 3 downto i*4) <= v.e.write_cr_data(i*4 + 3 downto i*4);
1328 else
1329 bypass_cr_data.data(i*4 + 3 downto i*4) <= cr_in(i*4 + 3 downto i*4);
1330 end if;
1331 end loop;
1332
1333 -- Outputs to loadstore1 (async)
1334 lv.op := e_in.insn_type;
1335 lv.nia := e_in.nia;
1336 lv.instr_tag := e_in.instr_tag;
1337 lv.addr1 := a_in;
1338 lv.addr2 := b_in;
1339 lv.data := c_in;
1340 lv.write_reg := e_in.write_reg;
1341 lv.length := e_in.data_len;
1342 lv.byte_reverse := e_in.byte_reverse xnor ctrl.msr(MSR_LE);
1343 lv.sign_extend := e_in.sign_extend;
1344 lv.update := e_in.update;
1345 lv.xerc := xerc_in;
1346 lv.reserve := e_in.reserve;
1347 lv.rc := e_in.rc;
1348 lv.insn := e_in.insn;
1349 -- decode l*cix and st*cix instructions here
1350 if e_in.insn(31 downto 26) = "011111" and e_in.insn(10 downto 9) = "11" and
1351 e_in.insn(5 downto 1) = "10101" then
1352 lv.ci := '1';
1353 end if;
1354 lv.virt_mode := ctrl.msr(MSR_DR);
1355 lv.priv_mode := not ctrl.msr(MSR_PR);
1356 lv.mode_32bit := not ctrl.msr(MSR_SF);
1357 lv.is_32bit := e_in.is_32bit;
1358 lv.repeat := e_in.repeat;
1359 lv.second := e_in.second;
1360
1361 -- Outputs to FPU
1362 fv.op := e_in.insn_type;
1363 fv.nia := e_in.nia;
1364 fv.insn := e_in.insn;
1365 fv.itag := e_in.instr_tag;
1366 fv.single := e_in.is_32bit;
1367 fv.fe_mode := ctrl.msr(MSR_FE0) & ctrl.msr(MSR_FE1);
1368 fv.fra := a_in;
1369 fv.frb := b_in;
1370 fv.frc := c_in;
1371 fv.frt := e_in.write_reg;
1372 fv.rc := e_in.rc;
1373 fv.out_cr := e_in.output_cr;
1374
1375 -- Update registers
1376 rin <= v;
1377
1378 -- update outputs
1379 l_out <= lv;
1380 e_out <= r.e;
1381 e_out.msr <= msr_copy(ctrl.msr);
1382 fp_out <= fv;
1383
1384 exception_log <= exception;
1385 irq_valid_log <= irq_valid;
1386 end process;
1387
1388 e1_log: if LOG_LENGTH > 0 generate
1389 signal log_data : std_ulogic_vector(14 downto 0);
1390 begin
1391 ex1_log : process(clk)
1392 begin
1393 if rising_edge(clk) then
1394 log_data <= ctrl.msr(MSR_EE) & ctrl.msr(MSR_PR) &
1395 ctrl.msr(MSR_IR) & ctrl.msr(MSR_DR) &
1396 exception_log &
1397 irq_valid_log &
1398 interrupt_in &
1399 "000" &
1400 r.e.write_enable &
1401 r.e.valid &
1402 (r.e.redirect or r.e.interrupt) &
1403 r.busy &
1404 flush_in;
1405 end if;
1406 end process;
1407 log_out <= log_data;
1408 end generate;
1409 end architecture behaviour;