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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_1_0/] [or1ksim/] [toplevel.c] - Blame information for rev 1765

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 1353 nogj
const char rcsrev[] = "$Revision: 1.103 $";
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 479 markom
void debugmem (unsigned long from, unsigned long to );
141 21 cmchen
 
142 479 markom
/* Resets all subunits */
143 1353 nogj
void sim_reset (void)
144 479 markom
{
145 728 markom
  SCHED_INIT();
146 479 markom
  uart_reset();
147
  dma_reset();
148
  eth_reset();
149
  gpio_reset();
150 645 markom
  vga_reset ();
151
  fb_reset ();
152 664 markom
  kbd_reset ();
153 876 rherveille
  ata_reset();
154 479 markom
  tick_reset();
155
  pm_reset();
156
  pic_reset();
157
  mc_reset();
158
  du_reset ();
159 557 markom
  cpu_reset();
160 479 markom
}
161
 
162 304 markom
/* Initalizes all devices and sim */
163
void sim_init ()
164 2 cvs
{
165 424 markom
  init_memory_table ();
166 269 markom
  init_labels();
167
  init_breakpoints();
168 361 markom
  initstats();
169
  build_automata();
170
 
171 305 markom
  if (config.sim.profile) {
172 361 markom
    runtime.sim.fprof = fopen(config.sim.prof_fn, "wt+");
173
    if(!runtime.sim.fprof) {
174 551 markom
      fprintf(stderr, "ERROR: Problems opening profile file.\n");
175
      exit (1);
176 173 markom
    } else
177 897 markom
      fprintf(runtime.sim.fprof, "+00000000 FFFFFFFF FFFFFFFF [outside_functions]\n");
178 173 markom
  }
179 294 markom
 
180 547 markom
  if (config.sim.mprofile) {
181
    runtime.sim.fmprof = fopen(config.sim.mprof_fn, "wb+");
182
    if(!runtime.sim.fmprof) {
183 551 markom
      fprintf(stderr, "ERROR: Problems opening memory profile file.\n");
184
      exit (1);
185 547 markom
    }
186
  }
187
 
188 294 markom
  if (config.sim.exe_log) {
189 361 markom
    runtime.sim.fexe_log = fopen(config.sim.exe_log_fn, "wt+");
190
    if(!runtime.sim.fexe_log) {
191 997 markom
      PRINTF("ERROR: Problems opening exe_log file.\n");
192 551 markom
      exit (1);
193 294 markom
    }
194
  }
195 263 markom
 
196 624 ivang
  if (config.sim.spr_log) {
197 997 markom
    PRINTF("OPENING SPRLOG\n");
198 624 ivang
    runtime.sim.fspr_log = fopen(config.sim.spr_log_fn, "wt+");
199
    if (!runtime.sim.fspr_log) {
200 997 markom
      PRINTF("ERROR: Problems opening spr_log file.\n");
201 624 ivang
      exit(1);
202
    }
203
  }
204
 
205 262 markom
  /* Initialize memory */
206
  {
207 361 markom
    extern struct dev_memarea *dev_list;
208 554 markom
    struct dev_memarea *area;
209 361 markom
    int i;
210
    if (config.memory.type == MT_RANDOM) {
211
      unsigned int val = 0;
212 123 markom
 
213 262 markom
      if (config.memory.random_seed == -1) {
214 551 markom
        runtime.memory.random_seed = time(NULL);
215 262 markom
        /* Print out the seed just in case we ever need to debug */
216 997 markom
        PRINTF("Seeding random generator with value %d\n", config.memory.random_seed);
217 551 markom
      } else
218
        runtime.memory.random_seed = config.memory.random_seed;
219
      srandom(runtime.memory.random_seed);
220 262 markom
 
221 554 markom
      for (area = dev_list; area; area = area->next)
222
        for(i = 0; i < area->size; i++) {
223 221 markom
          val = random();
224 554 markom
          setsim_mem8(i + area->addr_compare, val & 0xFF);
225 221 markom
        }
226 262 markom
    } else if(config.memory.type == MT_PATTERN) {
227 554 markom
      for (area = dev_list; area; area = area->next)
228
        for(i = 0; i < area->size; i++)
229
          setsim_mem8(i + area->addr_compare, config.memory.pattern);
230 269 markom
    } else if (config.memory.type != MT_UNKNOWN) {
231 262 markom
      fprintf(stderr, "Invalid memory configuration type.\n");
232 361 markom
      exit(1);
233 221 markom
    }
234 242 markom
  }
235 262 markom
 
236 361 markom
  if(runtime.sim.filename) {
237
    unsigned long endaddr = 0xFFFFFFFF;
238
    endaddr = loadcode(runtime.sim.filename, 0, 0); /* MM170901 always load at address zero.  */
239
    if (endaddr == -1) {
240
      fprintf(stderr, "Problems loading boot code.\n");
241
      exit(1);
242
    }
243
  }
244 551 markom
 
245
#if !FAST_SIM /* We assume we have valid configuration with fsim*/
246 361 markom
  /* Disable gdb debugging, if debug module is not available.  */
247
  if (config.debug.gdb_enabled && !config.debug.enabled) {
248
    config.debug.gdb_enabled = 0;
249
    if (config.sim.verbose)
250
      fprintf (stderr, "WARNING: Debug module not enabled, cannot start gdb.\n");
251
  }
252 551 markom
#endif
253
 
254 550 markom
  if (config.debug.gdb_enabled)
255 479 markom
    gdbcomm_init ();
256 551 markom
 
257
#if !FAST_SIM /* We assume we have valid configuration with fsim*/
258 361 markom
  /* Enable dependency stats, if we want to do history analisis */
259 394 markom
  if (config.sim.history && !config.cpu.dependstats) {
260
    config.cpu.dependstats = 1;
261 361 markom
    if (config.sim.verbose)
262 394 markom
      fprintf (stderr, "WARNING: dependstats stats must be enabled to do history analisis.\n");
263 361 markom
  }
264 551 markom
#endif
265
 
266
#if !FAST_SIM /* We assume we have valid configuration with fsim*/  
267 361 markom
  /* Debug forces verbose */
268
  if (config.sim.debug && !config.sim.verbose) {
269
    config.sim.verbose = 1;
270
    fprintf (stderr, "WARNING: verbose turned on.\n");
271
  }
272 551 markom
#endif
273 361 markom
 
274
  /* Start VAPI before device initialization.  */
275
  if (config.vapi.enabled) {
276 551 markom
    runtime.vapi.enabled = 1;
277 361 markom
    vapi_init ();
278
    if (config.sim.verbose)
279 997 markom
      PRINTF ("VAPI started, waiting for clients.\n");
280 361 markom
  }
281 538 markom
 
282 479 markom
  sim_reset ();
283 424 markom
 
284 543 simons
  lock_memory_table ();
285
 
286 361 markom
  /* Wait till all test are connected.  */
287 551 markom
  if (runtime.vapi.enabled) {
288 361 markom
    int numu = vapi_num_unconnected (0);
289
    if (numu) {
290 997 markom
      PRINTF ("\nWaiting for VAPI tests with ids:\n");
291 361 markom
      vapi_num_unconnected (1);
292 997 markom
      PRINTF ("\n");
293 1308 phoenix
      while ((numu = vapi_num_unconnected (0))) {
294 361 markom
        vapi_check ();
295 997 markom
        PRINTF ("\rStill waiting for %i VAPI test(s) to connect.       ", numu);
296 361 markom
        usleep (100);
297
      }
298 997 markom
      PRINTF ("\n");
299 361 markom
    }
300 997 markom
    PRINTF ("All devices connected                         \n");
301 361 markom
  }
302
  /* simulator is initialized */
303
  runtime.sim.init = 0;
304 304 markom
}
305 7 jrydberg
 
