]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - sim/d10v/simops.c
Tue Oct 29 12:13:52 1996 Martin M. Hunt <hunt@pizza.cygnus.com>
[thirdparty/binutils-gdb.git] / sim / d10v / simops.c
index 6b8329b38e3cac5ff010c4126eace5c05af9cb28..a7da24f66b304b0f62f05de115e052aa6a3658ea 100644 (file)
@@ -7,6 +7,9 @@
 #include "d10v_sim.h"
 #include "simops.h"
 #include "sys/syscall.h"
+#include "bfd.h"
+
+extern char *strrchr ();
 
 enum op_types {
   OP_VOID,
@@ -21,38 +24,66 @@ enum op_types {
   OP_CR_OUTPUT,
   OP_CR_REVERSE,
   OP_FLAG,
+  OP_FLAG_OUTPUT,
   OP_CONSTANT16,
+  OP_CONSTANT8,
   OP_CONSTANT3,
   OP_CONSTANT4,
   OP_MEMREF,
   OP_MEMREF2,
   OP_POSTDEC,
   OP_POSTINC,
-  OP_PREDEC
+  OP_PREDEC,
+  OP_R2,
+  OP_R3,
+  OP_R4,
+  OP_R2R3
 };
 
 #ifdef DEBUG
-static void trace_input PARAMS ((char *name,
-                                enum op_types in1,
-                                enum op_types in2,
-                                enum op_types in3));
+static void trace_input_func PARAMS ((char *name,
+                                     enum op_types in1,
+                                     enum op_types in2,
+                                     enum op_types in3));
+
+#define trace_input(name, in1, in2, in3) do { if (d10v_debug) trace_input_func (name, in1, in2, in3); } while (0)
+
+static void trace_output_func PARAMS ((enum op_types result));
 
-static void trace_output PARAMS ((enum op_types result));
+#define trace_output(result) do { if (d10v_debug) trace_output_func (result); } while (0)
+
+static int init_text_p = 0;
+static asection *text;
+static bfd_vma text_start;
+static bfd_vma text_end;
+extern bfd *exec_bfd;
 
 #ifndef SIZE_INSTRUCTION
-#define SIZE_INSTRUCTION 10
+#define SIZE_INSTRUCTION 8
 #endif
 
 #ifndef SIZE_OPERANDS
-#define SIZE_OPERANDS 24
+#define SIZE_OPERANDS 18
 #endif
 
 #ifndef SIZE_VALUES
 #define SIZE_VALUES 13
 #endif
 
+#ifndef SIZE_LOCATION
+#define SIZE_LOCATION 20
+#endif
+
+#ifndef SIZE_PC
+#define SIZE_PC 6
+#endif
+
+#ifndef SIZE_LINE_NUMBER
+#define SIZE_LINE_NUMBER 4
+#endif
+
 static void
-trace_input (name, in1, in2, in3)
+trace_input_func (name, in1, in2, in3)
      char *name;
      enum op_types in1;
      enum op_types in2;
@@ -61,10 +92,15 @@ trace_input (name, in1, in2, in3)
   char *comma;
   enum op_types in[3];
   int i;
-  char buf[80];
+  char buf[1024];
   char *p;
   long tmp;
   char *type;
+  asection *s;
+  const char *filename;
+  const char *functionname;
+  unsigned int linenumber;
+  bfd_vma byte_pc;
 
   if ((d10v_debug & DEBUG_TRACE) == 0)
     return;
@@ -77,13 +113,81 @@ trace_input (name, in1, in2, in3)
     case INS_RIGHT:            type = " R"; break;
     case INS_LEFT_PARALLEL:    type = "*L"; break;
     case INS_RIGHT_PARALLEL:   type = "*R"; break;
+    case INS_LEFT_COND_TEST:   type = "?L"; break;
+    case INS_RIGHT_COND_TEST:  type = "?R"; break;
+    case INS_LEFT_COND_EXE:    type = "&L"; break;
+    case INS_RIGHT_COND_EXE:   type = "&R"; break;
     case INS_LONG:             type = " B"; break;
     }
 
-  (*d10v_callback->printf_filtered) (d10v_callback,
-                                    "0x%.6x %s:  %-*s",
-                                    (unsigned)PC, type,
-                                    SIZE_INSTRUCTION, name);
+  if ((d10v_debug & DEBUG_LINE_NUMBER) == 0)
+    (*d10v_callback->printf_filtered) (d10v_callback,
+                                      "0x%.*x %s: %-*s ",
+                                      SIZE_PC, (unsigned)PC,
+                                      type,
+                                      SIZE_INSTRUCTION, name);
+
+  else
+    {
+      if (!init_text_p)
+       {
+         init_text_p = 1;
+         for (s = exec_bfd->sections; s; s = s->next)
+           if (strcmp (bfd_get_section_name (exec_bfd, s), ".text") == 0)
+             {
+               text = s;
+               text_start = bfd_get_section_vma (exec_bfd, s);
+               text_end = text_start + bfd_section_size (exec_bfd, s);
+               break;
+             }
+       }
+
+      buf[0] = '\0';
+      byte_pc = (bfd_vma)PC << 2;
+      if (text && byte_pc >= text_start && byte_pc < text_end)
+       {
+         filename = (const char *)0;
+         functionname = (const char *)0;
+         linenumber = 0;
+         if (bfd_find_nearest_line (exec_bfd, text, (struct symbol_cache_entry **)0, byte_pc - text_start,
+                                    &filename, &functionname, &linenumber))
+           {
+             p = buf;
+             if (linenumber)
+               {
+                 sprintf (p, "#%-*d ", SIZE_LINE_NUMBER, linenumber);
+                 p += strlen (p);
+               }
+             else
+               {
+                 sprintf (p, "%-*s ", SIZE_LINE_NUMBER+1, "---");
+                 p += SIZE_LINE_NUMBER+2;
+               }
+
+             if (functionname)
+               {
+                 sprintf (p, "%s ", functionname);
+                 p += strlen (p);
+               }
+             else if (filename)
+               {
+                 char *q = strrchr (filename, '/');
+                 sprintf (p, "%s ", (q) ? q+1 : filename);
+                 p += strlen (p);
+               }
+
+             if (*p == ' ')
+               *p = '\0';
+           }
+       }
+
+      (*d10v_callback->printf_filtered) (d10v_callback,
+                                        "0x%.*x %s: %-*.*s %-*s ",
+                                        SIZE_PC, (unsigned)PC,
+                                        type,
+                                        SIZE_LOCATION, SIZE_LOCATION, buf,
+                                        SIZE_INSTRUCTION, name);
+    }
 
   in[0] = in1;
   in[1] = in2;
