--- trunk/src/main.c 2007/10/08 16:18:51 14 +++ trunk/src/main.c 2007/10/08 16:21:53 38 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2005 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: main.c,v 1.244 2005/09/07 07:41:29 debug Exp $ + * $Id: main.c,v 1.296 2007/03/31 15:11:26 debug Exp $ */ #include @@ -37,25 +37,35 @@ #include "console.h" #include "cpu.h" +#include "debugger.h" #include "device.h" #include "diskimage.h" #include "emul.h" #include "machine.h" #include "misc.h" +#include "settings.h" +#include "timer.h" + +#ifdef TEST_NATIVE_X86 +#include "native_x86.h" +#endif extern volatile int single_step; extern int force_debugger_at_exit; -extern int show_opcode_statistics; extern int optind; extern char *optarg; +struct settings *global_settings; + int extra_argc; char **extra_argv; char *progname; -int fully_deterministic = 0; +size_t dyntrans_cache_size = DEFAULT_DYNTRANS_CACHE_SIZE; +int native_code_translation_enabled = 0; +int skip_srandom_call = 0; /***************************************************************************** @@ -192,17 +202,19 @@ { printf("GXemul"); #ifdef VERSION - printf("-" VERSION); + printf(" " VERSION); #endif - printf(" Copyright (C) 2003-2005 Anders Gavare\n"); + printf(" Copyright (C) 2003-2007 Anders Gavare\n"); printf("Read the source code and/or documentation for " "other Copyright messages.\n"); printf("\nusage: %s [machine, other, and general options] [file " "[...]]\n", progname); printf(" or %s [general options] @configfile\n", progname); +#ifdef UNSTABLE_DEVEL printf(" or %s [userland, other, and general options] file " "[args ...]\n", progname); +#endif if (!longusage) { printf("\nRun %s -h for help on command line options.\n", @@ -217,13 +229,6 @@ "with -E.)\n"); printf("\nOther options:\n"); - printf(" -A disable alignment checks in some cases (for higher" - " speed)\n"); -#ifdef BINTRANS - printf(" -B disable dynamic binary translation. (translation" - " is turned on\n by default, if the host " - "supports it)\n"); -#endif printf(" -C x try to emulate a specific CPU. (Use -H to get a " "list of types.)\n"); printf(" -d fname add fname as a disk image. You can add \"xxx:\"" @@ -237,32 +242,38 @@ printf(" gH;S; set geometry to H heads and S" " sectors-per-track\n"); printf(" i IDE\n"); + printf(" oOFS; set base offset to OFS (for ISO9660" + " filesystems)\n"); printf(" r read-only (don't allow changes to the" " file)\n"); printf(" s SCSI\n"); printf(" t tape\n"); + printf(" V add an overlay\n"); printf(" 0-7 force a specific ID\n"); - printf(" -I x emulate clock interrupts at x Hz (affects" - " rtc devices only, not\n"); - printf(" actual runtime speed) (this disables automatic" - " clock adjustments)\n"); + printf(" -I hz set the main cpu frequency to hz (not used by " + "all combinations\n of machines and guest OSes)\n"); printf(" -i display each instruction as it is executed\n"); - printf(" -J disable some speed tricks\n"); - printf(" -j name set the name of the kernel, for example:\n"); - printf(" -j netbsd for NetBSD/pmax\n"); - printf(" -j bsd for OpenBSD/pmax\n"); - printf(" -j vmunix for Ultrix/RISC\n"); + printf(" -J disable dyntrans instruction combinations\n"); + printf(" -j name set the name of the kernel; for DECstation " + "emulation, this passes\n the name to the bootloader," + " for example:\n"); + printf(" -j netbsd (NetBSD/pmax) " + "-j bsd (OpenBSD/pmax)\n"); + printf(" -j vmsprite (Sprite/pmax) " + "-j vmunix (Ultrix/RISC)\n"); + printf(" For other emulation modes, if the boot disk is an" + " ISO9660\n filesystem, -j sets the name of the" + " kernel to load.\n"); printf(" -M m emulate m MBs of physical RAM\n"); - printf(" -m nr run at most nr instructions (on any cpu)\n"); printf(" -N display nr of instructions/second average, at" " regular intervals\n"); printf(" -n nr set nr of CPUs (for SMP experiments)\n"); printf(" -O force netboot (tftp instead of disk), even when" " a disk image is\n" " present (for DECstation, SGI, and ARC emulation)\n"); - printf(" -o arg set the boot argument (for DEC, ARC, or SGI" - " emulation).\n"); - printf(" Default arg for DEC is '-a', for ARC '-aN'.\n"); + printf(" -o arg set the boot argument, for DEC, ARC, or SGI" + " emulation\n"); + printf(" (default arg for DEC is -a, for ARC/SGI -aN)\n"); printf(" -p pc add a breakpoint (remember to use the '0x' " "prefix for hex!)\n"); printf(" -Q no built-in PROM emulation (use this for " @@ -271,40 +282,54 @@ printf(" -r register dumps before every instruction\n"); printf(" -S initialize emulated RAM to random bytes, " "instead of zeroes\n"); - printf(" -T enter the single-step debugger on " - "unimplemented memory accesses\n"); + printf(" -s f:name write statistics to file 'name', " + "f is one or more of the following:\n"); + printf(" v virtual program counter\n"); + printf(" p physical equivalent of program counter\n"); + printf(" i internal ic->f representation of " + "the program counter\n"); + printf(" and optionally:\n"); + printf(" d disable statistics gathering at " + "startup\n"); + printf(" o overwrite instead of append\n"); + printf(" -T halt on non-existant memory accesses\n"); printf(" -t show function trace tree\n"); printf(" -U enable slow_serial_interrupts_hack_for_linux\n"); #ifdef WITH_X11 printf(" -X use X11\n"); -#endif /* WITH_X11 */ printf(" -x open up new xterms for emulated serial ports " - "(default is on when\n using configuration files, off" - " otherwise)\n"); -#ifdef WITH_X11 + "(default is on when\n using configuration files or" + " when X11 is used, off otherwise)\n"); printf(" -Y n scale down framebuffer windows by n x n times\n"); #endif /* WITH_X11 */ - printf(" -y x set max_random_cycles_per_chunk to x" - " (experimental)\n"); printf(" -Z n set nr of graphics cards, for emulating a " "dual-head or tripple-head\n" " environment (only for DECstation emulation)\n"); printf(" -z disp add disp as an X11 display to use for " "framebuffers\n"); +#ifdef UNSTABLE_DEVEL printf("\nUserland options:\n"); printf(" -u emul userland-only (syscall) emulation (use -H to" " get a list of\n available emulation modes)\n"); +#endif printf("\nGeneral options:\n"); - printf(" -D guarantee fully deterministic behaviour\n"); +#ifdef UNSTABLE_DEVEL + printf(" -b enable native code generation\n"); + printf(" -B disable native code generation\n"); +#endif + printf(" -c cmd add cmd as a command to run before starting " + "the simulation\n"); + printf(" -D skip the srandom call at startup\n"); printf(" -H display a list of possible CPU and " "machine types\n"); printf(" -h display this help message\n"); + printf(" -k n set dyntrans translation caches to n MB (default" + " size is %i MB)\n", DEFAULT_DYNTRANS_CACHE_SIZE / 1048576); printf(" -K force the debugger to be entered at the end " "of a simulation\n"); printf(" -q quiet mode (don't print startup messages)\n"); - printf(" -s show opcode usage statistics after simulation\n"); printf(" -V start up in the single-step debugger, paused\n"); printf(" -v verbose debug messages\n"); printf("\n"); @@ -335,28 +360,58 @@ char ***diskimagesp, int *n_diskimagesp) { int ch, res, using_switch_d = 0, using_switch_Z = 0; + int using_switch_e = 0, using_switch_E = 0; char *type = NULL, *subtype = NULL; int n_cpus_set = 0; int msopts = 0; /* Machine-specific options used */ struct machine *m = emul_add_machine(emul, "default"); - while ((ch = getopt(argc, argv, "ABC:Dd:E:e:HhI:iJj:KM:m:" - "Nn:Oo:p:QqRrSsTtUu:VvW:XxY:y:Z:z:")) != -1) { + char *opts = +#ifdef UNSTABLE_DEVEL + "bB" +#endif + "C:c:Dd:E:e:HhI:iJj:k:KM:Nn:Oo:p:QqRrSs:TtU" +#ifdef UNSTABLE_DEVEL + "u:" +#endif + "VvW:" +#ifdef WITH_X11 + "XxY:" +#endif + "Z:z:"; + + while ((ch = getopt(argc, argv, opts)) != -1) { switch (ch) { - case 'A': - m->dyntrans_alignment_check = 0; - msopts = 1; +#ifdef UNSTABLE_DEVEL + case 'b': +#ifndef NATIVE_CODE_GENERATION + printf("-b is not available on this host arch.\n"); + exit(1); +#else + native_code_translation_enabled = 1; break; +#endif case 'B': - m->bintrans_enable = 0; - msopts = 1; + native_code_translation_enabled = 0; break; +#endif /* UNSTABLE_DEVEL */ case 'C': m->cpu_name = strdup(optarg); msopts = 1; break; + case 'c': + emul->n_debugger_cmds ++; + emul->debugger_cmds = realloc(emul->debugger_cmds, + emul->n_debugger_cmds * sizeof(char *)); + if (emul->debugger_cmds == NULL) { + fatal("out of memory\n"); + exit(1); + } + emul->debugger_cmds[emul->n_debugger_cmds-1] = + strdup(optarg); + break; case 'D': - fully_deterministic = 1; + skip_srandom_call = 1; break; case 'd': /* diskimage_add() is called further down */ @@ -372,10 +427,18 @@ msopts = 1; break; case 'E': + if (using_switch_E ++ > 0) { + fprintf(stderr, "-E already used.\n"); + exit(1); + } type = optarg; msopts = 1; break; case 'e': + if (using_switch_e ++ > 0) { + fprintf(stderr, "-e already used.\n"); + exit(1); + } subtype = optarg; msopts = 1; break; @@ -387,7 +450,6 @@ exit(1); case 'I': m->emulated_hz = atoi(optarg); - m->automatic_clock_adjustment = 0; msopts = 1; break; case 'i': @@ -395,7 +457,7 @@ msopts = 1; break; case 'J': - m->speed_tricks = 0; + m->allow_instruction_combinations = 0; msopts = 1; break; case 'j': @@ -406,6 +468,14 @@ } msopts = 1; break; + case 'k': + dyntrans_cache_size = atoi(optarg) * 1048576; + if (dyntrans_cache_size < 1) { + fprintf(stderr, "The dyntrans cache size must" + " be at least 1 MB.\n"); + exit(1); + } + break; case 'K': force_debugger_at_exit = 1; break; @@ -413,10 +483,6 @@ m->physical_ram_in_mb = atoi(optarg); msopts = 1; break; - case 'm': - m->max_instructions = atoi(optarg); - msopts = 1; - break; case 'N': m->show_nr_of_instructions = 1; msopts = 1; @@ -472,10 +538,12 @@ msopts = 1; break; case 's': - show_opcode_statistics = 1; + machine_statistics_init(m, optarg); + native_code_translation_enabled = 0; + msopts = 1; break; case 'T': - m->single_step_on_bad_addr = 1; + m->halt_on_nonexistant_memaccess = 1; msopts = 1; break; case 't': @@ -496,7 +564,7 @@ msopts = 1; break; case 'V': - single_step = 1; + single_step = ENTER_SINGLE_STEPPING; break; case 'v': verbose ++; @@ -507,22 +575,22 @@ case 'X': m->use_x11 = 1; msopts = 1; - break; + /* FALL-THROUGH */ case 'x': console_allow_slaves(1); break; case 'Y': m->x11_scaledown = atoi(optarg); + if (m->x11_scaledown < -1) { + m->x11_scaleup = - m->x11_scaledown; + m->x11_scaledown = 1; + } if (m->x11_scaledown < 1) { fprintf(stderr, "Invalid scaledown value.\n"); exit(1); } msopts = 1; break; - case 'y': - m->max_random_cycles_per_chunk = atoi(optarg); - msopts = 1; - break; case 'Z': m->n_gfx_cards = atoi(optarg); using_switch_Z = 1; @@ -594,12 +662,6 @@ quiet_mode = 0; } - if ((m->instruction_trace || m->register_dump || m->show_trace_tree) - && m->bintrans_enable) { - fprintf(stderr, "Implicitly turning off bintrans.\n"); - m->bintrans_enable = 0; - } - /* * Usually, an executable filename must be supplied. @@ -646,39 +708,9 @@ exit(1); } - if (m->bintrans_enable) { - m->speed_tricks = 0; - /* TODO: Print a warning about this? */ - } - - if (m->n_breakpoints > 0 && - m->bintrans_enable && m->arch == ARCH_MIPS) { - fprintf(stderr, "Breakpoints and MIPS binary translation " - "don't work too well together right now.\n"); - exit(1); - } - -#ifndef BINTRANS - if (m->bintrans_enable) { - fprintf(stderr, "WARNING: %s was compiled without " - "bintrans support. Ignoring -b.\n", progname); - m->bintrans_enable = 0; - } -#endif - -#ifndef WITH_X11 - if (m->use_x11) { - fprintf(stderr, "WARNING: %s was compiled without " - "X11 support. Ignoring -X.\n", progname); - m->use_x11 = 0; - } -#endif - if (!using_switch_Z && !m->use_x11) m->n_gfx_cards = 0; - m->bintrans_enabled_from_start = m->bintrans_enable; - return 0; } @@ -695,18 +727,83 @@ */ int main(int argc, char *argv[]) { + /* Setting constants: */ + const int constant_yes = 1; + const int constant_true = 1; + const int constant_no = 0; + const int constant_false = 0; + struct emul **emuls; char **diskimages = NULL; int n_diskimages = 0; int n_emuls; int i; + +/* + * Experimental profil() code, which works on my laptop. + * Don't use it for anything else. + */ +#ifdef USE_PROFIL + uint16_t samples[0x100000]; + memset(samples, 0, sizeof(samples)); + profil((char *)samples, sizeof(samples), (vm_offset_t) 0x400000, 8192); +#endif + +#ifndef NATIVE_CODE_GENERATION + native_code_translation_enabled = 0; +#endif + + +/* Experimental test code: */ +#ifdef TEST_NATIVE_X86 + test_native_x86(); +#endif + + + progname = argv[0]; + + /* + * Create the settings object, and add global settings to it: + * + * Read-only "constants": yes, no, true, false. + * Global emulator settings: verbose, single_step, ... + */ + global_settings = settings_new(); + + settings_add(global_settings, "yes", 0, SETTINGS_TYPE_INT, + SETTINGS_FORMAT_YESNO, (void *)&constant_yes); + settings_add(global_settings, "no", 0, SETTINGS_TYPE_INT, + SETTINGS_FORMAT_YESNO, (void *)&constant_no); + settings_add(global_settings, "true", 0, SETTINGS_TYPE_INT, + SETTINGS_FORMAT_BOOL, (void *)&constant_true); + settings_add(global_settings, "false", 0, SETTINGS_TYPE_INT, + SETTINGS_FORMAT_BOOL, (void *)&constant_false); + + /* Read-only settings: */ + settings_add(global_settings, "native_code_translation_enabled", 0, + SETTINGS_TYPE_INT, SETTINGS_FORMAT_YESNO, + (void *)&native_code_translation_enabled); + settings_add(global_settings, "single_step", 0, + SETTINGS_TYPE_INT, SETTINGS_FORMAT_YESNO, (void *)&single_step); + + /* Read/write settings: */ + settings_add(global_settings, "force_debugger_at_exit", 1, + SETTINGS_TYPE_INT, SETTINGS_FORMAT_YESNO, + (void *)&force_debugger_at_exit); + settings_add(global_settings, "verbose", 1, + SETTINGS_TYPE_INT, SETTINGS_FORMAT_YESNO, (void *)&verbose); + settings_add(global_settings, "quiet_mode", 1, + SETTINGS_TYPE_INT, SETTINGS_FORMAT_YESNO, (void *)&quiet_mode); + + /* Initialize all emulator subsystems: */ console_init(); cpu_init(); device_init(); machine_init(); + timer_init(); useremul_init(); emuls = malloc(sizeof(struct emul *)); @@ -717,37 +814,34 @@ /* Allocate space for a simple emul setup: */ n_emuls = 1; - emuls[0] = emul_new(NULL); + emuls[0] = emul_new(NULL, 0); if (emuls[0] == NULL) { fprintf(stderr, "out of memory\n"); exit(1); } + settings_add(global_settings, "emul[0]", 1, + SETTINGS_TYPE_SUBSETTINGS, 0, emuls[0]->settings); get_cmd_args(argc, argv, emuls[0], &diskimages, &n_diskimages); - if (!fully_deterministic) { - /* TODO: More than just time(). Use gettimeofday(). */ - srandom(time(NULL) ^ (getpid() << 12)); - } else { - /* Fully deterministic. -I must have been supplied. */ - if (emuls[0]->machines[0]->automatic_clock_adjustment) { - fatal("Cannot have -D without -I.\n"); - exit(1); - } + if (!skip_srandom_call) { + struct timeval tv; + gettimeofday(&tv, NULL); + srandom(tv.tv_sec ^ getpid() ^ tv.tv_usec); } /* Print startup message: */ debug("GXemul"); #ifdef VERSION - debug("-" VERSION); + debug(" " VERSION); #endif - debug(" Copyright (C) 2003-2005 Anders Gavare\n"); + debug(" Copyright (C) 2003-2007 Anders Gavare\n"); debug("Read the source code and/or documentation for " "other Copyright messages.\n\n"); - if (emuls[0]->machines[0]->machine_type == MACHINE_NONE) + if (emuls[0]->machines[0]->machine_type == MACHINE_NONE) { n_emuls --; - else { + } else { for (i=0; imachines[0], diskimages[i]); } @@ -772,6 +866,7 @@ /* Initialize emulations from config files: */ for (i=1; isettings); } } @@ -803,10 +911,37 @@ } device_set_exit_on_error(0); + console_warn_if_slaves_are_needed(1); + /* Run all emulations: */ emul_run(emuls, n_emuls); + + /* + * Deinitialize everything: + */ + + console_deinit(); + + for (i=0; i