2 * Copyright (c) 2012-2014 Bastian Koppelmann C-Lab/University Paderborn
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 #include "qemu/osdep.h"
19 #include "qemu/host-utils.h"
20 #include "exec/helper-proto.h"
21 #include "exec/exec-all.h"
22 #include "exec/cpu_ldst.h"
23 #include <zlib.h> /* for crc32 */
26 /* Exception helpers */
28 static void QEMU_NORETURN
29 raise_exception_sync_internal(CPUTriCoreState
*env
, uint32_t class, int tin
,
30 uintptr_t pc
, uint32_t fcd_pc
)
32 CPUState
*cs
= CPU(tricore_env_get_cpu(env
));
33 /* in case we come from a helper-call we need to restore the PC */
34 cpu_restore_state(cs
, pc
, true);
36 /* Tin is loaded into d[15] */
39 if (class == TRAPC_CTX_MNG
&& tin
== TIN3_FCU
) {
40 /* upper context cannot be saved, if the context list is empty */
45 /* The return address in a[11] is updated */
46 if (class == TRAPC_CTX_MNG
&& tin
== TIN3_FCD
) {
47 env
->SYSCON
|= MASK_SYSCON_FCD_SF
;
48 /* when we run out of CSAs after saving a context a FCD trap is taken
49 and the return address is the start of the trap handler which used
51 env
->gpr_a
[11] = fcd_pc
;
52 } else if (class == TRAPC_SYSCALL
) {
53 env
->gpr_a
[11] = env
->PC
+ 4;
55 env
->gpr_a
[11] = env
->PC
;
57 /* The stack pointer in A[10] is set to the Interrupt Stack Pointer (ISP)
58 when the processor was not previously using the interrupt stack
59 (in case of PSW.IS = 0). The stack pointer bit is set for using the
60 interrupt stack: PSW.IS = 1. */
61 if ((env
->PSW
& MASK_PSW_IS
) == 0) {
62 env
->gpr_a
[10] = env
->ISP
;
64 env
->PSW
|= MASK_PSW_IS
;
65 /* The I/O mode is set to Supervisor mode, which means all permissions
66 are enabled: PSW.IO = 10 B .*/
67 env
->PSW
|= (2 << 10);
69 /*The current Protection Register Set is set to 0: PSW.PRS = 00 B .*/
70 env
->PSW
&= ~MASK_PSW_PRS
;
72 /* The Call Depth Counter (CDC) is cleared, and the call depth limit is
73 set for 64: PSW.CDC = 0000000 B .*/
74 env
->PSW
&= ~MASK_PSW_CDC
;
76 /* Call Depth Counter is enabled, PSW.CDE = 1. */
77 env
->PSW
|= MASK_PSW_CDE
;
79 /* Write permission to global registers A[0], A[1], A[8], A[9] is
80 disabled: PSW.GW = 0. */
81 env
->PSW
&= ~MASK_PSW_GW
;
83 /*The interrupt system is globally disabled: ICR.IE = 0. The ‘old’
84 ICR.IE and ICR.CCPN are saved */
86 /* PCXI.PIE = ICR.IE */
87 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE_1_3
) +
88 ((env
->ICR
& MASK_ICR_IE_1_3
) << 15));
89 /* PCXI.PCPN = ICR.CCPN */
90 env
->PCXI
= (env
->PCXI
& 0xffffff) +
91 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
92 /* Update PC using the trap vector table */
93 env
->PC
= env
->BTV
| (class << 5);
98 void helper_raise_exception_sync(CPUTriCoreState
*env
, uint32_t class,
101 raise_exception_sync_internal(env
, class, tin
, 0, 0);
104 static void raise_exception_sync_helper(CPUTriCoreState
*env
, uint32_t class,
105 uint32_t tin
, uintptr_t pc
)
107 raise_exception_sync_internal(env
, class, tin
, pc
, 0);
110 /* Addressing mode helper */
112 static uint16_t reverse16(uint16_t val
)
114 uint8_t high
= (uint8_t)(val
>> 8);
115 uint8_t low
= (uint8_t)(val
& 0xff);
119 rl
= (uint16_t)((high
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023);
120 rh
= (uint16_t)((low
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023);
122 return (rh
<< 8) | rl
;
125 uint32_t helper_br_update(uint32_t reg
)
127 uint32_t index
= reg
& 0xffff;
128 uint32_t incr
= reg
>> 16;
129 uint32_t new_index
= reverse16(reverse16(index
) + reverse16(incr
));
130 return reg
- index
+ new_index
;
133 uint32_t helper_circ_update(uint32_t reg
, uint32_t off
)
135 uint32_t index
= reg
& 0xffff;
136 uint32_t length
= reg
>> 16;
137 int32_t new_index
= index
+ off
;
143 return reg
- index
+ new_index
;
146 static uint32_t ssov32(CPUTriCoreState
*env
, int64_t arg
)
149 int64_t max_pos
= INT32_MAX
;
150 int64_t max_neg
= INT32_MIN
;
152 env
->PSW_USB_V
= (1 << 31);
153 env
->PSW_USB_SV
= (1 << 31);
154 ret
= (target_ulong
)max_pos
;
157 env
->PSW_USB_V
= (1 << 31);
158 env
->PSW_USB_SV
= (1 << 31);
159 ret
= (target_ulong
)max_neg
;
162 ret
= (target_ulong
)arg
;
165 env
->PSW_USB_AV
= arg
^ arg
* 2u;
166 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
170 static uint32_t suov32_pos(CPUTriCoreState
*env
, uint64_t arg
)
173 uint64_t max_pos
= UINT32_MAX
;
175 env
->PSW_USB_V
= (1 << 31);
176 env
->PSW_USB_SV
= (1 << 31);
177 ret
= (target_ulong
)max_pos
;
180 ret
= (target_ulong
)arg
;
182 env
->PSW_USB_AV
= arg
^ arg
* 2u;
183 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
187 static uint32_t suov32_neg(CPUTriCoreState
*env
, int64_t arg
)
192 env
->PSW_USB_V
= (1 << 31);
193 env
->PSW_USB_SV
= (1 << 31);
197 ret
= (target_ulong
)arg
;
199 env
->PSW_USB_AV
= arg
^ arg
* 2u;
200 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
204 static uint32_t ssov16(CPUTriCoreState
*env
, int32_t hw0
, int32_t hw1
)
206 int32_t max_pos
= INT16_MAX
;
207 int32_t max_neg
= INT16_MIN
;
211 av0
= hw0
^ hw0
* 2u;
213 env
->PSW_USB_V
= (1 << 31);
215 } else if (hw0
< max_neg
) {
216 env
->PSW_USB_V
= (1 << 31);
220 av1
= hw1
^ hw1
* 2u;
222 env
->PSW_USB_V
= (1 << 31);
224 } else if (hw1
< max_neg
) {
225 env
->PSW_USB_V
= (1 << 31);
229 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
230 env
->PSW_USB_AV
= (av0
| av1
) << 16;
231 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
232 return (hw0
& 0xffff) | (hw1
<< 16);
235 static uint32_t suov16(CPUTriCoreState
*env
, int32_t hw0
, int32_t hw1
)
237 int32_t max_pos
= UINT16_MAX
;
241 av0
= hw0
^ hw0
* 2u;
243 env
->PSW_USB_V
= (1 << 31);
245 } else if (hw0
< 0) {
246 env
->PSW_USB_V
= (1 << 31);
250 av1
= hw1
^ hw1
* 2u;
252 env
->PSW_USB_V
= (1 << 31);
254 } else if (hw1
< 0) {
255 env
->PSW_USB_V
= (1 << 31);
259 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
260 env
->PSW_USB_AV
= (av0
| av1
) << 16;
261 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
262 return (hw0
& 0xffff) | (hw1
<< 16);
265 target_ulong
helper_add_ssov(CPUTriCoreState
*env
, target_ulong r1
,
268 int64_t t1
= sextract64(r1
, 0, 32);
269 int64_t t2
= sextract64(r2
, 0, 32);
270 int64_t result
= t1
+ t2
;
271 return ssov32(env
, result
);
274 uint64_t helper_add64_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
280 ovf
= (result
^ r1
) & ~(r1
^ r2
);
281 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
282 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
284 env
->PSW_USB_V
= (1 << 31);
285 env
->PSW_USB_SV
= (1 << 31);
286 /* ext_ret > MAX_INT */
287 if ((int64_t)r1
>= 0) {
289 /* ext_ret < MIN_INT */
299 target_ulong
helper_add_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
302 int32_t ret_hw0
, ret_hw1
;
304 ret_hw0
= sextract32(r1
, 0, 16) + sextract32(r2
, 0, 16);
305 ret_hw1
= sextract32(r1
, 16, 16) + sextract32(r2
, 16, 16);
306 return ssov16(env
, ret_hw0
, ret_hw1
);
309 uint32_t helper_addr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
312 int64_t mul_res0
= sextract64(r1
, 0, 32);
313 int64_t mul_res1
= sextract64(r1
, 32, 32);
314 int64_t r2_low
= sextract64(r2_l
, 0, 32);
315 int64_t r2_high
= sextract64(r2_h
, 0, 32);
316 int64_t result0
, result1
;
322 result0
= r2_low
+ mul_res0
+ 0x8000;
323 result1
= r2_high
+ mul_res1
+ 0x8000;
326 avf0
= result0
^ avf0
;
328 avf1
= result1
^ avf1
;
330 if (result0
> INT32_MAX
) {
333 } else if (result0
< INT32_MIN
) {
338 if (result1
> INT32_MAX
) {
341 } else if (result1
< INT32_MIN
) {
346 env
->PSW_USB_V
= ovf0
| ovf1
;
347 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
349 env
->PSW_USB_AV
= avf0
| avf1
;
350 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
352 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
355 uint32_t helper_addsur_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
358 int64_t mul_res0
= sextract64(r1
, 0, 32);
359 int64_t mul_res1
= sextract64(r1
, 32, 32);
360 int64_t r2_low
= sextract64(r2_l
, 0, 32);
361 int64_t r2_high
= sextract64(r2_h
, 0, 32);
362 int64_t result0
, result1
;
368 result0
= r2_low
- mul_res0
+ 0x8000;
369 result1
= r2_high
+ mul_res1
+ 0x8000;
372 avf0
= result0
^ avf0
;
374 avf1
= result1
^ avf1
;
376 if (result0
> INT32_MAX
) {
379 } else if (result0
< INT32_MIN
) {
384 if (result1
> INT32_MAX
) {
387 } else if (result1
< INT32_MIN
) {
392 env
->PSW_USB_V
= ovf0
| ovf1
;
393 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
395 env
->PSW_USB_AV
= avf0
| avf1
;
396 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
398 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
402 target_ulong
helper_add_suov(CPUTriCoreState
*env
, target_ulong r1
,
405 int64_t t1
= extract64(r1
, 0, 32);
406 int64_t t2
= extract64(r2
, 0, 32);
407 int64_t result
= t1
+ t2
;
408 return suov32_pos(env
, result
);
411 target_ulong
helper_add_h_suov(CPUTriCoreState
*env
, target_ulong r1
,
414 int32_t ret_hw0
, ret_hw1
;
416 ret_hw0
= extract32(r1
, 0, 16) + extract32(r2
, 0, 16);
417 ret_hw1
= extract32(r1
, 16, 16) + extract32(r2
, 16, 16);
418 return suov16(env
, ret_hw0
, ret_hw1
);
421 target_ulong
helper_sub_ssov(CPUTriCoreState
*env
, target_ulong r1
,
424 int64_t t1
= sextract64(r1
, 0, 32);
425 int64_t t2
= sextract64(r2
, 0, 32);
426 int64_t result
= t1
- t2
;
427 return ssov32(env
, result
);
430 uint64_t helper_sub64_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
436 ovf
= (result
^ r1
) & (r1
^ r2
);
437 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
438 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
440 env
->PSW_USB_V
= (1 << 31);
441 env
->PSW_USB_SV
= (1 << 31);
442 /* ext_ret > MAX_INT */
443 if ((int64_t)r1
>= 0) {
445 /* ext_ret < MIN_INT */
455 target_ulong
helper_sub_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
458 int32_t ret_hw0
, ret_hw1
;
460 ret_hw0
= sextract32(r1
, 0, 16) - sextract32(r2
, 0, 16);
461 ret_hw1
= sextract32(r1
, 16, 16) - sextract32(r2
, 16, 16);
462 return ssov16(env
, ret_hw0
, ret_hw1
);
465 uint32_t helper_subr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
468 int64_t mul_res0
= sextract64(r1
, 0, 32);
469 int64_t mul_res1
= sextract64(r1
, 32, 32);
470 int64_t r2_low
= sextract64(r2_l
, 0, 32);
471 int64_t r2_high
= sextract64(r2_h
, 0, 32);
472 int64_t result0
, result1
;
478 result0
= r2_low
- mul_res0
+ 0x8000;
479 result1
= r2_high
- mul_res1
+ 0x8000;
482 avf0
= result0
^ avf0
;
484 avf1
= result1
^ avf1
;
486 if (result0
> INT32_MAX
) {
489 } else if (result0
< INT32_MIN
) {
494 if (result1
> INT32_MAX
) {
497 } else if (result1
< INT32_MIN
) {
502 env
->PSW_USB_V
= ovf0
| ovf1
;
503 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
505 env
->PSW_USB_AV
= avf0
| avf1
;
506 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
508 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
511 uint32_t helper_subadr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
514 int64_t mul_res0
= sextract64(r1
, 0, 32);
515 int64_t mul_res1
= sextract64(r1
, 32, 32);
516 int64_t r2_low
= sextract64(r2_l
, 0, 32);
517 int64_t r2_high
= sextract64(r2_h
, 0, 32);
518 int64_t result0
, result1
;
524 result0
= r2_low
+ mul_res0
+ 0x8000;
525 result1
= r2_high
- mul_res1
+ 0x8000;
528 avf0
= result0
^ avf0
;
530 avf1
= result1
^ avf1
;
532 if (result0
> INT32_MAX
) {
535 } else if (result0
< INT32_MIN
) {
540 if (result1
> INT32_MAX
) {
543 } else if (result1
< INT32_MIN
) {
548 env
->PSW_USB_V
= ovf0
| ovf1
;
549 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
551 env
->PSW_USB_AV
= avf0
| avf1
;
552 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
554 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
557 target_ulong
helper_sub_suov(CPUTriCoreState
*env
, target_ulong r1
,
560 int64_t t1
= extract64(r1
, 0, 32);
561 int64_t t2
= extract64(r2
, 0, 32);
562 int64_t result
= t1
- t2
;
563 return suov32_neg(env
, result
);
566 target_ulong
helper_sub_h_suov(CPUTriCoreState
*env
, target_ulong r1
,
569 int32_t ret_hw0
, ret_hw1
;
571 ret_hw0
= extract32(r1
, 0, 16) - extract32(r2
, 0, 16);
572 ret_hw1
= extract32(r1
, 16, 16) - extract32(r2
, 16, 16);
573 return suov16(env
, ret_hw0
, ret_hw1
);
576 target_ulong
helper_mul_ssov(CPUTriCoreState
*env
, target_ulong r1
,
579 int64_t t1
= sextract64(r1
, 0, 32);
580 int64_t t2
= sextract64(r2
, 0, 32);
581 int64_t result
= t1
* t2
;
582 return ssov32(env
, result
);
585 target_ulong
helper_mul_suov(CPUTriCoreState
*env
, target_ulong r1
,
588 int64_t t1
= extract64(r1
, 0, 32);
589 int64_t t2
= extract64(r2
, 0, 32);
590 int64_t result
= t1
* t2
;
592 return suov32_pos(env
, result
);
595 target_ulong
helper_sha_ssov(CPUTriCoreState
*env
, target_ulong r1
,
598 int64_t t1
= sextract64(r1
, 0, 32);
599 int32_t t2
= sextract64(r2
, 0, 6);
608 return ssov32(env
, result
);
611 uint32_t helper_abs_ssov(CPUTriCoreState
*env
, target_ulong r1
)
614 result
= ((int32_t)r1
>= 0) ? r1
: (0 - r1
);
615 return ssov32(env
, result
);
618 uint32_t helper_abs_h_ssov(CPUTriCoreState
*env
, target_ulong r1
)
620 int32_t ret_h0
, ret_h1
;
622 ret_h0
= sextract32(r1
, 0, 16);
623 ret_h0
= (ret_h0
>= 0) ? ret_h0
: (0 - ret_h0
);
625 ret_h1
= sextract32(r1
, 16, 16);
626 ret_h1
= (ret_h1
>= 0) ? ret_h1
: (0 - ret_h1
);
628 return ssov16(env
, ret_h0
, ret_h1
);
631 target_ulong
helper_absdif_ssov(CPUTriCoreState
*env
, target_ulong r1
,
634 int64_t t1
= sextract64(r1
, 0, 32);
635 int64_t t2
= sextract64(r2
, 0, 32);
643 return ssov32(env
, result
);
646 uint32_t helper_absdif_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
650 int32_t ret_h0
, ret_h1
;
652 t1
= sextract32(r1
, 0, 16);
653 t2
= sextract32(r2
, 0, 16);
660 t1
= sextract32(r1
, 16, 16);
661 t2
= sextract32(r2
, 16, 16);
668 return ssov16(env
, ret_h0
, ret_h1
);
671 target_ulong
helper_madd32_ssov(CPUTriCoreState
*env
, target_ulong r1
,
672 target_ulong r2
, target_ulong r3
)
674 int64_t t1
= sextract64(r1
, 0, 32);
675 int64_t t2
= sextract64(r2
, 0, 32);
676 int64_t t3
= sextract64(r3
, 0, 32);
679 result
= t2
+ (t1
* t3
);
680 return ssov32(env
, result
);
683 target_ulong
helper_madd32_suov(CPUTriCoreState
*env
, target_ulong r1
,
684 target_ulong r2
, target_ulong r3
)
686 uint64_t t1
= extract64(r1
, 0, 32);
687 uint64_t t2
= extract64(r2
, 0, 32);
688 uint64_t t3
= extract64(r3
, 0, 32);
691 result
= t2
+ (t1
* t3
);
692 return suov32_pos(env
, result
);
695 uint64_t helper_madd64_ssov(CPUTriCoreState
*env
, target_ulong r1
,
696 uint64_t r2
, target_ulong r3
)
699 int64_t t1
= sextract64(r1
, 0, 32);
700 int64_t t3
= sextract64(r3
, 0, 32);
705 ovf
= (ret
^ mul
) & ~(mul
^ r2
);
708 env
->PSW_USB_AV
= t1
^ t1
* 2u;
709 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
711 if ((int64_t)ovf
< 0) {
712 env
->PSW_USB_V
= (1 << 31);
713 env
->PSW_USB_SV
= (1 << 31);
714 /* ext_ret > MAX_INT */
717 /* ext_ret < MIN_INT */
729 helper_madd32_q_add_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
735 env
->PSW_USB_AV
= (result
^ result
* 2u);
736 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
738 /* we do the saturation by hand, since we produce an overflow on the host
739 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
740 case, we flip the saturated value. */
741 if (r2
== 0x8000000000000000LL
) {
742 if (result
> 0x7fffffffLL
) {
743 env
->PSW_USB_V
= (1 << 31);
744 env
->PSW_USB_SV
= (1 << 31);
746 } else if (result
< -0x80000000LL
) {
747 env
->PSW_USB_V
= (1 << 31);
748 env
->PSW_USB_SV
= (1 << 31);
754 if (result
> 0x7fffffffLL
) {
755 env
->PSW_USB_V
= (1 << 31);
756 env
->PSW_USB_SV
= (1 << 31);
758 } else if (result
< -0x80000000LL
) {
759 env
->PSW_USB_V
= (1 << 31);
760 env
->PSW_USB_SV
= (1 << 31);
766 return (uint32_t)result
;
769 uint64_t helper_madd64_q_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2
,
770 uint32_t r3
, uint32_t n
)
772 int64_t t1
= (int64_t)r1
;
773 int64_t t2
= sextract64(r2
, 0, 32);
774 int64_t t3
= sextract64(r3
, 0, 32);
778 mul
= (t2
* t3
) << n
;
781 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
782 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
784 ovf
= (result
^ mul
) & ~(mul
^ t1
);
785 /* we do the saturation by hand, since we produce an overflow on the host
786 if the mul was (0x80000000 * 0x80000000) << 1). If this is the
787 case, we flip the saturated value. */
788 if ((r2
== 0x80000000) && (r3
== 0x80000000) && (n
== 1)) {
790 env
->PSW_USB_V
= (1 << 31);
791 env
->PSW_USB_SV
= (1 << 31);
792 /* ext_ret > MAX_INT */
795 /* ext_ret < MIN_INT */
804 env
->PSW_USB_V
= (1 << 31);
805 env
->PSW_USB_SV
= (1 << 31);
806 /* ext_ret > MAX_INT */
809 /* ext_ret < MIN_INT */
817 return (uint64_t)result
;
820 uint32_t helper_maddr_q_ssov(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
821 uint32_t r3
, uint32_t n
)
823 int64_t t1
= sextract64(r1
, 0, 32);
824 int64_t t2
= sextract64(r2
, 0, 32);
825 int64_t t3
= sextract64(r3
, 0, 32);
828 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
831 mul
= (t2
* t3
) << n
;
834 ret
= t1
+ mul
+ 0x8000;
836 env
->PSW_USB_AV
= ret
^ ret
* 2u;
837 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
839 if (ret
> 0x7fffffffll
) {
840 env
->PSW_USB_V
= (1 << 31);
841 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
843 } else if (ret
< -0x80000000ll
) {
844 env
->PSW_USB_V
= (1 << 31);
845 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
850 return ret
& 0xffff0000ll
;
853 uint64_t helper_madd64_suov(CPUTriCoreState
*env
, target_ulong r1
,
854 uint64_t r2
, target_ulong r3
)
857 uint64_t t1
= extract64(r1
, 0, 32);
858 uint64_t t3
= extract64(r3
, 0, 32);
864 env
->PSW_USB_AV
= t1
^ t1
* 2u;
865 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
868 env
->PSW_USB_V
= (1 << 31);
869 env
->PSW_USB_SV
= (1 << 31);
878 target_ulong
helper_msub32_ssov(CPUTriCoreState
*env
, target_ulong r1
,
879 target_ulong r2
, target_ulong r3
)
881 int64_t t1
= sextract64(r1
, 0, 32);
882 int64_t t2
= sextract64(r2
, 0, 32);
883 int64_t t3
= sextract64(r3
, 0, 32);
886 result
= t2
- (t1
* t3
);
887 return ssov32(env
, result
);
890 target_ulong
helper_msub32_suov(CPUTriCoreState
*env
, target_ulong r1
,
891 target_ulong r2
, target_ulong r3
)
893 uint64_t t1
= extract64(r1
, 0, 32);
894 uint64_t t2
= extract64(r2
, 0, 32);
895 uint64_t t3
= extract64(r3
, 0, 32);
902 env
->PSW_USB_AV
= result
^ result
* 2u;
903 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
904 /* we calculate ovf by hand here, because the multiplication can overflow on
905 the host, which would give false results if we compare to less than
908 env
->PSW_USB_V
= (1 << 31);
909 env
->PSW_USB_SV
= (1 << 31);
917 uint64_t helper_msub64_ssov(CPUTriCoreState
*env
, target_ulong r1
,
918 uint64_t r2
, target_ulong r3
)
921 int64_t t1
= sextract64(r1
, 0, 32);
922 int64_t t3
= sextract64(r3
, 0, 32);
927 ovf
= (ret
^ r2
) & (mul
^ r2
);
930 env
->PSW_USB_AV
= t1
^ t1
* 2u;
931 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
933 if ((int64_t)ovf
< 0) {
934 env
->PSW_USB_V
= (1 << 31);
935 env
->PSW_USB_SV
= (1 << 31);
936 /* ext_ret > MAX_INT */
939 /* ext_ret < MIN_INT */
949 uint64_t helper_msub64_suov(CPUTriCoreState
*env
, target_ulong r1
,
950 uint64_t r2
, target_ulong r3
)
953 uint64_t t1
= extract64(r1
, 0, 32);
954 uint64_t t3
= extract64(r3
, 0, 32);
960 env
->PSW_USB_AV
= t1
^ t1
* 2u;
961 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
964 env
->PSW_USB_V
= (1 << 31);
965 env
->PSW_USB_SV
= (1 << 31);
975 helper_msub32_q_sub_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
978 int64_t t1
= (int64_t)r1
;
979 int64_t t2
= (int64_t)r2
;
983 env
->PSW_USB_AV
= (result
^ result
* 2u);
984 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
986 /* we do the saturation by hand, since we produce an overflow on the host
987 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
988 case, we flip the saturated value. */
989 if (r2
== 0x8000000000000000LL
) {
990 if (result
> 0x7fffffffLL
) {
991 env
->PSW_USB_V
= (1 << 31);
992 env
->PSW_USB_SV
= (1 << 31);
994 } else if (result
< -0x80000000LL
) {
995 env
->PSW_USB_V
= (1 << 31);
996 env
->PSW_USB_SV
= (1 << 31);
1002 if (result
> 0x7fffffffLL
) {
1003 env
->PSW_USB_V
= (1 << 31);
1004 env
->PSW_USB_SV
= (1 << 31);
1006 } else if (result
< -0x80000000LL
) {
1007 env
->PSW_USB_V
= (1 << 31);
1008 env
->PSW_USB_SV
= (1 << 31);
1014 return (uint32_t)result
;
1017 uint64_t helper_msub64_q_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2
,
1018 uint32_t r3
, uint32_t n
)
1020 int64_t t1
= (int64_t)r1
;
1021 int64_t t2
= sextract64(r2
, 0, 32);
1022 int64_t t3
= sextract64(r3
, 0, 32);
1023 int64_t result
, mul
;
1026 mul
= (t2
* t3
) << n
;
1029 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
1030 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1032 ovf
= (result
^ t1
) & (t1
^ mul
);
1033 /* we do the saturation by hand, since we produce an overflow on the host
1034 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
1035 case, we flip the saturated value. */
1036 if (mul
== 0x8000000000000000LL
) {
1038 env
->PSW_USB_V
= (1 << 31);
1039 env
->PSW_USB_SV
= (1 << 31);
1040 /* ext_ret > MAX_INT */
1043 /* ext_ret < MIN_INT */
1052 env
->PSW_USB_V
= (1 << 31);
1053 env
->PSW_USB_SV
= (1 << 31);
1054 /* ext_ret > MAX_INT */
1057 /* ext_ret < MIN_INT */
1066 return (uint64_t)result
;
1069 uint32_t helper_msubr_q_ssov(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
1070 uint32_t r3
, uint32_t n
)
1072 int64_t t1
= sextract64(r1
, 0, 32);
1073 int64_t t2
= sextract64(r2
, 0, 32);
1074 int64_t t3
= sextract64(r3
, 0, 32);
1077 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
1080 mul
= (t2
* t3
) << n
;
1083 ret
= t1
- mul
+ 0x8000;
1085 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1086 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1088 if (ret
> 0x7fffffffll
) {
1089 env
->PSW_USB_V
= (1 << 31);
1090 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1092 } else if (ret
< -0x80000000ll
) {
1093 env
->PSW_USB_V
= (1 << 31);
1094 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1099 return ret
& 0xffff0000ll
;
1102 uint32_t helper_abs_b(CPUTriCoreState
*env
, target_ulong arg
)
1109 for (i
= 0; i
< 4; i
++) {
1110 b
= sextract32(arg
, i
* 8, 8);
1111 b
= (b
>= 0) ? b
: (0 - b
);
1112 ovf
|= (b
> 0x7F) || (b
< -0x80);
1114 ret
|= (b
& 0xff) << (i
* 8);
1117 env
->PSW_USB_V
= ovf
<< 31;
1118 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1119 env
->PSW_USB_AV
= avf
<< 24;
1120 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1125 uint32_t helper_abs_h(CPUTriCoreState
*env
, target_ulong arg
)
1132 for (i
= 0; i
< 2; i
++) {
1133 h
= sextract32(arg
, i
* 16, 16);
1134 h
= (h
>= 0) ? h
: (0 - h
);
1135 ovf
|= (h
> 0x7FFF) || (h
< -0x8000);
1137 ret
|= (h
& 0xffff) << (i
* 16);
1140 env
->PSW_USB_V
= ovf
<< 31;
1141 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1142 env
->PSW_USB_AV
= avf
<< 16;
1143 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1148 uint32_t helper_absdif_b(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1156 for (i
= 0; i
< 4; i
++) {
1157 extr_r2
= sextract32(r2
, i
* 8, 8);
1158 b
= sextract32(r1
, i
* 8, 8);
1159 b
= (b
> extr_r2
) ? (b
- extr_r2
) : (extr_r2
- b
);
1160 ovf
|= (b
> 0x7F) || (b
< -0x80);
1162 ret
|= (b
& 0xff) << (i
* 8);
1165 env
->PSW_USB_V
= ovf
<< 31;
1166 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1167 env
->PSW_USB_AV
= avf
<< 24;
1168 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1172 uint32_t helper_absdif_h(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1180 for (i
= 0; i
< 2; i
++) {
1181 extr_r2
= sextract32(r2
, i
* 16, 16);
1182 h
= sextract32(r1
, i
* 16, 16);
1183 h
= (h
> extr_r2
) ? (h
- extr_r2
) : (extr_r2
- h
);
1184 ovf
|= (h
> 0x7FFF) || (h
< -0x8000);
1186 ret
|= (h
& 0xffff) << (i
* 16);
1189 env
->PSW_USB_V
= ovf
<< 31;
1190 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1191 env
->PSW_USB_AV
= avf
<< 16;
1192 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1197 uint32_t helper_addr_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1200 int64_t mul_res0
= sextract64(r1
, 0, 32);
1201 int64_t mul_res1
= sextract64(r1
, 32, 32);
1202 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1203 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1204 int64_t result0
, result1
;
1205 uint32_t ovf0
, ovf1
;
1206 uint32_t avf0
, avf1
;
1210 result0
= r2_low
+ mul_res0
+ 0x8000;
1211 result1
= r2_high
+ mul_res1
+ 0x8000;
1213 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1217 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1221 env
->PSW_USB_V
= ovf0
| ovf1
;
1222 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1224 avf0
= result0
* 2u;
1225 avf0
= result0
^ avf0
;
1226 avf1
= result1
* 2u;
1227 avf1
= result1
^ avf1
;
1229 env
->PSW_USB_AV
= avf0
| avf1
;
1230 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1232 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1235 uint32_t helper_addsur_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1238 int64_t mul_res0
= sextract64(r1
, 0, 32);
1239 int64_t mul_res1
= sextract64(r1
, 32, 32);
1240 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1241 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1242 int64_t result0
, result1
;
1243 uint32_t ovf0
, ovf1
;
1244 uint32_t avf0
, avf1
;
1248 result0
= r2_low
- mul_res0
+ 0x8000;
1249 result1
= r2_high
+ mul_res1
+ 0x8000;
1251 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1255 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1259 env
->PSW_USB_V
= ovf0
| ovf1
;
1260 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1262 avf0
= result0
* 2u;
1263 avf0
= result0
^ avf0
;
1264 avf1
= result1
* 2u;
1265 avf1
= result1
^ avf1
;
1267 env
->PSW_USB_AV
= avf0
| avf1
;
1268 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1270 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1273 uint32_t helper_maddr_q(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
1274 uint32_t r3
, uint32_t n
)
1276 int64_t t1
= sextract64(r1
, 0, 32);
1277 int64_t t2
= sextract64(r2
, 0, 32);
1278 int64_t t3
= sextract64(r3
, 0, 32);
1281 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
1284 mul
= (t2
* t3
) << n
;
1287 ret
= t1
+ mul
+ 0x8000;
1289 if ((ret
> 0x7fffffffll
) || (ret
< -0x80000000ll
)) {
1290 env
->PSW_USB_V
= (1 << 31);
1291 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1295 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1296 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1298 return ret
& 0xffff0000ll
;
1301 uint32_t helper_add_b(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1304 int32_t extr_r1
, extr_r2
;
1309 for (i
= 0; i
< 4; i
++) {
1310 extr_r1
= sextract32(r1
, i
* 8, 8);
1311 extr_r2
= sextract32(r2
, i
* 8, 8);
1313 b
= extr_r1
+ extr_r2
;
1314 ovf
|= ((b
> 0x7f) || (b
< -0x80));
1316 ret
|= ((b
& 0xff) << (i
*8));
1319 env
->PSW_USB_V
= (ovf
<< 31);
1320 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1321 env
->PSW_USB_AV
= avf
<< 24;
1322 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1327 uint32_t helper_add_h(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1330 int32_t extr_r1
, extr_r2
;
1335 for (i
= 0; i
< 2; i
++) {
1336 extr_r1
= sextract32(r1
, i
* 16, 16);
1337 extr_r2
= sextract32(r2
, i
* 16, 16);
1338 h
= extr_r1
+ extr_r2
;
1339 ovf
|= ((h
> 0x7fff) || (h
< -0x8000));
1341 ret
|= (h
& 0xffff) << (i
* 16);
1344 env
->PSW_USB_V
= (ovf
<< 31);
1345 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1346 env
->PSW_USB_AV
= (avf
<< 16);
1347 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1352 uint32_t helper_subr_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1355 int64_t mul_res0
= sextract64(r1
, 0, 32);
1356 int64_t mul_res1
= sextract64(r1
, 32, 32);
1357 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1358 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1359 int64_t result0
, result1
;
1360 uint32_t ovf0
, ovf1
;
1361 uint32_t avf0
, avf1
;
1365 result0
= r2_low
- mul_res0
+ 0x8000;
1366 result1
= r2_high
- mul_res1
+ 0x8000;
1368 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1372 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1376 env
->PSW_USB_V
= ovf0
| ovf1
;
1377 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1379 avf0
= result0
* 2u;
1380 avf0
= result0
^ avf0
;
1381 avf1
= result1
* 2u;
1382 avf1
= result1
^ avf1
;
1384 env
->PSW_USB_AV
= avf0
| avf1
;
1385 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1387 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1390 uint32_t helper_subadr_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1393 int64_t mul_res0
= sextract64(r1
, 0, 32);
1394 int64_t mul_res1
= sextract64(r1
, 32, 32);
1395 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1396 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1397 int64_t result0
, result1
;
1398 uint32_t ovf0
, ovf1
;
1399 uint32_t avf0
, avf1
;
1403 result0
= r2_low
+ mul_res0
+ 0x8000;
1404 result1
= r2_high
- mul_res1
+ 0x8000;
1406 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1410 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1414 env
->PSW_USB_V
= ovf0
| ovf1
;
1415 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1417 avf0
= result0
* 2u;
1418 avf0
= result0
^ avf0
;
1419 avf1
= result1
* 2u;
1420 avf1
= result1
^ avf1
;
1422 env
->PSW_USB_AV
= avf0
| avf1
;
1423 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1425 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1428 uint32_t helper_msubr_q(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
1429 uint32_t r3
, uint32_t n
)
1431 int64_t t1
= sextract64(r1
, 0, 32);
1432 int64_t t2
= sextract64(r2
, 0, 32);
1433 int64_t t3
= sextract64(r3
, 0, 32);
1436 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
1439 mul
= (t2
* t3
) << n
;
1442 ret
= t1
- mul
+ 0x8000;
1444 if ((ret
> 0x7fffffffll
) || (ret
< -0x80000000ll
)) {
1445 env
->PSW_USB_V
= (1 << 31);
1446 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1450 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1451 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1453 return ret
& 0xffff0000ll
;
1456 uint32_t helper_sub_b(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1459 int32_t extr_r1
, extr_r2
;
1464 for (i
= 0; i
< 4; i
++) {
1465 extr_r1
= sextract32(r1
, i
* 8, 8);
1466 extr_r2
= sextract32(r2
, i
* 8, 8);
1468 b
= extr_r1
- extr_r2
;
1469 ovf
|= ((b
> 0x7f) || (b
< -0x80));
1471 ret
|= ((b
& 0xff) << (i
*8));
1474 env
->PSW_USB_V
= (ovf
<< 31);
1475 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1476 env
->PSW_USB_AV
= avf
<< 24;
1477 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1482 uint32_t helper_sub_h(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1485 int32_t extr_r1
, extr_r2
;
1490 for (i
= 0; i
< 2; i
++) {
1491 extr_r1
= sextract32(r1
, i
* 16, 16);
1492 extr_r2
= sextract32(r2
, i
* 16, 16);
1493 h
= extr_r1
- extr_r2
;
1494 ovf
|= ((h
> 0x7fff) || (h
< -0x8000));
1496 ret
|= (h
& 0xffff) << (i
* 16);
1499 env
->PSW_USB_V
= (ovf
<< 31);
1500 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1501 env
->PSW_USB_AV
= avf
<< 16;
1502 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1507 uint32_t helper_eq_b(target_ulong r1
, target_ulong r2
)
1514 for (i
= 0; i
< 4; i
++) {
1515 if ((r1
& msk
) == (r2
& msk
)) {
1524 uint32_t helper_eq_h(target_ulong r1
, target_ulong r2
)
1528 if ((r1
& 0xffff) == (r2
& 0xffff)) {
1532 if ((r1
& 0xffff0000) == (r2
& 0xffff0000)) {
1539 uint32_t helper_eqany_b(target_ulong r1
, target_ulong r2
)
1544 for (i
= 0; i
< 4; i
++) {
1545 ret
|= (sextract32(r1
, i
* 8, 8) == sextract32(r2
, i
* 8, 8));
1551 uint32_t helper_eqany_h(target_ulong r1
, target_ulong r2
)
1555 ret
= (sextract32(r1
, 0, 16) == sextract32(r2
, 0, 16));
1556 ret
|= (sextract32(r1
, 16, 16) == sextract32(r2
, 16, 16));
1561 uint32_t helper_lt_b(target_ulong r1
, target_ulong r2
)
1566 for (i
= 0; i
< 4; i
++) {
1567 if (sextract32(r1
, i
* 8, 8) < sextract32(r2
, i
* 8, 8)) {
1568 ret
|= (0xff << (i
* 8));
1575 uint32_t helper_lt_bu(target_ulong r1
, target_ulong r2
)
1580 for (i
= 0; i
< 4; i
++) {
1581 if (extract32(r1
, i
* 8, 8) < extract32(r2
, i
* 8, 8)) {
1582 ret
|= (0xff << (i
* 8));
1589 uint32_t helper_lt_h(target_ulong r1
, target_ulong r2
)
1593 if (sextract32(r1
, 0, 16) < sextract32(r2
, 0, 16)) {
1597 if (sextract32(r1
, 16, 16) < sextract32(r2
, 16, 16)) {
1604 uint32_t helper_lt_hu(target_ulong r1
, target_ulong r2
)
1608 if (extract32(r1
, 0, 16) < extract32(r2
, 0, 16)) {
1612 if (extract32(r1
, 16, 16) < extract32(r2
, 16, 16)) {
1619 #define EXTREMA_H_B(name, op) \
1620 uint32_t helper_##name ##_b(target_ulong r1, target_ulong r2) \
1622 int32_t i, extr_r1, extr_r2; \
1625 for (i = 0; i < 4; i++) { \
1626 extr_r1 = sextract32(r1, i * 8, 8); \
1627 extr_r2 = sextract32(r2, i * 8, 8); \
1628 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1629 ret |= (extr_r1 & 0xff) << (i * 8); \
1634 uint32_t helper_##name ##_bu(target_ulong r1, target_ulong r2)\
1637 uint32_t extr_r1, extr_r2; \
1640 for (i = 0; i < 4; i++) { \
1641 extr_r1 = extract32(r1, i * 8, 8); \
1642 extr_r2 = extract32(r2, i * 8, 8); \
1643 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1644 ret |= (extr_r1 & 0xff) << (i * 8); \
1649 uint32_t helper_##name ##_h(target_ulong r1, target_ulong r2) \
1651 int32_t extr_r1, extr_r2; \
1654 extr_r1 = sextract32(r1, 0, 16); \
1655 extr_r2 = sextract32(r2, 0, 16); \
1656 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1657 ret = ret & 0xffff; \
1659 extr_r1 = sextract32(r1, 16, 16); \
1660 extr_r2 = sextract32(r2, 16, 16); \
1661 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1662 ret |= extr_r1 << 16; \
1667 uint32_t helper_##name ##_hu(target_ulong r1, target_ulong r2)\
1669 uint32_t extr_r1, extr_r2; \
1672 extr_r1 = extract32(r1, 0, 16); \
1673 extr_r2 = extract32(r2, 0, 16); \
1674 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1675 ret = ret & 0xffff; \
1677 extr_r1 = extract32(r1, 16, 16); \
1678 extr_r2 = extract32(r2, 16, 16); \
1679 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1680 ret |= extr_r1 << (16); \
1685 uint64_t helper_ix##name(uint64_t r1, uint32_t r2) \
1687 int64_t r2l, r2h, r1hl; \
1690 ret = ((r1 + 2) & 0xffff); \
1691 r2l = sextract64(r2, 0, 16); \
1692 r2h = sextract64(r2, 16, 16); \
1693 r1hl = sextract64(r1, 32, 16); \
1695 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
1696 ret |= (r2l & 0xffff) << 32; \
1697 ret |= extract64(r1, 0, 16) << 16; \
1698 } else if ((r2h op r2l) && (r2h op r1hl)) { \
1699 ret |= extract64(r2, 16, 16) << 32; \
1700 ret |= extract64(r1 + 1, 0, 16) << 16; \
1702 ret |= r1 & 0xffffffff0000ull; \
1707 uint64_t helper_ix##name ##_u(uint64_t r1, uint32_t r2) \
1709 int64_t r2l, r2h, r1hl; \
1712 ret = ((r1 + 2) & 0xffff); \
1713 r2l = extract64(r2, 0, 16); \
1714 r2h = extract64(r2, 16, 16); \
1715 r1hl = extract64(r1, 32, 16); \
1717 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
1718 ret |= (r2l & 0xffff) << 32; \
1719 ret |= extract64(r1, 0, 16) << 16; \
1720 } else if ((r2h op r2l) && (r2h op r1hl)) { \
1721 ret |= extract64(r2, 16, 16) << 32; \
1722 ret |= extract64(r1 + 1, 0, 16) << 16; \
1724 ret |= r1 & 0xffffffff0000ull; \
1734 uint32_t helper_clo_h(target_ulong r1
)
1736 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1737 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1739 ret_hw0
= clo32(ret_hw0
<< 16);
1740 ret_hw1
= clo32(ret_hw1
<< 16);
1749 return ret_hw0
| (ret_hw1
<< 16);
1752 uint32_t helper_clz_h(target_ulong r1
)
1754 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1755 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1757 ret_hw0
= clz32(ret_hw0
<< 16);
1758 ret_hw1
= clz32(ret_hw1
<< 16);
1767 return ret_hw0
| (ret_hw1
<< 16);
1770 uint32_t helper_cls_h(target_ulong r1
)
1772 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1773 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1775 ret_hw0
= clrsb32(ret_hw0
<< 16);
1776 ret_hw1
= clrsb32(ret_hw1
<< 16);
1785 return ret_hw0
| (ret_hw1
<< 16);
1788 uint32_t helper_sh(target_ulong r1
, target_ulong r2
)
1790 int32_t shift_count
= sextract32(r2
, 0, 6);
1792 if (shift_count
== -32) {
1794 } else if (shift_count
< 0) {
1795 return r1
>> -shift_count
;
1797 return r1
<< shift_count
;
1801 uint32_t helper_sh_h(target_ulong r1
, target_ulong r2
)
1803 int32_t ret_hw0
, ret_hw1
;
1804 int32_t shift_count
;
1806 shift_count
= sextract32(r2
, 0, 5);
1808 if (shift_count
== -16) {
1810 } else if (shift_count
< 0) {
1811 ret_hw0
= extract32(r1
, 0, 16) >> -shift_count
;
1812 ret_hw1
= extract32(r1
, 16, 16) >> -shift_count
;
1813 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1815 ret_hw0
= extract32(r1
, 0, 16) << shift_count
;
1816 ret_hw1
= extract32(r1
, 16, 16) << shift_count
;
1817 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1821 uint32_t helper_sha(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1823 int32_t shift_count
;
1827 shift_count
= sextract32(r2
, 0, 6);
1828 t1
= sextract32(r1
, 0, 32);
1830 if (shift_count
== 0) {
1831 env
->PSW_USB_C
= env
->PSW_USB_V
= 0;
1833 } else if (shift_count
== -32) {
1834 env
->PSW_USB_C
= r1
;
1837 } else if (shift_count
> 0) {
1838 result
= t1
<< shift_count
;
1840 env
->PSW_USB_C
= ((result
& 0xffffffff00000000ULL
) != 0);
1842 env
->PSW_USB_V
= (((result
> 0x7fffffffLL
) ||
1843 (result
< -0x80000000LL
)) << 31);
1845 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1846 ret
= (uint32_t)result
;
1849 env
->PSW_USB_C
= (r1
& ((1 << -shift_count
) - 1));
1850 ret
= t1
>> -shift_count
;
1853 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1854 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1859 uint32_t helper_sha_h(target_ulong r1
, target_ulong r2
)
1861 int32_t shift_count
;
1862 int32_t ret_hw0
, ret_hw1
;
1864 shift_count
= sextract32(r2
, 0, 5);
1866 if (shift_count
== 0) {
1868 } else if (shift_count
< 0) {
1869 ret_hw0
= sextract32(r1
, 0, 16) >> -shift_count
;
1870 ret_hw1
= sextract32(r1
, 16, 16) >> -shift_count
;
1871 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1873 ret_hw0
= sextract32(r1
, 0, 16) << shift_count
;
1874 ret_hw1
= sextract32(r1
, 16, 16) << shift_count
;
1875 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1879 uint32_t helper_bmerge(target_ulong r1
, target_ulong r2
)
1884 for (i
= 0; i
< 16; i
++) {
1885 ret
|= (r1
& 1) << (2 * i
+ 1);
1886 ret
|= (r2
& 1) << (2 * i
);
1893 uint64_t helper_bsplit(uint32_t r1
)
1899 for (i
= 0; i
< 32; i
= i
+ 2) {
1901 ret
|= (r1
& 1) << (i
/2);
1904 ret
|= (uint64_t)(r1
& 1) << (i
/2 + 32);
1910 uint32_t helper_parity(target_ulong r1
)
1917 for (i
= 0; i
< 8; i
++) {
1923 for (i
= 0; i
< 8; i
++) {
1930 for (i
= 0; i
< 8; i
++) {
1937 for (i
= 0; i
< 8; i
++) {
1946 uint32_t helper_pack(uint32_t carry
, uint32_t r1_low
, uint32_t r1_high
,
1950 int32_t fp_exp
, fp_frac
, temp_exp
, fp_exp_frac
;
1951 int32_t int_exp
= r1_high
;
1952 int32_t int_mant
= r1_low
;
1953 uint32_t flag_rnd
= (int_mant
& (1 << 7)) && (
1954 (int_mant
& (1 << 8)) ||
1955 (int_mant
& 0x7f) ||
1957 if (((int_mant
& (1<<31)) == 0) && (int_exp
== 255)) {
1959 fp_frac
= extract32(int_mant
, 8, 23);
1960 } else if ((int_mant
& (1<<31)) && (int_exp
>= 127)) {
1963 } else if ((int_mant
& (1<<31)) && (int_exp
<= -128)) {
1966 } else if (int_mant
== 0) {
1970 if (((int_mant
& (1 << 31)) == 0)) {
1973 temp_exp
= int_exp
+ 128;
1975 fp_exp_frac
= (((temp_exp
& 0xff) << 23) |
1976 extract32(int_mant
, 8, 23))
1978 fp_exp
= extract32(fp_exp_frac
, 23, 8);
1979 fp_frac
= extract32(fp_exp_frac
, 0, 23);
1981 ret
= r2
& (1 << 31);
1982 ret
= ret
+ (fp_exp
<< 23);
1983 ret
= ret
+ (fp_frac
& 0x7fffff);
1988 uint64_t helper_unpack(target_ulong arg1
)
1990 int32_t fp_exp
= extract32(arg1
, 23, 8);
1991 int32_t fp_frac
= extract32(arg1
, 0, 23);
1993 int32_t int_exp
, int_mant
;
1995 if (fp_exp
== 255) {
1997 int_mant
= (fp_frac
<< 7);
1998 } else if ((fp_exp
== 0) && (fp_frac
== 0)) {
2001 } else if ((fp_exp
== 0) && (fp_frac
!= 0)) {
2003 int_mant
= (fp_frac
<< 7);
2005 int_exp
= fp_exp
- 127;
2006 int_mant
= (fp_frac
<< 7);
2007 int_mant
|= (1 << 30);
2016 uint64_t helper_dvinit_b_13(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2019 int32_t abs_sig_dividend
, abs_divisor
;
2021 ret
= sextract32(r1
, 0, 32);
2023 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2027 abs_sig_dividend
= abs((int32_t)r1
) >> 8;
2028 abs_divisor
= abs((int32_t)r2
);
2030 ofv if (a/b >= 255) <=> (a/255 >= b) */
2031 env
->PSW_USB_V
= (abs_sig_dividend
>= abs_divisor
) << 31;
2032 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2033 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2034 env
->PSW_USB_AV
= 0;
2039 uint64_t helper_dvinit_b_131(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2041 uint64_t ret
= sextract32(r1
, 0, 32);
2044 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2048 env
->PSW_USB_V
= ((r2
== 0) || ((r2
== 0xffffffff) && (r1
== 0xffffff80)));
2049 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2050 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2051 env
->PSW_USB_AV
= 0;
2056 uint64_t helper_dvinit_h_13(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2059 int32_t abs_sig_dividend
, abs_divisor
;
2061 ret
= sextract32(r1
, 0, 32);
2063 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2067 abs_sig_dividend
= abs((int32_t)r1
) >> 16;
2068 abs_divisor
= abs((int32_t)r2
);
2070 ofv if (a/b >= 0xffff) <=> (a/0xffff >= b) */
2071 env
->PSW_USB_V
= (abs_sig_dividend
>= abs_divisor
) << 31;
2072 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2073 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2074 env
->PSW_USB_AV
= 0;
2079 uint64_t helper_dvinit_h_131(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2081 uint64_t ret
= sextract32(r1
, 0, 32);
2084 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2088 env
->PSW_USB_V
= ((r2
== 0) || ((r2
== 0xffffffff) && (r1
== 0xffff8000)));
2089 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2090 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2091 env
->PSW_USB_AV
= 0;
2096 uint64_t helper_dvadj(uint64_t r1
, uint32_t r2
)
2098 int32_t x_sign
= (r1
>> 63);
2099 int32_t q_sign
= x_sign
^ (r2
>> 31);
2100 int32_t eq_pos
= x_sign
& ((r1
>> 32) == r2
);
2101 int32_t eq_neg
= x_sign
& ((r1
>> 32) == -r2
);
2105 if ((q_sign
& ~eq_neg
) | eq_pos
) {
2106 quotient
= (r1
+ 1) & 0xffffffff;
2108 quotient
= r1
& 0xffffffff;
2111 if (eq_pos
| eq_neg
) {
2114 remainder
= (r1
& 0xffffffff00000000ull
);
2116 return remainder
| quotient
;
2119 uint64_t helper_dvstep(uint64_t r1
, uint32_t r2
)
2121 int32_t dividend_sign
= extract64(r1
, 63, 1);
2122 int32_t divisor_sign
= extract32(r2
, 31, 1);
2123 int32_t quotient_sign
= (dividend_sign
!= divisor_sign
);
2124 int32_t addend
, dividend_quotient
, remainder
;
2127 if (quotient_sign
) {
2132 dividend_quotient
= (int32_t)r1
;
2133 remainder
= (int32_t)(r1
>> 32);
2135 for (i
= 0; i
< 8; i
++) {
2136 remainder
= (remainder
<< 1) | extract32(dividend_quotient
, 31, 1);
2137 dividend_quotient
<<= 1;
2138 temp
= remainder
+ addend
;
2139 if ((temp
< 0) == dividend_sign
) {
2142 if (((temp
< 0) == dividend_sign
)) {
2143 dividend_quotient
= dividend_quotient
| !quotient_sign
;
2145 dividend_quotient
= dividend_quotient
| quotient_sign
;
2148 return ((uint64_t)remainder
<< 32) | (uint32_t)dividend_quotient
;
2151 uint64_t helper_dvstep_u(uint64_t r1
, uint32_t r2
)
2153 int32_t dividend_quotient
= extract64(r1
, 0, 32);
2154 int64_t remainder
= extract64(r1
, 32, 32);
2157 for (i
= 0; i
< 8; i
++) {
2158 remainder
= (remainder
<< 1) | extract32(dividend_quotient
, 31, 1);
2159 dividend_quotient
<<= 1;
2160 temp
= (remainder
& 0xffffffff) - r2
;
2164 dividend_quotient
= dividend_quotient
| !(temp
< 0);
2166 return ((uint64_t)remainder
<< 32) | (uint32_t)dividend_quotient
;
2169 uint64_t helper_divide(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2171 int32_t quotient
, remainder
;
2172 int32_t dividend
= (int32_t)r1
;
2173 int32_t divisor
= (int32_t)r2
;
2176 if (dividend
>= 0) {
2177 quotient
= 0x7fffffff;
2180 quotient
= 0x80000000;
2183 env
->PSW_USB_V
= (1 << 31);
2184 } else if ((divisor
== 0xffffffff) && (dividend
== 0x80000000)) {
2185 quotient
= 0x7fffffff;
2187 env
->PSW_USB_V
= (1 << 31);
2189 remainder
= dividend
% divisor
;
2190 quotient
= (dividend
- remainder
)/divisor
;
2193 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2194 env
->PSW_USB_AV
= 0;
2195 return ((uint64_t)remainder
<< 32) | (uint32_t)quotient
;
2198 uint64_t helper_divide_u(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2200 uint32_t quotient
, remainder
;
2201 uint32_t dividend
= r1
;
2202 uint32_t divisor
= r2
;
2205 quotient
= 0xffffffff;
2207 env
->PSW_USB_V
= (1 << 31);
2209 remainder
= dividend
% divisor
;
2210 quotient
= (dividend
- remainder
)/divisor
;
2213 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2214 env
->PSW_USB_AV
= 0;
2215 return ((uint64_t)remainder
<< 32) | quotient
;
2218 uint64_t helper_mul_h(uint32_t arg00
, uint32_t arg01
,
2219 uint32_t arg10
, uint32_t arg11
, uint32_t n
)
2221 uint32_t result0
, result1
;
2223 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2224 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2225 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2226 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2228 result1
= 0x7fffffff;
2230 result1
= (((uint32_t)(arg00
* arg10
)) << n
);
2233 result0
= 0x7fffffff;
2235 result0
= (((uint32_t)(arg01
* arg11
)) << n
);
2237 return (((uint64_t)result1
<< 32)) | result0
;
2240 uint64_t helper_mulm_h(uint32_t arg00
, uint32_t arg01
,
2241 uint32_t arg10
, uint32_t arg11
, uint32_t n
)
2244 int64_t result0
, result1
;
2246 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2247 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2248 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2249 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2252 result1
= 0x7fffffff;
2254 result1
= (((int32_t)arg00
* (int32_t)arg10
) << n
);
2257 result0
= 0x7fffffff;
2259 result0
= (((int32_t)arg01
* (int32_t)arg11
) << n
);
2261 ret
= (result1
+ result0
);
2265 uint32_t helper_mulr_h(uint32_t arg00
, uint32_t arg01
,
2266 uint32_t arg10
, uint32_t arg11
, uint32_t n
)
2268 uint32_t result0
, result1
;
2270 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2271 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2272 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2273 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2276 result1
= 0x7fffffff;
2278 result1
= ((arg00
* arg10
) << n
) + 0x8000;
2281 result0
= 0x7fffffff;
2283 result0
= ((arg01
* arg11
) << n
) + 0x8000;
2285 return (result1
& 0xffff0000) | (result0
>> 16);
2288 uint32_t helper_crc32(uint32_t arg0
, uint32_t arg1
)
2291 stl_be_p(buf
, arg0
);
2293 return crc32(arg1
, buf
, 4);
2296 /* context save area (CSA) related helpers */
2298 static int cdc_increment(target_ulong
*psw
)
2300 if ((*psw
& MASK_PSW_CDC
) == 0x7f) {
2305 /* check for overflow */
2306 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2307 int mask
= (1u << (7 - lo
)) - 1;
2308 int count
= *psw
& mask
;
2316 static int cdc_decrement(target_ulong
*psw
)
2318 if ((*psw
& MASK_PSW_CDC
) == 0x7f) {
2321 /* check for underflow */
2322 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2323 int mask
= (1u << (7 - lo
)) - 1;
2324 int count
= *psw
& mask
;
2332 static bool cdc_zero(target_ulong
*psw
)
2334 int cdc
= *psw
& MASK_PSW_CDC
;
2335 /* Returns TRUE if PSW.CDC.COUNT == 0 or if PSW.CDC ==
2336 7'b1111111, otherwise returns FALSE. */
2340 /* find CDC.COUNT */
2341 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2342 int mask
= (1u << (7 - lo
)) - 1;
2343 int count
= *psw
& mask
;
2347 static void save_context_upper(CPUTriCoreState
*env
, int ea
)
2349 cpu_stl_data(env
, ea
, env
->PCXI
);
2350 cpu_stl_data(env
, ea
+4, psw_read(env
));
2351 cpu_stl_data(env
, ea
+8, env
->gpr_a
[10]);
2352 cpu_stl_data(env
, ea
+12, env
->gpr_a
[11]);
2353 cpu_stl_data(env
, ea
+16, env
->gpr_d
[8]);
2354 cpu_stl_data(env
, ea
+20, env
->gpr_d
[9]);
2355 cpu_stl_data(env
, ea
+24, env
->gpr_d
[10]);
2356 cpu_stl_data(env
, ea
+28, env
->gpr_d
[11]);
2357 cpu_stl_data(env
, ea
+32, env
->gpr_a
[12]);
2358 cpu_stl_data(env
, ea
+36, env
->gpr_a
[13]);
2359 cpu_stl_data(env
, ea
+40, env
->gpr_a
[14]);
2360 cpu_stl_data(env
, ea
+44, env
->gpr_a
[15]);
2361 cpu_stl_data(env
, ea
+48, env
->gpr_d
[12]);
2362 cpu_stl_data(env
, ea
+52, env
->gpr_d
[13]);
2363 cpu_stl_data(env
, ea
+56, env
->gpr_d
[14]);
2364 cpu_stl_data(env
, ea
+60, env
->gpr_d
[15]);
2367 static void save_context_lower(CPUTriCoreState
*env
, int ea
)
2369 cpu_stl_data(env
, ea
, env
->PCXI
);
2370 cpu_stl_data(env
, ea
+4, env
->gpr_a
[11]);
2371 cpu_stl_data(env
, ea
+8, env
->gpr_a
[2]);
2372 cpu_stl_data(env
, ea
+12, env
->gpr_a
[3]);
2373 cpu_stl_data(env
, ea
+16, env
->gpr_d
[0]);
2374 cpu_stl_data(env
, ea
+20, env
->gpr_d
[1]);
2375 cpu_stl_data(env
, ea
+24, env
->gpr_d
[2]);
2376 cpu_stl_data(env
, ea
+28, env
->gpr_d
[3]);
2377 cpu_stl_data(env
, ea
+32, env
->gpr_a
[4]);
2378 cpu_stl_data(env
, ea
+36, env
->gpr_a
[5]);
2379 cpu_stl_data(env
, ea
+40, env
->gpr_a
[6]);
2380 cpu_stl_data(env
, ea
+44, env
->gpr_a
[7]);
2381 cpu_stl_data(env
, ea
+48, env
->gpr_d
[4]);
2382 cpu_stl_data(env
, ea
+52, env
->gpr_d
[5]);
2383 cpu_stl_data(env
, ea
+56, env
->gpr_d
[6]);
2384 cpu_stl_data(env
, ea
+60, env
->gpr_d
[7]);
2387 static void restore_context_upper(CPUTriCoreState
*env
, int ea
,
2388 target_ulong
*new_PCXI
, target_ulong
*new_PSW
)
2390 *new_PCXI
= cpu_ldl_data(env
, ea
);
2391 *new_PSW
= cpu_ldl_data(env
, ea
+4);
2392 env
->gpr_a
[10] = cpu_ldl_data(env
, ea
+8);
2393 env
->gpr_a
[11] = cpu_ldl_data(env
, ea
+12);
2394 env
->gpr_d
[8] = cpu_ldl_data(env
, ea
+16);
2395 env
->gpr_d
[9] = cpu_ldl_data(env
, ea
+20);
2396 env
->gpr_d
[10] = cpu_ldl_data(env
, ea
+24);
2397 env
->gpr_d
[11] = cpu_ldl_data(env
, ea
+28);
2398 env
->gpr_a
[12] = cpu_ldl_data(env
, ea
+32);
2399 env
->gpr_a
[13] = cpu_ldl_data(env
, ea
+36);
2400 env
->gpr_a
[14] = cpu_ldl_data(env
, ea
+40);
2401 env
->gpr_a
[15] = cpu_ldl_data(env
, ea
+44);
2402 env
->gpr_d
[12] = cpu_ldl_data(env
, ea
+48);
2403 env
->gpr_d
[13] = cpu_ldl_data(env
, ea
+52);
2404 env
->gpr_d
[14] = cpu_ldl_data(env
, ea
+56);
2405 env
->gpr_d
[15] = cpu_ldl_data(env
, ea
+60);
2408 static void restore_context_lower(CPUTriCoreState
*env
, int ea
,
2409 target_ulong
*ra
, target_ulong
*pcxi
)
2411 *pcxi
= cpu_ldl_data(env
, ea
);
2412 *ra
= cpu_ldl_data(env
, ea
+4);
2413 env
->gpr_a
[2] = cpu_ldl_data(env
, ea
+8);
2414 env
->gpr_a
[3] = cpu_ldl_data(env
, ea
+12);
2415 env
->gpr_d
[0] = cpu_ldl_data(env
, ea
+16);
2416 env
->gpr_d
[1] = cpu_ldl_data(env
, ea
+20);
2417 env
->gpr_d
[2] = cpu_ldl_data(env
, ea
+24);
2418 env
->gpr_d
[3] = cpu_ldl_data(env
, ea
+28);
2419 env
->gpr_a
[4] = cpu_ldl_data(env
, ea
+32);
2420 env
->gpr_a
[5] = cpu_ldl_data(env
, ea
+36);
2421 env
->gpr_a
[6] = cpu_ldl_data(env
, ea
+40);
2422 env
->gpr_a
[7] = cpu_ldl_data(env
, ea
+44);
2423 env
->gpr_d
[4] = cpu_ldl_data(env
, ea
+48);
2424 env
->gpr_d
[5] = cpu_ldl_data(env
, ea
+52);
2425 env
->gpr_d
[6] = cpu_ldl_data(env
, ea
+56);
2426 env
->gpr_d
[7] = cpu_ldl_data(env
, ea
+60);
2429 void helper_call(CPUTriCoreState
*env
, uint32_t next_pc
)
2431 target_ulong tmp_FCX
;
2433 target_ulong new_FCX
;
2436 psw
= psw_read(env
);
2437 /* if (FCX == 0) trap(FCU); */
2438 if (env
->FCX
== 0) {
2440 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2442 /* if (PSW.CDE) then if (cdc_increment()) then trap(CDO); */
2443 if (psw
& MASK_PSW_CDE
) {
2444 if (cdc_increment(&psw
)) {
2446 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CDO
, GETPC());
2450 psw
|= MASK_PSW_CDE
;
2451 /* tmp_FCX = FCX; */
2453 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2454 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2455 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2456 /* new_FCX = M(EA, word); */
2457 new_FCX
= cpu_ldl_data(env
, ea
);
2458 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2459 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2461 save_context_upper(env
, ea
);
2463 /* PCXI.PCPN = ICR.CCPN; */
2464 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2465 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2466 /* PCXI.PIE = ICR.IE; */
2467 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE_1_3
) +
2468 ((env
->ICR
& MASK_ICR_IE_1_3
) << 15));
2470 env
->PCXI
|= MASK_PCXI_UL
;
2472 /* PCXI[19: 0] = FCX[19: 0]; */
2473 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2474 /* FCX[19: 0] = new_FCX[19: 0]; */
2475 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2476 /* A[11] = next_pc[31: 0]; */
2477 env
->gpr_a
[11] = next_pc
;
2479 /* if (tmp_FCX == LCX) trap(FCD);*/
2480 if (tmp_FCX
== env
->LCX
) {
2482 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2484 psw_write(env
, psw
);
2487 void helper_ret(CPUTriCoreState
*env
)
2490 target_ulong new_PCXI
;
2491 target_ulong new_PSW
, psw
;
2493 psw
= psw_read(env
);
2494 /* if (PSW.CDE) then if (cdc_decrement()) then trap(CDU);*/
2495 if (psw
& MASK_PSW_CDE
) {
2496 if (cdc_decrement(&psw
)) {
2498 psw_write(env
, psw
);
2499 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CDU
, GETPC());
2502 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2503 if ((env
->PCXI
& 0xfffff) == 0) {
2505 psw_write(env
, psw
);
2506 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2508 /* if (PCXI.UL == 0) then trap(CTYP); */
2509 if ((env
->PCXI
& MASK_PCXI_UL
) == 0) {
2511 cdc_increment(&psw
); /* restore to the start of helper */
2512 psw_write(env
, psw
);
2513 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2515 /* PC = {A11 [31: 1], 1’b0}; */
2516 env
->PC
= env
->gpr_a
[11] & 0xfffffffe;
2518 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
2519 ea
= ((env
->PCXI
& MASK_PCXI_PCXS
) << 12) +
2520 ((env
->PCXI
& MASK_PCXI_PCXO
) << 6);
2521 /* {new_PCXI, new_PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2522 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2523 restore_context_upper(env
, ea
, &new_PCXI
, &new_PSW
);
2524 /* M(EA, word) = FCX; */
2525 cpu_stl_data(env
, ea
, env
->FCX
);
2526 /* FCX[19: 0] = PCXI[19: 0]; */
2527 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2528 /* PCXI = new_PCXI; */
2529 env
->PCXI
= new_PCXI
;
2531 if (tricore_feature(env
, TRICORE_FEATURE_13
)) {
2533 psw_write(env
, new_PSW
);
2535 /* PSW = {new_PSW[31:26], PSW[25:24], new_PSW[23:0]}; */
2536 psw_write(env
, (new_PSW
& ~(0x3000000)) + (psw
& (0x3000000)));
2540 void helper_bisr(CPUTriCoreState
*env
, uint32_t const9
)
2542 target_ulong tmp_FCX
;
2544 target_ulong new_FCX
;
2546 if (env
->FCX
== 0) {
2548 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2552 ea
= ((env
->FCX
& 0xf0000) << 12) + ((env
->FCX
& 0xffff) << 6);
2554 /* new_FCX = M(EA, word); */
2555 new_FCX
= cpu_ldl_data(env
, ea
);
2556 /* M(EA, 16 * word) = {PCXI, A[11], A[2], A[3], D[0], D[1], D[2], D[3], A[4]
2557 , A[5], A[6], A[7], D[4], D[5], D[6], D[7]}; */
2558 save_context_lower(env
, ea
);
2561 /* PCXI.PCPN = ICR.CCPN */
2562 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2563 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2564 /* PCXI.PIE = ICR.IE */
2565 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE_1_3
) +
2566 ((env
->ICR
& MASK_ICR_IE_1_3
) << 15));
2568 env
->PCXI
&= ~(MASK_PCXI_UL
);
2569 /* PCXI[19: 0] = FCX[19: 0] */
2570 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2571 /* FXC[19: 0] = new_FCX[19: 0] */
2572 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2574 env
->ICR
|= MASK_ICR_IE_1_3
;
2576 env
->ICR
|= const9
; /* ICR.CCPN = const9[7: 0];*/
2578 if (tmp_FCX
== env
->LCX
) {
2580 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2584 void helper_rfe(CPUTriCoreState
*env
)
2587 target_ulong new_PCXI
;
2588 target_ulong new_PSW
;
2589 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2590 if ((env
->PCXI
& 0xfffff) == 0) {
2591 /* raise csu trap */
2592 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2594 /* if (PCXI.UL == 0) then trap(CTYP); */
2595 if ((env
->PCXI
& MASK_PCXI_UL
) == 0) {
2596 /* raise CTYP trap */
2597 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2599 /* if (!cdc_zero() AND PSW.CDE) then trap(NEST); */
2600 if (!cdc_zero(&(env
->PSW
)) && (env
->PSW
& MASK_PSW_CDE
)) {
2601 /* raise NEST trap */
2602 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_NEST
, GETPC());
2604 env
->PC
= env
->gpr_a
[11] & ~0x1;
2605 /* ICR.IE = PCXI.PIE; */
2606 env
->ICR
= (env
->ICR
& ~MASK_ICR_IE_1_3
)
2607 + ((env
->PCXI
& MASK_PCXI_PIE_1_3
) >> 15);
2608 /* ICR.CCPN = PCXI.PCPN; */
2609 env
->ICR
= (env
->ICR
& ~MASK_ICR_CCPN
) +
2610 ((env
->PCXI
& MASK_PCXI_PCPN
) >> 24);
2611 /*EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0};*/
2612 ea
= ((env
->PCXI
& MASK_PCXI_PCXS
) << 12) +
2613 ((env
->PCXI
& MASK_PCXI_PCXO
) << 6);
2614 /*{new_PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2615 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2616 restore_context_upper(env
, ea
, &new_PCXI
, &new_PSW
);
2617 /* M(EA, word) = FCX;*/
2618 cpu_stl_data(env
, ea
, env
->FCX
);
2619 /* FCX[19: 0] = PCXI[19: 0]; */
2620 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2621 /* PCXI = new_PCXI; */
2622 env
->PCXI
= new_PCXI
;
2624 psw_write(env
, new_PSW
);
2627 void helper_rfm(CPUTriCoreState
*env
)
2629 env
->PC
= (env
->gpr_a
[11] & ~0x1);
2630 /* ICR.IE = PCXI.PIE; */
2631 env
->ICR
= (env
->ICR
& ~MASK_ICR_IE_1_3
)
2632 | ((env
->PCXI
& MASK_PCXI_PIE_1_3
) >> 15);
2633 /* ICR.CCPN = PCXI.PCPN; */
2634 env
->ICR
= (env
->ICR
& ~MASK_ICR_CCPN
) |
2635 ((env
->PCXI
& MASK_PCXI_PCPN
) >> 24);
2636 /* {PCXI, PSW, A[10], A[11]} = M(DCX, 4 * word); */
2637 env
->PCXI
= cpu_ldl_data(env
, env
->DCX
);
2638 psw_write(env
, cpu_ldl_data(env
, env
->DCX
+4));
2639 env
->gpr_a
[10] = cpu_ldl_data(env
, env
->DCX
+8);
2640 env
->gpr_a
[11] = cpu_ldl_data(env
, env
->DCX
+12);
2642 if (tricore_feature(env
, TRICORE_FEATURE_131
)) {
2647 void helper_ldlcx(CPUTriCoreState
*env
, uint32_t ea
)
2650 /* insn doesn't load PCXI and RA */
2651 restore_context_lower(env
, ea
, &dummy
, &dummy
);
2654 void helper_lducx(CPUTriCoreState
*env
, uint32_t ea
)
2657 /* insn doesn't load PCXI and PSW */
2658 restore_context_upper(env
, ea
, &dummy
, &dummy
);
2661 void helper_stlcx(CPUTriCoreState
*env
, uint32_t ea
)
2663 save_context_lower(env
, ea
);
2666 void helper_stucx(CPUTriCoreState
*env
, uint32_t ea
)
2668 save_context_upper(env
, ea
);
2671 void helper_svlcx(CPUTriCoreState
*env
)
2673 target_ulong tmp_FCX
;
2675 target_ulong new_FCX
;
2677 if (env
->FCX
== 0) {
2679 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2681 /* tmp_FCX = FCX; */
2683 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2684 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2685 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2686 /* new_FCX = M(EA, word); */
2687 new_FCX
= cpu_ldl_data(env
, ea
);
2688 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2689 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2691 save_context_lower(env
, ea
);
2693 /* PCXI.PCPN = ICR.CCPN; */
2694 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2695 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2696 /* PCXI.PIE = ICR.IE; */
2697 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE_1_3
) +
2698 ((env
->ICR
& MASK_ICR_IE_1_3
) << 15));
2700 env
->PCXI
&= ~MASK_PCXI_UL
;
2702 /* PCXI[19: 0] = FCX[19: 0]; */
2703 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2704 /* FCX[19: 0] = new_FCX[19: 0]; */
2705 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2707 /* if (tmp_FCX == LCX) trap(FCD);*/
2708 if (tmp_FCX
== env
->LCX
) {
2710 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2714 void helper_svucx(CPUTriCoreState
*env
)
2716 target_ulong tmp_FCX
;
2718 target_ulong new_FCX
;
2720 if (env
->FCX
== 0) {
2722 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2724 /* tmp_FCX = FCX; */
2726 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2727 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2728 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2729 /* new_FCX = M(EA, word); */
2730 new_FCX
= cpu_ldl_data(env
, ea
);
2731 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2732 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2734 save_context_upper(env
, ea
);
2736 /* PCXI.PCPN = ICR.CCPN; */
2737 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2738 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2739 /* PCXI.PIE = ICR.IE; */
2740 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE_1_3
) +
2741 ((env
->ICR
& MASK_ICR_IE_1_3
) << 15));
2743 env
->PCXI
|= MASK_PCXI_UL
;
2745 /* PCXI[19: 0] = FCX[19: 0]; */
2746 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2747 /* FCX[19: 0] = new_FCX[19: 0]; */
2748 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2750 /* if (tmp_FCX == LCX) trap(FCD);*/
2751 if (tmp_FCX
== env
->LCX
) {
2753 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2757 void helper_rslcx(CPUTriCoreState
*env
)
2760 target_ulong new_PCXI
;
2761 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2762 if ((env
->PCXI
& 0xfffff) == 0) {
2764 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2766 /* if (PCXI.UL == 1) then trap(CTYP); */
2767 if ((env
->PCXI
& MASK_PCXI_UL
) != 0) {
2769 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2771 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
2772 ea
= ((env
->PCXI
& MASK_PCXI_PCXS
) << 12) +
2773 ((env
->PCXI
& MASK_PCXI_PCXO
) << 6);
2774 /* {new_PCXI, A[11], A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2775 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2776 restore_context_lower(env
, ea
, &env
->gpr_a
[11], &new_PCXI
);
2777 /* M(EA, word) = FCX; */
2778 cpu_stl_data(env
, ea
, env
->FCX
);
2779 /* M(EA, word) = FCX; */
2780 cpu_stl_data(env
, ea
, env
->FCX
);
2781 /* FCX[19: 0] = PCXI[19: 0]; */
2782 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2783 /* PCXI = new_PCXI; */
2784 env
->PCXI
= new_PCXI
;
2787 void helper_psw_write(CPUTriCoreState
*env
, uint32_t arg
)
2789 psw_write(env
, arg
);
2792 uint32_t helper_psw_read(CPUTriCoreState
*env
)
2794 return psw_read(env
);
2798 static inline void QEMU_NORETURN
do_raise_exception_err(CPUTriCoreState
*env
,
2803 CPUState
*cs
= CPU(tricore_env_get_cpu(env
));
2804 cs
->exception_index
= exception
;
2805 env
->error_code
= error_code
;
2806 /* now we have a real cpu fault */
2807 cpu_loop_exit_restore(cs
, pc
);
2810 void tlb_fill(CPUState
*cs
, target_ulong addr
, int size
,
2811 MMUAccessType access_type
, int mmu_idx
, uintptr_t retaddr
)
2814 ret
= cpu_tricore_handle_mmu_fault(cs
, addr
, access_type
, mmu_idx
);
2816 TriCoreCPU
*cpu
= TRICORE_CPU(cs
);
2817 CPUTriCoreState
*env
= &cpu
->env
;
2818 do_raise_exception_err(env
, cs
->exception_index
,
2819 env
->error_code
, retaddr
);