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

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc1/] [or1ksim/] [sim-config.c] - Blame information for rev 1580

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1358 nogj
/* sim-config.c -- Simulator configuration
2 645 markom
   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 configuration. Eventually this one will be a lot bigger. */
21
 
22
#include <stdlib.h>
23 805 markom
#include <limits.h>
24 1308 phoenix
#include <string.h>
25
#include <ctype.h>
26
 
27 1350 nogj
#include "config.h"
28
 
29
#ifdef HAVE_INTTYPES_H
30
#include <inttypes.h>
31
#endif
32
 
33
#include "port.h"
34
#include "arch.h"
35 645 markom
#include "sim-config.h"
36
#include "abstract.h"
37 1432 nogj
#include "opcode/or32.h"
38
#include "spr_defs.h"
39
#include "execute.h"
40 645 markom
#include "sprs.h"
41
#include "pic.h"
42
#include "stats.h"
43
#include "icache_model.h"
44
#include "dcache_model.h"
45
 
46
#include "profiler.h"
47
#include "mprofiler.h"
48 897 markom
#include "cuc.h"
49 645 markom
 
50 1358 nogj
#include "debug.h"
51
 
52 1459 nogj
DEFAULT_DEBUG_CHANNEL(config);
53
 
54 1358 nogj
#define WARNING(s) fprintf (stderr, "WARNING: config.%s: %s\n", cur_section->name, (s))
55 645 markom
#define MERROR(s) {fprintf (stderr, "ERROR: %s\n", s); if (runtime.sim.init) exit (1);}
56
 
57
struct config config;
58
struct runtime runtime;
59
 
60 1358 nogj
struct config_section *cur_section;
61 645 markom
 
62 1358 nogj
struct config_section *sections = NULL;
63
 
64 1557 nogj
extern char *or1ksim_ver;
65
 
