File: sc_rvd.h

package info (click to toggle)
systemc 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,768 kB
  • sloc: cpp: 181,958; sh: 4,925; asm: 2,700; perl: 1,980; ansic: 1,931; makefile: 1,761; fortran: 492; python: 482; awk: 157; csh: 50
file content (264 lines) | stat: -rw-r--r-- 8,995 bytes parent folder | download | duplicates (5)
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
/*****************************************************************************

  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
  more contributor license agreements.  See the NOTICE file distributed
  with this work for additional information regarding copyright ownership.
  Accellera licenses this file to you under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with the
  License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  implied.  See the License for the specific language governing
  permissions and limitations under the License.

 *****************************************************************************/
#if !defined(sc_rvd_h_included)
#define sc_rvd_h_included


// This header file contains classes implementing a channel, in port, and 
// out port that may be used to transfer data via a "ready-valid" protocol.
// The ready-valid protocol uses two bits to specify the state of the channel,
// a ready assertion for the read side of the channel, and a valid assertion
// for the write side of the channel. The protocol has the following 
// characteristics:
//    (1) If the valid signal is true there is data available in the channel,
//        if the ready signal is true the read side of the channel is ready
//        to read data.
//    (2) Data transfers on clocks where the ready and valid signals are
//        both asserted.
//    (3) When writing data to the channel it is necessary to wait for the
//        ready signal to be asserted before the valid signal can be deasserted.
//    (4) When reading data from the channel it is necessary to wait for
//        valid signal to be asserted before data can be transferred, and 
//        the ready signal deasserted.
//    (5) A call to the reset() method of the read and write ports is 
//        necessary to place the channel in its initial state.
//    (6) There is always a one clock "bubble" on the first data transfer
//        in a sequence. However, multiple values can be transferred, one
//        value per clock after that by having the read and write sides
//        leave their handshake signals asserted. When the transfer is 
//        complete the signals should be deasserted.

// +============================================================================
// | sc_rvd_export_in<D> - EXPORT OF INPUT INTERFACE FOR A COMMUNICATION 
// |                       CHANNEL WITH A TOGGLE-TOGGLE HANDSHAKE
// | 
// | This class provides the interface necessary for binding an input for a
// | data transfer channel that is controlled using a ready-valid handshake. 
// |
// +============================================================================
template<typename D>
class sc_rvd_export_in
{
  public:
    sc_rvd_export_in(const char* name=sc_gen_unique_name("sc_rvd_export_in")) :
        m_data( (std::string(name)+"m_data").c_str() ),
        m_ready( (std::string(name)+"m_ready").c_str() ),
        m_valid( (std::string(name)+"m_valid").c_str() )
    {}

  public:
    sc_export<sc_signal_in_if<D> >     m_data;   // data to transfer.
    sc_export<sc_signal_out_if<bool> > m_ready;  // valid signal.
    sc_export<sc_signal_in_if<bool> >  m_valid;  // ready signal.
};

// +============================================================================
// | sc_rvd_export_out<D> - EXPORT OF INTERFACE SIGNALS FOR A COMMUNICATION 
// |                        CHANNEL WITH A TOGGLE-TOGGLE HANDSHAKE
// | 
// | This class provides the interface necessary for binding an output for a
// | data transfer channel that is controlled using a ready-valid handshake. 
// |
// +============================================================================
template<typename D>
class sc_rvd_export_out
{
  public:
    sc_rvd_export_out(const char* name=sc_gen_unique_name("sc_rvd_export_out")):
        m_data( (std::string(name)+"m_data").c_str() ),
        m_ready( (std::string(name)+"m_ready").c_str() ),
        m_valid( (std::string(name)+"m_valid").c_str() )
    {}

  public:
    sc_export<sc_signal_out_if<D> >     m_data;   // data to transfer.
    sc_export<sc_signal_in_if<bool> >   m_ready;  // valid signal.
    sc_export<sc_signal_out_if<bool> >  m_valid;  // ready signal.
};

