/[gxemul]/trunk/src/cpu.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/cpu.c

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

revision 12 by dpavlin, Mon Oct 8 16:18:38 2007 UTC revision 28 by dpavlin, Mon Oct 8 16:20:26 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *  Copyright (C) 2005  Anders Gavare.  All rights reserved.   *  Copyright (C) 2005-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: cpu.c,v 1.316 2005/08/16 05:37:09 debug Exp $   *  $Id: cpu.c,v 1.348 2006/07/20 21:52:59 debug Exp $
29   *   *
30   *  Common routines for CPU emulation. (Not specific to any CPU type.)   *  Common routines for CPU emulation. (Not specific to any CPU type.)
31   */   */
# Line 33  Line 33 
33  #include <stdio.h>  #include <stdio.h>
34  #include <stdlib.h>  #include <stdlib.h>
35  #include <sys/types.h>  #include <sys/types.h>
36    #include <sys/mman.h>
37  #include <string.h>  #include <string.h>
38    
39  #include "cpu.h"  #include "cpu.h"
# Line 41  Line 42 
42  #include "misc.h"  #include "misc.h"
43    
44    
 extern int quiet_mode;  
 extern int show_opcode_statistics;  
   
   
45  static struct cpu_family *first_cpu_family = NULL;  static struct cpu_family *first_cpu_family = NULL;
46    
47    
# Line 97  struct cpu *cpu_new(struct memory *mem, Line 94  struct cpu *cpu_new(struct memory *mem,
94                                              "NULL\n");                                              "NULL\n");
95                                          exit(1);                                          exit(1);
96                                  }                                  }
97                                  return cpu;                                  break;
98                          }                          }
99                  }                  }
100    
101                  fp = fp->next;                  fp = fp->next;
102          }          }
103    
104          fatal("\ncpu_new(): unknown cpu type '%s'\n", cpu_type_name);          if (fp == NULL) {
105          exit(1);                  fatal("\ncpu_new(): unknown cpu type '%s'\n", cpu_type_name);
106  }                  return NULL;
107            }
108    
109            fp->init_tables(cpu);
110    
111  /*          return cpu;
  *  cpu_show_full_statistics():  
  *  
  *  Show detailed statistics on opcode usage on each cpu.  
  */  
 void cpu_show_full_statistics(struct machine *m)  
 {  
         if (m->cpu_family == NULL ||  
             m->cpu_family->show_full_statistics == NULL)  
                 fatal("cpu_show_full_statistics(): NULL\n");  
         else  
                 m->cpu_family->show_full_statistics(m);  
112  }  }
113    
114    
# Line 165  void cpu_register_match(struct machine * Line 153  void cpu_register_match(struct machine *
153   *  tracing.   *  tracing.
154   */   */
155  int cpu_disassemble_instr(struct machine *m, struct cpu *cpu,  int cpu_disassemble_instr(struct machine *m, struct cpu *cpu,
156          unsigned char *instr, int running, uint64_t addr, int bintrans)          unsigned char *instr, int running, uint64_t addr)
157  {  {
158          if (m->cpu_family == NULL || m->cpu_family->disassemble_instr == NULL) {          if (m->cpu_family == NULL || m->cpu_family->disassemble_instr == NULL) {
159                  fatal("cpu_disassemble_instr(): NULL\n");                  fatal("cpu_disassemble_instr(): NULL\n");
160                  return 0;                  return 0;
161          } else          } else
162                  return m->cpu_family->disassemble_instr(cpu, instr,                  return m->cpu_family->disassemble_instr(cpu, instr,
163                      running, addr, bintrans);                      running, addr);
164  }  }
165    
166    
# Line 181  int cpu_disassemble_instr(struct machine Line 169  int cpu_disassemble_instr(struct machine
169   *   *
170   *  Dump cpu registers in a relatively readable format.   *  Dump cpu registers in a relatively readable format.
171   *   *
172   *  gprs: set to non-zero to dump GPRs. (CPU dependant.)   *  gprs: set to non-zero to dump GPRs. (CPU dependent.)
173   *  coprocs: set bit 0..x to dump registers in coproc 0..x. (CPU dependant.)   *  coprocs: set bit 0..x to dump registers in coproc 0..x. (CPU dependent.)
174   */   */
175  void cpu_register_dump(struct machine *m, struct cpu *cpu,  void cpu_register_dump(struct machine *m, struct cpu *cpu,
176          int gprs, int coprocs)          int gprs, int coprocs)
# Line 195  void cpu_register_dump(struct machine *m Line 183  void cpu_register_dump(struct machine *m
183    
184    
185  /*  /*
186     *  cpu_gdb_stub():
187     *
188     *  Execute a "remote GDB" command. Return value is a pointer to a newly
189     *  allocated response string, if the command was successfully executed. If
190     *  there was an error, NULL is returned.
191     */
192    char *cpu_gdb_stub(struct cpu *cpu, char *cmd)
193    {
194            if (cpu->machine->cpu_family == NULL ||
195                cpu->machine->cpu_family->gdb_stub == NULL) {
196                    fatal("cpu_gdb_stub(): NULL\n");
197                    return NULL;
198            } else
199                    return cpu->machine->cpu_family->gdb_stub(cpu, cmd);
200    }
201    
202    
203    /*
204   *  cpu_interrupt():   *  cpu_interrupt():
205   *   *
206   *  Assert an interrupt.   *  Assert an interrupt.
# Line 244  void cpu_functioncall_trace(struct cpu * Line 250  void cpu_functioncall_trace(struct cpu *
250                  fatal("cpu%i:\t", cpu->cpu_id);                  fatal("cpu%i:\t", cpu->cpu_id);
251    
252          cpu->trace_tree_depth ++;          cpu->trace_tree_depth ++;
253            if (cpu->trace_tree_depth > 100)
254                    cpu->trace_tree_depth = 100;
255          for (i=0; i<cpu->trace_tree_depth; i++)          for (i=0; i<cpu->trace_tree_depth; i++)
256                  fatal("  ");                  fatal("  ");
257    
# Line 254  void cpu_functioncall_trace(struct cpu * Line 262  void cpu_functioncall_trace(struct cpu *
262                  fatal("%s", symbol);                  fatal("%s", symbol);
263          else {          else {
264                  if (cpu->is_32bit)                  if (cpu->is_32bit)
265                          fatal("0x%08x", (int)f);                          fatal("0x%"PRIx32, (uint32_t) f);
266                  else                  else
267                          fatal("0x%llx", (long long)f);                          fatal("0x%"PRIx64, (uint64_t) f);
268          }          }
269          fatal("(");          fatal("(");
270    
# Line 264  void cpu_functioncall_trace(struct cpu * Line 272  void cpu_functioncall_trace(struct cpu *
272                  cpu->machine->cpu_family->functioncall_trace(cpu, f, n_args);                  cpu->machine->cpu_family->functioncall_trace(cpu, f, n_args);
273    
274          fatal(")>\n");          fatal(")>\n");
275    
276    #ifdef PRINT_MEMORY_CHECKSUM
277            /*  Temporary hack for finding bugs:  */
278            fatal("call chksum=%016"PRIx64"\n", memory_checksum(cpu->mem));
279    #endif
280  }  }
281    
282    
# Line 292  void cpu_functioncall_trace_return(struc Line 305  void cpu_functioncall_trace_return(struc
305   */   */
306  void cpu_create_or_reset_tc(struct cpu *cpu)  void cpu_create_or_reset_tc(struct cpu *cpu)
307  {  {
308            size_t s = DYNTRANS_CACHE_SIZE + DYNTRANS_CACHE_MARGIN;
309    
310          if (cpu->translation_cache == NULL)          if (cpu->translation_cache == NULL)
311                  cpu->translation_cache = zeroed_alloc(DYNTRANS_CACHE_SIZE +                  cpu->translation_cache = zeroed_alloc(s);
                     DYNTRANS_CACHE_MARGIN);  
312    
313          /*  Create an empty table at the beginning of the translation cache:  */          /*  Create an empty table at the beginning of the translation cache:  */
314          memset(cpu->translation_cache, 0, sizeof(uint32_t)          memset(cpu->translation_cache, 0, sizeof(uint32_t)
# Line 307  void cpu_create_or_reset_tc(struct cpu * Line 321  void cpu_create_or_reset_tc(struct cpu *
321           *  There might be other translation pointers that still point to           *  There might be other translation pointers that still point to
322           *  within the translation_cache region. Let's invalidate those too:           *  within the translation_cache region. Let's invalidate those too:
323           */           */
324          if (cpu->invalidate_code_translation_caches != NULL)          if (cpu->invalidate_code_translation != NULL)
325                  cpu->invalidate_code_translation_caches(cpu);                  cpu->invalidate_code_translation(cpu, 0, INVALIDATE_ALL);
 }  
   
   
 /*  
  *  cpu_run():  
  *  
  *  Run instructions on all CPUs in this machine, for a "medium duration"  
  *  (or until all CPUs have halted).  
  *  
  *  Return value is 1 if anything happened, 0 if all CPUs are stopped.  
  */  
 int cpu_run(struct emul *emul, struct machine *m)  
 {  
         if (m->cpu_family == NULL || m->cpu_family->run == NULL) {  
                 fatal("cpu_run(): NULL\n");  
                 return 0;  
         } else  
                 return m->cpu_family->run(emul, m);  
326  }  }
327    
328    
# Line 334  int cpu_run(struct emul *emul, struct ma Line 330  int cpu_run(struct emul *emul, struct ma
330   *  cpu_dumpinfo():   *  cpu_dumpinfo():
331   *   *
332   *  Dumps info about a CPU using debug(). "cpu0: CPUNAME, running" (or similar)   *  Dumps info about a CPU using debug(). "cpu0: CPUNAME, running" (or similar)
333   *  is outputed, and it is up to CPU dependant code to complete the line.   *  is outputed, and it is up to CPU dependent code to complete the line.
334   */   */
335  void cpu_dumpinfo(struct machine *m, struct cpu *cpu)  void cpu_dumpinfo(struct machine *m, struct cpu *cpu)
336  {  {
# Line 356  void cpu_dumpinfo(struct machine *m, str Line 352  void cpu_dumpinfo(struct machine *m, str
352  void cpu_list_available_types(void)  void cpu_list_available_types(void)
353  {  {
354          struct cpu_family *fp;          struct cpu_family *fp;
355          int iadd = 4;          int iadd = DEBUG_INDENTATION;
356    
357          fp = first_cpu_family;          fp = first_cpu_family;
358    
# Line 391  void cpu_run_deinit(struct machine *mach Line 387  void cpu_run_deinit(struct machine *mach
387          int te;          int te;
388    
389          /*          /*
390           *  Two last ticks of every hardware device.  This will allow           *  Two last ticks of every hardware device.  This will allow e.g.
391           *  framebuffers to draw the last updates to the screen before           *  framebuffers to draw the last updates to the screen before halting.
392           *  halting.           *
393             *  TODO: This should be refactored when redesigning the mainbus
394             *        concepts!
395           */           */
396          for (te=0; te<machine->n_tick_entries; te++) {          for (te=0; te<machine->n_tick_entries; te++) {
397                  machine->tick_func[te](machine->cpus[0],                  machine->tick_func[te](machine->cpus[0],
# Line 402  void cpu_run_deinit(struct machine *mach Line 400  void cpu_run_deinit(struct machine *mach
400                      machine->tick_extra[te]);                      machine->tick_extra[te]);
401          }          }
402    
403          debug("cpu_run_deinit(): All CPUs halted.\n");          if (machine->show_nr_of_instructions)
   
         if (machine->show_nr_of_instructions || !quiet_mode)  
404                  cpu_show_cycles(machine, 1);                  cpu_show_cycles(machine, 1);
405    
         if (show_opcode_statistics)  
                 cpu_show_full_statistics(machine);  
   
406          fflush(stdout);          fflush(stdout);
407  }  }
408    
# Line 428  void cpu_show_cycles(struct machine *mac Line 421  void cpu_show_cycles(struct machine *mac
421          char *symbol;          char *symbol;
422          int64_t mseconds, ninstrs, is, avg;          int64_t mseconds, ninstrs, is, avg;
423          struct timeval tv;          struct timeval tv;
424          int h, m, s, ms, d, instrs_per_cycle = 1;          int h, m, s, ms, d;
425    
426          static int64_t mseconds_last = 0;          static int64_t mseconds_last = 0;
427          static int64_t ninstrs_last = -1;          static int64_t ninstrs_last = -1;
428    
         switch (machine->arch) {  
         case ARCH_MIPS:  
                 instrs_per_cycle = machine->cpus[machine->bootstrap_cpu]->  
                     cd.mips.cpu_type.instrs_per_cycle;  
                 break;  
         }  
   
429          pc = machine->cpus[machine->bootstrap_cpu]->pc;          pc = machine->cpus[machine->bootstrap_cpu]->pc;
430    
431          gettimeofday(&tv, NULL);          gettimeofday(&tv, NULL);
# Line 452  void cpu_show_cycles(struct machine *mac Line 438  void cpu_show_cycles(struct machine *mac
438          if (mseconds - mseconds_last == 0)          if (mseconds - mseconds_last == 0)
439                  mseconds ++;                  mseconds ++;
440    
441          ninstrs = machine->ncycles_since_gettimeofday * instrs_per_cycle;          ninstrs = machine->ninstrs_since_gettimeofday;
442    
443          if (machine->automatic_clock_adjustment) {          if (machine->automatic_clock_adjustment) {
444                  static int first_adjustment = 1;                  static int first_adjustment = 1;
445    
446                  /*  Current nr of cycles per second:  */                  /*  Current nr of cycles per second:  */
447                  int64_t cur_cycles_per_second = 1000 *                  int64_t cur_cycles_per_second = 1000 *
448                      (ninstrs-ninstrs_last) / (mseconds-mseconds_last)                      (ninstrs-ninstrs_last) / (mseconds-mseconds_last);
449                      / instrs_per_cycle;  
450                    /*  fatal("[ CYCLES PER SECOND = %"PRIi64" ]\n",
451                        cur_cycles_per_second);  */
452    
453                  if (cur_cycles_per_second < 1000000)                  if (cur_cycles_per_second < 1000000)
454                          cur_cycles_per_second = 1000000;                          cur_cycles_per_second = 1000000;
# Line 473  void cpu_show_cycles(struct machine *mac Line 461  void cpu_show_cycles(struct machine *mac
461                              cur_cycles_per_second) / 16;                              cur_cycles_per_second) / 16;
462                  }                  }
463    
464                  /*  debug("[ updating emulated_hz to %lli Hz ]\n",                  /*  fatal("[ updating emulated_hz to %"PRIi64" Hz ]\n",
465                      (long long)machine->emulated_hz);  */                      machine->emulated_hz);  */
466          }          }
467    
468    
# Line 482  void cpu_show_cycles(struct machine *mac Line 470  void cpu_show_cycles(struct machine *mac
470          if (!machine->show_nr_of_instructions && !forced)          if (!machine->show_nr_of_instructions && !forced)
471                  goto do_return;                  goto do_return;
472    
473          printf("[ %lli instrs",          printf("[ %"PRIi64" instrs", (int64_t)machine->ninstrs);
             (long long)(machine->ncycles * instrs_per_cycle));  
