File: table_mod.c

package info (click to toggle)
iverilog 12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,148 kB
  • sloc: cpp: 109,972; ansic: 62,713; yacc: 10,216; sh: 3,470; vhdl: 3,246; perl: 1,814; makefile: 1,774; python: 78; csh: 2
file content (728 lines) | stat: -rw-r--r-- 22,835 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
 *  Copyright (C) 2011-2021  Cary R. (cygcary@yahoo.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "sys_priv.h"
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "table_mod.h"
#include "ivl_alloc.h"

/*
 * Flag used to enable/disable debug output.
 */
static unsigned table_model_debug = 0;

static p_table_mod *tables = 0;
static unsigned table_count = 0;

/*
 * Routine to cleanup the table model data at the end of simulation.
 */
static PLI_INT32 cleanup_table_mod(p_cb_data cause)
{
      unsigned idx;

      (void)cause; /* Parameter is not used. */

      for (idx = 0; idx < table_count; idx += 1) {
	    free(tables[idx]->indep);
	    free(tables[idx]->indep_val);
	    if (tables[idx]->have_fname) free(tables[idx]->file.name);
	    if (tables[idx]->have_ctl) {
		  free(tables[idx]->control.info.interp);
		  free(tables[idx]->control.info.extrap_low);
		  free(tables[idx]->control.info.extrap_high);
	    }
            free(tables[idx]);
      }
      free(tables);
      tables = 0;
      table_count = 0;

      return 0;
}

/*
 * Create an empty table model object and add it to the list of table
 * model objects.
 */
static p_table_mod create_table(void)
{
	/* Create an empty table model object. */
      p_table_mod obj = (p_table_mod) malloc(sizeof(s_table_mod));
      assert(obj);

	/* Add it to the list of tables. */
      table_count += 1;
      tables = (p_table_mod *) realloc(tables, sizeof(p_table_mod)*table_count);
      tables[table_count-1] = obj;

	/* Initialize and return the table object. */
      obj->indep = 0;
      obj->indep_val = 0;
      obj->have_fname = 0;
      obj->have_ctl = 0;
      obj->control.arg = 0;
      obj->depend = 0;
      obj->dims = 0;
      obj->fields = 0;
      return obj;
}

/*
 * Check to see if this is a constant string. It returns 1 if the argument
 * is a constant string otherwise it returns 0.
 */
static unsigned is_const_string_obj(vpiHandle arg)
{
      unsigned rtn = 0;

      assert(arg);

      switch (vpi_get(vpiType, arg)) {
	case vpiConstant:
	case vpiParameter:
	      /* This must be a constant string. */
	    if (vpi_get(vpiConstType, arg) == vpiStringConst) rtn = 1;
      }

      return rtn;
}

/*
 * Get any command line flags. For now we only have a debug flag.
 */
static void check_command_line_flags(void)
{
      struct t_vpi_vlog_info vlog_info;
      static unsigned command_line_processed = 0;

	/* If we have already processed the arguments then just return. */
      if (command_line_processed) return;

      vpi_get_vlog_info(&vlog_info);

      for (int idx = 0; idx < vlog_info.argc; idx += 1) {
	    if (strcmp(vlog_info.argv[idx], "-table-model-debug") == 0) {
		  table_model_debug = 1;
	    }
      }

      command_line_processed = 1;
}

/*
 * Check that the given $table_model() call has valid arguments.
 */
static PLI_INT32 sys_table_model_compiletf(ICARUS_VPI_CONST PLI_BYTE8 *name)
{
      vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
      vpiHandle argv = vpi_iterate(vpiArgument, callh);
      vpiHandle arg;
      p_table_mod table = create_table();

	/* See if there are any table model arguments. */
      check_command_line_flags();

	/* Check that there are arguments. */
      if (argv == 0) {
	    vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
	               (int)vpi_get(vpiLineNo, callh));
	    vpi_printf("%s requires at least two arguments.\n", name);
	    vpip_set_return_value(1);
	    vpi_control(vpiFinish, 1);
	    return 0;
      }

	/* The first N (dimensions) arguments must be numeric. */
      for (arg = vpi_scan(argv);
           arg && is_numeric_obj(arg);
           arg = vpi_scan(argv)) {
	    table->dims += 1;
	    table->indep = (vpiHandle *)realloc(table->indep,
	                                        sizeof(vpiHandle)*table->dims);
	    table->indep[table->dims-1] = arg;
      }

	/* We must have a data file. */
      if (arg == 0) {
	    vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
	               (int)vpi_get(vpiLineNo, callh));
	    vpi_printf("%s requires a file name argument.\n", name);
	    vpip_set_return_value(1);
	    vpi_control(vpiFinish, 1);
	    return 0;
      }

	/* For now we only allow a constant string (file name). */
      if (! is_const_string_obj(arg)) {
	    vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
	               (int)vpi_get(vpiLineNo, callh));
	    vpi_printf("%s file name argument must be a constant string.\n",
	               name);
	    vpip_set_return_value(1);
	    vpi_control(vpiFinish, 1);
	    return 0;
      }
      table->file.arg = arg;

	/* There may be an optional constant string (control string). */
      arg = vpi_scan(argv);
      if (arg) {
	    if (! is_const_string_obj(arg)) {
		  vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
		            (int)vpi_get(vpiLineNo, callh));
		  vpi_printf("%s control string argument must be a constant "
		             "string.\n", name);
		  vpip_set_return_value(1);
		  vpi_control(vpiFinish, 1);
		  return 0;
	    }
	    check_for_extra_args(argv, callh, name, "N numeric and 1 or 2 "
	                         " constant string arguments", 0);
	    table->control.arg = arg;
      }

	/* Save the table data information. */
      vpi_put_userdata(callh, table);

      return 0;
}

