OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc3/] [or1ksim/] [toplevel.c] - Blame information for rev 1692

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 cvs
/* toplevel.c -- Top level simulator source file
2
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
/* Simulator commands. Help and version output. SIGINT processing.
21
Stdout redirection is specific to linux (I need to fix this). */
22
 
23 16 jrydberg
 
24 2 cvs
#include <stdio.h>
25
#include <ctype.h>
26
#include <string.h>
27
#include <stdlib.h>
28 46 lampret
#include <unistd.h>
29 2 cvs
#include <signal.h>
30
#include <stdarg.h>
31 123 markom
#include <fcntl.h>
32 728 markom
#include <limits.h>
33 1308 phoenix
#include <time.h>
34 2 cvs
 
35 1350 nogj
#include "config.h"
36
 
37
#ifdef HAVE_INTTYPES_H
38
#include <inttypes.h>
39
#endif
40
 
41
#include "port.h"
42 2 cvs
#include "arch.h"
43
#include "parse.h"
44
#include "abstract.h"
45 261 markom
#include "labels.h"
46 69 lampret
#include "sim-config.h"
47 1432 nogj
#include "opcode/or32.h"
48 103 lampret
#include "spr_defs.h"
49 1432 nogj
#include "execute.h"
50 518 markom
#include "sprs.h"
51 304 markom
#include "vapi.h"
52 479 markom
#include "gdbcomm.h"
53
#include "debug_unit.h"
54 28 lampret
#include "coff.h"
55 728 markom
#include "sched.h"
56 632 ivang
#include "profiler.h"
57
#include "mprofiler.h"
58 1308 phoenix
#include "pm.h"
59
#include "pic.h"
60
#include "stats.h"
61
#include "immu.h"
62
#include "dmmu.h"
63
#include "dcache_model.h"
64
#include "icache_model.h"
65
#include "branch_predict.h"
66
#include "dumpverilog.h"
67
#include "trace.h"
68
#include "cuc.h"
69 1557 nogj
#include "tick.h"
70 632 ivang
 
71 1645 nogj
const char *or1ksim_ver = "0.2.0";
72 2 cvs
 
73 344 markom
inline void debug(int level, const char *format, ...)
74 2 cvs
{
75 7 jrydberg
  char *p;
76
  va_list ap;
77 2 cvs
 
78 344 markom
  if (config.sim.debug >= level) {
79 69 lampret
    if ((p = malloc(1000)) == NULL)
80
      return;
81
    va_start(ap, format);
82
    (void) vsnprintf(p, 1000, format, ap);
83
    va_end(ap);
84 997 markom
    PRINTF("%s", p);
85 69 lampret
    fflush(stdout);
86
    free(p);
87
  } else {
88
#if DEBUG
89 7 jrydberg
  if ((p = malloc(1000)) == NULL)
90
    return;
91
  va_start(ap, format);
92
  (void) vsnprintf(p, 1000, format, ap);
93
  va_end(ap);
94 997 markom
  PRINTF("%s\n", p);
95 7 jrydberg
  fflush(stdout);
96
  free(p);
97 2 cvs
#endif
98 69 lampret
  }
99 2 cvs
}
100
 
101 304 markom
void ctrl_c(signum)
102 7 jrydberg
     int signum;
103 2 cvs
{
104 1480 nogj
  /* Incase the user pressed ctrl+c twice without the sim reacting kill it.
105
   * This is incase the sim locks up in a high level routine, without executeing
106
   * any (or) code */
107 1593 nogj
  if(runtime.sim.iprompt && !runtime.sim.iprompt_run)
108 1480 nogj
    sim_done();
109 551 markom
  runtime.sim.iprompt = 1;
110 7 jrydberg
  signal(SIGINT, ctrl_c);
111 2 cvs
}
112
 
113 1471 nogj
/* Periodically checks runtime.sim.iprompt to see if ctrl_c has been pressed */
114
void check_int(void *dat)
115 2 cvs
{
116 1471 nogj
  if(runtime.sim.iprompt) {
117
    set_stall_state (0);
118
    handle_sim_command();
119
  }
120
  SCHED_ADD(check_int, NULL, CHECK_INT_TIME);
121
}
122
 
123 1360 nogj
struct sim_reset_hook {
124
  void *dat;
125
  void (*reset_hook)(void *);
126
  struct sim_reset_hook *next;
127
};
128 21 cmchen
 
129 1550 nogj
static struct sim_reset_hook *sim_reset_hooks = NULL;
130 1360 nogj
 
131
/* Registers a new reset hook, called when sim_reset below is called */
132
void reg_sim_reset(void (*reset_hook)(void *), void *dat)
133
{
134
  struct sim_reset_hook *new = malloc(sizeof(struct sim_reset_hook));
135
 
136
  if(!new) {
137
    fprintf(stderr, "reg_sim_reset: Out-of-memory\n");
138
    exit(1);
139
  }
140
 
141
  new->dat = dat;
142
  new->reset_hook = reset_hook;
143
  new->next = sim_reset_hooks;
144
  sim_reset_hooks = new;
145
}
146
 
