File: t_vpi_multidim.cpp

package info (click to toggle)
verilator 5.038-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 162,552 kB
  • sloc: cpp: 139,204; python: 20,931; ansic: 10,222; yacc: 6,000; lex: 1,925; makefile: 1,260; sh: 494; perl: 282; fortran: 22
file content (458 lines) | stat: -rw-r--r-- 16,293 bytes parent folder | download
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// -*- mode: C++; c-file-style: "cc-mode" -*-
//*************************************************************************
//
// Copyright 2024 by Wilson Snyder. This program is free software; you can
// redistribute it and/or modify it under the terms of either the GNU
// Lesser General Public License Version 3 or the Perl Artistic License
// Version 2.0.
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
//
//*************************************************************************

#ifdef IS_VPI

#include "vpi_user.h"

#include <cstdlib>

#else

#include "verilated.h"
#include "verilated_vcd_c.h"
#include "verilated_vpi.h"

#include "Vt_vpi_multidim.h"
#include "Vt_vpi_multidim__Dpi.h"
#include "svdpi.h"

#endif

#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <random>

// These require the above. Comment prevents clang-format moving them
#include "TestCheck.h"
#include "TestSimulator.h"
#include "TestVpi.h"

int errors = 0;

// TEST START

void _arr_type_check(TestVpiHandle& arr_h, int expType, int expSize, int expRangeHigh,
                     int expRangeLow) {
    const int vpitype = vpi_get(vpiType, arr_h);
    TEST_CHECK_EQ(vpitype, expType);
    const int vpisize = vpi_get(vpiSize, arr_h);
    TEST_CHECK_EQ(vpisize, expSize);

    s_vpi_value value;
    value.format = vpiIntVal;

    TestVpiHandle left_h = vpi_handle(vpiLeftRange, arr_h);
    TEST_CHECK_NZ(left_h);
    vpi_get_value(left_h, &value);
    TEST_CHECK_EQ(value.value.integer, expRangeHigh);

    TestVpiHandle right_h = vpi_handle(vpiRightRange, arr_h);
    TEST_CHECK_NZ(right_h);
    vpi_get_value(right_h, &value);
    TEST_CHECK_EQ(value.value.integer, expRangeLow);
}