/*
 * Initialize the control string fields with the default value.
 */
static void initialize_control_fields_def(p_table_mod table)
{
      char *val;
      unsigned width = table->dims;

	/* Set the default interpolation for each dimension. */
      val = (char *) malloc(width);
      assert(val);
      table->control.info.interp = memset(val, IVL_LINEAR_INTERP, width);

	/* Set the default low extrapolation for each dimension. */
      val = (char *) malloc(width);
      assert(val);
      table->control.info.extrap_low = memset(val, IVL_LINEAR_EXTRAP, width);

	/* Set the default low extrapolation for each dimension. */
      val = (char *) malloc(width);
      assert(val);
      table->control.info.extrap_high = memset(val, IVL_LINEAR_EXTRAP, width);

	/* By default the dependent data column is the first one after the
	 * independent data column(s). */
      table->depend = 1;

	/* Set the number of control fields. */
      table->fields = width;

	/* We have defined the interpolation/extrapolation fields. */
      table->have_ctl = 1;
}

/*
 * Parse the extrapolation control codes.
 */
static unsigned parse_extrap(vpiHandle callh, p_table_mod table, unsigned idx,
                             char **extrap, char *control)
{
	/* Advance to the first extrapolation code. */
      *extrap += 1;

	/* By default both the low and high extrapolation is set by the
	 * first extrapolation character. We'll override the high value
	 * later if there is a second character. */
      switch (**extrap) {
	  /* Clamp (no) extrapolation. */
	case 'C':
	    table->control.info.extrap_low[idx] = IVL_CONSTANT_EXTRAP;
	    table->control.info.extrap_high[idx] = IVL_CONSTANT_EXTRAP;
	    break;

	  /* Linear extrapolation. */
	case 'L':
	    table->control.info.extrap_low[idx] = IVL_LINEAR_EXTRAP;
	    table->control.info.extrap_high[idx] = IVL_LINEAR_EXTRAP;
	    break;

	  /* Error on extrapolation. */
	case 'E':
	    table->control.info.extrap_low[idx] = IVL_ERROR_EXTRAP;
	    table->control.info.extrap_high[idx] = IVL_ERROR_EXTRAP;
	    break;

	  /* There are no extrapolation characters so use the default
	   * (linear extrapolation). */
	case 0:
	case ',':
	    table->control.info.extrap_low[idx] = IVL_LINEAR_EXTRAP;
	    table->control.info.extrap_high[idx] = IVL_LINEAR_EXTRAP;
	    return 0;

	  /* Anything else is an error. */
	default:
	    vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
	               (int)vpi_get(vpiLineNo, callh));
	    vpi_printf("Unknown extrapolation code '%c' for dimension "
	               "%u: %s\n", **extrap, idx+1, control);
	    vpip_set_return_value(1);
	    vpi_control(vpiFinish, 1);
	    return 1;
      }

	/* Advance to the next (high) extrapolation code. */
      *extrap += 1;

	/* Override the high extrapolation value if needed. */
      switch (**extrap) {
	  /* Clamp (no) extrapolation. */
	case 'C':
	    table->control.info.extrap_high[idx] = IVL_CONSTANT_EXTRAP;
	    break;

	  /* Linear extrapolation. */
	case 'L':
	    table->control.info.extrap_high[idx] = IVL_LINEAR_EXTRAP;
	    break;

	  /* Error on extrapolation. */
	case 'E':
	    table->control.info.extrap_high[idx] = IVL_ERROR_EXTRAP;
	    break;

	  /* There is no high extrapolation character so just return. */
	case 0:
	case ',':
	    return 0;

	  /* Anything else is an error. */
	default:
	    vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
	               (int)vpi_get(vpiLineNo, callh));
	    vpi_printf("Unknown high extrapolation code '%c' for dimension "
	               "%u: %s\n", **extrap, idx+1, control);
	    vpip_set_return_value(1);
	    vpi_control(vpiFinish, 1);
	    return 1;
      }

	/* Advance to the next field. */
      *extrap += 1;

      return 0;
}

