quic/qbox
Loading...
Searching...
No Matches
target-signal-socket.h
1/*
2 * This file is part of libgsutils
3 * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
4 * Author: GreenSocs 2022
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9#ifndef _LIBGSUTILS_PORTS_TARGET_SIGNAL_SOCKET_H
10#define _LIBGSUTILS_PORTS_TARGET_SIGNAL_SOCKET_H
11
12#include <functional>
13#include <cassert>
14
15#include <systemc>
16
17template <class T>
19
20template <class T>
21class TargetSignalSocketProxy : public sc_core::sc_signal_inout_if<T>
22{
23public:
24 using Iface = sc_core::sc_signal_inout_if<T>;
25 using ValueChangedCallback = std::function<void(const T&)>;
26
27protected:
28 TargetSignalSocket<T>& m_parent;
29
30 T m_val;
31 ValueChangedCallback m_cb;
32 sc_core::sc_event m_ev;
33
34public:
35 TargetSignalSocketProxy(TargetSignalSocket<T>& parent): m_parent{ parent }, m_val{}, m_cb{ nullptr }, m_ev{} {}
36
37 void register_value_changed_cb(const ValueChangedCallback& cb) { m_cb = cb; }
38
39 TargetSignalSocket<T>& get_parent() { return m_parent; }
40
41 void notify() { m_ev.notify(); }
42
43 /* SystemC interfaces */
44
45 /* sc_core::sc_signal_in_if<T> */
46 virtual const sc_core::sc_event& default_event() const { return m_ev; }
47
48 virtual const sc_core::sc_event& value_changed_event() const { return m_ev; }
49
50 virtual const T& read() const { return m_val; }
51
52 virtual const T& get_data_ref() const { return m_val; }
53
54 virtual bool event() const
55 {
56 /* Not implemented */
57 assert(false);
58 return false;
59 }
60
61 /* sc_core::sc_signal_write_if<T> */
62 virtual void write(const T& val)
63 {
64 bool changed = (val != m_val);
65
66 m_val = val;
67
68 if (m_cb) {
69 m_cb(val);
70 }
71
72 if (changed) {
73 m_ev.notify();
74 }
75 }
76};
77
78template <>
79class TargetSignalSocketProxy<bool> : public sc_core::sc_signal_inout_if<bool>
80{
81public:
82 using Iface = sc_core::sc_signal_inout_if<bool>;
83 using ValueChangedCallback = std::function<void(const bool&)>;
84
85protected:
87
88 bool m_val;
89 ValueChangedCallback m_cb;
90 sc_core::sc_event m_ev;
91 sc_core::sc_event m_posedge_ev;
92 sc_core::sc_event m_negedge_ev;
93
94public:
96 : m_parent{ parent }, m_val{ false }, m_cb{ nullptr }, m_ev{}
97 {
98 }
99
100 void register_value_changed_cb(const ValueChangedCallback& cb) { m_cb = cb; }
101
102 TargetSignalSocket<bool>& get_parent() { return m_parent; }
103
104 void notify() { m_ev.notify(); }
105
106 /* SystemC interfaces */
107
108 /* sc_core::sc_signal_in_if<bool> */
109 virtual const sc_core::sc_event& default_event() const { return m_ev; }
110
111 virtual const sc_core::sc_event& value_changed_event() const { return m_ev; }
112
113 virtual const sc_core::sc_event& posedge_event() const { return m_posedge_ev; }
114
115 virtual const sc_core::sc_event& negedge_event() const { return m_negedge_ev; }
116
117 virtual const bool& read() const { return m_val; }
118
119 virtual const bool& get_data_ref() const { return m_val; }
120
121 virtual bool event() const
122 {
123 /* Not implemented */
124 assert(false);
125 return false;
126 }
127
128 virtual bool posedge() const
129 {
130 /* Not implemented */
131 assert(false);
132 return false;
133 }
134
135 virtual bool negedge() const
136 {
137 /* Not implemented */
138 assert(false);
139 return false;
140 }
141
142 /* sc_core::sc_signal_write_if<bool> */
143 virtual void write(const bool& val)
144 {
145 bool changed = (val != m_val);
146 m_val = val;
147
148 if (m_cb) {
149 m_cb(val);
150 }
151
152 if (changed) {
153 m_ev.notify();
154 val ? m_posedge_ev.notify() : m_negedge_ev.notify();
155 }
156 }
157};
158
159/* TODO: TargetSignalSocketProxy specialization for sc_dt::sc_logic type */
160
161template <class T>
162class TargetSignalSocket : public sc_core::sc_export<sc_core::sc_signal_inout_if<T> >
163{
164public:
165 using Iface = typename TargetSignalSocketProxy<T>::Iface;
166 using Parent = sc_core::sc_export<Iface>;
167 using ValueChangedCallback = typename TargetSignalSocketProxy<T>::ValueChangedCallback;
168
169protected:
171
172public:
173 TargetSignalSocket(const char* name): Parent(name), m_proxy(*this) { Parent::bind(m_proxy); }
174
175 void register_value_changed_cb(const ValueChangedCallback& cb) { m_proxy.register_value_changed_cb(cb); }
176
177 const T& read() const { return m_proxy.read(); }
178};
179
180#endif
Definition target.h:160
Definition target-signal-socket.h:22
Definition target-signal-socket.h:163