66 1550 nogj
void init_defconfig(void)
67 645 markom
{
68
  memset(&config, 0, sizeof(config));
69
  /* Sim */
70
  config.sim.exe_log = 0;
71 672 markom
  config.sim.exe_log_type = EXE_LOG_HARDWARE;
72
  config.sim.exe_log_start = 0;
73
  config.sim.exe_log_end = 0;
74
  config.sim.exe_log_marker = 0;
75 645 markom
  strcpy (config.sim.exe_log_fn, "executed.log");
76 1098 markom
  config.sim.profile = 0;
77
  config.sim.mprofile = 0;
78 645 markom
 
79
  config.sim.debug = 0;
80
  config.sim.verbose = 1;
81
 
82
  strcpy (config.sim.prof_fn, "sim.profile");
83
  strcpy (config.sim.mprof_fn, "sim.mprofile");
84 823 ivang
  strcpy (config.sim.fstdout, "stdout.txt");
85 645 markom
  strcpy (runtime.sim.script_fn, "(default)");
86
  config.sim.clkcycle_ps = 4000; /* 4000 for 4ns (250MHz) */
87 805 markom
  if (config.sim.clkcycle_ps) config.sim.system_kfreq = (long)((1000000000.0 / (double)config.sim.clkcycle_ps));
88
  else config.sim.system_kfreq = INT_MAX;
89
  if (config.sim.system_kfreq <= 0) config.sim.system_kfreq = 1;
90 645 markom
 
91
  /* IMMU & DMMU*/
92
  config.immu.enabled = 0;
93 856 markom
  config.immu.hitdelay = 1;
94
  config.immu.missdelay = 1;
95 645 markom
  config.dmmu.enabled = 0;
96 856 markom
  config.dmmu.hitdelay = 1;
97
  config.dmmu.missdelay = 1;
98 645 markom
 
99
  /* IC & DC */
100
  config.ic.enabled = 0;
101 856 markom
  config.ic.hitdelay = 1;
102
  config.ic.missdelay = 1;
103 645 markom
  config.ic.nways = 0;
104
  config.ic.nsets = 0;
105
  config.ic.ustates = 0;
106
  config.dc.enabled = 0;
107 856 markom
  config.dc.load_hitdelay = 2;
108
  config.dc.load_missdelay = 2;
109 645 markom
  config.dc.nways = 0;
110
  config.dc.nsets = 0;
111
  config.dc.ustates = 0;
112
  config.dc.store_hitdelay = 0;
113
  config.dc.store_missdelay = 0;
114 876 rherveille
 
115 645 markom
  /* CPU */
116
  config.cpu.superscalar = 0;
117
  config.sim.history = 0;
118
  config.cpu.hazards = 0;
119
  config.cpu.dependstats = 0;
120
  config.cpu.sbuf_len = 0;
121
  config.cpu.upr = SPR_UPR_UP | SPR_UPR_DCP | SPR_UPR_ICP | SPR_UPR_DMP
122
                 | SPR_UPR_IMP | SPR_UPR_OB32P | SPR_UPR_DUP | SPR_UPR_PICP
123
                 | SPR_UPR_PMP | SPR_UPR_TTP;
124 912 lampret
  config.cpu.sr = 0x00008001;
125 645 markom
 
126
  /* Debug */
127
  config.debug.enabled = 0;
128
  config.debug.gdb_enabled = 0;
129
  config.debug.server_port = 0;
130
 
131
  /* VAPI */
132
  config.vapi.enabled = 0;
133
  strcpy (config.vapi.vapi_fn, "vapi.log");
134
 
135
  /* PM */
136
  config.pm.enabled = 0;
137 897 markom
 
138
  /* CUC */
139
  strcpy (config.cuc.timings_fn, "virtex.tim");
140
  config.cuc.memory_order = MO_STRONG;
141
  config.cuc.calling_convention = 1;
142
  config.cuc.enable_bursts = 1;
143
  config.cuc.no_multicycle = 1;
144 970 simons
 
145 645 markom
  /* Configure runtime */
146
  memset(&runtime, 0, sizeof(runtime));
147
 
148
  /* Sim */
149
  runtime.sim.fexe_log = NULL;
150
  runtime.sim.iprompt = 0;
151
  runtime.sim.fprof = NULL;
152
  runtime.sim.fmprof = NULL;
153
  runtime.sim.init = 1;
154 998 markom
  runtime.sim.fout = stdout;
155 645 markom
  runtime.sim.script_file_specified = 0;
156 883 markom
  runtime.simcmd.profile = 0;
157
  runtime.simcmd.mprofile = 0;
158 645 markom
 
159
  /* VAPI */
160
  runtime.vapi.vapi_file = NULL;
161
  runtime.vapi.enabled = 0;
162
}
163
 
164 1557 nogj
static void version(void)
165
{
166
  PRINTF ("\n");
167
  PRINTF ("OpenRISC 1000 (OR32) Architectural Simulator, version %s\n",
168
          or1ksim_ver);
169
  PRINTF ("Copyright (C) 1999 Damjan Lampret, lampret@opencores.org\n");
170
  PRINTF ("Copyright (C) 2000 Damjan Lampret, lampret@opencores.org\n");
171
  PRINTF ("                   Jimmy Chen-Min Chen, jimmy@ee.nctu.edu.tw\n");
172
  PRINTF ("                   Johan Rydberg, johan.rydberg@insight.se\n");
173
  PRINTF ("                   Marko Mlinar, markom@opencores.org\n");
174
  PRINTF ("Copyright (C) 2001 Simon Srot, simons@opencores.org\n");
175
  PRINTF ("                   Marko Mlinar, markom@opencores.org\n");
176
  PRINTF ("Copyright (C) 2002 Marko Mlinar, markom@opencores.org\n");
177
  PRINTF ("                   Simon Srot, simons@opencores.org\n");
178
  PRINTF ("Visit http://www.opencores.org for more information about ");
179
  PRINTF ("OpenRISC 1000 and\nother open source cores.\n\n");
180
  PRINTF ("This software comes with ABSOLUTELY NO WARRANTY; for ");
181
  PRINTF ("details see COPYING.\nThis is free software, and you ");
182
  PRINTF ("are welcome to redistribute it under certain\nconditions; ");
183
  PRINTF ("for details see COPYING.\n");
184
}
185
 