/*
 * Load the control string fields for the given string. Return 0 if there
 * is a problem parsing the control string.
 */
static unsigned initialize_control_fields(vpiHandle callh, p_table_mod table,
                                          char *control)
{
      unsigned num_fields, num_ignore, idx;
      char *cp;

	/* If we need to fail these must be defined to zero until memory
	 * has actually been allocated. */
      table->control.info.interp = 0;
      table->control.info.extrap_low = 0;
      table->control.info.extrap_high = 0;

	/* We have defined the interpolation/extrapolation fields. */
      table->have_ctl = 1;

	/* Look for the dependent data field. */
      cp = strchr(control, ';');
      if (cp) {
	    size_t len;
	    unsigned long val;
	    char *end;
	      /* NULL terminate the independent part of the string. */
	    *cp = 0;
	    cp += 1;
	    len = strlen(cp);
	      /* If the length is zero or not all digits then the string
	       * is invalid. */
// HERE: Do we need to support/ignore an underscore? What about in the file?
	    if ((! len) || (len != strspn(cp, "0123456789"))) {
		  vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
		             (int)vpi_get(vpiLineNo, callh));
		  vpi_printf("Control string dependent selector is "
		             "invalid: %s.\n", cp);
		  vpip_set_return_value(1);
		  vpi_control(vpiFinish, 1);
		  return 1;
	    }
	    val = strtoul(cp, &end, 10);
	    assert(*end == 0);
	      /* If the value is too big also report an error. */
	    if (val > UINT_MAX) {
		  vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
		             (int)vpi_get(vpiLineNo, callh));
		  vpi_printf("Control string dependent value is "
		             "to large: %lu.\n", val);
		  vpip_set_return_value(1);
		  vpi_control(vpiFinish, 1);
		  return 1;
	    }
	    table->depend = (unsigned) val;
      } else table->depend = 1;

	/* Find the number of interpolation/extrapolation control fields. */
      num_fields = 1;
      cp = control;
      while ((cp = strchr(cp, ','))) {
	    cp += 1;
	    num_fields += 1;
      }

	/* We must have at least as many control fields as dimensions. */
      if (num_fields < table->dims) {
	    vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
	               (int)vpi_get(vpiLineNo, callh));
	    vpi_printf("Not enough control field(s) (%u) for the dimension(s) "
	               "(%u).", num_fields, table->dims);
	    vpip_set_return_value(1);
	    vpi_control(vpiFinish, 1);
	    return 1;
      }

	/* Allocate space for the interpolation/extrapolation values. */
      table->control.info.interp = (char *) malloc(num_fields);
      assert(table->control.info.interp);
      table->control.info.extrap_low = (char *) malloc(num_fields);
      assert(table->control.info.extrap_low);
      table->control.info.extrap_high = (char *) malloc(num_fields);
      assert(table->control.info.extrap_high);

	/* Parse the individual dimension control string. */
      num_ignore = 0;
      cp = control;
      for (idx = 0; idx < num_fields; idx += 1) {
	    switch (*cp) {
		/* Closest point interpolation. */
	      case 'D':
		  table->control.info.interp[idx] = IVL_CLOSEST_POINT;
		  if (parse_extrap(callh, table, idx, &cp, control)) return 0;
		  break;

		/* Linear interpolation. */
	      case '1':
		  table->control.info.interp[idx] = IVL_LINEAR_INTERP;
		  if (parse_extrap(callh, table, idx, &cp, control)) return 0;
		  break;

		/* Quadratic interpolation. */
	      case '2':
		  table->control.info.interp[idx] = IVL_QUADRATIC_INTERP;
		  if (parse_extrap(callh, table, idx, &cp, control)) return 0;
		  break;

		/* Cubic interpolation. */
	      case '3':
		  table->control.info.interp[idx] = IVL_CUBIC_INTERP;
		  if (parse_extrap(callh, table, idx, &cp, control)) return 0;
		  break;

		/* Ignore column. No extrapolation codes are allowed. */
	      case 'I':
		  table->control.info.interp[idx] = IVL_IGNORE_COLUMN;
		  table->control.info.extrap_low[idx] = IVL_ERROR_EXTRAP;
		  table->control.info.extrap_high[idx] = IVL_ERROR_EXTRAP;
		  cp += 1;
		  num_ignore += 1;
		  break;

		/* This field is not specified so use the default values.
		 * Linear interpolation and extrapolation. */
	      case 0:
		  assert(idx == num_fields-1);
	      case ',':
		  table->control.info.interp[idx] = IVL_LINEAR_INTERP;
		  table->control.info.extrap_low[idx] = IVL_LINEAR_EXTRAP;
		  table->control.info.extrap_high[idx] = IVL_LINEAR_EXTRAP;
		  break;

		/* Anything else is an error. */
	      default:
		  vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
		             (int)vpi_get(vpiLineNo, callh));
		  vpi_printf("Unknown interpolation code '%c' for dimension "
		             "%u: %s\n", *cp, idx+1, control);
		  vpip_set_return_value(1);
		  vpi_control(vpiFinish, 1);
		  return 1;
	    }

	      /* Verify that there is no extra junk at the end of the field. */
	    if (*cp && (*cp != ',')) {
		  vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
		             (int)vpi_get(vpiLineNo, callh));
		  vpi_printf("Extra control characters found for dimension "
		             "%u: %s\n", idx+1, control);
		  vpip_set_return_value(1);
		  vpi_control(vpiFinish, 1);
		  return 1;
	    }

	      /* Advance to the next field if we are not at the end of the
	       * control string. */
	    if (*cp == ',') cp += 1;
      }

	/* We must have a usable control field for each dimensions. */
      if ((num_fields - num_ignore) != table->dims) {
	    vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
	               (int)vpi_get(vpiLineNo, callh));
	    vpi_printf("Usable control field(s) (%u) do not match dimension(s) "
	               "(%u).", (num_fields - num_ignore), table->dims);
	    vpip_set_return_value(1);
	    vpi_control(vpiFinish, 1);
	    return 1;
      }

	/* Set the number of control fields. */
      table->fields = num_fields;

      return 0;
}

