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 1506

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

powered by: WebSVN 2.1.0

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