/[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 24 by dpavlin, Mon Oct 8 16:19:56 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *  Copyright (C) 2003-2005  Anders Gavare.  All rights reserved.   *  Copyright (C) 2003-2006  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.254 2006/06/22 13:22:40 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 70  extern struct diskimage *diskimages[]; Line 69  extern struct diskimage *diskimages[];
69  static char *diskimage_types[] = DISKIMAGE_TYPES;  static char *diskimage_types[] = DISKIMAGE_TYPES;
70    
71    
72    static void print_separator(void)
73    {
74            int i = 79;
75            while (i-- > 0)
76                    debug("-");
77            debug("\n");
78    }
79    
80    
81  /*  /*
82   *  add_dump_points():   *  add_dump_points():
83   *   *
# Line 96  static void add_dump_points(struct machi Line 104  static void add_dump_points(struct machi
104                          uint64_t addr;                          uint64_t addr;
105                          int res = get_symbol_addr(&m->symbol_context,                          int res = get_symbol_addr(&m->symbol_context,
106                              m->breakpoint_string[i], &addr);                              m->breakpoint_string[i], &addr);
107                          if (!res)                          if (!res) {
108                                  fprintf(stderr,                                  fprintf(stderr,
109                                      "WARNING! Breakpoint '%s' could not be"                                      "ERROR! Breakpoint '%s' could not be"
110                                          " parsed\n",                                          " parsed\n",
111                                      m->breakpoint_string[i]);                                      m->breakpoint_string[i]);
112                          else {                          } else {
113                                  dp = addr;                                  dp = addr;
114                                  string_flag = 1;                                  string_flag = 1;
115                          }                          }
# Line 112  static void add_dump_points(struct machi Line 120  static void add_dump_points(struct machi
120                   *  were automatically converted into the correct address.                   *  were automatically converted into the correct address.
121                   */                   */
122    
123                  if ((dp >> 32) == 0 && ((dp >> 31) & 1))                  if (m->arch == ARCH_MIPS) {
124                          dp |= 0xffffffff00000000ULL;                          if ((dp >> 32) == 0 && ((dp >> 31) & 1))
125                                    dp |= 0xffffffff00000000ULL;
126                    }
127    
128                  m->breakpoint_addr[i] = dp;                  m->breakpoint_addr[i] = dp;
129    
130                  debug("breakpoint %i: 0x%016llx", i, (long long)dp);                  debug("breakpoint %i: 0x%llx", i, (long long)dp);
131                  if (string_flag)                  if (string_flag)
132                          debug(" (%s)", m->breakpoint_string[i]);                          debug(" (%s)", m->breakpoint_string[i]);
133                  debug("\n");                  debug("\n");
# Line 149  static int iso_load_bootblock(struct mac Line 160  static int iso_load_bootblock(struct mac
160          int *n_loadp, char ***load_namesp)          int *n_loadp, char ***load_namesp)
161  {  {
162          char str[35];          char str[35];
163          int filenr, i, ofs, dirlen, res = 0, res2, iadd = 4;          int filenr, i, ofs, dirlen, res = 0, res2, iadd = DEBUG_INDENTATION;
164          int found_dir;          int found_dir;
165          uint64_t dirofs;          uint64_t dirofs;
166          uint64_t fileofs, filelen;          uint64_t fileofs, filelen;
# Line 158  static int iso_load_bootblock(struct mac Line 169  static int iso_load_bootblock(struct mac
169          char *p, *filename_orig;          char *p, *filename_orig;
170          char *filename = strdup(cpu->machine->boot_kernel_filename);          char *filename = strdup(cpu->machine->boot_kernel_filename);
171          unsigned char *filebuf = NULL;          unsigned char *filebuf = NULL;
172          char *tmpfilename = NULL;          char *tmpfname = NULL;
173          char **new_array;          char **new_array;
174          int tmpfile_handle;          int tmpfile_handle;
175    
# Line 204  static int iso_load_bootblock(struct mac Line 215  static int iso_load_bootblock(struct mac
215                  fatal("WARNING: Root directory length mismatch?\n");                  fatal("WARNING: Root directory length mismatch?\n");
216    
217          dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) +          dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) +
218              (buf[0x8f] << 24)) * 2048;              ((uint64_t)buf[0x8f] << 24)) * 2048;
219    
220          /*  debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs);  */          /*  debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs);  */
221    
# Line 225  static int iso_load_bootblock(struct mac Line 236  static int iso_load_bootblock(struct mac
236          dp = dirbuf; filenr = 1;          dp = dirbuf; filenr = 1;
237          p = NULL;          p = NULL;
238          while (dp < dirbuf + dirlen) {          while (dp < dirbuf + dirlen) {
239                  int i, nlen = dp[0];                  size_t i, nlen = dp[0];
240                  int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) + (dp[5] << 24);                  int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) +
241                        ((uint64_t)dp[5] << 24);
242                  int y = dp[6] + (dp[7] << 8);                  int y = dp[6] + (dp[7] << 8);
243                  char direntry[65];                  char direntry[65];
244    
# Line 296  static int iso_load_bootblock(struct mac Line 308  static int iso_load_bootblock(struct mac
308          }          }
309    
310          for (;;) {          for (;;) {
311                  int len, i;                  size_t len, i;
312    
313                  /*  Too close to another sector? Then realign.  */                  /*  Too close to another sector? Then realign.  */
314                  if ((dirofs & 2047) + 70 > 2047) {                  if ((dirofs & 2047) + 70 > 2047) {
# Line 361  static int iso_load_bootblock(struct mac Line 373  static int iso_load_bootblock(struct mac
373          }          }
374    
375          fileofs = match_entry[2] + (match_entry[3] << 8) +          fileofs = match_entry[2] + (match_entry[3] << 8) +
376              (match_entry[4] << 16) + (match_entry[5] << 24);              (match_entry[4] << 16) + ((uint64_t)match_entry[5] << 24);
377          filelen = match_entry[10] + (match_entry[11] << 8) +          filelen = match_entry[10] + (match_entry[11] << 8) +
378              (match_entry[12] << 16) + (match_entry[13] << 24);              (match_entry[12] << 16) + ((uint64_t)match_entry[13] << 24);
379          fileofs *= 2048;          fileofs *= 2048;
380    
381          /*  debug("filelen=%llx fileofs=%llx\n", (long long)filelen,          /*  debug("filelen=%llx fileofs=%llx\n", (long long)filelen,
# Line 376  static int iso_load_bootblock(struct mac Line 388  static int iso_load_bootblock(struct mac
388                  goto ret;                  goto ret;
389          }          }
390    
391          tmpfilename = strdup("/tmp/gxemul.XXXXXXXXXXXX");          tmpfname = strdup("/tmp/gxemul.XXXXXXXXXXXX");
   
         debug("extracting %lli bytes into %s\n",  
             (long long)filelen, tmpfilename);  
392    
393          res2 = diskimage_access(m, disk_id, disk_type, 0, fileofs, filebuf,          res2 = diskimage_access(m, disk_id, disk_type, 0, fileofs, filebuf,
394              filelen);              filelen);
# Line 388  static int iso_load_bootblock(struct mac Line 397  static int iso_load_bootblock(struct mac
397                  goto ret;                  goto ret;
398          }          }
399    
400          tmpfile_handle = mkstemp(tmpfilename);          tmpfile_handle = mkstemp(tmpfname);
401          if (tmpfile_handle < 0) {          if (tmpfile_handle < 0) {
402                  fatal("could not create %s\n", tmpfilename);                  fatal("could not create %s\n", tmpfname);
403                  exit(1);                  exit(1);
404          }          }
405          write(tmpfile_handle, filebuf, filelen);          write(tmpfile_handle, filebuf, filelen);
406          close(tmpfile_handle);          close(tmpfile_handle);
407    
408            debug("extracted %lli bytes into %s\n", (long long)filelen, tmpfname);
409    
410          /*  Add the temporary filename to the load_namesp array:  */          /*  Add the temporary filename to the load_namesp array:  */
411          (*n_loadp)++;          (*n_loadp)++;
412          new_array = malloc(sizeof(char *) * (*n_loadp));          new_array = malloc(sizeof(char *) * (*n_loadp));
# Line 409  static int iso_load_bootblock(struct mac Line 420  static int iso_load_bootblock(struct mac
420          /*  This adds a Backspace char in front of the filename; this          /*  This adds a Backspace char in front of the filename; this
421              is a special hack which causes the file to be removed once              is a special hack which causes the file to be removed once
422              it has been loaded.  */              it has been loaded.  */
423          tmpfilename = realloc(tmpfilename, strlen(tmpfilename) + 2);          tmpfname = realloc(tmpfname, strlen(tmpfname) + 2);
424          memmove(tmpfilename + 1, tmpfilename, strlen(tmpfilename) + 1);          memmove(tmpfname + 1, tmpfname, strlen(tmpfname) + 1);
425          tmpfilename[0] = 8;          tmpfname[0] = 8;
426    
427          (*load_namesp)[*n_loadp - 1] = tmpfilename;          (*load_namesp)[*n_loadp - 1] = tmpfname;
428    
429          res = 1;          res = 1;
430    
# Line 435  ret: Line 446  ret:
446    
447    
448  /*  /*
449     *  apple_load_bootblock():
450     *
451     *  Try to load a kernel from a disk image with an Apple Partition Table.
452     *
453     *  TODO: This function uses too many magic offsets and so on; it should be
454     *  cleaned up some day. See http://www.awprofessional.com/articles/
455     *      article.asp?p=376123&seqNum=3&rl=1  for some info on the Apple
456     *  partition format.
457     *
458     *  Returns 1 on success, 0 on failure.
459     */
460    static int apple_load_bootblock(struct machine *m, struct cpu *cpu,
461            int disk_id, int disk_type, int *n_loadp, char ***load_namesp)
462    {
463            unsigned char buf[0x8000];
464            int res, partnr, n_partitions = 0, n_hfs_partitions = 0;
465            uint64_t hfs_start, hfs_length;
466    
467            res = diskimage_access(m, disk_id, disk_type, 0, 0x0, buf, sizeof(buf));
468            if (!res) {
469                    fatal("apple_load_bootblock: couldn't read the disk "
470                        "image. Aborting.\n");
471                    return 0;
472            }
473    
474            partnr = 0;
475            do {
476                    int start, length;
477                    int ofs = 0x200 * (partnr + 1);
478                    if (partnr == 0)
479                            n_partitions = buf[ofs + 7];
480                    start = ((uint64_t)buf[ofs + 8] << 24) + (buf[ofs + 9] << 16) +
481                        (buf[ofs + 10] << 8) + buf[ofs + 11];
482                    length = ((uint64_t)buf[ofs+12] << 24) + (buf[ofs + 13] << 16) +
483                        (buf[ofs + 14] << 8) + buf[ofs + 15];
484    
485                    debug("partition %i: '%s', type '%s', start %i, length %i\n",
486                        partnr, buf + ofs + 0x10, buf + ofs + 0x30,
487                        start, length);
488    
489                    if (strcmp((char *)buf + ofs + 0x30, "Apple_HFS") == 0) {
490                            n_hfs_partitions ++;
491                            hfs_start = 512 * start;
492                            hfs_length = 512 * length;
493                    }
494    
495                    /*  Any more partitions?  */
496                    partnr ++;
497            } while (partnr < n_partitions);
498    
499            if (n_hfs_partitions == 0) {
500                    fatal("Error: No HFS partition found! TODO\n");
501                    return 0;
502            }
503            if (n_hfs_partitions >= 2) {
504                    fatal("Error: Too many HFS partitions found! TODO\n");
505                    return 0;
506            }
507    
508            return 0;
509    }
510    
511    
512    /*
513   *  load_bootblock():   *  load_bootblock():
514   *   *
515   *  For some emulation modes, it is possible to boot from a harddisk image by   *  For some emulation modes, it is possible to boot from a harddisk image by
# Line 459  static int load_bootblock(struct machine Line 534  static int load_bootblock(struct machine
534                  return 0;                  return 0;
535    
536          switch (m->machine_type) {          switch (m->machine_type) {
537          case MACHINE_DEC:          case MACHINE_PMAX:
538                  /*                  /*
539                   *  The first few bytes of a disk contains information about                   *  The first few bytes of a disk contains information about
540                   *  where the bootblock(s) are located. (These are all 32-bit                   *  where the bootblock(s) are located. (These are all 32-bit
# Line 480  static int load_bootblock(struct machine Line 555  static int load_bootblock(struct machine
555                      minibuf, sizeof(minibuf));                      minibuf, sizeof(minibuf));
556    
557                  bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8)                  bootblock_loadaddr = minibuf[0x10] + (minibuf[0x11] << 8)
558                    + (minibuf[0x12] << 16) + (minibuf[0x13] << 24);                    + (minibuf[0x12] << 16) + ((uint64_t)minibuf[0x13] << 24);
559    
560                  /*  Convert loadaddr to uncached:  */                  /*  Convert loadaddr to uncached:  */
561                  if ((bootblock_loadaddr & 0xf0000000ULL) != 0x80000000 &&                  if ((bootblock_loadaddr & 0xf0000000ULL) != 0x80000000 &&
# Line 491  static int load_bootblock(struct machine Line 566  static int load_bootblock(struct machine
566                  bootblock_loadaddr |= 0xffffffffa0000000ULL;                  bootblock_loadaddr |= 0xffffffffa0000000ULL;
567    
568                  bootblock_pc = minibuf[0x14] + (minibuf[0x15] << 8)                  bootblock_pc = minibuf[0x14] + (minibuf[0x15] << 8)
569                    + (minibuf[0x16] << 16) + (minibuf[0x17] << 24);                    + (minibuf[0x16] << 16) + ((uint64_t)minibuf[0x17] << 24);
570    
571                  bootblock_pc &= 0x0fffffffULL;                  bootblock_pc &= 0x0fffffffULL;
572                  bootblock_pc |= 0xffffffffa0000000ULL;                  bootblock_pc |= 0xffffffffa0000000ULL;
# Line 512  static int load_bootblock(struct machine Line 587  static int load_bootblock(struct machine
587                          }                          }
588    
589                          n_blocks = minibuf[0] + (minibuf[1] << 8)                          n_blocks = minibuf[0] + (minibuf[1] << 8)
590                            + (minibuf[2] << 16) + (minibuf[3] << 24);                            + (minibuf[2] << 16) + ((uint64_t)minibuf[3] << 24);
591    
592                          bootblock_offset = (minibuf[4] + (minibuf[5] << 8)                          bootblock_offset = (minibuf[4] + (minibuf[5] << 8) +
593                            + (minibuf[6] << 16) + (minibuf[7] << 24)) * 512;                            (minibuf[6]<<16) + ((uint64_t)minibuf[7]<<24)) * 512;
594    
595                          if (n_blocks < 1)                          if (n_blocks < 1)
596                                  break;                                  break;
# Line 586  static int load_bootblock(struct machine Line 661  static int load_bootblock(struct machine
661    
662          /*          /*
663           *  Try reading a kernel manually from the disk. The code here           *  Try reading a kernel manually from the disk. The code here
664           *  does not rely on machine-dependant boot blocks etc.           *  does not rely on machine-dependent boot blocks etc.
665           */           */
666          /*  ISO9660: (0x800 bytes at 0x8000)  */          /*  ISO9660: (0x800 bytes at 0x8000)  */
667          bootblock_buf = malloc(0x800);          bootblock_buf = malloc(0x800);
# Line 623  static int load_bootblock(struct machine Line 698  static int load_bootblock(struct machine
698                              n_loadp, load_namesp);                              n_loadp, load_namesp);
699          }          }
700    
701            if (retval != 0)
702                    goto ret_ok;
703    
704            /*  Apple parition table:  */
705            res = diskimage_access(m, boot_disk_id, boot_disk_type,
706                0, 0x0, bootblock_buf, 0x800);
707            if (!res) {
708                    fatal("Couldn't read the disk image. Aborting.\n");
709                    return 0;
710            }
711            if (bootblock_buf[0x000] == 'E' && bootblock_buf[0x001] == 'R' &&
712                bootblock_buf[0x200] == 'P' && bootblock_buf[0x201] == 'M') {
713                    /*  We can't load a kernel if the name
714                        isn't specified.  */
715                    if (cpu->machine->boot_kernel_filename == NULL ||
716                        cpu->machine->boot_kernel_filename[0] == '\0')
717                            fatal("\nApple partition table, but no kernel "
718                                "specified? (Use the -j option.)\n");
719                    else
720                            retval = apple_load_bootblock(m, cpu, boot_disk_id,
721                                boot_disk_type, n_loadp, load_namesp);
722            }
723    
724    ret_ok:
725          free(bootblock_buf);          free(bootblock_buf);
726          return retval;          return retval;
727  }  }
# Line 709  static void add_arc_components(struct ma Line 808  static void add_arc_components(struct ma
808    
809          len += 1048576 * m->memory_offset_in_mb;          len += 1048576 * m->memory_offset_in_mb;
810    
811          /*  NOTE/TODO: magic 12MB end of load program area  */          /*
812             *  NOTE/TODO: magic 12MB end of load program area
813             *
814             *  Hm. This breaks the old FreeBSD/MIPS snapshots...
815             */
816    #if 0
817          arcbios_add_memory_descriptor(cpu,          arcbios_add_memory_descriptor(cpu,
818              0x60000 + m->memory_offset_in_mb * 1048576,              0x60000 + m->memory_offset_in_mb * 1048576,
819              start-0x60000 - m->memory_offset_in_mb * 1048576,              start-0x60000 - m->memory_offset_in_mb * 1048576,
820              ARCBIOS_MEM_FreeMemory);              ARCBIOS_MEM_FreeMemory);
821    #endif
822          arcbios_add_memory_descriptor(cpu,          arcbios_add_memory_descriptor(cpu,
823              start, len, ARCBIOS_MEM_LoadedProgram);              start, len, ARCBIOS_MEM_LoadedProgram);
824    
# Line 806  static void add_arc_components(struct ma Line 911  static void add_arc_components(struct ma
911  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,
912          int n_devices, char **device_names)          int n_devices, char **device_names)
913  {  {
         struct emul *emul;  
914          struct cpu *cpu;          struct cpu *cpu;
915          int i, iadd=4;          int i, iadd = DEBUG_INDENTATION;
916          uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0;          uint64_t memory_amount, entrypoint = 0, gp = 0, toc = 0;
917          int byte_order;          int byte_order;
918    
         emul = m->emul;  
   
919          debug("machine \"%s\":\n", m->name);          debug("machine \"%s\":\n", m->name);
920          debug_indentation(iadd);          debug_indentation(iadd);
921    
# Line 834  void emul_machine_setup(struct machine * Line 936  void emul_machine_setup(struct machine *
936    
937          m->cpu_family = cpu_family_ptr_by_number(m->arch);          m->cpu_family = cpu_family_ptr_by_number(m->arch);
938    
939          if (m->arch != ARCH_MIPS)          if (m->arch == ARCH_ALPHA)
940                  m->bintrans_enable = 0;                  m->arch_pagesize = 8192;
941    
942          machine_memsize_fix(m);          machine_memsize_fix(m);
943    
# Line 857  void emul_machine_setup(struct machine * Line 959  void emul_machine_setup(struct machine *
959                  debug(" (offset by %iMB)", m->memory_offset_in_mb);                  debug(" (offset by %iMB)", m->memory_offset_in_mb);
960                  memory_amount += 1048576 * m->memory_offset_in_mb;                  memory_amount += 1048576 * m->memory_offset_in_mb;
961          }          }
962          m->memory = memory_new(memory_amount);          m->memory = memory_new(memory_amount, m->arch);
963          if (m->machine_type != MACHINE_USERLAND)          if (m->machine_type != MACHINE_USERLAND)
964                  debug("\n");                  debug("\n");
965    
# Line 884  void emul_machine_setup(struct machine * Line 986  void emul_machine_setup(struct machine *
986          }          }
987          memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus);          memset(m->cpus, 0, sizeof(struct cpu *) * m->ncpus);
988    
         /*  Initialize dynamic binary translation, if available:  */  
         if (m->bintrans_enable)  
                 bintrans_init(m, m->memory);  
   
