* simops.c (Multiply64): Don't store into register zero.
[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 /* breakpoint */
1939 int
1940 OP_FFFF ()
1941 {
1942   sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1943                    sim_stopped, SIGTRAP);
1944   return 0;
1945 }
1946
1947 /* di */
1948 int
1949 OP_16007E0 ()
1950 {
1951   trace_input ("di", OP_NONE, 0);
1952   PSW |= PSW_ID;
1953   trace_output (OP_NONE);
1954
1955   return 4;
1956 }
1957
1958 /* ei */
1959 int
1960 OP_16087E0 ()
1961 {
1962   trace_input ("ei", OP_NONE, 0);
1963   PSW &= ~PSW_ID;
1964   trace_output (OP_NONE);
1965
1966   return 4;
1967 }
1968
1969 /* halt */
1970 int
1971 OP_12007E0 ()
1972 {
1973   trace_input ("halt", OP_NONE, 0);
1974   /* FIXME this should put processor into a mode where NMI still handled */
1975   trace_output (OP_NONE);
1976   sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
1977                    sim_stopped, SIGTRAP);
1978   return 0;
1979 }
1980
1981 /* reti */
1982 int
1983 OP_14007E0 ()
1984 {
1985   trace_input ("reti", OP_NONE, 0);
1986   trace_output (OP_NONE);
1987
1988   /* Restore for NMI if only NP on, otherwise is interrupt or exception.  */
1989   if ((PSW & (PSW_NP | PSW_EP)) == PSW_NP)
1990     {
1991       PC = FEPC - 4;
1992       PSW = FEPSW;
1993     }
1994   else
1995     {
1996       PC = EIPC - 4;
1997       PSW = EIPSW;
1998     }
1999
2000   return 0;
2001 }
2002
2003 /* trap */
2004 int
2005 OP_10007E0 ()
2006 {
2007   trace_input ("trap", OP_TRAP, 0);
2008   trace_output (OP_TRAP);
2009
2010   /* Trap 31 is used for simulating OS I/O functions */
2011
2012   if (OP[0] == 31)
2013     {
2014       int save_errno = errno;   
2015       errno = 0;
2016
2017 /* Registers passed to trap 0 */
2018
2019 #define FUNC   State.regs[6]    /* function number, return value */
2020 #define PARM1  State.regs[7]    /* optional parm 1 */
2021 #define PARM2  State.regs[8]    /* optional parm 2 */
2022 #define PARM3  State.regs[9]    /* optional parm 3 */
2023
2024 /* Registers set by trap 0 */
2025
2026 #define RETVAL State.regs[10]   /* return value */
2027 #define RETERR State.regs[11]   /* return error code */
2028
2029 /* Turn a pointer in a register into a pointer into real memory. */
2030
2031 #define MEMPTR(x) (map (x))
2032
2033       switch (FUNC)
2034         {
2035
2036 #ifdef HAVE_FORK
2037 #ifdef SYS_fork
2038         case SYS_fork:
2039           RETVAL = fork ();
2040           break;
2041 #endif
2042 #endif
2043
2044 #ifdef HAVE_EXECVE
2045 #ifdef SYS_execv
2046         case SYS_execve:
2047           {
2048             char *path = fetch_str (simulator, PARM1);
2049             char **argv = fetch_argv (simulator, PARM2);
2050             char **envp = fetch_argv (simulator, PARM3);
2051             RETVAL = execve (path, argv, envp);
2052             zfree (path);
2053             freeargv (argv);
2054             freeargv (envp);
2055             break;
2056           }
2057 #endif
2058 #endif
2059
2060 #if HAVE_EXECV
2061 #ifdef SYS_execv
2062         case SYS_execv:
2063           {
2064             char *path = fetch_str (simulator, PARM1);
2065             char **argv = fetch_argv (simulator, PARM2);
2066             RETVAL = execv (path, argv);
2067             zfree (path);
2068             freeargv (argv);
2069             break;
2070           }
2071 #endif
2072 #endif
2073
2074 #if 0
2075 #ifdef SYS_pipe
2076         case SYS_pipe:
2077           {
2078             reg_t buf;
2079             int host_fd[2];
2080
2081             buf = PARM1;
2082             RETVAL = pipe (host_fd);
2083             SW (buf, host_fd[0]);
2084             buf += sizeof(uint16);
2085             SW (buf, host_fd[1]);
2086           }
2087           break;
2088 #endif
2089 #endif
2090
2091 #if 0
2092 #ifdef SYS_wait
2093         case SYS_wait:
2094           {
2095             int status;
2096
2097             RETVAL = wait (&status);
2098             SW (PARM1, status);
2099           }
2100           break;
2101 #endif
2102 #endif
2103
2104 #ifdef SYS_read
2105         case SYS_read:
2106           {
2107             char *buf = zalloc (PARM3);
2108             RETVAL = sim_io_read (simulator, PARM1, buf, PARM3);
2109             sim_write (simulator, PARM2, buf, PARM3);
2110             zfree (buf);
2111             break;
2112           }
2113 #endif
2114
2115 #ifdef SYS_write
2116         case SYS_write:
2117           {
2118             char *buf = zalloc (PARM3);
2119             sim_read (simulator, PARM2, buf, PARM3);
2120             if (PARM1 == 1)
2121               RETVAL = sim_io_write_stdout (simulator, buf, PARM3);
2122             else
2123               RETVAL = sim_io_write (simulator, PARM1, buf, PARM3);
2124             zfree (buf);
2125             break;
2126           }
2127 #endif
2128
2129 #ifdef SYS_lseek
2130         case SYS_lseek:
2131           RETVAL = sim_io_lseek (simulator, PARM1, PARM2, PARM3);
2132           break;
2133 #endif
2134
2135 #ifdef SYS_close
2136         case SYS_close:
2137           RETVAL = sim_io_close (simulator, PARM1);
2138           break;
2139 #endif
2140
2141 #ifdef SYS_open
2142         case SYS_open:
2143           {
2144             char *buf = fetch_str (simulator, PARM1);
2145             RETVAL = sim_io_open (simulator, buf, PARM2);
2146             zfree (buf);
2147             break;
2148           }
2149 #endif
2150
2151 #ifdef SYS_exit
2152         case SYS_exit:
2153           if ((PARM1 & 0xffff0000) == 0xdead0000 && (PARM1 & 0xffff) != 0)
2154             /* get signal encoded by kill */
2155             sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
2156                              sim_signalled, PARM1 & 0xffff);
2157           else if (PARM1 == 0xdead)
2158             /* old libraries */
2159             sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
2160                              sim_exited, SIGABRT);
2161           else
2162             /* PARM1 has exit status */
2163             sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
2164                              sim_exited, PARM1);
2165           break;
2166 #endif
2167
2168 #if !defined(__GO32__) && !defined(_WIN32)
2169 #ifdef SYS_stat
2170         case SYS_stat:  /* added at hmsi */
2171           /* stat system call */
2172           {
2173             struct stat host_stat;
2174             reg_t buf;
2175             char *path = fetch_str (simulator, PARM1);
2176
2177             RETVAL = stat (path, &host_stat);
2178
2179             zfree (path);
2180             buf = PARM2;
2181
2182             /* Just wild-assed guesses.  */
2183             store_mem (buf, 2, host_stat.st_dev);
2184             store_mem (buf + 2, 2, host_stat.st_ino);
2185             store_mem (buf + 4, 4, host_stat.st_mode);
2186             store_mem (buf + 8, 2, host_stat.st_nlink);
2187             store_mem (buf + 10, 2, host_stat.st_uid);
2188             store_mem (buf + 12, 2, host_stat.st_gid);
2189             store_mem (buf + 14, 2, host_stat.st_rdev);
2190             store_mem (buf + 16, 4, host_stat.st_size);
2191             store_mem (buf + 20, 4, host_stat.st_atime);
2192             store_mem (buf + 28, 4, host_stat.st_mtime);
2193             store_mem (buf + 36, 4, host_stat.st_ctime);
2194           }
2195           break;
2196 #endif
2197 #endif
2198
2199 #ifdef HAVE_CHOWN
2200 #ifdef SYS_chown
2201         case SYS_chown:
2202           {
2203             char *path = fetch_str (simulator, PARM1);
2204             RETVAL = chown (path, PARM2, PARM3);
2205             zfree (path);
2206           }
2207           break;
2208 #endif
2209 #endif
2210
2211 #if HAVE_CHMOD
2212 #ifdef SYS_chmod
2213         case SYS_chmod:
2214           {
2215             char *path = fetch_str (simulator, PARM1);
2216             RETVAL = chmod (path, PARM2);
2217             zfree (path);
2218           }
2219           break;
2220 #endif
2221 #endif
2222
2223 #ifdef SYS_time
2224 #if HAVE_TIME
2225         case SYS_time:
2226           {
2227             time_t now;
2228             RETVAL = time (&now);
2229             store_mem (PARM1, 4, now);
2230           }
2231           break;
2232 #endif
2233 #endif
2234
2235 #if !defined(__GO32__) && !defined(_WIN32)
2236 #ifdef SYS_times
2237         case SYS_times:
2238           {
2239             struct tms tms;
2240             RETVAL = times (&tms);
2241             store_mem (PARM1, 4, tms.tms_utime);
2242             store_mem (PARM1 + 4, 4, tms.tms_stime);
2243             store_mem (PARM1 + 8, 4, tms.tms_cutime);
2244             store_mem (PARM1 + 12, 4, tms.tms_cstime);
2245             break;
2246           }
2247 #endif
2248 #endif
2249
2250 #ifdef SYS_gettimeofday
2251 #if !defined(__GO32__) && !defined(_WIN32)
2252         case SYS_gettimeofday:
2253           {
2254             struct timeval t;
2255             struct timezone tz;
2256             RETVAL = gettimeofday (&t, &tz);
2257             store_mem (PARM1, 4, t.tv_sec);
2258             store_mem (PARM1 + 4, 4, t.tv_usec);
2259             store_mem (PARM2, 4, tz.tz_minuteswest);
2260             store_mem (PARM2 + 4, 4, tz.tz_dsttime);
2261             break;
2262           }
2263 #endif
2264 #endif
2265
2266 #ifdef SYS_utime
2267 #if HAVE_UTIME
2268         case SYS_utime:
2269           {
2270             /* Cast the second argument to void *, to avoid type mismatch
2271                if a prototype is present.  */
2272             sim_io_error (simulator, "Utime not supported");
2273             /* RETVAL = utime (path, (void *) MEMPTR (PARM2)); */
2274           }
2275           break;
2276 #endif
2277 #endif
2278
2279         default:
2280           abort ();
2281         }
2282       RETERR = errno;
2283       errno = save_errno;
2284
2285       return 4;
2286     }
2287   else
2288     {                           /* Trap 0 -> 30 */
2289       EIPC = PC + 4;
2290       EIPSW = PSW;
2291       /* Mask out EICC */
2292       ECR &= 0xffff0000;
2293       ECR |= 0x40 + OP[0];
2294       /* Flag that we are now doing exception processing.  */
2295       PSW |= PSW_EP | PSW_ID;
2296       PC = ((OP[0] < 0x10) ? 0x40 : 0x50) - 4;
2297
2298       return 0;
2299     }
2300 }
2301
2302 /* ldsr, reg,reg */
2303 int
2304 OP_2007E0 ()
2305 {
2306   trace_input ("ldsr", OP_LDSR, 0);
2307   
2308   State.sregs[ OP[1] ] = State.regs[ OP[0] ];
2309   
2310   trace_output (OP_LDSR);
2311
2312   return 4;
2313 }
2314
2315 /* stsr */
2316 int
2317 OP_4007E0 ()
2318 {
2319   trace_input ("stsr", OP_STSR, 0);
2320   
2321   State.regs[ OP[1] ] = State.sregs[ OP[0] ];
2322   
2323   trace_output (OP_STSR);
2324
2325   return 4;
2326 }
2327
2328 /* start-sanitize-v850e */
2329 /* tst1 reg2, [reg1] */
2330 int
2331 OP_E607E0 (void)
2332 {
2333   int temp;
2334
2335   trace_input ("tst1", OP_BIT, 1);
2336
2337   temp = load_mem (State.regs[ OP[0] ], 1);
2338   
2339   PSW &= ~PSW_Z;
2340   if ((temp & (1 << State.regs[ OP[1] & 0x7 ])) == 0)
2341     PSW |= PSW_Z;
2342   
2343   trace_output (OP_BIT);
2344
2345   return 4;
2346 }
2347
2348 /* end-sanitize-v850e */
2349 /* start-sanitize-v850e */
2350 /* mulu reg1, reg2, reg3 */
2351 int
2352 OP_22207E0 (void)
2353 {
2354   trace_input ("mulu", OP_REG_REG_REG, 0);
2355
2356   Multiply64 (false, State.regs[ OP[0] ]);
2357
2358   trace_output (OP_REG_REG_REG);
2359
2360   return 4;
2361 }
2362
2363 /* end-sanitize-v850e */
2364 /* start-sanitize-v850e */
2365
2366 #define BIT_CHANGE_OP( name, binop )            \
2367   unsigned int bit;                             \
2368   unsigned int temp;                            \
2369                                                 \
2370   trace_input (name, OP_BIT_CHANGE, 0);         \
2371                                                 \
2372   bit  = 1 << State.regs[ OP[1] & 0x7 ];        \
2373   temp = load_mem (State.regs[ OP[0] ], 1);     \
2374                                                 \
2375   PSW &= ~PSW_Z;                                \
2376   if ((temp & bit) == 0)                        \
2377     PSW |= PSW_Z;                               \
2378   temp binop bit;                               \
2379                                                 \
2380   store_mem (State.regs[ OP[0] ], 1, temp);     \
2381                                                 \
2382   trace_output (OP_BIT_CHANGE);                 \
2383                                                 \
2384   return 4;
2385
2386 /* clr1 reg2, [reg1] */
2387 int
2388 OP_E407E0 (void)
2389 {
2390   BIT_CHANGE_OP ("clr1", &= ~ );
2391 }
2392
2393 /* not1 reg2, [reg1] */
2394 int
2395 OP_E207E0 (void)
2396 {
2397   BIT_CHANGE_OP ("not1", ^= );
2398 }
2399
2400 /* set1 */
2401 int
2402 OP_E007E0 (void)
2403 {
2404   BIT_CHANGE_OP ("set1", |= );
2405 }
2406
2407 /* sasf */
2408 int
2409 OP_20007E0 (void)
2410 {
2411   trace_input ("sasf", OP_EX1, 0);
2412   
2413   State.regs[ OP[1] ] = (State.regs[ OP[1] ] << 1) | condition_met (OP[0]);
2414   
2415   trace_output (OP_EX1);
2416
2417   return 4;
2418 }
2419 /* end-sanitize-v850e */
2420 /* start-sanitize-v850eq */
2421 /* This function is courtesy of Sugimoto at NEC, via Seow Tan (Soew_Tan@el.nec.com) */
2422 void
2423 divun
2424 (
2425   unsigned int       N,
2426   unsigned long int  als,
2427   unsigned long int  sfi,
2428   unsigned long int *  quotient_ptr,
2429   unsigned long int *  remainder_ptr,
2430   boolean *          overflow_ptr
2431 )
2432 {
2433   unsigned long   ald = sfi >> (N - 1);
2434   unsigned long   alo = als;
2435   unsigned int    Q   = 1;
2436   unsigned int    C;
2437   unsigned int    S   = 0;
2438   unsigned int    i;
2439   unsigned int    R1  = 1;
2440   unsigned int    DBZ = (als == 0) ? 1 : 0;
2441   unsigned long   alt = Q ? ~als : als;
2442
2443   /* 1st Loop */
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   S   = alo >> 31;
2455   sfi = (sfi << (32-N+1)) | Q;
2456   ald = (alo << 1) | (sfi >> 31);
2457
2458   /* 2nd - N-1th Loop */
2459   for (i = 2; i < N; i++)
2460     {
2461       alt = Q ? ~als : als;
2462       alo = ald + alt + Q;
2463       C   = (((alt >> 31) & (ald >> 31))
2464              | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2465       C   = C ^ Q;
2466       Q   = ~(C ^ S) & 1;
2467       R1  = (alo == 0) ? 0 : (R1 & Q);
2468       if ((S ^ (alo>>31)) && !C && !DBZ)
2469         {
2470           DBZ = 1;
2471         }
2472       S   = alo >> 31;
2473       sfi = (sfi << 1) | Q;
2474       ald = (alo << 1) | (sfi >> 31);
2475     }
2476   
2477   /* Nth Loop */
2478   alt = Q ? ~als : als;
2479   alo = ald + alt + Q;
2480   C   = (((alt >> 31) & (ald >> 31))
2481          | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2482   C   = C ^ Q;
2483   Q   = ~(C ^ S) & 1;
2484   R1  = (alo == 0) ? 0 : (R1 & Q);
2485   if ((S ^ (alo>>31)) && !C)
2486     {
2487       DBZ = 1;
2488     }
2489   
2490   * quotient_ptr  = (sfi << 1) | Q;
2491   * remainder_ptr = Q ? alo : (alo + als);
2492   * overflow_ptr  = DBZ | R1;
2493 }
2494
2495 /* This function is courtesy of Sugimoto at NEC, via Seow Tan (Soew_Tan@el.nec.com) */
2496 void
2497 divn
2498 (
2499   unsigned int       N,
2500   unsigned long int  als,
2501   unsigned long int  sfi,
2502   signed long int *  quotient_ptr,
2503   signed long int *  remainder_ptr,
2504   boolean *          overflow_ptr
2505 )
2506 {
2507   unsigned long   ald = (signed long) sfi >> (N - 1);
2508   unsigned long   alo = als;
2509   unsigned int    SS  = als >> 31;
2510   unsigned int    SD  = sfi >> 31;
2511   unsigned int    R1  = 1;
2512   unsigned int    OV;
2513   unsigned int    DBZ = als == 0 ? 1 : 0;
2514   unsigned int    Q   = ~(SS ^ SD) & 1;
2515   unsigned int    C;
2516   unsigned int    S;
2517   unsigned int    i;
2518   unsigned long   alt = Q ? ~als : als;
2519
2520
2521   /* 1st Loop */
2522   
2523   alo = ald + alt + Q;
2524   C   = (((alt >> 31) & (ald >> 31))
2525          | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2526   Q   = C ^ SS;
2527   R1  = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2528   S   = alo >> 31;
2529   sfi = (sfi << (32-N+1)) | Q;
2530   ald = (alo << 1) | (sfi >> 31);
2531   if ((alo >> 31) ^ (ald >> 31))
2532     {
2533       DBZ = 1;
2534     }
2535
2536   /* 2nd - N-1th Loop */
2537   
2538   for (i = 2; i < N; i++)
2539     {
2540       alt = Q ? ~als : als;
2541       alo = ald + alt + Q;
2542       C   = (((alt >> 31) & (ald >> 31))
2543              | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2544       Q   = C ^ SS;
2545       R1  = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2546       S   = alo >> 31;
2547       sfi = (sfi << 1) | Q;
2548       ald = (alo << 1) | (sfi >> 31);
2549       if ((alo >> 31) ^ (ald >> 31))
2550         {
2551           DBZ = 1;
2552         }
2553     }
2554
2555   /* Nth Loop */
2556   alt = Q ? ~als : als;
2557   alo = ald + alt + Q;
2558   C   = (((alt >> 31) & (ald >> 31))
2559          | (((alt >> 31) ^ (ald >> 31)) & (~alo >> 31)));
2560   Q   = C ^ SS;
2561   R1  = (alo == 0) ? 0 : (R1 & (Q ^ (SS ^ SD)));
2562   sfi = (sfi << (32-N+1));
2563   ald = alo;
2564
2565   /* End */
2566   if (alo != 0)
2567     {
2568       alt = Q ? ~als : als;
2569       alo = ald + alt + Q;
2570     }
2571   R1  = R1 & ((~alo >> 31) ^ SD);
2572   if ((alo != 0) && ((Q ^ (SS ^ SD)) ^ R1)) alo = ald;
2573   if (N != 32)
2574     ald = sfi = (long) ((sfi >> 1) | (SS ^ SD) << 31) >> (32-N-1) | Q;
2575   else
2576     ald = sfi = sfi | Q;
2577   
2578   OV = DBZ | ((alo == 0) ? 0 : R1);
2579   
2580   * remainder_ptr = alo;
2581
2582   /* Adj */
2583   if (((alo != 0) && ((SS ^ SD) ^ R1))
2584       || ((alo == 0) && (SS ^ R1)))
2585     alo = ald + 1;
2586   else
2587     alo = ald;
2588   
2589   OV  = (DBZ | R1) ? OV : ((alo >> 31) & (~ald >> 31));
2590
2591   * quotient_ptr  = alo;
2592   * overflow_ptr  = OV;
2593 }
2594
2595 /* sdivun imm5, reg1, reg2, reg3 */
2596 int
2597 OP_1C207E0 (void)
2598 {
2599   unsigned long int  quotient;
2600   unsigned long int  remainder;
2601   unsigned long int  divide_by;
2602   unsigned long int  divide_this;
2603   boolean            overflow = false;
2604   unsigned int       imm5;
2605       
2606   trace_input ("sdivun", 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   divun (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 & 0x80000000) PSW |= PSW_S;
2624   
2625   trace_output (OP_IMM_REG_REG_REG);
2626
2627   return 4;
2628 }
2629
2630 /* sdivn imm5, reg1, reg2, reg3 */
2631 int
2632 OP_1C007E0 (void)
2633 {
2634   signed long int  quotient;
2635   signed long int  remainder;
2636   signed long int  divide_by;
2637   signed long int  divide_this;
2638   boolean          overflow = false;
2639   unsigned int     imm5;
2640       
2641   trace_input ("sdivn", OP_IMM_REG_REG_REG, 0);
2642
2643   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2644
2645   divide_by   = State.regs[ OP[0] ];
2646   divide_this = State.regs[ OP[1] ] << imm5;
2647
2648   divn (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 <  0) PSW |= PSW_S;
2659   
2660   trace_output (OP_IMM_REG_REG_REG);
2661
2662   return 4;
2663 }
2664
2665 /* sdivhun imm5, reg1, reg2, reg3 */
2666 int
2667 OP_18207E0 (void)
2668 {
2669   unsigned long int  quotient;
2670   unsigned long int  remainder;
2671   unsigned long int  divide_by;
2672   unsigned long int  divide_this;
2673   boolean            overflow = false;
2674   unsigned int       imm5;
2675       
2676   trace_input ("sdivhun", OP_IMM_REG_REG_REG, 0);
2677
2678   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2679
2680   divide_by   = State.regs[ OP[0] ] & 0xffff;
2681   divide_this = State.regs[ OP[1] ] << imm5;
2682
2683   divun (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 & 0x80000000) PSW |= PSW_S;
2694   
2695   trace_output (OP_IMM_REG_REG_REG);
2696
2697   return 4;
2698 }
2699
2700 /* sdivhn imm5, reg1, reg2, reg3 */
2701 int
2702 OP_18007E0 (void)
2703 {
2704   signed long int  quotient;
2705   signed long int  remainder;
2706   signed long int  divide_by;
2707   signed long int  divide_this;
2708   boolean          overflow = false;
2709   unsigned int     imm5;
2710       
2711   trace_input ("sdivhn", OP_IMM_REG_REG_REG, 0);
2712
2713   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
2714
2715   divide_by   = EXTEND16 (State.regs[ OP[0] ]);
2716   divide_this = State.regs[ OP[1] ] << imm5;
2717
2718   divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
2719   
2720   State.regs[ OP[1]       ] = quotient;
2721   State.regs[ OP[2] >> 11 ] = remainder;
2722   
2723   /* Set condition codes.  */
2724   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2725   
2726   if (overflow)      PSW |= PSW_OV;
2727   if (quotient == 0) PSW |= PSW_Z;
2728   if (quotient <  0) PSW |= PSW_S;
2729   
2730   trace_output (OP_IMM_REG_REG_REG);
2731
2732   return 4;
2733 }
2734
2735 /* end-sanitize-v850eq */
2736 /* start-sanitize-v850e */
2737 /* divu  reg1, reg2, reg3 */
2738 int
2739 OP_2C207E0 (void)
2740 {
2741   unsigned long int quotient;
2742   unsigned long int remainder;
2743   unsigned long int divide_by;
2744   unsigned long int divide_this;
2745   boolean           overflow = false;
2746   
2747   trace_input ("divu", OP_REG_REG_REG, 0);
2748   
2749   /* Compute the result.  */
2750   
2751   divide_by   = State.regs[ OP[0] ];
2752   divide_this = State.regs[ OP[1] ];
2753   
2754   if (divide_by == 0)
2755     {
2756       overflow = true;
2757       divide_by  = 1;
2758     }
2759   
2760   State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
2761   State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2762   
2763   /* Set condition codes.  */
2764   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2765   
2766   if (overflow)      PSW |= PSW_OV;
2767   if (quotient == 0) PSW |= PSW_Z;
2768   if (quotient & 0x80000000) PSW |= PSW_S;
2769   
2770   trace_output (OP_REG_REG_REG);
2771
2772   return 4;
2773 }
2774
2775 /* end-sanitize-v850e */
2776 /* start-sanitize-v850e */
2777 /* div  reg1, reg2, reg3 */
2778 int
2779 OP_2C007E0 (void)
2780 {
2781   signed long int quotient;
2782   signed long int remainder;
2783   signed long int divide_by;
2784   signed long int divide_this;
2785   boolean         overflow = false;
2786   
2787   trace_input ("div", OP_REG_REG_REG, 0);
2788   
2789   /* Compute the result.  */
2790   
2791   divide_by   = State.regs[ OP[0] ];
2792   divide_this = State.regs[ OP[1] ];
2793   
2794   if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
2795     {
2796       overflow  = true;
2797       divide_by = 1;
2798     }
2799   
2800   State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
2801   State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2802   
2803   /* Set condition codes.  */
2804   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2805   
2806   if (overflow)      PSW |= PSW_OV;
2807   if (quotient == 0) PSW |= PSW_Z;
2808   if (quotient <  0) PSW |= PSW_S;
2809   
2810   trace_output (OP_REG_REG_REG);
2811
2812   return 4;
2813 }
2814
2815 /* end-sanitize-v850e */
2816 /* start-sanitize-v850e */
2817 /* divhu  reg1, reg2, reg3 */
2818 int
2819 OP_28207E0 (void)
2820 {
2821   unsigned long int quotient;
2822   unsigned long int remainder;
2823   unsigned long int divide_by;
2824   unsigned long int divide_this;
2825   boolean           overflow = false;
2826   
2827   trace_input ("divhu", OP_REG_REG_REG, 0);
2828   
2829   /* Compute the result.  */
2830   
2831   divide_by   = State.regs[ OP[0] ] & 0xffff;
2832   divide_this = State.regs[ OP[1] ];
2833   
2834   if (divide_by == 0)
2835     {
2836       overflow = true;
2837       divide_by  = 1;
2838     }
2839   
2840   State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
2841   State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2842   
2843   /* Set condition codes.  */
2844   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2845   
2846   if (overflow)      PSW |= PSW_OV;
2847   if (quotient == 0) PSW |= PSW_Z;
2848   if (quotient & 0x80000000) PSW |= PSW_S;
2849   
2850   trace_output (OP_REG_REG_REG);
2851
2852   return 4;
2853 }
2854
2855 /* end-sanitize-v850e */
2856 /* start-sanitize-v850e */
2857 /* divh  reg1, reg2, reg3 */
2858 int
2859 OP_28007E0 (void)
2860 {
2861   signed long int quotient;
2862   signed long int remainder;
2863   signed long int divide_by;
2864   signed long int divide_this;
2865   boolean         overflow = false;
2866   
2867   trace_input ("divh", OP_REG_REG_REG, 0);
2868   
2869   /* Compute the result.  */
2870   
2871   divide_by  = State.regs[ OP[0] ];
2872   divide_this = EXTEND16 (State.regs[ OP[1] ]);
2873   
2874   if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
2875     {
2876       overflow = true;
2877       divide_by  = 1;
2878     }
2879   
2880   State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
2881   State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
2882   
2883   /* Set condition codes.  */
2884   PSW &= ~(PSW_Z | PSW_S | PSW_OV);
2885   
2886   if (overflow)      PSW |= PSW_OV;
2887   if (quotient == 0) PSW |= PSW_Z;
2888   if (quotient <  0) PSW |= PSW_S;
2889   
2890   trace_output (OP_REG_REG_REG);
2891
2892   return 4;
2893 }
2894
2895 /* end-sanitize-v850e */
2896 /* start-sanitize-v850e */
2897 /* mulu imm9, reg2, reg3 */
2898 int
2899 OP_24207E0 (void)
2900 {
2901   trace_input ("mulu", OP_IMM_REG_REG, 0);
2902
2903   Multiply64 (false, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
2904
2905   trace_output (OP_IMM_REG_REG);
2906
2907   return 4;
2908 }
2909
2910 /* end-sanitize-v850e */
2911 /* start-sanitize-v850e */
2912 /* mul imm9, reg2, reg3 */
2913 int
2914 OP_24007E0 (void)
2915 {
2916   trace_input ("mul", OP_IMM_REG_REG, 0);
2917
2918   Multiply64 (true, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
2919
2920   trace_output (OP_IMM_REG_REG);
2921
2922   return 4;
2923 }
2924
2925 /* end-sanitize-v850e */
2926 /* start-sanitize-v850e */
2927 /* cmov imm5, reg2, reg3 */
2928 int
2929 OP_30007E0 (void)
2930 {
2931   trace_input ("cmov", OP_IMM_REG_REG, 0);
2932
2933   State.regs[ OP[2] >> 11 ] = condition_met (OP[0]) ? SEXT5( OP[0] ) : State.regs[ OP[1] ];
2934   
2935   trace_output (OP_IMM_REG_REG);
2936
2937   return 4;
2938   
2939 }
2940
2941 /* end-sanitize-v850e */
2942 /* start-sanitize-v850e */
2943 /* ctret */
2944 int
2945 OP_14407E0 (void)
2946 {
2947   trace_input ("ctret", OP_NONE, 0);
2948
2949   PC  = CTPC;
2950   PSW = CTPSW;
2951
2952   trace_output (OP_NONE);
2953
2954   return 0;
2955 }
2956
2957 /* end-sanitize-v850e */
2958 /* start-sanitize-v850e */
2959 /* hsw */
2960 int
2961 OP_34407E0 (void)
2962 {
2963   unsigned long value;
2964   
2965   trace_input ("hsw", OP_REG_REG3, 0);
2966
2967   value = State.regs[ OP[ 1 ] ];
2968   value >>= 16;
2969   value |= (State.regs[ OP[ 1 ] ] << 16);
2970   
2971   State.regs[ OP[2] >> 11 ] = value;
2972
2973   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
2974
2975   if (value == 0) PSW |= PSW_Z;
2976   if (value & 0x80000000) PSW |= PSW_S;
2977   if (((value & 0xffff) == 0) || (value & 0xffff0000) == 0) PSW |= PSW_CY;
2978
2979   trace_output (OP_REG_REG3);
2980   
2981   return 4;
2982 }
2983
2984 /* end-sanitize-v850e */
2985 /* start-sanitize-v850e */
2986 #define WORDHASNULLBYTE(x) (((x) - 0x01010101) & ~(x)&0x80808080)
2987
2988 /* bsw */
2989 int
2990 OP_34007E0 (void)
2991 {
2992   unsigned long value;
2993   
2994   trace_input ("bsw", OP_REG_REG3, 0);
2995
2996   value = State.regs[ OP[ 1 ] ];
2997   value >>= 24;
2998   value |= (State.regs[ OP[ 1 ] ] << 24);
2999   value |= ((State.regs[ OP[ 1 ] ] << 8) & 0x00ff0000);
3000   value |= ((State.regs[ OP[ 1 ] ] >> 8) & 0x0000ff00);
3001   
3002   State.regs[ OP[2] >> 11 ] = value;
3003
3004   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
3005
3006   if (value == 0) PSW |= PSW_Z;
3007   if (value & 0x80000000) PSW |= PSW_S;
3008   if (WORDHASNULLBYTE (value)) PSW |= PSW_CY;
3009
3010   trace_output (OP_REG_REG3);
3011   
3012   return 4;
3013 }
3014
3015 /* end-sanitize-v850e */
3016 /* start-sanitize-v850e */
3017 /* bsh */
3018 int
3019 OP_34207E0 (void)
3020 {
3021   unsigned long value;
3022   
3023   trace_input ("bsh", OP_REG_REG3, 0);
3024
3025   value   = State.regs[ OP[ 1 ] ];
3026   value >>= 8;
3027   value  |= ((State.regs[ OP[ 1 ] ] << 8) & 0xff00ff00);
3028   value  |= ((State.regs[ OP[ 1 ] ] >> 8) & 0x000000ff);
3029   
3030   State.regs[ OP[2] >> 11 ] = value;
3031
3032   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
3033
3034   if (value == 0) PSW |= PSW_Z;
3035   if (value & 0x80000000) PSW |= PSW_S;
3036   if (((value & 0xff) == 0) || (value & 0x00ff) == 0) PSW |= PSW_CY;
3037
3038   trace_output (OP_REG_REG3);
3039   
3040   return 4;
3041 }
3042
3043 /* end-sanitize-v850e */
3044 /* start-sanitize-v850e */
3045 /* ld.hu */
3046 int
3047 OP_107E0 (void)
3048 {
3049   int adr;
3050
3051   trace_input ("ld.hu", OP_LOAD32, 2);
3052
3053   adr = State.regs[ OP[0] ] + EXTEND16 (OP[2] & ~1);
3054   adr &= ~0x1;
3055       
3056   State.regs[ OP[1] ] = load_mem (adr, 2);
3057       
3058   trace_output (OP_LOAD32);
3059   
3060   return 4;
3061 }
3062
3063 /* end-sanitize-v850e */
3064 /* start-sanitize-v850e */
3065 /* ld.bu */
3066 int
3067 OP_10780 (void)
3068 {
3069   int adr;
3070
3071   trace_input ("ld.bu", OP_LOAD32, 1);
3072
3073   adr = (State.regs[ OP[0] ]
3074          + (EXTEND16 (OP[2] & ~1) | ((OP[3] >> 5) & 1)));
3075       
3076   State.regs[ OP[1] ] = load_mem (adr, 1);
3077   
3078   trace_output (OP_LOAD32);
3079   
3080   return 4;
3081 }
3082
3083 /* prepare list12, imm5, imm32 */
3084 int
3085 OP_1B0780 (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 = load_mem (PC + 4, 4);
3102   
3103   trace_output (OP_PUSHPOP1);
3104
3105   return 8;
3106 }
3107
3108 /* prepare list12, imm5, imm16-32 */
3109 int
3110 OP_130780 (void)
3111 {
3112   int  i;
3113   
3114   trace_input ("prepare", OP_PUSHPOP1, 0);
3115   
3116   /* Store the registers with lower number registers being placed at higher addresses.  */
3117   for (i = 0; i < 12; i++)
3118     if ((OP[3] & (1 << type1_regs[ i ])))
3119       {
3120         SP -= 4;
3121         store_mem (SP, 4, State.regs[ 20 + i ]);
3122       }
3123   
3124   SP -= (OP[3] & 0x3e) << 1;
3125
3126   EP = load_mem (PC + 4, 2) << 16;
3127   
3128   trace_output (OP_PUSHPOP1);
3129
3130   return 6;
3131 }
3132
3133 /* prepare list12, imm5, imm16 */
3134 int
3135 OP_B0780 (void)
3136 {
3137   int  i;
3138   
3139   trace_input ("prepare", OP_PUSHPOP1, 0);
3140   
3141   /* Store the registers with lower number registers being placed at higher addresses.  */
3142   for (i = 0; i < 12; i++)
3143     if ((OP[3] & (1 << type1_regs[ i ])))
3144       {
3145         SP -= 4;
3146         store_mem (SP, 4, State.regs[ 20 + i ]);
3147       }
3148   
3149   SP -= (OP[3] & 0x3e) << 1;
3150
3151   EP = EXTEND16 (load_mem (PC + 4, 2));
3152   
3153   trace_output (OP_PUSHPOP1);
3154
3155   return 6;
3156 }
3157
3158 /* prepare list12, imm5, sp */
3159 int
3160 OP_30780 (void)
3161 {
3162   int  i;
3163   
3164   trace_input ("prepare", OP_PUSHPOP1, 0);
3165   
3166   /* Store the registers with lower number registers being placed at higher addresses.  */
3167   for (i = 0; i < 12; i++)
3168     if ((OP[3] & (1 << type1_regs[ i ])))
3169       {
3170         SP -= 4;
3171         store_mem (SP, 4, State.regs[ 20 + i ]);
3172       }
3173   
3174   SP -= (OP[3] & 0x3e) << 1;
3175
3176   EP = SP;
3177   
3178   trace_output (OP_PUSHPOP1);
3179
3180   return 4;
3181 }
3182
3183 /* end-sanitize-v850e */
3184 /* start-sanitize-v850e */
3185 /* sld.hu */
3186 int
3187 OP_70 (void)
3188 {
3189   unsigned long result;
3190   
3191   result  = load_mem (State.regs[30] + ((OP[3] & 0xf) << 1), 2);
3192
3193   /* start-sanitize-v850eq */
3194   if (PSW & PSW_US)
3195     {
3196       trace_input ("sld.h", OP_LOAD16, 2);
3197       State.regs[ OP[1] ] = EXTEND16 (result);
3198     }
3199   else
3200     {
3201 /* end-sanitize-v850eq */
3202   trace_input ("sld.hu", OP_LOAD16, 2);
3203   
3204   State.regs[ OP[1] ] = result;
3205 /* start-sanitize-v850eq */
3206     }
3207 /* end-sanitize-v850eq */
3208   
3209   trace_output (OP_LOAD16);
3210   
3211   return 2;
3212 }
3213
3214 /* end-sanitize-v850e */
3215 /* start-sanitize-v850e */
3216 /* cmov reg1, reg2, reg3 */
3217 int
3218 OP_32007E0 (void)
3219 {
3220   trace_input ("cmov", OP_REG_REG_REG, 0);
3221
3222   State.regs[ OP[2] >> 11 ] = condition_met (OP[0]) ? State.regs[ OP[0] ] : State.regs[ OP[1] ];
3223   
3224   trace_output (OP_REG_REG_REG);
3225
3226   return 4;
3227 }
3228
3229 /* end-sanitize-v850e */
3230 /* start-sanitize-v850e */
3231 /* mul reg1, reg2, reg3 */
3232 int
3233 OP_22007E0 (void)
3234 {
3235   trace_input ("mul", OP_REG_REG_REG, 0);
3236
3237   Multiply64 (true, State.regs[ OP[0] ]);
3238
3239   trace_output (OP_REG_REG_REG);
3240
3241   return 4;
3242 }
3243
3244 /* end-sanitize-v850e */
3245 /* start-sanitize-v850eq */
3246
3247 /* popmh list18 */
3248 int
3249 OP_307F0 (void)
3250 {
3251   int i;
3252   
3253   trace_input ("popmh", OP_PUSHPOP2, 0);
3254   
3255   if (OP[3] & (1 << 19))
3256     {
3257       if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
3258         {
3259           FEPSW = load_mem ( SP      & ~ 3, 4);
3260           FEPC  = load_mem ((SP + 4) & ~ 3, 4);
3261         }
3262       else
3263         {
3264           EIPSW = load_mem ( SP      & ~ 3, 4);
3265           EIPC  = load_mem ((SP + 4) & ~ 3, 4);
3266         }
3267       
3268       SP += 8;
3269     }
3270   
3271   /* Load the registers with lower number registers being retrieved from higher addresses.  */
3272   for (i = 16; i--;)
3273     if ((OP[3] & (1 << type2_regs[ i ])))
3274       {
3275         State.regs[ i + 16 ] = load_mem (SP & ~ 3, 4);
3276         SP += 4;
3277       }
3278   
3279   trace_output (OP_PUSHPOP2);
3280
3281   return 4;
3282 }
3283
3284 /* popml lsit18 */
3285 int
3286 OP_107F0 (void)
3287 {
3288   int i;
3289
3290   trace_input ("popml", OP_PUSHPOP3, 0);
3291
3292   if (OP[3] & (1 << 19))
3293     {
3294       if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
3295         {
3296           FEPSW = load_mem ( SP      & ~ 3, 4);
3297           FEPC =  load_mem ((SP + 4) & ~ 3, 4);
3298         }
3299       else
3300         {
3301           EIPSW = load_mem ( SP      & ~ 3, 4);
3302           EIPC  = load_mem ((SP + 4) & ~ 3, 4);
3303         }
3304       
3305       SP += 8;
3306     }
3307   
3308   if (OP[3] & (1 << 3))
3309     {
3310       PSW = load_mem (SP & ~ 3, 4);
3311       SP += 4;
3312     }
3313   
3314   /* Load the registers with lower number registers being retrieved from higher addresses.  */
3315   for (i = 15; i--;)
3316     if ((OP[3] & (1 << type3_regs[ i ])))
3317       {
3318         State.regs[ i + 1 ] = load_mem (SP & ~ 3, 4);
3319         SP += 4;
3320       }
3321   
3322   trace_output (OP_PUSHPOP2);
3323
3324   return 4;
3325 }
3326
3327 /* pushmh list18 */
3328 int
3329 OP_307E0 (void)
3330 {
3331   int i;
3332
3333   trace_input ("pushmh", OP_PUSHPOP2, 0);
3334   
3335   /* Store the registers with lower number registers being placed at higher addresses.  */
3336   for (i = 0; i < 16; i++)
3337     if ((OP[3] & (1 << type2_regs[ i ])))
3338       {
3339         SP -= 4;
3340         store_mem (SP & ~ 3, 4, State.regs[ i + 16 ]);
3341       }
3342   
3343   if (OP[3] & (1 << 19))
3344     {
3345       SP -= 8;
3346       
3347       if ((PSW & PSW_NP) && ((PSW & PSW_EP) == 0))
3348         {
3349           store_mem ((SP + 4) & ~ 3, 4, FEPC);
3350           store_mem ( SP      & ~ 3, 4, FEPSW);
3351         }
3352       else
3353         {
3354           store_mem ((SP + 4) & ~ 3, 4, EIPC);
3355           store_mem ( SP      & ~ 3, 4, EIPSW);
3356         }
3357     }
3358   
3359   trace_output (OP_PUSHPOP2);
3360
3361   return 4;
3362 }
3363
3364 /* end-sanitize-v850eq */