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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [toplevel.c] - Blame information for rev 1372

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

powered by: WebSVN 2.1.0

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