306 304 markom
/* Cleanup */
307
void sim_done ()
308
{
309
  if (config.sim.profile) {
310 1350 nogj
    fprintf(runtime.sim.fprof,"-%08llX FFFFFFFF\n", runtime.sim.cycles);
311 361 markom
    fclose(runtime.sim.fprof);
312 304 markom
  }
313 547 markom
 
314 847 markom
  if (config.sim.mprofile) fclose(runtime.sim.fmprof);
315 361 markom
  if (config.sim.exe_log)   fclose(runtime.sim.fexe_log);
316 551 markom
  if (runtime.vapi.enabled)  vapi_done ();
317 426 markom
  done_memory_table ();
318 304 markom
  exit(0);
319
}
320
 
321 728 markom
/* Executes jobs in time queue */
322
static inline void do_scheduler ()
323
{
324
  void (*func)(int);
325
  int param;
326 632 ivang
 
327 728 markom
  /* Execute all jobs till now */
328
  do {
329
    func = SCHED_PEEK().func;
330
    param = SCHED_PEEK().param;
331
    SCHED_REMOVE();
332
    func (param);
333 884 markom
  } while (runtime.sim.cycles >= SCHED_PEEK().time);
334 728 markom
}
335 632 ivang
 
336 728 markom
/* Main function */
337 304 markom
int main(argc, argv)
338
     int argc;
339
     char *argv[];
