File: t_dpi_lib_c.cpp

package info (click to toggle)
verilator 4.038-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 29,596 kB
  • sloc: cpp: 90,585; perl: 15,101; ansic: 8,573; yacc: 3,626; lex: 1,616; makefile: 1,101; sh: 175; python: 145
file content (134 lines) | stat: -rw-r--r-- 4,479 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
// -*- mode: C++; c-file-style: "cc-mode" -*-
//*************************************************************************
//
// Copyright 2009-2017 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
//
//*************************************************************************

#include <cstdio>
#include <cstring>
#include <iostream>
#include "svdpi.h"

//======================================================================

#if defined(VERILATOR)
# include "Vt_dpi_lib__Dpi.h"
#elif defined(VCS)
# include "../vc_hdrs.h"
#elif defined(NC)
# define NEED_EXTERNS
#else
# error "Unknown simulator for DPI test"
#endif

#ifdef NEED_EXTERNS
extern "C" {
    // If get ncsim: *F,NOFDPI: Function {foo} not found in default libdpi.
    // Then probably forgot to list a function here.

    extern int dpii_failure();
    extern void dpii_check();
}
#endif

//======================================================================

int failure = 0;
int dpii_failure() { return failure; }

#define CHECK_RESULT_HEX(got, exp) \
    do { \
        if ((got) != (exp)) { \
            std::cout << std::dec << "%Error: " << __FILE__ << ":" << __LINE__ << std::hex \
                      << ": GOT=" << (got) << "   EXP=" << (exp) << std::endl; \
            failure = __LINE__; \
        } \
    } while (0)

//======================================================================

void dpii_lib_bit_check() {
    svBitVecVal bv[3];
    bv[0] = 0xa3a2a1a0;  // 31..0
    bv[1] = 0xa7a6a5a4;  // 63..32
    bv[2] = 0xabaaa9a8;  // 95..64
    CHECK_RESULT_HEX((int)svGetBitselBit(bv, 32), 0);
    CHECK_RESULT_HEX((int)svGetBitselBit(bv, 33), 0);
    CHECK_RESULT_HEX((int)svGetBitselBit(bv, 34), 1);
    CHECK_RESULT_HEX((int)svGetBitselBit(bv, 35), 0);
    CHECK_RESULT_HEX((int)svGetBitselBit(bv, 36), 0);
    CHECK_RESULT_HEX((int)svGetBitselBit(bv, 37), 1);
    CHECK_RESULT_HEX((int)svGetBitselBit(bv, 38), 0);
    CHECK_RESULT_HEX((int)svGetBitselBit(bv, 39), 1);

    svPutBitselBit(bv, 32, 1);
    svPutBitselBit(bv, 33, 0);
    svPutBitselBit(bv, 34, 1);
    svPutBitselBit(bv, 35, 1);
    CHECK_RESULT_HEX(bv[0], 0xa3a2a1a0);
    CHECK_RESULT_HEX(bv[1], 0xa7a6a5ad);
    CHECK_RESULT_HEX(bv[2], 0xabaaa9a8);

    svBitVecVal btmp[1];
    svGetPartselBit(btmp, bv, 40, 8);
    CHECK_RESULT_HEX(btmp[0], 0xa5);

    svPutPartselBit(bv, btmp[0], 48, 8);
    CHECK_RESULT_HEX(bv[0], 0xa3a2a1a0);
    CHECK_RESULT_HEX(bv[1], 0xa7a5a5ad);
    CHECK_RESULT_HEX(bv[2], 0xabaaa9a8);
}

void dpii_lib_logic_check() {
    svLogicVecVal lv[3];
    lv[0].aval = 0xb3b2b1b0;  // 31..0
    lv[1].aval = 0xb7b6b5b4;  // 63..32
    lv[2].aval = 0xbbbab9b8;  // 95..64
    lv[0].bval = 0xc3c2c1c0;  // 31..0
    lv[1].bval = 0xc7c6c5c4;  // 63..32
    lv[2].bval = 0xcbcac9c8;  // 95..64
    CHECK_RESULT_HEX((int)svGetBitselLogic(lv, 32), 0);
    CHECK_RESULT_HEX((int)svGetBitselLogic(lv, 33), 0);
    CHECK_RESULT_HEX((int)svGetBitselLogic(lv, 34), 3);
    CHECK_RESULT_HEX((int)svGetBitselLogic(lv, 35), 0);
    CHECK_RESULT_HEX((int)svGetBitselLogic(lv, 36), 1);
    CHECK_RESULT_HEX((int)svGetBitselLogic(lv, 37), 1);
    CHECK_RESULT_HEX((int)svGetBitselLogic(lv, 38), 2);
    CHECK_RESULT_HEX((int)svGetBitselLogic(lv, 39), 3);

    svPutBitselLogic(lv, 32, 1);
    svPutBitselLogic(lv, 33, 0);
    svPutBitselLogic(lv, 34, 1);
    svPutBitselLogic(lv, 35, 3);
    CHECK_RESULT_HEX(lv[0].aval, 0xb3b2b1b0);
    CHECK_RESULT_HEX(lv[1].aval, 0xb7b6b5bd);
    CHECK_RESULT_HEX(lv[2].aval, 0xbbbab9b8);
    CHECK_RESULT_HEX(lv[0].bval, 0xc3c2c1c0);
    CHECK_RESULT_HEX(lv[1].bval, 0xc7c6c5c8);
    CHECK_RESULT_HEX(lv[2].bval, 0xcbcac9c8);

    svLogicVecVal ltmp[1];
    svGetPartselLogic(ltmp, lv, 40, 8);
    CHECK_RESULT_HEX(ltmp[0].aval, 0xb5);
    CHECK_RESULT_HEX(ltmp[0].bval, 0xc5);

    svPutPartselLogic(lv, ltmp[0], 48, 8);
    CHECK_RESULT_HEX(lv[0].aval, 0xb3b2b1b0);
    CHECK_RESULT_HEX(lv[1].aval, 0xb7b5b5bd);
    CHECK_RESULT_HEX(lv[2].aval, 0xbbbab9b8);
    CHECK_RESULT_HEX(lv[0].bval, 0xc3c2c1c0);
    CHECK_RESULT_HEX(lv[1].bval, 0xc7c5c5c8);
    CHECK_RESULT_HEX(lv[2].bval, 0xcbcac9c8);
}

//======================================================================

void dpii_check() {
    dpii_lib_bit_check();
    dpii_lib_logic_check();
}