Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / verif / model / pcie / pcie_common / tlm / tlm_data_channel.hpp
CommitLineData
86530b38
AT
1// ========== Copyright Header Begin ==========================================
2//
3// OpenSPARC T2 Processor File: tlm_data_channel.hpp
4// Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved
5// 4150 Network Circle, Santa Clara, California 95054, U.S.A.
6//
7// * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8//
9// This program is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; version 2 of the License.
12//
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21//
22// For the avoidance of doubt, and except that if any non-GPL license
23// choice is available it will apply instead, Sun elects to use only
24// the General Public License version 2 (GPLv2) at this time for any
25// software where a choice of GPL license versions is made
26// available with the language indicating that GPLv2 or any later version
27// may be used, or where a choice of which version of the GPL is applied is
28// otherwise unspecified.
29//
30// Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
31// CA 95054 USA or visit www.sun.com if you need additional information or
32// have any questions.
33//
34// ========== Copyright Header End ============================================
35#ifndef INC_tlm_data_channel_hpp__
36#define INC_tlm_data_channel_hpp__
37
38#include <systemc.h>
39
40#include "pcie_common/logger.hpp"
41#include "pcie_common/packet_if.hpp"
42
43namespace pcie {
44
45 USING_NAMESPACE(Logger)
46
47 template < typename DATA >
48 class tlm_data_channel : public packet_if <DATA>, public sc_module
49 {
50 public:
51 /**
52 * This channel constructor is used for reset-able modules. This makes access to the
53 * data channel non-blocking.
54 */
55 tlm_data_channel(sc_event *parent_global_ev, uint8 *global_event_type, int fifo_size = 256) :
56 sc_module( sc_module_name( sc_gen_unique_name("tlm_data_channel") ) ) ,
57 in_reset(false),
58 data_fifo( fifo_size )
59 {
60 this->global_ev = parent_global_ev;
61 this->global_event_type = global_event_type;
62 LOG_INFO << "TLM Data Channel: " << name()
63 << " Init with fifo size " << fifo_size << "\n";
64 SC_THREAD(reset);
65 }
66
67 tlm_data_channel( sc_module_name module_name ,
68 sc_event *parent_global_ev, uint8 *global_event_type, int fifo_size = 256) :
69 sc_module( module_name ) ,
70 in_reset(false),
71 data_fifo( fifo_size )
72 {
73 this->global_ev = parent_global_ev;
74 this->global_event_type = global_event_type;
75 LOG_INFO << "TLM Data Channel: " << name()
76 << " Init with fifo size " << fifo_size << "\n";
77 SC_THREAD(reset);
78 }
79
80 tlm_data_channel( int fifo_size = 256) :
81 sc_module( sc_module_name( sc_gen_unique_name("tlm_data_channel") ) ) ,
82 in_reset(false),
83 data_fifo( fifo_size )
84 {
85 this->global_ev=&dummy;
86 LOG_INFO << "TLM Data Channel: " << name()
87 << " Init with fifo size " << fifo_size << "\n";
88 SC_THREAD(reset);
89 }
90
91 tlm_data_channel( sc_module_name module_name ,
92 int fifo_size = 256) :
93 sc_module( module_name ) ,
94 in_reset(false),
95 data_fifo( fifo_size )
96 {
97 this->global_ev=&dummy;
98 LOG_INFO << "TLM Data Channel: " << name()
99 << " Init with fifo size " << fifo_size << "\n";
100 SC_THREAD(reset);
101 }
102
103 /**
104 * Blocking interface Returns packet from the channel
105 * Throws an exception if the channel is in reset
106 */
107 int get_packet(DATA &p) {
108 if (in_reset) {
109 throw sc_exception();
110 }
111 if (data_fifo.nb_can_get()) {
112 p = data_fifo.get();
113 return 0;
114 } else {
115 wait (data_fifo.ok_to_get() | local_ev);
116 if (in_reset) {
117 throw sc_exception();
118 }
119 p = data_fifo.get();
120 return 0;
121 }
122 return 0;
123 }
124
125 bool get_packet_nb(DATA &p) {
126 if (!in_reset) {
127 return data_fifo.nb_get(p);
128 } else {
129 LOG_DEBUG<<"DEBUG: TLM data channel throwing an exception";
130 throw sc_exception();
131 }
132 return false;
133 }
134
135 bool is_packet_ready() {
136 if (!in_reset) {
137 return data_fifo.nb_can_get();
138 }
139 return false;
140 }
141
142 int send_packet(const DATA &p) {
143 if (in_reset) {
144 throw sc_exception();
145 }
146 if (data_fifo.nb_can_put()) {
147 data_fifo.put(p);
148 return 0;
149 } else {
150 wait (data_fifo.ok_to_put() | local_ev);
151 if (in_reset) {
152 throw sc_exception();
153 }
154 data_fifo.put(p);
155 }
156 return 0;
157 }
158
159 void purge_channel(){
160 while(data_fifo.nb_can_get()) data_fifo.get();
161 }
162
163 SC_HAS_PROCESS( tlm_data_channel );
164
165 protected:
166
167 bool in_reset; ///< flag indicates that this channel is in reset
168 tlm_fifo<DATA> data_fifo;
169 sc_event *global_ev; ///< Global event pointer
170 sc_event local_ev; ///< local reset event to prevent channel blocking
171 uint8 *global_event_type; ///< Global event type
172 sc_event dummy;
173
174 void reset () {
175 while (true) {
176 wait (*global_ev);
177 switch (*global_event_type) {
178
179 case POR_RESET_ENTER:
180 case WMR_RESET_ENTER:
181 LOG_WARNING<<"TLM DATA Channel " << name() << " IN reset";
182 in_reset = true;
183 local_ev.notify();
184 while(data_fifo.nb_can_get()) data_fifo.get();
185 break;
186
187 case SW_RESET_CHNL_EXIT:
188 LOG_INFO<<"TLM DATA Channel " << name() << " OUT reset";
189 local_ev.notify();
190 in_reset = false;
191 break;
192 }
193 }
194 }
195
196 void init () {
197 }
198 };
199}
200#endif