daily update
[external/binutils.git] / sim / v850 / simops.c
index 543ab91..a14b0b2 100644 (file)
@@ -1,8 +1,9 @@
-#include <signal.h>
 #include "sim-main.h"
 #include "v850_sim.h"
 #include "simops.h"
 
+#include <sys/types.h>
+
 #ifdef HAVE_UTIME_H
 #include <utime.h>
 #endif
 #endif
 #endif
 
-
-
-
-
- /* FIXME - should be including a version of syscall.h that does not
-    pollute the name space */
-#include "../../libgloss/v850/sys/syscall.h"
+#include "targ-vals.h"
 
 #include "libiberty.h"
 
 #include <sys/time.h>
 #endif
 
-/* start-sanitize-v850e */
 /* This is an array of the bit positions of registers r20 .. r31 in
    that order in a prepare/dispose instruction.  */
 int type1_regs[12] = { 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 0, 21 };
-/* end-sanitize-v850e */
-/* start-sanitize-v850eq */
 /* This is an array of the bit positions of registers r16 .. r31 in
    that order in a push/pop instruction.  */
 int type2_regs[16] = { 3, 2, 1, 0, 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 20, 21};
 /* This is an array of the bit positions of registers r1 .. r15 in
    that order in a push/pop instruction.  */
 int type3_regs[15] = { 2, 1, 0, 27, 26, 25, 24, 31, 30, 29, 28, 23, 22, 20, 21};
-/* end-sanitize-v850eq */
 
 #ifdef DEBUG
 #ifndef SIZE_INSTRUCTION
@@ -68,7 +59,7 @@ unsigned32 trace_values[3];
 int trace_num_values;
 unsigned32 trace_pc;
 const char *trace_name;
-const char *trace_module;
+int trace_module;
 
 
 void
@@ -83,7 +74,7 @@ trace_input (name, type, size)
 
   trace_pc = PC;
   trace_name = name;
-  trace_module = "alu";
+  trace_module = TRACE_ALU_IDX;
 
   switch (type)
     {
@@ -100,9 +91,7 @@ trace_input (name, type, size)
       trace_num_values = 1;
       break;
       
-      /* start-sanitize-v850e */
     case OP_BIT_CHANGE:
-      /* end-sanitize-v850e */
     case OP_REG_REG:
     case OP_REG_REG_CMP:
       trace_values[0] = State.regs[OP[1]];
@@ -224,7 +213,8 @@ trace_result (int has_result, unsigned32 result)
     int i;
     for (i = 0; i < trace_num_values; i++)
       {
-       sprintf (chp, "%*s0x%.8lx", SIZE_VALUES - 10, "", trace_values[i]);
+       sprintf (chp, "%*s0x%.8lx", SIZE_VALUES - 10, "",
+                (long) trace_values[i]);
        chp = strchr (chp, '\0');
       }
     while (i++ < 3)
@@ -238,10 +228,7 @@ trace_result (int has_result, unsigned32 result)
   if (has_result)
     sprintf (chp, " :: 0x%.8lx", (unsigned long)result);
   
-  trace_one_insn (simulator, STATE_CPU (simulator, 0), trace_pc,
-                 TRACE_LINENUM_P (STATE_CPU (simulator, 0)),
-                 "simops", __LINE__, trace_module, 
-                 "%-*s -%s", SIZE_INSTRUCTION, trace_name, buf);
+  trace_generic (simulator, STATE_CPU (simulator, 0), trace_module, buf);
 }
 
 void
@@ -332,7 +319,6 @@ condition_met (unsigned code)
   
   return 1;
 }
-/* start-sanitize-v850e */
 
 static unsigned long
 Add32 (unsigned long a1, unsigned long a2, int * carry)
@@ -345,7 +331,7 @@ Add32 (unsigned long a1, unsigned long a2, int * carry)
 }
 
 static void
-Multiply64 (boolean sign, unsigned long op0)
+Multiply64 (int sign, unsigned long op0)
 {
   unsigned long op1;
   unsigned long lo;
@@ -409,7 +395,6 @@ Multiply64 (boolean sign, unsigned long op0)
   return;
 }
 
-/* end-sanitize-v850e */
 \f
 /* Read a null terminated string from memory, return in a buffer */
 static char *
@@ -420,7 +405,7 @@ fetch_str (sd, addr)
   char *buf;
   int nr = 0;
   while (sim_core_read_1 (STATE_CPU (sd, 0),
-                         PC, sim_core_read_map, addr + nr) != 0)
+                         PC, read_map, addr + nr) != 0)
     nr++;
   buf = NZALLOC (char, nr + 1);
   sim_read (simulator, addr, buf, nr);
@@ -440,7 +425,7 @@ fetch_argv (sd, addr)
   while (1)
     {
       unsigned32 a = sim_core_read_4 (STATE_CPU (sd, 0),
-                                     PC, sim_core_read_map, addr + nr * 4);
+                                     PC, read_map, addr + nr * 4);
       if (a == 0) break;
       buf[nr] = fetch_str (sd, a);
       nr ++;
@@ -788,50 +773,6 @@ OP_6E0 ()
   return 4;
 }
 
