URL
https://opencores.org/ocsvn/or1k/or1k/trunk
Subversion Repositories or1k
[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [toplevel.c] - Rev 427
Go to most recent revision | Compare with Previous | Blame | View Log
/* toplevel.c -- Top level simulator source file Copyright (C) 1999 Damjan Lampret, lampret@opencores.org This file is part of OpenRISC 1000 Architectural Simulator. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* Simulator commands. Help and version output. SIGINT processing. Stdout redirection is specific to linux (I need to fix this). */ #include "config.h" #include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <stdarg.h> #include <fcntl.h> #ifdef HAVE_LIBREADLINE #include <readline/readline.h> #include <readline/history.h> #endif /* HAVE_LIBREADLINE */ #include "arch.h" #include "parse.h" #include "abstract.h" #include "labels.h" #include "trace.h" #include "execute.h" #include "sim-config.h" #include "spr_defs.h" #include "dma.h" #include "vapi.h" #include "coff.h" #include "gdbcomm.h" /* CVS revision number. */ const char rcsrev[] = "$Revision: 1.51 $"; /* Continuos run versus single step tracing switch. */ int cont_run; /* History of execution */ int histexec[HISTEXEC_LEN]; char *sim_commands [] = { "q", "t", "help", "de", "dm", "run", "pr", "pm", "pc", "reset", "break", "hist", "stats", "stall" "info", "r", "dv", "set", 0 }; inline void debug(int level, const char *format, ...) { char *p; va_list ap; if (config.sim.debug >= level) { if ((p = malloc(1000)) == NULL) return; va_start(ap, format); (void) vsnprintf(p, 1000, format, ap); va_end(ap); printf("%s", p); fflush(stdout); free(p); } else { #if DEBUG if ((p = malloc(1000)) == NULL) return; va_start(ap, format); (void) vsnprintf(p, 1000, format, ap); va_end(ap); printf("%s\n", p); fflush(stdout); free(p); #endif } } void ctrl_c(signum) int signum; { extern int cpu_stalled; /* CZ from debug_interface */ cont_run = cpu_stalled ? 0 : 1; config.sim.iprompt = 1; cpu_stalled = 0; signal(SIGINT, ctrl_c); } void version() { printf ("\n"); printf ("OpenRISC 1000 (OR32) Architectural Simulator, %s\n", rcsrev); printf ("Copyright (C) 1999 Damjan Lampret, lampret@opencores.org\n"); printf ("Copyright (C) 2000 Damjan Lampret, lampret@opencores.org\n"); printf (" Jimmy Chen-Min Chen, jimmy@ee.nctu.edu.tw\n"); printf (" Johan Rydberg, johan.rydberg@insight.se\n"); printf (" Marko Mlinar, markom@opencores.org\n"); printf ("Copyright (C) 2001 Simon Srot, simons@opencores.org\n"); printf (" Marko Mlinar, markom@opencores.org\n"); printf ("Visit http://www.opencores.org for more information about "); printf ("OpenRISC 1000 and\nother open source cores.\n\n"); printf ("This software comes with ABSOLUTELY NO WARRANTY; for "); printf ("details see COPYING.\nThis is free software, and you "); printf ("are welcome to redistribute it under certain\nconditions; "); printf ("for details see COPYING.\n"); } void help() { printf("q - quit simulator\n"); printf("r - display all registers\n"); printf("t - execute next instruction\n"); printf("run <cycles> [<hush>] - execute <cycles> instructions, no reg dump if hush\n"); printf("pr <r> <value> - patch register <r> with <value>\n"); printf("dm <fromaddr> [<toaddr>] - display memory from <fromaddr> to <toaddr>\n"); printf("de <fromaddr> [<toaddr>] - debug insn memory\n"); printf("pm <addr> <value> - patch memory location <addr> with <value>\n"); printf("pc <value> - patch PC register with <value>\n"); printf("break <addr> - toggle breakpoint at address <addr>\n"); printf("reset - simulator reset\n"); printf("hist - execution history\n"); printf("stall - stalls the processor and gives control to the debugger\n"); printf("stats <num|clear> - execution statistics num or clear it.\n"); printf("info - configuration info (caches etc.)\n"); printf("dv <fromaddr> [<toaddr>] [<modname>] - dumps memory as verilog (use redirect)\n"); printf("dh <fromaddr> [<toaddr>] - dumps memory as hex code (use redirect)\n"); printf("set <section> <item> = <param> - set configuration. See sim.cfg for more information.\n"); printf("<cmd> > <filename> - redirect simulator stdout to <filename> (and not emulated printf)\n"); printf("debug - toggles simulator debug mode\n"); printf("help - available commands (this list)\n"); } void debugmem( unsigned long from, unsigned long to ); /* Initalizes all devices and sim */ void sim_init () { init_memory_table (); init_labels(); init_breakpoints(); initstats(); build_automata(); if (GDB_ENABLED) gdbcomm_init (); if (config.sim.profile) { runtime.sim.fprof = fopen(config.sim.prof_fn, "wt+"); if(!runtime.sim.fprof) { config.sim.profile = 0; fprintf(stderr, "WARNING: Problems opening profile file. Profiling disabled. \n"); } else fprintf(runtime.sim.fprof, "+00000000 FFFFFFFF FFFFFFFF main\n"); } if (config.sim.exe_log) { runtime.sim.fexe_log = fopen(config.sim.exe_log_fn, "wt+"); if(!runtime.sim.fexe_log) { config.sim.exe_log = 0; printf("WARNING: Problems opening exe_log file. Execution logging disabled. \n"); } } /* Initialize memory */ { extern struct dev_memarea *dev_list; int i; if (config.memory.type == MT_RANDOM) { unsigned int val = 0; if (config.memory.random_seed == -1) { config.memory.random_seed = time(NULL); /* Print out the seed just in case we ever need to debug */ printf("Seeding random generator with value %d\n", config.memory.random_seed); } srandom(config.memory.random_seed); for (cur_area = dev_list; cur_area; cur_area = cur_area->next) for(i = 0; i < cur_area->size; i++) { val = random(); setsim_mem8(i + cur_area->addr_compare, val & 0xFF); } } else if(config.memory.type == MT_PATTERN) { for (cur_area = dev_list; cur_area; cur_area = cur_area->next) for(i = 0; i < cur_area->size; i++) setsim_mem8(i + cur_area->addr_compare, config.memory.pattern); } else if (config.memory.type != MT_UNKNOWN) { fprintf(stderr, "Invalid memory configuration type.\n"); exit(1); } } if(runtime.sim.filename) { unsigned long endaddr = 0xFFFFFFFF; endaddr = loadcode(runtime.sim.filename, 0, 0); /* MM170901 always load at address zero. */ if (endaddr == -1) { fprintf(stderr, "Problems loading boot code.\n"); exit(1); } } /* Disable gdb debugging, if debug module is not available. */ if (config.debug.gdb_enabled && !config.debug.enabled) { config.debug.gdb_enabled = 0; if (config.sim.verbose) fprintf (stderr, "WARNING: Debug module not enabled, cannot start gdb.\n"); } /* Enable dependency stats, if we want to do history analisis */ if (config.sim.history && !config.cpu.dependstats) { config.cpu.dependstats = 1; if (config.sim.verbose) fprintf (stderr, "WARNING: dependstats stats must be enabled to do history analisis.\n"); } /* Debug forces verbose */ if (config.sim.debug && !config.sim.verbose) { config.sim.verbose = 1; fprintf (stderr, "WARNING: verbose turned on.\n"); } /* Start VAPI before device initialization. */ if (config.vapi.enabled) { vapi_init (); if (config.sim.verbose) printf ("VAPI started, waiting for clients.\n"); } lock_memory_table (); uart_reset(); dma_reset(); eth_reset(); tick_reset(); pm_reset(); pic_reset(); mc_reset(); reset(); /* Wait till all test are connected. */ if (config.vapi.enabled) { int numu = vapi_num_unconnected (0); if (numu) { printf ("\nWaiting for VAPI tests with ids:\n"); vapi_num_unconnected (1); printf ("\n"); while (numu = vapi_num_unconnected (0)) { vapi_check (); printf ("\rStill waiting for %i VAPI test(s) to connect. ", numu); usleep (100); } printf ("\n"); } printf ("All devices connected \n"); } /* simulator is initialized */ runtime.sim.init = 0; } /* Display info about various modules */ void sim_info () { sprs_status(); memory_table_status (); itlb_status(-1); dtlb_status(-1); ic_info(); dc_info(); if (config.cpu.bpb) bpb_info(); if (config.cpu.btic) btic_info(); if (config.uarts_enabled) uart_status(); if (config.dmas_enabled) dma_status(); if (config.ethernets_enabled) eth_status(); } /* Cleanup */ void sim_done () { if (config.sim.profile) { extern int cycles; fprintf(runtime.sim.fprof,"-%08X FFFFFFFF\n", cycles); fclose(runtime.sim.fprof); } if (config.sim.exe_log) fclose(runtime.sim.fexe_log); if (config.vapi.enabled) vapi_done (); done_memory_table (); exit(0); } int main(argc, argv) int argc; char *argv[]; { char *linestr; char item1[500], b2[500], prev_str[500] = ""; char *redirstr; int hush = 0; int first_prompt = 1; int trace_fd = 0; srand(getpid()); init_defconfig(); if (parse_args(argc, argv)) { printf("Usage: %s [options] <filename>\n", argv[0]); printf("Options:\n"); printf(" -v version and copyright note\n"); printf(" -i enable interactive command prompt\n"); printf(" -f or --file load script file [sim.cfg]\n"); printf(" --nosrv do not launch JTAG proxy server\n"); /* (CZ) */ printf(" --srv <n> launch JTAG proxy server on port <n>; [random]\n"); /* (CZ) */ printf(" --profile enable profiling\n"); exit(-1); } #ifdef HAVE_LIBREADLINE initialize_readline (); /* Bind our completer. */ #endif /* Read configuration file. */ if (!runtime.sim.script_file_specified) read_script_file ("sim.cfg"); if (!runtime.sim.script_file_specified && config.sim.verbose) fprintf (stderr, "WARNING: No config file read, assuming default configuration.\n"); print_config(); sim_init (); signal(SIGINT, ctrl_c); while(1) { if (config.sim.iprompt) { if (GDB_ENABLED) { printf ("(sim) "); fflush(stdout); HandleServerSocket(true); /* block & check_stdin = true */ } #ifdef HAVE_LIBREADLINE /* Must disable readline in new mode. It isn't compatible with non blocking environments */ if(!GDB_ENABLED) linestr = readline("(sim) "); else linestr = fgets(b2, sizeof b2, stdin); #else if(!GDB_ENABLED) printf ("(sim) "); linestr = fgets(b2, sizeof b2, stdin); #endif } else strcpy(linestr = b2, "run -1 hush"); if (!linestr) break; linestr = stripwhite (linestr); #ifdef HAVE_LIBREADLINE /* Readline only works in the old mode */ if(!server_fd) { if (strlen(linestr) == 0) { char *l = repeat_last_command (); if (l) { free (linestr); linestr = l; } } if (*linestr) { add_history (linestr); } } #endif /* HAVE_LIBREADLINE */ if (redirstr = strstr(linestr, ">")) { *redirstr = '\0'; strtoken(&redirstr[1], item1, 1); freopen(item1, "w+", stdout); } if (linestr[0] == '\n') strcpy (linestr, &prev_str[0]); else strcpy (&prev_str[0], linestr); strtoken(linestr, item1, 1); if (strcmp(item1, "q") == 0) { /* quit */ printf ("\n"); sim_done (); } else if (strcmp(item1, "help") == 0) /* help */ help(); else if (strcmp(item1, "t") == 0) { /* trace */ cont_run = 1; } else if (strcmp(item1, "dm") == 0) { /* dump memory */ char item2[20]; char item3[20]; static int from = 0, to = 0; strtoken(linestr, item2, 2); strtoken(linestr, item3, 3); if (strlen(item2)) { if (item2[0] == '_') from = eval_label(item2); else from = strtoul(item2, NULL, 0); to = from + 0x40; } if (strlen(item3)) to = strtoul(item3, NULL, 0); dumpmemory(from, to, 0, 1); printf("\n"); } else if (strcmp(item1, "dv") == 0) {/* dump memory as verilog*/ char item2[20]; char item3[20]; char item4[20]; static int from = 0, to = 0; strtoken(linestr, item2, 2); strtoken(linestr, item3, 3); strtoken(linestr, item4, 4); if (strlen(item2)) { if (item2[0] == '_') from = eval_label(item2); else from = strtoul(item2, NULL, 0); to = from + 0x40; } if (strlen(item3)) to = strtoul(item3, NULL, 0); if (!strlen(item4)) strcpy(item4, "or1k_mem"); dumpverilog(item4, from, to); printf("\n"); } else if (strcmp(item1, "dh") == 0) {/* dump memory as hex*/ char item2[20]; char item3[20]; static int from = 0, to = 0; strtoken(linestr, item2, 2); strtoken(linestr, item3, 3); if (strlen(item2)) { if (item2[0] == '_') from = eval_label(item2); else from = strtoul(item2, NULL, 0); to = from + 0x40; } if (strlen(item3)) to = strtoul(item3, NULL, 0); dumphex(from, to); printf("\n"); } else if (strcmp(item1, "pm") == 0) { /* patch memory */ char item2[20]; char item3[20]; static int addr = 0; int breakpoint = 0; strtoken(linestr, item2, 2); strtoken(linestr, item3, 3); if (strlen(item2)) if (item2[0] == '_') addr = eval_label(item2); else addr = strtoul(item2, NULL, 0); set_mem32(addr, strtoul(item3, NULL, 0), &breakpoint); } else if (strcmp(item1, "pr") == 0) { /* patch regs */ char item2[20]; char item3[20]; strtoken(linestr, item2, 2); strtoken(linestr, item3, 3); set_reg32(strtoul(item2, NULL,0), strtoul(item3, NULL, 0)); } else if (strcmp(item1, "pc") == 0) { /* patch PC */ char item2[20]; strtoken(linestr, item2, 2); pcnext = strtoul(item2, NULL, 0); } else if (strcmp(item1, "break") == 0) { /* set/clear breakpoint */ char item2[20]; strtoken(linestr, item2, 2); set_insnbrkpoint(strtoul(item2, NULL, 0)); } else if (strcmp(item1, "r") == 0) { /* dump regs */ dumpreg(); } else if (strcmp(item1, "de") == 0) { /* reset simulator */ char item2[20]; char item3[20]; static int from = 0, to = 0; strtoken(linestr, item2, 2); strtoken(linestr, item3, 3); if (strlen(item2)) { if (item2[0] == '_') from = eval_label(item2); else from = strtoul(item2, NULL, 0); to = from + 0x40; } if (strlen(item3)) to = strtoul(item3, NULL, 0); debugmem(from, to); printf("\n"); } else if (strcmp(item1, "reset") == 0) { /* reset simulator */ uart_reset(); dma_reset(); eth_reset(); tick_reset(); pm_reset(); pic_reset(); reset(); /* Old or new mode */ } else if (strcmp(item1, "debug") == 0) { /* debug mode */ config.sim.debug ^= 1; } else if (strcmp(item1, "hist") == 0) { /* dump history */ int i; for(i = HISTEXEC_LEN; i; i--) dumpmemory(histexec[i - 1], histexec[i - 1] + 4, 1, 1); printf("\n"); } else if (strcmp(item1, "run") == 0) { /* run */ char item2[20]; char item3[20]; strtoken(linestr, item2, 2); strtoken(linestr, item3, 3); if (strcmp(item3, "hush") == 0) hush = 1; else hush = 0; cont_run = strtol(item2, NULL, 0); } else if(!strcmp(item1, "stall")) { /* Added by CZ 210801 */ extern int cpu_stalled; /* CZ from debug_interface */ cpu_stalled = 1; config.sim.iprompt = 0; cont_run = -1; hush = 1; } else if (!strcmp(item1, "trace")) { /* Added by CZ 210801 */ char item2[256]; strtoken(linestr, item2, 2); if(trace_fd) { close(trace_fd); trace_fd = 0; } if(strcmp(item2,"off")) /* if we're not being turned off */ { if(item2[0]) trace_fd = open(item2,O_CREAT | O_NOCTTY | O_TRUNC | O_WRONLY, 0644); else trace_fd = dup(1); if(trace_fd < 0) { perror(item2[0]?item2:"stdout"); trace_fd = 0; } } } else if (strcmp(item1, "stats") == 0) { /* stats */ char item2[20]; int i = 0; strtoken(linestr, item2, 2); if (strcmp(item2, "clear") == 0) { initstats(); printf("Cleared.\n"); } else { i = strtoul(item2, NULL, 0); printstats(i); } } else if (strcmp(item1, "info") == 0) /* configuration info */ sim_info (); else if (strcmp(item1, "set") == 0) { /* configuration info */ char *s = linestr; int i; extern section; extern struct section sections[]; while (*s != ' ' && *s) s++; set_config_command (s); } else printf("%s: Unknown command.\n", linestr); /* MM: 'run -1' means endless execution. */ while(cont_run != 0) { int debug_slowdown = DEBUG_SLOWDOWN; extern int cycle_delay; /* Added by CZ 27/05/01. Set during exception. */ extern int cpu_stalled; /* CZ from debug_interface */ if (cpu_stalled) if(GDB_ENABLED) { BlockJTAG(); HandleServerSocket(false); continue; } else fprintf (stderr, "WARNING: CPU stalled and gdb connection not enabled."); if (!testsprbits(SPR_PMR, SPR_PMR_DME | SPR_PMR_SME)) { if(cycle_delay <= 0) { unsigned int addr; if (cont_run > 0) cont_run--; if(fetch()) { printf ("Breakpoint hit.\n"); cont_run = 0; /* memory breakpoint encountered */ break; } addr = iqueue[0].insn_addr; /* If trace_fd is non zero, we want to make sure that disassemble is called */ decode_execute(&iqueue[0],trace_fd); if(trace_fd) { char sTemp[256]; char sTemp2[256]; unsigned long value; extern char *disassembled; extern unsigned long reg[]; /* The objects passed to the trace command should not be hardcoded like this...instead what to dump should be passed on the command line. FIX THIS LATER... */ value = (evalsim_mem8(0x306bc) << 24) + (evalsim_mem8(0x306bd) << 16) + (evalsim_mem8(0x306be) << 8) + evalsim_mem8(0x306bf); sprintf(sTemp,"0x%06x: %s",addr,disassembled); memset(sTemp2,' ',sizeof(sTemp2)); strncpy(sTemp2,sTemp,strlen(sTemp)); sprintf(&sTemp2[40],"<0x%08x,0x%08x> [0x%08x]\n", reg[3],reg[4],value); write(trace_fd,sTemp2,strlen(sTemp2)); } update_pc(); analysis(); if (!hush) dumpreg(); } else cycle_delay--; pic_clock(); dc_clock(); ic_clock(); if (!testsprbits(SPR_PMR, SPR_PMR_SME)) tick_clock(); } pm_clock(); if (config.uarts_enabled) uart_clock(); if (config.dmas_enabled) dma_clock(); if (config.ethernets_enabled) eth_clock(); if (config.sim.exe_log) dump_exe_log(); if (config.vapi.enabled) vapi_check(); if (GDB_ENABLED && debug_slowdown-- == 0) { debug_slowdown = DEBUG_SLOWDOWN; HandleServerSocket(false); /* block & check_stdin = false */ } } hush = 0; fflush(stdout); freopen("/dev/fd/0", "w+", stdout); if (!config.sim.iprompt) /* non-interactive quit */ sim_done(); #ifdef HAVE_LIBREADLINE if (linestr) free (linestr); #endif } sim_done(); } #ifdef HAVE_LIBREADLINE char *command_generator (); char **sim_completion (); /* Tell the GNU readline library how to complete. We want to try to complete on command names if this is the first word in the line, or on filenames if not. */ void initialize_readline () { /* Allow conditional parsing of the ~/.inputrc file. */ rl_readline_name = "or1ksim"; /* Tell the completer that we want a crack first. */ rl_attempted_completion_function = (CPPFunction *)sim_completion; } /* Attempt to complete on the contents of TEXT. START and END bound the region of rl_line_buffer that contains the word to complete. TEXT is the word to complete. We can use the entire contents of rl_line_buffer in case we want to do some simple parsing. Return the array of matches, or NULL if there aren't any. */ char ** sim_completion (text, start, end) char *text; int start, end; { char **matches; matches = (char **)NULL; /* If this word is at the start of the line, then it is a command to complete. Otherwise it is the name of a file in the current directory. */ if (start == 0) matches = completion_matches (text, command_generator); return (matches); } /* Generator function for command completion. STATE lets us know whether to start from scratch; without any state (i.e. STATE == 0), then we start at the top of the list. */ char * command_generator (text, state) char *text; int state; { static int list_index, len; char *name; /* If this is a new word to complete, initialize now. This includes saving the length of TEXT for efficiency, and initializing the index variable to 0. */ if (!state) { list_index = 0; len = strlen (text); } /* Return the next name which partially matches from the command list. */ while (name = sim_commands[list_index]) { list_index++; if (strncmp (name, text, len) == 0) return (dupstr(name)); } /* If no names matched, then return NULL. */ return ((char *)NULL); } /* Repeats the last command. */ char * repeat_last_command () { int offset = where_history (); HIST_ENTRY *hist; if (hist = history_get (offset)) return dupstr (hist->line); return 0; } #endif extern char *disassembled; void debugmem( unsigned long from, unsigned long to ) { int i; printf("starting to dump mem...\n"); for(i=from; i<to; ) { struct label_entry *entry; unsigned int _insn; printf("i=%x :: ", i); if (verify_memoryarea(i) && (entry = get_label(i))) printf("label: %s |", entry->name); iqueue[0].insn = _insn = evalsim_mem32(i); iqueue[0].insn_index = insn_decode(_insn); disassemble_insn (_insn); printf("%08x %s\n", _insn, disassembled); i += insn_len( iqueue[0].insn_index ); } }
Go to most recent revision | Compare with Previous | Blame | View Log