-
Notifications
You must be signed in to change notification settings - Fork 0
/
electronicpacket_interface.cpp
201 lines (163 loc) · 5.67 KB
/
electronicpacket_interface.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
/*
* electronicpacket_interface.cpp
* PhoenixSim
*
* Created by Johnnie Chan on 7/8/11.
* Copyright 2011 Johnnie Chan. All rights reserved.
*
*/
#include "electronicpacket_interface.h"
#include <stdexcept>
using namespace std;
using namespace PhoenixSim;
ElectronicPacket_Interface::ElectronicPacket_Interface(sc_module_name name, sc_time p_clockPeriod, int p_bufferWidth, int p_bufferDepth, int p_maxPacketSize)
:Source_Interface(name, 1, 1, p_bufferWidth), maxPacketSize(p_maxPacketSize), clockPeriod(p_clockPeriod), bufferWidth(p_bufferWidth), requestSuccessful(false),
currentMessage(NULL), currentPacket(NULL), remainingData(0), creditsRemaining(p_bufferDepth), bufferDepth(p_bufferDepth), receivingMessage(NULL), transmissionComplete(false)
{
creditReceivedOptions = new sc_spawn_options;
creditReceivedOptions->spawn_method();
creditReceivedOptions->dont_initialize();
creditReceivedOptions->set_sensitivity(&(creditInputPort[0]->value_changed()));
sc_spawn( sc_bind(&ElectronicPacket_Interface::CreditReceived, this, 0) , "module_credit_received" , creditReceivedOptions);
networkMessageReceivedOptions = new sc_spawn_options;
networkMessageReceivedOptions->spawn_method();
networkMessageReceivedOptions->dont_initialize();
networkMessageReceivedOptions->set_sensitivity(&(inputPort[0]->value_changed()));
sc_spawn( sc_bind(&ElectronicPacket_Interface::NetworkMessageReceived, this, 0) , "module_networkmessage_received" , networkMessageReceivedOptions);
processorDataReceivedOptions = new sc_spawn_options;
processorDataReceivedOptions->spawn_method();
processorDataReceivedOptions->dont_initialize();
processorDataReceivedOptions->set_sensitivity(&(processorDataInputPort[0]->value_changed()));
sc_spawn( sc_bind(&ElectronicPacket_Interface::ProcessorDataReceived, this, 0) , "module_processordata_received" , processorDataReceivedOptions);
SC_METHOD(Controller);
dont_initialize();
sensitive << controllerEvent;
}
ElectronicPacket_Interface::~ElectronicPacket_Interface()
{
}
void ElectronicPacket_Interface::NetworkMessageReceived(int inputPortId)
{
if(inputPort[inputPortId]->read() != NULL)
{
if(receivingMessage == NULL)
{
receivingMessage = inputPort[inputPortId]->read();
}
else if(receivingMessage != inputPort[inputPortId]->read())
{
creditOutputPort[inputPortId]->write(ceil(receivingMessage->messageSize / double(bufferWidth)));
if(receivingMessage->messageComplete)
{
processorDataOutputPort[0]->write(receivingMessage);
}
else
{
delete receivingMessage;
}
receivingMessage = inputPort[inputPortId]->read();
}
}
else
{
if(receivingMessage != NULL)
{
creditOutputPort[inputPortId]->write(ceil(receivingMessage->messageSize / double(bufferWidth)));
if(receivingMessage->messageComplete)
{
processorDataOutputPort[0]->write(receivingMessage);
}
else
{
delete receivingMessage;
}
receivingMessage = NULL;
}
}
}
void ElectronicPacket_Interface::CreditReceived(int inputPortId)
{
creditsRemaining += creditInputPort[inputPortId]->read();
controllerEvent.notify(clockPeriod);
assert(bufferDepth >= creditsRemaining);
//std::cout<<this->name()<<" adding credits "<<creditsRemaining<<endl;
}
void ElectronicPacket_Interface::ProcessorDataReceived(int inputPortId)
{
if(processorDataInputPort[inputPortId]->read() != NULL)
{
messageQueue.push(processorDataInputPort[inputPortId]->read());
if(messageQueue.size() > 100000)
{
throw runtime_error("message Queue reached 100000 messages, time to quit.");
}
controllerEvent.notify(clockPeriod);
}
}
void ElectronicPacket_Interface::Controller()
{
if(requestSuccessful || Request())
{
if(Prepare())
{
channelReadyTime = Send() + sc_time_stamp();
controllerEvent.notify(channelReadyTime - sc_time_stamp());
}
else if(transmissionComplete)
{
Complete();
}
}
}
bool ElectronicPacket_Interface::Request()
{
if(currentMessage == NULL && messageQueue.size() > 0)
{
currentMessage = messageQueue.front();
messageQueue.pop();
remainingData = currentMessage->messageSize;
requestSuccessful = true;
return true;
}
return false;
}
bool ElectronicPacket_Interface::Prepare()
{
if(remainingData > 0 && sc_time_stamp() >= channelReadyTime)
{
// Make sure there is enough room for at least a single bit payload
assert(maxPacketSize - DATA_HEADER_SIZE_ > 0);
// Check if there are currently enough credits
if( ceil(min(maxPacketSize, remainingData + DATA_HEADER_SIZE_) / double(bufferWidth)) > creditsRemaining)
{
return false;
}
currentPacket = new ElectronicMessage;
(*currentPacket) = (*currentMessage);
currentPacket->sequenceId = currentSequenceId++;
currentPacket->messageComplete = remainingData <= maxPacketSize - DATA_HEADER_SIZE_;
currentPacket->messageSize = min(maxPacketSize, remainingData + DATA_HEADER_SIZE_);
return true;
}
return false;
}
sc_time ElectronicPacket_Interface::Send()
{
sc_time waitTime = ceil(currentPacket->messageSize / double(bufferWidth)) * clockPeriod;
creditsRemaining -= ceil(currentPacket->messageSize / double(bufferWidth));
assert(creditsRemaining >= 0);
remainingData -= currentPacket->messageSize - DATA_HEADER_SIZE_;
transmissionComplete = remainingData == 0;
outputPort[0]->write(currentPacket);
currentPacket = NULL;
return waitTime;
}
void ElectronicPacket_Interface::Complete()
{
transmissionComplete = false;
//std::cout<<sc_time_stamp()<<" "<<this->name()<<" whole message sent in "<< currentSequenceId <<" pieces" << currentMessage->sourceAddress <<"-->"<<currentMessage->destinationAddress <<endl;
currentMessage = NULL;
requestSuccessful = false;
currentSequenceId = 0;
controllerEvent.notify(clockPeriod);
}