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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_73/] [or1ksim/] [toplevel.c] - Blame information for rev 1780

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 632 ivang
 
70 2 cvs
/* CVS revision number. */
71 1471 nogj
const char rcsrev[] = "$Revision: 1.125 $";
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 551 markom
  runtime.sim.iprompt = 1;
105 7 jrydberg
  signal(SIGINT, ctrl_c);
106 2 cvs
}
107
 
108 1471 nogj
/* Periodically checks runtime.sim.iprompt to see if ctrl_c has been pressed */
109
void check_int(void *dat)
110 2 cvs
{
111 1471 nogj
  if(runtime.sim.iprompt) {
112
    set_stall_state (0);
113
    handle_sim_command();
114
  }
115
  SCHED_ADD(check_int, NULL, CHECK_INT_TIME);
116
}
117
 
118
void version(void)
119
{
120 997 markom
  PRINTF ("\n");
121
  PRINTF ("OpenRISC 1000 (OR32) Architectural Simulator, %s\n", rcsrev);
122
  PRINTF ("Copyright (C) 1999 Damjan Lampret, lampret@opencores.org\n");
123
  PRINTF ("Copyright (C) 2000 Damjan Lampret, lampret@opencores.org\n");
124
  PRINTF ("                   Jimmy Chen-Min Chen, jimmy@ee.nctu.edu.tw\n");
125
  PRINTF ("                   Johan Rydberg, johan.rydberg@insight.se\n");
126
  PRINTF ("                   Marko Mlinar, markom@opencores.org\n");
127
  PRINTF ("Copyright (C) 2001 Simon Srot, simons@opencores.org\n");
128
  PRINTF ("                   Marko Mlinar, markom@opencores.org\n");
129
  PRINTF ("Copyright (C) 2002 Marko Mlinar, markom@opencores.org\n");
130
  PRINTF ("                   Simon Srot, simons@opencores.org\n");
131
  PRINTF ("Visit http://www.opencores.org for more information about ");
132
  PRINTF ("OpenRISC 1000 and\nother open source cores.\n\n");
133
  PRINTF ("This software comes with ABSOLUTELY NO WARRANTY; for ");
134
  PRINTF ("details see COPYING.\nThis is free software, and you ");
135
  PRINTF ("are welcome to redistribute it under certain\nconditions; ");
136
  PRINTF ("for details see COPYING.\n");
137 2 cvs
}
138
 
139 1360 nogj
struct sim_reset_hook {
140
  void *dat;
141
  void (*reset_hook)(void *);
142
  struct sim_reset_hook *next;
143
};
144 21 cmchen
 
145 1360 nogj
struct sim_reset_hook *sim_reset_hooks = NULL;
146
 
147
/* Registers a new reset hook, called when sim_reset below is called */
148
void reg_sim_reset(void (*reset_hook)(void *), void *dat)
149
{
150
  struct sim_reset_hook *new = malloc(sizeof(struct sim_reset_hook));
151
 
152
  if(!new) {
153
    fprintf(stderr, "reg_sim_reset: Out-of-memory\n");
154
    exit(1);
155
  }
156
 
157
  new->dat = dat;
158
  new->reset_hook = reset_hook;
159
  new->next = sim_reset_hooks;
160
  sim_reset_hooks = new;
161
}
162
 
163 479 markom
/* Resets all subunits */
164 1353 nogj
void sim_reset (void)
165 479 markom
{
166 1360 nogj
  struct sim_reset_hook *cur_reset = sim_reset_hooks;
167
 
168 1390 nogj
  /* We absolutely MUST reset the scheduler first */
169
  sched_reset();
170 1360 nogj
 
171
  while(cur_reset) {
172
    cur_reset->reset_hook(cur_reset->dat);
173
    cur_reset = cur_reset->next;
174
  }
175
 
176 479 markom
  tick_reset();
177
  pm_reset();
178
  pic_reset();
179
  du_reset ();
180 1452 nogj
 
181 1471 nogj
  /* Make sure that runtime.sim.iprompt is the first thing to get checked */
182
  SCHED_ADD(check_int, NULL, 0);
183
 
184 1452 nogj
  lock_memory_table ();
185
 
186
  /* FIXME: Lame-ass way to get runtime.sim.mem_cycles not going into overly
187
   * negative numbers.  This happens because parse.c uses setsim_mem32 to load
188
   * the program but set_mem32 calls dc_simulate_write, which inturn calls
189
   * setsim_mem32.  This mess of memory statistics needs to be sorted out for
190
   * good one day */
191
  runtime.sim.mem_cycles = 0;
192 557 markom
  cpu_reset();
193 479 markom
}
194
 
