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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc1/] [or1ksim/] [sim-config.c] - Blame information for rev 1550

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

powered by: WebSVN 2.1.0

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