-
Notifications
You must be signed in to change notification settings - Fork 0
/
centralprocessingunit.cpp
188 lines (169 loc) · 5.27 KB
/
centralprocessingunit.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
/*******************************************************************
*
* DESCRIPTION: Atomic CentralProcessingUnit MainMemory
*
* AUTHOR: Yu Wang
*
* EMAIL: mailto://[email protected]
*
* DATE: 15/10/2012
*
*******************************************************************/
/** include files **/
#include "centralprocessingunit.h" // class CentralProcessingUnit
#include "message.h" // class ExternalMessage, InternalMessage
#include "distri.h" // class Distribution
#include "mainsimu.h" // class MainSimulator
#include "strutil.h" // str2float( ... )
#include <stdlib.h>
/** public functions **/
/*******************************************************************
* Function Name: CentralProcessingUnit
* Description:
********************************************************************/
CentralProcessingUnit::CentralProcessingUnit( const string &name )
: Atomic( name )
, ErrorOut( addOutputPort( "ErrorOut" ) )
, ControlIn( addInputPort( "ControlIn" ) )
, DataIn( addInputPort( "DataIn" ) )
, AddressOut( addOutputPort( "AddressOut" ) )
, DataReceived( addOutputPort( "DataReceived" ) )
, DataAccessNum( 0 )
, TotalDataAccessNum( 0 )
, AddressSending( 0 )
, DataReceivedFlag( 0 )
, SendingPreparationTime( 0, 0, 3, 0 )
, DataTimingOutTime( 0, 1, 20, 0 )
, ErrorFlag( 0 )
{
int index;
index = 1;
try
{
dist = Distribution::create( MainSimulator::Instance().getParameter( description(), "distribution" ) );
MASSERT( dist );
for ( register int i = 0 ; i < dist->varCount() ; i++ )
{
string parameter( MainSimulator::Instance().getParameter( description(), dist->getVar(i) ) ) ;
dist->setVar( i, str2float( parameter ) ) ;
}
} catch( InvalidDistribution &e )
{
e.addText( "The model " + description() + " has distribution problems!" ) ;
e.print(cerr);
MTHROW( e ) ;
} catch( MException &e )
{
MTHROW( e ) ;
}
/* string time( MainSimulator::Instance().getParameter( description(), "MemoryAccessTime" ) ) ;
if( time != "" )
MemoryAccessTime = time ;*/
}
/*******************************************************************
* Function Name: initFunction
* Description: Resetea la lista
* Precondition: El tiempo del proximo evento interno es Infinito
********************************************************************/
Model &CentralProcessingUnit::initFunction()
{
this->passivate();
//packet = 0;
DataAccessNum = 0;
TotalDataAccessNum = 0;
AddressSending = 0;
DataReceivedFlag = 0;
ErrorFlag = 0;
transfertimes = 0;
// cout << "hello world!" << endl;
return *this ;
}
/*******************************************************************
* Function Name: externalFunction
* Description:
********************************************************************/
Model &CentralProcessingUnit::externalFunction( const ExternalMessage &msg )
{
if( msg.port() == ControlIn && this->state() == passive )
{
TotalDataAccessNum = static_cast < int > (msg.value());
DataAccessNum = 0;
DataReceivedFlag = 0;
holdIn( active, Time::Zero ); // triger the internal function
}
if( AddressSending && msg.port() == DataIn && this->state()== active && AddressSending)
{
DataAccessNum++;
AddressSending = 0;
DataReceivedFlag = 1;
Data = static_cast < int > (msg.value());
holdIn( active, Time::Zero );
transfertimes=0;
}
return *this;
}
/*******************************************************************
* Function Name: internalFunction
* Description:
********************************************************************/
Model &CentralProcessingUnit::internalFunction( const InternalMessage & )
{
if( DataAccessNum >= TotalDataAccessNum )
{
this->passivate();
}
if( ErrorFlag )
{
this->passivate();
}
else if( ( DataReceivedFlag || !DataAccessNum ) && (DataAccessNum < TotalDataAccessNum) && !AddressSending )
{
Address = rand() % 32;
DataReceivedFlag = 0;
AddressSending = 1;
transfertimes = 1;
holdIn( active, Time( static_cast<float>( fabs( distribution().get() ) ) ) ); // after a random time, we issue another memory access
}
else if( AddressSending && (transfertimes == 1))
{
//cout<<"DataTimingOutTime "<<DataTimingOutTime<<endl;
// AddressSending = 0;
transfertimes++;
holdIn( active, DataTimingOutTime );
}
else if(AddressSending && !DataReceivedFlag)
{
ErrorFlag=1;
AddressSending=0;
cout << "ErrorFlag "<< ErrorFlag <<endl;
holdIn( active, Time::Zero );
}
return *this ;
}
/*******************************************************************
* Function Name: outputFunction
* Description:
********************************************************************/
Model & CentralProcessingUnit::outputFunction( const InternalMessage &msg )
{
// cout<<" AddressSending "<<AddressSending<<endl;
// cout << "ErrorFlag "<< ErrorFlag <<endl;
if( ErrorFlag )
{
//cout<< "hello "<<endl;
sendOutput( msg.time(), ErrorOut, ErrorFlag ) ;
}
if( AddressSending && transfertimes==1 )
{
sendOutput( msg.time(), AddressOut, Address ) ;
}
else if( DataReceivedFlag )
{
sendOutput( msg.time(), DataReceived, Data ) ;
}
return *this ;
}
CentralProcessingUnit::~CentralProcessingUnit()
{
delete dist;
}