-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStimulus.cpp
executable file
·582 lines (512 loc) · 13.5 KB
/
Stimulus.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/****************************************************************************
* SCIC: A System C Interface Converter for DRAMSim
*
* Copyright (C) 2011 Myoungsoo Jung
* Pennsylvania State University
* David Donofrio
* John Shalf
* Lawrence Berkeley National Lab.
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*****************************************************************************/
#include <iostream>
#include <fstream>
#include <sstream>
#include <getopt.h>
#include <map>
#include <list>
#include "SystemConfiguration.h"
using namespace DRAMSim;
#include "assert.h"
#include "systemc.h"
#include "Transaction.h"
#include "Stimulus.h"
#ifndef NO_STORAGE
#include "BusPacket.h"
#endif
////////////////////////////////////////////////////////////////////////////////
//
// Method: run
// FullName: Stimulus::run
// Access: public
// Returns: void
//
// Descriptions - A CPU model's SystemC thread
//
//////////////////////////////////////////////////////////////////////////////
void Stimulus::run()
{
resetDataOutPort();
idleState();
wait(SC_ZERO_TIME);
if(_ptraceFile == NULL)
{
ERROR("No trace input file")
exit(0);
}
std::string line;
void * pBuffer;
TransactionType transType;
uint64_t nDestAddr;
uint64_t nSyncClk = 0;
while(1)
{
if(_prtCA->read() == true)
{
#if (UNIT_TEST_SEQUENTIAL_DATA == 1)
if(_prtWI->read() == true)
{
verifyData();
}
#else
getDataPort(_nReadBuffer);
if(_pfCallback != NULL)
{
uint32_t nTransId = NULL_SIG(32);
if (_prtWI->read() == true)
{
std::map<uint32_t, uint32_t>::iterator iterElem = _writeTransIdMap.find(_nReadBuffer[0]);
assert(iterElem != _writeTransIdMap.end());
nTransId = iterElem->second;
_writeTransIdMap.erase(iterElem);
}
else
{
nTransId = _nReadBuffer[1];
}
assert(nTransId != NULL_SIG(32));
_pfCallback(nTransId, _prtAI->read() , _nReadBuffer[0], (_prtWI->read() == true) ? STIMUL_IO_WRITE : STIMUL_IO_READ);
}
#endif
}
#if (UNIT_TEST_SEQUENTIAL_DATA == 1)
if(sequentialIo(4*1024*1024) == false)
{
idleState();
}
#else
if(_pfCallback == NULL)
{
if(_ptraceFile->eof() != true)
{
if(_prtRB->read() == false)
{
//
// stimulus issues I/O to memory only if the bus interface unit has room
//
getline(*_ptraceFile, line);
if (line.size() > 0)
{
pBuffer = parseTraceFileLine(line, nDestAddr, transType, nSyncClk, _traceType);
//DEBUG("Stimulus issues (address) :" << std::hex << nDestAddr << std::dec);
sendIo(nDestAddr, transType, pBuffer);
}
}
}
else
{
//
// there is no trace input to run anymore
//
idleState();
}
}
#endif
_nClockCycle++;
wait();
}
}
void Stimulus::RegisterCallback(GETRESP_PF pfCallback)
{
_pfCallback = pfCallback;
}
uint32_t Stimulus::IssueRequest(uint32_t nTransId, uint32_t nAddr, uint32_t nData, STIMUL_IO_TYPE nIoType)
{
uint32_t nResult = STIMUL_ERROR;
if(nIoType >= STIMUL_IO_MAXVAL)
{
nResult |= STIMUL_ERROR_INVALID_IOTYPE;
}
else if (_prtRB->read() == false)
{
nResult |= STIMUL_ERROR_DRAM_BUSY;
}
else
{
_nWriteBuffer[0] = nData;
_nWriteBuffer[1] = nTransId;
if(nIoType == STIMUL_IO_WRITE)
{
_writeTransIdMap.insert(std::make_pair(nAddr, nTransId));
}
sendIo(nAddr, (nIoType == STIMUL_IO_READ) ? DATA_READ : DATA_WRITE, (void *)_nWriteBuffer);
nResult = STIMUL_SUCCESS;
}
return nResult;
}
#if (UNIT_TEST_SEQUENTIAL_DATA == 1)
////////////////////////////////////////////////////////////////////////////////
//
// Method: sequentialIo
// FullName: Stimulus::sequentialIo
// Access: public
// Returns: bool
// Parameter: uint64_t nMaxAddrTest
//
// Descriptions -
//
//////////////////////////////////////////////////////////////////////////////
bool Stimulus::sequentialIo(uint64_t nMaxAddrTest)
{
if(_bWrite == true)
{
if(_nAddrKeeper < nMaxAddrTest)
{
_nReadBuffer[0] = _nAddrKeeper;
sendIo(_nAddrKeeper, DATA_WRITE, &_nReadBuffer);
_nAddrKeeper++;
}
else
{
_nAddrKeeper = 0;
_bWrite = true;
}
}
else
{
if(_nAddrKeeper < nMaxAddrTest)
{
sendIo(_nAddrKeeper, DATA_READ, &_nReadBuffer);
_nAddrKeeper++;
}
else
{
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
//
// Method: verifyData
// FullName: Stimulus::verifyData
// Access: public
// Returns: void
//
// Descriptions -
//
//////////////////////////////////////////////////////////////////////////////
void Stimulus::verifyData()
{
getDataPort(_nReadBuffer);
if(_prtAI->read() != _nReadBuffer[0])
{
std::cerr << "Data mismatch !! write was " << hex << _prtAI->read() << " read data is " << hex << _nReadBuffer[0] << std::endl;
}
}
#endif
////////////////////////////////////////////////////////////////////////////////
//
// Method: sendIo
// FullName: Stimulus::sendIo
// Access: public
// Returns: void
// Parameter: uint64_t nDestAddr
// Parameter: TransactionType transType
// Parameter: void * pBuffer
//
// Descriptions -
//
//////////////////////////////////////////////////////////////////////////////
void Stimulus::sendIo( uint64_t nDestAddr, TransactionType transType, void * pBuffer )
{
_prtAO->write(nDestAddr);
if(transType == DATA_WRITE)
{
_prtWE->write(true);
if(pBuffer == NULL)
{
resetDataOutPort();
}
else
{
setDataOutPort((uint64_t *)pBuffer);
}
}
else if(transType == DATA_READ)
{
_prtWE->write(false);
resetDataOutPort();
}
//
// issue I/O transaction
//
_prtDE->write(true);
}
////////////////////////////////////////////////////////////////////////////////
//
// Method: idleState
// FullName: Stimulus::idleState
// Access: public
// Returns: void
//
// Descriptions -
//
//////////////////////////////////////////////////////////////////////////////
void Stimulus::idleState()
{
_prtDE->write(false);
}
////////////////////////////////////////////////////////////////////////////////
//
// Method: resetDataOutPort
// FullName: Stimulus::resetDataOutPort
// Access: public
// Returns: void
//
// Descriptions -
//
//////////////////////////////////////////////////////////////////////////////
void Stimulus::resetDataOutPort()
{
for (int buffIdx = 0; buffIdx < 4; ++buffIdx)
{
_prtDataOut[buffIdx]->write(NULL_SIG64);
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Method: setDataOutPort
// FullName: Stimulus::setDataOutPort
// Access: public
// Returns: void
// Parameter: uint64_t * pData
//
// Descriptions -
//
//////////////////////////////////////////////////////////////////////////////
void Stimulus::setDataOutPort(uint64_t *pData)
{
assert(pData != NULL);
for (int buffIdx = 0; buffIdx < 4; ++buffIdx)
{
_prtDataOut[buffIdx]->write(pData[buffIdx]);
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Method: getDataPort
// FullName: Stimulus::getDataPort
// Access: public
// Returns: void
// Parameter: uint64_t * pData
//
// Descriptions -
//
//////////////////////////////////////////////////////////////////////////////
void Stimulus::getDataPort(uint64_t *pData)
{
assert(pData != NULL);
for (int buffIdx = 0; buffIdx < 4; ++buffIdx)
{
pData[buffIdx] = _prtDataIn[buffIdx]->read();
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Method: parseTraceFileLine
// FullName: Stimulus::parseTraceFileLine
// Access: public
// Returns: void *
// Parameter: string & line
// Parameter: uint64_t & addr
// Parameter: enum TransactionType & transType
// Parameter: uint64_t & clockCycle
// Parameter: TraceType type
//
// Descriptions -
//
//////////////////////////////////////////////////////////////////////////////
void * Stimulus::parseTraceFileLine(string &line, uint64_t &addr, enum TransactionType &transType, uint64_t &clockCycle, TraceType type)
{
size_t previousIndex=0;
size_t spaceIndex=0;
uint64_t *dataBuffer = NULL;
string addressStr="", cmdStr="", dataStr="", ccStr="";
#if (USE_SYSTEM_C_INTERFACE == 1)
bool useClockCycle = true;
#else
#ifndef _SIM_
bool useClockCycle = false;
#else
bool useClockCycle = true;
#endif
#endif
switch (type)
{
case k6:
{
spaceIndex = line.find_first_of(" ", 0);
addressStr = line.substr(0, spaceIndex);
previousIndex = spaceIndex;
spaceIndex = line.find_first_not_of(" ", previousIndex);
cmdStr = line.substr(spaceIndex, line.find_first_of(" ", spaceIndex) - spaceIndex);
previousIndex = line.find_first_of(" ", spaceIndex);
spaceIndex = line.find_first_not_of(" ", previousIndex);
ccStr = line.substr(spaceIndex, line.find_first_of(" ", spaceIndex) - spaceIndex);
if (cmdStr.compare("P_MEM_WR")==0 ||
cmdStr.compare("BOFF")==0)
{
transType = DATA_WRITE;
}
else if (cmdStr.compare("P_FETCH")==0 ||
cmdStr.compare("P_MEM_RD")==0 ||
cmdStr.compare("P_LOCK_RD")==0 ||
cmdStr.compare("P_LOCK_WR")==0)
{
transType = DATA_READ;
}
else
{
ERROR("== Unknown Command : "<<cmdStr);
exit(0);
}
istringstream a(addressStr.substr(2));//gets rid of 0x
a>>hex>>addr;
//if this is set to false, clockCycle will remain at 0, and every line read from the trace
// will be allowed to be issued
if (useClockCycle)
{
istringstream b(ccStr);
b>>clockCycle;
}
break;
}
case mase:
{
spaceIndex = line.find_first_of(" ", 0);
addressStr = line.substr(0, spaceIndex);
previousIndex = spaceIndex;
spaceIndex = line.find_first_not_of(" ", previousIndex);
cmdStr = line.substr(spaceIndex, line.find_first_of(" ", spaceIndex) - spaceIndex);
previousIndex = line.find_first_of(" ", spaceIndex);
spaceIndex = line.find_first_not_of(" ", previousIndex);
ccStr = line.substr(spaceIndex, line.find_first_of(" ", spaceIndex) - spaceIndex);
if (cmdStr.compare("IFETCH")==0||
cmdStr.compare("READ")==0)
{
transType = DATA_READ;
}
else if (cmdStr.compare("WRITE")==0)
{
transType = DATA_WRITE;
}
else
{
ERROR("== Unknown command in tracefile : "<<cmdStr);
}
istringstream a(addressStr.substr(2));//gets rid of 0x
a>>hex>>addr;
//if this is set to false, clockCycle will remain at 0, and every line read from the trace
// will be allowed to be issued
if (useClockCycle)
{
istringstream b(ccStr);
b>>clockCycle;
}
break;
}
case misc:
spaceIndex = line.find_first_of(" ", spaceIndex+1);
if (spaceIndex == string::npos)
{
ERROR("Malformed line: '"<< line <<"'");
}
addressStr = line.substr(previousIndex,spaceIndex);
previousIndex=spaceIndex;
spaceIndex = line.find_first_of(" ", spaceIndex+1);
if (spaceIndex == string::npos)
{
cmdStr = line.substr(previousIndex+1);
}
else
{
cmdStr = line.substr(previousIndex+1,spaceIndex-previousIndex-1);
dataStr = line.substr(spaceIndex+1);
}
//convert address string -> number
istringstream b(addressStr.substr(2)); //substr(2) chops off 0x characters
b >>hex>> addr;
// parse command
if (cmdStr.compare("read") == 0)
{
transType=DATA_READ;
}
else if (cmdStr.compare("write") == 0)
{
transType=DATA_WRITE;
}
else
{
ERROR("INVALID COMMAND '"<<cmdStr<<"'");
exit(-1);
}
if (SHOW_SIM_OUTPUT)
{
DEBUGN("ADDR='"<<hex<<addr<<dec<<"',CMD='"<<transType<<"'");//',DATA='"<<dataBuffer[0]<<"'");
}
//parse data
//if we are running in a no storage mode, don't allocate space, just return NULL
#ifndef NO_STORAGE
if (dataStr.size() > 0 && transType == DATA_WRITE)
{
// 32 bytes of data per transaction
dataBuffer = (uint64_t *)calloc(sizeof(uint64_t),4);
size_t strlen = dataStr.size();
for (int i=0; i < 4; i++)
{
size_t startIndex = i*16;
if (startIndex > strlen)
{
break;
}
size_t charsLeft = min(((size_t)16), strlen - startIndex + 1);
string piece = dataStr.substr(i*16,charsLeft);
istringstream iss(piece);
iss >> hex >> dataBuffer[i];
}
PRINTN("\tDATA=");
BusPacket::printData(dataBuffer);
}
PRINT("");
#endif
break;
}
return dataBuffer;
}
void Stimulus::Initialize()
{
_pfCallback = NULL;
#if (UNIT_TEST_SEQUENTIAL_DATA == 1)
_nAddrKeeper = 0;
_bWrite = false;
#endif
}
void Stimulus::Reset()
{
Initialize();
resetDataOutPort();
}