-/* divh reg1, reg2 */
-int
-OP_40 ()
-{
-  unsigned int op0, op1, result, ov, s, z;
-  int temp;
-
-  trace_input ("divh", OP_REG_REG, 0);
-
-  /* Compute the result.  */
-  temp = EXTEND16 (State.regs[ OP[0] ]);
-  op0 = temp;
-  op1 = State.regs[OP[1]];
-  
-  if (op0 == 0xffffffff && op1 == 0x80000000)
-    {
-      result = 0x80000000;
-      ov = 1;
-    }
-  else if (op0 != 0)
-    {
-      result = op1 / op0;
-      ov = 0;
-    }
-  else
-    {
-      result = 0x0;
-      ov = 1;
-    }
-  
-  /* Compute the condition codes.  */
-  z = (result == 0);
-  s = (result & 0x80000000);
-  
-  /* Store the result and condition codes.  */
-  State.regs[OP[1]] = result;
-  PSW &= ~(PSW_Z | PSW_S | PSW_OV);
-  PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
-         | (ov ? PSW_OV : 0));
-  trace_output (OP_REG_REG);
-
-  return 2;
-}
-
 /* cmp reg, reg */
 int
 OP_1E0 ()
@@ -923,18 +864,29 @@ OP_C0 ()
        && (op0 & 0x80000000) != (result & 0x80000000));
   sat = ov;
   
+  /* Handle saturated results.  */
+  if (sat && s)
+    {
+      /* An overflow that results in a negative result implies that we
+        became too positive.  */
+      result = 0x7fffffff;
+      s = 0;
+    }
+  else if (sat)
+    {
+      /* Any other overflow must have thus been too negative.  */
+      result = 0x80000000;
+      s = 1;
+      z = 0;
+    }
+
   /* Store the result and condition codes.  */
   State.regs[OP[1]] = result;
   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
          | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
          | (sat ? PSW_SAT : 0));
-  
-  /* Handle saturated results.  */
-  if (sat && s)
-    State.regs[OP[1]] = 0x80000000;
-  else if (sat)
-    State.regs[OP[1]] = 0x7fffffff;
+
   trace_output (OP_REG_REG);
 
   return 2;
@@ -964,18 +916,28 @@ OP_220 ()
        && (op0 & 0x80000000) != (result & 0x80000000));
   sat = ov;
 
+  /* Handle saturated results.  */
+  if (sat && s)
+    {
+      /* An overflow that results in a negative result implies that we
+        became too positive.  */
+      result = 0x7fffffff;
+      s = 0;
+    }
+  else if (sat)
+    {
+      /* Any other overflow must have thus been too negative.  */
+      result = 0x80000000;
+      s = 1;
+      z = 0;
+    }
+
   /* Store the result and condition codes.  */
   State.regs[OP[1]] = result;
   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
   PSW |= ((z ? PSW_Z : 0) | (s ? PSW_S : 0)
                | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
                | (sat ? PSW_SAT : 0));
-
-  /* Handle saturated results.  */
-  if (sat && s)
-    State.regs[OP[1]] = 0x80000000;
-  else if (sat)
-    State.regs[OP[1]] = 0x7fffffff;
   trace_output (OP_IMM_REG);
 
   return 2;
@@ -1001,7 +963,23 @@ OP_A0 ()
   ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
        && (op1 & 0x80000000) != (result & 0x80000000));
   sat = ov;
-  
+
+  /* Handle saturated results.  */
+  if (sat && s)
+    {
+      /* An overflow that results in a negative result implies that we
+        became too positive.  */
+      result = 0x7fffffff;
+      s = 0;
+    }
+  else if (sat)
+    {
+      /* Any other overflow must have thus been too negative.  */
+      result = 0x80000000;
+      s = 1;
+      z = 0;
+    }
+
   /* Store the result and condition codes.  */
   State.regs[OP[1]] = result;
   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
@@ -1009,11 +987,6 @@ OP_A0 ()
          | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
          | (sat ? PSW_SAT : 0));
   
-  /* Handle saturated results.  */
-  if (sat && s)
-    State.regs[OP[1]] = 0x80000000;
-  else if (sat)
-    State.regs[OP[1]] = 0x7fffffff;
   trace_output (OP_REG_REG);
   return 2;
 }
@@ -1041,6 +1014,22 @@ OP_660 ()
        && (op1 & 0x80000000) != (result & 0x80000000));
   sat = ov;
 
+  /* Handle saturated results.  */
+  if (sat && s)
+    {
+      /* An overflow that results in a negative result implies that we
+        became too positive.  */
+      result = 0x7fffffff;
+      s = 0;
+    }
+  else if (sat)
+    {
+      /* Any other overflow must have thus been too negative.  */
+      result = 0x80000000;
+      s = 1;
+      z = 0;
+    }
+
   /* Store the result and condition codes.  */
   State.regs[OP[1]] = result;
   PSW &= ~(PSW_Z | PSW_S | PSW_CY | PSW_OV);
@@ -1048,11 +1037,6 @@ OP_660 ()
                | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
                | (sat ? PSW_SAT : 0));
 
-  /* Handle saturated results.  */
-  if (sat && s)
-    State.regs[OP[1]] = 0x80000000;
-  else if (sat)
-    State.regs[OP[1]] = 0x7fffffff;
   trace_output (OP_IMM_REG);
 
   return 4;
