/[gxemul]/trunk/src/emul.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/src/emul.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 10 by dpavlin, Mon Oct 8 16:18:27 2007 UTC revision 36 by dpavlin, Mon Oct 8 16:21:34 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *  Copyright (C) 2003-2005  Anders Gavare.  All rights reserved.   *  Copyright (C) 2003-2007  Anders Gavare.  All rights reserved.
3   *   *
4   *  Redistribution and use in source and binary forms, with or without   *  Redistribution and use in source and binary forms, with or without
5   *  modification, are permitted provided that the following conditions are met:   *  modification, are permitted provided that the following conditions are met:
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *   *
27   *   *
28   *  $Id: emul.c,v 1.211 2005/06/26 11:36:28 debug Exp $   *  $Id: emul.c,v 1.279 2007/03/16 14:45:30 debug Exp $
29   *   *
30   *  Emulation startup and misc. routines.   *  Emulation startup and misc. routines.
31   */   */
# Line 39  Line 39 
39  #include <unistd.h>  #include <unistd.h>
40    
41  #include "arcbios.h"  #include "arcbios.h"
 #include "bintrans.h"  
42  #include "cpu.h"  #include "cpu.h"
43  #include "emul.h"  #include "emul.h"
44  #include "console.h"  #include "console.h"
# Line 52  Line 51 
51  #include "mips_cpu_types.h"  #include "mips_cpu_types.h"
52  #include "misc.h"  #include "misc.h"
53  #include "net.h"  #include "net.h"
54    #include "settings.h"
55  #include "sgi_arcbios.h"  #include "sgi_arcbios.h"
56    #include "timer.h"
57  #include "x11.h"  #include "x11.h"
58    
59    
 extern int force_debugger_at_exit;  
   
60  extern int extra_argc;  extern int extra_argc;
61  extern char **extra_argv;  extern char **extra_argv;
62    
63  extern int verbose;  extern int verbose;
64  extern int quiet_mode;  extern int quiet_mode;
65    extern int force_debugger_at_exit;
66    extern int single_step;
67    extern int old_show_trace_tree;
68    extern int old_instruction_trace;
69    extern int old_quiet_mode;
70    extern int quiet_mode;
71    
72  extern struct emul *debugger_emul;  extern struct emul *debugger_emul;
73  extern struct diskimage *diskimages[];  extern struct diskimage *diskimages[];
74    
75  static char *diskimage_types[] = DISKIMAGE_TYPES;  
76    static void print_separator(void)
77    {
78            int i = 79;
79            while (i-- > 0)
80                    debug("-");
81            debug("\n");
82    }
83    
84    
85  /*  /*
# Line 96  static void add_dump_points(struct machi Line 108  static void add_dump_points(struct machi
108                          uint64_t addr;                          uint64_t addr;
109                          int res = get_symbol_addr(&m->symbol_context,                          int res = get_symbol_addr(&m->symbol_context,
110                              m->breakpoint_string[i], &addr);                              m->breakpoint_string[i], &addr);
111                          if (!res)                          if (!res) {
112                                  fprintf(stderr,                                  fprintf(stderr,
113                                      "WARNING! Breakpoint '%s' could not be"                                      "ERROR! Breakpoint '%s' could not be"
114                                          " parsed\n",                                          " parsed\n",
115                                      m->breakpoint_string[i]);                                      m->breakpoint_string[i]);
116                          else {                          } else {
117                                  dp = addr;                                  dp = addr;
118                                  string_flag = 1;                                  string_flag = 1;
119                          }                          }
# Line 112  static void add_dump_points(struct machi Line 124  static void add_dump_points(struct machi
124                   *  were automatically converted into the correct address.                   *  were automatically converted into the correct address.
125                   */                   */
126    
127                  if ((dp >> 32) == 0 && ((dp >> 31) & 1))                  if (m->arch == ARCH_MIPS) {
128                          dp |= 0xffffffff00000000ULL;                          if ((dp >> 32) == 0 && ((dp >> 31) & 1))
129                                    dp |= 0xffffffff00000000ULL;
130                    }
131    
132                  m->breakpoint_addr[i] = dp;                  m->breakpoint_addr[i] = dp;
133    
134                  debug("breakpoint %i: 0x%016llx", i, (long long)dp);                  debug("breakpoint %i: 0x%llx", i, (long long)dp);
135                  if (string_flag)                  if (string_flag)
136                          debug(" (%s)", m->breakpoint_string[i]);                          debug(" (%s)", m->breakpoint_string[i]);
137                  debug("\n");                  debug("\n");
# Line 129  static void add_dump_points(struct machi Line 144  static void add_dump_points(struct machi
144   */   */
145  static void fix_console(void)  static void fix_console(void)
146  {  {
147          console_deinit();          console_deinit_main();
148  }  }
149    
150    
151  /*  /*
152   *  iso_load_bootblock():   *  emul_new():
  *  
  *  Try to load a kernel from an ISO 9660 disk image. iso_type is 1 for  
  *  "CD001" (standard), 2 for "CDW01" (ECMA), and 3 for "CDROM" (Sierra).  
  *  
  *  TODO: This function uses too many magic offsets and so on; it should be  
  *  cleaned up some day.  
153   *   *
154   *  Returns 1 on success, 0 on failure.   *  Returns a reasonably initialized struct emul.
155   */   */
156  static int iso_load_bootblock(struct machine *m, struct cpu *cpu,  struct emul *emul_new(char *name, int id)
         int disk_id, int disk_type, int iso_type, unsigned char *buf,  
         int *n_loadp, char ***load_namesp)  
