File: sfunc.cc

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 (189 lines) | stat: -rw-r--r-- 4,994 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (c) 2006-2012 Stephen Williams (steve@icarus.com)
 *
 *    This source code is free software; you can redistribute it
 *    and/or modify it in source code form 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  "compile.h"
# include  "config.h"
#ifdef CHECK_WITH_VALGRIND
# include "vvp_cleanup.h"
#endif
# include  "sfunc.h"
# include  <cstdlib>
# include  <cstring>
# include  <iostream>
# include  <cassert>


sfunc_core::sfunc_core(vvp_net_t*net, vpiHandle sys,
		       unsigned argc, vpiHandle*argv)
: vvp_wide_fun_core(net, argc)
{
      sys_  = sys;
      argc_ = argc;
      argv_ = argv;
}

sfunc_core::~sfunc_core()
{
      delete sys_;
#ifdef CHECK_WITH_VALGRIND
      for (unsigned i = 0; i < argc_; i += 1) {
	    constant_delete(argv_[i]);
      }
#endif
      delete [] argv_;
}

/*
 * This method is only called when a trigger event occurs. Just arrange for
 * the function to be called.
 */
void sfunc_core::recv_vec4(vvp_net_ptr_t, const vvp_vector4_t&/*bit*/,
                           vvp_context_t)
{
      schedule_functor(this);
}

void sfunc_core::recv_vec4_from_inputs(unsigned port)
{
      vpiHandle vpi = argv_[port];
      struct __vpiBinaryConst*obj = dynamic_cast<__vpiBinaryConst*>(vpi);
      assert(obj);

      obj->bits = value(port);

        /* Schedule the actual call after this finishes. */
      schedule_functor(this);
}

void sfunc_core::recv_real_from_inputs(unsigned port)
{
      vpiHandle vpi = argv_[port];
      __vpiRealConst*obj = dynamic_cast<__vpiRealConst*>(vpi);
      assert(obj);

      obj->value = value_r(port);

        /* Schedule the actual call after this finishes. */
      schedule_functor(this);
}

void sfunc_core::run_run()
{
      vpip_execute_vpi_call(0, sys_);
}

static int make_vpi_argv(unsigned argc, vpiHandle*vpi_argv,
			const char*arg_string)
{
      unsigned idx = 0;
      const char*cp = arg_string;
      int return_type = 0;

      switch (*cp) {
	  case 'r': // real result
	    cp += 1;
	    return_type = -vpiRealVal;
	    break;

	  case 'v': // vector4_t
	    cp += 1;
	    return_type = strtoul(cp, 0, 10);
	    cp += strspn(cp, "0123456789");
	    break;

	  default:
	    fprintf(stderr, "Unsupported type %c(%d).\n", *cp, *cp);
	    assert(0);
	    break;
      }

      while (*cp) {
	    assert(idx < argc);

	    switch (*cp) {
		case 'r': // real
		  cp += 1;
		  vpi_argv[idx] = vpip_make_real_const(0.0);
		  break;

		case 'v': { // vector4_t (v<n>)
		      cp += 1;
		      unsigned wid = strtoul(cp, 0, 10);
		      cp += strspn(cp, "0123456789");
		      vpi_argv[idx] = vpip_make_binary_const(wid, "x");
		      break;
		}

		default:
		  fprintf(stderr, "Unsupported type %c(%d).\n", *cp, *cp);
		  assert(0);
	    }
	    idx += 1;
      }

      assert(idx == argc);
      return return_type;
}


void compile_sfunc(char*label, char*name,  char*format_string,
		   long file_idx, long lineno,
		   unsigned argc, struct symb_s*argv,
                   char*trigger_label)
{
      unsigned vec4_stack = 0;
      unsigned real_stack = 0;
      unsigned string_stack = 0;
      vpiHandle*vpi_argv = new vpiHandle[argc];
      int val_code = make_vpi_argv(argc, vpi_argv, format_string);
      unsigned val_width = 0;
      delete[] format_string;

	// The make_vpi_argv returns for the function return value a
	// >0 value for the vector width if this is a vector. Convert
	// it to the form that the vpip_build_vpi_call uses.
      if (val_code > 0) {
	    val_width = val_code;
	    val_code = -vpiVectorVal;
      }

      vvp_net_t*ptr = new vvp_net_t;

      vpiHandle sys = vpip_build_vpi_call(name, val_code, val_width, ptr,
                                          true, false, argc, vpi_argv,
					  vec4_stack, real_stack, string_stack,
                                          file_idx, lineno);
      assert(sys);

	/* Create and connect the functor to the label. */
      sfunc_core*score = new sfunc_core(ptr, sys, argc, vpi_argv);
      ptr->fun = score;
      define_functor_symbol(label, ptr);
      free(label);

	/* Link the inputs to the functor. */
      wide_inputs_connect(score, argc, argv);
      free(argv);

        /* If this function has a trigger event, connect the functor to
           that event. */
      if (trigger_label)
            input_connect(ptr, 0, trigger_label);
      delete[] name;
}