module.hh revision 13053:a7a320144bc1
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_CORE_MODULE_HH__
31#define __SYSTEMC_CORE_MODULE_HH__
32
33#include <cassert>
34#include <list>
35#include <map>
36#include <sstream>
37#include <string>
38#include <vector>
39
40#include "systemc/core/object.hh"
41#include "systemc/ext/core/sc_module.hh"
42
43namespace sc_core
44{
45
46class sc_port_base;
47
48} // namespace sc_core
49
50namespace sc_gem5
51{
52
53class UniqueNameGen
54{
55  private:
56    std::map<std::string, int> counts;
57    std::string buf;
58
59  public:
60    const char *
61    gen(std::string seed)
62    {
63        std::ostringstream os;
64        os << seed << "_" << counts[seed]++;
65        buf = os.str();
66        return buf.c_str();
67    }
68};
69
70class Module
71{
72  private:
73    const char *_name;
74    sc_core::sc_module *_sc_mod;
75    Object *_obj;
76
77    UniqueNameGen nameGen;
78
79  public:
80
81    Module(const char *name);
82    ~Module();
83
84    void finish(Object *this_obj);
85
86    const char *name() const { return _name; }
87
88    sc_core::sc_module *
89    sc_mod() const
90    {
91        assert(_sc_mod);
92        return _sc_mod;
93    }
94
95    void
96    sc_mod(sc_core::sc_module *sc_mod)
97    {
98        assert(!_sc_mod);
99        _sc_mod = sc_mod;
100    }
101
102    Object *
103    obj()
104    {
105        assert(_obj);
106        return _obj;
107    }
108
109    void pop();
110
111    const char *uniqueName(const char *seed) { return nameGen.gen(seed); }
112
113    std::vector<::sc_core::sc_port_base *> ports;
114};
115
116Module *currentModule();
117Module *newModule();
118
119void callbackModule(Module *m);
120Module *callbackModule();
121
122extern std::list<Module *> allModules;
123
124} // namespace sc_gem5
125
126#endif  //__SYSTEMC_CORE_MODULE_HH__
127