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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [bochs486/] [gui/] [paramtree.cc] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
/////////////////////////////////////////////////////////////////////////
2
// $Id: paramtree.cc 11634 2013-02-17 08:27:43Z vruppert $
3
/////////////////////////////////////////////////////////////////////////
4
//
5
//  Copyright (C) 2010-2013  The Bochs Project
6
//
7
//  This library is free software; you can redistribute it and/or
8
//  modify it under the terms of the GNU Lesser General Public
9
//  License as published by the Free Software Foundation; either
10
//  version 2 of the License, or (at your option) any later version.
11
//
12
//  This library is distributed in the hope that it will be useful,
13
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
//  Lesser General Public License for more details.
16
//
17
//  You should have received a copy of the GNU Lesser General Public
18
//  License along with this library; if not, write to the Free Software
19
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20
//
21
/////////////////////////////////////////////////////////////////////////
22
 
23
#include "bochs.h"
24
#include "paramtree.h"
25
 
26
/////////////////////////////////////////////////////////////////////////
27
// define methods of bx_param_* and family
28
/////////////////////////////////////////////////////////////////////////
29
 
30
extern bx_simulator_interface_c *SIM;
31
extern logfunctions *siminterface_log;
32
extern bx_list_c *root_param;
33
#define LOG_THIS siminterface_log->
34
 
35
const char* bx_param_c::default_text_format = NULL;
36
 
37
bx_param_c::bx_param_c(Bit32u id, const char *param_name, const char *param_desc)
38
  : bx_object_c(id),
39
    parent(NULL),
40
    description(NULL),
41
    label(NULL),
42
    ask_format(NULL),
43
    group_name(NULL)
44
{
45
  set_type(BXT_PARAM);
46
  this->name = new char[strlen(param_name)+1];
47
  strcpy(this->name, param_name);
48
  set_description(param_desc);
49
  this->text_format = default_text_format;
50
  this->long_text_format = default_text_format;
51
  this->runtime_param = 0;
52
  this->enabled = 1;
53
  // dependent_list must be initialized before the set(),
54
  // because set calls update_dependents().
55
  dependent_list = NULL;
56
}
57
 
58
bx_param_c::bx_param_c(Bit32u id, const char *param_name, const char *param_label, const char *param_desc)
59
  : bx_object_c(id),
60
    parent(NULL),
61
    description(NULL),
62
    label(NULL),
63
    ask_format(NULL),
64
    group_name(NULL)
65
{
66
  set_type(BXT_PARAM);
67
  this->name = new char[strlen(param_name)+1];
68
  strcpy(this->name, param_name);
69
  set_description(param_desc);
70
  set_label(param_label);
71
  this->text_format = default_text_format;
72
  this->long_text_format = default_text_format;
73
  this->runtime_param = 0;
74
  this->enabled = 1;
75
  // dependent_list must be initialized before the set(),
76
  // because set calls update_dependents().
77
  dependent_list = NULL;
78
}
79
 
80
bx_param_c::~bx_param_c()
81
{
82
  delete [] name;
83
  delete [] label;
84
  delete [] description;
85
  delete [] ask_format;
86
  delete [] group_name;
87
  if (dependent_list) delete dependent_list;
88
}
89
 
90
void bx_param_c::set_description(const char *text)
91
{
92
  delete [] this->description;
93
  if (text) {
94
    this->description = new char[strlen(text)+1];
95
    strcpy(this->description, text);
96
  } else {
97
    this->description = NULL;
98
  }
99
}
100
 
101
void bx_param_c::set_label(const char *text)
102
{
103
  delete [] label;
104
  if (text) {
105
    label = new char[strlen(text)+1];
106
    strcpy(label, text);
107
  } else {
108
    label = NULL;
109
  }
110
}
111
 
112
void bx_param_c::set_ask_format(const char *format)
113
{
114
  delete [] ask_format;
115
  if (format) {
116
    ask_format = new char[strlen(format)+1];
117
    strcpy(ask_format, format);
118
  } else {
119
    ask_format = NULL;
120
  }
121
}
122
 
123
void bx_param_c::set_group(const char *group)
124
{
125
  delete [] group_name;
126
  if (group) {
127
    group_name = new char[strlen(group)+1];
128
    strcpy(group_name, group);
129
  } else {
130
    group_name = NULL;
131
  }
132
}
133
 
134
int bx_param_c::get_param_path(char *path_out, int maxlen)
135
{
136
  if ((get_parent() == NULL) || (get_parent() == root_param)) {
137
    // Start with an empty string.
138
    // Never print the name of the root param.
139
    path_out[0] = 0;
140
  } else {
141
    // build path of the parent, add a period, add path of this node
142
    if (get_parent()->get_param_path(path_out, maxlen) > 0) {
143
      strncat(path_out, ".", maxlen);
144
    }
145
  }
146
  strncat(path_out, name, maxlen);
147
  return strlen(path_out);
148
}
149
 
