1 // -*- mode: C++; c-file-style: "cc-mode" -*-
2 //*************************************************************************
3 //
4 // Copyright 2020 by Yutetsu TAKATSUKASA. This program is free software; you can
5 // redistribute it and/or modify it under the terms of either the GNU
6 // Lesser General Public License Version 3 or the Perl Artistic License
7 // Version 2.0.
8 // SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
9 //
10 //*************************************************************************
11 
12 #include <cstdio>
13 #include <cstdlib>
14 #include <cstring>
15 #include <iostream>
16 #include <vector>
17 
18 // clang-format off
19 #if defined(NCSC)
20 // Used by NC's svdpi.h to pick up svLogicVecVal with _.aval and _.bval fields,
21 // rather than the IEEE 1800-2005 version which has _.a and _.b fields.
22 # define DPI_COMPATIBILITY_VERSION_1800v2012
23 #endif
24 
25 #include "svdpi.h"
26 
27 #if defined(VERILATOR)  // Verilator
28 # include "Vt_dpi_arg_inout_unpack__Dpi.h"
29 typedef long long sv_longint_t;
30 typedef unsigned long long sv_longint_unsigned_t;
31 # define NO_SHORTREAL
32 # define NO_UNPACK_STRUCT
33 # define CONSTARG const
34 #elif defined(VCS)  // VCS
35 # include "../vc_hdrs.h"
36 typedef long long sv_longint_t;
37 typedef unsigned long long sv_longint_unsigned_t;
38 # define NO_TIME
39 # define CONSTARG const
40 #elif defined(NCSC)  // NC
41 # include "dpi-exp.h"
42 # include "dpi-imp.h"
43 typedef long long sv_longint_t;
44 typedef unsigned long long sv_longint_unsigned_t;
45 # define NO_TIME
46 # define NO_INTEGER
47 # define NO_SHORTREAL
48 // Sadly NC does not declare pass-by reference input arguments as const
49 # define CONSTARG
50 #elif defined(MS)  // ModelSim
51 # include "dpi.h"
52 typedef int64_t sv_longint_t;
53 typedef uint64_t sv_longint_unsigned_t;
54 # define CONSTARG const
55 #else
56 # error "Unknown simulator for DPI test"
57 #endif
58 // clang-format on
59 
60 //======================================================================
61 // Implementations of imported functions
62 //======================================================================
63 
64 namespace {  // unnamed namespace
65 
66 const bool VERBOSE_MESSAGE = false;
67 
68 #define stop() \
69     do { \
70         printf(__FILE__ ":%d Bad value\n", __LINE__); \
71         abort(); \
72     } while (0)
73 
set_uint(svLogicVecVal * v0,sv_longint_unsigned_t val,int bitwidth)74 void set_uint(svLogicVecVal* v0, sv_longint_unsigned_t val, int bitwidth) {
75     for (int i = 0; i < bitwidth; ++i) {
76         if (i < 64)
77             svPutBitselLogic(v0, i, (val >> i) & 1);
78         else
79             svPutBitselLogic(v0, i, 0);
80     }
81 }
82 
set_uint(svBitVecVal * v0,sv_longint_unsigned_t val,int bitwidth)83 void set_uint(svBitVecVal* v0, sv_longint_unsigned_t val, int bitwidth) {
84     for (int i = 0; i < bitwidth; ++i) {
85         if (i < 64)
86             svPutBitselBit(v0, i, (val >> i) & 1);
87         else
88             svPutBitselBit(v0, i, 0);
89     }
90 }
91 
compare(const T & act,const T & exp)92 template <typename T> bool compare(const T& act, const T& exp) {
93     if (exp == act) {
94         if (VERBOSE_MESSAGE) std::cout << "OK Exp:" << exp << " actual:" << act << std::endl;
95         return true;
96     } else {
97         std::cout << "NG Exp:" << exp << " actual:" << act << std::endl;
98         return false;
99     }
100 }
101 
compare_scalar(const svScalar v0,sv_longint_unsigned_t val)102 bool compare_scalar(const svScalar v0, sv_longint_unsigned_t val) {
103     const bool act_bit = v0 == sv_1;
104     const bool exp_bit = val & 1;
105     if (act_bit != exp_bit) {
106         std::cout << "Mismatch at bit:" << 0 << " exp:" << exp_bit << " act:" << act_bit;
107         return false;
108     }
109     if (VERBOSE_MESSAGE) std::cout << "OK " << val << " as expected " << std::endl;
110     return true;
111 }
112 
compare(const svLogicVecVal * v0,sv_longint_unsigned_t val,int bitwidth)113 bool compare(const svLogicVecVal* v0, sv_longint_unsigned_t val, int bitwidth) {
114     for (int i = 0; i < bitwidth; ++i) {
115         const bool act_bit = svGetBitselLogic(v0, i);
116         const bool exp_bit = (i < 64) ? ((val >> i) & 1) : false;
117         if (act_bit != exp_bit) {
118             std::cout << "Mismatch at bit:" << i << " exp:" << exp_bit << " act:" << act_bit;
119             return false;
120         }
121     }
122     if (VERBOSE_MESSAGE) {
123         std::cout << "OK " << val << " as expected (width:" << bitwidth << ")" << std::endl;
124     }
125     return true;
126 }
127 
compare(const svBitVecVal * v0,sv_longint_unsigned_t val,int bitwidth)128 bool compare(const svBitVecVal* v0, sv_longint_unsigned_t val, int bitwidth) {
129     for (int i = 0; i < bitwidth; ++i) {
130         const bool act_bit = svGetBitselBit(v0, i);
131         const bool exp_bit = (i < 64) ? ((val >> i) & 1) : false;
132         if (act_bit != exp_bit) {
133             std::cout << "Mismatch at bit:" << i << " exp:" << exp_bit << " act:" << act_bit;
134             return false;
135         }
136     }
137     if (VERBOSE_MESSAGE) {
138         std::cout << "OK " << val << " as expected (width:" << bitwidth << ")" << std::endl;
139     }
140     return true;
141 }
142 
update_0d(T * v)143 template <typename T> bool update_0d(T* v) {
144     if (!compare<T>(*v, 42)) return false;
145     ++(*v);
146     return true;
147 }
148 
update_1d(T * v)149 template <typename T> bool update_1d(T* v) {
150     if (!compare<T>(v[0], 43)) return false;
151     if (!compare<T>(v[1], 44)) return false;
152     ++v[0];
153     ++v[1];
154     return true;
155 }
156 
update_2d(T * v)157 template <typename T> bool update_2d(T* v) {
158     if (!compare<T>(v[0 * 2 + 1], 45)) return false;
159     if (!compare<T>(v[1 * 2 + 1], 46)) return false;
160     if (!compare<T>(v[2 * 2 + 1], 47)) return false;
161     ++v[0 * 2 + 1];
162     ++v[1 * 2 + 1];
163     ++v[2 * 2 + 1];
164     return true;
165 }
166 
update_3d(T * v)167 template <typename T> bool update_3d(T* v) {
168     if (!compare<T>(v[(0 * 3 + 0) * 2 + 0], 48)) return false;
169     if (!compare<T>(v[(1 * 3 + 0) * 2 + 0], 49)) return false;
170     if (!compare<T>(v[(2 * 3 + 0) * 2 + 0], 50)) return false;
171     if (!compare<T>(v[(3 * 3 + 0) * 2 + 0], 51)) return false;
172     ++v[(0 * 3 + 0) * 2 + 0];
173     ++v[(1 * 3 + 0) * 2 + 0];
174     ++v[(2 * 3 + 0) * 2 + 0];
175     ++v[(3 * 3 + 0) * 2 + 0];
176     return true;
177 }
178 
update_0d_scalar(svScalar * v)179 bool update_0d_scalar(svScalar* v) {
180     if (!compare_scalar(v[0], sv_0)) return false;
181     v[0] = sv_1;
182     return true;
183 }
update_1d_scalar(svScalar * v)184 bool update_1d_scalar(svScalar* v) {
185     if (!compare_scalar(v[0], sv_1)) return false;
186     v[0] = sv_0;
187     if (!compare_scalar(v[1], sv_0)) return false;
188     v[1] = sv_1;
189     return true;
190 }
update_2d_scalar(svScalar * v)191 bool update_2d_scalar(svScalar* v) {
192     if (!compare_scalar(v[(0 * 2) + 1], sv_1)) return false;
193     v[(0 * 2) + 1] = sv_0;
194     if (!compare_scalar(v[(1 * 2) + 1], sv_0)) return false;
195     v[(1 * 2) + 1] = sv_1;
196     if (!compare_scalar(v[(2 * 2) + 1], sv_1)) return false;
197     v[(2 * 2) + 1] = sv_0;
198     return true;
199 }
update_3d_scalar(svScalar * v)200 bool update_3d_scalar(svScalar* v) {
201     if (!compare_scalar(v[((0 * 3) + 0) * 2 + 0], sv_0)) return false;
202     v[(0 * 3 + 0) * 2 + 0] = sv_1;
203     if (!compare_scalar(v[((1 * 3) + 0) * 2 + 0], sv_1)) return false;
204     v[(1 * 3 + 0) * 2 + 0] = sv_0;
205     if (!compare_scalar(v[((2 * 3) + 0) * 2 + 0], sv_0)) return false;
206     v[(2 * 3 + 0) * 2 + 0] = sv_1;
207     if (!compare_scalar(v[((3 * 3) + 0) * 2 + 0], sv_1)) return false;
208     v[(3 * 3 + 0) * 2 + 0] = sv_0;
209     return true;
210 }
211 
update_0d(T * v,int bitwidth)212 template <typename T> bool update_0d(T* v, int bitwidth) {
213     if (!compare(v, 42, bitwidth)) return false;
214     set_uint(v, 43, bitwidth);
215     return true;
216 }
update_1d(T * v,int bitwidth)217 template <typename T> bool update_1d(T* v, int bitwidth) {
218     const int unit = (bitwidth + 31) / 32;
219     if (!compare(v + unit * 0, 43, bitwidth)) return false;
220     if (!compare(v + unit * 1, 44, bitwidth)) return false;
221     set_uint(v + unit * 0, 44, bitwidth);
222     set_uint(v + unit * 1, 45, bitwidth);
223     return true;
224 }
update_2d(T * v,int bitwidth)225 template <typename T> bool update_2d(T* v, int bitwidth) {
226     const int unit = (bitwidth + 31) / 32;
227     if (!compare(v + unit * (0 * 2 + 1), 45, bitwidth)) return false;
228     if (!compare(v + unit * (1 * 2 + 1), 46, bitwidth)) return false;
229     if (!compare(v + unit * (2 * 2 + 1), 47, bitwidth)) return false;
230     set_uint(v + unit * (0 * 2 + 1), 46, bitwidth);
231     set_uint(v + unit * (1 * 2 + 1), 47, bitwidth);
232     set_uint(v + unit * (2 * 2 + 1), 48, bitwidth);
233     return true;
234 }
update_3d(T * v,int bitwidth)235 template <typename T> bool update_3d(T* v, int bitwidth) {
236     const int unit = (bitwidth + 31) / 32;
237     if (!compare(v + unit * ((0 * 3 + 0) * 2 + 0), 48, bitwidth)) return false;
238     if (!compare(v + unit * ((1 * 3 + 0) * 2 + 0), 49, bitwidth)) return false;
239     if (!compare(v + unit * ((2 * 3 + 0) * 2 + 0), 50, bitwidth)) return false;
240     if (!compare(v + unit * ((3 * 3 + 0) * 2 + 0), 51, bitwidth)) return false;
241     set_uint(v + unit * ((0 * 3 + 0) * 2 + 0), 49, bitwidth);
242     set_uint(v + unit * ((1 * 3 + 0) * 2 + 0), 50, bitwidth);
243     set_uint(v + unit * ((2 * 3 + 0) * 2 + 0), 51, bitwidth);
244     set_uint(v + unit * ((3 * 3 + 0) * 2 + 0), 52, bitwidth);
245     return true;
246 }
247 
set_values(T (& v)[4][3][2])248 template <typename T> void set_values(T (&v)[4][3][2]) {
249     for (int i = 0; i < 4; ++i)
250         for (int j = 0; j < 3; ++j)
251             for (int k = 0; k < 2; ++k) v[i][j][k] = 0;
252     v[3][2][1] = 42;
253     v[2][1][0] = 43;
254     v[2][1][1] = 44;
255     v[1][0][1] = 45;
256     v[1][1][1] = 46;
257     v[1][2][1] = 47;
258     v[0][0][0] = 48;
259     v[1][0][0] = 49;
260     v[2][0][0] = 50;
261     v[3][0][0] = 51;
262 }
263 
set_values(T (& v)[4][3][2][N],int bitwidth)264 template <typename T, size_t N> void set_values(T (&v)[4][3][2][N], int bitwidth) {
265     for (int i = 0; i < 4; ++i)
266         for (int j = 0; j < 3; ++j)
267             for (int k = 0; k < 2; ++k) set_uint(v[i][j][k], 0, bitwidth);
268     set_uint(v[3][2][1], 42, bitwidth);
269     set_uint(v[2][1][0], 43, bitwidth);
270     set_uint(v[2][1][1], 44, bitwidth);
271     set_uint(v[1][0][1], 45, bitwidth);
272     set_uint(v[1][1][1], 46, bitwidth);
273     set_uint(v[1][2][1], 47, bitwidth);
274     set_uint(v[0][0][0], 48, bitwidth);
275     set_uint(v[1][0][0], 49, bitwidth);
276     set_uint(v[2][0][0], 50, bitwidth);
277     set_uint(v[3][0][0], 51, bitwidth);
278 }
279 
check_0d(T v)280 template <typename T> bool check_0d(T v) { return compare<T>(v, 43); }
check_1d(const T (& v)[2])281 template <typename T> bool check_1d(const T (&v)[2]) {
282     return compare<T>(v[0], 44) && compare<T>(v[1], 45);
283 }
check_2d(const T (& v)[3][2])284 template <typename T> bool check_2d(const T (&v)[3][2]) {
285     return compare<T>(v[0][1], 46) && compare<T>(v[1][1], 47) && compare<T>(v[2][1], 48);
286 }
check_3d(const T (& v)[4][3][2])287 template <typename T> bool check_3d(const T (&v)[4][3][2]) {
288     return compare<T>(v[0][0][0], 49) && compare<T>(v[1][0][0], 50) && compare<T>(v[2][0][0], 51)
289            && compare<T>(v[3][0][0], 52);
290 }
291 
check_0d(const T (& v)[N],unsigned int bitwidth)292 template <typename T, size_t N> bool check_0d(const T (&v)[N], unsigned int bitwidth) {
293     return compare(v, 43, bitwidth);
294 }
check_1d(const T (& v)[2][N],unsigned int bitwidth)295 template <typename T, size_t N> bool check_1d(const T (&v)[2][N], unsigned int bitwidth) {
296     return compare(v[0], 44, bitwidth) && compare(v[1], 45, bitwidth);
297 }
check_2d(const T (& v)[3][2][N],unsigned int bitwidth)298 template <typename T, size_t N> bool check_2d(const T (&v)[3][2][N], unsigned int bitwidth) {
299     return compare(v[0][1], 46, bitwidth) && compare(v[1][1], 47, bitwidth)
300            && compare(v[2][1], 48, bitwidth);
301 }
check_3d(const T (& v)[4][3][2][N],unsigned int bitwidth)302 template <typename T, size_t N> bool check_3d(const T (&v)[4][3][2][N], unsigned int bitwidth) {
303     return compare(v[0][0][0], 49, bitwidth) && compare(v[1][0][0], 50, bitwidth)
304            && compare(v[2][0][0], 51, bitwidth) && compare(v[3][0][0], 52, bitwidth);
305 }
306 
307 }  // unnamed namespace
308 
get_non_null()309 void* get_non_null() {
310     static int v;
311     return &v;
312 }
313 
i_byte_0d(char * v)314 void i_byte_0d(char* v) {
315     if (!update_0d(v)) stop();
316 }
i_byte_1d(char * v)317 void i_byte_1d(char* v) {
318     if (!update_1d(v)) stop();
319 }
i_byte_2d(char * v)320 void i_byte_2d(char* v) {
321     if (!update_2d(v)) stop();
322 }
i_byte_3d(char * v)323 void i_byte_3d(char* v) {
324     if (!update_3d(v)) stop();
325 }
326 
i_byte_unsigned_0d(unsigned char * v)327 void i_byte_unsigned_0d(unsigned char* v) {
328     if (!update_0d(v)) stop();
329 }
i_byte_unsigned_1d(unsigned char * v)330 void i_byte_unsigned_1d(unsigned char* v) {
331     if (!update_1d(v)) stop();
332 }
i_byte_unsigned_2d(unsigned char * v)333 void i_byte_unsigned_2d(unsigned char* v) {
334     if (!update_2d(v)) stop();
335 }
i_byte_unsigned_3d(unsigned char * v)336 void i_byte_unsigned_3d(unsigned char* v) {
337     if (!update_3d(v)) stop();
338 }
339 
i_shortint_0d(short * v)340 void i_shortint_0d(short* v) {
341     if (!update_0d(v)) stop();
342 }
i_shortint_1d(short * v)343 void i_shortint_1d(short* v) {
344     if (!update_1d(v)) stop();
345 }
i_shortint_2d(short * v)346 void i_shortint_2d(short* v) {
347     if (!update_2d(v)) stop();
348 }
i_shortint_3d(short * v)349 void i_shortint_3d(short* v) {
350     if (!update_3d(v)) stop();
351 }
352 
i_shortint_unsigned_0d(unsigned short * v)353 void i_shortint_unsigned_0d(unsigned short* v) {
354     if (!update_0d(v)) stop();
355 }
i_shortint_unsigned_1d(unsigned short * v)356 void i_shortint_unsigned_1d(unsigned short* v) {
357     if (!update_1d(v)) stop();
358 }
i_shortint_unsigned_2d(unsigned short * v)359 void i_shortint_unsigned_2d(unsigned short* v) {
360     if (!update_2d(v)) stop();
361 }
i_shortint_unsigned_3d(unsigned short * v)362 void i_shortint_unsigned_3d(unsigned short* v) {
363     if (!update_3d(v)) stop();
364 }
365 
i_int_0d(int * v)366 void i_int_0d(int* v) {
367     if (!update_0d(v)) stop();
368 }
i_int_1d(int * v)369 void i_int_1d(int* v) {
370     if (!update_1d(v)) stop();
371 }
i_int_2d(int * v)372 void i_int_2d(int* v) {
373     if (!update_2d(v)) stop();
374 }
i_int_3d(int * v)375 void i_int_3d(int* v) {
376     if (!update_3d(v)) stop();
377 }
378 
i_int_unsigned_0d(unsigned int * v)379 void i_int_unsigned_0d(unsigned int* v) {
380     if (!update_0d(v)) stop();
381 }
i_int_unsigned_1d(unsigned int * v)382 void i_int_unsigned_1d(unsigned int* v) {
383     if (!update_1d(v)) stop();
384 }
i_int_unsigned_2d(unsigned int * v)385 void i_int_unsigned_2d(unsigned int* v) {
386     if (!update_2d(v)) stop();
387 }
i_int_unsigned_3d(unsigned int * v)388 void i_int_unsigned_3d(unsigned int* v) {
389     if (!update_3d(v)) stop();
390 }
391 
i_longint_0d(sv_longint_t * v)392 void i_longint_0d(sv_longint_t* v) {
393     if (!update_0d(v)) stop();
394 }
i_longint_1d(sv_longint_t * v)395 void i_longint_1d(sv_longint_t* v) {
396     if (!update_1d(v)) stop();
397 }
i_longint_2d(sv_longint_t * v)398 void i_longint_2d(sv_longint_t* v) {
399     if (!update_2d(v)) stop();
400 }
i_longint_3d(sv_longint_t * v)401 void i_longint_3d(sv_longint_t* v) {
402     if (!update_3d(v)) stop();
403 }
404 
i_longint_unsigned_0d(sv_longint_unsigned_t * v)405 void i_longint_unsigned_0d(sv_longint_unsigned_t* v) {
406     if (!update_0d(v)) stop();
407 }
i_longint_unsigned_1d(sv_longint_unsigned_t * v)408 void i_longint_unsigned_1d(sv_longint_unsigned_t* v) {
409     if (!update_1d(v)) stop();
410 }
i_longint_unsigned_2d(sv_longint_unsigned_t * v)411 void i_longint_unsigned_2d(sv_longint_unsigned_t* v) {
412     if (!update_2d(v)) stop();
413 }
i_longint_unsigned_3d(sv_longint_unsigned_t * v)414 void i_longint_unsigned_3d(sv_longint_unsigned_t* v) {
415     if (!update_3d(v)) stop();
416 }
417 
418 #ifndef NO_TIME
i_time_0d(svLogicVecVal * v)419 void i_time_0d(svLogicVecVal* v) {
420     if (!update_0d(v, 64)) stop();
421 }
i_time_1d(svLogicVecVal * v)422 void i_time_1d(svLogicVecVal* v) {
423     if (!update_1d(v, 64)) stop();
424 }
i_time_2d(svLogicVecVal * v)425 void i_time_2d(svLogicVecVal* v) {
426     if (!update_2d(v, 64)) stop();
427 }
i_time_3d(svLogicVecVal * v)428 void i_time_3d(svLogicVecVal* v) {
429     if (!update_3d(v, 64)) stop();
430 }
431 #endif
432 
433 #ifndef NO_INTEGER
i_integer_0d(svLogicVecVal * v)434 void i_integer_0d(svLogicVecVal* v) {
435     if (!update_0d(v, 32)) stop();
436 }
i_integer_1d(svLogicVecVal * v)437 void i_integer_1d(svLogicVecVal* v) {
438     if (!update_1d(v, 32)) stop();
439 }
i_integer_2d(svLogicVecVal * v)440 void i_integer_2d(svLogicVecVal* v) {
441     if (!update_2d(v, 32)) stop();
442 }
i_integer_3d(svLogicVecVal * v)443 void i_integer_3d(svLogicVecVal* v) {
444     if (!update_3d(v, 32)) stop();
445 }
446 #endif
447 
i_real_0d(double * v)448 void i_real_0d(double* v) { update_0d(v); }
i_real_1d(double * v)449 void i_real_1d(double* v) { update_1d(v); }
i_real_2d(double * v)450 void i_real_2d(double* v) { update_2d(v); }
i_real_3d(double * v)451 void i_real_3d(double* v) { update_3d(v); }
452 
453 #ifndef NO_SHORTREAL
i_shortreal_0d(float * v)454 void i_shortreal_0d(float* v) { update_0d(v); }
i_shortreal_1d(float * v)455 void i_shortreal_1d(float* v) { update_1d(v); }
i_shortreal_2d(float * v)456 void i_shortreal_2d(float* v) { update_2d(v); }
i_shortreal_3d(float * v)457 void i_shortreal_3d(float* v) { update_3d(v); }
458 #endif
459 
i_chandle_0d(void ** v)460 void i_chandle_0d(void** v) {
461     if (v[0]) stop();
462     v[0] = get_non_null();
463 }
i_chandle_1d(void ** v)464 void i_chandle_1d(void** v) {
465     if (v[0]) stop();
466     if (v[1]) stop();
467     v[0] = get_non_null();
468     v[1] = get_non_null();
469 }
i_chandle_2d(void ** v)470 void i_chandle_2d(void** v) {
471     if (v[2 * 0 + 1]) stop();
472     if (v[2 * 1 + 1]) stop();
473     if (v[2 * 2 + 1]) stop();
474     v[2 * 0 + 1] = get_non_null();
475     v[2 * 1 + 1] = get_non_null();
476     v[2 * 2 + 1] = get_non_null();
477 }
i_chandle_3d(void ** v)478 void i_chandle_3d(void** v) {
479     if (v[(0 * 3 + 0) * 2 + 0]) stop();
480     if (v[(1 * 3 + 0) * 2 + 0]) stop();
481     if (v[(2 * 3 + 0) * 2 + 0]) stop();
482     if (v[(3 * 3 + 0) * 2 + 0]) stop();
483     v[(0 * 3 + 0) * 2 + 0] = get_non_null();
484     v[(1 * 3 + 0) * 2 + 0] = get_non_null();
485     v[(2 * 3 + 0) * 2 + 0] = get_non_null();
486     v[(3 * 3 + 0) * 2 + 0] = get_non_null();
487 }
488 
i_string_0d(const char ** v)489 void i_string_0d(const char** v) {
490     static const char s[] = "43";
491     if (!compare<std::string>(v[0], "42")) stop();
492     v[0] = s;
493 }
i_string_1d(const char ** v)494 void i_string_1d(const char** v) {
495     static const char s0[] = "44";
496     static const char s1[] = "45";
497     if (!compare<std::string>(v[0], "43")) stop();
498     if (!compare<std::string>(v[1], "44")) stop();
499     v[0] = s0;
500     v[1] = s1;
501 }
i_string_2d(const char ** v)502 void i_string_2d(const char** v) {
503     static const char s0[] = "46";
504     static const char s1[] = "47";
505     static const char s2[] = "48";
506     if (!compare<std::string>(v[2 * 0 + 1], "45")) stop();
507     if (!compare<std::string>(v[2 * 1 + 1], "46")) stop();
508     if (!compare<std::string>(v[2 * 2 + 1], "47")) stop();
509     v[2 * 0 + 1] = s0;
510     v[2 * 1 + 1] = s1;
511     v[2 * 2 + 1] = s2;
512 }
i_string_3d(const char ** v)513 void i_string_3d(const char** v) {
514     static const char s0[] = "49";
515     static const char s1[] = "50";
516     static const char s2[] = "51";
517     static const char s3[] = "52";
518     if (!compare<std::string>(v[(0 * 3 + 0) * 2 + 0], "48")) stop();
519     if (!compare<std::string>(v[(1 * 3 + 0) * 2 + 0], "49")) stop();
520     if (!compare<std::string>(v[(2 * 3 + 0) * 2 + 0], "50")) stop();
521     if (!compare<std::string>(v[(3 * 3 + 0) * 2 + 0], "51")) stop();
522     v[(0 * 3 + 0) * 2 + 0] = s0;
523     v[(1 * 3 + 0) * 2 + 0] = s1;
524     v[(2 * 3 + 0) * 2 + 0] = s2;
525     v[(3 * 3 + 0) * 2 + 0] = s3;
526 }
527 
i_bit1_0d(svBit * v)528 void i_bit1_0d(svBit* v) { update_0d_scalar(v); }
i_bit1_1d(svBit * v)529 void i_bit1_1d(svBit* v) { update_1d_scalar(v); }
i_bit1_2d(svBit * v)530 void i_bit1_2d(svBit* v) { update_2d_scalar(v); }
i_bit1_3d(svBit * v)531 void i_bit1_3d(svBit* v) { update_3d_scalar(v); }
532 
i_bit7_0d(svBitVecVal * v)533 void i_bit7_0d(svBitVecVal* v) { update_0d(v, 7); }
i_bit7_1d(svBitVecVal * v)534 void i_bit7_1d(svBitVecVal* v) { update_1d(v, 7); }
i_bit7_2d(svBitVecVal * v)535 void i_bit7_2d(svBitVecVal* v) { update_2d(v, 7); }
i_bit7_3d(svBitVecVal * v)536 void i_bit7_3d(svBitVecVal* v) { update_3d(v, 7); }
537 
i_bit121_0d(svBitVecVal * v)538 void i_bit121_0d(svBitVecVal* v) { update_0d(v, 121); }
i_bit121_1d(svBitVecVal * v)539 void i_bit121_1d(svBitVecVal* v) { update_1d(v, 121); }
i_bit121_2d(svBitVecVal * v)540 void i_bit121_2d(svBitVecVal* v) { update_2d(v, 121); }
i_bit121_3d(svBitVecVal * v)541 void i_bit121_3d(svBitVecVal* v) { update_3d(v, 121); }
542 
i_logic1_0d(svLogic * v)543 void i_logic1_0d(svLogic* v) { update_0d_scalar(v); }
i_logic1_1d(svLogic * v)544 void i_logic1_1d(svLogic* v) { update_1d_scalar(v); }
i_logic1_2d(svLogic * v)545 void i_logic1_2d(svLogic* v) { update_2d_scalar(v); }
i_logic1_3d(svLogic * v)546 void i_logic1_3d(svLogic* v) { update_3d_scalar(v); }
547 
i_logic7_0d(svLogicVecVal * v)548 void i_logic7_0d(svLogicVecVal* v) { update_0d(v, 7); }
i_logic7_1d(svLogicVecVal * v)549 void i_logic7_1d(svLogicVecVal* v) { update_1d(v, 7); }
i_logic7_2d(svLogicVecVal * v)550 void i_logic7_2d(svLogicVecVal* v) { update_2d(v, 7); }
i_logic7_3d(svLogicVecVal * v)551 void i_logic7_3d(svLogicVecVal* v) { update_3d(v, 7); }
552 
i_logic121_0d(svLogicVecVal * v)553 void i_logic121_0d(svLogicVecVal* v) { update_0d(v, 121); }
i_logic121_1d(svLogicVecVal * v)554 void i_logic121_1d(svLogicVecVal* v) { update_1d(v, 121); }
i_logic121_2d(svLogicVecVal * v)555 void i_logic121_2d(svLogicVecVal* v) { update_2d(v, 121); }
i_logic121_3d(svLogicVecVal * v)556 void i_logic121_3d(svLogicVecVal* v) { update_3d(v, 121); }
557 
i_pack_struct_0d(svLogicVecVal * v)558 void i_pack_struct_0d(svLogicVecVal* v) { update_0d(v, 7); }
i_pack_struct_1d(svLogicVecVal * v)559 void i_pack_struct_1d(svLogicVecVal* v) { update_1d(v, 7); }
i_pack_struct_2d(svLogicVecVal * v)560 void i_pack_struct_2d(svLogicVecVal* v) { update_2d(v, 7); }
i_pack_struct_3d(svLogicVecVal * v)561 void i_pack_struct_3d(svLogicVecVal* v) { update_3d(v, 7); }
562 
563 #ifndef NO_UNPACK_STRUCT
i_unpack_struct_0d(unpack_struct_t * v)564 void i_unpack_struct_0d(unpack_struct_t* v) {
565     if (!compare(v->val, 42, 121)) stop();
566     set_uint(v->val, 43, 121);
567 }
i_unpack_struct_1d(unpack_struct_t * v)568 void i_unpack_struct_1d(unpack_struct_t* v) {
569     if (!compare(v[0].val, 43, 121)) stop();
570     if (!compare(v[1].val, 44, 121)) stop();
571     set_uint(v[0].val, 44, 121);
572     set_uint(v[1].val, 45, 121);
573 }
i_unpack_struct_2d(unpack_struct_t * v)574 void i_unpack_struct_2d(unpack_struct_t* v) {
575     if (!compare(v[0 * 2 + 1].val, 45, 121)) stop();
576     if (!compare(v[1 * 2 + 1].val, 46, 121)) stop();
577     if (!compare(v[2 * 2 + 1].val, 47, 121)) stop();
578     set_uint(v[0 * 2 + 1].val, 46, 121);
579     set_uint(v[1 * 2 + 1].val, 47, 121);
580     set_uint(v[2 * 2 + 1].val, 48, 121);
581 }
i_unpack_struct_3d(unpack_struct_t * v)582 void i_unpack_struct_3d(unpack_struct_t* v) {
583     if (!compare(v[(0 * 3 + 0) * 2 + 0].val, 48, 121)) stop();
584     if (!compare(v[(1 * 3 + 0) * 2 + 0].val, 49, 121)) stop();
585     if (!compare(v[(2 * 3 + 0) * 2 + 0].val, 50, 121)) stop();
586     if (!compare(v[(3 * 3 + 0) * 2 + 0].val, 51, 121)) stop();
587     set_uint(v[(0 * 3 + 0) * 2 + 0].val, 49, 121);
588     set_uint(v[(1 * 3 + 0) * 2 + 0].val, 50, 121);
589     set_uint(v[(2 * 3 + 0) * 2 + 0].val, 51, 121);
590     set_uint(v[(3 * 3 + 0) * 2 + 0].val, 52, 121);
591 }
592 #endif
593 
check_exports()594 void check_exports() {
595     {
596         char byte_array[4][3][2];
597         set_values(byte_array);
598         e_byte_0d(&byte_array[3][2][1]);
599         if (!check_0d(byte_array[3][2][1])) stop();
600         e_byte_1d(&byte_array[2][1][0]);
601         if (!check_1d(byte_array[2][1])) stop();
602         e_byte_2d(&byte_array[1][0][0]);
603         if (!check_2d(byte_array[1])) stop();
604         e_byte_3d(&byte_array[0][0][0]);
605         if (!check_3d(byte_array)) stop();
606     }
607     {
608         unsigned char byte_unsigned_array[4][3][2];
609         set_values(byte_unsigned_array);
610         e_byte_unsigned_0d(&byte_unsigned_array[3][2][1]);
611         if (!check_0d(byte_unsigned_array[3][2][1])) stop();
612         e_byte_unsigned_1d(&byte_unsigned_array[2][1][0]);
613         if (!check_1d(byte_unsigned_array[2][1])) stop();
614         e_byte_unsigned_2d(&byte_unsigned_array[1][0][0]);
615         if (!check_2d(byte_unsigned_array[1])) stop();
616         e_byte_unsigned_3d(&byte_unsigned_array[0][0][0]);
617         if (!check_3d(byte_unsigned_array)) stop();
618     }
619     {
620         short shortint_array[4][3][2];
621         set_values(shortint_array);
622         e_shortint_0d(&shortint_array[3][2][1]);
623         if (!check_0d(shortint_array[3][2][1])) stop();
624         e_shortint_1d(&shortint_array[2][1][0]);
625         if (!check_1d(shortint_array[2][1])) stop();
626         e_shortint_2d(&shortint_array[1][0][0]);
627         if (!check_2d(shortint_array[1])) stop();
628         e_shortint_3d(&shortint_array[0][0][0]);
629         if (!check_3d(shortint_array)) stop();
630     }
631     {
632         unsigned short shortint_unsigned_array[4][3][2];
633         set_values(shortint_unsigned_array);
634         e_shortint_unsigned_0d(&shortint_unsigned_array[3][2][1]);
635         if (!check_0d(shortint_unsigned_array[3][2][1])) stop();
636         e_shortint_unsigned_1d(&shortint_unsigned_array[2][1][0]);
637         if (!check_1d(shortint_unsigned_array[2][1])) stop();
638         e_shortint_unsigned_2d(&shortint_unsigned_array[1][0][0]);
639         if (!check_2d(shortint_unsigned_array[1])) stop();
640         e_shortint_unsigned_3d(&shortint_unsigned_array[0][0][0]);
641         if (!check_3d(shortint_unsigned_array)) stop();
642     }
643 
644     {
645         int int_array[4][3][2];
646         set_values(int_array);
647         e_int_0d(&int_array[3][2][1]);
648         if (!check_0d(int_array[3][2][1])) stop();
649         e_int_1d(&int_array[2][1][0]);
650         if (!check_1d(int_array[2][1])) stop();
651         e_int_2d(&int_array[1][0][0]);
652         if (!check_2d(int_array[1])) stop();
653         e_int_3d(&int_array[0][0][0]);
654         if (!check_3d(int_array)) stop();
655     }
656     {
657         unsigned int int_unsigned_array[4][3][2];
658         set_values(int_unsigned_array);
659         e_int_unsigned_0d(&int_unsigned_array[3][2][1]);
660         if (!check_0d(int_unsigned_array[3][2][1])) stop();
661         e_int_unsigned_1d(&int_unsigned_array[2][1][0]);
662         if (!check_1d(int_unsigned_array[2][1])) stop();
663         e_int_unsigned_2d(&int_unsigned_array[1][0][0]);
664         if (!check_2d(int_unsigned_array[1])) stop();
665         e_int_unsigned_3d(&int_unsigned_array[0][0][0]);
666         if (!check_3d(int_unsigned_array)) stop();
667     }
668 
669     {
670         sv_longint_t longint_array[4][3][2];
671         set_values(longint_array);
672         e_longint_0d(&longint_array[3][2][1]);
673         if (!check_0d(longint_array[3][2][1])) stop();
674         e_longint_1d(&longint_array[2][1][0]);
675         if (!check_1d(longint_array[2][1])) stop();
676         e_longint_2d(&longint_array[1][0][0]);
677         if (!check_2d(longint_array[1])) stop();
678         e_longint_3d(&longint_array[0][0][0]);
679         if (!check_3d(longint_array)) stop();
680     }
681     {
682         sv_longint_unsigned_t longint_unsigned_array[4][3][2];
683         set_values(longint_unsigned_array);
684         e_longint_unsigned_0d(&longint_unsigned_array[3][2][1]);
685         if (!check_0d(longint_unsigned_array[3][2][1])) stop();
686         e_longint_unsigned_1d(&longint_unsigned_array[2][1][0]);
687         if (!check_1d(longint_unsigned_array[2][1])) stop();
688         e_longint_unsigned_2d(&longint_unsigned_array[1][0][0]);
689         if (!check_2d(longint_unsigned_array[1])) stop();
690         e_longint_unsigned_3d(&longint_unsigned_array[0][0][0]);
691         if (!check_3d(longint_unsigned_array)) stop();
692     }
693 
694 #ifndef NO_TIME
695     {
696         svLogicVecVal time_array[4][3][2][2];
697         set_values(time_array, 64);
698         e_time_0d(time_array[3][2][1]);
699         if (!check_0d(time_array[3][2][1], 64)) stop();
700         e_time_1d(time_array[2][1][0]);
701         if (!check_1d(time_array[2][1], 64)) stop();
702         e_time_2d(time_array[1][0][0]);
703         if (!check_2d(time_array[1], 64)) stop();
704         e_time_3d(time_array[0][0][0]);
705         if (!check_3d(time_array, 64)) stop();
706     }
707 #endif
708 
709 #ifndef NO_INTEGER
710     {
711         svLogicVecVal integer_array[4][3][2][1];
712         set_values(integer_array, 32);
713         e_integer_0d(integer_array[3][2][1]);
714         if (!check_0d(integer_array[3][2][1], 32)) stop();
715         e_integer_1d(integer_array[2][1][0]);
716         if (!check_1d(integer_array[2][1], 32)) stop();
717         e_integer_2d(integer_array[1][0][0]);
718         if (!check_2d(integer_array[1], 32)) stop();
719         e_integer_3d(integer_array[0][0][0]);
720         if (!check_3d(integer_array, 32)) stop();
721     }
722 #endif
723 
724     {
725         double real_array[4][3][2];
726         set_values(real_array);
727         e_real_0d(&real_array[3][2][1]);
728         if (!check_0d(real_array[3][2][1])) stop();
729         e_real_1d(&real_array[2][1][0]);
730         if (!check_1d(real_array[2][1])) stop();
731         e_real_2d(&real_array[1][0][0]);
732         if (!check_2d(real_array[1])) stop();
733         e_real_3d(&real_array[0][0][0]);
734         if (!check_3d(real_array)) stop();
735     }
736 #ifndef NO_SHORTREAL
737     {
738         float shortreal_array[4][3][2];
739         set_values(shortreal_array);
740         e_shortreal_0d(&shortreal_array[3][2][1]);
741         if (!check_0d(shortreal_array[3][2][1])) stop();
742         e_shortreal_1d(&shortreal_array[2][1][0]);
743         if (!check_1d(shortreal_array[2][1])) stop();
744         e_shortreal_2d(&shortreal_array[1][0][0]);
745         if (!check_2d(shortreal_array[1])) stop();
746         e_shortreal_3d(&shortreal_array[0][0][0]);
747         if (!check_3d(shortreal_array)) stop();
748     }
749 #endif
750 
751     {
752         void* chandle_array[4][3][2];
753         for (int i = 0; i < 4; ++i)
754             for (int j = 0; j < 3; ++j)
755                 for (int k = 0; k < 2; ++k) chandle_array[i][j][k] = NULL;
756         chandle_array[3][2][1] = get_non_null();
757         e_chandle_0d(&chandle_array[3][2][1]);
758         if (chandle_array[3][2][1]) stop();
759 
760         chandle_array[2][1][0] = get_non_null();
761         chandle_array[2][1][1] = get_non_null();
762         e_chandle_1d(&chandle_array[2][1][0]);
763         if (chandle_array[2][1][0]) stop();
764         if (chandle_array[2][1][1]) stop();
765 
766         chandle_array[1][0][1] = get_non_null();
767         chandle_array[1][1][1] = get_non_null();
768         chandle_array[1][2][1] = get_non_null();
769         e_chandle_2d(&chandle_array[1][0][0]);
770         if (chandle_array[1][0][1]) stop();
771         if (chandle_array[1][1][1]) stop();
772         if (chandle_array[1][2][1]) stop();
773 
774         chandle_array[0][0][0] = get_non_null();
775         chandle_array[1][0][0] = get_non_null();
776         chandle_array[2][0][0] = get_non_null();
777         chandle_array[3][0][0] = get_non_null();
778         e_chandle_3d(&chandle_array[0][0][0]);
779         if (chandle_array[0][0][0]) stop();
780         if (chandle_array[1][0][0]) stop();
781         if (chandle_array[2][0][0]) stop();
782         if (chandle_array[3][0][0]) stop();
783     }
784 
785     {
786         const char* string_array[4][3][2];
787         for (int i = 0; i < 4; ++i)
788             for (int j = 0; j < 3; ++j)
789                 for (int k = 0; k < 2; ++k) string_array[i][j][k] = "";
790         string_array[3][2][1] = "42";
791         e_string_0d(&string_array[3][2][1]);
792         if (!compare<std::string>(string_array[3][2][1], "43")) stop();
793 
794         string_array[2][1][0] = "43";
795         string_array[2][1][1] = "44";
796         e_string_1d(&string_array[2][1][0]);
797         if (!compare<std::string>(string_array[2][1][0], "44")) stop();
798         if (!compare<std::string>(string_array[2][1][1], "45")) stop();
799 
800         string_array[1][0][1] = "45";
801         string_array[1][1][1] = "46";
802         string_array[1][2][1] = "47";
803         e_string_2d(&string_array[1][0][0]);
804         if (!compare<std::string>(string_array[1][0][1], "46")) stop();
805         if (!compare<std::string>(string_array[1][1][1], "47")) stop();
806         if (!compare<std::string>(string_array[1][2][1], "48")) stop();
807 
808         string_array[0][0][0] = "48";
809         string_array[1][0][0] = "49";
810         string_array[2][0][0] = "50";
811         string_array[3][0][0] = "51";
812         e_string_3d(&string_array[0][0][0]);
813         if (!compare<std::string>(string_array[0][0][0], "49")) stop();
814         if (!compare<std::string>(string_array[1][0][0], "50")) stop();
815         if (!compare<std::string>(string_array[2][0][0], "51")) stop();
816         if (!compare<std::string>(string_array[3][0][0], "52")) stop();
817     }
818 
819     {
820         svBitVecVal bit7_array[4][3][2][1];
821         set_values(bit7_array, 7);
822         e_bit7_0d(bit7_array[3][2][1]);
823         if (!check_0d(bit7_array[3][2][1], 7)) stop();
824         e_bit7_1d(bit7_array[2][1][0]);
825         if (!check_1d(bit7_array[2][1], 7)) stop();
826         e_bit7_2d(bit7_array[1][0][0]);
827         if (!check_2d(bit7_array[1], 7)) stop();
828         e_bit7_3d(bit7_array[0][0][0]);
829         if (!check_3d(bit7_array, 7)) stop();
830     }
831     {
832         svBitVecVal bit121_array[4][3][2][4];
833         set_values(bit121_array, 121);
834         e_bit121_0d(bit121_array[3][2][1]);
835         if (!check_0d(bit121_array[3][2][1], 121)) stop();
836         e_bit121_1d(bit121_array[2][1][0]);
837         if (!check_1d(bit121_array[2][1], 121)) stop();
838         e_bit121_2d(bit121_array[1][0][0]);
839         if (!check_2d(bit121_array[1], 121)) stop();
840         e_bit121_3d(bit121_array[0][0][0]);
841         if (!check_3d(bit121_array, 121)) stop();
842     }
843 
844     {
845         svLogicVecVal logic7_array[4][3][2][1];
846         set_values(logic7_array, 7);
847         e_logic7_0d(logic7_array[3][2][1]);
848         if (!check_0d(logic7_array[3][2][1], 7)) stop();
849         e_logic7_1d(logic7_array[2][1][0]);
850         if (!check_1d(logic7_array[2][1], 7)) stop();
851         e_logic7_2d(logic7_array[1][0][0]);
852         if (!check_2d(logic7_array[1], 7)) stop();
853         e_logic7_3d(logic7_array[0][0][0]);
854         if (!check_3d(logic7_array, 7)) stop();
855     }
856     {
857         svLogicVecVal logic121_array[4][3][2][4];
858         set_values(logic121_array, 121);
859         e_logic121_0d(logic121_array[3][2][1]);
860         if (!check_0d(logic121_array[3][2][1], 121)) stop();
861         e_logic121_1d(logic121_array[2][1][0]);
862         if (!check_1d(logic121_array[2][1], 121)) stop();
863         e_logic121_2d(logic121_array[1][0][0]);
864         if (!check_2d(logic121_array[1], 121)) stop();
865         e_logic121_3d(logic121_array[0][0][0]);
866         if (!check_3d(logic121_array, 121)) stop();
867     }
868 
869     {
870         svLogicVecVal pack_struct_array[4][3][2][1];
871         set_values(pack_struct_array, 7);
872         e_pack_struct_0d(pack_struct_array[3][2][1]);
873         if (!check_0d(pack_struct_array[3][2][1], 7)) stop();
874         e_pack_struct_1d(pack_struct_array[2][1][0]);
875         if (!check_1d(pack_struct_array[2][1], 7)) stop();
876         e_pack_struct_2d(pack_struct_array[1][0][0]);
877         if (!check_2d(pack_struct_array[1], 7)) stop();
878         e_pack_struct_3d(pack_struct_array[0][0][0]);
879         if (!check_3d(pack_struct_array, 7)) stop();
880     }
881 
882 #ifndef NO_UNPACK_STRUCT
883     {
884         unpack_struct_t unpack_struct_array[4][3][2];
885         set_uint(unpack_struct_array[3][2][1].val, 42, 121);
886         e_unpack_struct_0d(&unpack_struct_array[3][2][1]);
887         if (!compare(unpack_struct_array[3][2][1].val, 43, 121)) stop();
888 
889         set_uint(unpack_struct_array[2][1][0].val, 43, 121);
890         set_uint(unpack_struct_array[2][1][1].val, 44, 121);
891         e_unpack_struct_1d(&unpack_struct_array[2][1][0]);
892         if (!compare(unpack_struct_array[2][1][0].val, 44, 121)) stop();
893         if (!compare(unpack_struct_array[2][1][1].val, 45, 121)) stop();
894 
895         set_uint(unpack_struct_array[1][0][1].val, 45, 121);
896         set_uint(unpack_struct_array[1][1][1].val, 46, 121);
897         set_uint(unpack_struct_array[1][2][1].val, 47, 121);
898         e_unpack_struct_2d(&unpack_struct_array[1][0][0]);
899         if (!compare(unpack_struct_array[1][0][1].val, 46, 121)) stop();
900         if (!compare(unpack_struct_array[1][1][1].val, 47, 121)) stop();
901         if (!compare(unpack_struct_array[1][2][1].val, 48, 121)) stop();
902 
903         set_uint(unpack_struct_array[0][0][0].val, 48, 121);
904         set_uint(unpack_struct_array[1][0][0].val, 49, 121);
905         set_uint(unpack_struct_array[2][0][0].val, 50, 121);
906         set_uint(unpack_struct_array[3][0][0].val, 51, 121);
907         e_unpack_struct_3d(&unpack_struct_array[0][0][0]);
908         if (!compare(unpack_struct_array[0][0][0].val, 49, 121)) stop();
909         if (!compare(unpack_struct_array[1][0][0].val, 50, 121)) stop();
910         if (!compare(unpack_struct_array[2][0][0].val, 51, 121)) stop();
911         if (!compare(unpack_struct_array[3][0][0].val, 52, 121)) stop();
912     }
913 #endif
914 }
915