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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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