157  {  {
158          char str[35];          struct emul *e;
159          int filenr, i, ofs, dirlen, res = 0, res2, iadd = 4;          e = malloc(sizeof(struct emul));
160          int found_dir;          if (e == NULL) {
161          uint64_t dirofs;                  fprintf(stderr, "out of memory in emul_new()\n");
         uint64_t fileofs, filelen;  
         unsigned char *dirbuf = NULL, *dp;  
         unsigned char *match_entry = NULL;  
         char *p, *filename_orig;  
         char *filename = strdup(cpu->machine->boot_kernel_filename);  
         unsigned char *filebuf = NULL;  
         char *tmpfilename = NULL;  
         char **new_array;  
         int tmpfile_handle;  
   
         if (filename == NULL) {  
                 fatal("out of memory\n");  
                 exit(1);  
         }  
         filename_orig = filename;  
   
         debug("ISO9660 boot:\n");  
         debug_indentation(iadd);  
   
         /*  Volume ID:  */  
         ofs = iso_type == 3? 48 : 40;  
         memcpy(str, buf + ofs, sizeof(str));  
         str[32] = '\0';  i = 31;  
         while (i >= 0 && str[i]==' ')  
                 str[i--] = '\0';  
         if (str[0])  
                 debug("\"%s\"", str);  
         else {  
                 /*  System ID:  */  
                 ofs = iso_type == 3? 16 : 8;  
                 memcpy(str, buf + ofs, sizeof(str));  
                 str[32] = '\0';  i = 31;  
                 while (i >= 0 && str[i]==' ')  
                         str[i--] = '\0';  
                 if (str[0])  
                         debug("\"%s\"", str);  
                 else  
                         debug("(no ID)");  
         }  
   
         debug(":%s\n", filename);  
   
   
         /*  
          *  Traverse the directory structure to find the kernel.  
          */  
   
         dirlen = buf[0x84] + 256*buf[0x85] + 65536*buf[0x86];  
         if (dirlen != buf[0x8b] + 256*buf[0x8a] + 65536*buf[0x89])  
                 fatal("WARNING: Root directory length mismatch?\n");  
   
         dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) +  
             (buf[0x8f] << 24)) * 2048;  
   
         /*  debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs);  */  
   
         dirbuf = malloc(dirlen);  
         if (dirbuf == NULL) {  
                 fatal("out of memory in iso_load_bootblock()\n");  
                 exit(1);  
         }  
   
         res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, dirbuf,  
             dirlen);  
         if (!res2) {  
                 fatal("Couldn't read the disk image. Aborting.\n");  
                 goto ret;  
         }  
   
         found_dir = 1;  /*  Assume root dir  */  
         dp = dirbuf; filenr = 1;  
         p = NULL;  
         while (dp < dirbuf + dirlen) {  
                 int i, nlen = dp[0];  
                 int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) + (dp[5] << 24);  
                 int y = dp[6] + (dp[7] << 8);  
                 char direntry[65];  
   
                 dp += 8;  
   
                 /*  
                  *  As long as there is an \ or / in the filename, then we  
                  *  have not yet found the directory.  
                  */  
                 p = strchr(filename, '/');  
                 if (p == NULL)  
                         p = strchr(filename, '\\');  
   
                 /*  debug("%i%s: %i, %i, \"", filenr, filenr == found_dir?  
                     " [CURRENT]" : "", x, y);  */  
                 for (i=0; i<nlen && i<sizeof(direntry)-1; i++)  
                         if (dp[i]) {  
                                 direntry[i] = dp[i];  
                                 /*  debug("%c", dp[i]);  */  
                         } else  
                                 break;  
                 /*  debug("\"\n");  */  
                 direntry[i] = '\0';  
   
                 /*  A directory name match?  */  
                 if (p != NULL && strncasecmp(filename, direntry, nlen) == 0  
                     && nlen == (size_t)p - (size_t)filename && found_dir == y) {  
                         found_dir = filenr;  
                         filename = p+1;  
                         dirofs = 2048 * (int64_t)x;  
                 }  
   
                 dp += nlen;  
   
                 /*  16-bit aligned lenght:  */  
                 if (nlen & 1)  
                         dp ++;  
   
                 filenr ++;  
         }  
   
         p = strchr(filename, '/');  
         if (p == NULL)  
                 p = strchr(filename, '\\');  
   
         if (p != NULL) {  
                 char *blah = filename_orig;  
   
                 fatal("could not find '%s' in /", filename);  
   
                 /*  Print the first part of the filename:  */  
                 while (blah != filename)  
                         fatal("%c", *blah++);  
                   
                 fatal("\n");  
                 goto ret;  
         }  
   
         /*  debug("dirofs = 0x%llx\n", (long long)dirofs);  */  
   
         /*  Free the old dirbuf, and allocate a new one:  */  
         free(dirbuf);  
         dirbuf = malloc(512);  
         if (dirbuf == NULL) {  
                 fatal("out of memory in iso_load_bootblock()\n");  
162                  exit(1);                  exit(1);
163          }          }
164    
165          for (;;) {          memset(e, 0, sizeof(struct emul));
                 int len, i;  
   
                 /*  Too close to another sector? Then realign.  */  
                 if ((dirofs & 2047) + 70 > 2047) {  
                         dirofs = (dirofs | 2047) + 1;  
                         /*  debug("realign dirofs = 0x%llx\n", dirofs);  */  
                 }  
   
                 res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs,  
                     dirbuf, 256);  
                 if (!res2) {  
                         fatal("Couldn't read the disk image. Aborting.\n");  
                         goto ret;  
                 }  
   
                 dp = dirbuf;  
                 len = dp[0];  
                 if (len < 2)  
                         break;  
   
                 /*  
                  *  TODO: Actually parse the directory entry!  
                  *  
                  *  Haha, this must be rewritten.  
                  */  
                 for (i=32; i<len; i++) {  
                         if (i < len - strlen(filename))  
                                 if (strncasecmp(filename, (char *)dp + i,  
                                     strlen(filename)) == 0) {  
                                         /*  The filename was found somewhere  
                                             in the directory entry.  */  
                                         if (match_entry != NULL) {  
                                                 fatal("TODO: I'm too lazy to"  
                                                     " implement a correct "  
                                                     "directory parser right "  
                                                     "now... (BUG)\n");  
                                                 exit(1);  
                                         }  
                                         match_entry = malloc(512);  
                                         if (match_entry == NULL) {  
                                                 fatal("out of memory\n");  
                                                 exit(1);  
                                         }  
                                         memcpy(match_entry, dp, 512);  
                                         break;  
                                 }  
                 }  
   
                 dirofs += len;  
         }  
   
         if (match_entry == NULL) {  
                 char *blah = filename_orig;  
   
                 fatal("could not find '%s' in /", filename);  
   
                 /*  Print the first part of the filename:  */  
                 while (blah != filename)  
                         fatal("%c", *blah++);  
                   
                 fatal("\n");  
                 goto ret;  
         }  
   
         fileofs = match_entry[2] + (match_entry[3] << 8) +  
             (match_entry[4] << 16) + (match_entry[5] << 24);  
         filelen = match_entry[10] + (match_entry[11] << 8) +  
             (match_entry[12] << 16) + (match_entry[13] << 24);  
         fileofs *= 2048;  
   
         /*  debug("filelen=%llx fileofs=%llx\n", (long long)filelen,  
             (long long)fileofs);  */  
   
         filebuf = malloc(filelen);  
         if (filebuf == NULL) {  
                 fatal("could not allocate %lli bytes to read the file"  
                     " from the disk image!\n", (long long)filelen);  
                 goto ret;  
         }  
   
         tmpfilename = strdup("/tmp/gxemul.XXXXXXXXXXXX");  
   
         debug("extracting %lli bytes into %s\n",  
             (long long)filelen, tmpfilename);  
   
         res2 = diskimage_access(m, disk_id, disk_type, 0, fileofs, filebuf,  
             filelen);  
         if (!res2) {  
                 fatal("could not read the file from the disk image!\n");  
                 goto ret;  
         }  
   
         tmpfile_handle = mkstemp(tmpfilename);  
         if (tmpfile_handle < 0) {  
                 fatal("could not create %s\n", tmpfilename);  
                 exit(1);  
         }  
         write(tmpfile_handle, filebuf, filelen);  
         close(tmpfile_handle);  