186 645 markom
int parse_args(int argc, char *argv[])
187
{
188
  argv++; argc--;
189
  while (argc) {
190 847 markom
    if (strcmp(*argv, "profiler") == 0) {
191
      exit (main_profiler (argc, argv));
192
    } else
193
    if (strcmp(*argv, "mprofiler") == 0) {
194
      exit (main_mprofiler (argc, argv));
195
    } else
196
    if (*argv[0] != '-') {
197 645 markom
      runtime.sim.filename = argv[0];
198
      argc--;
199
      argv++;
200
    } else
201
    if (strcmp(*argv, "-f") == 0 || strcmp(*argv, "--file") == 0) {
202
      argv++; argc--;
203
      if (argv[0]) {
204
        read_script_file(argv[0]);
205
        argv++; argc--;
206
      } else {
207
        fprintf(stderr, "Configure filename not specified!\n");
208
        return 1;
209
      }
210
    } else
211
    if (strcmp(*argv, "--nosrv") == 0) {  /* (CZ) */
212
      config.debug.gdb_enabled = 0;
213
      argv++; argc--;
214
    } else
215
    if (strcmp(*argv, "--srv") == 0) {  /* (CZ) */
216
      char *s;
217
      if(!--argc)
218
        return 1;
219
      config.debug.enabled = 1;
220 1205 phoenix
      config.debug.gdb_enabled = 1;
221 645 markom
      config.debug.server_port = strtol(*(++argv),&s,10);
222
      if(*s)
223
        return 1;
224
      argv++; argc--;
225
    } else
226
    if (strcmp(*argv, "-i") == 0) {
227
      runtime.sim.iprompt = 1;
228
      argv++; argc--;
229
    } else
230
    if (strcmp(*argv, "-v") == 0) {
231
      version();
232
      exit(0);
233
    } else
234 883 markom
    if (strcmp(*argv, "--enable-profile") == 0) {
235
      runtime.simcmd.profile = 1;
236 645 markom
      argv++; argc--;
237
    } else
238 883 markom
    if (strcmp(*argv, "--enable-mprofile") == 0) {
239
      runtime.simcmd.mprofile = 1;
240 645 markom
      argv++; argc--;
241
    } else
242 1389 nogj
    if (strcmp(*argv, "-d") == 0) {
243
      parse_dbchs(*(++argv));
244
      argv++; argc -= 2;
245 645 markom
    } else {
246
      fprintf(stderr, "Unknown option: %s\n", *argv);
247
      return 1;
248
    }
249
  }
250
 
251
  if (!argc)
252
    return 0;
253
 
254
  return 0;
255
}
256
 
257 1550 nogj
void print_config(void)
258 645 markom
{
259
  if (config.sim.verbose) {
260
    char temp[20];
261 997 markom
    PRINTF("Verbose on, ");
262 645 markom
    if (config.sim.debug)
263 997 markom
      PRINTF("simdebug on, ");
264 645 markom
    else
265 997 markom
      PRINTF("simdebug off, ");
266 645 markom
    if (runtime.sim.iprompt)
267 997 markom
      PRINTF("interactive prompt on\n");
268 645 markom
    else
269 997 markom
      PRINTF("interactive prompt off\n");
270 645 markom
 
271 997 markom
    PRINTF("Machine initialization...\n");
272 645 markom
    generate_time_pretty (temp, config.sim.clkcycle_ps);
273 997 markom
    PRINTF("Clock cycle: %s\n", temp);
274 1506 nogj
    if (cpu_state.sprs[SPR_UPR] & SPR_UPR_DCP)
275 997 markom
      PRINTF("Data cache present.\n");
276 645 markom
    else
277 997 markom
      PRINTF("No data cache.\n");
278 1506 nogj
    if (cpu_state.sprs[SPR_UPR] & SPR_UPR_ICP)
279 997 markom
      PRINTF("Insn cache tag present.\n");
280 645 markom
    else
281 997 markom
      PRINTF("No instruction cache.\n");
282 645 markom
    if (config.bpb.enabled)
283 997 markom
      PRINTF("BPB simulation on.\n");
284 645 markom
    else
285 997 markom
      PRINTF("BPB simulation off.\n");
286 645 markom
    if (config.bpb.btic)
287 997 markom
      PRINTF("BTIC simulation on.\n");
288 645 markom
    else
289 997 markom
      PRINTF("BTIC simulation off.\n");
290 645 markom
  }
291
}
292
 
