1/*
2 * Copyright (c) 2012-2014, TU Delft
3 * Copyright (c) 2012-2014, TU Eindhoven
4 * Copyright (c) 2012-2014, TU Kaiserslautern
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36#include <cstdio>
37#include <algorithm>  // std::count
38#include <string>
39
40#include "CommandAnalysis.h"
41
42using std::cerr;
43using std::endl;
44using std::string;
45
46using namespace Data;
47
48
49// To get the time of completion of the issued command
50// Derived based on JEDEC specifications
51
52int64_t CommandAnalysis::timeToCompletion(MemCommand::cmds type)
53{
54  int64_t offset = 0;
55  const MemTimingSpec& memTimingSpec     = memSpec.memTimingSpec;
56  const MemArchitectureSpec& memArchSpec = memSpec.memArchSpec;
57
58  if (type == MemCommand::RD) {
59    offset = memTimingSpec.RL +
60                              memTimingSpec.DQSCK + 1 + (memArchSpec.burstLength /
61                                                         memArchSpec.dataRate);
62  } else if (type == MemCommand::WR) {
63    offset = memTimingSpec.WL +
64                              (memArchSpec.burstLength / memArchSpec.dataRate) +
65                              memTimingSpec.WR;
66  } else if (type == MemCommand::ACT) {
67    offset = memTimingSpec.RCD;
68  } else if ((type == MemCommand::PRE) || (type == MemCommand::PREA)) {
69    offset = memTimingSpec.RP;
70  }
71  return offset;
72} // CommandAnalysis::timeToCompletion
73
74
75// Returns the number of active banks based on the bank_state vector.
76unsigned CommandAnalysis::get_num_active_banks(void)
77{
78  return (unsigned)std::count(bank_state.begin(), bank_state.end(), BANK_ACTIVE);
79}
80
81// Naming-standard compliant wrapper
82unsigned CommandAnalysis::nActiveBanks(void)
83{
84  return CommandAnalysis::get_num_active_banks();
85}
86
87bool CommandAnalysis::isPrecharged(unsigned bank)
88{
89    return bank_state[bank] == BANK_PRECHARGED;
90}
91
92void CommandAnalysis::printWarningIfActive(const string& warning, int type, int64_t timestamp, unsigned bank)
93{
94  if (get_num_active_banks() != 0) {
95    printWarning(warning, type, timestamp, bank);
96  }
97}
98
99void CommandAnalysis::printWarningIfNotActive(const string& warning, int type, int64_t timestamp, unsigned bank)
100{
101  if (get_num_active_banks() == 0) {
102    printWarning(warning, type, timestamp, bank);
103  }
104}
105
106void CommandAnalysis::printWarningIfPoweredDown(const string& warning, int type, int64_t timestamp, unsigned bank)
107{
108  if (mem_state != 0) {
109    printWarning(warning, type, timestamp, bank);
110  }
111}
112
113void CommandAnalysis::printWarning(const string& warning, int type, int64_t timestamp, unsigned bank)
114{
115  cerr << "WARNING: " << warning << endl;
116  cerr << "Command: " << type << ", Timestamp: " << timestamp <<
117    ", Bank: " << bank << endl;
118}
119