sc_port_wrapper.hh revision 14126:cbb7113b701a
1/* 2 * Copyright 2019 Google LLC. 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 * Authors: Chun-Chen TK Hsu 28 */ 29 30#ifndef __SYSTEMC_SC_PORT_WRAPPER_HH__ 31#define __SYSTEMC_SC_PORT_WRAPPER_HH__ 32 33#include <string> 34#include <type_traits> 35 36#include "base/logging.hh" 37#include "sim/port.hh" 38#include "systemc/ext/core/sc_export.hh" 39#include "systemc/ext/core/sc_interface.hh" 40#include "systemc/ext/core/sc_port.hh" 41 42namespace sc_gem5 43{ 44 45// Forward declaration 46template <typename IF> 47class ScPortWrapper; 48template <typename IF> 49class ScInterfaceWrapper; 50template <typename IF> 51class ScExportWrapper; 52 53template <typename IF> 54class ScPortWrapper : public ::Port 55{ 56 public: 57 using ScPort = sc_core::sc_port_b<IF>; 58 59 ScPortWrapper(ScPort& p, const std::string& name, PortID id) 60 : Port(name, id), port_(p) 61 {} 62 63 ScPort& 64 port() 65 { 66 return port_; 67 } 68 69 void 70 unbind() override 71 { 72 panic("sc_port can't be unbound."); 73 } 74 75 void 76 bind(::Port& peer) override 77 { 78 // Try ScPortWrapper or ScInterfaceWrapper 79 if (auto* beer = dynamic_cast<ScPortWrapper<IF>*>(&peer)) { 80 port_.bind(beer->port()); 81 } else if (auto* iface = 82 dynamic_cast<ScInterfaceWrapper<IF>*>(&peer)) { 83 port_.bind(iface->interface()); 84 } else { 85 fatal("Attempt to bind sc_port %s to incompatible port %s.", 86 name(), peer.name()); 87 } 88 } 89 90 private: 91 ScPort& port_; 92}; 93 94template <typename IF> 95class ScInterfaceWrapper : public ::Port 96{ 97 public: 98 ScInterfaceWrapper(IF& i, const std::string name, PortID id) 99 : Port(name, id), iface_(i) 100 {} 101 102 IF& 103 interface() 104 { 105 return iface_; 106 } 107 108 void 109 unbind() override 110 { 111 panic("sc_interface can't be unbound."); 112 } 113 114 void 115 bind(::Port& peer) override 116 { 117 // fatal error if peer is neither ScPortWrapper nor ScExportWrapper 118 fatal_if(!dynamic_cast<ScPortWrapper<IF>*>(&peer) && 119 !dynamic_cast<ScExportWrapper<IF>*>(&peer), 120 "Attempt to bind sc_interface %s to incompatible port %s.", 121 name(), peer.name()); 122 123 // Don't bind to peer otherwise we may have error messages saying that 124 // this interface has already be bound since the peer may already did 125 // that. Just let sc_port or sc_export do the binding 126 } 127 128 private: 129 IF& iface_; 130}; 131 132template <typename IF> 133class ScExportWrapper : public ::Port 134{ 135 public: 136 using ScExport = sc_core::sc_export<IF>; 137 138 ScExportWrapper(ScExport& p, const std::string& name, PortID id) 139 : Port(name, id), port_(p) 140 {} 141 142 ScExport& 143 port() 144 { 145 return port_; 146 } 147 148 void 149 unbind() override 150 { 151 panic("sc_export cannot be unbound."); 152 } 153 154 void 155 bind(::Port& peer) override 156 { 157 auto* iface = dynamic_cast<ScInterfaceWrapper<IF>*>(&peer); 158 fatal_if(!iface, 159 "Attempt to bind sc_export %s to incompatible port %s.", 160 name(), peer.name()); 161 162 port_.bind(iface->interface()); 163 } 164 165 private: 166 ScExport& port_; 167}; 168 169} // namespace sc_gem5 170 171#endif // __SYSTEMC_SC_PORT_WRAPPER_HH__ 172