166    
167          /*  Add the temporary filename to the load_namesp array:  */          e->path = malloc(15);
168          (*n_loadp)++;          if (e->path == NULL) {
169          new_array = malloc(sizeof(char *) * (*n_loadp));                  fprintf(stderr, "out of memory\n");
         if (new_array == NULL) {  
                 fatal("out of memory\n");  
170                  exit(1);                  exit(1);
171          }          }
172          memcpy(new_array, *load_namesp, sizeof(char *) * (*n_loadp));          snprintf(e->path, 15, "emul[%i]", id);
         *load_namesp = new_array;  
   
         /*  This adds a Backspace char in front of the filename; this  
             is a special hack which causes the file to be removed once  
             it has been loaded.  */  
         tmpfilename = realloc(tmpfilename, strlen(tmpfilename) + 2);  
         memmove(tmpfilename + 1, tmpfilename, strlen(tmpfilename) + 1);  
         tmpfilename[0] = 8;  
   
         (*load_namesp)[*n_loadp - 1] = tmpfilename;  
   
         res = 1;  
   
 ret:  
         if (dirbuf != NULL)  
                 free(dirbuf);  
   
         if (filebuf != NULL)  
                 free(filebuf);  
173    
174          if (match_entry != NULL)          e->settings = settings_new();
                 free(match_entry);  
175    
176          free(filename_orig);          settings_add(e->settings, "n_machines", 0,
177                SETTINGS_TYPE_INT, SETTINGS_FORMAT_DECIMAL,
178                (void *) &e->n_machines);
179    
180          debug_indentation(-iadd);          /*  TODO: More settings?  */
         return res;  
 }  
   
   
 /*  
  *  load_bootblock():  
  *  
  *  For some emulation modes, it is possible to boot from a harddisk image by  
  *  loading a bootblock from a specific disk offset into memory, and executing  
  *  that, instead of requiring a separate kernel file.  It is then up to the  
  *  bootblock to load a kernel.  
  *  
  *  Returns 1 on success, 0 on failure.  
  */  
 static int load_bootblock(struct machine *m, struct cpu *cpu,  
         int *n_loadp, char ***load_namesp)  
 {  
         int boot_disk_id, boot_disk_type = 0, n_blocks, res, readofs,  
             iso_type, retval = 0;  
         unsigned char minibuf[0x20];  
         unsigned char *bootblock_buf;  
         uint64_t bootblock_offset;  
         uint64_t bootblock_loadaddr, bootblock_pc;  
   
         boot_disk_id = diskimage_bootdev(m, &boot_disk_type);  
         if (boot_disk_id < 0)  
                 return 0;  
   
         switch (m->machine_type) {  
         case MACHINE_DEC:  
                 /*  
                  *  The first few bytes of a disk contains information about  
                  *  where the bootblock(s) are located. (These are all 32-bit  
                  *  little-endian words.)  
                  *  
                  *  Offset 0x10 = load address  
                  *         0x14 = initial PC value  
                  *         0x18 = nr of 512-byte blocks to read  
                  *         0x1c = offset on disk to where the bootblocks  
                  *                are (in 512-byte units)  
                  *         0x20 = nr of blocks to read...  
                  *         0x24 = offset...  
                  *  
                  *  nr of blocks to read and offset are repeated until nr of  
                  *  blocks to read is zero.  
                  */  
                 res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0,  
                     minibuf, sizeof(minibuf));  
   
                 bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8)  
                   + (minibuf[0x12] << 16) + (minibuf[0x13] << 24);  
   
                 /*  Convert loadaddr to uncached:  */  
                 if ((bootblock_loadaddr & 0xf0000000ULL) != 0x80000000 &&  
                     (bootblock_loadaddr & 0xf0000000ULL) != 0xa0000000)  
                         fatal("\nWARNING! Weird load address 0x%08x.\n\n",  
                             (int)bootblock_loadaddr);  
                 bootblock_loadaddr &= 0x0fffffffULL;  
                 bootblock_loadaddr |= 0xffffffffa0000000ULL;  
   
                 bootblock_pc = minibuf[0x14] + (minibuf[0x15] << 8)  
                   + (minibuf[0x16] << 16) + (minibuf[0x17] << 24);  
   
                 bootblock_pc &= 0x0fffffffULL;  
                 bootblock_pc |= 0xffffffffa0000000ULL;  
                 cpu->pc = bootblock_pc;  
   
                 debug("DEC boot: loadaddr=0x%08x, pc=0x%08x",  
                     (int)bootblock_loadaddr, (int)bootblock_pc);  
   
                 readofs = 0x18;  
   
                 for (;;) {  
                         res = diskimage_access(m, boot_disk_id, boot_disk_type,  
                             0, readofs, minibuf, sizeof(minibuf));  
                         if (!res) {  
                                 fatal("Couldn't read the disk image. "  
                                     "Aborting.\n");  
                                 return 0;  
                         }  
181    
182                          n_blocks = minibuf[0] + (minibuf[1] << 8)          /*  Sane default values:  */
183                            + (minibuf[2] << 16) + (minibuf[3] << 24);          e->n_machines = 0;
184            e->next_serial_nr = 1;
                         bootblock_offset = (minibuf[4] + (minibuf[5] << 8)  
                           + (minibuf[6] << 16) + (minibuf[7] << 24)) * 512;  
   
                         if (n_blocks < 1)  
                                 break;  
   
                         debug(readofs == 0x18? ": %i" : " + %i", n_blocks);  
   
                         if (n_blocks * 512 > 65536)  
                                 fatal("\nWARNING! Unusually large bootblock "  
                                     "(%i bytes)\n\n", n_blocks * 512);  
   
                         bootblock_buf = malloc(n_blocks * 512);  
                         if (bootblock_buf == NULL) {  
                                 fprintf(stderr, "out of memory in "  
                                     "load_bootblock()\n");  
                                 exit(1);  
                         }  
   
                         res = diskimage_access(m, boot_disk_id, boot_disk_type,  
                             0, bootblock_offset, bootblock_buf, n_blocks * 512);  
                         if (!res) {  
                                 fatal("WARNING: could not load bootblocks from"  
                                     " disk offset 0x%llx\n",  
                                     (long long)bootblock_offset);  
                         }  
   
                         store_buf(cpu, bootblock_loadaddr,  
                             (char *)bootblock_buf, n_blocks * 512);  
   
                         bootblock_loadaddr += 512*n_blocks;  
                         free(bootblock_buf);  
                         readofs += 8;  
                 }  
   
                 debug(readofs == 0x18? ": no blocks?\n" : " blocks\n");  
                 return 1;  
   
         case MACHINE_X86:  
                 /*  TODO: "El Torito" etc?  */  
                 if (diskimage_is_a_cdrom(cpu->machine, boot_disk_id,  
                     boot_disk_type))  
                         break;  
