qualcomm/qbox
Loading...
Searching...
No Matches
multiinitiator-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_MULTIINITIATOR_SIGNAL_SOCKET_H
10#define _LIBGSUTILS_PORTS_MULTIINITIATOR_SIGNAL_SOCKET_H
11
12#include <systemc>
13#include <async_event.h>
14#include <scp/report.h>
15#include <mutex>
16#include "sysc/kernel/sc_time.h"
17#include "target-signal-socket.h"
18template <typename T = bool>
19class MultiInitiatorSignalSocket
20 : public sc_core::sc_port<sc_core::sc_signal_inout_if<T>, 0, sc_core::SC_ZERO_OR_MORE_BOUND>,
21 public sc_core::sc_module
22{
23 SCP_LOGGER();
24 std::vector<T> vals;
25 bool vals_valid = false;
26 std::mutex vals_mutex;
28
29public:
30 MultiInitiatorSignalSocket(const char* name = "MultiInitiatorSignalSocket")
31 : sc_core::sc_port<sc_core::sc_signal_inout_if<T>, 0, sc_core::SC_ZERO_OR_MORE_BOUND>(name)
32 , sc_core::sc_module(sc_core::sc_module_name((std::string(name) + "_thread").c_str()))
33 , ev(false)
34 {
35 SCP_DEBUG(())("Constructor");
36 SC_THREAD(writer);
37 }
38 void writer()
39 {
40 while (1) {
41 std::vector<T> vs;
42 {
43 std::lock_guard<std::mutex> lock(vals_mutex);
44 if (vals_valid) {
45 vs.swap(vals);
46 vals_valid = false;
47 }
48 }
49
50 if (vs.empty()) {
51 wait(ev);
52 continue;
53 }
54
55 for (auto v : vs) {
56 for (int i = 0; i < this->size(); i++) {
57 if (auto t = dynamic_cast<TargetSignalSocketProxy<bool>*>(this->operator[](i))) {
58 SCP_DEBUG(())("Writing {} to {}", v, t->get_parent().name());
59 }
60 this->operator[](i)->write(v);
61 }
62 wait(sc_core::SC_ZERO_TIME); // ensure all immediate events have been processed
63 }
64 }
65 }
66 void async_write_vector(const std::vector<T>& vs)
67 {
68 {
69 std::lock_guard<std::mutex> lock(vals_mutex);
70 vals.insert(vals.end(), vs.begin(), vs.end()); // COPY the vector to the end.
71 vals_valid = true;
72 }
73 ev.async_notify();
74 }
75};
76
77#endif
Definition target-signal-socket.h:22
Definition async_event.h:22