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