293 1358 nogj
struct config_param {
294
  char *name;
295
  enum param_t type;
296
  void (*func)(union param_val, void *dat);
297
  struct config_param *next;
298 645 markom
};
299
 
300 1358 nogj
void base_include (union param_val val, void *dat) {
301
  read_script_file (val.str_val);
302
  cur_section = NULL;
303
}
304 645 markom
 
305 1358 nogj
/*----------------------------------------------[ Simulator configuration ]---*/
306
void sim_debug (union param_val val, void *dat) {
307
  config.sim.debug = val.int_val;
308
}
309 645 markom
 
310 1358 nogj
void sim_verbose (union param_val val, void *dat) {
311
  config.sim.verbose = val.int_val;
312
}
313 645 markom
 
314 1358 nogj
void sim_profile (union param_val val, void *dat) {
315
  config.sim.profile = val.int_val;
316
}
317 645 markom
 
318 1358 nogj
void sim_prof_fn (union param_val val, void *dat) {
319
  strcpy(config.sim.prof_fn, val.str_val);
320
}
321 876 rherveille
 
322 1358 nogj
void sim_mprofile (union param_val val, void *dat) {
323
  config.sim.mprofile = val.int_val;
324 645 markom
}
325
 
326 1358 nogj
void sim_mprof_fn (union param_val val, void *dat) {
327
  strcpy(config.sim.mprof_fn, val.str_val);
328 645 markom
}
329
 
330 1358 nogj
void sim_history (union param_val val, void *dat) {
331
  config.sim.history = val.int_val;
332 645 markom
}
333
 
334 1358 nogj
void sim_exe_log (union param_val val, void *dat) {
335
  config.sim.exe_log = val.int_val;
336 645 markom
}
337
 
338 1358 nogj
void sim_exe_log_type (union param_val val, void *dat) {
339
  if (strcmp (val.str_val, "default") == 0)
340 672 markom
    config.sim.exe_log_type = EXE_LOG_HARDWARE;
341 1358 nogj
  else if (strcmp (val.str_val, "hardware") == 0)
342 672 markom
    config.sim.exe_log_type = EXE_LOG_HARDWARE;
343 1358 nogj
  else if (strcmp (val.str_val, "simple") == 0)
344 675 markom
    config.sim.exe_log_type = EXE_LOG_SIMPLE;
345 1358 nogj
  else if (strcmp (val.str_val, "software") == 0) {
346 672 markom
    config.sim.exe_log_type = EXE_LOG_SOFTWARE;
347
  } else {
348
    char tmp[200];
349 1549 nogj
    sprintf (tmp, "invalid execution log type '%s'.\n", val.str_val);
350 1358 nogj
    CONFIG_ERROR(tmp);
351 672 markom
  }
352
}
353
 
354 1358 nogj
void sim_exe_log_start (union param_val val, void *dat) {
355 1580 nogj
  config.sim.exe_log_start = val.longlong_val;
356 645 markom
}
357
 
358 1358 nogj
void sim_exe_log_end (union param_val val, void *dat) {
359 1580 nogj
  config.sim.exe_log_end = val.longlong_val;
360 645 markom
}
361
 
362 1358 nogj
void sim_exe_log_marker (union param_val val, void *dat) {
363
  config.sim.exe_log_marker = val.int_val;
364 645 markom
}
365
 
366 1358 nogj
void sim_exe_log_fn (union param_val val, void *dat) {
367
  strcpy(config.sim.exe_log_fn, val.str_val);
368 645 markom
}
369
 
370 1358 nogj
void sim_clkcycle (union param_val val, void *dat) {
371
  int len = strlen (val.str_val);
372
  int pos = len - 1;
373
  long time;
374
  if (len < 2) goto err;
375
  if (val.str_val[pos--] != 's') goto err;
376
  switch (val.str_val[pos--]) {
377
    case 'p': time = 1; break;
378
    case 'n': time = 1000; break;
379
    case 'u': time = 1000000; break;
380
    case 'm': time = 1000000000; break;
381
  default:
382
    goto err;
383
  }
384
  val.str_val[pos + 1] = 0;
385
  config.sim.clkcycle_ps = time * atol (val.str_val);
386
  return;
387
err:
388
  CONFIG_ERROR("invalid time format.");
389 645 markom
}
390
 
