gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
signal.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2022 Google, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer;
8  * redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution;
11  * neither the name of the copyright holders nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef __SIM_SIGNAL_HH__
29 #define __SIM_SIGNAL_HH__
30 
31 #include <functional>
32 
33 #include "base/logging.hh"
34 #include "sim/port.hh"
35 
36 namespace gem5
37 {
38 
39 template <typename State>
41 
42 template <typename State>
43 class SignalSinkPort : public Port
44 {
45  public:
46  using OnChangeFunc = std::function<void(const State &new_val)>;
47 
48  private:
50 
52 
53  State _state = {};
55 
56  protected:
57  void
58  set(const State &new_state)
59  {
60  if (new_state == _state)
61  return;
62 
63  _state = new_state;
64  if (_onChange)
66  }
67 
68  public:
69  SignalSinkPort(const std::string &_name, PortID _id=InvalidPortID) :
70  Port(_name, _id)
71  {}
72 
73  const State &state() const { return _state; }
74  void onChange(OnChangeFunc func) { _onChange = std::move(func); }
75 
76  void
77  bind(Port &peer) override
78  {
79  _source = dynamic_cast<SignalSourcePort<State> *>(&peer);
80  fatal_if(!_source, "Attempt to bind signal pin %s to "
81  "incompatible pin %s", name(), peer.name());
82  Port::bind(peer);
83  }
84  void
85  unbind() override
86  {
87  _source = nullptr;
88  Port::unbind();
89  }
90 };
91 
92 template <typename State>
93 class SignalSourcePort : public Port
94 {
95  private:
97  State _state = {};
98 
99  public:
100  SignalSourcePort(const std::string &_name, PortID _id=InvalidPortID) :
101  Port(_name, _id)
102  {}
103 
104  void
105  set(const State &new_state)
106  {
107  _state = new_state;
108  sink->set(new_state);
109  }
110 
111  const State &state() const { return _state; }
112 
113  void
114  bind(Port &peer) override
115  {
116  sink = dynamic_cast<SignalSinkPort<State> *>(&peer);
117  fatal_if(!sink, "Attempt to bind signal pin %s to "
118  "incompatible pin %s", name(), peer.name());
119  Port::bind(peer);
120  }
121  void
122  unbind() override
123  {
124  sink = nullptr;
125  Port::unbind();
126  }
127 };
128 
129 } // namespace gem5
130 
131 #endif //__SIM_SIGNAL_HH__
gem5::PortID
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition: types.hh:245
gem5::SignalSourcePort::bind
void bind(Port &peer) override
Definition: signal.hh:114
gem5::SignalSinkPort::state
const State & state() const
Definition: signal.hh:73
gem5::Port::name
const std::string name() const
Return port name (for DPRINTF).
Definition: port.hh:111
gem5::SignalSinkPort::onChange
void onChange(OnChangeFunc func)
Definition: signal.hh:74
gem5::SignalSourcePort::set
void set(const State &new_state)
Definition: signal.hh:105
gem5::SignalSinkPort< bool >::OnChangeFunc
std::function< void(const bool &new_val)> OnChangeFunc
Definition: signal.hh:46
gem5::SignalSinkPort::_state
State _state
Definition: signal.hh:53
gem5::SignalSinkPort::unbind
void unbind() override
Dettach from a peer port.
Definition: signal.hh:85
gem5::InvalidPortID
const PortID InvalidPortID
Definition: types.hh:246
gem5::SignalSinkPort::set
void set(const State &new_state)
Definition: signal.hh:58
gem5::SignalSourcePort::_state
State _state
Definition: signal.hh:97
gem5::Port::bind
virtual void bind(Port &peer)
Attach to a peer port.
Definition: port.hh:118
gem5::SignalSourcePort
Definition: signal.hh:40
gem5::SignalSourcePort::sink
SignalSinkPort< State > * sink
Definition: signal.hh:96
gem5::SignalSinkPort::_onChange
OnChangeFunc _onChange
Definition: signal.hh:54
port.hh
name
const std::string & name()
Definition: trace.cc:48
gem5::SignalSourcePort::state
const State & state() const
Definition: signal.hh:111
gem5::SignalSinkPort
Definition: signal.hh:43
gem5::Port::unbind
virtual void unbind()
Dettach from a peer port.
Definition: port.hh:126
gem5::SignalSourcePort::SignalSourcePort
SignalSourcePort(const std::string &_name, PortID _id=InvalidPortID)
Definition: signal.hh:100
gem5::Port
Ports are used to interface objects to each other.
Definition: port.hh:61
logging.hh
gem5::SignalSinkPort::SignalSinkPort
SignalSinkPort(const std::string &_name, PortID _id=InvalidPortID)
Definition: signal.hh:69
fatal_if
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:236
gem5::SignalSinkPort::bind
void bind(Port &peer) override
Attach to a peer port.
Definition: signal.hh:77
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::SignalSinkPort::_source
SignalSourcePort< State > * _source
Definition: signal.hh:51
gem5::SignalSourcePort::unbind
void unbind() override
Definition: signal.hh:122

Generated on Sun Jul 30 2023 01:57:00 for gem5 by doxygen 1.8.17