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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_70/] [or1ksim/] [toplevel.c] - Blame information for rev 1374

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 "pm.h"
58
#include "pic.h"
59 1344 nogj
#include "opcode/or32.h"
60 1308 phoenix
#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 1374 nogj
const char rcsrev[] = "$Revision: 1.115 $";
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 728 markom
  SCHED_INIT();
161 1360 nogj
 
162
  while(cur_reset) {
163
    cur_reset->reset_hook(cur_reset->dat);
164
    cur_reset = cur_reset->next;
165
  }
166
 
167 479 markom
  tick_reset();
168
  pm_reset();
169
  pic_reset();
170
  du_reset ();
171 557 markom
  cpu_reset();
172 479 markom
}
173
 
174 304 markom
/* Initalizes all devices and sim */
175
void sim_init ()
176 2 cvs
{
177 424 markom
  init_memory_table ();
178 269 markom
  init_labels();
179
  init_breakpoints();
180 361 markom
  initstats();
181
  build_automata();
182
 
183 305 markom
  if (config.sim.profile) {
184 361 markom
    runtime.sim.fprof = fopen(config.sim.prof_fn, "wt+");
185
    if(!runtime.sim.fprof) {
186 551 markom
      fprintf(stderr, "ERROR: Problems opening profile file.\n");
187
      exit (1);
188 173 markom
    } else
189 897 markom
      fprintf(runtime.sim.fprof, "+00000000 FFFFFFFF FFFFFFFF [outside_functions]\n");
190 173 markom
  }
191 294 markom
 
192 547 markom
  if (config.sim.mprofile) {
193
    runtime.sim.fmprof = fopen(config.sim.mprof_fn, "wb+");
194
    if(!runtime.sim.fmprof) {
195 551 markom
      fprintf(stderr, "ERROR: Problems opening memory profile file.\n");
196
      exit (1);
197 547 markom
    }
198
  }
199
 
200 294 markom
  if (config.sim.exe_log) {
201 361 markom
    runtime.sim.fexe_log = fopen(config.sim.exe_log_fn, "wt+");
202
    if(!runtime.sim.fexe_log) {
203 997 markom
      PRINTF("ERROR: Problems opening exe_log file.\n");
204 551 markom
      exit (1);
205 294 markom
    }
206
  }
207 263 markom
 
208 624 ivang
  if (config.sim.spr_log) {
209 997 markom
    PRINTF("OPENING SPRLOG\n");
210 624 ivang
    runtime.sim.fspr_log = fopen(config.sim.spr_log_fn, "wt+");
211
    if (!runtime.sim.fspr_log) {
212 997 markom
      PRINTF("ERROR: Problems opening spr_log file.\n");
213 624 ivang
      exit(1);
214
    }
215
  }
216
 
217 262 markom
  /* Initialize memory */
218
  {
219 361 markom
    extern struct dev_memarea *dev_list;
220 554 markom
    struct dev_memarea *area;
221 361 markom
    int i;
222
    if (config.memory.type == MT_RANDOM) {
223
      unsigned int val = 0;
224 123 markom
 
225 262 markom
      if (config.memory.random_seed == -1) {
226 551 markom
        runtime.memory.random_seed = time(NULL);
227 262 markom
        /* Print out the seed just in case we ever need to debug */
228 997 markom
        PRINTF("Seeding random generator with value %d\n", config.memory.random_seed);
229 551 markom
      } else
230
        runtime.memory.random_seed = config.memory.random_seed;
231
      srandom(runtime.memory.random_seed);
232 262 markom
 
233 554 markom
      for (area = dev_list; area; area = area->next)
234
        for(i = 0; i < area->size; i++) {
235 221 markom
          val = random();
236 554 markom
          setsim_mem8(i + area->addr_compare, val & 0xFF);
237 221 markom
        }
238 262 markom
    } else if(config.memory.type == MT_PATTERN) {
239 554 markom
      for (area = dev_list; area; area = area->next)
240
        for(i = 0; i < area->size; i++)
241
          setsim_mem8(i + area->addr_compare, config.memory.pattern);
242 269 markom
    } else if (config.memory.type != MT_UNKNOWN) {
243 262 markom
      fprintf(stderr, "Invalid memory configuration type.\n");
244 361 markom
      exit(1);
245 221 markom
    }
246 242 markom
  }
247 262 markom
 
248 361 markom
  if(runtime.sim.filename) {
249
    unsigned long endaddr = 0xFFFFFFFF;
250
    endaddr = loadcode(runtime.sim.filename, 0, 0); /* MM170901 always load at address zero.  */
251
    if (endaddr == -1) {
252
      fprintf(stderr, "Problems loading boot code.\n");
253
      exit(1);
254
    }
255
  }
256 551 markom
 
257
#if !FAST_SIM /* We assume we have valid configuration with fsim*/
258 361 markom
  /* Disable gdb debugging, if debug module is not available.  */
259
  if (config.debug.gdb_enabled && !config.debug.enabled) {
260
    config.debug.gdb_enabled = 0;
261
    if (config.sim.verbose)
262
      fprintf (stderr, "WARNING: Debug module not enabled, cannot start gdb.\n");
263
  }
264 551 markom
#endif
265
 
266 550 markom
  if (config.debug.gdb_enabled)
267 479 markom
    gdbcomm_init ();
268 551 markom
 
269
#if !FAST_SIM /* We assume we have valid configuration with fsim*/
270 361 markom
  /* Enable dependency stats, if we want to do history analisis */
271 394 markom
  if (config.sim.history && !config.cpu.dependstats) {
272
    config.cpu.dependstats = 1;
273 361 markom
    if (config.sim.verbose)
274 394 markom
      fprintf (stderr, "WARNING: dependstats stats must be enabled to do history analisis.\n");
275 361 markom
  }
276 551 markom
#endif
277
 
278
#if !FAST_SIM /* We assume we have valid configuration with fsim*/  
279 361 markom
  /* Debug forces verbose */
280
  if (config.sim.debug && !config.sim.verbose) {
281
    config.sim.verbose = 1;
282
    fprintf (stderr, "WARNING: verbose turned on.\n");
283
  }
284 551 markom
#endif
285 361 markom
 
286
  /* Start VAPI before device initialization.  */
287
  if (config.vapi.enabled) {
288 551 markom
    runtime.vapi.enabled = 1;
289 361 markom
    vapi_init ();
290
    if (config.sim.verbose)
291 997 markom
      PRINTF ("VAPI started, waiting for clients.\n");
292 361 markom
  }
293 538 markom
 
294 479 markom
  sim_reset ();
295 424 markom
 
296 543 simons
  lock_memory_table ();
297
 
298 361 markom
  /* Wait till all test are connected.  */
299 551 markom
  if (runtime.vapi.enabled) {
300 361 markom
    int numu = vapi_num_unconnected (0);
301
    if (numu) {
302 997 markom
      PRINTF ("\nWaiting for VAPI tests with ids:\n");
303 361 markom
      vapi_num_unconnected (1);
304 997 markom
      PRINTF ("\n");
305 1308 phoenix
      while ((numu = vapi_num_unconnected (0))) {
306 361 markom
        vapi_check ();
307 997 markom
        PRINTF ("\rStill waiting for %i VAPI test(s) to connect.       ", numu);
308 361 markom
        usleep (100);
309
      }
310 997 markom
      PRINTF ("\n");
311 361 markom
    }
312 997 markom
    PRINTF ("All devices connected                         \n");
313 361 markom
  }
314
  /* simulator is initialized */
315
  runtime.sim.init = 0;
316 304 markom
}
317 7 jrydberg
 