// +============================================================================
// | sc_rvd_in<D> - PORT TO READ VALUES FROM AN sc_rvd CHANNEL
// | 
// | This class provides the interface to read values from an sc_rvd channel.
// +============================================================================
template<typename D>
class sc_rvd_in
{
  public: // constructor:
    sc_rvd_in( const char* name=sc_gen_unique_name("sc_rvd_in") ) :
        m_data( (std::string(name)+"_data").c_str() ), 
        m_ready( (std::string(name)+"_ready").c_str() ), 
        m_valid( (std::string(name)+"_valid").c_str() ) 
    { }

  public: // channel binding method and operator:
    template<typename CHANNEL>
    inline void bind( CHANNEL& channel )
    {
        m_data(channel.m_data);
        m_ready(channel.m_ready);
        m_valid(channel.m_valid);
    }

    template<typename CHANNEL>
    inline void operator () ( CHANNEL& channel )
    {
        bind(channel);
    }

  public: // channel access methods and operators:
    inline bool nb_can_read()
    {
        return m_valid.read();
    }

    inline bool nb_read( D& data )
    {
	data = m_data;
	if ( m_valid.read() == true )
	{
	    m_ready = true;
	    return true;
	}
	else
	{
	    return false;
	}
    }

    inline void reset()
    {
        m_ready = false;
    }

    inline D read()
    {
        m_ready = true;
	do { ::wait(); } while ( m_valid.read() == false );
	m_ready = false;
	return m_data.read();
    }

    inline operator D ()
    {
	return read();
    }

  protected: // channel connections:
    sc_in<D>     m_data;  // data to be transferred.
    sc_out<bool> m_ready; // true if this port is ready to read a value.
    sc_in<bool>  m_valid; // true if m_data has a value to be read.
};

// +============================================================================
// | sc_rvd_out<D> - PORT TO WRITE VALUES TO AN sc_rvd CHANNEL
// | 
// | This class provides the interface to write values to an sc_rvd channel.
// +============================================================================
template<typename D>
class sc_rvd_out
{
  public: // constructor:
    sc_rvd_out( const char* name=sc_gen_unique_name("sc_rvd_out") ) :
        m_data( (std::string(name)+"_data").c_str() ), 
        m_ready( (std::string(name)+"_ready").c_str() ), 
        m_valid( (std::string(name)+"_valid").c_str() ) 
    { }

  public: // channel binding method and operator:
    template<typename CHANNEL>
    inline void bind( CHANNEL& channel )
    {
        m_data(channel.m_data);
        m_ready(channel.m_ready);
        m_valid(channel.m_valid);
    }
    template<typename CHANNEL>
    inline void operator () ( CHANNEL& channel )
    {
        bind(channel);
    }

  public: // channel access methods and operators:
    inline bool nb_can_write()
    {
        return m_valid.read() == false || m_ready.read() == true;
    }

    inline bool nb_write( const D& data )
    {
	if ( m_ready.read() == true )
	{
	    m_data = data;
	    m_valid = true;
	    return true;
	}
	else
	{
	    return false;
	}
    }

    inline void reset()
    {
        m_valid = false;
    }

    inline void write( const D& data )
    {
	m_data = data;
        m_valid = true;
	do { ::wait(); } while ( m_ready.read() == false );
	m_valid = false;
    }

    inline const D& operator = ( const D& data )
    {
        write( data );
	return data;
    }

  protected: // channel connections:
    sc_out<D>    m_data;  // data to be transferred.
    sc_in<bool>  m_ready; // true if this port is ready to read a value.
    sc_out<bool> m_valid; // true if m_data has a value to be read.
};

// +============================================================================
// | sc_rvd<D> - COMMUNICATION CHANNEL WITH A READY-VALID HANDSHAKE
// | 
// | This class provides the signals necessary for a data transfer channel 
// | that is controlled using a ready-valid handshake. 
// |
// +============================================================================
template<typename D>
class sc_rvd
{
  public:
    typedef sc_rvd<D>     channel; // channel.
    typedef sc_rvd_in<D>  in;      // input port.
    typedef sc_rvd_out<D> out;     // output port.
    
  public:
    sc_signal<D>    m_data;  // data to be transferred.
    sc_signal<bool> m_ready; // true if the channel reader if ready for data.
    sc_signal<bool> m_valid; // true if the channel writer has provided data.
};

#endif // !defined(sc_rvd_h_included)