150
const char* bx_param_c::set_default_format(const char *f)
151
{
152
  const char *old = default_text_format;
153
  default_text_format = f;
154
  return old;
155
}
156
 
157
bx_param_num_c::bx_param_num_c(bx_param_c *parent,
158
    const char *name,
159
    const char *label,
160
    const char *description,
161
    Bit64s min, Bit64s max, Bit64s initial_val,
162
    bx_bool is_shadow)
163
  : bx_param_c(SIM->gen_param_id(), name, label, description)
164
{
165
  set_type(BXT_PARAM_NUM);
166
  this->min = min;
167
  this->max = max;
168
  this->initial_val = initial_val;
169
  this->val.number = initial_val;
170
  this->handler = NULL;
171
  this->save_handler = NULL;
172
  this->restore_handler = NULL;
173
  this->enable_handler = NULL;
174
  this->base = default_base;
175
  this->is_shadow = is_shadow;
176
  if (!is_shadow) {
177
    set(initial_val);
178
  }
179
  if (parent) {
180
    BX_ASSERT(parent->get_type() == BXT_LIST);
181
    this->parent = (bx_list_c *)parent;
182
    this->parent->add(this);
183
  }
184
}
185
 
186
Bit32u bx_param_num_c::default_base = BASE_DEC;
187
 
188
Bit32u bx_param_num_c::set_default_base(Bit32u val)
189
{
190
  Bit32u old = default_base;
191
  default_base = val;
192
  return old;
193
}
194
 
195
void bx_param_num_c::set_handler(param_event_handler handler)
196
{
197
  this->handler = handler;
198
  // now that there's a handler, call set once to run the handler immediately
199
  //set (get ());
200
}
201
 
202
void bx_param_num_c::set_sr_handlers(void *devptr, param_save_handler save, param_restore_handler restore)
203
{
204
  sr_devptr = devptr;
205
  save_handler = save;
206
  restore_handler = restore;
207
}
208
 
209
void bx_param_num_c::set_dependent_list(bx_list_c *l)
210
{
211
  dependent_list = l;
212
  update_dependents();
213
}
214
 
215
Bit64s bx_param_num_c::get64()
216
{
217
  if (save_handler) {
218
    return (*save_handler)(sr_devptr, this);
219
  }
220
  if (handler) {
221
    // the handler can decide what value to return and/or do some side effect
222
    return (*handler)(this, 0, val.number);
223
  } else {
224
    // just return the value
225
    return val.number;
226
  }
227
}
228
 
229
void bx_param_num_c::set(Bit64s newval)
230
{
231
  if (handler) {
232
    // the handler can override the new value and/or perform some side effect
233
    val.number = newval;
234
    (*handler)(this, 1, newval);
235
  } else {
236
    // just set the value.  This code does not check max/min.
237
    val.number = newval;
238
  }
239
  if (restore_handler) {
240
    val.number = newval;
241
    (*restore_handler)(sr_devptr, this, newval);
242
  }
243
  if ((val.number < min || val.number > max) && (Bit64u)max != BX_MAX_BIT64U)
244
    BX_PANIC(("numerical parameter '%s' was set to " FMT_LL "d, which is out of range " FMT_LL "d to " FMT_LL "d", get_name (), val.number, min, max));
245
  if (dependent_list != NULL) update_dependents();
246
}
247
 
248
void bx_param_num_c::set_range(Bit64u min, Bit64u max)
249
{
250
  this->min = min;
251
  this->max = max;
252
}
253
 
254
void bx_param_num_c::set_initial_val(Bit64s initial_val)
255
{
256
  this->val.number = this->initial_val = initial_val;
257
}
258
 
259
void bx_param_num_c::update_dependents()
260
{
261
  if (dependent_list) {
262
    int en = val.number && enabled;
263
    for (int i=0; i<dependent_list->get_size(); i++) {
264
      bx_param_c *param = dependent_list->get(i);
265
      if (param != this)
266
        param->set_enabled(en);
267
    }
268
  }
269
}
270
 
271
void bx_param_num_c::set_enabled(int en)
272
{
273
  // The enable handler may wish to allow/disallow the action
274
  if (enable_handler) {
275
    en = (*enable_handler)(this, en);
276
  }
277
  bx_param_c::set_enabled(en);
278
  update_dependents();
279
}
280
 
281
// Signed 64 bit
282
bx_shadow_num_c::bx_shadow_num_c(bx_param_c *parent,
283
    const char *name,
284
    Bit64s *ptr_to_real_val,
285
    int base,
286
    Bit8u highbit,
287
    Bit8u lowbit)
288
: bx_param_num_c(parent, name, NULL, NULL, BX_MIN_BIT64S, BX_MAX_BIT64S, *ptr_to_real_val, 1)
289
{
290
  this->varsize = 64;
291
  this->lowbit = lowbit;
292
  this->mask = ((BX_MAX_BIT64S >> (63 - (highbit - lowbit))) << lowbit);
293
  val.p64bit = ptr_to_real_val;
294
  if (base == BASE_HEX) {
295
    this->base = base;
296
    this->text_format = "0x"FMT_LL"x";
297
  }
298
}
299
 
