target-tricore: Add instructions of RRR1 opcode format, which have 0x43 as first...
[qemu.git] / target-tricore / op_helper.c
blob2755d45aa7c8867886b15ec55f4d16223cd1c1e6
1 /*
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 <stdlib.h>
18 #include "cpu.h"
19 #include "qemu/host-utils.h"
20 #include "exec/helper-proto.h"
21 #include "exec/cpu_ldst.h"
23 /* Addressing mode helper */
25 static uint16_t reverse16(uint16_t val)
27 uint8_t high = (uint8_t)(val >> 8);
28 uint8_t low = (uint8_t)(val & 0xff);
30 uint16_t rh, rl;
32 rl = (uint16_t)((high * 0x0202020202ULL & 0x010884422010ULL) % 1023);
33 rh = (uint16_t)((low * 0x0202020202ULL & 0x010884422010ULL) % 1023);
35 return (rh << 8) | rl;
38 uint32_t helper_br_update(uint32_t reg)
40 uint32_t index = reg & 0xffff;
41 uint32_t incr = reg >> 16;
42 uint32_t new_index = reverse16(reverse16(index) + reverse16(incr));
43 return reg - index + new_index;
46 uint32_t helper_circ_update(uint32_t reg, uint32_t off)
48 uint32_t index = reg & 0xffff;
49 uint32_t length = reg >> 16;
50 int32_t new_index = index + off;
51 if (new_index < 0) {
52 new_index += length;
53 } else {
54 new_index %= length;
56 return reg - index + new_index;
59 static uint32_t ssov32(CPUTriCoreState *env, int64_t arg)
61 uint32_t ret;
62 int64_t max_pos = INT32_MAX;
63 int64_t max_neg = INT32_MIN;
64 if (arg > max_pos) {
65 env->PSW_USB_V = (1 << 31);
66 env->PSW_USB_SV = (1 << 31);
67 ret = (target_ulong)max_pos;
68 } else {
69 if (arg < max_neg) {
70 env->PSW_USB_V = (1 << 31);
71 env->PSW_USB_SV = (1 << 31);
72 ret = (target_ulong)max_neg;
73 } else {
74 env->PSW_USB_V = 0;
75 ret = (target_ulong)arg;
78 env->PSW_USB_AV = arg ^ arg * 2u;
79 env->PSW_USB_SAV |= env->PSW_USB_AV;
80 return ret;
83 static uint32_t suov32_pos(CPUTriCoreState *env, uint64_t arg)
85 uint32_t ret;
86 uint64_t max_pos = UINT32_MAX;
87 if (arg > max_pos) {
88 env->PSW_USB_V = (1 << 31);
89 env->PSW_USB_SV = (1 << 31);
90 ret = (target_ulong)max_pos;
91 } else {
92 env->PSW_USB_V = 0;
93 ret = (target_ulong)arg;
95 env->PSW_USB_AV = arg ^ arg * 2u;
96 env->PSW_USB_SAV |= env->PSW_USB_AV;
97 return ret;
100 static uint32_t suov32_neg(CPUTriCoreState *env, int64_t arg)
102 uint32_t ret;
104 if (arg < 0) {
105 env->PSW_USB_V = (1 << 31);
106 env->PSW_USB_SV = (1 << 31);
107 ret = 0;
108 } else {
109 env->PSW_USB_V = 0;
110 ret = (target_ulong)arg;
112 env->PSW_USB_AV = arg ^ arg * 2u;
113 env->PSW_USB_SAV |= env->PSW_USB_AV;
114 return ret;
117 static uint32_t ssov16(CPUTriCoreState *env, int32_t hw0, int32_t hw1)
119 int32_t max_pos = INT16_MAX;
120 int32_t max_neg = INT16_MIN;
121 int32_t av0, av1;
123 env->PSW_USB_V = 0;
124 av0 = hw0 ^ hw0 * 2u;
125 if (hw0 > max_pos) {
126 env->PSW_USB_V = (1 << 31);
127 hw0 = max_pos;
128 } else if (hw0 < max_neg) {
129 env->PSW_USB_V = (1 << 31);
130 hw0 = max_neg;
133 av1 = hw1 ^ hw1 * 2u;
134 if (hw1 > max_pos) {
135 env->PSW_USB_V = (1 << 31);
136 hw1 = max_pos;
137 } else if (hw1 < max_neg) {
138 env->PSW_USB_V = (1 << 31);
139 hw1 = max_neg;
142 env->PSW_USB_SV |= env->PSW_USB_V;
143 env->PSW_USB_AV = (av0 | av1) << 16;
144 env->PSW_USB_SAV |= env->PSW_USB_AV;
145 return (hw0 & 0xffff) | (hw1 << 16);
148 static uint32_t suov16(CPUTriCoreState *env, int32_t hw0, int32_t hw1)
150 int32_t max_pos = UINT16_MAX;
151 int32_t av0, av1;
153 env->PSW_USB_V = 0;
154 av0 = hw0 ^ hw0 * 2u;
155 if (hw0 > max_pos) {
156 env->PSW_USB_V = (1 << 31);
157 hw0 = max_pos;
158 } else if (hw0 < 0) {
159 env->PSW_USB_V = (1 << 31);
160 hw0 = 0;
163 av1 = hw1 ^ hw1 * 2u;
164 if (hw1 > max_pos) {
165 env->PSW_USB_V = (1 << 31);
166 hw1 = max_pos;
167 } else if (hw1 < 0) {
168 env->PSW_USB_V = (1 << 31);
169 hw1 = 0;
172 env->PSW_USB_SV |= env->PSW_USB_V;
173 env->PSW_USB_AV = (av0 | av1) << 16;
174 env->PSW_USB_SAV |= env->PSW_USB_AV;
175 return (hw0 & 0xffff) | (hw1 << 16);
178 target_ulong helper_add_ssov(CPUTriCoreState *env, target_ulong r1,
179 target_ulong r2)
181 int64_t t1 = sextract64(r1, 0, 32);
182 int64_t t2 = sextract64(r2, 0, 32);
183 int64_t result = t1 + t2;
184 return ssov32(env, result);
187 uint64_t helper_add64_ssov(CPUTriCoreState *env, uint64_t r1, uint64_t r2)
189 uint64_t result;
190 int64_t ovf;
192 result = r1 + r2;
193 ovf = (result ^ r1) & ~(r1 ^ r2);
194 env->PSW_USB_AV = (result ^ result * 2u) >> 32;
195 env->PSW_USB_SAV |= env->PSW_USB_AV;
196 if (ovf < 0) {
197 env->PSW_USB_V = (1 << 31);
198 env->PSW_USB_SV = (1 << 31);
199 /* ext_ret > MAX_INT */
200 if ((int64_t)r1 >= 0) {
201 result = INT64_MAX;
202 /* ext_ret < MIN_INT */
203 } else {
204 result = INT64_MIN;
206 } else {
207 env->PSW_USB_V = 0;
209 return result;
212 target_ulong helper_add_h_ssov(CPUTriCoreState *env, target_ulong r1,
213 target_ulong r2)
215 int32_t ret_hw0, ret_hw1;
217 ret_hw0 = sextract32(r1, 0, 16) + sextract32(r2, 0, 16);
218 ret_hw1 = sextract32(r1, 16, 16) + sextract32(r2, 16, 16);
219 return ssov16(env, ret_hw0, ret_hw1);
222 uint32_t helper_addr_h_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l,
223 uint32_t r2_h)
225 int64_t mul_res0 = sextract64(r1, 0, 32);
226 int64_t mul_res1 = sextract64(r1, 32, 32);
227 int64_t r2_low = sextract64(r2_l, 0, 32);
228 int64_t r2_high = sextract64(r2_h, 0, 32);
229 int64_t result0, result1;
230 uint32_t ovf0, ovf1;
231 uint32_t avf0, avf1;
233 ovf0 = ovf1 = 0;
235 result0 = r2_low + mul_res0 + 0x8000;
236 result1 = r2_high + mul_res1 + 0x8000;
238 avf0 = result0 * 2u;
239 avf0 = result0 ^ avf0;
240 avf1 = result1 * 2u;
241 avf1 = result1 ^ avf1;
243 if (result0 > INT32_MAX) {
244 ovf0 = (1 << 31);
245 result0 = INT32_MAX;
246 } else if (result0 < INT32_MIN) {
247 ovf0 = (1 << 31);
248 result0 = INT32_MIN;
251 if (result1 > INT32_MAX) {
252 ovf1 = (1 << 31);
253 result1 = INT32_MAX;
254 } else if (result1 < INT32_MIN) {
255 ovf1 = (1 << 31);
256 result1 = INT32_MIN;
259 env->PSW_USB_V = ovf0 | ovf1;
260 env->PSW_USB_SV |= env->PSW_USB_V;
262 env->PSW_USB_AV = avf0 | avf1;
263 env->PSW_USB_SAV |= env->PSW_USB_AV;
265 return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL);
269 target_ulong helper_add_suov(CPUTriCoreState *env, target_ulong r1,
270 target_ulong r2)
272 int64_t t1 = extract64(r1, 0, 32);
273 int64_t t2 = extract64(r2, 0, 32);
274 int64_t result = t1 + t2;
275 return suov32_pos(env, result);
278 target_ulong helper_add_h_suov(CPUTriCoreState *env, target_ulong r1,
279 target_ulong r2)
281 int32_t ret_hw0, ret_hw1;
283 ret_hw0 = extract32(r1, 0, 16) + extract32(r2, 0, 16);
284 ret_hw1 = extract32(r1, 16, 16) + extract32(r2, 16, 16);
285 return suov16(env, ret_hw0, ret_hw1);
288 target_ulong helper_sub_ssov(CPUTriCoreState *env, target_ulong r1,
289 target_ulong r2)
291 int64_t t1 = sextract64(r1, 0, 32);
292 int64_t t2 = sextract64(r2, 0, 32);
293 int64_t result = t1 - t2;
294 return ssov32(env, result);
297 target_ulong helper_sub_h_ssov(CPUTriCoreState *env, target_ulong r1,
298 target_ulong r2)
300 int32_t ret_hw0, ret_hw1;
302 ret_hw0 = sextract32(r1, 0, 16) - sextract32(r2, 0, 16);
303 ret_hw1 = sextract32(r1, 16, 16) - sextract32(r2, 16, 16);
304 return ssov16(env, ret_hw0, ret_hw1);
307 target_ulong helper_sub_suov(CPUTriCoreState *env, target_ulong r1,
308 target_ulong r2)
310 int64_t t1 = extract64(r1, 0, 32);
311 int64_t t2 = extract64(r2, 0, 32);
312 int64_t result = t1 - t2;
313 return suov32_neg(env, result);
316 target_ulong helper_sub_h_suov(CPUTriCoreState *env, target_ulong r1,
317 target_ulong r2)
319 int32_t ret_hw0, ret_hw1;
321 ret_hw0 = extract32(r1, 0, 16) - extract32(r2, 0, 16);
322 ret_hw1 = extract32(r1, 16, 16) - extract32(r2, 16, 16);
323 return suov16(env, ret_hw0, ret_hw1);
326 target_ulong helper_mul_ssov(CPUTriCoreState *env, target_ulong r1,
327 target_ulong r2)
329 int64_t t1 = sextract64(r1, 0, 32);
330 int64_t t2 = sextract64(r2, 0, 32);
331 int64_t result = t1 * t2;
332 return ssov32(env, result);
335 target_ulong helper_mul_suov(CPUTriCoreState *env, target_ulong r1,
336 target_ulong r2)
338 int64_t t1 = extract64(r1, 0, 32);
339 int64_t t2 = extract64(r2, 0, 32);
340 int64_t result = t1 * t2;
342 return suov32_pos(env, result);
345 target_ulong helper_sha_ssov(CPUTriCoreState *env, target_ulong r1,
346 target_ulong r2)
348 int64_t t1 = sextract64(r1, 0, 32);
349 int32_t t2 = sextract64(r2, 0, 6);
350 int64_t result;
351 if (t2 == 0) {
352 result = t1;
353 } else if (t2 > 0) {
354 result = t1 << t2;
355 } else {
356 result = t1 >> -t2;
358 return ssov32(env, result);
361 uint32_t helper_abs_ssov(CPUTriCoreState *env, target_ulong r1)
363 target_ulong result;
364 result = ((int32_t)r1 >= 0) ? r1 : (0 - r1);
365 return ssov32(env, result);
368 uint32_t helper_abs_h_ssov(CPUTriCoreState *env, target_ulong r1)
370 int32_t ret_h0, ret_h1;
372 ret_h0 = sextract32(r1, 0, 16);
373 ret_h0 = (ret_h0 >= 0) ? ret_h0 : (0 - ret_h0);
375 ret_h1 = sextract32(r1, 16, 16);
376 ret_h1 = (ret_h1 >= 0) ? ret_h1 : (0 - ret_h1);
378 return ssov16(env, ret_h0, ret_h1);
381 target_ulong helper_absdif_ssov(CPUTriCoreState *env, target_ulong r1,
382 target_ulong r2)
384 int64_t t1 = sextract64(r1, 0, 32);
385 int64_t t2 = sextract64(r2, 0, 32);
386 int64_t result;
388 if (t1 > t2) {
389 result = t1 - t2;
390 } else {
391 result = t2 - t1;
393 return ssov32(env, result);
396 uint32_t helper_absdif_h_ssov(CPUTriCoreState *env, target_ulong r1,
397 target_ulong r2)
399 int32_t t1, t2;
400 int32_t ret_h0, ret_h1;
402 t1 = sextract32(r1, 0, 16);
403 t2 = sextract32(r2, 0, 16);
404 if (t1 > t2) {
405 ret_h0 = t1 - t2;
406 } else {
407 ret_h0 = t2 - t1;
410 t1 = sextract32(r1, 16, 16);
411 t2 = sextract32(r2, 16, 16);
412 if (t1 > t2) {
413 ret_h1 = t1 - t2;
414 } else {
415 ret_h1 = t2 - t1;
418 return ssov16(env, ret_h0, ret_h1);
421 target_ulong helper_madd32_ssov(CPUTriCoreState *env, target_ulong r1,
422 target_ulong r2, target_ulong r3)
424 int64_t t1 = sextract64(r1, 0, 32);
425 int64_t t2 = sextract64(r2, 0, 32);
426 int64_t t3 = sextract64(r3, 0, 32);
427 int64_t result;
429 result = t2 + (t1 * t3);
430 return ssov32(env, result);
433 target_ulong helper_madd32_suov(CPUTriCoreState *env, target_ulong r1,
434 target_ulong r2, target_ulong r3)
436 uint64_t t1 = extract64(r1, 0, 32);
437 uint64_t t2 = extract64(r2, 0, 32);
438 uint64_t t3 = extract64(r3, 0, 32);
439 int64_t result;
441 result = t2 + (t1 * t3);
442 return suov32_pos(env, result);
445 uint64_t helper_madd64_ssov(CPUTriCoreState *env, target_ulong r1,
446 uint64_t r2, target_ulong r3)
448 uint64_t ret, ovf;
449 int64_t t1 = sextract64(r1, 0, 32);
450 int64_t t3 = sextract64(r3, 0, 32);
451 int64_t mul;
453 mul = t1 * t3;
454 ret = mul + r2;
455 ovf = (ret ^ mul) & ~(mul ^ r2);
457 t1 = ret >> 32;
458 env->PSW_USB_AV = t1 ^ t1 * 2u;
459 env->PSW_USB_SAV |= env->PSW_USB_AV;
461 if ((int64_t)ovf < 0) {
462 env->PSW_USB_V = (1 << 31);
463 env->PSW_USB_SV = (1 << 31);
464 /* ext_ret > MAX_INT */
465 if (mul >= 0) {
466 ret = INT64_MAX;
467 /* ext_ret < MIN_INT */
468 } else {
469 ret = INT64_MIN;
471 } else {
472 env->PSW_USB_V = 0;
475 return ret;
478 uint32_t
479 helper_madd32_q_add_ssov(CPUTriCoreState *env, uint64_t r1, uint64_t r2)
481 int64_t result;
483 result = (r1 + r2);
485 env->PSW_USB_AV = (result ^ result * 2u);
486 env->PSW_USB_SAV |= env->PSW_USB_AV;
488 /* we do the saturation by hand, since we produce an overflow on the host
489 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
490 case, we flip the saturated value. */
491 if (r2 == 0x8000000000000000LL) {
492 if (result > 0x7fffffffLL) {
493 env->PSW_USB_V = (1 << 31);
494 env->PSW_USB_SV = (1 << 31);
495 result = INT32_MIN;
496 } else if (result < -0x80000000LL) {
497 env->PSW_USB_V = (1 << 31);
498 env->PSW_USB_SV = (1 << 31);
499 result = INT32_MAX;
500 } else {
501 env->PSW_USB_V = 0;
503 } else {
504 if (result > 0x7fffffffLL) {
505 env->PSW_USB_V = (1 << 31);
506 env->PSW_USB_SV = (1 << 31);
507 result = INT32_MAX;
508 } else if (result < -0x80000000LL) {
509 env->PSW_USB_V = (1 << 31);
510 env->PSW_USB_SV = (1 << 31);
511 result = INT32_MIN;
512 } else {
513 env->PSW_USB_V = 0;
516 return (uint32_t)result;
519 uint64_t helper_madd64_q_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2,
520 uint32_t r3, uint32_t n)
522 int64_t t1 = (int64_t)r1;
523 int64_t t2 = sextract64(r2, 0, 32);
524 int64_t t3 = sextract64(r3, 0, 32);
525 int64_t result, mul;
526 int64_t ovf;
528 mul = (t2 * t3) << n;
529 result = mul + t1;
531 env->PSW_USB_AV = (result ^ result * 2u) >> 32;
532 env->PSW_USB_SAV |= env->PSW_USB_AV;
534 ovf = (result ^ mul) & ~(mul ^ t1);
535 /* we do the saturation by hand, since we produce an overflow on the host
536 if the mul was (0x80000000 * 0x80000000) << 1). If this is the
537 case, we flip the saturated value. */
538 if ((r2 == 0x80000000) && (r3 == 0x80000000) && (n == 1)) {
539 if (ovf >= 0) {
540 env->PSW_USB_V = (1 << 31);
541 env->PSW_USB_SV = (1 << 31);
542 /* ext_ret > MAX_INT */
543 if (mul < 0) {
544 result = INT64_MAX;
545 /* ext_ret < MIN_INT */
546 } else {
547 result = INT64_MIN;
549 } else {
550 env->PSW_USB_V = 0;
552 } else {
553 if (ovf < 0) {
554 env->PSW_USB_V = (1 << 31);
555 env->PSW_USB_SV = (1 << 31);
556 /* ext_ret > MAX_INT */
557 if (mul >= 0) {
558 result = INT64_MAX;
559 /* ext_ret < MIN_INT */
560 } else {
561 result = INT64_MIN;
563 } else {
564 env->PSW_USB_V = 0;
567 return (uint64_t)result;
570 uint32_t helper_maddr_q_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2,
571 uint32_t r3, uint32_t n)
573 int64_t t1 = sextract64(r1, 0, 32);
574 int64_t t2 = sextract64(r2, 0, 32);
575 int64_t t3 = sextract64(r3, 0, 32);
576 int64_t mul, ret;
578 if ((t2 == -0x8000ll) && (t3 == -0x8000ll) && (n == 1)) {
579 mul = 0x7fffffff;
580 } else {
581 mul = (t2 * t3) << n;
584 ret = t1 + mul + 0x8000;
586 env->PSW_USB_AV = ret ^ ret * 2u;
587 env->PSW_USB_SAV |= env->PSW_USB_AV;
589 if (ret > 0x7fffffffll) {
590 env->PSW_USB_V = (1 << 31);
591 env->PSW_USB_SV |= env->PSW_USB_V;
592 ret = INT32_MAX;
593 } else if (ret < -0x80000000ll) {
594 env->PSW_USB_V = (1 << 31);
595 env->PSW_USB_SV |= env->PSW_USB_V;
596 ret = INT32_MIN;
597 } else {
598 env->PSW_USB_V = 0;
600 return ret & 0xffff0000ll;
603 uint64_t helper_madd64_suov(CPUTriCoreState *env, target_ulong r1,
604 uint64_t r2, target_ulong r3)
606 uint64_t ret, mul;
607 uint64_t t1 = extract64(r1, 0, 32);
608 uint64_t t3 = extract64(r3, 0, 32);
610 mul = t1 * t3;
611 ret = mul + r2;
613 t1 = ret >> 32;
614 env->PSW_USB_AV = t1 ^ t1 * 2u;
615 env->PSW_USB_SAV |= env->PSW_USB_AV;
617 if (ret < r2) {
618 env->PSW_USB_V = (1 << 31);
619 env->PSW_USB_SV = (1 << 31);
620 /* saturate */
621 ret = UINT64_MAX;
622 } else {
623 env->PSW_USB_V = 0;
625 return ret;
628 target_ulong helper_msub32_ssov(CPUTriCoreState *env, target_ulong r1,
629 target_ulong r2, target_ulong r3)
631 int64_t t1 = sextract64(r1, 0, 32);
632 int64_t t2 = sextract64(r2, 0, 32);
633 int64_t t3 = sextract64(r3, 0, 32);
634 int64_t result;
636 result = t2 - (t1 * t3);
637 return ssov32(env, result);
640 target_ulong helper_msub32_suov(CPUTriCoreState *env, target_ulong r1,
641 target_ulong r2, target_ulong r3)
643 uint64_t t1 = extract64(r1, 0, 32);
644 uint64_t t2 = extract64(r2, 0, 32);
645 uint64_t t3 = extract64(r3, 0, 32);
646 uint64_t result;
647 uint64_t mul;
649 mul = (t1 * t3);
650 result = t2 - mul;
652 env->PSW_USB_AV = result ^ result * 2u;
653 env->PSW_USB_SAV |= env->PSW_USB_AV;
654 /* we calculate ovf by hand here, because the multiplication can overflow on
655 the host, which would give false results if we compare to less than
656 zero */
657 if (mul > t2) {
658 env->PSW_USB_V = (1 << 31);
659 env->PSW_USB_SV = (1 << 31);
660 result = 0;
661 } else {
662 env->PSW_USB_V = 0;
664 return result;
667 uint64_t helper_msub64_ssov(CPUTriCoreState *env, target_ulong r1,
668 uint64_t r2, target_ulong r3)
670 uint64_t ret, ovf;
671 int64_t t1 = sextract64(r1, 0, 32);
672 int64_t t3 = sextract64(r3, 0, 32);
673 int64_t mul;
675 mul = t1 * t3;
676 ret = r2 - mul;
677 ovf = (ret ^ r2) & (mul ^ r2);
679 t1 = ret >> 32;
680 env->PSW_USB_AV = t1 ^ t1 * 2u;
681 env->PSW_USB_SAV |= env->PSW_USB_AV;
683 if ((int64_t)ovf < 0) {
684 env->PSW_USB_V = (1 << 31);
685 env->PSW_USB_SV = (1 << 31);
686 /* ext_ret > MAX_INT */
687 if (mul < 0) {
688 ret = INT64_MAX;
689 /* ext_ret < MIN_INT */
690 } else {
691 ret = INT64_MIN;
693 } else {
694 env->PSW_USB_V = 0;
696 return ret;
699 uint64_t helper_msub64_suov(CPUTriCoreState *env, target_ulong r1,
700 uint64_t r2, target_ulong r3)
702 uint64_t ret, mul;
703 uint64_t t1 = extract64(r1, 0, 32);
704 uint64_t t3 = extract64(r3, 0, 32);
706 mul = t1 * t3;
707 ret = r2 - mul;
709 t1 = ret >> 32;
710 env->PSW_USB_AV = t1 ^ t1 * 2u;
711 env->PSW_USB_SAV |= env->PSW_USB_AV;
713 if (ret > r2) {
714 env->PSW_USB_V = (1 << 31);
715 env->PSW_USB_SV = (1 << 31);
716 /* saturate */
717 ret = 0;
718 } else {
719 env->PSW_USB_V = 0;
721 return ret;
724 uint32_t helper_abs_b(CPUTriCoreState *env, target_ulong arg)
726 int32_t b, i;
727 int32_t ovf = 0;
728 int32_t avf = 0;
729 int32_t ret = 0;
731 for (i = 0; i < 4; i++) {
732 b = sextract32(arg, i * 8, 8);
733 b = (b >= 0) ? b : (0 - b);
734 ovf |= (b > 0x7F) || (b < -0x80);
735 avf |= b ^ b * 2u;
736 ret |= (b & 0xff) << (i * 8);
739 env->PSW_USB_V = ovf << 31;
740 env->PSW_USB_SV |= env->PSW_USB_V;
741 env->PSW_USB_AV = avf << 24;
742 env->PSW_USB_SAV |= env->PSW_USB_AV;
744 return ret;
747 uint32_t helper_abs_h(CPUTriCoreState *env, target_ulong arg)
749 int32_t h, i;
750 int32_t ovf = 0;
751 int32_t avf = 0;
752 int32_t ret = 0;
754 for (i = 0; i < 2; i++) {
755 h = sextract32(arg, i * 16, 16);
756 h = (h >= 0) ? h : (0 - h);
757 ovf |= (h > 0x7FFF) || (h < -0x8000);
758 avf |= h ^ h * 2u;
759 ret |= (h & 0xffff) << (i * 16);
762 env->PSW_USB_V = ovf << 31;
763 env->PSW_USB_SV |= env->PSW_USB_V;
764 env->PSW_USB_AV = avf << 16;
765 env->PSW_USB_SAV |= env->PSW_USB_AV;
767 return ret;
770 uint32_t helper_absdif_b(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
772 int32_t b, i;
773 int32_t extr_r2;
774 int32_t ovf = 0;
775 int32_t avf = 0;
776 int32_t ret = 0;
778 for (i = 0; i < 4; i++) {
779 extr_r2 = sextract32(r2, i * 8, 8);
780 b = sextract32(r1, i * 8, 8);
781 b = (b > extr_r2) ? (b - extr_r2) : (extr_r2 - b);
782 ovf |= (b > 0x7F) || (b < -0x80);
783 avf |= b ^ b * 2u;
784 ret |= (b & 0xff) << (i * 8);
787 env->PSW_USB_V = ovf << 31;
788 env->PSW_USB_SV |= env->PSW_USB_V;
789 env->PSW_USB_AV = avf << 24;
790 env->PSW_USB_SAV |= env->PSW_USB_AV;
791 return ret;
794 uint32_t helper_absdif_h(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
796 int32_t h, i;
797 int32_t extr_r2;
798 int32_t ovf = 0;
799 int32_t avf = 0;
800 int32_t ret = 0;
802 for (i = 0; i < 2; i++) {
803 extr_r2 = sextract32(r2, i * 16, 16);
804 h = sextract32(r1, i * 16, 16);
805 h = (h > extr_r2) ? (h - extr_r2) : (extr_r2 - h);
806 ovf |= (h > 0x7FFF) || (h < -0x8000);
807 avf |= h ^ h * 2u;
808 ret |= (h & 0xffff) << (i * 16);
811 env->PSW_USB_V = ovf << 31;
812 env->PSW_USB_SV |= env->PSW_USB_V;
813 env->PSW_USB_AV = avf << 16;
814 env->PSW_USB_SAV |= env->PSW_USB_AV;
816 return ret;
819 uint32_t helper_addr_h(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l,
820 uint32_t r2_h)
822 int64_t mul_res0 = sextract64(r1, 0, 32);
823 int64_t mul_res1 = sextract64(r1, 32, 32);
824 int64_t r2_low = sextract64(r2_l, 0, 32);
825 int64_t r2_high = sextract64(r2_h, 0, 32);
826 int64_t result0, result1;
827 uint32_t ovf0, ovf1;
828 uint32_t avf0, avf1;
830 ovf0 = ovf1 = 0;
832 result0 = r2_low + mul_res0 + 0x8000;
833 result1 = r2_high + mul_res1 + 0x8000;
835 if ((result0 > INT32_MAX) || (result0 < INT32_MIN)) {
836 ovf0 = (1 << 31);
839 if ((result1 > INT32_MAX) || (result1 < INT32_MIN)) {
840 ovf1 = (1 << 31);
843 env->PSW_USB_V = ovf0 | ovf1;
844 env->PSW_USB_SV |= env->PSW_USB_V;
846 avf0 = result0 * 2u;
847 avf0 = result0 ^ avf0;
848 avf1 = result1 * 2u;
849 avf1 = result1 ^ avf1;
851 env->PSW_USB_AV = avf0 | avf1;
852 env->PSW_USB_SAV |= env->PSW_USB_AV;
854 return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL);
857 uint32_t helper_maddr_q(CPUTriCoreState *env, uint32_t r1, uint32_t r2,
858 uint32_t r3, uint32_t n)
860 int64_t t1 = sextract64(r1, 0, 32);
861 int64_t t2 = sextract64(r2, 0, 32);
862 int64_t t3 = sextract64(r3, 0, 32);
863 int64_t mul, ret;
865 if ((t2 == -0x8000ll) && (t3 == -0x8000ll) && (n == 1)) {
866 mul = 0x7fffffff;
867 } else {
868 mul = (t2 * t3) << n;
871 ret = t1 + mul + 0x8000;
873 if ((ret > 0x7fffffffll) || (ret < -0x80000000ll)) {
874 env->PSW_USB_V = (1 << 31);
875 env->PSW_USB_SV |= env->PSW_USB_V;
876 } else {
877 env->PSW_USB_V = 0;
879 env->PSW_USB_AV = ret ^ ret * 2u;
880 env->PSW_USB_SAV |= env->PSW_USB_AV;
882 return ret & 0xffff0000ll;
885 uint32_t helper_add_b(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
887 int32_t b, i;
888 int32_t extr_r1, extr_r2;
889 int32_t ovf = 0;
890 int32_t avf = 0;
891 uint32_t ret = 0;
893 for (i = 0; i < 4; i++) {
894 extr_r1 = sextract32(r1, i * 8, 8);
895 extr_r2 = sextract32(r2, i * 8, 8);
897 b = extr_r1 + extr_r2;
898 ovf |= ((b > 0x7f) || (b < -0x80));
899 avf |= b ^ b * 2u;
900 ret |= ((b & 0xff) << (i*8));
903 env->PSW_USB_V = (ovf << 31);
904 env->PSW_USB_SV |= env->PSW_USB_V;
905 env->PSW_USB_AV = avf << 24;
906 env->PSW_USB_SAV |= env->PSW_USB_AV;
908 return ret;
911 uint32_t helper_add_h(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
913 int32_t h, i;
914 int32_t extr_r1, extr_r2;
915 int32_t ovf = 0;
916 int32_t avf = 0;
917 int32_t ret = 0;
919 for (i = 0; i < 2; i++) {
920 extr_r1 = sextract32(r1, i * 16, 16);
921 extr_r2 = sextract32(r2, i * 16, 16);
922 h = extr_r1 + extr_r2;
923 ovf |= ((h > 0x7fff) || (h < -0x8000));
924 avf |= h ^ h * 2u;
925 ret |= (h & 0xffff) << (i * 16);
928 env->PSW_USB_V = (ovf << 31);
929 env->PSW_USB_SV |= env->PSW_USB_V;
930 env->PSW_USB_AV = (avf << 16);
931 env->PSW_USB_SAV |= env->PSW_USB_AV;
933 return ret;
936 uint32_t helper_sub_b(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
938 int32_t b, i;
939 int32_t extr_r1, extr_r2;
940 int32_t ovf = 0;
941 int32_t avf = 0;
942 uint32_t ret = 0;
944 for (i = 0; i < 4; i++) {
945 extr_r1 = sextract32(r1, i * 8, 8);
946 extr_r2 = sextract32(r2, i * 8, 8);
948 b = extr_r1 - extr_r2;
949 ovf |= ((b > 0x7f) || (b < -0x80));
950 avf |= b ^ b * 2u;
951 ret |= ((b & 0xff) << (i*8));
954 env->PSW_USB_V = (ovf << 31);
955 env->PSW_USB_SV |= env->PSW_USB_V;
956 env->PSW_USB_AV = avf << 24;
957 env->PSW_USB_SAV |= env->PSW_USB_AV;
959 return ret;
962 uint32_t helper_sub_h(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
964 int32_t h, i;
965 int32_t extr_r1, extr_r2;
966 int32_t ovf = 0;
967 int32_t avf = 0;
968 int32_t ret = 0;
970 for (i = 0; i < 2; i++) {
971 extr_r1 = sextract32(r1, i * 16, 16);
972 extr_r2 = sextract32(r2, i * 16, 16);
973 h = extr_r1 - extr_r2;
974 ovf |= ((h > 0x7fff) || (h < -0x8000));
975 avf |= h ^ h * 2u;
976 ret |= (h & 0xffff) << (i * 16);
979 env->PSW_USB_V = (ovf << 31);
980 env->PSW_USB_SV |= env->PSW_USB_V;
981 env->PSW_USB_AV = avf << 16;
982 env->PSW_USB_SAV |= env->PSW_USB_AV;
984 return ret;
987 uint32_t helper_eq_b(target_ulong r1, target_ulong r2)
989 int32_t ret;
990 int32_t i, msk;
992 ret = 0;
993 msk = 0xff;
994 for (i = 0; i < 4; i++) {
995 if ((r1 & msk) == (r2 & msk)) {
996 ret |= msk;
998 msk = msk << 8;
1001 return ret;
1004 uint32_t helper_eq_h(target_ulong r1, target_ulong r2)
1006 int32_t ret = 0;
1008 if ((r1 & 0xffff) == (r2 & 0xffff)) {
1009 ret = 0xffff;
1012 if ((r1 & 0xffff0000) == (r2 & 0xffff0000)) {
1013 ret |= 0xffff0000;
1016 return ret;
1019 uint32_t helper_eqany_b(target_ulong r1, target_ulong r2)
1021 int32_t i;
1022 uint32_t ret = 0;
1024 for (i = 0; i < 4; i++) {
1025 ret |= (sextract32(r1, i * 8, 8) == sextract32(r2, i * 8, 8));
1028 return ret;
1031 uint32_t helper_eqany_h(target_ulong r1, target_ulong r2)
1033 uint32_t ret;
1035 ret = (sextract32(r1, 0, 16) == sextract32(r2, 0, 16));
1036 ret |= (sextract32(r1, 16, 16) == sextract32(r2, 16, 16));
1038 return ret;
1041 uint32_t helper_lt_b(target_ulong r1, target_ulong r2)
1043 int32_t i;
1044 uint32_t ret = 0;
1046 for (i = 0; i < 4; i++) {
1047 if (sextract32(r1, i * 8, 8) < sextract32(r2, i * 8, 8)) {
1048 ret |= (0xff << (i * 8));
1052 return ret;
1055 uint32_t helper_lt_bu(target_ulong r1, target_ulong r2)
1057 int32_t i;
1058 uint32_t ret = 0;
1060 for (i = 0; i < 4; i++) {
1061 if (extract32(r1, i * 8, 8) < extract32(r2, i * 8, 8)) {
1062 ret |= (0xff << (i * 8));
1066 return ret;
1069 uint32_t helper_lt_h(target_ulong r1, target_ulong r2)
1071 uint32_t ret = 0;
1073 if (sextract32(r1, 0, 16) < sextract32(r2, 0, 16)) {
1074 ret |= 0xffff;
1077 if (sextract32(r1, 16, 16) < sextract32(r2, 16, 16)) {
1078 ret |= 0xffff0000;
1081 return ret;
1084 uint32_t helper_lt_hu(target_ulong r1, target_ulong r2)
1086 uint32_t ret = 0;
1088 if (extract32(r1, 0, 16) < extract32(r2, 0, 16)) {
1089 ret |= 0xffff;
1092 if (extract32(r1, 16, 16) < extract32(r2, 16, 16)) {
1093 ret |= 0xffff0000;
1096 return ret;
1099 #define EXTREMA_H_B(name, op) \
1100 uint32_t helper_##name ##_b(target_ulong r1, target_ulong r2) \
1102 int32_t i, extr_r1, extr_r2; \
1103 uint32_t ret = 0; \
1105 for (i = 0; i < 4; i++) { \
1106 extr_r1 = sextract32(r1, i * 8, 8); \
1107 extr_r2 = sextract32(r2, i * 8, 8); \
1108 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1109 ret |= (extr_r1 & 0xff) << (i * 8); \
1111 return ret; \
1114 uint32_t helper_##name ##_bu(target_ulong r1, target_ulong r2)\
1116 int32_t i; \
1117 uint32_t extr_r1, extr_r2; \
1118 uint32_t ret = 0; \
1120 for (i = 0; i < 4; i++) { \
1121 extr_r1 = extract32(r1, i * 8, 8); \
1122 extr_r2 = extract32(r2, i * 8, 8); \
1123 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1124 ret |= (extr_r1 & 0xff) << (i * 8); \
1126 return ret; \
1129 uint32_t helper_##name ##_h(target_ulong r1, target_ulong r2) \
1131 int32_t extr_r1, extr_r2; \
1132 uint32_t ret = 0; \
1134 extr_r1 = sextract32(r1, 0, 16); \
1135 extr_r2 = sextract32(r2, 0, 16); \
1136 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1137 ret = ret & 0xffff; \
1139 extr_r1 = sextract32(r1, 16, 16); \
1140 extr_r2 = sextract32(r2, 16, 16); \
1141 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1142 ret |= extr_r1 << 16; \
1144 return ret; \
1147 uint32_t helper_##name ##_hu(target_ulong r1, target_ulong r2)\
1149 uint32_t extr_r1, extr_r2; \
1150 uint32_t ret = 0; \
1152 extr_r1 = extract32(r1, 0, 16); \
1153 extr_r2 = extract32(r2, 0, 16); \
1154 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1155 ret = ret & 0xffff; \
1157 extr_r1 = extract32(r1, 16, 16); \
1158 extr_r2 = extract32(r2, 16, 16); \
1159 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1160 ret |= extr_r1 << (16); \
1162 return ret; \
1165 uint64_t helper_ix##name(uint64_t r1, uint32_t r2) \
1167 int64_t r2l, r2h, r1hl; \
1168 uint64_t ret = 0; \
1170 ret = ((r1 + 2) & 0xffff); \
1171 r2l = sextract64(r2, 0, 16); \
1172 r2h = sextract64(r2, 16, 16); \
1173 r1hl = sextract64(r1, 32, 16); \
1175 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
1176 ret |= (r2l & 0xffff) << 32; \
1177 ret |= extract64(r1, 0, 16) << 16; \
1178 } else if ((r2h op r2l) && (r2h op r1hl)) { \
1179 ret |= extract64(r2, 16, 16) << 32; \
1180 ret |= extract64(r1 + 1, 0, 16) << 16; \
1181 } else { \
1182 ret |= r1 & 0xffffffff0000ull; \
1184 return ret; \
1187 uint64_t helper_ix##name ##_u(uint64_t r1, uint32_t r2) \
1189 int64_t r2l, r2h, r1hl; \
1190 uint64_t ret = 0; \
1192 ret = ((r1 + 2) & 0xffff); \
1193 r2l = extract64(r2, 0, 16); \
1194 r2h = extract64(r2, 16, 16); \
1195 r1hl = extract64(r1, 32, 16); \
1197 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
1198 ret |= (r2l & 0xffff) << 32; \
1199 ret |= extract64(r1, 0, 16) << 16; \
1200 } else if ((r2h op r2l) && (r2h op r1hl)) { \
1201 ret |= extract64(r2, 16, 16) << 32; \
1202 ret |= extract64(r1 + 1, 0, 16) << 16; \
1203 } else { \
1204 ret |= r1 & 0xffffffff0000ull; \
1206 return ret; \
1209 EXTREMA_H_B(max, >)
1210 EXTREMA_H_B(min, <)
1212 #undef EXTREMA_H_B
1214 uint32_t helper_clo(target_ulong r1)
1216 return clo32(r1);
1219 uint32_t helper_clo_h(target_ulong r1)
1221 uint32_t ret_hw0 = extract32(r1, 0, 16);
1222 uint32_t ret_hw1 = extract32(r1, 16, 16);
1224 ret_hw0 = clo32(ret_hw0 << 16);
1225 ret_hw1 = clo32(ret_hw1 << 16);
1227 if (ret_hw0 > 16) {
1228 ret_hw0 = 16;
1230 if (ret_hw1 > 16) {
1231 ret_hw1 = 16;
1234 return ret_hw0 | (ret_hw1 << 16);
1237 uint32_t helper_clz(target_ulong r1)
1239 return clz32(r1);
1242 uint32_t helper_clz_h(target_ulong r1)
1244 uint32_t ret_hw0 = extract32(r1, 0, 16);
1245 uint32_t ret_hw1 = extract32(r1, 16, 16);
1247 ret_hw0 = clz32(ret_hw0 << 16);
1248 ret_hw1 = clz32(ret_hw1 << 16);
1250 if (ret_hw0 > 16) {
1251 ret_hw0 = 16;
1253 if (ret_hw1 > 16) {
1254 ret_hw1 = 16;
1257 return ret_hw0 | (ret_hw1 << 16);
1260 uint32_t helper_cls(target_ulong r1)
1262 return clrsb32(r1);
1265 uint32_t helper_cls_h(target_ulong r1)
1267 uint32_t ret_hw0 = extract32(r1, 0, 16);
1268 uint32_t ret_hw1 = extract32(r1, 16, 16);
1270 ret_hw0 = clrsb32(ret_hw0 << 16);
1271 ret_hw1 = clrsb32(ret_hw1 << 16);
1273 if (ret_hw0 > 15) {
1274 ret_hw0 = 15;
1276 if (ret_hw1 > 15) {
1277 ret_hw1 = 15;
1280 return ret_hw0 | (ret_hw1 << 16);
1283 uint32_t helper_sh(target_ulong r1, target_ulong r2)
1285 int32_t shift_count = sextract32(r2, 0, 6);
1287 if (shift_count == -32) {
1288 return 0;
1289 } else if (shift_count < 0) {
1290 return r1 >> -shift_count;
1291 } else {
1292 return r1 << shift_count;
1296 uint32_t helper_sh_h(target_ulong r1, target_ulong r2)
1298 int32_t ret_hw0, ret_hw1;
1299 int32_t shift_count;
1301 shift_count = sextract32(r2, 0, 5);
1303 if (shift_count == -16) {
1304 return 0;
1305 } else if (shift_count < 0) {
1306 ret_hw0 = extract32(r1, 0, 16) >> -shift_count;
1307 ret_hw1 = extract32(r1, 16, 16) >> -shift_count;
1308 return (ret_hw0 & 0xffff) | (ret_hw1 << 16);
1309 } else {
1310 ret_hw0 = extract32(r1, 0, 16) << shift_count;
1311 ret_hw1 = extract32(r1, 16, 16) << shift_count;
1312 return (ret_hw0 & 0xffff) | (ret_hw1 << 16);
1316 uint32_t helper_sha(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
1318 int32_t shift_count;
1319 int64_t result, t1;
1320 uint32_t ret;
1322 shift_count = sextract32(r2, 0, 6);
1323 t1 = sextract32(r1, 0, 32);
1325 if (shift_count == 0) {
1326 env->PSW_USB_C = env->PSW_USB_V = 0;
1327 ret = r1;
1328 } else if (shift_count == -32) {
1329 env->PSW_USB_C = r1;
1330 env->PSW_USB_V = 0;
1331 ret = t1 >> 31;
1332 } else if (shift_count > 0) {
1333 result = t1 << shift_count;
1334 /* calc carry */
1335 env->PSW_USB_C = ((result & 0xffffffff00000000ULL) != 0);
1336 /* calc v */
1337 env->PSW_USB_V = (((result > 0x7fffffffLL) ||
1338 (result < -0x80000000LL)) << 31);
1339 /* calc sv */
1340 env->PSW_USB_SV |= env->PSW_USB_V;
1341 ret = (uint32_t)result;
1342 } else {
1343 env->PSW_USB_V = 0;
1344 env->PSW_USB_C = (r1 & ((1 << -shift_count) - 1));
1345 ret = t1 >> -shift_count;
1348 env->PSW_USB_AV = ret ^ ret * 2u;
1349 env->PSW_USB_SAV |= env->PSW_USB_AV;
1351 return ret;
1354 uint32_t helper_sha_h(target_ulong r1, target_ulong r2)
1356 int32_t shift_count;
1357 int32_t ret_hw0, ret_hw1;
1359 shift_count = sextract32(r2, 0, 5);
1361 if (shift_count == 0) {
1362 return r1;
1363 } else if (shift_count < 0) {
1364 ret_hw0 = sextract32(r1, 0, 16) >> -shift_count;
1365 ret_hw1 = sextract32(r1, 16, 16) >> -shift_count;
1366 return (ret_hw0 & 0xffff) | (ret_hw1 << 16);
1367 } else {
1368 ret_hw0 = sextract32(r1, 0, 16) << shift_count;
1369 ret_hw1 = sextract32(r1, 16, 16) << shift_count;
1370 return (ret_hw0 & 0xffff) | (ret_hw1 << 16);
1374 uint32_t helper_bmerge(target_ulong r1, target_ulong r2)
1376 uint32_t i, ret;
1378 ret = 0;
1379 for (i = 0; i < 16; i++) {
1380 ret |= (r1 & 1) << (2 * i + 1);
1381 ret |= (r2 & 1) << (2 * i);
1382 r1 = r1 >> 1;
1383 r2 = r2 >> 1;
1385 return ret;
1388 uint64_t helper_bsplit(uint32_t r1)
1390 int32_t i;
1391 uint64_t ret;
1393 ret = 0;
1394 for (i = 0; i < 32; i = i + 2) {
1395 /* even */
1396 ret |= (r1 & 1) << (i/2);
1397 r1 = r1 >> 1;
1398 /* odd */
1399 ret |= (uint64_t)(r1 & 1) << (i/2 + 32);
1400 r1 = r1 >> 1;
1402 return ret;
1405 uint32_t helper_parity(target_ulong r1)
1407 uint32_t ret;
1408 uint32_t nOnes, i;
1410 ret = 0;
1411 nOnes = 0;
1412 for (i = 0; i < 8; i++) {
1413 ret ^= (r1 & 1);
1414 r1 = r1 >> 1;
1416 /* second byte */
1417 nOnes = 0;
1418 for (i = 0; i < 8; i++) {
1419 nOnes ^= (r1 & 1);
1420 r1 = r1 >> 1;
1422 ret |= nOnes << 8;
1423 /* third byte */
1424 nOnes = 0;
1425 for (i = 0; i < 8; i++) {
1426 nOnes ^= (r1 & 1);
1427 r1 = r1 >> 1;
1429 ret |= nOnes << 16;
1430 /* fourth byte */
1431 nOnes = 0;
1432 for (i = 0; i < 8; i++) {
1433 nOnes ^= (r1 & 1);
1434 r1 = r1 >> 1;
1436 ret |= nOnes << 24;
1438 return ret;
1441 uint32_t helper_pack(uint32_t carry, uint32_t r1_low, uint32_t r1_high,
1442 target_ulong r2)
1444 uint32_t ret;
1445 int32_t fp_exp, fp_frac, temp_exp, fp_exp_frac;
1446 int32_t int_exp = r1_high;
1447 int32_t int_mant = r1_low;
1448 uint32_t flag_rnd = (int_mant & (1 << 7)) && (
1449 (int_mant & (1 << 8)) ||
1450 (int_mant & 0x7f) ||
1451 (carry != 0));
1452 if (((int_mant & (1<<31)) == 0) && (int_exp == 255)) {
1453 fp_exp = 255;
1454 fp_frac = extract32(int_mant, 8, 23);
1455 } else if ((int_mant & (1<<31)) && (int_exp >= 127)) {
1456 fp_exp = 255;
1457 fp_frac = 0;
1458 } else if ((int_mant & (1<<31)) && (int_exp <= -128)) {
1459 fp_exp = 0;
1460 fp_frac = 0;
1461 } else if (int_mant == 0) {
1462 fp_exp = 0;
1463 fp_frac = 0;
1464 } else {
1465 if (((int_mant & (1 << 31)) == 0)) {
1466 temp_exp = 0;
1467 } else {
1468 temp_exp = int_exp + 128;
1470 fp_exp_frac = (((temp_exp & 0xff) << 23) |
1471 extract32(int_mant, 8, 23))
1472 + flag_rnd;
1473 fp_exp = extract32(fp_exp_frac, 23, 8);
1474 fp_frac = extract32(fp_exp_frac, 0, 23);
1476 ret = r2 & (1 << 31);
1477 ret = ret + (fp_exp << 23);
1478 ret = ret + (fp_frac & 0x7fffff);
1480 return ret;
1483 uint64_t helper_unpack(target_ulong arg1)
1485 int32_t fp_exp = extract32(arg1, 23, 8);
1486 int32_t fp_frac = extract32(arg1, 0, 23);
1487 uint64_t ret;
1488 int32_t int_exp, int_mant;
1490 if (fp_exp == 255) {
1491 int_exp = 255;
1492 int_mant = (fp_frac << 7);
1493 } else if ((fp_exp == 0) && (fp_frac == 0)) {
1494 int_exp = -127;
1495 int_mant = 0;
1496 } else if ((fp_exp == 0) && (fp_frac != 0)) {
1497 int_exp = -126;
1498 int_mant = (fp_frac << 7);
1499 } else {
1500 int_exp = fp_exp - 127;
1501 int_mant = (fp_frac << 7);
1502 int_mant |= (1 << 30);
1504 ret = int_exp;
1505 ret = ret << 32;
1506 ret |= int_mant;
1508 return ret;
1511 uint64_t helper_dvinit_b_13(CPUTriCoreState *env, uint32_t r1, uint32_t r2)
1513 uint64_t ret;
1514 int32_t abs_sig_dividend, abs_base_dividend, abs_divisor;
1515 int32_t quotient_sign;
1517 ret = sextract32(r1, 0, 32);
1518 ret = ret << 24;
1519 quotient_sign = 0;
1520 if (!((r1 & 0x80000000) == (r2 & 0x80000000))) {
1521 ret |= 0xffffff;
1522 quotient_sign = 1;
1525 abs_sig_dividend = abs(r1) >> 7;
1526 abs_base_dividend = abs(r1) & 0x7f;
1527 abs_divisor = abs(r1);
1528 /* calc overflow */
1529 env->PSW_USB_V = 0;
1530 if ((quotient_sign) && (abs_divisor)) {
1531 env->PSW_USB_V = (((abs_sig_dividend == abs_divisor) &&
1532 (abs_base_dividend >= abs_divisor)) ||
1533 (abs_sig_dividend > abs_divisor));
1534 } else {
1535 env->PSW_USB_V = (abs_sig_dividend >= abs_divisor);
1537 env->PSW_USB_V = env->PSW_USB_V << 31;
1538 env->PSW_USB_SV |= env->PSW_USB_V;
1539 env->PSW_USB_AV = 0;
1541 return ret;
1544 uint64_t helper_dvinit_b_131(CPUTriCoreState *env, uint32_t r1, uint32_t r2)
1546 uint64_t ret = sextract32(r1, 0, 32);
1548 ret = ret << 24;
1549 if (!((r1 & 0x80000000) == (r2 & 0x80000000))) {
1550 ret |= 0xffffff;
1552 /* calc overflow */
1553 env->PSW_USB_V = ((r2 == 0) || ((r2 == 0xffffffff) && (r1 == 0xffffff80)));
1554 env->PSW_USB_V = env->PSW_USB_V << 31;
1555 env->PSW_USB_SV |= env->PSW_USB_V;
1556 env->PSW_USB_AV = 0;
1558 return ret;
1561 uint64_t helper_dvinit_h_13(CPUTriCoreState *env, uint32_t r1, uint32_t r2)
1563 uint64_t ret;
1564 int32_t abs_sig_dividend, abs_base_dividend, abs_divisor;
1565 int32_t quotient_sign;
1567 ret = sextract32(r1, 0, 32);
1568 ret = ret << 16;
1569 quotient_sign = 0;
1570 if (!((r1 & 0x80000000) == (r2 & 0x80000000))) {
1571 ret |= 0xffff;
1572 quotient_sign = 1;
1575 abs_sig_dividend = abs(r1) >> 7;
1576 abs_base_dividend = abs(r1) & 0x7f;
1577 abs_divisor = abs(r1);
1578 /* calc overflow */
1579 env->PSW_USB_V = 0;
1580 if ((quotient_sign) && (abs_divisor)) {
1581 env->PSW_USB_V = (((abs_sig_dividend == abs_divisor) &&
1582 (abs_base_dividend >= abs_divisor)) ||
1583 (abs_sig_dividend > abs_divisor));
1584 } else {
1585 env->PSW_USB_V = (abs_sig_dividend >= abs_divisor);
1587 env->PSW_USB_V = env->PSW_USB_V << 31;
1588 env->PSW_USB_SV |= env->PSW_USB_V;
1589 env->PSW_USB_AV = 0;
1591 return ret;
1594 uint64_t helper_dvinit_h_131(CPUTriCoreState *env, uint32_t r1, uint32_t r2)
1596 uint64_t ret = sextract32(r1, 0, 32);
1598 ret = ret << 16;
1599 if (!((r1 & 0x80000000) == (r2 & 0x80000000))) {
1600 ret |= 0xffff;
1602 /* calc overflow */
1603 env->PSW_USB_V = ((r2 == 0) || ((r2 == 0xffffffff) && (r1 == 0xffff8000)));
1604 env->PSW_USB_V = env->PSW_USB_V << 31;
1605 env->PSW_USB_SV |= env->PSW_USB_V;
1606 env->PSW_USB_AV = 0;
1608 return ret;
1611 uint64_t helper_dvadj(uint64_t r1, uint32_t r2)
1613 int32_t x_sign = (r1 >> 63);
1614 int32_t q_sign = x_sign ^ (r2 >> 31);
1615 int32_t eq_pos = x_sign & ((r1 >> 32) == r2);
1616 int32_t eq_neg = x_sign & ((r1 >> 32) == -r2);
1617 uint32_t quotient;
1618 uint64_t ret, remainder;
1620 if ((q_sign & ~eq_neg) | eq_pos) {
1621 quotient = (r1 + 1) & 0xffffffff;
1622 } else {
1623 quotient = r1 & 0xffffffff;
1626 if (eq_pos | eq_neg) {
1627 remainder = 0;
1628 } else {
1629 remainder = (r1 & 0xffffffff00000000ull);
1631 ret = remainder|quotient;
1632 return ret;
1635 uint64_t helper_dvstep(uint64_t r1, uint32_t r2)
1637 int32_t dividend_sign = extract64(r1, 63, 1);
1638 int32_t divisor_sign = extract32(r2, 31, 1);
1639 int32_t quotient_sign = (dividend_sign != divisor_sign);
1640 int32_t addend, dividend_quotient, remainder;
1641 int32_t i, temp;
1643 if (quotient_sign) {
1644 addend = r2;
1645 } else {
1646 addend = -r2;
1648 dividend_quotient = (int32_t)r1;
1649 remainder = (int32_t)(r1 >> 32);
1651 for (i = 0; i < 8; i++) {
1652 remainder = (remainder << 1) | extract32(dividend_quotient, 31, 1);
1653 dividend_quotient <<= 1;
1654 temp = remainder + addend;
1655 if ((temp < 0) == dividend_sign) {
1656 remainder = temp;
1658 if (((temp < 0) == dividend_sign)) {
1659 dividend_quotient = dividend_quotient | !quotient_sign;
1660 } else {
1661 dividend_quotient = dividend_quotient | quotient_sign;
1664 return ((uint64_t)remainder << 32) | (uint32_t)dividend_quotient;
1667 uint64_t helper_dvstep_u(uint64_t r1, uint32_t r2)
1669 int32_t dividend_quotient = extract64(r1, 0, 32);
1670 int64_t remainder = extract64(r1, 32, 32);
1671 int32_t i;
1672 int64_t temp;
1673 for (i = 0; i < 8; i++) {
1674 remainder = (remainder << 1) | extract32(dividend_quotient, 31, 1);
1675 dividend_quotient <<= 1;
1676 temp = (remainder & 0xffffffff) - r2;
1677 if (temp >= 0) {
1678 remainder = temp;
1680 dividend_quotient = dividend_quotient | !(temp < 0);
1682 return ((uint64_t)remainder << 32) | (uint32_t)dividend_quotient;
1685 uint64_t helper_mul_h(uint32_t arg00, uint32_t arg01,
1686 uint32_t arg10, uint32_t arg11, uint32_t n)
1688 uint64_t ret;
1689 uint32_t result0, result1;
1691 int32_t sc1 = ((arg00 & 0xffff) == 0x8000) &&
1692 ((arg10 & 0xffff) == 0x8000) && (n == 1);
1693 int32_t sc0 = ((arg01 & 0xffff) == 0x8000) &&
1694 ((arg11 & 0xffff) == 0x8000) && (n == 1);
1695 if (sc1) {
1696 result1 = 0x7fffffff;
1697 } else {
1698 result1 = (((uint32_t)(arg00 * arg10)) << n);
1700 if (sc0) {
1701 result0 = 0x7fffffff;
1702 } else {
1703 result0 = (((uint32_t)(arg01 * arg11)) << n);
1705 ret = (((uint64_t)result1 << 32)) | result0;
1706 return ret;
1709 uint64_t helper_mulm_h(uint32_t arg00, uint32_t arg01,
1710 uint32_t arg10, uint32_t arg11, uint32_t n)
1712 uint64_t ret;
1713 int64_t result0, result1;
1715 int32_t sc1 = ((arg00 & 0xffff) == 0x8000) &&
1716 ((arg10 & 0xffff) == 0x8000) && (n == 1);
1717 int32_t sc0 = ((arg01 & 0xffff) == 0x8000) &&
1718 ((arg11 & 0xffff) == 0x8000) && (n == 1);
1720 if (sc1) {
1721 result1 = 0x7fffffff;
1722 } else {
1723 result1 = (((int32_t)arg00 * (int32_t)arg10) << n);
1725 if (sc0) {
1726 result0 = 0x7fffffff;
1727 } else {
1728 result0 = (((int32_t)arg01 * (int32_t)arg11) << n);
1730 ret = (result1 + result0);
1731 ret = ret << 16;
1732 return ret;
1734 uint32_t helper_mulr_h(uint32_t arg00, uint32_t arg01,
1735 uint32_t arg10, uint32_t arg11, uint32_t n)
1737 uint32_t result0, result1;
1739 int32_t sc1 = ((arg00 & 0xffff) == 0x8000) &&
1740 ((arg10 & 0xffff) == 0x8000) && (n == 1);
1741 int32_t sc0 = ((arg01 & 0xffff) == 0x8000) &&
1742 ((arg11 & 0xffff) == 0x8000) && (n == 1);
1744 if (sc1) {
1745 result1 = 0x7fffffff;
1746 } else {
1747 result1 = ((arg00 * arg10) << n) + 0x8000;
1749 if (sc0) {
1750 result0 = 0x7fffffff;
1751 } else {
1752 result0 = ((arg01 * arg11) << n) + 0x8000;
1754 return (result1 & 0xffff0000) | (result0 >> 16);
1757 /* context save area (CSA) related helpers */
1759 static int cdc_increment(target_ulong *psw)
1761 if ((*psw & MASK_PSW_CDC) == 0x7f) {
1762 return 0;
1765 (*psw)++;
1766 /* check for overflow */
1767 int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7));
1768 int mask = (1u << (7 - lo)) - 1;
1769 int count = *psw & mask;
1770 if (count == 0) {
1771 (*psw)--;
1772 return 1;
1774 return 0;
1777 static int cdc_decrement(target_ulong *psw)
1779 if ((*psw & MASK_PSW_CDC) == 0x7f) {
1780 return 0;
1782 /* check for underflow */
1783 int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7));
1784 int mask = (1u << (7 - lo)) - 1;
1785 int count = *psw & mask;
1786 if (count == 0) {
1787 return 1;
1789 (*psw)--;
1790 return 0;
1793 static bool cdc_zero(target_ulong *psw)
1795 int cdc = *psw & MASK_PSW_CDC;
1796 /* Returns TRUE if PSW.CDC.COUNT == 0 or if PSW.CDC ==
1797 7'b1111111, otherwise returns FALSE. */
1798 if (cdc == 0x7f) {
1799 return true;
1801 /* find CDC.COUNT */
1802 int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7));
1803 int mask = (1u << (7 - lo)) - 1;
1804 int count = *psw & mask;
1805 return count == 0;
1808 static void save_context_upper(CPUTriCoreState *env, int ea)
1810 cpu_stl_data(env, ea, env->PCXI);
1811 cpu_stl_data(env, ea+4, env->PSW);
1812 cpu_stl_data(env, ea+8, env->gpr_a[10]);
1813 cpu_stl_data(env, ea+12, env->gpr_a[11]);
1814 cpu_stl_data(env, ea+16, env->gpr_d[8]);
1815 cpu_stl_data(env, ea+20, env->gpr_d[9]);
1816 cpu_stl_data(env, ea+24, env->gpr_d[10]);
1817 cpu_stl_data(env, ea+28, env->gpr_d[11]);
1818 cpu_stl_data(env, ea+32, env->gpr_a[12]);
1819 cpu_stl_data(env, ea+36, env->gpr_a[13]);
1820 cpu_stl_data(env, ea+40, env->gpr_a[14]);
1821 cpu_stl_data(env, ea+44, env->gpr_a[15]);
1822 cpu_stl_data(env, ea+48, env->gpr_d[12]);
1823 cpu_stl_data(env, ea+52, env->gpr_d[13]);
1824 cpu_stl_data(env, ea+56, env->gpr_d[14]);
1825 cpu_stl_data(env, ea+60, env->gpr_d[15]);
1828 static void save_context_lower(CPUTriCoreState *env, int ea)
1830 cpu_stl_data(env, ea, env->PCXI);
1831 cpu_stl_data(env, ea+4, env->gpr_a[11]);
1832 cpu_stl_data(env, ea+8, env->gpr_a[2]);
1833 cpu_stl_data(env, ea+12, env->gpr_a[3]);
1834 cpu_stl_data(env, ea+16, env->gpr_d[0]);
1835 cpu_stl_data(env, ea+20, env->gpr_d[1]);
1836 cpu_stl_data(env, ea+24, env->gpr_d[2]);
1837 cpu_stl_data(env, ea+28, env->gpr_d[3]);
1838 cpu_stl_data(env, ea+32, env->gpr_a[4]);
1839 cpu_stl_data(env, ea+36, env->gpr_a[5]);
1840 cpu_stl_data(env, ea+40, env->gpr_a[6]);
1841 cpu_stl_data(env, ea+44, env->gpr_a[7]);
1842 cpu_stl_data(env, ea+48, env->gpr_d[4]);
1843 cpu_stl_data(env, ea+52, env->gpr_d[5]);
1844 cpu_stl_data(env, ea+56, env->gpr_d[6]);
1845 cpu_stl_data(env, ea+60, env->gpr_d[7]);
1848 static void restore_context_upper(CPUTriCoreState *env, int ea,
1849 target_ulong *new_PCXI, target_ulong *new_PSW)
1851 *new_PCXI = cpu_ldl_data(env, ea);
1852 *new_PSW = cpu_ldl_data(env, ea+4);
1853 env->gpr_a[10] = cpu_ldl_data(env, ea+8);
1854 env->gpr_a[11] = cpu_ldl_data(env, ea+12);
1855 env->gpr_d[8] = cpu_ldl_data(env, ea+16);
1856 env->gpr_d[9] = cpu_ldl_data(env, ea+20);
1857 env->gpr_d[10] = cpu_ldl_data(env, ea+24);
1858 env->gpr_d[11] = cpu_ldl_data(env, ea+28);
1859 env->gpr_a[12] = cpu_ldl_data(env, ea+32);
1860 env->gpr_a[13] = cpu_ldl_data(env, ea+36);
1861 env->gpr_a[14] = cpu_ldl_data(env, ea+40);
1862 env->gpr_a[15] = cpu_ldl_data(env, ea+44);
1863 env->gpr_d[12] = cpu_ldl_data(env, ea+48);
1864 env->gpr_d[13] = cpu_ldl_data(env, ea+52);
1865 env->gpr_d[14] = cpu_ldl_data(env, ea+56);
1866 env->gpr_d[15] = cpu_ldl_data(env, ea+60);
1869 static void restore_context_lower(CPUTriCoreState *env, int ea,
1870 target_ulong *ra, target_ulong *pcxi)
1872 *pcxi = cpu_ldl_data(env, ea);
1873 *ra = cpu_ldl_data(env, ea+4);
1874 env->gpr_a[2] = cpu_ldl_data(env, ea+8);
1875 env->gpr_a[3] = cpu_ldl_data(env, ea+12);
1876 env->gpr_d[0] = cpu_ldl_data(env, ea+16);
1877 env->gpr_d[1] = cpu_ldl_data(env, ea+20);
1878 env->gpr_d[2] = cpu_ldl_data(env, ea+24);
1879 env->gpr_d[3] = cpu_ldl_data(env, ea+28);
1880 env->gpr_a[4] = cpu_ldl_data(env, ea+32);
1881 env->gpr_a[5] = cpu_ldl_data(env, ea+36);
1882 env->gpr_a[6] = cpu_ldl_data(env, ea+40);
1883 env->gpr_a[7] = cpu_ldl_data(env, ea+44);
1884 env->gpr_d[4] = cpu_ldl_data(env, ea+48);
1885 env->gpr_d[5] = cpu_ldl_data(env, ea+52);
1886 env->gpr_d[6] = cpu_ldl_data(env, ea+56);
1887 env->gpr_d[7] = cpu_ldl_data(env, ea+60);
1890 void helper_call(CPUTriCoreState *env, uint32_t next_pc)
1892 target_ulong tmp_FCX;
1893 target_ulong ea;
1894 target_ulong new_FCX;
1895 target_ulong psw;
1897 psw = psw_read(env);
1898 /* if (FCX == 0) trap(FCU); */
1899 if (env->FCX == 0) {
1900 /* FCU trap */
1902 /* if (PSW.CDE) then if (cdc_increment()) then trap(CDO); */
1903 if (psw & MASK_PSW_CDE) {
1904 if (cdc_increment(&psw)) {
1905 /* CDO trap */
1908 /* PSW.CDE = 1;*/
1909 psw |= MASK_PSW_CDE;
1910 /* tmp_FCX = FCX; */
1911 tmp_FCX = env->FCX;
1912 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
1913 ea = ((env->FCX & MASK_FCX_FCXS) << 12) +
1914 ((env->FCX & MASK_FCX_FCXO) << 6);
1915 /* new_FCX = M(EA, word); */
1916 new_FCX = cpu_ldl_data(env, ea);
1917 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
1918 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
1919 D[15]}; */
1920 save_context_upper(env, ea);
1922 /* PCXI.PCPN = ICR.CCPN; */
1923 env->PCXI = (env->PCXI & 0xffffff) +
1924 ((env->ICR & MASK_ICR_CCPN) << 24);
1925 /* PCXI.PIE = ICR.IE; */
1926 env->PCXI = ((env->PCXI & ~MASK_PCXI_PIE) +
1927 ((env->ICR & MASK_ICR_IE) << 15));
1928 /* PCXI.UL = 1; */
1929 env->PCXI |= MASK_PCXI_UL;
1931 /* PCXI[19: 0] = FCX[19: 0]; */
1932 env->PCXI = (env->PCXI & 0xfff00000) + (env->FCX & 0xfffff);
1933 /* FCX[19: 0] = new_FCX[19: 0]; */
1934 env->FCX = (env->FCX & 0xfff00000) + (new_FCX & 0xfffff);
1935 /* A[11] = next_pc[31: 0]; */
1936 env->gpr_a[11] = next_pc;
1938 /* if (tmp_FCX == LCX) trap(FCD);*/
1939 if (tmp_FCX == env->LCX) {
1940 /* FCD trap */
1942 psw_write(env, psw);
1945 void helper_ret(CPUTriCoreState *env)
1947 target_ulong ea;
1948 target_ulong new_PCXI;
1949 target_ulong new_PSW, psw;
1951 psw = psw_read(env);
1952 /* if (PSW.CDE) then if (cdc_decrement()) then trap(CDU);*/
1953 if (env->PSW & MASK_PSW_CDE) {
1954 if (cdc_decrement(&(env->PSW))) {
1955 /* CDU trap */
1958 /* if (PCXI[19: 0] == 0) then trap(CSU); */
1959 if ((env->PCXI & 0xfffff) == 0) {
1960 /* CSU trap */
1962 /* if (PCXI.UL == 0) then trap(CTYP); */
1963 if ((env->PCXI & MASK_PCXI_UL) == 0) {
1964 /* CTYP trap */
1966 /* PC = {A11 [31: 1], 1’b0}; */
1967 env->PC = env->gpr_a[11] & 0xfffffffe;
1969 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
1970 ea = ((env->PCXI & MASK_PCXI_PCXS) << 12) +
1971 ((env->PCXI & MASK_PCXI_PCXO) << 6);
1972 /* {new_PCXI, new_PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
1973 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
1974 restore_context_upper(env, ea, &new_PCXI, &new_PSW);
1975 /* M(EA, word) = FCX; */
1976 cpu_stl_data(env, ea, env->FCX);
1977 /* FCX[19: 0] = PCXI[19: 0]; */
1978 env->FCX = (env->FCX & 0xfff00000) + (env->PCXI & 0x000fffff);
1979 /* PCXI = new_PCXI; */
1980 env->PCXI = new_PCXI;
1982 if (tricore_feature(env, TRICORE_FEATURE_13)) {
1983 /* PSW = new_PSW */
1984 psw_write(env, new_PSW);
1985 } else {
1986 /* PSW = {new_PSW[31:26], PSW[25:24], new_PSW[23:0]}; */
1987 psw_write(env, (new_PSW & ~(0x3000000)) + (psw & (0x3000000)));
1991 void helper_bisr(CPUTriCoreState *env, uint32_t const9)
1993 target_ulong tmp_FCX;
1994 target_ulong ea;
1995 target_ulong new_FCX;
1997 if (env->FCX == 0) {
1998 /* FCU trap */
2001 tmp_FCX = env->FCX;
2002 ea = ((env->FCX & 0xf0000) << 12) + ((env->FCX & 0xffff) << 6);
2004 /* new_FCX = M(EA, word); */
2005 new_FCX = cpu_ldl_data(env, ea);
2006 /* M(EA, 16 * word) = {PCXI, A[11], A[2], A[3], D[0], D[1], D[2], D[3], A[4]
2007 , A[5], A[6], A[7], D[4], D[5], D[6], D[7]}; */
2008 save_context_lower(env, ea);
2011 /* PCXI.PCPN = ICR.CCPN */
2012 env->PCXI = (env->PCXI & 0xffffff) +
2013 ((env->ICR & MASK_ICR_CCPN) << 24);
2014 /* PCXI.PIE = ICR.IE */
2015 env->PCXI = ((env->PCXI & ~MASK_PCXI_PIE) +
2016 ((env->ICR & MASK_ICR_IE) << 15));
2017 /* PCXI.UL = 0 */
2018 env->PCXI &= ~(MASK_PCXI_UL);
2019 /* PCXI[19: 0] = FCX[19: 0] */
2020 env->PCXI = (env->PCXI & 0xfff00000) + (env->FCX & 0xfffff);
2021 /* FXC[19: 0] = new_FCX[19: 0] */
2022 env->FCX = (env->FCX & 0xfff00000) + (new_FCX & 0xfffff);
2023 /* ICR.IE = 1 */
2024 env->ICR |= MASK_ICR_IE;
2026 env->ICR |= const9; /* ICR.CCPN = const9[7: 0];*/
2028 if (tmp_FCX == env->LCX) {
2029 /* FCD trap */
2033 void helper_rfe(CPUTriCoreState *env)
2035 target_ulong ea;
2036 target_ulong new_PCXI;
2037 target_ulong new_PSW;
2038 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2039 if ((env->PCXI & 0xfffff) == 0) {
2040 /* raise csu trap */
2042 /* if (PCXI.UL == 0) then trap(CTYP); */
2043 if ((env->PCXI & MASK_PCXI_UL) == 0) {
2044 /* raise CTYP trap */
2046 /* if (!cdc_zero() AND PSW.CDE) then trap(NEST); */
2047 if (!cdc_zero(&(env->PSW)) && (env->PSW & MASK_PSW_CDE)) {
2048 /* raise MNG trap */
2050 /* ICR.IE = PCXI.PIE; */
2051 env->ICR = (env->ICR & ~MASK_ICR_IE) + ((env->PCXI & MASK_PCXI_PIE) >> 15);
2052 /* ICR.CCPN = PCXI.PCPN; */
2053 env->ICR = (env->ICR & ~MASK_ICR_CCPN) +
2054 ((env->PCXI & MASK_PCXI_PCPN) >> 24);
2055 /*EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0};*/
2056 ea = ((env->PCXI & MASK_PCXI_PCXS) << 12) +
2057 ((env->PCXI & MASK_PCXI_PCXO) << 6);
2058 /*{new_PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2059 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2060 restore_context_upper(env, ea, &new_PCXI, &new_PSW);
2061 /* M(EA, word) = FCX;*/
2062 cpu_stl_data(env, ea, env->FCX);
2063 /* FCX[19: 0] = PCXI[19: 0]; */
2064 env->FCX = (env->FCX & 0xfff00000) + (env->PCXI & 0x000fffff);
2065 /* PCXI = new_PCXI; */
2066 env->PCXI = new_PCXI;
2067 /* write psw */
2068 psw_write(env, new_PSW);
2071 void helper_ldlcx(CPUTriCoreState *env, uint32_t ea)
2073 uint32_t dummy;
2074 /* insn doesn't load PCXI and RA */
2075 restore_context_lower(env, ea, &dummy, &dummy);
2078 void helper_lducx(CPUTriCoreState *env, uint32_t ea)
2080 uint32_t dummy;
2081 /* insn doesn't load PCXI and PSW */
2082 restore_context_upper(env, ea, &dummy, &dummy);
2085 void helper_stlcx(CPUTriCoreState *env, uint32_t ea)
2087 save_context_lower(env, ea);
2090 void helper_stucx(CPUTriCoreState *env, uint32_t ea)
2092 save_context_upper(env, ea);
2095 void helper_psw_write(CPUTriCoreState *env, uint32_t arg)
2097 psw_write(env, arg);
2100 uint32_t helper_psw_read(CPUTriCoreState *env)
2102 return psw_read(env);
2106 static inline void QEMU_NORETURN do_raise_exception_err(CPUTriCoreState *env,
2107 uint32_t exception,
2108 int error_code,
2109 uintptr_t pc)
2111 CPUState *cs = CPU(tricore_env_get_cpu(env));
2112 cs->exception_index = exception;
2113 env->error_code = error_code;
2115 if (pc) {
2116 /* now we have a real cpu fault */
2117 cpu_restore_state(cs, pc);
2120 cpu_loop_exit(cs);
2123 void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
2124 uintptr_t retaddr)
2126 int ret;
2127 ret = cpu_tricore_handle_mmu_fault(cs, addr, is_write, mmu_idx);
2128 if (ret) {
2129 TriCoreCPU *cpu = TRICORE_CPU(cs);
2130 CPUTriCoreState *env = &cpu->env;
2131 do_raise_exception_err(env, cs->exception_index,
2132 env->error_code, retaddr);