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 "systemc/core/process.hh"
31#include "systemc/core/scheduler.hh"
32#include "systemc/ext/channel/sc_signal_resolved.hh"
33#include "systemc/ext/core/sc_module.hh" // for sc_gen_unique_name
34
35namespace sc_core
36{
37
38sc_signal_resolved::sc_signal_resolved() : sc_interface(),
39        sc_signal<sc_dt::sc_logic, SC_MANY_WRITERS>(
40                sc_gen_unique_name("signal_resolved"))
41{}
42
43sc_signal_resolved::sc_signal_resolved(const char *name) :
44        sc_interface(), sc_signal<sc_dt::sc_logic, SC_MANY_WRITERS>(name)
45{}
46
47sc_signal_resolved::~sc_signal_resolved() {}
48void sc_signal_resolved::register_port(sc_port_base &, const char *) {}
49
50void
51sc_signal_resolved::write(const sc_dt::sc_logic &l)
52{
53    ::sc_gem5::Process *p = ::sc_gem5::scheduler.current();
54
55    auto it = inputs.find(p);
56    if (it == inputs.end()) {
57        inputs.emplace(p, l);
58        request_update();
59    } else if (it->second != l) {
60        it->second = l;
61        request_update();
62    }
63}
64
65sc_signal_resolved &
66sc_signal_resolved::operator = (const sc_dt::sc_logic &l)
67{
68    write(l);
69    return *this;
70}
71
72sc_signal_resolved &
73sc_signal_resolved::operator = (const sc_signal_resolved &r)
74{
75    write(r.read());
76    return *this;
77}
78
79void
80sc_signal_resolved::update()
81{
82    using sc_dt::Log_0;
83    using sc_dt::Log_1;
84    using sc_dt::Log_Z;
85    using sc_dt::Log_X;
86    static sc_dt::sc_logic_value_t merge_table[4][4] = {
87        { Log_0, Log_X, Log_0, Log_X },
88        { Log_X, Log_1, Log_1, Log_X },
89        { Log_0, Log_1, Log_Z, Log_X },
90        { Log_X, Log_X, Log_X, Log_X }
91    };
92
93    // Resolve the inputs, and give the result to the underlying signal class.
94    m_new_val = Log_Z;
95    for (auto &input: inputs)
96        m_new_val = merge_table[m_new_val.value()][input.second.value()];
97
98    // Ask the signal to update it's value.
99    sc_signal<sc_dt::sc_logic, SC_MANY_WRITERS>::update();
100}
101
102} // namespace sc_core
103