/*
 * Print out the interpolation code.
 */
static void dump_interp(char interp)
{
      switch (interp) {
	case IVL_CLOSEST_POINT:    fprintf(stderr, "D"); break;
	case IVL_LINEAR_INTERP:    fprintf(stderr, "1"); break;
	case IVL_QUADRATIC_INTERP: fprintf(stderr, "2"); break;
	case IVL_CUBIC_INTERP:     fprintf(stderr, "3"); break;
	case IVL_IGNORE_COLUMN:    fprintf(stderr, "I"); break;
	default: fprintf(stderr, "<%d>", interp); break;
      }
}

/*
 * Print out the extrapolation code.
 */
static void dump_extrap(char extrap)
{
      switch (extrap) {
	case IVL_CONSTANT_EXTRAP: fprintf(stderr, "C"); break;
	case IVL_LINEAR_EXTRAP:   fprintf(stderr, "L"); break;
	case IVL_ERROR_EXTRAP:    fprintf(stderr, "E"); break;
	default: fprintf(stderr, "<%d>", extrap); break;
      }
}

/*
 * Print some diagnostic information about the $table_model() call.
 */
static void dump_diag_info(vpiHandle callh, p_table_mod table)
{
      unsigned idx;
      char msg[64];

      snprintf(msg, sizeof(msg), "DEBUG: %s:%d:", vpi_get_str(vpiFile, callh),
               (int)vpi_get(vpiLineNo, callh));
      msg[sizeof(msg)-1] = 0;
      fprintf(stderr, "%s Building %u variable table using \"%s\" and\n",
              msg, table->dims, table->file.name);
      fprintf(stderr, "%*s the following control string: ",
              (int) strlen(msg), " ");
      dump_interp(table->control.info.interp[0]);
      dump_extrap(table->control.info.extrap_low[0]);
      dump_extrap(table->control.info.extrap_high[0]);
      for (idx = 1; idx < table->fields; idx += 1) {
	    fprintf(stderr, ",");
	    dump_interp(table->control.info.interp[idx]);
	    dump_extrap(table->control.info.extrap_low[idx]);
	    dump_extrap(table->control.info.extrap_high[idx]);
      }
      fprintf(stderr, ";%u\n", table->depend);
      fprintf(stderr, "%*s The data file must have at least %u columns.\n",
              (int) strlen(msg), " ", table->fields+table->depend);
}

