sc_port.hh revision 13293:60c727f33e16
1/* 2 * Copyright 2018 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 * Authors: Gabe Black 28 */ 29 30#ifndef __SYSTEMC_EXT_CORE_SC_PORT_HH__ 31#define __SYSTEMC_EXT_CORE_SC_PORT_HH__ 32 33#include <typeinfo> 34#include <vector> 35 36#include "../utils/sc_report_handler.hh" 37#include "sc_module.hh" // for sc_gen_unique_name 38#include "sc_object.hh" 39 40namespace sc_gem5 41{ 42 43class Port; 44 45}; 46 47namespace sc_core 48{ 49 50class sc_interface; 51class sc_trace_file; 52 53// Nonstandard 54// Despite having a warning "FOR INTERNAL USE ONLY!" in all caps above this 55// class definition in the Accellera implementation, it appears in their 56// examples and test programs, and so we need to have it here as well. 57struct sc_trace_params 58{ 59 sc_trace_file *tf; 60 std::string name; 61 62 sc_trace_params(sc_trace_file *tf, const std::string &name) : 63 tf(tf), name(name) 64 {} 65}; 66typedef std::vector<sc_trace_params *> sc_trace_params_vec; 67 68enum sc_port_policy 69{ 70 SC_ONE_OR_MORE_BOUND, // Default 71 SC_ZERO_OR_MORE_BOUND, 72 SC_ALL_BOUND 73}; 74 75class sc_port_base : public sc_object 76{ 77 public: 78 sc_port_base(const char *name, int n, sc_port_policy p); 79 virtual ~sc_port_base(); 80 81 void warn_unimpl(const char *func) const; 82 83 int maxSize() const; 84 int size() const; 85 86 const char *kind() const { return "sc_port_base"; } 87 88 protected: 89 // Implementation defined, but depended on by the tests. 90 void bind(sc_interface &); 91 void bind(sc_port_base &); 92 93 friend class ::sc_gem5::Module; 94 95 // Implementation defined, but depended on by the tests. 96 virtual int vbind(sc_interface &) = 0; 97 virtual int vbind(sc_port_base &) = 0; 98 99 virtual void before_end_of_elaboration() = 0; 100 virtual void end_of_elaboration() = 0; 101 virtual void start_of_simulation() = 0; 102 virtual void end_of_simulation() = 0; 103 104 void report_error(const char *id, const char *add_msg) const; 105 106 private: 107 friend class ::sc_gem5::Port; 108 friend class ::sc_gem5::Kernel; 109 110 virtual sc_interface *_gem5Interface(int n) const = 0; 111 virtual void _gem5AddInterface(sc_interface *i) = 0; 112 113 ::sc_gem5::Port *_gem5Port; 114 virtual const char *_ifTypeName() const = 0; 115 virtual sc_port_policy _portPolicy() const = 0; 116}; 117 118template <class IF> 119class sc_port_b : public sc_port_base 120{ 121 public: 122 void operator () (IF &i) { bind(i); } 123 void operator () (sc_port_b<IF> &p) { bind(p); } 124 125 virtual void bind(IF &i) { sc_port_base::bind(i); } 126 virtual void bind(sc_port_b<IF> &p) { sc_port_base::bind(p); } 127 128 IF * 129 operator -> () 130 { 131 if (_interfaces.empty()) { 132 report_error("(E112) get interface failed", "port is not bound"); 133 sc_abort(); 134 } 135 return _interfaces[0]; 136 } 137 const IF * 138 operator -> () const 139 { 140 if (_interfaces.empty()) { 141 report_error("(E112) get interface failed", "port is not bound"); 142 sc_abort(); 143 } 144 return _interfaces[0]; 145 } 146 147 IF * 148 operator [] (int n) 149 { 150 if (n < 0 || n >= size()) { 151 report_error("(E112) get interface failed", "index out of range"); 152 return NULL; 153 } 154 return _interfaces[n]; 155 } 156 const IF * 157 operator [] (int n) const 158 { 159 if (n < 0 || n >= size()) { 160 report_error("(E112) get interface failed", "index out of range"); 161 return NULL; 162 } 163 return _interfaces[n]; 164 } 165 166 sc_interface * 167 get_interface() 168 { 169 if (_interfaces.empty()) 170 return NULL; 171 return _interfaces[0]; 172 } 173 const sc_interface * 174 get_interface() const 175 { 176 if (_interfaces.empty()) 177 return NULL; 178 return _interfaces[0]; 179 } 180 181 protected: 182 void before_end_of_elaboration() {} 183 void end_of_elaboration() {} 184 void start_of_simulation() {} 185 void end_of_simulation() {} 186 187 explicit sc_port_b(int n, sc_port_policy p) : 188 sc_port_base(sc_gen_unique_name("port"), n, p) 189 {} 190 sc_port_b(const char *name, int n, sc_port_policy p) : 191 sc_port_base(name, n, p) 192 {} 193 virtual ~sc_port_b() {} 194 195 // Implementation defined, but depended on by the tests. 196 int 197 vbind(sc_interface &i) 198 { 199 IF *interface = dynamic_cast<IF *>(&i); 200 if (!interface) 201 return 2; 202 sc_port_base::bind(*interface); 203 return 0; 204 } 205 int 206 vbind(sc_port_base &pb) 207 { 208 sc_port_b<IF> *p = dynamic_cast<sc_port_b<IF> *>(&pb); 209 if (!p) 210 return 2; 211 sc_port_base::bind(*p); 212 return 0; 213 } 214 215 private: 216 std::vector<IF *> _interfaces; 217 218 sc_interface * 219 _gem5Interface(int n) const 220 { 221 if (n < 0 || n >= size()) { 222 report_error("(E112) get interface failed", "index out of range"); 223 return NULL; 224 } 225 return _interfaces[n]; 226 } 227 void 228 _gem5AddInterface(sc_interface *iface) 229 { 230 IF *interface = dynamic_cast<IF *>(iface); 231 sc_assert(interface); 232 for (int i = 0; i < _interfaces.size(); i++) { 233 if (interface == _interfaces[i]) { 234 report_error("(E107) bind interface to port failed", 235 "interface already bound to port"); 236 } 237 } 238 _interfaces.push_back(interface); 239 } 240 241 const char *_ifTypeName() const { return typeid(IF).name(); } 242 243 // Disabled 244 sc_port_b() {} 245 sc_port_b(const sc_port_b<IF> &) {} 246 sc_port_b<IF> &operator = (const sc_port_b<IF> &) { return *this; } 247}; 248 249template <class IF, int N=1, sc_port_policy P=SC_ONE_OR_MORE_BOUND> 250class sc_port : public sc_port_b<IF> 251{ 252 public: 253 sc_port() : sc_port_b<IF>(N, P) {} 254 explicit sc_port(const char *name) : sc_port_b<IF>(name, N, P) {} 255 virtual ~sc_port() {} 256 257 // Deprecated binding constructors. 258 explicit sc_port(const IF &interface) : sc_port_b<IF>(N, P) 259 { 260 this->warn_unimpl(__PRETTY_FUNCTION__); 261 // Should warn that these are deprecated. See Accellera sc_port.h. 262 sc_port_b<IF>::bind(const_cast<IF &>(interface)); 263 } 264 sc_port(const char *name, const IF &interface) : sc_port_b<IF>(name, N, P) 265 { 266 this->warn_unimpl(__PRETTY_FUNCTION__); 267 // Should warn that these are deprecated. See Accellera sc_port.h. 268 sc_port_b<IF>::bind(const_cast<IF &>(interface)); 269 } 270 explicit sc_port(sc_port_b<IF> &parent) : sc_port_b<IF>(N, P) 271 { 272 this->warn_unimpl(__PRETTY_FUNCTION__); 273 // Should warn that these are deprecated. See Accellera sc_port.h. 274 sc_port_b<IF>::bind(parent); 275 } 276 sc_port(const char *name, sc_port_b<IF> &parent) : 277 sc_port_b<IF>(name, N, P) 278 { 279 this->warn_unimpl(__PRETTY_FUNCTION__); 280 // Should warn that these are deprecated. See Accellera sc_port.h. 281 sc_port_b<IF>::bind(parent); 282 } 283 explicit sc_port(sc_port<IF, N, P> &parent) : sc_port_b<IF>(N, P) 284 { 285 this->warn_unimpl(__PRETTY_FUNCTION__); 286 // Should warn that these are deprecated. See Accellera sc_port.h. 287 sc_port_b<IF>::bind(parent); 288 } 289 sc_port(const char *name, sc_port<IF, N, P> &parent) : 290 sc_port_b<IF>(name, N, P) 291 { 292 this->warn_unimpl(__PRETTY_FUNCTION__); 293 // Should warn that these are deprecated. See Accellera sc_port.h. 294 sc_port_b<IF>::bind(parent); 295 } 296 297 virtual const char *kind() const { return "sc_port"; } 298 299 private: 300 // Disabled 301 sc_port(const sc_port<IF, N, P> &) {} 302 sc_port<IF, N, P> &operator = (const sc_port<IF, N, P> &) { return *this; } 303 304 virtual sc_port_policy _portPolicy() const { return P; } 305}; 306 307} // namespace sc_core 308 309#endif //__SYSTEMC_EXT_CORE_SC_PORT_HH__ 310