989          debug("cpu0");          debug("cpu0");
990          if (m->ncpus > 1)          if (m->ncpus > 1)
991                  debug(" .. cpu%i", m->ncpus - 1);                  debug(" .. cpu%i", m->ncpus - 1);
992          debug(": ");          debug(": ");
993          for (i=0; i<m->ncpus; i++) {          for (i=0; i<m->ncpus; i++) {
994                  m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name);                  m->cpus[i] = cpu_new(m->memory, m, i, m->cpu_name);
995                  if (m->bintrans_enable)                  if (m->cpus[i] == NULL) {
996                          bintrans_init_cpu(m->cpus[i]);                          fprintf(stderr, "Unable to create CPU object. "
997                                "Aborting.");
998                            exit(1);
999                    }
1000          }          }
1001          debug("\n");          debug("\n");
1002    
# Line 906  void emul_machine_setup(struct machine * Line 1007  void emul_machine_setup(struct machine *
1007                  m->cpus[m->ncpus] = cpu_new(m->memory, m,                  m->cpus[m->ncpus] = cpu_new(m->memory, m,
1008                      0  /*  use 0 here to show info with debug()  */,                      0  /*  use 0 here to show info with debug()  */,
1009                      "Allegrex" /*  TODO  */);                      "Allegrex" /*  TODO  */);
                 if (m->bintrans_enable)  
                         bintrans_init_cpu(m->cpus[m->ncpus]);  
