module.hh revision 13268
112863Sgabeblack@google.com/*
212863Sgabeblack@google.com * Copyright 2018 Google, Inc.
312863Sgabeblack@google.com *
412863Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512863Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612863Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712863Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812863Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912863Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012863Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112863Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212863Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312863Sgabeblack@google.com * this software without specific prior written permission.
1412863Sgabeblack@google.com *
1512863Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612863Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712863Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812863Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912863Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012863Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112863Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212863Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312863Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412863Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512863Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612863Sgabeblack@google.com *
2712863Sgabeblack@google.com * Authors: Gabe Black
2812863Sgabeblack@google.com */
2912863Sgabeblack@google.com
3012863Sgabeblack@google.com#ifndef __SYSTEMC_CORE_MODULE_HH__
3112863Sgabeblack@google.com#define __SYSTEMC_CORE_MODULE_HH__
3212863Sgabeblack@google.com
3312950Sgabeblack@google.com#include <cassert>
3413046Sgabeblack@google.com#include <list>
3513035Sgabeblack@google.com#include <map>
3613035Sgabeblack@google.com#include <sstream>
3713035Sgabeblack@google.com#include <string>
3813053Sgabeblack@google.com#include <vector>
3912950Sgabeblack@google.com
4012950Sgabeblack@google.com#include "systemc/core/object.hh"
4112950Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh"
4212950Sgabeblack@google.com
4313053Sgabeblack@google.comnamespace sc_core
4413053Sgabeblack@google.com{
4513053Sgabeblack@google.com
4613053Sgabeblack@google.comclass sc_port_base;
4713059Sgabeblack@google.comclass sc_export_base;
4813053Sgabeblack@google.com
4913053Sgabeblack@google.com} // namespace sc_core
5013053Sgabeblack@google.com
5112950Sgabeblack@google.comnamespace sc_gem5
5212863Sgabeblack@google.com{
5312863Sgabeblack@google.com
5413035Sgabeblack@google.comclass UniqueNameGen
5513035Sgabeblack@google.com{
5613035Sgabeblack@google.com  private:
5713035Sgabeblack@google.com    std::map<std::string, int> counts;
5813035Sgabeblack@google.com    std::string buf;
5913035Sgabeblack@google.com
6013035Sgabeblack@google.com  public:
6113035Sgabeblack@google.com    const char *
6213035Sgabeblack@google.com    gen(std::string seed)
6313035Sgabeblack@google.com    {
6413035Sgabeblack@google.com        std::ostringstream os;
6513035Sgabeblack@google.com        os << seed << "_" << counts[seed]++;
6613035Sgabeblack@google.com        buf = os.str();
6713035Sgabeblack@google.com        return buf.c_str();
6813035Sgabeblack@google.com    }
6913035Sgabeblack@google.com};
7013035Sgabeblack@google.com
7112863Sgabeblack@google.comclass Module
7212863Sgabeblack@google.com{
7312863Sgabeblack@google.com  private:
7412863Sgabeblack@google.com    const char *_name;
7512950Sgabeblack@google.com    sc_core::sc_module *_sc_mod;
7612950Sgabeblack@google.com    Object *_obj;
7713191Sgabeblack@google.com    bool _ended;
7813191Sgabeblack@google.com    bool _deprecatedConstructor;
7912863Sgabeblack@google.com
8013035Sgabeblack@google.com    UniqueNameGen nameGen;
8113035Sgabeblack@google.com
8212863Sgabeblack@google.com  public:
8312950Sgabeblack@google.com    Module(const char *name);
8412982Sgabeblack@google.com    ~Module();
8512982Sgabeblack@google.com
8613268Sgabeblack@google.com    static Module *
8713268Sgabeblack@google.com    fromScModule(::sc_core::sc_module *mod)
8813268Sgabeblack@google.com    {
8913268Sgabeblack@google.com        return mod->_gem5_module;
9013268Sgabeblack@google.com    }
9113268Sgabeblack@google.com
9212950Sgabeblack@google.com    void finish(Object *this_obj);
9312863Sgabeblack@google.com
9412950Sgabeblack@google.com    const char *name() const { return _name; }
9513191Sgabeblack@google.com    void endModule() { _ended = true; }
9613191Sgabeblack@google.com    void deprecatedConstructor() { _deprecatedConstructor = true; }
9712950Sgabeblack@google.com
9812950Sgabeblack@google.com    sc_core::sc_module *
9912950Sgabeblack@google.com    sc_mod() const
10012950Sgabeblack@google.com    {
10112950Sgabeblack@google.com        assert(_sc_mod);
10212950Sgabeblack@google.com        return _sc_mod;
10312950Sgabeblack@google.com    }
10412950Sgabeblack@google.com
10512950Sgabeblack@google.com    void
10612950Sgabeblack@google.com    sc_mod(sc_core::sc_module *sc_mod)
10712950Sgabeblack@google.com    {
10812950Sgabeblack@google.com        assert(!_sc_mod);
10912950Sgabeblack@google.com        _sc_mod = sc_mod;
11012950Sgabeblack@google.com    }
11112950Sgabeblack@google.com
11212950Sgabeblack@google.com    Object *
11312950Sgabeblack@google.com    obj()
11412950Sgabeblack@google.com    {
11512950Sgabeblack@google.com        assert(_obj);
11612950Sgabeblack@google.com        return _obj;
11712950Sgabeblack@google.com    }
11812950Sgabeblack@google.com
11912863Sgabeblack@google.com    void pop();
12013035Sgabeblack@google.com
12113035Sgabeblack@google.com    const char *uniqueName(const char *seed) { return nameGen.gen(seed); }
12213053Sgabeblack@google.com
12313091Sgabeblack@google.com    void bindPorts(std::vector<const ::sc_core::sc_bind_proxy *> &proxies);
12413091Sgabeblack@google.com
12513053Sgabeblack@google.com    std::vector<::sc_core::sc_port_base *> ports;
12613059Sgabeblack@google.com    std::vector<::sc_core::sc_export_base *> exports;
12713191Sgabeblack@google.com
12813191Sgabeblack@google.com    void beforeEndOfElaboration();
12913191Sgabeblack@google.com    void endOfElaboration();
13013191Sgabeblack@google.com    void startOfSimulation();
13113191Sgabeblack@google.com    void endOfSimulation();
13212863Sgabeblack@google.com};
13312863Sgabeblack@google.com
13412950Sgabeblack@google.comModule *currentModule();
13513079Sgabeblack@google.comModule *newModuleChecked();
13612950Sgabeblack@google.comModule *newModule();
13712863Sgabeblack@google.com
13813268Sgabeblack@google.comstatic inline Module *
13913268Sgabeblack@google.compickParentModule()
14013268Sgabeblack@google.com{
14113268Sgabeblack@google.com    ::sc_core::sc_object *obj = pickParentObj();
14213268Sgabeblack@google.com    auto mod = dynamic_cast<::sc_core::sc_module *>(obj);
14313268Sgabeblack@google.com    if (!mod)
14413268Sgabeblack@google.com        return nullptr;
14513268Sgabeblack@google.com    return Module::fromScModule(mod);
14613268Sgabeblack@google.com}
14713268Sgabeblack@google.comstatic inline void
14813268Sgabeblack@google.compushParentModule(Module *m)
14913268Sgabeblack@google.com{
15013268Sgabeblack@google.com    pushParentObj(m->obj()->sc_obj());
15113268Sgabeblack@google.com}
15213268Sgabeblack@google.comstatic inline void
15313268Sgabeblack@google.compopParentModule()
15413268Sgabeblack@google.com{
15513268Sgabeblack@google.com    assert(pickParentModule());
15613268Sgabeblack@google.com    popParentObj();
15713268Sgabeblack@google.com}
15813045Sgabeblack@google.com
15913046Sgabeblack@google.comextern std::list<Module *> allModules;
16012982Sgabeblack@google.com
16112863Sgabeblack@google.com} // namespace sc_gem5
16212863Sgabeblack@google.com
16312863Sgabeblack@google.com#endif  //__SYSTEMC_CORE_MODULE_HH__
164