@@ -1074,10 +1058,26 @@ OP_80 ()
   /* Compute the condition codes.  */
   z = (result == 0);
   s = (result & 0x80000000);
-  cy = (result < op0);
-  ov = ((op1 & 0x80000000) != (op0 & 0x80000000)
-       && (op1 & 0x80000000) != (result & 0x80000000));
+  cy = (op0 < op1);
+  ov = ((op0 & 0x80000000) != (op1 & 0x80000000)
+       && (op0 & 0x80000000) != (result & 0x80000000));
   sat = ov;
+
+  /* Handle saturated results.  */
+  if (sat && s)
+    {
+      /* An overflow that results in a negative result implies that we
+        became too positive.  */
+      result = 0x7fffffff;
+      s = 0;
+    }
+  else if (sat)
+    {
+      /* Any other overflow must have thus been too negative.  */
+      result = 0x80000000;
+      s = 1;
+      z = 0;
+    }
   
   /* Store the result and condition codes.  */
   State.regs[OP[1]] = result;
@@ -1086,11 +1086,6 @@ OP_80 ()
          | (cy ? PSW_CY : 0) | (ov ? PSW_OV : 0)
          | (sat ? PSW_SAT : 0));
   
-  /* Handle saturated results.  */
-  if (sat && s)
-    State.regs[OP[1]] = 0x80000000;
-  else if (sat)
-    State.regs[OP[1]] = 0x7fffffff;
   trace_output (OP_REG_REG);
 
   return 2;
@@ -1163,7 +1158,7 @@ OP_2A0 ()
   /* Compute the condition codes.  */
   z = (result == 0);
   s = (result & 0x80000000);
-  cy = (op1 & (1 << (op0 - 1)));
+  cy = op0 ? (op1 & (1 << (op0 - 1))) : 0;
 
   /* Store the result and condition codes.  */
   State.regs[ OP[1] ] = result;
@@ -1190,7 +1185,7 @@ OP_A007E0 ()
   /* Compute the condition codes.  */
   z = (result == 0);
   s = (result & 0x80000000);
-  cy = (op1 & (1 << (op0 - 1)));
+  cy = op0 ? (op1 & (1 << (op0 - 1))) : 0;
 
   /* Store the result and condition codes.  */
   State.regs[OP[1]] = result;
@@ -1216,7 +1211,7 @@ OP_2C0 ()
   /* Compute the condition codes.  */
   z = (result == 0);
   s = (result & 0x80000000);
-  cy = (op1 & (1 << (32 - op0)));
+  cy = op0 ? (op1 & (1 << (32 - op0))) : 0;
 
   /* Store the result and condition codes.  */
   State.regs[OP[1]] = result;
@@ -1242,7 +1237,7 @@ OP_C007E0 ()
   /* Compute the condition codes.  */
   z = (result == 0);
   s = (result & 0x80000000);
-  cy = (op1 & (1 << (32 - op0)));
+  cy = op0 ? (op1 & (1 << (32 - op0))) : 0;
 
   /* Store the result and condition codes.  */
   State.regs[OP[1]] = result;
@@ -1268,7 +1263,7 @@ OP_280 ()
   /* Compute the condition codes.  */
   z = (result == 0);
   s = (result & 0x80000000);
-  cy = (op1 & (1 << (op0 - 1)));
+  cy = op0 ? (op1 & (1 << (op0 - 1))) : 0;
 
   /* Store the result and condition codes.  */
   State.regs[OP[1]] = result;
@@ -1294,7 +1289,7 @@ OP_8007E0 ()
   /* Compute the condition codes.  */
   z = (result == 0);
   s = (result & 0x80000000);
-  cy = (op1 & (1 << (op0 - 1)));
+  cy = op0 ? (op1 & (1 << (op0 - 1))) : 0;
 
   /* Store the result and condition codes.  */
   State.regs[OP[1]] = result;
@@ -1600,7 +1595,7 @@ OP_12007E0 ()
   /* FIXME this should put processor into a mode where NMI still handled */
   trace_output (OP_NONE);
   sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
-                  sim_stopped, SIGTRAP);
+                  sim_stopped, SIM_SIGTRAP);
   return 0;
 }
 
@@ -1634,50 +1629,55 @@ OP_10007E0 ()
 
 #define MEMPTR(x) (map (x))
 
