Merge pull request #173 from Jbalkind/core-vcs-syntax
[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 );
17 port (
18 clk : in std_ulogic;
19 rst : in std_ulogic;
20
21 -- asynchronous
22 flush_out : out std_ulogic;
23 stall_out : out std_ulogic;
24
25 e_in : in Decode2ToExecute1Type;
26 l_in : in Loadstore1ToExecute1Type;
27
28 i_in : in XicsToExecute1Type;
29
30 -- asynchronous
31 l_out : out Execute1ToLoadstore1Type;
32 f_out : out Execute1ToFetch1Type;
33
34 e_out : out Execute1ToWritebackType;
35
36 dbg_msr_out : out std_ulogic_vector(63 downto 0);
37
38 icache_inval : out std_ulogic;
39 terminate_out : out std_ulogic
40 );
41 end entity execute1;
42
43 architecture behaviour of execute1 is
44 type reg_type is record
45 e : Execute1ToWritebackType;
46 lr_update : std_ulogic;
47 next_lr : std_ulogic_vector(63 downto 0);
48 mul_in_progress : std_ulogic;
49 div_in_progress : std_ulogic;
50 cntz_in_progress : std_ulogic;
51 slow_op_dest : gpr_index_t;
52 slow_op_rc : std_ulogic;
53 slow_op_oe : std_ulogic;
54 slow_op_xerc : xer_common_t;
55 ldst_nia : std_ulogic_vector(63 downto 0);
56 end record;
57 constant reg_type_init : reg_type :=
58 (e => Execute1ToWritebackInit, lr_update => '0',
59 mul_in_progress => '0', div_in_progress => '0', cntz_in_progress => '0',
60 slow_op_rc => '0', slow_op_oe => '0', slow_op_xerc => xerc_init,
61 next_lr => (others => '0'), ldst_nia => (others => '0'), others => (others => '0'));
62
63 signal r, rin : reg_type;
64
65 signal a_in, b_in, c_in : std_ulogic_vector(63 downto 0);
66
67 signal ctrl: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
68 signal ctrl_tmp: ctrl_t := (irq_state => WRITE_SRR0, others => (others => '0'));
69 signal right_shift, rot_clear_left, rot_clear_right: std_ulogic;
70 signal rot_sign_ext: std_ulogic;
71 signal rotator_result: std_ulogic_vector(63 downto 0);
72 signal rotator_carry: std_ulogic;
73 signal logical_result: std_ulogic_vector(63 downto 0);
74 signal countzero_result: std_ulogic_vector(63 downto 0);
75 signal popcnt_result: std_ulogic_vector(63 downto 0);
76 signal parity_result: std_ulogic_vector(63 downto 0);
77
78 -- multiply signals
79 signal x_to_multiply: Execute1ToMultiplyType;
80 signal multiply_to_x: MultiplyToExecute1Type;
81
82 -- divider signals
83 signal x_to_divider: Execute1ToDividerType;
84 signal divider_to_x: DividerToExecute1Type;
85
86 type privilege_level is (USER, SUPER);
87 type op_privilege_array is array(insn_type_t) of privilege_level;
88 constant op_privilege: op_privilege_array := (
89 OP_ATTN => SUPER,
90 OP_MFMSR => SUPER,
91 OP_MTMSRD => SUPER,
92 OP_RFID => SUPER,
93 OP_TLBIE => SUPER,
94 others => USER
95 );
96
97 function instr_is_privileged(op: insn_type_t; insn: std_ulogic_vector(31 downto 0))
98 return boolean is
99 begin
100 if op_privilege(op) = SUPER then
101 return true;
102 elsif op = OP_MFSPR or op = OP_MTSPR then
103 return insn(20) = '1';
104 else
105 return false;
106 end if;
107 end;
108
109 procedure set_carry(e: inout Execute1ToWritebackType;
110 carry32 : in std_ulogic;
111 carry : in std_ulogic) is
112 begin
113 e.xerc.ca32 := carry32;
114 e.xerc.ca := carry;
115 e.write_xerc_enable := '1';
116 end;
117
118 procedure set_ov(e: inout Execute1ToWritebackType;
119 ov : in std_ulogic;
120 ov32 : in std_ulogic) is
121 begin
122 e.xerc.ov32 := ov32;
123 e.xerc.ov := ov;
124 if ov = '1' then
125 e.xerc.so := '1';
126 end if;
127 e.write_xerc_enable := '1';
128 end;
129
130 function calc_ov(msb_a : std_ulogic; msb_b: std_ulogic;
131 ca: std_ulogic; msb_r: std_ulogic) return std_ulogic is
132 begin
133 return (ca xor msb_r) and not (msb_a xor msb_b);
134 end;
135
136 function decode_input_carry(ic : carry_in_t;
137 xerc : xer_common_t) return std_ulogic is
138 begin
139 case ic is
140 when ZERO =>
141 return '0';
142 when CA =>
143 return xerc.ca;
144 when ONE =>
145 return '1';
146 end case;
147 end;
148
149 function msr_copy(msr: std_ulogic_vector(63 downto 0))
150 return std_ulogic_vector is
151 variable msr_out: std_ulogic_vector(63 downto 0);
152 begin
153 -- ISA says this:
154 -- Defined MSR bits are classified as either full func-
155 -- tion or partial function. Full function MSR bits are
156 -- saved in SRR1 or HSRR1 when an interrupt other
157 -- than a System Call Vectored interrupt occurs and
158 -- restored by rfscv, rfid, or hrfid, while partial func-
159 -- tion MSR bits are not saved or restored.
160 -- Full function MSR bits lie in the range 0:32, 37:41, and
161 -- 48:63, and partial function MSR bits lie in the range
162 -- 33:36 and 42:47. (Note this is IBM bit numbering).
163 msr_out := (others => '0');
164 msr_out(63 downto 31) := msr(63 downto 31);
165 msr_out(26 downto 22) := msr(26 downto 22);
166 msr_out(15 downto 0) := msr(15 downto 0);
167 return msr_out;
168 end;
169
170 begin
171
172 rotator_0: entity work.rotator
173 port map (
174 rs => c_in,
175 ra => a_in,
176 shift => b_in(6 downto 0),
177 insn => e_in.insn,
178 is_32bit => e_in.is_32bit,
179 right_shift => right_shift,
180 arith => e_in.is_signed,
181 clear_left => rot_clear_left,
182 clear_right => rot_clear_right,
183 sign_ext_rs => rot_sign_ext,
184 result => rotator_result,
185 carry_out => rotator_carry
186 );
187
188 logical_0: entity work.logical
189 port map (
190 rs => c_in,
191 rb => b_in,
192 op => e_in.insn_type,
193 invert_in => e_in.invert_a,
194 invert_out => e_in.invert_out,
195 result => logical_result,
196 datalen => e_in.data_len,
197 popcnt => popcnt_result,
198 parity => parity_result
199 );
200
201 countzero_0: entity work.zero_counter
202 port map (
203 clk => clk,
204 rs => c_in,
205 count_right => e_in.insn(10),
206 is_32bit => e_in.is_32bit,
207 result => countzero_result
208 );
209
210 multiply_0: entity work.multiply
211 port map (
212 clk => clk,
213 m_in => x_to_multiply,
214 m_out => multiply_to_x
215 );
216
217 divider_0: entity work.divider
218 port map (
219 clk => clk,
220 rst => rst,
221 d_in => x_to_divider,
222 d_out => divider_to_x
223 );
224
225 dbg_msr_out <= ctrl.msr;
226
227 a_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data1 = '1' else e_in.read_data1;
228 b_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data2 = '1' else e_in.read_data2;
229 c_in <= r.e.write_data when EX1_BYPASS and e_in.bypass_data3 = '1' else e_in.read_data3;
230
231 execute1_0: process(clk)
232 begin
233 if rising_edge(clk) then
234 if rst = '1' then
235 r <= reg_type_init;
236 ctrl.msr <= (MSR_SF => '1', MSR_LE => '1', others => '0');
237 ctrl.irq_state <= WRITE_SRR0;
238 else
239 r <= rin;
240 ctrl <= ctrl_tmp;
241 assert not (r.lr_update = '1' and e_in.valid = '1')
242 report "LR update collision with valid in EX1"
243 severity failure;
244 if r.lr_update = '1' then
245 report "LR update to " & to_hstring(r.next_lr);
246 end if;
247 end if;
248 end if;
249 end process;
250
251 execute1_1: process(all)
252 variable v : reg_type;
253 variable a_inv : std_ulogic_vector(63 downto 0);
254 variable result : std_ulogic_vector(63 downto 0);
255 variable newcrf : std_ulogic_vector(3 downto 0);
256 variable result_with_carry : std_ulogic_vector(64 downto 0);
257 variable result_en : std_ulogic;
258 variable crnum : crnum_t;
259 variable crbit : integer range 0 to 31;
260 variable scrnum : crnum_t;
261 variable lo, hi : integer;
262 variable sh, mb, me : std_ulogic_vector(5 downto 0);
263 variable sh32, mb32, me32 : std_ulogic_vector(4 downto 0);
264 variable bo, bi : std_ulogic_vector(4 downto 0);
265 variable bf, bfa : std_ulogic_vector(2 downto 0);
266 variable cr_op : std_ulogic_vector(9 downto 0);
267 variable cr_operands : std_ulogic_vector(1 downto 0);
268 variable bt, ba, bb : std_ulogic_vector(4 downto 0);
269 variable btnum, banum, bbnum : integer range 0 to 31;
270 variable crresult : std_ulogic;
271 variable l : std_ulogic;
272 variable next_nia : std_ulogic_vector(63 downto 0);
273 variable carry_32, carry_64 : std_ulogic;
274 variable sign1, sign2 : std_ulogic;
275 variable abs1, abs2 : signed(63 downto 0);
276 variable overflow : std_ulogic;
277 variable negative : std_ulogic;
278 variable zerohi, zerolo : std_ulogic;
279 variable msb_a, msb_b : std_ulogic;
280 variable a_lt : std_ulogic;
281 variable lv : Execute1ToLoadstore1Type;
282 variable irq_valid : std_ulogic;
283 variable exception : std_ulogic;
284 variable exception_nextpc : std_ulogic;
285 variable trapval : std_ulogic_vector(4 downto 0);
286 variable illegal : std_ulogic;
287 begin
288 result := (others => '0');
289 result_with_carry := (others => '0');
290 result_en := '0';
291 newcrf := (others => '0');
292
293 v := r;
294 v.e := Execute1ToWritebackInit;
295 lv := Execute1ToLoadstore1Init;
296
297 -- XER forwarding. To avoid having to track XER hazards, we
298 -- use the previously latched value.
299 --
300 -- If the XER was modified by a multiply or a divide, those are
301 -- single issue, we'll get the up to date value from decode2 from
302 -- the register file.
303 --
304 -- If it was modified by an instruction older than the previous
305 -- one in EX1, it will have also hit writeback and will be up
306 -- to date in decode2.
307 --
308 -- That leaves us with the case where it was updated by the previous
309 -- instruction in EX1. In that case, we can forward it back here.
310 --
311 -- This will break if we allow pipelining of multiply and divide,
312 -- but ideally, those should go via EX1 anyway and run as a state
313 -- machine from here.
314 --
315 -- One additional hazard to beware of is an XER:SO modifying instruction
316 -- in EX1 followed immediately by a store conditional. Due to our
317 -- writeback latency, the store will go down the LSU with the previous
318 -- XER value, thus the stcx. will set CR0:SO using an obsolete SO value.
319 --
320 -- We will need to handle that if we ever make stcx. not single issue
321 --
322 -- We always pass a valid XER value downto writeback even when
323 -- we aren't updating it, in order for XER:SO -> CR0:SO transfer
324 -- to work for RC instructions.
325 --
326 if r.e.write_xerc_enable = '1' then
327 v.e.xerc := r.e.xerc;
328 else
329 v.e.xerc := e_in.xerc;
330 end if;
331
332 v.lr_update := '0';
333 v.mul_in_progress := '0';
334 v.div_in_progress := '0';
335 v.cntz_in_progress := '0';
336
337 -- signals to multiply unit
338 x_to_multiply <= Execute1ToMultiplyInit;
339 x_to_multiply.insn_type <= e_in.insn_type;
340 x_to_multiply.is_32bit <= e_in.is_32bit;
341
342 if e_in.is_32bit = '1' then
343 if e_in.is_signed = '1' then
344 x_to_multiply.data1 <= (others => a_in(31));
345 x_to_multiply.data1(31 downto 0) <= a_in(31 downto 0);
346 x_to_multiply.data2 <= (others => b_in(31));
347 x_to_multiply.data2(31 downto 0) <= b_in(31 downto 0);
348 else
349 x_to_multiply.data1 <= '0' & x"00000000" & a_in(31 downto 0);
350 x_to_multiply.data2 <= '0' & x"00000000" & b_in(31 downto 0);
351 end if;
352 else
353 if e_in.is_signed = '1' then
354 x_to_multiply.data1 <= a_in(63) & a_in;
355 x_to_multiply.data2 <= b_in(63) & b_in;
356 else
357 x_to_multiply.data1 <= '0' & a_in;
358 x_to_multiply.data2 <= '0' & b_in;
359 end if;
360 end if;
361
362 -- signals to divide unit
363 sign1 := '0';
364 sign2 := '0';
365 if e_in.is_signed = '1' then
366 if e_in.is_32bit = '1' then
367 sign1 := a_in(31);
368 sign2 := b_in(31);
369 else
370 sign1 := a_in(63);
371 sign2 := b_in(63);
372 end if;
373 end if;
374 -- take absolute values
375 if sign1 = '0' then
376 abs1 := signed(a_in);
377 else
378 abs1 := - signed(a_in);
379 end if;
380 if sign2 = '0' then
381 abs2 := signed(b_in);
382 else
383 abs2 := - signed(b_in);
384 end if;
385
386 x_to_divider <= Execute1ToDividerInit;
387 x_to_divider.is_signed <= e_in.is_signed;
388 x_to_divider.is_32bit <= e_in.is_32bit;
389 if e_in.insn_type = OP_MOD then
390 x_to_divider.is_modulus <= '1';
391 end if;
392 x_to_divider.neg_result <= sign1 xor (sign2 and not x_to_divider.is_modulus);
393 if e_in.is_32bit = '0' then
394 -- 64-bit forms
395 if e_in.insn_type = OP_DIVE then
396 x_to_divider.is_extended <= '1';
397 end if;
398 x_to_divider.dividend <= std_ulogic_vector(abs1);
399 x_to_divider.divisor <= std_ulogic_vector(abs2);
400 else
401 -- 32-bit forms
402 x_to_divider.is_extended <= '0';
403 if e_in.insn_type = OP_DIVE then -- extended forms
404 x_to_divider.dividend <= std_ulogic_vector(abs1(31 downto 0)) & x"00000000";
405 else
406 x_to_divider.dividend <= x"00000000" & std_ulogic_vector(abs1(31 downto 0));
407 end if;
408 x_to_divider.divisor <= x"00000000" & std_ulogic_vector(abs2(31 downto 0));
409 end if;
410
411 ctrl_tmp <= ctrl;
412 -- FIXME: run at 512MHz not core freq
413 ctrl_tmp.tb <= std_ulogic_vector(unsigned(ctrl.tb) + 1);
414 ctrl_tmp.dec <= std_ulogic_vector(unsigned(ctrl.dec) - 1);
415
416 irq_valid := '0';
417 if ctrl.msr(MSR_EE) = '1' then
418 if ctrl.dec(63) = '1' then
419 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#900#, 64));
420 report "IRQ valid: DEC";
421 irq_valid := '1';
422 elsif i_in.irq = '1' then
423 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#500#, 64));
424 report "IRQ valid: External";
425 irq_valid := '1';
426 end if;
427 end if;
428
429 terminate_out <= '0';
430 icache_inval <= '0';
431 stall_out <= '0';
432 f_out <= Execute1ToFetch1TypeInit;
433 -- send MSR[IR] and ~MSR[PR] up to fetch1
434 f_out.virt_mode <= ctrl.msr(MSR_IR);
435 f_out.priv_mode <= not ctrl.msr(MSR_PR);
436
437 -- Next insn adder used in a couple of places
438 next_nia := std_ulogic_vector(unsigned(e_in.nia) + 4);
439
440 -- rotator control signals
441 right_shift <= '1' when e_in.insn_type = OP_SHR else '0';
442 rot_clear_left <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCL else '0';
443 rot_clear_right <= '1' when e_in.insn_type = OP_RLC or e_in.insn_type = OP_RLCR else '0';
444 rot_sign_ext <= '1' when e_in.insn_type = OP_EXTSWSLI else '0';
445
446 ctrl_tmp.irq_state <= WRITE_SRR0;
447 exception := '0';
448 illegal := '0';
449 exception_nextpc := '0';
450 v.e.exc_write_enable := '0';
451 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
452 v.e.exc_write_data := e_in.nia;
453
454 if ctrl.irq_state = WRITE_SRR1 then
455 v.e.exc_write_reg := fast_spr_num(SPR_SRR1);
456 v.e.exc_write_data := ctrl.srr1;
457 v.e.exc_write_enable := '1';
458 ctrl_tmp.msr(MSR_SF) <= '1';
459 ctrl_tmp.msr(MSR_EE) <= '0';
460 ctrl_tmp.msr(MSR_PR) <= '0';
461 ctrl_tmp.msr(MSR_IR) <= '0';
462 ctrl_tmp.msr(MSR_DR) <= '0';
463 ctrl_tmp.msr(MSR_RI) <= '0';
464 ctrl_tmp.msr(MSR_LE) <= '1';
465 f_out.redirect <= '1';
466 f_out.virt_mode <= '0';
467 f_out.priv_mode <= '1';
468 f_out.redirect_nia <= ctrl.irq_nia;
469 v.e.valid := e_in.valid;
470 report "Writing SRR1: " & to_hstring(ctrl.srr1);
471
472 elsif irq_valid = '1' and e_in.valid = '1' then
473 -- we need two cycles to write srr0 and 1
474 -- will need more when we have to write HEIR
475 -- Don't deliver the interrupt until we have a valid instruction
476 -- coming in, so we have a valid NIA to put in SRR0.
477 exception := '1';
478 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
479
480 elsif e_in.valid = '1' and ctrl.msr(MSR_PR) = '1' and
481 instr_is_privileged(e_in.insn_type, e_in.insn) then
482 -- generate a program interrupt
483 exception := '1';
484 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#700#, 64));
485 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
486 -- set bit 45 to indicate privileged instruction type interrupt
487 ctrl_tmp.srr1(63 - 45) <= '1';
488 report "privileged instruction";
489
490 elsif e_in.valid = '1' and e_in.unit = ALU then
491
492 report "execute nia " & to_hstring(e_in.nia);
493
494 v.e.valid := '1';
495 v.e.write_reg := e_in.write_reg;
496 v.slow_op_dest := gspr_to_gpr(e_in.write_reg);
497 v.slow_op_rc := e_in.rc;
498 v.slow_op_oe := e_in.oe;
499 v.slow_op_xerc := v.e.xerc;
500
501 case_0: case e_in.insn_type is
502
503 when OP_ILLEGAL =>
504 -- we need two cycles to write srr0 and 1
505 -- will need more when we have to write HEIR
506 illegal := '1';
507 when OP_SC =>
508 -- check bit 1 of the instruction is 1 so we know this is sc;
509 -- 0 would mean scv, so generate an illegal instruction interrupt
510 -- we need two cycles to write srr0 and 1
511 if e_in.insn(1) = '1' then
512 exception := '1';
513 exception_nextpc := '1';
514 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#C00#, 64));
515 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
516 report "sc";
517 else
518 illegal := '1';
519 end if;
520 when OP_ATTN =>
521 -- check bits 1-10 of the instruction to make sure it's attn
522 -- if not then it is illegal
523 if e_in.insn(10 downto 1) = "0100000000" then
524 terminate_out <= '1';
525 report "ATTN";
526 else
527 illegal := '1';
528 end if;
529 when OP_NOP =>
530 -- Do nothing
531 when OP_ADD | OP_CMP | OP_TRAP =>
532 if e_in.invert_a = '0' then
533 a_inv := a_in;
534 else
535 a_inv := not a_in;
536 end if;
537 result_with_carry := ppc_adde(a_inv, b_in,
538 decode_input_carry(e_in.input_carry, v.e.xerc));
539 result := result_with_carry(63 downto 0);
540 carry_32 := result(32) xor a_inv(32) xor b_in(32);
541 carry_64 := result_with_carry(64);
542 if e_in.insn_type = OP_ADD then
543 if e_in.output_carry = '1' then
544 set_carry(v.e, carry_32, carry_64);
545 end if;
546 if e_in.oe = '1' then
547 set_ov(v.e,
548 calc_ov(a_inv(63), b_in(63), carry_64, result_with_carry(63)),
549 calc_ov(a_inv(31), b_in(31), carry_32, result_with_carry(31)));
550 end if;
551 result_en := '1';
552 else
553 -- trap, CMP and CMPL instructions
554 -- Note, we have done RB - RA, not RA - RB
555 if e_in.insn_type = OP_CMP then
556 l := insn_l(e_in.insn);
557 else
558 l := not e_in.is_32bit;
559 end if;
560 zerolo := not (or (a_in(31 downto 0) xor b_in(31 downto 0)));
561 zerohi := not (or (a_in(63 downto 32) xor b_in(63 downto 32)));
562 if zerolo = '1' and (l = '0' or zerohi = '1') then
563 -- values are equal
564 trapval := "00100";
565 else
566 if l = '1' then
567 -- 64-bit comparison
568 msb_a := a_in(63);
569 msb_b := b_in(63);
570 else
571 -- 32-bit comparison
572 msb_a := a_in(31);
573 msb_b := b_in(31);
574 end if;
575 if msb_a /= msb_b then
576 -- Subtraction might overflow, but
577 -- comparison is clear from MSB difference.
578 -- for signed, 0 is greater; for unsigned, 1 is greater
579 trapval := msb_a & msb_b & '0' & msb_b & msb_a;
580 else
581 -- Subtraction cannot overflow since MSBs are equal.
582 -- carry = 1 indicates RA is smaller (signed or unsigned)
583 a_lt := (not l and carry_32) or (l and carry_64);
584 trapval := a_lt & not a_lt & '0' & a_lt & not a_lt;
585 end if;
586 end if;
587 if e_in.insn_type = OP_CMP then
588 if e_in.is_signed = '1' then
589 newcrf := trapval(4 downto 2) & v.e.xerc.so;
590 else
591 newcrf := trapval(1 downto 0) & trapval(2) & v.e.xerc.so;
592 end if;
593 bf := insn_bf(e_in.insn);
594 crnum := to_integer(unsigned(bf));
595 v.e.write_cr_enable := '1';
596 v.e.write_cr_mask := num_to_fxm(crnum);
597 for i in 0 to 7 loop
598 lo := i*4;
599 hi := lo + 3;
600 v.e.write_cr_data(hi downto lo) := newcrf;
601 end loop;
602 else
603 -- trap instructions (tw, twi, td, tdi)
604 if or (trapval and insn_to(e_in.insn)) = '1' then
605 -- generate trap-type program interrupt
606 exception := '1';
607 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#700#, 64));
608 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
609 -- set bit 46 to say trap occurred
610 ctrl_tmp.srr1(63 - 46) <= '1';
611 report "trap";
612 end if;
613 end if;
614 end if;
615 when OP_AND | OP_OR | OP_XOR =>
616 result := logical_result;
617 result_en := '1';
618 when OP_B =>
619 f_out.redirect <= '1';
620 if (insn_aa(e_in.insn)) then
621 f_out.redirect_nia <= b_in;
622 else
623 f_out.redirect_nia <= std_ulogic_vector(signed(e_in.nia) + signed(b_in));
624 end if;
625 when OP_BC =>
626 -- read_data1 is CTR
627 bo := insn_bo(e_in.insn);
628 bi := insn_bi(e_in.insn);
629 if bo(4-2) = '0' then
630 result := std_ulogic_vector(unsigned(a_in) - 1);
631 result_en := '1';
632 v.e.write_reg := fast_spr_num(SPR_CTR);
633 end if;
634 if ppc_bc_taken(bo, bi, e_in.cr, a_in) = 1 then
635 f_out.redirect <= '1';
636 if (insn_aa(e_in.insn)) then
637 f_out.redirect_nia <= b_in;
638 else
639 f_out.redirect_nia <= std_ulogic_vector(signed(e_in.nia) + signed(b_in));
640 end if;
641 end if;
642 when OP_BCREG =>
643 -- read_data1 is CTR
644 -- read_data2 is target register (CTR, LR or TAR)
645 bo := insn_bo(e_in.insn);
646 bi := insn_bi(e_in.insn);
647 if bo(4-2) = '0' and e_in.insn(10) = '0' then
648 result := std_ulogic_vector(unsigned(a_in) - 1);
649 result_en := '1';
650 v.e.write_reg := fast_spr_num(SPR_CTR);
651 end if;
652 if ppc_bc_taken(bo, bi, e_in.cr, a_in) = 1 then
653 f_out.redirect <= '1';
654 f_out.redirect_nia <= b_in(63 downto 2) & "00";
655 end if;
656
657 when OP_RFID =>
658 f_out.redirect <= '1';
659 f_out.virt_mode <= b_in(MSR_IR) or b_in(MSR_PR);
660 f_out.priv_mode <= not b_in(MSR_PR);
661 f_out.redirect_nia <= a_in(63 downto 2) & "00"; -- srr0
662 -- Can't use msr_copy here because the partial function MSR
663 -- bits should be left unchanged, not zeroed.
664 ctrl_tmp.msr(63 downto 31) <= b_in(63 downto 31);
665 ctrl_tmp.msr(26 downto 22) <= b_in(26 downto 22);
666 ctrl_tmp.msr(15 downto 0) <= b_in(15 downto 0);
667 if b_in(MSR_PR) = '1' then
668 ctrl_tmp.msr(MSR_EE) <= '1';
669 ctrl_tmp.msr(MSR_IR) <= '1';
670 ctrl_tmp.msr(MSR_DR) <= '1';
671 end if;
672
673 when OP_CMPB =>
674 result := ppc_cmpb(c_in, b_in);
675 result_en := '1';
676 when OP_CNTZ =>
677 v.e.valid := '0';
678 v.cntz_in_progress := '1';
679 stall_out <= '1';
680 when OP_EXTS =>
681 -- note data_len is a 1-hot encoding
682 negative := (e_in.data_len(0) and c_in(7)) or
683 (e_in.data_len(1) and c_in(15)) or
684 (e_in.data_len(2) and c_in(31));
685 result := (others => negative);
686 if e_in.data_len(2) = '1' then
687 result(31 downto 16) := c_in(31 downto 16);
688 end if;
689 if e_in.data_len(2) = '1' or e_in.data_len(1) = '1' then
690 result(15 downto 8) := c_in(15 downto 8);
691 end if;
692 result(7 downto 0) := c_in(7 downto 0);
693 result_en := '1';
694 when OP_ISEL =>
695 crbit := to_integer(unsigned(insn_bc(e_in.insn)));
696 if e_in.cr(31-crbit) = '1' then
697 result := a_in;
698 else
699 result := b_in;
700 end if;
701 result_en := '1';
702 when OP_CROP =>
703 cr_op := insn_cr(e_in.insn);
704 report "CR OP " & to_hstring(cr_op);
705 if cr_op(0) = '0' then -- MCRF
706 bf := insn_bf(e_in.insn);
707 bfa := insn_bfa(e_in.insn);
708 v.e.write_cr_enable := '1';
709 crnum := to_integer(unsigned(bf));
710 scrnum := to_integer(unsigned(bfa));
711 v.e.write_cr_mask := num_to_fxm(crnum);
712 for i in 0 to 7 loop
713 lo := (7-i)*4;
714 hi := lo + 3;
715 if i = scrnum then
716 newcrf := e_in.cr(hi downto lo);
717 end if;
718 end loop;
719 for i in 0 to 7 loop
720 lo := i*4;
721 hi := lo + 3;
722 v.e.write_cr_data(hi downto lo) := newcrf;
723 end loop;
724 else
725 v.e.write_cr_enable := '1';
726 bt := insn_bt(e_in.insn);
727 ba := insn_ba(e_in.insn);
728 bb := insn_bb(e_in.insn);
729 btnum := 31 - to_integer(unsigned(bt));
730 banum := 31 - to_integer(unsigned(ba));
731 bbnum := 31 - to_integer(unsigned(bb));
732 -- Bits 5-8 of cr_op give the truth table of the requested
733 -- logical operation
734 cr_operands := e_in.cr(banum) & e_in.cr(bbnum);
735 crresult := cr_op(5 + to_integer(unsigned(cr_operands)));
736 v.e.write_cr_mask := num_to_fxm((31-btnum) / 4);
737 for i in 0 to 31 loop
738 if i = btnum then
739 v.e.write_cr_data(i) := crresult;
740 else
741 v.e.write_cr_data(i) := e_in.cr(i);
742 end if;
743 end loop;
744 end if;
745 when OP_MFMSR =>
746 result := ctrl.msr;
747 result_en := '1';
748 when OP_MFSPR =>
749 report "MFSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
750 "=" & to_hstring(a_in);
751 result_en := '1';
752 if is_fast_spr(e_in.read_reg1) then
753 result := a_in;
754 if decode_spr_num(e_in.insn) = SPR_XER then
755 -- bits 0:31 and 35:43 are treated as reserved and return 0s when read using mfxer
756 result(63 downto 32) := (others => '0');
757 result(63-32) := v.e.xerc.so;
758 result(63-33) := v.e.xerc.ov;
759 result(63-34) := v.e.xerc.ca;
760 result(63-35 downto 63-43) := "000000000";
761 result(63-44) := v.e.xerc.ov32;
762 result(63-45) := v.e.xerc.ca32;
763 end if;
764 else
765 case decode_spr_num(e_in.insn) is
766 when SPR_TB =>
767 result := ctrl.tb;
768 when SPR_DEC =>
769 result := ctrl.dec;
770 when others =>
771 -- mfspr from unimplemented SPRs should be a nop in
772 -- supervisor mode and a program interrupt for user mode
773 result := c_in;
774 if ctrl.msr(MSR_PR) = '1' then
775 illegal := '1';
776 end if;
777 end case;
778 end if;
779 when OP_MFCR =>
780 if e_in.insn(20) = '0' then
781 -- mfcr
782 result := x"00000000" & e_in.cr;
783 else
784 -- mfocrf
785 crnum := fxm_to_num(insn_fxm(e_in.insn));
786 result := (others => '0');
787 for i in 0 to 7 loop
788 lo := (7-i)*4;
789 hi := lo + 3;
790 if crnum = i then
791 result(hi downto lo) := e_in.cr(hi downto lo);
792 end if;
793 end loop;
794 end if;
795 result_en := '1';
796 when OP_MTCRF =>
797 v.e.write_cr_enable := '1';
798 if e_in.insn(20) = '0' then
799 -- mtcrf
800 v.e.write_cr_mask := insn_fxm(e_in.insn);
801 else
802 -- mtocrf: We require one hot priority encoding here
803 crnum := fxm_to_num(insn_fxm(e_in.insn));
804 v.e.write_cr_mask := num_to_fxm(crnum);
805 end if;
806 v.e.write_cr_data := c_in(31 downto 0);
807 when OP_MTMSRD =>
808 if e_in.insn(16) = '1' then
809 -- just update EE and RI
810 ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
811 ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
812 else
813 -- Architecture says to leave out bits 3 (HV), 51 (ME)
814 -- and 63 (LE) (IBM bit numbering)
815 ctrl_tmp.msr(63 downto 61) <= c_in(63 downto 61);
816 ctrl_tmp.msr(59 downto 13) <= c_in(59 downto 13);
817 ctrl_tmp.msr(11 downto 1) <= c_in(11 downto 1);
818 if c_in(MSR_PR) = '1' then
819 ctrl_tmp.msr(MSR_EE) <= '1';
820 ctrl_tmp.msr(MSR_IR) <= '1';
821 ctrl_tmp.msr(MSR_DR) <= '1';
822 end if;
823 end if;
824 when OP_MTSPR =>
825 report "MTSPR to SPR " & integer'image(decode_spr_num(e_in.insn)) &
826 "=" & to_hstring(c_in);
827 if is_fast_spr(e_in.write_reg) then
828 result := c_in;
829 result_en := '1';
830 if decode_spr_num(e_in.insn) = SPR_XER then
831 v.e.xerc.so := c_in(63-32);
832 v.e.xerc.ov := c_in(63-33);
833 v.e.xerc.ca := c_in(63-34);
834 v.e.xerc.ov32 := c_in(63-44);
835 v.e.xerc.ca32 := c_in(63-45);
836 v.e.write_xerc_enable := '1';
837 end if;
838 else
839 -- slow spr
840 case decode_spr_num(e_in.insn) is
841 when SPR_DEC =>
842 ctrl_tmp.dec <= c_in;
843 when others =>
844 -- mtspr to unimplemented SPRs should be a nop in
845 -- supervisor mode and a program interrupt for user mode
846 if ctrl.msr(MSR_PR) = '1' then
847 illegal := '1';
848 end if;
849 end case;
850 end if;
851 when OP_POPCNT =>
852 result := popcnt_result;
853 result_en := '1';
854 when OP_PRTY =>
855 result := parity_result;
856 result_en := '1';
857 when OP_RLC | OP_RLCL | OP_RLCR | OP_SHL | OP_SHR | OP_EXTSWSLI =>
858 result := rotator_result;
859 if e_in.output_carry = '1' then
860 set_carry(v.e, rotator_carry, rotator_carry);
861 end if;
862 result_en := '1';
863
864 when OP_ISYNC =>
865 f_out.redirect <= '1';
866 f_out.redirect_nia <= next_nia;
867
868 when OP_ICBI =>
869 icache_inval <= '1';
870
871 when OP_MUL_L64 | OP_MUL_H64 | OP_MUL_H32 =>
872 v.e.valid := '0';
873 v.mul_in_progress := '1';
874 stall_out <= '1';
875 x_to_multiply.valid <= '1';
876
877 when OP_DIV | OP_DIVE | OP_MOD =>
878 v.e.valid := '0';
879 v.div_in_progress := '1';
880 stall_out <= '1';
881 x_to_divider.valid <= '1';
882
883 when others =>
884 terminate_out <= '1';
885 report "illegal";
886 end case;
887
888 v.e.rc := e_in.rc and e_in.valid;
889
890 -- Update LR on the next cycle after a branch link
891 --
892 -- WARNING: The LR update isn't tracked by our hazard tracker. This
893 -- will work (well I hope) because it only happens on branches
894 -- which will flush all decoded instructions. By the time
895 -- fetch catches up, we'll have the new LR. This will
896 -- *not* work properly however if we have a branch predictor,
897 -- in which case the solution would probably be to keep a
898 -- local cache of the updated LR in execute1 (flushed on
899 -- exceptions) that is used instead of the value from
900 -- decode when its content is valid.
901 if e_in.lr = '1' then
902 v.lr_update := '1';
903 v.next_lr := next_nia;
904 v.e.valid := '0';
905 report "Delayed LR update to " & to_hstring(next_nia);
906 stall_out <= '1';
907 end if;
908
909 elsif e_in.valid = '1' then
910 -- instruction for other units, i.e. LDST
911 v.ldst_nia := e_in.nia;
912 v.e.valid := '0';
913 if e_in.unit = LDST then
914 lv.valid := '1';
915 end if;
916
917 elsif r.lr_update = '1' then
918 result_en := '1';
919 result := r.next_lr;
920 v.e.write_reg := fast_spr_num(SPR_LR);
921 v.e.valid := '1';
922 elsif r.cntz_in_progress = '1' then
923 -- cnt[lt]z always takes two cycles
924 result := countzero_result;
925 result_en := '1';
926 v.e.write_reg := gpr_to_gspr(v.slow_op_dest);
927 v.e.rc := v.slow_op_rc;
928 v.e.xerc := v.slow_op_xerc;
929 v.e.valid := '1';
930 elsif r.mul_in_progress = '1' or r.div_in_progress = '1' then
931 if (r.mul_in_progress = '1' and multiply_to_x.valid = '1') or
932 (r.div_in_progress = '1' and divider_to_x.valid = '1') then
933 if r.mul_in_progress = '1' then
934 result := multiply_to_x.write_reg_data;
935 overflow := multiply_to_x.overflow;
936 else
937 result := divider_to_x.write_reg_data;
938 overflow := divider_to_x.overflow;
939 end if;
940 result_en := '1';
941 v.e.write_reg := gpr_to_gspr(v.slow_op_dest);
942 v.e.rc := v.slow_op_rc;
943 v.e.xerc := v.slow_op_xerc;
944 v.e.write_xerc_enable := v.slow_op_oe;
945 -- We must test oe because the RC update code in writeback
946 -- will use the xerc value to set CR0:SO so we must not clobber
947 -- xerc if OE wasn't set.
948 if v.slow_op_oe = '1' then
949 v.e.xerc.ov := overflow;
950 v.e.xerc.ov32 := overflow;
951 v.e.xerc.so := v.slow_op_xerc.so or overflow;
952 end if;
953 v.e.valid := '1';
954 else
955 stall_out <= '1';
956 v.mul_in_progress := r.mul_in_progress;
957 v.div_in_progress := r.div_in_progress;
958 end if;
959 end if;
960
961 if illegal = '1' then
962 exception := '1';
963 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#700#, 64));
964 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
965 -- Since we aren't doing Hypervisor emulation assist (0xe40) we
966 -- set bit 44 to indicate we have an illegal
967 ctrl_tmp.srr1(63 - 44) <= '1';
968 report "illegal";
969 end if;
970 if exception = '1' then
971 v.e.exc_write_enable := '1';
972 if exception_nextpc = '1' then
973 v.e.exc_write_data := next_nia;
974 end if;
975 ctrl_tmp.irq_state <= WRITE_SRR1;
976 v.e.valid := '1';
977 end if;
978
979 v.e.write_data := result;
980 v.e.write_enable := result_en;
981
982 -- generate DSI or DSegI for load/store exceptions
983 -- or ISI or ISegI for instruction fetch exceptions
984 if l_in.exception = '1' then
985 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
986 if l_in.instr_fault = '0' then
987 if l_in.segment_fault = '0' then
988 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#300#, 64));
989 else
990 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#380#, 64));
991 end if;
992 else
993 if l_in.segment_fault = '0' then
994 ctrl_tmp.srr1(63 - 33) <= l_in.invalid;
995 ctrl_tmp.srr1(63 - 35) <= l_in.perm_error; -- noexec fault
996 ctrl_tmp.srr1(63 - 44) <= l_in.badtree;
997 ctrl_tmp.srr1(63 - 45) <= l_in.rc_error;
998 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#400#, 64));
999 else
1000 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#480#, 64));
1001 end if;
1002 end if;
1003 v.e.exc_write_enable := '1';
1004 v.e.exc_write_reg := fast_spr_num(SPR_SRR0);
1005 v.e.exc_write_data := r.ldst_nia;
1006 report "ldst exception writing srr0=" & to_hstring(r.ldst_nia);
1007 ctrl_tmp.irq_state <= WRITE_SRR1;
1008 v.e.valid := '1'; -- complete the original load or store
1009 end if;
1010
1011 -- Outputs to loadstore1 (async)
1012 lv.op := e_in.insn_type;
1013 lv.nia := e_in.nia;
1014 lv.addr1 := a_in;
1015 lv.addr2 := b_in;
1016 lv.data := c_in;
1017 lv.write_reg := gspr_to_gpr(e_in.write_reg);
1018 lv.length := e_in.data_len;
1019 lv.byte_reverse := e_in.byte_reverse;
1020 lv.sign_extend := e_in.sign_extend;
1021 lv.update := e_in.update;
1022 lv.update_reg := gspr_to_gpr(e_in.read_reg1);
1023 lv.xerc := v.e.xerc;
1024 lv.reserve := e_in.reserve;
1025 lv.rc := e_in.rc;
1026 lv.insn := e_in.insn;
1027 -- decode l*cix and st*cix instructions here
1028 if e_in.insn(31 downto 26) = "011111" and e_in.insn(10 downto 9) = "11" and
1029 e_in.insn(5 downto 1) = "10101" then
1030 lv.ci := '1';
1031 end if;
1032 lv.virt_mode := ctrl.msr(MSR_DR);
1033 lv.priv_mode := not ctrl.msr(MSR_PR);
1034
1035 -- Update registers
1036 rin <= v;
1037
1038 -- update outputs
1039 --f_out <= r.f;
1040 l_out <= lv;
1041 e_out <= r.e;
1042 flush_out <= f_out.redirect;
1043 end process;
1044 end architecture behaviour;