sc_signal.hh revision 13274
112841Sgabeblack@google.com/*
212841Sgabeblack@google.com * Copyright 2018 Google, Inc.
312841Sgabeblack@google.com *
412841Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512841Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612841Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812841Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012841Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112841Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212841Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312841Sgabeblack@google.com * this software without specific prior written permission.
1412841Sgabeblack@google.com *
1512841Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612841Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712841Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812841Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912841Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012841Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112841Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212841Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312841Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412841Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512841Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612841Sgabeblack@google.com *
2712841Sgabeblack@google.com * Authors: Gabe Black
2812841Sgabeblack@google.com */
2912841Sgabeblack@google.com
3012841Sgabeblack@google.com#ifndef __SYSTEMC_EXT_CHANNEL_SC_SIGNAL_HH__
3112841Sgabeblack@google.com#define __SYSTEMC_EXT_CHANNEL_SC_SIGNAL_HH__
3212841Sgabeblack@google.com
3312841Sgabeblack@google.com#include <iostream>
3413274Sgabeblack@google.com#include <sstream>
3512933Sgabeblack@google.com#include <string>
3612933Sgabeblack@google.com#include <vector>
3712841Sgabeblack@google.com
3813044Sgabeblack@google.com#include "../core/sc_event.hh"
3912841Sgabeblack@google.com#include "../core/sc_module.hh" // for sc_gen_unique_name
4012841Sgabeblack@google.com#include "../core/sc_prim.hh"
4113044Sgabeblack@google.com#include "../dt/bit/sc_logic.hh"
4212841Sgabeblack@google.com#include "sc_signal_inout_if.hh"
4312841Sgabeblack@google.com
4412841Sgabeblack@google.comnamespace sc_core
4512841Sgabeblack@google.com{
4612841Sgabeblack@google.com
4712841Sgabeblack@google.comclass sc_port_base;
4812841Sgabeblack@google.com
4912841Sgabeblack@google.comtemplate <class T, sc_writer_policy WRITER_POLICY=SC_ONE_WRITER>
5012841Sgabeblack@google.comclass sc_signal : public sc_signal_inout_if<T>,
5112841Sgabeblack@google.com                  public sc_prim_channel
5212841Sgabeblack@google.com{
5312841Sgabeblack@google.com  public:
5412841Sgabeblack@google.com    sc_signal() : sc_signal_inout_if<T>(),
5513044Sgabeblack@google.com                  sc_prim_channel(sc_gen_unique_name("signal")),
5613274Sgabeblack@google.com                  m_cur_val(T()), m_new_val(T()), _changeStamp(~0ULL),
5713274Sgabeblack@google.com                  _gem5Writer(NULL)
5812841Sgabeblack@google.com    {}
5913044Sgabeblack@google.com    explicit sc_signal(const char *name) :
6013044Sgabeblack@google.com        sc_signal_inout_if<T>(), sc_prim_channel(name),
6113274Sgabeblack@google.com        m_cur_val(T()), m_new_val(T()), _changeStamp(~0ULL),
6213274Sgabeblack@google.com        _gem5Writer(NULL)
6312841Sgabeblack@google.com    {}
6412912Sgabeblack@google.com    explicit sc_signal(const char *name, const T &initial_value) :
6513044Sgabeblack@google.com        sc_signal_inout_if<T>(), sc_prim_channel(name),
6613274Sgabeblack@google.com        m_cur_val(initial_value), m_new_val(initial_value),
6713274Sgabeblack@google.com        _changeStamp(~0ULL), _gem5Writer(NULL)
6813044Sgabeblack@google.com    {}
6912841Sgabeblack@google.com    virtual ~sc_signal() {}
7012841Sgabeblack@google.com
7112841Sgabeblack@google.com    virtual void
7213274Sgabeblack@google.com    register_port(sc_port_base &port, const char *iface_type_name)
7312841Sgabeblack@google.com    {
7413274Sgabeblack@google.com        if (WRITER_POLICY == SC_ONE_WRITER &&
7513274Sgabeblack@google.com                std::string(iface_type_name) ==
7613274Sgabeblack@google.com                typeid(sc_signal_inout_if<T>).name()) {
7713274Sgabeblack@google.com            if (_gem5Writer) {
7813274Sgabeblack@google.com                std::ostringstream ss;
7913274Sgabeblack@google.com                ss << "\n signal " << "`" << name() << "' (" <<
8013274Sgabeblack@google.com                    kind() << ")";
8113274Sgabeblack@google.com                ss << "\n first driver `" << _gem5Writer->name() << "'  (" <<
8213274Sgabeblack@google.com                    _gem5Writer->kind() << ")";
8313274Sgabeblack@google.com                ss << "\n second driver `" << port.name() << "' (" <<
8413274Sgabeblack@google.com                    port.kind() << ")";
8513274Sgabeblack@google.com                SC_REPORT_ERROR(
8613274Sgabeblack@google.com                        "(E115) sc_signal<T> cannot have more than one driver",
8713274Sgabeblack@google.com                        ss.str().c_str());
8813274Sgabeblack@google.com            }
8913274Sgabeblack@google.com            _gem5Writer = &port;
9013274Sgabeblack@google.com        }
9112841Sgabeblack@google.com    }
9212841Sgabeblack@google.com
9313044Sgabeblack@google.com    virtual const T &read() const { return m_cur_val; }
9413044Sgabeblack@google.com    operator const T&() const { return read(); }
9512841Sgabeblack@google.com
9612841Sgabeblack@google.com    virtual sc_writer_policy
9712841Sgabeblack@google.com    get_writer_policy() const
9812841Sgabeblack@google.com    {
9912841Sgabeblack@google.com        return WRITER_POLICY;
10012841Sgabeblack@google.com    }
10112841Sgabeblack@google.com    virtual void
10213044Sgabeblack@google.com    write(const T &t)
10312841Sgabeblack@google.com    {
10413044Sgabeblack@google.com        m_new_val = t;
10513044Sgabeblack@google.com        bool changed = !(m_cur_val == m_new_val);
10613044Sgabeblack@google.com        //TODO check whether this write follows the write policy.
10713044Sgabeblack@google.com        if (changed)
10813044Sgabeblack@google.com            request_update();
10912841Sgabeblack@google.com    }
11012841Sgabeblack@google.com    sc_signal<T, WRITER_POLICY> &
11113044Sgabeblack@google.com    operator = (const T &t)
11212841Sgabeblack@google.com    {
11313044Sgabeblack@google.com        write(t);
11412841Sgabeblack@google.com        return *this;
11512841Sgabeblack@google.com    }
11612841Sgabeblack@google.com    sc_signal<T, WRITER_POLICY> &
11713044Sgabeblack@google.com    operator = (const sc_signal<T, WRITER_POLICY> &s)
11812841Sgabeblack@google.com    {
11913044Sgabeblack@google.com        write(s.read());
12012841Sgabeblack@google.com        return *this;
12112841Sgabeblack@google.com    }
12212841Sgabeblack@google.com
12312841Sgabeblack@google.com    virtual const sc_event &
12412841Sgabeblack@google.com    default_event() const
12512841Sgabeblack@google.com    {
12613044Sgabeblack@google.com        return value_changed_event();
12712841Sgabeblack@google.com    }
12812841Sgabeblack@google.com    virtual const sc_event &
12912841Sgabeblack@google.com    value_changed_event() const
13012841Sgabeblack@google.com    {
13113044Sgabeblack@google.com        return _valueChangedEvent;
13212841Sgabeblack@google.com    }
13312841Sgabeblack@google.com    virtual bool
13412841Sgabeblack@google.com    event() const
13512841Sgabeblack@google.com    {
13613141Sgabeblack@google.com        return _changeStamp == ::sc_gem5::getChangeStamp();
13712841Sgabeblack@google.com    }
13812841Sgabeblack@google.com
13913044Sgabeblack@google.com    virtual void print(std::ostream &os=std::cout) const { os << m_cur_val; }
14012841Sgabeblack@google.com    virtual void
14113044Sgabeblack@google.com    dump(std::ostream &os=std::cout) const
14212841Sgabeblack@google.com    {
14313044Sgabeblack@google.com        os << "     name = " << name() << ::std::endl;
14413044Sgabeblack@google.com        os << "    value = " << m_cur_val << ::std::endl;
14513044Sgabeblack@google.com        os << "new value = " << m_new_val << ::std::endl;
14612841Sgabeblack@google.com    }
14712841Sgabeblack@google.com    virtual const char *kind() const { return "sc_signal"; }
14812841Sgabeblack@google.com
14912841Sgabeblack@google.com  protected:
15012841Sgabeblack@google.com    virtual void
15112841Sgabeblack@google.com    update()
15212841Sgabeblack@google.com    {
15313044Sgabeblack@google.com        if (m_new_val == m_cur_val)
15413044Sgabeblack@google.com            return;
15513044Sgabeblack@google.com
15613044Sgabeblack@google.com        m_cur_val = m_new_val;
15713205Sgabeblack@google.com        _signalChange();
15813205Sgabeblack@google.com        _changeStamp = ::sc_gem5::getChangeStamp();
15913205Sgabeblack@google.com        _valueChangedEvent.notify(SC_ZERO_TIME);
16013205Sgabeblack@google.com    }
16113205Sgabeblack@google.com
16213205Sgabeblack@google.com    void
16313205Sgabeblack@google.com    _signalChange()
16413205Sgabeblack@google.com    {
16513141Sgabeblack@google.com        _changeStamp = ::sc_gem5::getChangeStamp();
16613044Sgabeblack@google.com        _valueChangedEvent.notify(SC_ZERO_TIME);
16712841Sgabeblack@google.com    }
16812841Sgabeblack@google.com
16912945Sgabeblack@google.com    // These members which store the current and future value of the signal
17012945Sgabeblack@google.com    // are not specified in the standard but are referred to directly by one
17112945Sgabeblack@google.com    // of the tests.
17212945Sgabeblack@google.com    T m_cur_val;
17312945Sgabeblack@google.com    T m_new_val;
17412945Sgabeblack@google.com
17512841Sgabeblack@google.com  private:
17613044Sgabeblack@google.com    sc_event _valueChangedEvent;
17713141Sgabeblack@google.com    uint64_t _changeStamp;
17813274Sgabeblack@google.com    sc_port_base *_gem5Writer;
17913044Sgabeblack@google.com
18012841Sgabeblack@google.com    // Disabled
18112841Sgabeblack@google.com    sc_signal(const sc_signal<T, WRITER_POLICY> &) :
18212841Sgabeblack@google.com            sc_signal_inout_if<T>(), sc_prim_channel("")
18312841Sgabeblack@google.com    {}
18412841Sgabeblack@google.com};
18512841Sgabeblack@google.com
18612841Sgabeblack@google.comtemplate <class T, sc_writer_policy WRITER_POLICY>
18712841Sgabeblack@google.cominline std::ostream &
18813142Sgabeblack@google.comoperator << (std::ostream &os, const sc_signal<T, WRITER_POLICY> &s)
18912841Sgabeblack@google.com{
19013142Sgabeblack@google.com    os << s.read();
19112841Sgabeblack@google.com    return os;
19212841Sgabeblack@google.com}
19312841Sgabeblack@google.com
19412841Sgabeblack@google.comtemplate <sc_writer_policy WRITER_POLICY>
19512841Sgabeblack@google.comclass sc_signal<bool, WRITER_POLICY> :
19612841Sgabeblack@google.com    public sc_signal_inout_if<bool>, public sc_prim_channel
19712841Sgabeblack@google.com{
19812841Sgabeblack@google.com  public:
19913044Sgabeblack@google.com    sc_signal() : sc_signal_inout_if<bool>(),
20013044Sgabeblack@google.com                  sc_prim_channel(sc_gen_unique_name("signal")),
20113141Sgabeblack@google.com                  m_cur_val(bool()), m_new_val(bool()),
20213274Sgabeblack@google.com                  _changeStamp(~0ULL), _posStamp(~0ULL), _negStamp(~0ULL),
20313274Sgabeblack@google.com                  _gem5Writer(NULL)
20413044Sgabeblack@google.com    {}
20513044Sgabeblack@google.com    explicit sc_signal(const char *name) :
20613044Sgabeblack@google.com        sc_signal_inout_if<bool>(), sc_prim_channel(name),
20713141Sgabeblack@google.com        m_cur_val(bool()), m_new_val(bool()),
20813274Sgabeblack@google.com        _changeStamp(~0ULL), _posStamp(~0ULL), _negStamp(~0ULL),
20913274Sgabeblack@google.com        _gem5Writer(NULL)
21013044Sgabeblack@google.com    {}
21112912Sgabeblack@google.com    explicit sc_signal(const char *name, const bool &initial_value) :
21213044Sgabeblack@google.com        sc_signal_inout_if<bool>(), sc_prim_channel(name),
21313141Sgabeblack@google.com        m_cur_val(initial_value), m_new_val(initial_value),
21413274Sgabeblack@google.com        _changeStamp(~0ULL), _posStamp(~0ULL), _negStamp(~0ULL),
21513274Sgabeblack@google.com        _gem5Writer(NULL)
21613044Sgabeblack@google.com    {}
21713044Sgabeblack@google.com    virtual ~sc_signal() {}
21812841Sgabeblack@google.com
21912841Sgabeblack@google.com    virtual void
22013274Sgabeblack@google.com    register_port(sc_port_base &port, const char *iface_type_name)
22112841Sgabeblack@google.com    {
22213274Sgabeblack@google.com        if (WRITER_POLICY == SC_ONE_WRITER &&
22313274Sgabeblack@google.com                std::string(iface_type_name) ==
22413274Sgabeblack@google.com                typeid(sc_signal_inout_if<bool>).name()) {
22513274Sgabeblack@google.com            if (_gem5Writer) {
22613274Sgabeblack@google.com                std::ostringstream ss;
22713274Sgabeblack@google.com                ss << "\n signal " << "`" << name() << "' (" <<
22813274Sgabeblack@google.com                    kind() << ")";
22913274Sgabeblack@google.com                ss << "\n first driver `" << _gem5Writer->name() << "'  (" <<
23013274Sgabeblack@google.com                    _gem5Writer->kind() << ")";
23113274Sgabeblack@google.com                ss << "\n second driver `" << port.name() << "' (" <<
23213274Sgabeblack@google.com                    port.kind() << ")";
23313274Sgabeblack@google.com                SC_REPORT_ERROR(
23413274Sgabeblack@google.com                        "(E115) sc_signal<T> cannot have more than one driver",
23513274Sgabeblack@google.com                        ss.str().c_str());
23613274Sgabeblack@google.com            }
23713274Sgabeblack@google.com            _gem5Writer = &port;
23813274Sgabeblack@google.com        }
23912841Sgabeblack@google.com    }
24012841Sgabeblack@google.com
24113044Sgabeblack@google.com    virtual const bool &read() const { return m_cur_val; }
24213044Sgabeblack@google.com    operator const bool &() const { return read(); }
24312841Sgabeblack@google.com
24412841Sgabeblack@google.com    virtual sc_writer_policy
24512841Sgabeblack@google.com    get_writer_policy() const
24612841Sgabeblack@google.com    {
24712841Sgabeblack@google.com        return WRITER_POLICY;
24812841Sgabeblack@google.com    }
24912841Sgabeblack@google.com    virtual void
25013044Sgabeblack@google.com    write(const bool &b)
25112841Sgabeblack@google.com    {
25213044Sgabeblack@google.com        m_new_val = b;
25313044Sgabeblack@google.com        bool changed = !(m_cur_val == m_new_val);
25413044Sgabeblack@google.com        //TODO check whether this write follows the write policy.
25513044Sgabeblack@google.com        if (changed)
25613044Sgabeblack@google.com            request_update();
25712841Sgabeblack@google.com    }
25812841Sgabeblack@google.com    sc_signal<bool, WRITER_POLICY> &
25913044Sgabeblack@google.com    operator = (const bool &b)
26012841Sgabeblack@google.com    {
26113044Sgabeblack@google.com        write(b);
26212841Sgabeblack@google.com        return *this;
26312841Sgabeblack@google.com    }
26412841Sgabeblack@google.com    sc_signal<bool, WRITER_POLICY> &
26513044Sgabeblack@google.com    operator = (const sc_signal<bool, WRITER_POLICY> &s)
26612841Sgabeblack@google.com    {
26713044Sgabeblack@google.com        write(s.read());
26812841Sgabeblack@google.com        return *this;
26912841Sgabeblack@google.com    }
27012841Sgabeblack@google.com
27112841Sgabeblack@google.com    virtual const sc_event &
27212841Sgabeblack@google.com    default_event() const
27312841Sgabeblack@google.com    {
27413044Sgabeblack@google.com        return value_changed_event();
27512841Sgabeblack@google.com    }
27612841Sgabeblack@google.com
27712841Sgabeblack@google.com    virtual const sc_event &
27812841Sgabeblack@google.com    value_changed_event() const
27912841Sgabeblack@google.com    {
28013044Sgabeblack@google.com        return _valueChangedEvent;
28112841Sgabeblack@google.com    }
28212841Sgabeblack@google.com    virtual const sc_event &
28312841Sgabeblack@google.com    posedge_event() const
28412841Sgabeblack@google.com    {
28513044Sgabeblack@google.com        return _posedgeEvent;
28612841Sgabeblack@google.com    }
28712841Sgabeblack@google.com    virtual const sc_event &
28812841Sgabeblack@google.com    negedge_event() const
28912841Sgabeblack@google.com    {
29013044Sgabeblack@google.com        return _negedgeEvent;
29112841Sgabeblack@google.com    }
29212841Sgabeblack@google.com
29312841Sgabeblack@google.com    virtual bool
29412841Sgabeblack@google.com    event() const
29512841Sgabeblack@google.com    {
29613141Sgabeblack@google.com        return _changeStamp == ::sc_gem5::getChangeStamp();
29712841Sgabeblack@google.com    }
29812841Sgabeblack@google.com    virtual bool
29912841Sgabeblack@google.com    posedge() const
30012841Sgabeblack@google.com    {
30113141Sgabeblack@google.com        return _posStamp == ::sc_gem5::getChangeStamp();
30212841Sgabeblack@google.com    }
30312841Sgabeblack@google.com    virtual bool
30412841Sgabeblack@google.com    negedge() const
30512841Sgabeblack@google.com    {
30613141Sgabeblack@google.com        return _negStamp == ::sc_gem5::getChangeStamp();
30712841Sgabeblack@google.com    }
30812841Sgabeblack@google.com
30913044Sgabeblack@google.com    virtual void print(std::ostream &os=std::cout) const { os << m_cur_val; }
31012841Sgabeblack@google.com    virtual void
31113044Sgabeblack@google.com    dump(std::ostream &os=std::cout) const
31212841Sgabeblack@google.com    {
31313044Sgabeblack@google.com        os << "     name = " << name() << ::std::endl;
31413044Sgabeblack@google.com        os << "    value = " << m_cur_val << ::std::endl;
31513044Sgabeblack@google.com        os << "new value = " << m_new_val << ::std::endl;
31612841Sgabeblack@google.com    }
31712841Sgabeblack@google.com    virtual const char *kind() const { return "sc_signal"; }
31812841Sgabeblack@google.com
31912841Sgabeblack@google.com  protected:
32012841Sgabeblack@google.com    virtual void
32112841Sgabeblack@google.com    update()
32212841Sgabeblack@google.com    {
32313044Sgabeblack@google.com        if (m_new_val == m_cur_val)
32413044Sgabeblack@google.com            return;
32513044Sgabeblack@google.com
32613044Sgabeblack@google.com        m_cur_val = m_new_val;
32713205Sgabeblack@google.com        _signalChange();
32813141Sgabeblack@google.com        if (m_cur_val) {
32913205Sgabeblack@google.com            _posStamp = ::sc_gem5::getChangeStamp();
33013044Sgabeblack@google.com            _posedgeEvent.notify(SC_ZERO_TIME);
33113141Sgabeblack@google.com        } else {
33213205Sgabeblack@google.com            _negStamp = ::sc_gem5::getChangeStamp();
33313044Sgabeblack@google.com            _negedgeEvent.notify(SC_ZERO_TIME);
33413141Sgabeblack@google.com        }
33512841Sgabeblack@google.com    }
33612841Sgabeblack@google.com
33713205Sgabeblack@google.com    void
33813205Sgabeblack@google.com    _signalChange()
33913205Sgabeblack@google.com    {
34013205Sgabeblack@google.com        _changeStamp = ::sc_gem5::getChangeStamp();
34113205Sgabeblack@google.com        _valueChangedEvent.notify(SC_ZERO_TIME);
34213205Sgabeblack@google.com    }
34313205Sgabeblack@google.com
34413044Sgabeblack@google.com    bool m_cur_val;
34513044Sgabeblack@google.com    bool m_new_val;
34613044Sgabeblack@google.com
34712841Sgabeblack@google.com  private:
34813044Sgabeblack@google.com    sc_event _valueChangedEvent;
34913044Sgabeblack@google.com    sc_event _posedgeEvent;
35013044Sgabeblack@google.com    sc_event _negedgeEvent;
35113044Sgabeblack@google.com
35213141Sgabeblack@google.com    uint64_t _changeStamp;
35313141Sgabeblack@google.com    uint64_t _posStamp;
35413141Sgabeblack@google.com    uint64_t _negStamp;
35513141Sgabeblack@google.com
35613274Sgabeblack@google.com    sc_port_base *_gem5Writer;
35713274Sgabeblack@google.com
35812841Sgabeblack@google.com    // Disabled
35912841Sgabeblack@google.com    sc_signal(const sc_signal<bool, WRITER_POLICY> &) :
36012841Sgabeblack@google.com            sc_signal_inout_if<bool>(), sc_prim_channel("")
36112841Sgabeblack@google.com    {}
36212841Sgabeblack@google.com};
36312841Sgabeblack@google.com
36412841Sgabeblack@google.comtemplate <sc_writer_policy WRITER_POLICY>
36512841Sgabeblack@google.comclass sc_signal<sc_dt::sc_logic, WRITER_POLICY> :
36612841Sgabeblack@google.com    public sc_signal_inout_if<sc_dt::sc_logic>, public sc_prim_channel
36712841Sgabeblack@google.com{
36812841Sgabeblack@google.com  public:
36913044Sgabeblack@google.com    sc_signal() : sc_signal_inout_if<sc_dt::sc_logic>(),
37013044Sgabeblack@google.com                  sc_prim_channel(sc_gen_unique_name("signal")),
37113141Sgabeblack@google.com                  m_cur_val(sc_dt::sc_logic()), m_new_val(sc_dt::sc_logic()),
37213274Sgabeblack@google.com                  _changeStamp(~0ULL), _posStamp(~0ULL), _negStamp(~0ULL),
37313274Sgabeblack@google.com                  _gem5Writer(NULL)
37413044Sgabeblack@google.com    {}
37513044Sgabeblack@google.com    explicit sc_signal(const char *name) :
37613044Sgabeblack@google.com        sc_signal_inout_if<sc_dt::sc_logic>(), sc_prim_channel(name),
37713141Sgabeblack@google.com        m_cur_val(sc_dt::sc_logic()), m_new_val(sc_dt::sc_logic()),
37813274Sgabeblack@google.com        _changeStamp(~0ULL), _posStamp(~0ULL), _negStamp(~0ULL),
37913274Sgabeblack@google.com        _gem5Writer(NULL)
38013044Sgabeblack@google.com    {}
38112912Sgabeblack@google.com    explicit sc_signal(const char *name,
38212912Sgabeblack@google.com            const sc_dt::sc_logic &initial_value) :
38313044Sgabeblack@google.com        sc_signal_inout_if<sc_dt::sc_logic>(), sc_prim_channel(name),
38413141Sgabeblack@google.com        m_cur_val(initial_value), m_new_val(initial_value),
38513274Sgabeblack@google.com        _changeStamp(~0ULL), _posStamp(~0ULL), _negStamp(~0ULL),
38613274Sgabeblack@google.com        _gem5Writer(NULL)
38713044Sgabeblack@google.com    {}
38813044Sgabeblack@google.com    virtual ~sc_signal() {}
38912841Sgabeblack@google.com
39012841Sgabeblack@google.com    virtual void
39113274Sgabeblack@google.com    register_port(sc_port_base &port, const char *iface_type_name)
39212841Sgabeblack@google.com    {
39313274Sgabeblack@google.com        if (WRITER_POLICY == SC_ONE_WRITER &&
39413274Sgabeblack@google.com                std::string(iface_type_name) ==
39513274Sgabeblack@google.com                typeid(sc_signal_inout_if<sc_dt::sc_logic>).name()) {
39613274Sgabeblack@google.com            if (_gem5Writer) {
39713274Sgabeblack@google.com                std::ostringstream ss;
39813274Sgabeblack@google.com                ss << "\n signal " << "`" << name() << "' (" <<
39913274Sgabeblack@google.com                    kind() << ")";
40013274Sgabeblack@google.com                ss << "\n first driver `" << _gem5Writer->name() << "'  (" <<
40113274Sgabeblack@google.com                    _gem5Writer->kind() << ")";
40213274Sgabeblack@google.com                ss << "\n second driver `" << port.name() << "' (" <<
40313274Sgabeblack@google.com                    port.kind() << ")";
40413274Sgabeblack@google.com                SC_REPORT_ERROR(
40513274Sgabeblack@google.com                        "(E115) sc_signal<T> cannot have more than one driver",
40613274Sgabeblack@google.com                        ss.str().c_str());
40713274Sgabeblack@google.com            }
40813274Sgabeblack@google.com            _gem5Writer = &port;
40913274Sgabeblack@google.com        }
41012841Sgabeblack@google.com    }
41112841Sgabeblack@google.com
41213044Sgabeblack@google.com    virtual const sc_dt::sc_logic &read() const { return m_cur_val; }
41313044Sgabeblack@google.com    operator const sc_dt::sc_logic &() const { return read(); }
41412841Sgabeblack@google.com
41512841Sgabeblack@google.com    virtual sc_writer_policy
41612841Sgabeblack@google.com    get_writer_policy() const
41712841Sgabeblack@google.com    {
41812841Sgabeblack@google.com        return WRITER_POLICY;
41912841Sgabeblack@google.com    }
42012841Sgabeblack@google.com    virtual void
42113044Sgabeblack@google.com    write(const sc_dt::sc_logic &l)
42212841Sgabeblack@google.com    {
42313044Sgabeblack@google.com        m_new_val = l;
42413044Sgabeblack@google.com        bool changed = !(m_cur_val == m_new_val);
42513044Sgabeblack@google.com        //TODO check whether this write follows the write policy.
42613044Sgabeblack@google.com        if (changed)
42713044Sgabeblack@google.com            request_update();
42812841Sgabeblack@google.com    }
42912841Sgabeblack@google.com    sc_signal<sc_dt::sc_logic, WRITER_POLICY> &
43013044Sgabeblack@google.com    operator = (const sc_dt::sc_logic &l)
43112841Sgabeblack@google.com    {
43213044Sgabeblack@google.com        write(l);
43312841Sgabeblack@google.com        return *this;
43412841Sgabeblack@google.com    }
43512841Sgabeblack@google.com    sc_signal<sc_dt::sc_logic, WRITER_POLICY> &
43613044Sgabeblack@google.com    operator = (const sc_signal<sc_dt::sc_logic, WRITER_POLICY> &s)
43712841Sgabeblack@google.com    {
43813044Sgabeblack@google.com        write(s.read());
43912841Sgabeblack@google.com        return *this;
44012841Sgabeblack@google.com    }
44112841Sgabeblack@google.com
44212841Sgabeblack@google.com    virtual const sc_event &
44312841Sgabeblack@google.com    default_event() const
44412841Sgabeblack@google.com    {
44513044Sgabeblack@google.com        return value_changed_event();
44612841Sgabeblack@google.com    }
44712841Sgabeblack@google.com
44812841Sgabeblack@google.com    virtual const sc_event &
44912841Sgabeblack@google.com    value_changed_event() const
45012841Sgabeblack@google.com    {
45113044Sgabeblack@google.com        return _valueChangedEvent;
45212841Sgabeblack@google.com    }
45312841Sgabeblack@google.com    virtual const sc_event &
45412841Sgabeblack@google.com    posedge_event() const
45512841Sgabeblack@google.com    {
45613044Sgabeblack@google.com        return _posedgeEvent;
45712841Sgabeblack@google.com    }
45812841Sgabeblack@google.com    virtual const sc_event &
45912841Sgabeblack@google.com    negedge_event() const
46012841Sgabeblack@google.com    {
46113044Sgabeblack@google.com        return _negedgeEvent;
46212841Sgabeblack@google.com    }
46312841Sgabeblack@google.com
46412841Sgabeblack@google.com    virtual bool
46512841Sgabeblack@google.com    event() const
46612841Sgabeblack@google.com    {
46713141Sgabeblack@google.com        return _changeStamp == ::sc_gem5::getChangeStamp();
46812841Sgabeblack@google.com    }
46912841Sgabeblack@google.com    virtual bool
47012841Sgabeblack@google.com    posedge() const
47112841Sgabeblack@google.com    {
47213141Sgabeblack@google.com        return _posStamp == ::sc_gem5::getChangeStamp();
47312841Sgabeblack@google.com    }
47412841Sgabeblack@google.com    virtual bool
47512841Sgabeblack@google.com    negedge() const
47612841Sgabeblack@google.com    {
47713141Sgabeblack@google.com        return _negStamp == ::sc_gem5::getChangeStamp();
47812841Sgabeblack@google.com    }
47912841Sgabeblack@google.com
48013044Sgabeblack@google.com    virtual void print(std::ostream &os=std::cout) const { os << m_cur_val; }
48112841Sgabeblack@google.com    virtual void
48213044Sgabeblack@google.com    dump(std::ostream &os=std::cout) const
48312841Sgabeblack@google.com    {
48413044Sgabeblack@google.com        os << "     name = " << name() << ::std::endl;
48513044Sgabeblack@google.com        os << "    value = " << m_cur_val << ::std::endl;
48613044Sgabeblack@google.com        os << "new value = " << m_new_val << ::std::endl;
48712841Sgabeblack@google.com    }
48812841Sgabeblack@google.com    virtual const char *kind() const { return "sc_signal"; }
48912841Sgabeblack@google.com
49012841Sgabeblack@google.com  protected:
49112841Sgabeblack@google.com    virtual void
49212841Sgabeblack@google.com    update()
49312841Sgabeblack@google.com    {
49413044Sgabeblack@google.com        if (m_new_val == m_cur_val)
49513044Sgabeblack@google.com            return;
49613044Sgabeblack@google.com
49713044Sgabeblack@google.com        m_cur_val = m_new_val;
49813205Sgabeblack@google.com        _signalChange();
49913205Sgabeblack@google.com        if (m_cur_val == sc_dt::SC_LOGIC_1) {
50013205Sgabeblack@google.com            _posStamp = ::sc_gem5::getChangeStamp();
50113205Sgabeblack@google.com            _posedgeEvent.notify(SC_ZERO_TIME);
50213205Sgabeblack@google.com        } else if (m_cur_val == sc_dt::SC_LOGIC_0) {
50313205Sgabeblack@google.com            _negStamp = ::sc_gem5::getChangeStamp();
50413205Sgabeblack@google.com            _negedgeEvent.notify(SC_ZERO_TIME);
50513205Sgabeblack@google.com        }
50613205Sgabeblack@google.com    }
50713205Sgabeblack@google.com
50813205Sgabeblack@google.com    void
50913205Sgabeblack@google.com    _signalChange()
51013205Sgabeblack@google.com    {
51113205Sgabeblack@google.com        _changeStamp = ::sc_gem5::getChangeStamp();
51213044Sgabeblack@google.com        _valueChangedEvent.notify(SC_ZERO_TIME);
51312841Sgabeblack@google.com    }
51412841Sgabeblack@google.com
51513044Sgabeblack@google.com    sc_dt::sc_logic m_cur_val;
51613044Sgabeblack@google.com    sc_dt::sc_logic m_new_val;
51713044Sgabeblack@google.com
51812841Sgabeblack@google.com  private:
51913044Sgabeblack@google.com    sc_event _valueChangedEvent;
52013044Sgabeblack@google.com    sc_event _posedgeEvent;
52113044Sgabeblack@google.com    sc_event _negedgeEvent;
52213044Sgabeblack@google.com
52313141Sgabeblack@google.com    uint64_t _changeStamp;
52413141Sgabeblack@google.com    uint64_t _posStamp;
52513141Sgabeblack@google.com    uint64_t _negStamp;
52613141Sgabeblack@google.com
52713274Sgabeblack@google.com    sc_port_base *_gem5Writer;
52813274Sgabeblack@google.com
52912841Sgabeblack@google.com    // Disabled
53012841Sgabeblack@google.com    sc_signal(const sc_signal<sc_dt::sc_logic, WRITER_POLICY> &) :
53112841Sgabeblack@google.com            sc_signal_inout_if<sc_dt::sc_logic>(), sc_prim_channel("")
53212841Sgabeblack@google.com    {}
53312841Sgabeblack@google.com};
53412841Sgabeblack@google.com
53512841Sgabeblack@google.com} // namespace sc_core
53612841Sgabeblack@google.com
53712841Sgabeblack@google.com#endif  //__SYSTEMC_EXT_CHANNEL_SC_SIGNAL_HH__
538