+      RETERR = 0;
+
       switch (FUNC)
        {
 
 #ifdef HAVE_FORK
-#ifdef SYS_fork
-       case SYS_fork:
+#ifdef TARGET_SYS_fork
+       case TARGET_SYS_fork:
          RETVAL = fork ();
+         RETERR = errno;
          break;
 #endif
 #endif
 
 #ifdef HAVE_EXECVE
-#ifdef SYS_execv
-       case SYS_execve:
+#ifdef TARGET_SYS_execv
+       case TARGET_SYS_execve:
          {
            char *path = fetch_str (simulator, PARM1);
            char **argv = fetch_argv (simulator, PARM2);
            char **envp = fetch_argv (simulator, PARM3);
            RETVAL = execve (path, argv, envp);
-           zfree (path);
+           free (path);
            freeargv (argv);
            freeargv (envp);
+           RETERR = errno;
            break;
          }
 #endif
 #endif
 
 #if HAVE_EXECV
-#ifdef SYS_execv
-       case SYS_execv:
+#ifdef TARGET_SYS_execv
+       case TARGET_SYS_execv:
          {
            char *path = fetch_str (simulator, PARM1);
            char **argv = fetch_argv (simulator, PARM2);
            RETVAL = execv (path, argv);
-           zfree (path);
+           free (path);
            freeargv (argv);
+           RETERR = errno;
            break;
          }
 #endif
 #endif
 
 #if 0
-#ifdef SYS_pipe
-       case SYS_pipe:
+#ifdef TARGET_SYS_pipe
+       case TARGET_SYS_pipe:
          {
            reg_t buf;
            int host_fd[2];
@@ -1687,37 +1687,41 @@ OP_10007E0 ()
            SW (buf, host_fd[0]);
            buf += sizeof(uint16);
            SW (buf, host_fd[1]);
+           RETERR = errno;
          }
          break;
 #endif
 #endif
 
 #if 0
-#ifdef SYS_wait
-       case SYS_wait:
+#ifdef TARGET_SYS_wait
+       case TARGET_SYS_wait:
          {
            int status;
 
            RETVAL = wait (&status);
            SW (PARM1, status);
+           RETERR = errno;
          }
          break;
 #endif
 #endif
 
-#ifdef SYS_read
-       case SYS_read:
+#ifdef TARGET_SYS_read
+       case TARGET_SYS_read:
          {
            char *buf = zalloc (PARM3);
            RETVAL = sim_io_read (simulator, PARM1, buf, PARM3);
            sim_write (simulator, PARM2, buf, PARM3);
-           zfree (buf);
+           free (buf);
+           if ((int) RETVAL < 0)
+             RETERR = sim_io_get_errno (simulator);
            break;
          }
 #endif
 
-#ifdef SYS_write
-       case SYS_write:
+#ifdef TARGET_SYS_write
+       case TARGET_SYS_write:
          {
            char *buf = zalloc (PARM3);
            sim_read (simulator, PARM2, buf, PARM3);
@@ -1725,35 +1729,43 @@ OP_10007E0 ()
              RETVAL = sim_io_write_stdout (simulator, buf, PARM3);
            else
              RETVAL = sim_io_write (simulator, PARM1, buf, PARM3);
-           zfree (buf);
+           free (buf);
+           if ((int) RETVAL < 0)
+             RETERR = sim_io_get_errno (simulator);
            break;
          }
 #endif
 
-#ifdef SYS_lseek
-       case SYS_lseek:
+#ifdef TARGET_SYS_lseek
+       case TARGET_SYS_lseek:
          RETVAL = sim_io_lseek (simulator, PARM1, PARM2, PARM3);
+         if ((int) RETVAL < 0)
+           RETERR = sim_io_get_errno (simulator);
          break;
 #endif
 
-#ifdef SYS_close
-       case SYS_close:
+#ifdef TARGET_SYS_close
+       case TARGET_SYS_close:
          RETVAL = sim_io_close (simulator, PARM1);
+         if ((int) RETVAL < 0)
+           RETERR = sim_io_get_errno (simulator);
          break;
 #endif
 
-#ifdef SYS_open
-       case SYS_open:
+#ifdef TARGET_SYS_open
+       case TARGET_SYS_open:
          {
            char *buf = fetch_str (simulator, PARM1);
            RETVAL = sim_io_open (simulator, buf, PARM2);
-           zfree (buf);
+           free (buf);
+           if ((int) RETVAL < 0)
+             RETERR = sim_io_get_errno (simulator);
            break;
          }
 #endif
 
-#ifdef SYS_exit
-       case SYS_exit:
+#ifdef TARGET_SYS_exit
+       case TARGET_SYS_exit:
          if ((PARM1 & 0xffff0000) == 0xdead0000 && (PARM1 & 0xffff) != 0)
            /* get signal encoded by kill */
            sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
@@ -1761,7 +1773,7 @@ OP_10007E0 ()
          else if (PARM1 == 0xdead)
            /* old libraries */
            sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
-                            sim_exited, SIGABRT);
+                            sim_stopped, SIM_SIGABRT);
          else
            /* PARM1 has exit status */
            sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
@@ -1769,18 +1781,17 @@ OP_10007E0 ()
          break;
 #endif
 
-#if !defined(__GO32__) && !defined(_WIN32)
-#ifdef SYS_stat
-       case SYS_stat:  /* added at hmsi */
+#ifdef TARGET_SYS_stat
+       case TARGET_SYS_stat:   /* added at hmsi */
          /* stat system call */
          {
            struct stat host_stat;
            reg_t buf;
            char *path = fetch_str (simulator, PARM1);
 
-           RETVAL = stat (path, &host_stat);
+           RETVAL = sim_io_stat (simulator, path, &host_stat);
 
-           zfree (path);
+           free (path);
            buf = PARM2;
 
            /* Just wild-assed guesses.  */
@@ -1795,50 +1806,111 @@ OP_10007E0 ()
            store_mem (buf + 20, 4, host_stat.st_atime);
            store_mem (buf + 28, 4, host_stat.st_mtime);
            store_mem (buf + 36, 4, host_stat.st_ctime);
+
+           if ((int) RETVAL < 0)
+             RETERR = sim_io_get_errno (simulator);
          }
          break;
 #endif