391 1358 nogj
void sim_stdout (union param_val val, void *dat) {
392
  strcpy(config.sim.fstdout, val.str_val);
393 645 markom
}
394
 
395 1358 nogj
void reg_sim_sec (void) {
396
  struct config_section *sec = reg_config_sec("sim", NULL, NULL);
397 645 markom
 
398 1358 nogj
  reg_config_param(sec, "debug", paramt_int, sim_debug);
399
  reg_config_param(sec, "verbose", paramt_int, sim_verbose);
400
  reg_config_param(sec, "profile", paramt_int, sim_profile);
401
  reg_config_param(sec, "prof_fn", paramt_str, sim_prof_fn);
402
  reg_config_param(sec, "mprofile", paramt_int, sim_mprofile);
403
  reg_config_param(sec, "mprof_fn", paramt_str, sim_mprof_fn);
404
  reg_config_param(sec, "history", paramt_int, sim_history);
405
  reg_config_param(sec, "exe_log", paramt_int, sim_exe_log);
406
  reg_config_param(sec, "exe_log_type", paramt_word, sim_exe_log_type);
407 1580 nogj
  reg_config_param(sec, "exe_log_start", paramt_longlong, sim_exe_log_start);
408
  reg_config_param(sec, "exe_log_end", paramt_longlong, sim_exe_log_end);
409 1358 nogj
  reg_config_param(sec, "exe_log_marker", paramt_int, sim_exe_log_marker);
410
  reg_config_param(sec, "exe_log_fn", paramt_str, sim_exe_log_fn);
411
  reg_config_param(sec, "clkcycle", paramt_word, sim_clkcycle);
412
  reg_config_param(sec, "stdout", paramt_str, sim_stdout);
413 645 markom
}
414
 
415 1358 nogj
/*----------------------------------------------------[ CPU configuration ]---*/
416
void cpu_ver (union param_val val, void *dat) {
417
  config.cpu.ver = val.int_val;
418 645 markom
}
419
 
420 1358 nogj
void cpu_rev (union param_val val, void *dat) {
421
  config.cpu.rev = val.int_val;
422 645 markom
}
423
 
424 1358 nogj
void cpu_upr (union param_val val, void *dat) {
425
  config.cpu.upr = val.int_val;
426 725 ivang
}
427
 
428 1358 nogj
void cpu_sr (union param_val val, void *dat) {
429
  config.cpu.sr = val.int_val;
430 645 markom
}
431
 
432 1358 nogj
void cpu_hazards (union param_val val, void *dat) {
433
  config.cpu.hazards = val.int_val;
434 645 markom
}
435
 
436 1358 nogj
void cpu_superscalar (union param_val val, void *dat) {
437
  config.cpu.superscalar = val.int_val;
438 645 markom
}
439
 
440 1358 nogj
void cpu_dependstats (union param_val val, void *dat) {
441
  config.cpu.dependstats = val.int_val;
442 645 markom
}
443
 
444 1358 nogj
void cpu_sbuf_len (union param_val val, void *dat) {
445
  if (val.int_val >= MAX_SBUF_LEN) {
446
    config.cpu.sbuf_len = MAX_SBUF_LEN - 1;
447
    WARNING("sbuf_len too large; truncated.");
448
  } else if (val.int_val < 0) {
449
    config.cpu.sbuf_len = 0;
450
    WARNING("sbuf_len negative; disabled.");
451
  } else
452
    config.cpu.sbuf_len = val.int_val;
453 723 ivang
}
454
 
455 1358 nogj
void reg_cpu_sec(void)
456
{
457
  struct config_section *sec = reg_config_sec("cpu", NULL, NULL);
458 723 ivang
 
459 1358 nogj
  reg_config_param(sec, "ver", paramt_int, cpu_ver);
460
  reg_config_param(sec, "rev", paramt_int, cpu_rev);
461
  reg_config_param(sec, "upr", paramt_int, cpu_upr);
462
  reg_config_param(sec, "sr", paramt_int, cpu_sr);
463
  reg_config_param(sec, "hazards", paramt_int, cpu_hazards);
464
  reg_config_param(sec, "superscalar", paramt_int, cpu_superscalar);
465
  reg_config_param(sec, "dependstats", paramt_int, cpu_dependstats);
466
  reg_config_param(sec, "sbuf_len", paramt_int, cpu_sbuf_len);
467 645 markom
}
468
 