@@ -95,6 +199,10 @@ trace_input (name, in1, in2, in3)
       switch (in[i])
        {
        case OP_VOID:
+       case OP_R2:
+       case OP_R3:
+       case OP_R4:
+       case OP_R2R3:
          break;
 
        case OP_REG:
@@ -128,6 +236,12 @@ trace_input (name, in1, in2, in3)
          comma = ",";
          break;
 
+       case OP_CONSTANT8:
+         sprintf (p, "%s%d", comma, SEXT8(OP[i]));
+         p += strlen (p);
+         comma = ",";
+         break;
+
        case OP_CONSTANT4:
          sprintf (p, "%s%d", comma, SEXT4(OP[i]));
          p += strlen (p);
@@ -171,6 +285,7 @@ trace_input (name, in1, in2, in3)
          break;
 
        case OP_FLAG:
+       case OP_FLAG_OUTPUT:
          if (OP[i] == 0)
            sprintf (p, "%sf0", comma);
 
@@ -178,7 +293,7 @@ trace_input (name, in1, in2, in3)
            sprintf (p, "%sf1", comma);
 
          else
-           sprintf (p, "%scarry", comma);
+           sprintf (p, "%sc", comma);
 
          p += strlen (p);
          comma = ",";
@@ -211,6 +326,7 @@ trace_input (name, in1, in2, in3)
            case OP_DREG_OUTPUT:
            case OP_CR_OUTPUT:
            case OP_ACCUM_OUTPUT:
+           case OP_FLAG_OUTPUT:
              (*d10v_callback->printf_filtered) (d10v_callback, "%*s", SIZE_VALUES, "---");
              break;
 
@@ -251,6 +367,11 @@ trace_input (name, in1, in2, in3)
                                                 (uint16)SEXT4(OP[i]));
              break;
 
+           case OP_CONSTANT8:
+             (*d10v_callback->printf_filtered) (d10v_callback, "%*s0x%.4x", SIZE_VALUES-6, "",
+                                                (uint16)SEXT8(OP[i]));
+             break;
+
            case OP_CONSTANT3:
              (*d10v_callback->printf_filtered) (d10v_callback, "%*s0x%.4x", SIZE_VALUES-6, "",
                                                 (uint16)SEXT3(OP[i]));
@@ -277,13 +398,36 @@ trace_input (name, in1, in2, in3)
              (*d10v_callback->printf_filtered) (d10v_callback, "%*s0x%.4x", SIZE_VALUES-6, "",
                                                 (uint16)State.regs[OP[++i]]);
              break;
+
+           case OP_R2:
+             (*d10v_callback->printf_filtered) (d10v_callback, "%*s0x%.4x", SIZE_VALUES-6, "",
+                                                (uint16)State.regs[2]);
+             break;
+
+           case OP_R3:
+             (*d10v_callback->printf_filtered) (d10v_callback, "%*s0x%.4x", SIZE_VALUES-6, "",
+                                                (uint16)State.regs[3]);
+             break;
+
+           case OP_R4:
+             (*d10v_callback->printf_filtered) (d10v_callback, "%*s0x%.4x", SIZE_VALUES-6, "",
+                                                (uint16)State.regs[4]);
+             break;
+
+           case OP_R2R3:
+             (*d10v_callback->printf_filtered) (d10v_callback, "%*s0x%.4x", SIZE_VALUES-6, "",
+                                                (uint16)State.regs[2]);
+             (*d10v_callback->printf_filtered) (d10v_callback, "%*s0x%.4x", SIZE_VALUES-6, "",
+                                                (uint16)State.regs[3]);
+             i++;
+             break;
            }
        }
     }
 }
 
 static void
