gem5 v24.1.0.1
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 = {};
54
55 protected:
56 // if bypass_on_change is specified true, it will not call the _onChange
57 // function. Only _state will be updated if needed.
58 virtual void
59 set(const State &new_state, const bool bypass_on_change = false)
60 {
61 if (new_state == _state)
62 return;
63
64 _state = new_state;
65 if (!bypass_on_change && _onChange)
67 }
68
70
71 public:
72 SignalSinkPort(const std::string &_name, PortID _id=InvalidPortID) :
73 Port(_name, _id)
74 {}
75
76 const State &state() const { return _state; }
77 void onChange(OnChangeFunc func) { _onChange = std::move(func); }
78
79 void
80 bind(Port &peer) override
81 {
82 _source = dynamic_cast<SignalSourcePort<State> *>(&peer);
83 fatal_if(!_source, "Attempt to bind signal pin %s to "
84 "incompatible pin %s", name(), peer.name());
85 // The state of sink has to match the state of source.
86 _state = _source->state();
87 Port::bind(peer);
88 }
89 void
90 unbind() override
91 {
92 _source = nullptr;
94 }
95};
96
97template <typename State>
98class SignalSourcePort : public Port
99{
100 private:
102 State _state;
103
104 public:
105 SignalSourcePort(const std::string &_name, PortID _id = InvalidPortID)
106 : Port(_name, _id)
107 {
108 _state = {};
109 }
110
111 // Give an initial value to the _state instead of using a default value.
112 SignalSourcePort(const std::string &_name, PortID _id,
113 const State &init_state)
114 : SignalSourcePort(_name, _id)
115 {
116 _state = init_state;
117 }
118
119 // if bypass_on_change is specified true, it will not call the _onChange
120 // function. Only _state will be updated if needed.
121 void
122 set(const State &new_state, const bool bypass_on_change = false)
123 {
124 _state = new_state;
125 sink->set(new_state, bypass_on_change);
126 }
127
128 const State &state() const { return _state; }
129
130 void
131 bind(Port &peer) override
132 {
133 sink = dynamic_cast<SignalSinkPort<State> *>(&peer);
134 fatal_if(!sink, "Attempt to bind signal pin %s to "
135 "incompatible pin %s", name(), peer.name());
136 Port::bind(peer);
137 }
138 void
139 unbind() override
140 {
141 sink = nullptr;
142 Port::unbind();
143 }
144};
145
146} // namespace gem5
147
148#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:69
std::function< void(const State &new_val)> OnChangeFunc
Definition signal.hh:46
const State & state() const
Definition signal.hh:76
virtual void set(const State &new_state, const bool bypass_on_change=false)
Definition signal.hh:59
void onChange(OnChangeFunc func)
Definition signal.hh:77
void bind(Port &peer) override
Attach to a peer port.
Definition signal.hh:80
SignalSourcePort< State > * _source
Definition signal.hh:51
SignalSinkPort(const std::string &_name, PortID _id=InvalidPortID)
Definition signal.hh:72
void unbind() override
Dettach from a peer port.
Definition signal.hh:90
void bind(Port &peer) override
Attach to a peer port.
Definition signal.hh:131
const State & state() const
Definition signal.hh:128
SignalSourcePort(const std::string &_name, PortID _id, const State &init_state)
Definition signal.hh:112
void unbind() override
Dettach from a peer port.
Definition signal.hh:139
void set(const State &new_state, const bool bypass_on_change=false)
Definition signal.hh:122
SignalSourcePort(const std::string &_name, PortID _id=InvalidPortID)
Definition signal.hh:105
SignalSinkPort< State > * sink
Definition signal.hh:101
#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 Arm Limited 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 Mon Jan 13 2025 04:28:42 for gem5 by doxygen 1.9.8