185    
186                  bootblock_buf = malloc(512);          if (name != NULL) {
187                  if (bootblock_buf == NULL) {                  e->name = strdup(name);
188                          fprintf(stderr, "Out of memory.\n");                  if (e->name == NULL) {
189                            fprintf(stderr, "out of memory in emul_new()\n");
190                          exit(1);                          exit(1);
191                  }                  }
192    
193                  debug("loading PC bootsector from %s id %i\n",                  settings_add(e->settings, "name", 0,
194                      diskimage_types[boot_disk_type], boot_disk_id);                      SETTINGS_TYPE_STRING, SETTINGS_FORMAT_STRING,
195                        (void *) &e->name);
                 res = diskimage_access(m, boot_disk_id, boot_disk_type, 0, 0,  
                     bootblock_buf, 512);  
                 if (!res) {  
                         fatal("Couldn't read the disk image. Aborting.\n");  
                         return 0;  
                 }  
   
                 if (bootblock_buf[510] != 0x55 || bootblock_buf[511] != 0xaa)  
                         debug("WARNING! The 0x55,0xAA marker is missing! "  
                             "Booting anyway.\n");  
                 store_buf(cpu, 0x7c00, (char *)bootblock_buf, 512);  
                 free(bootblock_buf);  
   
                 return 1;  
         }  
   
   
         /*  
          *  Try reading a kernel manually from the disk. The code here  
          *  does not rely on machine-dependant boot blocks etc.  
          */  
         /*  ISO9660: (0x800 bytes at 0x8000)  */  
         bootblock_buf = malloc(0x800);  
         if (bootblock_buf == NULL) {  
                 fprintf(stderr, "Out of memory.\n");  
                 exit(1);  
196          }          }
197    
198          res = diskimage_access(m, boot_disk_id, boot_disk_type,          return e;
             0, 0x8000, bootblock_buf, 0x800);  
         if (!res) {  
                 fatal("Couldn't read the disk image. Aborting.\n");  
                 return 0;  
         }  
   
         iso_type = 0;  
         if (strncmp((char *)bootblock_buf+1, "CD001", 5) == 0)  
                 iso_type = 1;  
         if (strncmp((char *)bootblock_buf+1, "CDW01", 5) == 0)  
                 iso_type = 2;  
         if (strncmp((char *)bootblock_buf+1, "CDROM", 5) == 0)  
                 iso_type = 3;  
   
         if (iso_type != 0) {  
                 /*  We can't load a kernel if the name  
                     isn't specified.  */  
                 if (cpu->machine->boot_kernel_filename == NULL ||  
                     cpu->machine->boot_kernel_filename[0] == '\0')  
                         fatal("\nISO9660 filesystem, but no kernel "  
                             "specified? (Use the -j option.)\n");  
                 else  
                         retval = iso_load_bootblock(m, cpu, boot_disk_id,  
                             boot_disk_type, iso_type, bootblock_buf,  
                             n_loadp, load_namesp);  
         }  
   
         free(bootblock_buf);  
         return retval;  
