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/exec-all.h"
22 #include "exec/cpu_ldst.h"
23 #include <zlib.h> /* for crc32 */
26 /* Exception helpers */
28 static void QEMU_NORETURN
29 raise_exception_sync_internal(CPUTriCoreState
*env
, uint32_t class, int tin
,
30 uintptr_t pc
, uint32_t fcd_pc
)
32 CPUState
*cs
= CPU(tricore_env_get_cpu(env
));
33 /* in case we come from a helper-call we need to restore the PC */
35 cpu_restore_state(cs
, pc
);
38 /* Tin is loaded into d[15] */
41 if (class == TRAPC_CTX_MNG
&& tin
== TIN3_FCU
) {
42 /* upper context cannot be saved, if the context list is empty */
47 /* The return address in a[11] is updated */
48 if (class == TRAPC_CTX_MNG
&& tin
== TIN3_FCD
) {
49 env
->SYSCON
|= MASK_SYSCON_FCD_SF
;
50 /* when we run out of CSAs after saving a context a FCD trap is taken
51 and the return address is the start of the trap handler which used
53 env
->gpr_a
[11] = fcd_pc
;
54 } else if (class == TRAPC_SYSCALL
) {
55 env
->gpr_a
[11] = env
->PC
+ 4;
57 env
->gpr_a
[11] = env
->PC
;
59 /* The stack pointer in A[10] is set to the Interrupt Stack Pointer (ISP)
60 when the processor was not previously using the interrupt stack
61 (in case of PSW.IS = 0). The stack pointer bit is set for using the
62 interrupt stack: PSW.IS = 1. */
63 if ((env
->PSW
& MASK_PSW_IS
) == 0) {
64 env
->gpr_a
[10] = env
->ISP
;
66 env
->PSW
|= MASK_PSW_IS
;
67 /* The I/O mode is set to Supervisor mode, which means all permissions
68 are enabled: PSW.IO = 10 B .*/
69 env
->PSW
|= (2 << 10);
71 /*The current Protection Register Set is set to 0: PSW.PRS = 00 B .*/
72 env
->PSW
&= ~MASK_PSW_PRS
;
74 /* The Call Depth Counter (CDC) is cleared, and the call depth limit is
75 set for 64: PSW.CDC = 0000000 B .*/
76 env
->PSW
&= ~MASK_PSW_CDC
;
78 /* Call Depth Counter is enabled, PSW.CDE = 1. */
79 env
->PSW
|= MASK_PSW_CDE
;
81 /* Write permission to global registers A[0], A[1], A[8], A[9] is
82 disabled: PSW.GW = 0. */
83 env
->PSW
&= ~MASK_PSW_GW
;
85 /*The interrupt system is globally disabled: ICR.IE = 0. The ‘old’
86 ICR.IE and ICR.CCPN are saved */
88 /* PCXI.PIE = ICR.IE */
89 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
90 ((env
->ICR
& MASK_ICR_IE
) << 15));
91 /* PCXI.PCPN = ICR.CCPN */
92 env
->PCXI
= (env
->PCXI
& 0xffffff) +
93 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
94 /* Update PC using the trap vector table */
95 env
->PC
= env
->BTV
| (class << 5);
100 void helper_raise_exception_sync(CPUTriCoreState
*env
, uint32_t class,
103 raise_exception_sync_internal(env
, class, tin
, 0, 0);
106 static void raise_exception_sync_helper(CPUTriCoreState
*env
, uint32_t class,
107 uint32_t tin
, uintptr_t pc
)
109 raise_exception_sync_internal(env
, class, tin
, pc
, 0);
112 /* Addressing mode helper */
114 static uint16_t reverse16(uint16_t val
)
116 uint8_t high
= (uint8_t)(val
>> 8);
117 uint8_t low
= (uint8_t)(val
& 0xff);
121 rl
= (uint16_t)((high
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023);
122 rh
= (uint16_t)((low
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023);
124 return (rh
<< 8) | rl
;
127 uint32_t helper_br_update(uint32_t reg
)
129 uint32_t index
= reg
& 0xffff;
130 uint32_t incr
= reg
>> 16;
131 uint32_t new_index
= reverse16(reverse16(index
) + reverse16(incr
));
132 return reg
- index
+ new_index
;
135 uint32_t helper_circ_update(uint32_t reg
, uint32_t off
)
137 uint32_t index
= reg
& 0xffff;
138 uint32_t length
= reg
>> 16;
139 int32_t new_index
= index
+ off
;
145 return reg
- index
+ new_index
;
148 static uint32_t ssov32(CPUTriCoreState
*env
, int64_t arg
)
151 int64_t max_pos
= INT32_MAX
;
152 int64_t max_neg
= INT32_MIN
;
154 env
->PSW_USB_V
= (1 << 31);
155 env
->PSW_USB_SV
= (1 << 31);
156 ret
= (target_ulong
)max_pos
;
159 env
->PSW_USB_V
= (1 << 31);
160 env
->PSW_USB_SV
= (1 << 31);
161 ret
= (target_ulong
)max_neg
;
164 ret
= (target_ulong
)arg
;
167 env
->PSW_USB_AV
= arg
^ arg
* 2u;
168 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
172 static uint32_t suov32_pos(CPUTriCoreState
*env
, uint64_t arg
)
175 uint64_t max_pos
= UINT32_MAX
;
177 env
->PSW_USB_V
= (1 << 31);
178 env
->PSW_USB_SV
= (1 << 31);
179 ret
= (target_ulong
)max_pos
;
182 ret
= (target_ulong
)arg
;
184 env
->PSW_USB_AV
= arg
^ arg
* 2u;
185 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
189 static uint32_t suov32_neg(CPUTriCoreState
*env
, int64_t arg
)
194 env
->PSW_USB_V
= (1 << 31);
195 env
->PSW_USB_SV
= (1 << 31);
199 ret
= (target_ulong
)arg
;
201 env
->PSW_USB_AV
= arg
^ arg
* 2u;
202 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
206 static uint32_t ssov16(CPUTriCoreState
*env
, int32_t hw0
, int32_t hw1
)
208 int32_t max_pos
= INT16_MAX
;
209 int32_t max_neg
= INT16_MIN
;
213 av0
= hw0
^ hw0
* 2u;
215 env
->PSW_USB_V
= (1 << 31);
217 } else if (hw0
< max_neg
) {
218 env
->PSW_USB_V
= (1 << 31);
222 av1
= hw1
^ hw1
* 2u;
224 env
->PSW_USB_V
= (1 << 31);
226 } else if (hw1
< max_neg
) {
227 env
->PSW_USB_V
= (1 << 31);
231 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
232 env
->PSW_USB_AV
= (av0
| av1
) << 16;
233 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
234 return (hw0
& 0xffff) | (hw1
<< 16);
237 static uint32_t suov16(CPUTriCoreState
*env
, int32_t hw0
, int32_t hw1
)
239 int32_t max_pos
= UINT16_MAX
;
243 av0
= hw0
^ hw0
* 2u;
245 env
->PSW_USB_V
= (1 << 31);
247 } else if (hw0
< 0) {
248 env
->PSW_USB_V
= (1 << 31);
252 av1
= hw1
^ hw1
* 2u;
254 env
->PSW_USB_V
= (1 << 31);
256 } else if (hw1
< 0) {
257 env
->PSW_USB_V
= (1 << 31);
261 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
262 env
->PSW_USB_AV
= (av0
| av1
) << 16;
263 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
264 return (hw0
& 0xffff) | (hw1
<< 16);
267 target_ulong
helper_add_ssov(CPUTriCoreState
*env
, target_ulong r1
,
270 int64_t t1
= sextract64(r1
, 0, 32);
271 int64_t t2
= sextract64(r2
, 0, 32);
272 int64_t result
= t1
+ t2
;
273 return ssov32(env
, result
);
276 uint64_t helper_add64_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
282 ovf
= (result
^ r1
) & ~(r1
^ r2
);
283 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
284 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
286 env
->PSW_USB_V
= (1 << 31);
287 env
->PSW_USB_SV
= (1 << 31);
288 /* ext_ret > MAX_INT */
289 if ((int64_t)r1
>= 0) {
291 /* ext_ret < MIN_INT */
301 target_ulong
helper_add_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
304 int32_t ret_hw0
, ret_hw1
;
306 ret_hw0
= sextract32(r1
, 0, 16) + sextract32(r2
, 0, 16);
307 ret_hw1
= sextract32(r1
, 16, 16) + sextract32(r2
, 16, 16);
308 return ssov16(env
, ret_hw0
, ret_hw1
);
311 uint32_t helper_addr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
314 int64_t mul_res0
= sextract64(r1
, 0, 32);
315 int64_t mul_res1
= sextract64(r1
, 32, 32);
316 int64_t r2_low
= sextract64(r2_l
, 0, 32);
317 int64_t r2_high
= sextract64(r2_h
, 0, 32);
318 int64_t result0
, result1
;
324 result0
= r2_low
+ mul_res0
+ 0x8000;
325 result1
= r2_high
+ mul_res1
+ 0x8000;
328 avf0
= result0
^ avf0
;
330 avf1
= result1
^ avf1
;
332 if (result0
> INT32_MAX
) {
335 } else if (result0
< INT32_MIN
) {
340 if (result1
> INT32_MAX
) {
343 } else if (result1
< INT32_MIN
) {
348 env
->PSW_USB_V
= ovf0
| ovf1
;
349 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
351 env
->PSW_USB_AV
= avf0
| avf1
;
352 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
354 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
357 uint32_t helper_addsur_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
360 int64_t mul_res0
= sextract64(r1
, 0, 32);
361 int64_t mul_res1
= sextract64(r1
, 32, 32);
362 int64_t r2_low
= sextract64(r2_l
, 0, 32);
363 int64_t r2_high
= sextract64(r2_h
, 0, 32);
364 int64_t result0
, result1
;
370 result0
= r2_low
- mul_res0
+ 0x8000;
371 result1
= r2_high
+ mul_res1
+ 0x8000;
374 avf0
= result0
^ avf0
;
376 avf1
= result1
^ avf1
;
378 if (result0
> INT32_MAX
) {
381 } else if (result0
< INT32_MIN
) {
386 if (result1
> INT32_MAX
) {
389 } else if (result1
< INT32_MIN
) {
394 env
->PSW_USB_V
= ovf0
| ovf1
;
395 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
397 env
->PSW_USB_AV
= avf0
| avf1
;
398 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
400 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
404 target_ulong
helper_add_suov(CPUTriCoreState
*env
, target_ulong r1
,
407 int64_t t1
= extract64(r1
, 0, 32);
408 int64_t t2
= extract64(r2
, 0, 32);
409 int64_t result
= t1
+ t2
;
410 return suov32_pos(env
, result
);
413 target_ulong
helper_add_h_suov(CPUTriCoreState
*env
, target_ulong r1
,
416 int32_t ret_hw0
, ret_hw1
;
418 ret_hw0
= extract32(r1
, 0, 16) + extract32(r2
, 0, 16);
419 ret_hw1
= extract32(r1
, 16, 16) + extract32(r2
, 16, 16);
420 return suov16(env
, ret_hw0
, ret_hw1
);
423 target_ulong
helper_sub_ssov(CPUTriCoreState
*env
, target_ulong r1
,
426 int64_t t1
= sextract64(r1
, 0, 32);
427 int64_t t2
= sextract64(r2
, 0, 32);
428 int64_t result
= t1
- t2
;
429 return ssov32(env
, result
);
432 uint64_t helper_sub64_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
438 ovf
= (result
^ r1
) & (r1
^ r2
);
439 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
440 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
442 env
->PSW_USB_V
= (1 << 31);
443 env
->PSW_USB_SV
= (1 << 31);
444 /* ext_ret > MAX_INT */
445 if ((int64_t)r1
>= 0) {
447 /* ext_ret < MIN_INT */
457 target_ulong
helper_sub_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
460 int32_t ret_hw0
, ret_hw1
;
462 ret_hw0
= sextract32(r1
, 0, 16) - sextract32(r2
, 0, 16);
463 ret_hw1
= sextract32(r1
, 16, 16) - sextract32(r2
, 16, 16);
464 return ssov16(env
, ret_hw0
, ret_hw1
);
467 uint32_t helper_subr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
470 int64_t mul_res0
= sextract64(r1
, 0, 32);
471 int64_t mul_res1
= sextract64(r1
, 32, 32);
472 int64_t r2_low
= sextract64(r2_l
, 0, 32);
473 int64_t r2_high
= sextract64(r2_h
, 0, 32);
474 int64_t result0
, result1
;
480 result0
= r2_low
- mul_res0
+ 0x8000;
481 result1
= r2_high
- mul_res1
+ 0x8000;
484 avf0
= result0
^ avf0
;
486 avf1
= result1
^ avf1
;
488 if (result0
> INT32_MAX
) {
491 } else if (result0
< INT32_MIN
) {
496 if (result1
> INT32_MAX
) {
499 } else if (result1
< INT32_MIN
) {
504 env
->PSW_USB_V
= ovf0
| ovf1
;
505 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
507 env
->PSW_USB_AV
= avf0
| avf1
;
508 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
510 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
513 uint32_t helper_subadr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
516 int64_t mul_res0
= sextract64(r1
, 0, 32);
517 int64_t mul_res1
= sextract64(r1
, 32, 32);
518 int64_t r2_low
= sextract64(r2_l
, 0, 32);
519 int64_t r2_high
= sextract64(r2_h
, 0, 32);
520 int64_t result0
, result1
;
526 result0
= r2_low
+ mul_res0
+ 0x8000;
527 result1
= r2_high
- mul_res1
+ 0x8000;
530 avf0
= result0
^ avf0
;
532 avf1
= result1
^ avf1
;
534 if (result0
> INT32_MAX
) {
537 } else if (result0
< INT32_MIN
) {
542 if (result1
> INT32_MAX
) {
545 } else if (result1
< INT32_MIN
) {
550 env
->PSW_USB_V
= ovf0
| ovf1
;
551 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
553 env
->PSW_USB_AV
= avf0
| avf1
;
554 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
556 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
559 target_ulong
helper_sub_suov(CPUTriCoreState
*env
, target_ulong r1
,
562 int64_t t1
= extract64(r1
, 0, 32);
563 int64_t t2
= extract64(r2
, 0, 32);
564 int64_t result
= t1
- t2
;
565 return suov32_neg(env
, result
);
568 target_ulong
helper_sub_h_suov(CPUTriCoreState
*env
, target_ulong r1
,
571 int32_t ret_hw0
, ret_hw1
;
573 ret_hw0
= extract32(r1
, 0, 16) - extract32(r2
, 0, 16);
574 ret_hw1
= extract32(r1
, 16, 16) - extract32(r2
, 16, 16);
575 return suov16(env
, ret_hw0
, ret_hw1
);
578 target_ulong
helper_mul_ssov(CPUTriCoreState
*env
, target_ulong r1
,
581 int64_t t1
= sextract64(r1
, 0, 32);
582 int64_t t2
= sextract64(r2
, 0, 32);
583 int64_t result
= t1
* t2
;
584 return ssov32(env
, result
);
587 target_ulong
helper_mul_suov(CPUTriCoreState
*env
, target_ulong r1
,
590 int64_t t1
= extract64(r1
, 0, 32);
591 int64_t t2
= extract64(r2
, 0, 32);
592 int64_t result
= t1
* t2
;
594 return suov32_pos(env
, result
);
597 target_ulong
helper_sha_ssov(CPUTriCoreState
*env
, target_ulong r1
,
600 int64_t t1
= sextract64(r1
, 0, 32);
601 int32_t t2
= sextract64(r2
, 0, 6);
610 return ssov32(env
, result
);
613 uint32_t helper_abs_ssov(CPUTriCoreState
*env
, target_ulong r1
)
616 result
= ((int32_t)r1
>= 0) ? r1
: (0 - r1
);
617 return ssov32(env
, result
);
620 uint32_t helper_abs_h_ssov(CPUTriCoreState
*env
, target_ulong r1
)
622 int32_t ret_h0
, ret_h1
;
624 ret_h0
= sextract32(r1
, 0, 16);
625 ret_h0
= (ret_h0
>= 0) ? ret_h0
: (0 - ret_h0
);
627 ret_h1
= sextract32(r1
, 16, 16);
628 ret_h1
= (ret_h1
>= 0) ? ret_h1
: (0 - ret_h1
);
630 return ssov16(env
, ret_h0
, ret_h1
);
633 target_ulong
helper_absdif_ssov(CPUTriCoreState
*env
, target_ulong r1
,
636 int64_t t1
= sextract64(r1
, 0, 32);
637 int64_t t2
= sextract64(r2
, 0, 32);
645 return ssov32(env
, result
);
648 uint32_t helper_absdif_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
652 int32_t ret_h0
, ret_h1
;
654 t1
= sextract32(r1
, 0, 16);
655 t2
= sextract32(r2
, 0, 16);
662 t1
= sextract32(r1
, 16, 16);
663 t2
= sextract32(r2
, 16, 16);
670 return ssov16(env
, ret_h0
, ret_h1
);
673 target_ulong
helper_madd32_ssov(CPUTriCoreState
*env
, target_ulong r1
,
674 target_ulong r2
, target_ulong r3
)
676 int64_t t1
= sextract64(r1
, 0, 32);
677 int64_t t2
= sextract64(r2
, 0, 32);
678 int64_t t3
= sextract64(r3
, 0, 32);
681 result
= t2
+ (t1
* t3
);
682 return ssov32(env
, result
);
685 target_ulong
helper_madd32_suov(CPUTriCoreState
*env
, target_ulong r1
,
686 target_ulong r2
, target_ulong r3
)
688 uint64_t t1
= extract64(r1
, 0, 32);
689 uint64_t t2
= extract64(r2
, 0, 32);
690 uint64_t t3
= extract64(r3
, 0, 32);
693 result
= t2
+ (t1
* t3
);
694 return suov32_pos(env
, result
);
697 uint64_t helper_madd64_ssov(CPUTriCoreState
*env
, target_ulong r1
,
698 uint64_t r2
, target_ulong r3
)
701 int64_t t1
= sextract64(r1
, 0, 32);
702 int64_t t3
= sextract64(r3
, 0, 32);
707 ovf
= (ret
^ mul
) & ~(mul
^ r2
);
710 env
->PSW_USB_AV
= t1
^ t1
* 2u;
711 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
713 if ((int64_t)ovf
< 0) {
714 env
->PSW_USB_V
= (1 << 31);
715 env
->PSW_USB_SV
= (1 << 31);
716 /* ext_ret > MAX_INT */
719 /* ext_ret < MIN_INT */
731 helper_madd32_q_add_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
737 env
->PSW_USB_AV
= (result
^ result
* 2u);
738 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
740 /* we do the saturation by hand, since we produce an overflow on the host
741 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
742 case, we flip the saturated value. */
743 if (r2
== 0x8000000000000000LL
) {
744 if (result
> 0x7fffffffLL
) {
745 env
->PSW_USB_V
= (1 << 31);
746 env
->PSW_USB_SV
= (1 << 31);
748 } else if (result
< -0x80000000LL
) {
749 env
->PSW_USB_V
= (1 << 31);
750 env
->PSW_USB_SV
= (1 << 31);
756 if (result
> 0x7fffffffLL
) {
757 env
->PSW_USB_V
= (1 << 31);
758 env
->PSW_USB_SV
= (1 << 31);
760 } else if (result
< -0x80000000LL
) {
761 env
->PSW_USB_V
= (1 << 31);
762 env
->PSW_USB_SV
= (1 << 31);
768 return (uint32_t)result
;
771 uint64_t helper_madd64_q_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2
,
772 uint32_t r3
, uint32_t n
)
774 int64_t t1
= (int64_t)r1
;
775 int64_t t2
= sextract64(r2
, 0, 32);
776 int64_t t3
= sextract64(r3
, 0, 32);
780 mul
= (t2
* t3
) << n
;
783 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
784 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
786 ovf
= (result
^ mul
) & ~(mul
^ t1
);
787 /* we do the saturation by hand, since we produce an overflow on the host
788 if the mul was (0x80000000 * 0x80000000) << 1). If this is the
789 case, we flip the saturated value. */
790 if ((r2
== 0x80000000) && (r3
== 0x80000000) && (n
== 1)) {
792 env
->PSW_USB_V
= (1 << 31);
793 env
->PSW_USB_SV
= (1 << 31);
794 /* ext_ret > MAX_INT */
797 /* ext_ret < MIN_INT */
806 env
->PSW_USB_V
= (1 << 31);
807 env
->PSW_USB_SV
= (1 << 31);
808 /* ext_ret > MAX_INT */
811 /* ext_ret < MIN_INT */
819 return (uint64_t)result
;
822 uint32_t helper_maddr_q_ssov(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
823 uint32_t r3
, uint32_t n
)
825 int64_t t1
= sextract64(r1
, 0, 32);
826 int64_t t2
= sextract64(r2
, 0, 32);
827 int64_t t3
= sextract64(r3
, 0, 32);
830 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
833 mul
= (t2
* t3
) << n
;
836 ret
= t1
+ mul
+ 0x8000;
838 env
->PSW_USB_AV
= ret
^ ret
* 2u;
839 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
841 if (ret
> 0x7fffffffll
) {
842 env
->PSW_USB_V
= (1 << 31);
843 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
845 } else if (ret
< -0x80000000ll
) {
846 env
->PSW_USB_V
= (1 << 31);
847 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
852 return ret
& 0xffff0000ll
;
855 uint64_t helper_madd64_suov(CPUTriCoreState
*env
, target_ulong r1
,
856 uint64_t r2
, target_ulong r3
)
859 uint64_t t1
= extract64(r1
, 0, 32);
860 uint64_t t3
= extract64(r3
, 0, 32);
866 env
->PSW_USB_AV
= t1
^ t1
* 2u;
867 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
870 env
->PSW_USB_V
= (1 << 31);
871 env
->PSW_USB_SV
= (1 << 31);
880 target_ulong
helper_msub32_ssov(CPUTriCoreState
*env
, target_ulong r1
,
881 target_ulong r2
, target_ulong r3
)
883 int64_t t1
= sextract64(r1
, 0, 32);
884 int64_t t2
= sextract64(r2
, 0, 32);
885 int64_t t3
= sextract64(r3
, 0, 32);
888 result
= t2
- (t1
* t3
);
889 return ssov32(env
, result
);
892 target_ulong
helper_msub32_suov(CPUTriCoreState
*env
, target_ulong r1
,
893 target_ulong r2
, target_ulong r3
)
895 uint64_t t1
= extract64(r1
, 0, 32);
896 uint64_t t2
= extract64(r2
, 0, 32);
897 uint64_t t3
= extract64(r3
, 0, 32);
904 env
->PSW_USB_AV
= result
^ result
* 2u;
905 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
906 /* we calculate ovf by hand here, because the multiplication can overflow on
907 the host, which would give false results if we compare to less than
910 env
->PSW_USB_V
= (1 << 31);
911 env
->PSW_USB_SV
= (1 << 31);
919 uint64_t helper_msub64_ssov(CPUTriCoreState
*env
, target_ulong r1
,
920 uint64_t r2
, target_ulong r3
)
923 int64_t t1
= sextract64(r1
, 0, 32);
924 int64_t t3
= sextract64(r3
, 0, 32);
929 ovf
= (ret
^ r2
) & (mul
^ r2
);
932 env
->PSW_USB_AV
= t1
^ t1
* 2u;
933 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
935 if ((int64_t)ovf
< 0) {
936 env
->PSW_USB_V
= (1 << 31);
937 env
->PSW_USB_SV
= (1 << 31);
938 /* ext_ret > MAX_INT */
941 /* ext_ret < MIN_INT */
951 uint64_t helper_msub64_suov(CPUTriCoreState
*env
, target_ulong r1
,
952 uint64_t r2
, target_ulong r3
)
955 uint64_t t1
= extract64(r1
, 0, 32);
956 uint64_t t3
= extract64(r3
, 0, 32);
962 env
->PSW_USB_AV
= t1
^ t1
* 2u;
963 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
966 env
->PSW_USB_V
= (1 << 31);
967 env
->PSW_USB_SV
= (1 << 31);
977 helper_msub32_q_sub_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
980 int64_t t1
= (int64_t)r1
;
981 int64_t t2
= (int64_t)r2
;
985 env
->PSW_USB_AV
= (result
^ result
* 2u);
986 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
988 /* we do the saturation by hand, since we produce an overflow on the host
989 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
990 case, we flip the saturated value. */
991 if (r2
== 0x8000000000000000LL
) {
992 if (result
> 0x7fffffffLL
) {
993 env
->PSW_USB_V
= (1 << 31);
994 env
->PSW_USB_SV
= (1 << 31);
996 } else if (result
< -0x80000000LL
) {
997 env
->PSW_USB_V
= (1 << 31);
998 env
->PSW_USB_SV
= (1 << 31);
1004 if (result
> 0x7fffffffLL
) {
1005 env
->PSW_USB_V
= (1 << 31);
1006 env
->PSW_USB_SV
= (1 << 31);
1008 } else if (result
< -0x80000000LL
) {
1009 env
->PSW_USB_V
= (1 << 31);
1010 env
->PSW_USB_SV
= (1 << 31);
1016 return (uint32_t)result
;
1019 uint64_t helper_msub64_q_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2
,
1020 uint32_t r3
, uint32_t n
)
1022 int64_t t1
= (int64_t)r1
;
1023 int64_t t2
= sextract64(r2
, 0, 32);
1024 int64_t t3
= sextract64(r3
, 0, 32);
1025 int64_t result
, mul
;
1028 mul
= (t2
* t3
) << n
;
1031 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
1032 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1034 ovf
= (result
^ t1
) & (t1
^ mul
);
1035 /* we do the saturation by hand, since we produce an overflow on the host
1036 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
1037 case, we flip the saturated value. */
1038 if (mul
== 0x8000000000000000LL
) {
1040 env
->PSW_USB_V
= (1 << 31);
1041 env
->PSW_USB_SV
= (1 << 31);
1042 /* ext_ret > MAX_INT */
1045 /* ext_ret < MIN_INT */
1054 env
->PSW_USB_V
= (1 << 31);
1055 env
->PSW_USB_SV
= (1 << 31);
1056 /* ext_ret > MAX_INT */
1059 /* ext_ret < MIN_INT */
1068 return (uint64_t)result
;
1071 uint32_t helper_msubr_q_ssov(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
1072 uint32_t r3
, uint32_t n
)
1074 int64_t t1
= sextract64(r1
, 0, 32);
1075 int64_t t2
= sextract64(r2
, 0, 32);
1076 int64_t t3
= sextract64(r3
, 0, 32);
1079 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
1082 mul
= (t2
* t3
) << n
;
1085 ret
= t1
- mul
+ 0x8000;
1087 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1088 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1090 if (ret
> 0x7fffffffll
) {
1091 env
->PSW_USB_V
= (1 << 31);
1092 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1094 } else if (ret
< -0x80000000ll
) {
1095 env
->PSW_USB_V
= (1 << 31);
1096 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1101 return ret
& 0xffff0000ll
;
1104 uint32_t helper_abs_b(CPUTriCoreState
*env
, target_ulong arg
)
1111 for (i
= 0; i
< 4; i
++) {
1112 b
= sextract32(arg
, i
* 8, 8);
1113 b
= (b
>= 0) ? b
: (0 - b
);
1114 ovf
|= (b
> 0x7F) || (b
< -0x80);
1116 ret
|= (b
& 0xff) << (i
* 8);
1119 env
->PSW_USB_V
= ovf
<< 31;
1120 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1121 env
->PSW_USB_AV
= avf
<< 24;
1122 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1127 uint32_t helper_abs_h(CPUTriCoreState
*env
, target_ulong arg
)
1134 for (i
= 0; i
< 2; i
++) {
1135 h
= sextract32(arg
, i
* 16, 16);
1136 h
= (h
>= 0) ? h
: (0 - h
);
1137 ovf
|= (h
> 0x7FFF) || (h
< -0x8000);
1139 ret
|= (h
& 0xffff) << (i
* 16);
1142 env
->PSW_USB_V
= ovf
<< 31;
1143 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1144 env
->PSW_USB_AV
= avf
<< 16;
1145 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1150 uint32_t helper_absdif_b(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1158 for (i
= 0; i
< 4; i
++) {
1159 extr_r2
= sextract32(r2
, i
* 8, 8);
1160 b
= sextract32(r1
, i
* 8, 8);
1161 b
= (b
> extr_r2
) ? (b
- extr_r2
) : (extr_r2
- b
);
1162 ovf
|= (b
> 0x7F) || (b
< -0x80);
1164 ret
|= (b
& 0xff) << (i
* 8);
1167 env
->PSW_USB_V
= ovf
<< 31;
1168 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1169 env
->PSW_USB_AV
= avf
<< 24;
1170 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1174 uint32_t helper_absdif_h(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1182 for (i
= 0; i
< 2; i
++) {
1183 extr_r2
= sextract32(r2
, i
* 16, 16);
1184 h
= sextract32(r1
, i
* 16, 16);
1185 h
= (h
> extr_r2
) ? (h
- extr_r2
) : (extr_r2
- h
);
1186 ovf
|= (h
> 0x7FFF) || (h
< -0x8000);
1188 ret
|= (h
& 0xffff) << (i
* 16);
1191 env
->PSW_USB_V
= ovf
<< 31;
1192 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1193 env
->PSW_USB_AV
= avf
<< 16;
1194 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1199 uint32_t helper_addr_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1202 int64_t mul_res0
= sextract64(r1
, 0, 32);
1203 int64_t mul_res1
= sextract64(r1
, 32, 32);
1204 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1205 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1206 int64_t result0
, result1
;
1207 uint32_t ovf0
, ovf1
;
1208 uint32_t avf0
, avf1
;
1212 result0
= r2_low
+ mul_res0
+ 0x8000;
1213 result1
= r2_high
+ mul_res1
+ 0x8000;
1215 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1219 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1223 env
->PSW_USB_V
= ovf0
| ovf1
;
1224 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1226 avf0
= result0
* 2u;
1227 avf0
= result0
^ avf0
;
1228 avf1
= result1
* 2u;
1229 avf1
= result1
^ avf1
;
1231 env
->PSW_USB_AV
= avf0
| avf1
;
1232 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1234 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1237 uint32_t helper_addsur_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1240 int64_t mul_res0
= sextract64(r1
, 0, 32);
1241 int64_t mul_res1
= sextract64(r1
, 32, 32);
1242 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1243 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1244 int64_t result0
, result1
;
1245 uint32_t ovf0
, ovf1
;
1246 uint32_t avf0
, avf1
;
1250 result0
= r2_low
- mul_res0
+ 0x8000;
1251 result1
= r2_high
+ mul_res1
+ 0x8000;
1253 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1257 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1261 env
->PSW_USB_V
= ovf0
| ovf1
;
1262 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1264 avf0
= result0
* 2u;
1265 avf0
= result0
^ avf0
;
1266 avf1
= result1
* 2u;
1267 avf1
= result1
^ avf1
;
1269 env
->PSW_USB_AV
= avf0
| avf1
;
1270 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1272 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1275 uint32_t helper_maddr_q(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
1276 uint32_t r3
, uint32_t n
)
1278 int64_t t1
= sextract64(r1
, 0, 32);
1279 int64_t t2
= sextract64(r2
, 0, 32);
1280 int64_t t3
= sextract64(r3
, 0, 32);
1283 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
1286 mul
= (t2
* t3
) << n
;
1289 ret
= t1
+ mul
+ 0x8000;
1291 if ((ret
> 0x7fffffffll
) || (ret
< -0x80000000ll
)) {
1292 env
->PSW_USB_V
= (1 << 31);
1293 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1297 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1298 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1300 return ret
& 0xffff0000ll
;
1303 uint32_t helper_add_b(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1306 int32_t extr_r1
, extr_r2
;
1311 for (i
= 0; i
< 4; i
++) {
1312 extr_r1
= sextract32(r1
, i
* 8, 8);
1313 extr_r2
= sextract32(r2
, i
* 8, 8);
1315 b
= extr_r1
+ extr_r2
;
1316 ovf
|= ((b
> 0x7f) || (b
< -0x80));
1318 ret
|= ((b
& 0xff) << (i
*8));
1321 env
->PSW_USB_V
= (ovf
<< 31);
1322 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1323 env
->PSW_USB_AV
= avf
<< 24;
1324 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1329 uint32_t helper_add_h(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1332 int32_t extr_r1
, extr_r2
;
1337 for (i
= 0; i
< 2; i
++) {
1338 extr_r1
= sextract32(r1
, i
* 16, 16);
1339 extr_r2
= sextract32(r2
, i
* 16, 16);
1340 h
= extr_r1
+ extr_r2
;
1341 ovf
|= ((h
> 0x7fff) || (h
< -0x8000));
1343 ret
|= (h
& 0xffff) << (i
* 16);
1346 env
->PSW_USB_V
= (ovf
<< 31);
1347 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1348 env
->PSW_USB_AV
= (avf
<< 16);
1349 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1354 uint32_t helper_subr_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1357 int64_t mul_res0
= sextract64(r1
, 0, 32);
1358 int64_t mul_res1
= sextract64(r1
, 32, 32);
1359 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1360 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1361 int64_t result0
, result1
;
1362 uint32_t ovf0
, ovf1
;
1363 uint32_t avf0
, avf1
;
1367 result0
= r2_low
- mul_res0
+ 0x8000;
1368 result1
= r2_high
- mul_res1
+ 0x8000;
1370 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1374 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1378 env
->PSW_USB_V
= ovf0
| ovf1
;
1379 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1381 avf0
= result0
* 2u;
1382 avf0
= result0
^ avf0
;
1383 avf1
= result1
* 2u;
1384 avf1
= result1
^ avf1
;
1386 env
->PSW_USB_AV
= avf0
| avf1
;
1387 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1389 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1392 uint32_t helper_subadr_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1395 int64_t mul_res0
= sextract64(r1
, 0, 32);
1396 int64_t mul_res1
= sextract64(r1
, 32, 32);
1397 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1398 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1399 int64_t result0
, result1
;
1400 uint32_t ovf0
, ovf1
;
1401 uint32_t avf0
, avf1
;
1405 result0
= r2_low
+ mul_res0
+ 0x8000;
1406 result1
= r2_high
- mul_res1
+ 0x8000;
1408 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1412 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1416 env
->PSW_USB_V
= ovf0
| ovf1
;
1417 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1419 avf0
= result0
* 2u;
1420 avf0
= result0
^ avf0
;
1421 avf1
= result1
* 2u;
1422 avf1
= result1
^ avf1
;
1424 env
->PSW_USB_AV
= avf0
| avf1
;
1425 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1427 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1430 uint32_t helper_msubr_q(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
1431 uint32_t r3
, uint32_t n
)
1433 int64_t t1
= sextract64(r1
, 0, 32);
1434 int64_t t2
= sextract64(r2
, 0, 32);
1435 int64_t t3
= sextract64(r3
, 0, 32);
1438 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
1441 mul
= (t2
* t3
) << n
;
1444 ret
= t1
- mul
+ 0x8000;
1446 if ((ret
> 0x7fffffffll
) || (ret
< -0x80000000ll
)) {
1447 env
->PSW_USB_V
= (1 << 31);
1448 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1452 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1453 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1455 return ret
& 0xffff0000ll
;
1458 uint32_t helper_sub_b(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1461 int32_t extr_r1
, extr_r2
;
1466 for (i
= 0; i
< 4; i
++) {
1467 extr_r1
= sextract32(r1
, i
* 8, 8);
1468 extr_r2
= sextract32(r2
, i
* 8, 8);
1470 b
= extr_r1
- extr_r2
;
1471 ovf
|= ((b
> 0x7f) || (b
< -0x80));
1473 ret
|= ((b
& 0xff) << (i
*8));
1476 env
->PSW_USB_V
= (ovf
<< 31);
1477 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1478 env
->PSW_USB_AV
= avf
<< 24;
1479 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1484 uint32_t helper_sub_h(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1487 int32_t extr_r1
, extr_r2
;
1492 for (i
= 0; i
< 2; i
++) {
1493 extr_r1
= sextract32(r1
, i
* 16, 16);
1494 extr_r2
= sextract32(r2
, i
* 16, 16);
1495 h
= extr_r1
- extr_r2
;
1496 ovf
|= ((h
> 0x7fff) || (h
< -0x8000));
1498 ret
|= (h
& 0xffff) << (i
* 16);
1501 env
->PSW_USB_V
= (ovf
<< 31);
1502 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1503 env
->PSW_USB_AV
= avf
<< 16;
1504 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1509 uint32_t helper_eq_b(target_ulong r1
, target_ulong r2
)
1516 for (i
= 0; i
< 4; i
++) {
1517 if ((r1
& msk
) == (r2
& msk
)) {
1526 uint32_t helper_eq_h(target_ulong r1
, target_ulong r2
)
1530 if ((r1
& 0xffff) == (r2
& 0xffff)) {
1534 if ((r1
& 0xffff0000) == (r2
& 0xffff0000)) {
1541 uint32_t helper_eqany_b(target_ulong r1
, target_ulong r2
)
1546 for (i
= 0; i
< 4; i
++) {
1547 ret
|= (sextract32(r1
, i
* 8, 8) == sextract32(r2
, i
* 8, 8));
1553 uint32_t helper_eqany_h(target_ulong r1
, target_ulong r2
)
1557 ret
= (sextract32(r1
, 0, 16) == sextract32(r2
, 0, 16));
1558 ret
|= (sextract32(r1
, 16, 16) == sextract32(r2
, 16, 16));
1563 uint32_t helper_lt_b(target_ulong r1
, target_ulong r2
)
1568 for (i
= 0; i
< 4; i
++) {
1569 if (sextract32(r1
, i
* 8, 8) < sextract32(r2
, i
* 8, 8)) {
1570 ret
|= (0xff << (i
* 8));
1577 uint32_t helper_lt_bu(target_ulong r1
, target_ulong r2
)
1582 for (i
= 0; i
< 4; i
++) {
1583 if (extract32(r1
, i
* 8, 8) < extract32(r2
, i
* 8, 8)) {
1584 ret
|= (0xff << (i
* 8));
1591 uint32_t helper_lt_h(target_ulong r1
, target_ulong r2
)
1595 if (sextract32(r1
, 0, 16) < sextract32(r2
, 0, 16)) {
1599 if (sextract32(r1
, 16, 16) < sextract32(r2
, 16, 16)) {
1606 uint32_t helper_lt_hu(target_ulong r1
, target_ulong r2
)
1610 if (extract32(r1
, 0, 16) < extract32(r2
, 0, 16)) {
1614 if (extract32(r1
, 16, 16) < extract32(r2
, 16, 16)) {
1621 #define EXTREMA_H_B(name, op) \
1622 uint32_t helper_##name ##_b(target_ulong r1, target_ulong r2) \
1624 int32_t i, extr_r1, extr_r2; \
1627 for (i = 0; i < 4; i++) { \
1628 extr_r1 = sextract32(r1, i * 8, 8); \
1629 extr_r2 = sextract32(r2, i * 8, 8); \
1630 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1631 ret |= (extr_r1 & 0xff) << (i * 8); \
1636 uint32_t helper_##name ##_bu(target_ulong r1, target_ulong r2)\
1639 uint32_t extr_r1, extr_r2; \
1642 for (i = 0; i < 4; i++) { \
1643 extr_r1 = extract32(r1, i * 8, 8); \
1644 extr_r2 = extract32(r2, i * 8, 8); \
1645 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1646 ret |= (extr_r1 & 0xff) << (i * 8); \
1651 uint32_t helper_##name ##_h(target_ulong r1, target_ulong r2) \
1653 int32_t extr_r1, extr_r2; \
1656 extr_r1 = sextract32(r1, 0, 16); \
1657 extr_r2 = sextract32(r2, 0, 16); \
1658 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1659 ret = ret & 0xffff; \
1661 extr_r1 = sextract32(r1, 16, 16); \
1662 extr_r2 = sextract32(r2, 16, 16); \
1663 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1664 ret |= extr_r1 << 16; \
1669 uint32_t helper_##name ##_hu(target_ulong r1, target_ulong r2)\
1671 uint32_t extr_r1, extr_r2; \
1674 extr_r1 = extract32(r1, 0, 16); \
1675 extr_r2 = extract32(r2, 0, 16); \
1676 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1677 ret = ret & 0xffff; \
1679 extr_r1 = extract32(r1, 16, 16); \
1680 extr_r2 = extract32(r2, 16, 16); \
1681 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1682 ret |= extr_r1 << (16); \
1687 uint64_t helper_ix##name(uint64_t r1, uint32_t r2) \
1689 int64_t r2l, r2h, r1hl; \
1692 ret = ((r1 + 2) & 0xffff); \
1693 r2l = sextract64(r2, 0, 16); \
1694 r2h = sextract64(r2, 16, 16); \
1695 r1hl = sextract64(r1, 32, 16); \
1697 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
1698 ret |= (r2l & 0xffff) << 32; \
1699 ret |= extract64(r1, 0, 16) << 16; \
1700 } else if ((r2h op r2l) && (r2h op r1hl)) { \
1701 ret |= extract64(r2, 16, 16) << 32; \
1702 ret |= extract64(r1 + 1, 0, 16) << 16; \
1704 ret |= r1 & 0xffffffff0000ull; \
1709 uint64_t helper_ix##name ##_u(uint64_t r1, uint32_t r2) \
1711 int64_t r2l, r2h, r1hl; \
1714 ret = ((r1 + 2) & 0xffff); \
1715 r2l = extract64(r2, 0, 16); \
1716 r2h = extract64(r2, 16, 16); \
1717 r1hl = extract64(r1, 32, 16); \
1719 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
1720 ret |= (r2l & 0xffff) << 32; \
1721 ret |= extract64(r1, 0, 16) << 16; \
1722 } else if ((r2h op r2l) && (r2h op r1hl)) { \
1723 ret |= extract64(r2, 16, 16) << 32; \
1724 ret |= extract64(r1 + 1, 0, 16) << 16; \
1726 ret |= r1 & 0xffffffff0000ull; \
1736 uint32_t helper_clo(target_ulong r1
)
1741 uint32_t helper_clo_h(target_ulong r1
)
1743 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1744 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1746 ret_hw0
= clo32(ret_hw0
<< 16);
1747 ret_hw1
= clo32(ret_hw1
<< 16);
1756 return ret_hw0
| (ret_hw1
<< 16);
1759 uint32_t helper_clz(target_ulong r1
)
1764 uint32_t helper_clz_h(target_ulong r1
)
1766 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1767 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1769 ret_hw0
= clz32(ret_hw0
<< 16);
1770 ret_hw1
= clz32(ret_hw1
<< 16);
1779 return ret_hw0
| (ret_hw1
<< 16);
1782 uint32_t helper_cls(target_ulong r1
)
1787 uint32_t helper_cls_h(target_ulong r1
)
1789 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1790 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1792 ret_hw0
= clrsb32(ret_hw0
<< 16);
1793 ret_hw1
= clrsb32(ret_hw1
<< 16);
1802 return ret_hw0
| (ret_hw1
<< 16);
1805 uint32_t helper_sh(target_ulong r1
, target_ulong r2
)
1807 int32_t shift_count
= sextract32(r2
, 0, 6);
1809 if (shift_count
== -32) {
1811 } else if (shift_count
< 0) {
1812 return r1
>> -shift_count
;
1814 return r1
<< shift_count
;
1818 uint32_t helper_sh_h(target_ulong r1
, target_ulong r2
)
1820 int32_t ret_hw0
, ret_hw1
;
1821 int32_t shift_count
;
1823 shift_count
= sextract32(r2
, 0, 5);
1825 if (shift_count
== -16) {
1827 } else if (shift_count
< 0) {
1828 ret_hw0
= extract32(r1
, 0, 16) >> -shift_count
;
1829 ret_hw1
= extract32(r1
, 16, 16) >> -shift_count
;
1830 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1832 ret_hw0
= extract32(r1
, 0, 16) << shift_count
;
1833 ret_hw1
= extract32(r1
, 16, 16) << shift_count
;
1834 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1838 uint32_t helper_sha(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1840 int32_t shift_count
;
1844 shift_count
= sextract32(r2
, 0, 6);
1845 t1
= sextract32(r1
, 0, 32);
1847 if (shift_count
== 0) {
1848 env
->PSW_USB_C
= env
->PSW_USB_V
= 0;
1850 } else if (shift_count
== -32) {
1851 env
->PSW_USB_C
= r1
;
1854 } else if (shift_count
> 0) {
1855 result
= t1
<< shift_count
;
1857 env
->PSW_USB_C
= ((result
& 0xffffffff00000000ULL
) != 0);
1859 env
->PSW_USB_V
= (((result
> 0x7fffffffLL
) ||
1860 (result
< -0x80000000LL
)) << 31);
1862 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1863 ret
= (uint32_t)result
;
1866 env
->PSW_USB_C
= (r1
& ((1 << -shift_count
) - 1));
1867 ret
= t1
>> -shift_count
;
1870 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1871 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1876 uint32_t helper_sha_h(target_ulong r1
, target_ulong r2
)
1878 int32_t shift_count
;
1879 int32_t ret_hw0
, ret_hw1
;
1881 shift_count
= sextract32(r2
, 0, 5);
1883 if (shift_count
== 0) {
1885 } else if (shift_count
< 0) {
1886 ret_hw0
= sextract32(r1
, 0, 16) >> -shift_count
;
1887 ret_hw1
= sextract32(r1
, 16, 16) >> -shift_count
;
1888 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1890 ret_hw0
= sextract32(r1
, 0, 16) << shift_count
;
1891 ret_hw1
= sextract32(r1
, 16, 16) << shift_count
;
1892 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1896 uint32_t helper_bmerge(target_ulong r1
, target_ulong r2
)
1901 for (i
= 0; i
< 16; i
++) {
1902 ret
|= (r1
& 1) << (2 * i
+ 1);
1903 ret
|= (r2
& 1) << (2 * i
);
1910 uint64_t helper_bsplit(uint32_t r1
)
1916 for (i
= 0; i
< 32; i
= i
+ 2) {
1918 ret
|= (r1
& 1) << (i
/2);
1921 ret
|= (uint64_t)(r1
& 1) << (i
/2 + 32);
1927 uint32_t helper_parity(target_ulong r1
)
1934 for (i
= 0; i
< 8; i
++) {
1940 for (i
= 0; i
< 8; i
++) {
1947 for (i
= 0; i
< 8; i
++) {
1954 for (i
= 0; i
< 8; i
++) {
1963 uint32_t helper_pack(uint32_t carry
, uint32_t r1_low
, uint32_t r1_high
,
1967 int32_t fp_exp
, fp_frac
, temp_exp
, fp_exp_frac
;
1968 int32_t int_exp
= r1_high
;
1969 int32_t int_mant
= r1_low
;
1970 uint32_t flag_rnd
= (int_mant
& (1 << 7)) && (
1971 (int_mant
& (1 << 8)) ||
1972 (int_mant
& 0x7f) ||
1974 if (((int_mant
& (1<<31)) == 0) && (int_exp
== 255)) {
1976 fp_frac
= extract32(int_mant
, 8, 23);
1977 } else if ((int_mant
& (1<<31)) && (int_exp
>= 127)) {
1980 } else if ((int_mant
& (1<<31)) && (int_exp
<= -128)) {
1983 } else if (int_mant
== 0) {
1987 if (((int_mant
& (1 << 31)) == 0)) {
1990 temp_exp
= int_exp
+ 128;
1992 fp_exp_frac
= (((temp_exp
& 0xff) << 23) |
1993 extract32(int_mant
, 8, 23))
1995 fp_exp
= extract32(fp_exp_frac
, 23, 8);
1996 fp_frac
= extract32(fp_exp_frac
, 0, 23);
1998 ret
= r2
& (1 << 31);
1999 ret
= ret
+ (fp_exp
<< 23);
2000 ret
= ret
+ (fp_frac
& 0x7fffff);
2005 uint64_t helper_unpack(target_ulong arg1
)
2007 int32_t fp_exp
= extract32(arg1
, 23, 8);
2008 int32_t fp_frac
= extract32(arg1
, 0, 23);
2010 int32_t int_exp
, int_mant
;
2012 if (fp_exp
== 255) {
2014 int_mant
= (fp_frac
<< 7);
2015 } else if ((fp_exp
== 0) && (fp_frac
== 0)) {
2018 } else if ((fp_exp
== 0) && (fp_frac
!= 0)) {
2020 int_mant
= (fp_frac
<< 7);
2022 int_exp
= fp_exp
- 127;
2023 int_mant
= (fp_frac
<< 7);
2024 int_mant
|= (1 << 30);
2033 uint64_t helper_dvinit_b_13(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2036 int32_t abs_sig_dividend
, abs_divisor
;
2038 ret
= sextract32(r1
, 0, 32);
2040 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2044 abs_sig_dividend
= abs((int32_t)r1
) >> 8;
2045 abs_divisor
= abs((int32_t)r2
);
2047 ofv if (a/b >= 255) <=> (a/255 >= b) */
2048 env
->PSW_USB_V
= (abs_sig_dividend
>= abs_divisor
) << 31;
2049 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2050 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2051 env
->PSW_USB_AV
= 0;
2056 uint64_t helper_dvinit_b_131(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2058 uint64_t ret
= sextract32(r1
, 0, 32);
2061 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2065 env
->PSW_USB_V
= ((r2
== 0) || ((r2
== 0xffffffff) && (r1
== 0xffffff80)));
2066 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2067 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2068 env
->PSW_USB_AV
= 0;
2073 uint64_t helper_dvinit_h_13(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2076 int32_t abs_sig_dividend
, abs_divisor
;
2078 ret
= sextract32(r1
, 0, 32);
2080 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2084 abs_sig_dividend
= abs((int32_t)r1
) >> 16;
2085 abs_divisor
= abs((int32_t)r2
);
2087 ofv if (a/b >= 0xffff) <=> (a/0xffff >= b) */
2088 env
->PSW_USB_V
= (abs_sig_dividend
>= abs_divisor
) << 31;
2089 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2090 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2091 env
->PSW_USB_AV
= 0;
2096 uint64_t helper_dvinit_h_131(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2098 uint64_t ret
= sextract32(r1
, 0, 32);
2101 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2105 env
->PSW_USB_V
= ((r2
== 0) || ((r2
== 0xffffffff) && (r1
== 0xffff8000)));
2106 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2107 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2108 env
->PSW_USB_AV
= 0;
2113 uint64_t helper_dvadj(uint64_t r1
, uint32_t r2
)
2115 int32_t x_sign
= (r1
>> 63);
2116 int32_t q_sign
= x_sign
^ (r2
>> 31);
2117 int32_t eq_pos
= x_sign
& ((r1
>> 32) == r2
);
2118 int32_t eq_neg
= x_sign
& ((r1
>> 32) == -r2
);
2122 if ((q_sign
& ~eq_neg
) | eq_pos
) {
2123 quotient
= (r1
+ 1) & 0xffffffff;
2125 quotient
= r1
& 0xffffffff;
2128 if (eq_pos
| eq_neg
) {
2131 remainder
= (r1
& 0xffffffff00000000ull
);
2133 return remainder
| quotient
;
2136 uint64_t helper_dvstep(uint64_t r1
, uint32_t r2
)
2138 int32_t dividend_sign
= extract64(r1
, 63, 1);
2139 int32_t divisor_sign
= extract32(r2
, 31, 1);
2140 int32_t quotient_sign
= (dividend_sign
!= divisor_sign
);
2141 int32_t addend
, dividend_quotient
, remainder
;
2144 if (quotient_sign
) {
2149 dividend_quotient
= (int32_t)r1
;
2150 remainder
= (int32_t)(r1
>> 32);
2152 for (i
= 0; i
< 8; i
++) {
2153 remainder
= (remainder
<< 1) | extract32(dividend_quotient
, 31, 1);
2154 dividend_quotient
<<= 1;
2155 temp
= remainder
+ addend
;
2156 if ((temp
< 0) == dividend_sign
) {
2159 if (((temp
< 0) == dividend_sign
)) {
2160 dividend_quotient
= dividend_quotient
| !quotient_sign
;
2162 dividend_quotient
= dividend_quotient
| quotient_sign
;
2165 return ((uint64_t)remainder
<< 32) | (uint32_t)dividend_quotient
;
2168 uint64_t helper_dvstep_u(uint64_t r1
, uint32_t r2
)
2170 int32_t dividend_quotient
= extract64(r1
, 0, 32);
2171 int64_t remainder
= extract64(r1
, 32, 32);
2174 for (i
= 0; i
< 8; i
++) {
2175 remainder
= (remainder
<< 1) | extract32(dividend_quotient
, 31, 1);
2176 dividend_quotient
<<= 1;
2177 temp
= (remainder
& 0xffffffff) - r2
;
2181 dividend_quotient
= dividend_quotient
| !(temp
< 0);
2183 return ((uint64_t)remainder
<< 32) | (uint32_t)dividend_quotient
;
2186 uint64_t helper_divide(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2188 int32_t quotient
, remainder
;
2189 int32_t dividend
= (int32_t)r1
;
2190 int32_t divisor
= (int32_t)r2
;
2193 if (dividend
>= 0) {
2194 quotient
= 0x7fffffff;
2197 quotient
= 0x80000000;
2200 env
->PSW_USB_V
= (1 << 31);
2201 } else if ((divisor
== 0xffffffff) && (dividend
== 0x80000000)) {
2202 quotient
= 0x7fffffff;
2204 env
->PSW_USB_V
= (1 << 31);
2206 remainder
= dividend
% divisor
;
2207 quotient
= (dividend
- remainder
)/divisor
;
2210 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2211 env
->PSW_USB_AV
= 0;
2212 return ((uint64_t)remainder
<< 32) | (uint32_t)quotient
;
2215 uint64_t helper_divide_u(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2217 uint32_t quotient
, remainder
;
2218 uint32_t dividend
= r1
;
2219 uint32_t divisor
= r2
;
2222 quotient
= 0xffffffff;
2224 env
->PSW_USB_V
= (1 << 31);
2226 remainder
= dividend
% divisor
;
2227 quotient
= (dividend
- remainder
)/divisor
;
2230 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2231 env
->PSW_USB_AV
= 0;
2232 return ((uint64_t)remainder
<< 32) | quotient
;
2235 uint64_t helper_mul_h(uint32_t arg00
, uint32_t arg01
,
2236 uint32_t arg10
, uint32_t arg11
, uint32_t n
)
2238 uint32_t result0
, result1
;
2240 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2241 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2242 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2243 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2245 result1
= 0x7fffffff;
2247 result1
= (((uint32_t)(arg00
* arg10
)) << n
);
2250 result0
= 0x7fffffff;
2252 result0
= (((uint32_t)(arg01
* arg11
)) << n
);
2254 return (((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
)
2308 stl_be_p(buf
, arg0
);
2310 return crc32(arg1
, buf
, 4);
2313 /* context save area (CSA) related helpers */
2315 static int cdc_increment(target_ulong
*psw
)
2317 if ((*psw
& MASK_PSW_CDC
) == 0x7f) {
2322 /* check for overflow */
2323 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2324 int mask
= (1u << (7 - lo
)) - 1;
2325 int count
= *psw
& mask
;
2333 static int cdc_decrement(target_ulong
*psw
)
2335 if ((*psw
& MASK_PSW_CDC
) == 0x7f) {
2338 /* check for underflow */
2339 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2340 int mask
= (1u << (7 - lo
)) - 1;
2341 int count
= *psw
& mask
;
2349 static bool cdc_zero(target_ulong
*psw
)
2351 int cdc
= *psw
& MASK_PSW_CDC
;
2352 /* Returns TRUE if PSW.CDC.COUNT == 0 or if PSW.CDC ==
2353 7'b1111111, otherwise returns FALSE. */
2357 /* find CDC.COUNT */
2358 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2359 int mask
= (1u << (7 - lo
)) - 1;
2360 int count
= *psw
& mask
;
2364 static void save_context_upper(CPUTriCoreState
*env
, int ea
)
2366 cpu_stl_data(env
, ea
, env
->PCXI
);
2367 cpu_stl_data(env
, ea
+4, psw_read(env
));
2368 cpu_stl_data(env
, ea
+8, env
->gpr_a
[10]);
2369 cpu_stl_data(env
, ea
+12, env
->gpr_a
[11]);
2370 cpu_stl_data(env
, ea
+16, env
->gpr_d
[8]);
2371 cpu_stl_data(env
, ea
+20, env
->gpr_d
[9]);
2372 cpu_stl_data(env
, ea
+24, env
->gpr_d
[10]);
2373 cpu_stl_data(env
, ea
+28, env
->gpr_d
[11]);
2374 cpu_stl_data(env
, ea
+32, env
->gpr_a
[12]);
2375 cpu_stl_data(env
, ea
+36, env
->gpr_a
[13]);
2376 cpu_stl_data(env
, ea
+40, env
->gpr_a
[14]);
2377 cpu_stl_data(env
, ea
+44, env
->gpr_a
[15]);
2378 cpu_stl_data(env
, ea
+48, env
->gpr_d
[12]);
2379 cpu_stl_data(env
, ea
+52, env
->gpr_d
[13]);
2380 cpu_stl_data(env
, ea
+56, env
->gpr_d
[14]);
2381 cpu_stl_data(env
, ea
+60, env
->gpr_d
[15]);
2384 static void save_context_lower(CPUTriCoreState
*env
, int ea
)
2386 cpu_stl_data(env
, ea
, env
->PCXI
);
2387 cpu_stl_data(env
, ea
+4, env
->gpr_a
[11]);
2388 cpu_stl_data(env
, ea
+8, env
->gpr_a
[2]);
2389 cpu_stl_data(env
, ea
+12, env
->gpr_a
[3]);
2390 cpu_stl_data(env
, ea
+16, env
->gpr_d
[0]);
2391 cpu_stl_data(env
, ea
+20, env
->gpr_d
[1]);
2392 cpu_stl_data(env
, ea
+24, env
->gpr_d
[2]);
2393 cpu_stl_data(env
, ea
+28, env
->gpr_d
[3]);
2394 cpu_stl_data(env
, ea
+32, env
->gpr_a
[4]);
2395 cpu_stl_data(env
, ea
+36, env
->gpr_a
[5]);
2396 cpu_stl_data(env
, ea
+40, env
->gpr_a
[6]);
2397 cpu_stl_data(env
, ea
+44, env
->gpr_a
[7]);
2398 cpu_stl_data(env
, ea
+48, env
->gpr_d
[4]);
2399 cpu_stl_data(env
, ea
+52, env
->gpr_d
[5]);
2400 cpu_stl_data(env
, ea
+56, env
->gpr_d
[6]);
2401 cpu_stl_data(env
, ea
+60, env
->gpr_d
[7]);
2404 static void restore_context_upper(CPUTriCoreState
*env
, int ea
,
2405 target_ulong
*new_PCXI
, target_ulong
*new_PSW
)
2407 *new_PCXI
= cpu_ldl_data(env
, ea
);
2408 *new_PSW
= cpu_ldl_data(env
, ea
+4);
2409 env
->gpr_a
[10] = cpu_ldl_data(env
, ea
+8);
2410 env
->gpr_a
[11] = cpu_ldl_data(env
, ea
+12);
2411 env
->gpr_d
[8] = cpu_ldl_data(env
, ea
+16);
2412 env
->gpr_d
[9] = cpu_ldl_data(env
, ea
+20);
2413 env
->gpr_d
[10] = cpu_ldl_data(env
, ea
+24);
2414 env
->gpr_d
[11] = cpu_ldl_data(env
, ea
+28);
2415 env
->gpr_a
[12] = cpu_ldl_data(env
, ea
+32);
2416 env
->gpr_a
[13] = cpu_ldl_data(env
, ea
+36);
2417 env
->gpr_a
[14] = cpu_ldl_data(env
, ea
+40);
2418 env
->gpr_a
[15] = cpu_ldl_data(env
, ea
+44);
2419 env
->gpr_d
[12] = cpu_ldl_data(env
, ea
+48);
2420 env
->gpr_d
[13] = cpu_ldl_data(env
, ea
+52);
2421 env
->gpr_d
[14] = cpu_ldl_data(env
, ea
+56);
2422 env
->gpr_d
[15] = cpu_ldl_data(env
, ea
+60);
2425 static void restore_context_lower(CPUTriCoreState
*env
, int ea
,
2426 target_ulong
*ra
, target_ulong
*pcxi
)
2428 *pcxi
= cpu_ldl_data(env
, ea
);
2429 *ra
= cpu_ldl_data(env
, ea
+4);
2430 env
->gpr_a
[2] = cpu_ldl_data(env
, ea
+8);
2431 env
->gpr_a
[3] = cpu_ldl_data(env
, ea
+12);
2432 env
->gpr_d
[0] = cpu_ldl_data(env
, ea
+16);
2433 env
->gpr_d
[1] = cpu_ldl_data(env
, ea
+20);
2434 env
->gpr_d
[2] = cpu_ldl_data(env
, ea
+24);
2435 env
->gpr_d
[3] = cpu_ldl_data(env
, ea
+28);
2436 env
->gpr_a
[4] = cpu_ldl_data(env
, ea
+32);
2437 env
->gpr_a
[5] = cpu_ldl_data(env
, ea
+36);
2438 env
->gpr_a
[6] = cpu_ldl_data(env
, ea
+40);
2439 env
->gpr_a
[7] = cpu_ldl_data(env
, ea
+44);
2440 env
->gpr_d
[4] = cpu_ldl_data(env
, ea
+48);
2441 env
->gpr_d
[5] = cpu_ldl_data(env
, ea
+52);
2442 env
->gpr_d
[6] = cpu_ldl_data(env
, ea
+56);
2443 env
->gpr_d
[7] = cpu_ldl_data(env
, ea
+60);
2446 void helper_call(CPUTriCoreState
*env
, uint32_t next_pc
)
2448 target_ulong tmp_FCX
;
2450 target_ulong new_FCX
;
2453 psw
= psw_read(env
);
2454 /* if (FCX == 0) trap(FCU); */
2455 if (env
->FCX
== 0) {
2457 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2459 /* if (PSW.CDE) then if (cdc_increment()) then trap(CDO); */
2460 if (psw
& MASK_PSW_CDE
) {
2461 if (cdc_increment(&psw
)) {
2463 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CDO
, GETPC());
2467 psw
|= MASK_PSW_CDE
;
2468 /* tmp_FCX = FCX; */
2470 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2471 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2472 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2473 /* new_FCX = M(EA, word); */
2474 new_FCX
= cpu_ldl_data(env
, ea
);
2475 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2476 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2478 save_context_upper(env
, ea
);
2480 /* PCXI.PCPN = ICR.CCPN; */
2481 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2482 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2483 /* PCXI.PIE = ICR.IE; */
2484 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
2485 ((env
->ICR
& MASK_ICR_IE
) << 15));
2487 env
->PCXI
|= MASK_PCXI_UL
;
2489 /* PCXI[19: 0] = FCX[19: 0]; */
2490 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2491 /* FCX[19: 0] = new_FCX[19: 0]; */
2492 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2493 /* A[11] = next_pc[31: 0]; */
2494 env
->gpr_a
[11] = next_pc
;
2496 /* if (tmp_FCX == LCX) trap(FCD);*/
2497 if (tmp_FCX
== env
->LCX
) {
2499 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2501 psw_write(env
, psw
);
2504 void helper_ret(CPUTriCoreState
*env
)
2507 target_ulong new_PCXI
;
2508 target_ulong new_PSW
, psw
;
2510 psw
= psw_read(env
);
2511 /* if (PSW.CDE) then if (cdc_decrement()) then trap(CDU);*/
2512 if (psw
& MASK_PSW_CDE
) {
2513 if (cdc_decrement(&psw
)) {
2515 psw_write(env
, psw
);
2516 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CDU
, GETPC());
2519 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2520 if ((env
->PCXI
& 0xfffff) == 0) {
2522 psw_write(env
, psw
);
2523 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2525 /* if (PCXI.UL == 0) then trap(CTYP); */
2526 if ((env
->PCXI
& MASK_PCXI_UL
) == 0) {
2528 cdc_increment(&psw
); /* restore to the start of helper */
2529 psw_write(env
, psw
);
2530 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2532 /* PC = {A11 [31: 1], 1’b0}; */
2533 env
->PC
= env
->gpr_a
[11] & 0xfffffffe;
2535 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
2536 ea
= ((env
->PCXI
& MASK_PCXI_PCXS
) << 12) +
2537 ((env
->PCXI
& MASK_PCXI_PCXO
) << 6);
2538 /* {new_PCXI, new_PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2539 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2540 restore_context_upper(env
, ea
, &new_PCXI
, &new_PSW
);
2541 /* M(EA, word) = FCX; */
2542 cpu_stl_data(env
, ea
, env
->FCX
);
2543 /* FCX[19: 0] = PCXI[19: 0]; */
2544 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2545 /* PCXI = new_PCXI; */
2546 env
->PCXI
= new_PCXI
;
2548 if (tricore_feature(env
, TRICORE_FEATURE_13
)) {
2550 psw_write(env
, new_PSW
);
2552 /* PSW = {new_PSW[31:26], PSW[25:24], new_PSW[23:0]}; */
2553 psw_write(env
, (new_PSW
& ~(0x3000000)) + (psw
& (0x3000000)));
2557 void helper_bisr(CPUTriCoreState
*env
, uint32_t const9
)
2559 target_ulong tmp_FCX
;
2561 target_ulong new_FCX
;
2563 if (env
->FCX
== 0) {
2565 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2569 ea
= ((env
->FCX
& 0xf0000) << 12) + ((env
->FCX
& 0xffff) << 6);
2571 /* new_FCX = M(EA, word); */
2572 new_FCX
= cpu_ldl_data(env
, ea
);
2573 /* M(EA, 16 * word) = {PCXI, A[11], A[2], A[3], D[0], D[1], D[2], D[3], A[4]
2574 , A[5], A[6], A[7], D[4], D[5], D[6], D[7]}; */
2575 save_context_lower(env
, ea
);
2578 /* PCXI.PCPN = ICR.CCPN */
2579 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2580 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2581 /* PCXI.PIE = ICR.IE */
2582 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
2583 ((env
->ICR
& MASK_ICR_IE
) << 15));
2585 env
->PCXI
&= ~(MASK_PCXI_UL
);
2586 /* PCXI[19: 0] = FCX[19: 0] */
2587 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2588 /* FXC[19: 0] = new_FCX[19: 0] */
2589 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2591 env
->ICR
|= MASK_ICR_IE
;
2593 env
->ICR
|= const9
; /* ICR.CCPN = const9[7: 0];*/
2595 if (tmp_FCX
== env
->LCX
) {
2597 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2601 void helper_rfe(CPUTriCoreState
*env
)
2604 target_ulong new_PCXI
;
2605 target_ulong new_PSW
;
2606 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2607 if ((env
->PCXI
& 0xfffff) == 0) {
2608 /* raise csu trap */
2609 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2611 /* if (PCXI.UL == 0) then trap(CTYP); */
2612 if ((env
->PCXI
& MASK_PCXI_UL
) == 0) {
2613 /* raise CTYP trap */
2614 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2616 /* if (!cdc_zero() AND PSW.CDE) then trap(NEST); */
2617 if (!cdc_zero(&(env
->PSW
)) && (env
->PSW
& MASK_PSW_CDE
)) {
2618 /* raise NEST trap */
2619 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_NEST
, GETPC());
2621 env
->PC
= env
->gpr_a
[11] & ~0x1;
2622 /* ICR.IE = PCXI.PIE; */
2623 env
->ICR
= (env
->ICR
& ~MASK_ICR_IE
) + ((env
->PCXI
& MASK_PCXI_PIE
) >> 15);
2624 /* ICR.CCPN = PCXI.PCPN; */
2625 env
->ICR
= (env
->ICR
& ~MASK_ICR_CCPN
) +
2626 ((env
->PCXI
& MASK_PCXI_PCPN
) >> 24);
2627 /*EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0};*/
2628 ea
= ((env
->PCXI
& MASK_PCXI_PCXS
) << 12) +
2629 ((env
->PCXI
& MASK_PCXI_PCXO
) << 6);
2630 /*{new_PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2631 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2632 restore_context_upper(env
, ea
, &new_PCXI
, &new_PSW
);
2633 /* M(EA, word) = FCX;*/
2634 cpu_stl_data(env
, ea
, env
->FCX
);
2635 /* FCX[19: 0] = PCXI[19: 0]; */
2636 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2637 /* PCXI = new_PCXI; */
2638 env
->PCXI
= new_PCXI
;
2640 psw_write(env
, new_PSW
);
2643 void helper_rfm(CPUTriCoreState
*env
)
2645 env
->PC
= (env
->gpr_a
[11] & ~0x1);
2646 /* ICR.IE = PCXI.PIE; */
2647 env
->ICR
= (env
->ICR
& ~MASK_ICR_IE
) |
2648 ((env
->PCXI
& MASK_PCXI_PIE
) >> 15);
2649 /* ICR.CCPN = PCXI.PCPN; */
2650 env
->ICR
= (env
->ICR
& ~MASK_ICR_CCPN
) |
2651 ((env
->PCXI
& MASK_PCXI_PCPN
) >> 24);
2652 /* {PCXI, PSW, A[10], A[11]} = M(DCX, 4 * word); */
2653 env
->PCXI
= cpu_ldl_data(env
, env
->DCX
);
2654 psw_write(env
, cpu_ldl_data(env
, env
->DCX
+4));
2655 env
->gpr_a
[10] = cpu_ldl_data(env
, env
->DCX
+8);
2656 env
->gpr_a
[11] = cpu_ldl_data(env
, env
->DCX
+12);
2658 if (tricore_feature(env
, TRICORE_FEATURE_131
)) {
2663 void helper_ldlcx(CPUTriCoreState
*env
, uint32_t ea
)
2666 /* insn doesn't load PCXI and RA */
2667 restore_context_lower(env
, ea
, &dummy
, &dummy
);
2670 void helper_lducx(CPUTriCoreState
*env
, uint32_t ea
)
2673 /* insn doesn't load PCXI and PSW */
2674 restore_context_upper(env
, ea
, &dummy
, &dummy
);
2677 void helper_stlcx(CPUTriCoreState
*env
, uint32_t ea
)
2679 save_context_lower(env
, ea
);
2682 void helper_stucx(CPUTriCoreState
*env
, uint32_t ea
)
2684 save_context_upper(env
, ea
);
2687 void helper_svlcx(CPUTriCoreState
*env
)
2689 target_ulong tmp_FCX
;
2691 target_ulong new_FCX
;
2693 if (env
->FCX
== 0) {
2695 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2697 /* tmp_FCX = FCX; */
2699 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2700 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2701 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2702 /* new_FCX = M(EA, word); */
2703 new_FCX
= cpu_ldl_data(env
, ea
);
2704 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2705 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2707 save_context_lower(env
, ea
);
2709 /* PCXI.PCPN = ICR.CCPN; */
2710 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2711 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2712 /* PCXI.PIE = ICR.IE; */
2713 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
2714 ((env
->ICR
& MASK_ICR_IE
) << 15));
2716 env
->PCXI
&= ~MASK_PCXI_UL
;
2718 /* PCXI[19: 0] = FCX[19: 0]; */
2719 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2720 /* FCX[19: 0] = new_FCX[19: 0]; */
2721 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2723 /* if (tmp_FCX == LCX) trap(FCD);*/
2724 if (tmp_FCX
== env
->LCX
) {
2726 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2730 void helper_svucx(CPUTriCoreState
*env
)
2732 target_ulong tmp_FCX
;
2734 target_ulong new_FCX
;
2736 if (env
->FCX
== 0) {
2738 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2740 /* tmp_FCX = FCX; */
2742 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2743 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2744 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2745 /* new_FCX = M(EA, word); */
2746 new_FCX
= cpu_ldl_data(env
, ea
);
2747 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2748 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2750 save_context_upper(env
, ea
);
2752 /* PCXI.PCPN = ICR.CCPN; */
2753 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2754 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2755 /* PCXI.PIE = ICR.IE; */
2756 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
2757 ((env
->ICR
& MASK_ICR_IE
) << 15));
2759 env
->PCXI
|= MASK_PCXI_UL
;
2761 /* PCXI[19: 0] = FCX[19: 0]; */
2762 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2763 /* FCX[19: 0] = new_FCX[19: 0]; */
2764 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2766 /* if (tmp_FCX == LCX) trap(FCD);*/
2767 if (tmp_FCX
== env
->LCX
) {
2769 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2773 void helper_rslcx(CPUTriCoreState
*env
)
2776 target_ulong new_PCXI
;
2777 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2778 if ((env
->PCXI
& 0xfffff) == 0) {
2780 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2782 /* if (PCXI.UL == 1) then trap(CTYP); */
2783 if ((env
->PCXI
& MASK_PCXI_UL
) != 0) {
2785 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2787 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
2788 ea
= ((env
->PCXI
& MASK_PCXI_PCXS
) << 12) +
2789 ((env
->PCXI
& MASK_PCXI_PCXO
) << 6);
2790 /* {new_PCXI, A[11], A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2791 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2792 restore_context_lower(env
, ea
, &env
->gpr_a
[11], &new_PCXI
);
2793 /* M(EA, word) = FCX; */
2794 cpu_stl_data(env
, ea
, env
->FCX
);
2795 /* M(EA, word) = FCX; */
2796 cpu_stl_data(env
, ea
, env
->FCX
);
2797 /* FCX[19: 0] = PCXI[19: 0]; */
2798 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2799 /* PCXI = new_PCXI; */
2800 env
->PCXI
= new_PCXI
;
2803 void helper_psw_write(CPUTriCoreState
*env
, uint32_t arg
)
2805 psw_write(env
, arg
);
2808 uint32_t helper_psw_read(CPUTriCoreState
*env
)
2810 return psw_read(env
);
2814 static inline void QEMU_NORETURN
do_raise_exception_err(CPUTriCoreState
*env
,
2819 CPUState
*cs
= CPU(tricore_env_get_cpu(env
));
2820 cs
->exception_index
= exception
;
2821 env
->error_code
= error_code
;
2824 /* now we have a real cpu fault */
2825 cpu_restore_state(cs
, pc
);
2831 void tlb_fill(CPUState
*cs
, target_ulong addr
, MMUAccessType access_type
,
2832 int mmu_idx
, uintptr_t retaddr
)
2835 ret
= cpu_tricore_handle_mmu_fault(cs
, addr
, access_type
, mmu_idx
);
2837 TriCoreCPU
*cpu
= TRICORE_CPU(cs
);
2838 CPUTriCoreState
*env
= &cpu
->env
;
2839 do_raise_exception_err(env
, cs
->exception_index
,
2840 env
->error_code
, retaddr
);