OpenASIP  2.0
MemoryGridTable.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 MemoryGridTable.cc
26  *
27  * Implementation of MemoryGridTable 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 "MemoryGridTable.hh"
35 #include "Conversion.hh"
36 #include "WxConversion.hh"
37 #include "Memory.hh"
38 
39 using std::string;
40 
41 const Word MemoryGridTable::MAX_ROWS = 600000000;
42 const string MemoryGridTable::NOT_AVAILABLE = "N/A";
43 
44 /**
45  * The Constructor.
46  *
47  * @param memory Memory to display in the grid.
48  */
50  wxGridTableBase(),
51  memory_(memory),
52  dataMode_(DATA_HEX),
53  sizeMode_(SIZE_MAU),
54  numberOfColumns_(8) {
55 
56  start_ = memory.start();
57  end_ = memory.end();
58  mauSize_ = memory.MAUSize();
59 
60  // Kludge to avoid overflow with some calculations when the AS size = 2^32.
61  // The grid-widget is not capable of displaying enough rows,
62  // so the last address wouldn't be displayed anyway.
63  if (start_ == 0 && end_ == 0xffffffff) end_ = 0xfffffffe;
64 }
65 
66 
67 /**
68  * The Destructor.
69  */
71 }
72 
73 
74 /**
75  * Returns row count of the grid.
76  *
77  * The row count is limited to MAX_ROWS due to limtiations of wxGrid.
78  *
79  * @return Number of rows in the table.
80  */
81 int
83 
84  Word size = end_ - start_ + 1;
85  Word cells = size / sizeOfCell();
86  Word rows = cells / numberOfColumns_;
87  if ((cells % numberOfColumns_) != 0) {
88  rows++;
89  }
90  if (rows > MAX_ROWS) {
91  rows = MAX_ROWS;
92  }
93  return rows;
94 }
95 
96 /**
97  * Returns column count of the grid.
98  *
99  * @return Number of coulmns in the table.
100  */
101 int
103  return numberOfColumns_;
104 }
105 
106 
107 /**
108  * Returns true, if the given cell is empty, false otherwise.
109  *
110  * @param row Row of the cell.
111  * @param col Column of the cell.
112  * @return True, if the column is empty.
113  */
114 bool
115 MemoryGridTable::IsEmptyCell(int /* row UNUSED */, int /* col UNUSED */) {
116  return false;
117 }
118 
119 
120 /**
121  * Returns cell value as a wxString.
122  *
123  * Returns memory contents corresponding to the cell coordinates. The
124  * memory value is formatted to the string depending on the size and
125  * type modes set.
126  *
127  * @param row Row of the cell.
128  * @param col Column of the cell.
129  * @return Cell contents as a wxString.
130  */
131 wxString
132 MemoryGridTable::GetValue(int row, int col) {
133 
134  unsigned addr =
135  start_ + (row * numberOfColumns_ * sizeOfCell()) +
136  (col * sizeOfCell());
137 
138  if ((addr + sizeOfCell()) > (end_ + 1)) {
140  }
141 
142  wxString value = memoryContents(addr);
143  return value;
144 }
145 
146 
147 /**
148  * Returns row label of a grid row.
149  *
150  * The label is the memory address of the first cell in the row.
151  *
152  * @param row Row number.
153  * @return Label for the grid row.
154  */
155 wxString
157 
158  string address = Conversion::toHexString(
159  start_ + row * numberOfColumns_ * sizeOfCell());
160 
161  return WxConversion::toWxString(address);
162 }
163 
164 
165 /**
166  * Returns column label of a grid column.
167  *
168  * The label is the offset of the column compared to the first cell in the
169  * row.
170  *
171  * @param col Column number.
172  * @return Label for the grid column.
173  */
174 wxString
176  string offset = Conversion::toHexString(col * sizeOfCell());
177  return WxConversion::toWxString(offset);
178 }
179 
180 
181 /**
182  * Not implemented, use setCellValue() instead.
183  */
184 void
185 MemoryGridTable::SetValue(int, int, const wxString&) {
186  // Do nothing.
187 }
188 
189 
190 /**
191  * Returns contents of the given memory contents as a wxString.
192  *
193  * The string formatting depends on the current sizeMode_ and dataMode_ set.
194  *
195  * @param addr Memory address to return.
196  * @return Memory contents as a wxString.
197  */
198 wxString
200 
201  unsigned size = sizeOfCell();
202  string dataString = NOT_AVAILABLE;
203  unsigned int cellSize = sizeOfCell() * mauSize_;
204 
205  if ((size * mauSize_) <= sizeof(SIntWord) * BYTE_BITWIDTH) {
206 
207  // read one word
208  ULongWord data = 0;
209 
210  if (addr < start_ || addr > end_) {
211  // memory not available
213  } else {
214  memory_.read(addr, size, data);
215  }
216 
217 
218  if (dataMode_ == DATA_BIN) {
219  dataString =
220  Conversion::toBinary(static_cast<int>(data), cellSize);
221  } else if (dataMode_ == DATA_HEX) {
222  unsigned digits = cellSize / 4;
223  if ((cellSize % 4) != 0) {
224  cellSize++;
225  }
226  dataString = Conversion::toHexString(data, digits);
227  } else if (dataMode_ == DATA_SIGNED_INT) {
228  int extendedValue = data;
229  extendedValue =
230  extendedValue <<
231  ((sizeof(extendedValue)*BYTE_BITWIDTH) - cellSize);
232  extendedValue =
233  extendedValue >>
234  ((sizeof(extendedValue)*BYTE_BITWIDTH) - cellSize);
235  dataString = Conversion::toString(extendedValue);
236  } else if (dataMode_ == DATA_UNSIGNED_INT) {
237  dataString = Conversion::toString(data);
238  } else if (dataMode_ == DATA_FLOAT &&
239  cellSize == sizeof(FloatWord) * BYTE_BITWIDTH) {
240 
241  FloatWord flt;
242  memory_.read(addr, flt);
243  dataString = Conversion::toString(flt);
244  }
245  } else if ((size * mauSize_) <= sizeof(DoubleWord) * BYTE_BITWIDTH) {
246 
247  // Only double display is available due to the limitations of
248  // stringstream hex/bin conversion.
249  if (dataMode_ == DATA_DOUBLE
250  && cellSize == sizeof(DoubleWord) * BYTE_BITWIDTH) {
251 
252  // read one double word
253  DoubleWord data = 0;
254  if (addr < start_ || addr > end_) {
255  // memory not available
257  } else {
258  memory_.read(addr, data);
259  }
260 
261  dataString = Conversion::toString(data);
262  }
263  }
264  return WxConversion::toWxString(dataString);
265 }
266 
267 
268 
269 /**
270  * Sets the number of columns to display.
271  *
272  * @param columns New table width.
273  */
274 void
276  numberOfColumns_ = columns;
277 }
278 
279 
280 /**
281  * Sets the data display mode of the table.
282  *
283  * @param mode Data display mode to set.
284  */
285 void
287  dataMode_ = mode;
288 }
289 
290 
291 /**
292  * Sets the memory size per cell.
293  *
294  * @param mode Size mode to set.
295  */
296 void
298  sizeMode_ = mode;
299 }
300 
301 
302 /**
303  * Sets the memory value correspoding to a cell.
304  *
305  * @param row Row of the cell.
306  * @param column Column of the cell.
307  * @param memoryValue Value to set.
308  */
309 void
310 MemoryGridTable::writeValue(int row, int column, UIntWord memoryValue) {
311  Word address = start_;
312  address += cellAddress(row, column);
313  int size = sizeOfCell();
314  if (address < end_) {
315  memory_.write(address, size, memoryValue);
317  }
318 }
319 
320 
321 /**
322  * Sets the memory value correspoding to a cell.
323  *
324  * @param row Row of the cell.
325  * @param column Column of the cell.
326  * @param memoryValue Value to set.
327  */
328 void
329 MemoryGridTable::writeValue(int row, int column, DoubleWord memoryValue) {
330  Word address = start_;
331  address += cellAddress(row, column);
332  if (address < end_) {
333  memory_.write(address, memoryValue);
335  }
336 }
337 
338 
339 /**
340  * Calculates the address of given cell.
341  *
342  * @param row The selected row.
343  * @param colummn The selected column.
344  * @return Address of the cell.
345  */
346 Word
347 MemoryGridTable::cellAddress(int row, int column) const {
348  Word address = 0;
349  unsigned size = sizeOfCell();
350  address += column * size;
351  address += row * numberOfColumns_ * size;
352  return address;
353 }
354 
355 
356 /**
357  * Returns row and column nubmer of the address in the table.
358  *
359  * @param address Memory address to find.
360  * @param row Variable to set the row to.
361  * @param col Variable to set the column to.
362  */
363 void
364 MemoryGridTable::findAddress(Word addr, int& row, int& col) {
365  unsigned cellSize = sizeOfCell();
366  row = addr / ((unsigned)numberOfColumns_ * cellSize);
367  col = (addr % ((unsigned)numberOfColumns_ * cellSize)) / cellSize;
368 }
369 
370 
371 /**
372  * Returns size of memory displayed in a single cell.
373  *
374  * @return Memory size of a cell.
375  */
376 unsigned
378  if (sizeMode_ == SIZE_MAU) {
379  return 1;
380  } else if (sizeMode_ == SIZE_TWO_MAUS) {
381  return 2;
382  } else if (sizeMode_ == SIZE_FOUR_MAUS) {
383  return 4;
384  } else if (sizeMode_ == SIZE_EIGHT_MAUS) {
385  return 8;
386  }
387  assert(false);
388  return 0;
389 }
UIntWord
Word UIntWord
Definition: BaseType.hh:144
MemoryGridTable::memory_
Memory & memory_
Memory to access.
Definition: MemoryGridTable.hh:90
MemoryGridTable::sizeMode_
SizeMode sizeMode_
Current size mode of the window (1/2/4... MAUs per cell).
Definition: MemoryGridTable.hh:104
WxConversion::toWxString
static wxString toWxString(const std::string &source)
mode
mode
Definition: tceopgen.cc:45
MemoryGridTable::setDataMode
void setDataMode(DataMode mode)
Definition: MemoryGridTable.cc:286
Memory.hh
MemoryGridTable::SetValue
virtual void SetValue(int row, int col, const wxString &value)
Definition: MemoryGridTable.cc:185
MemoryGridTable::GetNumberRows
virtual int GetNumberRows()
Definition: MemoryGridTable.cc:82
Memory::MAUSize
virtual ULongWord MAUSize()
Definition: Memory.hh:118
MemoryGridTable::memoryContents
wxString memoryContents(ULongWord addr)
Definition: MemoryGridTable.cc:199
MemoryGridTable::GetNumberCols
virtual int GetNumberCols()
Definition: MemoryGridTable.cc:102
MemoryGridTable::IsEmptyCell
virtual bool IsEmptyCell(int row, int col)
Definition: MemoryGridTable.cc:115
MemoryGridTable::SIZE_MAU
@ SIZE_MAU
Definition: MemoryGridTable.hh:65
MemoryGridTable::DATA_SIGNED_INT
@ DATA_SIGNED_INT
Definition: MemoryGridTable.hh:74
MemoryGridTable::SizeMode
SizeMode
Definition: MemoryGridTable.hh:64
Conversion::toString
static std::string toString(const T &source)
MemoryGridTable::DATA_UNSIGNED_INT
@ DATA_UNSIGNED_INT
Definition: MemoryGridTable.hh:75
assert
#define assert(condition)
Definition: Application.hh:86
MemoryGridTable::numberOfColumns_
unsigned numberOfColumns_
Current number of columns in the grid.
Definition: MemoryGridTable.hh:108
Conversion::toBinary
static std::string toBinary(unsigned int source, unsigned int stringWidth=0)
Definition: Conversion.cc:155
MemoryGridTable::DATA_HEX
@ DATA_HEX
Definition: MemoryGridTable.hh:73
MemoryGridTable::DataMode
DataMode
Definition: MemoryGridTable.hh:71
Conversion.hh
FloatWord
float FloatWord
Definition: BaseType.hh:160
MemoryGridTable::DATA_FLOAT
@ DATA_FLOAT
Definition: MemoryGridTable.hh:77
MemoryGridTable::SIZE_EIGHT_MAUS
@ SIZE_EIGHT_MAUS
Definition: MemoryGridTable.hh:68
MemoryGridTable::start_
Word start_
Start address of the memory range to display.
Definition: MemoryGridTable.hh:92
MemoryGridTable::GetValue
virtual wxString GetValue(int row, int col)
Definition: MemoryGridTable.cc:132
SIntWord
SignedWord SIntWord
Definition: BaseType.hh:149
MemoryGridTable::GetRowLabelValue
virtual wxString GetRowLabelValue(int row)
Definition: MemoryGridTable.cc:156
MemoryGridTable::dataMode_
DataMode dataMode_
Current data mode of the window (hex/binary/int...).
Definition: MemoryGridTable.hh:102
DoubleWord
double DoubleWord
Definition: BaseType.hh:166
MemoryGridTable::findAddress
void findAddress(Word addr, int &row, int &col)
Definition: MemoryGridTable.cc:364
Memory::read
virtual Memory::MAU read(ULongWord address)=0
Definition: Memory.cc:160
MemoryGridTable::GetColLabelValue
virtual wxString GetColLabelValue(int col)
Definition: MemoryGridTable.cc:175
MemoryGridTable::mauSize_
int mauSize_
Size of MAU in bits.
Definition: MemoryGridTable.hh:106
Conversion::toHexString
static std::string toHexString(T source, std::size_t digits=0, bool include0x=true)
MemoryGridTable::writeValue
void writeValue(int row, int column, UIntWord memoryValue)
Definition: MemoryGridTable.cc:310
MemoryGridTable::DATA_DOUBLE
@ DATA_DOUBLE
Definition: MemoryGridTable.hh:76
MemoryGridTable::setNumberOfColumns
void setNumberOfColumns(unsigned columns)
Definition: MemoryGridTable.cc:275
Memory::advanceClock
virtual void advanceClock()
Definition: Memory.cc:819
MemoryGridTable::MAX_ROWS
static const Word MAX_ROWS
Maximum number of rows to display in the window.
Definition: MemoryGridTable.hh:100
MemoryGridTable.hh
Memory::write
virtual void write(ULongWord address, MAU data)=0
Definition: Memory.cc:95
BYTE_BITWIDTH
const Byte BYTE_BITWIDTH
Definition: BaseType.hh:136
MemoryGridTable::setSizeMode
void setSizeMode(SizeMode mode)
Definition: MemoryGridTable.cc:297
MemoryGridTable::MemoryGridTable
MemoryGridTable(Memory &memory)
Definition: MemoryGridTable.cc:49
Memory::end
virtual ULongWord end()
Definition: Memory.hh:117
MemoryGridTable::end_
Word end_
End address of the memory range to display.
Definition: MemoryGridTable.hh:94
ULongWord
unsigned long ULongWord
Definition: BaseType.hh:51
WxConversion.hh
MemoryGridTable::SIZE_FOUR_MAUS
@ SIZE_FOUR_MAUS
Definition: MemoryGridTable.hh:67
MemoryGridTable::cellAddress
Word cellAddress(int row, int column) const
Definition: MemoryGridTable.cc:347
MemoryGridTable::sizeOfCell
unsigned sizeOfCell() const
Definition: MemoryGridTable.cc:377
MemoryGridTable::DATA_BIN
@ DATA_BIN
Definition: MemoryGridTable.hh:72
MemoryGridTable::SIZE_TWO_MAUS
@ SIZE_TWO_MAUS
Definition: MemoryGridTable.hh:66
Memory
Definition: Memory.hh:74
MemoryGridTable::NOT_AVAILABLE
static const std::string NOT_AVAILABLE
String that is displayed in a cell that is not in the current AS.
Definition: MemoryGridTable.hh:97
MemoryGridTable::~MemoryGridTable
virtual ~MemoryGridTable()
Definition: MemoryGridTable.cc:70
Memory::start
virtual ULongWord start()
Definition: Memory.hh:116