300
// Unsigned 64 bit
301
bx_shadow_num_c::bx_shadow_num_c(bx_param_c *parent,
302
    const char *name,
303
    Bit64u *ptr_to_real_val,
304
    int base,
305
    Bit8u highbit,
306
    Bit8u lowbit)
307
: bx_param_num_c(parent, name, NULL, NULL, BX_MIN_BIT64U, BX_MAX_BIT64U, *ptr_to_real_val, 1)
308
{
309
  this->varsize = 64;
310
  this->lowbit = lowbit;
311
  this->mask = ((BX_MAX_BIT64U >> (63 - (highbit - lowbit))) << lowbit);
312
  val.p64bit = (Bit64s*) ptr_to_real_val;
313
  if (base == BASE_HEX) {
314
    this->base = base;
315
    this->text_format = "0x"FMT_LL"x";
316
  }
317
}
318
 
319
// Signed 32 bit
320
bx_shadow_num_c::bx_shadow_num_c(bx_param_c *parent,
321
    const char *name,
322
    Bit32s *ptr_to_real_val,
323
    int base,
324
    Bit8u highbit,
325
    Bit8u lowbit)
326
: bx_param_num_c(parent, name, NULL, NULL, BX_MIN_BIT32S, BX_MAX_BIT32S, *ptr_to_real_val, 1)
327
{
328
  this->varsize = 32;
329
  this->lowbit = lowbit;
330
  this->mask = ((BX_MAX_BIT32S >> (31 - (highbit - lowbit))) << lowbit);
331
  val.p32bit = ptr_to_real_val;
332
  if (base == BASE_HEX) {
333
    this->base = base;
334
    this->text_format = "0x%08x";
335
  }
336
}
337
 
338
// Unsigned 32 bit
339
bx_shadow_num_c::bx_shadow_num_c(bx_param_c *parent,
340
    const char *name,
341
    Bit32u *ptr_to_real_val,
342
    int base,
343
    Bit8u highbit,
344
    Bit8u lowbit)
345
: bx_param_num_c(parent, name, NULL, NULL, BX_MIN_BIT32U, BX_MAX_BIT32U, *ptr_to_real_val, 1)
346
{
347
  this->varsize = 32;
348
  this->lowbit = lowbit;
349
  this->mask = ((BX_MAX_BIT32U >> (31 - (highbit - lowbit))) << lowbit);
350
  val.p32bit = (Bit32s*) ptr_to_real_val;
351
  if (base == BASE_HEX) {
352
    this->base = base;
353
    this->text_format = "0x%08x";
354
  }
355
}
356
 
357
// Signed 16 bit
358
bx_shadow_num_c::bx_shadow_num_c(bx_param_c *parent,
359
    const char *name,
360
    Bit16s *ptr_to_real_val,
361
    int base,
362
    Bit8u highbit,
363
    Bit8u lowbit)
364
: bx_param_num_c(parent, name, NULL, NULL, BX_MIN_BIT16S, BX_MAX_BIT16S, *ptr_to_real_val, 1)
365
{
366
  this->varsize = 16;
367
  this->lowbit = lowbit;
368
  this->mask = ((BX_MAX_BIT16S >> (15 - (highbit - lowbit))) << lowbit);
369
  val.p16bit = ptr_to_real_val;
370
  if (base == BASE_HEX) {
371
    this->base = base;
372
    this->text_format = "0x%04x";
373
  }
374
}
375
 
376
// Unsigned 16 bit
377
bx_shadow_num_c::bx_shadow_num_c(bx_param_c *parent,
378
    const char *name,
379
    Bit16u *ptr_to_real_val,
380
    int base,
381
    Bit8u highbit,
382
    Bit8u lowbit)
383
: bx_param_num_c(parent, name, NULL, NULL, BX_MIN_BIT16U, BX_MAX_BIT16U, *ptr_to_real_val, 1)
384
{
385
  this->varsize = 16;
386
  this->lowbit = lowbit;
387
  this->mask = ((BX_MAX_BIT16U >> (15 - (highbit - lowbit))) << lowbit);
388
  val.p16bit = (Bit16s*) ptr_to_real_val;
389
  if (base == BASE_HEX) {
390
    this->base = base;
391
    this->text_format = "0x%04x";
392
  }
393
}
394
 
395
// Signed 8 bit
396
bx_shadow_num_c::bx_shadow_num_c(bx_param_c *parent,
397
    const char *name,
398
    Bit8s *ptr_to_real_val,
399
    int base,
400
    Bit8u highbit,
401
    Bit8u lowbit)
