OpenCores
URL https://opencores.org/ocsvn/spdif_transmitter/spdif_transmitter/trunk

Subversion Repositories spdif_transmitter

[/] [spdif_transmitter/] [trunk/] [testbench/] [sc_vpi_module.h] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 ultra_embe
#ifndef __SC_VPI_MODULE_H__
2
#define __SC_VPI_MODULE_H__
3
 
4
#include <systemc.h>
5
#include <assert.h>
6
#include <vpi_user.h>
7
 
8
static int sc_vpi_module_value_change(p_cb_data cb_data);
9
 
10
#define sc_vpi_module_read_output_int(obj, name)  \
11
{                                   \
12
    s_vpi_value  value_s;           \
13
    s_vpi_time   vpi_time_s;        \
14
                                    \
15
    value_s.format = vpiIntVal;     \
16
                                    \
17
    vpi_time_s.type = vpiSimTime;   \
18
    vpi_time_s.high = 0;            \
19
    vpi_time_s.low  = 0;            \
20
                                    \
21
    std::string path = m_hdl_name;  \
22
    path = path + "." + name;       \
23
    vpiHandle handle = vpi_handle_by_name(path.c_str(), NULL); \
24
    assert(handle != NULL);           \
25
                                      \
26
    vpi_get_value(handle, &value_s);  \
27
    obj.write(value_s.value.integer); \
28
}
29
 
30
#define sc_vpi_module_write_input_int(obj, name)  \
31
{                                   \
32
    s_vpi_value  value_s;           \
33
    s_vpi_time   vpi_time_s;        \
34
                                    \
35
    value_s.format = vpiIntVal;     \
36
                                    \
37
    vpi_time_s.type = vpiSimTime;   \
38
    vpi_time_s.high = 0;            \
39
    vpi_time_s.low  = 0;            \
40
                                    \
41
    std::string path = m_hdl_name;  \
42
    path = path + "." + name;       \
43
    vpiHandle handle = vpi_handle_by_name(path.c_str(), NULL); \
44
    assert(handle != NULL);           \
45
                                      \
46
    value_s.value.integer = obj.read();  \
47
    vpi_put_value(handle, &value_s, &vpi_time_s, vpiInertialDelay); \
48
}
49
 
50
class sc_vpi_module
51
{
52
public:
53
    std::string      m_hdl_name;
54
    uint64_t         m_last_time;
55
    sc_signal<bool>  m_stop;
56
 
57
    sc_vpi_module(sc_module_name name) : m_hdl_name((std::string)name)
58
    {
59
        m_last_time = 0;
60
        m_stop.write(false);
61
    }
62
 
63
    // Simulation control    
64
    void stopSimulation()  { m_stop.write(true); }
65
    bool isStopped()       { return m_stop.read(); }
66
 
67
    virtual void read_outputs(void) { }
68
    virtual void write_inputs(void) { }
69
 
70
    bool register_signal(const char *name)
71
    {
72
        static s_vpi_value value_s;
73
        static s_vpi_time  vpi_time;
74
        s_cb_data          cb_data_s;
75
 
76
        vpi_time.high = 0;
77
        vpi_time.low  = 0;
78
        vpi_time.type = vpiSimTime;
79
 
80
        // For each I/O
81
        std::string path = m_hdl_name;
82
        path = path + "." + name;
83
        vpiHandle handle = vpi_handle_by_name(path.c_str(), NULL);
84
        if (!handle)
85
            return false;
86
 
87
        // Attach value change callbacks for outputs
88
        cb_data_s.user_data = (PLI_BYTE8*)this;
89
        cb_data_s.reason    = cbValueChange;
90
        cb_data_s.cb_rtn    = sc_vpi_module_value_change;
91
        cb_data_s.time      = &vpi_time;
92
        cb_data_s.value     = &value_s;
93
 
94
        value_s.format      = vpiIntVal;
95
 
96
        cb_data_s.obj  = handle;
97
        vpi_register_cb(&cb_data_s);
98
 
99
        return true;
100
    }
101
 
102
    int value_change(void)
103
    {
104
        s_vpi_time vpi_time_s;
105
 
106
        vpi_time_s.type = vpiSimTime;
107
        vpi_time_s.high = 0;
108
        vpi_time_s.low  = 0;
109
 
110
        // Outputs
111
        read_outputs();
112
 
113
        // Get current time
114
        uint64_t time_value = 0;
115
        s_vpi_time time_now;
116
        time_now.type = vpiSimTime;
117
        vpi_get_time (0, &time_now);
118
 
119
        time_value = time_now.high;
120
        time_value <<= 32;
121
        time_value |= time_now.low;
122
 
123
        // Update systemC TB
124
        if(sc_pending_activity())
125
            sc_start((int)(time_value-m_last_time),SC_NS);
126
 
127
        m_last_time = time_value;
128
 
129
        // Inputs
130
        write_inputs();
131
 
132
        if (isStopped())
133
            vpi_sim_control(vpiFinish, 0);
134
 
135
        return 0;
136
    }
137
};
138
 
139
static int sc_vpi_module_value_change(p_cb_data cb_data)
140
{
141
    sc_vpi_module *p = (sc_vpi_module*)cb_data->user_data;
142
    return p->value_change();
143
}
144
 
145
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.