-trace_output (result)
+trace_output_func (result)
      enum op_types result;
 {
   if ((d10v_debug & (DEBUG_TRACE | DEBUG_VALUES)) == (DEBUG_TRACE | DEBUG_VALUES))
@@ -339,9 +483,22 @@ trace_output (result)
          break;
 
        case OP_FLAG:
+       case OP_FLAG_OUTPUT:
          (*d10v_callback->printf_filtered) (d10v_callback, " :: %*s F0=%d F1=%d C=%d\n", SIZE_VALUES, "",
                                             State.F0 != 0, State.F1 != 0, State.C != 0);
          break;
+
+       case OP_R2:
+         (*d10v_callback->printf_filtered) (d10v_callback, " :: %*s0x%.4x F0=%d F1=%d C=%d\n", SIZE_VALUES-6, "",
+                                            (uint16)State.regs[2],
+                                            State.F0 != 0, State.F1 != 0, State.C != 0);
+         break;
+
+       case OP_R2R3:
+         (*d10v_callback->printf_filtered) (d10v_callback, " :: %*s0x%.4x%.4x F0=%d F1=%d C=%d\n", SIZE_VALUES-10, "",
+                                            (uint16)State.regs[2], (uint16)State.regs[3],
+                                            State.F0 != 0, State.F1 != 0, State.C != 0);
+         break;
        }
     }
 }
@@ -580,10 +737,15 @@ OP_17001202 ()
 void
 OP_201 ()
 {
+  uint tmp = State.regs[OP[0]];
   if (OP[1] == 0)
     OP[1] = 16;
   trace_input ("addi", OP_REG, OP_CONSTANT16, OP_VOID);
   State.regs[OP[0]] += OP[1];
+  if (tmp > State.regs[OP[0]])
+    State.C = 1;
+  else
+    State.C = 0;
   trace_output (OP_REG);
 }
 
