OpenASIP  2.0
Proxim.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2017 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 Proxim.cc
26  *
27  * Implementation of Proxim class.
28  *
29  * @author Veli-Pekka J��skel�inen 2005 (vjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include <string>
34 #include <cstdlib>
35 #include <iostream>
36 #include <wx/cmdline.h>
37 #include <wx/imagpng.h>
38 #include "Proxim.hh"
39 #include "Application.hh"
40 #include "Exception.hh"
41 #include "SimulatorInterpreter.hh"
42 #include "ProximMainFrame.hh"
44 #include "GUIOptions.hh"
45 #include "Environment.hh"
46 #include "GUIOptionsSerializer.hh"
47 #include "ProximConstants.hh"
48 #include "FileSystem.hh"
49 #include "WxConversion.hh"
50 #include "ProximToolbox.hh"
51 #include "ErrorDialog.hh"
52 #include "tce_config.h"
53 #include "ObjectState.hh"
54 
55 IMPLEMENT_APP(Proxim)
56 
57 using std::cerr;
58 using std::endl;
59 using std::string;
60 
61 const std::string Proxim::CONFIG_FILE_NAME = "Proxim.conf";
62 const std::string Proxim::SCHEMA_FILE_NAME = "confschema.xsd";
63 
64 /**
65  * The constructor.
66  */
68  wxApp(), mainFrame_(NULL), commandRegistry_(NULL), simulation_(NULL),
69  options_(NULL) {
70 }
71 
72 
73 /**
74  * The Destructor.
75  */
77  delete commandRegistry_;
78 }
79 
80 
81 
82 /**
83  * Initializes the application.
84  *
85  * Creates command registry, application main frame and a simulation thread.
86  * The main frame console linereader is set as the linereader for the
87  * simulator thread.
88  *
89  * @return True, if the application was succesfully initialized.
90  */
91 bool
93 
95 
96  const wxCmdLineEntryDesc cmdLineDesc[] = {
97 
98 #if wxCHECK_VERSION(3,0,0)
99  { wxCMD_LINE_PARAM, NULL, NULL, "input file",
100 #else
101  { wxCMD_LINE_PARAM, NULL, NULL, _T("input file"),
102 #endif
103 
104  wxCMD_LINE_VAL_STRING,
105  (wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL) },
106  { wxCMD_LINE_NONE, NULL, NULL, NULL, wxCMD_LINE_VAL_NONE, 0 }
107  };
108 
109  wxCmdLineParser parser(cmdLineDesc);
110  parser.SetCmdLine(argc, argv);
111  parser.Parse();
112 
113  std::string machineToLoad = "";
114  std::string programToLoad = "";
115 
116  if (parser.GetParamCount() == 1) {
117  programToLoad = WxConversion::toString(parser.GetParam(0));
118  } else if (parser.GetParamCount() == 2) {
119  machineToLoad = WxConversion::toString(parser.GetParam(0));
120  programToLoad = WxConversion::toString(parser.GetParam(1));
121  } else if (parser.GetParamCount() > 2) {
122  std::cout << ProximConstants::PROXIM_TITLE << " "
123  << Application::TCEVersionString() << std::endl;
124  std::cout << "Usage: proxim [program file]" << endl;
125  std::cout << " proxim [machine file] [program file]" << endl;
126 
127  return false;
128  }
129 
130 
131  // load image handler for pngs
132  wxImage::AddHandler(new wxPNGHandler);
133 
136  options_ = new GUIOptions("proxim-configuration");
137 
138  loadOptions();
139 
141  _T("TTA Processor Simulator"), wxDefaultPosition, wxSize(800, 600));
142 
143  simulation_->Create();
145  simulation_->Run();
146 
147  mainFrame_->Show(true);
148  SetTopWindow(mainFrame_);
149 
150  SimulatorInterpreter* interpreter = simulation_->interpreter();
151 
152  if (machineToLoad != "") {
153  interpreter->interpret(
154  ProximConstants::SCL_LOAD_MACHINE + " " + machineToLoad);
155  if (interpreter->result().size() > 0) {
156  wxString message = _T("Error loading machine file '");
157  message.Append(WxConversion::toWxString(machineToLoad));
158  message.Append(_T("'.\n"));
159  message.Append(WxConversion::toWxString(interpreter->result()));
160  ErrorDialog dialog(mainFrame_, message);
161  dialog.ShowModal();
162  return true;
163  }
164  }
165 
166  if (programToLoad != "") {
167  interpreter->interpret(
168  ProximConstants::SCL_LOAD_PROGRAM + " " + programToLoad);
169  if (interpreter->result().size() > 0) {
170  wxString message = _T("Error loading program file '");
171  message.Append(WxConversion::toWxString(programToLoad));
172  message.Append(_T("'.\n"));
173  message.Append(WxConversion::toWxString(interpreter->result()));
174  ErrorDialog dialog(mainFrame_, message);
175  dialog.ShowModal();
176  }
177  }
178  return true;
179 }
180 
181 
182 /**
183  * Deletes the simulation thread when the application exits.
184  *
185  * @return Exit code.
186  */
187 int
189  simulation_->Delete();
190  simulation_->Wait();
191  delete simulation_;
192  return EXIT_SUCCESS;
193 }
194 
195 
196 /**
197  * Returns pointer to the simulation thread.
198  *
199  * @return Pointer to the simulation thread.
200  */
203  return simulation_;
204 }
205 
206 
207 /**
208  * Returns reference to the application command registry.
209  *
210  * The command registry stores all commands available for execution
211  * in the main fram menubar and toolbar.
212  *
213  * @return Reference to the application command registry.
214  */
217  return *commandRegistry_;
218 }
219 
220 /**
221  * Loads GUIOptions for Proxim from the configuration file.
222  *
223  * If the configuration file is erroneous or not found, default options
224  * will be used.
225  */
226 void
228 
229  if (options_ != NULL) {
230  delete options_;
231  }
232 
233  bool optionsOK = false;
234 
235  string configFile = Environment::confPath(CONFIG_FILE_NAME);
236 
237  string schemaFile = Environment::dataDirPath("Proxim") +
239 
241  reader.setSourceFile(configFile);
242  reader.setSchemaFile(schemaFile);
243  reader.setUseSchema(true);
244 
245  try {
246  ObjectState* optionsState = reader.readState();
247  options_ = new GUIOptions(optionsState);
248  delete optionsState;
249  options_->validate();
250  options_->setFileName(configFile);
251  optionsOK = true;
252  } catch (Exception& e) {
253  cerr << "Error loading config file " << configFile << ":" << endl
254  << e.errorMessage() << endl
255  << "Default options will be used." << endl;
256  }
257 
258  if (!optionsOK) {
260  } else {
262  }
263 }
264 
265 /**
266  * Creates a default set of options if the configuration file is erroneous
267  * or not found.
268  */
269 void
271 
272  ToolbarButton* buttonMachine = new ToolbarButton(
274 
275  ToolbarButton* buttonProgram = new ToolbarButton(
277 
278  ToolbarButton* buttonRun = new ToolbarButton(
280 
281  ToolbarButton* buttonResume = new ToolbarButton(
283 
284  ToolbarButton* buttonKill = new ToolbarButton(
286 
287  ToolbarButton* buttonStepi = new ToolbarButton(
289 
290  ToolbarButton* buttonNexti = new ToolbarButton(
292 
293  // F2 -> run
294  KeyboardShortcut* scRun = new KeyboardShortcut(
295  ProximConstants::COMMAND_NAME_RUN, 2, false, false, 0);
296 
297  // F4 -> kill
298  KeyboardShortcut* scKill = new KeyboardShortcut(
299  ProximConstants::COMMAND_NAME_KILL, 4, false, false, 0);
300 
301  // F5 -> stepi
302  KeyboardShortcut* scStepi = new KeyboardShortcut(
303  ProximConstants::COMMAND_NAME_STEPI, 5, false, false, 0);
304 
305  // F6 -> nexti
306  KeyboardShortcut* scNexti = new KeyboardShortcut(
307  ProximConstants::COMMAND_NAME_NEXTI, 6, false, false, 0);
308 
309  // F9 -> resume
310  KeyboardShortcut* scResume = new KeyboardShortcut(
311  ProximConstants::COMMAND_NAME_RESUME, 9, false, false, 0);
312 
313  // Ctrl + F -> find operation in assembly code window
314  KeyboardShortcut* scFind = new KeyboardShortcut(
315  ProximConstants::COMMAND_NAME_FIND, 0, true, false, int('F'));
316 
318  string fileName = Environment::userConfPath(CONFIG_FILE_NAME);
319 
320  options_->setFileName(fileName);
321 
322  options_->addToolbarButton(buttonMachine);
323  options_->addToolbarButton(buttonProgram);
324  options_->addToolbarButton(buttonRun);
325  options_->addToolbarButton(buttonResume);
326  options_->addToolbarButton(buttonKill);
327  options_->addToolbarButton(buttonStepi);
328  options_->addToolbarButton(buttonNexti);
332 
334  options_->addKeyboardShortcut(scStepi);
335  options_->addKeyboardShortcut(scNexti);
336  options_->addKeyboardShortcut(scResume);
337  options_->addKeyboardShortcut(scKill);
338  options_->addKeyboardShortcut(scFind);
339 }
340 
341 /**
342  * Returns references to the application options.
343  *
344  * @return GUIOptions of the application.
345  */
346 GUIOptions&
348  return *options_;
349 }
ProximConstants::COMMAND_NAME_NEXTI
static const std::string COMMAND_NAME_NEXTI
Name of the next instruction command.
Definition: ProximConstants.hh:122
ProximMainFrame
Definition: ProximMainFrame.hh:58
FileSystem.hh
WxConversion::toWxString
static wxString toWxString(const std::string &source)
Proxim::options_
GUIOptions * options_
Options defining toolbar, keyboard shortcuts etc.
Definition: Proxim.hh:74
XMLSerializer::setSourceFile
void setSourceFile(const std::string &fileName)
Definition: XMLSerializer.cc:115
GUIOptions::clearModified
void clearModified()
Definition: GUIOptions.cc:502
ScriptInterpreter::result
virtual std::string result()
Definition: ScriptInterpreter.cc:191
Exception.hh
GUIOptions::addToolbarButton
void addToolbarButton(ToolbarButton *button)
Definition: GUIOptions.cc:292
ProximConstants::CONFIGURATION_NAME
static const std::string CONFIGURATION_NAME
Configuration file top-level element name.
Definition: ProximConstants.hh:247
GUIOptionsSerializer.hh
GUIOptions.hh
ProximSimulationThread
Definition: ProximSimulationThread.hh:55
ProximSimulationThread::interpreter
SimulatorInterpreter * interpreter()
Definition: ProximSimulationThread.cc:244
Proxim::OnInit
virtual bool OnInit()
Definition: Proxim.cc:92
ObjectState
Definition: ObjectState.hh:59
Proxim::simulation
ProximSimulationThread * simulation()
Definition: Proxim.cc:202
ToolbarButton
Definition: ToolbarButton.hh:48
Proxim::mainFrame_
ProximMainFrame * mainFrame_
The application main frame.
Definition: Proxim.hh:68
ProximSimulationThread.hh
ProximConstants::COMMAND_NAME_OPEN_PROGRAM
static const std::string COMMAND_NAME_OPEN_PROGRAM
Name of the open program command.
Definition: ProximConstants.hh:104
Proxim.hh
GUIOptions::setToolbarVisibility
void setToolbarVisibility(bool visible)
Definition: GUIOptions.cc:256
Proxim::Proxim
Proxim()
Definition: Proxim.cc:67
GUIOptionsSerializer::readState
ObjectState * readState()
Definition: GUIOptionsSerializer.cc:149
Environment::userConfPath
static TCEString userConfPath(const std::string &fileName)
Definition: Environment.cc:303
ProximConstants::PROXIM_TITLE
static const std::string PROXIM_TITLE
Application title.
Definition: ProximConstants.hh:250
Proxim::OnExit
virtual int OnExit()
Definition: Proxim.cc:188
ProximConstants::SCL_LOAD_MACHINE
static const std::string SCL_LOAD_MACHINE
Command for loading a new machine in the simulator.
Definition: ProximConstants.hh:147
ProximToolbox.hh
ProximConstants::COMMAND_NAME_STEPI
static const std::string COMMAND_NAME_STEPI
Name of the step instruction command.
Definition: ProximConstants.hh:120
ProximConstants::COMMAND_NAME_KILL
static const std::string COMMAND_NAME_KILL
Name of the kill command.
Definition: ProximConstants.hh:126
SimulatorInterpreter
Definition: SimulatorInterpreter.hh:49
ErrorDialog
Definition: ErrorDialog.hh:42
XMLSerializer::setSchemaFile
void setSchemaFile(const std::string &fileName)
Definition: XMLSerializer.cc:168
ErrorDialog.hh
ProximConstants::COMMAND_NAME_FIND
static const std::string COMMAND_NAME_FIND
Name of the find command.
Definition: ProximConstants.hh:140
Proxim::~Proxim
virtual ~Proxim()
Definition: Proxim.cc:76
Application.hh
ObjectState.hh
CommandRegistry
Definition: CommandRegistry.hh:47
Proxim::commandRegistry
CommandRegistry & commandRegistry()
Definition: Proxim.cc:216
GUIOptions::addSeparator
void addSeparator(int position)
Definition: GUIOptions.cc:305
Proxim
Definition: Proxim.hh:53
TclInterpreter::interpret
virtual bool interpret(const std::string &commandLine)
Definition: TclInterpreter.cc:138
GUIOptions::TEXT
@ TEXT
Buttons contains only text.
Definition: GUIOptions.hh:65
Environment.hh
GUIOptions::setFileName
void setFileName(const std::string &fileName)
Definition: GUIOptions.cc:632
GUIOptions::addKeyboardShortcut
void addKeyboardShortcut(KeyboardShortcut *shortcut)
Definition: GUIOptions.cc:280
Exception
Definition: Exception.hh:54
ProximConstants::COMMAND_NAME_OPEN_MACHINE
static const std::string COMMAND_NAME_OPEN_MACHINE
Name of the open machine command.
Definition: ProximConstants.hh:106
GUIOptions::setToolbarLayout
void setToolbarLayout(ToolbarLayout layout)
Definition: GUIOptions.cc:268
XMLSerializer::setUseSchema
void setUseSchema(bool useSchema)
Definition: XMLSerializer.cc:179
GUIOptions::validate
virtual void validate() const
Definition: GUIOptions.cc:529
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
KeyboardShortcut
Definition: KeyboardShortcut.hh:50
ProximSimulationThread::initialize
void initialize(ProximMainFrame *gui)
Definition: ProximSimulationThread.cc:87
GUIOptions
Definition: GUIOptions.hh:58
ProximConstants::COMMAND_NAME_RUN
static const std::string COMMAND_NAME_RUN
Name of the run command.
Definition: ProximConstants.hh:118
Environment::confPath
static TCEString confPath(const std::string &fileName)
Definition: Environment.cc:277
Proxim::createDefaultOptions
void createDefaultOptions()
Definition: Proxim.cc:270
Proxim::options
GUIOptions & options()
Definition: Proxim.cc:347
Proxim::simulation_
ProximSimulationThread * simulation_
The simulation thread running the simulator backend.
Definition: Proxim.hh:72
ProximConstants.hh
Application::initialize
static void initialize()
Definition: Application.cc:99
ProximMainFrame.hh
SimulatorInterpreter.hh
ProximConstants::SCL_LOAD_PROGRAM
static const std::string SCL_LOAD_PROGRAM
Command for loading a new program in the simulator.
Definition: ProximConstants.hh:145
Proxim::loadOptions
void loadOptions()
Definition: Proxim.cc:227
WxConversion.hh
ProximConstants::COMMAND_NAME_RESUME
static const std::string COMMAND_NAME_RESUME
Name of the resume command.
Definition: ProximConstants.hh:128
Proxim::CONFIG_FILE_NAME
static const std::string CONFIG_FILE_NAME
Name of the config file.
Definition: Proxim.hh:77
WxConversion::toString
static std::string toString(const wxString &source)
Application::TCEVersionString
static std::string TCEVersionString()
Definition: Application.cc:510
Proxim::SCHEMA_FILE_NAME
static const std::string SCHEMA_FILE_NAME
Name of the xml-schema file.
Definition: Proxim.hh:79
Environment::dataDirPath
static std::string dataDirPath(const std::string &prog)
Definition: Environment.cc:176
Proxim::commandRegistry_
CommandRegistry * commandRegistry_
The application command registry.
Definition: Proxim.hh:70
GUIOptionsSerializer
Definition: GUIOptionsSerializer.hh:52
WxConversion::toCStringArray
static char ** toCStringArray(size_t elements, wxChar **source)
Definition: WxConversion.cc:123