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 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/cpu_ldst.h"
22 #include <zlib.h> /* for crc32 */
25 /* Exception helpers */
27 static void QEMU_NORETURN
28 raise_exception_sync_internal(CPUTriCoreState
*env
, uint32_t class, int tin
,
29 uintptr_t pc
, uint32_t fcd_pc
)
31 CPUState
*cs
= CPU(tricore_env_get_cpu(env
));
32 /* in case we come from a helper-call we need to restore the PC */
34 cpu_restore_state(cs
, pc
);
37 /* Tin is loaded into d[15] */
40 if (class == TRAPC_CTX_MNG
&& tin
== TIN3_FCU
) {
41 /* upper context cannot be saved, if the context list is empty */
46 /* The return address in a[11] is updated */
47 if (class == TRAPC_CTX_MNG
&& tin
== TIN3_FCD
) {
48 env
->SYSCON
|= MASK_SYSCON_FCD_SF
;
49 /* when we run out of CSAs after saving a context a FCD trap is taken
50 and the return address is the start of the trap handler which used
52 env
->gpr_a
[11] = fcd_pc
;
53 } else if (class == TRAPC_SYSCALL
) {
54 env
->gpr_a
[11] = env
->PC
+ 4;
56 env
->gpr_a
[11] = env
->PC
;
58 /* The stack pointer in A[10] is set to the Interrupt Stack Pointer (ISP)
59 when the processor was not previously using the interrupt stack
60 (in case of PSW.IS = 0). The stack pointer bit is set for using the
61 interrupt stack: PSW.IS = 1. */
62 if ((env
->PSW
& MASK_PSW_IS
) == 0) {
63 env
->gpr_a
[10] = env
->ISP
;
65 env
->PSW
|= MASK_PSW_IS
;
66 /* The I/O mode is set to Supervisor mode, which means all permissions
67 are enabled: PSW.IO = 10 B .*/
68 env
->PSW
|= (2 << 10);
70 /*The current Protection Register Set is set to 0: PSW.PRS = 00 B .*/
71 env
->PSW
&= ~MASK_PSW_PRS
;
73 /* The Call Depth Counter (CDC) is cleared, and the call depth limit is
74 set for 64: PSW.CDC = 0000000 B .*/
75 env
->PSW
&= ~MASK_PSW_CDC
;
77 /* Call Depth Counter is enabled, PSW.CDE = 1. */
78 env
->PSW
|= MASK_PSW_CDE
;
80 /* Write permission to global registers A[0], A[1], A[8], A[9] is
81 disabled: PSW.GW = 0. */
82 env
->PSW
&= ~MASK_PSW_GW
;
84 /*The interrupt system is globally disabled: ICR.IE = 0. The ‘old’
85 ICR.IE and ICR.CCPN are saved */
87 /* PCXI.PIE = ICR.IE */
88 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
89 ((env
->ICR
& MASK_ICR_IE
) << 15));
90 /* PCXI.PCPN = ICR.CCPN */
91 env
->PCXI
= (env
->PCXI
& 0xffffff) +
92 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
93 /* Update PC using the trap vector table */
94 env
->PC
= env
->BTV
| (class << 5);
99 void helper_raise_exception_sync(CPUTriCoreState
*env
, uint32_t class,
102 raise_exception_sync_internal(env
, class, tin
, 0, 0);
105 static void raise_exception_sync_helper(CPUTriCoreState
*env
, uint32_t class,
106 uint32_t tin
, uintptr_t pc
)
108 raise_exception_sync_internal(env
, class, tin
, pc
, 0);
111 /* Addressing mode helper */
113 static uint16_t reverse16(uint16_t val
)
115 uint8_t high
= (uint8_t)(val
>> 8);
116 uint8_t low
= (uint8_t)(val
& 0xff);
120 rl
= (uint16_t)((high
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023);
121 rh
= (uint16_t)((low
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023);
123 return (rh
<< 8) | rl
;
126 uint32_t helper_br_update(uint32_t reg
)
128 uint32_t index
= reg
& 0xffff;
129 uint32_t incr
= reg
>> 16;
130 uint32_t new_index
= reverse16(reverse16(index
) + reverse16(incr
));
131 return reg
- index
+ new_index
;
134 uint32_t helper_circ_update(uint32_t reg
, uint32_t off
)
136 uint32_t index
= reg
& 0xffff;
137 uint32_t length
= reg
>> 16;
138 int32_t new_index
= index
+ off
;
144 return reg
- index
+ new_index
;
147 static uint32_t ssov32(CPUTriCoreState
*env
, int64_t arg
)
150 int64_t max_pos
= INT32_MAX
;
151 int64_t max_neg
= INT32_MIN
;
153 env
->PSW_USB_V
= (1 << 31);
154 env
->PSW_USB_SV
= (1 << 31);
155 ret
= (target_ulong
)max_pos
;
158 env
->PSW_USB_V
= (1 << 31);
159 env
->PSW_USB_SV
= (1 << 31);
160 ret
= (target_ulong
)max_neg
;
163 ret
= (target_ulong
)arg
;
166 env
->PSW_USB_AV
= arg
^ arg
* 2u;
167 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
171 static uint32_t suov32_pos(CPUTriCoreState
*env
, uint64_t arg
)
174 uint64_t max_pos
= UINT32_MAX
;
176 env
->PSW_USB_V
= (1 << 31);
177 env
->PSW_USB_SV
= (1 << 31);
178 ret
= (target_ulong
)max_pos
;
181 ret
= (target_ulong
)arg
;
183 env
->PSW_USB_AV
= arg
^ arg
* 2u;
184 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
188 static uint32_t suov32_neg(CPUTriCoreState
*env
, int64_t arg
)
193 env
->PSW_USB_V
= (1 << 31);
194 env
->PSW_USB_SV
= (1 << 31);
198 ret
= (target_ulong
)arg
;
200 env
->PSW_USB_AV
= arg
^ arg
* 2u;
201 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
205 static uint32_t ssov16(CPUTriCoreState
*env
, int32_t hw0
, int32_t hw1
)
207 int32_t max_pos
= INT16_MAX
;
208 int32_t max_neg
= INT16_MIN
;
212 av0
= hw0
^ hw0
* 2u;
214 env
->PSW_USB_V
= (1 << 31);
216 } else if (hw0
< max_neg
) {
217 env
->PSW_USB_V
= (1 << 31);
221 av1
= hw1
^ hw1
* 2u;
223 env
->PSW_USB_V
= (1 << 31);
225 } else if (hw1
< max_neg
) {
226 env
->PSW_USB_V
= (1 << 31);
230 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
231 env
->PSW_USB_AV
= (av0
| av1
) << 16;
232 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
233 return (hw0
& 0xffff) | (hw1
<< 16);
236 static uint32_t suov16(CPUTriCoreState
*env
, int32_t hw0
, int32_t hw1
)
238 int32_t max_pos
= UINT16_MAX
;
242 av0
= hw0
^ hw0
* 2u;
244 env
->PSW_USB_V
= (1 << 31);
246 } else if (hw0
< 0) {
247 env
->PSW_USB_V
= (1 << 31);
251 av1
= hw1
^ hw1
* 2u;
253 env
->PSW_USB_V
= (1 << 31);
255 } else if (hw1
< 0) {
256 env
->PSW_USB_V
= (1 << 31);
260 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
261 env
->PSW_USB_AV
= (av0
| av1
) << 16;
262 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
263 return (hw0
& 0xffff) | (hw1
<< 16);
266 target_ulong
helper_add_ssov(CPUTriCoreState
*env
, target_ulong r1
,
269 int64_t t1
= sextract64(r1
, 0, 32);
270 int64_t t2
= sextract64(r2
, 0, 32);
271 int64_t result
= t1
+ t2
;
272 return ssov32(env
, result
);
275 uint64_t helper_add64_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
281 ovf
= (result
^ r1
) & ~(r1
^ r2
);
282 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
283 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
285 env
->PSW_USB_V
= (1 << 31);
286 env
->PSW_USB_SV
= (1 << 31);
287 /* ext_ret > MAX_INT */
288 if ((int64_t)r1
>= 0) {
290 /* ext_ret < MIN_INT */
300 target_ulong
helper_add_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
303 int32_t ret_hw0
, ret_hw1
;
305 ret_hw0
= sextract32(r1
, 0, 16) + sextract32(r2
, 0, 16);
306 ret_hw1
= sextract32(r1
, 16, 16) + sextract32(r2
, 16, 16);
307 return ssov16(env
, ret_hw0
, ret_hw1
);
310 uint32_t helper_addr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
313 int64_t mul_res0
= sextract64(r1
, 0, 32);
314 int64_t mul_res1
= sextract64(r1
, 32, 32);
315 int64_t r2_low
= sextract64(r2_l
, 0, 32);
316 int64_t r2_high
= sextract64(r2_h
, 0, 32);
317 int64_t result0
, result1
;
323 result0
= r2_low
+ mul_res0
+ 0x8000;
324 result1
= r2_high
+ mul_res1
+ 0x8000;
327 avf0
= result0
^ avf0
;
329 avf1
= result1
^ avf1
;
331 if (result0
> INT32_MAX
) {
334 } else if (result0
< INT32_MIN
) {
339 if (result1
> INT32_MAX
) {
342 } else if (result1
< INT32_MIN
) {
347 env
->PSW_USB_V
= ovf0
| ovf1
;
348 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
350 env
->PSW_USB_AV
= avf0
| avf1
;
351 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
353 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
356 uint32_t helper_addsur_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
359 int64_t mul_res0
= sextract64(r1
, 0, 32);
360 int64_t mul_res1
= sextract64(r1
, 32, 32);
361 int64_t r2_low
= sextract64(r2_l
, 0, 32);
362 int64_t r2_high
= sextract64(r2_h
, 0, 32);
363 int64_t result0
, result1
;
369 result0
= r2_low
- mul_res0
+ 0x8000;
370 result1
= r2_high
+ mul_res1
+ 0x8000;
373 avf0
= result0
^ avf0
;
375 avf1
= result1
^ avf1
;
377 if (result0
> INT32_MAX
) {
380 } else if (result0
< INT32_MIN
) {
385 if (result1
> INT32_MAX
) {
388 } else if (result1
< INT32_MIN
) {
393 env
->PSW_USB_V
= ovf0
| ovf1
;
394 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
396 env
->PSW_USB_AV
= avf0
| avf1
;
397 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
399 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
403 target_ulong
helper_add_suov(CPUTriCoreState
*env
, target_ulong r1
,
406 int64_t t1
= extract64(r1
, 0, 32);
407 int64_t t2
= extract64(r2
, 0, 32);
408 int64_t result
= t1
+ t2
;
409 return suov32_pos(env
, result
);
412 target_ulong
helper_add_h_suov(CPUTriCoreState
*env
, target_ulong r1
,
415 int32_t ret_hw0
, ret_hw1
;
417 ret_hw0
= extract32(r1
, 0, 16) + extract32(r2
, 0, 16);
418 ret_hw1
= extract32(r1
, 16, 16) + extract32(r2
, 16, 16);
419 return suov16(env
, ret_hw0
, ret_hw1
);
422 target_ulong
helper_sub_ssov(CPUTriCoreState
*env
, target_ulong r1
,
425 int64_t t1
= sextract64(r1
, 0, 32);
426 int64_t t2
= sextract64(r2
, 0, 32);
427 int64_t result
= t1
- t2
;
428 return ssov32(env
, result
);
431 uint64_t helper_sub64_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
437 ovf
= (result
^ r1
) & (r1
^ r2
);
438 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
439 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
441 env
->PSW_USB_V
= (1 << 31);
442 env
->PSW_USB_SV
= (1 << 31);
443 /* ext_ret > MAX_INT */
444 if ((int64_t)r1
>= 0) {
446 /* ext_ret < MIN_INT */
456 target_ulong
helper_sub_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
459 int32_t ret_hw0
, ret_hw1
;
461 ret_hw0
= sextract32(r1
, 0, 16) - sextract32(r2
, 0, 16);
462 ret_hw1
= sextract32(r1
, 16, 16) - sextract32(r2
, 16, 16);
463 return ssov16(env
, ret_hw0
, ret_hw1
);
466 uint32_t helper_subr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
469 int64_t mul_res0
= sextract64(r1
, 0, 32);
470 int64_t mul_res1
= sextract64(r1
, 32, 32);
471 int64_t r2_low
= sextract64(r2_l
, 0, 32);
472 int64_t r2_high
= sextract64(r2_h
, 0, 32);
473 int64_t result0
, result1
;
479 result0
= r2_low
- mul_res0
+ 0x8000;
480 result1
= r2_high
- mul_res1
+ 0x8000;
483 avf0
= result0
^ avf0
;
485 avf1
= result1
^ avf1
;
487 if (result0
> INT32_MAX
) {
490 } else if (result0
< INT32_MIN
) {
495 if (result1
> INT32_MAX
) {
498 } else if (result1
< INT32_MIN
) {
503 env
->PSW_USB_V
= ovf0
| ovf1
;
504 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
506 env
->PSW_USB_AV
= avf0
| avf1
;
507 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
509 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
512 uint32_t helper_subadr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
515 int64_t mul_res0
= sextract64(r1
, 0, 32);
516 int64_t mul_res1
= sextract64(r1
, 32, 32);
517 int64_t r2_low
= sextract64(r2_l
, 0, 32);
518 int64_t r2_high
= sextract64(r2_h
, 0, 32);
519 int64_t result0
, result1
;
525 result0
= r2_low
+ mul_res0
+ 0x8000;
526 result1
= r2_high
- mul_res1
+ 0x8000;
529 avf0
= result0
^ avf0
;
531 avf1
= result1
^ avf1
;
533 if (result0
> INT32_MAX
) {
536 } else if (result0
< INT32_MIN
) {
541 if (result1
> INT32_MAX
) {
544 } else if (result1
< INT32_MIN
) {
549 env
->PSW_USB_V
= ovf0
| ovf1
;
550 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
552 env
->PSW_USB_AV
= avf0
| avf1
;
553 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
555 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
558 target_ulong
helper_sub_suov(CPUTriCoreState
*env
, target_ulong r1
,
561 int64_t t1
= extract64(r1
, 0, 32);
562 int64_t t2
= extract64(r2
, 0, 32);
563 int64_t result
= t1
- t2
;
564 return suov32_neg(env
, result
);
567 target_ulong
helper_sub_h_suov(CPUTriCoreState
*env
, target_ulong r1
,
570 int32_t ret_hw0
, ret_hw1
;
572 ret_hw0
= extract32(r1
, 0, 16) - extract32(r2
, 0, 16);
573 ret_hw1
= extract32(r1
, 16, 16) - extract32(r2
, 16, 16);
574 return suov16(env
, ret_hw0
, ret_hw1
);
577 target_ulong
helper_mul_ssov(CPUTriCoreState
*env
, target_ulong r1
,
580 int64_t t1
= sextract64(r1
, 0, 32);
581 int64_t t2
= sextract64(r2
, 0, 32);
582 int64_t result
= t1
* t2
;
583 return ssov32(env
, result
);
586 target_ulong
helper_mul_suov(CPUTriCoreState
*env
, target_ulong r1
,
589 int64_t t1
= extract64(r1
, 0, 32);
590 int64_t t2
= extract64(r2
, 0, 32);
591 int64_t result
= t1
* t2
;
593 return suov32_pos(env
, result
);
596 target_ulong
helper_sha_ssov(CPUTriCoreState
*env
, target_ulong r1
,
599 int64_t t1
= sextract64(r1
, 0, 32);
600 int32_t t2
= sextract64(r2
, 0, 6);
609 return ssov32(env
, result
);
612 uint32_t helper_abs_ssov(CPUTriCoreState
*env
, target_ulong r1
)
615 result
= ((int32_t)r1
>= 0) ? r1
: (0 - r1
);
616 return ssov32(env
, result
);
619 uint32_t helper_abs_h_ssov(CPUTriCoreState
*env
, target_ulong r1
)
621 int32_t ret_h0
, ret_h1
;
623 ret_h0
= sextract32(r1
, 0, 16);
624 ret_h0
= (ret_h0
>= 0) ? ret_h0
: (0 - ret_h0
);
626 ret_h1
= sextract32(r1
, 16, 16);
627 ret_h1
= (ret_h1
>= 0) ? ret_h1
: (0 - ret_h1
);
629 return ssov16(env
, ret_h0
, ret_h1
);
632 target_ulong
helper_absdif_ssov(CPUTriCoreState
*env
, target_ulong r1
,
635 int64_t t1
= sextract64(r1
, 0, 32);
636 int64_t t2
= sextract64(r2
, 0, 32);
644 return ssov32(env
, result
);
647 uint32_t helper_absdif_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
651 int32_t ret_h0
, ret_h1
;
653 t1
= sextract32(r1
, 0, 16);
654 t2
= sextract32(r2
, 0, 16);
661 t1
= sextract32(r1
, 16, 16);
662 t2
= sextract32(r2
, 16, 16);
669 return ssov16(env
, ret_h0
, ret_h1
);
672 target_ulong
helper_madd32_ssov(CPUTriCoreState
*env
, target_ulong r1
,
673 target_ulong r2
, target_ulong r3
)
675 int64_t t1
= sextract64(r1
, 0, 32);
676 int64_t t2
= sextract64(r2
, 0, 32);
677 int64_t t3
= sextract64(r3
, 0, 32);
680 result
= t2
+ (t1
* t3
);
681 return ssov32(env
, result
);
684 target_ulong
helper_madd32_suov(CPUTriCoreState
*env
, target_ulong r1
,
685 target_ulong r2
, target_ulong r3
)
687 uint64_t t1
= extract64(r1
, 0, 32);
688 uint64_t t2
= extract64(r2
, 0, 32);
689 uint64_t t3
= extract64(r3
, 0, 32);
692 result
= t2
+ (t1
* t3
);
693 return suov32_pos(env
, result
);
696 uint64_t helper_madd64_ssov(CPUTriCoreState
*env
, target_ulong r1
,
697 uint64_t r2
, target_ulong r3
)
700 int64_t t1
= sextract64(r1
, 0, 32);
701 int64_t t3
= sextract64(r3
, 0, 32);
706 ovf
= (ret
^ mul
) & ~(mul
^ r2
);
709 env
->PSW_USB_AV
= t1
^ t1
* 2u;
710 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
712 if ((int64_t)ovf
< 0) {
713 env
->PSW_USB_V
= (1 << 31);
714 env
->PSW_USB_SV
= (1 << 31);
715 /* ext_ret > MAX_INT */
718 /* ext_ret < MIN_INT */
730 helper_madd32_q_add_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
736 env
->PSW_USB_AV
= (result
^ result
* 2u);
737 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
739 /* we do the saturation by hand, since we produce an overflow on the host
740 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
741 case, we flip the saturated value. */
742 if (r2
== 0x8000000000000000LL
) {
743 if (result
> 0x7fffffffLL
) {
744 env
->PSW_USB_V
= (1 << 31);
745 env
->PSW_USB_SV
= (1 << 31);
747 } else if (result
< -0x80000000LL
) {
748 env
->PSW_USB_V
= (1 << 31);
749 env
->PSW_USB_SV
= (1 << 31);
755 if (result
> 0x7fffffffLL
) {
756 env
->PSW_USB_V
= (1 << 31);
757 env
->PSW_USB_SV
= (1 << 31);
759 } else if (result
< -0x80000000LL
) {
760 env
->PSW_USB_V
= (1 << 31);
761 env
->PSW_USB_SV
= (1 << 31);
767 return (uint32_t)result
;
770 uint64_t helper_madd64_q_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2
,
771 uint32_t r3
, uint32_t n
)
773 int64_t t1
= (int64_t)r1
;
774 int64_t t2
= sextract64(r2
, 0, 32);
775 int64_t t3
= sextract64(r3
, 0, 32);
779 mul
= (t2
* t3
) << n
;
782 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
783 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
785 ovf
= (result
^ mul
) & ~(mul
^ t1
);
786 /* we do the saturation by hand, since we produce an overflow on the host
787 if the mul was (0x80000000 * 0x80000000) << 1). If this is the
788 case, we flip the saturated value. */
789 if ((r2
== 0x80000000) && (r3
== 0x80000000) && (n
== 1)) {
791 env
->PSW_USB_V
= (1 << 31);
792 env
->PSW_USB_SV
= (1 << 31);
793 /* ext_ret > MAX_INT */
796 /* ext_ret < MIN_INT */
805 env
->PSW_USB_V
= (1 << 31);
806 env
->PSW_USB_SV
= (1 << 31);
807 /* ext_ret > MAX_INT */
810 /* ext_ret < MIN_INT */
818 return (uint64_t)result
;
821 uint32_t helper_maddr_q_ssov(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
822 uint32_t r3
, uint32_t n
)
824 int64_t t1
= sextract64(r1
, 0, 32);
825 int64_t t2
= sextract64(r2
, 0, 32);
826 int64_t t3
= sextract64(r3
, 0, 32);
829 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
832 mul
= (t2
* t3
) << n
;
835 ret
= t1
+ mul
+ 0x8000;
837 env
->PSW_USB_AV
= ret
^ ret
* 2u;
838 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
840 if (ret
> 0x7fffffffll
) {
841 env
->PSW_USB_V
= (1 << 31);
842 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
844 } else if (ret
< -0x80000000ll
) {
845 env
->PSW_USB_V
= (1 << 31);
846 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
851 return ret
& 0xffff0000ll
;
854 uint64_t helper_madd64_suov(CPUTriCoreState
*env
, target_ulong r1
,
855 uint64_t r2
, target_ulong r3
)
858 uint64_t t1
= extract64(r1
, 0, 32);
859 uint64_t t3
= extract64(r3
, 0, 32);
865 env
->PSW_USB_AV
= t1
^ t1
* 2u;
866 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
869 env
->PSW_USB_V
= (1 << 31);
870 env
->PSW_USB_SV
= (1 << 31);
879 target_ulong
helper_msub32_ssov(CPUTriCoreState
*env
, target_ulong r1
,
880 target_ulong r2
, target_ulong r3
)
882 int64_t t1
= sextract64(r1
, 0, 32);
883 int64_t t2
= sextract64(r2
, 0, 32);
884 int64_t t3
= sextract64(r3
, 0, 32);
887 result
= t2
- (t1
* t3
);
888 return ssov32(env
, result
);
891 target_ulong
helper_msub32_suov(CPUTriCoreState
*env
, target_ulong r1
,
892 target_ulong r2
, target_ulong r3
)
894 uint64_t t1
= extract64(r1
, 0, 32);
895 uint64_t t2
= extract64(r2
, 0, 32);
896 uint64_t t3
= extract64(r3
, 0, 32);
903 env
->PSW_USB_AV
= result
^ result
* 2u;
904 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
905 /* we calculate ovf by hand here, because the multiplication can overflow on
906 the host, which would give false results if we compare to less than
909 env
->PSW_USB_V
= (1 << 31);
910 env
->PSW_USB_SV
= (1 << 31);
918 uint64_t helper_msub64_ssov(CPUTriCoreState
*env
, target_ulong r1
,
919 uint64_t r2
, target_ulong r3
)
922 int64_t t1
= sextract64(r1
, 0, 32);
923 int64_t t3
= sextract64(r3
, 0, 32);
928 ovf
= (ret
^ r2
) & (mul
^ r2
);
931 env
->PSW_USB_AV
= t1
^ t1
* 2u;
932 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
934 if ((int64_t)ovf
< 0) {
935 env
->PSW_USB_V
= (1 << 31);
936 env
->PSW_USB_SV
= (1 << 31);
937 /* ext_ret > MAX_INT */
940 /* ext_ret < MIN_INT */
950 uint64_t helper_msub64_suov(CPUTriCoreState
*env
, target_ulong r1
,
951 uint64_t r2
, target_ulong r3
)
954 uint64_t t1
= extract64(r1
, 0, 32);
955 uint64_t t3
= extract64(r3
, 0, 32);
961 env
->PSW_USB_AV
= t1
^ t1
* 2u;
962 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
965 env
->PSW_USB_V
= (1 << 31);
966 env
->PSW_USB_SV
= (1 << 31);
976 helper_msub32_q_sub_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
979 int64_t t1
= (int64_t)r1
;
980 int64_t t2
= (int64_t)r2
;
984 env
->PSW_USB_AV
= (result
^ result
* 2u);
985 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
987 /* we do the saturation by hand, since we produce an overflow on the host
988 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
989 case, we flip the saturated value. */
990 if (r2
== 0x8000000000000000LL
) {
991 if (result
> 0x7fffffffLL
) {
992 env
->PSW_USB_V
= (1 << 31);
993 env
->PSW_USB_SV
= (1 << 31);
995 } else if (result
< -0x80000000LL
) {
996 env
->PSW_USB_V
= (1 << 31);
997 env
->PSW_USB_SV
= (1 << 31);
1003 if (result
> 0x7fffffffLL
) {
1004 env
->PSW_USB_V
= (1 << 31);
1005 env
->PSW_USB_SV
= (1 << 31);
1007 } else if (result
< -0x80000000LL
) {
1008 env
->PSW_USB_V
= (1 << 31);
1009 env
->PSW_USB_SV
= (1 << 31);
1015 return (uint32_t)result
;
1018 uint64_t helper_msub64_q_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2
,
1019 uint32_t r3
, uint32_t n
)
1021 int64_t t1
= (int64_t)r1
;
1022 int64_t t2
= sextract64(r2
, 0, 32);
1023 int64_t t3
= sextract64(r3
, 0, 32);
1024 int64_t result
, mul
;
1027 mul
= (t2
* t3
) << n
;
1030 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
1031 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1033 ovf
= (result
^ t1
) & (t1
^ mul
);
1034 /* we do the saturation by hand, since we produce an overflow on the host
1035 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
1036 case, we flip the saturated value. */
1037 if (mul
== 0x8000000000000000LL
) {
1039 env
->PSW_USB_V
= (1 << 31);
1040 env
->PSW_USB_SV
= (1 << 31);
1041 /* ext_ret > MAX_INT */
1044 /* ext_ret < MIN_INT */
1051 env
->PSW_USB_V
= (1 << 31);
1052 env
->PSW_USB_SV
= (1 << 31);
1053 /* ext_ret > MAX_INT */
1056 /* ext_ret < MIN_INT */
1065 return (uint64_t)result
;
1068 uint32_t helper_msubr_q_ssov(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
1069 uint32_t r3
, uint32_t n
)
1071 int64_t t1
= sextract64(r1
, 0, 32);
1072 int64_t t2
= sextract64(r2
, 0, 32);
1073 int64_t t3
= sextract64(r3
, 0, 32);
1076 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
1079 mul
= (t2
* t3
) << n
;
1082 ret
= t1
- mul
+ 0x8000;
1084 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1085 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1087 if (ret
> 0x7fffffffll
) {
1088 env
->PSW_USB_V
= (1 << 31);
1089 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1091 } else if (ret
< -0x80000000ll
) {
1092 env
->PSW_USB_V
= (1 << 31);
1093 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1098 return ret
& 0xffff0000ll
;
1101 uint32_t helper_abs_b(CPUTriCoreState
*env
, target_ulong arg
)
1108 for (i
= 0; i
< 4; i
++) {
1109 b
= sextract32(arg
, i
* 8, 8);
1110 b
= (b
>= 0) ? b
: (0 - b
);
1111 ovf
|= (b
> 0x7F) || (b
< -0x80);
1113 ret
|= (b
& 0xff) << (i
* 8);
1116 env
->PSW_USB_V
= ovf
<< 31;
1117 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1118 env
->PSW_USB_AV
= avf
<< 24;
1119 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1124 uint32_t helper_abs_h(CPUTriCoreState
*env
, target_ulong arg
)
1131 for (i
= 0; i
< 2; i
++) {
1132 h
= sextract32(arg
, i
* 16, 16);
1133 h
= (h
>= 0) ? h
: (0 - h
);
1134 ovf
|= (h
> 0x7FFF) || (h
< -0x8000);
1136 ret
|= (h
& 0xffff) << (i
* 16);
1139 env
->PSW_USB_V
= ovf
<< 31;
1140 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1141 env
->PSW_USB_AV
= avf
<< 16;
1142 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1147 uint32_t helper_absdif_b(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1155 for (i
= 0; i
< 4; i
++) {
1156 extr_r2
= sextract32(r2
, i
* 8, 8);
1157 b
= sextract32(r1
, i
* 8, 8);
1158 b
= (b
> extr_r2
) ? (b
- extr_r2
) : (extr_r2
- b
);
1159 ovf
|= (b
> 0x7F) || (b
< -0x80);
1161 ret
|= (b
& 0xff) << (i
* 8);
1164 env
->PSW_USB_V
= ovf
<< 31;
1165 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1166 env
->PSW_USB_AV
= avf
<< 24;
1167 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1171 uint32_t helper_absdif_h(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1179 for (i
= 0; i
< 2; i
++) {
1180 extr_r2
= sextract32(r2
, i
* 16, 16);
1181 h
= sextract32(r1
, i
* 16, 16);
1182 h
= (h
> extr_r2
) ? (h
- extr_r2
) : (extr_r2
- h
);
1183 ovf
|= (h
> 0x7FFF) || (h
< -0x8000);
1185 ret
|= (h
& 0xffff) << (i
* 16);
1188 env
->PSW_USB_V
= ovf
<< 31;
1189 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1190 env
->PSW_USB_AV
= avf
<< 16;
1191 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1196 uint32_t helper_addr_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1199 int64_t mul_res0
= sextract64(r1
, 0, 32);
1200 int64_t mul_res1
= sextract64(r1
, 32, 32);
1201 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1202 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1203 int64_t result0
, result1
;
1204 uint32_t ovf0
, ovf1
;
1205 uint32_t avf0
, avf1
;
1209 result0
= r2_low
+ mul_res0
+ 0x8000;
1210 result1
= r2_high
+ mul_res1
+ 0x8000;
1212 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1216 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1220 env
->PSW_USB_V
= ovf0
| ovf1
;
1221 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1223 avf0
= result0
* 2u;
1224 avf0
= result0
^ avf0
;
1225 avf1
= result1
* 2u;
1226 avf1
= result1
^ avf1
;
1228 env
->PSW_USB_AV
= avf0
| avf1
;
1229 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1231 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1234 uint32_t helper_addsur_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1237 int64_t mul_res0
= sextract64(r1
, 0, 32);
1238 int64_t mul_res1
= sextract64(r1
, 32, 32);
1239 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1240 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1241 int64_t result0
, result1
;
1242 uint32_t ovf0
, ovf1
;
1243 uint32_t avf0
, avf1
;
1247 result0
= r2_low
- mul_res0
+ 0x8000;
1248 result1
= r2_high
+ mul_res1
+ 0x8000;
1250 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1254 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1258 env
->PSW_USB_V
= ovf0
| ovf1
;
1259 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1261 avf0
= result0
* 2u;
1262 avf0
= result0
^ avf0
;
1263 avf1
= result1
* 2u;
1264 avf1
= result1
^ avf1
;
1266 env
->PSW_USB_AV
= avf0
| avf1
;
1267 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1269 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1272 uint32_t helper_maddr_q(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
1273 uint32_t r3
, uint32_t n
)
1275 int64_t t1
= sextract64(r1
, 0, 32);
1276 int64_t t2
= sextract64(r2
, 0, 32);
1277 int64_t t3
= sextract64(r3
, 0, 32);
1280 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
1283 mul
= (t2
* t3
) << n
;
1286 ret
= t1
+ mul
+ 0x8000;
1288 if ((ret
> 0x7fffffffll
) || (ret
< -0x80000000ll
)) {
1289 env
->PSW_USB_V
= (1 << 31);
1290 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1294 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1295 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1297 return ret
& 0xffff0000ll
;
1300 uint32_t helper_add_b(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1303 int32_t extr_r1
, extr_r2
;
1308 for (i
= 0; i
< 4; i
++) {
1309 extr_r1
= sextract32(r1
, i
* 8, 8);
1310 extr_r2
= sextract32(r2
, i
* 8, 8);
1312 b
= extr_r1
+ extr_r2
;
1313 ovf
|= ((b
> 0x7f) || (b
< -0x80));
1315 ret
|= ((b
& 0xff) << (i
*8));
1318 env
->PSW_USB_V
= (ovf
<< 31);
1319 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1320 env
->PSW_USB_AV
= avf
<< 24;
1321 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1326 uint32_t helper_add_h(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1329 int32_t extr_r1
, extr_r2
;
1334 for (i
= 0; i
< 2; i
++) {
1335 extr_r1
= sextract32(r1
, i
* 16, 16);
1336 extr_r2
= sextract32(r2
, i
* 16, 16);
1337 h
= extr_r1
+ extr_r2
;
1338 ovf
|= ((h
> 0x7fff) || (h
< -0x8000));
1340 ret
|= (h
& 0xffff) << (i
* 16);
1343 env
->PSW_USB_V
= (ovf
<< 31);
1344 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1345 env
->PSW_USB_AV
= (avf
<< 16);
1346 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1351 uint32_t helper_subr_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1354 int64_t mul_res0
= sextract64(r1
, 0, 32);
1355 int64_t mul_res1
= sextract64(r1
, 32, 32);
1356 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1357 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1358 int64_t result0
, result1
;
1359 uint32_t ovf0
, ovf1
;
1360 uint32_t avf0
, avf1
;
1364 result0
= r2_low
- mul_res0
+ 0x8000;
1365 result1
= r2_high
- mul_res1
+ 0x8000;
1367 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1371 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1375 env
->PSW_USB_V
= ovf0
| ovf1
;
1376 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1378 avf0
= result0
* 2u;
1379 avf0
= result0
^ avf0
;
1380 avf1
= result1
* 2u;
1381 avf1
= result1
^ avf1
;
1383 env
->PSW_USB_AV
= avf0
| avf1
;
1384 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1386 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1389 uint32_t helper_subadr_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1392 int64_t mul_res0
= sextract64(r1
, 0, 32);
1393 int64_t mul_res1
= sextract64(r1
, 32, 32);
1394 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1395 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1396 int64_t result0
, result1
;
1397 uint32_t ovf0
, ovf1
;
1398 uint32_t avf0
, avf1
;
1402 result0
= r2_low
+ mul_res0
+ 0x8000;
1403 result1
= r2_high
- mul_res1
+ 0x8000;
1405 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1409 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1413 env
->PSW_USB_V
= ovf0
| ovf1
;
1414 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1416 avf0
= result0
* 2u;
1417 avf0
= result0
^ avf0
;
1418 avf1
= result1
* 2u;
1419 avf1
= result1
^ avf1
;
1421 env
->PSW_USB_AV
= avf0
| avf1
;
1422 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1424 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1427 uint32_t helper_msubr_q(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
1428 uint32_t r3
, uint32_t n
)
1430 int64_t t1
= sextract64(r1
, 0, 32);
1431 int64_t t2
= sextract64(r2
, 0, 32);
1432 int64_t t3
= sextract64(r3
, 0, 32);
1435 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
1438 mul
= (t2
* t3
) << n
;
1441 ret
= t1
- mul
+ 0x8000;
1443 if ((ret
> 0x7fffffffll
) || (ret
< -0x80000000ll
)) {
1444 env
->PSW_USB_V
= (1 << 31);
1445 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1449 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1450 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1452 return ret
& 0xffff0000ll
;
1455 uint32_t helper_sub_b(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1458 int32_t extr_r1
, extr_r2
;
1463 for (i
= 0; i
< 4; i
++) {
1464 extr_r1
= sextract32(r1
, i
* 8, 8);
1465 extr_r2
= sextract32(r2
, i
* 8, 8);
1467 b
= extr_r1
- extr_r2
;
1468 ovf
|= ((b
> 0x7f) || (b
< -0x80));
1470 ret
|= ((b
& 0xff) << (i
*8));
1473 env
->PSW_USB_V
= (ovf
<< 31);
1474 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1475 env
->PSW_USB_AV
= avf
<< 24;
1476 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1481 uint32_t helper_sub_h(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1484 int32_t extr_r1
, extr_r2
;
1489 for (i
= 0; i
< 2; i
++) {
1490 extr_r1
= sextract32(r1
, i
* 16, 16);
1491 extr_r2
= sextract32(r2
, i
* 16, 16);
1492 h
= extr_r1
- extr_r2
;
1493 ovf
|= ((h
> 0x7fff) || (h
< -0x8000));
1495 ret
|= (h
& 0xffff) << (i
* 16);
1498 env
->PSW_USB_V
= (ovf
<< 31);
1499 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1500 env
->PSW_USB_AV
= avf
<< 16;
1501 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1506 uint32_t helper_eq_b(target_ulong r1
, target_ulong r2
)
1513 for (i
= 0; i
< 4; i
++) {
1514 if ((r1
& msk
) == (r2
& msk
)) {
1523 uint32_t helper_eq_h(target_ulong r1
, target_ulong r2
)
1527 if ((r1
& 0xffff) == (r2
& 0xffff)) {
1531 if ((r1
& 0xffff0000) == (r2
& 0xffff0000)) {
1538 uint32_t helper_eqany_b(target_ulong r1
, target_ulong r2
)
1543 for (i
= 0; i
< 4; i
++) {
1544 ret
|= (sextract32(r1
, i
* 8, 8) == sextract32(r2
, i
* 8, 8));
1550 uint32_t helper_eqany_h(target_ulong r1
, target_ulong r2
)
1554 ret
= (sextract32(r1
, 0, 16) == sextract32(r2
, 0, 16));
1555 ret
|= (sextract32(r1
, 16, 16) == sextract32(r2
, 16, 16));
1560 uint32_t helper_lt_b(target_ulong r1
, target_ulong r2
)
1565 for (i
= 0; i
< 4; i
++) {
1566 if (sextract32(r1
, i
* 8, 8) < sextract32(r2
, i
* 8, 8)) {
1567 ret
|= (0xff << (i
* 8));
1574 uint32_t helper_lt_bu(target_ulong r1
, target_ulong r2
)
1579 for (i
= 0; i
< 4; i
++) {
1580 if (extract32(r1
, i
* 8, 8) < extract32(r2
, i
* 8, 8)) {
1581 ret
|= (0xff << (i
* 8));
1588 uint32_t helper_lt_h(target_ulong r1
, target_ulong r2
)
1592 if (sextract32(r1
, 0, 16) < sextract32(r2
, 0, 16)) {
1596 if (sextract32(r1
, 16, 16) < sextract32(r2
, 16, 16)) {
1603 uint32_t helper_lt_hu(target_ulong r1
, target_ulong r2
)
1607 if (extract32(r1
, 0, 16) < extract32(r2
, 0, 16)) {
1611 if (extract32(r1
, 16, 16) < extract32(r2
, 16, 16)) {
1618 #define EXTREMA_H_B(name, op) \
1619 uint32_t helper_##name ##_b(target_ulong r1, target_ulong r2) \
1621 int32_t i, extr_r1, extr_r2; \
1624 for (i = 0; i < 4; i++) { \
1625 extr_r1 = sextract32(r1, i * 8, 8); \
1626 extr_r2 = sextract32(r2, i * 8, 8); \
1627 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1628 ret |= (extr_r1 & 0xff) << (i * 8); \
1633 uint32_t helper_##name ##_bu(target_ulong r1, target_ulong r2)\
1636 uint32_t extr_r1, extr_r2; \
1639 for (i = 0; i < 4; i++) { \
1640 extr_r1 = extract32(r1, i * 8, 8); \
1641 extr_r2 = extract32(r2, i * 8, 8); \
1642 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1643 ret |= (extr_r1 & 0xff) << (i * 8); \
1648 uint32_t helper_##name ##_h(target_ulong r1, target_ulong r2) \
1650 int32_t extr_r1, extr_r2; \
1653 extr_r1 = sextract32(r1, 0, 16); \
1654 extr_r2 = sextract32(r2, 0, 16); \
1655 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1656 ret = ret & 0xffff; \
1658 extr_r1 = sextract32(r1, 16, 16); \
1659 extr_r2 = sextract32(r2, 16, 16); \
1660 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1661 ret |= extr_r1 << 16; \
1666 uint32_t helper_##name ##_hu(target_ulong r1, target_ulong r2)\
1668 uint32_t extr_r1, extr_r2; \
1671 extr_r1 = extract32(r1, 0, 16); \
1672 extr_r2 = extract32(r2, 0, 16); \
1673 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1674 ret = ret & 0xffff; \
1676 extr_r1 = extract32(r1, 16, 16); \
1677 extr_r2 = extract32(r2, 16, 16); \
1678 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1679 ret |= extr_r1 << (16); \
1684 uint64_t helper_ix##name(uint64_t r1, uint32_t r2) \
1686 int64_t r2l, r2h, r1hl; \
1689 ret = ((r1 + 2) & 0xffff); \
1690 r2l = sextract64(r2, 0, 16); \
1691 r2h = sextract64(r2, 16, 16); \
1692 r1hl = sextract64(r1, 32, 16); \
1694 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
1695 ret |= (r2l & 0xffff) << 32; \
1696 ret |= extract64(r1, 0, 16) << 16; \
1697 } else if ((r2h op r2l) && (r2h op r1hl)) { \
1698 ret |= extract64(r2, 16, 16) << 32; \
1699 ret |= extract64(r1 + 1, 0, 16) << 16; \
1701 ret |= r1 & 0xffffffff0000ull; \
1706 uint64_t helper_ix##name ##_u(uint64_t r1, uint32_t r2) \
1708 int64_t r2l, r2h, r1hl; \
1711 ret = ((r1 + 2) & 0xffff); \
1712 r2l = extract64(r2, 0, 16); \
1713 r2h = extract64(r2, 16, 16); \
1714 r1hl = extract64(r1, 32, 16); \
1716 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
1717 ret |= (r2l & 0xffff) << 32; \
1718 ret |= extract64(r1, 0, 16) << 16; \
1719 } else if ((r2h op r2l) && (r2h op r1hl)) { \
1720 ret |= extract64(r2, 16, 16) << 32; \
1721 ret |= extract64(r1 + 1, 0, 16) << 16; \
1723 ret |= r1 & 0xffffffff0000ull; \
1733 uint32_t helper_clo(target_ulong r1
)
1738 uint32_t helper_clo_h(target_ulong r1
)
1740 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1741 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1743 ret_hw0
= clo32(ret_hw0
<< 16);
1744 ret_hw1
= clo32(ret_hw1
<< 16);
1753 return ret_hw0
| (ret_hw1
<< 16);
1756 uint32_t helper_clz(target_ulong r1
)
1761 uint32_t helper_clz_h(target_ulong r1
)
1763 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1764 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1766 ret_hw0
= clz32(ret_hw0
<< 16);
1767 ret_hw1
= clz32(ret_hw1
<< 16);
1776 return ret_hw0
| (ret_hw1
<< 16);
1779 uint32_t helper_cls(target_ulong r1
)
1784 uint32_t helper_cls_h(target_ulong r1
)
1786 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1787 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1789 ret_hw0
= clrsb32(ret_hw0
<< 16);
1790 ret_hw1
= clrsb32(ret_hw1
<< 16);
1799 return ret_hw0
| (ret_hw1
<< 16);
1802 uint32_t helper_sh(target_ulong r1
, target_ulong r2
)
1804 int32_t shift_count
= sextract32(r2
, 0, 6);
1806 if (shift_count
== -32) {
1808 } else if (shift_count
< 0) {
1809 return r1
>> -shift_count
;
1811 return r1
<< shift_count
;
1815 uint32_t helper_sh_h(target_ulong r1
, target_ulong r2
)
1817 int32_t ret_hw0
, ret_hw1
;
1818 int32_t shift_count
;
1820 shift_count
= sextract32(r2
, 0, 5);
1822 if (shift_count
== -16) {
1824 } else if (shift_count
< 0) {
1825 ret_hw0
= extract32(r1
, 0, 16) >> -shift_count
;
1826 ret_hw1
= extract32(r1
, 16, 16) >> -shift_count
;
1827 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1829 ret_hw0
= extract32(r1
, 0, 16) << shift_count
;
1830 ret_hw1
= extract32(r1
, 16, 16) << shift_count
;
1831 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1835 uint32_t helper_sha(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1837 int32_t shift_count
;
1841 shift_count
= sextract32(r2
, 0, 6);
1842 t1
= sextract32(r1
, 0, 32);
1844 if (shift_count
== 0) {
1845 env
->PSW_USB_C
= env
->PSW_USB_V
= 0;
1847 } else if (shift_count
== -32) {
1848 env
->PSW_USB_C
= r1
;
1851 } else if (shift_count
> 0) {
1852 result
= t1
<< shift_count
;
1854 env
->PSW_USB_C
= ((result
& 0xffffffff00000000ULL
) != 0);
1856 env
->PSW_USB_V
= (((result
> 0x7fffffffLL
) ||
1857 (result
< -0x80000000LL
)) << 31);
1859 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1860 ret
= (uint32_t)result
;
1863 env
->PSW_USB_C
= (r1
& ((1 << -shift_count
) - 1));
1864 ret
= t1
>> -shift_count
;
1867 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1868 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1873 uint32_t helper_sha_h(target_ulong r1
, target_ulong r2
)
1875 int32_t shift_count
;
1876 int32_t ret_hw0
, ret_hw1
;
1878 shift_count
= sextract32(r2
, 0, 5);
1880 if (shift_count
== 0) {
1882 } else if (shift_count
< 0) {
1883 ret_hw0
= sextract32(r1
, 0, 16) >> -shift_count
;
1884 ret_hw1
= sextract32(r1
, 16, 16) >> -shift_count
;
1885 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1887 ret_hw0
= sextract32(r1
, 0, 16) << shift_count
;
1888 ret_hw1
= sextract32(r1
, 16, 16) << shift_count
;
1889 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1893 uint32_t helper_bmerge(target_ulong r1
, target_ulong r2
)
1898 for (i
= 0; i
< 16; i
++) {
1899 ret
|= (r1
& 1) << (2 * i
+ 1);
1900 ret
|= (r2
& 1) << (2 * i
);
1907 uint64_t helper_bsplit(uint32_t r1
)
1913 for (i
= 0; i
< 32; i
= i
+ 2) {
1915 ret
|= (r1
& 1) << (i
/2);
1918 ret
|= (uint64_t)(r1
& 1) << (i
/2 + 32);
1924 uint32_t helper_parity(target_ulong r1
)
1931 for (i
= 0; i
< 8; i
++) {
1937 for (i
= 0; i
< 8; i
++) {
1944 for (i
= 0; i
< 8; i
++) {
1951 for (i
= 0; i
< 8; i
++) {
1960 uint32_t helper_pack(uint32_t carry
, uint32_t r1_low
, uint32_t r1_high
,
1964 int32_t fp_exp
, fp_frac
, temp_exp
, fp_exp_frac
;
1965 int32_t int_exp
= r1_high
;
1966 int32_t int_mant
= r1_low
;
1967 uint32_t flag_rnd
= (int_mant
& (1 << 7)) && (
1968 (int_mant
& (1 << 8)) ||
1969 (int_mant
& 0x7f) ||
1971 if (((int_mant
& (1<<31)) == 0) && (int_exp
== 255)) {
1973 fp_frac
= extract32(int_mant
, 8, 23);
1974 } else if ((int_mant
& (1<<31)) && (int_exp
>= 127)) {
1977 } else if ((int_mant
& (1<<31)) && (int_exp
<= -128)) {
1980 } else if (int_mant
== 0) {
1984 if (((int_mant
& (1 << 31)) == 0)) {
1987 temp_exp
= int_exp
+ 128;
1989 fp_exp_frac
= (((temp_exp
& 0xff) << 23) |
1990 extract32(int_mant
, 8, 23))
1992 fp_exp
= extract32(fp_exp_frac
, 23, 8);
1993 fp_frac
= extract32(fp_exp_frac
, 0, 23);
1995 ret
= r2
& (1 << 31);
1996 ret
= ret
+ (fp_exp
<< 23);
1997 ret
= ret
+ (fp_frac
& 0x7fffff);
2002 uint64_t helper_unpack(target_ulong arg1
)
2004 int32_t fp_exp
= extract32(arg1
, 23, 8);
2005 int32_t fp_frac
= extract32(arg1
, 0, 23);
2007 int32_t int_exp
, int_mant
;
2009 if (fp_exp
== 255) {
2011 int_mant
= (fp_frac
<< 7);
2012 } else if ((fp_exp
== 0) && (fp_frac
== 0)) {
2015 } else if ((fp_exp
== 0) && (fp_frac
!= 0)) {
2017 int_mant
= (fp_frac
<< 7);
2019 int_exp
= fp_exp
- 127;
2020 int_mant
= (fp_frac
<< 7);
2021 int_mant
|= (1 << 30);
2030 uint64_t helper_dvinit_b_13(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2033 int32_t abs_sig_dividend
, abs_divisor
;
2035 ret
= sextract32(r1
, 0, 32);
2037 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2041 abs_sig_dividend
= abs((int32_t)r1
) >> 8;
2042 abs_divisor
= abs((int32_t)r2
);
2044 ofv if (a/b >= 255) <=> (a/255 >= b) */
2045 env
->PSW_USB_V
= (abs_sig_dividend
>= abs_divisor
) << 31;
2046 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2047 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2048 env
->PSW_USB_AV
= 0;
2053 uint64_t helper_dvinit_b_131(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2055 uint64_t ret
= sextract32(r1
, 0, 32);
2058 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2062 env
->PSW_USB_V
= ((r2
== 0) || ((r2
== 0xffffffff) && (r1
== 0xffffff80)));
2063 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2064 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2065 env
->PSW_USB_AV
= 0;
2070 uint64_t helper_dvinit_h_13(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2073 int32_t abs_sig_dividend
, abs_divisor
;
2075 ret
= sextract32(r1
, 0, 32);
2077 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2081 abs_sig_dividend
= abs((int32_t)r1
) >> 16;
2082 abs_divisor
= abs((int32_t)r2
);
2084 ofv if (a/b >= 0xffff) <=> (a/0xffff >= b) */
2085 env
->PSW_USB_V
= (abs_sig_dividend
>= abs_divisor
) << 31;
2086 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2087 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2088 env
->PSW_USB_AV
= 0;
2093 uint64_t helper_dvinit_h_131(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2095 uint64_t ret
= sextract32(r1
, 0, 32);
2098 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2102 env
->PSW_USB_V
= ((r2
== 0) || ((r2
== 0xffffffff) && (r1
== 0xffff8000)));
2103 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2104 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2105 env
->PSW_USB_AV
= 0;
2110 uint64_t helper_dvadj(uint64_t r1
, uint32_t r2
)
2112 int32_t x_sign
= (r1
>> 63);
2113 int32_t q_sign
= x_sign
^ (r2
>> 31);
2114 int32_t eq_pos
= x_sign
& ((r1
>> 32) == r2
);
2115 int32_t eq_neg
= x_sign
& ((r1
>> 32) == -r2
);
2117 uint64_t ret
, remainder
;
2119 if ((q_sign
& ~eq_neg
) | eq_pos
) {
2120 quotient
= (r1
+ 1) & 0xffffffff;
2122 quotient
= r1
& 0xffffffff;
2125 if (eq_pos
| eq_neg
) {
2128 remainder
= (r1
& 0xffffffff00000000ull
);
2130 ret
= remainder
|quotient
;
2134 uint64_t helper_dvstep(uint64_t r1
, uint32_t r2
)
2136 int32_t dividend_sign
= extract64(r1
, 63, 1);
2137 int32_t divisor_sign
= extract32(r2
, 31, 1);
2138 int32_t quotient_sign
= (dividend_sign
!= divisor_sign
);
2139 int32_t addend
, dividend_quotient
, remainder
;
2142 if (quotient_sign
) {
2147 dividend_quotient
= (int32_t)r1
;
2148 remainder
= (int32_t)(r1
>> 32);
2150 for (i
= 0; i
< 8; i
++) {
2151 remainder
= (remainder
<< 1) | extract32(dividend_quotient
, 31, 1);
2152 dividend_quotient
<<= 1;
2153 temp
= remainder
+ addend
;
2154 if ((temp
< 0) == dividend_sign
) {
2157 if (((temp
< 0) == dividend_sign
)) {
2158 dividend_quotient
= dividend_quotient
| !quotient_sign
;
2160 dividend_quotient
= dividend_quotient
| quotient_sign
;
2163 return ((uint64_t)remainder
<< 32) | (uint32_t)dividend_quotient
;
2166 uint64_t helper_dvstep_u(uint64_t r1
, uint32_t r2
)
2168 int32_t dividend_quotient
= extract64(r1
, 0, 32);
2169 int64_t remainder
= extract64(r1
, 32, 32);
2172 for (i
= 0; i
< 8; i
++) {
2173 remainder
= (remainder
<< 1) | extract32(dividend_quotient
, 31, 1);
2174 dividend_quotient
<<= 1;
2175 temp
= (remainder
& 0xffffffff) - r2
;
2179 dividend_quotient
= dividend_quotient
| !(temp
< 0);
2181 return ((uint64_t)remainder
<< 32) | (uint32_t)dividend_quotient
;
2184 uint64_t helper_divide(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2186 int32_t quotient
, remainder
;
2187 int32_t dividend
= (int32_t)r1
;
2188 int32_t divisor
= (int32_t)r2
;
2191 if (dividend
>= 0) {
2192 quotient
= 0x7fffffff;
2195 quotient
= 0x80000000;
2198 env
->PSW_USB_V
= (1 << 31);
2199 } else if ((divisor
== 0xffffffff) && (dividend
== 0x80000000)) {
2200 quotient
= 0x7fffffff;
2202 env
->PSW_USB_V
= (1 << 31);
2204 remainder
= dividend
% divisor
;
2205 quotient
= (dividend
- remainder
)/divisor
;
2208 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2209 env
->PSW_USB_AV
= 0;
2210 return ((uint64_t)remainder
<< 32) | (uint32_t)quotient
;
2213 uint64_t helper_divide_u(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2215 uint32_t quotient
, remainder
;
2216 uint32_t dividend
= r1
;
2217 uint32_t divisor
= r2
;
2220 quotient
= 0xffffffff;
2222 env
->PSW_USB_V
= (1 << 31);
2224 remainder
= dividend
% divisor
;
2225 quotient
= (dividend
- remainder
)/divisor
;
2228 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2229 env
->PSW_USB_AV
= 0;
2230 return ((uint64_t)remainder
<< 32) | quotient
;
2233 uint64_t helper_mul_h(uint32_t arg00
, uint32_t arg01
,
2234 uint32_t arg10
, uint32_t arg11
, uint32_t n
)
2237 uint32_t result0
, result1
;
2239 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2240 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2241 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2242 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2244 result1
= 0x7fffffff;
2246 result1
= (((uint32_t)(arg00
* arg10
)) << n
);
2249 result0
= 0x7fffffff;
2251 result0
= (((uint32_t)(arg01
* arg11
)) << n
);
2253 ret
= (((uint64_t)result1
<< 32)) | result0
;
2257 uint64_t helper_mulm_h(uint32_t arg00
, uint32_t arg01
,
2258 uint32_t arg10
, uint32_t arg11
, uint32_t n
)
2261 int64_t result0
, result1
;
2263 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2264 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2265 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2266 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2269 result1
= 0x7fffffff;
2271 result1
= (((int32_t)arg00
* (int32_t)arg10
) << n
);
2274 result0
= 0x7fffffff;
2276 result0
= (((int32_t)arg01
* (int32_t)arg11
) << n
);
2278 ret
= (result1
+ result0
);
2282 uint32_t helper_mulr_h(uint32_t arg00
, uint32_t arg01
,
2283 uint32_t arg10
, uint32_t arg11
, uint32_t n
)
2285 uint32_t result0
, result1
;
2287 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2288 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2289 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2290 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2293 result1
= 0x7fffffff;
2295 result1
= ((arg00
* arg10
) << n
) + 0x8000;
2298 result0
= 0x7fffffff;
2300 result0
= ((arg01
* arg11
) << n
) + 0x8000;
2302 return (result1
& 0xffff0000) | (result0
>> 16);
2305 uint32_t helper_crc32(uint32_t arg0
, uint32_t arg1
)
2309 stl_be_p(buf
, arg0
);
2311 ret
= crc32(arg1
, buf
, 4);
2315 /* context save area (CSA) related helpers */
2317 static int cdc_increment(target_ulong
*psw
)
2319 if ((*psw
& MASK_PSW_CDC
) == 0x7f) {
2324 /* check for overflow */
2325 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2326 int mask
= (1u << (7 - lo
)) - 1;
2327 int count
= *psw
& mask
;
2335 static int cdc_decrement(target_ulong
*psw
)
2337 if ((*psw
& MASK_PSW_CDC
) == 0x7f) {
2340 /* check for underflow */
2341 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2342 int mask
= (1u << (7 - lo
)) - 1;
2343 int count
= *psw
& mask
;
2351 static bool cdc_zero(target_ulong
*psw
)
2353 int cdc
= *psw
& MASK_PSW_CDC
;
2354 /* Returns TRUE if PSW.CDC.COUNT == 0 or if PSW.CDC ==
2355 7'b1111111, otherwise returns FALSE. */
2359 /* find CDC.COUNT */
2360 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2361 int mask
= (1u << (7 - lo
)) - 1;
2362 int count
= *psw
& mask
;
2366 static void save_context_upper(CPUTriCoreState
*env
, int ea
)
2368 cpu_stl_data(env
, ea
, env
->PCXI
);
2369 cpu_stl_data(env
, ea
+4, psw_read(env
));
2370 cpu_stl_data(env
, ea
+8, env
->gpr_a
[10]);
2371 cpu_stl_data(env
, ea
+12, env
->gpr_a
[11]);
2372 cpu_stl_data(env
, ea
+16, env
->gpr_d
[8]);
2373 cpu_stl_data(env
, ea
+20, env
->gpr_d
[9]);
2374 cpu_stl_data(env
, ea
+24, env
->gpr_d
[10]);
2375 cpu_stl_data(env
, ea
+28, env
->gpr_d
[11]);
2376 cpu_stl_data(env
, ea
+32, env
->gpr_a
[12]);
2377 cpu_stl_data(env
, ea
+36, env
->gpr_a
[13]);
2378 cpu_stl_data(env
, ea
+40, env
->gpr_a
[14]);
2379 cpu_stl_data(env
, ea
+44, env
->gpr_a
[15]);
2380 cpu_stl_data(env
, ea
+48, env
->gpr_d
[12]);
2381 cpu_stl_data(env
, ea
+52, env
->gpr_d
[13]);
2382 cpu_stl_data(env
, ea
+56, env
->gpr_d
[14]);
2383 cpu_stl_data(env
, ea
+60, env
->gpr_d
[15]);
2386 static void save_context_lower(CPUTriCoreState
*env
, int ea
)
2388 cpu_stl_data(env
, ea
, env
->PCXI
);
2389 cpu_stl_data(env
, ea
+4, env
->gpr_a
[11]);
2390 cpu_stl_data(env
, ea
+8, env
->gpr_a
[2]);
2391 cpu_stl_data(env
, ea
+12, env
->gpr_a
[3]);
2392 cpu_stl_data(env
, ea
+16, env
->gpr_d
[0]);
2393 cpu_stl_data(env
, ea
+20, env
->gpr_d
[1]);
2394 cpu_stl_data(env
, ea
+24, env
->gpr_d
[2]);
2395 cpu_stl_data(env
, ea
+28, env
->gpr_d
[3]);
2396 cpu_stl_data(env
, ea
+32, env
->gpr_a
[4]);
2397 cpu_stl_data(env
, ea
+36, env
->gpr_a
[5]);
2398 cpu_stl_data(env
, ea
+40, env
->gpr_a
[6]);
2399 cpu_stl_data(env
, ea
+44, env
->gpr_a
[7]);
2400 cpu_stl_data(env
, ea
+48, env
->gpr_d
[4]);
2401 cpu_stl_data(env
, ea
+52, env
->gpr_d
[5]);
2402 cpu_stl_data(env
, ea
+56, env
->gpr_d
[6]);
2403 cpu_stl_data(env
, ea
+60, env
->gpr_d
[7]);
2406 static void restore_context_upper(CPUTriCoreState
*env
, int ea
,
2407 target_ulong
*new_PCXI
, target_ulong
*new_PSW
)
2409 *new_PCXI
= cpu_ldl_data(env
, ea
);
2410 *new_PSW
= cpu_ldl_data(env
, ea
+4);
2411 env
->gpr_a
[10] = cpu_ldl_data(env
, ea
+8);
2412 env
->gpr_a
[11] = cpu_ldl_data(env
, ea
+12);
2413 env
->gpr_d
[8] = cpu_ldl_data(env
, ea
+16);
2414 env
->gpr_d
[9] = cpu_ldl_data(env
, ea
+20);
2415 env
->gpr_d
[10] = cpu_ldl_data(env
, ea
+24);
2416 env
->gpr_d
[11] = cpu_ldl_data(env
, ea
+28);
2417 env
->gpr_a
[12] = cpu_ldl_data(env
, ea
+32);
2418 env
->gpr_a
[13] = cpu_ldl_data(env
, ea
+36);
2419 env
->gpr_a
[14] = cpu_ldl_data(env
, ea
+40);
2420 env
->gpr_a
[15] = cpu_ldl_data(env
, ea
+44);
2421 env
->gpr_d
[12] = cpu_ldl_data(env
, ea
+48);
2422 env
->gpr_d
[13] = cpu_ldl_data(env
, ea
+52);
2423 env
->gpr_d
[14] = cpu_ldl_data(env
, ea
+56);
2424 env
->gpr_d
[15] = cpu_ldl_data(env
, ea
+60);
2427 static void restore_context_lower(CPUTriCoreState
*env
, int ea
,
2428 target_ulong
*ra
, target_ulong
*pcxi
)
2430 *pcxi
= cpu_ldl_data(env
, ea
);
2431 *ra
= cpu_ldl_data(env
, ea
+4);
2432 env
->gpr_a
[2] = cpu_ldl_data(env
, ea
+8);
2433 env
->gpr_a
[3] = cpu_ldl_data(env
, ea
+12);
2434 env
->gpr_d
[0] = cpu_ldl_data(env
, ea
+16);
2435 env
->gpr_d
[1] = cpu_ldl_data(env
, ea
+20);
2436 env
->gpr_d
[2] = cpu_ldl_data(env
, ea
+24);
2437 env
->gpr_d
[3] = cpu_ldl_data(env
, ea
+28);
2438 env
->gpr_a
[4] = cpu_ldl_data(env
, ea
+32);
2439 env
->gpr_a
[5] = cpu_ldl_data(env
, ea
+36);
2440 env
->gpr_a
[6] = cpu_ldl_data(env
, ea
+40);
2441 env
->gpr_a
[7] = cpu_ldl_data(env
, ea
+44);
2442 env
->gpr_d
[4] = cpu_ldl_data(env
, ea
+48);
2443 env
->gpr_d
[5] = cpu_ldl_data(env
, ea
+52);
2444 env
->gpr_d
[6] = cpu_ldl_data(env
, ea
+56);
2445 env
->gpr_d
[7] = cpu_ldl_data(env
, ea
+60);
2448 void helper_call(CPUTriCoreState
*env
, uint32_t next_pc
)
2450 target_ulong tmp_FCX
;
2452 target_ulong new_FCX
;
2455 psw
= psw_read(env
);
2456 /* if (FCX == 0) trap(FCU); */
2457 if (env
->FCX
== 0) {
2459 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2461 /* if (PSW.CDE) then if (cdc_increment()) then trap(CDO); */
2462 if (psw
& MASK_PSW_CDE
) {
2463 if (cdc_increment(&psw
)) {
2465 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CDO
, GETPC());
2469 psw
|= MASK_PSW_CDE
;
2470 /* tmp_FCX = FCX; */
2472 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2473 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2474 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2475 /* new_FCX = M(EA, word); */
2476 new_FCX
= cpu_ldl_data(env
, ea
);
2477 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2478 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2480 save_context_upper(env
, ea
);
2482 /* PCXI.PCPN = ICR.CCPN; */
2483 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2484 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2485 /* PCXI.PIE = ICR.IE; */
2486 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
2487 ((env
->ICR
& MASK_ICR_IE
) << 15));
2489 env
->PCXI
|= MASK_PCXI_UL
;
2491 /* PCXI[19: 0] = FCX[19: 0]; */
2492 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2493 /* FCX[19: 0] = new_FCX[19: 0]; */
2494 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2495 /* A[11] = next_pc[31: 0]; */
2496 env
->gpr_a
[11] = next_pc
;
2498 /* if (tmp_FCX == LCX) trap(FCD);*/
2499 if (tmp_FCX
== env
->LCX
) {
2501 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2503 psw_write(env
, psw
);
2506 void helper_ret(CPUTriCoreState
*env
)
2509 target_ulong new_PCXI
;
2510 target_ulong new_PSW
, psw
;
2512 psw
= psw_read(env
);
2513 /* if (PSW.CDE) then if (cdc_decrement()) then trap(CDU);*/
2514 if (psw
& MASK_PSW_CDE
) {
2515 if (cdc_decrement(&psw
)) {
2517 psw_write(env
, psw
);
2518 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CDU
, GETPC());
2521 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2522 if ((env
->PCXI
& 0xfffff) == 0) {
2524 psw_write(env
, psw
);
2525 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2527 /* if (PCXI.UL == 0) then trap(CTYP); */
2528 if ((env
->PCXI
& MASK_PCXI_UL
) == 0) {
2530 cdc_increment(&psw
); /* restore to the start of helper */
2531 psw_write(env
, psw
);
2532 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2534 /* PC = {A11 [31: 1], 1’b0}; */
2535 env
->PC
= env
->gpr_a
[11] & 0xfffffffe;
2537 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
2538 ea
= ((env
->PCXI
& MASK_PCXI_PCXS
) << 12) +
2539 ((env
->PCXI
& MASK_PCXI_PCXO
) << 6);
2540 /* {new_PCXI, new_PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2541 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2542 restore_context_upper(env
, ea
, &new_PCXI
, &new_PSW
);
2543 /* M(EA, word) = FCX; */
2544 cpu_stl_data(env
, ea
, env
->FCX
);
2545 /* FCX[19: 0] = PCXI[19: 0]; */
2546 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2547 /* PCXI = new_PCXI; */
2548 env
->PCXI
= new_PCXI
;
2550 if (tricore_feature(env
, TRICORE_FEATURE_13
)) {
2552 psw_write(env
, new_PSW
);
2554 /* PSW = {new_PSW[31:26], PSW[25:24], new_PSW[23:0]}; */
2555 psw_write(env
, (new_PSW
& ~(0x3000000)) + (psw
& (0x3000000)));
2559 void helper_bisr(CPUTriCoreState
*env
, uint32_t const9
)
2561 target_ulong tmp_FCX
;
2563 target_ulong new_FCX
;
2565 if (env
->FCX
== 0) {
2567 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2571 ea
= ((env
->FCX
& 0xf0000) << 12) + ((env
->FCX
& 0xffff) << 6);
2573 /* new_FCX = M(EA, word); */
2574 new_FCX
= cpu_ldl_data(env
, ea
);
2575 /* M(EA, 16 * word) = {PCXI, A[11], A[2], A[3], D[0], D[1], D[2], D[3], A[4]
2576 , A[5], A[6], A[7], D[4], D[5], D[6], D[7]}; */
2577 save_context_lower(env
, ea
);
2580 /* PCXI.PCPN = ICR.CCPN */
2581 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2582 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2583 /* PCXI.PIE = ICR.IE */
2584 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
2585 ((env
->ICR
& MASK_ICR_IE
) << 15));
2587 env
->PCXI
&= ~(MASK_PCXI_UL
);
2588 /* PCXI[19: 0] = FCX[19: 0] */
2589 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2590 /* FXC[19: 0] = new_FCX[19: 0] */
2591 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2593 env
->ICR
|= MASK_ICR_IE
;
2595 env
->ICR
|= const9
; /* ICR.CCPN = const9[7: 0];*/
2597 if (tmp_FCX
== env
->LCX
) {
2599 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2603 void helper_rfe(CPUTriCoreState
*env
)
2606 target_ulong new_PCXI
;
2607 target_ulong new_PSW
;
2608 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2609 if ((env
->PCXI
& 0xfffff) == 0) {
2610 /* raise csu trap */
2611 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2613 /* if (PCXI.UL == 0) then trap(CTYP); */
2614 if ((env
->PCXI
& MASK_PCXI_UL
) == 0) {
2615 /* raise CTYP trap */
2616 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2618 /* if (!cdc_zero() AND PSW.CDE) then trap(NEST); */
2619 if (!cdc_zero(&(env
->PSW
)) && (env
->PSW
& MASK_PSW_CDE
)) {
2620 /* raise NEST trap */
2621 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_NEST
, GETPC());
2623 env
->PC
= env
->gpr_a
[11] & ~0x1;
2624 /* ICR.IE = PCXI.PIE; */
2625 env
->ICR
= (env
->ICR
& ~MASK_ICR_IE
) + ((env
->PCXI
& MASK_PCXI_PIE
) >> 15);
2626 /* ICR.CCPN = PCXI.PCPN; */
2627 env
->ICR
= (env
->ICR
& ~MASK_ICR_CCPN
) +
2628 ((env
->PCXI
& MASK_PCXI_PCPN
) >> 24);
2629 /*EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0};*/
2630 ea
= ((env
->PCXI
& MASK_PCXI_PCXS
) << 12) +
2631 ((env
->PCXI
& MASK_PCXI_PCXO
) << 6);
2632 /*{new_PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2633 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2634 restore_context_upper(env
, ea
, &new_PCXI
, &new_PSW
);
2635 /* M(EA, word) = FCX;*/
2636 cpu_stl_data(env
, ea
, env
->FCX
);
2637 /* FCX[19: 0] = PCXI[19: 0]; */
2638 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2639 /* PCXI = new_PCXI; */
2640 env
->PCXI
= new_PCXI
;
2642 psw_write(env
, new_PSW
);
2645 void helper_rfm(CPUTriCoreState
*env
)
2647 env
->PC
= (env
->gpr_a
[11] & ~0x1);
2648 /* ICR.IE = PCXI.PIE; */
2649 env
->ICR
= (env
->ICR
& ~MASK_ICR_IE
) |
2650 ((env
->PCXI
& MASK_PCXI_PIE
) >> 15);
2651 /* ICR.CCPN = PCXI.PCPN; */
2652 env
->ICR
= (env
->ICR
& ~MASK_ICR_CCPN
) |
2653 ((env
->PCXI
& MASK_PCXI_PCPN
) >> 24);
2654 /* {PCXI, PSW, A[10], A[11]} = M(DCX, 4 * word); */
2655 env
->PCXI
= cpu_ldl_data(env
, env
->DCX
);
2656 psw_write(env
, cpu_ldl_data(env
, env
->DCX
+4));
2657 env
->gpr_a
[10] = cpu_ldl_data(env
, env
->DCX
+8);
2658 env
->gpr_a
[11] = cpu_ldl_data(env
, env
->DCX
+12);
2660 if (tricore_feature(env
, TRICORE_FEATURE_131
)) {
2665 void helper_ldlcx(CPUTriCoreState
*env
, uint32_t ea
)
2668 /* insn doesn't load PCXI and RA */
2669 restore_context_lower(env
, ea
, &dummy
, &dummy
);
2672 void helper_lducx(CPUTriCoreState
*env
, uint32_t ea
)
2675 /* insn doesn't load PCXI and PSW */
2676 restore_context_upper(env
, ea
, &dummy
, &dummy
);
2679 void helper_stlcx(CPUTriCoreState
*env
, uint32_t ea
)
2681 save_context_lower(env
, ea
);
2684 void helper_stucx(CPUTriCoreState
*env
, uint32_t ea
)
2686 save_context_upper(env
, ea
);
2689 void helper_svlcx(CPUTriCoreState
*env
)
2691 target_ulong tmp_FCX
;
2693 target_ulong new_FCX
;
2695 if (env
->FCX
== 0) {
2697 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2699 /* tmp_FCX = FCX; */
2701 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2702 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2703 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2704 /* new_FCX = M(EA, word); */
2705 new_FCX
= cpu_ldl_data(env
, ea
);
2706 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2707 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2709 save_context_lower(env
, ea
);
2711 /* PCXI.PCPN = ICR.CCPN; */
2712 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2713 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2714 /* PCXI.PIE = ICR.IE; */
2715 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
2716 ((env
->ICR
& MASK_ICR_IE
) << 15));
2718 env
->PCXI
&= ~MASK_PCXI_UL
;
2720 /* PCXI[19: 0] = FCX[19: 0]; */
2721 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2722 /* FCX[19: 0] = new_FCX[19: 0]; */
2723 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2725 /* if (tmp_FCX == LCX) trap(FCD);*/
2726 if (tmp_FCX
== env
->LCX
) {
2728 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2732 void helper_svucx(CPUTriCoreState
*env
)
2734 target_ulong tmp_FCX
;
2736 target_ulong new_FCX
;
2738 if (env
->FCX
== 0) {
2740 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2742 /* tmp_FCX = FCX; */
2744 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2745 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2746 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2747 /* new_FCX = M(EA, word); */
2748 new_FCX
= cpu_ldl_data(env
, ea
);
2749 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2750 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2752 save_context_upper(env
, ea
);
2754 /* PCXI.PCPN = ICR.CCPN; */
2755 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2756 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2757 /* PCXI.PIE = ICR.IE; */
2758 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
2759 ((env
->ICR
& MASK_ICR_IE
) << 15));
2761 env
->PCXI
|= MASK_PCXI_UL
;
2763 /* PCXI[19: 0] = FCX[19: 0]; */
2764 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2765 /* FCX[19: 0] = new_FCX[19: 0]; */
2766 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2768 /* if (tmp_FCX == LCX) trap(FCD);*/
2769 if (tmp_FCX
== env
->LCX
) {
2771 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2775 void helper_rslcx(CPUTriCoreState
*env
)
2778 target_ulong new_PCXI
;
2779 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2780 if ((env
->PCXI
& 0xfffff) == 0) {
2782 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2784 /* if (PCXI.UL == 1) then trap(CTYP); */
2785 if ((env
->PCXI
& MASK_PCXI_UL
) != 0) {
2787 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2789 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
2790 ea
= ((env
->PCXI
& MASK_PCXI_PCXS
) << 12) +
2791 ((env
->PCXI
& MASK_PCXI_PCXO
) << 6);
2792 /* {new_PCXI, A[11], A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2793 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2794 restore_context_lower(env
, ea
, &env
->gpr_a
[11], &new_PCXI
);
2795 /* M(EA, word) = FCX; */
2796 cpu_stl_data(env
, ea
, env
->FCX
);
2797 /* M(EA, word) = FCX; */
2798 cpu_stl_data(env
, ea
, env
->FCX
);
2799 /* FCX[19: 0] = PCXI[19: 0]; */
2800 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2801 /* PCXI = new_PCXI; */
2802 env
->PCXI
= new_PCXI
;
2805 void helper_psw_write(CPUTriCoreState
*env
, uint32_t arg
)
2807 psw_write(env
, arg
);
2810 uint32_t helper_psw_read(CPUTriCoreState
*env
)
2812 return psw_read(env
);
2816 static inline void QEMU_NORETURN
do_raise_exception_err(CPUTriCoreState
*env
,
2821 CPUState
*cs
= CPU(tricore_env_get_cpu(env
));
2822 cs
->exception_index
= exception
;
2823 env
->error_code
= error_code
;
2826 /* now we have a real cpu fault */
2827 cpu_restore_state(cs
, pc
);
2833 void tlb_fill(CPUState
*cs
, target_ulong addr
, int is_write
, int mmu_idx
,
2837 ret
= cpu_tricore_handle_mmu_fault(cs
, addr
, is_write
, mmu_idx
);
2839 TriCoreCPU
*cpu
= TRICORE_CPU(cs
);
2840 CPUTriCoreState
*env
= &cpu
->env
;
2841 do_raise_exception_err(env
, cs
->exception_index
,
2842 env
->error_code
, retaddr
);