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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [sim-config.c] - Blame information for rev 1595

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

powered by: WebSVN 2.1.0

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