Gabe Black has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/66672?usp=email )

Change subject: dev: Implement a "Signal" port which has a templated State type.
......................................................................

dev: Implement a "Signal" port which has a templated State type.

This port type transmits a value of the templated State type. When the
value changes, the sink port will call the registered callback with
the new value.

Change-Id: I72eaf74658a2c63bece95e48c1a72694874eaad8
---
A src/sim/signal.hh
1 file changed, 144 insertions(+), 0 deletions(-)



diff --git a/src/sim/signal.hh b/src/sim/signal.hh
new file mode 100644
index 0000000..3cb3f62
--- /dev/null
+++ b/src/sim/signal.hh
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2022 Google, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __SIM_SIGNAL_HH__
+#define __SIM_SIGNAL_HH__
+
+#include <functional>
+
+#include "base/logging.hh"
+#include "sim/port.hh"
+
+namespace gem5
+{
+
+template <typename State>
+class SignalSourcePort;
+
+template <typename State>
+class SignalSinkPort : public Port
+{
+  public:
+    using OnChangeFunc = std::function<void(const State &new_val)>;
+
+  private:
+    friend SignalSourcePort<State>;
+
+    SignalSourcePort<State> *_source = nullptr;
+
+    State _state = {};
+    OnChangeFunc _onChange;
+
+  protected:
+    void
+    set(const State &new_state)
+    {
+        if (new_state == _state)
+            return;
+
+        _state = new_state;
+        if (_onChange)
+            _onChange(_state);
+    }
+
+  public:
+    SignalSinkPort(const std::string &_name, PortID _id=InvalidPortID) :
+        Port(_name, _id)
+    {}
+
+    const State &state() const { return _state; }
+    void onChange(OnChangeFunc func) { _onChange = std::move(func); }
+
+    void
+    bind(Port &peer) override
+    {
+        _source = dynamic_cast<SignalSourcePort<State> *>(&peer);
+        fatal_if(!_source, "Attempt to bind signal pin %s to "
+                "incompatible pin %s", name(), peer.name());
+        Port::bind(peer);
+    }
+    void
+    unbind() override
+    {
+        _source = nullptr;
+        Port::unbind();
+    }
+};
+
+template <typename State>
+class SignalSourcePort : public Port
+{
+  private:
+    SignalSinkPort<State> *sink = nullptr;
+    State _state = {};
+
+  public:
+    SignalSourcePort(const std::string &_name, PortID _id=InvalidPortID) :
+        Port(_name, _id)
+    {}
+
+    void
+    set(const State &new_state)
+    {
+        _state = new_state;
+        sink->set(new_state);
+    }
+
+    const State &state() const { return _state; }
+
+    void
+    bind(Port &peer) override
+    {
+        sink = dynamic_cast<SignalSinkPort<State> *>(&peer);
+        fatal_if(!sink, "Attempt to bind signal pin %s to "
+                "incompatible pin %s", name(), peer.name());
+        Port::bind(peer);
+    }
+    void
+    unbind() override
+    {
+        sink = nullptr;
+        Port::unbind();
+    }
+};
+
+} // namespace gem5
+
+#endif //__SIM_SIGNAL_HH__

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/66672?usp=email To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I72eaf74658a2c63bece95e48c1a72694874eaad8
Gerrit-Change-Number: 66672
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <gabe.bl...@gmail.com>
Gerrit-CC: Gabe Black <gabebl...@google.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org

Reply via email to