+
+#ifdef TARGET_SYS_fstat
+       case TARGET_SYS_fstat:
+         /* fstat system call */
+         {
+           struct stat host_stat;
+           reg_t buf;
+
+           RETVAL = sim_io_fstat (simulator, PARM1, &host_stat);
+
+           buf = PARM2;
+
+           /* Just wild-assed guesses.  */
+           store_mem (buf, 2, host_stat.st_dev);
+           store_mem (buf + 2, 2, host_stat.st_ino);
+           store_mem (buf + 4, 4, host_stat.st_mode);
+           store_mem (buf + 8, 2, host_stat.st_nlink);
+           store_mem (buf + 10, 2, host_stat.st_uid);
+           store_mem (buf + 12, 2, host_stat.st_gid);
+           store_mem (buf + 14, 2, host_stat.st_rdev);
+           store_mem (buf + 16, 4, host_stat.st_size);
+           store_mem (buf + 20, 4, host_stat.st_atime);
+           store_mem (buf + 28, 4, host_stat.st_mtime);
+           store_mem (buf + 36, 4, host_stat.st_ctime);
+
+           if ((int) RETVAL < 0)
+             RETERR = sim_io_get_errno (simulator);
+         }
+         break;
+#endif
+
+#ifdef TARGET_SYS_rename
+       case TARGET_SYS_rename:
+         {
+           char *oldpath = fetch_str (simulator, PARM1);
+           char *newpath = fetch_str (simulator, PARM2);
+           RETVAL = sim_io_rename (simulator, oldpath, newpath);
+           free (oldpath);
+           free (newpath);
+           if ((int) RETVAL < 0)
+             RETERR = sim_io_get_errno (simulator);
+         }
+         break;
+#endif
+
+#ifdef TARGET_SYS_unlink
+       case TARGET_SYS_unlink:
+         {
+           char *path = fetch_str (simulator, PARM1);
+           RETVAL = sim_io_unlink (simulator, path);
+           free (path);
+           if ((int) RETVAL < 0)
+             RETERR = sim_io_get_errno (simulator);
+         }
+         break;
 #endif
 
 #ifdef HAVE_CHOWN
-#ifdef SYS_chown
-       case SYS_chown:
+#ifdef TARGET_SYS_chown
+       case TARGET_SYS_chown:
          {
            char *path = fetch_str (simulator, PARM1);
            RETVAL = chown (path, PARM2, PARM3);
-           zfree (path);
+           free (path);
+           RETERR = errno;
          }
          break;
 #endif
 #endif
 
 #if HAVE_CHMOD
-#ifdef SYS_chmod
-       case SYS_chmod:
+#ifdef TARGET_SYS_chmod
+       case TARGET_SYS_chmod:
          {
            char *path = fetch_str (simulator, PARM1);
            RETVAL = chmod (path, PARM2);
-           zfree (path);
+           free (path);
+           RETERR = errno;
          }
          break;
 #endif
 #endif
 
-#ifdef SYS_time
+#ifdef TARGET_SYS_time
 #if HAVE_TIME
-       case SYS_time:
+       case TARGET_SYS_time:
          {
            time_t now;
            RETVAL = time (&now);
            store_mem (PARM1, 4, now);
+           RETERR = errno;
          }
          break;
 #endif
 #endif
 
 #if !defined(__GO32__) && !defined(_WIN32)
-#ifdef SYS_times
-       case SYS_times:
+#ifdef TARGET_SYS_times
+       case TARGET_SYS_times:
          {
            struct tms tms;
            RETVAL = times (&tms);
@@ -1846,14 +1918,15 @@ OP_10007E0 ()
            store_mem (PARM1 + 4, 4, tms.tms_stime);
            store_mem (PARM1 + 8, 4, tms.tms_cutime);
            store_mem (PARM1 + 12, 4, tms.tms_cstime);
+           reterr = errno;
            break;
          }
 #endif
 #endif
 
-#ifdef SYS_gettimeofday
+#ifdef TARGET_SYS_gettimeofday
 #if !defined(__GO32__) && !defined(_WIN32)
-       case SYS_gettimeofday:
+       case TARGET_SYS_gettimeofday:
          {
            struct timeval t;
            struct timezone tz;
@@ -1862,14 +1935,15 @@ OP_10007E0 ()
            store_mem (PARM1 + 4, 4, t.tv_usec);
            store_mem (PARM2, 4, tz.tz_minuteswest);
            store_mem (PARM2 + 4, 4, tz.tz_dsttime);
+           RETERR = errno;
            break;
          }
 #endif
 #endif
 
-#ifdef SYS_utime
+#ifdef TARGET_SYS_utime
 #if HAVE_UTIME
-       case SYS_utime:
+       case TARGET_SYS_utime:
          {
            /* Cast the second argument to void *, to avoid type mismatch
               if a prototype is present.  */
@@ -1883,7 +1957,6 @@ OP_10007E0 ()
        default:
          abort ();
        }
-      RETERR = errno;
       errno = save_errno;
 
       return 4;
