--- trunk/src/emul.c 2007/10/08 16:20:10 26 +++ trunk/src/emul.c 2007/10/08 16:21:17 34 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2006 Anders Gavare. All rights reserved. + * Copyright (C) 2003-2007 Anders Gavare. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -25,7 +25,7 @@ * SUCH DAMAGE. * * - * $Id: emul.c,v 1.255 2006/06/24 19:52:27 debug Exp $ + * $Id: emul.c,v 1.278 2007/02/10 14:37:38 debug Exp $ * * Emulation startup and misc. routines. */ @@ -51,10 +51,15 @@ #include "mips_cpu_types.h" #include "misc.h" #include "net.h" +#include "settings.h" #include "sgi_arcbios.h" +#include "timer.h" #include "x11.h" +/* #define ISO_DEBUG */ + + extern int extra_argc; extern char **extra_argv; @@ -144,7 +149,7 @@ */ static void fix_console(void) { - console_deinit(); + console_deinit_main(); } @@ -221,7 +226,9 @@ dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) + ((uint64_t)buf[0x8f] << 24)) * 2048; - /* debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); */ +#ifdef ISO_DEBUG + debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); +#endif dirbuf = malloc(dirlen); if (dirbuf == NULL) { @@ -256,22 +263,30 @@ if (p == NULL) p = strchr(filename, '\\'); - /* debug("%i%s: %i, %i, \"", filenr, filenr == found_dir? - " [CURRENT]" : "", x, y); */ +#ifdef ISO_DEBUG + debug("%i%s: %i, %i, \"", filenr, filenr == found_dir? + " [CURRENT]" : "", x, y); +#endif for (i=0; imachine_type) { + + case MACHINE_DREAMCAST: + if (!diskimage_is_a_cdrom(cpu->machine, boot_disk_id, + boot_disk_type)) { + fatal("The Dreamcast emulation mode can only boot" + " from CD images, not from other disk types.\n"); + exit(1); + } + + bootblock_buf = malloc(32768); + if (bootblock_buf == NULL) { + fprintf(stderr, "Out of memory.\n"); + exit(1); + } + + debug("loading Dreamcast IP.BIN from %s id %i\n", + diskimage_types[boot_disk_type], boot_disk_id); + + res = diskimage_access(m, boot_disk_id, boot_disk_type, + 0, base_offset, bootblock_buf, 0x8000); + if (!res) { + fatal("Couldn't read the disk image. Aborting.\n"); + return 0; + } + + if (strncmp((char *)bootblock_buf, "SEGA ", 5) != 0) { + fatal("This is not a Dreamcast IP.BIN header.\n"); + free(bootblock_buf); + return 0; + } + + /* Store IP.BIN at 0x8c008000, and set entry point. */ + store_buf(cpu, 0x8c008000, (char *)bootblock_buf, 32768); + cpu->pc = 0x8c008300; + + /* Remember the name of the file to boot (1ST_READ.BIN): */ + if (cpu->machine->boot_kernel_filename == NULL || + cpu->machine->boot_kernel_filename[0] == '\0') { + int i = 0x60; + while (i < 0x70) { + if (bootblock_buf[i] == ' ') + bootblock_buf[i] = 0; + i ++; + } + cpu->machine->boot_kernel_filename = strdup( + (char *)bootblock_buf + 0x60); + } + + debug("boot filename: %s\n", + cpu->machine->boot_kernel_filename); + + free(bootblock_buf); + + break; + case MACHINE_PMAX: /* * The first few bytes of a disk contains information about @@ -563,9 +635,18 @@ /* 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 & 0xf0000000ULL) != 0xa0000000) { + fatal("\nWARNING! Weird load address 0x%08"PRIx32 + " for SCSI id %i.\n\n", + (uint32_t)bootblock_loadaddr, boot_disk_id); + if (bootblock_loadaddr == 0) { + fatal("I'm assuming that this is _not_ a " + "DEC bootblock.\nAre you sure you are" + " booting from the correct disk?\n"); + exit(1); + } + } + bootblock_loadaddr &= 0x0fffffffULL; bootblock_loadaddr |= 0xffffffffa0000000ULL; @@ -630,36 +711,6 @@ 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; - - bootblock_buf = malloc(512); - if (bootblock_buf == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(1); - } - - debug("loading PC bootsector from %s id %i\n", - diskimage_types[boot_disk_type], boot_disk_id); - - 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; } @@ -667,7 +718,7 @@ * Try reading a kernel manually from the disk. The code here * does not rely on machine-dependent boot blocks etc. */ - /* ISO9660: (0x800 bytes at 0x8000) */ + /* ISO9660: (0x800 bytes at 0x8000 + base_offset) */ bootblock_buf = malloc(0x800); if (bootblock_buf == NULL) { fprintf(stderr, "Out of memory.\n"); @@ -675,7 +726,7 @@ } res = diskimage_access(m, boot_disk_id, boot_disk_type, - 0, 0x8000, bootblock_buf, 0x800); + 0, base_offset + 0x8000, bootblock_buf, 0x800); if (!res) { fatal("Couldn't read the disk image. Aborting.\n"); return 0; @@ -690,8 +741,10 @@ iso_type = 3; if (iso_type != 0) { - /* We can't load a kernel if the name - isn't specified. */ + /* + * If the user specified a kernel name, then load it from + * disk. + */ if (cpu->machine->boot_kernel_filename == NULL || cpu->machine->boot_kernel_filename[0] == '\0') fatal("\nISO9660 filesystem, but no kernel " @@ -714,8 +767,6 @@ } if (bootblock_buf[0x000] == 'E' && bootblock_buf[0x001] == 'R' && bootblock_buf[0x200] == 'P' && bootblock_buf[0x201] == 'M') { - /* 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("\nApple partition table, but no kernel " @@ -736,7 +787,7 @@ * * Returns a reasonably initialized struct emul. */ -struct emul *emul_new(char *name) +struct emul *emul_new(char *name, int id) { struct emul *e; e = malloc(sizeof(struct emul)); @@ -747,6 +798,21 @@ memset(e, 0, sizeof(struct emul)); + e->path = malloc(15); + if (e->path == NULL) { + fprintf(stderr, "out of memory\n"); + exit(1); + } + snprintf(e->path, 15, "emul[%i]", id); + + e->settings = settings_new(); + + settings_add(e->settings, "n_machines", 0, + SETTINGS_TYPE_INT, SETTINGS_FORMAT_DECIMAL, + (void *) &e->n_machines); + + /* TODO: More settings? */ + /* Sane default values: */ e->n_machines = 0; e->next_serial_nr = 1; @@ -757,6 +823,10 @@ fprintf(stderr, "out of memory in emul_new()\n"); exit(1); } + + settings_add(e->settings, "name", 0, + SETTINGS_TYPE_STRING, SETTINGS_FORMAT_STRING, + (void *) &e->name); } return e; @@ -764,6 +834,34 @@ /* + * emul_destroy(): + * + * Destroys a previously created emul object. + */ +void emul_destroy(struct emul *emul) +{ + int i; + + if (emul->name != NULL) { + settings_remove(emul->settings, "name"); + free(emul->name); + } + + for (i=0; in_machines; i++) + machine_destroy(emul->machines[i]); + + if (emul->machines != NULL) + free(emul->machines); + + /* Remove any remaining level-1 settings: */ + settings_remove_all(emul->settings); + settings_destroy(emul->settings); + + free(emul); +} + + +/* * emul_add_machine(): * * Calls machine_new(), adds the new machine into the emul struct, and @@ -774,10 +872,14 @@ struct machine *emul_add_machine(struct emul *e, char *name) { struct machine *m; + char tmpstr[20]; + int i; - m = machine_new(name, e); + m = machine_new(name, e, e->n_machines); m->serial_nr = (e->next_serial_nr ++); + i = e->n_machines; + e->n_machines ++; e->machines = realloc(e->machines, sizeof(struct machine *) * e->n_machines); @@ -786,7 +888,12 @@ exit(1); } - e->machines[e->n_machines - 1] = m; + e->machines[i] = m; + + snprintf(tmpstr, sizeof(tmpstr), "machine[%i]", i); + settings_add(e->settings, tmpstr, 1, SETTINGS_TYPE_SUBSETTINGS, 0, + e->machines[i]->settings); + return m; } @@ -1182,20 +1289,6 @@ } } - /* 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; - } - byte_order = NO_BYTE_ORDER_OVERRIDE; /* @@ -1237,13 +1330,8 @@ } break; - case ARCH_HPPA: - break; - - case ARCH_I960: - break; - - case ARCH_IA64: + case ARCH_RCA180X: + cpu->pc &= 0xffff; break; case ARCH_M68K: @@ -1271,7 +1359,7 @@ break; case ARCH_SH: - if (cpu->cd.sh.bits == 32) + if (cpu->cd.sh.cpu_type.bits == 32) cpu->pc &= 0xffffffffULL; cpu->pc &= ~1; break; @@ -1279,22 +1367,8 @@ case ARCH_SPARC: break; - case ARCH_X86: - /* - * 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); - } + case ARCH_TRANSPUTER: + cpu->pc &= 0xffffffffULL; break; default: @@ -1332,8 +1406,7 @@ useremul_setup(cpu, n_load, load_names); /* Startup the bootstrap CPU: */ - cpu->bootstrap_cpu_flag = 1; - cpu->running = 1; + cpu->running = 1; /* ... or pause all CPUs, if start_paused is set: */ if (m->start_paused) { @@ -1394,11 +1467,6 @@ debug("0x%016llx", (long long)entrypoint); break; - case ARCH_X86: - debug("0x%04x:0x%llx", cpu->cd.x86.s[X86_S_CS], - (long long)cpu->pc); - break; - default: if (cpu->is_32bit) debug("0x%08x", (int)cpu->pc); @@ -1463,7 +1531,9 @@ /* Create a simple network: */ emul->net = net_init(emul, NET_INIT_FLAG_GATEWAY, - "10.0.0.0", 8, NULL, 0, 0); + NET_DEFAULT_IPV4_MASK, + NET_DEFAULT_IPV4_LEN, + NULL, 0, 0, NULL); } else { /* Userland pseudo-machine: */ debug("Syscall emulation (userland-only) setup...\n"); @@ -1482,10 +1552,10 @@ * * Create an emul struct by reading settings from a configuration file. */ -struct emul *emul_create_from_configfile(char *fname) +struct emul *emul_create_from_configfile(char *fname, int id) { int iadd = DEBUG_INDENTATION; - struct emul *e = emul_new(fname); + struct emul *e = emul_new(fname, id); debug("Creating emulation from configfile \"%s\":\n", fname); debug_indentation(iadd); @@ -1572,6 +1642,9 @@ cpu_functioncall_trace(emuls[0]->machines[0]->cpus[0], emuls[0]->machines[0]->cpus[0]->pc); + /* Start emulated clocks: */ + timer_start(); + /* * MAIN LOOP: * @@ -1582,22 +1655,22 @@ go = 0; /* Flush X11 and serial console output every now and then: */ - if (emuls[0]->machines[0]->ncycles > - emuls[0]->machines[0]->ncycles_flush + (1<<18)) { + if (emuls[0]->machines[0]->ninstrs > + emuls[0]->machines[0]->ninstrs_flush + (1<<19)) { x11_check_event(emuls, n_emuls); console_flush(); - emuls[0]->machines[0]->ncycles_flush = - emuls[0]->machines[0]->ncycles; + emuls[0]->machines[0]->ninstrs_flush = + emuls[0]->machines[0]->ninstrs; } - if (emuls[0]->machines[0]->ncycles > - emuls[0]->machines[0]->ncycles_show + (1<<25)) { - emuls[0]->machines[0]->ncycles_since_gettimeofday += - (emuls[0]->machines[0]->ncycles - - emuls[0]->machines[0]->ncycles_show); + if (emuls[0]->machines[0]->ninstrs > + emuls[0]->machines[0]->ninstrs_show + (1<<25)) { + emuls[0]->machines[0]->ninstrs_since_gettimeofday += + (emuls[0]->machines[0]->ninstrs - + emuls[0]->machines[0]->ninstrs_show); cpu_show_cycles(emuls[0]->machines[0], 0); - emuls[0]->machines[0]->ncycles_show = - emuls[0]->machines[0]->ncycles; + emuls[0]->machines[0]->ninstrs_show = + emuls[0]->machines[0]->ninstrs; } if (single_step == ENTER_SINGLE_STEPPING) { @@ -1616,18 +1689,24 @@ if (single_step == SINGLE_STEPPING) debugger(); - e = emuls[0]; /* Note: Only 1 emul supported now. */ + for (i=0; in_machines; j++) { - if (e->machines[j]->gdb.port > 0) - debugger_gdb_check_incoming(e->machines[j]); - - anything = machine_run(e->machines[j]); - if (anything) - go = 1; + for (j=0; jn_machines; j++) { + if (e->machines[j]->gdb.port > 0) + debugger_gdb_check_incoming( + e->machines[j]); + + anything = machine_run(e->machines[j]); + if (anything) + go = 1; + } } } + /* Stop any running timers: */ + timer_stop(); + /* Deinitialize all CPUs in all machines in all emulations: */ for (i=0; in_machines; j++) @@ -1654,11 +1733,11 @@ printf("Press enter to quit.\n"); while (!console_charavail(MAIN_CONSOLE)) { x11_check_event(emuls, n_emuls); - usleep(1); + usleep(10000); } console_readchar(MAIN_CONSOLE); } - console_deinit(); + console_deinit_main(); }