File: sys_countdrivers.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 (225 lines) | stat: -rw-r--r-- 6,926 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
/*
 *  Copyright (C) 2012-2021  Martin Whitaker. (icarus@martin-whitaker.me.uk)
 *
 *  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 <inttypes.h>
#include <stdlib.h>
#include "ivl_alloc.h"

/*
 * Check to see if an argument is a single bit net.
 */
static void check_net_arg(vpiHandle arg, vpiHandle callh, const char *name)
{
      assert(arg);

      switch (vpi_get(vpiType, arg)) {
	  case vpiPartSelect:
            if (vpi_get(vpiType, vpi_handle(vpiParent, arg)) != vpiNet)
                  break;
	    // fallthrough
	  case vpiNet:
	    if (vpi_get(vpiSize, arg) != 1)
                  break;
            return;
	  default:
            break;
      }
      vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
                 (int)vpi_get(vpiLineNo, callh));
      vpi_printf("%s's first argument must be a scalar net or "
                 "a bit-select of a vector net.\n", name);
      vpip_set_return_value(1);
      vpi_control(vpiFinish, 1);
}

/*
 * Check to see if an argument is a variable.
 */
static void check_var_arg(vpiHandle arg, vpiHandle callh, const char *name,
                          const char *arg_name)
{
      assert(arg);

      switch (vpi_get(vpiType, arg)) {
	  case vpiPartSelect:
            if (vpi_get(vpiType, vpi_handle(vpiParent, arg)) == vpiNet)
                  break;
	  case vpiMemoryWord:
	  case vpiBitVar:
	  case vpiReg:
	  case vpiIntegerVar:
	  case vpiIntVar:
	  case vpiLongIntVar:
	  case vpiTimeVar:
	    return;
	  default:
            break;
      }
      vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
                 (int)vpi_get(vpiLineNo, callh));
      vpi_printf("%s's %s argument must be a variable.\n",
                 name, arg_name);
      vpip_set_return_value(1);
      vpi_control(vpiFinish, 1);
}

static PLI_INT32 sys_countdrivers_sizetf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
    (void)name;  /* Parameter is not used. */
    return 1;
}

/*
 * Check that the given $countdrivers() call has valid arguments.
 */
static PLI_INT32 sys_countdrivers_compiletf(ICARUS_VPI_CONST PLI_BYTE8 *name)
{
      vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
      vpiHandle argv = vpi_iterate(vpiArgument, callh);
      vpiHandle arg;
      unsigned arg_num;

	/* 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 one argument.\n", name);
	    vpip_set_return_value(1);
	    vpi_control(vpiFinish, 1);
	    return 0;
      }

	/* The first argument must be a scalar net or a net bit select. */
      arg = vpi_scan(argv);
      check_net_arg(arg, callh, name);

        /* The optional arguments must be variables. */
      for (arg_num = 2; arg_num < 7; arg_num += 1) {
	    const char *arg_name = NULL;
	    switch (arg_num) {
	        case 2: arg_name = "second"; break;
	        case 3: arg_name = "third";  break;
	        case 4: arg_name = "fourth"; break;
	        case 5: arg_name = "fifth";  break;
	        case 6: arg_name = "sixth";  break;
	        default: assert(0);
	    }

	    arg = vpi_scan(argv);
            if (arg == 0)
                  return 0;

            check_var_arg(arg, callh, name, arg_name);
      }

      /* Make sure there are no extra arguments. */
      check_for_extra_args(argv, callh, name, "six arguments", 0);
      return 0;
}

/*
 * The runtime code for $countdrivers().
 */
static PLI_INT32 sys_countdrivers_calltf(ICARUS_VPI_CONST PLI_BYTE8 *name)
{
      vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
      vpiHandle argv = vpi_iterate(vpiArgument, callh);
      vpiHandle arg;
      unsigned idx;
      unsigned counts[4];
      unsigned num_drivers;
      s_vpi_value val;

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

        /* All returned values are integers. */
      val.format = vpiIntVal;

        /* Get the base net reference and bit select */
      idx = 0;
      arg = vpi_scan(argv);
      assert(arg);
      if (vpi_get(vpiType, arg) == vpiPartSelect) {
            idx = vpi_get(vpiLeftRange, arg);
            arg = vpi_handle(vpiParent, arg);
            assert(arg);
      }

        /* Get the net driver counts from the runtime. */
      vpip_count_drivers(arg, idx, counts);
      num_drivers = counts[0] + counts[1] + counts[2];

        /* Handle optional net_is_forced argument. */
      arg = vpi_scan(argv);
      if (arg == 0) goto args_done;
      val.value.integer = counts[3];
      vpi_put_value(arg, &val, 0, vpiNoDelay);

        /* Handle optional number_of_01x_drivers argument. */
      arg = vpi_scan(argv);
      if (arg == 0) goto args_done;
      val.value.integer = num_drivers;
      vpi_put_value(arg, &val, 0, vpiNoDelay);

        /* Handle optional number_of_0_drivers argument. */
      arg = vpi_scan(argv);
      if (arg == 0) goto args_done;
      val.value.integer = counts[0];
      vpi_put_value(arg, &val, 0, vpiNoDelay);

        /* Handle optional number_of_1_drivers argument. */
      arg = vpi_scan(argv);
      if (arg == 0) goto args_done;
      val.value.integer = counts[1];
      vpi_put_value(arg, &val, 0, vpiNoDelay);

        /* Handle optional number_of_x_drivers argument. */
      arg = vpi_scan(argv);
      if (arg == 0) goto args_done;
      val.value.integer = counts[2];
      vpi_put_value(arg, &val, 0, vpiNoDelay);

        /* Free the argument iterator. */
      vpi_free_object(argv);

args_done:
      val.value.integer = (num_drivers > 1) ? 1 : 0;
      vpi_put_value(callh, &val, 0, vpiNoDelay);
      return 0;
}

/*
 * Routine to register the system tasks/functions provided in this file.
 */
void sys_countdrivers_register(void)
{
      s_vpi_systf_data tf_data;
      vpiHandle res;

      tf_data.type        = vpiSysFunc;
      tf_data.sysfunctype = vpiSizedFunc;
      tf_data.tfname      = "$countdrivers";
      tf_data.calltf      = sys_countdrivers_calltf;
      tf_data.compiletf   = sys_countdrivers_compiletf;
      tf_data.sizetf      = sys_countdrivers_sizetf;
      tf_data.user_data   = "$countdrivers";
      res = vpi_register_systf(&tf_data);
      vpip_make_systf_system_defined(res);
}