199  }  }
200    
201    
202  /*  /*
203   *  emul_new():   *  emul_destroy():
204   *   *
205   *  Returns a reasonably initialized struct emul.   *  Destroys a previously created emul object.
206   */   */
207  struct emul *emul_new(char *name)  void emul_destroy(struct emul *emul)
208  {  {
209          struct emul *e;          int i;
210          e = malloc(sizeof(struct emul));  
211          if (e == NULL) {          if (emul->name != NULL) {
212                  fprintf(stderr, "out of memory in emul_new()\n");                  settings_remove(emul->settings, "name");
213                  exit(1);                  free(emul->name);
214          }          }
215    
216          memset(e, 0, sizeof(struct emul));          for (i=0; i<emul->n_machines; i++)
217                    machine_destroy(emul->machines[i]);
218    
219          /*  Sane default values:  */          if (emul->machines != NULL)
220          e->n_machines = 0;                  free(emul->machines);
         e->next_serial_nr = 1;  
221    
222          if (name != NULL) {          /*  Remove any remaining level-1 settings:  */
223                  e->name = strdup(name);          settings_remove_all(emul->settings);
224                  if (e->name == NULL) {          settings_destroy(emul->settings);
                         fprintf(stderr, "out of memory in emul_new()\n");  
                         exit(1);  
                 }  
         }  
225    
226          return e;          free(emul);
227  }  }
228    
229    
# Line 671  struct emul *emul_new(char *name) Line 238  struct emul *emul_new(char *name)
238  struct machine *emul_add_machine(struct emul *e, char *name)  struct machine *emul_add_machine(struct emul *e, char *name)
239  {  {
240          struct machine *m;          struct machine *m;
241            char tmpstr[20];
242            int i;
243    
244          m = machine_new(name, e);          m = machine_new(name, e, e->n_machines);
245          m->serial_nr = (e->next_serial_nr ++);          m->serial_nr = (e->next_serial_nr ++);
246    
247            i = e->n_machines;
248    
249          e->n_machines ++;          e->n_machines ++;
250          e->machines = realloc(e->machines,          e->machines = realloc(e->machines,
251              sizeof(struct machine *) * e->n_machines);              sizeof(struct machine *) * e->n_machines);
# Line 683  struct machine *emul_add_machine(struct Line 254  struct machine *emul_add_machine(struct
254                  exit(1);                  exit(1);
255          }          }
256    
257          e->machines[e->n_machines - 1] = m;          e->machines[i] = m;
258    
259            snprintf(tmpstr, sizeof(tmpstr), "machine[%i]", i);
260            settings_add(e->settings, tmpstr, 1, SETTINGS_TYPE_SUBSETTINGS, 0,
261                e->machines[i]->settings);
262    
263          return m;          return m;
264  }  }
265    
# Line 709  static void add_arc_components(struct ma Line 285  static void add_arc_components(struct ma
285    
286          len += 1048576 * m->memory_offset_in_mb;          len += 1048576 * m->memory_offset_in_mb;
287    
288          /*  NOTE/TODO: magic 12MB end of load program area  */          /*
289             *  NOTE/TODO: magic 12MB end of load program area
290             *
291             *  Hm. This breaks the old FreeBSD/MIPS snapshots...
292             */
293    #if 0
294          arcbios_add_memory_descriptor(cpu,          arcbios_add_memory_descriptor(cpu,
295              0x60000 + m->memory_offset_in_mb * 1048576,              0x60000 + m->memory_offset_in_mb * 1048576,
296              start-0x60000 - m->memory_offset_in_mb * 1048576,              start-0x60000 - m->memory_offset_in_mb * 1048576,
297              ARCBIOS_MEM_FreeMemory);              ARCBIOS_MEM_FreeMemory);
298    #endif
299          arcbios_add_memory_descriptor(cpu,          arcbios_add_memory_descriptor(cpu,
300              start, len, ARCBIOS_MEM_LoadedProgram);              start, len, ARCBIOS_MEM_LoadedProgram);
301    
# Line 806  static void add_arc_components(struct ma Line 388  static void add_arc_components(struct ma
388  void emul_machine_setup(struct machine *m, int n_load, char **load_names,  void emul_machine_setup(struct machine *m, int n_load, char **load_names,
389          int n_devices, char **device_names)          int n_devices, char **device_names)
390  {  {
         struct emul *emul;  
391          struct cpu *cpu;          struct cpu *cpu;
392          int i, iadd=4;          int i, iadd = DEBUG_INDENTATION;
393          uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0;          uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0;
394          int byte_order;          int byte_order;
395    
         emul = m->emul;  
   
396          debug("machine \"%s\":\n", m->name);          debug("machine \"%s\":\n", m->name);
397          debug_indentation(iadd);          debug_indentation(iadd);
398    
# Line 834  void emul_machine_setup(struct machine * Line 413  void emul_machine_setup(struct machine *
413    
414          m->cpu_family = cpu_family_ptr_by_number(m->arch);          m->cpu_family = cpu_family_ptr_by_number(m->arch);
415    
416          if (m->arch != ARCH_MIPS)          if (m->arch == ARCH_ALPHA)
417                  m->bintrans_enable = 0;                  m->arch_pagesize = 8192;
418    
419          machine_memsize_fix(m);          machine_memsize_fix(m);
420    
# Line 857  void emul_machine_setup(struct machine * Line 436  void emul_machine_setup(struct machine *
436                  debug(" (offset by %iMB)", m->memory_offset_in_mb);                  debug(" (offset by %iMB)", m->memory_offset_in_mb);
437                  memory_amount += 1048576 * m->memory_offset_in_mb;                  memory_amount += 1048576 * m->memory_offset_in_mb;
438          }          }
439          m->memory = memory_new(memory_amount);          m->memory = memory_new(memory_amount, m->arch);
440          if (m->machine_type != MACHINE_USERLAND)          if (m->machine_type != MACHINE_USERLAND)
441                  debug("\n");                  debug("\n");
442    
# Line 884  void emul_machine_setup(struct machine * Line 463  void emul_machine_setup(struct machine *
463          }          }
464          memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus);          memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus);
465    
         /*  Initialize dynamic binary translation, if available:  */  
         if (m->bintrans_enable)  
                 bintrans_init(m, m->memory);  
   
466          debug("cpu0");          debug("cpu0");
467          if (m->ncpus > 1)          if (m->ncpus > 1)
468                  debug(" .. cpu%i", m->ncpus - 1);                  debug(" .. cpu%i", m->ncpus - 1);
469          debug(": ");          debug(": ");
470          for (i=0; i<m->ncpus; i++) {          for (i=0; i<m->ncpus; i++) {
471                  m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name);                  m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name);
472                  if (m->bintrans_enable)                  if (m->cpus[i] == NULL) {
473                          bintrans_init_cpu(m->cpus[i]);                          fprintf(stderr, "Unable to create CPU object. "
474                                "Aborting.");
475                            exit(1);
476                    }
477          }          }
478          debug("\n");          debug("\n");
479    
# Line 906  void emul_machine_setup(struct machine * Line 484  void emul_machine_setup(struct machine *
484                  m->cpus[m->ncpus] = cpu_new(m->memory, m,                  m->cpus[m->ncpus] = cpu_new(m->memory, m,
485                      0  /*  use 0 here to show info with debug()  */,                      0  /*  use 0 here to show info with debug()  */,
486                      "Allegrex" /*  TODO  */);                      "Allegrex" /*  TODO  */);
                 if (m->bintrans_enable)  
                         bintrans_init_cpu(m->cpus[m->ncpus]);  
487                  debug("\n");                  debug("\n");
488                  m->ncpus ++;                  m->ncpus ++;
489          }          }
# Line 924  void emul_machine_setup(struct machine * Line 500  void emul_machine_setup(struct machine *
500          if (m->userland_emul != NULL) {          if (m->userland_emul != NULL) {
501                  useremul_name_to_useremul(cpu,                  useremul_name_to_useremul(cpu,
502                      m->userland_emul, NULL, NULL, NULL);                      m->userland_emul, NULL, NULL, NULL);
503                  cpu->memory_rw = userland_memory_rw;  
504                    switch (m->arch) {
505    #ifdef ENABLE_ALPHA
506                    case ARCH_ALPHA:
507                            cpu->memory_rw = alpha_userland_memory_rw;
508                            break;
509    #endif
510                    default:cpu->memory_rw = userland_memory_rw;
511                    }
512          }          }
513    
514          if (m->use_x11)          if (m->use_x11)
# Line 955  void emul_machine_setup(struct machine * Line 539  void emul_machine_setup(struct machine *
539          }          }
540    
541          diskimage_dump_info(m);          diskimage_dump_info(m);
542            console_debug_dump(m);
543    
544          /*  Load files (ROM code, boot code, ...) into memory:  */          /*  Load files (ROM code, boot code, ...) into memory:  */
545          if (n_load == 0) {          if (n_load == 0) {
# Line 1070  void emul_machine_setup(struct machine * Line 655  void emul_machine_setup(struct machine *
655                          }                          }
656                  }                  }
657    
                 /*  Special things required _before_ loading the file:  */  
                 switch (m->arch) {  
                 case ARCH_X86:  
                         /*  
                          *  X86 machines normally don't need to load any files,  
                          *  they can boot from disk directly. Therefore, an x86  
                          *  machine usually boots up in 16-bit real mode. When  
                          *  loading a 32-bit (or even 64-bit) ELF, that's not  
                          *  very nice, hence this special case.  
                          */  
                         pc_bios_simple_pmode_setup(cpu);  
                         break;  
                 }  
   