469 1358 nogj
void reg_config_secs(void)
470
{
471
  reg_config_param(reg_config_sec("base", NULL, NULL), "include", paramt_str,
472
                   base_include);
473 645 markom
 
474 1358 nogj
  reg_sim_sec();
475
  reg_cpu_sec();
476
  reg_memory_sec();
477
  reg_mc_sec();
478
  reg_uart_sec();
479
  reg_dma_sec();
480
  reg_debug_sec();
481
  reg_vapi_sec();
482
  reg_ethernet_sec();
483
  reg_immu_sec();
484
  reg_dmmu_sec();
485
  reg_ic_sec();
486
  reg_dc_sec();
487
  reg_gpio_sec();
488
  reg_bpb_sec();
489
  reg_pm_sec();
490
  reg_vga_sec();
491
  reg_fb_sec();
492
  reg_kbd_sec();
493
  reg_ata_sec();
494
  reg_cuc_sec();
495 645 markom
}
496
 
497 1459 nogj
/* Returns a user friendly string of type */
498
static char *get_paramt_str(enum param_t type)
499
{
500
  switch(type) {
501
  case paramt_int:
502
    return "integer";
503 1580 nogj
  case paramt_longlong:
504
    return "longlong";
505 1459 nogj
  case paramt_addr:
506
    return "address";
507
  case paramt_str:
508
    return "string";
509
  case paramt_word:
510
    return "word";
511
  case paramt_none:
512
    return "none";
513
  }
514
  return "";
515
}
516
 
517 1358 nogj
void reg_config_param(struct config_section *sec, const char *param,
518
                      enum param_t type,
519
                      void (*param_cb)(union param_val, void *))
520
{
521
  struct config_param *new = malloc(sizeof(struct config_param));
522 645 markom
 
523 1459 nogj
  TRACE("Registering config param `%s' to section `%s', type %s\n", param,
524
        sec->name, get_paramt_str(type));
525
 
526 1358 nogj
  if(!new) {
527
    fprintf(stderr, "Out-of-memory\n");
528
    exit(1);
529
  }
530 645 markom
 
531 1358 nogj
  if(!(new->name = strdup(param))) {
532
    fprintf(stderr, "Out-of-memory\n");
533
    exit(1);
534
  }
535 645 markom
 
536 1358 nogj
  new->func = param_cb;
537
  new->type = type;
538 645 markom
 
539 1358 nogj
  new->next = sec->params;
540
  sec->params = new;
541 645 markom
}
542
 
543 1358 nogj
struct config_section *reg_config_sec(const char *section,
544
                                      void *(*sec_start)(void),
545
                                      void (*sec_end)(void *))
546
{
547
  struct config_section *new = malloc(sizeof(struct config_section));
548 645 markom
 
549 1459 nogj
  TRACE("Registering config section `%s'\n", section);
550
 
551 1358 nogj
  if(!new) {
552
    fprintf(stderr, "Out-of-memory\n");
553
    exit(1);
554
  }
555 645 markom
 
556 1358 nogj
  if(!(new->name = strdup(section))) {
557
    fprintf(stderr, "Out-of-memory\n");
558
    exit(1);
559 645 markom
  }
560
 
561 1358 nogj
  new->next = sections;
562
  new->sec_start = sec_start;
563
  new->sec_end = sec_end;
564
  new->params = NULL;
565 645 markom
 
566 1358 nogj
  sections = new;
567 645 markom
 
568 1358 nogj
  return new;
569 645 markom
}
570
 