318 304 markom
/* Cleanup */
319
void sim_done ()
320
{
321
  if (config.sim.profile) {
322 1350 nogj
    fprintf(runtime.sim.fprof,"-%08llX FFFFFFFF\n", runtime.sim.cycles);
323 361 markom
    fclose(runtime.sim.fprof);
324 304 markom
  }
325 547 markom
 
326 847 markom
  if (config.sim.mprofile) fclose(runtime.sim.fmprof);
327 361 markom
  if (config.sim.exe_log)   fclose(runtime.sim.fexe_log);
328 551 markom
  if (runtime.vapi.enabled)  vapi_done ();
329 426 markom
  done_memory_table ();
330 304 markom
  exit(0);
331
}
332
 
333 728 markom
/* Executes jobs in time queue */
334
static inline void do_scheduler ()
335
{
336 1365 nogj
  void (*func)(void *);
337
  void *param;
338 632 ivang
 
339 728 markom
  /* Execute all jobs till now */
340
  do {
341
    func = SCHED_PEEK().func;
342
    param = SCHED_PEEK().param;
343
    SCHED_REMOVE();
344
    func (param);
345 884 markom
  } while (runtime.sim.cycles >= SCHED_PEEK().time);
346 728 markom
}
347 632 ivang
 
348 728 markom
/* Main function */
349 304 markom
int main(argc, argv)
350
     int argc;
351
     char *argv[];