195 304 markom
/* Initalizes all devices and sim */
196
void sim_init ()
197 2 cvs
{
198 424 markom
  init_memory_table ();
199 269 markom
  init_labels();
200
  init_breakpoints();
201 361 markom
  initstats();
202
  build_automata();
203 1390 nogj
 
204 1452 nogj
#if DYNAMIC_EXECUTION
205
  /* Note: This must be called before the scheduler is used */
206
  init_dyn_recomp();
207
#endif
208
 
209 1390 nogj
  sched_init();
210 361 markom
 
211 305 markom
  if (config.sim.profile) {
212 361 markom
    runtime.sim.fprof = fopen(config.sim.prof_fn, "wt+");
213
    if(!runtime.sim.fprof) {
214 551 markom
      fprintf(stderr, "ERROR: Problems opening profile file.\n");
215
      exit (1);
216 173 markom
    } else
217 897 markom
      fprintf(runtime.sim.fprof, "+00000000 FFFFFFFF FFFFFFFF [outside_functions]\n");
218 173 markom
  }
219 294 markom
 
220 547 markom
  if (config.sim.mprofile) {
221
    runtime.sim.fmprof = fopen(config.sim.mprof_fn, "wb+");
222
    if(!runtime.sim.fmprof) {
223 551 markom
      fprintf(stderr, "ERROR: Problems opening memory profile file.\n");
224
      exit (1);
225 547 markom
    }
226
  }
227
 
228 294 markom
  if (config.sim.exe_log) {
229 361 markom
    runtime.sim.fexe_log = fopen(config.sim.exe_log_fn, "wt+");
230
    if(!runtime.sim.fexe_log) {
231 997 markom
      PRINTF("ERROR: Problems opening exe_log file.\n");
232 551 markom
      exit (1);
233 294 markom
    }
234
  }
235 263 markom
 
236 624 ivang
  if (config.sim.spr_log) {
237 997 markom
    PRINTF("OPENING SPRLOG\n");
238 624 ivang
    runtime.sim.fspr_log = fopen(config.sim.spr_log_fn, "wt+");
239
    if (!runtime.sim.fspr_log) {
240 997 markom
      PRINTF("ERROR: Problems opening spr_log file.\n");
241 624 ivang
      exit(1);
242
    }
243
  }
244
 
245 262 markom
  /* Initialize memory */
246
  {
247 361 markom
    extern struct dev_memarea *dev_list;
248 554 markom
    struct dev_memarea *area;
249 361 markom
    int i;
250
    if (config.memory.type == MT_RANDOM) {
251
      unsigned int val = 0;
252 123 markom
 
253 262 markom
      if (config.memory.random_seed == -1) {
254 551 markom
        runtime.memory.random_seed = time(NULL);
255 262 markom
        /* Print out the seed just in case we ever need to debug */
256 997 markom
        PRINTF("Seeding random generator with value %d\n", config.memory.random_seed);
257 551 markom
      } else
258
        runtime.memory.random_seed = config.memory.random_seed;
259
      srandom(runtime.memory.random_seed);
260 262 markom
 
261 554 markom
      for (area = dev_list; area; area = area->next)
262
        for(i = 0; i < area->size; i++) {
263 221 markom
          val = random();
264 554 markom
          setsim_mem8(i + area->addr_compare, val & 0xFF);
265 221 markom
        }
266 262 markom
    } else if(config.memory.type == MT_PATTERN) {
267 554 markom
      for (area = dev_list; area; area = area->next)
268
        for(i = 0; i < area->size; i++)
269
          setsim_mem8(i + area->addr_compare, config.memory.pattern);
270 269 markom
    } else if (config.memory.type != MT_UNKNOWN) {
271 262 markom
      fprintf(stderr, "Invalid memory configuration type.\n");
272 361 markom
      exit(1);
273 221 markom
    }
274 242 markom
  }
275 262 markom
 
276 361 markom
  if(runtime.sim.filename) {
277
    unsigned long endaddr = 0xFFFFFFFF;
278
    endaddr = loadcode(runtime.sim.filename, 0, 0); /* MM170901 always load at address zero.  */
279
    if (endaddr == -1) {
280
      fprintf(stderr, "Problems loading boot code.\n");
281
      exit(1);
282
    }
283
  }
284 551 markom
 
285 361 markom
  /* Disable gdb debugging, if debug module is not available.  */
286
  if (config.debug.gdb_enabled && !config.debug.enabled) {
287
    config.debug.gdb_enabled = 0;
288
    if (config.sim.verbose)
289
      fprintf (stderr, "WARNING: Debug module not enabled, cannot start gdb.\n");
290
  }
291 551 markom
 
292 550 markom
  if (config.debug.gdb_enabled)
293 479 markom
    gdbcomm_init ();
294 551 markom
 
295 361 markom
  /* Enable dependency stats, if we want to do history analisis */
296 394 markom
  if (config.sim.history && !config.cpu.dependstats) {
297
    config.cpu.dependstats = 1;
298 361 markom
    if (config.sim.verbose)
299 394 markom
      fprintf (stderr, "WARNING: dependstats stats must be enabled to do history analisis.\n");
300 361 markom
  }
301 551 markom
 
302 361 markom
  /* Debug forces verbose */
303
  if (config.sim.debug && !config.sim.verbose) {
304
    config.sim.verbose = 1;
305
    fprintf (stderr, "WARNING: verbose turned on.\n");
306
  }
307
 
308
  /* Start VAPI before device initialization.  */
309
  if (config.vapi.enabled) {
310 551 markom
    runtime.vapi.enabled = 1;
311 361 markom
    vapi_init ();
312
    if (config.sim.verbose)
313 997 markom
      PRINTF ("VAPI started, waiting for clients.\n");
314 361 markom
  }
315 538 markom
 
316 479 markom
  sim_reset ();
317 424 markom
 
318 361 markom
  /* Wait till all test are connected.  */
319 551 markom
  if (runtime.vapi.enabled) {
320 361 markom
    int numu = vapi_num_unconnected (0);
321
    if (numu) {
322 997 markom
      PRINTF ("\nWaiting for VAPI tests with ids:\n");
323 361 markom
      vapi_num_unconnected (1);
324 997 markom
      PRINTF ("\n");
325 1308 phoenix
      while ((numu = vapi_num_unconnected (0))) {
326 361 markom
        vapi_check ();
327 997 markom
        PRINTF ("\rStill waiting for %i VAPI test(s) to connect.       ", numu);
328 361 markom
        usleep (100);
329
      }
330 997 markom
      PRINTF ("\n");
331 361 markom
    }
332 997 markom
    PRINTF ("All devices connected                         \n");
333 361 markom
  }
334
  /* simulator is initialized */
335
  runtime.sim.init = 0;
336 304 markom
}
337 7 jrydberg
 
