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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc1/] [or1ksim/] [toplevel.c] - Blame information for rev 1360

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

powered by: WebSVN 2.1.0

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