void _arr_iter_check(const char* name, int wordSize, const int* lows) {
    TestVpiHandle arr_h
        = vpi_handle_by_name(const_cast<PLI_BYTE8*>(TestSimulator::rooted(name)), NULL);
    TEST_CHECK_NZ(arr_h);

    _arr_type_check(arr_h, vpiRegArray, 4, lows[0] + 1, lows[0]);

    {
        // can't iterate through RegArrays on a nested RegArray
        TestVpiHandle arr_iter_h = vpi_iterate(vpiRegArray, arr_h);
        TEST_CHECK_Z(vpi_scan(arr_iter_h));
        arr_iter_h.freed();
    }

    if (!TestSimulator::is_questa()) {
        // but we can access them by index (Questa can't)
        for (int idx = lows[0]; idx < lows[0] + 2; idx++) {
            TestVpiHandle arr_elem_h = vpi_handle_by_index(arr_h, idx);
            TEST_CHECK_NZ(arr_elem_h);

            // first indexing yields size-2 RegArrays
            _arr_type_check(arr_elem_h, vpiRegArray, 2, lows[1] + 1, lows[1]);

            for (int idx2 = lows[1]; idx2 < lows[1] + 2; idx2++) {
                TestVpiHandle arr_elem2_h = vpi_handle_by_index(arr_elem_h, idx2);
                TEST_CHECK_NZ(arr_elem2_h);

                // second indexing yields wordSize Regs
                _arr_type_check(arr_elem2_h, vpiReg, wordSize, lows[2] + 1, lows[2]);
            }
        }
    }

    {
        // it's also possible to directly iterate through all four Regs
        TestVpiHandle arr_iter_h = vpi_iterate(vpiReg, arr_h);
        for (int idx = 0; idx < 4; idx++) {
            TestVpiHandle arr_elem_h = vpi_scan(arr_iter_h);
            TEST_CHECK_NZ(arr_elem_h);

            // which gives us wordSize Regs
            _arr_type_check(arr_elem_h, vpiReg, wordSize, lows[2] + 1, lows[2]);

            {
                // can't iterate through Regs on a nested Reg
                TestVpiHandle arr_iter2_h = vpi_iterate(vpiReg, arr_elem_h);
                TEST_CHECK_Z(vpi_scan(arr_iter2_h));
                arr_iter2_h.freed();
            }

            // but we can access them by index
            for (int idx2 = lows[2]; idx2 < lows[2] + 2; idx2++) {
                TestVpiHandle arr_elem2_h = vpi_handle_by_index(arr_elem_h, idx2);
                TEST_CHECK_NZ(arr_elem2_h);

                // first indexing yields wordSize / 2 Regs
                _arr_type_check(arr_elem2_h, vpiReg, wordSize / 2, lows[3] + wordSize / 2 - 1,
                                lows[3]);

                for (int idx3 = lows[3]; idx3 < lows[3] + wordSize / 2; idx3++) {
                    TestVpiHandle arr_elem3_h = vpi_handle_by_index(arr_elem2_h, idx3);
                    TEST_CHECK_NZ(arr_elem3_h);
                    {
                        // second indexing yields size-1 RegBits (no support for RegBit VPI type
                        // yet)
                        const int vpitype = vpi_get(vpiType, arr_elem3_h);
                        if (TestSimulator::is_verilator()) {
                            TEST_CHECK_EQ(vpitype, vpiReg);
                        } else {
                            TEST_CHECK_EQ(vpitype, vpiRegBit);
                        }
                        const int vpisize = vpi_get(vpiSize, arr_elem3_h);
                        TEST_CHECK_EQ(vpisize, 1);
                    }
                }
            }

            // iterating through packed ranges
            TestVpiHandle range_iter_h = vpi_iterate(vpiRange, arr_elem_h);
            for (int idx2 = 0; idx2 < 2; idx2++) {
                TestVpiHandle range_h = vpi_scan(range_iter_h);
                TEST_CHECK_NZ(range_h);
                {
                    s_vpi_value value;
                    value.format = vpiIntVal;
                    TestVpiHandle side_h = vpi_handle(vpiLeftRange, range_h);
                    TEST_CHECK_NZ(side_h);
                    vpi_get_value(side_h, &value);
                    if (idx2 == 0) {
                        TEST_CHECK_EQ(value.value.integer, lows[2] + 1);
                    } else {
                        TEST_CHECK_EQ(value.value.integer, lows[3] + wordSize / 2 - 1);
                    }
                    side_h = vpi_handle(vpiRightRange, range_h);
                    TEST_CHECK_NZ(side_h);
                    vpi_get_value(side_h, &value);
                    if (idx2 == 0) {
                        TEST_CHECK_EQ(value.value.integer, lows[2]);
                    } else {
                        TEST_CHECK_EQ(value.value.integer, lows[3]);
                    }
                }
            }
            TEST_CHECK_Z(vpi_scan(range_iter_h));
            range_iter_h.freed();
        }
        TEST_CHECK_Z(vpi_scan(arr_iter_h));
        arr_iter_h.freed();
    }

    {
        // iterating through unpacked ranges
        TestVpiHandle range_iter_h = vpi_iterate(vpiRange, arr_h);
        for (int idx = 0; idx < 2; idx++) {
            TestVpiHandle range_h = vpi_scan(range_iter_h);
            TEST_CHECK_NZ(range_h);
            {
                s_vpi_value value;
                value.format = vpiIntVal;
                TestVpiHandle side_h = vpi_handle(vpiLeftRange, range_h);
                TEST_CHECK_NZ(side_h);
                vpi_get_value(side_h, &value);
                if (idx == 0) {
                    TEST_CHECK_EQ(value.value.integer, lows[0] + 1);
                } else {
                    TEST_CHECK_EQ(value.value.integer, lows[1] + 1);
                }
                side_h = vpi_handle(vpiRightRange, range_h);
                TEST_CHECK_NZ(side_h);
                vpi_get_value(side_h, &value);
                if (idx == 0) {
                    TEST_CHECK_EQ(value.value.integer, lows[0]);
                } else {
                    TEST_CHECK_EQ(value.value.integer, lows[1]);
                }
            }
        }
        TEST_CHECK_Z(vpi_scan(range_iter_h));
        range_iter_h.freed();
    }
}