147 479 markom
/* Resets all subunits */
148 1353 nogj
void sim_reset (void)
149 479 markom
{
150 1360 nogj
  struct sim_reset_hook *cur_reset = sim_reset_hooks;
151
 
152 1390 nogj
  /* We absolutely MUST reset the scheduler first */
153
  sched_reset();
154 1360 nogj
 
155
  while(cur_reset) {
156
    cur_reset->reset_hook(cur_reset->dat);
157
    cur_reset = cur_reset->next;
158
  }
159
 
160 479 markom
  tick_reset();
161
  pm_reset();
162
  pic_reset();
163
  du_reset ();
164 1452 nogj
 
165 1471 nogj
  /* Make sure that runtime.sim.iprompt is the first thing to get checked */
166 1486 nogj
  SCHED_ADD(check_int, NULL, 1);
167 1471 nogj
 
168 1452 nogj
  /* FIXME: Lame-ass way to get runtime.sim.mem_cycles not going into overly
169
   * negative numbers.  This happens because parse.c uses setsim_mem32 to load
170
   * the program but set_mem32 calls dc_simulate_write, which inturn calls
171
   * setsim_mem32.  This mess of memory statistics needs to be sorted out for
172
   * good one day */
173
  runtime.sim.mem_cycles = 0;
174 557 markom
  cpu_reset();
175 479 markom
}
176
 
177 304 markom
/* Initalizes all devices and sim */
178 1486 nogj
void sim_init (void)
179 2 cvs
{
180 269 markom
  init_labels();
181
  init_breakpoints();
182 361 markom
  initstats();
183
  build_automata();
184 1390 nogj
 
185 1452 nogj
#if DYNAMIC_EXECUTION
186
  /* Note: This must be called before the scheduler is used */
187
  init_dyn_recomp();
188
#endif
189
 
190 1390 nogj
  sched_init();
191 361 markom
 
192 305 markom
  if (config.sim.profile) {
193 361 markom
    runtime.sim.fprof = fopen(config.sim.prof_fn, "wt+");
194
    if(!runtime.sim.fprof) {
195 551 markom
      fprintf(stderr, "ERROR: Problems opening profile file.\n");
196
      exit (1);
197 173 markom
    } else
198 897 markom
      fprintf(runtime.sim.fprof, "+00000000 FFFFFFFF FFFFFFFF [outside_functions]\n");
199 173 markom
  }
200 294 markom
 
201 547 markom
  if (config.sim.mprofile) {
202
    runtime.sim.fmprof = fopen(config.sim.mprof_fn, "wb+");
203
    if(!runtime.sim.fmprof) {
204 551 markom
      fprintf(stderr, "ERROR: Problems opening memory profile file.\n");
205
      exit (1);
206 547 markom
    }
207
  }
208
 
209 294 markom
  if (config.sim.exe_log) {
210 361 markom
    runtime.sim.fexe_log = fopen(config.sim.exe_log_fn, "wt+");
211
    if(!runtime.sim.fexe_log) {
212 997 markom
      PRINTF("ERROR: Problems opening exe_log file.\n");
213 551 markom
      exit (1);
214 294 markom
    }
215
  }
216 263 markom
 
217 361 markom
  if(runtime.sim.filename) {
218
    unsigned long endaddr = 0xFFFFFFFF;
219
    endaddr = loadcode(runtime.sim.filename, 0, 0); /* MM170901 always load at address zero.  */
220
    if (endaddr == -1) {
221
      fprintf(stderr, "Problems loading boot code.\n");
222
      exit(1);
223
    }
224
  }
225 551 markom
 
226 361 markom
  /* Disable gdb debugging, if debug module is not available.  */
227
  if (config.debug.gdb_enabled && !config.debug.enabled) {
228
    config.debug.gdb_enabled = 0;
229
    if (config.sim.verbose)
230
      fprintf (stderr, "WARNING: Debug module not enabled, cannot start gdb.\n");
231
  }
232 551 markom
 
233 550 markom
  if (config.debug.gdb_enabled)
234 479 markom
    gdbcomm_init ();
235 551 markom
 
236 361 markom
  /* Enable dependency stats, if we want to do history analisis */
237 394 markom
  if (config.sim.history && !config.cpu.dependstats) {
238
    config.cpu.dependstats = 1;
239 361 markom
    if (config.sim.verbose)
240 394 markom
      fprintf (stderr, "WARNING: dependstats stats must be enabled to do history analisis.\n");
241 361 markom
  }
242 551 markom
 
243 361 markom
  /* Debug forces verbose */
244
  if (config.sim.debug && !config.sim.verbose) {
245
    config.sim.verbose = 1;
246
    fprintf (stderr, "WARNING: verbose turned on.\n");
247
  }
248
 
249
  /* Start VAPI before device initialization.  */
250
  if (config.vapi.enabled) {
251 551 markom
    runtime.vapi.enabled = 1;
252 361 markom
    vapi_init ();
253
    if (config.sim.verbose)
254 997 markom
      PRINTF ("VAPI started, waiting for clients.\n");
255 361 markom
  }
256 538 markom
 
257 479 markom
  sim_reset ();
258 424 markom
 
259 361 markom
  /* Wait till all test are connected.  */
260 551 markom
  if (runtime.vapi.enabled) {
261 361 markom
    int numu = vapi_num_unconnected (0);
262
    if (numu) {
263 997 markom
      PRINTF ("\nWaiting for VAPI tests with ids:\n");
264 361 markom
      vapi_num_unconnected (1);
265 997 markom
      PRINTF ("\n");
266 1308 phoenix
      while ((numu = vapi_num_unconnected (0))) {
267 361 markom
        vapi_check ();
268 997 markom
        PRINTF ("\rStill waiting for %i VAPI test(s) to connect.       ", numu);
269 361 markom
        usleep (100);
270
      }
271 997 markom
      PRINTF ("\n");
272 361 markom
    }
273 997 markom
    PRINTF ("All devices connected                         \n");
274 361 markom
  }
275
  /* simulator is initialized */
276
  runtime.sim.init = 0;
277 304 markom
}
278 7 jrydberg
 