474    
475          if (!machine->automatic_clock_adjustment) {          if (!machine->automatic_clock_adjustment) {
476                  d = machine->emulated_hz / 1000;                  d = machine->emulated_hz / 1000;
477                  if (d < 1)                  if (d < 1)
478                          d = 1;                          d = 1;
479                  ms = machine->ncycles / d;                  ms = machine->ninstrs / d;
480                  h = ms / 3600000;                  h = ms / 3600000;
481                  ms -= 3600000 * h;                  ms -= 3600000 * h;
482                  m = ms / 60000;                  m = ms / 60000;
# Line 497  void cpu_show_cycles(struct machine *mac Line 484  void cpu_show_cycles(struct machine *mac
484                  s = ms / 1000;                  s = ms / 1000;
485                  ms -= 1000 * s;                  ms -= 1000 * s;
486    
487                  printf("emulated time = %02i:%02i:%02i.%03i; ", h, m, s, ms);                  printf(", emulated time = %02i:%02i:%02i.%03i; ", h, m, s, ms);
488          }          }
489    
490          /*  Instructions per second, and average so far:  */          /*  Instructions per second, and average so far:  */
# Line 507  void cpu_show_cycles(struct machine *mac Line 494  void cpu_show_cycles(struct machine *mac
494                  is = 0;                  is = 0;
495          if (avg < 0)          if (avg < 0)
496                  avg = 0;                  avg = 0;
497          printf("; i/s=%lli avg=%lli", (long long)is, (long long)avg);          printf("; i/s=%"PRIi64" avg=%"PRIi64, is, avg);
498    
499          symbol = get_symbol_name(&machine->symbol_context, pc, &offset);          symbol = get_symbol_name(&machine->symbol_context, pc, &offset);
500    
501          if (machine->ncpus == 1) {          if (machine->ncpus == 1) {
502                  if (machine->cpus[machine->bootstrap_cpu]->is_32bit)                  if (machine->cpus[machine->bootstrap_cpu]->is_32bit)
503                          printf("; pc=0x%08x", (int)pc);                          printf("; pc=0x%08"PRIx32, (uint32_t) pc);
504                  else                  else
505                          printf("; pc=0x%016llx", (long long)pc);                          printf("; pc=0x%016"PRIx64, (uint64_t) pc);
506          }          }
507    
508          if (symbol != NULL)          if (symbol != NULL)
# Line 536  do_return: Line 523  do_return:
523   */   */
524  void cpu_run_init(struct machine *machine)  void cpu_run_init(struct machine *machine)
525  {  {
526          int ncpus = machine->ncpus;          machine->ninstrs_flush = 0;
527          int te;          machine->ninstrs = 0;
528            machine->ninstrs_show = 0;
         machine->a_few_cycles = 1048576;  
         machine->ncycles_flush = 0;  
         machine->ncycles = 0;  
         machine->ncycles_show = 0;  
   
         /*  
          *  Instead of doing { one cycle, check hardware ticks }, we  
          *  can do { n cycles, check hardware ticks }, as long as  
          *  n is at most as much as the lowest number of cycles/tick  
          *  for any hardware device.  
          */  
         for (te=0; te<machine->n_tick_entries; te++) {  
                 if (machine->ticks_reset_value[te] < machine->a_few_cycles)  
                         machine->a_few_cycles = machine->ticks_reset_value[te];  
         }  
   
         machine->a_few_cycles >>= 1;  
         if (machine->a_few_cycles < 1)  
                 machine->a_few_cycles = 1;  
   
         if (ncpus > 1 && machine->max_random_cycles_per_chunk == 0)  
                 machine->a_few_cycles = 1;  
   
         /*  debug("cpu_run_init(): a_few_cycles = %i\n",  
             machine->a_few_cycles);  */  
529    
530          /*  For performance measurement:  */          /*  For performance measurement:  */
531          gettimeofday(&machine->starttime, NULL);          gettimeofday(&machine->starttime, NULL);
532          machine->ncycles_since_gettimeofday = 0;          machine->ninstrs_since_gettimeofday = 0;
533  }  }
534    
535    
# Line 653  void cpu_init(void) Line 615  void cpu_init(void)
615          add_cpu_family(arm_cpu_family_init, ARCH_ARM);          add_cpu_family(arm_cpu_family_init, ARCH_ARM);
616  #endif  #endif
617    
618    #ifdef ENABLE_AVR
619            add_cpu_family(avr_cpu_family_init, ARCH_AVR);
620    #endif
621    
622    #ifdef ENABLE_HPPA
623            add_cpu_family(hppa_cpu_family_init, ARCH_HPPA);
624    #endif
625    
626    #ifdef ENABLE_I960
627            add_cpu_family(i960_cpu_family_init, ARCH_I960);
628    #endif
629    
630  #ifdef ENABLE_IA64  #ifdef ENABLE_IA64
631          add_cpu_family(ia64_cpu_family_init, ARCH_IA64);          add_cpu_family(ia64_cpu_family_init, ARCH_IA64);
632  #endif  #endif
# Line 669  void cpu_init(void) Line 643  void cpu_init(void)
643          add_cpu_family(ppc_cpu_family_init, ARCH_PPC);          add_cpu_family(ppc_cpu_family_init, ARCH_PPC);
644  #endif  #endif
645    
646    #ifdef ENABLE_SH
647            add_cpu_family(sh_cpu_family_init, ARCH_SH);
648    #endif
649    
650  #ifdef ENABLE_SPARC  #ifdef ENABLE_SPARC
651          add_cpu_family(sparc_cpu_family_init, ARCH_SPARC);          add_cpu_family(sparc_cpu_family_init, ARCH_SPARC);
652  #endif  #endif
653    
654    #ifdef ENABLE_TRANSPUTER
655            add_cpu_family(transputer_cpu_family_init, ARCH_TRANSPUTER);
656    #endif
657    
658  #ifdef ENABLE_X86  #ifdef ENABLE_X86
659          add_cpu_family(x86_cpu_family_init, ARCH_X86);          add_cpu_family(x86_cpu_family_init, ARCH_X86);
660  #endif  #endif

Legend:
Removed from v.12  
changed lines
  Added in v.28

  ViewVC Help
Powered by ViewVC 1.1.26