@@ -1897,13 +1970,12 @@ OP_10007E0 ()
       ECR |= 0x40 + OP[0];
       /* Flag that we are now doing exception processing.  */
       PSW |= PSW_EP | PSW_ID;
-      PC = ((OP[0] < 0x10) ? 0x40 : 0x50) - 4;
+      PC = (OP[0] < 0x10) ? 0x40 : 0x50;
 
       return 0;
     }
 }
 
-/* start-sanitize-v850e */
 /* tst1 reg2, [reg1] */
 int
 OP_E607E0 (void)
@@ -1915,7 +1987,7 @@ OP_E607E0 (void)
   temp = load_mem (State.regs[ OP[0] ], 1);
   
   PSW &= ~PSW_Z;
-  if ((temp & (1 << State.regs[ OP[1] & 0x7 ])) == 0)
+  if ((temp & (1 << (State.regs[ OP[1] ] & 0x7))) == 0)
     PSW |= PSW_Z;
   
   trace_output (OP_BIT);
@@ -1923,31 +1995,26 @@ OP_E607E0 (void)
   return 4;
 }
 
-/* end-sanitize-v850e */
-/* start-sanitize-v850e */
 /* mulu reg1, reg2, reg3 */
 int
 OP_22207E0 (void)
 {
   trace_input ("mulu", OP_REG_REG_REG, 0);
 
-  Multiply64 (false, State.regs[ OP[0] ]);
+  Multiply64 (0, State.regs[ OP[0] ]);
 
   trace_output (OP_REG_REG_REG);
 
   return 4;
 }
 
-/* end-sanitize-v850e */
-/* start-sanitize-v850e */
-
 #define BIT_CHANGE_OP( name, binop )           \
   unsigned int bit;                            \
   unsigned int temp;                           \
                                                \
   trace_input (name, OP_BIT_CHANGE, 0);                \
                                                \
-  bit  = 1 << State.regs[ OP[1] & 0x7 ];       \
+  bit  = 1 << (State.regs[ OP[1] ] & 0x7);     \
   temp = load_mem (State.regs[ OP[0] ], 1);    \
                                                \
   PSW &= ~PSW_Z;                               \
@@ -1994,18 +2061,18 @@ OP_20007E0 (void)
 
   return 4;
 }