352
{
353 361 markom
  srand(getpid());
354
  init_defconfig();
355 1358 nogj
  reg_config_secs();
356 361 markom
  if (parse_args(argc, argv)) {
357 997 markom
    PRINTF("Usage: %s [options] <filename>\n", argv[0]);
358
    PRINTF("Options:\n");
359
    PRINTF(" -v                   version and copyright note\n");
360
    PRINTF(" -i                   enable interactive command prompt\n");
361
    PRINTF(" --nosrv              do not launch JTAG proxy server\n"); /* (CZ) */
362
    PRINTF(" --srv <n>            launch JTAG proxy server on port <n>; [random]\n"); /* (CZ) */
363 551 markom
#if !FAST_SIM
364 997 markom
    PRINTF(" -f or --file         load script file [sim.cfg]\n");
365
    PRINTF(" --enable-profile     enable profiling.\n");
366
    PRINTF(" --enable-mprofile    enable memory profiling.\n");
367 551 markom
#endif
368 997 markom
    PRINTF(" --output-cfg         prints C structure of current\n");
369
    PRINTF("                      configuration to standard output\n");
370
    PRINTF("\nor   : %s ", argv[0]);
371 847 markom
    mp_help ();
372 997 markom
    PRINTF("\nor   : %s ", argv[0]);
373 847 markom
    prof_help ();
374 361 markom
    exit(-1);
375
  }
376 304 markom
 
377 551 markom
#if !FAST_SIM
378 304 markom
  /* Read configuration file.  */
379 361 markom
  if (!runtime.sim.script_file_specified)
380
    read_script_file ("sim.cfg");
381 883 markom
 
382
  /* Overide parameters with command line ones */
383
  if (runtime.simcmd.profile) config.sim.profile = 1;
384
  if (runtime.simcmd.mprofile) config.sim.mprofile = 1;
385
 
386 424 markom
  if (!runtime.sim.script_file_specified && config.sim.verbose)
387
    fprintf (stderr, "WARNING: No config file read, assuming default configuration.\n");
388 551 markom
#else
389 997 markom
  PRINTF ("\n\tNOTE: running fast sim with fixed configuration!\n\n");
390 551 markom
#endif
391 549 markom
  if (runtime.sim.output_cfg) {
392
    output_cfg (stdout);
393
    exit (0);
394
  }
395 304 markom
  print_config();
396 336 markom
  sim_init ();
397 304 markom
  signal(SIGINT, ctrl_c);
398 335 markom
 
399 1353 nogj
  runtime.sim.hush = 1;
400
  runtime.sim.cont_run = -1;
401
 
402 361 markom
  while(1) {
403 1353 nogj
    if (runtime.sim.iprompt)
404
        handle_sim_command();
405 7 jrydberg
 
406 714 markom
    { /* Needed by execution */
407
      extern int do_stats;
408
      do_stats = config.cpu.dependstats || config.cpu.superscalar || config.cpu.dependstats
409
              || config.sim.history || config.sim.exe_log;
410
    }
411
 
412 361 markom
    /* MM: 'run -1' means endless execution.  */
413 884 markom
    while(runtime.sim.cont_run) {
414 557 markom
      IFF (config.debug.enabled) {
415 1353 nogj
        du_clock(); // reset watchpoints
416 884 markom
        if (runtime.cpu.stalled) {
417 557 markom
          if(config.debug.gdb_enabled) {
418
            BlockJTAG();
419
            HandleServerSocket(false);
420 605 markom
          } else {
421
            fprintf (stderr, "WARNING: CPU stalled and gdb connection not enabled.");
422 884 markom
            runtime.sim.cont_run = 0;
423 605 markom
          }
424 557 markom
          continue;
425
        }
426 479 markom
      }
427 127 chris
 
428 537 markom
      /* Each cycle has counter of mem_cycles; this value is joined with cycles
429
         at the end of the cycle; no sim originated memory accesses should be
430
         performed inbetween. */
431 884 markom
      runtime.sim.mem_cycles = 0;
432 557 markom
      if (!config.pm.enabled || !testsprbits(SPR_PMR, SPR_PMR_DME | SPR_PMR_SME)) {
433 884 markom
        if (runtime.sim.cont_run > 0) runtime.sim.cont_run--;
434 599 simons
        pic_clock ();
435
        if (cpu_clock ()) break;
436
        if (config.dc.enabled) dc_clock();
437
        if (config.ic.enabled) ic_clock();
438 261 markom
      }
439 304 markom
 
440 551 markom
      if (config.vapi.enabled && runtime.vapi.enabled) vapi_check();
441 557 markom
      if (config.debug.gdb_enabled) HandleServerSocket(false); /* block & check_stdin = false */
442
      IFF(config.debug.enabled)
443 479 markom
        if (testsprbits(SPR_DMR1, SPR_DMR1_ST)) set_stall_state (1);
444 557 markom
 
445 884 markom
      runtime.sim.cycles += runtime.sim.mem_cycles;
446
      if (runtime.sim.cycles >= SCHED_PEEK().time) do_scheduler ();
447 1353 nogj
      if (!runtime.sim.hush) dumpreg();
448 361 markom
    }
449 1353 nogj
    runtime.sim.hush = 0;
450 361 markom
    fflush(stdout);
451 997 markom
    runtime.sim.fout = stdout;
452 2 cvs
 
453 551 markom
    if (!runtime.sim.iprompt)  /* non-interactive quit */
454 361 markom
      sim_done();
455
  }
456
  sim_done();
457 2 cvs
}

powered by: WebSVN 2.1.0

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