--- trunk/src/cpu.c 2007/10/08 16:18:11 6 +++ trunk/src/cpu.c 2007/10/08 16:18:27 10 @@ -25,7 +25,7 @@ * SUCH DAMAGE. * * - * $Id: cpu.c,v 1.295 2005/06/02 00:08:41 debug Exp $ + * $Id: cpu.c,v 1.298 2005/06/27 10:43:16 debug Exp $ * * Common routines for CPU emulation. (Not specific to any CPU type.) */ @@ -56,7 +56,7 @@ struct cpu *cpu_new(struct memory *mem, struct machine *machine, int cpu_id, char *name) { - struct cpu *c; + struct cpu *cpu; struct cpu_family *fp; char *cpu_type_name; @@ -71,19 +71,35 @@ exit(1); } + cpu = malloc(sizeof(struct cpu)); + if (cpu == NULL) { + fprintf(stderr, "out of memory\n"); + exit(1); + } + + memset(cpu, 0, sizeof(struct cpu)); + cpu->memory_rw = NULL; + cpu->name = cpu_type_name; + cpu->mem = mem; + cpu->machine = machine; + cpu->cpu_id = cpu_id; + cpu->byte_order = EMUL_LITTLE_ENDIAN; + cpu->bootstrap_cpu_flag = 0; + cpu->running = 0; + fp = first_cpu_family; while (fp != NULL) { if (fp->cpu_new != NULL) { - c = fp->cpu_new(mem, machine, cpu_id, cpu_type_name); - if (c != NULL) { - /* Some sanity-checks: */ - if (c->memory_rw == NULL) { - fatal("No memory_rw?\n"); + if (fp->cpu_new(cpu, mem, machine, cpu_id, + cpu_type_name)) { + /* Sanity check: */ + if (cpu->memory_rw == NULL) { + fatal("\ncpu_new(): memory_rw == " + "NULL\n"); exit(1); } - - return c; + return cpu; } } @@ -307,8 +323,7 @@ debug("cpu_run_deinit(): All CPUs halted.\n"); if (machine->show_nr_of_instructions || !quiet_mode) - cpu_show_cycles(machine, &machine->starttime, - machine->ncycles, 1); + cpu_show_cycles(machine, 1); if (show_opcode_statistics) cpu_show_full_statistics(machine); @@ -325,11 +340,10 @@ * line to stdout about how many instructions/cycles have been executed so * far. */ -void cpu_show_cycles(struct machine *machine, - struct timeval *starttime, int64_t ncycles, int forced) +void cpu_show_cycles(struct machine *machine, int forced) { uint64_t offset, pc; - int is_32bit = 0, instrs_per_cycle; + int is_32bit = 0, instrs_per_cycle = 1; char *symbol; int64_t mseconds, ninstrs; struct timeval tv; @@ -338,22 +352,25 @@ static int64_t mseconds_last = 0; static int64_t ninstrs_last = -1; - if (machine->arch != ARCH_MIPS) { - fatal("cpu_show_cycles(): not yet for !MIPS\n"); - return; + switch (machine->arch) { + case ARCH_MIPS: + if (machine->cpus[machine->bootstrap_cpu]->cd.mips. + cpu_type.isa_level < 3 || machine->cpus[machine-> + bootstrap_cpu]->cd.mips.cpu_type.isa_level == 32) + is_32bit = 1; + instrs_per_cycle = machine->cpus[machine->bootstrap_cpu]-> + cd.mips.cpu_type.instrs_per_cycle; + break; + case ARCH_ARM: + is_32bit = 1; + break; } - if (machine->cpus[machine->bootstrap_cpu]->cd.mips.cpu_type.isa_level - < 3 || machine->cpus[machine->bootstrap_cpu]->cd.mips.cpu_type. - isa_level == 32) - is_32bit = 1; pc = machine->cpus[machine->bootstrap_cpu]->pc; - instrs_per_cycle = machine->cpus[machine->bootstrap_cpu]-> - cd.mips.cpu_type.instrs_per_cycle; gettimeofday(&tv, NULL); - mseconds = (tv.tv_sec - starttime->tv_sec) * 1000 - + (tv.tv_usec - starttime->tv_usec) / 1000; + mseconds = (tv.tv_sec - machine->starttime.tv_sec) * 1000 + + (tv.tv_usec - machine->starttime.tv_usec) / 1000; if (mseconds == 0) mseconds = 1; @@ -361,7 +378,7 @@ if (mseconds - mseconds_last == 0) mseconds ++; - ninstrs = ncycles * instrs_per_cycle; + ninstrs = machine->ncycles_since_gettimeofday * instrs_per_cycle; if (machine->automatic_clock_adjustment) { static int first_adjustment = 1; @@ -391,14 +408,14 @@ if (!machine->show_nr_of_instructions && !forced) goto do_return; - - printf("[ "); + printf("[ %lli instrs", + (long long)(machine->ncycles * instrs_per_cycle)); if (!machine->automatic_clock_adjustment) { d = machine->emulated_hz / 1000; if (d < 1) d = 1; - ms = ncycles / d; + ms = machine->ncycles / d; h = ms / 3600000; ms -= 3600000 * h; m = ms / 60000; @@ -409,13 +426,8 @@ printf("emulated time = %02i:%02i:%02i.%03i; ", h, m, s, ms); } - printf("cycles=%lli", (long long) ncycles); - - if (instrs_per_cycle > 1) - printf(" (%lli instrs)", (long long) ninstrs); - /* Instructions per second, and average so far: */ - printf("; i/s=%lli avg=%lli", + printf("; i/s=%lli avg=%lli; ", (long long) ((long long)1000 * (ninstrs-ninstrs_last) / (mseconds-mseconds_last)), (long long) ((long long)1000 * ninstrs / mseconds)); @@ -423,11 +435,13 @@ symbol = get_symbol_name(&machine->symbol_context, pc, &offset); if (is_32bit) - printf("; pc=%08x", (int)pc); + printf("pc=0x%08x", (int)pc); else - printf("; pc=%016llx", (long long)pc); + printf("pc=0x%016llx", (long long)pc); - printf(" <%s> ]\n", symbol? symbol : "no symbol"); + if (symbol != NULL) + printf(" <%s>", symbol); + printf(" ]\n"); do_return: ninstrs_last = ninstrs; @@ -474,6 +488,7 @@ /* For performance measurement: */ gettimeofday(&machine->starttime, NULL); + machine->ncycles_since_gettimeofday = 0; } @@ -550,12 +565,9 @@ void cpu_init(void) { /* Note: These are registered in alphabetic order. */ - add_cpu_family(alpha_cpu_family_init, ARCH_ALPHA); add_cpu_family(arm_cpu_family_init, ARCH_ARM); - add_cpu_family(hppa_cpu_family_init, ARCH_HPPA); add_cpu_family(mips_cpu_family_init, ARCH_MIPS); add_cpu_family(ppc_cpu_family_init, ARCH_PPC); - add_cpu_family(sparc_cpu_family_init, ARCH_SPARC); add_cpu_family(urisc_cpu_family_init, ARCH_URISC); add_cpu_family(x86_cpu_family_init, ARCH_X86); }