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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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