scc  2022.4.0
SystemC components library
sc_owning_signal.h
1 /*******************************************************************************
2  * Copyright 2020 MINRES Technologies GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 
17 #ifndef _SCC_SC_SIGNAL_GP_H_
18 #define _SCC_SC_SIGNAL_GP_H_
19 
20 #include <sysc/communication/sc_signal.h>
21 #include <tlm>
27 namespace scc {
35 template <class T, sc_core::sc_writer_policy POL = sc_core::SC_ONE_WRITER> class sc_owning_signal : public sc_core::sc_signal<T*, POL> {
36 protected:
37  using policy_type = sc_core::sc_writer_policy_check<POL>;
38  using super = sc_core::sc_signal<T*, POL>;
39  using type = T*;
40 
41 public: // constructors and destructor:
43  : sc_core::sc_signal<T*, POL>(sc_core::sc_gen_unique_name("signal")) {}
44 
45  explicit sc_owning_signal(const char* name_)
46  : sc_core::sc_signal<T*, POL>(name_, nullptr) {}
47 
48  sc_owning_signal(const char* name_, T* initial_value_)
49  : sc_core::sc_signal<T*, POL>(name_, initial_value_) {}
50 
51  virtual ~sc_owning_signal() {}
52 
53  // write the new value
54  void write(const type& value_) override {
55  bool value_changed = !(super::m_cur_val == value_);
56  if(!policy_type::check_write(this, value_changed))
57  return;
58  // release a potentially previosu written value
59  if(super::m_new_val && super::m_new_val != super::m_cur_val && super::m_new_val->has_mm())
60  super::m_new_val->release();
61  super::m_new_val = value_;
62  // acquire the scheduled value
63  if(super::m_new_val && super::m_new_val->has_mm())
64  super::m_new_val->acquire();
65  if(value_changed)
66  super::request_update();
67  }
68 
69  void clear() {
70  super::m_new_val = nullptr;
71  if(super::m_new_val != super::m_cur_val)
72  super::request_update();
73  }
74 
75  using super::operator=;
76 
77 protected:
78  void update() override {
79  if(!(super::m_new_val == super::m_cur_val)) {
80  if(super::m_cur_val && super::m_cur_val->has_mm())
81  super::m_cur_val->release();
82  super::update();
83  }
84  }
85 };
86 
87 } // namespace scc // end of scc-sysc
89 #endif /* _SCC_SC_SIGNAL_GP_H_ */
sc_signal which takes ownership of the data (acquire()/release())
SCC SystemC utilities.