340
{
341 361 markom
  srand(getpid());
342
  init_defconfig();
343
  if (parse_args(argc, argv)) {
344 997 markom
    PRINTF("Usage: %s [options] <filename>\n", argv[0]);
345
    PRINTF("Options:\n");
346
    PRINTF(" -v                   version and copyright note\n");
347
    PRINTF(" -i                   enable interactive command prompt\n");
348
    PRINTF(" --nosrv              do not launch JTAG proxy server\n"); /* (CZ) */
349
    PRINTF(" --srv <n>            launch JTAG proxy server on port <n>; [random]\n"); /* (CZ) */
350 551 markom
#if !FAST_SIM
351 997 markom
    PRINTF(" -f or --file         load script file [sim.cfg]\n");
352
    PRINTF(" --enable-profile     enable profiling.\n");
353
    PRINTF(" --enable-mprofile    enable memory profiling.\n");
354 551 markom
#endif
355 997 markom
    PRINTF(" --output-cfg         prints C structure of current\n");
356
    PRINTF("                      configuration to standard output\n");
357
    PRINTF("\nor   : %s ", argv[0]);
358 847 markom
    mp_help ();
359 997 markom
    PRINTF("\nor   : %s ", argv[0]);
360 847 markom
    prof_help ();
361 361 markom
    exit(-1);
362
  }
363 304 markom
 
364 551 markom
#if !FAST_SIM
365 304 markom
  /* Read configuration file.  */
366 361 markom
  if (!runtime.sim.script_file_specified)
367
    read_script_file ("sim.cfg");
368 883 markom
 
369
  /* Overide parameters with command line ones */
370
  if (runtime.simcmd.profile) config.sim.profile = 1;
371
  if (runtime.simcmd.mprofile) config.sim.mprofile = 1;
372
 
373 424 markom
  if (!runtime.sim.script_file_specified && config.sim.verbose)
374
    fprintf (stderr, "WARNING: No config file read, assuming default configuration.\n");
375 551 markom
#else
376 997 markom
  PRINTF ("\n\tNOTE: running fast sim with fixed configuration!\n\n");
377 551 markom
#endif
378 549 markom
  if (runtime.sim.output_cfg) {
379
    output_cfg (stdout);
380
    exit (0);
381
  }
382 304 markom
  print_config();
383 336 markom
  sim_init ();
384 304 markom
  signal(SIGINT, ctrl_c);
385 335 markom
 
386 1353 nogj
  runtime.sim.hush = 1;
387
  runtime.sim.cont_run = -1;
388
 
389 361 markom
  while(1) {
390 1353 nogj
    if (runtime.sim.iprompt)
391
        handle_sim_command();
392 7 jrydberg
 
393 714 markom
    { /* Needed by execution */
394
      extern int do_stats;
395
      do_stats = config.cpu.dependstats || config.cpu.superscalar || config.cpu.dependstats
396
              || config.sim.history || config.sim.exe_log;
397
    }
398
 
399 361 markom
    /* MM: 'run -1' means endless execution.  */
400 884 markom
    while(runtime.sim.cont_run) {
401 557 markom
      IFF (config.debug.enabled) {
402 1353 nogj
        du_clock(); // reset watchpoints
403 884 markom
        if (runtime.cpu.stalled) {
404 557 markom
          if(config.debug.gdb_enabled) {
405
            BlockJTAG();
406
            HandleServerSocket(false);
407 605 markom
          } else {
408
            fprintf (stderr, "WARNING: CPU stalled and gdb connection not enabled.");
409 884 markom
            runtime.sim.cont_run = 0;
410 605 markom
          }
411 557 markom
          continue;
412
        }
413 479 markom
      }
414 127 chris
 
415 537 markom
      /* Each cycle has counter of mem_cycles; this value is joined with cycles
416
         at the end of the cycle; no sim originated memory accesses should be
417
         performed inbetween. */
418 884 markom
      runtime.sim.mem_cycles = 0;
419 557 markom
      if (!config.pm.enabled || !testsprbits(SPR_PMR, SPR_PMR_DME | SPR_PMR_SME)) {
420 884 markom
        if (runtime.sim.cont_run > 0) runtime.sim.cont_run--;
421 599 simons
        pic_clock ();
422
        if (cpu_clock ()) break;
423
        if (config.dc.enabled) dc_clock();
424
        if (config.ic.enabled) ic_clock();
425 261 markom
      }
426 304 markom
 
427 549 markom
      if (config.dmas) dma_clock();
428
      if (config.ethernets) eth_clock();
429
      if (config.ngpios) gpio_clock();
430 551 markom
      if (config.vapi.enabled && runtime.vapi.enabled) vapi_check();
431 557 markom
      if (config.debug.gdb_enabled) HandleServerSocket(false); /* block & check_stdin = false */
432
      IFF(config.debug.enabled)
433 479 markom
        if (testsprbits(SPR_DMR1, SPR_DMR1_ST)) set_stall_state (1);
434 557 markom
 
435 884 markom
      runtime.sim.cycles += runtime.sim.mem_cycles;
436
      if (runtime.sim.cycles >= SCHED_PEEK().time) do_scheduler ();
437 1353 nogj
      if (!runtime.sim.hush) dumpreg();
438 361 markom
    }
439 1353 nogj
    runtime.sim.hush = 0;
440 361 markom
    fflush(stdout);
441 997 markom
    runtime.sim.fout = stdout;
442 2 cvs
 
443 551 markom
    if (!runtime.sim.iprompt)  /* non-interactive quit */
444 361 markom
      sim_done();
445
  }
446
  sim_done();
447 2 cvs
}
448 7 jrydberg
 
449
 

powered by: WebSVN 2.1.0

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