-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.cpp
206 lines (169 loc) · 4.55 KB
/
hub.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "GlobalTypeDefs.h"
#include "processingelement.h"
#include "hub.h"
//---------------------------------------------------------------------------
void THub::rxProcess()
{
if (reset.read())
{
rx_cycle_count =0;
//Clear outputs and indexes of receiving protocol
for (int i = 0; i < DIRECTIONS+1; i++)
{
ack_rx[i].write(0);
current_level_rx[i] = 0;
}
reservation_table.clear();
routed_flits = 0;
local_drained = 0;
}
else
{
// For each channel decide if a new flit can be accepted
//
// This process simply sees a flow of incoming flits. All arbitration
// and wormhole related issues are addressed in the txProcess()
for (int i = 0; i < DIRECTIONS+1; i++)
{
// To accept a new flit, the following conditions must match:
//
// 1) there is an incoming request
// 2) there is a free slot in the input buffer of direction i
if ((req_rx[i].read() == 1 - current_level_rx[i]) && !buffer[i].IsFull() )
{
rx_cycle_count=0;
TFlit received_flit = flit_rx[i].read();
// Store the incoming flit in to buffer
// if(received_flit.src_id != received_flit.dst_id)
// {
buffer[i].Push(received_flit);
// }
if (TGlobalParams::verbose_mode > VERBOSE_OFF)
{
cout << sc_time_stamp().to_double()/1000 << ": HUBID[" << hub_id << "], Input[" << i << "], Received flit: " << received_flit <<" pendind flits "<<buffer[i].
Size()<< endl;
}
// Negate the old value for Alternating Bit Protocol (ABP)
current_level_rx[i] = 1 - current_level_rx[i];
// Incoming flit
stats.power.Buffering();
}
ack_rx[i].write(current_level_rx[i]);
}
}
stats.power.Leakage();
}
void THub::txProcess()
{
if (reset.read())
{
tx_cycle_count=0;
// Clear outputs and indexes of transmitting protocol
for (int i = 0; i < DIRECTIONS+1; i++)
{
req_tx[i].write(0);
current_level_tx[i] = 0;
}
start_from_port =0;
}
else
{
// 1st phase: Reservation
for (int j = 0; j < (DIRECTIONS+1); j++)
{
int i = (start_from_port + j) % (DIRECTIONS+1);
if (!buffer[i].IsEmpty())
{
TFlit flit = buffer[i].Front();
// if (flit.flit_type == FLIT_TYPE_HEAD)
// {
// prepare data for routing
TRouteData route_data;
route_data.current_id = hub_id;
route_data.src_id = flit.src_id;
route_data.dst_id = flit.dst_id;
route_data.dir_in = i;
int o = route(route_data);
stats.power.Arbitration();
if (reservation_table.isAvailable(o))
{
routed_flits++;
stats.power.Crossbar();
reservation_table.reserve(i, o);
if (TGlobalParams::verbose_mode > VERBOSE_OFF)
{
cout << sc_time_stamp().to_double() / 1000 << ": HUBTX[" << hub_id << "], Input[" << i << "] (" << buffer[i].Size() << " flits)" << ", reserved Output["<< o << "], flit: " << flit << endl;
}
}
// }
}
}
start_from_port++;
// 2nd phase: Forwarding
for (int i = 0; i < DIRECTIONS+1; i++)
{
if (!buffer[i].IsEmpty())
{
TFlit flit = buffer[i].Front();
int o = reservation_table.getOutputPort(i);
if (o != NOT_RESERVED)
{
if (current_level_tx[o] == ack_tx[o].read())
{
flit_tx[o].write(flit);
current_level_tx[o] = 1 - current_level_tx[o];
req_tx[o].write(current_level_tx[o]);
buffer[i].Pop();
if (TGlobalParams::verbose_mode > VERBOSE_OFF)
{
cout << sc_time_stamp().to_double() / 1000 << ": Router[" << hub_id << "], Input[" << i <<"] forward to Output[" << o << "], flit: "<< flit <<" (" << buffer[i].Size() << " flits)"<< endl;
}
stats.power.Link(false);
reservation_table.release(o);
}
}
}
}
}
stats.power.Leakage();
}
int THub::route(const TRouteData & route_data)
{
stats.power.Routing();
int direction = routingFunction(route_data);
return direction;
}
int THub::routingFunction(const TRouteData & route_data)
{
int Port_id_inter = route_data.dst_id - (4 * hub_id); // reduces destination id to local port id
int dir_out;
if(Port_id_inter>=0 && Port_id_inter <= 3)
{
dir_out = Port_id_inter;
}
else
{
dir_out = 4;
}
return dir_out;
}
//---------------------------------------------------------------------------
int THub::selectionRandom(const vector < int >&directions)
{
return directions[rand() % directions.size()];
}
unsigned long THub::getRoutedFlits()
{
return routed_flits;
}
unsigned int THub::getFlitsCount()
{
unsigned count = 0;
for (int i = 0; i < DIRECTIONS; i++)
count += buffer[i].Size();
return count;
}
double THub::getPower()
{
return stats.power.getPower();
}