1 /*
2 * Copyright (c) 2012-2015 Stephen Williams (steve@icarus.com)
3 *
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20
21 # include "compile.h"
22 # include "vpi_priv.h"
23 # include "vvp_net_sig.h"
24 # include "vvp_darray.h"
25 # include "array_common.h"
26 # include "schedule.h"
27 #ifdef CHECK_WITH_VALGRIND
28 # include "vvp_cleanup.h"
29 #endif
30 # include <cstdio>
31 # include <cstdlib>
32 # include <cstring>
33 # include <cassert>
34 # include "ivl_alloc.h"
35
36 using namespace std;
37
__vpiDarrayVar(__vpiScope * sc,const char * na,vvp_net_t * ne)38 __vpiDarrayVar::__vpiDarrayVar(__vpiScope*sc, const char*na, vvp_net_t*ne)
39 : __vpiBaseVar(sc, na, ne)
40 {
41 }
42
get_size() const43 unsigned __vpiDarrayVar::get_size() const
44 {
45 vvp_fun_signal_object*fun = dynamic_cast<vvp_fun_signal_object*> (get_net()->fun);
46 if(!fun)
47 return 0;
48
49 vvp_object_t val = fun->get_object();
50 vvp_darray*aval = val.peek<vvp_darray>();
51
52 if(!aval)
53 return 0;
54
55 return aval->get_size();
56 }
57
get_left_range()58 vpiHandle __vpiDarrayVar::get_left_range()
59 {
60 left_range_.set_value(0);
61 return &left_range_;
62 }
63
get_right_range()64 vpiHandle __vpiDarrayVar::get_right_range()
65 {
66 right_range_.set_value(get_size() - 1);
67 return &right_range_;
68 }
69
get_word_size() const70 int __vpiDarrayVar::get_word_size() const
71 {
72 vvp_vector4_t new_vec;
73 vvp_darray*aobj = get_vvp_darray();
74 aobj->get_word(0, new_vec);
75 return new_vec.size();
76 }
77
get_word_str(struct __vpiArrayWord * word,int code)78 char*__vpiDarrayVar::get_word_str(struct __vpiArrayWord*word, int code)
79 {
80 unsigned index = word->get_index();
81
82 if (code == vpiFile) { // Not implemented for now!
83 return simple_set_rbuf_str(file_names[0]);
84 }
85
86 char sidx [64];
87 snprintf(sidx, 63, "%d", (int)index);
88 return generic_get_str(code, scope_, name_, sidx);
89 }
90
get_word_value(struct __vpiArrayWord * word,p_vpi_value vp)91 void __vpiDarrayVar::get_word_value(struct __vpiArrayWord*word, p_vpi_value vp)
92 {
93 unsigned index = word->get_index();
94 vvp_darray*aobj = get_vvp_darray();
95
96 if(vp->format == vpiObjTypeVal) {
97 if(dynamic_cast<vvp_darray_real*>(aobj))
98 vp->format = vpiRealVal;
99 else if(dynamic_cast<vvp_darray_string*>(aobj))
100 vp->format = vpiStringVal;
101 else
102 vp->format = vpiVectorVal;
103 }
104
105 switch(vp->format) {
106 case vpiBinStrVal:
107 case vpiOctStrVal:
108 case vpiDecStrVal:
109 case vpiHexStrVal:
110 case vpiScalarVal:
111 case vpiIntVal:
112 {
113 vvp_vector4_t v;
114 aobj->get_word(index, v); // width == 1?
115 vpip_vec4_get_value(v, 1, false, vp); // TODO sign?
116 }
117 break;
118
119 case vpiVectorVal: // TODO vpip_vec2_ or vpip_vec4_?
120 {
121 vvp_vector4_t v;
122 aobj->get_word(index, v);
123 vpip_vec2_get_value(v, v.size(), false, vp); // TODO sign?
124 }
125 break;
126
127 case vpiRealVal:
128 {
129 double d;
130 aobj->get_word(index, d);
131 vpip_real_get_value(d, vp);
132 }
133 break;
134
135 case vpiStringVal:
136 {
137 string s;
138 aobj->get_word(index, s);
139 vpip_string_get_value(s, vp);
140 }
141 break;
142
143 default:
144 fprintf(stderr, "vpi sorry: format is not implemented\n");
145 assert(false);
146 }
147 }
148
put_word_value(struct __vpiArrayWord * word,p_vpi_value vp,int)149 void __vpiDarrayVar::put_word_value(struct __vpiArrayWord*word, p_vpi_value vp, int)
150 {
151 unsigned index = word->get_index();
152 vvp_darray*aobj = get_vvp_darray();
153
154 switch(vp->format) {
155 case vpiScalarVal:
156 {
157 vvp_vector4_t vec(1, vp->value.scalar);
158 aobj->set_word(index, vec);
159 }
160 break;
161
162 case vpiIntVal:
163 {
164 vvp_vector4_t vec;
165 unsigned long val = vp->value.integer;
166 vec.setarray(0, 8 * sizeof(vp->value.integer), &val);
167 aobj->set_word(index, vec);
168 }
169 break;
170
171 case vpiVectorVal: // 2 vs 4 state logic?
172 {
173 int size = get_word_size();
174 PLI_INT32 a = 0, b = 0;
175 vvp_vector4_t new_vec(size);
176 p_vpi_vecval vec = vp->value.vector;
177 vec--; // it will be increased in the first loop iteration
178
179 for(int i = 0; i < size; ++i) {
180 int new_bit;
181 if(i % (8 * sizeof(vec->aval)) == 0) {
182 ++vec;
183 a = vec->aval;
184 b = vec->bval;
185 }
186
187 // convert to vvp_bit4_t
188 new_bit = ((b & 1) << 2) | (a & 1);
189 new_vec.set_bit(i, (vvp_bit4_t) new_bit);
190
191 a >>= 1;
192 b >>= 1;
193 }
194 aobj->set_word(index, new_vec);
195 }
196 break;
197
198 case vpiRealVal:
199 aobj->set_word(index, vp->value.real);
200 break;
201
202 case vpiStringVal:
203 aobj->set_word(index, std::string(vp->value.str));
204 break;
205
206 default:
207 fprintf(stderr, "vpi sorry: format is not implemented");
208 assert(false);
209 }
210 }
211
get_iter_index(struct __vpiArrayIterator *,int idx)212 vpiHandle __vpiDarrayVar::get_iter_index(struct __vpiArrayIterator*, int idx)
213 {
214 if (vals_words == 0) make_vals_words();
215
216 return &(vals_words[idx].as_word);
217 }
218
vpi_get(int code)219 int __vpiDarrayVar::vpi_get(int code)
220 {
221 switch (code) {
222 case vpiArrayType:
223 return vpiDynamicArray;
224 case vpiLeftRange:
225 return 0;
226 case vpiRightRange:
227 return get_size() - 1;
228 case vpiSize:
229 return get_size();
230
231 default:
232 fprintf(stderr, "vpi sorry: property is not implemented");
233 assert(false);
234 return 0;
235 }
236 }
237
vpi_get_str(int code)238 char* __vpiDarrayVar::vpi_get_str(int code)
239 {
240 if (code == vpiFile) { // Not implemented for now!
241 return simple_set_rbuf_str(file_names[0]);
242 }
243
244 return generic_get_str(code, scope_, name_, NULL);
245 }
246
vpi_handle(int code)247 vpiHandle __vpiDarrayVar::vpi_handle(int code)
248 {
249 switch (code) {
250 case vpiLeftRange:
251 return get_left_range();
252
253 case vpiRightRange:
254 return get_right_range();
255
256 case vpiScope:
257 return scope_;
258
259 case vpiModule:
260 return vpip_module(scope_);
261 }
262
263 return 0;
264 }
265
vpi_index(int index)266 vpiHandle __vpiDarrayVar::vpi_index(int index)
267 {
268 if (index >= (long) get_size())
269 return 0;
270 if (index < 0)
271 return 0;
272
273 if (vals_words == 0)
274 make_vals_words();
275
276 return &(vals_words[index].as_word);
277 }
278
vpi_get_value(p_vpi_value val)279 void __vpiDarrayVar::vpi_get_value(p_vpi_value val)
280 {
281 val->format = vpiSuppressVal;
282 }
283
get_vvp_darray() const284 vvp_darray*__vpiDarrayVar::get_vvp_darray() const
285 {
286 vvp_fun_signal_object*fun = dynamic_cast<vvp_fun_signal_object*> (get_net()->fun);
287 assert(fun);
288 vvp_object_t obj = fun->get_object();
289
290 return obj.peek<vvp_darray>();
291 }
292
vpip_make_darray_var(const char * name,vvp_net_t * net)293 vpiHandle vpip_make_darray_var(const char*name, vvp_net_t*net)
294 {
295 __vpiScope*scope = vpip_peek_current_scope();
296 const char*use_name = name ? vpip_name_string(name) : 0;
297
298 __vpiDarrayVar*obj = new __vpiDarrayVar(scope, use_name, net);
299
300 return obj;
301 }
302
__vpiQueueVar(__vpiScope * sc,const char * na,vvp_net_t * ne)303 __vpiQueueVar::__vpiQueueVar(__vpiScope*sc, const char*na, vvp_net_t*ne)
304 : __vpiBaseVar(sc, na, ne)
305 {
306 }
307
get_type_code(void) const308 int __vpiQueueVar::get_type_code(void) const
309 { return vpiArrayVar; }
310
311
vpi_get(int code)312 int __vpiQueueVar::vpi_get(int code)
313 {
314 vvp_fun_signal_object*fun = dynamic_cast<vvp_fun_signal_object*> (get_net()->fun);
315 assert(fun);
316 vvp_object_t val = fun->get_object();
317 vvp_queue*aval = val.peek<vvp_queue>();
318
319 switch (code) {
320 case vpiArrayType:
321 return vpiQueueArray;
322 case vpiSize:
323 if (aval == 0)
324 return 0;
325 else
326 return aval->get_size();
327
328 default:
329 return 0;
330 }
331 }
332
vpi_get_value(p_vpi_value val)333 void __vpiQueueVar::vpi_get_value(p_vpi_value val)
334 {
335 val->format = vpiSuppressVal;
336 }
337
338
vpip_make_queue_var(const char * name,vvp_net_t * net)339 vpiHandle vpip_make_queue_var(const char*name, vvp_net_t*net)
340 {
341 __vpiScope*scope = vpip_peek_current_scope();
342 const char*use_name = name ? vpip_name_string(name) : 0;
343
344 __vpiQueueVar*obj = new __vpiQueueVar(scope, use_name, net);
345
346 return obj;
347 }
348
349 #ifdef CHECK_WITH_VALGRIND
darray_delete(vpiHandle item)350 void darray_delete(vpiHandle item)
351 {
352 __vpiDarrayVar*obj = dynamic_cast<__vpiDarrayVar*>(item);
353 if (obj->vals_words) delete [] (obj->vals_words-1);
354 delete obj;
355 }
356
queue_delete(vpiHandle item)357 void queue_delete(vpiHandle item)
358 {
359 __vpiQueueVar*obj = dynamic_cast<__vpiQueueVar*>(item);
360 delete obj;
361 }
362 #endif
363