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 unsigned32 trace_values[3];
70 const char *trace_name;
71 const char *trace_module;
75 trace_input (name, type, size)
81 if (!TRACE_ALU_P (STATE_CPU (simulator, 0)))
99 trace_values[0] = State.regs[OP[0]];
100 trace_num_values = 1;
103 /* start-sanitize-v850e */
105 /* end-sanitize-v850e */
108 trace_values[0] = State.regs[OP[1]];
109 trace_values[1] = State.regs[OP[0]];
110 trace_num_values = 2;
115 trace_values[0] = SEXT5 (OP[0]);
116 trace_values[1] = OP[1];
117 trace_num_values = 2;
120 case OP_IMM_REG_MOVE:
121 trace_values[0] = SEXT5 (OP[0]);
122 trace_num_values = 1;
126 trace_values[0] = State.pc;
127 trace_values[1] = SEXT9 (OP[0]);
128 trace_values[2] = PSW;
129 trace_num_values = 3;
133 trace_values[0] = OP[1] * size;
134 trace_values[1] = State.regs[30];
135 trace_num_values = 2;
139 trace_values[0] = State.regs[OP[0]];
140 trace_values[1] = OP[1] * size;
141 trace_values[2] = State.regs[30];
142 trace_num_values = 3;
146 trace_values[0] = EXTEND16 (OP[2]);
147 trace_values[1] = State.regs[OP[0]];
148 trace_num_values = 2;
152 trace_values[0] = State.regs[OP[1]];
153 trace_values[1] = EXTEND16 (OP[2]);
154 trace_values[2] = State.regs[OP[0]];
155 trace_num_values = 3;
159 trace_values[0] = SEXT22 (OP[0]);
160 trace_values[1] = State.pc;
161 trace_num_values = 2;
165 trace_values[0] = EXTEND16 (OP[0]) << size;
166 trace_values[1] = State.regs[OP[1]];
167 trace_num_values = 2;
170 case OP_IMM16_REG_REG:
171 trace_values[0] = EXTEND16 (OP[2]) << size;
172 trace_values[1] = State.regs[OP[1]];
173 trace_num_values = 2;
176 case OP_UIMM_REG_REG:
177 trace_values[0] = (OP[0] & 0xffff) << size;
178 trace_values[1] = State.regs[OP[1]];
179 trace_num_values = 2;
182 case OP_UIMM16_REG_REG:
183 trace_values[0] = (OP[2]) << size;
184 trace_values[1] = State.regs[OP[1]];
185 trace_num_values = 2;
189 trace_num_values = 0;
193 trace_values[0] = PSW;
194 trace_num_values = 1;
198 trace_num_values = 0;
202 trace_values[0] = State.regs[OP[0]];
203 trace_num_values = 1;
207 trace_values[0] = State.sregs[OP[1]];
208 trace_num_values = 1;
214 trace_result (int has_result, unsigned32 result)
222 /* write out the values saved during the trace_input call */
225 for (i = 0; i < trace_num_values; i++)
227 sprintf (chp, "%*s0x%.8lx", SIZE_VALUES - 10, "", trace_values[i]);
228 chp = strchr (chp, '\0');
232 sprintf (chp, "%*s", SIZE_VALUES, "");
233 chp = strchr (chp, '\0');
237 /* append any result to the end of the buffer */
239 sprintf (chp, " :: 0x%.8lx", (unsigned long)result);
241 trace_one_insn (simulator, STATE_CPU (simulator, 0), trace_pc,
242 TRACE_LINENUM_P (STATE_CPU (simulator, 0)),
243 "simops", __LINE__, trace_module,
244 "%-*s -%s", SIZE_INSTRUCTION, trace_name, buf);
248 trace_output (result)
249 enum op_types result;
251 if (!TRACE_ALU_P (STATE_CPU (simulator, 0)))
273 trace_result (1, State.regs[OP[0]]);
277 case OP_REG_REG_MOVE:
279 case OP_IMM_REG_MOVE:
282 trace_result (1, State.regs[OP[1]]);
286 case OP_UIMM_REG_REG:
287 case OP_IMM16_REG_REG:
288 case OP_UIMM16_REG_REG:
289 trace_result (1, State.regs[OP[1]]);
294 trace_result (1, State.regs[OP[1]]);
300 trace_result (1, State.sregs[OP[1]]);
307 /* Returns 1 if the specific condition is met, returns 0 otherwise. */
309 condition_met (unsigned code)
311 unsigned int psw = PSW;
315 case 0x0: return ((psw & PSW_OV) != 0);
316 case 0x1: return ((psw & PSW_CY) != 0);
317 case 0x2: return ((psw & PSW_Z) != 0);
318 case 0x3: return ((((psw & PSW_CY) != 0) | ((psw & PSW_Z) != 0)) != 0);
319 case 0x4: return ((psw & PSW_S) != 0);
320 /*case 0x5: return 1;*/
321 case 0x6: return ((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) != 0);
322 case 0x7: return (((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) || ((psw & PSW_Z) != 0)) != 0);
323 case 0x8: return ((psw & PSW_OV) == 0);
324 case 0x9: return ((psw & PSW_CY) == 0);
325 case 0xa: return ((psw & PSW_Z) == 0);
326 case 0xb: return ((((psw & PSW_CY) != 0) | ((psw & PSW_Z) != 0)) == 0);
327 case 0xc: return ((psw & PSW_S) == 0);
328 case 0xd: return ((psw & PSW_SAT) != 0);
329 case 0xe: return ((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) == 0);
330 case 0xf: return (((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) || ((psw & PSW_Z) != 0)) == 0);
335 /* start-sanitize-v850e */
338 Add32 (unsigned long a1, unsigned long a2, int * carry)
340 unsigned long result = (a1 + a2);
342 * carry = (result < a1);
348 Multiply64 (boolean sign, unsigned long op0)
359 op1 = State.regs[ OP[1] ];
363 /* Compute sign of result and adjust operands if necessary. */
365 sign = (op0 ^ op1) & 0x80000000;
367 if (((signed long) op0) < 0)
370 if (((signed long) op1) < 0)
374 /* We can split the 32x32 into four 16x16 operations. This ensures
375 that we do not lose precision on 32bit only hosts: */
376 lo = ( (op0 & 0xFFFF) * (op1 & 0xFFFF));
377 mid1 = ( (op0 & 0xFFFF) * ((op1 >> 16) & 0xFFFF));
378 mid2 = (((op0 >> 16) & 0xFFFF) * (op1 & 0xFFFF));
379 hi = (((op0 >> 16) & 0xFFFF) * ((op1 >> 16) & 0xFFFF));
381 /* We now need to add all of these results together, taking care
382 to propogate the carries from the additions: */
383 RdLo = Add32 (lo, (mid1 << 16), & carry);
385 RdLo = Add32 (RdLo, (mid2 << 16), & carry);
386 RdHi += (carry + ((mid1 >> 16) & 0xFFFF) + ((mid2 >> 16) & 0xFFFF) + hi);
390 /* Negate result if necessary. */
394 if (RdLo == 0xFFFFFFFF)
403 /* Don't store into register 0. */
405 State.regs[ OP[1] ] = RdLo;
407 State.regs[ OP[2] >> 11 ] = RdHi;
412 /* end-sanitize-v850e */
414 /* Read a null terminated string from memory, return in a buffer */
422 while (sim_core_read_1 (STATE_CPU (sd, 0),
423 PC, sim_core_read_map, addr + nr) != 0)
425 buf = NZALLOC (char, nr + 1);
426 sim_read (simulator, addr, buf, nr);
430 /* Read a null terminated argument vector from memory, return in a
433 fetch_argv (sd, addr)
439 char **buf = xmalloc (max_nr * sizeof (char*));
442 unsigned32 a = sim_core_read_4 (STATE_CPU (sd, 0),
443 PC, sim_core_read_map, addr + nr * 4);
445 buf[nr] = fetch_str (sd, a);
447 if (nr == max_nr - 1)
450 buf = xrealloc (buf, max_nr * sizeof (char*));
462 unsigned long result;
464 result = load_mem (State.regs[30] + (OP[3] & 0x7f), 1);
466 /* start-sanitize-v850eq */
469 trace_input ("sld.bu", OP_LOAD16, 1);
470 State.regs[ OP[1] ] = result;
474 /* end-sanitize-v850eq */
475 trace_input ("sld.b", OP_LOAD16, 1);
477 State.regs[ OP[1] ] = EXTEND8 (result);
478 /* start-sanitize-v850eq */
480 /* end-sanitize-v850eq */
482 trace_output (OP_LOAD16);
491 unsigned long result;
493 result = load_mem (State.regs[30] + ((OP[3] & 0x7f) << 1), 2);
495 /* start-sanitize-v850eq */
498 trace_input ("sld.hu", OP_LOAD16, 2);
499 State.regs[ OP[1] ] = result;
503 /* end-sanitize-v850eq */
504 trace_input ("sld.h", OP_LOAD16, 2);
506 State.regs[ OP[1] ] = EXTEND16 (result);
507 /* start-sanitize-v850eq */
509 /* end-sanitize-v850eq */
511 trace_output (OP_LOAD16);
520 trace_input ("sld.w", OP_LOAD16, 4);
522 State.regs[ OP[1] ] = load_mem (State.regs[30] + ((OP[3] & 0x7f) << 1), 4);
524 trace_output (OP_LOAD16);
533 trace_input ("sst.b", OP_STORE16, 1);
535 store_mem (State.regs[30] + (OP[3] & 0x7f), 1, State.regs[ OP[1] ]);
537 trace_output (OP_STORE16);
546 trace_input ("sst.h", OP_STORE16, 2);
548 store_mem (State.regs[30] + ((OP[3] & 0x7f) << 1), 2, State.regs[ OP[1] ]);
550 trace_output (OP_STORE16);
559 trace_input ("sst.w", OP_STORE16, 4);
561 store_mem (State.regs[30] + ((OP[3] & 0x7e) << 1), 4, State.regs[ OP[1] ]);
563 trace_output (OP_STORE16);
574 trace_input ("ld.b", OP_LOAD32, 1);
576 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
578 State.regs[ OP[1] ] = EXTEND8 (load_mem (adr, 1));
580 trace_output (OP_LOAD32);
591 trace_input ("ld.h", OP_LOAD32, 2);
593 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
596 State.regs[ OP[1] ] = EXTEND16 (load_mem (adr, 2));
598 trace_output (OP_LOAD32);
609 trace_input ("ld.w", OP_LOAD32, 4);
611 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
614 State.regs[ OP[1] ] = load_mem (adr, 4);
616 trace_output (OP_LOAD32);
625 trace_input ("st.b", OP_STORE32, 1);
627 store_mem (State.regs[ OP[0] ] + EXTEND16 (OP[2]), 1, State.regs[ OP[1] ]);
629 trace_output (OP_STORE32);
640 trace_input ("st.h", OP_STORE32, 2);
642 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
645 store_mem (adr, 2, State.regs[ OP[1] ]);
647 trace_output (OP_STORE32);
658 trace_input ("st.w", OP_STORE32, 4);
660 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
663 store_mem (adr, 4, State.regs[ OP[1] ]);
665 trace_output (OP_STORE32);
673 trace_input ("Bcond", OP_COND_BR, 0);
674 trace_output (OP_COND_BR);
676 if (condition_met (code))
677 return SEXT9 (((OP[3] & 0x70) >> 3) | ((OP[3] & 0xf800) >> 7));
794 /* jarl/jr disp22, reg */
798 trace_input ("jarl/jr", OP_JUMP, 0);
801 State.regs[ OP[1] ] = PC + 4;
803 trace_output (OP_JUMP);
805 return SEXT22 (((OP[3] & 0x3f) << 16) | OP[2]);
812 unsigned int op0, op1, result, z, s, cy, ov;
814 trace_input ("add", OP_REG_REG, 0);
816 /* Compute the result. */
818 op0 = State.regs[ OP[0] ];
819 op1 = State.regs[ OP[1] ];
823 /* Compute the condition codes. */
825 s = (result & 0x80000000);
826 cy = (result < op0 || result < op1);
827 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
828 && (op0 & 0x80000000) != (result & 0x80000000));
830 /* Store the result and condition codes. */
831 State.regs[OP[1]] = result;
832 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
833 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
834 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
835 trace_output (OP_REG_REG);
840 /* add sign_extend(imm5), reg */
844 unsigned int op0, op1, result, z, s, cy, ov;
847 trace_input ("add", OP_IMM_REG, 0);
849 /* Compute the result. */
850 temp = SEXT5 (OP[0]);
852 op1 = State.regs[OP[1]];
855 /* Compute the condition codes. */
857 s = (result & 0x80000000);
858 cy = (result < op0 || result < op1);
859 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
860 && (op0 & 0x80000000) != (result & 0x80000000));
862 /* Store the result and condition codes. */
863 State.regs[OP[1]] = result;
864 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
865 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
866 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
867 trace_output (OP_IMM_REG);
872 /* addi sign_extend(imm16), reg, reg */
876 unsigned int op0, op1, result, z, s, cy, ov;
878 trace_input ("addi", OP_IMM16_REG_REG, 0);
880 /* Compute the result. */
882 op0 = EXTEND16 (OP[2]);
883 op1 = State.regs[ OP[0] ];
886 /* Compute the condition codes. */
888 s = (result & 0x80000000);
889 cy = (result < op0 || result < op1);
890 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
891 && (op0 & 0x80000000) != (result & 0x80000000));
893 /* Store the result and condition codes. */
894 State.regs[OP[1]] = result;
895 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
896 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
897 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
898 trace_output (OP_IMM16_REG_REG);
907 unsigned int op0, op1, result, z, s, cy, ov;
909 trace_input ("sub", OP_REG_REG, 0);
910 /* Compute the result. */
911 op0 = State.regs[ OP[0] ];
912 op1 = State.regs[ OP[1] ];
915 /* Compute the condition codes. */
917 s = (result & 0x80000000);
919 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
920 && (op1 & 0x80000000) != (result & 0x80000000));
922 /* Store the result and condition codes. */
923 State.regs[OP[1]] = result;
924 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
925 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
926 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
927 trace_output (OP_REG_REG);
932 /* subr reg1, reg2 */
936 unsigned int op0, op1, result, z, s, cy, ov;
938 trace_input ("subr", OP_REG_REG, 0);
939 /* Compute the result. */
940 op0 = State.regs[ OP[0] ];
941 op1 = State.regs[ OP[1] ];
944 /* Compute the condition codes. */
946 s = (result & 0x80000000);
948 ov = ((op0 & 0x80000000) != (op1 & 0x80000000)
949 && (op0 & 0x80000000) != (result & 0x80000000));
951 /* Store the result and condition codes. */
952 State.regs[OP[1]] = result;
953 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
954 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
955 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
956 trace_output (OP_REG_REG);
965 trace_input ("mulh", OP_REG_REG, 0);
967 State.regs[ OP[1] ] = (EXTEND16 (State.regs[ OP[1] ]) * EXTEND16 (State.regs[ OP[0] ]));
969 trace_output (OP_REG_REG);
974 /* mulh sign_extend(imm5), reg2 */
978 trace_input ("mulh", OP_IMM_REG, 0);
980 State.regs[ OP[1] ] = EXTEND16 (State.regs[ OP[1] ]) * SEXT5 (OP[0]);
982 trace_output (OP_IMM_REG);
987 /* mulhi imm16, reg1, reg2 */
991 trace_input ("mulhi", OP_IMM16_REG_REG, 0);
993 State.regs[ OP[1] ] = EXTEND16 (State.regs[ OP[0] ]) * EXTEND16 (OP[2]);
995 trace_output (OP_IMM16_REG_REG);
1000 /* divh reg1, reg2 */
1004 unsigned int op0, op1, result, ov, s, z;
1007 trace_input ("divh", OP_REG_REG, 0);
1009 /* Compute the result. */
1010 temp = EXTEND16 (State.regs[ OP[0] ]);
1012 op1 = State.regs[OP[1]];
1014 if (op0 == 0xffffffff && op1 == 0x80000000)
1016 result = 0x80000000;
1030 /* Compute the condition codes. */
1032 s = (result & 0x80000000);
1034 /* Store the result and condition codes. */
1035 State.regs[OP[1]] = result;
1036 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1037 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1038 | (ov ? PSW_OV : 0));
1039 trace_output (OP_REG_REG);
1048 unsigned int op0, op1, result, z, s, cy, ov;
1050 trace_input ("cmp", OP_REG_REG_CMP, 0);
1051 /* Compute the result. */
1052 op0 = State.regs[ OP[0] ];
1053 op1 = State.regs[ OP[1] ];
1056 /* Compute the condition codes. */
1058 s = (result & 0x80000000);
1060 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1061 && (op1 & 0x80000000) != (result & 0x80000000));
1063 /* Set condition codes. */
1064 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1065 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1066 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
1067 trace_output (OP_REG_REG_CMP);
1072 /* cmp sign_extend(imm5), reg */
1076 unsigned int op0, op1, result, z, s, cy, ov;
1079 /* Compute the result. */
1080 trace_input ("cmp", OP_IMM_REG_CMP, 0);
1081 temp = SEXT5 (OP[0]);
1083 op1 = State.regs[OP[1]];
1086 /* Compute the condition codes. */
1088 s = (result & 0x80000000);
1090 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1091 && (op1 & 0x80000000) != (result & 0x80000000));
1093 /* Set condition codes. */
1094 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1095 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1096 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
1097 trace_output (OP_IMM_REG_CMP);
1102 /* setf cccc,reg2 */
1106 trace_input ("setf", OP_EX1, 0);
1108 State.regs[ OP[1] ] = condition_met (OP[0]);
1110 trace_output (OP_EX1);
1115 /* satadd reg,reg */
1119 unsigned int op0, op1, result, z, s, cy, ov, sat;
1121 trace_input ("satadd", OP_REG_REG, 0);
1122 /* Compute the result. */
1123 op0 = State.regs[ OP[0] ];
1124 op1 = State.regs[ OP[1] ];
1127 /* Compute the condition codes. */
1129 s = (result & 0x80000000);
1130 cy = (result < op0 || result < op1);
1131 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
1132 && (op0 & 0x80000000) != (result & 0x80000000));
1135 /* Store the result and condition codes. */
1136 State.regs[OP[1]] = result;
1137 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1138 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1139 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1140 | (sat ? PSW_SAT : 0));
1142 /* Handle saturated results. */
1144 State.regs[OP[1]] = 0x80000000;
1146 State.regs[OP[1]] = 0x7fffffff;
1147 trace_output (OP_REG_REG);
1152 /* satadd sign_extend(imm5), reg */
1156 unsigned int op0, op1, result, z, s, cy, ov, sat;
1160 trace_input ("satadd", OP_IMM_REG, 0);
1162 /* Compute the result. */
1163 temp = SEXT5 (OP[0]);
1165 op1 = State.regs[OP[1]];
1168 /* Compute the condition codes. */
1170 s = (result & 0x80000000);
1171 cy = (result < op0 || result < op1);
1172 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
1173 && (op0 & 0x80000000) != (result & 0x80000000));
1176 /* Store the result and condition codes. */
1177 State.regs[OP[1]] = result;
1178 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1179 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1180 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1181 | (sat ? PSW_SAT : 0));
1183 /* Handle saturated results. */
1185 State.regs[OP[1]] = 0x80000000;
1187 State.regs[OP[1]] = 0x7fffffff;
1188 trace_output (OP_IMM_REG);
1193 /* satsub reg1, reg2 */
1197 unsigned int op0, op1, result, z, s, cy, ov, sat;
1199 trace_input ("satsub", OP_REG_REG, 0);
1201 /* Compute the result. */
1202 op0 = State.regs[ OP[0] ];
1203 op1 = State.regs[ OP[1] ];
1206 /* Compute the condition codes. */
1208 s = (result & 0x80000000);
1210 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1211 && (op1 & 0x80000000) != (result & 0x80000000));
1214 /* Store the result and condition codes. */
1215 State.regs[OP[1]] = result;
1216 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1217 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1218 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1219 | (sat ? PSW_SAT : 0));
1221 /* Handle saturated results. */
1223 State.regs[OP[1]] = 0x80000000;
1225 State.regs[OP[1]] = 0x7fffffff;
1226 trace_output (OP_REG_REG);
1230 /* satsubi sign_extend(imm16), reg */
1234 unsigned int op0, op1, result, z, s, cy, ov, sat;
1237 trace_input ("satsubi", OP_IMM_REG, 0);
1239 /* Compute the result. */
1240 temp = EXTEND16 (OP[2]);
1242 op1 = State.regs[ OP[0] ];
1245 /* Compute the condition codes. */
1247 s = (result & 0x80000000);
1249 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1250 && (op1 & 0x80000000) != (result & 0x80000000));
1253 /* Store the result and condition codes. */
1254 State.regs[OP[1]] = result;
1255 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1256 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1257 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1258 | (sat ? PSW_SAT : 0));
1260 /* Handle saturated results. */
1262 State.regs[OP[1]] = 0x80000000;
1264 State.regs[OP[1]] = 0x7fffffff;
1265 trace_output (OP_IMM_REG);
1270 /* satsubr reg,reg */
1274 unsigned int op0, op1, result, z, s, cy, ov, sat;
1276 trace_input ("satsubr", OP_REG_REG, 0);
1278 /* Compute the result. */
1279 op0 = State.regs[ OP[0] ];
1280 op1 = State.regs[ OP[1] ];
1283 /* Compute the condition codes. */
1285 s = (result & 0x80000000);
1286 cy = (result < op0);
1287 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1288 && (op1 & 0x80000000) != (result & 0x80000000));
1291 /* Store the result and condition codes. */
1292 State.regs[OP[1]] = result;
1293 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1294 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1295 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1296 | (sat ? PSW_SAT : 0));
1298 /* Handle saturated results. */
1300 State.regs[OP[1]] = 0x80000000;
1302 State.regs[OP[1]] = 0x7fffffff;
1303 trace_output (OP_REG_REG);
1312 unsigned int op0, op1, result, z, s;
1314 trace_input ("tst", OP_REG_REG_CMP, 0);
1316 /* Compute the result. */
1317 op0 = State.regs[ OP[0] ];
1318 op1 = State.regs[ OP[1] ];
1321 /* Compute the condition codes. */
1323 s = (result & 0x80000000);
1325 /* Store the condition codes. */
1326 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1327 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1328 trace_output (OP_REG_REG_CMP);
1333 /* mov sign_extend(imm5), reg */
1337 int value = SEXT5 (OP[0]);
1339 trace_input ("mov", OP_IMM_REG_MOVE, 0);
1341 State.regs[ OP[1] ] = value;
1343 trace_output (OP_IMM_REG_MOVE);
1348 /* movhi imm16, reg, reg */
1352 trace_input ("movhi", OP_UIMM16_REG_REG, 16);
1354 State.regs[ OP[1] ] = State.regs[ OP[0] ] + (OP[2] << 16);
1356 trace_output (OP_UIMM16_REG_REG);
1361 /* sar zero_extend(imm5),reg1 */
1365 unsigned int op0, op1, result, z, s, cy;
1367 trace_input ("sar", OP_IMM_REG, 0);
1369 op1 = State.regs[ OP[1] ];
1370 result = (signed)op1 >> op0;
1372 /* Compute the condition codes. */
1374 s = (result & 0x80000000);
1375 cy = (op1 & (1 << (op0 - 1)));
1377 /* Store the result and condition codes. */
1378 State.regs[ OP[1] ] = result;
1379 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1380 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1381 | (cy ? PSW_CY : 0));
1382 trace_output (OP_IMM_REG);
1387 /* sar reg1, reg2 */
1391 unsigned int op0, op1, result, z, s, cy;
1393 trace_input ("sar", OP_REG_REG, 0);
1395 op0 = State.regs[ OP[0] ] & 0x1f;
1396 op1 = State.regs[ OP[1] ];
1397 result = (signed)op1 >> op0;
1399 /* Compute the condition codes. */
1401 s = (result & 0x80000000);
1402 cy = (op1 & (1 << (op0 - 1)));
1404 /* Store the result and condition codes. */
1405 State.regs[OP[1]] = result;
1406 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1407 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1408 | (cy ? PSW_CY : 0));
1409 trace_output (OP_REG_REG);
1414 /* shl zero_extend(imm5),reg1 */
1418 unsigned int op0, op1, result, z, s, cy;
1420 trace_input ("shl", OP_IMM_REG, 0);
1422 op1 = State.regs[ OP[1] ];
1423 result = op1 << op0;
1425 /* Compute the condition codes. */
1427 s = (result & 0x80000000);
1428 cy = (op1 & (1 << (32 - op0)));
1430 /* Store the result and condition codes. */
1431 State.regs[OP[1]] = result;
1432 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1433 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1434 | (cy ? PSW_CY : 0));
1435 trace_output (OP_IMM_REG);
1440 /* shl reg1, reg2 */
1444 unsigned int op0, op1, result, z, s, cy;
1446 trace_input ("shl", OP_REG_REG, 0);
1447 op0 = State.regs[ OP[0] ] & 0x1f;
1448 op1 = State.regs[ OP[1] ];
1449 result = op1 << op0;
1451 /* Compute the condition codes. */
1453 s = (result & 0x80000000);
1454 cy = (op1 & (1 << (32 - op0)));
1456 /* Store the result and condition codes. */
1457 State.regs[OP[1]] = result;
1458 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1459 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1460 | (cy ? PSW_CY : 0));
1461 trace_output (OP_REG_REG);
1466 /* shr zero_extend(imm5),reg1 */
1470 unsigned int op0, op1, result, z, s, cy;
1472 trace_input ("shr", OP_IMM_REG, 0);
1474 op1 = State.regs[ OP[1] ];
1475 result = op1 >> op0;
1477 /* Compute the condition codes. */
1479 s = (result & 0x80000000);
1480 cy = (op1 & (1 << (op0 - 1)));
1482 /* Store the result and condition codes. */
1483 State.regs[OP[1]] = result;
1484 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1485 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1486 | (cy ? PSW_CY : 0));
1487 trace_output (OP_IMM_REG);
1492 /* shr reg1, reg2 */
1496 unsigned int op0, op1, result, z, s, cy;
1498 trace_input ("shr", OP_REG_REG, 0);
1499 op0 = State.regs[ OP[0] ] & 0x1f;
1500 op1 = State.regs[ OP[1] ];
1501 result = op1 >> op0;
1503 /* Compute the condition codes. */
1505 s = (result & 0x80000000);
1506 cy = (op1 & (1 << (op0 - 1)));
1508 /* Store the result and condition codes. */
1509 State.regs[OP[1]] = result;
1510 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1511 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1512 | (cy ? PSW_CY : 0));
1513 trace_output (OP_REG_REG);
1522 unsigned int op0, op1, result, z, s;
1524 trace_input ("or", OP_REG_REG, 0);
1526 /* Compute the result. */
1527 op0 = State.regs[ OP[0] ];
1528 op1 = State.regs[ OP[1] ];
1531 /* Compute the condition codes. */
1533 s = (result & 0x80000000);
1535 /* Store the result and condition codes. */
1536 State.regs[OP[1]] = result;
1537 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1538 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1539 trace_output (OP_REG_REG);
1544 /* ori zero_extend(imm16), reg, reg */
1548 unsigned int op0, op1, result, z, s;
1550 trace_input ("ori", OP_UIMM16_REG_REG, 0);
1552 op1 = State.regs[ OP[0] ];
1555 /* Compute the condition codes. */
1557 s = (result & 0x80000000);
1559 /* Store the result and condition codes. */
1560 State.regs[OP[1]] = result;
1561 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1562 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1563 trace_output (OP_UIMM16_REG_REG);
1572 unsigned int op0, op1, result, z, s;
1574 trace_input ("and", OP_REG_REG, 0);
1576 /* Compute the result. */
1577 op0 = State.regs[ OP[0] ];
1578 op1 = State.regs[ OP[1] ];
1581 /* Compute the condition codes. */
1583 s = (result & 0x80000000);
1585 /* Store the result and condition codes. */
1586 State.regs[OP[1]] = result;
1587 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1588 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1589 trace_output (OP_REG_REG);
1594 /* andi zero_extend(imm16), reg, reg */
1598 unsigned int result, z;
1600 trace_input ("andi", OP_UIMM16_REG_REG, 0);
1602 result = OP[2] & State.regs[ OP[0] ];
1604 /* Compute the condition codes. */
1607 /* Store the result and condition codes. */
1608 State.regs[ OP[1] ] = result;
1610 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1611 PSW |= (z ? PSW_Z : 0);
1613 trace_output (OP_UIMM16_REG_REG);
1622 unsigned int op0, op1, result, z, s;
1624 trace_input ("xor", OP_REG_REG, 0);
1626 /* Compute the result. */
1627 op0 = State.regs[ OP[0] ];
1628 op1 = State.regs[ OP[1] ];
1631 /* Compute the condition codes. */
1633 s = (result & 0x80000000);
1635 /* Store the result and condition codes. */
1636 State.regs[OP[1]] = result;
1637 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1638 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1639 trace_output (OP_REG_REG);
1644 /* xori zero_extend(imm16), reg, reg */
1648 unsigned int op0, op1, result, z, s;
1650 trace_input ("xori", OP_UIMM16_REG_REG, 0);
1652 op1 = State.regs[ OP[0] ];
1655 /* Compute the condition codes. */
1657 s = (result & 0x80000000);
1659 /* Store the result and condition codes. */
1660 State.regs[OP[1]] = result;
1661 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1662 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1663 trace_output (OP_UIMM16_REG_REG);
1668 /* not reg1, reg2 */
1672 unsigned int op0, result, z, s;
1674 trace_input ("not", OP_REG_REG_MOVE, 0);
1675 /* Compute the result. */
1676 op0 = State.regs[ OP[0] ];
1679 /* Compute the condition codes. */
1681 s = (result & 0x80000000);
1683 /* Store the result and condition codes. */
1684 State.regs[OP[1]] = result;
1685 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1686 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1687 trace_output (OP_REG_REG_MOVE);
1696 unsigned int op0, op1, op2;
1699 trace_input ("set1", OP_BIT, 0);
1700 op0 = State.regs[ OP[0] ];
1702 temp = EXTEND16 (OP[2]);
1704 temp = load_mem (op0 + op2, 1);
1706 if ((temp & (1 << op1)) == 0)
1709 store_mem (op0 + op2, 1, temp);
1710 trace_output (OP_BIT);
1719 unsigned int op0, op1, op2;
1722 trace_input ("not1", OP_BIT, 0);
1723 op0 = State.regs[ OP[0] ];
1725 temp = EXTEND16 (OP[2]);
1727 temp = load_mem (op0 + op2, 1);
1729 if ((temp & (1 << op1)) == 0)
1732 store_mem (op0 + op2, 1, temp);
1733 trace_output (OP_BIT);
1742 unsigned int op0, op1, op2;
1745 trace_input ("clr1", OP_BIT, 0);
1746 op0 = State.regs[ OP[0] ];
1748 temp = EXTEND16 (OP[2]);
1750 temp = load_mem (op0 + op2, 1);
1752 if ((temp & (1 << op1)) == 0)
1754 temp &= ~(1 << op1);
1755 store_mem (op0 + op2, 1, temp);
1756 trace_output (OP_BIT);
1765 unsigned int op0, op1, op2;
1768 trace_input ("tst1", OP_BIT, 0);
1769 op0 = State.regs[ OP[0] ];
1771 temp = EXTEND16 (OP[2]);
1773 temp = load_mem (op0 + op2, 1);
1775 if ((temp & (1 << op1)) == 0)
1777 trace_output (OP_BIT);
1786 trace_input ("di", OP_NONE, 0);
1788 trace_output (OP_NONE);
1797 trace_input ("ei", OP_NONE, 0);
1799 trace_output (OP_NONE);
1808 trace_input ("halt", OP_NONE, 0);
1809 /* FIXME this should put processor into a mode where NMI still handled */
1810 trace_output (OP_NONE);
1811 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1812 sim_stopped, SIGTRAP);
1820 trace_input ("trap", OP_TRAP, 0);
1821 trace_output (OP_TRAP);
1823 /* Trap 31 is used for simulating OS I/O functions */
1827 int save_errno = errno;
1830 /* Registers passed to trap 0 */
1832 #define FUNC State.regs[6] /* function number, return value */
1833 #define PARM1 State.regs[7] /* optional parm 1 */
1834 #define PARM2 State.regs[8] /* optional parm 2 */
1835 #define PARM3 State.regs[9] /* optional parm 3 */
1837 /* Registers set by trap 0 */
1839 #define RETVAL State.regs[10] /* return value */
1840 #define RETERR State.regs[11] /* return error code */
1842 /* Turn a pointer in a register into a pointer into real memory. */
1844 #define MEMPTR(x) (map (x))
1861 char *path = fetch_str (simulator, PARM1);
1862 char **argv = fetch_argv (simulator, PARM2);
1863 char **envp = fetch_argv (simulator, PARM3);
1864 RETVAL = execve (path, argv, envp);
1877 char *path = fetch_str (simulator, PARM1);
1878 char **argv = fetch_argv (simulator, PARM2);
1879 RETVAL = execv (path, argv);
1895 RETVAL = pipe (host_fd);
1896 SW (buf, host_fd[0]);
1897 buf += sizeof(uint16);
1898 SW (buf, host_fd[1]);
1910 RETVAL = wait (&status);
1920 char *buf = zalloc (PARM3);
1921 RETVAL = sim_io_read (simulator, PARM1, buf, PARM3);
1922 sim_write (simulator, PARM2, buf, PARM3);
1931 char *buf = zalloc (PARM3);
1932 sim_read (simulator, PARM2, buf, PARM3);
1934 RETVAL = sim_io_write_stdout (simulator, buf, PARM3);
1936 RETVAL = sim_io_write (simulator, PARM1, buf, PARM3);
1944 RETVAL = sim_io_lseek (simulator, PARM1, PARM2, PARM3);
1950 RETVAL = sim_io_close (simulator, PARM1);
1957 char *buf = fetch_str (simulator, PARM1);
1958 RETVAL = sim_io_open (simulator, buf, PARM2);
1966 if ((PARM1 & 0xffff0000) == 0xdead0000 && (PARM1 & 0xffff) != 0)
1967 /* get signal encoded by kill */
1968 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1969 sim_signalled, PARM1 & 0xffff);
1970 else if (PARM1 == 0xdead)
1972 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1973 sim_exited, SIGABRT);
1975 /* PARM1 has exit status */
1976 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1981 #if !defined(__GO32__) && !defined(_WIN32)
1983 case SYS_stat: /* added at hmsi */
1984 /* stat system call */
1986 struct stat host_stat;
1988 char *path = fetch_str (simulator, PARM1);
1990 RETVAL = stat (path, &host_stat);
1995 /* Just wild-assed guesses. */
1996 store_mem (buf, 2, host_stat.st_dev);
1997 store_mem (buf + 2, 2, host_stat.st_ino);
1998 store_mem (buf + 4, 4, host_stat.st_mode);
1999 store_mem (buf + 8, 2, host_stat.st_nlink);
2000 store_mem (buf + 10, 2, host_stat.st_uid);
2001 store_mem (buf + 12, 2, host_stat.st_gid);
2002 store_mem (buf + 14, 2, host_stat.st_rdev);
2003 store_mem (buf + 16, 4, host_stat.st_size);
2004 store_mem (buf + 20, 4, host_stat.st_atime);
2005 store_mem (buf + 28, 4, host_stat.st_mtime);
2006 store_mem (buf + 36, 4, host_stat.st_ctime);
2016 char *path = fetch_str (simulator, PARM1);
2017 RETVAL = chown (path, PARM2, PARM3);
2028 char *path = fetch_str (simulator, PARM1);
2029 RETVAL = chmod (path, PARM2);
2041 RETVAL = time (&now);
2042 store_mem (PARM1, 4, now);
2048 #if !defined(__GO32__) && !defined(_WIN32)
2053 RETVAL = times (&tms);
2054 store_mem (PARM1, 4, tms.tms_utime);
2055 store_mem (PARM1 + 4, 4, tms.tms_stime);
2056 store_mem (PARM1 + 8, 4, tms.tms_cutime);
2057 store_mem (PARM1 + 12, 4, tms.tms_cstime);
2063 #ifdef SYS_gettimeofday
2064 #if !defined(__GO32__) && !defined(_WIN32)
2065 case SYS_gettimeofday:
2069 RETVAL = gettimeofday (&t, &tz);
2070 store_mem (PARM1, 4, t.tv_sec);
2071 store_mem (PARM1 + 4, 4, t.tv_usec);
2072 store_mem (PARM2, 4, tz.tz_minuteswest);
2073 store_mem (PARM2 + 4, 4, tz.tz_dsttime);
2083 /* Cast the second argument to void *, to avoid type mismatch
2084 if a prototype is present. */
2085 sim_io_error (simulator, "Utime not supported");
2086 /* RETVAL = utime (path, (void *) MEMPTR (PARM2)); */
2101 { /* Trap 0 -> 30 */
2106 ECR |= 0x40 + OP[0];
2107 /* Flag that we are now doing exception processing. */
2108 PSW |= PSW_EP | PSW_ID;
2109 PC = ((OP[0] < 0x10) ? 0x40 : 0x50) - 4;
2115 /* start-sanitize-v850e */
2116 /* tst1 reg2, [reg1] */
2122 trace_input ("tst1", OP_BIT, 1);
2124 temp = load_mem (State.regs[ OP[0] ], 1);
2127 if ((temp & (1 << State.regs[ OP[1] & 0x7 ])) == 0)
2130 trace_output (OP_BIT);
2135 /* end-sanitize-v850e */
2136 /* start-sanitize-v850e */
2137 /* mulu reg1, reg2, reg3 */
2141 trace_input ("mulu", OP_REG_REG_REG, 0);
2143 Multiply64 (false, State.regs[ OP[0] ]);
2145 trace_output (OP_REG_REG_REG);
2150 /* end-sanitize-v850e */
2151 /* start-sanitize-v850e */
2153 #define BIT_CHANGE_OP( name, binop ) \
2155 unsigned int temp; \
2157 trace_input (name, OP_BIT_CHANGE, 0); \
2159 bit = 1 << State.regs[ OP[1] & 0x7 ]; \
2160 temp = load_mem (State.regs[ OP[0] ], 1); \
2163 if ((temp & bit) == 0) \
2167 store_mem (State.regs[ OP[0] ], 1, temp); \
2169 trace_output (OP_BIT_CHANGE); \
2173 /* clr1 reg2, [reg1] */
2177 BIT_CHANGE_OP ("clr1", &= ~ );
2180 /* not1 reg2, [reg1] */
2184 BIT_CHANGE_OP ("not1", ^= );
2191 BIT_CHANGE_OP ("set1", |= );
2198 trace_input ("sasf", OP_EX1, 0);
2200 State.regs[ OP[1] ] = (State.regs[ OP[1] ] << 1) | condition_met (OP[0]);
2202 trace_output (OP_EX1);
2206 /* end-sanitize-v850e */
2207 /* start-sanitize-v850eq */
2208 /* This function is courtesy of Sugimoto at NEC, via Seow Tan (Soew_Tan@el.nec.com) */
2213 unsigned long int als,
2214 unsigned long int sfi,
2215 unsigned long int * quotient_ptr,
2216 unsigned long int * remainder_ptr,
2217 boolean * overflow_ptr
2220 unsigned long ald = sfi >> (N - 1);
2221 unsigned long alo = als;
2226 unsigned int R1 = 1;
2227 unsigned int DBZ = (als == 0) ? 1 : 0;
2228 unsigned long alt = Q ? ~als : als;
2231 alo = ald + alt + Q;
2232 C = (((alt >> 31) & (ald >> 31))
2233 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2236 R1 = (alo == 0) ? 0 : (R1 & Q);
2237 if ((S ^ (alo>>31)) && !C)
2242 sfi = (sfi << (32-N+1)) | Q;
2243 ald = (alo << 1) | (sfi >> 31);
2245 /* 2nd - N-1th Loop */
2246 for (i = 2; i < N; i++)
2248 alt = Q ? ~als : als;
2249 alo = ald + alt + Q;
2250 C = (((alt >> 31) & (ald >> 31))
2251 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2254 R1 = (alo == 0) ? 0 : (R1 & Q);
2255 if ((S ^ (alo>>31)) && !C && !DBZ)
2260 sfi = (sfi << 1) | Q;
2261 ald = (alo << 1) | (sfi >> 31);
2265 alt = Q ? ~als : als;
2266 alo = ald + alt + Q;
2267 C = (((alt >> 31) & (ald >> 31))
2268 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2271 R1 = (alo == 0) ? 0 : (R1 & Q);
2272 if ((S ^ (alo>>31)) && !C)
2277 * quotient_ptr = (sfi << 1) | Q;
2278 * remainder_ptr = Q ? alo : (alo + als);
2279 * overflow_ptr = DBZ | R1;
2282 /* This function is courtesy of Sugimoto at NEC, via Seow Tan (Soew_Tan@el.nec.com) */
2287 unsigned long int als,
2288 unsigned long int sfi,
2289 signed long int * quotient_ptr,
2290 signed long int * remainder_ptr,
2291 boolean * overflow_ptr
2294 unsigned long ald = (signed long) sfi >> (N - 1);
2295 unsigned long alo = als;
2296 unsigned int SS = als >> 31;
2297 unsigned int SD = sfi >> 31;
2298 unsigned int R1 = 1;
2300 unsigned int DBZ = als == 0 ? 1 : 0;
2301 unsigned int Q = ~(SS ^ SD) & 1;
2305 unsigned long alt = Q ? ~als : als;
2310 alo = ald + alt + Q;
2311 C = (((alt >> 31) & (ald >> 31))
2312 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2314 R1 = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2316 sfi = (sfi << (32-N+1)) | Q;
2317 ald = (alo << 1) | (sfi >> 31);
2318 if ((alo >> 31) ^ (ald >> 31))
2323 /* 2nd - N-1th Loop */
2325 for (i = 2; i < N; i++)
2327 alt = Q ? ~als : als;
2328 alo = ald + alt + Q;
2329 C = (((alt >> 31) & (ald >> 31))
2330 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2332 R1 = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2334 sfi = (sfi << 1) | Q;
2335 ald = (alo << 1) | (sfi >> 31);
2336 if ((alo >> 31) ^ (ald >> 31))
2343 alt = Q ? ~als : als;
2344 alo = ald + alt + Q;
2345 C = (((alt >> 31) & (ald >> 31))
2346 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2348 R1 = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2349 sfi = (sfi << (32-N+1));
2355 alt = Q ? ~als : als;
2356 alo = ald + alt + Q;
2358 R1 = R1 & ((~alo >> 31) ^ SD);
2359 if ((alo != 0) && ((Q ^ (SS ^ SD)) ^ R1)) alo = ald;
2361 ald = sfi = (long) ((sfi >> 1) | (SS ^ SD) << 31) >> (32-N-1) | Q;
2363 ald = sfi = sfi | Q;
2365 OV = DBZ | ((alo == 0) ? 0 : R1);
2367 * remainder_ptr = alo;
2370 if (((alo != 0) && ((SS ^ SD) ^ R1))
2371 || ((alo == 0) && (SS ^ R1)))
2376 OV = (DBZ | R1) ? OV : ((alo >> 31) & (~ald >> 31));
2378 * quotient_ptr = alo;
2379 * overflow_ptr = OV;
2382 /* sdivun imm5, reg1, reg2, reg3 */
2386 unsigned long int quotient;
2387 unsigned long int remainder;
2388 unsigned long int divide_by;
2389 unsigned long int divide_this;
2390 boolean overflow = false;
2393 trace_input ("sdivun", OP_IMM_REG_REG_REG, 0);
2395 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2397 divide_by = State.regs[ OP[0] ];
2398 divide_this = State.regs[ OP[1] ] << imm5;
2400 divun (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2402 State.regs[ OP[1] ] = quotient;
2403 State.regs[ OP[2] >> 11 ] = remainder;
2405 /* Set condition codes. */
2406 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2408 if (overflow) PSW |= PSW_OV;
2409 if (quotient == 0) PSW |= PSW_Z;
2410 if (quotient & 0x80000000) PSW |= PSW_S;
2412 trace_output (OP_IMM_REG_REG_REG);
2417 /* sdivn imm5, reg1, reg2, reg3 */
2421 signed long int quotient;
2422 signed long int remainder;
2423 signed long int divide_by;
2424 signed long int divide_this;
2425 boolean overflow = false;
2428 trace_input ("sdivn", OP_IMM_REG_REG_REG, 0);
2430 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2432 divide_by = State.regs[ OP[0] ];
2433 divide_this = State.regs[ OP[1] ] << imm5;
2435 divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2437 State.regs[ OP[1] ] = quotient;
2438 State.regs[ OP[2] >> 11 ] = remainder;
2440 /* Set condition codes. */
2441 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2443 if (overflow) PSW |= PSW_OV;
2444 if (quotient == 0) PSW |= PSW_Z;
2445 if (quotient < 0) PSW |= PSW_S;
2447 trace_output (OP_IMM_REG_REG_REG);
2452 /* sdivhun imm5, reg1, reg2, reg3 */
2456 unsigned long int quotient;
2457 unsigned long int remainder;
2458 unsigned long int divide_by;
2459 unsigned long int divide_this;
2460 boolean overflow = false;
2463 trace_input ("sdivhun", OP_IMM_REG_REG_REG, 0);
2465 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2467 divide_by = State.regs[ OP[0] ] & 0xffff;
2468 divide_this = State.regs[ OP[1] ] << imm5;
2470 divun (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2472 State.regs[ OP[1] ] = quotient;
2473 State.regs[ OP[2] >> 11 ] = remainder;
2475 /* Set condition codes. */
2476 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2478 if (overflow) PSW |= PSW_OV;
2479 if (quotient == 0) PSW |= PSW_Z;
2480 if (quotient & 0x80000000) PSW |= PSW_S;
2482 trace_output (OP_IMM_REG_REG_REG);
2487 /* sdivhn imm5, reg1, reg2, reg3 */
2491 signed long int quotient;
2492 signed long int remainder;
2493 signed long int divide_by;
2494 signed long int divide_this;
2495 boolean overflow = false;
2498 trace_input ("sdivhn", OP_IMM_REG_REG_REG, 0);
2500 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2502 divide_by = EXTEND16 (State.regs[ OP[0] ]);
2503 divide_this = State.regs[ OP[1] ] << imm5;
2505 divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2507 State.regs[ OP[1] ] = quotient;
2508 State.regs[ OP[2] >> 11 ] = remainder;
2510 /* Set condition codes. */
2511 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2513 if (overflow) PSW |= PSW_OV;
2514 if (quotient == 0) PSW |= PSW_Z;
2515 if (quotient < 0) PSW |= PSW_S;
2517 trace_output (OP_IMM_REG_REG_REG);
2522 /* end-sanitize-v850eq */
2523 /* start-sanitize-v850e */
2524 /* divu reg1, reg2, reg3 */
2528 unsigned long int quotient;
2529 unsigned long int remainder;
2530 unsigned long int divide_by;
2531 unsigned long int divide_this;
2532 boolean overflow = false;
2534 trace_input ("divu", OP_REG_REG_REG, 0);
2536 /* Compute the result. */
2538 divide_by = State.regs[ OP[0] ];
2539 divide_this = State.regs[ OP[1] ];
2547 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2548 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2550 /* Set condition codes. */
2551 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2553 if (overflow) PSW |= PSW_OV;
2554 if (quotient == 0) PSW |= PSW_Z;
2555 if (quotient & 0x80000000) PSW |= PSW_S;
2557 trace_output (OP_REG_REG_REG);
2562 /* end-sanitize-v850e */
2563 /* start-sanitize-v850e */
2564 /* div reg1, reg2, reg3 */
2568 signed long int quotient;
2569 signed long int remainder;
2570 signed long int divide_by;
2571 signed long int divide_this;
2572 boolean overflow = false;
2574 trace_input ("div", OP_REG_REG_REG, 0);
2576 /* Compute the result. */
2578 divide_by = State.regs[ OP[0] ];
2579 divide_this = State.regs[ OP[1] ];
2581 if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
2587 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2588 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2590 /* Set condition codes. */
2591 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2593 if (overflow) PSW |= PSW_OV;
2594 if (quotient == 0) PSW |= PSW_Z;
2595 if (quotient < 0) PSW |= PSW_S;
2597 trace_output (OP_REG_REG_REG);
2602 /* end-sanitize-v850e */
2603 /* start-sanitize-v850e */
2604 /* divhu reg1, reg2, reg3 */
2608 unsigned long int quotient;
2609 unsigned long int remainder;
2610 unsigned long int divide_by;
2611 unsigned long int divide_this;
2612 boolean overflow = false;
2614 trace_input ("divhu", OP_REG_REG_REG, 0);
2616 /* Compute the result. */
2618 divide_by = State.regs[ OP[0] ] & 0xffff;
2619 divide_this = State.regs[ OP[1] ];
2627 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2628 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2630 /* Set condition codes. */
2631 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2633 if (overflow) PSW |= PSW_OV;
2634 if (quotient == 0) PSW |= PSW_Z;
2635 if (quotient & 0x80000000) PSW |= PSW_S;
2637 trace_output (OP_REG_REG_REG);
2642 /* end-sanitize-v850e */
2643 /* start-sanitize-v850e */
2644 /* divh reg1, reg2, reg3 */
2648 signed long int quotient;
2649 signed long int remainder;
2650 signed long int divide_by;
2651 signed long int divide_this;
2652 boolean overflow = false;
2654 trace_input ("divh", OP_REG_REG_REG, 0);
2656 /* Compute the result. */
2658 divide_by = State.regs[ OP[0] ];
2659 divide_this = EXTEND16 (State.regs[ OP[1] ]);
2661 if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
2667 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2668 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2670 /* Set condition codes. */
2671 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2673 if (overflow) PSW |= PSW_OV;
2674 if (quotient == 0) PSW |= PSW_Z;
2675 if (quotient < 0) PSW |= PSW_S;
2677 trace_output (OP_REG_REG_REG);
2682 /* end-sanitize-v850e */
2683 /* start-sanitize-v850e */
2684 /* mulu imm9, reg2, reg3 */
2688 trace_input ("mulu", OP_IMM_REG_REG, 0);
2690 Multiply64 (false, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
2692 trace_output (OP_IMM_REG_REG);
2697 /* end-sanitize-v850e */
2698 /* start-sanitize-v850e */
2699 /* mul imm9, reg2, reg3 */
2703 trace_input ("mul", OP_IMM_REG_REG, 0);
2705 Multiply64 (true, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
2707 trace_output (OP_IMM_REG_REG);
2712 /* end-sanitize-v850e */
2713 /* start-sanitize-v850e */
2714 /* cmov imm5, reg2, reg3 */
2718 trace_input ("cmov", OP_IMM_REG_REG, 0);
2720 State.regs[ OP[2] >> 11 ] = condition_met (OP[0]) ? SEXT5( OP[0] ) : State.regs[ OP[1] ];
2722 trace_output (OP_IMM_REG_REG);
2728 /* end-sanitize-v850e */
2729 /* start-sanitize-v850e */
2736 trace_input ("ld.hu", OP_LOAD32, 2);
2738 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
2741 State.regs[ OP[1] ] = load_mem (adr, 2);
2743 trace_output (OP_LOAD32);
2748 /* end-sanitize-v850e */
2749 /* start-sanitize-v850e */
2756 trace_input ("ld.bu", OP_LOAD32, 1);
2758 adr = (State.regs[ OP[0] ]
2759 + (EXTEND16 (OP[2] & ~1) | ((OP[3] >> 5) & 1)));
2761 State.regs[ OP[1] ] = load_mem (adr, 1);
2763 trace_output (OP_LOAD32);
2768 /* prepare list12, imm5, imm32 */
2774 trace_input ("prepare", OP_PUSHPOP1, 0);
2776 /* Store the registers with lower number registers being placed at higher addresses. */
2777 for (i = 0; i < 12; i++)
2778 if ((OP[3] & (1 << type1_regs[ i ])))
2781 store_mem (SP, 4, State.regs[ 20 + i ]);
2784 SP -= (OP[3] & 0x3e) << 1;
2786 EP = load_mem (PC + 4, 4);
2788 trace_output (OP_PUSHPOP1);
2793 /* prepare list12, imm5, imm16-32 */
2799 trace_input ("prepare", OP_PUSHPOP1, 0);
2801 /* Store the registers with lower number registers being placed at higher addresses. */
2802 for (i = 0; i < 12; i++)
2803 if ((OP[3] & (1 << type1_regs[ i ])))
2806 store_mem (SP, 4, State.regs[ 20 + i ]);
2809 SP -= (OP[3] & 0x3e) << 1;
2811 EP = load_mem (PC + 4, 2) << 16;
2813 trace_output (OP_PUSHPOP1);
2818 /* prepare list12, imm5, imm16 */
2824 trace_input ("prepare", OP_PUSHPOP1, 0);
2826 /* Store the registers with lower number registers being placed at higher addresses. */
2827 for (i = 0; i < 12; i++)
2828 if ((OP[3] & (1 << type1_regs[ i ])))
2831 store_mem (SP, 4, State.regs[ 20 + i ]);
2834 SP -= (OP[3] & 0x3e) << 1;
2836 EP = EXTEND16 (load_mem (PC + 4, 2));
2838 trace_output (OP_PUSHPOP1);
2843 /* prepare list12, imm5, sp */
2849 trace_input ("prepare", OP_PUSHPOP1, 0);
2851 /* Store the registers with lower number registers being placed at higher addresses. */
2852 for (i = 0; i < 12; i++)
2853 if ((OP[3] & (1 << type1_regs[ i ])))
2856 store_mem (SP, 4, State.regs[ 20 + i ]);
2859 SP -= (OP[3] & 0x3e) << 1;
2863 trace_output (OP_PUSHPOP1);
2868 /* end-sanitize-v850e */
2869 /* start-sanitize-v850e */
2874 unsigned long result;
2876 result = load_mem (State.regs[30] + ((OP[3] & 0xf) << 1), 2);
2878 /* start-sanitize-v850eq */
2881 trace_input ("sld.h", OP_LOAD16, 2);
2882 State.regs[ OP[1] ] = EXTEND16 (result);
2886 /* end-sanitize-v850eq */
2887 trace_input ("sld.hu", OP_LOAD16, 2);
2889 State.regs[ OP[1] ] = result;
2890 /* start-sanitize-v850eq */
2892 /* end-sanitize-v850eq */
2894 trace_output (OP_LOAD16);
2899 /* end-sanitize-v850e */
2900 /* start-sanitize-v850e */
2901 /* cmov reg1, reg2, reg3 */
2905 trace_input ("cmov", OP_REG_REG_REG, 0);
2907 State.regs[ OP[2] >> 11 ] = condition_met (OP[0]) ? State.regs[ OP[0] ] : State.regs[ OP[1] ];
2909 trace_output (OP_REG_REG_REG);
2914 /* end-sanitize-v850e */
2915 /* start-sanitize-v850e */
2916 /* mul reg1, reg2, reg3 */
2920 trace_input ("mul", OP_REG_REG_REG, 0);
2922 Multiply64 (true, State.regs[ OP[0] ]);
2924 trace_output (OP_REG_REG_REG);
2929 /* end-sanitize-v850e */
2930 /* start-sanitize-v850eq */
2938 trace_input ("popmh", OP_PUSHPOP2, 0);
2940 if (OP[3] & (1 << 19))
2942 if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
2944 FEPSW = load_mem ( SP & ~ 3, 4);
2945 FEPC = load_mem ((SP + 4) & ~ 3, 4);
2949 EIPSW = load_mem ( SP & ~ 3, 4);
2950 EIPC = load_mem ((SP + 4) & ~ 3, 4);
2956 /* Load the registers with lower number registers being retrieved from higher addresses. */
2958 if ((OP[3] & (1 << type2_regs[ i ])))
2960 State.regs[ i + 16 ] = load_mem (SP & ~ 3, 4);
2964 trace_output (OP_PUSHPOP2);
2975 trace_input ("popml", OP_PUSHPOP3, 0);
2977 if (OP[3] & (1 << 19))
2979 if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
2981 FEPSW = load_mem ( SP & ~ 3, 4);
2982 FEPC = load_mem ((SP + 4) & ~ 3, 4);
2986 EIPSW = load_mem ( SP & ~ 3, 4);
2987 EIPC = load_mem ((SP + 4) & ~ 3, 4);
2993 if (OP[3] & (1 << 3))
2995 PSW = load_mem (SP & ~ 3, 4);
2999 /* Load the registers with lower number registers being retrieved from higher addresses. */
3001 if ((OP[3] & (1 << type3_regs[ i ])))
3003 State.regs[ i + 1 ] = load_mem (SP & ~ 3, 4);
3007 trace_output (OP_PUSHPOP2);
3018 trace_input ("pushmh", OP_PUSHPOP2, 0);
3020 /* Store the registers with lower number registers being placed at higher addresses. */
3021 for (i = 0; i < 16; i++)
3022 if ((OP[3] & (1 << type2_regs[ i ])))
3025 store_mem (SP & ~ 3, 4, State.regs[ i + 16 ]);
3028 if (OP[3] & (1 << 19))
3032 if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
3034 store_mem ((SP + 4) & ~ 3, 4, FEPC);
3035 store_mem ( SP & ~ 3, 4, FEPSW);
3039 store_mem ((SP + 4) & ~ 3, 4, EIPC);
3040 store_mem ( SP & ~ 3, 4, EIPSW);
3044 trace_output (OP_PUSHPOP2);
3049 /* end-sanitize-v850eq */