gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
36namespace gem5
37{
38
39template <typename State>
40class SignalSourcePort;
41
42template <typename State>
43class 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 // if bypass_on_change is specified true, it will not call the _onChange
58 // function. Only _state will be updated if needed.
59 void
60 set(const State &new_state, const bool bypass_on_change = false)
61 {
62 if (new_state == _state)
63 return;
64
65 _state = new_state;
66 if (!bypass_on_change && _onChange)
68 }
69
70 public:
71 SignalSinkPort(const std::string &_name, PortID _id=InvalidPortID) :
72 Port(_name, _id)
73 {}
74
75 const State &state() const { return _state; }
76 void onChange(OnChangeFunc func) { _onChange = std::move(func); }
77
78 void
79 bind(Port &peer) override
80 {
81 _source = dynamic_cast<SignalSourcePort<State> *>(&peer);
82 fatal_if(!_source, "Attempt to bind signal pin %s to "
83 "incompatible pin %s", name(), peer.name());
84 // The state of sink has to match the state of source.
85 _state = _source->state();
86 Port::bind(peer);
87 }
88 void
89 unbind() override
90 {
91 _source = nullptr;
93 }
94};
95
96template <typename State>
97class SignalSourcePort : public Port
98{
99 private:
101 State _state;
102
103 public:
104 SignalSourcePort(const std::string &_name, PortID _id = InvalidPortID)
105 : Port(_name, _id)
106 {
107 _state = {};
108 }
109
110 // Give an initial value to the _state instead of using a default value.
111 SignalSourcePort(const std::string &_name, PortID _id,
112 const State &init_state)
113 : SignalSourcePort(_name, _id)
114 {
115 _state = init_state;
116 }
117
118 // if bypass_on_change is specified true, it will not call the _onChange
119 // function. Only _state will be updated if needed.
120 void
121 set(const State &new_state, const bool bypass_on_change = false)
122 {
123 _state = new_state;
124 sink->set(new_state, bypass_on_change);
125 }
126
127 const State &state() const { return _state; }
128
129 void
130 bind(Port &peer) override
131 {
132 sink = dynamic_cast<SignalSinkPort<State> *>(&peer);
133 fatal_if(!sink, "Attempt to bind signal pin %s to "
134 "incompatible pin %s", name(), peer.name());
135 Port::bind(peer);
136 }
137 void
138 unbind() override
139 {
140 sink = nullptr;
141 Port::unbind();
142 }
143};
144
145} // namespace gem5
146
147#endif //__SIM_SIGNAL_HH__
Ports are used to interface objects to each other.
Definition port.hh:62
const std::string name() const
Return port name (for DPRINTF).
Definition port.hh:111
virtual void unbind()
Dettach from a peer port.
Definition port.hh:126
virtual void bind(Port &peer)
Attach to a peer port.
Definition port.hh:118
OnChangeFunc _onChange
Definition signal.hh:54
void set(const State &new_state, const bool bypass_on_change=false)
Definition signal.hh:60
std::function< void(const State &new_val)> OnChangeFunc
Definition signal.hh:46
const State & state() const
Definition signal.hh:75
void onChange(OnChangeFunc func)
Definition signal.hh:76
void bind(Port &peer) override
Attach to a peer port.
Definition signal.hh:79
SignalSourcePort< State > * _source
Definition signal.hh:51
SignalSinkPort(const std::string &_name, PortID _id=InvalidPortID)
Definition signal.hh:71
void unbind() override
Dettach from a peer port.
Definition signal.hh:89
void bind(Port &peer) override
Attach to a peer port.
Definition signal.hh:130
const State & state() const
Definition signal.hh:127
SignalSourcePort(const std::string &_name, PortID _id, const State &init_state)
Definition signal.hh:111
void unbind() override
Dettach from a peer port.
Definition signal.hh:138
void set(const State &new_state, const bool bypass_on_change=false)
Definition signal.hh:121
SignalSourcePort(const std::string &_name, PortID _id=InvalidPortID)
Definition signal.hh:104
SignalSinkPort< State > * sink
Definition signal.hh:100
#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
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
const PortID InvalidPortID
Definition types.hh:246
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition types.hh:245
Port Object Declaration.

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0