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