402
: bx_param_num_c(parent, name, NULL, NULL, BX_MIN_BIT8S, BX_MAX_BIT8S, *ptr_to_real_val, 1)
403
{
404
  this->varsize = 8;
405
  this->lowbit = lowbit;
406
  this->mask = ((BX_MAX_BIT8S >> (7 - (highbit - lowbit))) << lowbit);
407
  this->mask = (1 << (highbit - lowbit)) - 1;
408
  val.p8bit = ptr_to_real_val;
409
  if (base == BASE_HEX) {
410
    this->base = base;
411
    this->text_format = "0x%02x";
412
  }
413
}
414
 
415
// Unsigned 8 bit
416
bx_shadow_num_c::bx_shadow_num_c(bx_param_c *parent,
417
    const char *name,
418
    Bit8u *ptr_to_real_val,
419
    int base,
420
    Bit8u highbit,
421
    Bit8u lowbit)
422
: bx_param_num_c(parent, name, NULL, NULL, BX_MIN_BIT8U, BX_MAX_BIT8U, *ptr_to_real_val, 1)
423
{
424
  this->varsize = 8;
425
  this->lowbit = lowbit;
426
  this->mask = ((BX_MAX_BIT8U >> (7 - (highbit - lowbit))) << lowbit);
427
  val.p8bit = (Bit8s*) ptr_to_real_val;
428
  if (base == BASE_HEX) {
429
    this->base = base;
430
    this->text_format = "0x%02x";
431
  }
432
}
433
 
434
Bit64s bx_shadow_num_c::get64()
435
{
436
  Bit64u current = 0;
437
  switch (varsize) {
438
    case 8: current = *(val.p8bit);  break;
439
    case 16: current = *(val.p16bit);  break;
440
    case 32: current = *(val.p32bit);  break;
441
    case 64: current = *(val.p64bit);  break;
442
    default: BX_PANIC(("unsupported varsize %d", varsize));
443
  }
444
  current = (current >> lowbit) & mask;
445
  if (handler) {
446
    // the handler can decide what value to return and/or do some side effect
447
    return (*handler)(this, 0, current) & mask;
448
  } else {
449
    // just return the value
450
    return current;
451
  }
452
}
453
 
454
void bx_shadow_num_c::set(Bit64s newval)
455
{
456
  Bit64u tmp = 0;
457
  if (((newval < min) || (newval > max)) && (min != BX_MIN_BIT64S) && ((Bit64u)max != BX_MAX_BIT64U))
458
    BX_PANIC(("numerical parameter %s was set to " FMT_LL "d, which is out of range " FMT_LL "d to " FMT_LL "d", get_name (), newval, min, max));
459
  switch (varsize) {
460
    case 8:
461
      tmp = *(val.p8bit) & ~(mask << lowbit);
462
      tmp |= (newval & mask) << lowbit;
463
      *(val.p8bit) = (Bit8s)tmp;
464
      break;
465
    case 16:
466
      tmp = *(val.p16bit) & ~(mask << lowbit);
467
      tmp |= (newval & mask) << lowbit;
468
      *(val.p16bit) = (Bit16s)tmp;
469
      break;
470
    case 32:
471
      tmp = *(val.p32bit) & ~(mask << lowbit);
472
      tmp |= (newval & mask) << lowbit;
473
      *(val.p32bit) = (Bit32s)tmp;
474
      break;
475
    case 64:
476
      tmp = *(val.p64bit) & ~(mask << lowbit);
477
      tmp |= (newval & mask) << lowbit;
478
      *(val.p64bit) = (Bit64s)tmp;
479
      break;
480
    default:
481
      BX_PANIC(("unsupported varsize %d", varsize));
482
  }
483
  if (handler) {
484
    // the handler can override the new value and/or perform some side effect
485
    (*handler)(this, 1, tmp);
486
  }
487
}
488
 
489
void bx_shadow_num_c::reset()
490
{
491
  BX_PANIC(("reset not supported on bx_shadow_num_c yet"));
492
}
493
 
494
bx_param_bool_c::bx_param_bool_c(bx_param_c *parent,
495
    const char *name,
496
    const char *label,
497
    const char *description,
498
    Bit64s initial_val,
499
    bx_bool is_shadow)
500
  : bx_param_num_c(parent, name, label, description, 0, 1, initial_val, is_shadow)
501
{
502
  set_type(BXT_PARAM_BOOL);
503
}
504
 
505
bx_shadow_bool_c::bx_shadow_bool_c(bx_param_c *parent,
506
      const char *name,
507
      const char *label,
508
      bx_bool *ptr_to_real_val,
509
      Bit8u bitnum)
510
  : bx_param_bool_c(parent, name, label, NULL, (Bit64s) *ptr_to_real_val, 1)
511
{
512
  val.pbool = ptr_to_real_val;
513
  this->bitnum = bitnum;
514
}
515
 
516
bx_shadow_bool_c::bx_shadow_bool_c(bx_param_c *parent,
517
      const char *name,
518
      bx_bool *ptr_to_real_val,
519
      Bit8u bitnum)
520
  : bx_param_bool_c(parent, name, NULL, NULL, (Bit64s) *ptr_to_real_val, 1)