@@ -618,7 +780,7 @@ OP_C01 ()
 void
 OP_4900 ()
 {
-  trace_input ("bl.s", OP_CONSTANT16, OP_VOID, OP_VOID);
+  trace_input ("bl.s", OP_CONSTANT8, OP_R2, OP_R3);
   State.regs[13] = PC+1;
   PC += SEXT8 (OP[0]);
   trace_output (OP_VOID);
@@ -628,7 +790,7 @@ OP_4900 ()
 void
 OP_24800000 ()
 {
-  trace_input ("bl.l", OP_CONSTANT16, OP_VOID, OP_VOID);
+  trace_input ("bl.l", OP_CONSTANT16, OP_R2, OP_R3);
   State.regs[13] = PC+1;
   PC += OP[0];
   trace_output (OP_VOID);
@@ -647,7 +809,7 @@ OP_A01 ()
 void
 OP_4800 ()
 {
-  trace_input ("bra.s", OP_CONSTANT16, OP_VOID, OP_VOID);
+  trace_input ("bra.s", OP_CONSTANT8, OP_VOID, OP_VOID);
   PC += SEXT8 (OP[0]);
   trace_output (OP_VOID);
 }
@@ -665,7 +827,7 @@ OP_24000000 ()
 void
 OP_4A00 ()
 {
-  trace_input ("brf0f.s", OP_CONSTANT16, OP_VOID, OP_VOID);
+  trace_input ("brf0f.s", OP_CONSTANT8, OP_VOID, OP_VOID);
   if (State.F0 == 0)
     PC += SEXT8 (OP[0]);
   trace_output (OP_FLAG);
@@ -685,7 +847,7 @@ OP_25000000 ()
 void
 OP_4B00 ()
 {
-  trace_input ("brf0t.s", OP_CONSTANT16, OP_VOID, OP_VOID);
+  trace_input ("brf0t.s", OP_CONSTANT8, OP_VOID, OP_VOID);
   if (State.F0)
     PC += SEXT8 (OP[0]);
   trace_output (OP_FLAG);
@@ -773,9 +935,9 @@ OP_1403 ()
 void
 OP_401 ()
 {
-  trace_input ("cmpeqi.s", OP_REG, OP_CONSTANT16, OP_VOID);
+  trace_input ("cmpeqi.s", OP_REG, OP_CONSTANT4, OP_VOID);
   State.F1 = State.F0;
-  State.F0 = (State.regs[OP[0]] == SEXT4(OP[1])) ? 1 : 0;  
+  State.F0 = (State.regs[OP[0]] == (reg_t)SEXT4(OP[1])) ? 1 : 0;  
   trace_output (OP_FLAG);
 }
 
@@ -785,7 +947,7 @@ OP_2000000 ()
 {
   trace_input ("cmpeqi.l", OP_REG, OP_CONSTANT16, OP_VOID);
   State.F1 = State.F0;
-  State.F0 = (State.regs[OP[0]] == OP[1]) ? 1 : 0;  
+  State.F0 = (State.regs[OP[0]] == (reg_t)OP[1]) ? 1 : 0;  
   trace_output (OP_FLAG);
 }
 
@@ -795,7 +957,7 @@ OP_601 ()
 {
   trace_input ("cmpi.s", OP_REG, OP_CONSTANT4, OP_VOID);
   State.F1 = State.F0;
-  State.F0 = ((int16)(State.regs[OP[0]]) < SEXT4(OP[1])) ? 1 : 0;  
+  State.F0 = ((int16)(State.regs[OP[0]]) < (int16)SEXT4(OP[1])) ? 1 : 0;  
   trace_output (OP_FLAG);
 }
 
@@ -825,7 +987,7 @@ OP_23000000 ()
 {
   trace_input ("cmpui", OP_REG, OP_CONSTANT16, OP_VOID);
   State.F1 = State.F0;
-  State.F0 = (State.regs[OP[0]] < OP[1]) ? 1 : 0;  
+  State.F0 = (State.regs[OP[0]] < (reg_t)OP[1]) ? 1 : 0;  
   trace_output (OP_FLAG);
 }
 
@@ -835,7 +997,7 @@ OP_4E09 ()
 {
   uint8 *src, *dst;
   
-  trace_input ("cpfg", OP_FLAG, OP_VOID, OP_VOID);
+  trace_input ("cpfg", OP_FLAG_OUTPUT, OP_FLAG, OP_VOID);
   if (OP[0] == 0)
     dst = &State.F0;
   else
@@ -856,7 +1018,7 @@ OP_4E09 ()
 void
 OP_5F20 ()
 {
-  d10v_callback->printf_filtered(d10v_callback, "***** DBT *****  PC=%x\n",PC);
+  /* d10v_callback->printf_filtered(d10v_callback, "***** DBT *****  PC=%x\n",PC); */
   State.exception = SIGTRAP;
 }
 
@@ -880,7 +1042,7 @@ void
 OP_4E04 ()
 {
   trace_input ("exef0f", OP_VOID, OP_VOID, OP_VOID);
-  State.exe = (State.F0) ? 0 : 1;
+  State.exe = (State.F0 == 0);
   trace_output (OP_FLAG);
 }
 
@@ -889,7 +1051,7 @@ void
 OP_4E24 ()
 {
   trace_input ("exef0t", OP_VOID, OP_VOID, OP_VOID);
-  State.exe = State.F0;
+  State.exe = (State.F0 != 0);
   trace_output (OP_FLAG);
 }
 
@@ -898,7 +1060,7 @@ void
 OP_4E40 ()
 {
   trace_input ("exef1f", OP_VOID, OP_VOID, OP_VOID);
-  State.exe = (State.F1) ? 0 : 1;
+  State.exe = (State.F1 == 0);
   trace_output (OP_FLAG);
 }
 
@@ -907,7 +1069,7 @@ void
 OP_4E42 ()
 {
   trace_input ("exef1t", OP_VOID, OP_VOID, OP_VOID);
-  State.exe = State.F1;
+  State.exe = (State.F1 != 0);
   trace_output (OP_FLAG);
 }
 
@@ -916,7 +1078,7 @@ void
 OP_4E00 ()
 {
   trace_input ("exefaf", OP_VOID, OP_VOID, OP_VOID);
-  State.exe = (State.F0 | State.F1) ? 0 : 1;
+  State.exe = (State.F0 == 0) & (State.F1 == 0);
   trace_output (OP_FLAG);
 }
 
@@ -925,7 +1087,7 @@ void
 OP_4E02 ()
 {
   trace_input ("exefat", OP_VOID, OP_VOID, OP_VOID);
-  State.exe = (State.F0) ? 0 : (State.F1); 
+  State.exe = (State.F0 == 0) & (State.F1 != 0);
   trace_output (OP_FLAG);
 }
 
@@ -934,7 +1096,7 @@ void
 OP_4E20 ()
 {
   trace_input ("exetaf", OP_VOID, OP_VOID, OP_VOID);
-  State.exe = (State.F1) ? 0 : (State.F0);
+  State.exe = (State.F0 != 0) & (State.F1 == 0);
   trace_output (OP_FLAG);
 }
 
@@ -943,7 +1105,7 @@ void
 OP_4E22 ()
 {
   trace_input ("exetat", OP_VOID, OP_VOID, OP_VOID);
-  State.exe = (State.F0) ? (State.F1) : 0;
+  State.exe = (State.F0 != 0) & (State.F1 != 0);
   trace_output (OP_FLAG);
 }
 
@@ -1007,7 +1169,7 @@ OP_15002A02 ()
 void
 OP_4D00 ()
 {
-  trace_input ("jl", OP_REG, OP_VOID, OP_VOID);
+  trace_input ("jl", OP_REG, OP_R2, OP_R3);
   State.regs[13] = PC+1;
   PC = State.regs[OP[0]]; 
   trace_output (OP_VOID);
@@ -1017,7 +1179,10 @@ OP_4D00 ()
 void
 OP_4C00 ()
 {
-  trace_input ("jmp", OP_REG, OP_VOID, OP_VOID);
+  trace_input ("jmp", OP_REG,
+              (OP[0] == 13) ? OP_R2 : OP_VOID,
+              (OP[0] == 13) ? OP_R3 : OP_VOID);
+
   PC = State.regs[OP[0]]; 
   trace_output (OP_VOID);
 }
@@ -1064,9 +1229,10 @@ OP_6000 ()
 void
 OP_31000000 ()
 {
-  trace_input ("ld2w", OP_REG_OUTPUT, OP_MEMREF, OP_VOID);
-  State.regs[OP[0]] = RW (OP[1] + State.regs[OP[2]]);
-  State.regs[OP[0]+1] = RW (OP[1] + State.regs[OP[2]] + 2);
+  uint16 addr = State.regs[OP[2]];
+  trace_input ("ld2w", OP_REG_OUTPUT, OP_MEMREF2, OP_VOID);
+  State.regs[OP[0]] = RW (OP[1] + addr);
+  State.regs[OP[0]+1] = RW (OP[1] + addr + 2);
   trace_output (OP_DREG);
 }
 
@@ -1074,9 +1240,10 @@ OP_31000000 ()
 void
 OP_6601 ()
 {
+  uint16 addr = State.regs[OP[1]];
   trace_input ("ld2w", OP_REG_OUTPUT, OP_POSTDEC, OP_VOID);
-  State.regs[OP[0]] = RW (State.regs[OP[1]]);
-  State.regs[OP[0]+1] = RW (State.regs[OP[1]]+2);
+  State.regs[OP[0]] = RW (addr);
+  State.regs[OP[0]+1] = RW (addr+2);
   INC_ADDR(State.regs[OP[1]],-4);
   trace_output (OP_DREG);
 }
@@ -1085,21 +1252,23 @@ OP_6601 ()
 void
 OP_6201 ()
 {
+  uint16 addr = State.regs[OP[1]];
   trace_input ("ld2w", OP_REG_OUTPUT, OP_POSTINC, OP_VOID);
-  State.regs[OP[0]] = RW (State.regs[OP[1]]);
-  State.regs[OP[0]+1] = RW (State.regs[OP[1]]+2);
+  State.regs[OP[0]] = RW (addr);
+  State.regs[OP[0]+1] = RW (addr+2);
   INC_ADDR(State.regs[OP[1]],4);
-  trace_output (OP_REG);
+  trace_output (OP_DREG);
 }
 
 /* ld2w */
 void
 OP_6200 ()
 {
-  trace_input ("ld", OP_REG_OUTPUT, OP_MEMREF, OP_VOID);
-  State.regs[OP[0]] = RW (State.regs[OP[1]]);
-  State.regs[OP[0]+1] = RW (State.regs[OP[1]]+2);
-  trace_output (OP_REG);
+  uint16 addr = State.regs[OP[1]];
+  trace_input ("ld2w", OP_REG_OUTPUT, OP_MEMREF, OP_VOID);
+  State.regs[OP[0]] = RW (addr);
+  State.regs[OP[0]+1] = RW (addr+2);
+  trace_output (OP_DREG);
 }
 
 /* ldb */
@@ -1107,8 +1276,7 @@ void
 OP_38000000 ()
 {
   trace_input ("ldb", OP_REG_OUTPUT, OP_MEMREF2, OP_VOID);
-  State.regs[OP[0]] = RB (OP[1] + State.regs[OP[2]]);
-  SEXT8 (State.regs[OP[0]]);
+  State.regs[OP[0]] = SEXT8 (RB (OP[1] + State.regs[OP[2]]));
   trace_output (OP_REG);
 }
 
@@ -1117,8 +1285,7 @@ void
 OP_7000 ()
 {
   trace_input ("ldb", OP_REG_OUTPUT, OP_MEMREF, OP_VOID);
-  State.regs[OP[0]] = RB (State.regs[OP[1]]);
-  SEXT8 (State.regs[OP[0]]);
+  State.regs[OP[0]] = SEXT8 (RB (State.regs[OP[1]]));
   trace_output (OP_REG);
 }
 
@@ -1676,12 +1843,40 @@ void
 OP_5E00 ()
 {
   trace_input ("nop", OP_VOID, OP_VOID, OP_VOID);
-  trace_output (OP_VOID);
 
-  if (State.ins_type == INS_LEFT || State.ins_type == INS_LEFT_PARALLEL)
-    left_nops++;
-  else
-    right_nops++;
+  ins_type_counters[ (int)State.ins_type ]--;  /* don't count nops as normal instructions */
+  switch (State.ins_type)
+    {
+    default:
+      ins_type_counters[ (int)INS_UNKNOWN ]++;
+      break;
+
+    case INS_LEFT_PARALLEL:
+      /* Don't count a parallel op that includes a NOP as a true parallel op */
+      ins_type_counters[ (int)INS_RIGHT_PARALLEL ]--;
+      ins_type_counters[ (int)INS_RIGHT ]++;
+      ins_type_counters[ (int)INS_LEFT_NOPS ]++;
+      break;
+
+    case INS_LEFT:
+    case INS_LEFT_COND_EXE:
+      ins_type_counters[ (int)INS_LEFT_NOPS ]++;
+      break;
+
+    case INS_RIGHT_PARALLEL:
+      /* Don't count a parallel op that includes a NOP as a true parallel op */
+      ins_type_counters[ (int)INS_LEFT_PARALLEL ]--;
+      ins_type_counters[ (int)INS_LEFT ]++;
+      ins_type_counters[ (int)INS_RIGHT_NOPS ]++;
+      break;
+
+    case INS_RIGHT:
+    case INS_RIGHT_COND_EXE:
+      ins_type_counters[ (int)INS_RIGHT_NOPS ]++;
+      break;
+    }
+
+  trace_output (OP_VOID);
 }
 
 /* not */
@@ -1975,8 +2170,6 @@ OP_3201 ()
 void
 OP_460B ()
 {
-  uint16 tmp;
-
   trace_input ("slx", OP_REG, OP_FLAG, OP_VOID);
   State.regs[OP[0]] = (State.regs[OP[0]] << 1) | State.F0;
   trace_output (OP_REG);
@@ -2156,7 +2349,7 @@ OP_35000000 ()
 void
 OP_6A00 ()
 {
-  trace_input ("st2w", OP_REG, OP_MEMREF, OP_VOID);
+  trace_input ("st2w", OP_DREG, OP_MEMREF, OP_VOID);
   SW (State.regs[OP[1]],   State.regs[OP[0]]);
   SW (State.regs[OP[1]]+2, State.regs[OP[0]+1]);
   trace_output (OP_VOID);
@@ -2166,7 +2359,7 @@ OP_6A00 ()
 void
 OP_6E1F ()
 {
-  trace_input ("st2w", OP_REG, OP_PREDEC, OP_VOID);
+  trace_input ("st2w", OP_DREG, OP_PREDEC, OP_VOID);
   if ( OP[1] != 15 )
     {
       (*d10v_callback->printf_filtered) (d10v_callback, "ERROR: cannot pre-decrement any registers but r15 (SP).\n");
@@ -2183,7 +2376,7 @@ OP_6E1F ()
 void
 OP_6A01 ()
 {
-  trace_input ("st2w", OP_REG, OP_POSTDEC, OP_VOID);
+  trace_input ("st2w", OP_DREG, OP_POSTDEC, OP_VOID);
   SW (State.regs[OP[1]],   State.regs[OP[0]]);
   SW (State.regs[OP[1]]+2, State.regs[OP[0]+1]);
   INC_ADDR (State.regs[OP[1]],4);
@@ -2194,7 +2387,7 @@ OP_6A01 ()
 void
 OP_6E01 ()
 {
-  trace_input ("st2w", OP_REG, OP_POSTINC, OP_VOID);
+  trace_input ("st2w", OP_DREG, OP_POSTINC, OP_VOID);
   SW (State.regs[OP[1]],   State.regs[OP[0]]);
   SW (State.regs[OP[1]]+2, State.regs[OP[0]+1]);
   INC_ADDR (State.regs[OP[1]],-4);
@@ -2224,7 +2417,7 @@ void
 OP_5FE0 ()
 {
   trace_input ("stop", OP_VOID, OP_VOID, OP_VOID);
-  State.exception = SIGQUIT;
+  State.exception = SIG_D10V_STOP;
   trace_output (OP_VOID);
 }
 
@@ -2410,57 +2603,224 @@ OP_1 ()
 void
 OP_5F00 ()
 {
-  trace_input ("trap", OP_CONSTANT16, OP_REG, OP_VOID);
+  trace_input ("trap", OP_CONSTANT4, OP_VOID, OP_VOID);
   trace_output (OP_VOID);
-  
+
   switch (OP[0])
     {
     default:
+#if 0
       (*d10v_callback->printf_filtered) (d10v_callback, "Unknown trap code %d\n", OP[0]);
       State.exception = SIGILL;
+#else
+      /* Use any other traps for batch debugging. */
+      {
+       int i;
+       static int first_time = 1;
+
+       if (first_time)
+         {
+           first_time = 0;
+           (*d10v_callback->printf_filtered) (d10v_callback, "Trap  #     PC ");
+           for (i = 0; i < 16; i++)
+             (*d10v_callback->printf_filtered) (d10v_callback, "  %sr%d", (i > 9) ? "" : " ", i);
+           (*d10v_callback->printf_filtered) (d10v_callback, "         a0         a1 f0 f1 c\n");
+         }
+
+      (*d10v_callback->printf_filtered) (d10v_callback, "Trap %2d 0x%.4x:", (int)OP[0], (int)PC);
+
+      for (i = 0; i < 16; i++)
+       (*d10v_callback->printf_filtered) (d10v_callback, " %.4x", (int) State.regs[i]);
+
+      for (i = 0; i < 2; i++)
+       (*d10v_callback->printf_filtered) (d10v_callback, " %.2x%.8lx",
+                                          ((int)(State.a[i] >> 32) & 0xff),
+                                          ((unsigned long)State.a[i]) & 0xffffffff);
+
+      (*d10v_callback->printf_filtered) (d10v_callback, "  %d  %d %d\n",
+                                        State.F0 != 0, State.F1 != 0, State.C != 0);
+      break;
+#endif
 
     case 0:
       /* Trap 0 is used for simulating low-level I/O */
       {
-       int save_errno = errno; 
        errno = 0;
 
 /* Registers passed to trap 0 */
 
-#define FUNC   State.regs[2]   /* function number, return value */
-#define PARM1  State.regs[3]   /* optional parm 1 */
-#define PARM2  State.regs[4]   /* optional parm 2 */
-#define PARM3  State.regs[5]   /* optional parm 3 */
+#define FUNC   State.regs[6]   /* function number */
+#define PARM1  State.regs[2]   /* optional parm 1 */
+#define PARM2  State.regs[3]   /* optional parm 2 */
+#define PARM3  State.regs[4]   /* optional parm 3 */
+#define PARM4  State.regs[5]   /* optional parm 3 */
 
 /* Registers set by trap 0 */
 
-#define RETVAL State.regs[2]   /* return value */
-#define RETERR State.regs[3]   /* return error code */
+#define RETVAL State.regs[2]           /* return value */
+#define RETVAL_HIGH State.regs[2]      /* return value */
+#define RETVAL_LOW  State.regs[3]      /* return value */
+#define RETERR State.regs[4]           /* return error code */
 
 /* Turn a pointer in a register into a pointer into real memory. */
 
-#define MEMPTR(x) ((char *)((x) + State.imem))
+#define MEMPTR(x) ((char *)(dmem_addr(x)))
 
        switch (FUNC)
          {
 #if !defined(__GO32__) && !defined(_WIN32)
-#ifdef SYS_fork
          case SYS_fork:
            RETVAL = fork ();
+           trace_input ("<fork>", OP_VOID, OP_VOID, OP_VOID);
+           trace_output (OP_R2);
            break;
+
+         case SYS_getpid:
+           trace_input ("<getpid>", OP_VOID, OP_VOID, OP_VOID);
+           RETVAL = getpid ();
+           trace_output (OP_R2);
+           break;
+
+         case SYS_kill:
+           trace_input ("<kill>", OP_REG, OP_REG, OP_VOID);
+           if (PARM1 == getpid ())
+             {
+               trace_output (OP_VOID);
+               State.exception = PARM2;
+             }
+           else
+             {
+               int os_sig = -1;
+               switch (PARM2)
+                 {
+#ifdef SIGHUP
+                 case 1: os_sig = SIGHUP;      break;
+#endif
+#ifdef SIGINT
+                 case 2: os_sig = SIGINT;      break;
+#endif
+#ifdef SIGQUIT
+                 case 3: os_sig = SIGQUIT;     break;
+#endif
+#ifdef SIGILL
+                 case 4: os_sig = SIGILL;      break;
+#endif
+#ifdef SIGTRAP
+                 case 5: os_sig = SIGTRAP;     break;
+#endif
+#ifdef SIGABRT
+                 case 6: os_sig = SIGABRT;     break;
+#elif defined(SIGIOT)
+                 case 6: os_sig = SIGIOT;      break;
+#endif
+#ifdef SIGEMT
+                 case 7: os_sig = SIGEMT;      break;
+#endif
+#ifdef SIGFPE
+                 case 8: os_sig = SIGFPE;      break;
+#endif
+#ifdef SIGKILL
+                 case 9: os_sig = SIGKILL;     break;
+#endif
+#ifdef SIGBUS
+                 case 10: os_sig = SIGBUS;     break;
+#endif
+#ifdef SIGSEGV
+                 case 11: os_sig = SIGSEGV;    break;
+#endif
+#ifdef SIGSYS
+                 case 12: os_sig = SIGSYS;     break;
+#endif
+#ifdef SIGPIPE
+                 case 13: os_sig = SIGPIPE;    break;
+#endif
+#ifdef SIGALRM
+                 case 14: os_sig = SIGALRM;    break;
+#endif
+#ifdef SIGTERM
+                 case 15: os_sig = SIGTERM;    break;
+#endif
+#ifdef SIGURG
+                 case 16: os_sig = SIGURG;     break;
+#endif
+#ifdef SIGSTOP
+                 case 17: os_sig = SIGSTOP;    break;
+#endif
+#ifdef SIGTSTP
+                 case 18: os_sig = SIGTSTP;    break;
+#endif
+#ifdef SIGCONT
+                 case 19: os_sig = SIGCONT;    break;
+#endif
+#ifdef SIGCHLD
+                 case 20: os_sig = SIGCHLD;    break;
+#elif defined(SIGCLD)
+                 case 20: os_sig = SIGCLD;     break;
+#endif
+#ifdef SIGTTIN
+                 case 21: os_sig = SIGTTIN;    break;
+#endif
+#ifdef SIGTTOU
+                 case 22: os_sig = SIGTTOU;    break;
+#endif
+#ifdef SIGIO
+                 case 23: os_sig = SIGIO;      break;
+#elif defined (SIGPOLL)
+                 case 23: os_sig = SIGPOLL;    break;
+#endif
+#ifdef SIGXCPU
+                 case 24: os_sig = SIGXCPU;    break;
+#endif
+#ifdef SIGXFSZ
+                 case 25: os_sig = SIGXFSZ;    break;
+#endif
+#ifdef SIGVTALRM
+                 case 26: os_sig = SIGVTALRM;  break;
 #endif
-#ifdef SYS_execve
+#ifdef SIGPROF
+                 case 27: os_sig = SIGPROF;    break;
+#endif
+#ifdef SIGWINCH
+                 case 28: os_sig = SIGWINCH;   break;
+#endif
+#ifdef SIGLOST
+                 case 29: os_sig = SIGLOST;    break;
+#endif
+#ifdef SIGUSR1
+                 case 30: os_sig = SIGUSR1;    break;
+#endif
+#ifdef SIGUSR2
+                 case 31: os_sig = SIGUSR2;    break;
+#endif
+                 }
+
+               if (os_sig == -1)
+                 {
+                   trace_output (OP_VOID);
+                   (*d10v_callback->printf_filtered) (d10v_callback, "Unknown signal %d\n", PARM2);
+                   State.exception = SIGILL;
+                 }
+               else
+                 {
+                   RETVAL = kill (PARM1, PARM2);
+                   trace_output (OP_R2);
+                 }
+             }
+           break;
+
          case SYS_execve:
            RETVAL = execve (MEMPTR (PARM1), (char **) MEMPTR (PARM2),
                             (char **)MEMPTR (PARM3));
+           trace_input ("<execve>", OP_R2, OP_R3, OP_R4);
+           trace_output (OP_R2);
            break;
-#endif
-#ifdef SYS_execv
+
          case SYS_execv:
            RETVAL = execve (MEMPTR (PARM1), (char **) MEMPTR (PARM2), NULL);
+           trace_input ("<execv>", OP_R2, OP_R3, OP_VOID);
+           trace_output (OP_R2);
            break;
-#endif
-#ifdef SYS_pipe
+
          case SYS_pipe:
            {
              reg_t buf;
@@ -2471,28 +2831,43 @@ OP_5F00 ()
              SW (buf, host_fd[0]);
              buf += sizeof(uint16);
              SW (buf, host_fd[1]);
+             trace_input ("<pipe>", OP_R2, OP_VOID, OP_VOID);
+             trace_output (OP_R2);
            }
          break;
-#endif
-#ifdef SYS_wait
+
          case SYS_wait:
            {
              int status;
 
              RETVAL = wait (&status);
-             SW (PARM1, status);
+             if (PARM1)
+               SW (PARM1, status);
+             trace_input ("<wait>", OP_R2, OP_VOID, OP_VOID);
+             trace_output (OP_R2);
            }
          break;
-#endif
+#else
+         case SYS_getpid:
+           trace_input ("<getpid>", OP_VOID, OP_VOID, OP_VOID);
+           RETVAL = 1;
+           trace_output (OP_R2);
+           break;
+
+         case SYS_kill:
+           trace_input ("<kill>", OP_REG, OP_REG, OP_VOID);
+           trace_output (OP_VOID);
+           State.exception = PARM2;
+           break;
 #endif
 
-#ifdef SYS_read
          case SYS_read:
            RETVAL = d10v_callback->read (d10v_callback, PARM1, MEMPTR (PARM2),
                                          PARM3);
+           trace_input ("<read>", OP_R2, OP_R3, OP_R4);
+           trace_output (OP_R2);
            break;
-#endif
-#ifdef SYS_write
+
          case SYS_write:
            if (PARM1 == 1)
              RETVAL = (int)d10v_callback->write_stdout (d10v_callback,
@@ -2500,32 +2875,42 @@ OP_5F00 ()
            else
              RETVAL = (int)d10v_callback->write (d10v_callback, PARM1,
                                                  MEMPTR (PARM2), PARM3);
+           trace_input ("<write>", OP_R2, OP_R3, OP_R4);
+           trace_output (OP_R2);
            break;
-#endif
-#ifdef SYS_lseek
+
          case SYS_lseek:
-           RETVAL = d10v_callback->lseek (d10v_callback, PARM1, PARM2, PARM3);
+           {
+             unsigned long ret = d10v_callback->lseek (d10v_callback, PARM1,
+                       (((unsigned long)PARM2) << 16) || (unsigned long)PARM3,
+                       PARM4);
+             RETVAL_HIGH = ret >> 16;
+             RETVAL_LOW  = ret & 0xffff;
+           }
+           trace_input ("<lseek>", OP_R2, OP_R3, OP_R4);
+           trace_output (OP_R2R3);
            break;
-#endif
-#ifdef SYS_close
+
          case SYS_close:
            RETVAL = d10v_callback->close (d10v_callback, PARM1);
+           trace_input ("<close>", OP_R2, OP_VOID, OP_VOID);
+           trace_output (OP_R2);
            break;
-#endif
-#ifdef SYS_open
+
          case SYS_open:
            RETVAL = d10v_callback->open (d10v_callback, MEMPTR (PARM1), PARM2);
+           trace_input ("<open>", OP_R2, OP_R3, OP_R4);
+           trace_output (OP_R2);
+           trace_input ("<open>", OP_R2, OP_R3, OP_R4);
+           trace_output (OP_R2);
            break;
-#endif
-#ifdef SYS_exit
+
          case SYS_exit:
-           /* EXIT - caller can look in PARM1 to work out the 
-              reason */
-           State.exception = SIGQUIT;
+           State.exception = SIG_D10V_EXIT;
+           trace_input ("<exit>", OP_R2, OP_VOID, OP_VOID);
+           trace_output (OP_VOID);
            break;
-#endif
 
-#ifdef SYS_stat
          case SYS_stat:
            /* stat system call */
            {
@@ -2551,38 +2936,51 @@ OP_5F00 ()
              SLW (buf+28, host_stat.st_mtime);
              SLW (buf+36, host_stat.st_ctime);
            }
+           trace_input ("<stat>", OP_R2, OP_R3, OP_VOID);
+           trace_output (OP_R2);
            break;
-#endif
 
-#ifdef SYS_chown
          case SYS_chown:
            RETVAL = chown (MEMPTR (PARM1), PARM2, PARM3);
+           trace_input ("<chown>", OP_R2, OP_R3, OP_R4);
+           trace_output (OP_R2);
            break;
-#endif
-#ifdef SYS_chmod
+
          case SYS_chmod:
            RETVAL = chmod (MEMPTR (PARM1), PARM2);
+           trace_input ("<chmod>", OP_R2, OP_R3, OP_R4);
+           trace_output (OP_R2);
            break;
-#endif
-#ifdef SYS_utime
+
          case SYS_utime:
            /* Cast the second argument to void *, to avoid type mismatch
               if a prototype is present.  */
            RETVAL = utime (MEMPTR (PARM1), (void *) MEMPTR (PARM2));
+           trace_input ("<utime>", OP_R2, OP_R3, OP_R4);
+           trace_output (OP_R2);
            break;
-#endif
+
+         case SYS_time:
+           {
+             unsigned long ret = time (PARM1 ? MEMPTR (PARM1) : NULL);
+             RETVAL_HIGH = ret >> 16;
+             RETVAL_LOW  = ret & 0xffff;
+           }
+           trace_input ("<time>", OP_R2, OP_R3, OP_R4);
+           trace_output (OP_R2R3);
+           break;
+           
          default:
            abort ();
          }
-       RETERR = errno;
-       errno = save_errno;
+       RETERR = d10v_callback->get_errno(d10v_callback);
        break;
       }
 
     case 1:
       /* Trap 1 prints a string */
       {
-       char *fstr = State.regs[2] + State.imem;
+       char *fstr = dmem_addr(State.regs[2]);
        fputs (fstr, stdout);
        break;
       }
@@ -2590,7 +2988,7 @@ OP_5F00 ()
     case 2:
       /* Trap 2 calls printf */
       {
-       char *fstr = State.regs[2] + State.imem;
+       char *fstr = dmem_addr(State.regs[2]);
        (*d10v_callback->printf_filtered) (d10v_callback, fstr,
                                           (int16)State.regs[3],
                                           (int16)State.regs[4],
@@ -2602,6 +3000,7 @@ OP_5F00 ()
       /* Trap 3 writes a character */
       putchar (State.regs[2]);
       break;
+      }
     }
 }