2 * Copyright (c) 2012-2014 Bastian Koppelmann C-Lab/University Paderborn
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 #include "qemu/osdep.h"
19 #include "qemu/host-utils.h"
20 #include "exec/helper-proto.h"
21 #include "exec/cpu_ldst.h"
22 #include <zlib.h> /* for crc32 */
25 /* Exception helpers */
27 static void QEMU_NORETURN
28 raise_exception_sync_internal(CPUTriCoreState
*env
, uint32_t class, int tin
,
29 uintptr_t pc
, uint32_t fcd_pc
)
31 CPUState
*cs
= CPU(tricore_env_get_cpu(env
));
32 /* in case we come from a helper-call we need to restore the PC */
34 cpu_restore_state(cs
, pc
);
37 /* Tin is loaded into d[15] */
40 if (class == TRAPC_CTX_MNG
&& tin
== TIN3_FCU
) {
41 /* upper context cannot be saved, if the context list is empty */
46 /* The return address in a[11] is updated */
47 if (class == TRAPC_CTX_MNG
&& tin
== TIN3_FCD
) {
48 env
->SYSCON
|= MASK_SYSCON_FCD_SF
;
49 /* when we run out of CSAs after saving a context a FCD trap is taken
50 and the return address is the start of the trap handler which used
52 env
->gpr_a
[11] = fcd_pc
;
53 } else if (class == TRAPC_SYSCALL
) {
54 env
->gpr_a
[11] = env
->PC
+ 4;
56 env
->gpr_a
[11] = env
->PC
;
58 /* The stack pointer in A[10] is set to the Interrupt Stack Pointer (ISP)
59 when the processor was not previously using the interrupt stack
60 (in case of PSW.IS = 0). The stack pointer bit is set for using the
61 interrupt stack: PSW.IS = 1. */
62 if ((env
->PSW
& MASK_PSW_IS
) == 0) {
63 env
->gpr_a
[10] = env
->ISP
;
65 env
->PSW
|= MASK_PSW_IS
;
66 /* The I/O mode is set to Supervisor mode, which means all permissions
67 are enabled: PSW.IO = 10 B .*/
68 env
->PSW
|= (2 << 10);
70 /*The current Protection Register Set is set to 0: PSW.PRS = 00 B .*/
71 env
->PSW
&= ~MASK_PSW_PRS
;
73 /* The Call Depth Counter (CDC) is cleared, and the call depth limit is
74 set for 64: PSW.CDC = 0000000 B .*/
75 env
->PSW
&= ~MASK_PSW_CDC
;
77 /* Call Depth Counter is enabled, PSW.CDE = 1. */
78 env
->PSW
|= MASK_PSW_CDE
;
80 /* Write permission to global registers A[0], A[1], A[8], A[9] is
81 disabled: PSW.GW = 0. */
82 env
->PSW
&= ~MASK_PSW_GW
;
84 /*The interrupt system is globally disabled: ICR.IE = 0. The ‘old’
85 ICR.IE and ICR.CCPN are saved */
87 /* PCXI.PIE = ICR.IE */
88 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
89 ((env
->ICR
& MASK_ICR_IE
) << 15));
90 /* PCXI.PCPN = ICR.CCPN */
91 env
->PCXI
= (env
->PCXI
& 0xffffff) +
92 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
93 /* Update PC using the trap vector table */
94 env
->PC
= env
->BTV
| (class << 5);
99 void helper_raise_exception_sync(CPUTriCoreState
*env
, uint32_t class,
102 raise_exception_sync_internal(env
, class, tin
, 0, 0);
105 static void raise_exception_sync_helper(CPUTriCoreState
*env
, uint32_t class,
106 uint32_t tin
, uintptr_t pc
)
108 raise_exception_sync_internal(env
, class, tin
, pc
, 0);
111 /* Addressing mode helper */
113 static uint16_t reverse16(uint16_t val
)
115 uint8_t high
= (uint8_t)(val
>> 8);
116 uint8_t low
= (uint8_t)(val
& 0xff);
120 rl
= (uint16_t)((high
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023);
121 rh
= (uint16_t)((low
* 0x0202020202ULL
& 0x010884422010ULL
) % 1023);
123 return (rh
<< 8) | rl
;
126 uint32_t helper_br_update(uint32_t reg
)
128 uint32_t index
= reg
& 0xffff;
129 uint32_t incr
= reg
>> 16;
130 uint32_t new_index
= reverse16(reverse16(index
) + reverse16(incr
));
131 return reg
- index
+ new_index
;
134 uint32_t helper_circ_update(uint32_t reg
, uint32_t off
)
136 uint32_t index
= reg
& 0xffff;
137 uint32_t length
= reg
>> 16;
138 int32_t new_index
= index
+ off
;
144 return reg
- index
+ new_index
;
147 static uint32_t ssov32(CPUTriCoreState
*env
, int64_t arg
)
150 int64_t max_pos
= INT32_MAX
;
151 int64_t max_neg
= INT32_MIN
;
153 env
->PSW_USB_V
= (1 << 31);
154 env
->PSW_USB_SV
= (1 << 31);
155 ret
= (target_ulong
)max_pos
;
158 env
->PSW_USB_V
= (1 << 31);
159 env
->PSW_USB_SV
= (1 << 31);
160 ret
= (target_ulong
)max_neg
;
163 ret
= (target_ulong
)arg
;
166 env
->PSW_USB_AV
= arg
^ arg
* 2u;
167 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
171 static uint32_t suov32_pos(CPUTriCoreState
*env
, uint64_t arg
)
174 uint64_t max_pos
= UINT32_MAX
;
176 env
->PSW_USB_V
= (1 << 31);
177 env
->PSW_USB_SV
= (1 << 31);
178 ret
= (target_ulong
)max_pos
;
181 ret
= (target_ulong
)arg
;
183 env
->PSW_USB_AV
= arg
^ arg
* 2u;
184 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
188 static uint32_t suov32_neg(CPUTriCoreState
*env
, int64_t arg
)
193 env
->PSW_USB_V
= (1 << 31);
194 env
->PSW_USB_SV
= (1 << 31);
198 ret
= (target_ulong
)arg
;
200 env
->PSW_USB_AV
= arg
^ arg
* 2u;
201 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
205 static uint32_t ssov16(CPUTriCoreState
*env
, int32_t hw0
, int32_t hw1
)
207 int32_t max_pos
= INT16_MAX
;
208 int32_t max_neg
= INT16_MIN
;
212 av0
= hw0
^ hw0
* 2u;
214 env
->PSW_USB_V
= (1 << 31);
216 } else if (hw0
< max_neg
) {
217 env
->PSW_USB_V
= (1 << 31);
221 av1
= hw1
^ hw1
* 2u;
223 env
->PSW_USB_V
= (1 << 31);
225 } else if (hw1
< max_neg
) {
226 env
->PSW_USB_V
= (1 << 31);
230 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
231 env
->PSW_USB_AV
= (av0
| av1
) << 16;
232 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
233 return (hw0
& 0xffff) | (hw1
<< 16);
236 static uint32_t suov16(CPUTriCoreState
*env
, int32_t hw0
, int32_t hw1
)
238 int32_t max_pos
= UINT16_MAX
;
242 av0
= hw0
^ hw0
* 2u;
244 env
->PSW_USB_V
= (1 << 31);
246 } else if (hw0
< 0) {
247 env
->PSW_USB_V
= (1 << 31);
251 av1
= hw1
^ hw1
* 2u;
253 env
->PSW_USB_V
= (1 << 31);
255 } else if (hw1
< 0) {
256 env
->PSW_USB_V
= (1 << 31);
260 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
261 env
->PSW_USB_AV
= (av0
| av1
) << 16;
262 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
263 return (hw0
& 0xffff) | (hw1
<< 16);
266 target_ulong
helper_add_ssov(CPUTriCoreState
*env
, target_ulong r1
,
269 int64_t t1
= sextract64(r1
, 0, 32);
270 int64_t t2
= sextract64(r2
, 0, 32);
271 int64_t result
= t1
+ t2
;
272 return ssov32(env
, result
);
275 uint64_t helper_add64_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
281 ovf
= (result
^ r1
) & ~(r1
^ r2
);
282 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
283 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
285 env
->PSW_USB_V
= (1 << 31);
286 env
->PSW_USB_SV
= (1 << 31);
287 /* ext_ret > MAX_INT */
288 if ((int64_t)r1
>= 0) {
290 /* ext_ret < MIN_INT */
300 target_ulong
helper_add_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
303 int32_t ret_hw0
, ret_hw1
;
305 ret_hw0
= sextract32(r1
, 0, 16) + sextract32(r2
, 0, 16);
306 ret_hw1
= sextract32(r1
, 16, 16) + sextract32(r2
, 16, 16);
307 return ssov16(env
, ret_hw0
, ret_hw1
);
310 uint32_t helper_addr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
313 int64_t mul_res0
= sextract64(r1
, 0, 32);
314 int64_t mul_res1
= sextract64(r1
, 32, 32);
315 int64_t r2_low
= sextract64(r2_l
, 0, 32);
316 int64_t r2_high
= sextract64(r2_h
, 0, 32);
317 int64_t result0
, result1
;
323 result0
= r2_low
+ mul_res0
+ 0x8000;
324 result1
= r2_high
+ mul_res1
+ 0x8000;
327 avf0
= result0
^ avf0
;
329 avf1
= result1
^ avf1
;
331 if (result0
> INT32_MAX
) {
334 } else if (result0
< INT32_MIN
) {
339 if (result1
> INT32_MAX
) {
342 } else if (result1
< INT32_MIN
) {
347 env
->PSW_USB_V
= ovf0
| ovf1
;
348 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
350 env
->PSW_USB_AV
= avf0
| avf1
;
351 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
353 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
356 uint32_t helper_addsur_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
359 int64_t mul_res0
= sextract64(r1
, 0, 32);
360 int64_t mul_res1
= sextract64(r1
, 32, 32);
361 int64_t r2_low
= sextract64(r2_l
, 0, 32);
362 int64_t r2_high
= sextract64(r2_h
, 0, 32);
363 int64_t result0
, result1
;
369 result0
= r2_low
- mul_res0
+ 0x8000;
370 result1
= r2_high
+ mul_res1
+ 0x8000;
373 avf0
= result0
^ avf0
;
375 avf1
= result1
^ avf1
;
377 if (result0
> INT32_MAX
) {
380 } else if (result0
< INT32_MIN
) {
385 if (result1
> INT32_MAX
) {
388 } else if (result1
< INT32_MIN
) {
393 env
->PSW_USB_V
= ovf0
| ovf1
;
394 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
396 env
->PSW_USB_AV
= avf0
| avf1
;
397 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
399 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
403 target_ulong
helper_add_suov(CPUTriCoreState
*env
, target_ulong r1
,
406 int64_t t1
= extract64(r1
, 0, 32);
407 int64_t t2
= extract64(r2
, 0, 32);
408 int64_t result
= t1
+ t2
;
409 return suov32_pos(env
, result
);
412 target_ulong
helper_add_h_suov(CPUTriCoreState
*env
, target_ulong r1
,
415 int32_t ret_hw0
, ret_hw1
;
417 ret_hw0
= extract32(r1
, 0, 16) + extract32(r2
, 0, 16);
418 ret_hw1
= extract32(r1
, 16, 16) + extract32(r2
, 16, 16);
419 return suov16(env
, ret_hw0
, ret_hw1
);
422 target_ulong
helper_sub_ssov(CPUTriCoreState
*env
, target_ulong r1
,
425 int64_t t1
= sextract64(r1
, 0, 32);
426 int64_t t2
= sextract64(r2
, 0, 32);
427 int64_t result
= t1
- t2
;
428 return ssov32(env
, result
);
431 uint64_t helper_sub64_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
437 ovf
= (result
^ r1
) & (r1
^ r2
);
438 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
439 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
441 env
->PSW_USB_V
= (1 << 31);
442 env
->PSW_USB_SV
= (1 << 31);
443 /* ext_ret > MAX_INT */
444 if ((int64_t)r1
>= 0) {
446 /* ext_ret < MIN_INT */
456 target_ulong
helper_sub_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
459 int32_t ret_hw0
, ret_hw1
;
461 ret_hw0
= sextract32(r1
, 0, 16) - sextract32(r2
, 0, 16);
462 ret_hw1
= sextract32(r1
, 16, 16) - sextract32(r2
, 16, 16);
463 return ssov16(env
, ret_hw0
, ret_hw1
);
466 uint32_t helper_subr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
469 int64_t mul_res0
= sextract64(r1
, 0, 32);
470 int64_t mul_res1
= sextract64(r1
, 32, 32);
471 int64_t r2_low
= sextract64(r2_l
, 0, 32);
472 int64_t r2_high
= sextract64(r2_h
, 0, 32);
473 int64_t result0
, result1
;
479 result0
= r2_low
- mul_res0
+ 0x8000;
480 result1
= r2_high
- mul_res1
+ 0x8000;
483 avf0
= result0
^ avf0
;
485 avf1
= result1
^ avf1
;
487 if (result0
> INT32_MAX
) {
490 } else if (result0
< INT32_MIN
) {
495 if (result1
> INT32_MAX
) {
498 } else if (result1
< INT32_MIN
) {
503 env
->PSW_USB_V
= ovf0
| ovf1
;
504 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
506 env
->PSW_USB_AV
= avf0
| avf1
;
507 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
509 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
512 uint32_t helper_subadr_h_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
515 int64_t mul_res0
= sextract64(r1
, 0, 32);
516 int64_t mul_res1
= sextract64(r1
, 32, 32);
517 int64_t r2_low
= sextract64(r2_l
, 0, 32);
518 int64_t r2_high
= sextract64(r2_h
, 0, 32);
519 int64_t result0
, result1
;
525 result0
= r2_low
+ mul_res0
+ 0x8000;
526 result1
= r2_high
- mul_res1
+ 0x8000;
529 avf0
= result0
^ avf0
;
531 avf1
= result1
^ avf1
;
533 if (result0
> INT32_MAX
) {
536 } else if (result0
< INT32_MIN
) {
541 if (result1
> INT32_MAX
) {
544 } else if (result1
< INT32_MIN
) {
549 env
->PSW_USB_V
= ovf0
| ovf1
;
550 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
552 env
->PSW_USB_AV
= avf0
| avf1
;
553 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
555 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
558 target_ulong
helper_sub_suov(CPUTriCoreState
*env
, target_ulong r1
,
561 int64_t t1
= extract64(r1
, 0, 32);
562 int64_t t2
= extract64(r2
, 0, 32);
563 int64_t result
= t1
- t2
;
564 return suov32_neg(env
, result
);
567 target_ulong
helper_sub_h_suov(CPUTriCoreState
*env
, target_ulong r1
,
570 int32_t ret_hw0
, ret_hw1
;
572 ret_hw0
= extract32(r1
, 0, 16) - extract32(r2
, 0, 16);
573 ret_hw1
= extract32(r1
, 16, 16) - extract32(r2
, 16, 16);
574 return suov16(env
, ret_hw0
, ret_hw1
);
577 target_ulong
helper_mul_ssov(CPUTriCoreState
*env
, target_ulong r1
,
580 int64_t t1
= sextract64(r1
, 0, 32);
581 int64_t t2
= sextract64(r2
, 0, 32);
582 int64_t result
= t1
* t2
;
583 return ssov32(env
, result
);
586 target_ulong
helper_mul_suov(CPUTriCoreState
*env
, target_ulong r1
,
589 int64_t t1
= extract64(r1
, 0, 32);
590 int64_t t2
= extract64(r2
, 0, 32);
591 int64_t result
= t1
* t2
;
593 return suov32_pos(env
, result
);
596 target_ulong
helper_sha_ssov(CPUTriCoreState
*env
, target_ulong r1
,
599 int64_t t1
= sextract64(r1
, 0, 32);
600 int32_t t2
= sextract64(r2
, 0, 6);
609 return ssov32(env
, result
);
612 uint32_t helper_abs_ssov(CPUTriCoreState
*env
, target_ulong r1
)
615 result
= ((int32_t)r1
>= 0) ? r1
: (0 - r1
);
616 return ssov32(env
, result
);
619 uint32_t helper_abs_h_ssov(CPUTriCoreState
*env
, target_ulong r1
)
621 int32_t ret_h0
, ret_h1
;
623 ret_h0
= sextract32(r1
, 0, 16);
624 ret_h0
= (ret_h0
>= 0) ? ret_h0
: (0 - ret_h0
);
626 ret_h1
= sextract32(r1
, 16, 16);
627 ret_h1
= (ret_h1
>= 0) ? ret_h1
: (0 - ret_h1
);
629 return ssov16(env
, ret_h0
, ret_h1
);
632 target_ulong
helper_absdif_ssov(CPUTriCoreState
*env
, target_ulong r1
,
635 int64_t t1
= sextract64(r1
, 0, 32);
636 int64_t t2
= sextract64(r2
, 0, 32);
644 return ssov32(env
, result
);
647 uint32_t helper_absdif_h_ssov(CPUTriCoreState
*env
, target_ulong r1
,
651 int32_t ret_h0
, ret_h1
;
653 t1
= sextract32(r1
, 0, 16);
654 t2
= sextract32(r2
, 0, 16);
661 t1
= sextract32(r1
, 16, 16);
662 t2
= sextract32(r2
, 16, 16);
669 return ssov16(env
, ret_h0
, ret_h1
);
672 target_ulong
helper_madd32_ssov(CPUTriCoreState
*env
, target_ulong r1
,
673 target_ulong r2
, target_ulong r3
)
675 int64_t t1
= sextract64(r1
, 0, 32);
676 int64_t t2
= sextract64(r2
, 0, 32);
677 int64_t t3
= sextract64(r3
, 0, 32);
680 result
= t2
+ (t1
* t3
);
681 return ssov32(env
, result
);
684 target_ulong
helper_madd32_suov(CPUTriCoreState
*env
, target_ulong r1
,
685 target_ulong r2
, target_ulong r3
)
687 uint64_t t1
= extract64(r1
, 0, 32);
688 uint64_t t2
= extract64(r2
, 0, 32);
689 uint64_t t3
= extract64(r3
, 0, 32);
692 result
= t2
+ (t1
* t3
);
693 return suov32_pos(env
, result
);
696 uint64_t helper_madd64_ssov(CPUTriCoreState
*env
, target_ulong r1
,
697 uint64_t r2
, target_ulong r3
)
700 int64_t t1
= sextract64(r1
, 0, 32);
701 int64_t t3
= sextract64(r3
, 0, 32);
706 ovf
= (ret
^ mul
) & ~(mul
^ r2
);
709 env
->PSW_USB_AV
= t1
^ t1
* 2u;
710 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
712 if ((int64_t)ovf
< 0) {
713 env
->PSW_USB_V
= (1 << 31);
714 env
->PSW_USB_SV
= (1 << 31);
715 /* ext_ret > MAX_INT */
718 /* ext_ret < MIN_INT */
730 helper_madd32_q_add_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
736 env
->PSW_USB_AV
= (result
^ result
* 2u);
737 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
739 /* we do the saturation by hand, since we produce an overflow on the host
740 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
741 case, we flip the saturated value. */
742 if (r2
== 0x8000000000000000LL
) {
743 if (result
> 0x7fffffffLL
) {
744 env
->PSW_USB_V
= (1 << 31);
745 env
->PSW_USB_SV
= (1 << 31);
747 } else if (result
< -0x80000000LL
) {
748 env
->PSW_USB_V
= (1 << 31);
749 env
->PSW_USB_SV
= (1 << 31);
755 if (result
> 0x7fffffffLL
) {
756 env
->PSW_USB_V
= (1 << 31);
757 env
->PSW_USB_SV
= (1 << 31);
759 } else if (result
< -0x80000000LL
) {
760 env
->PSW_USB_V
= (1 << 31);
761 env
->PSW_USB_SV
= (1 << 31);
767 return (uint32_t)result
;
770 uint64_t helper_madd64_q_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2
,
771 uint32_t r3
, uint32_t n
)
773 int64_t t1
= (int64_t)r1
;
774 int64_t t2
= sextract64(r2
, 0, 32);
775 int64_t t3
= sextract64(r3
, 0, 32);
779 mul
= (t2
* t3
) << n
;
782 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
783 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
785 ovf
= (result
^ mul
) & ~(mul
^ t1
);
786 /* we do the saturation by hand, since we produce an overflow on the host
787 if the mul was (0x80000000 * 0x80000000) << 1). If this is the
788 case, we flip the saturated value. */
789 if ((r2
== 0x80000000) && (r3
== 0x80000000) && (n
== 1)) {
791 env
->PSW_USB_V
= (1 << 31);
792 env
->PSW_USB_SV
= (1 << 31);
793 /* ext_ret > MAX_INT */
796 /* ext_ret < MIN_INT */
805 env
->PSW_USB_V
= (1 << 31);
806 env
->PSW_USB_SV
= (1 << 31);
807 /* ext_ret > MAX_INT */
810 /* ext_ret < MIN_INT */
818 return (uint64_t)result
;
821 uint32_t helper_maddr_q_ssov(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
822 uint32_t r3
, uint32_t n
)
824 int64_t t1
= sextract64(r1
, 0, 32);
825 int64_t t2
= sextract64(r2
, 0, 32);
826 int64_t t3
= sextract64(r3
, 0, 32);
829 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
832 mul
= (t2
* t3
) << n
;
835 ret
= t1
+ mul
+ 0x8000;
837 env
->PSW_USB_AV
= ret
^ ret
* 2u;
838 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
840 if (ret
> 0x7fffffffll
) {
841 env
->PSW_USB_V
= (1 << 31);
842 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
844 } else if (ret
< -0x80000000ll
) {
845 env
->PSW_USB_V
= (1 << 31);
846 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
851 return ret
& 0xffff0000ll
;
854 uint64_t helper_madd64_suov(CPUTriCoreState
*env
, target_ulong r1
,
855 uint64_t r2
, target_ulong r3
)
858 uint64_t t1
= extract64(r1
, 0, 32);
859 uint64_t t3
= extract64(r3
, 0, 32);
865 env
->PSW_USB_AV
= t1
^ t1
* 2u;
866 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
869 env
->PSW_USB_V
= (1 << 31);
870 env
->PSW_USB_SV
= (1 << 31);
879 target_ulong
helper_msub32_ssov(CPUTriCoreState
*env
, target_ulong r1
,
880 target_ulong r2
, target_ulong r3
)
882 int64_t t1
= sextract64(r1
, 0, 32);
883 int64_t t2
= sextract64(r2
, 0, 32);
884 int64_t t3
= sextract64(r3
, 0, 32);
887 result
= t2
- (t1
* t3
);
888 return ssov32(env
, result
);
891 target_ulong
helper_msub32_suov(CPUTriCoreState
*env
, target_ulong r1
,
892 target_ulong r2
, target_ulong r3
)
894 uint64_t t1
= extract64(r1
, 0, 32);
895 uint64_t t2
= extract64(r2
, 0, 32);
896 uint64_t t3
= extract64(r3
, 0, 32);
903 env
->PSW_USB_AV
= result
^ result
* 2u;
904 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
905 /* we calculate ovf by hand here, because the multiplication can overflow on
906 the host, which would give false results if we compare to less than
909 env
->PSW_USB_V
= (1 << 31);
910 env
->PSW_USB_SV
= (1 << 31);
918 uint64_t helper_msub64_ssov(CPUTriCoreState
*env
, target_ulong r1
,
919 uint64_t r2
, target_ulong r3
)
922 int64_t t1
= sextract64(r1
, 0, 32);
923 int64_t t3
= sextract64(r3
, 0, 32);
928 ovf
= (ret
^ r2
) & (mul
^ r2
);
931 env
->PSW_USB_AV
= t1
^ t1
* 2u;
932 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
934 if ((int64_t)ovf
< 0) {
935 env
->PSW_USB_V
= (1 << 31);
936 env
->PSW_USB_SV
= (1 << 31);
937 /* ext_ret > MAX_INT */
940 /* ext_ret < MIN_INT */
950 uint64_t helper_msub64_suov(CPUTriCoreState
*env
, target_ulong r1
,
951 uint64_t r2
, target_ulong r3
)
954 uint64_t t1
= extract64(r1
, 0, 32);
955 uint64_t t3
= extract64(r3
, 0, 32);
961 env
->PSW_USB_AV
= t1
^ t1
* 2u;
962 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
965 env
->PSW_USB_V
= (1 << 31);
966 env
->PSW_USB_SV
= (1 << 31);
976 helper_msub32_q_sub_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint64_t r2
)
979 int64_t t1
= (int64_t)r1
;
980 int64_t t2
= (int64_t)r2
;
984 env
->PSW_USB_AV
= (result
^ result
* 2u);
985 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
987 /* we do the saturation by hand, since we produce an overflow on the host
988 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
989 case, we flip the saturated value. */
990 if (r2
== 0x8000000000000000LL
) {
991 if (result
> 0x7fffffffLL
) {
992 env
->PSW_USB_V
= (1 << 31);
993 env
->PSW_USB_SV
= (1 << 31);
995 } else if (result
< -0x80000000LL
) {
996 env
->PSW_USB_V
= (1 << 31);
997 env
->PSW_USB_SV
= (1 << 31);
1003 if (result
> 0x7fffffffLL
) {
1004 env
->PSW_USB_V
= (1 << 31);
1005 env
->PSW_USB_SV
= (1 << 31);
1007 } else if (result
< -0x80000000LL
) {
1008 env
->PSW_USB_V
= (1 << 31);
1009 env
->PSW_USB_SV
= (1 << 31);
1015 return (uint32_t)result
;
1018 uint64_t helper_msub64_q_ssov(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2
,
1019 uint32_t r3
, uint32_t n
)
1021 int64_t t1
= (int64_t)r1
;
1022 int64_t t2
= sextract64(r2
, 0, 32);
1023 int64_t t3
= sextract64(r3
, 0, 32);
1024 int64_t result
, mul
;
1027 mul
= (t2
* t3
) << n
;
1030 env
->PSW_USB_AV
= (result
^ result
* 2u) >> 32;
1031 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1033 ovf
= (result
^ t1
) & (t1
^ mul
);
1034 /* we do the saturation by hand, since we produce an overflow on the host
1035 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
1036 case, we flip the saturated value. */
1037 if (mul
== 0x8000000000000000LL
) {
1039 env
->PSW_USB_V
= (1 << 31);
1040 env
->PSW_USB_SV
= (1 << 31);
1041 /* ext_ret > MAX_INT */
1044 /* ext_ret < MIN_INT */
1053 env
->PSW_USB_V
= (1 << 31);
1054 env
->PSW_USB_SV
= (1 << 31);
1055 /* ext_ret > MAX_INT */
1058 /* ext_ret < MIN_INT */
1067 return (uint64_t)result
;
1070 uint32_t helper_msubr_q_ssov(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
1071 uint32_t r3
, uint32_t n
)
1073 int64_t t1
= sextract64(r1
, 0, 32);
1074 int64_t t2
= sextract64(r2
, 0, 32);
1075 int64_t t3
= sextract64(r3
, 0, 32);
1078 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
1081 mul
= (t2
* t3
) << n
;
1084 ret
= t1
- mul
+ 0x8000;
1086 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1087 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1089 if (ret
> 0x7fffffffll
) {
1090 env
->PSW_USB_V
= (1 << 31);
1091 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1093 } else if (ret
< -0x80000000ll
) {
1094 env
->PSW_USB_V
= (1 << 31);
1095 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1100 return ret
& 0xffff0000ll
;
1103 uint32_t helper_abs_b(CPUTriCoreState
*env
, target_ulong arg
)
1110 for (i
= 0; i
< 4; i
++) {
1111 b
= sextract32(arg
, i
* 8, 8);
1112 b
= (b
>= 0) ? b
: (0 - b
);
1113 ovf
|= (b
> 0x7F) || (b
< -0x80);
1115 ret
|= (b
& 0xff) << (i
* 8);
1118 env
->PSW_USB_V
= ovf
<< 31;
1119 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1120 env
->PSW_USB_AV
= avf
<< 24;
1121 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1126 uint32_t helper_abs_h(CPUTriCoreState
*env
, target_ulong arg
)
1133 for (i
= 0; i
< 2; i
++) {
1134 h
= sextract32(arg
, i
* 16, 16);
1135 h
= (h
>= 0) ? h
: (0 - h
);
1136 ovf
|= (h
> 0x7FFF) || (h
< -0x8000);
1138 ret
|= (h
& 0xffff) << (i
* 16);
1141 env
->PSW_USB_V
= ovf
<< 31;
1142 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1143 env
->PSW_USB_AV
= avf
<< 16;
1144 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1149 uint32_t helper_absdif_b(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1157 for (i
= 0; i
< 4; i
++) {
1158 extr_r2
= sextract32(r2
, i
* 8, 8);
1159 b
= sextract32(r1
, i
* 8, 8);
1160 b
= (b
> extr_r2
) ? (b
- extr_r2
) : (extr_r2
- b
);
1161 ovf
|= (b
> 0x7F) || (b
< -0x80);
1163 ret
|= (b
& 0xff) << (i
* 8);
1166 env
->PSW_USB_V
= ovf
<< 31;
1167 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1168 env
->PSW_USB_AV
= avf
<< 24;
1169 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1173 uint32_t helper_absdif_h(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1181 for (i
= 0; i
< 2; i
++) {
1182 extr_r2
= sextract32(r2
, i
* 16, 16);
1183 h
= sextract32(r1
, i
* 16, 16);
1184 h
= (h
> extr_r2
) ? (h
- extr_r2
) : (extr_r2
- h
);
1185 ovf
|= (h
> 0x7FFF) || (h
< -0x8000);
1187 ret
|= (h
& 0xffff) << (i
* 16);
1190 env
->PSW_USB_V
= ovf
<< 31;
1191 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1192 env
->PSW_USB_AV
= avf
<< 16;
1193 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1198 uint32_t helper_addr_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1201 int64_t mul_res0
= sextract64(r1
, 0, 32);
1202 int64_t mul_res1
= sextract64(r1
, 32, 32);
1203 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1204 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1205 int64_t result0
, result1
;
1206 uint32_t ovf0
, ovf1
;
1207 uint32_t avf0
, avf1
;
1211 result0
= r2_low
+ mul_res0
+ 0x8000;
1212 result1
= r2_high
+ mul_res1
+ 0x8000;
1214 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1218 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1222 env
->PSW_USB_V
= ovf0
| ovf1
;
1223 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1225 avf0
= result0
* 2u;
1226 avf0
= result0
^ avf0
;
1227 avf1
= result1
* 2u;
1228 avf1
= result1
^ avf1
;
1230 env
->PSW_USB_AV
= avf0
| avf1
;
1231 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1233 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1236 uint32_t helper_addsur_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1239 int64_t mul_res0
= sextract64(r1
, 0, 32);
1240 int64_t mul_res1
= sextract64(r1
, 32, 32);
1241 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1242 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1243 int64_t result0
, result1
;
1244 uint32_t ovf0
, ovf1
;
1245 uint32_t avf0
, avf1
;
1249 result0
= r2_low
- mul_res0
+ 0x8000;
1250 result1
= r2_high
+ mul_res1
+ 0x8000;
1252 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1256 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1260 env
->PSW_USB_V
= ovf0
| ovf1
;
1261 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1263 avf0
= result0
* 2u;
1264 avf0
= result0
^ avf0
;
1265 avf1
= result1
* 2u;
1266 avf1
= result1
^ avf1
;
1268 env
->PSW_USB_AV
= avf0
| avf1
;
1269 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1271 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1274 uint32_t helper_maddr_q(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
1275 uint32_t r3
, uint32_t n
)
1277 int64_t t1
= sextract64(r1
, 0, 32);
1278 int64_t t2
= sextract64(r2
, 0, 32);
1279 int64_t t3
= sextract64(r3
, 0, 32);
1282 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
1285 mul
= (t2
* t3
) << n
;
1288 ret
= t1
+ mul
+ 0x8000;
1290 if ((ret
> 0x7fffffffll
) || (ret
< -0x80000000ll
)) {
1291 env
->PSW_USB_V
= (1 << 31);
1292 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1296 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1297 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1299 return ret
& 0xffff0000ll
;
1302 uint32_t helper_add_b(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1305 int32_t extr_r1
, extr_r2
;
1310 for (i
= 0; i
< 4; i
++) {
1311 extr_r1
= sextract32(r1
, i
* 8, 8);
1312 extr_r2
= sextract32(r2
, i
* 8, 8);
1314 b
= extr_r1
+ extr_r2
;
1315 ovf
|= ((b
> 0x7f) || (b
< -0x80));
1317 ret
|= ((b
& 0xff) << (i
*8));
1320 env
->PSW_USB_V
= (ovf
<< 31);
1321 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1322 env
->PSW_USB_AV
= avf
<< 24;
1323 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1328 uint32_t helper_add_h(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1331 int32_t extr_r1
, extr_r2
;
1336 for (i
= 0; i
< 2; i
++) {
1337 extr_r1
= sextract32(r1
, i
* 16, 16);
1338 extr_r2
= sextract32(r2
, i
* 16, 16);
1339 h
= extr_r1
+ extr_r2
;
1340 ovf
|= ((h
> 0x7fff) || (h
< -0x8000));
1342 ret
|= (h
& 0xffff) << (i
* 16);
1345 env
->PSW_USB_V
= (ovf
<< 31);
1346 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1347 env
->PSW_USB_AV
= (avf
<< 16);
1348 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1353 uint32_t helper_subr_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1356 int64_t mul_res0
= sextract64(r1
, 0, 32);
1357 int64_t mul_res1
= sextract64(r1
, 32, 32);
1358 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1359 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1360 int64_t result0
, result1
;
1361 uint32_t ovf0
, ovf1
;
1362 uint32_t avf0
, avf1
;
1366 result0
= r2_low
- mul_res0
+ 0x8000;
1367 result1
= r2_high
- mul_res1
+ 0x8000;
1369 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1373 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1377 env
->PSW_USB_V
= ovf0
| ovf1
;
1378 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1380 avf0
= result0
* 2u;
1381 avf0
= result0
^ avf0
;
1382 avf1
= result1
* 2u;
1383 avf1
= result1
^ avf1
;
1385 env
->PSW_USB_AV
= avf0
| avf1
;
1386 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1388 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1391 uint32_t helper_subadr_h(CPUTriCoreState
*env
, uint64_t r1
, uint32_t r2_l
,
1394 int64_t mul_res0
= sextract64(r1
, 0, 32);
1395 int64_t mul_res1
= sextract64(r1
, 32, 32);
1396 int64_t r2_low
= sextract64(r2_l
, 0, 32);
1397 int64_t r2_high
= sextract64(r2_h
, 0, 32);
1398 int64_t result0
, result1
;
1399 uint32_t ovf0
, ovf1
;
1400 uint32_t avf0
, avf1
;
1404 result0
= r2_low
+ mul_res0
+ 0x8000;
1405 result1
= r2_high
- mul_res1
+ 0x8000;
1407 if ((result0
> INT32_MAX
) || (result0
< INT32_MIN
)) {
1411 if ((result1
> INT32_MAX
) || (result1
< INT32_MIN
)) {
1415 env
->PSW_USB_V
= ovf0
| ovf1
;
1416 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1418 avf0
= result0
* 2u;
1419 avf0
= result0
^ avf0
;
1420 avf1
= result1
* 2u;
1421 avf1
= result1
^ avf1
;
1423 env
->PSW_USB_AV
= avf0
| avf1
;
1424 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1426 return (result1
& 0xffff0000ULL
) | ((result0
>> 16) & 0xffffULL
);
1429 uint32_t helper_msubr_q(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
,
1430 uint32_t r3
, uint32_t n
)
1432 int64_t t1
= sextract64(r1
, 0, 32);
1433 int64_t t2
= sextract64(r2
, 0, 32);
1434 int64_t t3
= sextract64(r3
, 0, 32);
1437 if ((t2
== -0x8000ll
) && (t3
== -0x8000ll
) && (n
== 1)) {
1440 mul
= (t2
* t3
) << n
;
1443 ret
= t1
- mul
+ 0x8000;
1445 if ((ret
> 0x7fffffffll
) || (ret
< -0x80000000ll
)) {
1446 env
->PSW_USB_V
= (1 << 31);
1447 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1451 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1452 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1454 return ret
& 0xffff0000ll
;
1457 uint32_t helper_sub_b(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1460 int32_t extr_r1
, extr_r2
;
1465 for (i
= 0; i
< 4; i
++) {
1466 extr_r1
= sextract32(r1
, i
* 8, 8);
1467 extr_r2
= sextract32(r2
, i
* 8, 8);
1469 b
= extr_r1
- extr_r2
;
1470 ovf
|= ((b
> 0x7f) || (b
< -0x80));
1472 ret
|= ((b
& 0xff) << (i
*8));
1475 env
->PSW_USB_V
= (ovf
<< 31);
1476 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1477 env
->PSW_USB_AV
= avf
<< 24;
1478 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1483 uint32_t helper_sub_h(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1486 int32_t extr_r1
, extr_r2
;
1491 for (i
= 0; i
< 2; i
++) {
1492 extr_r1
= sextract32(r1
, i
* 16, 16);
1493 extr_r2
= sextract32(r2
, i
* 16, 16);
1494 h
= extr_r1
- extr_r2
;
1495 ovf
|= ((h
> 0x7fff) || (h
< -0x8000));
1497 ret
|= (h
& 0xffff) << (i
* 16);
1500 env
->PSW_USB_V
= (ovf
<< 31);
1501 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1502 env
->PSW_USB_AV
= avf
<< 16;
1503 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1508 uint32_t helper_eq_b(target_ulong r1
, target_ulong r2
)
1515 for (i
= 0; i
< 4; i
++) {
1516 if ((r1
& msk
) == (r2
& msk
)) {
1525 uint32_t helper_eq_h(target_ulong r1
, target_ulong r2
)
1529 if ((r1
& 0xffff) == (r2
& 0xffff)) {
1533 if ((r1
& 0xffff0000) == (r2
& 0xffff0000)) {
1540 uint32_t helper_eqany_b(target_ulong r1
, target_ulong r2
)
1545 for (i
= 0; i
< 4; i
++) {
1546 ret
|= (sextract32(r1
, i
* 8, 8) == sextract32(r2
, i
* 8, 8));
1552 uint32_t helper_eqany_h(target_ulong r1
, target_ulong r2
)
1556 ret
= (sextract32(r1
, 0, 16) == sextract32(r2
, 0, 16));
1557 ret
|= (sextract32(r1
, 16, 16) == sextract32(r2
, 16, 16));
1562 uint32_t helper_lt_b(target_ulong r1
, target_ulong r2
)
1567 for (i
= 0; i
< 4; i
++) {
1568 if (sextract32(r1
, i
* 8, 8) < sextract32(r2
, i
* 8, 8)) {
1569 ret
|= (0xff << (i
* 8));
1576 uint32_t helper_lt_bu(target_ulong r1
, target_ulong r2
)
1581 for (i
= 0; i
< 4; i
++) {
1582 if (extract32(r1
, i
* 8, 8) < extract32(r2
, i
* 8, 8)) {
1583 ret
|= (0xff << (i
* 8));
1590 uint32_t helper_lt_h(target_ulong r1
, target_ulong r2
)
1594 if (sextract32(r1
, 0, 16) < sextract32(r2
, 0, 16)) {
1598 if (sextract32(r1
, 16, 16) < sextract32(r2
, 16, 16)) {
1605 uint32_t helper_lt_hu(target_ulong r1
, target_ulong r2
)
1609 if (extract32(r1
, 0, 16) < extract32(r2
, 0, 16)) {
1613 if (extract32(r1
, 16, 16) < extract32(r2
, 16, 16)) {
1620 #define EXTREMA_H_B(name, op) \
1621 uint32_t helper_##name ##_b(target_ulong r1, target_ulong r2) \
1623 int32_t i, extr_r1, extr_r2; \
1626 for (i = 0; i < 4; i++) { \
1627 extr_r1 = sextract32(r1, i * 8, 8); \
1628 extr_r2 = sextract32(r2, i * 8, 8); \
1629 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1630 ret |= (extr_r1 & 0xff) << (i * 8); \
1635 uint32_t helper_##name ##_bu(target_ulong r1, target_ulong r2)\
1638 uint32_t extr_r1, extr_r2; \
1641 for (i = 0; i < 4; i++) { \
1642 extr_r1 = extract32(r1, i * 8, 8); \
1643 extr_r2 = extract32(r2, i * 8, 8); \
1644 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1645 ret |= (extr_r1 & 0xff) << (i * 8); \
1650 uint32_t helper_##name ##_h(target_ulong r1, target_ulong r2) \
1652 int32_t extr_r1, extr_r2; \
1655 extr_r1 = sextract32(r1, 0, 16); \
1656 extr_r2 = sextract32(r2, 0, 16); \
1657 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1658 ret = ret & 0xffff; \
1660 extr_r1 = sextract32(r1, 16, 16); \
1661 extr_r2 = sextract32(r2, 16, 16); \
1662 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1663 ret |= extr_r1 << 16; \
1668 uint32_t helper_##name ##_hu(target_ulong r1, target_ulong r2)\
1670 uint32_t extr_r1, extr_r2; \
1673 extr_r1 = extract32(r1, 0, 16); \
1674 extr_r2 = extract32(r2, 0, 16); \
1675 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1676 ret = ret & 0xffff; \
1678 extr_r1 = extract32(r1, 16, 16); \
1679 extr_r2 = extract32(r2, 16, 16); \
1680 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1681 ret |= extr_r1 << (16); \
1686 uint64_t helper_ix##name(uint64_t r1, uint32_t r2) \
1688 int64_t r2l, r2h, r1hl; \
1691 ret = ((r1 + 2) & 0xffff); \
1692 r2l = sextract64(r2, 0, 16); \
1693 r2h = sextract64(r2, 16, 16); \
1694 r1hl = sextract64(r1, 32, 16); \
1696 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
1697 ret |= (r2l & 0xffff) << 32; \
1698 ret |= extract64(r1, 0, 16) << 16; \
1699 } else if ((r2h op r2l) && (r2h op r1hl)) { \
1700 ret |= extract64(r2, 16, 16) << 32; \
1701 ret |= extract64(r1 + 1, 0, 16) << 16; \
1703 ret |= r1 & 0xffffffff0000ull; \
1708 uint64_t helper_ix##name ##_u(uint64_t r1, uint32_t r2) \
1710 int64_t r2l, r2h, r1hl; \
1713 ret = ((r1 + 2) & 0xffff); \
1714 r2l = extract64(r2, 0, 16); \
1715 r2h = extract64(r2, 16, 16); \
1716 r1hl = extract64(r1, 32, 16); \
1718 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
1719 ret |= (r2l & 0xffff) << 32; \
1720 ret |= extract64(r1, 0, 16) << 16; \
1721 } else if ((r2h op r2l) && (r2h op r1hl)) { \
1722 ret |= extract64(r2, 16, 16) << 32; \
1723 ret |= extract64(r1 + 1, 0, 16) << 16; \
1725 ret |= r1 & 0xffffffff0000ull; \
1735 uint32_t helper_clo(target_ulong r1
)
1740 uint32_t helper_clo_h(target_ulong r1
)
1742 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1743 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1745 ret_hw0
= clo32(ret_hw0
<< 16);
1746 ret_hw1
= clo32(ret_hw1
<< 16);
1755 return ret_hw0
| (ret_hw1
<< 16);
1758 uint32_t helper_clz(target_ulong r1
)
1763 uint32_t helper_clz_h(target_ulong r1
)
1765 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1766 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1768 ret_hw0
= clz32(ret_hw0
<< 16);
1769 ret_hw1
= clz32(ret_hw1
<< 16);
1778 return ret_hw0
| (ret_hw1
<< 16);
1781 uint32_t helper_cls(target_ulong r1
)
1786 uint32_t helper_cls_h(target_ulong r1
)
1788 uint32_t ret_hw0
= extract32(r1
, 0, 16);
1789 uint32_t ret_hw1
= extract32(r1
, 16, 16);
1791 ret_hw0
= clrsb32(ret_hw0
<< 16);
1792 ret_hw1
= clrsb32(ret_hw1
<< 16);
1801 return ret_hw0
| (ret_hw1
<< 16);
1804 uint32_t helper_sh(target_ulong r1
, target_ulong r2
)
1806 int32_t shift_count
= sextract32(r2
, 0, 6);
1808 if (shift_count
== -32) {
1810 } else if (shift_count
< 0) {
1811 return r1
>> -shift_count
;
1813 return r1
<< shift_count
;
1817 uint32_t helper_sh_h(target_ulong r1
, target_ulong r2
)
1819 int32_t ret_hw0
, ret_hw1
;
1820 int32_t shift_count
;
1822 shift_count
= sextract32(r2
, 0, 5);
1824 if (shift_count
== -16) {
1826 } else if (shift_count
< 0) {
1827 ret_hw0
= extract32(r1
, 0, 16) >> -shift_count
;
1828 ret_hw1
= extract32(r1
, 16, 16) >> -shift_count
;
1829 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1831 ret_hw0
= extract32(r1
, 0, 16) << shift_count
;
1832 ret_hw1
= extract32(r1
, 16, 16) << shift_count
;
1833 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1837 uint32_t helper_sha(CPUTriCoreState
*env
, target_ulong r1
, target_ulong r2
)
1839 int32_t shift_count
;
1843 shift_count
= sextract32(r2
, 0, 6);
1844 t1
= sextract32(r1
, 0, 32);
1846 if (shift_count
== 0) {
1847 env
->PSW_USB_C
= env
->PSW_USB_V
= 0;
1849 } else if (shift_count
== -32) {
1850 env
->PSW_USB_C
= r1
;
1853 } else if (shift_count
> 0) {
1854 result
= t1
<< shift_count
;
1856 env
->PSW_USB_C
= ((result
& 0xffffffff00000000ULL
) != 0);
1858 env
->PSW_USB_V
= (((result
> 0x7fffffffLL
) ||
1859 (result
< -0x80000000LL
)) << 31);
1861 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
1862 ret
= (uint32_t)result
;
1865 env
->PSW_USB_C
= (r1
& ((1 << -shift_count
) - 1));
1866 ret
= t1
>> -shift_count
;
1869 env
->PSW_USB_AV
= ret
^ ret
* 2u;
1870 env
->PSW_USB_SAV
|= env
->PSW_USB_AV
;
1875 uint32_t helper_sha_h(target_ulong r1
, target_ulong r2
)
1877 int32_t shift_count
;
1878 int32_t ret_hw0
, ret_hw1
;
1880 shift_count
= sextract32(r2
, 0, 5);
1882 if (shift_count
== 0) {
1884 } else if (shift_count
< 0) {
1885 ret_hw0
= sextract32(r1
, 0, 16) >> -shift_count
;
1886 ret_hw1
= sextract32(r1
, 16, 16) >> -shift_count
;
1887 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1889 ret_hw0
= sextract32(r1
, 0, 16) << shift_count
;
1890 ret_hw1
= sextract32(r1
, 16, 16) << shift_count
;
1891 return (ret_hw0
& 0xffff) | (ret_hw1
<< 16);
1895 uint32_t helper_bmerge(target_ulong r1
, target_ulong r2
)
1900 for (i
= 0; i
< 16; i
++) {
1901 ret
|= (r1
& 1) << (2 * i
+ 1);
1902 ret
|= (r2
& 1) << (2 * i
);
1909 uint64_t helper_bsplit(uint32_t r1
)
1915 for (i
= 0; i
< 32; i
= i
+ 2) {
1917 ret
|= (r1
& 1) << (i
/2);
1920 ret
|= (uint64_t)(r1
& 1) << (i
/2 + 32);
1926 uint32_t helper_parity(target_ulong r1
)
1933 for (i
= 0; i
< 8; i
++) {
1939 for (i
= 0; i
< 8; i
++) {
1946 for (i
= 0; i
< 8; i
++) {
1953 for (i
= 0; i
< 8; i
++) {
1962 uint32_t helper_pack(uint32_t carry
, uint32_t r1_low
, uint32_t r1_high
,
1966 int32_t fp_exp
, fp_frac
, temp_exp
, fp_exp_frac
;
1967 int32_t int_exp
= r1_high
;
1968 int32_t int_mant
= r1_low
;
1969 uint32_t flag_rnd
= (int_mant
& (1 << 7)) && (
1970 (int_mant
& (1 << 8)) ||
1971 (int_mant
& 0x7f) ||
1973 if (((int_mant
& (1<<31)) == 0) && (int_exp
== 255)) {
1975 fp_frac
= extract32(int_mant
, 8, 23);
1976 } else if ((int_mant
& (1<<31)) && (int_exp
>= 127)) {
1979 } else if ((int_mant
& (1<<31)) && (int_exp
<= -128)) {
1982 } else if (int_mant
== 0) {
1986 if (((int_mant
& (1 << 31)) == 0)) {
1989 temp_exp
= int_exp
+ 128;
1991 fp_exp_frac
= (((temp_exp
& 0xff) << 23) |
1992 extract32(int_mant
, 8, 23))
1994 fp_exp
= extract32(fp_exp_frac
, 23, 8);
1995 fp_frac
= extract32(fp_exp_frac
, 0, 23);
1997 ret
= r2
& (1 << 31);
1998 ret
= ret
+ (fp_exp
<< 23);
1999 ret
= ret
+ (fp_frac
& 0x7fffff);
2004 uint64_t helper_unpack(target_ulong arg1
)
2006 int32_t fp_exp
= extract32(arg1
, 23, 8);
2007 int32_t fp_frac
= extract32(arg1
, 0, 23);
2009 int32_t int_exp
, int_mant
;
2011 if (fp_exp
== 255) {
2013 int_mant
= (fp_frac
<< 7);
2014 } else if ((fp_exp
== 0) && (fp_frac
== 0)) {
2017 } else if ((fp_exp
== 0) && (fp_frac
!= 0)) {
2019 int_mant
= (fp_frac
<< 7);
2021 int_exp
= fp_exp
- 127;
2022 int_mant
= (fp_frac
<< 7);
2023 int_mant
|= (1 << 30);
2032 uint64_t helper_dvinit_b_13(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2035 int32_t abs_sig_dividend
, abs_divisor
;
2037 ret
= sextract32(r1
, 0, 32);
2039 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2043 abs_sig_dividend
= abs((int32_t)r1
) >> 8;
2044 abs_divisor
= abs((int32_t)r2
);
2046 ofv if (a/b >= 255) <=> (a/255 >= b) */
2047 env
->PSW_USB_V
= (abs_sig_dividend
>= abs_divisor
) << 31;
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_b_131(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2057 uint64_t ret
= sextract32(r1
, 0, 32);
2060 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2064 env
->PSW_USB_V
= ((r2
== 0) || ((r2
== 0xffffffff) && (r1
== 0xffffff80)));
2065 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2066 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2067 env
->PSW_USB_AV
= 0;
2072 uint64_t helper_dvinit_h_13(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2075 int32_t abs_sig_dividend
, abs_divisor
;
2077 ret
= sextract32(r1
, 0, 32);
2079 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2083 abs_sig_dividend
= abs((int32_t)r1
) >> 16;
2084 abs_divisor
= abs((int32_t)r2
);
2086 ofv if (a/b >= 0xffff) <=> (a/0xffff >= b) */
2087 env
->PSW_USB_V
= (abs_sig_dividend
>= abs_divisor
) << 31;
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_dvinit_h_131(CPUTriCoreState
*env
, uint32_t r1
, uint32_t r2
)
2097 uint64_t ret
= sextract32(r1
, 0, 32);
2100 if (!((r1
& 0x80000000) == (r2
& 0x80000000))) {
2104 env
->PSW_USB_V
= ((r2
== 0) || ((r2
== 0xffffffff) && (r1
== 0xffff8000)));
2105 env
->PSW_USB_V
= env
->PSW_USB_V
<< 31;
2106 env
->PSW_USB_SV
|= env
->PSW_USB_V
;
2107 env
->PSW_USB_AV
= 0;
2112 uint64_t helper_dvadj(uint64_t r1
, uint32_t r2
)
2114 int32_t x_sign
= (r1
>> 63);
2115 int32_t q_sign
= x_sign
^ (r2
>> 31);
2116 int32_t eq_pos
= x_sign
& ((r1
>> 32) == r2
);
2117 int32_t eq_neg
= x_sign
& ((r1
>> 32) == -r2
);
2119 uint64_t ret
, remainder
;
2121 if ((q_sign
& ~eq_neg
) | eq_pos
) {
2122 quotient
= (r1
+ 1) & 0xffffffff;
2124 quotient
= r1
& 0xffffffff;
2127 if (eq_pos
| eq_neg
) {
2130 remainder
= (r1
& 0xffffffff00000000ull
);
2132 ret
= 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
)
2239 uint32_t result0
, result1
;
2241 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2242 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2243 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2244 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2246 result1
= 0x7fffffff;
2248 result1
= (((uint32_t)(arg00
* arg10
)) << n
);
2251 result0
= 0x7fffffff;
2253 result0
= (((uint32_t)(arg01
* arg11
)) << n
);
2255 ret
= (((uint64_t)result1
<< 32)) | result0
;
2259 uint64_t helper_mulm_h(uint32_t arg00
, uint32_t arg01
,
2260 uint32_t arg10
, uint32_t arg11
, uint32_t n
)
2263 int64_t result0
, result1
;
2265 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2266 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2267 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2268 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2271 result1
= 0x7fffffff;
2273 result1
= (((int32_t)arg00
* (int32_t)arg10
) << n
);
2276 result0
= 0x7fffffff;
2278 result0
= (((int32_t)arg01
* (int32_t)arg11
) << n
);
2280 ret
= (result1
+ result0
);
2284 uint32_t helper_mulr_h(uint32_t arg00
, uint32_t arg01
,
2285 uint32_t arg10
, uint32_t arg11
, uint32_t n
)
2287 uint32_t result0
, result1
;
2289 int32_t sc1
= ((arg00
& 0xffff) == 0x8000) &&
2290 ((arg10
& 0xffff) == 0x8000) && (n
== 1);
2291 int32_t sc0
= ((arg01
& 0xffff) == 0x8000) &&
2292 ((arg11
& 0xffff) == 0x8000) && (n
== 1);
2295 result1
= 0x7fffffff;
2297 result1
= ((arg00
* arg10
) << n
) + 0x8000;
2300 result0
= 0x7fffffff;
2302 result0
= ((arg01
* arg11
) << n
) + 0x8000;
2304 return (result1
& 0xffff0000) | (result0
>> 16);
2307 uint32_t helper_crc32(uint32_t arg0
, uint32_t arg1
)
2311 stl_be_p(buf
, arg0
);
2313 ret
= crc32(arg1
, buf
, 4);
2317 /* context save area (CSA) related helpers */
2319 static int cdc_increment(target_ulong
*psw
)
2321 if ((*psw
& MASK_PSW_CDC
) == 0x7f) {
2326 /* check for overflow */
2327 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2328 int mask
= (1u << (7 - lo
)) - 1;
2329 int count
= *psw
& mask
;
2337 static int cdc_decrement(target_ulong
*psw
)
2339 if ((*psw
& MASK_PSW_CDC
) == 0x7f) {
2342 /* check for underflow */
2343 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2344 int mask
= (1u << (7 - lo
)) - 1;
2345 int count
= *psw
& mask
;
2353 static bool cdc_zero(target_ulong
*psw
)
2355 int cdc
= *psw
& MASK_PSW_CDC
;
2356 /* Returns TRUE if PSW.CDC.COUNT == 0 or if PSW.CDC ==
2357 7'b1111111, otherwise returns FALSE. */
2361 /* find CDC.COUNT */
2362 int lo
= clo32((*psw
& MASK_PSW_CDC
) << (32 - 7));
2363 int mask
= (1u << (7 - lo
)) - 1;
2364 int count
= *psw
& mask
;
2368 static void save_context_upper(CPUTriCoreState
*env
, int ea
)
2370 cpu_stl_data(env
, ea
, env
->PCXI
);
2371 cpu_stl_data(env
, ea
+4, psw_read(env
));
2372 cpu_stl_data(env
, ea
+8, env
->gpr_a
[10]);
2373 cpu_stl_data(env
, ea
+12, env
->gpr_a
[11]);
2374 cpu_stl_data(env
, ea
+16, env
->gpr_d
[8]);
2375 cpu_stl_data(env
, ea
+20, env
->gpr_d
[9]);
2376 cpu_stl_data(env
, ea
+24, env
->gpr_d
[10]);
2377 cpu_stl_data(env
, ea
+28, env
->gpr_d
[11]);
2378 cpu_stl_data(env
, ea
+32, env
->gpr_a
[12]);
2379 cpu_stl_data(env
, ea
+36, env
->gpr_a
[13]);
2380 cpu_stl_data(env
, ea
+40, env
->gpr_a
[14]);
2381 cpu_stl_data(env
, ea
+44, env
->gpr_a
[15]);
2382 cpu_stl_data(env
, ea
+48, env
->gpr_d
[12]);
2383 cpu_stl_data(env
, ea
+52, env
->gpr_d
[13]);
2384 cpu_stl_data(env
, ea
+56, env
->gpr_d
[14]);
2385 cpu_stl_data(env
, ea
+60, env
->gpr_d
[15]);
2388 static void save_context_lower(CPUTriCoreState
*env
, int ea
)
2390 cpu_stl_data(env
, ea
, env
->PCXI
);
2391 cpu_stl_data(env
, ea
+4, env
->gpr_a
[11]);
2392 cpu_stl_data(env
, ea
+8, env
->gpr_a
[2]);
2393 cpu_stl_data(env
, ea
+12, env
->gpr_a
[3]);
2394 cpu_stl_data(env
, ea
+16, env
->gpr_d
[0]);
2395 cpu_stl_data(env
, ea
+20, env
->gpr_d
[1]);
2396 cpu_stl_data(env
, ea
+24, env
->gpr_d
[2]);
2397 cpu_stl_data(env
, ea
+28, env
->gpr_d
[3]);
2398 cpu_stl_data(env
, ea
+32, env
->gpr_a
[4]);
2399 cpu_stl_data(env
, ea
+36, env
->gpr_a
[5]);
2400 cpu_stl_data(env
, ea
+40, env
->gpr_a
[6]);
2401 cpu_stl_data(env
, ea
+44, env
->gpr_a
[7]);
2402 cpu_stl_data(env
, ea
+48, env
->gpr_d
[4]);
2403 cpu_stl_data(env
, ea
+52, env
->gpr_d
[5]);
2404 cpu_stl_data(env
, ea
+56, env
->gpr_d
[6]);
2405 cpu_stl_data(env
, ea
+60, env
->gpr_d
[7]);
2408 static void restore_context_upper(CPUTriCoreState
*env
, int ea
,
2409 target_ulong
*new_PCXI
, target_ulong
*new_PSW
)
2411 *new_PCXI
= cpu_ldl_data(env
, ea
);
2412 *new_PSW
= cpu_ldl_data(env
, ea
+4);
2413 env
->gpr_a
[10] = cpu_ldl_data(env
, ea
+8);
2414 env
->gpr_a
[11] = cpu_ldl_data(env
, ea
+12);
2415 env
->gpr_d
[8] = cpu_ldl_data(env
, ea
+16);
2416 env
->gpr_d
[9] = cpu_ldl_data(env
, ea
+20);
2417 env
->gpr_d
[10] = cpu_ldl_data(env
, ea
+24);
2418 env
->gpr_d
[11] = cpu_ldl_data(env
, ea
+28);
2419 env
->gpr_a
[12] = cpu_ldl_data(env
, ea
+32);
2420 env
->gpr_a
[13] = cpu_ldl_data(env
, ea
+36);
2421 env
->gpr_a
[14] = cpu_ldl_data(env
, ea
+40);
2422 env
->gpr_a
[15] = cpu_ldl_data(env
, ea
+44);
2423 env
->gpr_d
[12] = cpu_ldl_data(env
, ea
+48);
2424 env
->gpr_d
[13] = cpu_ldl_data(env
, ea
+52);
2425 env
->gpr_d
[14] = cpu_ldl_data(env
, ea
+56);
2426 env
->gpr_d
[15] = cpu_ldl_data(env
, ea
+60);
2429 static void restore_context_lower(CPUTriCoreState
*env
, int ea
,
2430 target_ulong
*ra
, target_ulong
*pcxi
)
2432 *pcxi
= cpu_ldl_data(env
, ea
);
2433 *ra
= cpu_ldl_data(env
, ea
+4);
2434 env
->gpr_a
[2] = cpu_ldl_data(env
, ea
+8);
2435 env
->gpr_a
[3] = cpu_ldl_data(env
, ea
+12);
2436 env
->gpr_d
[0] = cpu_ldl_data(env
, ea
+16);
2437 env
->gpr_d
[1] = cpu_ldl_data(env
, ea
+20);
2438 env
->gpr_d
[2] = cpu_ldl_data(env
, ea
+24);
2439 env
->gpr_d
[3] = cpu_ldl_data(env
, ea
+28);
2440 env
->gpr_a
[4] = cpu_ldl_data(env
, ea
+32);
2441 env
->gpr_a
[5] = cpu_ldl_data(env
, ea
+36);
2442 env
->gpr_a
[6] = cpu_ldl_data(env
, ea
+40);
2443 env
->gpr_a
[7] = cpu_ldl_data(env
, ea
+44);
2444 env
->gpr_d
[4] = cpu_ldl_data(env
, ea
+48);
2445 env
->gpr_d
[5] = cpu_ldl_data(env
, ea
+52);
2446 env
->gpr_d
[6] = cpu_ldl_data(env
, ea
+56);
2447 env
->gpr_d
[7] = cpu_ldl_data(env
, ea
+60);
2450 void helper_call(CPUTriCoreState
*env
, uint32_t next_pc
)
2452 target_ulong tmp_FCX
;
2454 target_ulong new_FCX
;
2457 psw
= psw_read(env
);
2458 /* if (FCX == 0) trap(FCU); */
2459 if (env
->FCX
== 0) {
2461 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2463 /* if (PSW.CDE) then if (cdc_increment()) then trap(CDO); */
2464 if (psw
& MASK_PSW_CDE
) {
2465 if (cdc_increment(&psw
)) {
2467 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CDO
, GETPC());
2471 psw
|= MASK_PSW_CDE
;
2472 /* tmp_FCX = FCX; */
2474 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2475 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2476 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2477 /* new_FCX = M(EA, word); */
2478 new_FCX
= cpu_ldl_data(env
, ea
);
2479 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2480 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2482 save_context_upper(env
, ea
);
2484 /* PCXI.PCPN = ICR.CCPN; */
2485 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2486 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2487 /* PCXI.PIE = ICR.IE; */
2488 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
2489 ((env
->ICR
& MASK_ICR_IE
) << 15));
2491 env
->PCXI
|= MASK_PCXI_UL
;
2493 /* PCXI[19: 0] = FCX[19: 0]; */
2494 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2495 /* FCX[19: 0] = new_FCX[19: 0]; */
2496 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2497 /* A[11] = next_pc[31: 0]; */
2498 env
->gpr_a
[11] = next_pc
;
2500 /* if (tmp_FCX == LCX) trap(FCD);*/
2501 if (tmp_FCX
== env
->LCX
) {
2503 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2505 psw_write(env
, psw
);
2508 void helper_ret(CPUTriCoreState
*env
)
2511 target_ulong new_PCXI
;
2512 target_ulong new_PSW
, psw
;
2514 psw
= psw_read(env
);
2515 /* if (PSW.CDE) then if (cdc_decrement()) then trap(CDU);*/
2516 if (psw
& MASK_PSW_CDE
) {
2517 if (cdc_decrement(&psw
)) {
2519 psw_write(env
, psw
);
2520 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CDU
, GETPC());
2523 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2524 if ((env
->PCXI
& 0xfffff) == 0) {
2526 psw_write(env
, psw
);
2527 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2529 /* if (PCXI.UL == 0) then trap(CTYP); */
2530 if ((env
->PCXI
& MASK_PCXI_UL
) == 0) {
2532 cdc_increment(&psw
); /* restore to the start of helper */
2533 psw_write(env
, psw
);
2534 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2536 /* PC = {A11 [31: 1], 1’b0}; */
2537 env
->PC
= env
->gpr_a
[11] & 0xfffffffe;
2539 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
2540 ea
= ((env
->PCXI
& MASK_PCXI_PCXS
) << 12) +
2541 ((env
->PCXI
& MASK_PCXI_PCXO
) << 6);
2542 /* {new_PCXI, new_PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2543 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2544 restore_context_upper(env
, ea
, &new_PCXI
, &new_PSW
);
2545 /* M(EA, word) = FCX; */
2546 cpu_stl_data(env
, ea
, env
->FCX
);
2547 /* FCX[19: 0] = PCXI[19: 0]; */
2548 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2549 /* PCXI = new_PCXI; */
2550 env
->PCXI
= new_PCXI
;
2552 if (tricore_feature(env
, TRICORE_FEATURE_13
)) {
2554 psw_write(env
, new_PSW
);
2556 /* PSW = {new_PSW[31:26], PSW[25:24], new_PSW[23:0]}; */
2557 psw_write(env
, (new_PSW
& ~(0x3000000)) + (psw
& (0x3000000)));
2561 void helper_bisr(CPUTriCoreState
*env
, uint32_t const9
)
2563 target_ulong tmp_FCX
;
2565 target_ulong new_FCX
;
2567 if (env
->FCX
== 0) {
2569 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2573 ea
= ((env
->FCX
& 0xf0000) << 12) + ((env
->FCX
& 0xffff) << 6);
2575 /* new_FCX = M(EA, word); */
2576 new_FCX
= cpu_ldl_data(env
, ea
);
2577 /* M(EA, 16 * word) = {PCXI, A[11], A[2], A[3], D[0], D[1], D[2], D[3], A[4]
2578 , A[5], A[6], A[7], D[4], D[5], D[6], D[7]}; */
2579 save_context_lower(env
, ea
);
2582 /* PCXI.PCPN = ICR.CCPN */
2583 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2584 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2585 /* PCXI.PIE = ICR.IE */
2586 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
2587 ((env
->ICR
& MASK_ICR_IE
) << 15));
2589 env
->PCXI
&= ~(MASK_PCXI_UL
);
2590 /* PCXI[19: 0] = FCX[19: 0] */
2591 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2592 /* FXC[19: 0] = new_FCX[19: 0] */
2593 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2595 env
->ICR
|= MASK_ICR_IE
;
2597 env
->ICR
|= const9
; /* ICR.CCPN = const9[7: 0];*/
2599 if (tmp_FCX
== env
->LCX
) {
2601 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2605 void helper_rfe(CPUTriCoreState
*env
)
2608 target_ulong new_PCXI
;
2609 target_ulong new_PSW
;
2610 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2611 if ((env
->PCXI
& 0xfffff) == 0) {
2612 /* raise csu trap */
2613 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2615 /* if (PCXI.UL == 0) then trap(CTYP); */
2616 if ((env
->PCXI
& MASK_PCXI_UL
) == 0) {
2617 /* raise CTYP trap */
2618 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2620 /* if (!cdc_zero() AND PSW.CDE) then trap(NEST); */
2621 if (!cdc_zero(&(env
->PSW
)) && (env
->PSW
& MASK_PSW_CDE
)) {
2622 /* raise NEST trap */
2623 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_NEST
, GETPC());
2625 env
->PC
= env
->gpr_a
[11] & ~0x1;
2626 /* ICR.IE = PCXI.PIE; */
2627 env
->ICR
= (env
->ICR
& ~MASK_ICR_IE
) + ((env
->PCXI
& MASK_PCXI_PIE
) >> 15);
2628 /* ICR.CCPN = PCXI.PCPN; */
2629 env
->ICR
= (env
->ICR
& ~MASK_ICR_CCPN
) +
2630 ((env
->PCXI
& MASK_PCXI_PCPN
) >> 24);
2631 /*EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0};*/
2632 ea
= ((env
->PCXI
& MASK_PCXI_PCXS
) << 12) +
2633 ((env
->PCXI
& MASK_PCXI_PCXO
) << 6);
2634 /*{new_PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2635 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2636 restore_context_upper(env
, ea
, &new_PCXI
, &new_PSW
);
2637 /* M(EA, word) = FCX;*/
2638 cpu_stl_data(env
, ea
, env
->FCX
);
2639 /* FCX[19: 0] = PCXI[19: 0]; */
2640 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2641 /* PCXI = new_PCXI; */
2642 env
->PCXI
= new_PCXI
;
2644 psw_write(env
, new_PSW
);
2647 void helper_rfm(CPUTriCoreState
*env
)
2649 env
->PC
= (env
->gpr_a
[11] & ~0x1);
2650 /* ICR.IE = PCXI.PIE; */
2651 env
->ICR
= (env
->ICR
& ~MASK_ICR_IE
) |
2652 ((env
->PCXI
& MASK_PCXI_PIE
) >> 15);
2653 /* ICR.CCPN = PCXI.PCPN; */
2654 env
->ICR
= (env
->ICR
& ~MASK_ICR_CCPN
) |
2655 ((env
->PCXI
& MASK_PCXI_PCPN
) >> 24);
2656 /* {PCXI, PSW, A[10], A[11]} = M(DCX, 4 * word); */
2657 env
->PCXI
= cpu_ldl_data(env
, env
->DCX
);
2658 psw_write(env
, cpu_ldl_data(env
, env
->DCX
+4));
2659 env
->gpr_a
[10] = cpu_ldl_data(env
, env
->DCX
+8);
2660 env
->gpr_a
[11] = cpu_ldl_data(env
, env
->DCX
+12);
2662 if (tricore_feature(env
, TRICORE_FEATURE_131
)) {
2667 void helper_ldlcx(CPUTriCoreState
*env
, uint32_t ea
)
2670 /* insn doesn't load PCXI and RA */
2671 restore_context_lower(env
, ea
, &dummy
, &dummy
);
2674 void helper_lducx(CPUTriCoreState
*env
, uint32_t ea
)
2677 /* insn doesn't load PCXI and PSW */
2678 restore_context_upper(env
, ea
, &dummy
, &dummy
);
2681 void helper_stlcx(CPUTriCoreState
*env
, uint32_t ea
)
2683 save_context_lower(env
, ea
);
2686 void helper_stucx(CPUTriCoreState
*env
, uint32_t ea
)
2688 save_context_upper(env
, ea
);
2691 void helper_svlcx(CPUTriCoreState
*env
)
2693 target_ulong tmp_FCX
;
2695 target_ulong new_FCX
;
2697 if (env
->FCX
== 0) {
2699 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2701 /* tmp_FCX = FCX; */
2703 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2704 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2705 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2706 /* new_FCX = M(EA, word); */
2707 new_FCX
= cpu_ldl_data(env
, ea
);
2708 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2709 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2711 save_context_lower(env
, ea
);
2713 /* PCXI.PCPN = ICR.CCPN; */
2714 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2715 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2716 /* PCXI.PIE = ICR.IE; */
2717 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
2718 ((env
->ICR
& MASK_ICR_IE
) << 15));
2720 env
->PCXI
&= ~MASK_PCXI_UL
;
2722 /* PCXI[19: 0] = FCX[19: 0]; */
2723 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2724 /* FCX[19: 0] = new_FCX[19: 0]; */
2725 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2727 /* if (tmp_FCX == LCX) trap(FCD);*/
2728 if (tmp_FCX
== env
->LCX
) {
2730 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2734 void helper_svucx(CPUTriCoreState
*env
)
2736 target_ulong tmp_FCX
;
2738 target_ulong new_FCX
;
2740 if (env
->FCX
== 0) {
2742 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCU
, GETPC());
2744 /* tmp_FCX = FCX; */
2746 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2747 ea
= ((env
->FCX
& MASK_FCX_FCXS
) << 12) +
2748 ((env
->FCX
& MASK_FCX_FCXO
) << 6);
2749 /* new_FCX = M(EA, word); */
2750 new_FCX
= cpu_ldl_data(env
, ea
);
2751 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2752 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2754 save_context_upper(env
, ea
);
2756 /* PCXI.PCPN = ICR.CCPN; */
2757 env
->PCXI
= (env
->PCXI
& 0xffffff) +
2758 ((env
->ICR
& MASK_ICR_CCPN
) << 24);
2759 /* PCXI.PIE = ICR.IE; */
2760 env
->PCXI
= ((env
->PCXI
& ~MASK_PCXI_PIE
) +
2761 ((env
->ICR
& MASK_ICR_IE
) << 15));
2763 env
->PCXI
|= MASK_PCXI_UL
;
2765 /* PCXI[19: 0] = FCX[19: 0]; */
2766 env
->PCXI
= (env
->PCXI
& 0xfff00000) + (env
->FCX
& 0xfffff);
2767 /* FCX[19: 0] = new_FCX[19: 0]; */
2768 env
->FCX
= (env
->FCX
& 0xfff00000) + (new_FCX
& 0xfffff);
2770 /* if (tmp_FCX == LCX) trap(FCD);*/
2771 if (tmp_FCX
== env
->LCX
) {
2773 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_FCD
, GETPC());
2777 void helper_rslcx(CPUTriCoreState
*env
)
2780 target_ulong new_PCXI
;
2781 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2782 if ((env
->PCXI
& 0xfffff) == 0) {
2784 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CSU
, GETPC());
2786 /* if (PCXI.UL == 1) then trap(CTYP); */
2787 if ((env
->PCXI
& MASK_PCXI_UL
) != 0) {
2789 raise_exception_sync_helper(env
, TRAPC_CTX_MNG
, TIN3_CTYP
, GETPC());
2791 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
2792 ea
= ((env
->PCXI
& MASK_PCXI_PCXS
) << 12) +
2793 ((env
->PCXI
& MASK_PCXI_PCXO
) << 6);
2794 /* {new_PCXI, A[11], A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2795 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2796 restore_context_lower(env
, ea
, &env
->gpr_a
[11], &new_PCXI
);
2797 /* M(EA, word) = FCX; */
2798 cpu_stl_data(env
, ea
, env
->FCX
);
2799 /* M(EA, word) = FCX; */
2800 cpu_stl_data(env
, ea
, env
->FCX
);
2801 /* FCX[19: 0] = PCXI[19: 0]; */
2802 env
->FCX
= (env
->FCX
& 0xfff00000) + (env
->PCXI
& 0x000fffff);
2803 /* PCXI = new_PCXI; */
2804 env
->PCXI
= new_PCXI
;
2807 void helper_psw_write(CPUTriCoreState
*env
, uint32_t arg
)
2809 psw_write(env
, arg
);
2812 uint32_t helper_psw_read(CPUTriCoreState
*env
)
2814 return psw_read(env
);
2818 static inline void QEMU_NORETURN
do_raise_exception_err(CPUTriCoreState
*env
,
2823 CPUState
*cs
= CPU(tricore_env_get_cpu(env
));
2824 cs
->exception_index
= exception
;
2825 env
->error_code
= error_code
;
2828 /* now we have a real cpu fault */
2829 cpu_restore_state(cs
, pc
);
2835 void tlb_fill(CPUState
*cs
, target_ulong addr
, int is_write
, int mmu_idx
,
2839 ret
= cpu_tricore_handle_mmu_fault(cs
, addr
, is_write
, mmu_idx
);
2841 TriCoreCPU
*cpu
= TRICORE_CPU(cs
);
2842 CPUTriCoreState
*env
= &cpu
->env
;
2843 do_raise_exception_err(env
, cs
->exception_index
,
2844 env
->error_code
, retaddr
);