521
{
522
  val.pbool = ptr_to_real_val;
523
  this->bitnum = bitnum;
524
}
525
 
526
Bit64s bx_shadow_bool_c::get64()
527
{
528
  if (handler) {
529
    // the handler can decide what value to return and/or do some side effect
530
    Bit64s ret = (*handler)(this, 0, (Bit64s) *(val.pbool));
531
    return (ret>>bitnum) & 1;
532
  } else {
533
    // just return the value
534
    return (*(val.pbool)) & 1;
535
  }
536
}
537
 
538
void bx_shadow_bool_c::set(Bit64s newval)
539
{
540
  // only change the bitnum bit
541
  Bit64s tmp = (newval&1) << bitnum;
542
  *(val.pbool) &= ~tmp;
543
  *(val.pbool) |= tmp;
544
  if (handler) {
545
    // the handler can override the new value and/or perform some side effect
546
    (*handler)(this, 1, newval&1);
547
  }
548
}
549
 
550
bx_param_enum_c::bx_param_enum_c(bx_param_c *parent,
551
      const char *name,
552
      const char *label,
553
      const char *description,
554
      const char **choices,
555
      Bit64s initial_val,
556
      Bit64s value_base)
557
  : bx_param_num_c(parent, name, label, description, value_base, BX_MAX_BIT64S, initial_val)
558
{
559
  set_type(BXT_PARAM_ENUM);
560
  this->choices = choices;
561
  // count number of choices, set max
562
  const char **p = choices;
563
  while (*p != NULL) p++;
564
  this->min = value_base;
565
  // now that the max is known, replace the BX_MAX_BIT64S sent to the parent
566
  // class constructor with the real max.
567
  this->max = value_base + (p - choices - 1);
568
  this->deps_bitmap = NULL;
569
  set(initial_val);
570
}
571
 
572
bx_param_enum_c::~bx_param_enum_c()
573
{
574
  if (deps_bitmap != NULL) {
575
    free(deps_bitmap);
576
  }
577
}
578
 
579
 
580
void bx_param_enum_c::set(Bit64s val)
581
{
582
  bx_param_num_c::set(val);
583
  update_dependents();
584
}
585
 
586
int bx_param_enum_c::find_by_name(const char *string)
587
{
588
  const char **p;
589
  for (p=&choices[0]; *p; p++) {
590
    if (!strcmp(string, *p))
591
      return p-choices;
592
  }
593
  return -1;
594
}
595
 
596
bx_bool bx_param_enum_c::set_by_name(const char *string)
597
{
598
  int n = find_by_name(string);
599
  if (n<0) return 0;
600
  set(n + min);
601
  return 1;
602
}
603
 
604
void bx_param_enum_c::set_dependent_list(bx_list_c *l, bx_bool enable_all)
605
{
606
  dependent_list = l;
607
  deps_bitmap = (Bit64u*)malloc((size_t)(sizeof(Bit64u) * (max - min + 1)));
608
  for (int i=0; i<(max-min+1); i++) {
609
    if (enable_all) {
610
      deps_bitmap[i] = (1 << (l->get_size())) - 1;
611
    } else {
612
      deps_bitmap[i] = 0;
613
    }
614
  }
615
  update_dependents();
616
}
617
 
618
void bx_param_enum_c::set_dependent_bitmap(Bit64s value, Bit64u bitmap)
619
{
620
  if (deps_bitmap != NULL) {
621
    deps_bitmap[value - min] = bitmap;
622
  }
623
  update_dependents();
624
}
625
 
626
Bit64u bx_param_enum_c::get_dependent_bitmap(Bit64s value)
627
{
628
  if (deps_bitmap != NULL) {
629
    return deps_bitmap[value - min];
630
  }
631
  return 0;
632
}
633
 
634
void bx_param_enum_c::update_dependents()
635
{
636
  if ((dependent_list != NULL) && (deps_bitmap != NULL)) {
637
    Bit64u en_bmap = deps_bitmap[val.number - min];
638
    Bit64u mask = 0x1;
639
    for (int i=0; i<dependent_list->get_size(); i++) {
640
      int en = (en_bmap & mask) && enabled;
641
      bx_param_c *param = dependent_list->get(i);
642
      if (param != this)
643
        param->set_enabled(en);
644
      mask <<= 1;
645
    }
646
  }
647
}
648
 
649
void bx_param_enum_c::set_enabled(int en)
650
{
651
  // The enable handler may wish to allow/disallow the action
652
  if (enable_handler) {
653
    en = (*enable_handler)(this, en);
654
  }
655
  bx_param_c::set_enabled(en);
656
  update_dependents();
657
}
658
 
659
bx_param_string_c::bx_param_string_c(bx_param_c *parent,
660
    const char *name,
661
    const char *label,
662
    const char *description,
663
    const char *initial_val,
664
    int maxsize)
665
  : bx_param_c(SIM->gen_param_id(), name, label, description)