/*
 * Initialize the table model data structure.
 *
 * The first time $table_model() is called we need to evaluate the file name
 * and control strings. Then we must load the data from the file.
 */
static unsigned initialize_table_model(vpiHandle callh, const char *name,
                                       p_table_mod table)
{
      s_vpi_value val;
      FILE *fp;

	/* Get the table file name. */
      table->file.name = get_filename(callh, name, table->file.arg);
      table->have_fname = 1;
      if (table->file.name == 0) return 1;

	/* Open the file. */
      fp = fopen(table->file.name, "r");
      if (fp == 0) {
	    vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
	               (int)vpi_get(vpiLineNo, callh));
	    vpi_printf("%s() cannot open file \"%s\" for reading (%s).\n",
	               name, table->file.name, strerror(errno));
	    return 1;
      }

	/* Check to see if there is a control string. */
      if (table->control.arg) {
	    val.format = vpiStringVal;
	    vpi_get_value(table->control.arg, &val);
	    if (*(val.value.str) == 0) {
		   /* If there is an empty control string field then we just
		    * use the default values for interpolation/extrapolation.
		    * We also assume that there only dimensions + 1 fields. */
		  initialize_control_fields_def(table);
	    } else {
		  if (initialize_control_fields(callh, table, val.value.str)) {
			return 1;
		  }
	    }
      } else {
	      /* If there is no control string field then we just use the
	       * default values for interpolation/extrapolation. We also
	       * assume that there only dimensions + 1 fields. */
	    initialize_control_fields_def(table);
      }

	/* If requested dump some diagnostic information. */
      if (table_model_debug) dump_diag_info(callh, table);

	/* Parse the given file and produce the basic data structure. We
	 * need to have columns for each control string field and for the
	 * dependent data. */
      if (parse_table_model(fp, callh, table)) return 1;
