File: v2009_array.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 (316 lines) | stat: -rw-r--r-- 9,533 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
/*
 *  Copyright (C) 2013-2021  Cary R. (cygcary@yahoo.com)
 *  Copyright (C) 2014  Stephen Williams (steve@icarus.com)
 *  Copyright (C) 2014  CERN
 *  @author Maciej Suminski (maciej.suminski@cern.ch)
 *
 *  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>

static PLI_INT32 one_array_arg_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
      vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
      vpiHandle argv;
      vpiHandle arg;

      argv = vpi_iterate(vpiArgument, callh);
      if (argv == 0) {
	    vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
	               (int)vpi_get(vpiLineNo, callh));
	    vpi_printf("%s requires an array argument.\n", name);
	    vpip_set_return_value(1);
	    vpi_control(vpiFinish, 1);
	    return 0;
      }

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

      arg = vpi_scan(argv);
      if (arg != 0) {
	    vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
	               (int)vpi_get(vpiLineNo, callh));
	    vpi_printf("%s has too many arguments.\n", name);
	    vpip_set_return_value(1);
	    vpi_control(vpiFinish, 1);
	    return 0;
      }

      return 0;
}

// Checks if a function is passed an array and optionally an integer.
static PLI_INT32 array_int_opt_arg_compiletf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
#if defined(__GNUC__)
      const int MAX_ARGC = 3;   // one more is to verify there are at most 2 args
#else
#define MAX_ARGC 3
#endif
      vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
      vpiHandle argv, arg[MAX_ARGC];
      PLI_INT32 arg_type[MAX_ARGC];
      int argc;

      argv = vpi_iterate(vpiArgument, callh);
      argc = 0;

      if(argv) {
          for(int i = 0; i < MAX_ARGC; ++i) {
              arg[i] = vpi_scan(argv);
              if(arg[i]) {
                  arg_type[i] = vpi_get(vpiType, arg[i]);
                  ++argc;
              } else {
                  break;
              }
          }
      }

      if (!argv || argc == MAX_ARGC || (arg_type[0] != vpiRegArray && arg_type[0] != vpiStringVar) ||
          (argc == 2 && arg_type[1] != vpiIntegerVar &&
          !(arg_type[1] == vpiConstant && vpi_get(vpiConstType, arg[1]) == vpiBinaryConst))) {
            vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
                      (int)vpi_get(vpiLineNo, callh));
            vpi_printf("%s expects an array and optionally an integer.\n", name);

            if(argc == MAX_ARGC)
                vpi_free_object(argv);

	    vpip_set_return_value(1);
            vpi_control(vpiFinish, 0);
            return 0;
      }

      return 0;
}

static PLI_INT32 func_not_implemented_compiletf(ICARUS_VPI_CONST PLI_BYTE8* name)
{
      vpiHandle callh = vpi_handle(vpiSysTfCall, 0);

      vpi_printf("SORRY: %s:%d: function %s() is not currently implemented.\n",
                 vpi_get_str(vpiFile, callh), (int)vpi_get(vpiLineNo, callh),
                 name);
      vpip_set_return_value(1);
      vpi_control(vpiFinish, 1);
      return 0;
}

static PLI_INT32 array_get_property(int property, ICARUS_VPI_CONST PLI_BYTE8*name)
{
      vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
      vpiHandle argv, array, dim;
      s_vpi_value value;

      argv = vpi_iterate(vpiArgument, callh);
      if (argv == 0) {
            vpi_printf("ERROR: %s:%d: ", vpi_get_str(vpiFile, callh),
                       (int)vpi_get(vpiLineNo, callh));
            vpi_printf("%s requires an array argument.\n", name);
	    vpip_set_return_value(1);
            vpi_control(vpiFinish, 1);
            return 0;
      }

      array = vpi_scan(argv);
      dim = vpi_scan(argv);

      if(dim != 0) {
          vpi_printf("SORRY: %s:%d: multiple dimensions are not handled yet.\n",
                   vpi_get_str(vpiFile, callh), (int)vpi_get(vpiLineNo, callh));
	  vpip_set_return_value(1);
          vpi_control(vpiFinish, 1);
      }

      value.format = vpiIntVal;
      value.value.integer = vpi_get(property, array);
      vpi_put_value(callh, &value, 0, vpiNoDelay);

      return 0;
}

static PLI_INT32 left_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
    return array_get_property(vpiLeftRange, name);
}

static PLI_INT32 right_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
    return array_get_property(vpiRightRange, name);
}

static void high_array(const char*name, vpiHandle callh, vpiHandle arg)
{
      s_vpi_value value;
      int array_type;
      int size;

      switch ( (array_type = vpi_get(vpiArrayType, arg)) ) {
	  case vpiDynamicArray:
	  case vpiQueueArray:
	    size = vpi_get(vpiSize, arg);
	    value.format = vpiIntVal;
	    value.value.integer = size - 1;
	    vpi_put_value(callh, &value, 0, vpiNoDelay);
	    break;

	  default:
	    vpi_printf("SORRY: %s:%d: function %s() argument object code is %d\n",
		       vpi_get_str(vpiFile,callh), (int)vpi_get(vpiLineNo, callh),
		       name, array_type);
	    break;
      }
}

static PLI_INT32 high_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
      vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
      vpiHandle argv;
      vpiHandle arg;
      int object_code;

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

      argv = vpi_iterate(vpiArgument, callh);
      assert(argv);
      arg = vpi_scan(argv);
      assert(arg);
      vpi_free_object(argv);

      switch ( (object_code = vpi_get(vpiType, arg)) ) {
	  case vpiArrayVar:
	    high_array(name, callh, arg);
	    break;

	  default:
	    vpi_printf("SORRY: %s:%d: function %s() argument object code is %d\n",
		       vpi_get_str(vpiFile,callh), (int)vpi_get(vpiLineNo, callh),
		       name, object_code);
	    return 0;
      }

      return 0;
}

static void low_array(const char*name, vpiHandle callh, vpiHandle arg)
{
      s_vpi_value value;
      int array_type;

      switch ( (array_type = vpi_get(vpiArrayType, arg)) ) {
	  case vpiDynamicArray:
	  case vpiQueueArray:
	    value.format = vpiIntVal;
	    value.value.integer = 0;
	    vpi_put_value(callh, &value, 0, vpiNoDelay);
	    break;

	  default:
	    vpi_printf("SORRY: %s:%d: function %s() argument object code is %d\n",
		       vpi_get_str(vpiFile,callh), (int)vpi_get(vpiLineNo, callh),
		       name, array_type);
	    break;
      }
}

static PLI_INT32 low_calltf(ICARUS_VPI_CONST PLI_BYTE8*name)
{
      vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
      vpiHandle argv;
      vpiHandle arg;
      int object_code;

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

      argv = vpi_iterate(vpiArgument, callh);
      assert(argv);
      arg = vpi_scan(argv);
      assert(arg);
      vpi_free_object(argv);

      switch ( (object_code = vpi_get(vpiType, arg)) ) {
	  case vpiArrayVar:
	    low_array(name, callh, arg);
	    break;

	  default:
	    vpi_printf("SORRY: %s:%d: function %s() argument object code is %d\n",
		       vpi_get_str(vpiFile,callh), (int)vpi_get(vpiLineNo, callh),
		       name, object_code);
	    return 0;
      }

      return 0;
}

void v2009_array_register(void)
{
      s_vpi_systf_data tf_data;
      vpiHandle res;

      tf_data.type        = vpiSysFunc;
      tf_data.sysfunctype = vpiIntFunc;
      tf_data.calltf      = 0;
      tf_data.compiletf   = func_not_implemented_compiletf;;
      tf_data.sizetf      = 0;

      tf_data.tfname      = "$high";
      tf_data.user_data   = "$high";
      tf_data.compiletf   = one_array_arg_compiletf;
      tf_data.calltf      = high_calltf;
      res = vpi_register_systf(&tf_data);
      vpip_make_systf_system_defined(res);

      tf_data.tfname      = "$low";
      tf_data.user_data   = "$low";
      tf_data.compiletf   = one_array_arg_compiletf;
      tf_data.calltf      = low_calltf;
      res = vpi_register_systf(&tf_data);
      vpip_make_systf_system_defined(res);

      tf_data.tfname      = "$left";
      tf_data.user_data   = "$left";
      tf_data.compiletf   = array_int_opt_arg_compiletf;
      tf_data.calltf      = left_calltf;
      res = vpi_register_systf(&tf_data);
      vpip_make_systf_system_defined(res);

      tf_data.tfname      = "$right";
      tf_data.user_data   = "$right";
      tf_data.compiletf   = array_int_opt_arg_compiletf;
      tf_data.calltf      = right_calltf;
      res = vpi_register_systf(&tf_data);
      vpip_make_systf_system_defined(res);

	/* These functions are not currently implemented. */
      tf_data.tfname      = "$dimensions";
      tf_data.user_data   = "$dimensions";
      res = vpi_register_systf(&tf_data);
      vpip_make_systf_system_defined(res);

      tf_data.tfname      = "$unpacked_dimensions";
      tf_data.user_data   = "$unpacked_dimensions";
      res = vpi_register_systf(&tf_data);
      vpip_make_systf_system_defined(res);

      tf_data.tfname      = "$increment";
      tf_data.user_data   = "$increment";
      res = vpi_register_systf(&tf_data);
      vpip_make_systf_system_defined(res);
}