sim: Add a takeOverFrom method to the base Port class.
authorGabe Black <gabeblack@google.com>
Sat, 17 Aug 2019 06:12:11 +0000 (23:12 -0700)
committerGabe Black <gabeblack@google.com>
Tue, 27 Aug 2019 22:17:57 +0000 (22:17 +0000)
This makes it easier, safer, and less verbose to swap out ports when
a CPU takes over for another CPU, for instance.

Change-Id: Ice08e4dcb4b04dc66b1841331092a78b4f6f5a96
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/20233
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Gabe Black <gabeblack@google.com>

src/sim/port.hh

index ee4b548a5e019ed5c16fe20e73a6688af81a6c21..1114172637702fbaaa16e5b7be3b5d80604e67b1 100644 (file)
@@ -126,6 +126,25 @@ class Port
 
     /** Is this port currently connected to a peer? */
     bool isConnected() const { return _connected; }
+
+    /** A utility function to make it easier to swap out ports. */
+    void
+    takeOverFrom(Port *old)
+    {
+        assert(old);
+        assert(old->isConnected());
+        assert(!isConnected());
+        Port &peer = old->getPeer();
+        assert(peer.isConnected());
+
+        // Disconnect the original binding.
+        old->unbind();
+        peer.unbind();
+
+        // Connect the new binding.
+        peer.bind(*this);
+        bind(peer);
+    }
 };
 
 #endif //__SIM_PORT_HH__