25 #include "targ-vals.h"
27 #include "libiberty.h"
30 #if !defined(__GO32__) && !defined(_WIN32)
32 #include <sys/times.h>
36 /* This is an array of the bit positions of registers r20 .. r31 in
37 that order in a prepare/dispose instruction. */
38 int type1_regs[12] = { 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 0, 21 };
39 /* This is an array of the bit positions of registers r16 .. r31 in
40 that order in a push/pop instruction. */
41 int type2_regs[16] = { 3, 2, 1, 0, 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 20, 21};
42 /* This is an array of the bit positions of registers r1 .. r15 in
43 that order in a push/pop instruction. */
44 int type3_regs[15] = { 2, 1, 0, 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 20, 21};
47 #ifndef SIZE_INSTRUCTION
48 #define SIZE_INSTRUCTION 18
52 #define SIZE_VALUES 11
56 unsigned32 trace_values[3];
59 const char *trace_name;
64 trace_input (name, type, size)
70 if (!TRACE_ALU_P (STATE_CPU (simulator, 0)))
75 trace_module = TRACE_ALU_IDX;
88 trace_values[0] = State.regs[OP[0]];
95 trace_values[0] = State.regs[OP[1]];
96 trace_values[1] = State.regs[OP[0]];
102 trace_values[0] = SEXT5 (OP[0]);
103 trace_values[1] = OP[1];
104 trace_num_values = 2;
107 case OP_IMM_REG_MOVE:
108 trace_values[0] = SEXT5 (OP[0]);
109 trace_num_values = 1;
113 trace_values[0] = State.pc;
114 trace_values[1] = SEXT9 (OP[0]);
115 trace_values[2] = PSW;
116 trace_num_values = 3;
120 trace_values[0] = OP[1] * size;
121 trace_values[1] = State.regs[30];
122 trace_num_values = 2;
126 trace_values[0] = State.regs[OP[0]];
127 trace_values[1] = OP[1] * size;
128 trace_values[2] = State.regs[30];
129 trace_num_values = 3;
133 trace_values[0] = EXTEND16 (OP[2]);
134 trace_values[1] = State.regs[OP[0]];
135 trace_num_values = 2;
139 trace_values[0] = State.regs[OP[1]];
140 trace_values[1] = EXTEND16 (OP[2]);
141 trace_values[2] = State.regs[OP[0]];
142 trace_num_values = 3;
146 trace_values[0] = SEXT22 (OP[0]);
147 trace_values[1] = State.pc;
148 trace_num_values = 2;
152 trace_values[0] = EXTEND16 (OP[0]) << size;
153 trace_values[1] = State.regs[OP[1]];
154 trace_num_values = 2;
157 case OP_IMM16_REG_REG:
158 trace_values[0] = EXTEND16 (OP[2]) << size;
159 trace_values[1] = State.regs[OP[1]];
160 trace_num_values = 2;
163 case OP_UIMM_REG_REG:
164 trace_values[0] = (OP[0] & 0xffff) << size;
165 trace_values[1] = State.regs[OP[1]];
166 trace_num_values = 2;
169 case OP_UIMM16_REG_REG:
170 trace_values[0] = (OP[2]) << size;
171 trace_values[1] = State.regs[OP[1]];
172 trace_num_values = 2;
176 trace_num_values = 0;
180 trace_values[0] = PSW;
181 trace_num_values = 1;
185 trace_num_values = 0;
189 trace_values[0] = State.regs[OP[0]];
190 trace_num_values = 1;
194 trace_values[0] = State.sregs[OP[1]];
195 trace_num_values = 1;
201 trace_result (int has_result, unsigned32 result)
209 /* write out the values saved during the trace_input call */
212 for (i = 0; i < trace_num_values; i++)
214 sprintf (chp, "%*s0x%.8lx", SIZE_VALUES - 10, "",
215 (long) trace_values[i]);
216 chp = strchr (chp, '\0');
220 sprintf (chp, "%*s", SIZE_VALUES, "");
221 chp = strchr (chp, '\0');
225 /* append any result to the end of the buffer */
227 sprintf (chp, " :: 0x%.8lx", (unsigned long)result);
229 trace_generic (simulator, STATE_CPU (simulator, 0), trace_module, buf);
233 trace_output (result)
234 enum op_types result;
236 if (!TRACE_ALU_P (STATE_CPU (simulator, 0)))
258 trace_result (1, State.regs[OP[0]]);
262 case OP_REG_REG_MOVE:
264 case OP_IMM_REG_MOVE:
267 trace_result (1, State.regs[OP[1]]);
271 case OP_UIMM_REG_REG:
272 case OP_IMM16_REG_REG:
273 case OP_UIMM16_REG_REG:
274 trace_result (1, State.regs[OP[1]]);
279 trace_result (1, State.regs[OP[1]]);
285 trace_result (1, State.sregs[OP[1]]);
292 /* Returns 1 if the specific condition is met, returns 0 otherwise. */
294 condition_met (unsigned code)
296 unsigned int psw = PSW;
300 case 0x0: return ((psw & PSW_OV) != 0);
301 case 0x1: return ((psw & PSW_CY) != 0);
302 case 0x2: return ((psw & PSW_Z) != 0);
303 case 0x3: return ((((psw & PSW_CY) != 0) | ((psw & PSW_Z) != 0)) != 0);
304 case 0x4: return ((psw & PSW_S) != 0);
305 /*case 0x5: return 1;*/
306 case 0x6: return ((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) != 0);
307 case 0x7: return (((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) || ((psw & PSW_Z) != 0)) != 0);
308 case 0x8: return ((psw & PSW_OV) == 0);
309 case 0x9: return ((psw & PSW_CY) == 0);
310 case 0xa: return ((psw & PSW_Z) == 0);
311 case 0xb: return ((((psw & PSW_CY) != 0) | ((psw & PSW_Z) != 0)) == 0);
312 case 0xc: return ((psw & PSW_S) == 0);
313 case 0xd: return ((psw & PSW_SAT) != 0);
314 case 0xe: return ((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) == 0);
315 case 0xf: return (((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) || ((psw & PSW_Z) != 0)) == 0);
322 Add32 (unsigned long a1, unsigned long a2, int * carry)
324 unsigned long result = (a1 + a2);
326 * carry = (result < a1);
332 Multiply64 (int sign, unsigned long op0)
343 op1 = State.regs[ OP[1] ];
347 /* Compute sign of result and adjust operands if necessary. */
349 sign = (op0 ^ op1) & 0x80000000;
351 if (((signed long) op0) < 0)
354 if (((signed long) op1) < 0)
358 /* We can split the 32x32 into four 16x16 operations. This ensures
359 that we do not lose precision on 32bit only hosts: */
360 lo = ( (op0 & 0xFFFF) * (op1 & 0xFFFF));
361 mid1 = ( (op0 & 0xFFFF) * ((op1 >> 16) & 0xFFFF));
362 mid2 = (((op0 >> 16) & 0xFFFF) * (op1 & 0xFFFF));
363 hi = (((op0 >> 16) & 0xFFFF) * ((op1 >> 16) & 0xFFFF));
365 /* We now need to add all of these results together, taking care
366 to propogate the carries from the additions: */
367 RdLo = Add32 (lo, (mid1 << 16), & carry);
369 RdLo = Add32 (RdLo, (mid2 << 16), & carry);
370 RdHi += (carry + ((mid1 >> 16) & 0xFFFF) + ((mid2 >> 16) & 0xFFFF) + hi);
374 /* Negate result if necessary. */
378 if (RdLo == 0xFFFFFFFF)
387 /* Don't store into register 0. */
389 State.regs[ OP[1] ] = RdLo;
391 State.regs[ OP[2] >> 11 ] = RdHi;
397 /* Read a null terminated string from memory, return in a buffer */
405 while (sim_core_read_1 (STATE_CPU (sd, 0),
406 PC, read_map, addr + nr) != 0)
408 buf = NZALLOC (char, nr + 1);
409 sim_read (simulator, addr, buf, nr);
413 /* Read a null terminated argument vector from memory, return in a
416 fetch_argv (sd, addr)
422 char **buf = xmalloc (max_nr * sizeof (char*));
425 unsigned32 a = sim_core_read_4 (STATE_CPU (sd, 0),
426 PC, read_map, addr + nr * 4);
428 buf[nr] = fetch_str (sd, a);
430 if (nr == max_nr - 1)
433 buf = xrealloc (buf, max_nr * sizeof (char*));
445 trace_input ("sst.b", OP_STORE16, 1);
447 store_mem (State.regs[30] + (OP[3] & 0x7f), 1, State.regs[ OP[1] ]);
449 trace_output (OP_STORE16);
458 trace_input ("sst.h", OP_STORE16, 2);
460 store_mem (State.regs[30] + ((OP[3] & 0x7f) << 1), 2, State.regs[ OP[1] ]);
462 trace_output (OP_STORE16);
471 trace_input ("sst.w", OP_STORE16, 4);
473 store_mem (State.regs[30] + ((OP[3] & 0x7e) << 1), 4, State.regs[ OP[1] ]);
475 trace_output (OP_STORE16);
486 trace_input ("ld.b", OP_LOAD32, 1);
488 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
490 State.regs[ OP[1] ] = EXTEND8 (load_mem (adr, 1));
492 trace_output (OP_LOAD32);
503 trace_input ("ld.h", OP_LOAD32, 2);
505 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
508 State.regs[ OP[1] ] = EXTEND16 (load_mem (adr, 2));
510 trace_output (OP_LOAD32);
521 trace_input ("ld.w", OP_LOAD32, 4);
523 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
526 State.regs[ OP[1] ] = load_mem (adr, 4);
528 trace_output (OP_LOAD32);
537 trace_input ("st.b", OP_STORE32, 1);
539 store_mem (State.regs[ OP[0] ] + EXTEND16 (OP[2]), 1, State.regs[ OP[1] ]);
541 trace_output (OP_STORE32);
552 trace_input ("st.h", OP_STORE32, 2);
554 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
557 store_mem (adr, 2, State.regs[ OP[1] ]);
559 trace_output (OP_STORE32);
570 trace_input ("st.w", OP_STORE32, 4);
572 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
575 store_mem (adr, 4, State.regs[ OP[1] ]);
577 trace_output (OP_STORE32);
586 unsigned int op0, op1, result, z, s, cy, ov;
588 trace_input ("add", OP_REG_REG, 0);
590 /* Compute the result. */
592 op0 = State.regs[ OP[0] ];
593 op1 = State.regs[ OP[1] ];
597 /* Compute the condition codes. */
599 s = (result & 0x80000000);
600 cy = (result < op0 || result < op1);
601 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
602 && (op0 & 0x80000000) != (result & 0x80000000));
604 /* Store the result and condition codes. */
605 State.regs[OP[1]] = result;
606 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
607 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
608 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
609 trace_output (OP_REG_REG);
614 /* add sign_extend(imm5), reg */
618 unsigned int op0, op1, result, z, s, cy, ov;
621 trace_input ("add", OP_IMM_REG, 0);
623 /* Compute the result. */
624 temp = SEXT5 (OP[0]);
626 op1 = State.regs[OP[1]];
629 /* Compute the condition codes. */
631 s = (result & 0x80000000);
632 cy = (result < op0 || result < op1);
633 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
634 && (op0 & 0x80000000) != (result & 0x80000000));
636 /* Store the result and condition codes. */
637 State.regs[OP[1]] = result;
638 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
639 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
640 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
641 trace_output (OP_IMM_REG);
646 /* addi sign_extend(imm16), reg, reg */
650 unsigned int op0, op1, result, z, s, cy, ov;
652 trace_input ("addi", OP_IMM16_REG_REG, 0);
654 /* Compute the result. */
656 op0 = EXTEND16 (OP[2]);
657 op1 = State.regs[ OP[0] ];
660 /* Compute the condition codes. */
662 s = (result & 0x80000000);
663 cy = (result < op0 || result < op1);
664 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
665 && (op0 & 0x80000000) != (result & 0x80000000));
667 /* Store the result and condition codes. */
668 State.regs[OP[1]] = result;
669 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
670 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
671 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
672 trace_output (OP_IMM16_REG_REG);
681 unsigned int op0, op1, result, z, s, cy, ov;
683 trace_input ("sub", OP_REG_REG, 0);
684 /* Compute the result. */
685 op0 = State.regs[ OP[0] ];
686 op1 = State.regs[ OP[1] ];
689 /* Compute the condition codes. */
691 s = (result & 0x80000000);
693 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
694 && (op1 & 0x80000000) != (result & 0x80000000));
696 /* Store the result and condition codes. */
697 State.regs[OP[1]] = result;
698 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
699 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
700 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
701 trace_output (OP_REG_REG);
706 /* subr reg1, reg2 */
710 unsigned int op0, op1, result, z, s, cy, ov;
712 trace_input ("subr", OP_REG_REG, 0);
713 /* Compute the result. */
714 op0 = State.regs[ OP[0] ];
715 op1 = State.regs[ OP[1] ];
718 /* Compute the condition codes. */
720 s = (result & 0x80000000);
722 ov = ((op0 & 0x80000000) != (op1 & 0x80000000)
723 && (op0 & 0x80000000) != (result & 0x80000000));
725 /* Store the result and condition codes. */
726 State.regs[OP[1]] = result;
727 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
728 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
729 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
730 trace_output (OP_REG_REG);
739 trace_input ("mulh", OP_REG_REG, 0);
741 State.regs[ OP[1] ] = (EXTEND16 (State.regs[ OP[1] ]) * EXTEND16 (State.regs[ OP[0] ]));
743 trace_output (OP_REG_REG);
748 /* mulh sign_extend(imm5), reg2 */
752 trace_input ("mulh", OP_IMM_REG, 0);
754 State.regs[ OP[1] ] = EXTEND16 (State.regs[ OP[1] ]) * SEXT5 (OP[0]);
756 trace_output (OP_IMM_REG);
761 /* mulhi imm16, reg1, reg2 */
765 trace_input ("mulhi", OP_IMM16_REG_REG, 0);
767 State.regs[ OP[1] ] = EXTEND16 (State.regs[ OP[0] ]) * EXTEND16 (OP[2]);
769 trace_output (OP_IMM16_REG_REG);
778 unsigned int op0, op1, result, z, s, cy, ov;
780 trace_input ("cmp", OP_REG_REG_CMP, 0);
781 /* Compute the result. */
782 op0 = State.regs[ OP[0] ];
783 op1 = State.regs[ OP[1] ];
786 /* Compute the condition codes. */
788 s = (result & 0x80000000);
790 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
791 && (op1 & 0x80000000) != (result & 0x80000000));
793 /* Set condition codes. */
794 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
795 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
796 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
797 trace_output (OP_REG_REG_CMP);
802 /* cmp sign_extend(imm5), reg */
806 unsigned int op0, op1, result, z, s, cy, ov;
809 /* Compute the result. */
810 trace_input ("cmp", OP_IMM_REG_CMP, 0);
811 temp = SEXT5 (OP[0]);
813 op1 = State.regs[OP[1]];
816 /* Compute the condition codes. */
818 s = (result & 0x80000000);
820 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
821 && (op1 & 0x80000000) != (result & 0x80000000));
823 /* Set condition codes. */
824 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
825 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
826 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
827 trace_output (OP_IMM_REG_CMP);
836 trace_input ("setf", OP_EX1, 0);
838 State.regs[ OP[1] ] = condition_met (OP[0]);
840 trace_output (OP_EX1);
849 unsigned int op0, op1, result, z, s, cy, ov, sat;
851 trace_input ("satadd", OP_REG_REG, 0);
852 /* Compute the result. */
853 op0 = State.regs[ OP[0] ];
854 op1 = State.regs[ OP[1] ];
857 /* Compute the condition codes. */
859 s = (result & 0x80000000);
860 cy = (result < op0 || result < op1);
861 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
862 && (op0 & 0x80000000) != (result & 0x80000000));
865 /* Store the result and condition codes. */
866 State.regs[OP[1]] = result;
867 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
868 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
869 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
870 | (sat ? PSW_SAT : 0));
872 /* Handle saturated results. */
874 State.regs[OP[1]] = 0x80000000;
876 State.regs[OP[1]] = 0x7fffffff;
877 trace_output (OP_REG_REG);
882 /* satadd sign_extend(imm5), reg */
886 unsigned int op0, op1, result, z, s, cy, ov, sat;
890 trace_input ("satadd", OP_IMM_REG, 0);
892 /* Compute the result. */
893 temp = SEXT5 (OP[0]);
895 op1 = State.regs[OP[1]];
898 /* Compute the condition codes. */
900 s = (result & 0x80000000);
901 cy = (result < op0 || result < op1);
902 ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
903 && (op0 & 0x80000000) != (result & 0x80000000));
906 /* Store the result and condition codes. */
907 State.regs[OP[1]] = result;
908 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
909 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
910 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
911 | (sat ? PSW_SAT : 0));
913 /* Handle saturated results. */
915 State.regs[OP[1]] = 0x80000000;
917 State.regs[OP[1]] = 0x7fffffff;
918 trace_output (OP_IMM_REG);
923 /* satsub reg1, reg2 */
927 unsigned int op0, op1, result, z, s, cy, ov, sat;
929 trace_input ("satsub", OP_REG_REG, 0);
931 /* Compute the result. */
932 op0 = State.regs[ OP[0] ];
933 op1 = State.regs[ OP[1] ];
936 /* Compute the condition codes. */
938 s = (result & 0x80000000);
940 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
941 && (op1 & 0x80000000) != (result & 0x80000000));
944 /* Store the result and condition codes. */
945 State.regs[OP[1]] = result;
946 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
947 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
948 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
949 | (sat ? PSW_SAT : 0));
951 /* Handle saturated results. */
953 State.regs[OP[1]] = 0x80000000;
955 State.regs[OP[1]] = 0x7fffffff;
956 trace_output (OP_REG_REG);
960 /* satsubi sign_extend(imm16), reg */
964 unsigned int op0, op1, result, z, s, cy, ov, sat;
967 trace_input ("satsubi", OP_IMM_REG, 0);
969 /* Compute the result. */
970 temp = EXTEND16 (OP[2]);
972 op1 = State.regs[ OP[0] ];
975 /* Compute the condition codes. */
977 s = (result & 0x80000000);
979 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
980 && (op1 & 0x80000000) != (result & 0x80000000));
983 /* Store the result and condition codes. */
984 State.regs[OP[1]] = result;
985 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
986 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
987 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
988 | (sat ? PSW_SAT : 0));
990 /* Handle saturated results. */
992 State.regs[OP[1]] = 0x80000000;
994 State.regs[OP[1]] = 0x7fffffff;
995 trace_output (OP_IMM_REG);
1000 /* satsubr reg,reg */
1004 unsigned int op0, op1, result, z, s, cy, ov, sat;
1006 trace_input ("satsubr", OP_REG_REG, 0);
1008 /* Compute the result. */
1009 op0 = State.regs[ OP[0] ];
1010 op1 = State.regs[ OP[1] ];
1013 /* Compute the condition codes. */
1015 s = (result & 0x80000000);
1016 cy = (result < op0);
1017 ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1018 && (op1 & 0x80000000) != (result & 0x80000000));
1021 /* Store the result and condition codes. */
1022 State.regs[OP[1]] = result;
1023 PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1024 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1025 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1026 | (sat ? PSW_SAT : 0));
1028 /* Handle saturated results. */
1030 State.regs[OP[1]] = 0x80000000;
1032 State.regs[OP[1]] = 0x7fffffff;
1033 trace_output (OP_REG_REG);
1042 unsigned int op0, op1, result, z, s;
1044 trace_input ("tst", OP_REG_REG_CMP, 0);
1046 /* Compute the result. */
1047 op0 = State.regs[ OP[0] ];
1048 op1 = State.regs[ OP[1] ];
1051 /* Compute the condition codes. */
1053 s = (result & 0x80000000);
1055 /* Store the condition codes. */
1056 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1057 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1058 trace_output (OP_REG_REG_CMP);
1063 /* mov sign_extend(imm5), reg */
1067 int value = SEXT5 (OP[0]);
1069 trace_input ("mov", OP_IMM_REG_MOVE, 0);
1071 State.regs[ OP[1] ] = value;
1073 trace_output (OP_IMM_REG_MOVE);
1078 /* movhi imm16, reg, reg */
1082 trace_input ("movhi", OP_UIMM16_REG_REG, 16);
1084 State.regs[ OP[1] ] = State.regs[ OP[0] ] + (OP[2] << 16);
1086 trace_output (OP_UIMM16_REG_REG);
1091 /* sar zero_extend(imm5),reg1 */
1095 unsigned int op0, op1, result, z, s, cy;
1097 trace_input ("sar", OP_IMM_REG, 0);
1099 op1 = State.regs[ OP[1] ];
1100 result = (signed)op1 >> op0;
1102 /* Compute the condition codes. */
1104 s = (result & 0x80000000);
1105 cy = (op1 & (1 << (op0 - 1)));
1107 /* Store the result and condition codes. */
1108 State.regs[ OP[1] ] = result;
1109 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1110 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1111 | (cy ? PSW_CY : 0));
1112 trace_output (OP_IMM_REG);
1117 /* sar reg1, reg2 */
1121 unsigned int op0, op1, result, z, s, cy;
1123 trace_input ("sar", OP_REG_REG, 0);
1125 op0 = State.regs[ OP[0] ] & 0x1f;
1126 op1 = State.regs[ OP[1] ];
1127 result = (signed)op1 >> op0;
1129 /* Compute the condition codes. */
1131 s = (result & 0x80000000);
1132 cy = (op1 & (1 << (op0 - 1)));
1134 /* Store the result and condition codes. */
1135 State.regs[OP[1]] = result;
1136 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1137 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1138 | (cy ? PSW_CY : 0));
1139 trace_output (OP_REG_REG);
1144 /* shl zero_extend(imm5),reg1 */
1148 unsigned int op0, op1, result, z, s, cy;
1150 trace_input ("shl", OP_IMM_REG, 0);
1152 op1 = State.regs[ OP[1] ];
1153 result = op1 << op0;
1155 /* Compute the condition codes. */
1157 s = (result & 0x80000000);
1158 cy = (op1 & (1 << (32 - op0)));
1160 /* Store the result and condition codes. */
1161 State.regs[OP[1]] = result;
1162 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1163 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1164 | (cy ? PSW_CY : 0));
1165 trace_output (OP_IMM_REG);
1170 /* shl reg1, reg2 */
1174 unsigned int op0, op1, result, z, s, cy;
1176 trace_input ("shl", OP_REG_REG, 0);
1177 op0 = State.regs[ OP[0] ] & 0x1f;
1178 op1 = State.regs[ OP[1] ];
1179 result = op1 << op0;
1181 /* Compute the condition codes. */
1183 s = (result & 0x80000000);
1184 cy = (op1 & (1 << (32 - op0)));
1186 /* Store the result and condition codes. */
1187 State.regs[OP[1]] = result;
1188 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1189 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1190 | (cy ? PSW_CY : 0));
1191 trace_output (OP_REG_REG);
1196 /* shr zero_extend(imm5),reg1 */
1200 unsigned int op0, op1, result, z, s, cy;
1202 trace_input ("shr", OP_IMM_REG, 0);
1204 op1 = State.regs[ OP[1] ];
1205 result = op1 >> op0;
1207 /* Compute the condition codes. */
1209 s = (result & 0x80000000);
1210 cy = (op1 & (1 << (op0 - 1)));
1212 /* Store the result and condition codes. */
1213 State.regs[OP[1]] = result;
1214 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1215 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1216 | (cy ? PSW_CY : 0));
1217 trace_output (OP_IMM_REG);
1222 /* shr reg1, reg2 */
1226 unsigned int op0, op1, result, z, s, cy;
1228 trace_input ("shr", OP_REG_REG, 0);
1229 op0 = State.regs[ OP[0] ] & 0x1f;
1230 op1 = State.regs[ OP[1] ];
1231 result = op1 >> op0;
1233 /* Compute the condition codes. */
1235 s = (result & 0x80000000);
1236 cy = (op1 & (1 << (op0 - 1)));
1238 /* Store the result and condition codes. */
1239 State.regs[OP[1]] = result;
1240 PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1241 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1242 | (cy ? PSW_CY : 0));
1243 trace_output (OP_REG_REG);
1252 unsigned int op0, op1, result, z, s;
1254 trace_input ("or", OP_REG_REG, 0);
1256 /* Compute the result. */
1257 op0 = State.regs[ OP[0] ];
1258 op1 = State.regs[ OP[1] ];
1261 /* Compute the condition codes. */
1263 s = (result & 0x80000000);
1265 /* Store the result and condition codes. */
1266 State.regs[OP[1]] = result;
1267 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1268 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1269 trace_output (OP_REG_REG);
1274 /* ori zero_extend(imm16), reg, reg */
1278 unsigned int op0, op1, result, z, s;
1280 trace_input ("ori", OP_UIMM16_REG_REG, 0);
1282 op1 = State.regs[ OP[0] ];
1285 /* Compute the condition codes. */
1287 s = (result & 0x80000000);
1289 /* Store the result and condition codes. */
1290 State.regs[OP[1]] = result;
1291 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1292 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1293 trace_output (OP_UIMM16_REG_REG);
1302 unsigned int op0, op1, result, z, s;
1304 trace_input ("and", OP_REG_REG, 0);
1306 /* Compute the result. */
1307 op0 = State.regs[ OP[0] ];
1308 op1 = State.regs[ OP[1] ];
1311 /* Compute the condition codes. */
1313 s = (result & 0x80000000);
1315 /* Store the result and condition codes. */
1316 State.regs[OP[1]] = result;
1317 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1318 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1319 trace_output (OP_REG_REG);
1324 /* andi zero_extend(imm16), reg, reg */
1328 unsigned int result, z;
1330 trace_input ("andi", OP_UIMM16_REG_REG, 0);
1332 result = OP[2] & State.regs[ OP[0] ];
1334 /* Compute the condition codes. */
1337 /* Store the result and condition codes. */
1338 State.regs[ OP[1] ] = result;
1340 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1341 PSW |= (z ? PSW_Z : 0);
1343 trace_output (OP_UIMM16_REG_REG);
1352 unsigned int op0, op1, result, z, s;
1354 trace_input ("xor", OP_REG_REG, 0);
1356 /* Compute the result. */
1357 op0 = State.regs[ OP[0] ];
1358 op1 = State.regs[ OP[1] ];
1361 /* Compute the condition codes. */
1363 s = (result & 0x80000000);
1365 /* Store the result and condition codes. */
1366 State.regs[OP[1]] = result;
1367 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1368 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1369 trace_output (OP_REG_REG);
1374 /* xori zero_extend(imm16), reg, reg */
1378 unsigned int op0, op1, result, z, s;
1380 trace_input ("xori", OP_UIMM16_REG_REG, 0);
1382 op1 = State.regs[ OP[0] ];
1385 /* Compute the condition codes. */
1387 s = (result & 0x80000000);
1389 /* Store the result and condition codes. */
1390 State.regs[OP[1]] = result;
1391 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1392 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1393 trace_output (OP_UIMM16_REG_REG);
1398 /* not reg1, reg2 */
1402 unsigned int op0, result, z, s;
1404 trace_input ("not", OP_REG_REG_MOVE, 0);
1405 /* Compute the result. */
1406 op0 = State.regs[ OP[0] ];
1409 /* Compute the condition codes. */
1411 s = (result & 0x80000000);
1413 /* Store the result and condition codes. */
1414 State.regs[OP[1]] = result;
1415 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1416 PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1417 trace_output (OP_REG_REG_MOVE);
1426 unsigned int op0, op1, op2;
1429 trace_input ("set1", OP_BIT, 0);
1430 op0 = State.regs[ OP[0] ];
1432 temp = EXTEND16 (OP[2]);
1434 temp = load_mem (op0 + op2, 1);
1436 if ((temp & (1 << op1)) == 0)
1439 store_mem (op0 + op2, 1, temp);
1440 trace_output (OP_BIT);
1449 unsigned int op0, op1, op2;
1452 trace_input ("not1", OP_BIT, 0);
1453 op0 = State.regs[ OP[0] ];
1455 temp = EXTEND16 (OP[2]);
1457 temp = load_mem (op0 + op2, 1);
1459 if ((temp & (1 << op1)) == 0)
1462 store_mem (op0 + op2, 1, temp);
1463 trace_output (OP_BIT);
1472 unsigned int op0, op1, op2;
1475 trace_input ("clr1", OP_BIT, 0);
1476 op0 = State.regs[ OP[0] ];
1478 temp = EXTEND16 (OP[2]);
1480 temp = load_mem (op0 + op2, 1);
1482 if ((temp & (1 << op1)) == 0)
1484 temp &= ~(1 << op1);
1485 store_mem (op0 + op2, 1, temp);
1486 trace_output (OP_BIT);
1495 unsigned int op0, op1, op2;
1498 trace_input ("tst1", OP_BIT, 0);
1499 op0 = State.regs[ OP[0] ];
1501 temp = EXTEND16 (OP[2]);
1503 temp = load_mem (op0 + op2, 1);
1505 if ((temp & (1 << op1)) == 0)
1507 trace_output (OP_BIT);
1516 trace_input ("di", OP_NONE, 0);
1518 trace_output (OP_NONE);
1527 trace_input ("ei", OP_NONE, 0);
1529 trace_output (OP_NONE);
1538 trace_input ("halt", OP_NONE, 0);
1539 /* FIXME this should put processor into a mode where NMI still handled */
1540 trace_output (OP_NONE);
1541 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1542 sim_stopped, SIM_SIGTRAP);
1550 trace_input ("trap", OP_TRAP, 0);
1551 trace_output (OP_TRAP);
1553 /* Trap 31 is used for simulating OS I/O functions */
1557 int save_errno = errno;
1560 /* Registers passed to trap 0 */
1562 #define FUNC State.regs[6] /* function number, return value */
1563 #define PARM1 State.regs[7] /* optional parm 1 */
1564 #define PARM2 State.regs[8] /* optional parm 2 */
1565 #define PARM3 State.regs[9] /* optional parm 3 */
1567 /* Registers set by trap 0 */
1569 #define RETVAL State.regs[10] /* return value */
1570 #define RETERR State.regs[11] /* return error code */
1572 /* Turn a pointer in a register into a pointer into real memory. */
1574 #define MEMPTR(x) (map (x))
1580 #ifdef TARGET_SYS_fork
1581 case TARGET_SYS_fork:
1588 #ifdef TARGET_SYS_execv
1589 case TARGET_SYS_execve:
1591 char *path = fetch_str (simulator, PARM1);
1592 char **argv = fetch_argv (simulator, PARM2);
1593 char **envp = fetch_argv (simulator, PARM3);
1594 RETVAL = execve (path, argv, envp);
1604 #ifdef TARGET_SYS_execv
1605 case TARGET_SYS_execv:
1607 char *path = fetch_str (simulator, PARM1);
1608 char **argv = fetch_argv (simulator, PARM2);
1609 RETVAL = execv (path, argv);
1618 #ifdef TARGET_SYS_pipe
1619 case TARGET_SYS_pipe:
1625 RETVAL = pipe (host_fd);
1626 SW (buf, host_fd[0]);
1627 buf += sizeof(uint16);
1628 SW (buf, host_fd[1]);
1635 #ifdef TARGET_SYS_wait
1636 case TARGET_SYS_wait:
1640 RETVAL = wait (&status);
1647 #ifdef TARGET_SYS_read
1648 case TARGET_SYS_read:
1650 char *buf = zalloc (PARM3);
1651 RETVAL = sim_io_read (simulator, PARM1, buf, PARM3);
1652 sim_write (simulator, PARM2, buf, PARM3);
1658 #ifdef TARGET_SYS_write
1659 case TARGET_SYS_write:
1661 char *buf = zalloc (PARM3);
1662 sim_read (simulator, PARM2, buf, PARM3);
1664 RETVAL = sim_io_write_stdout (simulator, buf, PARM3);
1666 RETVAL = sim_io_write (simulator, PARM1, buf, PARM3);
1672 #ifdef TARGET_SYS_lseek
1673 case TARGET_SYS_lseek:
1674 RETVAL = sim_io_lseek (simulator, PARM1, PARM2, PARM3);
1678 #ifdef TARGET_SYS_close
1679 case TARGET_SYS_close:
1680 RETVAL = sim_io_close (simulator, PARM1);
1684 #ifdef TARGET_SYS_open
1685 case TARGET_SYS_open:
1687 char *buf = fetch_str (simulator, PARM1);
1688 RETVAL = sim_io_open (simulator, buf, PARM2);
1694 #ifdef TARGET_SYS_exit
1695 case TARGET_SYS_exit:
1696 if ((PARM1 & 0xffff0000) == 0xdead0000 && (PARM1 & 0xffff) != 0)
1697 /* get signal encoded by kill */
1698 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1699 sim_signalled, PARM1 & 0xffff);
1700 else if (PARM1 == 0xdead)
1702 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1703 sim_stopped, SIM_SIGABRT);
1705 /* PARM1 has exit status */
1706 sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1711 #if !defined(__GO32__) && !defined(_WIN32)
1712 #ifdef TARGET_SYS_stat
1713 case TARGET_SYS_stat: /* added at hmsi */
1714 /* stat system call */
1716 struct stat host_stat;
1718 char *path = fetch_str (simulator, PARM1);
1720 RETVAL = stat (path, &host_stat);
1725 /* Just wild-assed guesses. */
1726 store_mem (buf, 2, host_stat.st_dev);
1727 store_mem (buf + 2, 2, host_stat.st_ino);
1728 store_mem (buf + 4, 4, host_stat.st_mode);
1729 store_mem (buf + 8, 2, host_stat.st_nlink);
1730 store_mem (buf + 10, 2, host_stat.st_uid);
1731 store_mem (buf + 12, 2, host_stat.st_gid);
1732 store_mem (buf + 14, 2, host_stat.st_rdev);
1733 store_mem (buf + 16, 4, host_stat.st_size);
1734 store_mem (buf + 20, 4, host_stat.st_atime);
1735 store_mem (buf + 28, 4, host_stat.st_mtime);
1736 store_mem (buf + 36, 4, host_stat.st_ctime);
1743 #ifdef TARGET_SYS_chown
1744 case TARGET_SYS_chown:
1746 char *path = fetch_str (simulator, PARM1);
1747 RETVAL = chown (path, PARM2, PARM3);
1755 #ifdef TARGET_SYS_chmod
1756 case TARGET_SYS_chmod:
1758 char *path = fetch_str (simulator, PARM1);
1759 RETVAL = chmod (path, PARM2);
1766 #ifdef TARGET_SYS_time
1768 case TARGET_SYS_time:
1771 RETVAL = time (&now);
1772 store_mem (PARM1, 4, now);
1778 #if !defined(__GO32__) && !defined(_WIN32)
1779 #ifdef TARGET_SYS_times
1780 case TARGET_SYS_times:
1783 RETVAL = times (&tms);
1784 store_mem (PARM1, 4, tms.tms_utime);
1785 store_mem (PARM1 + 4, 4, tms.tms_stime);
1786 store_mem (PARM1 + 8, 4, tms.tms_cutime);
1787 store_mem (PARM1 + 12, 4, tms.tms_cstime);
1793 #ifdef TARGET_SYS_gettimeofday
1794 #if !defined(__GO32__) && !defined(_WIN32)
1795 case TARGET_SYS_gettimeofday:
1799 RETVAL = gettimeofday (&t, &tz);
1800 store_mem (PARM1, 4, t.tv_sec);
1801 store_mem (PARM1 + 4, 4, t.tv_usec);
1802 store_mem (PARM2, 4, tz.tz_minuteswest);
1803 store_mem (PARM2 + 4, 4, tz.tz_dsttime);
1809 #ifdef TARGET_SYS_utime
1811 case TARGET_SYS_utime:
1813 /* Cast the second argument to void *, to avoid type mismatch
1814 if a prototype is present. */
1815 sim_io_error (simulator, "Utime not supported");
1816 /* RETVAL = utime (path, (void *) MEMPTR (PARM2)); */
1831 { /* Trap 0 -> 30 */
1836 ECR |= 0x40 + OP[0];
1837 /* Flag that we are now doing exception processing. */
1838 PSW |= PSW_EP | PSW_ID;
1839 PC = (OP[0] < 0x10) ? 0x40 : 0x50;
1845 /* tst1 reg2, [reg1] */
1851 trace_input ("tst1", OP_BIT, 1);
1853 temp = load_mem (State.regs[ OP[0] ], 1);
1856 if ((temp & (1 << (State.regs[ OP[1] ] & 0x7))) == 0)
1859 trace_output (OP_BIT);
1864 /* mulu reg1, reg2, reg3 */
1868 trace_input ("mulu", OP_REG_REG_REG, 0);
1870 Multiply64 (0, State.regs[ OP[0] ]);
1872 trace_output (OP_REG_REG_REG);
1877 #define BIT_CHANGE_OP( name, binop ) \
1879 unsigned int temp; \
1881 trace_input (name, OP_BIT_CHANGE, 0); \
1883 bit = 1 << (State.regs[ OP[1] ] & 0x7); \
1884 temp = load_mem (State.regs[ OP[0] ], 1); \
1887 if ((temp & bit) == 0) \
1891 store_mem (State.regs[ OP[0] ], 1, temp); \
1893 trace_output (OP_BIT_CHANGE); \
1897 /* clr1 reg2, [reg1] */
1901 BIT_CHANGE_OP ("clr1", &= ~ );
1904 /* not1 reg2, [reg1] */
1908 BIT_CHANGE_OP ("not1", ^= );
1915 BIT_CHANGE_OP ("set1", |= );
1922 trace_input ("sasf", OP_EX1, 0);
1924 State.regs[ OP[1] ] = (State.regs[ OP[1] ] << 1) | condition_met (OP[0]);
1926 trace_output (OP_EX1);
1931 /* This function is courtesy of Sugimoto at NEC, via Seow Tan
1932 (Soew_Tan@el.nec.com) */
1937 unsigned long int als,
1938 unsigned long int sfi,
1939 unsigned32 /*unsigned long int*/ * quotient_ptr,
1940 unsigned32 /*unsigned long int*/ * remainder_ptr,
1944 unsigned long ald = sfi >> (N - 1);
1945 unsigned long alo = als;
1950 unsigned int R1 = 1;
1951 unsigned int DBZ = (als == 0) ? 1 : 0;
1952 unsigned long alt = Q ? ~als : als;
1955 alo = ald + alt + Q;
1956 C = (((alt >> 31) & (ald >> 31))
1957 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
1960 R1 = (alo == 0) ? 0 : (R1 & Q);
1961 if ((S ^ (alo>>31)) && !C)
1966 sfi = (sfi << (32-N+1)) | Q;
1967 ald = (alo << 1) | (sfi >> 31);
1969 /* 2nd - N-1th Loop */
1970 for (i = 2; i < N; i++)
1972 alt = Q ? ~als : als;
1973 alo = ald + alt + Q;
1974 C = (((alt >> 31) & (ald >> 31))
1975 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
1978 R1 = (alo == 0) ? 0 : (R1 & Q);
1979 if ((S ^ (alo>>31)) && !C && !DBZ)
1984 sfi = (sfi << 1) | Q;
1985 ald = (alo << 1) | (sfi >> 31);
1989 alt = Q ? ~als : als;
1990 alo = ald + alt + Q;
1991 C = (((alt >> 31) & (ald >> 31))
1992 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
1995 R1 = (alo == 0) ? 0 : (R1 & Q);
1996 if ((S ^ (alo>>31)) && !C)
2001 * quotient_ptr = (sfi << 1) | Q;
2002 * remainder_ptr = Q ? alo : (alo + als);
2003 * overflow_ptr = DBZ | R1;
2006 /* This function is courtesy of Sugimoto at NEC, via Seow Tan (Soew_Tan@el.nec.com) */
2011 unsigned long int als,
2012 unsigned long int sfi,
2013 signed32 /*signed long int*/ * quotient_ptr,
2014 signed32 /*signed long int*/ * remainder_ptr,
2018 unsigned long ald = (signed long) sfi >> (N - 1);
2019 unsigned long alo = als;
2020 unsigned int SS = als >> 31;
2021 unsigned int SD = sfi >> 31;
2022 unsigned int R1 = 1;
2024 unsigned int DBZ = als == 0 ? 1 : 0;
2025 unsigned int Q = ~(SS ^ SD) & 1;
2029 unsigned long alt = Q ? ~als : als;
2034 alo = ald + alt + Q;
2035 C = (((alt >> 31) & (ald >> 31))
2036 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2038 R1 = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2040 sfi = (sfi << (32-N+1)) | Q;
2041 ald = (alo << 1) | (sfi >> 31);
2042 if ((alo >> 31) ^ (ald >> 31))
2047 /* 2nd - N-1th Loop */
2049 for (i = 2; i < N; i++)
2051 alt = Q ? ~als : als;
2052 alo = ald + alt + Q;
2053 C = (((alt >> 31) & (ald >> 31))
2054 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2056 R1 = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2058 sfi = (sfi << 1) | Q;
2059 ald = (alo << 1) | (sfi >> 31);
2060 if ((alo >> 31) ^ (ald >> 31))
2067 alt = Q ? ~als : als;
2068 alo = ald + alt + Q;
2069 C = (((alt >> 31) & (ald >> 31))
2070 | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2072 R1 = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2073 sfi = (sfi << (32-N+1));
2079 alt = Q ? ~als : als;
2080 alo = ald + alt + Q;
2082 R1 = R1 & ((~alo >> 31) ^ SD);
2083 if ((alo != 0) && ((Q ^ (SS ^ SD)) ^ R1)) alo = ald;
2085 ald = sfi = (long) ((sfi >> 1) | (SS ^ SD) << 31) >> (32-N-1) | Q;
2087 ald = sfi = sfi | Q;
2089 OV = DBZ | ((alo == 0) ? 0 : R1);
2091 * remainder_ptr = alo;
2094 if (((alo != 0) && ((SS ^ SD) ^ R1))
2095 || ((alo == 0) && (SS ^ R1)))
2100 OV = (DBZ | R1) ? OV : ((alo >> 31) & (~ald >> 31));
2102 * quotient_ptr = alo;
2103 * overflow_ptr = OV;
2106 /* sdivun imm5, reg1, reg2, reg3 */
2110 unsigned32 /*unsigned long int*/ quotient;
2111 unsigned32 /*unsigned long int*/ remainder;
2112 unsigned long int divide_by;
2113 unsigned long int divide_this;
2117 trace_input ("sdivun", OP_IMM_REG_REG_REG, 0);
2119 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2121 divide_by = State.regs[ OP[0] ];
2122 divide_this = State.regs[ OP[1] ] << imm5;
2124 divun (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2126 State.regs[ OP[1] ] = quotient;
2127 State.regs[ OP[2] >> 11 ] = remainder;
2129 /* Set condition codes. */
2130 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2132 if (overflow) PSW |= PSW_OV;
2133 if (quotient == 0) PSW |= PSW_Z;
2134 if (quotient & 0x80000000) PSW |= PSW_S;
2136 trace_output (OP_IMM_REG_REG_REG);
2141 /* sdivn imm5, reg1, reg2, reg3 */
2145 signed32 /*signed long int*/ quotient;
2146 signed32 /*signed long int*/ remainder;
2147 signed long int divide_by;
2148 signed long int divide_this;
2152 trace_input ("sdivn", OP_IMM_REG_REG_REG, 0);
2154 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2156 divide_by = State.regs[ OP[0] ];
2157 divide_this = State.regs[ OP[1] ] << imm5;
2159 divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2161 State.regs[ OP[1] ] = quotient;
2162 State.regs[ OP[2] >> 11 ] = remainder;
2164 /* Set condition codes. */
2165 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2167 if (overflow) PSW |= PSW_OV;
2168 if (quotient == 0) PSW |= PSW_Z;
2169 if (quotient < 0) PSW |= PSW_S;
2171 trace_output (OP_IMM_REG_REG_REG);
2176 /* sdivhun imm5, reg1, reg2, reg3 */
2180 unsigned32 /*unsigned long int*/ quotient;
2181 unsigned32 /*unsigned long int*/ remainder;
2182 unsigned long int divide_by;
2183 unsigned long int divide_this;
2187 trace_input ("sdivhun", OP_IMM_REG_REG_REG, 0);
2189 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2191 divide_by = State.regs[ OP[0] ] & 0xffff;
2192 divide_this = State.regs[ OP[1] ] << imm5;
2194 divun (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2196 State.regs[ OP[1] ] = quotient;
2197 State.regs[ OP[2] >> 11 ] = remainder;
2199 /* Set condition codes. */
2200 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2202 if (overflow) PSW |= PSW_OV;
2203 if (quotient == 0) PSW |= PSW_Z;
2204 if (quotient & 0x80000000) PSW |= PSW_S;
2206 trace_output (OP_IMM_REG_REG_REG);
2211 /* sdivhn imm5, reg1, reg2, reg3 */
2215 signed32 /*signed long int*/ quotient;
2216 signed32 /*signed long int*/ remainder;
2217 signed long int divide_by;
2218 signed long int divide_this;
2222 trace_input ("sdivhn", OP_IMM_REG_REG_REG, 0);
2224 imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2226 divide_by = EXTEND16 (State.regs[ OP[0] ]);
2227 divide_this = State.regs[ OP[1] ] << imm5;
2229 divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2231 State.regs[ OP[1] ] = quotient;
2232 State.regs[ OP[2] >> 11 ] = remainder;
2234 /* Set condition codes. */
2235 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2237 if (overflow) PSW |= PSW_OV;
2238 if (quotient == 0) PSW |= PSW_Z;
2239 if (quotient < 0) PSW |= PSW_S;
2241 trace_output (OP_IMM_REG_REG_REG);
2246 /* divu reg1, reg2, reg3 */
2250 unsigned long int quotient;
2251 unsigned long int remainder;
2252 unsigned long int divide_by;
2253 unsigned long int divide_this;
2256 trace_input ("divu", OP_REG_REG_REG, 0);
2258 /* Compute the result. */
2260 divide_by = State.regs[ OP[0] ];
2261 divide_this = State.regs[ OP[1] ];
2269 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2270 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2272 /* Set condition codes. */
2273 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2275 if (overflow) PSW |= PSW_OV;
2276 if (quotient == 0) PSW |= PSW_Z;
2277 if (quotient & 0x80000000) PSW |= PSW_S;
2279 trace_output (OP_REG_REG_REG);
2284 /* div reg1, reg2, reg3 */
2288 signed long int quotient;
2289 signed long int remainder;
2290 signed long int divide_by;
2291 signed long int divide_this;
2294 trace_input ("div", OP_REG_REG_REG, 0);
2296 /* Compute the result. */
2298 divide_by = State.regs[ OP[0] ];
2299 divide_this = State.regs[ OP[1] ];
2301 if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
2307 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2308 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2310 /* Set condition codes. */
2311 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2313 if (overflow) PSW |= PSW_OV;
2314 if (quotient == 0) PSW |= PSW_Z;
2315 if (quotient < 0) PSW |= PSW_S;
2317 trace_output (OP_REG_REG_REG);
2322 /* divhu reg1, reg2, reg3 */
2326 unsigned long int quotient;
2327 unsigned long int remainder;
2328 unsigned long int divide_by;
2329 unsigned long int divide_this;
2332 trace_input ("divhu", OP_REG_REG_REG, 0);
2334 /* Compute the result. */
2336 divide_by = State.regs[ OP[0] ] & 0xffff;
2337 divide_this = State.regs[ OP[1] ];
2345 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2346 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2348 /* Set condition codes. */
2349 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2351 if (overflow) PSW |= PSW_OV;
2352 if (quotient == 0) PSW |= PSW_Z;
2353 if (quotient & 0x80000000) PSW |= PSW_S;
2355 trace_output (OP_REG_REG_REG);
2360 /* divh reg1, reg2, reg3 */
2364 signed long int quotient;
2365 signed long int remainder;
2366 signed long int divide_by;
2367 signed long int divide_this;
2370 trace_input ("divh", OP_REG_REG_REG, 0);
2372 /* Compute the result. */
2374 divide_by = State.regs[ OP[0] ];
2375 divide_this = EXTEND16 (State.regs[ OP[1] ]);
2377 if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
2383 State.regs[ OP[1] ] = quotient = divide_this / divide_by;
2384 State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2386 /* Set condition codes. */
2387 PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2389 if (overflow) PSW |= PSW_OV;
2390 if (quotient == 0) PSW |= PSW_Z;
2391 if (quotient < 0) PSW |= PSW_S;
2393 trace_output (OP_REG_REG_REG);
2398 /* mulu imm9, reg2, reg3 */
2402 trace_input ("mulu", OP_IMM_REG_REG, 0);
2404 Multiply64 (0, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
2406 trace_output (OP_IMM_REG_REG);
2411 /* mul imm9, reg2, reg3 */
2415 trace_input ("mul", OP_IMM_REG_REG, 0);
2417 Multiply64 (1, SEXT9 ((OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0)));
2419 trace_output (OP_IMM_REG_REG);
2430 trace_input ("ld.hu", OP_LOAD32, 2);
2432 adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
2435 State.regs[ OP[1] ] = load_mem (adr, 2);
2437 trace_output (OP_LOAD32);
2449 trace_input ("ld.bu", OP_LOAD32, 1);
2451 adr = (State.regs[ OP[0] ]
2452 + (EXTEND16 (OP[2] & ~1) | ((OP[3] >> 5) & 1)));
2454 State.regs[ OP[1] ] = load_mem (adr, 1);
2456 trace_output (OP_LOAD32);
2461 /* prepare list12, imm5, imm32 */
2467 trace_input ("prepare", OP_PUSHPOP1, 0);
2469 /* Store the registers with lower number registers being placed at higher addresses. */
2470 for (i = 0; i < 12; i++)
2471 if ((OP[3] & (1 << type1_regs[ i ])))
2474 store_mem (SP, 4, State.regs[ 20 + i ]);
2477 SP -= (OP[3] & 0x3e) << 1;
2479 EP = load_mem (PC + 4, 4);
2481 trace_output (OP_PUSHPOP1);
2486 /* prepare list12, imm5, imm16-32 */
2492 trace_input ("prepare", OP_PUSHPOP1, 0);
2494 /* Store the registers with lower number registers being placed at higher addresses. */
2495 for (i = 0; i < 12; i++)
2496 if ((OP[3] & (1 << type1_regs[ i ])))
2499 store_mem (SP, 4, State.regs[ 20 + i ]);
2502 SP -= (OP[3] & 0x3e) << 1;
2504 EP = load_mem (PC + 4, 2) << 16;
2506 trace_output (OP_PUSHPOP1);
2511 /* prepare list12, imm5, imm16 */
2517 trace_input ("prepare", OP_PUSHPOP1, 0);
2519 /* Store the registers with lower number registers being placed at higher addresses. */
2520 for (i = 0; i < 12; i++)
2521 if ((OP[3] & (1 << type1_regs[ i ])))
2524 store_mem (SP, 4, State.regs[ 20 + i ]);
2527 SP -= (OP[3] & 0x3e) << 1;
2529 EP = EXTEND16 (load_mem (PC + 4, 2));
2531 trace_output (OP_PUSHPOP1);
2536 /* prepare list12, imm5, sp */
2542 trace_input ("prepare", OP_PUSHPOP1, 0);
2544 /* Store the registers with lower number registers being placed at higher addresses. */
2545 for (i = 0; i < 12; i++)
2546 if ((OP[3] & (1 << type1_regs[ i ])))
2549 store_mem (SP, 4, State.regs[ 20 + i ]);
2552 SP -= (OP[3] & 0x3e) << 1;
2556 trace_output (OP_PUSHPOP1);
2561 /* mul reg1, reg2, reg3 */
2565 trace_input ("mul", OP_REG_REG_REG, 0);
2567 Multiply64 (1, State.regs[ OP[0] ]);
2569 trace_output (OP_REG_REG_REG);
2580 trace_input ("popmh", OP_PUSHPOP2, 0);
2582 if (OP[3] & (1 << 19))
2584 if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
2586 FEPSW = load_mem ( SP & ~ 3, 4);
2587 FEPC = load_mem ((SP + 4) & ~ 3, 4);
2591 EIPSW = load_mem ( SP & ~ 3, 4);
2592 EIPC = load_mem ((SP + 4) & ~ 3, 4);
2598 /* Load the registers with lower number registers being retrieved from higher addresses. */
2600 if ((OP[3] & (1 << type2_regs[ i ])))
2602 State.regs[ i + 16 ] = load_mem (SP & ~ 3, 4);
2606 trace_output (OP_PUSHPOP2);
2617 trace_input ("popml", OP_PUSHPOP3, 0);
2619 if (OP[3] & (1 << 19))
2621 if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
2623 FEPSW = load_mem ( SP & ~ 3, 4);
2624 FEPC = load_mem ((SP + 4) & ~ 3, 4);
2628 EIPSW = load_mem ( SP & ~ 3, 4);
2629 EIPC = load_mem ((SP + 4) & ~ 3, 4);
2635 if (OP[3] & (1 << 3))
2637 PSW = load_mem (SP & ~ 3, 4);
2641 /* Load the registers with lower number registers being retrieved from higher addresses. */
2643 if ((OP[3] & (1 << type3_regs[ i ])))
2645 State.regs[ i + 1 ] = load_mem (SP & ~ 3, 4);
2649 trace_output (OP_PUSHPOP2);
2660 trace_input ("pushmh", OP_PUSHPOP2, 0);
2662 /* Store the registers with lower number registers being placed at higher addresses. */
2663 for (i = 0; i < 16; i++)
2664 if ((OP[3] & (1 << type2_regs[ i ])))
2667 store_mem (SP & ~ 3, 4, State.regs[ i + 16 ]);
2670 if (OP[3] & (1 << 19))
2674 if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
2676 store_mem ((SP + 4) & ~ 3, 4, FEPC);
2677 store_mem ( SP & ~ 3, 4, FEPSW);
2681 store_mem ((SP + 4) & ~ 3, 4, EIPC);
2682 store_mem ( SP & ~ 3, 4, EIPSW);
2686 trace_output (OP_PUSHPOP2);