338 304 markom
/* Cleanup */
339 1446 nogj
void sim_done (void)
340 304 markom
{
341
  if (config.sim.profile) {
342 1350 nogj
    fprintf(runtime.sim.fprof,"-%08llX FFFFFFFF\n", runtime.sim.cycles);
343 361 markom
    fclose(runtime.sim.fprof);
344 304 markom
  }
345 547 markom
 
346 847 markom
  if (config.sim.mprofile) fclose(runtime.sim.fmprof);
347 361 markom
  if (config.sim.exe_log)   fclose(runtime.sim.fexe_log);
348 551 markom
  if (runtime.vapi.enabled)  vapi_done ();
349 426 markom
  done_memory_table ();
350 304 markom
  exit(0);
351
}
352
 
353 728 markom
/* Executes jobs in time queue */
354 1452 nogj
#if !(DYNAMIC_EXECUTION)
355
static inline
356
#endif
357
void do_scheduler (void)
358 728 markom
{
359 1390 nogj
  struct sched_entry *tmp;
360 632 ivang
 
361 728 markom
  /* Execute all jobs till now */
362
  do {
363 1390 nogj
    tmp = scheduler.job_queue;
364
    scheduler.job_queue = tmp->next;
365
    tmp->next = scheduler.free_job_queue;
366
    scheduler.free_job_queue = tmp;
367
 
368 1452 nogj
#if DYNAMIC_EXECUTION
369
    /* This is done here and not after the loop has run because the job function
370
     * may raise an exception in which case set_sched_cycle would never be
371
     * called. */
372
    set_sched_cycle(scheduler.job_queue->time);
373
#endif
374
    TRACE_(sched)("Setting to-go cycles to %"PRIi32" at %lli\n",
375
                  scheduler.job_queue->time, runtime.sim.cycles);
376
 
377 1390 nogj
    tmp->func (tmp->param);
378
  } while (scheduler.job_queue->time <= 0);
379 728 markom
}
380 632 ivang
 