666
{
667
  set_type(BXT_PARAM_STRING);
668
  int initial_val_size = strlen(initial_val) + 1;
669
  if (maxsize < 0) {
670
    maxsize = initial_val_size;
671
  } else if (initial_val_size > maxsize) {
672
    initial_val_size = maxsize;
673
  }
674
  this->val = new char[maxsize];
675
  this->initial_val = new char[maxsize];
676
  this->handler = NULL;
677
  this->enable_handler = NULL;
678
  this->maxsize = maxsize;
679
  strncpy(this->val, initial_val, initial_val_size);
680
  if (maxsize > initial_val_size)
681
    memset(this->val + initial_val_size, 0, maxsize - initial_val_size);
682
  strncpy(this->initial_val, initial_val, maxsize);
683
  this->options = 0;
684
  set(initial_val);
685
  if (parent) {
686
    BX_ASSERT(parent->get_type() == BXT_LIST);
687
    this->parent = (bx_list_c *)parent;
688
    this->parent->add(this);
689
  }
690
}
691
 
692
bx_param_filename_c::bx_param_filename_c(bx_param_c *parent,
693
    const char *name,
694
    const char *label,
695
    const char *description,
696
    const char *initial_val,
697
    int maxsize)
698
  : bx_param_string_c(parent, name, label, description, initial_val, maxsize)
699
{
700
  set_options(IS_FILENAME);
701
  int len = strlen(initial_val);
702
  if ((len > 4) && (initial_val[len - 4] == '.')) {
703
    ext = &initial_val[len - 3];
704
  } else {
705
    ext = NULL;
706
  }
707
}
708
 
709
bx_param_string_c::~bx_param_string_c()
710
{
711
  if (val != NULL) delete [] val;
712
  if (initial_val != NULL) delete [] initial_val;
713
}
714
 
715
void bx_param_string_c::reset()
716
{
717
  set(initial_val);
718
}
719
 
720
void bx_param_string_c::set_handler(param_string_event_handler handler)
721
{
722
  this->handler = handler;
723
}
724
 
725
void bx_param_string_c::set_enable_handler(param_enable_handler handler)
726
{
727
  this->enable_handler = handler;
728
}
729
 
730
void bx_param_string_c::update_dependents()
731
{
732
  if (dependent_list) {
733
    int en = (strlen(val) > 0) && (strcmp(val, "none")) && enabled;
734
    for (int i=0; i<dependent_list->get_size(); i++) {
735
      bx_param_c *param = dependent_list->get(i);
736
      if (param != this)
737
        param->set_enabled(en);
738
    }
739
  }
740
}
741
 
742
void bx_param_string_c::set_enabled(int en)
743
{
744
  // The enable handler may wish to allow/disallow the action
745
  if (enable_handler) {
746
    en = (*enable_handler)(this, en);
747
  }
748
  bx_param_c::set_enabled(en);
749
  if (dependent_list != NULL) update_dependents();
750
}
751
 
752
void bx_param_string_c::set_dependent_list(bx_list_c *l)
753
{
754
  dependent_list = l;
755
  update_dependents();
756
}
757
 
758
Bit32s bx_param_string_c::get(char *buf, int len)
759
{
760
  if (options & RAW_BYTES)
761
    memcpy(buf, val, len);
762
  else
763
    strncpy(buf, val, len);
764
  if (handler) {
765
    // the handler can choose to replace the value in val/len.  Also its
766
    // return value is passed back as the return value of get.
767
    (*handler)(this, 0, buf, buf, len);
768
  }
769
  return 0;
770
}
771
 
772
void bx_param_string_c::set(const char *buf)
773
{
774
  char *oldval = new char[maxsize];
775
 
776
  if (options & RAW_BYTES) {
777
    memcpy(oldval, val, maxsize);
778
    memcpy(val, buf, maxsize);
779
  } else {
780
    strncpy(oldval, val, maxsize);
781
    oldval[maxsize - 1] = 0;
782
    strncpy(val, buf, maxsize);
783
    val[maxsize - 1] = 0;
784
  }
785
  if (handler) {
786
    // the handler can return a different char* to be copied into the value
787
    buf = (*handler)(this, 1, oldval, buf, -1);
788
  }
789
  delete [] oldval;
790
  if (dependent_list != NULL) update_dependents();
791
}
792
 
793
bx_bool bx_param_string_c::equals(const char *buf)
794
{
795
  if (options & RAW_BYTES)
796
    return (memcmp(val, buf, maxsize) == 0);
797
  else
798
    return (strncmp(val, buf, maxsize) == 0);
799
}
800
 
801
void bx_param_string_c::set_initial_val(const char *buf)
802
{
803
  if (options & RAW_BYTES)
804
    memcpy(initial_val, buf, maxsize);
805
  else
806
    strncpy(initial_val, buf, maxsize);
807
  set(initial_val);
808
}
809
 
