OpenASIP 2.2
Loading...
Searching...
No Matches
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
52
53
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 */
67bool
68MemWriteCommand::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
132std::string
Word UIntWord
Definition BaseType.hh:144
bool checkArgumentCount(int argumentCount, int minimum, int maximum)
ScriptInterpreter * interpreter() const
static bool fileIsReadable(const std::string fileName)
static uintmax_t sizeInBytes(const std::string &filePath)
static bool fileExists(const std::string fileName)
virtual ~MemWriteCommand()
virtual std::string helpText() const
virtual bool execute(const std::vector< DataObject > &arguments)
boost::shared_ptr< Memory > MemoryPtr
virtual void setError(bool state)
virtual void setResult(DataObject *result)
bool setMemoryPointer(MemorySystem::MemoryPtr &memory, const std::string &addressSpaceName)
bool setMemoryAddress(const std::string &addressString, std::string &addressSpaceName, std::size_t &memoryAddress)
static SimulatorTextGenerator & textGenerator()
static bool ciEqual(const std::string &a, const std::string &b)
virtual boost::format text(int textId)
@ TXT_INTERP_HELP_LOADDATA
Help text for command "load_data" of the CLI.