658                  byte_order = NO_BYTE_ORDER_OVERRIDE;                  byte_order = NO_BYTE_ORDER_OVERRIDE;
659    
660                  /*                  /*
# Line 1103  void emul_machine_setup(struct machine * Line 674  void emul_machine_setup(struct machine *
674                  cpu->pc = entrypoint;                  cpu->pc = entrypoint;
675    
676                  switch (m->arch) {                  switch (m->arch) {
677    
678                    case ARCH_ALPHA:
679                            /*  For position-independent code:  */
680                            cpu->cd.alpha.r[ALPHA_T12] = cpu->pc;
681                            break;
682    
683                    case ARCH_ARM:
684                            if (cpu->pc & 3) {
685                                    fatal("ARM: lowest bits of pc set: TODO\n");
686                                    exit(1);
687                            }
688                            cpu->pc &= 0xfffffffc;
689                            break;
690    
691                    case ARCH_AVR:
692                            cpu->pc &= 0xfffff;
693                            if (cpu->pc & 1) {
694                                    fatal("AVR: lowest bit of pc set: TODO\n");
695                                    exit(1);
696                            }
697                            break;
698    
699                    case ARCH_RCA180X:
700                            cpu->pc &= 0xffff;
701                            break;
702    
703                    case ARCH_M68K:
704                            break;
705    
706                  case ARCH_MIPS:                  case ARCH_MIPS:
707                          if ((cpu->pc >> 32) == 0                          if ((cpu->pc >> 32) == 0 && (cpu->pc & 0x80000000ULL))
                             && (cpu->pc & 0x80000000ULL))  
708                                  cpu->pc |= 0xffffffff00000000ULL;                                  cpu->pc |= 0xffffffff00000000ULL;
709    
710                          cpu->cd.mips.gpr[MIPS_GPR_GP] = gp;                          cpu->cd.mips.gpr[MIPS_GPR_GP] = gp;
# Line 1121  void emul_machine_setup(struct machine * Line 720  void emul_machine_setup(struct machine *
720                              spec/x458.html for more info.  */                              spec/x458.html for more info.  */
721                          cpu->cd.ppc.gpr[2] = toc;                          cpu->cd.ppc.gpr[2] = toc;
722                          /*  TODO  */                          /*  TODO  */
723                            if (cpu->cd.ppc.bits == 32)
724                                    cpu->pc &= 0xffffffffULL;
725                          break;                          break;
726    
727                  case ARCH_ALPHA:                  case ARCH_SH:
728                  case ARCH_HPPA:                          if (cpu->cd.sh.cpu_type.bits == 32)
729                  case ARCH_SPARC:                                  cpu->pc &= 0xffffffffULL;
730                  case ARCH_URISC:                          cpu->pc &= ~1;
731                          break;                          break;
732    
733                  case ARCH_ARM:                  case ARCH_SPARC:
                         cpu->pc &= 0xfffffffc;  
                         cpu->cd.arm.r[ARM_PC] = cpu->pc;  
734                          break;                          break;
735    
736                  case ARCH_X86:                  case ARCH_TRANSPUTER:
737                          /*                          cpu->pc &= 0xffffffffULL;
                          *  NOTE: The toc field is used to indicate an ELF32  
                          *  or ELF64 load.  
                          */  
                         switch (toc) {  
                         case 0: /*  16-bit? TODO  */  
                                 cpu->pc &= 0xffffffffULL;  
                                 break;  
                         case 1: /*  32-bit.  */  
                                 cpu->pc &= 0xffffffffULL;  
                                 break;  
                         case 2: /*  64-bit:  TODO  */  
                                 fatal("64-bit x86 load. TODO\n");  
                                 exit(1);  
                         }  
738                          break;                          break;
739    
740                  default:                  default:
# Line 1187  void emul_machine_setup(struct machine * Line 772  void emul_machine_setup(struct machine *
772                  useremul_setup(cpu, n_load, load_names);                  useremul_setup(cpu, n_load, load_names);
773    
774          /*  Startup the bootstrap CPU:  */          /*  Startup the bootstrap CPU:  */
775          cpu->bootstrap_cpu_flag = 1;          cpu->running = 1;
         cpu->running            = 1;  
