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