/* Simulator for Motorola's MCore processor
-   Copyright (C) 1999 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2000 Free Software Foundation, Inc.
    Contributed by Cygnus Solutions.
 
 This file is part of GDB, the GNU debugger.
 typedef long int           word;
 typedef unsigned long int  uword;
 
+static int            target_big_endian = 0;
 static unsigned long  heap_ptr = 0;
 host_callback *       callback;
 
      the least significant.  */
   retval = 0;
 
+  if (! target_big_endian)
+    {
+      for (p = endaddr; p > startaddr;)
+       retval = (retval << 8) | * -- p;
+    }
+  else
     {
       for (p = startaddr; p < endaddr;)
        retval = (retval << 8) | * p ++;
   unsigned char * startaddr = (unsigned char *)addr;
   unsigned char * endaddr = startaddr + len;
 
+  if (! target_big_endian)
+    {
+      for (p = startaddr; p < endaddr;)
+       {
+         * p ++ = val & 0xff;
+         val >>= 8;
+       }
+    }
+  else
     {
       for (p = endaddr; p > startaddr;)
        {
    Keeping this data in target byte order simplifies the register
    read/write functions.  Keeping this data in native order improves
    the performance of the simulator.  Simulation speed is deemed more
-   important. */
+   important.  */
 
 /* The ordering of the mcore_regset structure is matched in the
    gdb/config/mcore/tm-mcore.h file in the REGISTER_NAMES macro.  */
       
          cpu.asregs.exception = SIGBUS;
        }
+      else if (! target_big_endian)
+       {
+         unsigned char * p = cpu.mem + x;
+         p[3] = v >> 24;
+         p[2] = v >> 16;
+         p[1] = v >> 8;
+         p[0] = v;
+       }
       else
        {
          unsigned char * p = cpu.mem + x;
       
          cpu.asregs.exception = SIGBUS;
        }
+      else if (! target_big_endian)
+       {
+         unsigned char * p = cpu.mem + x;
+         p[1] = v >> 8;
+         p[0] = v;
+       }
       else
        {
          unsigned char * p = cpu.mem + x;
          cpu.asregs.exception = SIGBUS;
          return 0;
        }
+      else if (! target_big_endian)
+       {
+         unsigned char * p = cpu.mem + x;
+         return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
+       }
       else
        {
          unsigned char * p = cpu.mem + x;
          cpu.asregs.exception = SIGBUS;
          return 0;
        }
+      else if (! target_big_endian)
+       {
+         unsigned char * p = cpu.mem + x;
+         return (p[1] << 8) | p[0];
+       }
       else
        {
          unsigned char * p = cpu.mem + x;
       
       if (pc & 02)
        {
+         if (! target_big_endian)
+           inst = ibuf >> 16;
+         else
            inst = ibuf & 0xFFFF;
          needfetch = 1;
        }
       else
        {
+         if (! target_big_endian)
+           inst = ibuf & 0xFFFF;
+         else
            inst = ibuf >> 16;
        }
 
   if (prog_bfd == NULL)
     return SIM_RC_FAIL;
   
+  target_big_endian = bfd_big_endian (prog_bfd);
     
   if (abfd == NULL)
     bfd_close (prog_bfd);