OpenASIP  2.0
TTASimTandem.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file TTASimTandem.cc
26  *
27  * Implementation of a simulator that runs both compiled simulation and
28  * the interpretive (debugging) simulation in parallel and compares the
29  * execution to detect differences in the simulations, which indicate there's
30  * a missimulation bug in one or both of the engines.
31  *
32  * @author Pekka Jääskeläinen 2009 (pjaaskel-no.spam-cs.tut.fi)
33  * @note rating: red
34  */
35 
36 #include <iostream>
37 #include <algorithm>
38 #include <boost/format.hpp>
39 #include "Application.hh"
40 #include "Machine.hh"
41 #include "Program.hh"
42 #include "SimulatorFrontend.hh"
43 #include "RegisterFile.hh"
44 #include "SimValue.hh"
45 #include "POMDisassembler.hh"
46 #include "DisassemblyFUPort.hh"
47 #include "Instruction.hh"
48 #include "Procedure.hh"
49 
50 /**
51  * Simulates the given program+machine in "tandem" with the two
52  * simulation engines.
53  */
54 void
58 
59  // initialize both simulation engines
61  interp.loadMachine(machine);
62  interp.loadProgram(program);
63 
65 
66  // leave the compile simulation engine files at /tmp
67  compiled.setCompiledSimulationLeaveDirty(true);
68 
69  compiled.loadMachine(machine);
70  compiled.loadProgram(program);
71  // reduce startup time by using dynamic compilation of the simulation
72  // engine
73  compiled.setStaticCompilation(false);
74 
75 
76  assert(compiled.isSimulationInitialized());
78 
79  ClockCycleCount simulatedCycles = 0;
80  while (!compiled.hasSimulationEnded()) {
81  ClockCycleCount stepping;
82  try {
83  compiled.step(1);
84 
85  // the compiled simulator compiled BB at a time so we need to
86  // ask it how many cycle to advance the cycle level interp. engine
87  // to get to the same position in the simulation
88  stepping =
89  compiled.cycleCount() - simulatedCycles;
90 
91  interp.step(stepping);
92  simulatedCycles += stepping;
93  } catch (const Exception& e) {
94  std::cerr << "Simulation error: " << e.errorMessage() << std::endl;
95  return;
96 
97  }
98 
99  if (!compiled.compareState(interp, &std::cerr))
100  return;
101 
102  // print out the cycle count after simulating at least 1M cycles
103  if (simulatedCycles / 1000000 > (simulatedCycles - stepping) / 1000000)
104  std::cout
105  << "simulated " << simulatedCycles << " cycles" << std::endl;
106  }
107 
108  std::cout
109  << "stop reasons:" << std::endl
110  << "* compiled engine:" << std::endl;
111  for (unsigned i = 0; i < compiled.stopReasonCount(); ++i) {
112  std::cout << "** " << compiled.stopReason(i) << std::endl;
113  }
114  std::cout
115  << "* interp. engine:" << std::endl;
116  for (unsigned i = 0; i < interp.stopReasonCount(); ++i) {
117  std::cout << "** " << interp.stopReason(i) << std::endl;
118  }
119 
120 }
121 
122 int
123 main(int argc, char* argv[]) {
124 
126 
127  if (argc < 3) {
128  std::cerr << "usage: ttasim-tandem machine.adf program.tpef";
129 
130  }
131 
132  try {
138 
139  delete machine; machine = NULL;
140  delete program; program = NULL;
141  } catch (const Exception& e) {
142  std::cerr << e.errorMessage() << std::endl;
143  return EXIT_FAILURE;
144  }
145  return EXIT_SUCCESS;
146 }
TTAProgram::Program
Definition: Program.hh:63
SimulatorFrontend::SIM_COMPILED
@ SIM_COMPILED
Compiled, faster simulation.
Definition: SimulatorFrontend.hh:103
machine
TTAMachine::Machine * machine
the architecture definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:59
Procedure.hh
SimulatorFrontend::setStaticCompilation
void setStaticCompilation(bool value)
Definition: SimulatorFrontend.cc:1977
SimulatorFrontend::stopReasonCount
unsigned int stopReasonCount() const
Definition: SimulatorFrontend.cc:1344
POMDisassembler.hh
assert
#define assert(condition)
Definition: Application.hh:86
SimulatorFrontend::loadMachine
virtual void loadMachine(const std::string &fileName)
Definition: SimulatorFrontend.cc:534
Instruction.hh
Application.hh
SimulatorFrontend::compareState
bool compareState(SimulatorFrontend &other, std::ostream *differences=NULL)
Definition: SimulatorFrontend.cc:2415
SimulatorFrontend::loadProgram
virtual void loadProgram(const std::string &fileName)
Definition: SimulatorFrontend.cc:299
Machine.hh
Exception
Definition: Exception.hh:54
SimulatorFrontend.hh
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
SimulatorFrontend::cycleCount
ClockCycleCount cycleCount() const
Definition: SimulatorFrontend.cc:1194
SimulatorFrontend::step
virtual void step(double count=1)
Definition: SimulatorFrontend.cc:1034
Program.hh
RegisterFile.hh
SimValue.hh
Application::initialize
static void initialize()
Definition: Application.cc:99
TTAProgram::Program::loadFromTPEF
static Program * loadFromTPEF(const std::string &tpefFileName, const TTAMachine::Machine &theMachine)
Definition: Program.cc:1112
SimulatorFrontend::setCompiledSimulationLeaveDirty
void setCompiledSimulationLeaveDirty(bool dirty)
Definition: SimulatorFrontend.hh:157
SimulatorFrontend::stopReason
StopReason stopReason(unsigned int index) const
Definition: SimulatorFrontend.cc:1356
ClockCycleCount
CycleCount ClockCycleCount
Alias for ClockCycleCount.
Definition: SimulatorConstants.hh:57
main
int main(int argc, char *argv[])
Definition: TTASimTandem.cc:123
program
find Finds info of the inner loops in the program
Definition: InnerLoopFinder.cc:80
SimulatorFrontend::hasSimulationEnded
bool hasSimulationEnded() const
Definition: SimulatorFrontend.cc:1283
SimulatorFrontend
Definition: SimulatorFrontend.hh:89
SimulatorFrontend::isSimulationInitialized
bool isSimulationInitialized() const
Definition: SimulatorFrontend.cc:1228
DisassemblyFUPort.hh
TTAMachine::Machine
Definition: Machine.hh:73
tandemSimulate
void tandemSimulate(TTAMachine::Machine &machine, TTAProgram::Program &program)
Definition: TTASimTandem.cc:55
TTAMachine::Machine::loadFromADF
static Machine * loadFromADF(const std::string &adfFileName)
Definition: Machine.cc:905
SimulatorFrontend::SIM_NORMAL
@ SIM_NORMAL
Default, interpreted simulation (debugging engine).
Definition: SimulatorFrontend.hh:102