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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_62/] [or1ksim/] [toplevel.c] - Blame information for rev 1369

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

powered by: WebSVN 2.1.0

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