1010                  debug("\n");                  debug("\n");
1011                  m->ncpus ++;                  m->ncpus ++;
1012          }          }
# Line 924  void emul_machine_setup(struct machine * Line 1023  void emul_machine_setup(struct machine *
1023          if (m->userland_emul != NULL) {          if (m->userland_emul != NULL) {
1024                  useremul_name_to_useremul(cpu,                  useremul_name_to_useremul(cpu,
1025                      m->userland_emul, NULL, NULL, NULL);                      m->userland_emul, NULL, NULL, NULL);
1026                  cpu->memory_rw = userland_memory_rw;  
1027                    switch (m->arch) {
1028    #ifdef ENABLE_ALPHA
1029                    case ARCH_ALPHA:
1030                            cpu->memory_rw = alpha_userland_memory_rw;
1031                            break;
1032    #endif
1033                    default:cpu->memory_rw = userland_memory_rw;
1034                    }
1035          }          }
1036    
1037          if (m->use_x11)          if (m->use_x11)
# Line 955  void emul_machine_setup(struct machine * Line 1062  void emul_machine_setup(struct machine *
1062          }          }
1063    
1064          diskimage_dump_info(m);          diskimage_dump_info(m);
1065            console_debug_dump(m);
1066    
1067          /*  Load files (ROM code, boot code, ...) into memory:  */          /*  Load files (ROM code, boot code, ...) into memory:  */
1068          if (n_load == 0) {          if (n_load == 0) {
# Line 1103  void emul_machine_setup(struct machine * Line 1211  void emul_machine_setup(struct machine *
1211                  cpu->pc = entrypoint;                  cpu->pc = entrypoint;
1212    
1213                  switch (m->arch) {                  switch (m->arch) {
1214    
1215                    case ARCH_ALPHA:
1216                            /*  For position-independent code:  */
1217                            cpu->cd.alpha.r[ALPHA_T12] = cpu->pc;
1218                            break;
1219    
1220                    case ARCH_ARM:
1221                            if (cpu->pc & 3) {
1222                                    fatal("ARM: lowest bits of pc set: TODO\n");
1223                                    exit(1);
1224                            }
1225                            cpu->pc &= 0xfffffffc;
1226                            break;
1227    
1228                    case ARCH_AVR:
1229                            cpu->pc &= 0xfffff;
1230                            if (cpu->pc & 1) {
1231                                    fatal("AVR: lowest bit of pc set: TODO\n");
1232                                    exit(1);
1233                            }
1234                            break;
1235    
1236                    case ARCH_HPPA:
1237                            break;
1238    
1239                    case ARCH_I960:
1240                            break;
1241    
1242                    case ARCH_IA64:
1243                            break;
1244    
1245                    case ARCH_M68K:
1246                            break;
1247    
1248                  case ARCH_MIPS:                  case ARCH_MIPS:
1249                          if ((cpu->pc >> 32) == 0                          if ((cpu->pc >> 32) == 0 && (cpu->pc & 0x80000000ULL))
                             && (cpu->pc & 0x80000000ULL))  
1250                                  cpu->pc |= 0xffffffff00000000ULL;                                  cpu->pc |= 0xffffffff00000000ULL;
1251    
1252                          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 1262  void emul_machine_setup(struct machine *
1262                              spec/x458.html for more info.  */                              spec/x458.html for more info.  */
1263                          cpu->cd.ppc.gpr[2] = toc;                          cpu->cd.ppc.gpr[2] = toc;
1264                          /*  TODO  */                          /*  TODO  */
1265                            if (cpu->cd.ppc.bits == 32)
1266                                    cpu->pc &= 0xffffffffULL;
1267                          break;                          break;
1268    
1269                  case ARCH_ALPHA:                  case ARCH_SH:
1270                  case ARCH_HPPA:                          if (cpu->cd.sh.bits == 32)
1271                  case ARCH_SPARC:                                  cpu->pc &= 0xffffffffULL;
1272                  case ARCH_URISC:                          cpu->pc &= ~1;
1273                          break;                          break;
1274    
1275                  case ARCH_ARM:                  case ARCH_SPARC:
                         cpu->pc &= 0xfffffffc;  
                         cpu->cd.arm.r[ARM_PC] = cpu->pc;  
1276                          break;                          break;
1277    
1278                  case ARCH_X86:                  case ARCH_X86:
# Line 1200  void emul_machine_setup(struct machine * Line 1341  void emul_machine_setup(struct machine *
1341          add_dump_points(m);          add_dump_points(m);
1342    
1343          /*  TODO: This is MIPS-specific!  */          /*  TODO: This is MIPS-specific!  */
1344          if (m->machine_type == MACHINE_DEC &&          if (m->machine_type == MACHINE_PMAX &&
1345              cpu->cd.mips.cpu_type.mmu_model == MMU3K)              cpu->cd.mips.cpu_type.mmu_model == MMU3K)
1346                  add_symbol_name(&m->symbol_context,                  add_symbol_name(&m->symbol_context,
1347                      0x9fff0000, 0x10000, "r2k3k_cache", 0);                      0x9fff0000, 0x10000, "r2k3k_cache", 0, 0);
1348    
1349          symbol_recalc_sizes(&m->symbol_context);          symbol_recalc_sizes(&m->symbol_context);
1350    
         if (m->max_random_cycles_per_chunk > 0)  
                 debug("using random cycle chunks (1 to %i cycles)\n",  
                     m->max_random_cycles_per_chunk);  
   
1351          /*  Special hack for ARC/SGI emulation:  */          /*  Special hack for ARC/SGI emulation:  */
1352          if ((m->machine_type == MACHINE_ARC ||          if ((m->machine_type == MACHINE_ARC ||
1353              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 1355  void emul_machine_setup(struct machine *
1355    
1356          debug("starting cpu%i at ", m->bootstrap_cpu);          debug("starting cpu%i at ", m->bootstrap_cpu);
1357          switch (m->arch) {          switch (m->arch) {
1358    
1359            case ARCH_ARM:
1360                    /*  ARM cpus aren't 64-bit:  */
1361                    debug("0x%08x", (int)entrypoint);
1362                    break;
1363    
1364            case ARCH_AVR:
1365                    /*  Atmel AVR uses a 16-bit or 22-bit program counter:  */
1366                    debug("0x%04x", (int)entrypoint);
1367                    break;
1368    
1369          case ARCH_MIPS:          case ARCH_MIPS:
1370                  if (cpu->cd.mips.cpu_type.isa_level < 3 ||                  if (cpu->is_32bit) {
                     cpu->cd.mips.cpu_type.isa_level == 32) {  
1371                          debug("0x%08x", (int)m->cpus[                          debug("0x%08x", (int)m->cpus[
1372                              m->bootstrap_cpu]->pc);                              m->bootstrap_cpu]->pc);
1373                          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 1382  void emul_machine_setup(struct machine *
1382                                      cpu->cd.mips.gpr[MIPS_GPR_GP]);                                      cpu->cd.mips.gpr[MIPS_GPR_GP]);
1383                  }                  }
1384                  break;                  break;
1385    
1386          case ARCH_PPC:          case ARCH_PPC:
1387                  if (cpu->cd.ppc.bits == 32)                  if (cpu->cd.ppc.bits == 32)
1388                          debug("0x%08x", (int)entrypoint);                          debug("0x%08x", (int)entrypoint);
1389                  else                  else
1390                          debug("0x%016llx", (long long)entrypoint);                          debug("0x%016llx", (long long)entrypoint);
1391                  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];  
                         }  
1392    
                         snprintf(tmps, sizeof(tmps), "0x%%0%illx",  
                             cpu->cd.urisc.wordlen / 4);  
                         debug(tmps, (long long)entrypoint);  
                         cpu->pc = entrypoint;  
                 }  
                 break;  
1393          case ARCH_X86:          case ARCH_X86:
1394                  debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS],                  debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS],
1395                      (long long)cpu->pc);                      (long long)cpu->pc);
1396                  break;                  break;
1397    
1398          default:          default:
1399                  debug("0x%016llx", (long long)cpu->pc);                  if (cpu->is_32bit)
1400                            debug("0x%08x", (int)cpu->pc);
1401                    else
1402                            debug("0x%016llx", (long long)cpu->pc);
1403          }          }
1404          debug("\n");          debug("\n");
1405    
# Line 1289  void emul_machine_setup(struct machine * Line 1414  void emul_machine_setup(struct machine *
1414   */   */
1415  void emul_dumpinfo(struct emul *e)  void emul_dumpinfo(struct emul *e)
1416  {  {
1417          int j, nm, iadd = 4;          int j, nm, iadd = DEBUG_INDENTATION;
1418    
1419          if (e->net != NULL)          if (e->net != NULL)
1420                  net_dumpinfo(e->net);                  net_dumpinfo(e->net);
# Line 1318  void emul_dumpinfo(struct emul *e) Line 1443  void emul_dumpinfo(struct emul *e)
1443   */   */
1444  void emul_simple_init(struct emul *emul)  void emul_simple_init(struct emul *emul)
1445  {  {
1446          int iadd=4;          int iadd = DEBUG_INDENTATION;
1447          struct machine *m;          struct machine *m;
1448    
1449          if (emul->n_machines != 1) {          if (emul->n_machines != 1) {
# Line 1355  void emul_simple_init(struct emul *emul) Line 1480  void emul_simple_init(struct emul *emul)
1480   */   */
1481  struct emul *emul_create_from_configfile(char *fname)  struct emul *emul_create_from_configfile(char *fname)
1482  {  {
1483          int iadd = 4;          int iadd = DEBUG_INDENTATION;
1484          struct emul *e = emul_new(fname);          struct emul *e = emul_new(fname);
         FILE *f;  
         char buf[128];  
         size_t len;  
1485    
1486          debug("Creating emulation from configfile \"%s\":\n", fname);          debug("Creating emulation from configfile \"%s\":\n", fname);
1487          debug_indentation(iadd);          debug_indentation(iadd);
1488    
1489          f = fopen(fname, "r");          emul_parse_config(e, fname);
         if (f == NULL) {  
                 perror(fname);  
                 exit(1);  
         }  
1490    
         /*  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);  
   
         emul_parse_config(e, f);  
   
         fclose(f);  
1491          debug_indentation(-iadd);          debug_indentation(-iadd);
1492          return e;          return e;
1493  }  }
# Line 1409  void emul_run(struct emul **emuls, int n Line 1514  void emul_run(struct emul **emuls, int n
1514    
1515          atexit(fix_console);          atexit(fix_console);
1516    
         i = 79;  
         while (i-- > 0)  
                 debug("-");  
         debug("\n\n");  
   
1517          /*  Initialize the interactive debugger:  */          /*  Initialize the interactive debugger:  */
1518          debugger_init(emuls, n_emuls);          debugger_init(emuls, n_emuls);
1519    
1520            /*  Run any additional debugger commands before starting:  */
1521            for (i=0; i<n_emuls; i++) {
1522                    struct emul *emul = emuls[i];
1523                    if (emul->n_debugger_cmds > 0) {
1524                            int j;
1525                            if (i == 0)
1526                                    print_separator();
1527                            for (j = 0; j < emul->n_debugger_cmds; j ++) {
1528                                    debug("> %s\n", emul->debugger_cmds[j]);
1529                                    debugger_execute_cmd(emul->debugger_cmds[j],
1530                                        strlen(emul->debugger_cmds[j]));
1531                            }
1532                    }
1533            }
1534    
1535            print_separator();
1536            debug("\n");
1537    
1538    
1539          /*          /*
1540           *  console_init_main() makes sure that the terminal is in a           *  console_init_main() makes sure that the terminal is in a
1541           *  reasonable state.           *  reasonable state.
# Line 1441  void emul_run(struct emul **emuls, int n Line 1560  void emul_run(struct emul **emuls, int n
1560                  if (e == NULL)                  if (e == NULL)
1561                          continue;                          continue;
1562                  for (j=0; j<e->n_machines; j++)                  for (j=0; j<e->n_machines; j++)
1563                          cpu_run_init(e, e->machines[j]);                          cpu_run_init(e->machines[j]);
1564          }          }
1565    
1566            /*  TODO: Generalize:  */
1567            if (emuls[0]->machines[0]->show_trace_tree)
1568                    cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0],
1569                        emuls[0]->machines[0]->cpus[0]->pc);
1570    
1571          /*          /*
1572           *  MAIN LOOP:           *  MAIN LOOP:
1573           *           *
# Line 1461  void emul_run(struct emul **emuls, int n Line 1585  void emul_run(struct emul **emuls, int n
1585                                  continue;                                  continue;
1586    
1587                          for (j=0; j<e->n_machines; j++) {                          for (j=0; j<e->n_machines; j++) {
1588                                    if (e->machines[j]->gdb.port > 0)
1589                                            debugger_gdb_check_incoming(
1590                                                e->machines[j]);
1591    
1592                                  /*  TODO: cpu_run() is a strange name, since                                  /*  TODO: cpu_run() is a strange name, since
1593                                      there can be multiple cpus in a machine  */                                      there can be multiple cpus in a machine  */
1594                                  anything = cpu_run(e, e->machines[j]);                                  anything = cpu_run(e, e->machines[j]);
# Line 1476  void emul_run(struct emul **emuls, int n Line 1604  void emul_run(struct emul **emuls, int n
1604                  if (e == NULL)                  if (e == NULL)
1605                          continue;                          continue;
1606                  for (j=0; j<e->n_machines; j++)                  for (j=0; j<e->n_machines; j++)
1607                          cpu_run_deinit(e, e->machines[j]);                          cpu_run_deinit(e->machines[j]);
1608          }          }
1609    
1610          /*  force_debugger_at_exit flag set? Then enter the debugger:  */          /*  force_debugger_at_exit flag set? Then enter the debugger:  */

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

  ViewVC Help
Powered by ViewVC 1.1.26