571 1358 nogj
static void switch_param(char *param, struct config_param *cur_param)
572
{
573
  char *end_p;
574
  union param_val val;
575 645 markom
 
576 1358 nogj
    /* Skip over an = sign if it exists */
577
  if(*param == '=') {
578
    param++;
579
    while(*param && isspace(*param)) param++;
580 645 markom
  }
581
 
582 1358 nogj
  switch (cur_param->type) {
583
  case paramt_int:
584
    val.int_val = strtol(param, NULL, 0);
585 1580 nogj
    break;
586
  case paramt_longlong:
587
    val.longlong_val = strtoll(param, NULL, 0);
588
    break;
589 1358 nogj
  case paramt_addr:
590
    val.addr_val = strtoul(param, NULL, 0);
591
    break;
592
  case paramt_str:
593
    if(*param != '"') {
594
      CONFIG_ERROR("String value expected\n");
595
      return;
596
    }
597 645 markom
 
598 1358 nogj
    param++;
599
    end_p = param;
600
    while(*end_p && (*end_p != '"')) end_p++;
601
    *end_p = '\0';
602
    val.str_val = param;
603
    break;
604
  case paramt_word:
605
    end_p = param;
606
    while(*end_p && !isspace(*end_p)) end_p++;
607
    *end_p = '\0';
608
    val.str_val = param;
609
    break;
610
  case paramt_none:
611
    break;
612
  }
613 645 markom
 
614 1358 nogj
  cur_param->func(val, cur_section->dat);
615 645 markom
}
616
 
617
/* Read environment from a script file. Does not fail - assumes default configuration instead.
618
   The syntax of script file is:
619
   param = value
620
   section x
621
     data
622
     param = value
623
   end
624
 
625
   Example:
626
   section mc
627
     memory_table_file = sim.mem
628
     enable = 1
629
     POC = 0x47892344
630
   end
631
 
632
 */
633
 
634
void read_script_file (char *filename)
635
{
636
  FILE *f;
637
  char *home = getenv("HOME");
638
  char ctmp[STR_SIZE];
639
  int local = 1;
640 1358 nogj
  cur_section = NULL;
641 645 markom
 
642
  sprintf(ctmp, "%s/.or1k/%s", home, filename);
643 1557 nogj
  if ((f = fopen (filename, "rt")) || (home && (f = fopen (ctmp, "rt")))) {
644 1455 nogj
    if (config.sim.verbose)
645 997 markom
      PRINTF ("Reading script file from '%s'...\n", local ? filename : ctmp);
646 645 markom
    strcpy (runtime.sim.script_fn, local ? filename : ctmp);
647
 
648
    while (!feof(f)) {
649
      char param[STR_SIZE];
650 1308 phoenix
      if (fscanf(f, "%s ", param) != 1) break;
651 1358 nogj
      /* Is this a section? */
652 645 markom
      if (strcmp (param, "section") == 0) {
653 1358 nogj
        struct config_section *cur;
654
        cur_section = NULL;
655 1308 phoenix
        if (fscanf (f, "%s\n", param) != 1) {
656 645 markom
          fprintf (stderr, "%s: ERROR: Section name required.\n", local ? filename : ctmp);
657
          exit (1);
658
        }
659 1459 nogj
        TRACE("Came across section `%s'\n", param);
660 1358 nogj
        for (cur = sections; cur; cur = cur->next)
661
          if (strcmp (cur->name, param) == 0) {
662
            cur_section = cur;
663 645 markom
            break;
664
          }
665 1358 nogj
        if (!cur) {
666 1561 nogj
          fprintf (stderr, "WARNING: config: Unknown section: %s; ignoring.\n",
667
                   param);
668 645 markom
          /* just skip section */
669 1561 nogj
          while (fscanf (f, "%s\n", param) == 1 && strcmp (param, "end"));
670 1358 nogj
        } else {
671
          cur->dat = NULL;
672
          if (cur->sec_start)
673
            cur->dat = cur->sec_start();
674 645 markom
        }
675
      } else if (strcmp (param, "end") == 0) {
676 1358 nogj
        if(cur_section->sec_end)
677
          cur_section->sec_end(cur_section->dat);
678
        cur_section = NULL;
679 645 markom
      } else if (strncmp (param, "/*", 2) == 0) {
680
        char c0 = 0, c1 = 0;
681
        while (c0 != '*' || c1 != '/') {
682
          c0 = c1;
683
          c1 = fgetc(f);
684
          if (feof(f)) {
685
            fprintf (stderr, "%s: ERROR: Comment reached EOF.\n", local ? filename : ctmp);
686
            exit (1);
687
          }
688
        }
689
      } else {
690 1358 nogj
        struct config_param *cur_param;
691
        char *cur_p;
692 1459 nogj
        TRACE("Came across parameter `%s' in section `%s'\n", param,
693
              cur_section->name);
694 1358 nogj
        for (cur_param = cur_section->params; cur_param; cur_param = cur_param->next)
695
          if (strcmp (cur_param->name, param) == 0) {
696 645 markom
            break;
697
          }
698 1358 nogj
        if (!cur_param) {
699 645 markom
          char tmp[200];
700
          sprintf (tmp, "Invalid parameter: %s; ignoring.\n", param);
701
          WARNING(tmp);
702
          while (fgetc(f) != '\n' || feof(f));
703
          continue;
704
        }
705
 
706 1358 nogj
        if(cur_param->type == paramt_none)
707
          continue;
708
 
709 645 markom
        /* Parse parameter value */
710 1358 nogj
        cur_p = fgets (param, STR_SIZE, f);
711
 
712
        while(*cur_p && isspace(*cur_p)) cur_p++;
713
 
714
        switch_param(cur_p, cur_param);
715 645 markom
      }
716
    }
717
    fclose (f);
718
    runtime.sim.script_file_specified = 1;
719
  } else
720
    if (config.sim.verbose)
721
      fprintf (stderr, "WARNING: Cannot read script file from '%s',\nneither '%s'.\n", filename, ctmp);
722
}
723
 
