113207Sgabeblack@google.com/*
213207Sgabeblack@google.com * Copyright 2018 Google, Inc.
313207Sgabeblack@google.com *
413207Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
513207Sgabeblack@google.com * modification, are permitted provided that the following conditions are
613207Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
713207Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
813207Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
913207Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1013207Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1113207Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1213207Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1313207Sgabeblack@google.com * this software without specific prior written permission.
1413207Sgabeblack@google.com *
1513207Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1613207Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1713207Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1813207Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1913207Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2013207Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2113207Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2213207Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2313207Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2413207Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2513207Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2613207Sgabeblack@google.com *
2713207Sgabeblack@google.com * Authors: Gabe Black
2813207Sgabeblack@google.com */
2913207Sgabeblack@google.com
3013207Sgabeblack@google.com#include "systemc/core/port.hh"
3113207Sgabeblack@google.com
3213293Sgabeblack@google.com#include "base/logging.hh"
3313288Sgabeblack@google.com#include "systemc/core/process.hh"
3413207Sgabeblack@google.com#include "systemc/core/sensitivity.hh"
3513324Sgabeblack@google.com#include "systemc/ext/channel/messages.hh"
3613260Sgabeblack@google.com#include "systemc/ext/channel/sc_signal_in_if.hh"
3713207Sgabeblack@google.com
3813207Sgabeblack@google.comnamespace sc_gem5
3913207Sgabeblack@google.com{
4013207Sgabeblack@google.com
4113207Sgabeblack@google.comvoid
4213207Sgabeblack@google.comPort::finalizePort(StaticSensitivityPort *port)
4313207Sgabeblack@google.com{
4413207Sgabeblack@google.com    for (int i = 0; i < size(); i++)
4513207Sgabeblack@google.com        port->addEvent(&getInterface(i)->default_event());
4613207Sgabeblack@google.com}
4713207Sgabeblack@google.com
4813207Sgabeblack@google.comvoid
4913207Sgabeblack@google.comPort::finalizeFinder(StaticSensitivityFinder *finder)
5013207Sgabeblack@google.com{
5113207Sgabeblack@google.com    for (int i = 0; i < size(); i++)
5213207Sgabeblack@google.com        finder->addEvent(&finder->find(getInterface(i)));
5313207Sgabeblack@google.com}
5413207Sgabeblack@google.com
5513207Sgabeblack@google.comvoid
5613288Sgabeblack@google.comPort::finalizeReset(Reset *reset)
5713260Sgabeblack@google.com{
5813260Sgabeblack@google.com    assert(size() <= 1);
5913260Sgabeblack@google.com    if (size()) {
6013260Sgabeblack@google.com        auto iface =
6113260Sgabeblack@google.com            dynamic_cast<sc_core::sc_signal_in_if<bool> *>(getInterface(0));
6213260Sgabeblack@google.com        assert(iface);
6313288Sgabeblack@google.com        if (!reset->install(iface))
6413288Sgabeblack@google.com            delete reset;
6513260Sgabeblack@google.com    }
6613260Sgabeblack@google.com}
6713260Sgabeblack@google.com
6813260Sgabeblack@google.comvoid
6913207Sgabeblack@google.comPort::sensitive(StaticSensitivityPort *port)
7013207Sgabeblack@google.com{
7113207Sgabeblack@google.com    if (finalized)
7213207Sgabeblack@google.com        finalizePort(port);
7313207Sgabeblack@google.com    else
7413207Sgabeblack@google.com        sensitivities.push_back(new Sensitivity(port));
7513207Sgabeblack@google.com}
7613207Sgabeblack@google.com
7713207Sgabeblack@google.comvoid
7813207Sgabeblack@google.comPort::sensitive(StaticSensitivityFinder *finder)
7913207Sgabeblack@google.com{
8013207Sgabeblack@google.com    if (finalized)
8113207Sgabeblack@google.com        finalizeFinder(finder);
8213207Sgabeblack@google.com    else
8313207Sgabeblack@google.com        sensitivities.push_back(new Sensitivity(finder));
8413207Sgabeblack@google.com}
8513207Sgabeblack@google.com
8613207Sgabeblack@google.comvoid
8713288Sgabeblack@google.comPort::addReset(Reset *reset)
8813260Sgabeblack@google.com{
8913260Sgabeblack@google.com    if (finalized)
9013260Sgabeblack@google.com        finalizeReset(reset);
9113260Sgabeblack@google.com    else
9213288Sgabeblack@google.com        resets.push_back(reset);
9313260Sgabeblack@google.com}
9413260Sgabeblack@google.com
9513260Sgabeblack@google.comvoid
9613207Sgabeblack@google.comPort::finalize()
9713207Sgabeblack@google.com{
9813207Sgabeblack@google.com    if (finalized)
9913207Sgabeblack@google.com        return;
10013207Sgabeblack@google.com    finalized = true;
10113207Sgabeblack@google.com
10213207Sgabeblack@google.com    for (auto &b: bindings) {
10313207Sgabeblack@google.com        if (b->interface) {
10413207Sgabeblack@google.com            addInterface(b->interface);
10513207Sgabeblack@google.com        } else {
10613207Sgabeblack@google.com            b->port->_gem5Port->finalize();
10713207Sgabeblack@google.com            addInterfaces(b->port);
10813207Sgabeblack@google.com        }
10913207Sgabeblack@google.com        delete b;
11013207Sgabeblack@google.com    }
11113207Sgabeblack@google.com
11213207Sgabeblack@google.com    bindings.clear();
11313207Sgabeblack@google.com
11413207Sgabeblack@google.com    for (auto &s: sensitivities) {
11513207Sgabeblack@google.com        if (s->port)
11613207Sgabeblack@google.com            finalizePort(s->port);
11713288Sgabeblack@google.com        else
11813260Sgabeblack@google.com            finalizeFinder(s->finder);
11913207Sgabeblack@google.com        delete s;
12013207Sgabeblack@google.com    }
12113207Sgabeblack@google.com
12213207Sgabeblack@google.com    sensitivities.clear();
12313288Sgabeblack@google.com
12413288Sgabeblack@google.com    for (auto &r: resets)
12513288Sgabeblack@google.com        finalizeReset(r);
12613288Sgabeblack@google.com
12713288Sgabeblack@google.com    resets.clear();
12813293Sgabeblack@google.com
12913320Sgabeblack@google.com    if (size() > maxSize()) {
13013320Sgabeblack@google.com        std::ostringstream ss;
13113320Sgabeblack@google.com        ss << size() << " binds exceeds maximum of " << maxSize() <<
13213320Sgabeblack@google.com            " allowed";
13313324Sgabeblack@google.com        portBase->report_error(sc_core::SC_ID_COMPLETE_BINDING_,
13413324Sgabeblack@google.com                ss.str().c_str());
13513320Sgabeblack@google.com    }
13613320Sgabeblack@google.com
13713293Sgabeblack@google.com    switch (portBase->_portPolicy()) {
13813293Sgabeblack@google.com      case sc_core::SC_ONE_OR_MORE_BOUND:
13913293Sgabeblack@google.com        if (size() == 0)
14013324Sgabeblack@google.com            portBase->report_error(sc_core::SC_ID_COMPLETE_BINDING_,
14113324Sgabeblack@google.com                    "port not bound");
14213293Sgabeblack@google.com        break;
14313293Sgabeblack@google.com      case sc_core::SC_ALL_BOUND:
14413293Sgabeblack@google.com        if (size() < maxSize() || size() < 1) {
14513293Sgabeblack@google.com            std::stringstream ss;
14613293Sgabeblack@google.com            ss << size() << " actual binds is less than required " <<
14713293Sgabeblack@google.com                maxSize();
14813324Sgabeblack@google.com            portBase->report_error(sc_core::SC_ID_COMPLETE_BINDING_,
14913324Sgabeblack@google.com                    ss.str().c_str());
15013293Sgabeblack@google.com        }
15113293Sgabeblack@google.com        break;
15213293Sgabeblack@google.com      case sc_core::SC_ZERO_OR_MORE_BOUND:
15313293Sgabeblack@google.com        break;
15413293Sgabeblack@google.com      default:
15513293Sgabeblack@google.com        panic("Unrecognized port policy %d.", portBase->_portPolicy());
15613293Sgabeblack@google.com    }
15713207Sgabeblack@google.com}
15813207Sgabeblack@google.com
15913273Sgabeblack@google.comvoid
16013273Sgabeblack@google.comPort::regPort()
16113273Sgabeblack@google.com{
16213273Sgabeblack@google.com    if (!regPortNeeded)
16313273Sgabeblack@google.com        return;
16413273Sgabeblack@google.com
16513273Sgabeblack@google.com    for (int i = 0; i < size(); i++)
16613273Sgabeblack@google.com        getInterface(i)->register_port(*portBase, portBase->_ifTypeName());
16713273Sgabeblack@google.com}
16813273Sgabeblack@google.com
16913207Sgabeblack@google.comstd::list<Port *> allPorts;
17013207Sgabeblack@google.com
17113207Sgabeblack@google.com} // namespace sc_gem5
172