279 304 markom
/* Cleanup */
280 1446 nogj
void sim_done (void)
281 304 markom
{
282
  if (config.sim.profile) {
283 1350 nogj
    fprintf(runtime.sim.fprof,"-%08llX FFFFFFFF\n", runtime.sim.cycles);
284 361 markom
    fclose(runtime.sim.fprof);
285 304 markom
  }
286 547 markom
 
287 847 markom
  if (config.sim.mprofile) fclose(runtime.sim.fmprof);
288 361 markom
  if (config.sim.exe_log)   fclose(runtime.sim.fexe_log);
289 551 markom
  if (runtime.vapi.enabled)  vapi_done ();
290 426 markom
  done_memory_table ();
291 304 markom
  exit(0);
292
}
293
 
294 1471 nogj
void recalc_do_stats(void)
295
{
296
  extern int do_stats;
297 1550 nogj
  do_stats = config.cpu.superscalar || config.cpu.dependstats ||
298
             config.sim.history || config.sim.exe_log;
299 1471 nogj
}
300
 
301 728 markom
/* Main function */
302 304 markom
int main(argc, argv)
303
     int argc;
304
     char *argv[];
305
{
306 361 markom
  srand(getpid());
307
  init_defconfig();
308 1358 nogj
  reg_config_secs();
309 361 markom
  if (parse_args(argc, argv)) {
310 997 markom
    PRINTF("Usage: %s [options] <filename>\n", argv[0]);
311
    PRINTF("Options:\n");
312
    PRINTF(" -v                   version and copyright note\n");
313
    PRINTF(" -i                   enable interactive command prompt\n");
314
    PRINTF(" --nosrv              do not launch JTAG proxy server\n"); /* (CZ) */
315
    PRINTF(" --srv <n>            launch JTAG proxy server on port <n>; [random]\n"); /* (CZ) */
316
    PRINTF(" -f or --file         load script file [sim.cfg]\n");
317 1548 nogj
    PRINTF(" -d <debug config>    Enable debug channels\n");
318 997 markom
    PRINTF(" --enable-profile     enable profiling.\n");
319
    PRINTF(" --enable-mprofile    enable memory profiling.\n");
320
    PRINTF("\nor   : %s ", argv[0]);
321 847 markom
    mp_help ();
322 997 markom
    PRINTF("\nor   : %s ", argv[0]);
323 847 markom
    prof_help ();
324 361 markom
    exit(-1);
325
  }
326 304 markom
 
327
  /* Read configuration file.  */
328 361 markom
  if (!runtime.sim.script_file_specified)
329
    read_script_file ("sim.cfg");
330 883 markom
 
331
  /* Overide parameters with command line ones */
332
  if (runtime.simcmd.profile) config.sim.profile = 1;
333
  if (runtime.simcmd.mprofile) config.sim.mprofile = 1;
334
 
335 424 markom
  if (!runtime.sim.script_file_specified && config.sim.verbose)
336
    fprintf (stderr, "WARNING: No config file read, assuming default configuration.\n");
337 304 markom
  print_config();
338
  signal(SIGINT, ctrl_c);
339 335 markom
 
340 1353 nogj
  runtime.sim.hush = 1;
341 1471 nogj
  recalc_do_stats();
342 1353 nogj
 
343 1471 nogj
  sim_init ();
344
 
345 1692 nogj
#if DYNAMIC_EXECUTION
346
  dyn_main();
347
#else
348 1690 nogj
  exec_main();
349 1692 nogj
#endif
350 127 chris
 
351 361 markom
  sim_done();
352 2 cvs
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.