-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.cpp
105 lines (83 loc) · 2.63 KB
/
processor.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
/*
* processor.cpp
* PhoenixSim
*
* Created by Johnnie Chan on 7/11/11.
* Copyright 2011 Johnnie Chan. All rights reserved.
*
*/
#include "processor.h"
#include "trafficgeneratorfactory.h"
using namespace std;
using namespace PhoenixSim;
Average_StatisticsMachine Processor::networkLatency;
Single_StatisticsMachine Processor::lastMessageTime;
bool Processor::once = false;
Processor::Processor(sc_module_name name, string p_generator, TrafficGeneratorParameters* p_parameters)
{
inputPort = new sc_in<ElectronicMessage*>("input");
outputPort = new sc_out<ElectronicMessage*>("output");
trafficGenerator = TrafficGeneratorFactory::Instance()->CreateTrafficGenerator(p_generator);
trafficGenerator->SetTrafficGeneratorParameters(p_parameters);
SC_METHOD(Initialize);
SC_METHOD(SendMessage);
dont_initialize();
sensitive << sendMessageEvent;
SC_METHOD(MessageReceived);
dont_initialize();
sensitive << inputPort->value_changed();
}
Processor::~Processor()
{
if(!once)
{
once = true;
std::cout<<"Average Latency: "<<networkLatency.ComputeValue()<<endl;
std::cout<<"Samples: "<<networkLatency.GetCount()<<endl;
std::cout<<"Simulation Time: "<<lastMessageTime.ComputeValue()<<endl;
}
delete inputPort;
delete outputPort;
}
void Processor::Initialize()
{
ElectronicMessage* firstMessage = trafficGenerator->CreateFirstMessage();
if(firstMessage != NULL)
{
sendMessageEvent.notify(trafficGenerator->WaitTime(firstMessage));
messageQueue.push(firstMessage);
}
}
void Processor::SendMessage()
{
outputPort->write(messageQueue.front());
messageQueue.pop();
ElectronicMessage* nextMessage = trafficGenerator->MessageCreated();
if(nextMessage != NULL)
{
sc_time val = trafficGenerator->WaitTime(nextMessage);
sendMessageEvent.notify(val);
messageQueue.push(nextMessage);
}
}
void Processor::MessageReceived()
{
//std::cout<<sc_time_stamp()<<" "<<this->name()<<" first flit -- received correct msg id = "<<inputPort->read()->messageId<<" from "<<inputPort->read()->sourceAddress<<" --> "<<endl;
networkLatency.RecordValue((sc_time_stamp() - inputPort->read()->beginTime).to_seconds());
lastMessageTime.RecordValue(sc_time_stamp().to_seconds());
ElectronicMessage* nextMessage = trafficGenerator->MessageReceived(inputPort->read());
if(nextMessage != NULL)
{
sendMessageEvent.notify(trafficGenerator->WaitTime(nextMessage));
messageQueue.push(nextMessage);
}
}
void Processor::MessageTransmitted()
{
ElectronicMessage* nextMessage = trafficGenerator->MessageTransmitted();
if(nextMessage != NULL)
{
sendMessageEvent.notify(trafficGenerator->WaitTime(nextMessage));
messageQueue.push(nextMessage);
}
}