776    
777          /*  ... or pause all CPUs, if start_paused is set:  */          /*  ... or pause all CPUs, if start_paused is set:  */
778          if (m->start_paused) {          if (m->start_paused) {
# Line 1200  void emul_machine_setup(struct machine * Line 784  void emul_machine_setup(struct machine *
784          add_dump_points(m);          add_dump_points(m);
785    
786          /*  TODO: This is MIPS-specific!  */          /*  TODO: This is MIPS-specific!  */
787          if (m->machine_type == MACHINE_DEC &&          if (m->machine_type == MACHINE_PMAX &&
788              cpu->cd.mips.cpu_type.mmu_model == MMU3K)              cpu->cd.mips.cpu_type.mmu_model == MMU3K)
789                  add_symbol_name(&m->symbol_context,                  add_symbol_name(&m->symbol_context,
790                      0x9fff0000, 0x10000, "r2k3k_cache", 0);                      0x9fff0000, 0x10000, "r2k3k_cache", 0, 0);
791    
792          symbol_recalc_sizes(&m->symbol_context);          symbol_recalc_sizes(&m->symbol_context);
793    
         if (m->max_random_cycles_per_chunk > 0)  
                 debug("using random cycle chunks (1 to %i cycles)\n",  
                     m->max_random_cycles_per_chunk);  
   
794          /*  Special hack for ARC/SGI emulation:  */          /*  Special hack for ARC/SGI emulation:  */
795          if ((m->machine_type == MACHINE_ARC ||          if ((m->machine_type == MACHINE_ARC ||
796              m->machine_type == MACHINE_SGI) && m->prom_emulation)              m->machine_type == MACHINE_SGI) && m->prom_emulation)
# Line 1218  void emul_machine_setup(struct machine * Line 798  void emul_machine_setup(struct machine *
798    
799          debug("starting cpu%i at ", m->bootstrap_cpu);          debug("starting cpu%i at ", m->bootstrap_cpu);
800          switch (m->arch) {          switch (m->arch) {
801    
802            case ARCH_ARM:
803                    /*  ARM cpus aren't 64-bit:  */
804                    debug("0x%08x", (int)entrypoint);
805                    break;
806    
807            case ARCH_AVR:
808                    /*  Atmel AVR uses a 16-bit or 22-bit program counter:  */
809                    debug("0x%04x", (int)entrypoint);
810                    break;
811    
812          case ARCH_MIPS:          case ARCH_MIPS:
813                  if (cpu->cd.mips.cpu_type.isa_level < 3 ||                  if (cpu->is_32bit) {
                     cpu->cd.mips.cpu_type.isa_level == 32) {  
814                          debug("0x%08x", (int)m->cpus[                          debug("0x%08x", (int)m->cpus[
815                              m->bootstrap_cpu]->pc);                              m->bootstrap_cpu]->pc);
816                          if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0)                          if (cpu->cd.mips.gpr[MIPS_GPR_GP] != 0)
# Line 1235  void emul_machine_setup(struct machine * Line 825  void emul_machine_setup(struct machine *
825                                      cpu->cd.mips.gpr[MIPS_GPR_GP]);                                      cpu->cd.mips.gpr[MIPS_GPR_GP]);
826                  }                  }
827                  break;                  break;
828    
829          case ARCH_PPC:          case ARCH_PPC:
830                  if (cpu->cd.ppc.bits == 32)                  if (cpu->cd.ppc.bits == 32)
831                          debug("0x%08x", (int)entrypoint);                          debug("0x%08x", (int)entrypoint);
832                  else                  else
833                          debug("0x%016llx", (long long)entrypoint);                          debug("0x%016llx", (long long)entrypoint);
834                  break;                  break;
         case ARCH_ARM:  
                 /*  ARM cpus aren't 64-bit:  */  
                 debug("0x%08x", (int)entrypoint);  
                 break;  
         case ARCH_URISC:  
                 {  
                         char tmps[100];  
                         unsigned char buf[sizeof(uint64_t)];  
   
                         cpu->memory_rw(cpu, m->memory, 0, buf, sizeof(buf),  
                             MEM_READ, CACHE_NONE | NO_EXCEPTIONS);  
   
                         entrypoint = 0;  
                         for (i=0; i<cpu->cd.urisc.wordlen/8; i++) {  
                                 entrypoint <<= 8;  
                                 if (cpu->byte_order == EMUL_BIG_ENDIAN)  
                                         entrypoint += buf[i];  
                                 else  
                                         entrypoint += buf[cpu->  
                                             cd.urisc.wordlen/8 - 1 - i];  
                         }  
835    
                         snprintf(tmps, sizeof(tmps), "0x%%0%illx",  
                             cpu->cd.urisc.wordlen / 4);  
                         debug(tmps, (long long)entrypoint);  
                         cpu->pc = entrypoint;  
                 }  
                 break;  
         case ARCH_X86:  
                 debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS],  
                     (long long)cpu->pc);  
                 break;  
