2 * PPC emulation micro-operations for qemu.
4 * Copyright (c) 2003 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #define Ts0 (int32_t)T0
26 #define Ts1 (int32_t)T1
27 #define Ts2 (int32_t)T2
29 #define FT0 (env->ft0)
31 #define PPC_OP(name) void op_##name(void)
34 #include "op_template.h"
37 #include "op_template.h"
40 #include "op_template.h"
43 #include "op_template.h"
46 #include "op_template.h"
49 #include "op_template.h"
52 #include "op_template.h"
55 #include "op_template.h"
58 #include "op_template.h"
61 #include "op_template.h"
64 #include "op_template.h"
67 #include "op_template.h"
70 #include "op_template.h"
73 #include "op_template.h"
76 #include "op_template.h"
79 #include "op_template.h"
82 #include "op_template.h"
85 #include "op_template.h"
88 #include "op_template.h"
91 #include "op_template.h"
94 #include "op_template.h"
97 #include "op_template.h"
100 #include "op_template.h"
103 #include "op_template.h"
106 #include "op_template.h"
109 #include "op_template.h"
112 #include "op_template.h"
115 #include "op_template.h"
118 #include "op_template.h"
121 #include "op_template.h"
124 #include "op_template.h"
127 #include "op_template.h"
129 /* PPC state maintenance operations */
137 } else if (Ts0 > 0) {
152 } else if (Ts0 > 0) {
165 set_CRn(0, 0x02 | xer_ov);
172 set_CRn(0, 0x04 | xer_ov);
194 /* Update time base */
197 T0 = regs->spr[SPR_ENCODE(268)];
201 T1 = regs->spr[SPR_ENCODE(269)] + 1;
202 regs->spr[SPR_ENCODE(269)] = T1;
204 regs->spr[SPR_ENCODE(268)] = T0;
208 PPC_OP(raise_exception)
210 raise_exception(PARAM(1));
227 do_store_cr(PARAM(1), T0);
233 T0 = (xer_so << 3) | (xer_ov << 2) | (xer_ca << 1);
281 /* Set reservation */
282 PPC_OP(set_reservation)
284 regs->reserve = T1 & ~0x03;
288 /* Reset reservation */
289 PPC_OP(reset_reservation)
298 T0 = (T0 >> PARAM(1)) & 1;
304 T1 = (T1 >> PARAM(1)) & 1;
310 T1 = (T1 & PARAM(1)) | (T0 << PARAM(2));
315 #define __PPC_OP_B(name, target) \
318 regs->nip = (target); \
322 #define __PPC_OP_BL(name, target) \
325 regs->LR = PARAM(1); \
326 regs->nip = (target); \
330 #define PPC_OP_B(name, target) \
331 __PPC_OP_B(name, target); \
332 __PPC_OP_BL(name##l, target)
334 #define __PPC_OP_BC(name, cond, target) \
346 #define __PPC_OP_BCL(name, cond, target) \
351 regs->LR = PARAM(1); \
359 #define _PPC_OP_BC(name, namel, cond, target) \
360 __PPC_OP_BC(name, cond, target); \
361 __PPC_OP_BCL(namel, cond, target)
363 /* Branch to target */
364 #define PPC_OP_BC(name, cond) \
365 _PPC_OP_BC(b_##name, bl_##name, cond, PARAM(2))
367 PPC_OP_B(b, PARAM(1));
368 PPC_OP_BC(ctr, (regs->CTR != 0));
369 PPC_OP_BC(ctr_true, (regs->CTR != 0 && (T0 & PARAM(3)) != 0));
370 PPC_OP_BC(ctr_false, (regs->CTR != 0 && (T0 & PARAM(3)) == 0));
371 PPC_OP_BC(ctrz, (regs->CTR == 0));
372 PPC_OP_BC(ctrz_true, (regs->CTR == 0 && (T0 & PARAM(3)) != 0));
373 PPC_OP_BC(ctrz_false, (regs->CTR == 0 && (T0 & PARAM(3)) == 0));
374 PPC_OP_BC(true, ((T0 & PARAM(3)) != 0));
375 PPC_OP_BC(false, ((T0 & PARAM(3)) == 0));
378 #define PPC_OP_BCCTR(name, cond) \
379 _PPC_OP_BC(bctr_##name, bctrl_##name, cond, regs->CTR & ~0x03)
381 PPC_OP_B(bctr, regs->CTR & ~0x03);
382 PPC_OP_BCCTR(ctr, (regs->CTR != 0));
383 PPC_OP_BCCTR(ctr_true, (regs->CTR != 0 && (T0 & PARAM(2)) != 0));
384 PPC_OP_BCCTR(ctr_false, (regs->CTR != 0 && (T0 & PARAM(2)) == 0));
385 PPC_OP_BCCTR(ctrz, (regs->CTR == 0));
386 PPC_OP_BCCTR(ctrz_true, (regs->CTR == 0 && (T0 & PARAM(2)) != 0));
387 PPC_OP_BCCTR(ctrz_false, (regs->CTR == 0 && (T0 & PARAM(2)) == 0));
388 PPC_OP_BCCTR(true, ((T0 & PARAM(2)) != 0));
389 PPC_OP_BCCTR(false, ((T0 & PARAM(2)) == 0));
392 #define PPC_OP_BCLR(name, cond) \
393 _PPC_OP_BC(blr_##name, blrl_##name, cond, regs->LR & ~0x03)
395 PPC_OP_B(blr, regs->LR & ~0x03);
396 PPC_OP_BCLR(ctr, (regs->CTR != 0));
397 PPC_OP_BCLR(ctr_true, (regs->CTR != 0 && (T0 & PARAM(2)) != 0));
398 PPC_OP_BCLR(ctr_false, (regs->CTR != 0 && (T0 & PARAM(2)) == 0));
399 PPC_OP_BCLR(ctrz, (regs->CTR == 0));
400 PPC_OP_BCLR(ctrz_true, (regs->CTR == 0 && (T0 & PARAM(2)) != 0));
401 PPC_OP_BCLR(ctrz_false, (regs->CTR == 0 && (T0 & PARAM(2)) == 0));
402 PPC_OP_BCLR(true, ((T0 & PARAM(2)) != 0));
403 PPC_OP_BCLR(false, ((T0 & PARAM(2)) == 0));
405 /* CTR maintenance */
412 /*** Integer arithmetic ***/
424 if ((T2 ^ T1 ^ (-1)) & (T2 ^ T0) & (1 << 31)) {
455 if ((T2 ^ T1 ^ (-1)) & (T2 ^ T0) & (1 << 31)) {
465 /* candidate for helper (too long) */
470 if (T0 < T2 || (xer_ca == 1 && T0 == T2)) {
482 if (T0 < T2 || (xer_ca == 1 && T0 == T2)) {
487 if ((T2 ^ T1 ^ (-1)) & (T2 ^ T0) & (1 << 31)) {
503 /* add immediate carrying */
516 /* add to minus one extended */
530 if (T1 & (T1 ^ T0) & (1 << 31)) {
541 /* add to zero extended */
558 if ((T1 ^ (-1)) & (T1 ^ T0) & (1 << 31)) {
573 /* candidate for helper (too long) */
576 if ((Ts0 == INT32_MIN && Ts1 == -1) || Ts1 == 0) {
577 Ts0 = (-1) * (T0 >> 31);
586 if ((Ts0 == INT32_MIN && Ts1 == -1) || Ts1 == 0) {
589 T0 = (-1) * (T0 >> 31);
597 /* divide word unsigned */
621 /* multiply high word */
624 Ts0 = ((int64_t)Ts0 * (int64_t)Ts1) >> 32;
628 /* multiply high word unsigned */
631 T0 = ((uint64_t)T0 * (uint64_t)T1) >> 32;
635 /* multiply low immediate */
642 /* multiply low word */
651 int64_t res = (int64_t)Ts0 * (int64_t)Ts1;
653 if ((int32_t)res != res) {
666 if (T0 != 0x80000000) {
674 if (T0 == 0x80000000) {
695 if (((~T2) ^ T1 ^ (-1)) & ((~T2) ^ T0) & (1 << 31)) {
704 /* substract from carrying */
725 if (((~T2) ^ T1 ^ (-1)) & ((~T2) ^ T0) & (1 << 31)) {
734 /* substract from extended */
735 /* candidate for helper (too long) */
738 T0 = T1 + ~T0 + xer_ca;
739 if (T0 < T1 || (xer_ca == 1 && T0 == T1)) {
750 T0 = T1 + ~T0 + xer_ca;
751 if ((~T2 ^ T1 ^ (-1)) & (~T2 ^ T0) & (1 << 31)) {
757 if (T0 < T1 || (xer_ca == 1 && T0 == T1)) {
765 /* substract from immediate carrying */
768 T0 = PARAM(1) + ~T0 + 1;
769 if (T0 <= PARAM(1)) {
777 /* substract from minus one extended */
780 T0 = ~T0 + xer_ca - 1;
790 T0 = ~T0 + xer_ca - 1;
791 if (~T1 & (~T1 ^ T0) & (1 << 31)) {
802 /* substract from zero extended */
819 if ((~T1 ^ (-1)) & ((~T1) ^ T0) & (1 << 31)) {
833 /*** Integer comparison ***/
839 } else if (Ts0 > Ts1) {
847 /* compare immediate */
850 if (Ts0 < SPARAM(1)) {
852 } else if (Ts0 > SPARAM(1)) {
860 /* compare logical */
865 } else if (T0 > T1) {
873 /* compare logical immediate */
878 } else if (T0 > PARAM(1)) {
886 /*** Integer logical ***/
908 /* count leading zero */
912 for (T0 = 32; T1 > 0; T0--)
924 /* extend sign byte */
931 /* extend sign half word */
987 /*** Integer rotate ***/
988 /* rotate left word immediate then mask insert */
991 T0 = rotl(T0, PARAM(1) & PARAM(2)) | (T0 & PARAM(3));
995 /* rotate left immediate then and with mask insert */
998 T0 = rotl(T0, PARAM(1));
1004 T0 = T0 << PARAM(1);
1010 T0 = T0 >> PARAM(1);
1014 /* rotate left word then and with mask insert */
1017 T0 = rotl(T0, PARAM(1)) & PARAM(2);
1029 T0 = rotl(T0, T1) & PARAM(1);
1033 /*** Integer shift ***/
1034 /* shift left word */
1045 /* shift right algebraic word */
1048 Ts0 = do_sraw(Ts0, T1);
1052 /* shift right algebraic word immediate */
1056 Ts0 = Ts0 >> PARAM(1);
1057 if (Ts1 < 0 && (Ts1 & PARAM(2)) != 0) {
1065 /* shift right word */
1076 /*** Floating-Point arithmetic ***/
1078 /*** Floating-Point multiply-and-add ***/
1080 /*** Floating-Point round & convert ***/
1082 /*** Floating-Point compare ***/
1084 /*** Floating-Point status & ctrl register ***/
1086 /*** Integer load ***/
1087 #define ld16x(x) s_ext16(ld16(x))
1088 #define PPC_ILD_OPX(name, op) \
1089 PPC_OP(l##name##x_z) \
1094 PPC_OP(l##name##x) \
1101 #define PPC_ILD_OP(name, op) \
1102 PPC_OP(l##name##_z) \
1104 T1 = op(SPARAM(1)); \
1113 PPC_ILD_OPX(name, op)
1115 PPC_ILD_OP(bz, ld8);
1116 PPC_ILD_OP(ha, ld16x);
1117 PPC_ILD_OP(hz, ld16);
1118 PPC_ILD_OP(wz, ld32);
1120 /*** Integer store ***/
1121 #define PPC_IST_OPX(name, op) \
1122 PPC_OP(st##name##x_z) \
1127 PPC_OP(st##name##x) \
1134 #define PPC_IST_OP(name, op) \
1135 PPC_OP(st##name##_z) \
1137 op(SPARAM(1), T0); \
1146 PPC_IST_OPX(name, op);
1149 PPC_IST_OP(h, st16);
1150 PPC_IST_OP(w, st32);
1152 /*** Integer load and store with byte reverse ***/
1153 PPC_ILD_OPX(hbr, ld16r);
1154 PPC_ILD_OPX(wbr, ld32r);
1155 PPC_IST_OPX(hbr, st16r);
1156 PPC_IST_OPX(wbr, st32r);
1158 /*** Integer load and store multiple ***/
1161 do_lmw(PARAM(1), SPARAM(2) + T0);
1167 do_stmw(PARAM(1), SPARAM(2) + T0);
1171 /*** Integer load and store strings ***/
1174 do_lsw(PARAM(1), PARAM(2), T0);
1180 do_lsw(PARAM(1), T0, T1 + T2);
1186 do_stsw(PARAM(1), PARAM(2), 0);
1192 do_stsw(PARAM(1), PARAM(2), T0);
1198 do_stsw(PARAM(1), T0, T1);
1204 do_stsw(PARAM(1), T0, T1 + T2);
1211 T0 = regs->spr[PARAM(1)];
1216 regs->spr[PARAM(1)] = T0;
1222 T0 = do_load_fpscr();
1227 do_store_fpscr(PARAM(1), T0);
1230 /*** Floating-point store ***/
1232 static inline uint32_t dtos(uint64_t f)
1234 unsigned int e, m, s;
1235 e = (((f >> 52) & 0x7ff) - 1022) + 126;
1238 return (s << 31) | (e << 23) | m;
1241 static inline uint64_t stod(uint32_t f)
1243 unsigned int e, m, s;
1244 e = ((f >> 23) & 0xff) - 126 + 1022;
1246 m = f & ((1 << 23) - 1);
1247 return ((uint64_t)s << 63) | ((uint64_t)e << 52) | ((uint64_t)m << 29);
1252 st64(SPARAM(1), FT0);
1275 st32(SPARAM(1), dtos(FT0));
1281 st32(T0, dtos(FT0));
1286 st32(T0, dtos(FT0));
1292 st32(T0, dtos(FT0));
1295 /*** Floating-point load ***/
1298 FT0 = ld64(SPARAM(1));
1320 FT0 = stod(ld32(SPARAM(1)));
1326 FT0 = stod(ld32(T0));
1331 FT0 = stod(ld32(T0));
1337 FT0 = stod(ld32(T0));