381 1471 nogj
void recalc_do_stats(void)
382
{
383
  extern int do_stats;
384
  do_stats = config.cpu.dependstats || config.cpu.superscalar ||
385
             config.cpu.dependstats || config.sim.history || config.sim.exe_log;
386
}
387
 
388 728 markom
/* Main function */
389 304 markom
int main(argc, argv)
390
     int argc;
391
     char *argv[];
392
{
393 361 markom
  srand(getpid());
394
  init_defconfig();
395 1358 nogj
  reg_config_secs();
396 361 markom
  if (parse_args(argc, argv)) {
397 997 markom
    PRINTF("Usage: %s [options] <filename>\n", argv[0]);
398
    PRINTF("Options:\n");
399
    PRINTF(" -v                   version and copyright note\n");
400
    PRINTF(" -i                   enable interactive command prompt\n");
401
    PRINTF(" --nosrv              do not launch JTAG proxy server\n"); /* (CZ) */
402
    PRINTF(" --srv <n>            launch JTAG proxy server on port <n>; [random]\n"); /* (CZ) */
403
    PRINTF(" -f or --file         load script file [sim.cfg]\n");
404
    PRINTF(" --enable-profile     enable profiling.\n");
405
    PRINTF(" --enable-mprofile    enable memory profiling.\n");
406
    PRINTF("\nor   : %s ", argv[0]);
407 847 markom
    mp_help ();
408 997 markom
    PRINTF("\nor   : %s ", argv[0]);
409 847 markom
    prof_help ();
410 361 markom
    exit(-1);
411
  }
412 304 markom
 
413
  /* Read configuration file.  */
414 361 markom
  if (!runtime.sim.script_file_specified)
415
    read_script_file ("sim.cfg");
416 883 markom
 
417
  /* Overide parameters with command line ones */
418
  if (runtime.simcmd.profile) config.sim.profile = 1;
419
  if (runtime.simcmd.mprofile) config.sim.mprofile = 1;
420
 
421 424 markom
  if (!runtime.sim.script_file_specified && config.sim.verbose)
422
    fprintf (stderr, "WARNING: No config file read, assuming default configuration.\n");
423 304 markom
  print_config();
424
  signal(SIGINT, ctrl_c);
425 335 markom
 
426 1353 nogj
  runtime.sim.hush = 1;
427 1471 nogj
  recalc_do_stats();
428 1353 nogj
 
429 1471 nogj
  sim_init ();
430
 
431 361 markom
  while(1) {
432 1471 nogj
    long long time_start = runtime.sim.cycles;
433
    if (config.debug.enabled) {
434
      du_clock(); // reset watchpoints
435
      while (runtime.cpu.stalled) {
436
        if(config.debug.gdb_enabled) {
437
          BlockJTAG();
438
          HandleServerSocket(false);
439
        } else {
440
          fprintf (stderr, "WARNING: CPU stalled and gdb connection not enabled.\n");
441
          /* Dump the user into interactive mode.  From there he can decide what
442
           * to do. */
443
          handle_sim_command();
444
          sim_done();
445 557 markom
        }
446 1471 nogj
        if(runtime.sim.iprompt)
447
          handle_sim_command();
448 479 markom
      }
449 1471 nogj
    }
450 127 chris
 
451 1471 nogj
    /* Each cycle has counter of mem_cycles; this value is joined with cycles
452
       at the end of the cycle; no sim originated memory accesses should be
453
       performed inbetween. */
454
    runtime.sim.mem_cycles = 0;
455
    if (!config.pm.enabled || !testsprbits(SPR_PMR, SPR_PMR_DME | SPR_PMR_SME))
456
      if (cpu_clock ())
457
        /* A breakpoint has been hit, drop to interactive mode */
458
        handle_sim_command();
459 304 markom
 
460 1471 nogj
    if (config.vapi.enabled && runtime.vapi.enabled) vapi_check();
461
    if (config.debug.gdb_enabled) HandleServerSocket(false); /* block & check_stdin = false */
462
    if(config.debug.enabled)
463
      if (testsprbits(SPR_DMR1, SPR_DMR1_ST)) set_stall_state (1);
464 557 markom
 
465 1471 nogj
    runtime.sim.cycles += runtime.sim.mem_cycles;
466
    scheduler.job_queue->time -= runtime.sim.cycles - time_start;
467
    if (scheduler.job_queue->time <= 0) do_scheduler ();
468 361 markom
  }
469
  sim_done();
470 2 cvs
}

powered by: WebSVN 2.1.0

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