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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_67/] [or1ksim/] [toplevel.c] - Blame information for rev 1765

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

powered by: WebSVN 2.1.0

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