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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_40/] [or1ksim/] [toplevel.c] - Blame information for rev 1367

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

powered by: WebSVN 2.1.0

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