// HERE: once we are done with the control string should we build a table
//       of function pointer for the interp/extrap?

	/* Close the file now that we have loaded all the data. */
      if (fclose(fp)) {
	    vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
	               (int)vpi_get(vpiLineNo, callh));
	    vpi_printf("%s() cannot close file \"%s\" (%s).\n",
	               name, table->file.name, strerror(errno));
	    return 1;
      }

	/* Allocate space for the current argument values. */
      table->indep_val = (double*) malloc(sizeof(double)*table->dims);
      assert(table->indep_val);

      return 0;
}

/*
 * Routine to evaluate the table model using the current input values.
 */
static double eval_table_model(vpiHandle callh, p_table_mod table)
{
      unsigned idx;

      (void)callh; /* Parameter is not used. */
fprintf(stderr, "Evaluating table \"%s\" with %u variables\n",
        table->file.name, table->dims);
      for (idx = 0; idx < table->dims; idx += 1) {
fprintf(stderr, "  Arg %u: %#g\n", idx, table->indep_val[idx]);
      }

      return 0.0;
}

/*
 * Runtime routine for the $table_model().
 */
static PLI_INT32 sys_table_model_calltf(ICARUS_VPI_CONST PLI_BYTE8 *name)
{
      vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
      s_vpi_value val;
      p_table_mod table;
      unsigned idx;
      double result;

	/* Retrieve the table data. */
      table = vpi_get_userdata(callh);

	/* If this is the first call then build the data structure. */
      if ((table->have_fname == 0) &&
          initialize_table_model(callh, name, table)) {
	    vpip_set_return_value(1);
	    vpi_control(vpiFinish, 1);
	    return 0;
      }

	/* Load the current argument values into the table structure. */
      for (idx = 0; idx < table->dims; idx += 1) {
	    val.format = vpiRealVal;
	    vpi_get_value(table->indep[idx], &val);
	    table->indep_val[idx] = val.value.real;
      }

	/* Interpolate/extrapolate the data structure to find the value. */
      result = eval_table_model(callh, table);

	/* Return the calculated value. */
      val.format = vpiRealVal;
      val.value.real = result;
      vpi_put_value(callh, &val, 0, vpiNoDelay);

      return 0;
}

/*
 * Routine to register the system function provided in this file.
 */
void table_model_register(void)
{
      s_vpi_systf_data tf_data;
      s_cb_data cb;
      vpiHandle res;

      tf_data.type = vpiSysFunc;
      tf_data.sysfunctype = vpiSysFuncReal;
      tf_data.tfname = "$table_model";
      tf_data.calltf = sys_table_model_calltf;
      tf_data.compiletf = sys_table_model_compiletf;
      tf_data.sizetf = 0;  /* Not needed for a vpiSysFuncReal. */
      tf_data.user_data = "$table_model";
      res = vpi_register_systf(&tf_data);
      vpip_make_systf_system_defined(res);

	/* Create a callback to clear all the table model memory when the
	 * simulator finishes. */
      cb.time = NULL;
      cb.reason = cbEndOfSimulation;
      cb.cb_rtn = cleanup_table_mod;
      cb.user_data = 0x0;
      cb.obj = 0x0;

      vpi_register_cb(&cb);
}