sc_module.cc revision 13146
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#include <memory> 31#include <string> 32#include <vector> 33 34#include "base/logging.hh" 35#include "systemc/core/kernel.hh" 36#include "systemc/core/module.hh" 37#include "systemc/core/process_types.hh" 38#include "systemc/ext/core/sc_module.hh" 39#include "systemc/ext/core/sc_module_name.hh" 40#include "systemc/ext/utils/sc_report_handler.hh" 41 42namespace sc_gem5 43{ 44 45Process * 46newMethodProcess(const char *name, ProcessFuncWrapper *func) 47{ 48 Method *p = new Method(name, func); 49 if (::sc_core::sc_is_running()) { 50 std::string name = p->name(); 51 delete p; 52 SC_REPORT_ERROR("(E541) call to SC_METHOD in sc_module while " 53 "simulation running", name.c_str()); 54 return nullptr; 55 } 56 scheduler.reg(p); 57 return p; 58} 59 60Process * 61newThreadProcess(const char *name, ProcessFuncWrapper *func) 62{ 63 Thread *p = new Thread(name, func); 64 if (::sc_core::sc_is_running()) { 65 std::string name = p->name(); 66 delete p; 67 SC_REPORT_ERROR("(E542) call to SC_THREAD in sc_module while " 68 "simulation running", name.c_str()); 69 return nullptr; 70 } 71 scheduler.reg(p); 72 return p; 73} 74 75Process * 76newCThreadProcess(const char *name, ProcessFuncWrapper *func) 77{ 78 CThread *p = new CThread(name, func); 79 if (::sc_core::sc_is_running()) { 80 std::string name = p->name(); 81 delete p; 82 SC_REPORT_ERROR("(E543) call to SC_CTHREAD in sc_module while " 83 "simulation running", name.c_str()); 84 return nullptr; 85 } 86 scheduler.reg(p); 87 p->dontInitialize(); 88 return p; 89} 90 91UniqueNameGen nameGen; 92 93} // namespace sc_gem5 94 95namespace sc_core 96{ 97 98sc_bind_proxy::sc_bind_proxy(sc_interface &_interface) : 99 _interface(&_interface), _port(nullptr) 100{} 101 102sc_bind_proxy::sc_bind_proxy(sc_port_base &_port) : 103 _interface(nullptr), _port(&_port) 104{} 105 106const sc_bind_proxy SC_BIND_PROXY_NUL(*(sc_port_base *)nullptr); 107 108sc_module::~sc_module() { delete _gem5_module; } 109 110const sc_bind_proxy SC_BIND_PROXY_NIL(*(sc_port_base *)nullptr); 111 112void 113sc_module::operator () (const sc_bind_proxy &p001, 114 const sc_bind_proxy &p002, 115 const sc_bind_proxy &p003, 116 const sc_bind_proxy &p004, 117 const sc_bind_proxy &p005, 118 const sc_bind_proxy &p006, 119 const sc_bind_proxy &p007, 120 const sc_bind_proxy &p008, 121 const sc_bind_proxy &p009, 122 const sc_bind_proxy &p010, 123 const sc_bind_proxy &p011, 124 const sc_bind_proxy &p012, 125 const sc_bind_proxy &p013, 126 const sc_bind_proxy &p014, 127 const sc_bind_proxy &p015, 128 const sc_bind_proxy &p016, 129 const sc_bind_proxy &p017, 130 const sc_bind_proxy &p018, 131 const sc_bind_proxy &p019, 132 const sc_bind_proxy &p020, 133 const sc_bind_proxy &p021, 134 const sc_bind_proxy &p022, 135 const sc_bind_proxy &p023, 136 const sc_bind_proxy &p024, 137 const sc_bind_proxy &p025, 138 const sc_bind_proxy &p026, 139 const sc_bind_proxy &p027, 140 const sc_bind_proxy &p028, 141 const sc_bind_proxy &p029, 142 const sc_bind_proxy &p030, 143 const sc_bind_proxy &p031, 144 const sc_bind_proxy &p032, 145 const sc_bind_proxy &p033, 146 const sc_bind_proxy &p034, 147 const sc_bind_proxy &p035, 148 const sc_bind_proxy &p036, 149 const sc_bind_proxy &p037, 150 const sc_bind_proxy &p038, 151 const sc_bind_proxy &p039, 152 const sc_bind_proxy &p040, 153 const sc_bind_proxy &p041, 154 const sc_bind_proxy &p042, 155 const sc_bind_proxy &p043, 156 const sc_bind_proxy &p044, 157 const sc_bind_proxy &p045, 158 const sc_bind_proxy &p046, 159 const sc_bind_proxy &p047, 160 const sc_bind_proxy &p048, 161 const sc_bind_proxy &p049, 162 const sc_bind_proxy &p050, 163 const sc_bind_proxy &p051, 164 const sc_bind_proxy &p052, 165 const sc_bind_proxy &p053, 166 const sc_bind_proxy &p054, 167 const sc_bind_proxy &p055, 168 const sc_bind_proxy &p056, 169 const sc_bind_proxy &p057, 170 const sc_bind_proxy &p058, 171 const sc_bind_proxy &p059, 172 const sc_bind_proxy &p060, 173 const sc_bind_proxy &p061, 174 const sc_bind_proxy &p062, 175 const sc_bind_proxy &p063, 176 const sc_bind_proxy &p064) 177{ 178 std::vector<const ::sc_core::sc_bind_proxy *> proxies; 179 auto insert = [&proxies](const ::sc_core::sc_bind_proxy &p) -> bool { 180 if (!p.port() && !p.interface()) 181 return false; 182 proxies.push_back(&p); 183 return true; 184 }; 185 insert(p001) && insert(p002) && insert(p003) && insert(p004) && 186 insert(p005) && insert(p006) && insert(p007) && insert(p008) && 187 insert(p009) && insert(p010) && insert(p011) && insert(p012) && 188 insert(p013) && insert(p014) && insert(p015) && insert(p016) && 189 insert(p017) && insert(p018) && insert(p019) && insert(p020) && 190 insert(p021) && insert(p022) && insert(p023) && insert(p024) && 191 insert(p025) && insert(p026) && insert(p027) && insert(p028) && 192 insert(p029) && insert(p030) && insert(p031) && insert(p032) && 193 insert(p033) && insert(p034) && insert(p035) && insert(p036) && 194 insert(p037) && insert(p038) && insert(p039) && insert(p040) && 195 insert(p041) && insert(p042) && insert(p043) && insert(p044) && 196 insert(p045) && insert(p046) && insert(p047) && insert(p048) && 197 insert(p049) && insert(p050) && insert(p051) && insert(p052) && 198 insert(p053) && insert(p054) && insert(p055) && insert(p056) && 199 insert(p057) && insert(p058) && insert(p059) && insert(p060) && 200 insert(p061) && insert(p062) && insert(p063) && insert(p064); 201 _gem5_module->bindPorts(proxies); 202} 203 204const std::vector<sc_object *> & 205sc_module::get_child_objects() const 206{ 207 return _gem5_module->obj()->get_child_objects(); 208} 209 210const std::vector<sc_event *> & 211sc_module::get_child_events() const 212{ 213 return _gem5_module->obj()->get_child_events(); 214} 215 216sc_module::sc_module() : 217 sc_object(sc_gem5::newModuleChecked()->name()), 218 _gem5_module(sc_gem5::currentModule()) 219{} 220 221sc_module::sc_module(const sc_module_name &) : sc_module() {} 222sc_module::sc_module(const char *_name) : sc_module(sc_module_name(_name)) {} 223sc_module::sc_module(const std::string &_name) : 224 sc_module(sc_module_name(_name.c_str())) 225{} 226 227void 228sc_module::reset_signal_is(const sc_in<bool> &, bool) 229{ 230 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 231} 232 233void 234sc_module::reset_signal_is(const sc_inout<bool> &, bool) 235{ 236 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 237} 238 239void 240sc_module::reset_signal_is(const sc_out<bool> &, bool) 241{ 242 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 243} 244 245void 246sc_module::reset_signal_is(const sc_signal_in_if<bool> &, bool) 247{ 248 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 249} 250 251 252void 253sc_module::async_reset_signal_is(const sc_in<bool> &, bool) 254{ 255 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 256} 257 258void 259sc_module::async_reset_signal_is(const sc_inout<bool> &, bool) 260{ 261 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 262} 263 264void 265sc_module::async_reset_signal_is(const sc_out<bool> &, bool) 266{ 267 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 268} 269 270void 271sc_module::async_reset_signal_is(const sc_signal_in_if<bool> &, bool) 272{ 273 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 274} 275 276 277void 278sc_module::dont_initialize() 279{ 280 ::sc_gem5::Process::newest()->dontInitialize(); 281} 282 283void 284sc_module::set_stack_size(size_t size) 285{ 286 ::sc_gem5::Process::newest()->setStackSize(size); 287} 288 289 290void sc_module::next_trigger() { ::sc_core::next_trigger(); } 291 292void 293sc_module::next_trigger(const sc_event &e) 294{ 295 ::sc_core::next_trigger(e); 296} 297 298void 299sc_module::next_trigger(const sc_event_or_list &eol) 300{ 301 ::sc_core::next_trigger(eol); 302} 303 304void 305sc_module::next_trigger(const sc_event_and_list &eal) 306{ 307 ::sc_core::next_trigger(eal); 308} 309 310void 311sc_module::next_trigger(const sc_time &t) 312{ 313 ::sc_core::next_trigger(t); 314} 315 316void 317sc_module::next_trigger(double d, sc_time_unit u) 318{ 319 ::sc_core::next_trigger(d, u); 320} 321 322void 323sc_module::next_trigger(const sc_time &t, const sc_event &e) 324{ 325 ::sc_core::next_trigger(t, e); 326} 327 328void 329sc_module::next_trigger(double d, sc_time_unit u, const sc_event &e) 330{ 331 ::sc_core::next_trigger(d, u, e); 332} 333 334void 335sc_module::next_trigger(const sc_time &t, const sc_event_or_list &eol) 336{ 337 ::sc_core::next_trigger(t, eol); 338} 339 340void 341sc_module::next_trigger(double d, sc_time_unit u, const sc_event_or_list &eol) 342{ 343 ::sc_core::next_trigger(d, u, eol); 344} 345 346void 347sc_module::next_trigger(const sc_time &t, const sc_event_and_list &eal) 348{ 349 ::sc_core::next_trigger(t, eal); 350} 351 352void 353sc_module::next_trigger(double d, sc_time_unit u, const sc_event_and_list &eal) 354{ 355 ::sc_core::next_trigger(d, u, eal); 356} 357 358 359bool 360sc_module::timed_out() 361{ 362 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 363 return false; 364} 365 366 367void 368sc_module::wait() 369{ 370 ::sc_core::wait(); 371} 372 373void 374sc_module::wait(int i) 375{ 376 ::sc_core::wait(i); 377} 378 379void 380sc_module::wait(const sc_event &e) 381{ 382 ::sc_core::wait(e); 383} 384 385void 386sc_module::wait(const sc_event_or_list &eol) 387{ 388 ::sc_core::wait(eol); 389} 390 391void 392sc_module::wait(const sc_event_and_list &eal) 393{ 394 ::sc_core::wait(eal); 395} 396 397void 398sc_module::wait(const sc_time &t) 399{ 400 ::sc_core::wait(t); 401} 402 403void 404sc_module::wait(double d, sc_time_unit u) 405{ 406 ::sc_core::wait(d, u); 407} 408 409void 410sc_module::wait(const sc_time &t, const sc_event &e) 411{ 412 ::sc_core::wait(t, e); 413} 414 415void 416sc_module::wait(double d, sc_time_unit u, const sc_event &e) 417{ 418 ::sc_core::wait(d, u, e); 419} 420 421void 422sc_module::wait(const sc_time &t, const sc_event_or_list &eol) 423{ 424 ::sc_core::wait(t, eol); 425} 426 427void 428sc_module::wait(double d, sc_time_unit u, const sc_event_or_list &eol) 429{ 430 ::sc_core::wait(d, u, eol); 431} 432 433void 434sc_module::wait(const sc_time &t, const sc_event_and_list &eal) 435{ 436 ::sc_core::wait(t, eal); 437} 438 439void 440sc_module::wait(double d, sc_time_unit u, const sc_event_and_list &eal) 441{ 442 ::sc_core::wait(d, u, eal); 443} 444 445 446void 447sc_module::halt() 448{ 449 ::sc_core::halt(); 450} 451 452void 453sc_module::at_posedge(const sc_signal_in_if<bool> &s) 454{ 455 ::sc_core::at_posedge(s); 456} 457 458void 459sc_module::at_posedge(const sc_signal_in_if<sc_dt::sc_logic> &s) 460{ 461 ::sc_core::at_posedge(s); 462} 463 464void 465sc_module::at_negedge(const sc_signal_in_if<bool> &s) 466{ 467 ::sc_core::at_negedge(s); 468} 469 470void 471sc_module::at_negedge(const sc_signal_in_if<sc_dt::sc_logic> &s) 472{ 473 ::sc_core::at_negedge(s); 474} 475 476 477void 478next_trigger() 479{ 480 sc_gem5::Process *p = sc_gem5::scheduler.current(); 481 p->setDynamic(nullptr); 482} 483 484void 485next_trigger(const sc_event &e) 486{ 487 sc_gem5::Process *p = sc_gem5::scheduler.current(); 488 p->setDynamic(new ::sc_gem5::SensitivityEvent(p, &e)); 489} 490 491void 492next_trigger(const sc_event_or_list &eol) 493{ 494 sc_gem5::Process *p = sc_gem5::scheduler.current(); 495 p->setDynamic(new ::sc_gem5::SensitivityEventOrList(p, &eol)); 496} 497 498void 499next_trigger(const sc_event_and_list &eal) 500{ 501 sc_gem5::Process *p = sc_gem5::scheduler.current(); 502 p->setDynamic(new ::sc_gem5::SensitivityEventAndList(p, &eal)); 503} 504 505void 506next_trigger(const sc_time &t) 507{ 508 sc_gem5::Process *p = sc_gem5::scheduler.current(); 509 p->setDynamic(new ::sc_gem5::SensitivityTimeout(p, t)); 510} 511 512void 513next_trigger(double d, sc_time_unit u) 514{ 515 next_trigger(sc_time(d, u)); 516} 517 518void 519next_trigger(const sc_time &t, const sc_event &e) 520{ 521 sc_gem5::Process *p = sc_gem5::scheduler.current(); 522 p->setDynamic(new ::sc_gem5::SensitivityTimeoutAndEvent(p, t, &e)); 523} 524 525void 526next_trigger(double d, sc_time_unit u, const sc_event &e) 527{ 528 next_trigger(sc_time(d, u), e); 529} 530 531void 532next_trigger(const sc_time &t, const sc_event_or_list &eol) 533{ 534 sc_gem5::Process *p = sc_gem5::scheduler.current(); 535 p->setDynamic( 536 new ::sc_gem5::SensitivityTimeoutAndEventOrList(p, t, &eol)); 537} 538 539void 540next_trigger(double d, sc_time_unit u, const sc_event_or_list &eol) 541{ 542 next_trigger(sc_time(d, u), eol); 543} 544 545void 546next_trigger(const sc_time &t, const sc_event_and_list &eal) 547{ 548 sc_gem5::Process *p = sc_gem5::scheduler.current(); 549 p->setDynamic( 550 new ::sc_gem5::SensitivityTimeoutAndEventAndList(p, t, &eal)); 551} 552 553void 554next_trigger(double d, sc_time_unit u, const sc_event_and_list &eal) 555{ 556 next_trigger(sc_time(d, u), eal); 557} 558 559bool 560timed_out() 561{ 562 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 563 return false; 564} 565 566 567void 568wait() 569{ 570 sc_gem5::Process *p = sc_gem5::scheduler.current(); 571 p->setDynamic(nullptr); 572 sc_gem5::scheduler.yield(); 573} 574 575void 576wait(int n) 577{ 578 if (n <= 0) { 579 std::string msg = csprintf("n = %d", n); 580 SC_REPORT_ERROR("(E525) wait(n) is only valid for n > 0", msg.c_str()); 581 } 582 for (int i = 0; i < n; i++) 583 wait(); 584} 585 586void 587wait(const sc_event &e) 588{ 589 sc_gem5::Process *p = sc_gem5::scheduler.current(); 590 p->setDynamic(new ::sc_gem5::SensitivityEvent(p, &e)); 591 sc_gem5::scheduler.yield(); 592} 593 594void 595wait(const sc_event_or_list &eol) 596{ 597 sc_gem5::Process *p = sc_gem5::scheduler.current(); 598 p->setDynamic(new ::sc_gem5::SensitivityEventOrList(p, &eol)); 599 sc_gem5::scheduler.yield(); 600} 601 602void 603wait(const sc_event_and_list &eal) 604{ 605 sc_gem5::Process *p = sc_gem5::scheduler.current(); 606 p->setDynamic(new ::sc_gem5::SensitivityEventAndList(p, &eal)); 607 sc_gem5::scheduler.yield(); 608} 609 610void 611wait(const sc_time &t) 612{ 613 sc_gem5::Process *p = sc_gem5::scheduler.current(); 614 p->setDynamic(new ::sc_gem5::SensitivityTimeout(p, t)); 615 sc_gem5::scheduler.yield(); 616} 617 618void 619wait(double d, sc_time_unit u) 620{ 621 wait(sc_time(d, u)); 622} 623 624void 625wait(const sc_time &t, const sc_event &e) 626{ 627 sc_gem5::Process *p = sc_gem5::scheduler.current(); 628 p->setDynamic(new ::sc_gem5::SensitivityTimeoutAndEvent(p, t, &e)); 629 sc_gem5::scheduler.yield(); 630} 631 632void 633wait(double d, sc_time_unit u, const sc_event &e) 634{ 635 wait(sc_time(d, u), e); 636} 637 638void 639wait(const sc_time &t, const sc_event_or_list &eol) 640{ 641 sc_gem5::Process *p = sc_gem5::scheduler.current(); 642 p->setDynamic( 643 new ::sc_gem5::SensitivityTimeoutAndEventOrList(p, t, &eol)); 644 sc_gem5::scheduler.yield(); 645} 646 647void 648wait(double d, sc_time_unit u, const sc_event_or_list &eol) 649{ 650 wait(sc_time(d, u), eol); 651} 652 653void 654wait(const sc_time &t, const sc_event_and_list &eal) 655{ 656 sc_gem5::Process *p = sc_gem5::scheduler.current(); 657 p->setDynamic( 658 new ::sc_gem5::SensitivityTimeoutAndEventAndList(p, t, &eal)); 659 sc_gem5::scheduler.yield(); 660} 661 662void 663wait(double d, sc_time_unit u, const sc_event_and_list &eal) 664{ 665 wait(sc_time(d, u), eal); 666} 667 668void 669halt() 670{ 671 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 672} 673 674void 675at_posedge(const sc_signal_in_if<bool> &) 676{ 677 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 678} 679 680void 681at_posedge(const sc_signal_in_if<sc_dt::sc_logic> &) 682{ 683 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 684} 685 686void 687at_negedge(const sc_signal_in_if<bool> &) 688{ 689 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 690} 691 692void 693at_negedge(const sc_signal_in_if<sc_dt::sc_logic> &) 694{ 695 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 696} 697 698const char * 699sc_gen_unique_name(const char *seed) 700{ 701 ::sc_gem5::Module *mod = ::sc_gem5::currentModule(); 702 return mod ? mod->uniqueName(seed) : 703 ::sc_gem5::nameGen.gen(seed); 704} 705 706bool 707sc_hierarchical_name_exists(const char *name) 708{ 709 warn("%s not implemented.\n", __PRETTY_FUNCTION__); 710 return false; 711} 712 713bool 714sc_start_of_simulation_invoked() 715{ 716 return ::sc_gem5::kernel->startOfSimulationComplete(); 717} 718 719bool 720sc_end_of_simulation_invoked() 721{ 722 return ::sc_gem5::kernel->endOfSimulationComplete(); 723} 724 725sc_module * 726sc_module_sc_new(sc_module *mod) 727{ 728 static std::vector<std::unique_ptr<sc_module> > modules; 729 modules.emplace_back(mod); 730 return mod; 731} 732 733} // namespace sc_core 734