* simops.c (OP_40): Delete. Move code to...
[external/binutils.git] / sim / v850 / simops.c
1 #include "sim-main.h"
2 #include "v850_sim.h"
3 #include "simops.h"
4
5 #ifdef HAVE_UTIME_H
6 #include <utime.h>
7 #endif
8
9 #ifdef HAVE_TIME_H
10 #include <time.h>
11 #endif
12
13 #ifdef HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16
17 #ifdef HAVE_STRING_H
18 #include <string.h>
19 #else
20 #ifdef HAVE_STRINGS_H
21 #include <strings.h>
22 #endif
23 #endif
24
25 #include "targ-vals.h"
26
27 #include "libiberty.h"
28
29 #include <errno.h>
30 #if !defined(__GO32__) && !defined(_WIN32)
31 #include <sys/stat.h>
32 #include <sys/times.h>
33 #include <sys/time.h>
34 #endif
35
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};
45
46 #ifdef DEBUG
47 #ifndef SIZE_INSTRUCTION
48 #define SIZE_INSTRUCTION 18
49 #endif
50
51 #ifndef SIZE_VALUES
52 #define SIZE_VALUES 11
53 #endif
54
55
56 unsigned32 trace_values[3];
57 int trace_num_values;
58 unsigned32 trace_pc;
59 const char *trace_name;
60 int trace_module;
61
62
63 void
64 trace_input (name, type, size)
65      char *name;
66      enum op_types type;
67      int size;
68 {
69
70   if (!TRACE_ALU_P (STATE_CPU (simulator, 0)))
71     return;
72
73   trace_pc = PC;
74   trace_name = name;
75   trace_module = TRACE_ALU_IDX;
76
77   switch (type)
78     {
79     default:
80     case OP_UNKNOWN:
81     case OP_NONE:
82     case OP_TRAP:
83       trace_num_values = 0;
84       break;
85       
86     case OP_REG:
87     case OP_REG_REG_MOVE:
88       trace_values[0] = State.regs[OP[0]];
89       trace_num_values = 1;
90       break;
91       
92     case OP_BIT_CHANGE:
93     case OP_REG_REG:
94     case OP_REG_REG_CMP:
95       trace_values[0] = State.regs[OP[1]];
96       trace_values[1] = State.regs[OP[0]];
97       trace_num_values = 2;
98       break;
99       
100     case OP_IMM_REG:
101     case OP_IMM_REG_CMP:
102       trace_values[0] = SEXT5 (OP[0]);
103       trace_values[1] = OP[1];
104       trace_num_values = 2;
105       break;
106       
107     case OP_IMM_REG_MOVE:
108       trace_values[0] = SEXT5 (OP[0]);
109       trace_num_values = 1;
110       break;
111       
112     case OP_COND_BR:
113       trace_values[0] = State.pc;
114       trace_values[1] = SEXT9 (OP[0]);
115       trace_values[2] = PSW;
116       trace_num_values = 3;
117       break;
118       
119     case OP_LOAD16:
120       trace_values[0] = OP[1] * size;
121       trace_values[1] = State.regs[30];
122       trace_num_values = 2;
123       break;
124       
125     case OP_STORE16:
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;
130       break;
131       
132     case OP_LOAD32:
133       trace_values[0] = EXTEND16 (OP[2]);
134       trace_values[1] = State.regs[OP[0]];
135       trace_num_values = 2;
136       break;
137       
138     case OP_STORE32:
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;
143       break;
144       
145     case OP_JUMP:
146       trace_values[0] = SEXT22 (OP[0]);
147       trace_values[1] = State.pc;
148       trace_num_values = 2;
149       break;
150       
151     case OP_IMM_REG_REG:
152       trace_values[0] = EXTEND16 (OP[0]) << size;
153       trace_values[1] = State.regs[OP[1]];
154       trace_num_values = 2;
155       break;
156       
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;
161       break;
162       
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;
167       break;
168       
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;
173       break;
174       
175     case OP_BIT:
176       trace_num_values = 0;
177       break;
178       
179     case OP_EX1:
180       trace_values[0] = PSW;
181       trace_num_values = 1;
182       break;
183       
184     case OP_EX2:
185       trace_num_values = 0;
186       break;
187       
188     case OP_LDSR:
189       trace_values[0] = State.regs[OP[0]];
190       trace_num_values = 1;
191       break;
192       
193     case OP_STSR:
194       trace_values[0] = State.sregs[OP[1]];
195       trace_num_values = 1;
196     }
197   
198 }
199
200 void
201 trace_result (int has_result, unsigned32 result)
202 {
203   char buf[1000];
204   char *chp;
205
206   buf[0] = '\0';
207   chp = buf;
208
209   /* write out the values saved during the trace_input call */
210   {
211     int i;
212     for (i = 0; i < trace_num_values; i++)
213       {
214         sprintf (chp, "%*s0x%.8lx", SIZE_VALUES - 10, "",
215                  (long) trace_values[i]);
216         chp = strchr (chp, '\0');
217       }
218     while (i++ < 3)
219       {
220         sprintf (chp, "%*s", SIZE_VALUES, "");
221         chp = strchr (chp, '\0');
222       }
223   }
224
225   /* append any result to the end of the buffer */
226   if (has_result)
227     sprintf (chp, " :: 0x%.8lx", (unsigned long)result);
228   
229   trace_generic (simulator, STATE_CPU (simulator, 0), trace_module, buf);
230 }
231
232 void
233 trace_output (result)
234      enum op_types result;
235 {
236   if (!TRACE_ALU_P (STATE_CPU (simulator, 0)))
237     return;
238
239   switch (result)
240     {
241     default:
242     case OP_UNKNOWN:
243     case OP_NONE:
244     case OP_TRAP:
245     case OP_REG:
246     case OP_REG_REG_CMP:
247     case OP_IMM_REG_CMP:
248     case OP_COND_BR:
249     case OP_STORE16:
250     case OP_STORE32:
251     case OP_BIT:
252     case OP_EX2:
253       trace_result (0, 0);
254       break;
255       
256     case OP_LOAD16:
257     case OP_STSR:
258       trace_result (1, State.regs[OP[0]]);
259       break;
260       
261     case OP_REG_REG:
262     case OP_REG_REG_MOVE:
263     case OP_IMM_REG:
264     case OP_IMM_REG_MOVE:
265     case OP_LOAD32:
266     case OP_EX1:
267       trace_result (1, State.regs[OP[1]]);
268       break;
269       
270     case OP_IMM_REG_REG:
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]]);
275       break;
276       
277     case OP_JUMP:
278       if (OP[1] != 0)
279         trace_result (1, State.regs[OP[1]]);
280       else
281         trace_result (0, 0);
282       break;
283       
284     case OP_LDSR:
285       trace_result (1, State.sregs[OP[1]]);
286       break;
287     }
288 }
289 #endif
290
291 \f
292 /* Returns 1 if the specific condition is met, returns 0 otherwise.  */
293 int
294 condition_met (unsigned code)
295 {
296   unsigned int psw = PSW;
297
298   switch (code & 0xf)
299     {
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);
316     }
317   
318   return 1;
319 }
320
321 static unsigned long
322 Add32 (unsigned long a1, unsigned long a2, int * carry)
323 {
324   unsigned long result = (a1 + a2);
325
326   * carry = (result < a1);
327
328   return result;
329 }
330
331 static void
332 Multiply64 (int sign, unsigned long op0)
333 {
334   unsigned long op1;
335   unsigned long lo;
336   unsigned long mid1;
337   unsigned long mid2;
338   unsigned long hi;
339   unsigned long RdLo;
340   unsigned long RdHi;
341   int           carry;
342   
343   op1 = State.regs[ OP[1] ];
344
345   if (sign)
346     {
347       /* Compute sign of result and adjust operands if necessary.  */
348           
349       sign = (op0 ^ op1) & 0x80000000;
350           
351       if (((signed long) op0) < 0)
352         op0 = - op0;
353           
354       if (((signed long) op1) < 0)
355         op1 = - op1;
356     }
357       
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));
364   
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);
368   RdHi = carry;
369   RdLo = Add32 (RdLo, (mid2 << 16), & carry);
370   RdHi += (carry + ((mid1 >> 16) & 0xFFFF) + ((mid2 >> 16) & 0xFFFF) + hi);
371
372   if (sign)
373     {
374       /* Negate result if necessary.  */
375       
376       RdLo = ~ RdLo;
377       RdHi = ~ RdHi;
378       if (RdLo == 0xFFFFFFFF)
379         {
380           RdLo = 0;
381           RdHi += 1;
382         }
383       else
384         RdLo += 1;
385     }
386   
387   /* Don't store into register 0.  */
388   if (OP[1])
389     State.regs[ OP[1]       ] = RdLo;
390   if (OP[2] >> 11)
391     State.regs[ OP[2] >> 11 ] = RdHi;
392
393   return;
394 }
395
396 \f
397 /* Read a null terminated string from memory, return in a buffer */
398 static char *
399 fetch_str (sd, addr)
400      SIM_DESC sd;
401      address_word addr;
402 {
403   char *buf;
404   int nr = 0;
405   while (sim_core_read_1 (STATE_CPU (sd, 0),
406                           PC, read_map, addr + nr) != 0)
407     nr++;
408   buf = NZALLOC (char, nr + 1);
409   sim_read (simulator, addr, buf, nr);
410   return buf;
411 }
412
413 /* Read a null terminated argument vector from memory, return in a
414    buffer */
415 static char **
416 fetch_argv (sd, addr)
417      SIM_DESC sd;
418      address_word addr;
419 {
420   int max_nr = 64;
421   int nr = 0;
422   char **buf = xmalloc (max_nr * sizeof (char*));
423   while (1)
424     {
425       unsigned32 a = sim_core_read_4 (STATE_CPU (sd, 0),
426                                       PC, read_map, addr + nr * 4);
427       if (a == 0) break;
428       buf[nr] = fetch_str (sd, a);
429       nr ++;
430       if (nr == max_nr - 1)
431         {
432           max_nr += 50;
433           buf = xrealloc (buf, max_nr * sizeof (char*));
434         }
435     }
436   buf[nr] = 0;
437   return buf;
438 }
439
440 \f
441 /* sst.b */
442 int
443 OP_380 ()
444 {
445   trace_input ("sst.b", OP_STORE16, 1);
446
447   store_mem (State.regs[30] + (OP[3] & 0x7f), 1, State.regs[ OP[1] ]);
448   
449   trace_output (OP_STORE16);
450
451   return 2;
452 }
453
454 /* sst.h */
455 int
456 OP_480 ()
457 {
458   trace_input ("sst.h", OP_STORE16, 2);
459
460   store_mem (State.regs[30] + ((OP[3] & 0x7f) << 1), 2, State.regs[ OP[1] ]);
461   
462   trace_output (OP_STORE16);
463
464   return 2;
465 }
466
467 /* sst.w */
468 int
469 OP_501 ()
470 {
471   trace_input ("sst.w", OP_STORE16, 4);
472
473   store_mem (State.regs[30] + ((OP[3] & 0x7e) << 1), 4, State.regs[ OP[1] ]);
474   
475   trace_output (OP_STORE16);
476
477   return 2;
478 }
479
480 /* ld.b */
481 int
482 OP_700 ()
483 {
484   int adr;
485
486   trace_input ("ld.b", OP_LOAD32, 1);
487
488   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
489
490   State.regs[ OP[1] ] = EXTEND8 (load_mem (adr, 1));
491   
492   trace_output (OP_LOAD32);
493
494   return 4;
495 }
496
497 /* ld.h */
498 int
499 OP_720 ()
500 {
501   int adr;
502
503   trace_input ("ld.h", OP_LOAD32, 2);
504
505   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
506   adr &= ~0x1;
507   
508   State.regs[ OP[1] ] = EXTEND16 (load_mem (adr, 2));
509   
510   trace_output (OP_LOAD32);
511
512   return 4;
513 }
514
515 /* ld.w */
516 int
517 OP_10720 ()
518 {
519   int adr;
520
521   trace_input ("ld.w", OP_LOAD32, 4);
522
523   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
524   adr &= ~0x3;
525   
526   State.regs[ OP[1] ] = load_mem (adr, 4);
527   
528   trace_output (OP_LOAD32);
529
530   return 4;
531 }
532
533 /* st.b */
534 int
535 OP_740 ()
536 {
537   trace_input ("st.b", OP_STORE32, 1);
538
539   store_mem (State.regs[ OP[0] ] + EXTEND16 (OP[2]), 1, State.regs[ OP[1] ]);
540   
541   trace_output (OP_STORE32);
542
543   return 4;
544 }
545
546 /* st.h */
547 int
548 OP_760 ()
549 {
550   int adr;
551   
552   trace_input ("st.h", OP_STORE32, 2);
553
554   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
555   adr &= ~1;
556   
557   store_mem (adr, 2, State.regs[ OP[1] ]);
558   
559   trace_output (OP_STORE32);
560
561   return 4;
562 }
563
564 /* st.w */
565 int
566 OP_10760 ()
567 {
568   int adr;
569   
570   trace_input ("st.w", OP_STORE32, 4);
571
572   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
573   adr &= ~3;
574   
575   store_mem (adr, 4, State.regs[ OP[1] ]);
576   
577   trace_output (OP_STORE32);
578
579   return 4;
580 }
581
582 /* add reg, reg */
583 int
584 OP_1C0 ()
585 {
586   unsigned int op0, op1, result, z, s, cy, ov;
587
588   trace_input ("add", OP_REG_REG, 0);
589   
590   /* Compute the result.  */
591   
592   op0 = State.regs[ OP[0] ];
593   op1 = State.regs[ OP[1] ];
594   
595   result = op0 + op1;
596
597   /* Compute the condition codes.  */
598   z = (result == 0);
599   s = (result & 0x80000000);
600   cy = (result < op0 || result < op1);
601   ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
602         && (op0 & 0x80000000) != (result & 0x80000000));
603
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);
610
611   return 2;
612 }
613
614 /* add sign_extend(imm5), reg */
615 int
616 OP_240 ()
617 {
618   unsigned int op0, op1, result, z, s, cy, ov;
619   int temp;
620
621   trace_input ("add", OP_IMM_REG, 0);
622
623   /* Compute the result.  */
624   temp = SEXT5 (OP[0]);
625   op0 = temp;
626   op1 = State.regs[OP[1]];
627   result = op0 + op1;
628   
629   /* Compute the condition codes.  */
630   z = (result == 0);
631   s = (result & 0x80000000);
632   cy = (result < op0 || result < op1);
633   ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
634         && (op0 & 0x80000000) != (result & 0x80000000));
635
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);
642
643   return 2;
644 }
645
646 /* addi sign_extend(imm16), reg, reg */
647 int
648 OP_600 ()
649 {
650   unsigned int op0, op1, result, z, s, cy, ov;
651
652   trace_input ("addi", OP_IMM16_REG_REG, 0);
653
654   /* Compute the result.  */
655
656   op0 = EXTEND16 (OP[2]);
657   op1 = State.regs[ OP[0] ];
658   result = op0 + op1;
659   
660   /* Compute the condition codes.  */
661   z = (result == 0);
662   s = (result & 0x80000000);
663   cy = (result < op0 || result < op1);
664   ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
665         && (op0 & 0x80000000) != (result & 0x80000000));
666
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);
673
674   return 4;
675 }
676
677 /* sub reg1, reg2 */
678 int
679 OP_1A0 ()
680 {
681   unsigned int op0, op1, result, z, s, cy, ov;
682
683   trace_input ("sub", OP_REG_REG, 0);
684   /* Compute the result.  */
685   op0 = State.regs[ OP[0] ];
686   op1 = State.regs[ OP[1] ];
687   result = op1 - op0;
688
689   /* Compute the condition codes.  */
690   z = (result == 0);
691   s = (result & 0x80000000);
692   cy = (op1 < op0);
693   ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
694         && (op1 & 0x80000000) != (result & 0x80000000));
695
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);
702
703   return 2;
704 }
705
706 /* subr reg1, reg2 */
707 int
708 OP_180 ()
709 {
710   unsigned int op0, op1, result, z, s, cy, ov;
711
712   trace_input ("subr", OP_REG_REG, 0);
713   /* Compute the result.  */
714   op0 = State.regs[ OP[0] ];
715   op1 = State.regs[ OP[1] ];
716   result = op0 - op1;
717
718   /* Compute the condition codes.  */
719   z = (result == 0);
720   s = (result & 0x80000000);
721   cy = (op0 < op1);
722   ov = ((op0 & 0x80000000) != (op1 & 0x80000000)
723         && (op0 & 0x80000000) != (result & 0x80000000));
724
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);
731
732   return 2;
733 }
734
735 /* sxh reg1 */
736 int
737 OP_E0 ()
738 {
739   trace_input ("mulh", OP_REG_REG, 0);
740       
741   State.regs[ OP[1] ] = (EXTEND16 (State.regs[ OP[1] ]) * EXTEND16 (State.regs[ OP[0] ]));
742       
743   trace_output (OP_REG_REG);
744
745   return 2;
746 }
747
748 /* mulh sign_extend(imm5), reg2 */
749 int
750 OP_2E0 ()
751 {
752   trace_input ("mulh", OP_IMM_REG, 0);
753   
754   State.regs[ OP[1] ] = EXTEND16 (State.regs[ OP[1] ]) * SEXT5 (OP[0]);
755   
756   trace_output (OP_IMM_REG);
757
758   return 2;
759 }
760
761 /* mulhi imm16, reg1, reg2 */
762 int
763 OP_6E0 ()
764 {
765   trace_input ("mulhi", OP_IMM16_REG_REG, 0);
766   
767   State.regs[ OP[1] ] = EXTEND16 (State.regs[ OP[0] ]) * EXTEND16 (OP[2]);
768       
769   trace_output (OP_IMM16_REG_REG);
770   
771   return 4;
772 }
773
774 /* cmp reg, reg */
775 int
776 OP_1E0 ()
777 {
778   unsigned int op0, op1, result, z, s, cy, ov;
779
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] ];
784   result = op1 - op0;
785
786   /* Compute the condition codes.  */
787   z = (result == 0);
788   s = (result & 0x80000000);
789   cy = (op1 < op0);
790   ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
791         && (op1 & 0x80000000) != (result & 0x80000000));
792
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);
798
799   return 2;
800 }
801
802 /* cmp sign_extend(imm5), reg */
803 int
804 OP_260 ()
805 {
806   unsigned int op0, op1, result, z, s, cy, ov;
807   int temp;
808
809   /* Compute the result.  */
810   trace_input ("cmp", OP_IMM_REG_CMP, 0);
811   temp = SEXT5 (OP[0]);
812   op0 = temp;
813   op1 = State.regs[OP[1]];
814   result = op1 - op0;
815
816   /* Compute the condition codes.  */
817   z = (result == 0);
818   s = (result & 0x80000000);
819   cy = (op1 < op0);
820   ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
821         && (op1 & 0x80000000) != (result & 0x80000000));
822
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);
828
829   return 2;
830 }
831
832 /* setf cccc,reg2 */
833 int
834 OP_7E0 ()
835 {
836   trace_input ("setf", OP_EX1, 0);
837
838   State.regs[ OP[1] ] = condition_met (OP[0]);
839   
840   trace_output (OP_EX1);
841
842   return 4;
843 }
844
845 /* satadd reg,reg */
846 int
847 OP_C0 ()
848 {
849   unsigned int op0, op1, result, z, s, cy, ov, sat;
850   
851   trace_input ("satadd", OP_REG_REG, 0);
852   /* Compute the result.  */
853   op0 = State.regs[ OP[0] ];
854   op1 = State.regs[ OP[1] ];
855   result = op0 + op1;
856   
857   /* Compute the condition codes.  */
858   z = (result == 0);
859   s = (result & 0x80000000);
860   cy = (result < op0 || result < op1);
861   ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
862         && (op0 & 0x80000000) != (result & 0x80000000));
863   sat = ov;
864   
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));
871   
872   /* Handle saturated results.  */
873   if (sat && s)
874     State.regs[OP[1]] = 0x80000000;
875   else if (sat)
876     State.regs[OP[1]] = 0x7fffffff;
877   trace_output (OP_REG_REG);
878
879   return 2;
880 }
881
882 /* satadd sign_extend(imm5), reg */
883 int
884 OP_220 ()
885 {
886   unsigned int op0, op1, result, z, s, cy, ov, sat;
887
888   int temp;
889
890   trace_input ("satadd", OP_IMM_REG, 0);
891
892   /* Compute the result.  */
893   temp = SEXT5 (OP[0]);
894   op0 = temp;
895   op1 = State.regs[OP[1]];
896   result = op0 + op1;
897
898   /* Compute the condition codes.  */
899   z = (result == 0);
900   s = (result & 0x80000000);
901   cy = (result < op0 || result < op1);
902   ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
903         && (op0 & 0x80000000) != (result & 0x80000000));
904   sat = ov;
905
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));
912
913   /* Handle saturated results.  */
914   if (sat && s)
915     State.regs[OP[1]] = 0x80000000;
916   else if (sat)
917     State.regs[OP[1]] = 0x7fffffff;
918   trace_output (OP_IMM_REG);
919
920   return 2;
921 }
922
923 /* satsub reg1, reg2 */
924 int
925 OP_A0 ()
926 {
927   unsigned int op0, op1, result, z, s, cy, ov, sat;
928   
929   trace_input ("satsub", OP_REG_REG, 0);
930   
931   /* Compute the result.  */
932   op0 = State.regs[ OP[0] ];
933   op1 = State.regs[ OP[1] ];
934   result = op1 - op0;
935   
936   /* Compute the condition codes.  */
937   z = (result == 0);
938   s = (result & 0x80000000);
939   cy = (op1 < op0);
940   ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
941         && (op1 & 0x80000000) != (result & 0x80000000));
942   sat = ov;
943   
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));
950   
951   /* Handle saturated results.  */
952   if (sat && s)
953     State.regs[OP[1]] = 0x80000000;
954   else if (sat)
955     State.regs[OP[1]] = 0x7fffffff;
956   trace_output (OP_REG_REG);
957   return 2;
958 }
959
960 /* satsubi sign_extend(imm16), reg */
961 int
962 OP_660 ()
963 {
964   unsigned int op0, op1, result, z, s, cy, ov, sat;
965   int temp;
966
967   trace_input ("satsubi", OP_IMM_REG, 0);
968
969   /* Compute the result.  */
970   temp = EXTEND16 (OP[2]);
971   op0 = temp;
972   op1 = State.regs[ OP[0] ];
973   result = op1 - op0;
974
975   /* Compute the condition codes.  */
976   z = (result == 0);
977   s = (result & 0x80000000);
978   cy = (op1 < op0);
979   ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
980         && (op1 & 0x80000000) != (result & 0x80000000));
981   sat = ov;
982
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));
989
990   /* Handle saturated results.  */
991   if (sat && s)
992     State.regs[OP[1]] = 0x80000000;
993   else if (sat)
994     State.regs[OP[1]] = 0x7fffffff;
995   trace_output (OP_IMM_REG);
996
997   return 4;
998 }
999
1000 /* satsubr reg,reg */
1001 int
1002 OP_80 ()
1003 {
1004   unsigned int op0, op1, result, z, s, cy, ov, sat;
1005   
1006   trace_input ("satsubr", OP_REG_REG, 0);
1007   
1008   /* Compute the result.  */
1009   op0 = State.regs[ OP[0] ];
1010   op1 = State.regs[ OP[1] ];
1011   result = op0 - op1;
1012   
1013   /* Compute the condition codes.  */
1014   z = (result == 0);
1015   s = (result & 0x80000000);
1016   cy = (result < op0);
1017   ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1018         && (op1 & 0x80000000) != (result & 0x80000000));
1019   sat = ov;
1020   
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));
1027   
1028   /* Handle saturated results.  */
1029   if (sat && s)
1030     State.regs[OP[1]] = 0x80000000;
1031   else if (sat)
1032     State.regs[OP[1]] = 0x7fffffff;
1033   trace_output (OP_REG_REG);
1034
1035   return 2;
1036 }
1037
1038 /* tst reg,reg */
1039 int
1040 OP_160 ()
1041 {
1042   unsigned int op0, op1, result, z, s;
1043
1044   trace_input ("tst", OP_REG_REG_CMP, 0);
1045
1046   /* Compute the result.  */
1047   op0 = State.regs[ OP[0] ];
1048   op1 = State.regs[ OP[1] ];
1049   result = op0 & op1;
1050
1051   /* Compute the condition codes.  */
1052   z = (result == 0);
1053   s = (result & 0x80000000);
1054
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);
1059
1060   return 2;
1061 }
1062
1063 /* mov sign_extend(imm5), reg */
1064 int
1065 OP_200 ()
1066 {
1067   int value = SEXT5 (OP[0]);
1068   
1069   trace_input ("mov", OP_IMM_REG_MOVE, 0);
1070   
1071   State.regs[ OP[1] ] = value;
1072   
1073   trace_output (OP_IMM_REG_MOVE);
1074   
1075   return 2;
1076 }
1077
1078 /* movhi imm16, reg, reg */
1079 int
1080 OP_640 ()
1081 {
1082   trace_input ("movhi", OP_UIMM16_REG_REG, 16);
1083       
1084   State.regs[ OP[1] ] = State.regs[ OP[0] ] + (OP[2] << 16);
1085       
1086   trace_output (OP_UIMM16_REG_REG);
1087
1088   return 4;
1089 }
1090
1091 /* sar zero_extend(imm5),reg1 */
1092 int
1093 OP_2A0 ()
1094 {
1095   unsigned int op0, op1, result, z, s, cy;
1096
1097   trace_input ("sar", OP_IMM_REG, 0);
1098   op0 = OP[0];
1099   op1 = State.regs[ OP[1] ];
1100   result = (signed)op1 >> op0;
1101
1102   /* Compute the condition codes.  */
1103   z = (result == 0);
1104   s = (result & 0x80000000);
1105   cy = (op1 & (1 << (op0 - 1)));
1106
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);
1113
1114   return 2;
1115 }
1116
1117 /* sar reg1, reg2 */
1118 int
1119 OP_A007E0 ()
1120 {
1121   unsigned int op0, op1, result, z, s, cy;
1122
1123   trace_input ("sar", OP_REG_REG, 0);
1124   
1125   op0 = State.regs[ OP[0] ] & 0x1f;
1126   op1 = State.regs[ OP[1] ];
1127   result = (signed)op1 >> op0;
1128
1129   /* Compute the condition codes.  */
1130   z = (result == 0);
1131   s = (result & 0x80000000);
1132   cy = (op1 & (1 << (op0 - 1)));
1133
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);
1140
1141   return 4;
1142 }
1143
1144 /* shl zero_extend(imm5),reg1 */
1145 int
1146 OP_2C0 ()
1147 {
1148   unsigned int op0, op1, result, z, s, cy;
1149
1150   trace_input ("shl", OP_IMM_REG, 0);
1151   op0 = OP[0];
1152   op1 = State.regs[ OP[1] ];
1153   result = op1 << op0;
1154
1155   /* Compute the condition codes.  */
1156   z = (result == 0);
1157   s = (result & 0x80000000);
1158   cy = (op1 & (1 << (32 - op0)));
1159
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);
1166
1167   return 2;
1168 }
1169
1170 /* shl reg1, reg2 */
1171 int
1172 OP_C007E0 ()
1173 {
1174   unsigned int op0, op1, result, z, s, cy;
1175
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;
1180
1181   /* Compute the condition codes.  */
1182   z = (result == 0);
1183   s = (result & 0x80000000);
1184   cy = (op1 & (1 << (32 - op0)));
1185
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);
1192
1193   return 4;
1194 }
1195
1196 /* shr zero_extend(imm5),reg1 */
1197 int
1198 OP_280 ()
1199 {
1200   unsigned int op0, op1, result, z, s, cy;
1201
1202   trace_input ("shr", OP_IMM_REG, 0);
1203   op0 = OP[0];
1204   op1 = State.regs[ OP[1] ];
1205   result = op1 >> op0;
1206
1207   /* Compute the condition codes.  */
1208   z = (result == 0);
1209   s = (result & 0x80000000);
1210   cy = (op1 & (1 << (op0 - 1)));
1211
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);
1218
1219   return 2;
1220 }
1221
1222 /* shr reg1, reg2 */
1223 int
1224 OP_8007E0 ()
1225 {
1226   unsigned int op0, op1, result, z, s, cy;
1227
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;
1232
1233   /* Compute the condition codes.  */
1234   z = (result == 0);
1235   s = (result & 0x80000000);
1236   cy = (op1 & (1 << (op0 - 1)));
1237
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);
1244
1245   return 4;
1246 }
1247
1248 /* or reg, reg */
1249 int
1250 OP_100 ()
1251 {
1252   unsigned int op0, op1, result, z, s;
1253
1254   trace_input ("or", OP_REG_REG, 0);
1255
1256   /* Compute the result.  */
1257   op0 = State.regs[ OP[0] ];
1258   op1 = State.regs[ OP[1] ];
1259   result = op0 | op1;
1260
1261   /* Compute the condition codes.  */
1262   z = (result == 0);
1263   s = (result & 0x80000000);
1264
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);
1270
1271   return 2;
1272 }
1273
1274 /* ori zero_extend(imm16), reg, reg */
1275 int
1276 OP_680 ()
1277 {
1278   unsigned int op0, op1, result, z, s;
1279
1280   trace_input ("ori", OP_UIMM16_REG_REG, 0);
1281   op0 = OP[2];
1282   op1 = State.regs[ OP[0] ];
1283   result = op0 | op1;
1284
1285   /* Compute the condition codes.  */
1286   z = (result == 0);
1287   s = (result & 0x80000000);
1288
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);
1294
1295   return 4;
1296 }
1297
1298 /* and reg, reg */
1299 int
1300 OP_140 ()
1301 {
1302   unsigned int op0, op1, result, z, s;
1303
1304   trace_input ("and", OP_REG_REG, 0);
1305
1306   /* Compute the result.  */
1307   op0 = State.regs[ OP[0] ];
1308   op1 = State.regs[ OP[1] ];
1309   result = op0 & op1;
1310
1311   /* Compute the condition codes.  */
1312   z = (result == 0);
1313   s = (result & 0x80000000);
1314
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);
1320
1321   return 2;
1322 }
1323
1324 /* andi zero_extend(imm16), reg, reg */
1325 int
1326 OP_6C0 ()
1327 {
1328   unsigned int result, z;
1329
1330   trace_input ("andi", OP_UIMM16_REG_REG, 0);
1331
1332   result = OP[2] & State.regs[ OP[0] ];
1333
1334   /* Compute the condition codes.  */
1335   z = (result == 0);
1336
1337   /* Store the result and condition codes.  */
1338   State.regs[ OP[1] ] = result;
1339   
1340   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1341   PSW |= (z ? PSW_Z : 0);
1342   
1343   trace_output (OP_UIMM16_REG_REG);
1344
1345   return 4;
1346 }
1347
1348 /* xor reg, reg */
1349 int
1350 OP_120 ()
1351 {
1352   unsigned int op0, op1, result, z, s;
1353
1354   trace_input ("xor", OP_REG_REG, 0);
1355
1356   /* Compute the result.  */
1357   op0 = State.regs[ OP[0] ];
1358   op1 = State.regs[ OP[1] ];
1359   result = op0 ^ op1;
1360
1361   /* Compute the condition codes.  */
1362   z = (result == 0);
1363   s = (result & 0x80000000);
1364
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);
1370
1371   return 2;
1372 }
1373
1374 /* xori zero_extend(imm16), reg, reg */
1375 int
1376 OP_6A0 ()
1377 {
1378   unsigned int op0, op1, result, z, s;
1379
1380   trace_input ("xori", OP_UIMM16_REG_REG, 0);
1381   op0 = OP[2];
1382   op1 = State.regs[ OP[0] ];
1383   result = op0 ^ op1;
1384
1385   /* Compute the condition codes.  */
1386   z = (result == 0);
1387   s = (result & 0x80000000);
1388
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);
1394
1395   return 4;
1396 }
1397
1398 /* not reg1, reg2 */
1399 int
1400 OP_20 ()
1401 {
1402   unsigned int op0, result, z, s;
1403
1404   trace_input ("not", OP_REG_REG_MOVE, 0);
1405   /* Compute the result.  */
1406   op0 = State.regs[ OP[0] ];
1407   result = ~op0;
1408
1409   /* Compute the condition codes.  */
1410   z = (result == 0);
1411   s = (result & 0x80000000);
1412
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);
1418
1419   return 2;
1420 }
1421
1422 /* set1 */
1423 int
1424 OP_7C0 ()
1425 {
1426   unsigned int op0, op1, op2;
1427   int temp;
1428
1429   trace_input ("set1", OP_BIT, 0);
1430   op0 = State.regs[ OP[0] ];
1431   op1 = OP[1] & 0x7;
1432   temp = EXTEND16 (OP[2]);
1433   op2 = temp;
1434   temp = load_mem (op0 + op2, 1);
1435   PSW &= ~PSW_Z;
1436   if ((temp & (1 << op1)) == 0)
1437     PSW |= PSW_Z;
1438   temp |= (1 << op1);
1439   store_mem (op0 + op2, 1, temp);
1440   trace_output (OP_BIT);
1441
1442   return 4;
1443 }
1444
1445 /* not1 */
1446 int
1447 OP_47C0 ()
1448 {
1449   unsigned int op0, op1, op2;
1450   int temp;
1451
1452   trace_input ("not1", OP_BIT, 0);
1453   op0 = State.regs[ OP[0] ];
1454   op1 = OP[1] & 0x7;
1455   temp = EXTEND16 (OP[2]);
1456   op2 = temp;
1457   temp = load_mem (op0 + op2, 1);
1458   PSW &= ~PSW_Z;
1459   if ((temp & (1 << op1)) == 0)
1460     PSW |= PSW_Z;
1461   temp ^= (1 << op1);
1462   store_mem (op0 + op2, 1, temp);
1463   trace_output (OP_BIT);
1464
1465   return 4;
1466 }
1467
1468 /* clr1 */
1469 int
1470 OP_87C0 ()
1471 {
1472   unsigned int op0, op1, op2;
1473   int temp;
1474
1475   trace_input ("clr1", OP_BIT, 0);
1476   op0 = State.regs[ OP[0] ];
1477   op1 = OP[1] & 0x7;
1478   temp = EXTEND16 (OP[2]);
1479   op2 = temp;
1480   temp = load_mem (op0 + op2, 1);
1481   PSW &= ~PSW_Z;
1482   if ((temp & (1 << op1)) == 0)
1483     PSW |= PSW_Z;
1484   temp &= ~(1 << op1);
1485   store_mem (op0 + op2, 1, temp);
1486   trace_output (OP_BIT);
1487
1488   return 4;
1489 }
1490
1491 /* tst1 */
1492 int
1493 OP_C7C0 ()
1494 {
1495   unsigned int op0, op1, op2;
1496   int temp;
1497
1498   trace_input ("tst1", OP_BIT, 0);
1499   op0 = State.regs[ OP[0] ];
1500   op1 = OP[1] & 0x7;
1501   temp = EXTEND16 (OP[2]);
1502   op2 = temp;
1503   temp = load_mem (op0 + op2, 1);
1504   PSW &= ~PSW_Z;
1505   if ((temp & (1 << op1)) == 0)
1506     PSW |= PSW_Z;
1507   trace_output (OP_BIT);
1508
1509   return 4;
1510 }
1511
1512 /* di */
1513 int
1514 OP_16007E0 ()
1515 {
1516   trace_input ("di", OP_NONE, 0);
1517   PSW |= PSW_ID;
1518   trace_output (OP_NONE);
1519
1520   return 4;
1521 }
1522
1523 /* ei */
1524 int
1525 OP_16087E0 ()
1526 {
1527   trace_input ("ei", OP_NONE, 0);
1528   PSW &= ~PSW_ID;
1529   trace_output (OP_NONE);
1530
1531   return 4;
1532 }
1533
1534 /* halt */
1535 int
1536 OP_12007E0 ()
1537 {
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);
1543   return 0;
1544 }
1545
1546 /* trap */
1547 int
1548 OP_10007E0 ()
1549 {
1550   trace_input ("trap", OP_TRAP, 0);
1551   trace_output (OP_TRAP);
1552
1553   /* Trap 31 is used for simulating OS I/O functions */
1554
1555   if (OP[0] == 31)
1556     {
1557       int save_errno = errno;   
1558       errno = 0;
1559
1560 /* Registers passed to trap 0 */
1561
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 */
1566
1567 /* Registers set by trap 0 */
1568
1569 #define RETVAL State.regs[10]   /* return value */
1570 #define RETERR State.regs[11]   /* return error code */
1571
1572 /* Turn a pointer in a register into a pointer into real memory. */
1573
1574 #define MEMPTR(x) (map (x))
1575
1576       switch (FUNC)
1577         {
1578
1579 #ifdef HAVE_FORK
1580 #ifdef TARGET_SYS_fork
1581         case TARGET_SYS_fork:
1582           RETVAL = fork ();
1583           break;
1584 #endif
1585 #endif
1586
1587 #ifdef HAVE_EXECVE
1588 #ifdef TARGET_SYS_execv
1589         case TARGET_SYS_execve:
1590           {
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);
1595             zfree (path);
1596             freeargv (argv);
1597             freeargv (envp);
1598             break;
1599           }
1600 #endif
1601 #endif
1602
1603 #if HAVE_EXECV
1604 #ifdef TARGET_SYS_execv
1605         case TARGET_SYS_execv:
1606           {
1607             char *path = fetch_str (simulator, PARM1);
1608             char **argv = fetch_argv (simulator, PARM2);
1609             RETVAL = execv (path, argv);
1610             zfree (path);
1611             freeargv (argv);
1612             break;
1613           }
1614 #endif
1615 #endif
1616
1617 #if 0
1618 #ifdef TARGET_SYS_pipe
1619         case TARGET_SYS_pipe:
1620           {
1621             reg_t buf;
1622             int host_fd[2];
1623
1624             buf = PARM1;
1625             RETVAL = pipe (host_fd);
1626             SW (buf, host_fd[0]);
1627             buf += sizeof(uint16);
1628             SW (buf, host_fd[1]);
1629           }
1630           break;
1631 #endif
1632 #endif
1633
1634 #if 0
1635 #ifdef TARGET_SYS_wait
1636         case TARGET_SYS_wait:
1637           {
1638             int status;
1639
1640             RETVAL = wait (&status);
1641             SW (PARM1, status);
1642           }
1643           break;
1644 #endif
1645 #endif
1646
1647 #ifdef TARGET_SYS_read
1648         case TARGET_SYS_read:
1649           {
1650             char *buf = zalloc (PARM3);
1651             RETVAL = sim_io_read (simulator, PARM1, buf, PARM3);
1652             sim_write (simulator, PARM2, buf, PARM3);
1653             zfree (buf);
1654             break;
1655           }
1656 #endif
1657
1658 #ifdef TARGET_SYS_write
1659         case TARGET_SYS_write:
1660           {
1661             char *buf = zalloc (PARM3);
1662             sim_read (simulator, PARM2, buf, PARM3);
1663             if (PARM1 == 1)
1664               RETVAL = sim_io_write_stdout (simulator, buf, PARM3);
1665             else
1666               RETVAL = sim_io_write (simulator, PARM1, buf, PARM3);
1667             zfree (buf);
1668             break;
1669           }
1670 #endif
1671
1672 #ifdef TARGET_SYS_lseek
1673         case TARGET_SYS_lseek:
1674           RETVAL = sim_io_lseek (simulator, PARM1, PARM2, PARM3);
1675           break;
1676 #endif
1677
1678 #ifdef TARGET_SYS_close
1679         case TARGET_SYS_close:
1680           RETVAL = sim_io_close (simulator, PARM1);
1681           break;
1682 #endif
1683
1684 #ifdef TARGET_SYS_open
1685         case TARGET_SYS_open:
1686           {
1687             char *buf = fetch_str (simulator, PARM1);
1688             RETVAL = sim_io_open (simulator, buf, PARM2);
1689             zfree (buf);
1690             break;
1691           }
1692 #endif
1693
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)
1701             /* old libraries */
1702             sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1703                              sim_stopped, SIM_SIGABRT);
1704           else
1705             /* PARM1 has exit status */
1706             sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1707                              sim_exited, PARM1);
1708           break;
1709 #endif
1710
1711 #if !defined(__GO32__) && !defined(_WIN32)
1712 #ifdef TARGET_SYS_stat
1713         case TARGET_SYS_stat:   /* added at hmsi */
1714           /* stat system call */
1715           {
1716             struct stat host_stat;
1717             reg_t buf;
1718             char *path = fetch_str (simulator, PARM1);
1719
1720             RETVAL = stat (path, &host_stat);
1721
1722             zfree (path);
1723             buf = PARM2;
1724
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);
1737           }
1738           break;
1739 #endif
1740 #endif
1741
1742 #ifdef HAVE_CHOWN
1743 #ifdef TARGET_SYS_chown
1744         case TARGET_SYS_chown:
1745           {
1746             char *path = fetch_str (simulator, PARM1);
1747             RETVAL = chown (path, PARM2, PARM3);
1748             zfree (path);
1749           }
1750           break;
1751 #endif
1752 #endif
1753
1754 #if HAVE_CHMOD
1755 #ifdef TARGET_SYS_chmod
1756         case TARGET_SYS_chmod:
1757           {
1758             char *path = fetch_str (simulator, PARM1);
1759             RETVAL = chmod (path, PARM2);
1760             zfree (path);
1761           }
1762           break;
1763 #endif
1764 #endif
1765
1766 #ifdef TARGET_SYS_time
1767 #if HAVE_TIME
1768         case TARGET_SYS_time:
1769           {
1770             time_t now;
1771             RETVAL = time (&now);
1772             store_mem (PARM1, 4, now);
1773           }
1774           break;
1775 #endif
1776 #endif
1777
1778 #if !defined(__GO32__) && !defined(_WIN32)
1779 #ifdef TARGET_SYS_times
1780         case TARGET_SYS_times:
1781           {
1782             struct tms tms;
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);
1788             break;
1789           }
1790 #endif
1791 #endif
1792
1793 #ifdef TARGET_SYS_gettimeofday
1794 #if !defined(__GO32__) && !defined(_WIN32)
1795         case TARGET_SYS_gettimeofday:
1796           {
1797             struct timeval t;
1798             struct timezone tz;
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);
1804             break;
1805           }
1806 #endif
1807 #endif
1808
1809 #ifdef TARGET_SYS_utime
1810 #if HAVE_UTIME
1811         case TARGET_SYS_utime:
1812           {
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)); */
1817           }
1818           break;
1819 #endif
1820 #endif
1821
1822         default:
1823           abort ();
1824         }
1825       RETERR = errno;
1826       errno = save_errno;
1827
1828       return 4;
1829     }
1830   else
1831     {                           /* Trap 0 -> 30 */
1832       EIPC = PC + 4;
1833       EIPSW = PSW;
1834       /* Mask out EICC */
1835       ECR &= 0xffff0000;
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;
1840
1841       return 0;
1842     }
1843 }
1844
1845 /* tst1 reg2, [reg1] */
1846 int
1847 OP_E607E0 (void)
1848 {
1849   int temp;
1850
1851   trace_input ("tst1", OP_BIT, 1);
1852
1853   temp = load_mem (State.regs[ OP[0] ], 1);
1854   
1855   PSW &= ~PSW_Z;
1856   if ((temp & (1 << (State.regs[ OP[1] ] & 0x7))) == 0)
1857     PSW |= PSW_Z;
1858   
1859   trace_output (OP_BIT);
1860
1861   return 4;
1862 }
1863
1864 /* mulu reg1, reg2, reg3 */
1865 int
1866 OP_22207E0 (void)
1867 {
1868   trace_input ("mulu", OP_REG_REG_REG, 0);
1869
1870   Multiply64 (0, State.regs[ OP[0] ]);
1871
1872   trace_output (OP_REG_REG_REG);
1873
1874   return 4;
1875 }
1876
1877 #define BIT_CHANGE_OP( name, binop )            \
1878   unsigned int bit;                             \
1879   unsigned int temp;                            \
1880                                                 \
1881   trace_input (name, OP_BIT_CHANGE, 0);         \
1882                                                 \
1883   bit  = 1 << (State.regs[ OP[1] ] & 0x7);      \
1884   temp = load_mem (State.regs[ OP[0] ], 1);     \
1885                                                 \
1886   PSW &= ~PSW_Z;                                \
1887   if ((temp & bit) == 0)                        \
1888     PSW |= PSW_Z;                               \
1889   temp binop bit;                               \
1890                                                 \
1891   store_mem (State.regs[ OP[0] ], 1, temp);     \
1892                                                 \
1893   trace_output (OP_BIT_CHANGE);                 \
1894                                                 \
1895   return 4;
1896
1897 /* clr1 reg2, [reg1] */
1898 int
1899 OP_E407E0 (void)
1900 {
1901   BIT_CHANGE_OP ("clr1", &= ~ );
1902 }
1903
1904 /* not1 reg2, [reg1] */
1905 int
1906 OP_E207E0 (void)
1907 {
1908   BIT_CHANGE_OP ("not1", ^= );
1909 }
1910
1911 /* set1 */
1912 int
1913 OP_E007E0 (void)
1914 {
1915   BIT_CHANGE_OP ("set1", |= );
1916 }
1917
1918 /* sasf */
1919 int
1920 OP_20007E0 (void)
1921 {
1922   trace_input ("sasf", OP_EX1, 0);
1923   
1924   State.regs[ OP[1] ] = (State.regs[ OP[1] ] << 1) | condition_met (OP[0]);
1925   
1926   trace_output (OP_EX1);
1927
1928   return 4;
1929 }
1930
1931 /* This function is courtesy of Sugimoto at NEC, via Seow Tan
1932    (Soew_Tan@el.nec.com) */
1933 void
1934 divun
1935 (
1936   unsigned int       N,
1937   unsigned long int  als,
1938   unsigned long int  sfi,
1939   unsigned32 /*unsigned long int*/ *  quotient_ptr,
1940   unsigned32 /*unsigned long int*/ *  remainder_ptr,
1941   int *          overflow_ptr
1942 )
1943 {
1944   unsigned long   ald = sfi >> (N - 1);
1945   unsigned long   alo = als;
1946   unsigned int    Q   = 1;
1947   unsigned int    C;
1948   unsigned int    S   = 0;
1949   unsigned int    i;
1950   unsigned int    R1  = 1;
1951   unsigned int    DBZ = (als == 0) ? 1 : 0;
1952   unsigned long   alt = Q ? ~als : als;
1953
1954   /* 1st Loop */
1955   alo = ald + alt + Q;
1956   C   = (((alt >> 31) & (ald >> 31))
1957          | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
1958   C   = C ^ Q;
1959   Q   = ~(C ^ S) & 1;
1960   R1  = (alo == 0) ? 0 : (R1 & Q);
1961   if ((S ^ (alo>>31)) && !C)
1962     {
1963       DBZ = 1;
1964     }
1965   S   = alo >> 31;
1966   sfi = (sfi << (32-N+1)) | Q;
1967   ald = (alo << 1) | (sfi >> 31);
1968
1969   /* 2nd - N-1th Loop */
1970   for (i = 2; i < N; i++)
1971     {
1972       alt = Q ? ~als : als;
1973       alo = ald + alt + Q;
1974       C   = (((alt >> 31) & (ald >> 31))
1975              | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
1976       C   = C ^ Q;
1977       Q   = ~(C ^ S) & 1;
1978       R1  = (alo == 0) ? 0 : (R1 & Q);
1979       if ((S ^ (alo>>31)) && !C && !DBZ)
1980         {
1981           DBZ = 1;
1982         }
1983       S   = alo >> 31;
1984       sfi = (sfi << 1) | Q;
1985       ald = (alo << 1) | (sfi >> 31);
1986     }
1987   
1988   /* Nth Loop */
1989   alt = Q ? ~als : als;
1990   alo = ald + alt + Q;
1991   C   = (((alt >> 31) & (ald >> 31))
1992          | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
1993   C   = C ^ Q;
1994   Q   = ~(C ^ S) & 1;
1995   R1  = (alo == 0) ? 0 : (R1 & Q);
1996   if ((S ^ (alo>>31)) && !C)
1997     {
1998       DBZ = 1;
1999     }
2000   
2001   * quotient_ptr  = (sfi << 1) | Q;
2002   * remainder_ptr = Q ? alo : (alo + als);
2003   * overflow_ptr  = DBZ | R1;
2004 }
2005
2006 /* This function is courtesy of Sugimoto at NEC, via Seow Tan (Soew_Tan@el.nec.com) */
2007 void
2008 divn
2009 (
2010   unsigned int       N,
2011   unsigned long int  als,
2012   unsigned long int  sfi,
2013   signed32 /*signed long int*/ *  quotient_ptr,
2014   signed32 /*signed long int*/ *  remainder_ptr,
2015   int *          overflow_ptr
2016 )
2017 {
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;
2023   unsigned int    OV;
2024   unsigned int    DBZ = als == 0 ? 1 : 0;
2025   unsigned int    Q   = ~(SS ^ SD) & 1;
2026   unsigned int    C;
2027   unsigned int    S;
2028   unsigned int    i;
2029   unsigned long   alt = Q ? ~als : als;
2030
2031
2032   /* 1st Loop */
2033   
2034   alo = ald + alt + Q;
2035   C   = (((alt >> 31) & (ald >> 31))
2036          | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2037   Q   = C ^ SS;
2038   R1  = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2039   S   = alo >> 31;
2040   sfi = (sfi << (32-N+1)) | Q;
2041   ald = (alo << 1) | (sfi >> 31);
2042   if ((alo >> 31) ^ (ald >> 31))
2043     {
2044       DBZ = 1;
2045     }
2046
2047   /* 2nd - N-1th Loop */
2048   
2049   for (i = 2; i < N; i++)
2050     {
2051       alt = Q ? ~als : als;
2052       alo = ald + alt + Q;
2053       C   = (((alt >> 31) & (ald >> 31))
2054              | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2055       Q   = C ^ SS;
2056       R1  = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2057       S   = alo >> 31;
2058       sfi = (sfi << 1) | Q;
2059       ald = (alo << 1) | (sfi >> 31);
2060       if ((alo >> 31) ^ (ald >> 31))
2061         {
2062           DBZ = 1;
2063         }
2064     }
2065
2066   /* Nth Loop */
2067   alt = Q ? ~als : als;
2068   alo = ald + alt + Q;
2069   C   = (((alt >> 31) & (ald >> 31))
2070          | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2071   Q   = C ^ SS;
2072   R1  = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2073   sfi = (sfi << (32-N+1));
2074   ald = alo;
2075
2076   /* End */
2077   if (alo != 0)
2078     {
2079       alt = Q ? ~als : als;
2080       alo = ald + alt + Q;
2081     }
2082   R1  = R1 & ((~alo >> 31) ^ SD);
2083   if ((alo != 0) && ((Q ^ (SS ^ SD)) ^ R1)) alo = ald;
2084   if (N != 32)
2085     ald = sfi = (long) ((sfi >> 1) | (SS ^ SD) << 31) >> (32-N-1) | Q;
2086   else
2087     ald = sfi = sfi | Q;
2088   
2089   OV = DBZ | ((alo == 0) ? 0 : R1);
2090   
2091   * remainder_ptr = alo;
2092
2093   /* Adj */
2094   if (((alo != 0) && ((SS ^ SD) ^ R1))
2095       || ((alo == 0) && (SS ^ R1)))
2096     alo = ald + 1;
2097   else
2098     alo = ald;
2099   
2100   OV  = (DBZ | R1) ? OV : ((alo >> 31) & (~ald >> 31));
2101
2102   * quotient_ptr  = alo;
2103   * overflow_ptr  = OV;
2104 }
2105
2106 /* sdivun imm5, reg1, reg2, reg3 */
2107 int
2108 OP_1C207E0 (void)
2109 {
2110   unsigned32 /*unsigned long int*/  quotient;
2111   unsigned32 /*unsigned long int*/  remainder;
2112   unsigned long int  divide_by;
2113   unsigned long int  divide_this;
2114   int            overflow = 0;
2115   unsigned int       imm5;
2116       
2117   trace_input ("sdivun", OP_IMM_REG_REG_REG, 0);
2118
2119   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2120
2121   divide_by   = State.regs[ OP[0] ];
2122   divide_this = State.regs[ OP[1] ] << imm5;
2123
2124   divun (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2125   
2126   State.regs[ OP[1]       ] = quotient;
2127   State.regs[ OP[2] >> 11 ] = remainder;
2128   
2129   /* Set condition codes.  */
2130   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2131   
2132   if (overflow)      PSW |= PSW_OV;
2133   if (quotient == 0) PSW |= PSW_Z;
2134   if (quotient & 0x80000000) PSW |= PSW_S;
2135   
2136   trace_output (OP_IMM_REG_REG_REG);
2137
2138   return 4;
2139 }
2140
2141 /* sdivn imm5, reg1, reg2, reg3 */
2142 int
2143 OP_1C007E0 (void)
2144 {
2145   signed32 /*signed long int*/  quotient;
2146   signed32 /*signed long int*/  remainder;
2147   signed long int  divide_by;
2148   signed long int  divide_this;
2149   int          overflow = 0;
2150   unsigned int     imm5;
2151       
2152   trace_input ("sdivn", OP_IMM_REG_REG_REG, 0);
2153
2154   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2155
2156   divide_by   = State.regs[ OP[0] ];
2157   divide_this = State.regs[ OP[1] ] << imm5;
2158
2159   divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2160   
2161   State.regs[ OP[1]       ] = quotient;
2162   State.regs[ OP[2] >> 11 ] = remainder;
2163   
2164   /* Set condition codes.  */
2165   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2166   
2167   if (overflow)      PSW |= PSW_OV;
2168   if (quotient == 0) PSW |= PSW_Z;
2169   if (quotient <  0) PSW |= PSW_S;
2170   
2171   trace_output (OP_IMM_REG_REG_REG);
2172
2173   return 4;
2174 }
2175
2176 /* sdivhun imm5, reg1, reg2, reg3 */
2177 int
2178 OP_18207E0 (void)
2179 {
2180   unsigned32 /*unsigned long int*/  quotient;
2181   unsigned32 /*unsigned long int*/  remainder;
2182   unsigned long int  divide_by;
2183   unsigned long int  divide_this;
2184   int            overflow = 0;
2185   unsigned int       imm5;
2186       
2187   trace_input ("sdivhun", OP_IMM_REG_REG_REG, 0);
2188
2189   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2190
2191   divide_by   = State.regs[ OP[0] ] & 0xffff;
2192   divide_this = State.regs[ OP[1] ] << imm5;
2193
2194   divun (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2195   
2196   State.regs[ OP[1]       ] = quotient;
2197   State.regs[ OP[2] >> 11 ] = remainder;
2198   
2199   /* Set condition codes.  */
2200   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2201   
2202   if (overflow)      PSW |= PSW_OV;
2203   if (quotient == 0) PSW |= PSW_Z;
2204   if (quotient & 0x80000000) PSW |= PSW_S;
2205   
2206   trace_output (OP_IMM_REG_REG_REG);
2207
2208   return 4;
2209 }
2210
2211 /* sdivhn imm5, reg1, reg2, reg3 */
2212 int
2213 OP_18007E0 (void)
2214 {
2215   signed32 /*signed long int*/  quotient;
2216   signed32 /*signed long int*/  remainder;
2217   signed long int  divide_by;
2218   signed long int  divide_this;
2219   int          overflow = 0;
2220   unsigned int     imm5;
2221       
2222   trace_input ("sdivhn", OP_IMM_REG_REG_REG, 0);
2223
2224   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2225
2226   divide_by   = EXTEND16 (State.regs[ OP[0] ]);
2227   divide_this = State.regs[ OP[1] ] << imm5;
2228
2229   divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2230   
2231   State.regs[ OP[1]       ] = quotient;
2232   State.regs[ OP[2] >> 11 ] = remainder;
2233   
2234   /* Set condition codes.  */
2235   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2236   
2237   if (overflow)      PSW |= PSW_OV;
2238   if (quotient == 0) PSW |= PSW_Z;
2239   if (quotient <  0) PSW |= PSW_S;
2240   
2241   trace_output (OP_IMM_REG_REG_REG);
2242
2243   return 4;
2244 }
2245
2246 /* divu  reg1, reg2, reg3 */
2247 int
2248 OP_2C207E0 (void)
2249 {
2250   unsigned long int quotient;
2251   unsigned long int remainder;
2252   unsigned long int divide_by;
2253   unsigned long int divide_this;
2254   int           overflow = 0;
2255   
2256   trace_input ("divu", OP_REG_REG_REG, 0);
2257   
2258   /* Compute the result.  */
2259   
2260   divide_by   = State.regs[ OP[0] ];
2261   divide_this = State.regs[ OP[1] ];
2262   
2263   if (divide_by == 0)
2264     {
2265       overflow = 1;
2266       divide_by  = 1;
2267     }
2268   
2269   State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
2270   State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2271   
2272   /* Set condition codes.  */
2273   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2274   
2275   if (overflow)      PSW |= PSW_OV;
2276   if (quotient == 0) PSW |= PSW_Z;
2277   if (quotient & 0x80000000) PSW |= PSW_S;
2278   
2279   trace_output (OP_REG_REG_REG);
2280
2281   return 4;
2282 }
2283
2284 /* div  reg1, reg2, reg3 */
2285 int
2286 OP_2C007E0 (void)
2287 {
2288   signed long int quotient;
2289   signed long int remainder;
2290   signed long int divide_by;
2291   signed long int divide_this;
2292   int         overflow = 0;
2293   
2294   trace_input ("div", OP_REG_REG_REG, 0);
2295   
2296   /* Compute the result.  */
2297   
2298   divide_by   = State.regs[ OP[0] ];
2299   divide_this = State.regs[ OP[1] ];
2300   
2301   if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
2302     {
2303       overflow  = 1;
2304       divide_by = 1;
2305     }
2306   
2307   State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
2308   State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2309   
2310   /* Set condition codes.  */
2311   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2312   
2313   if (overflow)      PSW |= PSW_OV;
2314   if (quotient == 0) PSW |= PSW_Z;
2315   if (quotient <  0) PSW |= PSW_S;
2316   
2317   trace_output (OP_REG_REG_REG);
2318
2319   return 4;
2320 }
2321
2322 /* divhu  reg1, reg2, reg3 */
2323 int
2324 OP_28207E0 (void)
2325 {
2326   unsigned long int quotient;
2327   unsigned long int remainder;
2328   unsigned long int divide_by;
2329   unsigned long int divide_this;
2330   int           overflow = 0;
2331   
2332   trace_input ("divhu", OP_REG_REG_REG, 0);
2333   
2334   /* Compute the result.  */
2335   
2336   divide_by   = State.regs[ OP[0] ] & 0xffff;
2337   divide_this = State.regs[ OP[1] ];
2338   
2339   if (divide_by == 0)
2340     {
2341       overflow = 1;
2342       divide_by  = 1;
2343     }
2344   
2345   State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
2346   State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2347   
2348   /* Set condition codes.  */
2349   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2350   
2351   if (overflow)      PSW |= PSW_OV;
2352   if (quotient == 0) PSW |= PSW_Z;
2353   if (quotient & 0x80000000) PSW |= PSW_S;
2354   
2355   trace_output (OP_REG_REG_REG);
2356
2357   return 4;
2358 }
2359
2360 /* divh  reg1, reg2, reg3 */
2361 int
2362 OP_28007E0 (void)
2363 {
2364   signed long int quotient;
2365   signed long int remainder;
2366   signed long int divide_by;
2367   signed long int divide_this;
2368   int         overflow = 0;
2369   
2370   trace_input ("divh", OP_REG_REG_REG, 0);
2371   
2372   /* Compute the result.  */
2373   
2374   divide_by  = State.regs[ OP[0] ];
2375   divide_this = EXTEND16 (State.regs[ OP[1] ]);
2376   
2377   if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
2378     {
2379       overflow = 1;
2380       divide_by  = 1;
2381     }
2382   
2383   State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
2384   State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2385   
2386   /* Set condition codes.  */
2387   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2388   
2389   if (overflow)      PSW |= PSW_OV;
2390   if (quotient == 0) PSW |= PSW_Z;
2391   if (quotient <  0) PSW |= PSW_S;
2392   
2393   trace_output (OP_REG_REG_REG);
2394
2395   return 4;
2396 }
2397
2398 /* mulu imm9, reg2, reg3 */
2399 int
2400 OP_24207E0 (void)
2401 {
2402   trace_input ("mulu", OP_IMM_REG_REG, 0);
2403
2404   Multiply64 (0, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
2405
2406   trace_output (OP_IMM_REG_REG);
2407
2408   return 4;
2409 }
2410
2411 /* mul imm9, reg2, reg3 */
2412 int
2413 OP_24007E0 (void)
2414 {
2415   trace_input ("mul", OP_IMM_REG_REG, 0);
2416
2417   Multiply64 (1, SEXT9 ((OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0)));
2418
2419   trace_output (OP_IMM_REG_REG);
2420
2421   return 4;
2422 }
2423
2424 /* ld.hu */
2425 int
2426 OP_107E0 (void)
2427 {
2428   int adr;
2429
2430   trace_input ("ld.hu", OP_LOAD32, 2);
2431
2432   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
2433   adr &= ~0x1;
2434       
2435   State.regs[ OP[1] ] = load_mem (adr, 2);
2436       
2437   trace_output (OP_LOAD32);
2438   
2439   return 4;
2440 }
2441
2442
2443 /* ld.bu */
2444 int
2445 OP_10780 (void)
2446 {
2447   int adr;
2448
2449   trace_input ("ld.bu", OP_LOAD32, 1);
2450
2451   adr = (State.regs[ OP[0] ]
2452          + (EXTEND16 (OP[2] & ~1) | ((OP[3] >> 5) & 1)));
2453       
2454   State.regs[ OP[1] ] = load_mem (adr, 1);
2455   
2456   trace_output (OP_LOAD32);
2457   
2458   return 4;
2459 }
2460
2461 /* prepare list12, imm5, imm32 */
2462 int
2463 OP_1B0780 (void)
2464 {
2465   int  i;
2466   
2467   trace_input ("prepare", OP_PUSHPOP1, 0);
2468   
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 ])))
2472       {
2473         SP -= 4;
2474         store_mem (SP, 4, State.regs[ 20 + i ]);
2475       }
2476   
2477   SP -= (OP[3] & 0x3e) << 1;
2478
2479   EP = load_mem (PC + 4, 4);
2480   
2481   trace_output (OP_PUSHPOP1);
2482
2483   return 8;
2484 }
2485
2486 /* prepare list12, imm5, imm16-32 */
2487 int
2488 OP_130780 (void)
2489 {
2490   int  i;
2491   
2492   trace_input ("prepare", OP_PUSHPOP1, 0);
2493   
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 ])))
2497       {
2498         SP -= 4;
2499         store_mem (SP, 4, State.regs[ 20 + i ]);
2500       }
2501   
2502   SP -= (OP[3] & 0x3e) << 1;
2503
2504   EP = load_mem (PC + 4, 2) << 16;
2505   
2506   trace_output (OP_PUSHPOP1);
2507
2508   return 6;
2509 }
2510
2511 /* prepare list12, imm5, imm16 */
2512 int
2513 OP_B0780 (void)
2514 {
2515   int  i;
2516   
2517   trace_input ("prepare", OP_PUSHPOP1, 0);
2518   
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 ])))
2522       {
2523         SP -= 4;
2524         store_mem (SP, 4, State.regs[ 20 + i ]);
2525       }
2526   
2527   SP -= (OP[3] & 0x3e) << 1;
2528
2529   EP = EXTEND16 (load_mem (PC + 4, 2));
2530   
2531   trace_output (OP_PUSHPOP1);
2532
2533   return 6;
2534 }
2535
2536 /* prepare list12, imm5, sp */
2537 int
2538 OP_30780 (void)
2539 {
2540   int  i;
2541   
2542   trace_input ("prepare", OP_PUSHPOP1, 0);
2543   
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 ])))
2547       {
2548         SP -= 4;
2549         store_mem (SP, 4, State.regs[ 20 + i ]);
2550       }
2551   
2552   SP -= (OP[3] & 0x3e) << 1;
2553
2554   EP = SP;
2555   
2556   trace_output (OP_PUSHPOP1);
2557
2558   return 4;
2559 }
2560
2561 /* mul reg1, reg2, reg3 */
2562 int
2563 OP_22007E0 (void)
2564 {
2565   trace_input ("mul", OP_REG_REG_REG, 0);
2566
2567   Multiply64 (1, State.regs[ OP[0] ]);
2568
2569   trace_output (OP_REG_REG_REG);
2570
2571   return 4;
2572 }
2573
2574 /* popmh list18 */
2575 int
2576 OP_307F0 (void)
2577 {
2578   int i;
2579   
2580   trace_input ("popmh", OP_PUSHPOP2, 0);
2581   
2582   if (OP[3] & (1 << 19))
2583     {
2584       if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
2585         {
2586           FEPSW = load_mem ( SP      & ~ 3, 4);
2587           FEPC  = load_mem ((SP + 4) & ~ 3, 4);
2588         }
2589       else
2590         {
2591           EIPSW = load_mem ( SP      & ~ 3, 4);
2592           EIPC  = load_mem ((SP + 4) & ~ 3, 4);
2593         }
2594       
2595       SP += 8;
2596     }
2597   
2598   /* Load the registers with lower number registers being retrieved from higher addresses.  */
2599   for (i = 16; i--;)
2600     if ((OP[3] & (1 << type2_regs[ i ])))
2601       {
2602         State.regs[ i + 16 ] = load_mem (SP & ~ 3, 4);
2603         SP += 4;
2604       }
2605   
2606   trace_output (OP_PUSHPOP2);
2607
2608   return 4;
2609 }
2610
2611 /* popml lsit18 */
2612 int
2613 OP_107F0 (void)
2614 {
2615   int i;
2616
2617   trace_input ("popml", OP_PUSHPOP3, 0);
2618
2619   if (OP[3] & (1 << 19))
2620     {
2621       if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
2622         {
2623           FEPSW = load_mem ( SP      & ~ 3, 4);
2624           FEPC =  load_mem ((SP + 4) & ~ 3, 4);
2625         }
2626       else
2627         {
2628           EIPSW = load_mem ( SP      & ~ 3, 4);
2629           EIPC  = load_mem ((SP + 4) & ~ 3, 4);
2630         }
2631       
2632       SP += 8;
2633     }
2634   
2635   if (OP[3] & (1 << 3))
2636     {
2637       PSW = load_mem (SP & ~ 3, 4);
2638       SP += 4;
2639     }
2640   
2641   /* Load the registers with lower number registers being retrieved from higher addresses.  */
2642   for (i = 15; i--;)
2643     if ((OP[3] & (1 << type3_regs[ i ])))
2644       {
2645         State.regs[ i + 1 ] = load_mem (SP & ~ 3, 4);
2646         SP += 4;
2647       }
2648   
2649   trace_output (OP_PUSHPOP2);
2650
2651   return 4;
2652 }
2653
2654 /* pushmh list18 */
2655 int
2656 OP_307E0 (void)
2657 {
2658   int i;
2659
2660   trace_input ("pushmh", OP_PUSHPOP2, 0);
2661   
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 ])))
2665       {
2666         SP -= 4;
2667         store_mem (SP & ~ 3, 4, State.regs[ i + 16 ]);
2668       }
2669   
2670   if (OP[3] & (1 << 19))
2671     {
2672       SP -= 8;
2673       
2674       if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
2675         {
2676           store_mem ((SP + 4) & ~ 3, 4, FEPC);
2677           store_mem ( SP      & ~ 3, 4, FEPSW);
2678         }
2679       else
2680         {
2681           store_mem ((SP + 4) & ~ 3, 4, EIPC);
2682           store_mem ( SP      & ~ 3, 4, EIPSW);
2683         }
2684     }
2685   
2686   trace_output (OP_PUSHPOP2);
2687
2688   return 4;
2689 }
2690