836          default:          default:
837                  debug("0x%016llx", (long long)cpu->pc);                  if (cpu->is_32bit)
838                            debug("0x%08x", (int)cpu->pc);
839                    else
840                            debug("0x%016llx", (long long)cpu->pc);
841          }          }
842          debug("\n");          debug("\n");
843    
# Line 1289  void emul_machine_setup(struct machine * Line 852  void emul_machine_setup(struct machine *
852   */   */
853  void emul_dumpinfo(struct emul *e)  void emul_dumpinfo(struct emul *e)
854  {  {
855          int j, nm, iadd = 4;          int j, nm, iadd = DEBUG_INDENTATION;
856    
857          if (e->net != NULL)          if (e->net != NULL)
858                  net_dumpinfo(e->net);                  net_dumpinfo(e->net);
# Line 1318  void emul_dumpinfo(struct emul *e) Line 881  void emul_dumpinfo(struct emul *e)
881   */   */
882  void emul_simple_init(struct emul *emul)  void emul_simple_init(struct emul *emul)
883  {  {
884          int iadd=4;          int iadd = DEBUG_INDENTATION;
885          struct machine *m;          struct machine *m;
886    
887          if (emul->n_machines != 1) {          if (emul->n_machines != 1) {
# Line 1334  void emul_simple_init(struct emul *emul) Line 897  void emul_simple_init(struct emul *emul)
897    
898                  /*  Create a simple network:  */                  /*  Create a simple network:  */
899                  emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY,                  emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY,
900                      "10.0.0.0", 8, NULL, 0, 0);                      NET_DEFAULT_IPV4_MASK,
901                        NET_DEFAULT_IPV4_LEN,
902                        NULL, 0, 0, NULL);
903          } else {          } else {
904                  /*  Userland pseudo-machine:  */                  /*  Userland pseudo-machine:  */
905                  debug("Syscall emulation (userland-only) setup...\n");                  debug("Syscall emulation (userland-only) setup...\n");
# Line 1353  void emul_simple_init(struct emul *emul) Line 918  void emul_simple_init(struct emul *emul)
918   *   *
919   *  Create an emul struct by reading settings from a configuration file.   *  Create an emul struct by reading settings from a configuration file.
920   */   */
921  struct emul *emul_create_from_configfile(char *fname)  struct emul *emul_create_from_configfile(char *fname, int id)
922  {  {
923          int iadd = 4;          int iadd = DEBUG_INDENTATION;
924          struct emul *e = emul_new(fname);          struct emul *e = emul_new(fname, id);
         FILE *f;  
         char buf[128];  
         size_t len;  
925    
926          debug("Creating emulation from configfile \"%s\":\n", fname);          debug("Creating emulation from configfile \"%s\":\n", fname);
927          debug_indentation(iadd);          debug_indentation(iadd);
928    
929          f = fopen(fname, "r");          emul_parse_config(e, fname);
         if (f == NULL) {  
                 perror(fname);  
                 exit(1);  
         }  
   
         /*  Read header: (must be !!gxemul)  */  
         len = fread(buf, 1, 8, f);  
         if (len != 8 || strncmp(buf, "!!gxemul", 8) != 0) {  
                 fprintf(stderr, "%s: must start with '!!gxemul'\n", fname);  
                 exit(1);  
         }  
   
         /*  Restart from beginning:  */  
         rewind(f);  
930    
         emul_parse_config(e, f);  
   
         fclose(f);  
931          debug_indentation(-iadd);          debug_indentation(-iadd);
932          return e;          return e;
933  }  }
# Line 1409  void emul_run(struct emul **emuls, int n Line 954  void emul_run(struct emul **emuls, int n
954    
955          atexit(fix_console);          atexit(fix_console);
956    
         i = 79;  
         while (i-- > 0)  
                 debug("-");  
         debug("\n\n");  
   
957          /*  Initialize the interactive debugger:  */          /*  Initialize the interactive debugger:  */
958          debugger_init(emuls, n_emuls);          debugger_init(emuls, n_emuls);
959    
960            /*  Run any additional debugger commands before starting:  */
961            for (i=0; i<n_emuls; i++) {
962                    struct emul *emul = emuls[i];
963                    if (emul->n_debugger_cmds > 0) {
964                            int j;
965                            if (i == 0)
966                                    print_separator();
967                            for (j = 0; j < emul->n_debugger_cmds; j ++) {
968                                    debug("> %s\n", emul->debugger_cmds[j]);
969                                    debugger_execute_cmd(emul->debugger_cmds[j],
970                                        strlen(emul->debugger_cmds[j]));
971                            }
972                    }
973            }
974    
975            print_separator();
976            debug("\n");
977    
978    
979          /*          /*
980           *  console_init_main() makes sure that the terminal is in a           *  console_init_main() makes sure that the terminal is in a
981           *  reasonable state.           *  reasonable state.
# Line 1441  void emul_run(struct emul **emuls, int n Line 1000  void emul_run(struct emul **emuls, int n
1000                  if (e == NULL)                  if (e == NULL)
1001                          continue;                          continue;
1002                  for (j=0; j<e->n_machines; j++)                  for (j=0; j<e->n_machines; j++)
1003                          cpu_run_init(e, e->machines[j]);                          cpu_run_init(e->machines[j]);
1004          }          }
1005    
1006            /*  TODO: Generalize:  */
1007            if (emuls[0]->machines[0]->show_trace_tree)
1008                    cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0],
1009                        emuls[0]->machines[0]->cpus[0]->pc);
1010    
1011            /*  Start emulated clocks:  */
1012            timer_start();
1013    
1014          /*          /*
1015           *  MAIN LOOP:           *  MAIN LOOP:
1016           *           *
# Line 1453  void emul_run(struct emul **emuls, int n Line 1020  void emul_run(struct emul **emuls, int n
1020          while (go) {          while (go) {
1021                  go = 0;                  go = 0;
1022    
1023                  x11_check_event(emuls, n_emuls);                  /*  Flush X11 and serial console output every now and then:  */
1024                    if (emuls[0]->machines[0]->ninstrs >
1025                        emuls[0]->machines[0]->ninstrs_flush + (1<<19)) {
1026                            x11_check_event(emuls, n_emuls);
1027                            console_flush();
1028                            emuls[0]->machines[0]->ninstrs_flush =
1029                                emuls[0]->machines[0]->ninstrs;
1030                    }
1031    
1032                    if (emuls[0]->machines[0]->ninstrs >
1033                        emuls[0]->machines[0]->ninstrs_show + (1<<25)) {
1034                            emuls[0]->machines[0]->ninstrs_since_gettimeofday +=
1035                                (emuls[0]->machines[0]->ninstrs -
1036                                 emuls[0]->machines[0]->ninstrs_show);
1037                            cpu_show_cycles(emuls[0]->machines[0], 0);
1038                            emuls[0]->machines[0]->ninstrs_show =
1039                                emuls[0]->machines[0]->ninstrs;
1040                    }
1041    
1042                    if (single_step == ENTER_SINGLE_STEPPING) {
1043                            /*  TODO: Cleanup!  */
1044                            old_instruction_trace =
1045                                emuls[0]->machines[0]->instruction_trace;
1046                            old_quiet_mode = quiet_mode;
1047                            old_show_trace_tree =
1048                                emuls[0]->machines[0]->show_trace_tree;
1049                            emuls[0]->machines[0]->instruction_trace = 1;
1050                            emuls[0]->machines[0]->show_trace_tree = 1;
1051                            quiet_mode = 0;
1052                            single_step = SINGLE_STEPPING;
1053                    }
1054    
1055                    if (single_step == SINGLE_STEPPING)
1056                            debugger();
1057    
1058                  for (i=0; i<n_emuls; i++) {                  for (i=0; i<n_emuls; i++) {
1059                          e = emuls[i];                          e = emuls[i];
                         if (e == NULL)  
                                 continue;  
1060    
1061                          for (j=0; j<e->n_machines; j++) {                          for (j=0; j<e->n_machines; j++) {
1062                                  /*  TODO: cpu_run() is a strange name, since                                  if (e->machines[j]->gdb.port > 0)
1063                                      there can be multiple cpus in a machine  */                                          debugger_gdb_check_incoming(
1064                                  anything = cpu_run(e, e->machines[j]);                                              e->machines[j]);
1065    
1066                                    anything = machine_run(e->machines[j]);
1067                                  if (anything)                                  if (anything)
1068                                          go = 1;                                          go = 1;
1069                          }                          }
1070                  }                  }
1071          }          }
1072    
1073            /*  Stop any running timers:  */
1074            timer_stop();
1075    
1076          /*  Deinitialize all CPUs in all machines in all emulations:  */          /*  Deinitialize all CPUs in all machines in all emulations:  */
1077          for (i=0; i<n_emuls; i++) {          for (i=0; i<n_emuls; i++) {
1078                  e = emuls[i];                  e = emuls[i];
1079                  if (e == NULL)                  if (e == NULL)
1080                          continue;                          continue;
1081                  for (j=0; j<e->n_machines; j++)                  for (j=0; j<e->n_machines; j++)
1082                          cpu_run_deinit(e, e->machines[j]);                          cpu_run_deinit(e->machines[j]);
1083          }          }
1084    
1085          /*  force_debugger_at_exit flag set? Then enter the debugger:  */          /*  force_debugger_at_exit flag set? Then enter the debugger:  */
# Line 1486  void emul_run(struct emul **emuls, int n Line 1089  void emul_run(struct emul **emuls, int n
1089                  debugger();                  debugger();
1090          }          }
1091    
1092          /*  Any machine using X11? Then we should wait before exiting:  */          /*  Any machine using X11? Then wait before exiting:  */
1093          n = 0;          n = 0;
1094          for (i=0; i<n_emuls; i++)          for (i=0; i<n_emuls; i++)
1095                  for (j=0; j<emuls[i]->n_machines; j++)                  for (j=0; j<emuls[i]->n_machines; j++)
# Line 1496  void emul_run(struct emul **emuls, int n Line 1099  void emul_run(struct emul **emuls, int n
1099                  printf("Press enter to quit.\n");                  printf("Press enter to quit.\n");
1100                  while (!console_charavail(MAIN_CONSOLE)) {                  while (!console_charavail(MAIN_CONSOLE)) {
1101                          x11_check_event(emuls, n_emuls);                          x11_check_event(emuls, n_emuls);
1102                          usleep(1);                          usleep(10000);
1103                  }                  }
1104                  console_readchar(MAIN_CONSOLE);                  console_readchar(MAIN_CONSOLE);
1105          }          }
1106    
1107          console_deinit();          console_deinit_main();
1108  }  }
1109    

Legend:
Removed from v.10  
changed lines
  Added in v.36

  ViewVC Help
Powered by ViewVC 1.1.26