30 /* FIXME - should be including a version of syscall.h that does not
31 pollute the name space */
32 #include "../../libgloss/v850/sys/syscall.h"
34 #include "libiberty.h"
37 #if !defined(__GO32__) && !defined(_WIN32)
39 #include <sys/times.h>
43 /* start-sanitize-v850e */
44 /* This is an array of the bit positions of registers r20 .. r31 in
45 that order in a prepare/dispose instruction. */
46 int type1_regs[12] = { 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 0, 21 };
47 /* end-sanitize-v850e */
48 /* start-sanitize-v850eq */
49 /* This is an array of the bit positions of registers r16 .. r31 in
50 that order in a push/pop instruction. */
51 int type2_regs[16] = { 3, 2, 1, 0, 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 20, 21};
52 /* This is an array of the bit positions of registers r1 .. r15 in
53 that order in a push/pop instruction. */
54 int type3_regs[15] = { 2, 1, 0, 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 20, 21};
55 /* end-sanitize-v850eq */
58 #ifndef SIZE_INSTRUCTION
59 #define SIZE_INSTRUCTION 18
63 #define SIZE_VALUES 11
67 static unsigned32 trace_values[3];
68 static int trace_num_values;
69 static unsigned32 trace_pc;
70 static char *trace_name;
74 trace_input (name, type, size)
80 if (!TRACE_ALU_P (STATE_CPU (simulator, 0)))
97 trace_values[0] = State.regs[OP[0]];
101 /* start-sanitize-v850e */
103 /* end-sanitize-v850e */
106 trace_values[0] = State.regs[OP[1]];
107 trace_values[1] = State.regs[OP[0]];
108 trace_num_values = 2;
113 trace_values[0] = SEXT5 (OP[0]);
114 trace_values[1] = OP[1];
115 trace_num_values = 2;
118 case OP_IMM_REG_MOVE:
119 trace_values[0] = SEXT5 (OP[0]);
120 trace_num_values = 1;
124 trace_values[0] = State.pc;
125 trace_values[1] = SEXT9 (OP[0]);
126 trace_values[2] = PSW;
127 trace_num_values = 3;
131 trace_values[0] = OP[1] * size;
132 trace_values[1] = State.regs[30];
133 trace_num_values = 2;
137 trace_values[0] = State.regs[OP[0]];
138 trace_values[1] = OP[1] * size;
139 trace_values[2] = State.regs[30];
140 trace_num_values = 3;
144 trace_values[0] = EXTEND16 (OP[2]);
145 trace_values[1] = State.regs[OP[0]];
146 trace_num_values = 2;
150 trace_values[0] = State.regs[OP[1]];
151 trace_values[1] = EXTEND16 (OP[2]);
152 trace_values[2] = State.regs[OP[0]];
153 trace_num_values = 3;
157 trace_values[0] = SEXT22 (OP[0]);
158 trace_values[1] = State.pc;
159 trace_num_values = 2;
163 trace_values[0] = EXTEND16 (OP[0]) << size;
164 trace_values[1] = State.regs[OP[1]];
165 trace_num_values = 2;
168 case OP_UIMM_REG_REG:
169 trace_values[0] = (OP[0] & 0xffff) << size;
170 trace_values[1] = State.regs[OP[1]];
171 trace_num_values = 2;
175 trace_num_values = 0;
179 trace_values[0] = PSW;
180 trace_num_values = 1;
184 trace_num_values = 0;
188 trace_values[0] = State.regs[OP[0]];
189 trace_num_values = 1;
193 trace_values[0] = State.sregs[OP[1]];
194 trace_num_values = 1;
200 trace_output (result)
201 enum op_types result;
206 if (!TRACE_ALU_P (STATE_CPU (simulator, 0)))
212 /* write out the values saved during the trace_input call */
215 for (i = 0; i < trace_num_values; i++)
217 sprintf (chp, "%*s0x%.8lx", SIZE_VALUES - 10, "", trace_values[i]);
218 chp = strchr (chp, '\0');
222 sprintf (chp, "%*s", SIZE_VALUES, "");
223 chp = strchr (chp, '\0');
245 sprintf (chp, " :: 0x%.8lx", (unsigned long)State.regs[OP[0]]);
249 case OP_REG_REG_MOVE:
251 case OP_IMM_REG_MOVE:
254 sprintf (chp, " :: 0x%.8lx", (unsigned long)State.regs[OP[1]]);
258 case OP_UIMM_REG_REG:
259 sprintf (chp, " :: 0x%.8lx", (unsigned long)State.regs[OP[2]]);
264 sprintf (chp, " :: 0x%.8lx", (unsigned long)State.regs[OP[1]]);
268 sprintf (chp, " :: 0x%.8lx", (unsigned long)State.sregs[OP[1]]);
272 trace_one_insn (simulator, STATE_CPU (simulator, 0), trace_pc,
273 TRACE_LINENUM_P (STATE_CPU (simulator, 0)),
274 "simops", __LINE__, "alu",
275 "%-*s -%s", SIZE_INSTRUCTION, trace_name, buf);
281 /* Returns 1 if the specific condition is met, returns 0 otherwise. */
283 condition_met (unsigned code)
285 unsigned int psw = PSW;
289 case 0x0: return ((psw & PSW_OV) != 0);
290 case 0x1: return ((psw & PSW_CY) != 0);
291 case 0x2: return ((psw & PSW_Z) != 0);
292 case 0x3: return ((((psw & PSW_CY) != 0) | ((psw & PSW_Z) != 0)) != 0);
293 case 0x4: return ((psw & PSW_S) != 0);
294 /*case 0x5: return 1;*/
295 case 0x6: return ((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) != 0);
296 case 0x7: return (((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) || ((psw & PSW_Z) != 0)) != 0);
297 case 0x8: return ((psw & PSW_OV) == 0);
298 case 0x9: return ((psw & PSW_CY) == 0);
299 case 0xa: return ((psw & PSW_Z) == 0);
300 case 0xb: return ((((psw & PSW_CY) != 0) | ((psw & PSW_Z) != 0)) == 0);
301 case 0xc: return ((psw & PSW_S) == 0);
302 case 0xd: return ((psw & PSW_SAT) != 0);
303 case 0xe: return ((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) == 0);
304 case 0xf: return (((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) || ((psw & PSW_Z) != 0)) == 0);
309 /* start-sanitize-v850e */
312 Add32 (unsigned long a1, unsigned long a2, int * carry)
314 unsigned long result = (a1 + a2);
316 * carry = (result < a1);
322 Multiply64 (boolean sign, unsigned long op0)
333 op1 = State.regs[ OP[1] ];
337 /* Compute sign of result and adjust operands if necessary. */
339 sign = (op0 ^ op1) & 0x80000000;
341 if (((signed long) op0) < 0)
344 if (((signed long) op1) < 0)
348 /* We can split the 32x32 into four 16x16 operations. This ensures
349 that we do not lose precision on 32bit only hosts: */
350 lo = ( (op0 & 0xFFFF) * (op1 & 0xFFFF));
351 mid1 = ( (op0 & 0xFFFF) * ((op1 >> 16) & 0xFFFF));
352 mid2 = (((op0 >> 16) & 0xFFFF) * (op1 & 0xFFFF));
353 hi = (((op0 >> 16) & 0xFFFF) * ((op1 >> 16) & 0xFFFF));
355 /* We now need to add all of these results together, taking care
356 to propogate the carries from the additions: */
357 RdLo = Add32 (lo, (mid1 << 16), & carry);
359 RdLo = Add32 (RdLo, (mid2 << 16), & carry);
360 RdHi += (carry + ((mid1 >> 16) & 0xFFFF) + ((mid2 >> 16) & 0xFFFF) + hi);
364 /* Negate result if necessary. */
368 if (RdLo == 0xFFFFFFFF)
377 /* Don't store into register 0. */
379 State.regs[ OP[1] ] = RdLo;
381 State.regs[ OP[2] >> 11 ] = RdHi;
386 /* end-sanitize-v850e */
388 /* Read a null terminated string from memory, return in a buffer */
396 while (sim_core_read_1 (STATE_CPU (sd, 0),
397 PC, sim_core_read_map, addr + nr) != 0)
399 buf = NZALLOC (char, nr + 1);
400 sim_read (simulator, addr, buf, nr);
404 /* Read a null terminated argument vector from memory, return in a
407 fetch_argv (sd, addr)
413 char **buf = xmalloc (max_nr * sizeof (char*));
416 unsigned32 a = sim_core_read_4 (STATE_CPU (sd, 0),
417 PC, sim_core_read_map, addr + nr * 4);
419 buf[nr] = fetch_str (sd, a);
421 if (nr == max_nr - 1)
424 buf = xrealloc (buf, max_nr * sizeof (char*));
436 unsigned long result;
438 result = load_mem (State.regs[30] + (OP[3] & 0x7f), 1);
440 /* start-sanitize-v850eq */
443 trace_input ("sld.bu", OP_LOAD16, 1);
444 State.regs[ OP[1] ] = result;
448 /* end-sanitize-v850eq */
449 trace_input ("sld.b", OP_LOAD16, 1);
451 State.regs[ OP[1] ] = EXTEND8 (result);
452 /* start-sanitize-v850eq */
454 /* end-sanitize-v850eq */
456 trace_output (OP_LOAD16);
465 unsigned long result;
467 result = load_mem (State.regs[30] + ((OP[3] & 0x7f) << 1), 2);
469 /* start-sanitize-v850eq */
472 trace_input ("sld.hu", OP_LOAD16, 2);
473 State.regs[ OP[1] ] = result;
477 /* end-sanitize-v850eq */
478 trace_input ("sld.h", OP_LOAD16, 2);
480 State.regs[ OP[1] ] = EXTEND16 (result);
481 /* start-sanitize-v850eq */
483 /* end-sanitize-v850eq */
485 trace_output (OP_LOAD16);
494 trace_input ("sld.w", OP_LOAD16, 4);
496 State.regs[ OP[1] ] = load_mem (State.regs[30] + ((OP[3] & 0x7f) << 1), 4);
498 trace_output (OP_LOAD16);
507 trace_input ("sst.b", OP_STORE16, 1);
509 store_mem (State.regs[30] + (OP[3] & 0x7f), 1, State.regs[ OP[1] ]);
511 trace_output (OP_STORE16);
520 trace_input ("sst.h", OP_STORE16, 2);
522 store_mem (State.regs[30] + ((OP[3] & 0x7f) << 1), 2, State.regs[ OP[1] ]);
524 trace_output (OP_STORE16);
533 trace_input ("sst.w", OP_STORE16, 4);
535 store_mem (State.regs[30] + ((OP[3] & 0x7e) << 1), 4, State.regs[ OP[1] ]);
537 trace_output (OP_STORE16);
548 trace_input ("ld.b", OP_LOAD32, 1);
550 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
552 State.regs[ OP[1] ] = EXTEND8 (load_mem (adr, 1));
554 trace_output (OP_LOAD32);
565 trace_input ("ld.h", OP_LOAD32, 2);
567 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
570 State.regs[ OP[1] ] = EXTEND16 (load_mem (adr, 2));
572 trace_output (OP_LOAD32);
583 trace_input ("ld.w", OP_LOAD32, 4);
585 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
588 State.regs[ OP[1] ] = load_mem (adr, 4);
590 trace_output (OP_LOAD32);
599 trace_input ("st.b", OP_STORE32, 1);
601 store_mem (State.regs[ OP[0] ] + EXTEND16 (OP[2]), 1, State.regs[ OP[1] ]);
603 trace_output (OP_STORE32);
614 trace_input ("st.h", OP_STORE32, 2);
616 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
619 store_mem (adr, 2, State.regs[ OP[1] ]);
621 trace_output (OP_STORE32);
632 trace_input ("st.w", OP_STORE32, 4);
634 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
637 store_mem (adr, 4, State.regs[ OP[1] ]);
639 trace_output (OP_STORE32);
647 trace_input ("Bcond", OP_COND_BR, 0);
648 trace_output (OP_COND_BR);
650 if (condition_met (code))
651 return SEXT9 (((OP[3] & 0x70) >> 3) | ((OP[3] & 0xf800) >> 7));
768 /* jarl/jr disp22, reg */
772 trace_input ("jarl/jr", OP_JUMP, 0);
775 State.regs[ OP[1] ] = PC + 4;
777 trace_output (OP_JUMP);
779 return SEXT22 (((OP[3] & 0x3f) << 16) | OP[2]);
786 unsigned int op0, op1, result, z, s, cy, ov;
788 trace_input ("add", OP_REG_REG, 0);
790 /* Compute the result. */
792 op0 = State.regs[ OP[0] ];
793 op1 = State.regs[ OP[1] ];
797 /* Compute the condition codes. */
799 s = (result & 0x80000000);
800 cy = (result < op0 || result < op1);
801 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
802 && (op0 & 0x80000000) != (result & 0x80000000));
804 /* Store the result and condition codes. */
805 State.regs[OP[1]] = result;
806 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
807 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
808 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
809 trace_output (OP_REG_REG);
814 /* add sign_extend(imm5), reg */
818 unsigned int op0, op1, result, z, s, cy, ov;
821 trace_input ("add", OP_IMM_REG, 0);
823 /* Compute the result. */
824 temp = SEXT5 (OP[0]);
826 op1 = State.regs[OP[1]];
829 /* Compute the condition codes. */
831 s = (result & 0x80000000);
832 cy = (result < op0 || result < op1);
833 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
834 && (op0 & 0x80000000) != (result & 0x80000000));
836 /* Store the result and condition codes. */
837 State.regs[OP[1]] = result;
838 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
839 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
840 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
841 trace_output (OP_IMM_REG);
846 /* addi sign_extend(imm16), reg, reg */
850 unsigned int op0, op1, result, z, s, cy, ov;
852 trace_input ("addi", OP_IMM_REG_REG, 0);
854 /* Compute the result. */
856 op0 = EXTEND16 (OP[2]);
857 op1 = State.regs[ OP[0] ];
860 /* Compute the condition codes. */
862 s = (result & 0x80000000);
863 cy = (result < op0 || result < op1);
864 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
865 && (op0 & 0x80000000) != (result & 0x80000000));
867 /* Store the result and condition codes. */
868 State.regs[OP[1]] = result;
869 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
870 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
871 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
872 trace_output (OP_IMM_REG_REG);
881 unsigned int op0, op1, result, z, s, cy, ov;
883 trace_input ("sub", OP_REG_REG, 0);
884 /* Compute the result. */
885 op0 = State.regs[ OP[0] ];
886 op1 = State.regs[ OP[1] ];
889 /* Compute the condition codes. */
891 s = (result & 0x80000000);
893 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
894 && (op1 & 0x80000000) != (result & 0x80000000));
896 /* Store the result and condition codes. */
897 State.regs[OP[1]] = result;
898 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
899 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
900 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
901 trace_output (OP_REG_REG);
906 /* subr reg1, reg2 */
910 unsigned int op0, op1, result, z, s, cy, ov;
912 trace_input ("subr", OP_REG_REG, 0);
913 /* Compute the result. */
914 op0 = State.regs[ OP[0] ];
915 op1 = State.regs[ OP[1] ];
918 /* Compute the condition codes. */
920 s = (result & 0x80000000);
922 ov = ((op0 & 0x80000000) != (op1 & 0x80000000)
923 && (op0 & 0x80000000) != (result & 0x80000000));
925 /* Store the result and condition codes. */
926 State.regs[OP[1]] = result;
927 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
928 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
929 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
930 trace_output (OP_REG_REG);
936 /* mulh reg1, reg2 */
940 /* start-sanitize-v850e */
943 trace_input ("sxh", OP_REG, 0);
945 State.regs[ OP[0] ] = EXTEND16 (State.regs[ OP[0] ]);
947 trace_output (OP_REG);
950 /* end-sanitize-v850e */
952 trace_input ("mulh", OP_REG_REG, 0);
954 State.regs[ OP[1] ] = (EXTEND16 (State.regs[ OP[1] ]) * EXTEND16 (State.regs[ OP[0] ]));
956 trace_output (OP_REG_REG);
962 /* mulh sign_extend(imm5), reg2 */
966 trace_input ("mulh", OP_IMM_REG, 0);
968 State.regs[ OP[1] ] = EXTEND16 (State.regs[ OP[1] ]) * SEXT5 (OP[0]);
970 trace_output (OP_IMM_REG);
975 /* mulhi imm16, reg1, reg2 */
979 trace_input ("mulhi", OP_IMM_REG_REG, 0);
981 State.regs[ OP[1] ] = EXTEND16 (State.regs[ OP[0] ]) * EXTEND16 (OP[2]);
983 trace_output (OP_IMM_REG_REG);
988 /* divh reg1, reg2 */
993 /* start-sanitize-v850e */
998 trace_input ("switch", OP_REG, 0);
1000 adr = State.pc + 2 + (State.regs[ OP[0] ] << 1);
1001 State.pc = State.pc + 2 + (EXTEND16 (load_mem (adr, 2)) << 1);
1003 trace_output (OP_REG);
1006 /* end-sanitize-v850e */
1008 unsigned int op0, op1, result, ov, s, z;
1011 trace_input ("divh", OP_REG_REG, 0);
1013 /* Compute the result. */
1014 temp = EXTEND16 (State.regs[ OP[0] ]);
1016 op1 = State.regs[OP[1]];
1018 if (op0 == 0xffffffff && op1 == 0x80000000)
1020 result = 0x80000000;
1034 /* Compute the condition codes. */
1036 s = (result & 0x80000000);
1038 /* Store the result and condition codes. */
1039 State.regs[OP[1]] = result;
1040 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1041 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1042 | (ov ? PSW_OV : 0));
1043 trace_output (OP_REG_REG);
1053 unsigned int op0, op1, result, z, s, cy, ov;
1055 trace_input ("cmp", OP_REG_REG_CMP, 0);
1056 /* Compute the result. */
1057 op0 = State.regs[ OP[0] ];
1058 op1 = State.regs[ OP[1] ];
1061 /* Compute the condition codes. */
1063 s = (result & 0x80000000);
1065 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1066 && (op1 & 0x80000000) != (result & 0x80000000));
1068 /* Set condition codes. */
1069 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1070 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1071 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
1072 trace_output (OP_REG_REG_CMP);
1077 /* cmp sign_extend(imm5), reg */
1081 unsigned int op0, op1, result, z, s, cy, ov;
1084 /* Compute the result. */
1085 trace_input ("cmp", OP_IMM_REG_CMP, 0);
1086 temp = SEXT5 (OP[0]);
1088 op1 = State.regs[OP[1]];
1091 /* Compute the condition codes. */
1093 s = (result & 0x80000000);
1095 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1096 && (op1 & 0x80000000) != (result & 0x80000000));
1098 /* Set condition codes. */
1099 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1100 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1101 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
1102 trace_output (OP_IMM_REG_CMP);
1107 /* setf cccc,reg2 */
1111 trace_input ("setf", OP_EX1, 0);
1113 State.regs[ OP[1] ] = condition_met (OP[0]);
1115 trace_output (OP_EX1);
1120 /* satadd reg,reg */
1124 unsigned int op0, op1, result, z, s, cy, ov, sat;
1126 trace_input ("satadd", OP_REG_REG, 0);
1127 /* Compute the result. */
1128 op0 = State.regs[ OP[0] ];
1129 op1 = State.regs[ OP[1] ];
1132 /* Compute the condition codes. */
1134 s = (result & 0x80000000);
1135 cy = (result < op0 || result < op1);
1136 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
1137 && (op0 & 0x80000000) != (result & 0x80000000));
1140 /* Store the result and condition codes. */
1141 State.regs[OP[1]] = result;
1142 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1143 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1144 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1145 | (sat ? PSW_SAT : 0));
1147 /* Handle saturated results. */
1149 State.regs[OP[1]] = 0x80000000;
1151 State.regs[OP[1]] = 0x7fffffff;
1152 trace_output (OP_REG_REG);
1157 /* satadd sign_extend(imm5), reg */
1161 unsigned int op0, op1, result, z, s, cy, ov, sat;
1165 trace_input ("satadd", OP_IMM_REG, 0);
1167 /* Compute the result. */
1168 temp = SEXT5 (OP[0]);
1170 op1 = State.regs[OP[1]];
1173 /* Compute the condition codes. */
1175 s = (result & 0x80000000);
1176 cy = (result < op0 || result < op1);
1177 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
1178 && (op0 & 0x80000000) != (result & 0x80000000));
1181 /* Store the result and condition codes. */
1182 State.regs[OP[1]] = result;
1183 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1184 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1185 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1186 | (sat ? PSW_SAT : 0));
1188 /* Handle saturated results. */
1190 State.regs[OP[1]] = 0x80000000;
1192 State.regs[OP[1]] = 0x7fffffff;
1193 trace_output (OP_IMM_REG);
1198 /* satsub reg1, reg2 */
1203 /* start-sanitize-v850e */
1206 trace_input ("sxb", OP_REG, 0);
1208 State.regs[ OP[0] ] = EXTEND8 (State.regs[ OP[0] ]);
1210 trace_output (OP_REG);
1213 /* end-sanitize-v850e */
1215 unsigned int op0, op1, result, z, s, cy, ov, sat;
1217 trace_input ("satsub", OP_REG_REG, 0);
1219 /* Compute the result. */
1220 op0 = State.regs[ OP[0] ];
1221 op1 = State.regs[ OP[1] ];
1224 /* Compute the condition codes. */
1226 s = (result & 0x80000000);
1228 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1229 && (op1 & 0x80000000) != (result & 0x80000000));
1232 /* Store the result and condition codes. */
1233 State.regs[OP[1]] = result;
1234 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1235 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1236 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1237 | (sat ? PSW_SAT : 0));
1239 /* Handle saturated results. */
1241 State.regs[OP[1]] = 0x80000000;
1243 State.regs[OP[1]] = 0x7fffffff;
1244 trace_output (OP_REG_REG);
1250 /* satsubi sign_extend(imm16), reg */
1254 unsigned int op0, op1, result, z, s, cy, ov, sat;
1257 trace_input ("satsubi", OP_IMM_REG, 0);
1259 /* Compute the result. */
1260 temp = EXTEND16 (OP[2]);
1262 op1 = State.regs[ OP[0] ];
1265 /* Compute the condition codes. */
1267 s = (result & 0x80000000);
1269 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1270 && (op1 & 0x80000000) != (result & 0x80000000));
1273 /* Store the result and condition codes. */
1274 State.regs[OP[1]] = result;
1275 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1276 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1277 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1278 | (sat ? PSW_SAT : 0));
1280 /* Handle saturated results. */
1282 State.regs[OP[1]] = 0x80000000;
1284 State.regs[OP[1]] = 0x7fffffff;
1285 trace_output (OP_IMM_REG);
1290 /* satsubr reg,reg */
1294 unsigned int op0, op1, result, z, s, cy, ov, sat;
1296 trace_input ("satsubr", OP_REG_REG, 0);
1298 /* Compute the result. */
1299 op0 = State.regs[ OP[0] ];
1300 op1 = State.regs[ OP[1] ];
1303 /* Compute the condition codes. */
1305 s = (result & 0x80000000);
1306 cy = (result < op0);
1307 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1308 && (op1 & 0x80000000) != (result & 0x80000000));
1311 /* Store the result and condition codes. */
1312 State.regs[OP[1]] = result;
1313 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1314 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1315 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1316 | (sat ? PSW_SAT : 0));
1318 /* Handle saturated results. */
1320 State.regs[OP[1]] = 0x80000000;
1322 State.regs[OP[1]] = 0x7fffffff;
1323 trace_output (OP_REG_REG);
1332 unsigned int op0, op1, result, z, s;
1334 trace_input ("tst", OP_REG_REG_CMP, 0);
1336 /* Compute the result. */
1337 op0 = State.regs[ OP[0] ];
1338 op1 = State.regs[ OP[1] ];
1341 /* Compute the condition codes. */
1343 s = (result & 0x80000000);
1345 /* Store the condition codes. */
1346 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1347 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1348 trace_output (OP_REG_REG_CMP);
1357 trace_input ("mov", OP_REG_REG_MOVE, 0);
1359 State.regs[ OP[1] ] = State.regs[ OP[0] ];
1361 trace_output (OP_REG_REG_MOVE);
1366 /* mov sign_extend(imm5), reg */
1371 /* start-sanitize-v850e */
1376 trace_input ("callt", OP_LOAD16, 1);
1381 adr = CTBP + ((OP[3] & 0x3f) << 1);
1383 PC = CTBP + load_mem (adr, 1);
1385 trace_output (OP_LOAD16);
1390 /* end-sanitize-v850e */
1392 int value = SEXT5 (OP[0]);
1394 trace_input ("mov", OP_IMM_REG_MOVE, 0);
1396 State.regs[ OP[1] ] = value;
1398 trace_output (OP_IMM_REG_MOVE);
1404 /* mov imm32, reg1 */
1405 /* movea sign_extend(imm16), reg, reg */
1409 /* start-sanitize-v850e */
1412 trace_input ("mov", OP_IMM_REG, 4);
1414 State.regs[ OP[0] ] = load_mem (PC + 2, 4);
1416 trace_output (OP_IMM_REG);
1421 /* end-sanitize-v850e */
1423 trace_input ("movea", OP_IMM_REG_REG, 0);
1425 State.regs[ OP[1] ] = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
1427 trace_output (OP_IMM_REG_REG);
1433 /* dispose imm5, list12 [, reg1] */
1434 /* movhi imm16, reg, reg */
1438 /* start-sanitize-v850e */
1444 trace_input ("dispose", OP_PUSHPOP1, 0);
1446 SP += (OP[3] & 0x3e) << 1;
1448 /* Load the registers with lower number registers being retrieved from higher addresses. */
1450 if ((OP[3] & (1 << type1_regs[ i ])))
1452 State.regs[ 20 + i ] = load_mem (SP, 4);
1456 if ((OP[3] & 0x1f0000) != 0)
1458 PC = State.regs[ (OP[3] >> 16) & 0x1f];
1462 trace_output (OP_PUSHPOP1);
1465 /* end-sanitize-v850e */
1467 trace_input ("movhi", OP_UIMM_REG_REG, 16);
1469 State.regs[ OP[1] ] = State.regs[ OP[0] ] + (OP[2] << 16);
1471 trace_output (OP_UIMM_REG_REG);
1477 /* sar zero_extend(imm5),reg1 */
1481 unsigned int op0, op1, result, z, s, cy;
1483 trace_input ("sar", OP_IMM_REG, 0);
1485 op1 = State.regs[ OP[1] ];
1486 result = (signed)op1 >> op0;
1488 /* Compute the condition codes. */
1490 s = (result & 0x80000000);
1491 cy = (op1 & (1 << (op0 - 1)));
1493 /* Store the result and condition codes. */
1494 State.regs[ OP[1] ] = result;
1495 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1496 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1497 | (cy ? PSW_CY : 0));
1498 trace_output (OP_IMM_REG);
1503 /* sar reg1, reg2 */
1507 unsigned int op0, op1, result, z, s, cy;
1509 trace_input ("sar", OP_REG_REG, 0);
1511 op0 = State.regs[ OP[0] ] & 0x1f;
1512 op1 = State.regs[ OP[1] ];
1513 result = (signed)op1 >> op0;
1515 /* Compute the condition codes. */
1517 s = (result & 0x80000000);
1518 cy = (op1 & (1 << (op0 - 1)));
1520 /* Store the result and condition codes. */
1521 State.regs[OP[1]] = result;
1522 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1523 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1524 | (cy ? PSW_CY : 0));
1525 trace_output (OP_REG_REG);
1530 /* shl zero_extend(imm5),reg1 */
1534 unsigned int op0, op1, result, z, s, cy;
1536 trace_input ("shl", OP_IMM_REG, 0);
1538 op1 = State.regs[ OP[1] ];
1539 result = op1 << op0;
1541 /* Compute the condition codes. */
1543 s = (result & 0x80000000);
1544 cy = (op1 & (1 << (32 - op0)));
1546 /* Store the result and condition codes. */
1547 State.regs[OP[1]] = result;
1548 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1549 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1550 | (cy ? PSW_CY : 0));
1551 trace_output (OP_IMM_REG);
1556 /* shl reg1, reg2 */
1560 unsigned int op0, op1, result, z, s, cy;
1562 trace_input ("shl", OP_REG_REG, 0);
1563 op0 = State.regs[ OP[0] ] & 0x1f;
1564 op1 = State.regs[ OP[1] ];
1565 result = op1 << op0;
1567 /* Compute the condition codes. */
1569 s = (result & 0x80000000);
1570 cy = (op1 & (1 << (32 - op0)));
1572 /* Store the result and condition codes. */
1573 State.regs[OP[1]] = result;
1574 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1575 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1576 | (cy ? PSW_CY : 0));
1577 trace_output (OP_REG_REG);
1582 /* shr zero_extend(imm5),reg1 */
1586 unsigned int op0, op1, result, z, s, cy;
1588 trace_input ("shr", OP_IMM_REG, 0);
1590 op1 = State.regs[ OP[1] ];
1591 result = op1 >> op0;
1593 /* Compute the condition codes. */
1595 s = (result & 0x80000000);
1596 cy = (op1 & (1 << (op0 - 1)));
1598 /* Store the result and condition codes. */
1599 State.regs[OP[1]] = result;
1600 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1601 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1602 | (cy ? PSW_CY : 0));
1603 trace_output (OP_IMM_REG);
1608 /* shr reg1, reg2 */
1612 unsigned int op0, op1, result, z, s, cy;
1614 trace_input ("shr", OP_REG_REG, 0);
1615 op0 = State.regs[ OP[0] ] & 0x1f;
1616 op1 = State.regs[ OP[1] ];
1617 result = op1 >> op0;
1619 /* Compute the condition codes. */
1621 s = (result & 0x80000000);
1622 cy = (op1 & (1 << (op0 - 1)));
1624 /* Store the result and condition codes. */
1625 State.regs[OP[1]] = result;
1626 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1627 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1628 | (cy ? PSW_CY : 0));
1629 trace_output (OP_REG_REG);
1638 unsigned int op0, op1, result, z, s;
1640 trace_input ("or", OP_REG_REG, 0);
1642 /* Compute the result. */
1643 op0 = State.regs[ OP[0] ];
1644 op1 = State.regs[ OP[1] ];
1647 /* Compute the condition codes. */
1649 s = (result & 0x80000000);
1651 /* Store the result and condition codes. */
1652 State.regs[OP[1]] = result;
1653 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1654 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1655 trace_output (OP_REG_REG);
1660 /* ori zero_extend(imm16), reg, reg */
1664 unsigned int op0, op1, result, z, s;
1666 trace_input ("ori", OP_UIMM_REG_REG, 0);
1668 op1 = State.regs[ OP[0] ];
1671 /* Compute the condition codes. */
1673 s = (result & 0x80000000);
1675 /* Store the result and condition codes. */
1676 State.regs[OP[1]] = result;
1677 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1678 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1679 trace_output (OP_UIMM_REG_REG);
1688 unsigned int op0, op1, result, z, s;
1690 trace_input ("and", OP_REG_REG, 0);
1692 /* Compute the result. */
1693 op0 = State.regs[ OP[0] ];
1694 op1 = State.regs[ OP[1] ];
1697 /* Compute the condition codes. */
1699 s = (result & 0x80000000);
1701 /* Store the result and condition codes. */
1702 State.regs[OP[1]] = result;
1703 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1704 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1705 trace_output (OP_REG_REG);
1710 /* andi zero_extend(imm16), reg, reg */
1714 unsigned int result, z;
1716 trace_input ("andi", OP_UIMM_REG_REG, 0);
1718 result = OP[2] & State.regs[ OP[0] ];
1720 /* Compute the condition codes. */
1723 /* Store the result and condition codes. */
1724 State.regs[ OP[1] ] = result;
1726 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1727 PSW |= (z ? PSW_Z : 0);
1729 trace_output (OP_UIMM_REG_REG);
1738 unsigned int op0, op1, result, z, s;
1740 trace_input ("xor", OP_REG_REG, 0);
1742 /* Compute the result. */
1743 op0 = State.regs[ OP[0] ];
1744 op1 = State.regs[ OP[1] ];
1747 /* Compute the condition codes. */
1749 s = (result & 0x80000000);
1751 /* Store the result and condition codes. */
1752 State.regs[OP[1]] = result;
1753 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1754 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1755 trace_output (OP_REG_REG);
1760 /* xori zero_extend(imm16), reg, reg */
1764 unsigned int op0, op1, result, z, s;
1766 trace_input ("xori", OP_UIMM_REG_REG, 0);
1768 op1 = State.regs[ OP[0] ];
1771 /* Compute the condition codes. */
1773 s = (result & 0x80000000);
1775 /* Store the result and condition codes. */
1776 State.regs[OP[1]] = result;
1777 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1778 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1779 trace_output (OP_UIMM_REG_REG);
1784 /* not reg1, reg2 */
1788 unsigned int op0, result, z, s;
1790 trace_input ("not", OP_REG_REG_MOVE, 0);
1791 /* Compute the result. */
1792 op0 = State.regs[ OP[0] ];
1795 /* Compute the condition codes. */
1797 s = (result & 0x80000000);
1799 /* Store the result and condition codes. */
1800 State.regs[OP[1]] = result;
1801 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1802 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1803 trace_output (OP_REG_REG_MOVE);
1812 unsigned int op0, op1, op2;
1815 trace_input ("set1", OP_BIT, 0);
1816 op0 = State.regs[ OP[0] ];
1818 temp = EXTEND16 (OP[2]);
1820 temp = load_mem (op0 + op2, 1);
1822 if ((temp & (1 << op1)) == 0)
1825 store_mem (op0 + op2, 1, temp);
1826 trace_output (OP_BIT);
1835 unsigned int op0, op1, op2;
1838 trace_input ("not1", OP_BIT, 0);
1839 op0 = State.regs[ OP[0] ];
1841 temp = EXTEND16 (OP[2]);
1843 temp = load_mem (op0 + op2, 1);
1845 if ((temp & (1 << op1)) == 0)
1848 store_mem (op0 + op2, 1, temp);
1849 trace_output (OP_BIT);
1858 unsigned int op0, op1, op2;
1861 trace_input ("clr1", OP_BIT, 0);
1862 op0 = State.regs[ OP[0] ];
1864 temp = EXTEND16 (OP[2]);
1866 temp = load_mem (op0 + op2, 1);
1868 if ((temp & (1 << op1)) == 0)
1870 temp &= ~(1 << op1);
1871 store_mem (op0 + op2, 1, temp);
1872 trace_output (OP_BIT);
1881 unsigned int op0, op1, op2;
1884 trace_input ("tst1", OP_BIT, 0);
1885 op0 = State.regs[ OP[0] ];
1887 temp = EXTEND16 (OP[2]);
1889 temp = load_mem (op0 + op2, 1);
1891 if ((temp & (1 << op1)) == 0)
1893 trace_output (OP_BIT);
1902 trace_input ("di", OP_NONE, 0);
1904 trace_output (OP_NONE);
1913 trace_input ("ei", OP_NONE, 0);
1915 trace_output (OP_NONE);
1924 trace_input ("halt", OP_NONE, 0);
1925 /* FIXME this should put processor into a mode where NMI still handled */
1926 trace_output (OP_NONE);
1927 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1928 sim_stopped, SIGTRAP);
1936 trace_input ("reti", OP_NONE, 0);
1937 trace_output (OP_NONE);
1939 /* Restore for NMI if only NP on, otherwise is interrupt or exception. */
1940 if ((PSW & (PSW_NP | PSW_EP)) == PSW_NP)
1958 trace_input ("trap", OP_TRAP, 0);
1959 trace_output (OP_TRAP);
1961 /* Trap 31 is used for simulating OS I/O functions */
1965 int save_errno = errno;
1968 /* Registers passed to trap 0 */
1970 #define FUNC State.regs[6] /* function number, return value */
1971 #define PARM1 State.regs[7] /* optional parm 1 */
1972 #define PARM2 State.regs[8] /* optional parm 2 */
1973 #define PARM3 State.regs[9] /* optional parm 3 */
1975 /* Registers set by trap 0 */
1977 #define RETVAL State.regs[10] /* return value */
1978 #define RETERR State.regs[11] /* return error code */
1980 /* Turn a pointer in a register into a pointer into real memory. */
1982 #define MEMPTR(x) (map (x))
1999 char *path = fetch_str (simulator, PARM1);
2000 char **argv = fetch_argv (simulator, PARM2);
2001 char **envp = fetch_argv (simulator, PARM3);
2002 RETVAL = execve (path, argv, envp);
2015 char *path = fetch_str (simulator, PARM1);
2016 char **argv = fetch_argv (simulator, PARM2);
2017 RETVAL = execv (path, argv);
2033 RETVAL = pipe (host_fd);
2034 SW (buf, host_fd[0]);
2035 buf += sizeof(uint16);
2036 SW (buf, host_fd[1]);
2048 RETVAL = wait (&status);
2058 char *buf = zalloc (PARM3);
2059 RETVAL = sim_io_read (simulator, PARM1, buf, PARM3);
2060 sim_write (simulator, PARM2, buf, PARM3);
2069 char *buf = zalloc (PARM3);
2070 sim_read (simulator, PARM2, buf, PARM3);
2072 RETVAL = sim_io_write_stdout (simulator, buf, PARM3);
2074 RETVAL = sim_io_write (simulator, PARM1, buf, PARM3);
2082 RETVAL = sim_io_lseek (simulator, PARM1, PARM2, PARM3);
2088 RETVAL = sim_io_close (simulator, PARM1);
2095 char *buf = fetch_str (simulator, PARM1);
2096 RETVAL = sim_io_open (simulator, buf, PARM2);
2104 if ((PARM1 & 0xffff0000) == 0xdead0000 && (PARM1 & 0xffff) != 0)
2105 /* get signal encoded by kill */
2106 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
2107 sim_signalled, PARM1 & 0xffff);
2108 else if (PARM1 == 0xdead)
2110 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
2111 sim_exited, SIGABRT);
2113 /* PARM1 has exit status */
2114 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
2119 #if !defined(__GO32__) && !defined(_WIN32)
2121 case SYS_stat: /* added at hmsi */
2122 /* stat system call */
2124 struct stat host_stat;
2126 char *path = fetch_str (simulator, PARM1);
2128 RETVAL = stat (path, &host_stat);
2133 /* Just wild-assed guesses. */
2134 store_mem (buf, 2, host_stat.st_dev);
2135 store_mem (buf + 2, 2, host_stat.st_ino);
2136 store_mem (buf + 4, 4, host_stat.st_mode);
2137 store_mem (buf + 8, 2, host_stat.st_nlink);
2138 store_mem (buf + 10, 2, host_stat.st_uid);
2139 store_mem (buf + 12, 2, host_stat.st_gid);
2140 store_mem (buf + 14, 2, host_stat.st_rdev);
2141 store_mem (buf + 16, 4, host_stat.st_size);
2142 store_mem (buf + 20, 4, host_stat.st_atime);
2143 store_mem (buf + 28, 4, host_stat.st_mtime);
2144 store_mem (buf + 36, 4, host_stat.st_ctime);
2154 char *path = fetch_str (simulator, PARM1);
2155 RETVAL = chown (path, PARM2, PARM3);
2166 char *path = fetch_str (simulator, PARM1);
2167 RETVAL = chmod (path, PARM2);
2179 RETVAL = time (&now);
2180 store_mem (PARM1, 4, now);
2186 #if !defined(__GO32__) && !defined(_WIN32)
2191 RETVAL = times (&tms);
2192 store_mem (PARM1, 4, tms.tms_utime);
2193 store_mem (PARM1 + 4, 4, tms.tms_stime);
2194 store_mem (PARM1 + 8, 4, tms.tms_cutime);
2195 store_mem (PARM1 + 12, 4, tms.tms_cstime);
2201 #ifdef SYS_gettimeofday
2202 #if !defined(__GO32__) && !defined(_WIN32)
2203 case SYS_gettimeofday:
2207 RETVAL = gettimeofday (&t, &tz);
2208 store_mem (PARM1, 4, t.tv_sec);
2209 store_mem (PARM1 + 4, 4, t.tv_usec);
2210 store_mem (PARM2, 4, tz.tz_minuteswest);
2211 store_mem (PARM2 + 4, 4, tz.tz_dsttime);
2221 /* Cast the second argument to void *, to avoid type mismatch
2222 if a prototype is present. */
2223 sim_io_error (simulator, "Utime not supported");
2224 /* RETVAL = utime (path, (void *) MEMPTR (PARM2)); */
2239 { /* Trap 0 -> 30 */
2244 ECR |= 0x40 + OP[0];
2245 /* Flag that we are now doing exception processing. */
2246 PSW |= PSW_EP | PSW_ID;
2247 PC = ((OP[0] < 0x10) ? 0x40 : 0x50) - 4;
2253 /* start-sanitize-v850e */
2254 /* tst1 reg2, [reg1] */
2260 trace_input ("tst1", OP_BIT, 1);
2262 temp = load_mem (State.regs[ OP[0] ], 1);
2265 if ((temp & (1 << State.regs[ OP[1] & 0x7 ])) == 0)
2268 trace_output (OP_BIT);
2273 /* end-sanitize-v850e */
2274 /* start-sanitize-v850e */
2275 /* mulu reg1, reg2, reg3 */
2279 trace_input ("mulu", OP_REG_REG_REG, 0);
2281 Multiply64 (false, State.regs[ OP[0] ]);
2283 trace_output (OP_REG_REG_REG);
2288 /* end-sanitize-v850e */
2289 /* start-sanitize-v850e */
2291 #define BIT_CHANGE_OP( name, binop ) \
2293 unsigned int temp; \
2295 trace_input (name, OP_BIT_CHANGE, 0); \
2297 bit = 1 << State.regs[ OP[1] & 0x7 ]; \
2298 temp = load_mem (State.regs[ OP[0] ], 1); \
2301 if ((temp & bit) == 0) \
2305 store_mem (State.regs[ OP[0] ], 1, temp); \
2307 trace_output (OP_BIT_CHANGE); \
2311 /* clr1 reg2, [reg1] */
2315 BIT_CHANGE_OP ("clr1", &= ~ );
2318 /* not1 reg2, [reg1] */
2322 BIT_CHANGE_OP ("not1", ^= );
2329 BIT_CHANGE_OP ("set1", |= );
2336 trace_input ("sasf", OP_EX1, 0);
2338 State.regs[ OP[1] ] = (State.regs[ OP[1] ] << 1) | condition_met (OP[0]);
2340 trace_output (OP_EX1);
2344 /* end-sanitize-v850e */
2345 /* start-sanitize-v850eq */
2346 /* This function is courtesy of Sugimoto at NEC, via Seow Tan (Soew_Tan@el.nec.com) */
2351 unsigned long int als,
2352 unsigned long int sfi,
2353 unsigned long int * quotient_ptr,
2354 unsigned long int * remainder_ptr,
2355 boolean * overflow_ptr
2358 unsigned long ald = sfi >> (N - 1);
2359 unsigned long alo = als;
2364 unsigned int R1 = 1;
2365 unsigned int DBZ = (als == 0) ? 1 : 0;
2366 unsigned long alt = Q ? ~als : als;
2369 alo = ald + alt + Q;
2370 C = (((alt >> 31) & (ald >> 31))
2371 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2374 R1 = (alo == 0) ? 0 : (R1 & Q);
2375 if ((S ^ (alo>>31)) && !C)
2380 sfi = (sfi << (32-N+1)) | Q;
2381 ald = (alo << 1) | (sfi >> 31);
2383 /* 2nd - N-1th Loop */
2384 for (i = 2; i < N; i++)
2386 alt = Q ? ~als : als;
2387 alo = ald + alt + Q;
2388 C = (((alt >> 31) & (ald >> 31))
2389 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2392 R1 = (alo == 0) ? 0 : (R1 & Q);
2393 if ((S ^ (alo>>31)) && !C && !DBZ)
2398 sfi = (sfi << 1) | Q;
2399 ald = (alo << 1) | (sfi >> 31);
2403 alt = Q ? ~als : als;
2404 alo = ald + alt + Q;
2405 C = (((alt >> 31) & (ald >> 31))
2406 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2409 R1 = (alo == 0) ? 0 : (R1 & Q);
2410 if ((S ^ (alo>>31)) && !C)
2415 * quotient_ptr = (sfi << 1) | Q;
2416 * remainder_ptr = Q ? alo : (alo + als);
2417 * overflow_ptr = DBZ | R1;
2420 /* This function is courtesy of Sugimoto at NEC, via Seow Tan (Soew_Tan@el.nec.com) */
2425 unsigned long int als,
2426 unsigned long int sfi,
2427 signed long int * quotient_ptr,
2428 signed long int * remainder_ptr,
2429 boolean * overflow_ptr
2432 unsigned long ald = (signed long) sfi >> (N - 1);
2433 unsigned long alo = als;
2434 unsigned int SS = als >> 31;
2435 unsigned int SD = sfi >> 31;
2436 unsigned int R1 = 1;
2438 unsigned int DBZ = als == 0 ? 1 : 0;
2439 unsigned int Q = ~(SS ^ SD) & 1;
2443 unsigned long alt = Q ? ~als : als;
2448 alo = ald + alt + Q;
2449 C = (((alt >> 31) & (ald >> 31))
2450 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2452 R1 = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2454 sfi = (sfi << (32-N+1)) | Q;
2455 ald = (alo << 1) | (sfi >> 31);
2456 if ((alo >> 31) ^ (ald >> 31))
2461 /* 2nd - N-1th Loop */
2463 for (i = 2; i < N; i++)
2465 alt = Q ? ~als : als;
2466 alo = ald + alt + Q;
2467 C = (((alt >> 31) & (ald >> 31))
2468 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2470 R1 = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2472 sfi = (sfi << 1) | Q;
2473 ald = (alo << 1) | (sfi >> 31);
2474 if ((alo >> 31) ^ (ald >> 31))
2481 alt = Q ? ~als : als;
2482 alo = ald + alt + Q;
2483 C = (((alt >> 31) & (ald >> 31))
2484 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2486 R1 = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2487 sfi = (sfi << (32-N+1));
2493 alt = Q ? ~als : als;
2494 alo = ald + alt + Q;
2496 R1 = R1 & ((~alo >> 31) ^ SD);
2497 if ((alo != 0) && ((Q ^ (SS ^ SD)) ^ R1)) alo = ald;
2499 ald = sfi = (long) ((sfi >> 1) | (SS ^ SD) << 31) >> (32-N-1) | Q;
2501 ald = sfi = sfi | Q;
2503 OV = DBZ | ((alo == 0) ? 0 : R1);
2505 * remainder_ptr = alo;
2508 if (((alo != 0) && ((SS ^ SD) ^ R1))
2509 || ((alo == 0) && (SS ^ R1)))
2514 OV = (DBZ | R1) ? OV : ((alo >> 31) & (~ald >> 31));
2516 * quotient_ptr = alo;
2517 * overflow_ptr = OV;
2520 /* sdivun imm5, reg1, reg2, reg3 */
2524 unsigned long int quotient;
2525 unsigned long int remainder;
2526 unsigned long int divide_by;
2527 unsigned long int divide_this;
2528 boolean overflow = false;
2531 trace_input ("sdivun", OP_IMM_REG_REG_REG, 0);
2533 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2535 divide_by = State.regs[ OP[0] ];
2536 divide_this = State.regs[ OP[1] ] << imm5;
2538 divun (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2540 State.regs[ OP[1] ] = quotient;
2541 State.regs[ OP[2] >> 11 ] = remainder;
2543 /* Set condition codes. */
2544 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2546 if (overflow) PSW |= PSW_OV;
2547 if (quotient == 0) PSW |= PSW_Z;
2548 if (quotient & 0x80000000) PSW |= PSW_S;
2550 trace_output (OP_IMM_REG_REG_REG);
2555 /* sdivn imm5, reg1, reg2, reg3 */
2559 signed long int quotient;
2560 signed long int remainder;
2561 signed long int divide_by;
2562 signed long int divide_this;
2563 boolean overflow = false;
2566 trace_input ("sdivn", OP_IMM_REG_REG_REG, 0);
2568 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2570 divide_by = State.regs[ OP[0] ];
2571 divide_this = State.regs[ OP[1] ] << imm5;
2573 divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2575 State.regs[ OP[1] ] = quotient;
2576 State.regs[ OP[2] >> 11 ] = remainder;
2578 /* Set condition codes. */
2579 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2581 if (overflow) PSW |= PSW_OV;
2582 if (quotient == 0) PSW |= PSW_Z;
2583 if (quotient < 0) PSW |= PSW_S;
2585 trace_output (OP_IMM_REG_REG_REG);
2590 /* sdivhun imm5, reg1, reg2, reg3 */
2594 unsigned long int quotient;
2595 unsigned long int remainder;
2596 unsigned long int divide_by;
2597 unsigned long int divide_this;
2598 boolean overflow = false;
2601 trace_input ("sdivhun", OP_IMM_REG_REG_REG, 0);
2603 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2605 divide_by = State.regs[ OP[0] ] & 0xffff;
2606 divide_this = State.regs[ OP[1] ] << imm5;
2608 divun (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2610 State.regs[ OP[1] ] = quotient;
2611 State.regs[ OP[2] >> 11 ] = remainder;
2613 /* Set condition codes. */
2614 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2616 if (overflow) PSW |= PSW_OV;
2617 if (quotient == 0) PSW |= PSW_Z;
2618 if (quotient & 0x80000000) PSW |= PSW_S;
2620 trace_output (OP_IMM_REG_REG_REG);
2625 /* sdivhn imm5, reg1, reg2, reg3 */
2629 signed long int quotient;
2630 signed long int remainder;
2631 signed long int divide_by;
2632 signed long int divide_this;
2633 boolean overflow = false;
2636 trace_input ("sdivhn", OP_IMM_REG_REG_REG, 0);
2638 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2640 divide_by = EXTEND16 (State.regs[ OP[0] ]);
2641 divide_this = State.regs[ OP[1] ] << imm5;
2643 divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2645 State.regs[ OP[1] ] = quotient;
2646 State.regs[ OP[2] >> 11 ] = remainder;
2648 /* Set condition codes. */
2649 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2651 if (overflow) PSW |= PSW_OV;
2652 if (quotient == 0) PSW |= PSW_Z;
2653 if (quotient < 0) PSW |= PSW_S;
2655 trace_output (OP_IMM_REG_REG_REG);
2660 /* end-sanitize-v850eq */
2661 /* start-sanitize-v850e */
2662 /* divu reg1, reg2, reg3 */
2666 unsigned long int quotient;
2667 unsigned long int remainder;
2668 unsigned long int divide_by;
2669 unsigned long int divide_this;
2670 boolean overflow = false;
2672 trace_input ("divu", OP_REG_REG_REG, 0);
2674 /* Compute the result. */
2676 divide_by = State.regs[ OP[0] ];
2677 divide_this = State.regs[ OP[1] ];
2685 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2686 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2688 /* Set condition codes. */
2689 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2691 if (overflow) PSW |= PSW_OV;
2692 if (quotient == 0) PSW |= PSW_Z;
2693 if (quotient & 0x80000000) PSW |= PSW_S;
2695 trace_output (OP_REG_REG_REG);
2700 /* end-sanitize-v850e */
2701 /* start-sanitize-v850e */
2702 /* div reg1, reg2, reg3 */
2706 signed long int quotient;
2707 signed long int remainder;
2708 signed long int divide_by;
2709 signed long int divide_this;
2710 boolean overflow = false;
2712 trace_input ("div", OP_REG_REG_REG, 0);
2714 /* Compute the result. */
2716 divide_by = State.regs[ OP[0] ];
2717 divide_this = State.regs[ OP[1] ];
2719 if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
2725 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2726 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2728 /* Set condition codes. */
2729 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2731 if (overflow) PSW |= PSW_OV;
2732 if (quotient == 0) PSW |= PSW_Z;
2733 if (quotient < 0) PSW |= PSW_S;
2735 trace_output (OP_REG_REG_REG);
2740 /* end-sanitize-v850e */
2741 /* start-sanitize-v850e */
2742 /* divhu reg1, reg2, reg3 */
2746 unsigned long int quotient;
2747 unsigned long int remainder;
2748 unsigned long int divide_by;
2749 unsigned long int divide_this;
2750 boolean overflow = false;
2752 trace_input ("divhu", OP_REG_REG_REG, 0);
2754 /* Compute the result. */
2756 divide_by = State.regs[ OP[0] ] & 0xffff;
2757 divide_this = State.regs[ OP[1] ];
2765 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2766 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2768 /* Set condition codes. */
2769 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2771 if (overflow) PSW |= PSW_OV;
2772 if (quotient == 0) PSW |= PSW_Z;
2773 if (quotient & 0x80000000) PSW |= PSW_S;
2775 trace_output (OP_REG_REG_REG);
2780 /* end-sanitize-v850e */
2781 /* start-sanitize-v850e */
2782 /* divh reg1, reg2, reg3 */
2786 signed long int quotient;
2787 signed long int remainder;
2788 signed long int divide_by;
2789 signed long int divide_this;
2790 boolean overflow = false;
2792 trace_input ("divh", OP_REG_REG_REG, 0);
2794 /* Compute the result. */
2796 divide_by = State.regs[ OP[0] ];
2797 divide_this = EXTEND16 (State.regs[ OP[1] ]);
2799 if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
2805 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2806 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2808 /* Set condition codes. */
2809 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2811 if (overflow) PSW |= PSW_OV;
2812 if (quotient == 0) PSW |= PSW_Z;
2813 if (quotient < 0) PSW |= PSW_S;
2815 trace_output (OP_REG_REG_REG);
2820 /* end-sanitize-v850e */
2821 /* start-sanitize-v850e */
2822 /* mulu imm9, reg2, reg3 */
2826 trace_input ("mulu", OP_IMM_REG_REG, 0);
2828 Multiply64 (false, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
2830 trace_output (OP_IMM_REG_REG);
2835 /* end-sanitize-v850e */
2836 /* start-sanitize-v850e */
2837 /* mul imm9, reg2, reg3 */
2841 trace_input ("mul", OP_IMM_REG_REG, 0);
2843 Multiply64 (true, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
2845 trace_output (OP_IMM_REG_REG);
2850 /* end-sanitize-v850e */
2851 /* start-sanitize-v850e */
2852 /* cmov imm5, reg2, reg3 */
2856 trace_input ("cmov", OP_IMM_REG_REG, 0);
2858 State.regs[ OP[2] >> 11 ] = condition_met (OP[0]) ? SEXT5( OP[0] ) : State.regs[ OP[1] ];
2860 trace_output (OP_IMM_REG_REG);
2866 /* end-sanitize-v850e */
2867 /* start-sanitize-v850e */
2872 trace_input ("ctret", OP_NONE, 0);
2877 trace_output (OP_NONE);
2882 /* end-sanitize-v850e */
2883 /* start-sanitize-v850e */
2888 unsigned long value;
2890 trace_input ("hsw", OP_REG_REG3, 0);
2892 value = State.regs[ OP[ 1 ] ];
2894 value |= (State.regs[ OP[ 1 ] ] << 16);
2896 State.regs[ OP[2] >> 11 ] = value;
2898 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
2900 if (value == 0) PSW |= PSW_Z;
2901 if (value & 0x80000000) PSW |= PSW_S;
2902 if (((value & 0xffff) == 0) || (value & 0xffff0000) == 0) PSW |= PSW_CY;
2904 trace_output (OP_REG_REG3);
2909 /* end-sanitize-v850e */
2910 /* start-sanitize-v850e */
2911 #define WORDHASNULLBYTE(x) (((x) - 0x01010101) & ~(x)&0x80808080)
2917 unsigned long value;
2919 trace_input ("bsw", OP_REG_REG3, 0);
2921 value = State.regs[ OP[ 1 ] ];
2923 value |= (State.regs[ OP[ 1 ] ] << 24);
2924 value |= ((State.regs[ OP[ 1 ] ] << 8) & 0x00ff0000);
2925 value |= ((State.regs[ OP[ 1 ] ] >> 8) & 0x0000ff00);
2927 State.regs[ OP[2] >> 11 ] = value;
2929 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
2931 if (value == 0) PSW |= PSW_Z;
2932 if (value & 0x80000000) PSW |= PSW_S;
2933 if (WORDHASNULLBYTE (value)) PSW |= PSW_CY;
2935 trace_output (OP_REG_REG3);
2940 /* end-sanitize-v850e */
2941 /* start-sanitize-v850e */
2946 unsigned long value;
2948 trace_input ("bsh", OP_REG_REG3, 0);
2950 value = State.regs[ OP[ 1 ] ];
2952 value |= ((State.regs[ OP[ 1 ] ] << 8) & 0xff00ff00);
2953 value |= ((State.regs[ OP[ 1 ] ] >> 8) & 0x000000ff);
2955 State.regs[ OP[2] >> 11 ] = value;
2957 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
2959 if (value == 0) PSW |= PSW_Z;
2960 if (value & 0x80000000) PSW |= PSW_S;
2961 if (((value & 0xff) == 0) || (value & 0x00ff) == 0) PSW |= PSW_CY;
2963 trace_output (OP_REG_REG3);
2968 /* end-sanitize-v850e */
2969 /* start-sanitize-v850e */
2976 trace_input ("ld.hu", OP_LOAD32, 2);
2978 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
2981 State.regs[ OP[1] ] = load_mem (adr, 2);
2983 trace_output (OP_LOAD32);
2988 /* end-sanitize-v850e */
2989 /* start-sanitize-v850e */
2996 trace_input ("ld.bu", OP_LOAD32, 1);
2998 adr = (State.regs[ OP[0] ]
2999 + (EXTEND16 (OP[2] & ~1) | ((OP[3] >> 5) & 1)));
3001 State.regs[ OP[1] ] = load_mem (adr, 1);
3003 trace_output (OP_LOAD32);
3008 /* prepare list12, imm5, imm32 */
3014 trace_input ("prepare", OP_PUSHPOP1, 0);
3016 /* Store the registers with lower number registers being placed at higher addresses. */
3017 for (i = 0; i < 12; i++)
3018 if ((OP[3] & (1 << type1_regs[ i ])))
3021 store_mem (SP, 4, State.regs[ 20 + i ]);
3024 SP -= (OP[3] & 0x3e) << 1;
3026 EP = load_mem (PC + 4, 4);
3028 trace_output (OP_PUSHPOP1);
3033 /* prepare list12, imm5, imm16-32 */
3039 trace_input ("prepare", OP_PUSHPOP1, 0);
3041 /* Store the registers with lower number registers being placed at higher addresses. */
3042 for (i = 0; i < 12; i++)
3043 if ((OP[3] & (1 << type1_regs[ i ])))
3046 store_mem (SP, 4, State.regs[ 20 + i ]);
3049 SP -= (OP[3] & 0x3e) << 1;
3051 EP = load_mem (PC + 4, 2) << 16;
3053 trace_output (OP_PUSHPOP1);
3058 /* prepare list12, imm5, imm16 */
3064 trace_input ("prepare", OP_PUSHPOP1, 0);
3066 /* Store the registers with lower number registers being placed at higher addresses. */
3067 for (i = 0; i < 12; i++)
3068 if ((OP[3] & (1 << type1_regs[ i ])))
3071 store_mem (SP, 4, State.regs[ 20 + i ]);
3074 SP -= (OP[3] & 0x3e) << 1;
3076 EP = EXTEND16 (load_mem (PC + 4, 2));
3078 trace_output (OP_PUSHPOP1);
3083 /* prepare list12, imm5, sp */
3089 trace_input ("prepare", OP_PUSHPOP1, 0);
3091 /* Store the registers with lower number registers being placed at higher addresses. */
3092 for (i = 0; i < 12; i++)
3093 if ((OP[3] & (1 << type1_regs[ i ])))
3096 store_mem (SP, 4, State.regs[ 20 + i ]);
3099 SP -= (OP[3] & 0x3e) << 1;
3103 trace_output (OP_PUSHPOP1);
3108 /* end-sanitize-v850e */
3109 /* start-sanitize-v850e */
3114 unsigned long result;
3116 result = load_mem (State.regs[30] + ((OP[3] & 0xf) << 1), 2);
3118 /* start-sanitize-v850eq */
3121 trace_input ("sld.h", OP_LOAD16, 2);
3122 State.regs[ OP[1] ] = EXTEND16 (result);
3126 /* end-sanitize-v850eq */
3127 trace_input ("sld.hu", OP_LOAD16, 2);
3129 State.regs[ OP[1] ] = result;
3130 /* start-sanitize-v850eq */
3132 /* end-sanitize-v850eq */
3134 trace_output (OP_LOAD16);
3139 /* end-sanitize-v850e */
3140 /* start-sanitize-v850e */
3141 /* cmov reg1, reg2, reg3 */
3145 trace_input ("cmov", OP_REG_REG_REG, 0);
3147 State.regs[ OP[2] >> 11 ] = condition_met (OP[0]) ? State.regs[ OP[0] ] : State.regs[ OP[1] ];
3149 trace_output (OP_REG_REG_REG);
3154 /* end-sanitize-v850e */
3155 /* start-sanitize-v850e */
3156 /* mul reg1, reg2, reg3 */
3160 trace_input ("mul", OP_REG_REG_REG, 0);
3162 Multiply64 (true, State.regs[ OP[0] ]);
3164 trace_output (OP_REG_REG_REG);
3169 /* end-sanitize-v850e */
3170 /* start-sanitize-v850eq */
3178 trace_input ("popmh", OP_PUSHPOP2, 0);
3180 if (OP[3] & (1 << 19))
3182 if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
3184 FEPSW = load_mem ( SP & ~ 3, 4);
3185 FEPC = load_mem ((SP + 4) & ~ 3, 4);
3189 EIPSW = load_mem ( SP & ~ 3, 4);
3190 EIPC = load_mem ((SP + 4) & ~ 3, 4);
3196 /* Load the registers with lower number registers being retrieved from higher addresses. */
3198 if ((OP[3] & (1 << type2_regs[ i ])))
3200 State.regs[ i + 16 ] = load_mem (SP & ~ 3, 4);
3204 trace_output (OP_PUSHPOP2);
3215 trace_input ("popml", OP_PUSHPOP3, 0);
3217 if (OP[3] & (1 << 19))
3219 if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
3221 FEPSW = load_mem ( SP & ~ 3, 4);
3222 FEPC = load_mem ((SP + 4) & ~ 3, 4);
3226 EIPSW = load_mem ( SP & ~ 3, 4);
3227 EIPC = load_mem ((SP + 4) & ~ 3, 4);
3233 if (OP[3] & (1 << 3))
3235 PSW = load_mem (SP & ~ 3, 4);
3239 /* Load the registers with lower number registers being retrieved from higher addresses. */
3241 if ((OP[3] & (1 << type3_regs[ i ])))
3243 State.regs[ i + 1 ] = load_mem (SP & ~ 3, 4);
3247 trace_output (OP_PUSHPOP2);
3258 trace_input ("pushmh", OP_PUSHPOP2, 0);
3260 /* Store the registers with lower number registers being placed at higher addresses. */
3261 for (i = 0; i < 16; i++)
3262 if ((OP[3] & (1 << type2_regs[ i ])))
3265 store_mem (SP & ~ 3, 4, State.regs[ i + 16 ]);
3268 if (OP[3] & (1 << 19))
3272 if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
3274 store_mem ((SP + 4) & ~ 3, 4, FEPC);
3275 store_mem ( SP & ~ 3, 4, FEPSW);
3279 store_mem ((SP + 4) & ~ 3, 4, EIPC);
3280 store_mem ( SP & ~ 3, 4, EIPSW);
3284 trace_output (OP_PUSHPOP2);
3289 /* end-sanitize-v850eq */