-/* end-sanitize-v850e */
-/* start-sanitize-v850eq */
-/* This function is courtesy of Sugimoto at NEC, via Seow Tan (Soew_Tan@el.nec.com) */
+
+/* This function is courtesy of Sugimoto at NEC, via Seow Tan
+   (Soew_Tan@el.nec.com) */
 void
 divun
 (
   unsigned int       N,
   unsigned long int  als,
   unsigned long int  sfi,
-  unsigned long int *  quotient_ptr,
-  unsigned long int *  remainder_ptr,
-  boolean *          overflow_ptr
+  unsigned32 /*unsigned long int*/ *  quotient_ptr,
+  unsigned32 /*unsigned long int*/ *  remainder_ptr,
+  int *          overflow_ptr
 )
 {
   unsigned long   ald = sfi >> (N - 1);
@@ -2077,9 +2144,9 @@ divn
   unsigned int       N,
   unsigned long int  als,
   unsigned long int  sfi,
-  signed long int *  quotient_ptr,
-  signed long int *  remainder_ptr,
-  boolean *          overflow_ptr
+  signed32 /*signed long int*/ *  quotient_ptr,
+  signed32 /*signed long int*/ *  remainder_ptr,
+  int *          overflow_ptr
 )
 {
   unsigned long          ald = (signed long) sfi >> (N - 1);
@@ -2174,11 +2241,11 @@ divn
 int
 OP_1C207E0 (void)
 {
-  unsigned long int  quotient;
-  unsigned long int  remainder;
+  unsigned32 /*unsigned long int*/  quotient;
+  unsigned32 /*unsigned long int*/  remainder;
   unsigned long int  divide_by;
   unsigned long int  divide_this;
-  boolean            overflow = false;
+  int            overflow = 0;
   unsigned int       imm5;
       
   trace_input ("sdivun", OP_IMM_REG_REG_REG, 0);
@@ -2209,19 +2276,19 @@ OP_1C207E0 (void)
 int
 OP_1C007E0 (void)
 {
-  signed long int  quotient;
-  signed long int  remainder;
+  signed32 /*signed long int*/  quotient;
+  signed32 /*signed long int*/  remainder;
   signed long int  divide_by;
   signed long int  divide_this;
-  boolean          overflow = false;
+  int          overflow = 0;
   unsigned int     imm5;
       
   trace_input ("sdivn", OP_IMM_REG_REG_REG, 0);
 
   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
 
-  divide_by   = State.regs[ OP[0] ];
-  divide_this = State.regs[ OP[1] ] << imm5;
+  divide_by   = (signed32) State.regs[ OP[0] ];
+  divide_this = (signed32) (State.regs[ OP[1] ] << imm5);
 
   divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
   
@@ -2244,11 +2311,11 @@ OP_1C007E0 (void)
 int
 OP_18207E0 (void)
 {
-  unsigned long int  quotient;
-  unsigned long int  remainder;
+  unsigned32 /*unsigned long int*/  quotient;
+  unsigned32 /*unsigned long int*/  remainder;
   unsigned long int  divide_by;
   unsigned long int  divide_this;
-  boolean            overflow = false;
+  int            overflow = 0;
   unsigned int       imm5;
       
   trace_input ("sdivhun", OP_IMM_REG_REG_REG, 0);
@@ -2279,11 +2346,11 @@ OP_18207E0 (void)
 int
 OP_18007E0 (void)
 {
-  signed long int  quotient;
-  signed long int  remainder;
+  signed32 /*signed long int*/  quotient;
+  signed32 /*signed long int*/  remainder;
   signed long int  divide_by;
   signed long int  divide_this;
-  boolean          overflow = false;
+  int          overflow = 0;
   unsigned int     imm5;
       
   trace_input ("sdivhn", OP_IMM_REG_REG_REG, 0);
@@ -2291,7 +2358,7 @@ OP_18007E0 (void)
   imm5 = 32 - ((OP[3] & 0x3c0000) >> 17);
 
   divide_by   = EXTEND16 (State.regs[ OP[0] ]);
-  divide_this = State.regs[ OP[1] ] << imm5;
+  divide_this = (signed32) (State.regs[ OP[1] ] << imm5);
 
   divn (imm5, divide_by, divide_this, & quotient, & remainder, & overflow);
   
@@ -2310,8 +2377,6 @@ OP_18007E0 (void)
   return 4;
 }
 
-/* end-sanitize-v850eq */
-/* start-sanitize-v850e */
 /* divu  reg1, reg2, reg3 */
 int
 OP_2C207E0 (void)
@@ -2320,7 +2385,7 @@ OP_2C207E0 (void)
   unsigned long int remainder;
   unsigned long int divide_by;
   unsigned long int divide_this;
-  boolean           overflow = false;
+  int           overflow = 0;
   
   trace_input ("divu", OP_REG_REG_REG, 0);
   
@@ -2331,27 +2396,26 @@ OP_2C207E0 (void)
   
   if (divide_by == 0)
     {
-      overflow = true;
-      divide_by  = 1;
+      PSW |= PSW_OV;
     }
+  else
+    {
+      State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
+      State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
   
-  State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
-  State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
+      /* Set condition codes.  */
+      PSW &= ~(PSW_Z | PSW_S | PSW_OV);
   
-  /* Set condition codes.  */
-  PSW &= ~(PSW_Z | PSW_S | PSW_OV);
-  
-  if (overflow)      PSW |= PSW_OV;
-  if (quotient == 0) PSW |= PSW_Z;
-  if (quotient & 0x80000000) PSW |= PSW_S;
+      if (overflow)      PSW |= PSW_OV;
+      if (quotient == 0) PSW |= PSW_Z;
+      if (quotient & 0x80000000) PSW |= PSW_S;
+    }
   
   trace_output (OP_REG_REG_REG);
 
   return 4;
 }
 
-/* end-sanitize-v850e */
-/* start-sanitize-v850e */
 /* div  reg1, reg2, reg3 */
 int
 OP_2C007E0 (void)
@@ -2360,38 +2424,43 @@ OP_2C007E0 (void)
   signed long int remainder;
   signed long int divide_by;
   signed long int divide_this;
-  boolean         overflow = false;
   
   trace_input ("div", OP_REG_REG_REG, 0);
   
   /* Compute the result.  */
   
-  divide_by   = State.regs[ OP[0] ];
+  divide_by   = (signed32) State.regs[ OP[0] ];
   divide_this = State.regs[ OP[1] ];
   
-  if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
+  if (divide_by == 0)
     {
-      overflow  = true;
-      divide_by = 1;
+      PSW |= PSW_OV;
+    }
+  else if (divide_by == -1 && divide_this == (1L << 31))
+    {
+      PSW &= ~PSW_Z;
+      PSW |= PSW_OV | PSW_S;
+      State.regs[ OP[1] ] = (1 << 31);
+      State.regs[ OP[2] >> 11 ] = 0;
+    }
+  else
+    {
+      divide_this = (signed32) divide_this;
+      State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
+      State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
+      /* Set condition codes.  */
+      PSW &= ~(PSW_Z | PSW_S | PSW_OV);
+  
+      if (quotient == 0) PSW |= PSW_Z;
+      if (quotient <  0) PSW |= PSW_S;
     }
-  
-  State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
-  State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
-  
-  /* Set condition codes.  */
-  PSW &= ~(PSW_Z | PSW_S | PSW_OV);
-  
-  if (overflow)      PSW |= PSW_OV;
-  if (quotient == 0) PSW |= PSW_Z;
-  if (quotient <  0) PSW |= PSW_S;
   
   trace_output (OP_REG_REG_REG);
 
   return 4;
 }
 
-/* end-sanitize-v850e */
-/* start-sanitize-v850e */
 /* divhu  reg1, reg2, reg3 */
 int
 OP_28207E0 (void)
@@ -2400,7 +2469,7 @@ OP_28207E0 (void)
   unsigned long int remainder;
   unsigned long int divide_by;
   unsigned long int divide_this;
-  boolean           overflow = false;
+  int           overflow = 0;
   
   trace_input ("divhu", OP_REG_REG_REG, 0);
   
@@ -2411,27 +2480,26 @@ OP_28207E0 (void)
   
   if (divide_by == 0)
     {
-      overflow = true;
-      divide_by  = 1;
+      PSW |= PSW_OV;
     }
+  else
+    {
+      State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
+      State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
   
-  State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
-  State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
-  
-  /* Set condition codes.  */
-  PSW &= ~(PSW_Z | PSW_S | PSW_OV);
+      /* Set condition codes.  */
+      PSW &= ~(PSW_Z | PSW_S | PSW_OV);
   
-  if (overflow)      PSW |= PSW_OV;
-  if (quotient == 0) PSW |= PSW_Z;
-  if (quotient & 0x80000000) PSW |= PSW_S;
+      if (overflow)      PSW |= PSW_OV;
+      if (quotient == 0) PSW |= PSW_Z;
+      if (quotient & 0x80000000) PSW |= PSW_S;
+    }
   
   trace_output (OP_REG_REG_REG);
 
   return 4;
 }
 
-/* end-sanitize-v850e */
-/* start-sanitize-v850e */
 /* divh  reg1, reg2, reg3 */
 int
 OP_28007E0 (void)
@@ -2440,68 +2508,70 @@ OP_28007E0 (void)
   signed long int remainder;
   signed long int divide_by;
   signed long int divide_this;
-  boolean         overflow = false;
+  int         overflow = 0;
   
   trace_input ("divh", OP_REG_REG_REG, 0);
   
   /* Compute the result.  */
   
-  divide_by  = State.regs[ OP[0] ];
-  divide_this = EXTEND16 (State.regs[ OP[1] ]);
+  divide_by  = EXTEND16 (State.regs[ OP[0] ]);
+  divide_this = State.regs[ OP[1] ];
   
-  if (divide_by == 0 || (divide_by == -1 && divide_this == (1 << 31)))
+  if (divide_by == 0)
     {
-      overflow = true;
-      divide_by  = 1;
+      PSW |= PSW_OV;
     }
+  else if (divide_by == -1 && divide_this == (1L << 31))
+    {
+      PSW &= ~PSW_Z;
+      PSW |= PSW_OV | PSW_S;
+      State.regs[ OP[1] ] = (1 << 31);
+      State.regs[ OP[2] >> 11 ] = 0;
+    }
+  else
+    {
+      divide_this = (signed32) divide_this;
+      State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
+      State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
   
-  State.regs[ OP[1]       ] = quotient  = divide_this / divide_by;
-  State.regs[ OP[2] >> 11 ] = remainder = divide_this % divide_by;
-  
-  /* Set condition codes.  */
-  PSW &= ~(PSW_Z | PSW_S | PSW_OV);
+      /* Set condition codes.  */
+      PSW &= ~(PSW_Z | PSW_S | PSW_OV);
   
-  if (overflow)      PSW |= PSW_OV;
-  if (quotient == 0) PSW |= PSW_Z;
-  if (quotient <  0) PSW |= PSW_S;
+      if (quotient == 0) PSW |= PSW_Z;
+      if (quotient <  0) PSW |= PSW_S;
+    }
   
   trace_output (OP_REG_REG_REG);
 
   return 4;
 }
 
