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