Use trace_one_insn in trace functions. Buffer up trace data so that
[external/binutils.git] / sim / v850 / simops.c
1 #include <signal.h>
2 #include "sim-main.h"
3 #include "v850_sim.h"
4 #include "simops.h"
5
6 #ifdef HAVE_UTIME_H
7 #include <utime.h>
8 #endif
9
10 #ifdef HAVE_TIME_H
11 #include <time.h>
12 #endif
13
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17
18 #ifdef HAVE_STRING_H
19 #include <string.h>
20 #else
21 #ifdef HAVE_STRINGS_H
22 #include <strings.h>
23 #endif
24 #endif
25
26
27
28
29
30  /* FIXME - should be including a version of syscall.h that does not
31     pollute the name space */
32 #include "../../libgloss/v850/sys/syscall.h"
33
34 #include "libiberty.h"
35
36 #include <errno.h>
37 #if !defined(__GO32__) && !defined(_WIN32)
38 #include <sys/stat.h>
39 #include <sys/times.h>
40 #include <sys/time.h>
41 #endif
42
43 /* start-sanitize-v850e */
44 /* This is an array of the bit positions of registers r20 .. r31 in
45    that order in a prepare/dispose instruction.  */
46 int type1_regs[12] = { 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 0, 21 };
47 /* end-sanitize-v850e */
48 /* start-sanitize-v850eq */
49 /* This is an array of the bit positions of registers r16 .. r31 in
50    that order in a push/pop instruction.  */
51 int type2_regs[16] = { 3, 2, 1, 0, 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 20, 21};
52 /* This is an array of the bit positions of registers r1 .. r15 in
53    that order in a push/pop instruction.  */
54 int type3_regs[15] = { 2, 1, 0, 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 20, 21};
55 /* end-sanitize-v850eq */
56
57 #ifdef DEBUG
58 #ifndef SIZE_INSTRUCTION
59 #define SIZE_INSTRUCTION 18
60 #endif
61
62 #ifndef SIZE_VALUES
63 #define SIZE_VALUES 11
64 #endif
65
66
67 static unsigned32 trace_values[3];
68 static int trace_num_values;
69 static unsigned32 trace_pc;
70 static char *trace_name;
71
72
73 void
74 trace_input (name, type, size)
75      char *name;
76      enum op_types type;
77      int size;
78 {
79
80   if (!TRACE_ALU_P (STATE_CPU (simulator, 0)))
81     return;
82
83   trace_pc = PC;
84   trace_name = name;
85
86   switch (type)
87     {
88     default:
89     case OP_UNKNOWN:
90     case OP_NONE:
91     case OP_TRAP:
92       trace_num_values = 0;
93       break;
94       
95     case OP_REG:
96     case OP_REG_REG_MOVE:
97       trace_values[0] = State.regs[OP[0]];
98       trace_num_values = 1;
99       break;
100       
101       /* start-sanitize-v850e */
102     case OP_BIT_CHANGE:
103       /* end-sanitize-v850e */
104     case OP_REG_REG:
105     case OP_REG_REG_CMP:
106       trace_values[0] = State.regs[OP[1]];
107       trace_values[1] = State.regs[OP[0]];
108       trace_num_values = 2;
109       break;
110       
111     case OP_IMM_REG:
112     case OP_IMM_REG_CMP:
113       trace_values[0] = SEXT5 (OP[0]);
114       trace_values[1] = OP[1];
115       trace_num_values = 2;
116       break;
117       
118     case OP_IMM_REG_MOVE:
119       trace_values[0] = SEXT5 (OP[0]);
120       trace_num_values = 1;
121       break;
122       
123     case OP_COND_BR:
124       trace_values[0] = State.pc;
125       trace_values[1] = SEXT9 (OP[0]);
126       trace_values[2] = PSW;
127       trace_num_values = 3;
128       break;
129       
130     case OP_LOAD16:
131       trace_values[0] = OP[1] * size;
132       trace_values[1] = State.regs[30];
133       trace_num_values = 2;
134       break;
135       
136     case OP_STORE16:
137       trace_values[0] = State.regs[OP[0]];
138       trace_values[1] = OP[1] * size;
139       trace_values[2] = State.regs[30];
140       trace_num_values = 3;
141       break;
142       
143     case OP_LOAD32:
144       trace_values[0] = EXTEND16 (OP[2]);
145       trace_values[1] = State.regs[OP[0]];
146       trace_num_values = 2;
147       break;
148       
149     case OP_STORE32:
150       trace_values[0] = State.regs[OP[1]];
151       trace_values[1] = EXTEND16 (OP[2]);
152       trace_values[2] = State.regs[OP[0]];
153       trace_num_values = 3;
154       break;
155       
156     case OP_JUMP:
157       trace_values[0] = SEXT22 (OP[0]);
158       trace_values[1] = State.pc;
159       trace_num_values = 2;
160       break;
161       
162     case OP_IMM_REG_REG:
163       trace_values[0] = EXTEND16 (OP[0]) << size;
164       trace_values[1] = State.regs[OP[1]];
165       trace_num_values = 2;
166       break;
167       
168     case OP_UIMM_REG_REG:
169       trace_values[0] = (OP[0] & 0xffff) << size;
170       trace_values[1] = State.regs[OP[1]];
171       trace_num_values = 2;
172       break;
173       
174     case OP_BIT:
175       trace_num_values = 0;
176       break;
177       
178     case OP_EX1:
179       trace_values[0] = PSW;
180       trace_num_values = 1;
181       break;
182       
183     case OP_EX2:
184       trace_num_values = 0;
185       break;
186       
187     case OP_LDSR:
188       trace_values[0] = State.regs[OP[0]];
189       trace_num_values = 1;
190       break;
191       
192     case OP_STSR:
193       trace_values[0] = State.sregs[OP[1]];
194       trace_num_values = 1;
195     }
196   
197 }
198
199 void
200 trace_output (result)
201      enum op_types result;
202 {
203   char buf[1000];
204   char *chp;
205
206   if (!TRACE_ALU_P (STATE_CPU (simulator, 0)))
207     return;
208
209   buf[0] = '\0';
210   chp = buf;
211
212   /* write out the values saved during the trace_input call */
213   {
214     int i;
215     for (i = 0; i < trace_num_values; i++)
216       {
217         sprintf (chp, "%*s0x%.8lx", SIZE_VALUES - 10, "", trace_values[i]);
218         chp = strchr (chp, '\0');
219       }
220     while (i++ < 3)
221       {
222         sprintf (chp, "%*s", SIZE_VALUES, "");
223         chp = strchr (chp, '\0');
224       }
225   }
226
227   switch (result)
228     {
229     default:
230     case OP_UNKNOWN:
231     case OP_NONE:
232     case OP_TRAP:
233     case OP_REG:
234     case OP_REG_REG_CMP:
235     case OP_IMM_REG_CMP:
236     case OP_COND_BR:
237     case OP_STORE16:
238     case OP_STORE32:
239     case OP_BIT:
240     case OP_EX2:
241       break;
242       
243     case OP_LOAD16:
244     case OP_STSR:
245       sprintf (chp, " :: 0x%.8lx", (unsigned long)State.regs[OP[0]]);
246       break;
247       
248     case OP_REG_REG:
249     case OP_REG_REG_MOVE:
250     case OP_IMM_REG:
251     case OP_IMM_REG_MOVE:
252     case OP_LOAD32:
253     case OP_EX1:
254       sprintf (chp, " :: 0x%.8lx", (unsigned long)State.regs[OP[1]]);
255       break;
256       
257     case OP_IMM_REG_REG:
258     case OP_UIMM_REG_REG:
259       sprintf (chp, " :: 0x%.8lx", (unsigned long)State.regs[OP[2]]);
260       break;
261       
262     case OP_JUMP:
263       if (OP[1] != 0)
264         sprintf (chp, " :: 0x%.8lx", (unsigned long)State.regs[OP[1]]);
265       break;
266       
267     case OP_LDSR:
268       sprintf (chp, " :: 0x%.8lx", (unsigned long)State.sregs[OP[1]]);
269       break;
270     }
271   
272   trace_one_insn (simulator, STATE_CPU (simulator, 0), trace_pc,
273                   TRACE_LINENUM_P (STATE_CPU (simulator, 0)),
274                   "simops", __LINE__, "alu", 
275                   "%-*s -%s", SIZE_INSTRUCTION, trace_name, buf);
276
277 }
278 #endif
279
280 \f
281 /* Returns 1 if the specific condition is met, returns 0 otherwise.  */
282 static unsigned int
283 condition_met (unsigned code)
284 {
285   unsigned int psw = PSW;
286
287   switch (code & 0xf)
288     {
289       case 0x0: return ((psw & PSW_OV) != 0); 
290       case 0x1: return ((psw & PSW_CY) != 0);
291       case 0x2: return ((psw & PSW_Z) != 0);
292       case 0x3: return ((((psw & PSW_CY) != 0) | ((psw & PSW_Z) != 0)) != 0);
293       case 0x4: return ((psw & PSW_S) != 0);
294     /*case 0x5: return 1;*/
295       case 0x6: return ((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) != 0);
296       case 0x7: return (((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) || ((psw & PSW_Z) != 0)) != 0);
297       case 0x8: return ((psw & PSW_OV) == 0);
298       case 0x9: return ((psw & PSW_CY) == 0);
299       case 0xa: return ((psw & PSW_Z) == 0);
300       case 0xb: return ((((psw & PSW_CY) != 0) | ((psw & PSW_Z) != 0)) == 0);
301       case 0xc: return ((psw & PSW_S) == 0);
302       case 0xd: return ((psw & PSW_SAT) != 0);
303       case 0xe: return ((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) == 0);
304       case 0xf: return (((((psw & PSW_S) != 0) ^ ((psw & PSW_OV) != 0)) || ((psw & PSW_Z) != 0)) == 0);
305     }
306   
307   return 1;
308 }
309 /* start-sanitize-v850e */
310
311 static unsigned long
312 Add32 (unsigned long a1, unsigned long a2, int * carry)
313 {
314   unsigned long result = (a1 + a2);
315
316   * carry = (result < a1);
317
318   return result;
319 }
320
321 static void
322 Multiply64 (boolean sign, unsigned long op0)
323 {
324   unsigned long op1;
325   unsigned long lo;
326   unsigned long mid1;
327   unsigned long mid2;
328   unsigned long hi;
329   unsigned long RdLo;
330   unsigned long RdHi;
331   int           carry;
332   
333   op1 = State.regs[ OP[1] ];
334
335   if (sign)
336     {
337       /* Compute sign of result and adjust operands if necessary.  */
338           
339       sign = (op0 ^ op1) & 0x80000000;
340           
341       if (((signed long) op0) < 0)
342         op0 = - op0;
343           
344       if (((signed long) op1) < 0)
345         op1 = - op1;
346     }
347       
348   /* We can split the 32x32 into four 16x16 operations. This ensures
349      that we do not lose precision on 32bit only hosts: */
350   lo   = ( (op0        & 0xFFFF) *  (op1        & 0xFFFF));
351   mid1 = ( (op0        & 0xFFFF) * ((op1 >> 16) & 0xFFFF));
352   mid2 = (((op0 >> 16) & 0xFFFF) *  (op1        & 0xFFFF));
353   hi   = (((op0 >> 16) & 0xFFFF) * ((op1 >> 16) & 0xFFFF));
354   
355   /* We now need to add all of these results together, taking care
356      to propogate the carries from the additions: */
357   RdLo = Add32 (lo, (mid1 << 16), & carry);
358   RdHi = carry;
359   RdLo = Add32 (RdLo, (mid2 << 16), & carry);
360   RdHi += (carry + ((mid1 >> 16) & 0xFFFF) + ((mid2 >> 16) & 0xFFFF) + hi);
361
362   if (sign)
363     {
364       /* Negate result if necessary.  */
365       
366       RdLo = ~ RdLo;
367       RdHi = ~ RdHi;
368       if (RdLo == 0xFFFFFFFF)
369         {
370           RdLo = 0;
371           RdHi += 1;
372         }
373       else
374         RdLo += 1;
375     }
376   
377   /* Don't store into register 0.  */
378   if (OP[1])
379     State.regs[ OP[1]       ] = RdLo;
380   if (OP[2] >> 11)
381     State.regs[ OP[2] >> 11 ] = RdHi;
382
383   return;
384 }
385
386 /* end-sanitize-v850e */
387 \f
388 /* Read a null terminated string from memory, return in a buffer */
389 static char *
390 fetch_str (sd, addr)
391      SIM_DESC sd;
392      address_word addr;
393 {
394   char *buf;
395   int nr = 0;
396   while (sim_core_read_1 (STATE_CPU (sd, 0),
397                           PC, sim_core_read_map, addr + nr) != 0)
398     nr++;
399   buf = NZALLOC (char, nr + 1);
400   sim_read (simulator, addr, buf, nr);
401   return buf;
402 }
403
404 /* Read a null terminated argument vector from memory, return in a
405    buffer */
406 static char **
407 fetch_argv (sd, addr)
408      SIM_DESC sd;
409      address_word addr;
410 {
411   int max_nr = 64;
412   int nr = 0;
413   char **buf = xmalloc (max_nr * sizeof (char*));
414   while (1)
415     {
416       unsigned32 a = sim_core_read_4 (STATE_CPU (sd, 0),
417                                       PC, sim_core_read_map, addr + nr * 4);
418       if (a == 0) break;
419       buf[nr] = fetch_str (sd, a);
420       nr ++;
421       if (nr == max_nr - 1)
422         {
423           max_nr += 50;
424           buf = xrealloc (buf, max_nr * sizeof (char*));
425         }
426     }
427   buf[nr] = 0;
428   return buf;
429 }
430
431 \f
432 /* sld.b */
433 int
434 OP_300 ()
435 {
436   unsigned long result;
437   
438   result = load_mem (State.regs[30] + (OP[3] & 0x7f), 1);
439
440 /* start-sanitize-v850eq */
441   if (PSW & PSW_US)
442     {
443       trace_input ("sld.bu", OP_LOAD16, 1);
444       State.regs[ OP[1] ] = result;
445     }
446   else
447     {
448 /* end-sanitize-v850eq */
449   trace_input ("sld.b", OP_LOAD16, 1);
450   
451   State.regs[ OP[1] ] = EXTEND8 (result);
452 /* start-sanitize-v850eq */
453     }
454 /* end-sanitize-v850eq */
455   
456   trace_output (OP_LOAD16);
457   
458   return 2;
459 }
460
461 /* sld.h */
462 int
463 OP_400 ()
464 {
465   unsigned long result;
466   
467   result = load_mem (State.regs[30] + ((OP[3] & 0x7f) << 1), 2);
468
469 /* start-sanitize-v850eq */
470   if (PSW & PSW_US)
471     {
472       trace_input ("sld.hu", OP_LOAD16, 2);
473       State.regs[ OP[1] ] = result;
474     }
475   else
476     {
477 /* end-sanitize-v850eq */
478   trace_input ("sld.h", OP_LOAD16, 2);
479   
480   State.regs[ OP[1] ] = EXTEND16 (result);
481 /* start-sanitize-v850eq */
482     }
483 /* end-sanitize-v850eq */
484   
485   trace_output (OP_LOAD16);
486
487   return 2;
488 }
489
490 /* sld.w */
491 int
492 OP_500 ()
493 {
494   trace_input ("sld.w", OP_LOAD16, 4);
495   
496   State.regs[ OP[1] ] = load_mem (State.regs[30] + ((OP[3] & 0x7f) << 1), 4);
497   
498   trace_output (OP_LOAD16);
499   
500   return 2;
501 }
502
503 /* sst.b */
504 int
505 OP_380 ()
506 {
507   trace_input ("sst.b", OP_STORE16, 1);
508
509   store_mem (State.regs[30] + (OP[3] & 0x7f), 1, State.regs[ OP[1] ]);
510   
511   trace_output (OP_STORE16);
512
513   return 2;
514 }
515
516 /* sst.h */
517 int
518 OP_480 ()
519 {
520   trace_input ("sst.h", OP_STORE16, 2);
521
522   store_mem (State.regs[30] + ((OP[3] & 0x7f) << 1), 2, State.regs[ OP[1] ]);
523   
524   trace_output (OP_STORE16);
525
526   return 2;
527 }
528
529 /* sst.w */
530 int
531 OP_501 ()
532 {
533   trace_input ("sst.w", OP_STORE16, 4);
534
535   store_mem (State.regs[30] + ((OP[3] & 0x7e) << 1), 4, State.regs[ OP[1] ]);
536   
537   trace_output (OP_STORE16);
538
539   return 2;
540 }
541
542 /* ld.b */
543 int
544 OP_700 ()
545 {
546   int adr;
547
548   trace_input ("ld.b", OP_LOAD32, 1);
549
550   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
551
552   State.regs[ OP[1] ] = EXTEND8 (load_mem (adr, 1));
553   
554   trace_output (OP_LOAD32);
555
556   return 4;
557 }
558
559 /* ld.h */
560 int
561 OP_720 ()
562 {
563   int adr;
564
565   trace_input ("ld.h", OP_LOAD32, 2);
566
567   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
568   adr &= ~0x1;
569   
570   State.regs[ OP[1] ] = EXTEND16 (load_mem (adr, 2));
571   
572   trace_output (OP_LOAD32);
573
574   return 4;
575 }
576
577 /* ld.w */
578 int
579 OP_10720 ()
580 {
581   int adr;
582
583   trace_input ("ld.w", OP_LOAD32, 4);
584
585   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
586   adr &= ~0x3;
587   
588   State.regs[ OP[1] ] = load_mem (adr, 4);
589   
590   trace_output (OP_LOAD32);
591
592   return 4;
593 }
594
595 /* st.b */
596 int
597 OP_740 ()
598 {
599   trace_input ("st.b", OP_STORE32, 1);
600
601   store_mem (State.regs[ OP[0] ] + EXTEND16 (OP[2]), 1, State.regs[ OP[1] ]);
602   
603   trace_output (OP_STORE32);
604
605   return 4;
606 }
607
608 /* st.h */
609 int
610 OP_760 ()
611 {
612   int adr;
613   
614   trace_input ("st.h", OP_STORE32, 2);
615
616   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
617   adr &= ~1;
618   
619   store_mem (adr, 2, State.regs[ OP[1] ]);
620   
621   trace_output (OP_STORE32);
622
623   return 4;
624 }
625
626 /* st.w */
627 int
628 OP_10760 ()
629 {
630   int adr;
631   
632   trace_input ("st.w", OP_STORE32, 4);
633
634   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
635   adr &= ~3;
636   
637   store_mem (adr, 4, State.regs[ OP[1] ]);
638   
639   trace_output (OP_STORE32);
640
641   return 4;
642 }
643
644 static int
645 branch (int code)
646 {
647   trace_input ("Bcond", OP_COND_BR, 0);
648   trace_output (OP_COND_BR);
649
650   if (condition_met (code))
651     return SEXT9 (((OP[3] & 0x70) >> 3) | ((OP[3] & 0xf800) >> 7));
652   else
653     return 2;
654 }
655
656 /* bv disp9 */
657 int
658 OP_580 ()
659 {
660   return branch (0);
661 }
662
663 /* bl disp9 */
664 int
665 OP_581 ()
666 {
667   return branch (1);
668 }
669
670 /* be disp9 */
671 int
672 OP_582 ()
673 {
674   return branch (2);
675 }
676
677 /* bnh disp 9*/
678 int
679 OP_583 ()
680 {
681   return branch (3);
682 }
683
684 /* bn disp9 */
685 int
686 OP_584 ()
687 {
688   return branch (4);
689 }
690
691 /* br disp9 */
692 int
693 OP_585 ()
694 {
695   return branch (5);
696 }
697
698 /* blt disp9 */
699 int
700 OP_586 ()
701 {
702   return branch (6);
703 }
704
705 /* ble disp9 */
706 int
707 OP_587 ()
708 {
709   return branch (7);
710 }
711
712 /* bnv disp9 */
713 int
714 OP_588 ()
715 {
716   return branch (8);
717 }
718
719 /* bnl disp9 */
720 int
721 OP_589 ()
722 {
723   return branch (9);
724 }
725
726 /* bne disp9 */
727 int
728 OP_58A ()
729 {
730   return branch (10);
731 }
732
733 /* bh disp9 */
734 int
735 OP_58B ()
736 {
737   return branch (11);
738 }
739
740 /* bp disp9 */
741 int
742 OP_58C ()
743 {
744   return branch (12);
745 }
746
747 /* bsa disp9 */
748 int
749 OP_58D ()
750 {
751   return branch (13);
752 }
753
754 /* bge disp9 */
755 int
756 OP_58E ()
757 {
758   return branch (14);
759 }
760
761 /* bgt disp9 */
762 int
763 OP_58F ()
764 {
765   return branch (15);
766 }
767
768 /* jarl/jr disp22, reg */
769 int
770 OP_780 ()
771 {
772   trace_input ("jarl/jr", OP_JUMP, 0);
773
774   if (OP[ 1 ] != 0)
775     State.regs[ OP[1] ] = PC + 4;
776   
777   trace_output (OP_JUMP);
778   
779   return SEXT22 (((OP[3] & 0x3f) << 16) | OP[2]);
780 }
781
782 /* add reg, reg */
783 int
784 OP_1C0 ()
785 {
786   unsigned int op0, op1, result, z, s, cy, ov;
787
788   trace_input ("add", OP_REG_REG, 0);
789   
790   /* Compute the result.  */
791   
792   op0 = State.regs[ OP[0] ];
793   op1 = State.regs[ OP[1] ];
794   
795   result = op0 + op1;
796
797   /* Compute the condition codes.  */
798   z = (result == 0);
799   s = (result & 0x80000000);
800   cy = (result < op0 || result < op1);
801   ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
802         && (op0 & 0x80000000) != (result & 0x80000000));
803
804   /* Store the result and condition codes.  */
805   State.regs[OP[1]] = result;
806   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
807   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
808                      | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
809   trace_output (OP_REG_REG);
810
811   return 2;
812 }
813
814 /* add sign_extend(imm5), reg */
815 int
816 OP_240 ()
817 {
818   unsigned int op0, op1, result, z, s, cy, ov;
819   int temp;
820
821   trace_input ("add", OP_IMM_REG, 0);
822
823   /* Compute the result.  */
824   temp = SEXT5 (OP[0]);
825   op0 = temp;
826   op1 = State.regs[OP[1]];
827   result = op0 + op1;
828   
829   /* Compute the condition codes.  */
830   z = (result == 0);
831   s = (result & 0x80000000);
832   cy = (result < op0 || result < op1);
833   ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
834         && (op0 & 0x80000000) != (result & 0x80000000));
835
836   /* Store the result and condition codes.  */
837   State.regs[OP[1]] = result;
838   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
839   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
840                 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
841   trace_output (OP_IMM_REG);
842
843   return 2;
844 }
845
846 /* addi sign_extend(imm16), reg, reg */
847 int
848 OP_600 ()
849 {
850   unsigned int op0, op1, result, z, s, cy, ov;
851
852   trace_input ("addi", OP_IMM_REG_REG, 0);
853
854   /* Compute the result.  */
855
856   op0 = EXTEND16 (OP[2]);
857   op1 = State.regs[ OP[0] ];
858   result = op0 + op1;
859   
860   /* Compute the condition codes.  */
861   z = (result == 0);
862   s = (result & 0x80000000);
863   cy = (result < op0 || result < op1);
864   ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
865         && (op0 & 0x80000000) != (result & 0x80000000));
866
867   /* Store the result and condition codes.  */
868   State.regs[OP[1]] = result;
869   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
870   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
871                 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
872   trace_output (OP_IMM_REG_REG);
873
874   return 4;
875 }
876
877 /* sub reg1, reg2 */
878 int
879 OP_1A0 ()
880 {
881   unsigned int op0, op1, result, z, s, cy, ov;
882
883   trace_input ("sub", OP_REG_REG, 0);
884   /* Compute the result.  */
885   op0 = State.regs[ OP[0] ];
886   op1 = State.regs[ OP[1] ];
887   result = op1 - op0;
888
889   /* Compute the condition codes.  */
890   z = (result == 0);
891   s = (result & 0x80000000);
892   cy = (op1 < op0);
893   ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
894         && (op1 & 0x80000000) != (result & 0x80000000));
895
896   /* Store the result and condition codes.  */
897   State.regs[OP[1]] = result;
898   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
899   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
900                 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
901   trace_output (OP_REG_REG);
902
903   return 2;
904 }
905
906 /* subr reg1, reg2 */
907 int
908 OP_180 ()
909 {
910   unsigned int op0, op1, result, z, s, cy, ov;
911
912   trace_input ("subr", OP_REG_REG, 0);
913   /* Compute the result.  */
914   op0 = State.regs[ OP[0] ];
915   op1 = State.regs[ OP[1] ];
916   result = op0 - op1;
917
918   /* Compute the condition codes.  */
919   z = (result == 0);
920   s = (result & 0x80000000);
921   cy = (op0 < op1);
922   ov = ((op0 & 0x80000000) != (op1 & 0x80000000)
923         && (op0 & 0x80000000) != (result & 0x80000000));
924
925   /* Store the result and condition codes.  */
926   State.regs[OP[1]] = result;
927   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
928   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
929                 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
930   trace_output (OP_REG_REG);
931
932   return 2;
933 }
934
935 /* sxh reg1 */
936 /* mulh reg1, reg2 */
937 int
938 OP_E0 ()
939 {
940 /* start-sanitize-v850e */
941   if (OP[1] == 0)
942     {
943       trace_input ("sxh", OP_REG, 0);
944       
945       State.regs[ OP[0] ] = EXTEND16 (State.regs[ OP[0] ]);
946
947       trace_output (OP_REG);
948     }
949   else
950 /* end-sanitize-v850e */
951     {
952       trace_input ("mulh", OP_REG_REG, 0);
953       
954       State.regs[ OP[1] ] = (EXTEND16 (State.regs[ OP[1] ]) * EXTEND16 (State.regs[ OP[0] ]));
955       
956       trace_output (OP_REG_REG);
957     }
958
959   return 2;
960 }
961
962 /* mulh sign_extend(imm5), reg2 */
963 int
964 OP_2E0 ()
965 {
966   trace_input ("mulh", OP_IMM_REG, 0);
967   
968   State.regs[ OP[1] ] = EXTEND16 (State.regs[ OP[1] ]) * SEXT5 (OP[0]);
969   
970   trace_output (OP_IMM_REG);
971
972   return 2;
973 }
974
975 /* mulhi imm16, reg1, reg2 */
976 int
977 OP_6E0 ()
978 {
979   trace_input ("mulhi", OP_IMM_REG_REG, 0);
980   
981   State.regs[ OP[1] ] = EXTEND16 (State.regs[ OP[0] ]) * EXTEND16 (OP[2]);
982       
983   trace_output (OP_IMM_REG_REG);
984   
985   return 4;
986 }
987
988 /* divh reg1, reg2 */
989 /* switch  reg1 */
990 int
991 OP_40 ()
992 {
993 /* start-sanitize-v850e */
994   if (OP[1] == 0)
995     {
996       unsigned long adr;
997
998       trace_input ("switch", OP_REG, 0);
999       
1000       adr      = State.pc + 2 + (State.regs[ OP[0] ] << 1);
1001       State.pc = State.pc + 2 + (EXTEND16 (load_mem (adr, 2)) << 1);
1002
1003       trace_output (OP_REG);
1004     }
1005   else
1006 /* end-sanitize-v850e */
1007     {
1008       unsigned int op0, op1, result, ov, s, z;
1009       int temp;
1010
1011       trace_input ("divh", OP_REG_REG, 0);
1012
1013       /* Compute the result.  */
1014       temp = EXTEND16 (State.regs[ OP[0] ]);
1015       op0 = temp;
1016       op1 = State.regs[OP[1]];
1017       
1018       if (op0 == 0xffffffff && op1 == 0x80000000)
1019         {
1020           result = 0x80000000;
1021           ov = 1;
1022         }
1023       else if (op0 != 0)
1024         {
1025           result = op1 / op0;
1026           ov = 0;
1027         }
1028       else
1029         {
1030           result = 0x0;
1031           ov = 1;
1032         }
1033       
1034       /* Compute the condition codes.  */
1035       z = (result == 0);
1036       s = (result & 0x80000000);
1037       
1038       /* Store the result and condition codes.  */
1039       State.regs[OP[1]] = result;
1040       PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1041       PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1042               | (ov ? PSW_OV : 0));
1043       trace_output (OP_REG_REG);
1044     }
1045
1046   return 2;
1047 }
1048
1049 /* cmp reg, reg */
1050 int
1051 OP_1E0 ()
1052 {
1053   unsigned int op0, op1, result, z, s, cy, ov;
1054
1055   trace_input ("cmp", OP_REG_REG_CMP, 0);
1056   /* Compute the result.  */
1057   op0 = State.regs[ OP[0] ];
1058   op1 = State.regs[ OP[1] ];
1059   result = op1 - op0;
1060
1061   /* Compute the condition codes.  */
1062   z = (result == 0);
1063   s = (result & 0x80000000);
1064   cy = (op1 < op0);
1065   ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1066         && (op1 & 0x80000000) != (result & 0x80000000));
1067
1068   /* Set condition codes.  */
1069   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1070   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1071                 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
1072   trace_output (OP_REG_REG_CMP);
1073
1074   return 2;
1075 }
1076
1077 /* cmp sign_extend(imm5), reg */
1078 int
1079 OP_260 ()
1080 {
1081   unsigned int op0, op1, result, z, s, cy, ov;
1082   int temp;
1083
1084   /* Compute the result.  */
1085   trace_input ("cmp", OP_IMM_REG_CMP, 0);
1086   temp = SEXT5 (OP[0]);
1087   op0 = temp;
1088   op1 = State.regs[OP[1]];
1089   result = op1 - op0;
1090
1091   /* Compute the condition codes.  */
1092   z = (result == 0);
1093   s = (result & 0x80000000);
1094   cy = (op1 < op0);
1095   ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1096         && (op1 & 0x80000000) != (result & 0x80000000));
1097
1098   /* Set condition codes.  */
1099   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1100   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1101                 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0));
1102   trace_output (OP_IMM_REG_CMP);
1103
1104   return 2;
1105 }
1106
1107 /* setf cccc,reg2 */
1108 int
1109 OP_7E0 ()
1110 {
1111   trace_input ("setf", OP_EX1, 0);
1112
1113   State.regs[ OP[1] ] = condition_met (OP[0]);
1114   
1115   trace_output (OP_EX1);
1116
1117   return 4;
1118 }
1119
1120 /* satadd reg,reg */
1121 int
1122 OP_C0 ()
1123 {
1124   unsigned int op0, op1, result, z, s, cy, ov, sat;
1125   
1126   trace_input ("satadd", OP_REG_REG, 0);
1127   /* Compute the result.  */
1128   op0 = State.regs[ OP[0] ];
1129   op1 = State.regs[ OP[1] ];
1130   result = op0 + op1;
1131   
1132   /* Compute the condition codes.  */
1133   z = (result == 0);
1134   s = (result & 0x80000000);
1135   cy = (result < op0 || result < op1);
1136   ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
1137         && (op0 & 0x80000000) != (result & 0x80000000));
1138   sat = ov;
1139   
1140   /* Store the result and condition codes.  */
1141   State.regs[OP[1]] = result;
1142   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1143   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1144           | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1145           | (sat ? PSW_SAT : 0));
1146   
1147   /* Handle saturated results.  */
1148   if (sat && s)
1149     State.regs[OP[1]] = 0x80000000;
1150   else if (sat)
1151     State.regs[OP[1]] = 0x7fffffff;
1152   trace_output (OP_REG_REG);
1153
1154   return 2;
1155 }
1156
1157 /* satadd sign_extend(imm5), reg */
1158 int
1159 OP_220 ()
1160 {
1161   unsigned int op0, op1, result, z, s, cy, ov, sat;
1162
1163   int temp;
1164
1165   trace_input ("satadd", OP_IMM_REG, 0);
1166
1167   /* Compute the result.  */
1168   temp = SEXT5 (OP[0]);
1169   op0 = temp;
1170   op1 = State.regs[OP[1]];
1171   result = op0 + op1;
1172
1173   /* Compute the condition codes.  */
1174   z = (result == 0);
1175   s = (result & 0x80000000);
1176   cy = (result < op0 || result < op1);
1177   ov = ((op0 & 0x80000000) == (op1 & 0x80000000)
1178         && (op0 & 0x80000000) != (result & 0x80000000));
1179   sat = ov;
1180
1181   /* Store the result and condition codes.  */
1182   State.regs[OP[1]] = result;
1183   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1184   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1185                 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1186                 | (sat ? PSW_SAT : 0));
1187
1188   /* Handle saturated results.  */
1189   if (sat && s)
1190     State.regs[OP[1]] = 0x80000000;
1191   else if (sat)
1192     State.regs[OP[1]] = 0x7fffffff;
1193   trace_output (OP_IMM_REG);
1194
1195   return 2;
1196 }
1197
1198 /* satsub reg1, reg2 */
1199 /* sxb reg1 */
1200 int
1201 OP_A0 ()
1202 {
1203 /* start-sanitize-v850e */
1204   if (OP[1] == 0)
1205     {
1206       trace_input ("sxb", OP_REG, 0);
1207
1208       State.regs[ OP[0] ] = EXTEND8 (State.regs[ OP[0] ]);
1209
1210       trace_output (OP_REG);
1211     }
1212   else
1213 /* end-sanitize-v850e */
1214     {
1215       unsigned int op0, op1, result, z, s, cy, ov, sat;
1216
1217       trace_input ("satsub", OP_REG_REG, 0);
1218       
1219       /* Compute the result.  */
1220       op0 = State.regs[ OP[0] ];
1221       op1 = State.regs[ OP[1] ];
1222       result = op1 - op0;
1223       
1224       /* Compute the condition codes.  */
1225       z = (result == 0);
1226       s = (result & 0x80000000);
1227       cy = (op1 < op0);
1228       ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1229             && (op1 & 0x80000000) != (result & 0x80000000));
1230       sat = ov;
1231       
1232       /* Store the result and condition codes.  */
1233       State.regs[OP[1]] = result;
1234       PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1235       PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1236               | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1237               | (sat ? PSW_SAT : 0));
1238       
1239       /* Handle saturated results.  */
1240       if (sat && s)
1241         State.regs[OP[1]] = 0x80000000;
1242       else if (sat)
1243         State.regs[OP[1]] = 0x7fffffff;
1244       trace_output (OP_REG_REG);
1245     }
1246
1247   return 2;
1248 }
1249
1250 /* satsubi sign_extend(imm16), reg */
1251 int
1252 OP_660 ()
1253 {
1254   unsigned int op0, op1, result, z, s, cy, ov, sat;
1255   int temp;
1256
1257   trace_input ("satsubi", OP_IMM_REG, 0);
1258
1259   /* Compute the result.  */
1260   temp = EXTEND16 (OP[2]);
1261   op0 = temp;
1262   op1 = State.regs[ OP[0] ];
1263   result = op1 - op0;
1264
1265   /* Compute the condition codes.  */
1266   z = (result == 0);
1267   s = (result & 0x80000000);
1268   cy = (op1 < op0);
1269   ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1270         && (op1 & 0x80000000) != (result & 0x80000000));
1271   sat = ov;
1272
1273   /* Store the result and condition codes.  */
1274   State.regs[OP[1]] = result;
1275   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1276   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1277                 | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1278                 | (sat ? PSW_SAT : 0));
1279
1280   /* Handle saturated results.  */
1281   if (sat && s)
1282     State.regs[OP[1]] = 0x80000000;
1283   else if (sat)
1284     State.regs[OP[1]] = 0x7fffffff;
1285   trace_output (OP_IMM_REG);
1286
1287   return 4;
1288 }
1289
1290 /* satsubr reg,reg */
1291 int
1292 OP_80 ()
1293 {
1294   unsigned int op0, op1, result, z, s, cy, ov, sat;
1295   
1296   trace_input ("satsubr", OP_REG_REG, 0);
1297   
1298   /* Compute the result.  */
1299   op0 = State.regs[ OP[0] ];
1300   op1 = State.regs[ OP[1] ];
1301   result = op0 - op1;
1302   
1303   /* Compute the condition codes.  */
1304   z = (result == 0);
1305   s = (result & 0x80000000);
1306   cy = (result < op0);
1307   ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
1308         && (op1 & 0x80000000) != (result & 0x80000000));
1309   sat = ov;
1310   
1311   /* Store the result and condition codes.  */
1312   State.regs[OP[1]] = result;
1313   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
1314   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1315           | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
1316           | (sat ? PSW_SAT : 0));
1317   
1318   /* Handle saturated results.  */
1319   if (sat && s)
1320     State.regs[OP[1]] = 0x80000000;
1321   else if (sat)
1322     State.regs[OP[1]] = 0x7fffffff;
1323   trace_output (OP_REG_REG);
1324
1325   return 2;
1326 }
1327
1328 /* tst reg,reg */
1329 int
1330 OP_160 ()
1331 {
1332   unsigned int op0, op1, result, z, s;
1333
1334   trace_input ("tst", OP_REG_REG_CMP, 0);
1335
1336   /* Compute the result.  */
1337   op0 = State.regs[ OP[0] ];
1338   op1 = State.regs[ OP[1] ];
1339   result = op0 & op1;
1340
1341   /* Compute the condition codes.  */
1342   z = (result == 0);
1343   s = (result & 0x80000000);
1344
1345   /* Store the condition codes.  */
1346   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1347   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1348   trace_output (OP_REG_REG_CMP);
1349
1350   return 2;
1351 }
1352
1353 /* mov reg, reg */
1354 int
1355 OP_0 ()
1356 {
1357   trace_input ("mov", OP_REG_REG_MOVE, 0);
1358   
1359   State.regs[ OP[1] ] = State.regs[ OP[0] ];
1360   
1361   trace_output (OP_REG_REG_MOVE);
1362
1363   return 2;
1364 }
1365
1366 /* mov sign_extend(imm5), reg */
1367 /* callt imm6 */
1368 int
1369 OP_200 ()
1370 {
1371 /* start-sanitize-v850e */
1372   if (OP[1] == 0)
1373     {
1374       unsigned long adr;
1375       
1376       trace_input ("callt", OP_LOAD16, 1);
1377       
1378       CTPC  = PC + 2;
1379       CTPSW = PSW;
1380       
1381       adr = CTBP + ((OP[3] & 0x3f) << 1);
1382       
1383       PC = CTBP + load_mem (adr, 1);
1384
1385       trace_output (OP_LOAD16);
1386
1387       return 0;
1388     }
1389   else
1390 /* end-sanitize-v850e */
1391     {
1392       int value = SEXT5 (OP[0]);
1393  
1394       trace_input ("mov", OP_IMM_REG_MOVE, 0);
1395       
1396       State.regs[ OP[1] ] = value;
1397       
1398       trace_output (OP_IMM_REG_MOVE);
1399
1400       return 2;
1401     }
1402 }
1403
1404 /* mov imm32, reg1 */
1405 /* movea sign_extend(imm16), reg, reg  */
1406 int
1407 OP_620 ()
1408 {
1409 /* start-sanitize-v850e */
1410   if (OP[1] == 0)
1411     {
1412       trace_input ("mov", OP_IMM_REG, 4);
1413
1414       State.regs[ OP[0] ] = load_mem (PC + 2, 4);
1415       
1416       trace_output (OP_IMM_REG);
1417
1418       return 6;
1419     }
1420   else
1421 /* end-sanitize-v850e */
1422     {
1423       trace_input ("movea", OP_IMM_REG_REG, 0);
1424   
1425       State.regs[ OP[1] ] = State.regs[ OP[0] ] + EXTEND16 (OP[2]);
1426   
1427       trace_output (OP_IMM_REG_REG);
1428
1429       return 4;
1430     }
1431 }
1432
1433 /* dispose imm5, list12 [, reg1] */
1434 /* movhi imm16, reg, reg */
1435 int
1436 OP_640 ()
1437 {
1438 /* start-sanitize-v850e */
1439
1440   if (OP[1] == 0)
1441     {
1442       int        i;
1443       
1444       trace_input ("dispose", OP_PUSHPOP1, 0);
1445
1446       SP += (OP[3] & 0x3e) << 1;
1447
1448       /* Load the registers with lower number registers being retrieved from higher addresses.  */
1449       for (i = 12; i--;)
1450         if ((OP[3] & (1 << type1_regs[ i ])))
1451           {
1452             State.regs[ 20 + i ] = load_mem (SP, 4);
1453             SP += 4;
1454           }
1455
1456       if ((OP[3] & 0x1f0000) != 0)
1457         {
1458           PC = State.regs[ (OP[3] >> 16) & 0x1f];
1459           return 0;
1460         }
1461       
1462       trace_output (OP_PUSHPOP1);
1463     }
1464   else
1465 /* end-sanitize-v850e */
1466     {
1467       trace_input ("movhi", OP_UIMM_REG_REG, 16);
1468       
1469       State.regs[ OP[1] ] = State.regs[ OP[0] ] + (OP[2] << 16);
1470       
1471       trace_output (OP_UIMM_REG_REG);
1472     }
1473
1474   return 4;
1475 }
1476
1477 /* sar zero_extend(imm5),reg1 */
1478 int
1479 OP_2A0 ()
1480 {
1481   unsigned int op0, op1, result, z, s, cy;
1482
1483   trace_input ("sar", OP_IMM_REG, 0);
1484   op0 = OP[0];
1485   op1 = State.regs[ OP[1] ];
1486   result = (signed)op1 >> op0;
1487
1488   /* Compute the condition codes.  */
1489   z = (result == 0);
1490   s = (result & 0x80000000);
1491   cy = (op1 & (1 << (op0 - 1)));
1492
1493   /* Store the result and condition codes.  */
1494   State.regs[ OP[1] ] = result;
1495   PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1496   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1497                 | (cy ? PSW_CY : 0));
1498   trace_output (OP_IMM_REG);
1499
1500   return 2;
1501 }
1502
1503 /* sar reg1, reg2 */
1504 int
1505 OP_A007E0 ()
1506 {
1507   unsigned int op0, op1, result, z, s, cy;
1508
1509   trace_input ("sar", OP_REG_REG, 0);
1510   
1511   op0 = State.regs[ OP[0] ] & 0x1f;
1512   op1 = State.regs[ OP[1] ];
1513   result = (signed)op1 >> op0;
1514
1515   /* Compute the condition codes.  */
1516   z = (result == 0);
1517   s = (result & 0x80000000);
1518   cy = (op1 & (1 << (op0 - 1)));
1519
1520   /* Store the result and condition codes.  */
1521   State.regs[OP[1]] = result;
1522   PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1523   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1524                 | (cy ? PSW_CY : 0));
1525   trace_output (OP_REG_REG);
1526
1527   return 4;
1528 }
1529
1530 /* shl zero_extend(imm5),reg1 */
1531 int
1532 OP_2C0 ()
1533 {
1534   unsigned int op0, op1, result, z, s, cy;
1535
1536   trace_input ("shl", OP_IMM_REG, 0);
1537   op0 = OP[0];
1538   op1 = State.regs[ OP[1] ];
1539   result = op1 << op0;
1540
1541   /* Compute the condition codes.  */
1542   z = (result == 0);
1543   s = (result & 0x80000000);
1544   cy = (op1 & (1 << (32 - op0)));
1545
1546   /* Store the result and condition codes.  */
1547   State.regs[OP[1]] = result;
1548   PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1549   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1550                 | (cy ? PSW_CY : 0));
1551   trace_output (OP_IMM_REG);
1552
1553   return 2;
1554 }
1555
1556 /* shl reg1, reg2 */
1557 int
1558 OP_C007E0 ()
1559 {
1560   unsigned int op0, op1, result, z, s, cy;
1561
1562   trace_input ("shl", OP_REG_REG, 0);
1563   op0 = State.regs[ OP[0] ] & 0x1f;
1564   op1 = State.regs[ OP[1] ];
1565   result = op1 << op0;
1566
1567   /* Compute the condition codes.  */
1568   z = (result == 0);
1569   s = (result & 0x80000000);
1570   cy = (op1 & (1 << (32 - op0)));
1571
1572   /* Store the result and condition codes.  */
1573   State.regs[OP[1]] = result;
1574   PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1575   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1576                 | (cy ? PSW_CY : 0));
1577   trace_output (OP_REG_REG);
1578
1579   return 4;
1580 }
1581
1582 /* shr zero_extend(imm5),reg1 */
1583 int
1584 OP_280 ()
1585 {
1586   unsigned int op0, op1, result, z, s, cy;
1587
1588   trace_input ("shr", OP_IMM_REG, 0);
1589   op0 = OP[0];
1590   op1 = State.regs[ OP[1] ];
1591   result = op1 >> op0;
1592
1593   /* Compute the condition codes.  */
1594   z = (result == 0);
1595   s = (result & 0x80000000);
1596   cy = (op1 & (1 << (op0 - 1)));
1597
1598   /* Store the result and condition codes.  */
1599   State.regs[OP[1]] = result;
1600   PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1601   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1602                 | (cy ? PSW_CY : 0));
1603   trace_output (OP_IMM_REG);
1604
1605   return 2;
1606 }
1607
1608 /* shr reg1, reg2 */
1609 int
1610 OP_8007E0 ()
1611 {
1612   unsigned int op0, op1, result, z, s, cy;
1613
1614   trace_input ("shr", OP_REG_REG, 0);
1615   op0 = State.regs[ OP[0] ] & 0x1f;
1616   op1 = State.regs[ OP[1] ];
1617   result = op1 >> op0;
1618
1619   /* Compute the condition codes.  */
1620   z = (result == 0);
1621   s = (result & 0x80000000);
1622   cy = (op1 & (1 << (op0 - 1)));
1623
1624   /* Store the result and condition codes.  */
1625   State.regs[OP[1]] = result;
1626   PSW &= ~(PSW_Z | PSW_S | PSW_OV | PSW_CY);
1627   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
1628                 | (cy ? PSW_CY : 0));
1629   trace_output (OP_REG_REG);
1630
1631   return 4;
1632 }
1633
1634 /* or reg, reg */
1635 int
1636 OP_100 ()
1637 {
1638   unsigned int op0, op1, result, z, s;
1639
1640   trace_input ("or", OP_REG_REG, 0);
1641
1642   /* Compute the result.  */
1643   op0 = State.regs[ OP[0] ];
1644   op1 = State.regs[ OP[1] ];
1645   result = op0 | op1;
1646
1647   /* Compute the condition codes.  */
1648   z = (result == 0);
1649   s = (result & 0x80000000);
1650
1651   /* Store the result and condition codes.  */
1652   State.regs[OP[1]] = result;
1653   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1654   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1655   trace_output (OP_REG_REG);
1656
1657   return 2;
1658 }
1659
1660 /* ori zero_extend(imm16), reg, reg */
1661 int
1662 OP_680 ()
1663 {
1664   unsigned int op0, op1, result, z, s;
1665
1666   trace_input ("ori", OP_UIMM_REG_REG, 0);
1667   op0 = OP[2];
1668   op1 = State.regs[ OP[0] ];
1669   result = op0 | op1;
1670
1671   /* Compute the condition codes.  */
1672   z = (result == 0);
1673   s = (result & 0x80000000);
1674
1675   /* Store the result and condition codes.  */
1676   State.regs[OP[1]] = result;
1677   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1678   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1679   trace_output (OP_UIMM_REG_REG);
1680
1681   return 4;
1682 }
1683
1684 /* and reg, reg */
1685 int
1686 OP_140 ()
1687 {
1688   unsigned int op0, op1, result, z, s;
1689
1690   trace_input ("and", OP_REG_REG, 0);
1691
1692   /* Compute the result.  */
1693   op0 = State.regs[ OP[0] ];
1694   op1 = State.regs[ OP[1] ];
1695   result = op0 & op1;
1696
1697   /* Compute the condition codes.  */
1698   z = (result == 0);
1699   s = (result & 0x80000000);
1700
1701   /* Store the result and condition codes.  */
1702   State.regs[OP[1]] = result;
1703   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1704   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1705   trace_output (OP_REG_REG);
1706
1707   return 2;
1708 }
1709
1710 /* andi zero_extend(imm16), reg, reg */
1711 int
1712 OP_6C0 ()
1713 {
1714   unsigned int result, z;
1715
1716   trace_input ("andi", OP_UIMM_REG_REG, 0);
1717
1718   result = OP[2] & State.regs[ OP[0] ];
1719
1720   /* Compute the condition codes.  */
1721   z = (result == 0);
1722
1723   /* Store the result and condition codes.  */
1724   State.regs[ OP[1] ] = result;
1725   
1726   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1727   PSW |= (z ? PSW_Z : 0);
1728   
1729   trace_output (OP_UIMM_REG_REG);
1730
1731   return 4;
1732 }
1733
1734 /* xor reg, reg */
1735 int
1736 OP_120 ()
1737 {
1738   unsigned int op0, op1, result, z, s;
1739
1740   trace_input ("xor", OP_REG_REG, 0);
1741
1742   /* Compute the result.  */
1743   op0 = State.regs[ OP[0] ];
1744   op1 = State.regs[ OP[1] ];
1745   result = op0 ^ op1;
1746
1747   /* Compute the condition codes.  */
1748   z = (result == 0);
1749   s = (result & 0x80000000);
1750
1751   /* Store the result and condition codes.  */
1752   State.regs[OP[1]] = result;
1753   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1754   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1755   trace_output (OP_REG_REG);
1756
1757   return 2;
1758 }
1759
1760 /* xori zero_extend(imm16), reg, reg */
1761 int
1762 OP_6A0 ()
1763 {
1764   unsigned int op0, op1, result, z, s;
1765
1766   trace_input ("xori", OP_UIMM_REG_REG, 0);
1767   op0 = OP[2];
1768   op1 = State.regs[ OP[0] ];
1769   result = op0 ^ op1;
1770
1771   /* Compute the condition codes.  */
1772   z = (result == 0);
1773   s = (result & 0x80000000);
1774
1775   /* Store the result and condition codes.  */
1776   State.regs[OP[1]] = result;
1777   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1778   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1779   trace_output (OP_UIMM_REG_REG);
1780
1781   return 4;
1782 }
1783
1784 /* not reg1, reg2 */
1785 int
1786 OP_20 ()
1787 {
1788   unsigned int op0, result, z, s;
1789
1790   trace_input ("not", OP_REG_REG_MOVE, 0);
1791   /* Compute the result.  */
1792   op0 = State.regs[ OP[0] ];
1793   result = ~op0;
1794
1795   /* Compute the condition codes.  */
1796   z = (result == 0);
1797   s = (result & 0x80000000);
1798
1799   /* Store the result and condition codes.  */
1800   State.regs[OP[1]] = result;
1801   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
1802   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0));
1803   trace_output (OP_REG_REG_MOVE);
1804
1805   return 2;
1806 }
1807
1808 /* set1 */
1809 int
1810 OP_7C0 ()
1811 {
1812   unsigned int op0, op1, op2;
1813   int temp;
1814
1815   trace_input ("set1", OP_BIT, 0);
1816   op0 = State.regs[ OP[0] ];
1817   op1 = OP[1] & 0x7;
1818   temp = EXTEND16 (OP[2]);
1819   op2 = temp;
1820   temp = load_mem (op0 + op2, 1);
1821   PSW &= ~PSW_Z;
1822   if ((temp & (1 << op1)) == 0)
1823     PSW |= PSW_Z;
1824   temp |= (1 << op1);
1825   store_mem (op0 + op2, 1, temp);
1826   trace_output (OP_BIT);
1827
1828   return 4;
1829 }
1830
1831 /* not1 */
1832 int
1833 OP_47C0 ()
1834 {
1835   unsigned int op0, op1, op2;
1836   int temp;
1837
1838   trace_input ("not1", OP_BIT, 0);
1839   op0 = State.regs[ OP[0] ];
1840   op1 = OP[1] & 0x7;
1841   temp = EXTEND16 (OP[2]);
1842   op2 = temp;
1843   temp = load_mem (op0 + op2, 1);
1844   PSW &= ~PSW_Z;
1845   if ((temp & (1 << op1)) == 0)
1846     PSW |= PSW_Z;
1847   temp ^= (1 << op1);
1848   store_mem (op0 + op2, 1, temp);
1849   trace_output (OP_BIT);
1850
1851   return 4;
1852 }
1853
1854 /* clr1 */
1855 int
1856 OP_87C0 ()
1857 {
1858   unsigned int op0, op1, op2;
1859   int temp;
1860
1861   trace_input ("clr1", OP_BIT, 0);
1862   op0 = State.regs[ OP[0] ];
1863   op1 = OP[1] & 0x7;
1864   temp = EXTEND16 (OP[2]);
1865   op2 = temp;
1866   temp = load_mem (op0 + op2, 1);
1867   PSW &= ~PSW_Z;
1868   if ((temp & (1 << op1)) == 0)
1869     PSW |= PSW_Z;
1870   temp &= ~(1 << op1);
1871   store_mem (op0 + op2, 1, temp);
1872   trace_output (OP_BIT);
1873
1874   return 4;
1875 }
1876
1877 /* tst1 */
1878 int
1879 OP_C7C0 ()
1880 {
1881   unsigned int op0, op1, op2;
1882   int temp;
1883
1884   trace_input ("tst1", OP_BIT, 0);
1885   op0 = State.regs[ OP[0] ];
1886   op1 = OP[1] & 0x7;
1887   temp = EXTEND16 (OP[2]);
1888   op2 = temp;
1889   temp = load_mem (op0 + op2, 1);
1890   PSW &= ~PSW_Z;
1891   if ((temp & (1 << op1)) == 0)
1892     PSW |= PSW_Z;
1893   trace_output (OP_BIT);
1894
1895   return 4;
1896 }
1897
1898 /* di */
1899 int
1900 OP_16007E0 ()
1901 {
1902   trace_input ("di", OP_NONE, 0);
1903   PSW |= PSW_ID;
1904   trace_output (OP_NONE);
1905
1906   return 4;
1907 }
1908
1909 /* ei */
1910 int
1911 OP_16087E0 ()
1912 {
1913   trace_input ("ei", OP_NONE, 0);
1914   PSW &= ~PSW_ID;
1915   trace_output (OP_NONE);
1916
1917   return 4;
1918 }
1919
1920 /* halt */
1921 int
1922 OP_12007E0 ()
1923 {
1924   trace_input ("halt", OP_NONE, 0);
1925   /* FIXME this should put processor into a mode where NMI still handled */
1926   trace_output (OP_NONE);
1927   sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1928                    sim_stopped, SIGTRAP);
1929   return 0;
1930 }
1931
1932 /* reti */
1933 int
1934 OP_14007E0 ()
1935 {
1936   trace_input ("reti", OP_NONE, 0);
1937   trace_output (OP_NONE);
1938
1939   /* Restore for NMI if only NP on, otherwise is interrupt or exception.  */
1940   if ((PSW & (PSW_NP | PSW_EP)) == PSW_NP)
1941     {
1942       PC = FEPC - 4;
1943       PSW = FEPSW;
1944     }
1945   else
1946     {
1947       PC = EIPC - 4;
1948       PSW = EIPSW;
1949     }
1950
1951   return 0;
1952 }
1953
1954 /* trap */
1955 int
1956 OP_10007E0 ()
1957 {
1958   trace_input ("trap", OP_TRAP, 0);
1959   trace_output (OP_TRAP);
1960
1961   /* Trap 31 is used for simulating OS I/O functions */
1962
1963   if (OP[0] == 31)
1964     {
1965       int save_errno = errno;   
1966       errno = 0;
1967
1968 /* Registers passed to trap 0 */
1969
1970 #define FUNC   State.regs[6]    /* function number, return value */
1971 #define PARM1  State.regs[7]    /* optional parm 1 */
1972 #define PARM2  State.regs[8]    /* optional parm 2 */
1973 #define PARM3  State.regs[9]    /* optional parm 3 */
1974
1975 /* Registers set by trap 0 */
1976
1977 #define RETVAL State.regs[10]   /* return value */
1978 #define RETERR State.regs[11]   /* return error code */
1979
1980 /* Turn a pointer in a register into a pointer into real memory. */
1981
1982 #define MEMPTR(x) (map (x))
1983
1984       switch (FUNC)
1985         {
1986
1987 #ifdef HAVE_FORK
1988 #ifdef SYS_fork
1989         case SYS_fork:
1990           RETVAL = fork ();
1991           break;
1992 #endif
1993 #endif
1994
1995 #ifdef HAVE_EXECVE
1996 #ifdef SYS_execv
1997         case SYS_execve:
1998           {
1999             char *path = fetch_str (simulator, PARM1);
2000             char **argv = fetch_argv (simulator, PARM2);
2001             char **envp = fetch_argv (simulator, PARM3);
2002             RETVAL = execve (path, argv, envp);
2003             zfree (path);
2004             freeargv (argv);
2005             freeargv (envp);
2006             break;
2007           }
2008 #endif
2009 #endif
2010
2011 #if HAVE_EXECV
2012 #ifdef SYS_execv
2013         case SYS_execv:
2014           {
2015             char *path = fetch_str (simulator, PARM1);
2016             char **argv = fetch_argv (simulator, PARM2);
2017             RETVAL = execv (path, argv);
2018             zfree (path);
2019             freeargv (argv);
2020             break;
2021           }
2022 #endif
2023 #endif
2024
2025 #if 0
2026 #ifdef SYS_pipe
2027         case SYS_pipe:
2028           {
2029             reg_t buf;
2030             int host_fd[2];
2031
2032             buf = PARM1;
2033             RETVAL = pipe (host_fd);
2034             SW (buf, host_fd[0]);
2035             buf += sizeof(uint16);
2036             SW (buf, host_fd[1]);
2037           }
2038           break;
2039 #endif
2040 #endif
2041
2042 #if 0
2043 #ifdef SYS_wait
2044         case SYS_wait:
2045           {
2046             int status;
2047
2048             RETVAL = wait (&status);
2049             SW (PARM1, status);
2050           }
2051           break;
2052 #endif
2053 #endif
2054
2055 #ifdef SYS_read
2056         case SYS_read:
2057           {
2058             char *buf = zalloc (PARM3);
2059             RETVAL = sim_io_read (simulator, PARM1, buf, PARM3);
2060             sim_write (simulator, PARM2, buf, PARM3);
2061             zfree (buf);
2062             break;
2063           }
2064 #endif
2065
2066 #ifdef SYS_write
2067         case SYS_write:
2068           {
2069             char *buf = zalloc (PARM3);
2070             sim_read (simulator, PARM2, buf, PARM3);
2071             if (PARM1 == 1)
2072               RETVAL = sim_io_write_stdout (simulator, buf, PARM3);
2073             else
2074               RETVAL = sim_io_write (simulator, PARM1, buf, PARM3);
2075             zfree (buf);
2076             break;
2077           }
2078 #endif
2079
2080 #ifdef SYS_lseek
2081         case SYS_lseek:
2082           RETVAL = sim_io_lseek (simulator, PARM1, PARM2, PARM3);
2083           break;
2084 #endif
2085
2086 #ifdef SYS_close
2087         case SYS_close:
2088           RETVAL = sim_io_close (simulator, PARM1);
2089           break;
2090 #endif
2091
2092 #ifdef SYS_open
2093         case SYS_open:
2094           {
2095             char *buf = fetch_str (simulator, PARM1);
2096             RETVAL = sim_io_open (simulator, buf, PARM2);
2097             zfree (buf);
2098             break;
2099           }
2100 #endif
2101
2102 #ifdef SYS_exit
2103         case SYS_exit:
2104           if ((PARM1 & 0xffff0000) == 0xdead0000 && (PARM1 & 0xffff) != 0)
2105             /* get signal encoded by kill */
2106             sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
2107                              sim_signalled, PARM1 & 0xffff);
2108           else if (PARM1 == 0xdead)
2109             /* old libraries */
2110             sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
2111                              sim_exited, SIGABRT);
2112           else
2113             /* PARM1 has exit status */
2114             sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
2115                              sim_exited, PARM1);
2116           break;
2117 #endif
2118
2119 #if !defined(__GO32__) && !defined(_WIN32)
2120 #ifdef SYS_stat
2121         case SYS_stat:  /* added at hmsi */
2122           /* stat system call */
2123           {
2124             struct stat host_stat;
2125             reg_t buf;
2126             char *path = fetch_str (simulator, PARM1);
2127
2128             RETVAL = stat (path, &host_stat);
2129
2130             zfree (path);
2131             buf = PARM2;
2132
2133             /* Just wild-assed guesses.  */
2134             store_mem (buf, 2, host_stat.st_dev);
2135             store_mem (buf + 2, 2, host_stat.st_ino);
2136             store_mem (buf + 4, 4, host_stat.st_mode);
2137             store_mem (buf + 8, 2, host_stat.st_nlink);
2138             store_mem (buf + 10, 2, host_stat.st_uid);
2139             store_mem (buf + 12, 2, host_stat.st_gid);
2140             store_mem (buf + 14, 2, host_stat.st_rdev);
2141             store_mem (buf + 16, 4, host_stat.st_size);
2142             store_mem (buf + 20, 4, host_stat.st_atime);
2143             store_mem (buf + 28, 4, host_stat.st_mtime);
2144             store_mem (buf + 36, 4, host_stat.st_ctime);
2145           }
2146           break;
2147 #endif
2148 #endif
2149
2150 #ifdef HAVE_CHOWN
2151 #ifdef SYS_chown
2152         case SYS_chown:
2153           {
2154             char *path = fetch_str (simulator, PARM1);
2155             RETVAL = chown (path, PARM2, PARM3);
2156             zfree (path);
2157           }
2158           break;
2159 #endif
2160 #endif
2161
2162 #if HAVE_CHMOD
2163 #ifdef SYS_chmod
2164         case SYS_chmod:
2165           {
2166             char *path = fetch_str (simulator, PARM1);
2167             RETVAL = chmod (path, PARM2);
2168             zfree (path);
2169           }
2170           break;
2171 #endif
2172 #endif
2173
2174 #ifdef SYS_time
2175 #if HAVE_TIME
2176         case SYS_time:
2177           {
2178             time_t now;
2179             RETVAL = time (&now);
2180             store_mem (PARM1, 4, now);
2181           }
2182           break;
2183 #endif
2184 #endif
2185
2186 #if !defined(__GO32__) && !defined(_WIN32)
2187 #ifdef SYS_times
2188         case SYS_times:
2189           {
2190             struct tms tms;
2191             RETVAL = times (&tms);
2192             store_mem (PARM1, 4, tms.tms_utime);
2193             store_mem (PARM1 + 4, 4, tms.tms_stime);
2194             store_mem (PARM1 + 8, 4, tms.tms_cutime);
2195             store_mem (PARM1 + 12, 4, tms.tms_cstime);
2196             break;
2197           }
2198 #endif
2199 #endif
2200
2201 #ifdef SYS_gettimeofday
2202 #if !defined(__GO32__) && !defined(_WIN32)
2203         case SYS_gettimeofday:
2204           {
2205             struct timeval t;
2206             struct timezone tz;
2207             RETVAL = gettimeofday (&t, &tz);
2208             store_mem (PARM1, 4, t.tv_sec);
2209             store_mem (PARM1 + 4, 4, t.tv_usec);
2210             store_mem (PARM2, 4, tz.tz_minuteswest);
2211             store_mem (PARM2 + 4, 4, tz.tz_dsttime);
2212             break;
2213           }
2214 #endif
2215 #endif
2216
2217 #ifdef SYS_utime
2218 #if HAVE_UTIME
2219         case SYS_utime:
2220           {
2221             /* Cast the second argument to void *, to avoid type mismatch
2222                if a prototype is present.  */
2223             sim_io_error (simulator, "Utime not supported");
2224             /* RETVAL = utime (path, (void *) MEMPTR (PARM2)); */
2225           }
2226           break;
2227 #endif
2228 #endif
2229
2230         default:
2231           abort ();
2232         }
2233       RETERR = errno;
2234       errno = save_errno;
2235
2236       return 4;
2237     }
2238   else
2239     {                           /* Trap 0 -> 30 */
2240       EIPC = PC + 4;
2241       EIPSW = PSW;
2242       /* Mask out EICC */
2243       ECR &= 0xffff0000;
2244       ECR |= 0x40 + OP[0];
2245       /* Flag that we are now doing exception processing.  */
2246       PSW |= PSW_EP | PSW_ID;
2247       PC = ((OP[0] < 0x10) ? 0x40 : 0x50) - 4;
2248
2249       return 0;
2250     }
2251 }
2252
2253 /* start-sanitize-v850e */
2254 /* tst1 reg2, [reg1] */
2255 int
2256 OP_E607E0 (void)
2257 {
2258   int temp;
2259
2260   trace_input ("tst1", OP_BIT, 1);
2261
2262   temp = load_mem (State.regs[ OP[0] ], 1);
2263   
2264   PSW &= ~PSW_Z;
2265   if ((temp & (1 << State.regs[ OP[1] & 0x7 ])) == 0)
2266     PSW |= PSW_Z;
2267   
2268   trace_output (OP_BIT);
2269
2270   return 4;
2271 }
2272
2273 /* end-sanitize-v850e */
2274 /* start-sanitize-v850e */
2275 /* mulu reg1, reg2, reg3 */
2276 int
2277 OP_22207E0 (void)
2278 {
2279   trace_input ("mulu", OP_REG_REG_REG, 0);
2280
2281   Multiply64 (false, State.regs[ OP[0] ]);
2282
2283   trace_output (OP_REG_REG_REG);
2284
2285   return 4;
2286 }
2287
2288 /* end-sanitize-v850e */
2289 /* start-sanitize-v850e */
2290
2291 #define BIT_CHANGE_OP( name, binop )            \
2292   unsigned int bit;                             \
2293   unsigned int temp;                            \
2294                                                 \
2295   trace_input (name, OP_BIT_CHANGE, 0);         \
2296                                                 \
2297   bit  = 1 << State.regs[ OP[1] & 0x7 ];        \
2298   temp = load_mem (State.regs[ OP[0] ], 1);     \
2299                                                 \
2300   PSW &= ~PSW_Z;                                \
2301   if ((temp & bit) == 0)                        \
2302     PSW |= PSW_Z;                               \
2303   temp binop bit;                               \
2304                                                 \
2305   store_mem (State.regs[ OP[0] ], 1, temp);     \
2306                                                 \
2307   trace_output (OP_BIT_CHANGE);                 \
2308                                                 \
2309   return 4;
2310
2311 /* clr1 reg2, [reg1] */
2312 int
2313 OP_E407E0 (void)
2314 {
2315   BIT_CHANGE_OP ("clr1", &= ~ );
2316 }
2317
2318 /* not1 reg2, [reg1] */
2319 int
2320 OP_E207E0 (void)
2321 {
2322   BIT_CHANGE_OP ("not1", ^= );
2323 }
2324
2325 /* set1 */
2326 int
2327 OP_E007E0 (void)
2328 {
2329   BIT_CHANGE_OP ("set1", |= );
2330 }
2331
2332 /* sasf */
2333 int
2334 OP_20007E0 (void)
2335 {
2336   trace_input ("sasf", OP_EX1, 0);
2337   
2338   State.regs[ OP[1] ] = (State.regs[ OP[1] ] << 1) | condition_met (OP[0]);
2339   
2340   trace_output (OP_EX1);
2341
2342   return 4;
2343 }
2344 /* end-sanitize-v850e */
2345 /* start-sanitize-v850eq */
2346 /* This function is courtesy of Sugimoto at NEC, via Seow Tan (Soew_Tan@el.nec.com) */
2347 void
2348 divun
2349 (
2350   unsigned int       N,
2351   unsigned long int  als,
2352   unsigned long int  sfi,
2353   unsigned long int *  quotient_ptr,
2354   unsigned long int *  remainder_ptr,
2355   boolean *          overflow_ptr
2356 )
2357 {
2358   unsigned long   ald = sfi >> (N - 1);
2359   unsigned long   alo = als;
2360   unsigned int    Q   = 1;
2361   unsigned int    C;
2362   unsigned int    S   = 0;
2363   unsigned int    i;
2364   unsigned int    R1  = 1;
2365   unsigned int    DBZ = (als == 0) ? 1 : 0;
2366   unsigned long   alt = Q ? ~als : als;
2367
2368   /* 1st Loop */
2369   alo = ald + alt + Q;
2370   C   = (((alt >> 31) & (ald >> 31))
2371          | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2372   C   = C ^ Q;
2373   Q   = ~(C ^ S) & 1;
2374   R1  = (alo == 0) ? 0 : (R1 & Q);
2375   if ((S ^ (alo>>31)) && !C)
2376     {
2377       DBZ = 1;
2378     }
2379   S   = alo >> 31;
2380   sfi = (sfi << (32-N+1)) | Q;
2381   ald = (alo << 1) | (sfi >> 31);
2382
2383   /* 2nd - N-1th Loop */
2384   for (i = 2; i < N; i++)
2385     {
2386       alt = Q ? ~als : als;
2387       alo = ald + alt + Q;
2388       C   = (((alt >> 31) & (ald >> 31))
2389              | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2390       C   = C ^ Q;
2391       Q   = ~(C ^ S) & 1;
2392       R1  = (alo == 0) ? 0 : (R1 & Q);
2393       if ((S ^ (alo>>31)) && !C && !DBZ)
2394         {
2395           DBZ = 1;
2396         }
2397       S   = alo >> 31;
2398       sfi = (sfi << 1) | Q;
2399       ald = (alo << 1) | (sfi >> 31);
2400     }
2401   
2402   /* Nth Loop */
2403   alt = Q ? ~als : als;
2404   alo = ald + alt + Q;
2405   C   = (((alt >> 31) & (ald >> 31))
2406          | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2407   C   = C ^ Q;
2408   Q   = ~(C ^ S) & 1;
2409   R1  = (alo == 0) ? 0 : (R1 & Q);
2410   if ((S ^ (alo>>31)) && !C)
2411     {
2412       DBZ = 1;
2413     }
2414   
2415   * quotient_ptr  = (sfi << 1) | Q;
2416   * remainder_ptr = Q ? alo : (alo + als);
2417   * overflow_ptr  = DBZ | R1;
2418 }
2419
2420 /* This function is courtesy of Sugimoto at NEC, via Seow Tan (Soew_Tan@el.nec.com) */
2421 void
2422 divn
2423 (
2424   unsigned int       N,
2425   unsigned long int  als,
2426   unsigned long int  sfi,
2427   signed long int *  quotient_ptr,
2428   signed long int *  remainder_ptr,
2429   boolean *          overflow_ptr
2430 )
2431 {
2432   unsigned long   ald = (signed long) sfi >> (N - 1);
2433   unsigned long   alo = als;
2434   unsigned int    SS  = als >> 31;
2435   unsigned int    SD  = sfi >> 31;
2436   unsigned int    R1  = 1;
2437   unsigned int    OV;
2438   unsigned int    DBZ = als == 0 ? 1 : 0;
2439   unsigned int    Q   = ~(SS ^ SD) & 1;
2440   unsigned int    C;
2441   unsigned int    S;
2442   unsigned int    i;
2443   unsigned long   alt = Q ? ~als : als;
2444
2445
2446   /* 1st Loop */
2447   
2448   alo = ald + alt + Q;
2449   C   = (((alt >> 31) & (ald >> 31))
2450          | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2451   Q   = C ^ SS;
2452   R1  = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2453   S   = alo >> 31;
2454   sfi = (sfi << (32-N+1)) | Q;
2455   ald = (alo << 1) | (sfi >> 31);
2456   if ((alo >> 31) ^ (ald >> 31))
2457     {
2458       DBZ = 1;
2459     }
2460
2461   /* 2nd - N-1th Loop */
2462   
2463   for (i = 2; i < N; i++)
2464     {
2465       alt = Q ? ~als : als;
2466       alo = ald + alt + Q;
2467       C   = (((alt >> 31) & (ald >> 31))
2468              | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2469       Q   = C ^ SS;
2470       R1  = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2471       S   = alo >> 31;
2472       sfi = (sfi << 1) | Q;
2473       ald = (alo << 1) | (sfi >> 31);
2474       if ((alo >> 31) ^ (ald >> 31))
2475         {
2476           DBZ = 1;
2477         }
2478     }
2479
2480   /* Nth Loop */
2481   alt = Q ? ~als : als;
2482   alo = ald + alt + Q;
2483   C   = (((alt >> 31) & (ald >> 31))
2484          | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2485   Q   = C ^ SS;
2486   R1  = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2487   sfi = (sfi << (32-N+1));
2488   ald = alo;
2489
2490   /* End */
2491   if (alo != 0)
2492     {
2493       alt = Q ? ~als : als;
2494       alo = ald + alt + Q;
2495     }
2496   R1  = R1 & ((~alo >> 31) ^ SD);
2497   if ((alo != 0) && ((Q ^ (SS ^ SD)) ^ R1)) alo = ald;
2498   if (N != 32)
2499     ald = sfi = (long) ((sfi >> 1) | (SS ^ SD) << 31) >> (32-N-1) | Q;
2500   else
2501     ald = sfi = sfi | Q;
2502   
2503   OV = DBZ | ((alo == 0) ? 0 : R1);
2504   
2505   * remainder_ptr = alo;
2506
2507   /* Adj */
2508   if (((alo != 0) && ((SS ^ SD) ^ R1))
2509       || ((alo == 0) && (SS ^ R1)))
2510     alo = ald + 1;
2511   else
2512     alo = ald;
2513   
2514   OV  = (DBZ | R1) ? OV : ((alo >> 31) & (~ald >> 31));
2515
2516   * quotient_ptr  = alo;
2517   * overflow_ptr  = OV;
2518 }
2519
2520 /* sdivun imm5, reg1, reg2, reg3 */
2521 int
2522 OP_1C207E0 (void)
2523 {
2524   unsigned long int  quotient;
2525   unsigned long int  remainder;
2526   unsigned long int  divide_by;
2527   unsigned long int  divide_this;
2528   boolean            overflow = false;
2529   unsigned int       imm5;
2530       
2531   trace_input ("sdivun", OP_IMM_REG_REG_REG, 0);
2532
2533   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2534
2535   divide_by   = State.regs[ OP[0] ];
2536   divide_this = State.regs[ OP[1] ] << imm5;
2537
2538   divun (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2539   
2540   State.regs[ OP[1]       ] = quotient;
2541   State.regs[ OP[2] >> 11 ] = remainder;
2542   
2543   /* Set condition codes.  */
2544   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2545   
2546   if (overflow)      PSW |= PSW_OV;
2547   if (quotient == 0) PSW |= PSW_Z;
2548   if (quotient & 0x80000000) PSW |= PSW_S;
2549   
2550   trace_output (OP_IMM_REG_REG_REG);
2551
2552   return 4;
2553 }
2554
2555 /* sdivn imm5, reg1, reg2, reg3 */
2556 int
2557 OP_1C007E0 (void)
2558 {
2559   signed long int  quotient;
2560   signed long int  remainder;
2561   signed long int  divide_by;
2562   signed long int  divide_this;
2563   boolean          overflow = false;
2564   unsigned int     imm5;
2565       
2566   trace_input ("sdivn", OP_IMM_REG_REG_REG, 0);
2567
2568   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2569
2570   divide_by   = State.regs[ OP[0] ];
2571   divide_this = State.regs[ OP[1] ] << imm5;
2572
2573   divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2574   
2575   State.regs[ OP[1]       ] = quotient;
2576   State.regs[ OP[2] >> 11 ] = remainder;
2577   
2578   /* Set condition codes.  */
2579   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2580   
2581   if (overflow)      PSW |= PSW_OV;
2582   if (quotient == 0) PSW |= PSW_Z;
2583   if (quotient <  0) PSW |= PSW_S;
2584   
2585   trace_output (OP_IMM_REG_REG_REG);
2586
2587   return 4;
2588 }
2589
2590 /* sdivhun imm5, reg1, reg2, reg3 */
2591 int
2592 OP_18207E0 (void)
2593 {
2594   unsigned long int  quotient;
2595   unsigned long int  remainder;
2596   unsigned long int  divide_by;
2597   unsigned long int  divide_this;
2598   boolean            overflow = false;
2599   unsigned int       imm5;
2600       
2601   trace_input ("sdivhun", OP_IMM_REG_REG_REG, 0);
2602
2603   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2604
2605   divide_by   = State.regs[ OP[0] ] & 0xffff;
2606   divide_this = State.regs[ OP[1] ] << imm5;
2607
2608   divun (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2609   
2610   State.regs[ OP[1]       ] = quotient;
2611   State.regs[ OP[2] >> 11 ] = remainder;
2612   
2613   /* Set condition codes.  */
2614   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2615   
2616   if (overflow)      PSW |= PSW_OV;
2617   if (quotient == 0) PSW |= PSW_Z;
2618   if (quotient & 0x80000000) PSW |= PSW_S;
2619   
2620   trace_output (OP_IMM_REG_REG_REG);
2621
2622   return 4;
2623 }
2624
2625 /* sdivhn imm5, reg1, reg2, reg3 */
2626 int
2627 OP_18007E0 (void)
2628 {
2629   signed long int  quotient;
2630   signed long int  remainder;
2631   signed long int  divide_by;
2632   signed long int  divide_this;
2633   boolean          overflow = false;
2634   unsigned int     imm5;
2635       
2636   trace_input ("sdivhn", OP_IMM_REG_REG_REG, 0);
2637
2638   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2639
2640   divide_by   = EXTEND16 (State.regs[ OP[0] ]);
2641   divide_this = State.regs[ OP[1] ] << imm5;
2642
2643   divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2644   
2645   State.regs[ OP[1]       ] = quotient;
2646   State.regs[ OP[2] >> 11 ] = remainder;
2647   
2648   /* Set condition codes.  */
2649   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2650   
2651   if (overflow)      PSW |= PSW_OV;
2652   if (quotient == 0) PSW |= PSW_Z;
2653   if (quotient <  0) PSW |= PSW_S;
2654   
2655   trace_output (OP_IMM_REG_REG_REG);
2656
2657   return 4;
2658 }
2659
2660 /* end-sanitize-v850eq */
2661 /* start-sanitize-v850e */
2662 /* divu  reg1, reg2, reg3 */
2663 int
2664 OP_2C207E0 (void)
2665 {
2666   unsigned long int quotient;
2667   unsigned long int remainder;
2668   unsigned long int divide_by;
2669   unsigned long int divide_this;
2670   boolean           overflow = false;
2671   
2672   trace_input ("divu", OP_REG_REG_REG, 0);
2673   
2674   /* Compute the result.  */
2675   
2676   divide_by   = State.regs[ OP[0] ];
2677   divide_this = State.regs[ OP[1] ];
2678   
2679   if (divide_by == 0)
2680     {
2681       overflow = true;
2682       divide_by  = 1;
2683     }
2684   
2685   State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
2686   State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2687   
2688   /* Set condition codes.  */
2689   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2690   
2691   if (overflow)      PSW |= PSW_OV;
2692   if (quotient == 0) PSW |= PSW_Z;
2693   if (quotient & 0x80000000) PSW |= PSW_S;
2694   
2695   trace_output (OP_REG_REG_REG);
2696
2697   return 4;
2698 }
2699
2700 /* end-sanitize-v850e */
2701 /* start-sanitize-v850e */
2702 /* div  reg1, reg2, reg3 */
2703 int
2704 OP_2C007E0 (void)
2705 {
2706   signed long int quotient;
2707   signed long int remainder;
2708   signed long int divide_by;
2709   signed long int divide_this;
2710   boolean         overflow = false;
2711   
2712   trace_input ("div", OP_REG_REG_REG, 0);
2713   
2714   /* Compute the result.  */
2715   
2716   divide_by   = State.regs[ OP[0] ];
2717   divide_this = State.regs[ OP[1] ];
2718   
2719   if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
2720     {
2721       overflow  = true;
2722       divide_by = 1;
2723     }
2724   
2725   State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
2726   State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2727   
2728   /* Set condition codes.  */
2729   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2730   
2731   if (overflow)      PSW |= PSW_OV;
2732   if (quotient == 0) PSW |= PSW_Z;
2733   if (quotient <  0) PSW |= PSW_S;
2734   
2735   trace_output (OP_REG_REG_REG);
2736
2737   return 4;
2738 }
2739
2740 /* end-sanitize-v850e */
2741 /* start-sanitize-v850e */
2742 /* divhu  reg1, reg2, reg3 */
2743 int
2744 OP_28207E0 (void)
2745 {
2746   unsigned long int quotient;
2747   unsigned long int remainder;
2748   unsigned long int divide_by;
2749   unsigned long int divide_this;
2750   boolean           overflow = false;
2751   
2752   trace_input ("divhu", OP_REG_REG_REG, 0);
2753   
2754   /* Compute the result.  */
2755   
2756   divide_by   = State.regs[ OP[0] ] & 0xffff;
2757   divide_this = State.regs[ OP[1] ];
2758   
2759   if (divide_by == 0)
2760     {
2761       overflow = true;
2762       divide_by  = 1;
2763     }
2764   
2765   State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
2766   State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2767   
2768   /* Set condition codes.  */
2769   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2770   
2771   if (overflow)      PSW |= PSW_OV;
2772   if (quotient == 0) PSW |= PSW_Z;
2773   if (quotient & 0x80000000) PSW |= PSW_S;
2774   
2775   trace_output (OP_REG_REG_REG);
2776
2777   return 4;
2778 }
2779
2780 /* end-sanitize-v850e */
2781 /* start-sanitize-v850e */
2782 /* divh  reg1, reg2, reg3 */
2783 int
2784 OP_28007E0 (void)
2785 {
2786   signed long int quotient;
2787   signed long int remainder;
2788   signed long int divide_by;
2789   signed long int divide_this;
2790   boolean         overflow = false;
2791   
2792   trace_input ("divh", OP_REG_REG_REG, 0);
2793   
2794   /* Compute the result.  */
2795   
2796   divide_by  = State.regs[ OP[0] ];
2797   divide_this = EXTEND16 (State.regs[ OP[1] ]);
2798   
2799   if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
2800     {
2801       overflow = true;
2802       divide_by  = 1;
2803     }
2804   
2805   State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
2806   State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2807   
2808   /* Set condition codes.  */
2809   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2810   
2811   if (overflow)      PSW |= PSW_OV;
2812   if (quotient == 0) PSW |= PSW_Z;
2813   if (quotient <  0) PSW |= PSW_S;
2814   
2815   trace_output (OP_REG_REG_REG);
2816
2817   return 4;
2818 }
2819
2820 /* end-sanitize-v850e */
2821 /* start-sanitize-v850e */
2822 /* mulu imm9, reg2, reg3 */
2823 int
2824 OP_24207E0 (void)
2825 {
2826   trace_input ("mulu", OP_IMM_REG_REG, 0);
2827
2828   Multiply64 (false, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
2829
2830   trace_output (OP_IMM_REG_REG);
2831
2832   return 4;
2833 }
2834
2835 /* end-sanitize-v850e */
2836 /* start-sanitize-v850e */
2837 /* mul imm9, reg2, reg3 */
2838 int
2839 OP_24007E0 (void)
2840 {
2841   trace_input ("mul", OP_IMM_REG_REG, 0);
2842
2843   Multiply64 (true, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
2844
2845   trace_output (OP_IMM_REG_REG);
2846
2847   return 4;
2848 }
2849
2850 /* end-sanitize-v850e */
2851 /* start-sanitize-v850e */
2852 /* cmov imm5, reg2, reg3 */
2853 int
2854 OP_30007E0 (void)
2855 {
2856   trace_input ("cmov", OP_IMM_REG_REG, 0);
2857
2858   State.regs[ OP[2] >> 11 ] = condition_met (OP[0]) ? SEXT5( OP[0] ) : State.regs[ OP[1] ];
2859   
2860   trace_output (OP_IMM_REG_REG);
2861
2862   return 4;
2863   
2864 }
2865
2866 /* end-sanitize-v850e */
2867 /* start-sanitize-v850e */
2868 /* ctret */
2869 int
2870 OP_14407E0 (void)
2871 {
2872   trace_input ("ctret", OP_NONE, 0);
2873
2874   PC  = CTPC;
2875   PSW = CTPSW;
2876
2877   trace_output (OP_NONE);
2878
2879   return 0;
2880 }
2881
2882 /* end-sanitize-v850e */
2883 /* start-sanitize-v850e */
2884 /* hsw */
2885 int
2886 OP_34407E0 (void)
2887 {
2888   unsigned long value;
2889   
2890   trace_input ("hsw", OP_REG_REG3, 0);
2891
2892   value = State.regs[ OP[ 1 ] ];
2893   value >>= 16;
2894   value |= (State.regs[ OP[ 1 ] ] << 16);
2895   
2896   State.regs[ OP[2] >> 11 ] = value;
2897
2898   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
2899
2900   if (value == 0) PSW |= PSW_Z;
2901   if (value & 0x80000000) PSW |= PSW_S;
2902   if (((value & 0xffff) == 0) || (value & 0xffff0000) == 0) PSW |= PSW_CY;
2903
2904   trace_output (OP_REG_REG3);
2905   
2906   return 4;
2907 }
2908
2909 /* end-sanitize-v850e */
2910 /* start-sanitize-v850e */
2911 #define WORDHASNULLBYTE(x) (((x) - 0x01010101) & ~(x)&0x80808080)
2912
2913 /* bsw */
2914 int
2915 OP_34007E0 (void)
2916 {
2917   unsigned long value;
2918   
2919   trace_input ("bsw", OP_REG_REG3, 0);
2920
2921   value = State.regs[ OP[ 1 ] ];
2922   value >>= 24;
2923   value |= (State.regs[ OP[ 1 ] ] << 24);
2924   value |= ((State.regs[ OP[ 1 ] ] << 8) & 0x00ff0000);
2925   value |= ((State.regs[ OP[ 1 ] ] >> 8) & 0x0000ff00);
2926   
2927   State.regs[ OP[2] >> 11 ] = value;
2928
2929   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
2930
2931   if (value == 0) PSW |= PSW_Z;
2932   if (value & 0x80000000) PSW |= PSW_S;
2933   if (WORDHASNULLBYTE (value)) PSW |= PSW_CY;
2934
2935   trace_output (OP_REG_REG3);
2936   
2937   return 4;
2938 }
2939
2940 /* end-sanitize-v850e */
2941 /* start-sanitize-v850e */
2942 /* bsh */
2943 int
2944 OP_34207E0 (void)
2945 {
2946   unsigned long value;
2947   
2948   trace_input ("bsh", OP_REG_REG3, 0);
2949
2950   value   = State.regs[ OP[ 1 ] ];
2951   value >>= 8;
2952   value  |= ((State.regs[ OP[ 1 ] ] << 8) & 0xff00ff00);
2953   value  |= ((State.regs[ OP[ 1 ] ] >> 8) & 0x000000ff);
2954   
2955   State.regs[ OP[2] >> 11 ] = value;
2956
2957   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
2958
2959   if (value == 0) PSW |= PSW_Z;
2960   if (value & 0x80000000) PSW |= PSW_S;
2961   if (((value & 0xff) == 0) || (value & 0x00ff) == 0) PSW |= PSW_CY;
2962
2963   trace_output (OP_REG_REG3);
2964   
2965   return 4;
2966 }
2967
2968 /* end-sanitize-v850e */
2969 /* start-sanitize-v850e */
2970 /* ld.hu */
2971 int
2972 OP_107E0 (void)
2973 {
2974   int adr;
2975
2976   trace_input ("ld.hu", OP_LOAD32, 2);
2977
2978   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
2979   adr &= ~0x1;
2980       
2981   State.regs[ OP[1] ] = load_mem (adr, 2);
2982       
2983   trace_output (OP_LOAD32);
2984   
2985   return 4;
2986 }
2987
2988 /* end-sanitize-v850e */
2989 /* start-sanitize-v850e */
2990 /* ld.bu */
2991 int
2992 OP_10780 (void)
2993 {
2994   int adr;
2995
2996   trace_input ("ld.bu", OP_LOAD32, 1);
2997
2998   adr = (State.regs[ OP[0] ]
2999          + (EXTEND16 (OP[2] & ~1) | ((OP[3] >> 5) & 1)));
3000       
3001   State.regs[ OP[1] ] = load_mem (adr, 1);
3002   
3003   trace_output (OP_LOAD32);
3004   
3005   return 4;
3006 }
3007
3008 /* prepare list12, imm5, imm32 */
3009 int
3010 OP_1B0780 (void)
3011 {
3012   int  i;
3013   
3014   trace_input ("prepare", OP_PUSHPOP1, 0);
3015   
3016   /* Store the registers with lower number registers being placed at higher addresses.  */
3017   for (i = 0; i < 12; i++)
3018     if ((OP[3] & (1 << type1_regs[ i ])))
3019       {
3020         SP -= 4;
3021         store_mem (SP, 4, State.regs[ 20 + i ]);
3022       }
3023   
3024   SP -= (OP[3] & 0x3e) << 1;
3025
3026   EP = load_mem (PC + 4, 4);
3027   
3028   trace_output (OP_PUSHPOP1);
3029
3030   return 8;
3031 }
3032
3033 /* prepare list12, imm5, imm16-32 */
3034 int
3035 OP_130780 (void)
3036 {
3037   int  i;
3038   
3039   trace_input ("prepare", OP_PUSHPOP1, 0);
3040   
3041   /* Store the registers with lower number registers being placed at higher addresses.  */
3042   for (i = 0; i < 12; i++)
3043     if ((OP[3] & (1 << type1_regs[ i ])))
3044       {
3045         SP -= 4;
3046         store_mem (SP, 4, State.regs[ 20 + i ]);
3047       }
3048   
3049   SP -= (OP[3] & 0x3e) << 1;
3050
3051   EP = load_mem (PC + 4, 2) << 16;
3052   
3053   trace_output (OP_PUSHPOP1);
3054
3055   return 6;
3056 }
3057
3058 /* prepare list12, imm5, imm16 */
3059 int
3060 OP_B0780 (void)
3061 {
3062   int  i;
3063   
3064   trace_input ("prepare", OP_PUSHPOP1, 0);
3065   
3066   /* Store the registers with lower number registers being placed at higher addresses.  */
3067   for (i = 0; i < 12; i++)
3068     if ((OP[3] & (1 << type1_regs[ i ])))
3069       {
3070         SP -= 4;
3071         store_mem (SP, 4, State.regs[ 20 + i ]);
3072       }
3073   
3074   SP -= (OP[3] & 0x3e) << 1;
3075
3076   EP = EXTEND16 (load_mem (PC + 4, 2));
3077   
3078   trace_output (OP_PUSHPOP1);
3079
3080   return 6;
3081 }
3082
3083 /* prepare list12, imm5, sp */
3084 int
3085 OP_30780 (void)
3086 {
3087   int  i;
3088   
3089   trace_input ("prepare", OP_PUSHPOP1, 0);
3090   
3091   /* Store the registers with lower number registers being placed at higher addresses.  */
3092   for (i = 0; i < 12; i++)
3093     if ((OP[3] & (1 << type1_regs[ i ])))
3094       {
3095         SP -= 4;
3096         store_mem (SP, 4, State.regs[ 20 + i ]);
3097       }
3098   
3099   SP -= (OP[3] & 0x3e) << 1;
3100
3101   EP = SP;
3102   
3103   trace_output (OP_PUSHPOP1);
3104
3105   return 4;
3106 }
3107
3108 /* end-sanitize-v850e */
3109 /* start-sanitize-v850e */
3110 /* sld.hu */
3111 int
3112 OP_70 (void)
3113 {
3114   unsigned long result;
3115   
3116   result  = load_mem (State.regs[30] + ((OP[3] & 0xf) << 1), 2);
3117
3118   /* start-sanitize-v850eq */
3119   if (PSW & PSW_US)
3120     {
3121       trace_input ("sld.h", OP_LOAD16, 2);
3122       State.regs[ OP[1] ] = EXTEND16 (result);
3123     }
3124   else
3125     {
3126 /* end-sanitize-v850eq */
3127   trace_input ("sld.hu", OP_LOAD16, 2);
3128   
3129   State.regs[ OP[1] ] = result;
3130 /* start-sanitize-v850eq */
3131     }
3132 /* end-sanitize-v850eq */
3133   
3134   trace_output (OP_LOAD16);
3135   
3136   return 2;
3137 }
3138
3139 /* end-sanitize-v850e */
3140 /* start-sanitize-v850e */
3141 /* cmov reg1, reg2, reg3 */
3142 int
3143 OP_32007E0 (void)
3144 {
3145   trace_input ("cmov", OP_REG_REG_REG, 0);
3146
3147   State.regs[ OP[2] >> 11 ] = condition_met (OP[0]) ? State.regs[ OP[0] ] : State.regs[ OP[1] ];
3148   
3149   trace_output (OP_REG_REG_REG);
3150
3151   return 4;
3152 }
3153
3154 /* end-sanitize-v850e */
3155 /* start-sanitize-v850e */
3156 /* mul reg1, reg2, reg3 */
3157 int
3158 OP_22007E0 (void)
3159 {
3160   trace_input ("mul", OP_REG_REG_REG, 0);
3161
3162   Multiply64 (true, State.regs[ OP[0] ]);
3163
3164   trace_output (OP_REG_REG_REG);
3165
3166   return 4;
3167 }
3168
3169 /* end-sanitize-v850e */
3170 /* start-sanitize-v850eq */
3171
3172 /* popmh list18 */
3173 int
3174 OP_307F0 (void)
3175 {
3176   int i;
3177   
3178   trace_input ("popmh", OP_PUSHPOP2, 0);
3179   
3180   if (OP[3] & (1 << 19))
3181     {
3182       if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
3183         {
3184           FEPSW = load_mem ( SP      & ~ 3, 4);
3185           FEPC  = load_mem ((SP + 4) & ~ 3, 4);
3186         }
3187       else
3188         {
3189           EIPSW = load_mem ( SP      & ~ 3, 4);
3190           EIPC  = load_mem ((SP + 4) & ~ 3, 4);
3191         }
3192       
3193       SP += 8;
3194     }
3195   
3196   /* Load the registers with lower number registers being retrieved from higher addresses.  */
3197   for (i = 16; i--;)
3198     if ((OP[3] & (1 << type2_regs[ i ])))
3199       {
3200         State.regs[ i + 16 ] = load_mem (SP & ~ 3, 4);
3201         SP += 4;
3202       }
3203   
3204   trace_output (OP_PUSHPOP2);
3205
3206   return 4;
3207 }
3208
3209 /* popml lsit18 */
3210 int
3211 OP_107F0 (void)
3212 {
3213   int i;
3214
3215   trace_input ("popml", OP_PUSHPOP3, 0);
3216
3217   if (OP[3] & (1 << 19))
3218     {
3219       if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
3220         {
3221           FEPSW = load_mem ( SP      & ~ 3, 4);
3222           FEPC =  load_mem ((SP + 4) & ~ 3, 4);
3223         }
3224       else
3225         {
3226           EIPSW = load_mem ( SP      & ~ 3, 4);
3227           EIPC  = load_mem ((SP + 4) & ~ 3, 4);
3228         }
3229       
3230       SP += 8;
3231     }
3232   
3233   if (OP[3] & (1 << 3))
3234     {
3235       PSW = load_mem (SP & ~ 3, 4);
3236       SP += 4;
3237     }
3238   
3239   /* Load the registers with lower number registers being retrieved from higher addresses.  */
3240   for (i = 15; i--;)
3241     if ((OP[3] & (1 << type3_regs[ i ])))
3242       {
3243         State.regs[ i + 1 ] = load_mem (SP & ~ 3, 4);
3244         SP += 4;
3245       }
3246   
3247   trace_output (OP_PUSHPOP2);
3248
3249   return 4;
3250 }
3251
3252 /* pushmh list18 */
3253 int
3254 OP_307E0 (void)
3255 {
3256   int i;
3257
3258   trace_input ("pushmh", OP_PUSHPOP2, 0);
3259   
3260   /* Store the registers with lower number registers being placed at higher addresses.  */
3261   for (i = 0; i < 16; i++)
3262     if ((OP[3] & (1 << type2_regs[ i ])))
3263       {
3264         SP -= 4;
3265         store_mem (SP & ~ 3, 4, State.regs[ i + 16 ]);
3266       }
3267   
3268   if (OP[3] & (1 << 19))
3269     {
3270       SP -= 8;
3271       
3272       if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
3273         {
3274           store_mem ((SP + 4) & ~ 3, 4, FEPC);
3275           store_mem ( SP      & ~ 3, 4, FEPSW);
3276         }
3277       else
3278         {
3279           store_mem ((SP + 4) & ~ 3, 4, EIPC);
3280           store_mem ( SP      & ~ 3, 4, EIPSW);
3281         }
3282     }
3283   
3284   trace_output (OP_PUSHPOP2);
3285
3286   return 4;
3287 }
3288
3289 /* end-sanitize-v850eq */