void _arr_access_format_check(TestVpiHandle& reg_h, int wordSize, const int* lows,
                              const char* octVal_s, PLI_INT32 format) {
    constexpr int MAX_SPANSIZE = 1024;
    const int spanSize = wordSize / 2;
    assert(spanSize <= MAX_SPANSIZE);
    s_vpi_value value_in;
    s_vpi_value value_out;
    s_vpi_error_info e;
    char zero_s[2] = "0";

    // zero out the vector
    value_in.format = vpiOctStrVal;
    value_in.value.str = zero_s;
    vpi_put_value(reg_h, &value_in, NULL, vpiNoDelay);
    TEST_CHECK_Z(vpi_chk_error(&e));

    value_in.format = format;
    value_out.format = format;

    for (int i = 0; i < 2; i++) {
        TestVpiHandle subreg_h = vpi_handle_by_index(reg_h, lows[2] + i);
        TEST_CHECK_NZ(subreg_h);

        char octSpan_s[MAX_SPANSIZE / 3 + 1];
        strncpy(octSpan_s, &octVal_s[spanSize / 3 * (1 - i)], spanSize / 3);
        octSpan_s[spanSize / 3] = '\0';

        uint64_t intVal;
        t_vpi_vecval vecVal[2];
        sscanf(octSpan_s, "%" SCNo64, &intVal);
        char strVal_s[MAX_SPANSIZE + 1];  // max length of the string happens for binary

        if (format == vpiIntVal) {
            value_in.value.integer = intVal;
        } else if (format == vpiVectorVal) {
            if (spanSize > 32) {
                vecVal[1].aval = intVal >> 32;
                vecVal[1].bval = 0;
            }
            vecVal[0].aval = intVal;
            vecVal[0].bval = 0;
            value_in.value.vector = vecVal;
        } else if (format == vpiBinStrVal) {
            for (int j = 0; j < spanSize; j++)
                strVal_s[j] = (intVal >> (spanSize - j - 1)) % 2 + '0';
            strVal_s[spanSize] = '\0';
            value_in.value.str = strVal_s;
        } else if (format == vpiDecStrVal) {
            sprintf(strVal_s, "%" PRIu64, intVal);
            value_in.value.str = strVal_s;
        } else if (format == vpiHexStrVal) {
            sprintf(strVal_s, "%0*" PRIx64, (spanSize + 3) / 4, intVal);
            value_in.value.str = strVal_s;
        } else if (format == vpiOctStrVal) {
            sprintf(strVal_s, "%0*" PRIo64, (spanSize + 2) / 3, intVal);
            value_in.value.str = strVal_s;
        } else if (format == vpiStringVal) {
            const int byteCount = (spanSize + 7) / 8;
            for (int j = 0; j < byteCount; j++)
                strVal_s[j] = (intVal >> (8 * (byteCount - j - 1))) & 0xff;
            strVal_s[byteCount] = '\0';
            value_in.value.str = strVal_s;
        }

        vpi_put_value(subreg_h, &value_in, NULL, vpiNoDelay);
        TEST_CHECK_Z(vpi_chk_error(&e));

        vpi_get_value(subreg_h, &value_out);
        switch (format) {
        case vpiIntVal: TEST_CHECK_EQ(value_out.value.integer, value_in.value.integer); break;
        case vpiVectorVal:
            if (spanSize > 32)
                TEST_CHECK_EQ(value_out.value.vector[1].aval, value_in.value.vector[1].aval);
            TEST_CHECK_EQ(value_out.value.vector[0].aval, value_in.value.vector[0].aval);
            break;
        case vpiStringVal:
            TEST_CHECK_EQ(value_out.value.str[0],
                          value_in.value.str[0] ? value_in.value.str[0] : ' ');
            break;
        case vpiBinStrVal:
        case vpiDecStrVal:
        case vpiHexStrVal:
        case vpiOctStrVal: TEST_CHECK_CSTR(value_out.value.str, value_in.value.str); break;
        }
    }

    // validate the resulting flattened vector
    value_out.format = vpiOctStrVal;
    vpi_get_value(reg_h, &value_out);
    TEST_CHECK_CSTR(value_out.value.str, octVal_s);
}

std::default_random_engine rng;

void _arr_access_check(const char* name, int wordSize, const int* lows) {
    TestVpiHandle arr_h
        = vpi_handle_by_name(const_cast<PLI_BYTE8*>(TestSimulator::rooted(name)), NULL);
    TEST_CHECK_NZ(arr_h);

    std::uniform_int_distribution<uint64_t> rand64(std::numeric_limits<uint64_t>::min(),
                                                   std::numeric_limits<uint64_t>::max());

    constexpr int MAX_WORDSIZE = 128;
    assert(wordSize <= MAX_WORDSIZE);
    char octVal_s[MAX_WORDSIZE / 3 + 2];

    octVal_s[0] = '0' + (rand64(rng) % (1ULL << ((((wordSize - 1) % 3) + 1))));
    for (int i = 1; i < (wordSize + 2) / 3; ++i) octVal_s[i] = '0' + (rand64(rng) % 8);
    octVal_s[(wordSize + 2) / 3] = '\0';

    // Assume that reading/writing to the "flattened" packed register is already tested,
    // check only reading/writing to sub-regs and validate the flattened result.
    {
        TestVpiHandle arr_iter_h = vpi_iterate(vpiReg, arr_h);
        while (TestVpiHandle reg_h = vpi_scan(arr_iter_h)) {
            s_vpi_value value_in;
            s_vpi_value value_out;
            s_vpi_error_info e;

            value_out.format = vpiOctStrVal;
            value_in.format = vpiOctStrVal;
            value_in.value.str = octVal_s;
            vpi_put_value(reg_h, &value_in, NULL, vpiNoDelay);
            TEST_CHECK_Z(vpi_chk_error(&e));
            vpi_get_value(reg_h, &value_out);
            TEST_CHECK_CSTR(value_out.value.str, octVal_s);

            // test each I/O data format
            if (wordSize <= 64) {
                _arr_access_format_check(reg_h, wordSize, lows, octVal_s, vpiIntVal);
                _arr_access_format_check(reg_h, wordSize, lows, octVal_s, vpiDecStrVal);
            }
            _arr_access_format_check(reg_h, wordSize, lows, octVal_s, vpiVectorVal);
            _arr_access_format_check(reg_h, wordSize, lows, octVal_s, vpiBinStrVal);
            _arr_access_format_check(reg_h, wordSize, lows, octVal_s, vpiOctStrVal);
            _arr_access_format_check(reg_h, wordSize, lows, octVal_s, vpiHexStrVal);
            _arr_access_format_check(reg_h, wordSize, lows, octVal_s, vpiStringVal);
        }
        arr_iter_h.freed();
    }
}

