OpenASIP  2.0
MemWriteCommand.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2016 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 MemWriteCommand.cc
26  *
27  * Implementation of MemWriteCommand class
28  *
29  * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
30  * @author Alex Hirvonen 2016 (alex.hirvonen-no.spam-gmail.com)
31  * @note rating: red
32  */
33 
34 #include "MemWriteCommand.hh"
35 #include "SimulatorFrontend.hh"
36 #include "MemorySystem.hh"
37 #include "SimulatorToolbox.hh"
38 #include "Memory.hh"
39 #include "StringTools.hh"
40 #include "Address.hh"
41 #include "NullAddressSpace.hh"
42 #include "SimValue.hh"
43 #include "Conversion.hh"
44 #include "FileSystem.hh"
45 #include <iostream>
46 #include <fstream>
47 
48 
50  SimControlLanguageCommand("load_data") {
51 }
52 
53 
55 }
56 
57 /**
58  * Executes the "load_data" command.
59  *
60  * This low-level command reads binary data from file and loads it to memory
61  * starting at specified address.
62  *
63  * @param arguments Memory address to be written, file to read from, read size.
64  * @return True in case simulation is initialized and arguments are ok.
65  * @exception NumberFormatException Is never thrown by this command.
66  */
67 bool
68 MemWriteCommand::execute(const std::vector<DataObject>& arguments) {
69  const int argumentCount = arguments.size() - 1;
70  if (!checkArgumentCount(argumentCount, 2, 5)) {
71  return false;
72  }
73 
74  if (!checkProgramLoaded()) {
75  return false;
76  }
77 
78  size_t nextArg = 1;
79  size_t writeAddress;
80  std::string addressSpaceName = "";
81 
82  if (StringTools::ciEqual(arguments.at(nextArg).stringValue(), "/a")) {
83  addressSpaceName = arguments.at(2).stringValue();
84  nextArg = 3;
85  }
86 
87  const std::string addressString = arguments.at(nextArg).stringValue();
88  if (!setMemoryAddress(addressString, addressSpaceName, writeAddress)) {
89  return false;
90  }
91 
92  std::string fileName = arguments.at(nextArg + 1).stringValue();
93  if (!FileSystem::fileExists(fileName) ||
94  !FileSystem::fileIsReadable(fileName)) {
95  interpreter()->setResult("Error reading input file.");
96  interpreter()->setError(true);
97  return false;
98  }
99 
100  size_t readSize = FileSystem::sizeInBytes(fileName);
101  if (argumentCount == 3 || argumentCount == 5) {
102  readSize = arguments.at(nextArg + 2).integerValue();
103  }
104 
106  if (!setMemoryPointer(memory, addressSpaceName)) {
107  return false;
108  }
109 
110  std::ifstream inputFile(fileName.c_str(), std::ios::binary);
111  char dataByte;
112 
113  while (inputFile.get(dataByte) && readSize > 0) {
114  try {
115  memory->write(writeAddress, 1, (UIntWord)dataByte);
116  } catch (const OutOfRange&) {
120  interpreter()->setError(true);
121  return false;
122  }
123  writeAddress++;
124  readSize--;
125  }
126  memory->advanceClock();
127  inputFile.close();
128 
129  return true;
130 }
131 
132 std::string
136 }
UIntWord
Word UIntWord
Definition: BaseType.hh:144
CustomCommand::checkArgumentCount
bool checkArgumentCount(int argumentCount, int minimum, int maximum)
Definition: CustomCommand.cc:82
FileSystem.hh
MemWriteCommand::execute
virtual bool execute(const std::vector< DataObject > &arguments)
Definition: MemWriteCommand.cc:68
SimControlLanguageCommand::setMemoryPointer
bool setMemoryPointer(MemorySystem::MemoryPtr &memory, const std::string &addressSpaceName)
Definition: SimControlLanguageCommand.cc:780
Memory.hh
OutOfRange
Definition: Exception.hh:320
Texts::TXT_ADDRESS_OUT_OF_RANGE
@ TXT_ADDRESS_OUT_OF_RANGE
Definition: SimulatorTextGenerator.hh:192
SimControlLanguageCommand::checkProgramLoaded
bool checkProgramLoaded()
Definition: SimControlLanguageCommand.cc:180
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
MemorySystem.hh
SimControlLanguageCommand::setMemoryAddress
bool setMemoryAddress(const std::string &addressString, std::string &addressSpaceName, std::size_t &memoryAddress)
Definition: SimControlLanguageCommand.cc:753
MemorySystem::MemoryPtr
boost::shared_ptr< Memory > MemoryPtr
Definition: MemorySystem.hh:57
StringTools.hh
MemWriteCommand.hh
SimulatorToolbox.hh
Conversion.hh
NullAddressSpace.hh
SimulatorToolbox::textGenerator
static SimulatorTextGenerator & textGenerator()
Definition: SimulatorToolbox.cc:75
StringTools::ciEqual
static bool ciEqual(const std::string &a, const std::string &b)
Definition: StringTools.cc:240
FileSystem::sizeInBytes
static uintmax_t sizeInBytes(const std::string &filePath)
Definition: FileSystem.cc:376
SimulatorFrontend.hh
MemWriteCommand::helpText
virtual std::string helpText() const
Definition: MemWriteCommand.cc:133
CustomCommand::interpreter
ScriptInterpreter * interpreter() const
ScriptInterpreter::setResult
virtual void setResult(DataObject *result)
Definition: ScriptInterpreter.cc:128
FileSystem::fileExists
static bool fileExists(const std::string fileName)
Address.hh
SimValue.hh
Texts::TXT_INTERP_HELP_LOADDATA
@ TXT_INTERP_HELP_LOADDATA
Help text for command "load_data" of the CLI.
Definition: SimulatorTextGenerator.hh:107
MemWriteCommand::~MemWriteCommand
virtual ~MemWriteCommand()
Definition: MemWriteCommand.cc:54
ScriptInterpreter::setError
virtual void setError(bool state)
Definition: ScriptInterpreter.cc:205
SimControlLanguageCommand
Definition: SimControlLanguageCommand.hh:63
FileSystem::fileIsReadable
static bool fileIsReadable(const std::string fileName)
MemWriteCommand::MemWriteCommand
MemWriteCommand()
Definition: MemWriteCommand.cc:49