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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [toplevel.c] - Blame information for rev 1364

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

powered by: WebSVN 2.1.0

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