724
/* Utility for execution of set sim command.  */
725 1353 nogj
static int set_config (int argc, char **argv)
726 645 markom
{
727 1358 nogj
  struct config_section *cur;
728
  struct config_param *cur_param;
729 1353 nogj
 
730
  if (argc < 2) return 1;
731
 
732
  PRINTF ("sec:%s\n", argv[1]);
733 1358 nogj
  cur_section = NULL;
734
  for (cur = sections; cur; cur = cur->next)
735
    if (strcmp (cur->name, argv[1]) == 0) {
736
      cur_section = cur;
737 645 markom
      break;
738
    }
739
 
740 1358 nogj
  if (!cur_section) return 1;
741 1353 nogj
 
742
  if (argc < 3) return 2;
743 645 markom
 
744 1353 nogj
  PRINTF ("item:%s\n", argv[2]);
745 645 markom
  {
746 1358 nogj
    for (cur_param = cur->params; cur_param; cur_param = cur_param->next)
747
      if (strcmp (cur_param->name, argv[2]) == 0) {
748 645 markom
        break;
749
      }
750 1358 nogj
    if (!cur_param) return 2;
751 645 markom
 
752
    /* Parse parameter value */
753 1358 nogj
    if (cur_param->type) {
754 1353 nogj
      if (argc < 4) return 3;
755
      PRINTF ("params:%s\n", argv[3]);
756
    }
757 1358 nogj
 
758
    switch_param(argv[3], cur_param);
759 645 markom
  }
760
  return 0;
761
}
762
 
763
/* Executes set sim command, displays error.  */
764 1353 nogj
void set_config_command(int argc, char **argv)
765 645 markom
{
766 1358 nogj
  struct config_section *cur;
767
  struct config_param *cur_param;
768 1353 nogj
 
769
  switch (set_config (argc, argv)) {
770 645 markom
    case 1:
771 997 markom
      PRINTF ("Invalid or missing section name.  One of valid sections must be specified:\n");
772 1358 nogj
      for (cur = sections; cur; cur = cur->next)
773
        PRINTF ("%s ", cur->name);
774 997 markom
      PRINTF ("\n");
775 645 markom
      break;
776
    case 2:
777 997 markom
      PRINTF ("Invalid or missing item name.  One of valid items must be specified:\n");
778 1358 nogj
      for (cur_param = cur_section->params; cur_param; cur_param = cur_param->next)
779
        PRINTF ("%s ", cur_param->name);
780 997 markom
      PRINTF ("\n");
781 645 markom
      break;
782
    case 3:
783 997 markom
      PRINTF ("Invalid parameters specified.\n");
784 645 markom
      break;
785
  }
786
}
787
 

powered by: WebSVN 2.1.0

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