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 */
29 void raise_exception_sync_internal(CPUTriCoreState
*env
, uint32_t class, int tin
,
30 uintptr_t pc
, uint32_t fcd_pc
)
32 CPUState
*cs
= env_cpu(env
);
33 /* in case we come from a helper-call we need to restore the PC */
34 cpu_restore_state(cs
, pc
);
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 pcxi_set_pie(env
, icr_get_ie(env
));
89 /* PCXI.PCPN = ICR.CCPN */
90 pcxi_set_pcpn(env
, icr_get_ccpn(env
));
91 /* Update PC using the trap vector table */
92 env
->PC
= env
->BTV
| (class << 5);
97 void helper_raise_exception_sync(CPUTriCoreState
*env
, uint32_t class,
100 raise_exception_sync_internal(env
, class, tin
, 0, 0);
103 static void raise_exception_sync_helper(CPUTriCoreState
*env
, uint32_t class,
104 uint32_t tin
, uintptr_t pc
)
106 raise_exception_sync_internal(env
, class, tin
, pc
, 0);
109 /* Addressing mode helper */
111 static uint16_t reverse16(uint16_t val
)
113 uint8_t high
= (uint8_t)(val
>> 8);
114 uint8_t low
= (uint8_t)(val
& 0xff);
118 rl
= (uint16_t)((high
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023);
119 rh
= (uint16_t)((low
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023);
121 return (rh
<< 8) | rl
;
124 uint32_t helper_br_update(uint32_t reg
)
126 uint32_t index
= reg
& 0xffff;
127 uint32_t incr
= reg
>> 16;
128 uint32_t new_index
= reverse16(reverse16(index
) + reverse16(incr
));
129 return reg
- index
+ new_index
;
132 uint32_t helper_circ_update(uint32_t reg
, uint32_t off
)
134 uint32_t index
= reg
& 0xffff;
135 uint32_t length
= reg
>> 16;
136 int32_t new_index
= index
+ off
;
142 return reg
- index
+ new_index
;
145 static uint32_t ssov32(CPUTriCoreState
*env
, int64_t arg
)
148 int64_t max_pos
= INT32_MAX
;
149 int64_t max_neg
= INT32_MIN
;
151 env
->PSW_USB_V
= (1 << 31);
152 env
->PSW_USB_SV
= (1 << 31);
153 ret
= (target_ulong
)max_pos
;
156 env
->PSW_USB_V
= (1 << 31);
157 env
->PSW_USB_SV
= (1 << 31);
158 ret
= (target_ulong
)max_neg
;
161 ret
= (target_ulong
)arg
;
164 env
->PSW_USB_AV
= arg
^ arg
* 2u;
165 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
169 static uint32_t suov32_pos(CPUTriCoreState
*env
, uint64_t arg
)
172 uint64_t max_pos
= UINT32_MAX
;
174 env
->PSW_USB_V
= (1 << 31);
175 env
->PSW_USB_SV
= (1 << 31);
176 ret
= (target_ulong
)max_pos
;
179 ret
= (target_ulong
)arg
;
181 env
->PSW_USB_AV
= arg
^ arg
* 2u;
182 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
186 static uint32_t suov32_neg(CPUTriCoreState
*env
, int64_t arg
)
191 env
->PSW_USB_V
= (1 << 31);
192 env
->PSW_USB_SV
= (1 << 31);
196 ret
= (target_ulong
)arg
;
198 env
->PSW_USB_AV
= arg
^ arg
* 2u;
199 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
203 static uint32_t ssov16(CPUTriCoreState
*env
, int32_t hw0
, int32_t hw1
)
205 int32_t max_pos
= INT16_MAX
;
206 int32_t max_neg
= INT16_MIN
;
210 av0
= hw0
^ hw0
* 2u;
212 env
->PSW_USB_V
= (1 << 31);
214 } else if (hw0
< max_neg
) {
215 env
->PSW_USB_V
= (1 << 31);
219 av1
= hw1
^ hw1
* 2u;
221 env
->PSW_USB_V
= (1 << 31);
223 } else if (hw1
< max_neg
) {
224 env
->PSW_USB_V
= (1 << 31);
228 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
229 env
->PSW_USB_AV
= (av0
| av1
) << 16;
230 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
231 return (hw0
& 0xffff) | (hw1
<< 16);
234 static uint32_t suov16(CPUTriCoreState
*env
, int32_t hw0
, int32_t hw1
)
236 int32_t max_pos
= UINT16_MAX
;
240 av0
= hw0
^ hw0
* 2u;
242 env
->PSW_USB_V
= (1 << 31);
244 } else if (hw0
< 0) {
245 env
->PSW_USB_V
= (1 << 31);
249 av1
= hw1
^ hw1
* 2u;
251 env
->PSW_USB_V
= (1 << 31);
253 } else if (hw1
< 0) {
254 env
->PSW_USB_V
= (1 << 31);
258 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
259 env
->PSW_USB_AV
= (av0
| av1
) << 16;
260 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
261 return (hw0
& 0xffff) | (hw1
<< 16);
264 target_ulong
helper_add_ssov(CPUTriCoreState
*env
, target_ulong r1
,
267 int64_t t1
= sextract64(r1
, 0, 32);
268 int64_t t2
= sextract64(r2
, 0, 32);
269 int64_t result
= t1
+ t2
;
270 return ssov32(env
, result
);
273 uint64_t helper_add64_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
279 ovf
= (result
^ r1
) & ~(r1
^ r2
);
280 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
281 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
283 env
->PSW_USB_V
= (1 << 31);
284 env
->PSW_USB_SV
= (1 << 31);
285 /* ext_ret > MAX_INT */
286 if ((int64_t)r1
>= 0) {
288 /* ext_ret < MIN_INT */
298 target_ulong
helper_add_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
301 int32_t ret_hw0
, ret_hw1
;
303 ret_hw0
= sextract32(r1
, 0, 16) + sextract32(r2
, 0, 16);
304 ret_hw1
= sextract32(r1
, 16, 16) + sextract32(r2
, 16, 16);
305 return ssov16(env
, ret_hw0
, ret_hw1
);
308 uint32_t helper_addr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
311 int64_t mul_res0
= sextract64(r1
, 0, 32);
312 int64_t mul_res1
= sextract64(r1
, 32, 32);
313 int64_t r2_low
= sextract64(r2_l
, 0, 32);
314 int64_t r2_high
= sextract64(r2_h
, 0, 32);
315 int64_t result0
, result1
;
321 result0
= r2_low
+ mul_res0
+ 0x8000;
322 result1
= r2_high
+ mul_res1
+ 0x8000;
325 avf0
= result0
^ avf0
;
327 avf1
= result1
^ avf1
;
329 if (result0
> INT32_MAX
) {
332 } else if (result0
< INT32_MIN
) {
337 if (result1
> INT32_MAX
) {
340 } else if (result1
< INT32_MIN
) {
345 env
->PSW_USB_V
= ovf0
| ovf1
;
346 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
348 env
->PSW_USB_AV
= avf0
| avf1
;
349 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
351 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
354 uint32_t helper_addsur_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
357 int64_t mul_res0
= sextract64(r1
, 0, 32);
358 int64_t mul_res1
= sextract64(r1
, 32, 32);
359 int64_t r2_low
= sextract64(r2_l
, 0, 32);
360 int64_t r2_high
= sextract64(r2_h
, 0, 32);
361 int64_t result0
, result1
;
367 result0
= r2_low
- mul_res0
+ 0x8000;
368 result1
= r2_high
+ mul_res1
+ 0x8000;
371 avf0
= result0
^ avf0
;
373 avf1
= result1
^ avf1
;
375 if (result0
> INT32_MAX
) {
378 } else if (result0
< INT32_MIN
) {
383 if (result1
> INT32_MAX
) {
386 } else if (result1
< INT32_MIN
) {
391 env
->PSW_USB_V
= ovf0
| ovf1
;
392 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
394 env
->PSW_USB_AV
= avf0
| avf1
;
395 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
397 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
401 target_ulong
helper_add_suov(CPUTriCoreState
*env
, target_ulong r1
,
404 int64_t t1
= extract64(r1
, 0, 32);
405 int64_t t2
= extract64(r2
, 0, 32);
406 int64_t result
= t1
+ t2
;
407 return suov32_pos(env
, result
);
410 target_ulong
helper_add_h_suov(CPUTriCoreState
*env
, target_ulong r1
,
413 int32_t ret_hw0
, ret_hw1
;
415 ret_hw0
= extract32(r1
, 0, 16) + extract32(r2
, 0, 16);
416 ret_hw1
= extract32(r1
, 16, 16) + extract32(r2
, 16, 16);
417 return suov16(env
, ret_hw0
, ret_hw1
);
420 target_ulong
helper_sub_ssov(CPUTriCoreState
*env
, target_ulong r1
,
423 int64_t t1
= sextract64(r1
, 0, 32);
424 int64_t t2
= sextract64(r2
, 0, 32);
425 int64_t result
= t1
- t2
;
426 return ssov32(env
, result
);
429 uint64_t helper_sub64_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
435 ovf
= (result
^ r1
) & (r1
^ r2
);
436 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
437 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
439 env
->PSW_USB_V
= (1 << 31);
440 env
->PSW_USB_SV
= (1 << 31);
441 /* ext_ret > MAX_INT */
442 if ((int64_t)r1
>= 0) {
444 /* ext_ret < MIN_INT */
454 target_ulong
helper_sub_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
457 int32_t ret_hw0
, ret_hw1
;
459 ret_hw0
= sextract32(r1
, 0, 16) - sextract32(r2
, 0, 16);
460 ret_hw1
= sextract32(r1
, 16, 16) - sextract32(r2
, 16, 16);
461 return ssov16(env
, ret_hw0
, ret_hw1
);
464 uint32_t helper_subr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
467 int64_t mul_res0
= sextract64(r1
, 0, 32);
468 int64_t mul_res1
= sextract64(r1
, 32, 32);
469 int64_t r2_low
= sextract64(r2_l
, 0, 32);
470 int64_t r2_high
= sextract64(r2_h
, 0, 32);
471 int64_t result0
, result1
;
477 result0
= r2_low
- mul_res0
+ 0x8000;
478 result1
= r2_high
- mul_res1
+ 0x8000;
481 avf0
= result0
^ avf0
;
483 avf1
= result1
^ avf1
;
485 if (result0
> INT32_MAX
) {
488 } else if (result0
< INT32_MIN
) {
493 if (result1
> INT32_MAX
) {
496 } else if (result1
< INT32_MIN
) {
501 env
->PSW_USB_V
= ovf0
| ovf1
;
502 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
504 env
->PSW_USB_AV
= avf0
| avf1
;
505 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
507 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
510 uint32_t helper_subadr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
513 int64_t mul_res0
= sextract64(r1
, 0, 32);
514 int64_t mul_res1
= sextract64(r1
, 32, 32);
515 int64_t r2_low
= sextract64(r2_l
, 0, 32);
516 int64_t r2_high
= sextract64(r2_h
, 0, 32);
517 int64_t result0
, result1
;
523 result0
= r2_low
+ mul_res0
+ 0x8000;
524 result1
= r2_high
- mul_res1
+ 0x8000;
527 avf0
= result0
^ avf0
;
529 avf1
= result1
^ avf1
;
531 if (result0
> INT32_MAX
) {
534 } else if (result0
< INT32_MIN
) {
539 if (result1
> INT32_MAX
) {
542 } else if (result1
< INT32_MIN
) {
547 env
->PSW_USB_V
= ovf0
| ovf1
;
548 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
550 env
->PSW_USB_AV
= avf0
| avf1
;
551 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
553 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
556 target_ulong
helper_sub_suov(CPUTriCoreState
*env
, target_ulong r1
,
559 int64_t t1
= extract64(r1
, 0, 32);
560 int64_t t2
= extract64(r2
, 0, 32);
561 int64_t result
= t1
- t2
;
562 return suov32_neg(env
, result
);
565 target_ulong
helper_sub_h_suov(CPUTriCoreState
*env
, target_ulong r1
,
568 int32_t ret_hw0
, ret_hw1
;
570 ret_hw0
= extract32(r1
, 0, 16) - extract32(r2
, 0, 16);
571 ret_hw1
= extract32(r1
, 16, 16) - extract32(r2
, 16, 16);
572 return suov16(env
, ret_hw0
, ret_hw1
);
575 target_ulong
helper_mul_ssov(CPUTriCoreState
*env
, target_ulong r1
,
578 int64_t t1
= sextract64(r1
, 0, 32);
579 int64_t t2
= sextract64(r2
, 0, 32);
580 int64_t result
= t1
* t2
;
581 return ssov32(env
, result
);
584 target_ulong
helper_mul_suov(CPUTriCoreState
*env
, target_ulong r1
,
587 int64_t t1
= extract64(r1
, 0, 32);
588 int64_t t2
= extract64(r2
, 0, 32);
589 int64_t result
= t1
* t2
;
591 return suov32_pos(env
, result
);
594 target_ulong
helper_sha_ssov(CPUTriCoreState
*env
, target_ulong r1
,
597 int64_t t1
= sextract64(r1
, 0, 32);
598 int32_t t2
= sextract64(r2
, 0, 6);
607 return ssov32(env
, result
);
610 uint32_t helper_abs_ssov(CPUTriCoreState
*env
, target_ulong r1
)
613 result
= ((int32_t)r1
>= 0) ? r1
: (0 - r1
);
614 return ssov32(env
, result
);
617 uint32_t helper_abs_h_ssov(CPUTriCoreState
*env
, target_ulong r1
)
619 int32_t ret_h0
, ret_h1
;
621 ret_h0
= sextract32(r1
, 0, 16);
622 ret_h0
= (ret_h0
>= 0) ? ret_h0
: (0 - ret_h0
);
624 ret_h1
= sextract32(r1
, 16, 16);
625 ret_h1
= (ret_h1
>= 0) ? ret_h1
: (0 - ret_h1
);
627 return ssov16(env
, ret_h0
, ret_h1
);
630 target_ulong
helper_absdif_ssov(CPUTriCoreState
*env
, target_ulong r1
,
633 int64_t t1
= sextract64(r1
, 0, 32);
634 int64_t t2
= sextract64(r2
, 0, 32);
642 return ssov32(env
, result
);
645 uint32_t helper_absdif_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
649 int32_t ret_h0
, ret_h1
;
651 t1
= sextract32(r1
, 0, 16);
652 t2
= sextract32(r2
, 0, 16);
659 t1
= sextract32(r1
, 16, 16);
660 t2
= sextract32(r2
, 16, 16);
667 return ssov16(env
, ret_h0
, ret_h1
);
670 target_ulong
helper_madd32_ssov(CPUTriCoreState
*env
, target_ulong r1
,
671 target_ulong r2
, target_ulong r3
)
673 int64_t t1
= sextract64(r1
, 0, 32);
674 int64_t t2
= sextract64(r2
, 0, 32);
675 int64_t t3
= sextract64(r3
, 0, 32);
678 result
= t2
+ (t1
* t3
);
679 return ssov32(env
, result
);
682 target_ulong
helper_madd32_suov(CPUTriCoreState
*env
, target_ulong r1
,
683 target_ulong r2
, target_ulong r3
)
685 uint64_t t1
= extract64(r1
, 0, 32);
686 uint64_t t2
= extract64(r2
, 0, 32);
687 uint64_t t3
= extract64(r3
, 0, 32);
690 result
= t2
+ (t1
* t3
);
691 return suov32_pos(env
, result
);
694 uint64_t helper_madd64_ssov(CPUTriCoreState
*env
, target_ulong r1
,
695 uint64_t r2
, target_ulong r3
)
698 int64_t t1
= sextract64(r1
, 0, 32);
699 int64_t t3
= sextract64(r3
, 0, 32);
704 ovf
= (ret
^ mul
) & ~(mul
^ r2
);
707 env
->PSW_USB_AV
= t1
^ t1
* 2u;
708 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
710 if ((int64_t)ovf
< 0) {
711 env
->PSW_USB_V
= (1 << 31);
712 env
->PSW_USB_SV
= (1 << 31);
713 /* ext_ret > MAX_INT */
716 /* ext_ret < MIN_INT */
728 helper_madd32_q_add_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
734 env
->PSW_USB_AV
= (result
^ result
* 2u);
735 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
737 /* we do the saturation by hand, since we produce an overflow on the host
738 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
739 case, we flip the saturated value. */
740 if (r2
== 0x8000000000000000LL
) {
741 if (result
> 0x7fffffffLL
) {
742 env
->PSW_USB_V
= (1 << 31);
743 env
->PSW_USB_SV
= (1 << 31);
745 } else if (result
< -0x80000000LL
) {
746 env
->PSW_USB_V
= (1 << 31);
747 env
->PSW_USB_SV
= (1 << 31);
753 if (result
> 0x7fffffffLL
) {
754 env
->PSW_USB_V
= (1 << 31);
755 env
->PSW_USB_SV
= (1 << 31);
757 } else if (result
< -0x80000000LL
) {
758 env
->PSW_USB_V
= (1 << 31);
759 env
->PSW_USB_SV
= (1 << 31);
765 return (uint32_t)result
;
768 uint64_t helper_madd64_q_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2
,
769 uint32_t r3
, uint32_t n
)
771 int64_t t1
= (int64_t)r1
;
772 int64_t t2
= sextract64(r2
, 0, 32);
773 int64_t t3
= sextract64(r3
, 0, 32);
777 mul
= (t2
* t3
) << n
;
780 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
781 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
783 ovf
= (result
^ mul
) & ~(mul
^ t1
);
784 /* we do the saturation by hand, since we produce an overflow on the host
785 if the mul was (0x80000000 * 0x80000000) << 1). If this is the
786 case, we flip the saturated value. */
787 if ((r2
== 0x80000000) && (r3
== 0x80000000) && (n
== 1)) {
789 env
->PSW_USB_V
= (1 << 31);
790 env
->PSW_USB_SV
= (1 << 31);
791 /* ext_ret > MAX_INT */
794 /* ext_ret < MIN_INT */
803 env
->PSW_USB_V
= (1 << 31);
804 env
->PSW_USB_SV
= (1 << 31);
805 /* ext_ret > MAX_INT */
808 /* ext_ret < MIN_INT */
816 return (uint64_t)result
;
819 uint32_t helper_maddr_q_ssov(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
820 uint32_t r3
, uint32_t n
)
822 int64_t t1
= sextract64(r1
, 0, 32);
823 int64_t t2
= sextract64(r2
, 0, 32);
824 int64_t t3
= sextract64(r3
, 0, 32);
827 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
830 mul
= (t2
* t3
) << n
;
833 ret
= t1
+ mul
+ 0x8000;
835 env
->PSW_USB_AV
= ret
^ ret
* 2u;
836 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
838 if (ret
> 0x7fffffffll
) {
839 env
->PSW_USB_V
= (1 << 31);
840 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
842 } else if (ret
< -0x80000000ll
) {
843 env
->PSW_USB_V
= (1 << 31);
844 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
849 return ret
& 0xffff0000ll
;
852 uint64_t helper_madd64_suov(CPUTriCoreState
*env
, target_ulong r1
,
853 uint64_t r2
, target_ulong r3
)
856 uint64_t t1
= extract64(r1
, 0, 32);
857 uint64_t t3
= extract64(r3
, 0, 32);
863 env
->PSW_USB_AV
= t1
^ t1
* 2u;
864 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
867 env
->PSW_USB_V
= (1 << 31);
868 env
->PSW_USB_SV
= (1 << 31);
877 target_ulong
helper_msub32_ssov(CPUTriCoreState
*env
, target_ulong r1
,
878 target_ulong r2
, target_ulong r3
)
880 int64_t t1
= sextract64(r1
, 0, 32);
881 int64_t t2
= sextract64(r2
, 0, 32);
882 int64_t t3
= sextract64(r3
, 0, 32);
885 result
= t2
- (t1
* t3
);
886 return ssov32(env
, result
);
889 target_ulong
helper_msub32_suov(CPUTriCoreState
*env
, target_ulong r1
,
890 target_ulong r2
, target_ulong r3
)
892 uint64_t t1
= extract64(r1
, 0, 32);
893 uint64_t t2
= extract64(r2
, 0, 32);
894 uint64_t t3
= extract64(r3
, 0, 32);
901 env
->PSW_USB_AV
= result
^ result
* 2u;
902 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
903 /* we calculate ovf by hand here, because the multiplication can overflow on
904 the host, which would give false results if we compare to less than
907 env
->PSW_USB_V
= (1 << 31);
908 env
->PSW_USB_SV
= (1 << 31);
916 uint64_t helper_msub64_ssov(CPUTriCoreState
*env
, target_ulong r1
,
917 uint64_t r2
, target_ulong r3
)
920 int64_t t1
= sextract64(r1
, 0, 32);
921 int64_t t3
= sextract64(r3
, 0, 32);
926 ovf
= (ret
^ r2
) & (mul
^ r2
);
929 env
->PSW_USB_AV
= t1
^ t1
* 2u;
930 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
932 if ((int64_t)ovf
< 0) {
933 env
->PSW_USB_V
= (1 << 31);
934 env
->PSW_USB_SV
= (1 << 31);
935 /* ext_ret > MAX_INT */
938 /* ext_ret < MIN_INT */
948 uint64_t helper_msub64_suov(CPUTriCoreState
*env
, target_ulong r1
,
949 uint64_t r2
, target_ulong r3
)
952 uint64_t t1
= extract64(r1
, 0, 32);
953 uint64_t t3
= extract64(r3
, 0, 32);
959 env
->PSW_USB_AV
= t1
^ t1
* 2u;
960 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
963 env
->PSW_USB_V
= (1 << 31);
964 env
->PSW_USB_SV
= (1 << 31);
974 helper_msub32_q_sub_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
977 int64_t t1
= (int64_t)r1
;
978 int64_t t2
= (int64_t)r2
;
982 env
->PSW_USB_AV
= (result
^ result
* 2u);
983 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
985 /* we do the saturation by hand, since we produce an overflow on the host
986 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
987 case, we flip the saturated value. */
988 if (r2
== 0x8000000000000000LL
) {
989 if (result
> 0x7fffffffLL
) {
990 env
->PSW_USB_V
= (1 << 31);
991 env
->PSW_USB_SV
= (1 << 31);
993 } else if (result
< -0x80000000LL
) {
994 env
->PSW_USB_V
= (1 << 31);
995 env
->PSW_USB_SV
= (1 << 31);
1001 if (result
> 0x7fffffffLL
) {
1002 env
->PSW_USB_V
= (1 << 31);
1003 env
->PSW_USB_SV
= (1 << 31);
1005 } else if (result
< -0x80000000LL
) {
1006 env
->PSW_USB_V
= (1 << 31);
1007 env
->PSW_USB_SV
= (1 << 31);
1013 return (uint32_t)result
;
1016 uint64_t helper_msub64_q_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2
,
1017 uint32_t r3
, uint32_t n
)
1019 int64_t t1
= (int64_t)r1
;
1020 int64_t t2
= sextract64(r2
, 0, 32);
1021 int64_t t3
= sextract64(r3
, 0, 32);
1022 int64_t result
, mul
;
1025 mul
= (t2
* t3
) << n
;
1028 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
1029 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1031 ovf
= (result
^ t1
) & (t1
^ mul
);
1032 /* we do the saturation by hand, since we produce an overflow on the host
1033 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
1034 case, we flip the saturated value. */
1035 if (mul
== 0x8000000000000000LL
) {
1037 env
->PSW_USB_V
= (1 << 31);
1038 env
->PSW_USB_SV
= (1 << 31);
1039 /* ext_ret > MAX_INT */
1042 /* 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_h(target_ulong r1
)
1735 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1736 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1738 ret_hw0
= clo32(ret_hw0
<< 16);
1739 ret_hw1
= clo32(ret_hw1
<< 16);
1748 return ret_hw0
| (ret_hw1
<< 16);
1751 uint32_t helper_clz_h(target_ulong r1
)
1753 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1754 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1756 ret_hw0
= clz32(ret_hw0
<< 16);
1757 ret_hw1
= clz32(ret_hw1
<< 16);
1766 return ret_hw0
| (ret_hw1
<< 16);
1769 uint32_t helper_cls_h(target_ulong r1
)
1771 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1772 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1774 ret_hw0
= clrsb32(ret_hw0
<< 16);
1775 ret_hw1
= clrsb32(ret_hw1
<< 16);
1784 return ret_hw0
| (ret_hw1
<< 16);
1787 uint32_t helper_sh(target_ulong r1
, target_ulong r2
)
1789 int32_t shift_count
= sextract32(r2
, 0, 6);
1791 if (shift_count
== -32) {
1793 } else if (shift_count
< 0) {
1794 return r1
>> -shift_count
;
1796 return r1
<< shift_count
;
1800 uint32_t helper_sh_h(target_ulong r1
, target_ulong r2
)
1802 int32_t ret_hw0
, ret_hw1
;
1803 int32_t shift_count
;
1805 shift_count
= sextract32(r2
, 0, 5);
1807 if (shift_count
== -16) {
1809 } else if (shift_count
< 0) {
1810 ret_hw0
= extract32(r1
, 0, 16) >> -shift_count
;
1811 ret_hw1
= extract32(r1
, 16, 16) >> -shift_count
;
1812 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1814 ret_hw0
= extract32(r1
, 0, 16) << shift_count
;
1815 ret_hw1
= extract32(r1
, 16, 16) << shift_count
;
1816 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1820 uint32_t helper_sha(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1822 int32_t shift_count
;
1826 shift_count
= sextract32(r2
, 0, 6);
1827 t1
= sextract32(r1
, 0, 32);
1829 if (shift_count
== 0) {
1830 env
->PSW_USB_C
= env
->PSW_USB_V
= 0;
1832 } else if (shift_count
== -32) {
1833 env
->PSW_USB_C
= r1
;
1836 } else if (shift_count
> 0) {
1837 result
= t1
<< shift_count
;
1839 env
->PSW_USB_C
= ((result
& 0xffffffff00000000ULL
) != 0);
1841 env
->PSW_USB_V
= (((result
> 0x7fffffffLL
) ||
1842 (result
< -0x80000000LL
)) << 31);
1844 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1845 ret
= (uint32_t)result
;
1848 env
->PSW_USB_C
= (r1
& ((1 << -shift_count
) - 1));
1849 ret
= t1
>> -shift_count
;
1852 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1853 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1858 uint32_t helper_sha_h(target_ulong r1
, target_ulong r2
)
1860 int32_t shift_count
;
1861 int32_t ret_hw0
, ret_hw1
;
1863 shift_count
= sextract32(r2
, 0, 5);
1865 if (shift_count
== 0) {
1867 } else if (shift_count
< 0) {
1868 ret_hw0
= sextract32(r1
, 0, 16) >> -shift_count
;
1869 ret_hw1
= sextract32(r1
, 16, 16) >> -shift_count
;
1870 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1872 ret_hw0
= sextract32(r1
, 0, 16) << shift_count
;
1873 ret_hw1
= sextract32(r1
, 16, 16) << shift_count
;
1874 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1878 uint32_t helper_bmerge(target_ulong r1
, target_ulong r2
)
1883 for (i
= 0; i
< 16; i
++) {
1884 ret
|= (r1
& 1) << (2 * i
+ 1);
1885 ret
|= (r2
& 1) << (2 * i
);
1892 uint64_t helper_bsplit(uint32_t r1
)
1898 for (i
= 0; i
< 32; i
= i
+ 2) {
1900 ret
|= (r1
& 1) << (i
/2);
1903 ret
|= (uint64_t)(r1
& 1) << (i
/2 + 32);
1909 uint32_t helper_parity(target_ulong r1
)
1916 for (i
= 0; i
< 8; i
++) {
1922 for (i
= 0; i
< 8; i
++) {
1929 for (i
= 0; i
< 8; i
++) {
1936 for (i
= 0; i
< 8; i
++) {
1945 uint32_t helper_pack(uint32_t carry
, uint32_t r1_low
, uint32_t r1_high
,
1949 int32_t fp_exp
, fp_frac
, temp_exp
, fp_exp_frac
;
1950 int32_t int_exp
= r1_high
;
1951 int32_t int_mant
= r1_low
;
1952 uint32_t flag_rnd
= (int_mant
& (1 << 7)) && (
1953 (int_mant
& (1 << 8)) ||
1954 (int_mant
& 0x7f) ||
1956 if (((int_mant
& (1<<31)) == 0) && (int_exp
== 255)) {
1958 fp_frac
= extract32(int_mant
, 8, 23);
1959 } else if ((int_mant
& (1<<31)) && (int_exp
>= 127)) {
1962 } else if ((int_mant
& (1<<31)) && (int_exp
<= -128)) {
1965 } else if (int_mant
== 0) {
1969 if (((int_mant
& (1 << 31)) == 0)) {
1972 temp_exp
= int_exp
+ 128;
1974 fp_exp_frac
= (((temp_exp
& 0xff) << 23) |
1975 extract32(int_mant
, 8, 23))
1977 fp_exp
= extract32(fp_exp_frac
, 23, 8);
1978 fp_frac
= extract32(fp_exp_frac
, 0, 23);
1980 ret
= r2
& (1 << 31);
1981 ret
= ret
+ (fp_exp
<< 23);
1982 ret
= ret
+ (fp_frac
& 0x7fffff);
1987 uint64_t helper_unpack(target_ulong arg1
)
1989 int32_t fp_exp
= extract32(arg1
, 23, 8);
1990 int32_t fp_frac
= extract32(arg1
, 0, 23);
1992 int32_t int_exp
, int_mant
;
1994 if (fp_exp
== 255) {
1996 int_mant
= (fp_frac
<< 7);
1997 } else if ((fp_exp
== 0) && (fp_frac
== 0)) {
2000 } else if ((fp_exp
== 0) && (fp_frac
!= 0)) {
2002 int_mant
= (fp_frac
<< 7);
2004 int_exp
= fp_exp
- 127;
2005 int_mant
= (fp_frac
<< 7);
2006 int_mant
|= (1 << 30);
2015 uint64_t helper_dvinit_b_13(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2018 int32_t abs_sig_dividend
, abs_divisor
;
2020 ret
= sextract32(r1
, 0, 32);
2022 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2026 abs_sig_dividend
= abs((int32_t)r1
) >> 8;
2027 abs_divisor
= abs((int32_t)r2
);
2029 ofv if (a/b >= 255) <=> (a/255 >= b) */
2030 env
->PSW_USB_V
= (abs_sig_dividend
>= abs_divisor
) << 31;
2031 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2032 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2033 env
->PSW_USB_AV
= 0;
2038 uint64_t helper_dvinit_b_131(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2040 uint64_t ret
= sextract32(r1
, 0, 32);
2043 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2047 env
->PSW_USB_V
= ((r2
== 0) || ((r2
== 0xffffffff) && (r1
== 0xffffff80)));
2048 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2049 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2050 env
->PSW_USB_AV
= 0;
2055 uint64_t helper_dvinit_h_13(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2058 int32_t abs_sig_dividend
, abs_divisor
;
2060 ret
= sextract32(r1
, 0, 32);
2062 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2066 abs_sig_dividend
= abs((int32_t)r1
) >> 16;
2067 abs_divisor
= abs((int32_t)r2
);
2069 ofv if (a/b >= 0xffff) <=> (a/0xffff >= b) */
2070 env
->PSW_USB_V
= (abs_sig_dividend
>= abs_divisor
) << 31;
2071 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2072 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2073 env
->PSW_USB_AV
= 0;
2078 uint64_t helper_dvinit_h_131(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2080 uint64_t ret
= sextract32(r1
, 0, 32);
2083 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2087 env
->PSW_USB_V
= ((r2
== 0) || ((r2
== 0xffffffff) && (r1
== 0xffff8000)));
2088 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2089 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2090 env
->PSW_USB_AV
= 0;
2095 uint64_t helper_dvadj(uint64_t r1
, uint32_t r2
)
2097 int32_t x_sign
= (r1
>> 63);
2098 int32_t q_sign
= x_sign
^ (r2
>> 31);
2099 int32_t eq_pos
= x_sign
& ((r1
>> 32) == r2
);
2100 int32_t eq_neg
= x_sign
& ((r1
>> 32) == -r2
);
2104 if ((q_sign
& ~eq_neg
) | eq_pos
) {
2105 quotient
= (r1
+ 1) & 0xffffffff;
2107 quotient
= r1
& 0xffffffff;
2110 if (eq_pos
| eq_neg
) {
2113 remainder
= (r1
& 0xffffffff00000000ull
);
2115 return remainder
| quotient
;
2118 uint64_t helper_dvstep(uint64_t r1
, uint32_t r2
)
2120 int32_t dividend_sign
= extract64(r1
, 63, 1);
2121 int32_t divisor_sign
= extract32(r2
, 31, 1);
2122 int32_t quotient_sign
= (dividend_sign
!= divisor_sign
);
2123 int32_t addend
, dividend_quotient
, remainder
;
2126 if (quotient_sign
) {
2131 dividend_quotient
= (int32_t)r1
;
2132 remainder
= (int32_t)(r1
>> 32);
2134 for (i
= 0; i
< 8; i
++) {
2135 remainder
= (remainder
<< 1) | extract32(dividend_quotient
, 31, 1);
2136 dividend_quotient
<<= 1;
2137 temp
= remainder
+ addend
;
2138 if ((temp
< 0) == dividend_sign
) {
2141 if (((temp
< 0) == dividend_sign
)) {
2142 dividend_quotient
= dividend_quotient
| !quotient_sign
;
2144 dividend_quotient
= dividend_quotient
| quotient_sign
;
2147 return ((uint64_t)remainder
<< 32) | (uint32_t)dividend_quotient
;
2150 uint64_t helper_dvstep_u(uint64_t r1
, uint32_t r2
)
2152 int32_t dividend_quotient
= extract64(r1
, 0, 32);
2153 int64_t remainder
= extract64(r1
, 32, 32);
2156 for (i
= 0; i
< 8; i
++) {
2157 remainder
= (remainder
<< 1) | extract32(dividend_quotient
, 31, 1);
2158 dividend_quotient
<<= 1;
2159 temp
= (remainder
& 0xffffffff) - r2
;
2163 dividend_quotient
= dividend_quotient
| !(temp
< 0);
2165 return ((uint64_t)remainder
<< 32) | (uint32_t)dividend_quotient
;
2168 uint64_t helper_divide(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2170 int32_t quotient
, remainder
;
2171 int32_t dividend
= (int32_t)r1
;
2172 int32_t divisor
= (int32_t)r2
;
2175 if (dividend
>= 0) {
2176 quotient
= 0x7fffffff;
2179 quotient
= 0x80000000;
2182 env
->PSW_USB_V
= (1 << 31);
2183 } else if ((divisor
== 0xffffffff) && (dividend
== 0x80000000)) {
2184 quotient
= 0x7fffffff;
2186 env
->PSW_USB_V
= (1 << 31);
2188 remainder
= dividend
% divisor
;
2189 quotient
= (dividend
- remainder
)/divisor
;
2192 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2193 env
->PSW_USB_AV
= 0;
2194 return ((uint64_t)remainder
<< 32) | (uint32_t)quotient
;
2197 uint64_t helper_divide_u(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2199 uint32_t quotient
, remainder
;
2200 uint32_t dividend
= r1
;
2201 uint32_t divisor
= r2
;
2204 quotient
= 0xffffffff;
2206 env
->PSW_USB_V
= (1 << 31);
2208 remainder
= dividend
% divisor
;
2209 quotient
= (dividend
- remainder
)/divisor
;
2212 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2213 env
->PSW_USB_AV
= 0;
2214 return ((uint64_t)remainder
<< 32) | quotient
;
2217 uint64_t helper_mul_h(uint32_t arg00
, uint32_t arg01
,
2218 uint32_t arg10
, uint32_t arg11
, uint32_t n
)
2220 uint32_t result0
, result1
;
2222 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2223 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2224 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2225 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2227 result1
= 0x7fffffff;
2229 result1
= (((uint32_t)(arg00
* arg10
)) << n
);
2232 result0
= 0x7fffffff;
2234 result0
= (((uint32_t)(arg01
* arg11
)) << n
);
2236 return (((uint64_t)result1
<< 32)) | result0
;
2239 uint64_t helper_mulm_h(uint32_t arg00
, uint32_t arg01
,
2240 uint32_t arg10
, uint32_t arg11
, uint32_t n
)
2243 int64_t result0
, result1
;
2245 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2246 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2247 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2248 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2251 result1
= 0x7fffffff;
2253 result1
= (((int32_t)arg00
* (int32_t)arg10
) << n
);
2256 result0
= 0x7fffffff;
2258 result0
= (((int32_t)arg01
* (int32_t)arg11
) << n
);
2260 ret
= (result1
+ result0
);
2264 uint32_t helper_mulr_h(uint32_t arg00
, uint32_t arg01
,
2265 uint32_t arg10
, uint32_t arg11
, uint32_t n
)
2267 uint32_t result0
, result1
;
2269 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2270 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2271 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2272 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2275 result1
= 0x7fffffff;
2277 result1
= ((arg00
* arg10
) << n
) + 0x8000;
2280 result0
= 0x7fffffff;
2282 result0
= ((arg01
* arg11
) << n
) + 0x8000;
2284 return (result1
& 0xffff0000) | (result0
>> 16);
2287 uint32_t helper_crc32b(uint32_t arg0
, uint32_t arg1
)
2289 uint8_t buf
[1] = { arg0
& 0xff };
2291 return crc32(arg1
, buf
, 1);
2295 uint32_t helper_crc32_be(uint32_t arg0
, uint32_t arg1
)
2298 stl_be_p(buf
, arg0
);
2300 return crc32(arg1
, buf
, 4);
2303 uint32_t helper_crc32_le(uint32_t arg0
, uint32_t arg1
)
2306 stl_le_p(buf
, arg0
);
2308 return crc32(arg1
, buf
, 4);
2311 static uint32_t crc_div(uint32_t crc_in
, uint32_t data
, uint32_t gen
,
2312 uint32_t n
, uint32_t m
)
2317 for (i
= 0; i
< m
; i
++) {
2318 if (crc_in
& (1u << (n
- 1))) {
2320 if (data
& (1u << (m
- 1))) {
2326 if (data
& (1u << (m
- 1))) {
2336 uint32_t helper_crcn(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
)
2338 uint32_t crc_out
, crc_in
;
2339 uint32_t n
= extract32(arg0
, 12, 4) + 1;
2340 uint32_t gen
= extract32(arg0
, 16, n
);
2341 uint32_t inv
= extract32(arg0
, 9, 1);
2342 uint32_t le
= extract32(arg0
, 8, 1);
2343 uint32_t m
= extract32(arg0
, 0, 3) + 1;
2344 uint32_t data
= extract32(arg1
, 0, m
);
2345 uint32_t seed
= extract32(arg2
, 0, n
);
2351 data
= revbit32(data
) >> (32 - m
);
2360 crc_in
= (data
>> (m
- n
)) ^ seed
;
2362 crc_in
= (data
<< (n
- m
)) ^ seed
;
2365 crc_out
= crc_div(crc_in
, data
, gen
, n
, m
);
2371 return extract32(crc_out
, 0, n
);
2374 uint32_t helper_shuffle(uint32_t arg0
, uint32_t arg1
)
2377 uint32_t byte_select
;
2380 byte_select
= arg1
& 0x3;
2381 resb
= extract32(arg0
, byte_select
* 8, 8);
2384 byte_select
= (arg1
>> 2) & 0x3;
2385 resb
= extract32(arg0
, byte_select
* 8, 8);
2388 byte_select
= (arg1
>> 4) & 0x3;
2389 resb
= extract32(arg0
, byte_select
* 8, 8);
2392 byte_select
= (arg1
>> 6) & 0x3;
2393 resb
= extract32(arg0
, byte_select
* 8, 8);
2397 /* Assign the correct nibble position. */
2398 res
= ((res
& 0xf0f0f0f0) >> 4)
2399 | ((res
& 0x0f0f0f0f) << 4);
2400 /* Assign the correct bit position. */
2401 res
= ((res
& 0x88888888) >> 3)
2402 | ((res
& 0x44444444) >> 1)
2403 | ((res
& 0x22222222) << 1)
2404 | ((res
& 0x11111111) << 3);
2410 /* context save area (CSA) related helpers */
2412 static int cdc_increment(target_ulong
*psw
)
2414 if ((*psw
& MASK_PSW_CDC
) == 0x7f) {
2419 /* check for overflow */
2420 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2421 int mask
= (1u << (7 - lo
)) - 1;
2422 int count
= *psw
& mask
;
2430 static int cdc_decrement(target_ulong
*psw
)
2432 if ((*psw
& MASK_PSW_CDC
) == 0x7f) {
2435 /* check for underflow */
2436 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2437 int mask
= (1u << (7 - lo
)) - 1;
2438 int count
= *psw
& mask
;
2446 static bool cdc_zero(target_ulong
*psw
)
2448 int cdc
= *psw
& MASK_PSW_CDC
;
2449 /* Returns TRUE if PSW.CDC.COUNT == 0 or if PSW.CDC ==
2450 7'b1111111, otherwise returns FALSE. */
2454 /* find CDC.COUNT */
2455 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2456 int mask
= (1u << (7 - lo
)) - 1;
2457 int count
= *psw
& mask
;
2461 static void save_context_upper(CPUTriCoreState
*env
, target_ulong ea
)
2463 cpu_stl_data(env
, ea
, env
->PCXI
);
2464 cpu_stl_data(env
, ea
+4, psw_read(env
));
2465 cpu_stl_data(env
, ea
+8, env
->gpr_a
[10]);
2466 cpu_stl_data(env
, ea
+12, env
->gpr_a
[11]);
2467 cpu_stl_data(env
, ea
+16, env
->gpr_d
[8]);
2468 cpu_stl_data(env
, ea
+20, env
->gpr_d
[9]);
2469 cpu_stl_data(env
, ea
+24, env
->gpr_d
[10]);
2470 cpu_stl_data(env
, ea
+28, env
->gpr_d
[11]);
2471 cpu_stl_data(env
, ea
+32, env
->gpr_a
[12]);
2472 cpu_stl_data(env
, ea
+36, env
->gpr_a
[13]);
2473 cpu_stl_data(env
, ea
+40, env
->gpr_a
[14]);
2474 cpu_stl_data(env
, ea
+44, env
->gpr_a
[15]);
2475 cpu_stl_data(env
, ea
+48, env
->gpr_d
[12]);
2476 cpu_stl_data(env
, ea
+52, env
->gpr_d
[13]);
2477 cpu_stl_data(env
, ea
+56, env
->gpr_d
[14]);
2478 cpu_stl_data(env
, ea
+60, env
->gpr_d
[15]);
2481 static void save_context_lower(CPUTriCoreState
*env
, target_ulong ea
)
2483 cpu_stl_data(env
, ea
, env
->PCXI
);
2484 cpu_stl_data(env
, ea
+4, env
->gpr_a
[11]);
2485 cpu_stl_data(env
, ea
+8, env
->gpr_a
[2]);
2486 cpu_stl_data(env
, ea
+12, env
->gpr_a
[3]);
2487 cpu_stl_data(env
, ea
+16, env
->gpr_d
[0]);
2488 cpu_stl_data(env
, ea
+20, env
->gpr_d
[1]);
2489 cpu_stl_data(env
, ea
+24, env
->gpr_d
[2]);
2490 cpu_stl_data(env
, ea
+28, env
->gpr_d
[3]);
2491 cpu_stl_data(env
, ea
+32, env
->gpr_a
[4]);
2492 cpu_stl_data(env
, ea
+36, env
->gpr_a
[5]);
2493 cpu_stl_data(env
, ea
+40, env
->gpr_a
[6]);
2494 cpu_stl_data(env
, ea
+44, env
->gpr_a
[7]);
2495 cpu_stl_data(env
, ea
+48, env
->gpr_d
[4]);
2496 cpu_stl_data(env
, ea
+52, env
->gpr_d
[5]);
2497 cpu_stl_data(env
, ea
+56, env
->gpr_d
[6]);
2498 cpu_stl_data(env
, ea
+60, env
->gpr_d
[7]);
2501 static void restore_context_upper(CPUTriCoreState
*env
, target_ulong ea
,
2502 target_ulong
*new_PCXI
, target_ulong
*new_PSW
)
2504 *new_PCXI
= cpu_ldl_data(env
, ea
);
2505 *new_PSW
= cpu_ldl_data(env
, ea
+4);
2506 env
->gpr_a
[10] = cpu_ldl_data(env
, ea
+8);
2507 env
->gpr_a
[11] = cpu_ldl_data(env
, ea
+12);
2508 env
->gpr_d
[8] = cpu_ldl_data(env
, ea
+16);
2509 env
->gpr_d
[9] = cpu_ldl_data(env
, ea
+20);
2510 env
->gpr_d
[10] = cpu_ldl_data(env
, ea
+24);
2511 env
->gpr_d
[11] = cpu_ldl_data(env
, ea
+28);
2512 env
->gpr_a
[12] = cpu_ldl_data(env
, ea
+32);
2513 env
->gpr_a
[13] = cpu_ldl_data(env
, ea
+36);
2514 env
->gpr_a
[14] = cpu_ldl_data(env
, ea
+40);
2515 env
->gpr_a
[15] = cpu_ldl_data(env
, ea
+44);
2516 env
->gpr_d
[12] = cpu_ldl_data(env
, ea
+48);
2517 env
->gpr_d
[13] = cpu_ldl_data(env
, ea
+52);
2518 env
->gpr_d
[14] = cpu_ldl_data(env
, ea
+56);
2519 env
->gpr_d
[15] = cpu_ldl_data(env
, ea
+60);
2522 static void restore_context_lower(CPUTriCoreState
*env
, target_ulong ea
,
2523 target_ulong
*ra
, target_ulong
*pcxi
)
2525 *pcxi
= cpu_ldl_data(env
, ea
);
2526 *ra
= cpu_ldl_data(env
, ea
+4);
2527 env
->gpr_a
[2] = cpu_ldl_data(env
, ea
+8);
2528 env
->gpr_a
[3] = cpu_ldl_data(env
, ea
+12);
2529 env
->gpr_d
[0] = cpu_ldl_data(env
, ea
+16);
2530 env
->gpr_d
[1] = cpu_ldl_data(env
, ea
+20);
2531 env
->gpr_d
[2] = cpu_ldl_data(env
, ea
+24);
2532 env
->gpr_d
[3] = cpu_ldl_data(env
, ea
+28);
2533 env
->gpr_a
[4] = cpu_ldl_data(env
, ea
+32);
2534 env
->gpr_a
[5] = cpu_ldl_data(env
, ea
+36);
2535 env
->gpr_a
[6] = cpu_ldl_data(env
, ea
+40);
2536 env
->gpr_a
[7] = cpu_ldl_data(env
, ea
+44);
2537 env
->gpr_d
[4] = cpu_ldl_data(env
, ea
+48);
2538 env
->gpr_d
[5] = cpu_ldl_data(env
, ea
+52);
2539 env
->gpr_d
[6] = cpu_ldl_data(env
, ea
+56);
2540 env
->gpr_d
[7] = cpu_ldl_data(env
, ea
+60);
2543 void helper_call(CPUTriCoreState
*env
, uint32_t next_pc
)
2545 target_ulong tmp_FCX
;
2547 target_ulong new_FCX
;
2550 psw
= psw_read(env
);
2551 /* if (FCX == 0) trap(FCU); */
2552 if (env
->FCX
== 0) {
2554 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2556 /* if (PSW.CDE) then if (cdc_increment()) then trap(CDO); */
2557 if (psw
& MASK_PSW_CDE
) {
2558 if (cdc_increment(&psw
)) {
2560 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CDO
, GETPC());
2564 psw
|= MASK_PSW_CDE
;
2566 * we need to save PSW.CDE and not PSW.CDC into the CSAs. psw already
2567 * contains the CDC from cdc_increment(), so we cannot call psw_write()
2570 env
->PSW
|= MASK_PSW_CDE
;
2572 /* tmp_FCX = FCX; */
2574 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2575 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2576 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2577 /* new_FCX = M(EA, word); */
2578 new_FCX
= cpu_ldl_data(env
, ea
);
2579 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2580 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2582 save_context_upper(env
, ea
);
2584 /* PCXI.PCPN = ICR.CCPN; */
2585 pcxi_set_pcpn(env
, icr_get_ccpn(env
));
2586 /* PCXI.PIE = ICR.IE; */
2587 pcxi_set_pie(env
, icr_get_ie(env
));
2589 pcxi_set_ul(env
, 1);
2591 /* PCXI[19: 0] = FCX[19: 0]; */
2592 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2593 /* FCX[19: 0] = new_FCX[19: 0]; */
2594 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2595 /* A[11] = next_pc[31: 0]; */
2596 env
->gpr_a
[11] = next_pc
;
2598 /* if (tmp_FCX == LCX) trap(FCD);*/
2599 if (tmp_FCX
== env
->LCX
) {
2601 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2603 psw_write(env
, psw
);
2606 void helper_ret(CPUTriCoreState
*env
)
2609 target_ulong new_PCXI
;
2610 target_ulong new_PSW
, psw
;
2612 psw
= psw_read(env
);
2613 /* if (PSW.CDE) then if (cdc_decrement()) then trap(CDU);*/
2614 if (psw
& MASK_PSW_CDE
) {
2615 if (cdc_decrement(&psw
)) {
2617 psw_write(env
, psw
);
2618 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CDU
, GETPC());
2621 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2622 if ((env
->PCXI
& 0xfffff) == 0) {
2624 psw_write(env
, psw
);
2625 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2627 /* if (PCXI.UL == 0) then trap(CTYP); */
2628 if (pcxi_get_ul(env
) == 0) {
2630 cdc_increment(&psw
); /* restore to the start of helper */
2631 psw_write(env
, psw
);
2632 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2634 /* PC = {A11 [31: 1], 1’b0}; */
2635 env
->PC
= env
->gpr_a
[11] & 0xfffffffe;
2637 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
2638 ea
= (pcxi_get_pcxs(env
) << 28) |
2639 (pcxi_get_pcxo(env
) << 6);
2640 /* {new_PCXI, new_PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2641 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2642 restore_context_upper(env
, ea
, &new_PCXI
, &new_PSW
);
2643 /* M(EA, word) = FCX; */
2644 cpu_stl_data(env
, ea
, env
->FCX
);
2645 /* FCX[19: 0] = PCXI[19: 0]; */
2646 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2647 /* PCXI = new_PCXI; */
2648 env
->PCXI
= new_PCXI
;
2650 if (tricore_has_feature(env
, TRICORE_FEATURE_131
)) {
2651 /* PSW = {new_PSW[31:26], PSW[25:24], new_PSW[23:0]}; */
2652 psw_write(env
, (new_PSW
& ~(0x3000000)) + (psw
& (0x3000000)));
2653 } else { /* TRICORE_FEATURE_13 only */
2655 psw_write(env
, new_PSW
);
2659 void helper_bisr(CPUTriCoreState
*env
, uint32_t const9
)
2661 target_ulong tmp_FCX
;
2663 target_ulong new_FCX
;
2665 if (env
->FCX
== 0) {
2667 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2671 ea
= ((env
->FCX
& 0xf0000) << 12) + ((env
->FCX
& 0xffff) << 6);
2673 /* new_FCX = M(EA, word); */
2674 new_FCX
= cpu_ldl_data(env
, ea
);
2675 /* M(EA, 16 * word) = {PCXI, A[11], A[2], A[3], D[0], D[1], D[2], D[3], A[4]
2676 , A[5], A[6], A[7], D[4], D[5], D[6], D[7]}; */
2677 save_context_lower(env
, ea
);
2680 /* PCXI.PCPN = ICR.CCPN */
2681 pcxi_set_pcpn(env
, icr_get_ccpn(env
));
2682 /* PCXI.PIE = ICR.IE */
2683 pcxi_set_pie(env
, icr_get_ie(env
));
2685 pcxi_set_ul(env
, 0);
2687 /* PCXI[19: 0] = FCX[19: 0] */
2688 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2689 /* FXC[19: 0] = new_FCX[19: 0] */
2690 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2695 icr_set_ccpn(env
, const9
);
2697 if (tmp_FCX
== env
->LCX
) {
2699 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2703 void helper_rfe(CPUTriCoreState
*env
)
2706 target_ulong new_PCXI
;
2707 target_ulong new_PSW
;
2708 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2709 if ((env
->PCXI
& 0xfffff) == 0) {
2710 /* raise csu trap */
2711 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2713 /* if (PCXI.UL == 0) then trap(CTYP); */
2714 if (pcxi_get_ul(env
) == 0) {
2715 /* raise CTYP trap */
2716 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2718 /* if (!cdc_zero() AND PSW.CDE) then trap(NEST); */
2719 if (!cdc_zero(&(env
->PSW
)) && (env
->PSW
& MASK_PSW_CDE
)) {
2720 /* raise NEST trap */
2721 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_NEST
, GETPC());
2723 env
->PC
= env
->gpr_a
[11] & ~0x1;
2724 /* ICR.IE = PCXI.PIE; */
2725 icr_set_ie(env
, pcxi_get_pie(env
));
2727 /* ICR.CCPN = PCXI.PCPN; */
2728 icr_set_ccpn(env
, pcxi_get_pcpn(env
));
2730 /*EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0};*/
2731 ea
= (pcxi_get_pcxs(env
) << 28) |
2732 (pcxi_get_pcxo(env
) << 6);
2734 /*{new_PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2735 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2736 restore_context_upper(env
, ea
, &new_PCXI
, &new_PSW
);
2737 /* M(EA, word) = FCX;*/
2738 cpu_stl_data(env
, ea
, env
->FCX
);
2739 /* FCX[19: 0] = PCXI[19: 0]; */
2740 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2741 /* PCXI = new_PCXI; */
2742 env
->PCXI
= new_PCXI
;
2744 psw_write(env
, new_PSW
);
2747 void helper_rfm(CPUTriCoreState
*env
)
2749 env
->PC
= (env
->gpr_a
[11] & ~0x1);
2750 /* ICR.IE = PCXI.PIE; */
2751 icr_set_ie(env
, pcxi_get_pie(env
));
2752 /* ICR.CCPN = PCXI.PCPN; */
2753 icr_set_ccpn(env
, pcxi_get_pcpn(env
));
2755 /* {PCXI, PSW, A[10], A[11]} = M(DCX, 4 * word); */
2756 env
->PCXI
= cpu_ldl_data(env
, env
->DCX
);
2757 psw_write(env
, cpu_ldl_data(env
, env
->DCX
+4));
2758 env
->gpr_a
[10] = cpu_ldl_data(env
, env
->DCX
+8);
2759 env
->gpr_a
[11] = cpu_ldl_data(env
, env
->DCX
+12);
2761 if (tricore_has_feature(env
, TRICORE_FEATURE_131
)) {
2766 void helper_ldlcx(CPUTriCoreState
*env
, target_ulong ea
)
2769 /* insn doesn't load PCXI and RA */
2770 restore_context_lower(env
, ea
, &dummy
, &dummy
);
2773 void helper_lducx(CPUTriCoreState
*env
, target_ulong ea
)
2776 /* insn doesn't load PCXI and PSW */
2777 restore_context_upper(env
, ea
, &dummy
, &dummy
);
2780 void helper_stlcx(CPUTriCoreState
*env
, target_ulong ea
)
2782 save_context_lower(env
, ea
);
2785 void helper_stucx(CPUTriCoreState
*env
, target_ulong ea
)
2787 save_context_upper(env
, ea
);
2790 void helper_svlcx(CPUTriCoreState
*env
)
2792 target_ulong tmp_FCX
;
2794 target_ulong new_FCX
;
2796 if (env
->FCX
== 0) {
2798 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2800 /* tmp_FCX = FCX; */
2802 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2803 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2804 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2805 /* new_FCX = M(EA, word); */
2806 new_FCX
= cpu_ldl_data(env
, ea
);
2807 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2808 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2810 save_context_lower(env
, ea
);
2812 /* PCXI.PCPN = ICR.CCPN; */
2813 pcxi_set_pcpn(env
, icr_get_ccpn(env
));
2815 /* PCXI.PIE = ICR.IE; */
2816 pcxi_set_pie(env
, icr_get_ie(env
));
2819 pcxi_set_ul(env
, 0);
2821 /* PCXI[19: 0] = FCX[19: 0]; */
2822 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2823 /* FCX[19: 0] = new_FCX[19: 0]; */
2824 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2826 /* if (tmp_FCX == LCX) trap(FCD);*/
2827 if (tmp_FCX
== env
->LCX
) {
2829 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2833 void helper_svucx(CPUTriCoreState
*env
)
2835 target_ulong tmp_FCX
;
2837 target_ulong new_FCX
;
2839 if (env
->FCX
== 0) {
2841 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2843 /* tmp_FCX = FCX; */
2845 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2846 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2847 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2848 /* new_FCX = M(EA, word); */
2849 new_FCX
= cpu_ldl_data(env
, ea
);
2850 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2851 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2853 save_context_upper(env
, ea
);
2855 /* PCXI.PCPN = ICR.CCPN; */
2856 pcxi_set_pcpn(env
, icr_get_ccpn(env
));
2858 /* PCXI.PIE = ICR.IE; */
2859 pcxi_set_pie(env
, icr_get_ie(env
));
2862 pcxi_set_ul(env
, 1);
2864 /* PCXI[19: 0] = FCX[19: 0]; */
2865 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2866 /* FCX[19: 0] = new_FCX[19: 0]; */
2867 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2869 /* if (tmp_FCX == LCX) trap(FCD);*/
2870 if (tmp_FCX
== env
->LCX
) {
2872 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2876 void helper_rslcx(CPUTriCoreState
*env
)
2879 target_ulong new_PCXI
;
2880 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2881 if ((env
->PCXI
& 0xfffff) == 0) {
2883 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2885 /* if (PCXI.UL == 1) then trap(CTYP); */
2886 if (pcxi_get_ul(env
) == 1) {
2888 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2890 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
2891 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
2892 ea
= (pcxi_get_pcxs(env
) << 28) |
2893 (pcxi_get_pcxo(env
) << 6);
2895 /* {new_PCXI, A[11], A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2896 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2897 restore_context_lower(env
, ea
, &env
->gpr_a
[11], &new_PCXI
);
2898 /* M(EA, word) = FCX; */
2899 cpu_stl_data(env
, ea
, env
->FCX
);
2900 /* M(EA, word) = FCX; */
2901 cpu_stl_data(env
, ea
, env
->FCX
);
2902 /* FCX[19: 0] = PCXI[19: 0]; */
2903 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2904 /* PCXI = new_PCXI; */
2905 env
->PCXI
= new_PCXI
;
2908 void helper_psw_write(CPUTriCoreState
*env
, uint32_t arg
)
2910 psw_write(env
, arg
);
2913 uint32_t helper_psw_read(CPUTriCoreState
*env
)
2915 return psw_read(env
);