-/* end-sanitize-v850e */
-/* start-sanitize-v850e */
 /* mulu imm9, reg2, reg3 */
 int
 OP_24207E0 (void)
 {
   trace_input ("mulu", OP_IMM_REG_REG, 0);
 
-  Multiply64 (false, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
+  Multiply64 (0, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
 
   trace_output (OP_IMM_REG_REG);
 
   return 4;
 }
 
-/* end-sanitize-v850e */
-/* start-sanitize-v850e */
 /* mul imm9, reg2, reg3 */
 int
 OP_24007E0 (void)
 {
   trace_input ("mul", OP_IMM_REG_REG, 0);
 
-  Multiply64 (true, (OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0));
+  Multiply64 (1, SEXT9 ((OP[3] & 0x1f) | ((OP[3] >> 13) & 0x1e0)));
 
   trace_output (OP_IMM_REG_REG);
 
   return 4;
 }
 
-/* end-sanitize-v850e */
-/* start-sanitize-v850e */
 /* ld.hu */
 int
 OP_107E0 (void)
@@ -2520,8 +2590,7 @@ OP_107E0 (void)
   return 4;
 }
 
-/* end-sanitize-v850e */
-/* start-sanitize-v850e */
+
 /* ld.bu */
 int
 OP_10780 (void)
@@ -2640,24 +2709,19 @@ OP_30780 (void)
   return 4;
 }
 
-/* end-sanitize-v850e */
-/* start-sanitize-v850e */
 /* mul reg1, reg2, reg3 */
 int
 OP_22007E0 (void)
 {
   trace_input ("mul", OP_REG_REG_REG, 0);
 
-  Multiply64 (true, State.regs[ OP[0] ]);
+  Multiply64 (1, State.regs[ OP[0] ]);
 
   trace_output (OP_REG_REG_REG);
 
   return 4;
 }
 
-/* end-sanitize-v850e */
-/* start-sanitize-v850eq */
-
 /* popmh list18 */
 int
 OP_307F0 (void)
@@ -2775,4 +2839,3 @@ OP_307E0 (void)
   return 4;
 }
 
-/* end-sanitize-v850eq */