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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_52/] [or1ksim/] [toplevel.c] - Blame information for rev 1370

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

powered by: WebSVN 2.1.0

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