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