struct params {
    const char* name;
    int wordSize;
    const int lows[4];
};

void _multidim_check() {
    static struct params values[]
        = {{"arr_cdata", 6, {0, 1, 2, 3}},       {"arr_sdata", 12, {4, 5, 6, 7}},
           {"arr_idata", 30, {8, 9, 10, 11}},    {"arr_qdata", 60, {12, 13, 14, 15}},
           {"arr_wdata", 126, {16, 17, 18, 19}}, {nullptr, 0, {0, 0, 0, 0}}};
    struct params* value = values;
    while (value->name) {
        _arr_iter_check(value->name, value->wordSize, value->lows);
        _arr_access_check(value->name, value->wordSize, value->lows);
        value++;
    }
}

// TEST END

extern "C" int mon_check() {
    // Callback from initial block in monitor
    //if (int status = _mon_check_param()) return status;
    printf("-mon_check()\n");
    _multidim_check();
    return errors;
}

#ifdef IS_VPI

static int mon_check_vpi() {
    TestVpiHandle href = vpi_handle(vpiSysTfCall, 0);
    s_vpi_value vpi_value;

    vpi_value.format = vpiIntVal;
    vpi_value.value.integer = mon_check();
    vpi_put_value(href, &vpi_value, NULL, vpiNoDelay);

    return 0;
}

static s_vpi_systf_data vpi_systf_data[] = {{vpiSysFunc, vpiIntFunc, (PLI_BYTE8*)"$mon_check",
                                             (PLI_INT32(*)(PLI_BYTE8*))mon_check_vpi, 0, 0, 0},
                                            0};

void vpi_compat_bootstrap(void) {
    p_vpi_systf_data systf_data_p;
    systf_data_p = &(vpi_systf_data[0]);
    while (systf_data_p->type != 0) vpi_register_systf(systf_data_p++);
}

void (*vlog_startup_routines[])() = {vpi_compat_bootstrap, 0};

#else

int main(int argc, char** argv) {
    const std::unique_ptr<VerilatedContext> contextp{new VerilatedContext};

    uint64_t sim_time = 1100;  // TODO
    contextp->debug(0);
    contextp->commandArgs(argc, argv);

    const std::unique_ptr<VM_PREFIX> topp{new VM_PREFIX{contextp.get(),
                                                        // Note null name - we're flattening it out
                                                        ""}};

#ifdef VERILATOR
#ifdef TEST_VERBOSE
    contextp->scopesDump();
#endif
#endif

#if VM_TRACE
    contextp->traceEverOn(true);
    VL_PRINTF("Enabling waves...\n");
    VerilatedVcdC* tfp = new VerilatedVcdC;
    topp->trace(tfp, 99);
    tfp->open(STRINGIFY(TEST_OBJ_DIR) "/simx.vcd");
#endif

    topp->eval();
    topp->clk = 0;
    contextp->timeInc(10);

    while (contextp->time() < sim_time && !contextp->gotFinish()) {
        contextp->timeInc(1);
        topp->eval();
        VerilatedVpi::callValueCbs();
        topp->clk = !topp->clk;
        // mon_do();
#if VM_TRACE
        if (tfp) tfp->dump(main_time);
#endif
    }
    if (!contextp->gotFinish()) {
        vl_fatal(__FILE__, __LINE__, "main", "%Error: Timeout; never got a $finish");
    }
    topp->final();

#if VM_TRACE
    if (tfp) tfp->close();
#endif

    return 0;
}

#endif