810
bx_bool bx_param_string_c::isempty()
811
{
812
  return ((strlen(val) == 0) || !strcmp(val, "none"));
813
}
814
 
815
int bx_param_string_c::sprint(char *buf, int len, bx_bool dquotes)
816
{
817
  char tmpbyte[4];
818
 
819
  if (get_options() & RAW_BYTES) {
820
    buf[0] = 0;
821
    for (int j = 0; j < maxsize; j++) {
822
      if (j > 0) {
823
        tmpbyte[0] = separator;
824
        tmpbyte[1] = 0;
825
        strcat(buf, tmpbyte);
826
      }
827
      sprintf(tmpbyte, "%02x", (Bit8u)val[j]);
828
      strcat(buf, tmpbyte);
829
    }
830
  } else {
831
    if (!isempty()) {
832
      if (dquotes) {
833
        snprintf(buf, len, "\"%s\"", val);
834
      } else {
835
        snprintf(buf, len, "%s", val);
836
      }
837
    } else {
838
      strcpy(buf, "none");
839
    }
840
  }
841
  return strlen(buf);
842
}
843
 
844
bx_shadow_data_c::bx_shadow_data_c(bx_param_c *parent,
845
    const char *name,
846
    Bit8u *ptr_to_data,
847
    Bit32u data_size)
848
  : bx_param_c(SIM->gen_param_id(), name, "")
849
{
850
  set_type(BXT_PARAM_DATA);
851
  this->data_ptr = ptr_to_data;
852
  this->data_size = data_size;
853
  if (parent) {
854
    BX_ASSERT(parent->get_type() == BXT_LIST);
855
    this->parent = (bx_list_c *)parent;
856
    this->parent->add(this);
857
  }
858
}
859
 
860
bx_shadow_filedata_c::bx_shadow_filedata_c(bx_param_c *parent,
861
    const char *name, FILE **scratch_file_ptr_ptr)
862
  : bx_param_c(SIM->gen_param_id(), name, "")
863
{
864
  set_type(BXT_PARAM_FILEDATA);
865
  this->scratch_fpp = scratch_file_ptr_ptr;
866
  this->save_handler = NULL;
867
  this->restore_handler = NULL;
868
  if (parent) {
869
    BX_ASSERT(parent->get_type() == BXT_LIST);
870
    this->parent = (bx_list_c *)parent;
871
    this->parent->add(this);
872
  }
873
}
874
 
875
// Save handler: called before file save, Restore handler: called after file restore
876
void bx_shadow_filedata_c::set_sr_handlers(void *devptr, filedata_save_handler save, filedata_restore_handler restore)
877
{
878
  this->sr_devptr = devptr;
879
  this->save_handler = save;
880
  this->restore_handler = restore;
881
}
882
 
883
void bx_shadow_filedata_c::save(FILE *save_fp)
884
{
885
  if (save_handler)
886
    (*save_handler)(sr_devptr, save_fp);
887
}
888
 
889
void bx_shadow_filedata_c::restore(FILE *save_fp)
890
{
891
  if (restore_handler)
892
    (*restore_handler)(sr_devptr, save_fp);
893
}
894
 
895
bx_list_c::bx_list_c(bx_param_c *parent)
896
  : bx_param_c(SIM->gen_param_id(), "list", "")
897
{
898
  set_type(BXT_LIST);
899
  this->size = 0;
900
  this->list = NULL;
901
  this->parent = NULL;
902
  if (parent) {
903
    BX_ASSERT(parent->get_type() == BXT_LIST);
904
    this->parent = (bx_list_c *)parent;
905
    this->parent->add(this);
906
  }
907
  init("");
908
}
909
 
910
bx_list_c::bx_list_c(bx_param_c *parent, const char *name)
911
  : bx_param_c(SIM->gen_param_id(), name, "")
912
{
913
  set_type (BXT_LIST);
914
  this->size = 0;
915
  this->list = NULL;
916
  this->parent = NULL;
917
  if (parent) {
918
    BX_ASSERT(parent->get_type() == BXT_LIST);
919
    this->parent = (bx_list_c *)parent;
920
    this->parent->add(this);
921
  }
922
  init("");
923
}
924
 
925
bx_list_c::bx_list_c(bx_param_c *parent, const char *name, const char *title)
926
  : bx_param_c(SIM->gen_param_id(), name, "")
927
{
928
  set_type (BXT_LIST);
929
  this->size = 0;
930
  this->list = NULL;
931
  this->parent = NULL;
932
  if (parent) {
933
    BX_ASSERT(parent->get_type() == BXT_LIST);
934
    this->parent = (bx_list_c *)parent;
935
    this->parent->add(this);
936
  }
937
  init(title);
938
}
939
 
940
bx_list_c::bx_list_c(bx_param_c *parent, const char *name, const char *title, bx_param_c **init_list)
941
  : bx_param_c(SIM->gen_param_id(), name, "")
