SystemC 3.0.0
Accellera SystemC proof-of-concept library
sc_stub.h
Go to the documentation of this file.
1/*****************************************************************************
2
3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4 more contributor license agreements. See the NOTICE file distributed
5 with this work for additional information regarding copyright ownership.
6 Accellera licenses this file to you under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with the
8 License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 implied. See the License for the specific language governing
16 permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22 sc_stub.h -- sc_stub, sc_unbound, sc_tie
23
24 Original Authors: Martin Barnasconi, Ralph Goergen, NXP B.V. 2021/06/01
25
26 CHANGE LOG IS AT THE END OF THE FILE
27 *****************************************************************************/
28
29#ifndef SC_STUB_H
30#define SC_STUB_H
31
37
38namespace sc_core {
39
40
41// ----------------------------------------------------------------------------
42// CLASS : sc_stub_registry (implementation-defined)
43//
44// Registry for stubs.
45// ----------------------------------------------------------------------------
46
48{
49 friend class sc_simcontext;
50
51public:
52 void insert( sc_prim_channel* stub_ ) {
53 if( sc_is_running() ) {
54 SC_REPORT_ERROR( SC_ID_INSERT_STUB_, "simulation running" );
55 return;
56 }
57
58 if( m_simc->elaboration_done() ) {
59 SC_REPORT_ERROR( SC_ID_INSERT_STUB_, "elaboration done" );
60 return;
61 }
62
63 // insert
64 m_stub_vec.push_back( stub_ );
65 }
66
67 int size() const {
68 return static_cast<int>(m_stub_vec.size());
69 }
70
71 void clear() {
72 for (int i = 0; i < size(); i++) {
73 delete m_stub_vec[i];
74 }
75 }
76
77private:
78 // constructor
79 explicit sc_stub_registry( sc_simcontext& simc_ )
80 : m_stub_vec(), m_simc( &simc_ ) { }
81
82 // destructor
83 ~sc_stub_registry() {
84 clear();
85 };
86
87private:
88 std::vector<sc_prim_channel*> m_stub_vec;
89 sc_simcontext* m_simc;
90
91private:
92 // disabled
93 sc_stub_registry();
94 sc_stub_registry( const sc_stub_registry& );
95 sc_stub_registry& operator = ( const sc_stub_registry& );
96};
97
98
99// ----------------------------------------------------------------------------
100// CLASS : sc_stub (implementation-defined)
101//
102// Class sc_stub shall define a predefined channel which acts as an stub.
103// The class shall be used for the definition of sc_unbound and sc_tie.
104// ----------------------------------------------------------------------------
105
106template < typename T >
108{
109public:
111
112 explicit sc_stub( const char* nm )
113 : sc_core::sc_prim_channel(nm) {}
114
115 explicit sc_stub( const char* nm, const T& val )
116 : sc_core::sc_prim_channel(nm), m_init_val(val) {}
117
118 // reading a stub returns the initial value
119 // note: The value read from the channel is implementation-defined
120 const T& read() const { return m_init_val; }
121 operator const T& () const { return read(); }
122
123 // writing to a stub should do nothing.
124 virtual void write( const T& ) { /* do nothing */ }
125 this_type& operator = ( const T& ){ return *this; }
126 this_type& operator = ( const sc_signal_in_if<T>& ) { return *this; }
127 this_type& operator = ( const this_type& ) { return *this; }
128
129 const char* kind() const { return "sc_stub"; }
130
131 void print( ::std::ostream& os = ::std::cout ) const { os << "sc_stub"; }
132 void dump( ::std::ostream& os = ::std::cout ) const { os << "sc_stub"; }
133
134 // no event handling, since there are no value changes
135 // TODO: generate warning message?
136 const sc_event& default_event() const { return ev; }
137 const sc_event& value_changed_event() const { return ev; }
138 const sc_event& posedge_event() const { return ev; }
139 const sc_event& negedge_event() const { return ev; }
140 bool event() const { return false; }
141 bool posedge() const { return false; }
142 bool negedge() const { return false; }
143
144 // get a reference to the current value (for tracing)
145 const T& get_data_ref() const { return m_init_val; }
146
148
149private:
150 T m_init_val;
151 sc_event ev;
152}; // class sc_stub
153
154
155// ----------------------------------------------------------------------------
156// CLASS : sc_unbound_impl (implementation-defined)
157//
158// The class sc_unbound_impl is used to create object sc_unbound
159// ----------------------------------------------------------------------------
160
161struct sc_unbound_impl // implementation-defined
162{
163 // Only allowed to connect sc_unbound to interfaces of type output and inout
164 template < typename T >
166 {
169 return *stub;
170 }
171}; // struct sc_unbound_impl
172
173
174// ----------------------------------------------------------------------------
175// sc_unbound
176//
177// The static object sc_unbound shall define a predefined channel which acts as
178// an open connection. Each time sc_unbound is used in an application, a new
179// predefined channel shall be created with a name with prefix "sc_unbound",
180// followed by an underscore and series of one of more decimal digits from
181// the character set 0-9.
182//
183// It shall be error to bind sc_unbound to a port which is not of type
184// sc_port <sc_signal_inout_if<T> >. It shall be allowed to read and write
185// to this channel. The value read from the channel is implementation-defined.
186// Values written to the channel shall be ignored. The channel shall not
187// notify an event when values are written.
188//
189// NOTE—An application cannot not use sc_unbound for input ports of type
190// sc_port <sc_signal_in_if<T> > because its value is undefined.
191// ----------------------------------------------------------------------------
192
193static sc_unbound_impl const /* implementation defined */ sc_unbound = {};
194
195// ----------------------------------------------------------------------------
196// sc_tie
197//
198// The function sc_tie::value shall return a predefined channel to tie
199// the port to the specified value of type T. The type of the value shall be
200// compatible with the type of the associated channel to which the port
201// is bound. Each time sc_tie is used in an application, a new
202// predefined channel shall be created with a name with prefix "sc_tie",
203// followed by an underscore and series of one of more decimal digits from
204// the character set 0-9.
205//
206// It shall be error to bind sc_tie to a port which is not of type
207// sc_port<sc_signal_in_if<T> > or sc_port <sc_signal_inout_if<T> >.
208// It shall be allowed to read and write to this channel. The value read
209// from the channel shall correspond to the specified pre-defined types.
210// Values written to the channel shall be ignored. The channel shall not
211// notify an event when values are written.
212// ----------------------------------------------------------------------------
213
214namespace sc_tie {
215
216 template < typename T >
218 {
221 return *stub;
222 }
223
224} // namespace sc_tie
225
226} // namespace sc_core
227
228
229/*****************************************************************************
230
231 MODIFICATION LOG - modifiers, enter your name, affiliation, date and
232 changes you are making here.
233
234 Name, Affiliation, Date:
235 Description of Modification:
236
237 *****************************************************************************/
238//$Log: sc_stub.h,v $
239
240
241#endif // SC_STUB_H
242
243// Taf!
#define SC_REPORT_ERROR(msg_type, msg)
Definition: sc_report.h:217
SC_API const char * sc_gen_unique_name(const char *, bool preserve_first)
const char SC_ID_INSERT_STUB_[]
sc_simcontext * sc_get_curr_simcontext()
static sc_unbound_impl const sc_unbound
Definition: sc_stub.h:193
SC_API bool sc_is_running(const sc_simcontext *simc_p)
sc_core::sc_signal_in_if< T > & value(const T &val)
Definition: sc_stub.h:217
friend class sc_simcontext
Definition: sc_stub.h:49
void insert(sc_prim_channel *stub_)
Definition: sc_stub.h:52
const sc_event & default_event() const
Definition: sc_stub.h:136
const sc_event & value_changed_event() const
Definition: sc_stub.h:137
this_type & operator=(const T &)
Definition: sc_stub.h:125
sc_stub(const char *nm, const T &val)
Definition: sc_stub.h:115
const char * kind() const
Definition: sc_stub.h:129
bool posedge() const
Definition: sc_stub.h:141
const T & read() const
Definition: sc_stub.h:120
const T & get_data_ref() const
Definition: sc_stub.h:145
sc_stub< T > this_type
Definition: sc_stub.h:110
const sc_event & posedge_event() const
Definition: sc_stub.h:138
void dump(::std::ostream &os=::std::cout) const
Definition: sc_stub.h:132
sc_stub(const char *nm)
Definition: sc_stub.h:112
const sc_event & negedge_event() const
Definition: sc_stub.h:139
virtual void write(const T &)
Definition: sc_stub.h:124
void print(::std::ostream &os=::std::cout) const
Definition: sc_stub.h:131
bool event() const
Definition: sc_stub.h:140
bool negedge() const
Definition: sc_stub.h:142
bool elaboration_done() const
sc_stub_registry * get_stub_registry()