942
{
943
  set_type(BXT_LIST);
944
  this->size = 0;
945
  this->list = NULL;
946
  while (init_list[this->size] != NULL)
947
    add(init_list[this->size]);
948
  this->parent = NULL;
949
  if (parent) {
950
    BX_ASSERT(parent->get_type() == BXT_LIST);
951
    this->parent = (bx_list_c *)parent;
952
    this->parent->add(this);
953
  }
954
  init(title);
955
}
956
 
957
bx_list_c::~bx_list_c()
958
{
959
  if (list != NULL) {
960
    clear();
961
  }
962
  if (title != NULL) delete [] title;
963
}
964
 
965
void bx_list_c::init(const char *list_title)
966
{
967
  if (list_title) {
968
    this->title = new char[strlen(list_title)+1];
969
    strcpy(this->title, list_title);
970
  } else {
971
    this->title = new char[1];
972
    this->title[0] = 0;
973
  }
974
  this->options = 0;
975
  this->choice = 1;
976
}
977
 
978
void bx_list_c::set_parent(bx_param_c *newparent)
979
{
980
  if (parent) {
981
    // if this object already had a parent, the correct thing
982
    // to do would be to remove this object from the parent's
983
    // list of children.  Deleting children is currently
984
    // not supported.
985
    BX_PANIC(("bx_list_c::set_parent: changing from one parent to another is not supported"));
986
  }
987
  if (newparent) {
988
    BX_ASSERT(newparent->get_type() == BXT_LIST);
989
    parent = (bx_list_c *)newparent;
990
    parent->add(this);
991
  }
992
}
993
 
994
bx_list_c* bx_list_c::clone()
995
{
996
  bx_list_c *newlist = new bx_list_c(NULL, name, title);
997
  for (int i=0; i<get_size(); i++)
998
    newlist->add(get(i));
999
  newlist->set_options(options);
1000
  return newlist;
1001
}
1002
 
1003
void bx_list_c::add(bx_param_c *param)
1004
{
1005
  bx_listitem_t *item;
1006
 
1007
  if ((get_by_name(param->get_name()) != NULL) && (param->get_parent() == this)) {
1008
    BX_PANIC(("parameter '%s' already exists in list '%s'", param->get_name(), this->get_name()));
1009
    return;
1010
  }
1011
  item = (bx_listitem_t*) malloc(sizeof(bx_listitem_t));
1012
  if (item == NULL) {
1013
    BX_PANIC(("bx_list_c::add(): malloc() failed"));
1014
    return;
1015
  }
1016
  item->param = param;
1017
  item->next = NULL;
1018
  if (list == NULL) {
1019
    list = item;
1020
  } else {
1021
    bx_listitem_t *temp = list;
1022
    while (temp->next)
1023
      temp = temp->next;
1024
    temp->next = item;
1025
  }
1026
  size++;
1027
}
1028
 
1029
bx_param_c* bx_list_c::get(int index)
1030
{
1031
  BX_ASSERT(index >= 0 && index < size);
1032
  int i = 0;
1033
  bx_listitem_t *temp = list;
1034
  while (temp != NULL) {
1035
    if (i == index) {
1036
      return temp->param;
1037
    }
1038
    temp = temp->next;
1039
    i++;
1040
  }
1041
  return NULL;
1042
}
1043
 
1044
bx_param_c* bx_list_c::get_by_name(const char *name)
1045
{
1046
  bx_listitem_t *temp = list;
1047
  while (temp != NULL) {
1048
    bx_param_c *p = temp->param;
1049
    if (!stricmp(name, p->get_name())) {
1050
      return p;
1051
    }
1052
    temp = temp->next;
1053
  }
1054
  return NULL;
1055
}
1056
 
1057
void bx_list_c::reset()
1058
{
1059
  bx_listitem_t *temp = list;
1060
  while (temp != NULL) {
1061
    temp->param->reset();
1062
    temp = temp->next;
1063
  }
1064
}
1065
 
1066
void bx_list_c::clear()
1067
{
1068
  bx_listitem_t *temp = list, *next;
1069
  while (temp != NULL) {
1070
    if (temp->param->get_parent() == this) {
1071
      delete temp->param;
1072
    }
1073
    next = temp->next;
1074
    free(temp);
1075
    temp = next;
1076
  }
1077
  list = NULL;
1078
  size = 0;
1079
}
1080
 
1081
void bx_list_c::remove(const char *name)
1082
{
1083
  bx_listitem_t *item, *prev = NULL;
1084
 
1085
  for (item = list; item; item = item->next) {
1086
    bx_param_c *p = item->param;
1087
    if (!stricmp(name, p->get_name())) {
1088
      if (p->get_parent() == this) {
1089
        delete p;
1090
      }
1091
      if (prev == NULL) {
1092
        list = item->next;
1093
      } else {
1094
        prev->next = item->next;
1095
      }
1096
      free(item);
1097
      size--;
1098
      break;
1099
    } else {
1100
      prev = item;
1101
    }
1102
  }
1103
}

powered by: WebSVN 2.1.0

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