orca-sim
Public Member Functions | Private Attributes | List of all members
orcasim::modeling::Memory Class Reference

This class models a memory module. More...

#include <Memory.hpp>

Inheritance diagram for orcasim::modeling::Memory:
orcasim::base::UntimedModel orcasim::base::Model

Public Member Functions

 Memory (std::string name, uint32_t size, uint32_t base=0, bool wipe=false, std::string binname="")
 Construct a new Memory object. More...
 
 ~Memory ()
 Destroy the Memory object. More...
 
void Write (uint32_t addr, MemoryType *data, uint32_t length)
 Writes data to the memory. More...
 
void Read (uint32_t addr, MemoryType *buffer, uint32_t length)
 Reads data from a given memory location. More...
 
void LoadBin (std::string filename, MemoryAddr base, uint32_t size)
 Loads the content of a given file to the memory. More...
 
void SaveBin (std::string filename, MemoryAddr base, uint32_t size)
 Write the contents of memory cells into a binary file. More...
 
void Wipe ()
 Write zeroes to the whole addressable memory space. More...
 
void Wipe (MemoryAddr base, uint32_t length)
 Write zeroes to a region of the memory. More...
 
void Dump (uint32_t base, uint32_t length)
 Display the contents of the memory on the output. More...
 
void Dump ()
 Display the contents of the whole memory on the output. More...
 
MemoryTypeoperator[] (MemoryAddr addr)
 Shorthand access for the Read method using operator overloading on '[]' operator. More...
 
MemoryAddr GetBase ()
 (getter) Gets the base address, which is the first addressable memory cell in the module. More...
 
MemoryAddr GetSize ()
 (getter) Gets the size (alternatively, length) of the memory module, representing the number of addressable cells. More...
 
MemoryAddr GetLastAddr ()
 Get the address of the last addressable memory cell. More...
 
MemoryTypeGetMap (MemoryAddr addr)
 Gets a pointer of MemoryType type that points to the cell in address <addr>. More...
 
a name that identifies the model, advisably not empty.

Default ctor.

std::string GetName ()
 Getter method for the <_name> field. More...
 
void SetName (std::string s)
 Setter method for the <_name> field. More...
 

Private Attributes

MemoryType_mem
 The _mem attribute is an array of MemoryType elements, where MemoryType is the smalled memory unit that can be read or writen to the memory. More...
 
MemoryAddr _length
 The _length attribute denotes the number of cells of the memory module. More...
 
MemoryAddr _base
 The _base attribute indicates the first address of the memory module, an offset. More...
 

Detailed Description

This class models a memory module.

The module has no clock as it is an UntimedModel *

Definition at line 55 of file Memory.hpp.

Constructor & Destructor Documentation

§ Memory()

Memory::Memory ( std::string  name,
uint32_t  size,
uint32_t  base = 0,
bool  wipe = false,
std::string  binname = "" 
)

Construct a new Memory object.

Parameters
nameA string name to identify the object. There is no restriction on object names.
sizeThe number of cells to be emulated. The size of cell is defined by the MemoryType type.
base(optional) The base address for the emulated memory space. Default value is zero.
wipe(optional) If set to true, erase memory cells after class instantiation, writing zero to each cell. Default behavior
binname(optional)

Definition at line 36 of file Memory.cpp.

37  : UntimedModel(name) {
38  // creates a new array of MemoryType elements (must be disposed by dctor.)
39  _mem = new MemoryType[size];
40 
41  // set internal fields
42  _length = size;
43  _base = sram_base;
44 
45  // if wipe is true, write zeroes to the whole addressable space
46  if (wipe) this->Wipe();
47 
48  // if a binary file is provided, load that file into the base address
49  if (binname != "")
50  this->LoadBin(binname, _base, _length);
51 
52  #ifdef MEMORY_ENABLE_COUNTERS
53  // if counters are enabled, initiate the respective signals
54  // please note that these signals are not mapped to anywhere yet
55  _counter_nstore = new Signal<uint32_t>(GetName() + ".counters.store");
56  _counter_nload = new Signal<uint32_t>(GetName() + ".counters.load");
57 
58  // reset counters (must!)
59  _counter_nstore->Write(0);
60  _counter_nload->Write(0);
61  #endif
62 }
MemoryAddr _length
The _length attribute denotes the number of cells of the memory module.
Definition: Memory.hpp:71
MemoryType * _mem
The _mem attribute is an array of MemoryType elements, where MemoryType is the smalled memory unit th...
Definition: Memory.hpp:63
void Wipe()
Write zeroes to the whole addressable memory space.
Definition: Memory.cpp:189
UntimedModel(std::string name)
Default Ctor.
void LoadBin(std::string filename, MemoryAddr base, uint32_t size)
Loads the content of a given file to the memory.
Definition: Memory.cpp:222
MemoryAddr _base
The _base attribute indicates the first address of the memory module, an offset.
Definition: Memory.hpp:80
std::string GetName()
Getter method for the <_name> field.
Definition: Model.cpp:34
#define MemoryType
Definition: MemoryType.hpp:33

§ ~Memory()

Memory::~Memory ( )

Destroy the Memory object.

Definition at line 289 of file Memory.cpp.

289  {
290  // delete all cells
291  delete [] _mem;
292 
293  #ifdef MEMORY_ENABLE_COUNTERS
294  // delete signals that hold counters
295  delete(_counter_nstore);
296  delete(_counter_nload);
297  #endif
298 }
MemoryType * _mem
The _mem attribute is an array of MemoryType elements, where MemoryType is the smalled memory unit th...
Definition: Memory.hpp:63

Member Function Documentation

§ Dump() [1/2]

void Memory::Dump ( uint32_t  base,
uint32_t  length 
)

Display the contents of the memory on the output.

Data will be displayed as hexadecimal values.

Parameters
baseThe first address to display
lengthThe number of cells to display

Definition at line 259 of file Memory.cpp.

259  {
260  uint32_t k, l;
261 
262  // mask is necessary to correct a bug(?) when printing
263  // negative hexas.
264  uint32_t mask = 0x000000FF;
265  int8_t ch;
266 
267  // print in lines containing 16 bytes
268  for (k = 0; k < length; k += 16) {
269  // print the address
270  printf("\n%08x ", base + k);
271  for (l = 0; l < 16; l++) {
272  printf("%02x ", _mem[k + l + (base - _base)] & mask);
273  if (l == 7) putchar(' ');
274  }
275 
276  // print the data in ascii ("." is printed when no ascii char exists)
277  printf(" |");
278  for (l = 0; l < 16; l++) {
279  ch = _mem[k + l + (base - _base)];
280  if ((ch >= 32) && (ch <= 126))
281  putchar(ch);
282  else
283  putchar('.');
284  }
285  putchar('|');
286  }
287 }
MemoryType * _mem
The _mem attribute is an array of MemoryType elements, where MemoryType is the smalled memory unit th...
Definition: Memory.hpp:63
MemoryAddr _base
The _base attribute indicates the first address of the memory module, an offset.
Definition: Memory.hpp:80

§ Dump() [2/2]

void Memory::Dump ( )

Display the contents of the whole memory on the output.

Definition at line 255 of file Memory.cpp.

255  {
256  this->Dump(0, _length);
257 }
MemoryAddr _length
The _length attribute denotes the number of cells of the memory module.
Definition: Memory.hpp:71
void Dump()
Display the contents of the whole memory on the output.
Definition: Memory.cpp:255

§ GetBase()

MemoryAddr Memory::GetBase ( )

(getter) Gets the base address, which is the first addressable memory cell in the module.

Returns
MemoryAddr The address of the first memory cell.

Definition at line 173 of file Memory.cpp.

173  {
174  return _base;
175 }
MemoryAddr _base
The _base attribute indicates the first address of the memory module, an offset.
Definition: Memory.hpp:80

§ GetLastAddr()

MemoryAddr Memory::GetLastAddr ( )

Get the address of the last addressable memory cell.

The address is given by (base + length - 1).

Returns
MemoryAddr The address of the last cell.

Definition at line 183 of file Memory.cpp.

183  {
184  return (_base + _length) - 1;
185 }
MemoryAddr _length
The _length attribute denotes the number of cells of the memory module.
Definition: Memory.hpp:71
MemoryAddr _base
The _base attribute indicates the first address of the memory module, an offset.
Definition: Memory.hpp:80

§ GetMap()

MemoryType * Memory::GetMap ( MemoryAddr  addr)

Gets a pointer of MemoryType type that points to the cell in address <addr>.

Although the pointer points to a single cell, neighbor cells can be accessed by incrementing on the pointer. Please note that this method bypassed the encapsuling methods Write and Read, and its use should be avoided for data manipulation purpose.

Parameters
addrThe addres to which the pointer must point to.
Returns
MemoryType* A pointer to the given address.

Definition at line 144 of file Memory.cpp.

144  {
145  #ifdef MEMORY_MAP_ADDRESS_CHECKING
146  // check whether we're trying to get the pointer
147  // to a cell whose address is lower than the base
148  // address
149  if (addr < _sram_base) {
150  stringstream s;
151  s << this->GetName() << ": unable to map from to addr (0x" << std::hex
152  << addr << ") lower than sram base.";
153  throw std::runtime_error(s.str());
154  }
155 
156  // check whether we're trying to get the pointer
157  // to a cell whose address is higher than the last
158  // address
159  if (addr > GetLastAddr()) {
160  stringstream s;
161  s << this->GetName() << ": unable to map from to addr (0x" << std::hex
162  << addr << ") higher than last mapped address of (0x" << std::hex
163  << GetLastAddr() << ").";
164  throw std::runtime_error(s.str());
165  }
166  #endif
167 
168  // return the address to the requested cell
169  return &(_mem[addr - _base]);
170 }
MemoryType * _mem
The _mem attribute is an array of MemoryType elements, where MemoryType is the smalled memory unit th...
Definition: Memory.hpp:63
MemoryAddr GetLastAddr()
Get the address of the last addressable memory cell.
Definition: Memory.cpp:183
MemoryAddr _base
The _base attribute indicates the first address of the memory module, an offset.
Definition: Memory.hpp:80
std::string GetName()
Getter method for the <_name> field.
Definition: Model.cpp:34

§ GetName()

std::string Model::GetName ( )
inherited

Getter method for the <_name> field.

Definition at line 34 of file Model.cpp.

34  {
35  return _name;
36 }
std::string _name
A name for the model.
Definition: Model.hpp:47

§ GetSize()

uint32_t Memory::GetSize ( )

(getter) Gets the size (alternatively, length) of the memory module, representing the number of addressable cells.

Returns
MemoryAddr The number of addressable cells.

Definition at line 178 of file Memory.cpp.

178  {
179  return _length;
180 }
MemoryAddr _length
The _length attribute denotes the number of cells of the memory module.
Definition: Memory.hpp:71

§ LoadBin()

void Memory::LoadBin ( std::string  filename,
MemoryAddr  base,
uint32_t  size 
)

Loads the content of a given file to the memory.

File contents are treated as binaray data.

Parameters
filenameName of the file to be read.
locationLocation of the memory area in which the contents of the loaded file will be put.

Definition at line 222 of file Memory.cpp.

222  {
223  // try to open the file
224  std::ifstream f(filename, std::ios::binary | std::ios::in | std::ios::out);
225 
226  // if we could open the file properly, load it at the base address
227  if (f.is_open()) {
228  f.read(reinterpret_cast<char*>(&_mem[base -_base]),
229  sizeof(_mem[0]) * size);
230  f.close();
231 
232  // if could not open the file, throw exception
233  } else {
234  std::string err_msg = this->GetName() + ": unable to load '"
235  + filename + "'.";
236  throw std::runtime_error(err_msg);
237  }
238 }
MemoryType * _mem
The _mem attribute is an array of MemoryType elements, where MemoryType is the smalled memory unit th...
Definition: Memory.hpp:63
MemoryAddr _base
The _base attribute indicates the first address of the memory module, an offset.
Definition: Memory.hpp:80
std::string GetName()
Getter method for the <_name> field.
Definition: Model.cpp:34

§ operator[]()

MemoryType& orcasim::modeling::Memory::operator[] ( MemoryAddr  addr)
inline

Shorthand access for the Read method using operator overloading on '[]' operator.

On contrary of Read method, this operator is capable of returning only a single memory cell value. Please note that its counter for writing is not implemented.

Parameters
addrThe address of the cell to read from
Returns
MemoryType The value stored in the request cell

Definition at line 192 of file Memory.hpp.

192  {
193  return _mem[_base + addr];
194  }
MemoryType * _mem
The _mem attribute is an array of MemoryType elements, where MemoryType is the smalled memory unit th...
Definition: Memory.hpp:63
MemoryAddr _base
The _base attribute indicates the first address of the memory module, an offset.
Definition: Memory.hpp:80

§ Read()

void Memory::Read ( uint32_t  addr,
MemoryType buffer,
uint32_t  length 
)

Reads data from a given memory location.

Parameters
addrLocation to read from.
bufferBuffer to where the read data will be copied.
lengthLength of the data that will be copyied from memory to the buffer.

Definition at line 111 of file Memory.cpp.

111  {
112  #ifdef MEMORY_READ_ADDRESS_CHECKING
113  // check whether we'are trying to read from an address
114  // lower than the base address
115  if (addr < _sram_base) {
116  stringstream s;
117  s << this->GetName() << ": unable to read from addr (0x" << std::hex
118  << addr << ") lower than sram base.";
119  throw std::runtime_error(s.str());
120  }
121 
122  // check whether we're trying to read from an address
123  // higher than the last address
124  if ((addr + length) - 1 > GetLastAddr()) {
125  stringstream s;
126  s << this->GetName() << ": unable to read from addr (0x" << std::hex
127  << addr << ") higher than last mapped address of (0x" << std::hex
128  << GetLastAddr() << ").";
129  throw std::runtime_error(s.str());
130  }
131  #endif
132 
133  // read data from memory
134  // @TODO(ad): check whether this has the best performance
135  for (uint32_t i = 0; i < length; i++)
136  buffer[i] = _mem[(addr - _base) + i];
137 
138  #ifdef MEMORY_ENABLE_COUNTERS
139  // increment number of loades into nstore counter
140  _counter_nload->Inc(1);
141  #endif
142 }
MemoryType * _mem
The _mem attribute is an array of MemoryType elements, where MemoryType is the smalled memory unit th...
Definition: Memory.hpp:63
MemoryAddr GetLastAddr()
Get the address of the last addressable memory cell.
Definition: Memory.cpp:183
MemoryAddr _base
The _base attribute indicates the first address of the memory module, an offset.
Definition: Memory.hpp:80
std::string GetName()
Getter method for the <_name> field.
Definition: Model.cpp:34

§ SaveBin()

void Memory::SaveBin ( std::string  filename,
MemoryAddr  base,
uint32_t  size 
)

Write the contents of memory cells into a binary file.

Parameters
filenamefile name, overwrites file is it exists already
sizenumber of memory cells to save
addraddress of the first cell to save

Definition at line 240 of file Memory.cpp.

240  {
241  std::ofstream f(filename, std::ifstream::binary);
242 
243  if (f.is_open()) {
244  f.write(reinterpret_cast<char*>(&_mem[base -_base]),
245  sizeof(_mem[0]) * size);
246  f.close();
247  } else {
248  std::string err_msg = this->GetName() +
249  ": unable to save'" + filename + "'.";
250  throw std::runtime_error(err_msg);
251  }
252 }
MemoryType * _mem
The _mem attribute is an array of MemoryType elements, where MemoryType is the smalled memory unit th...
Definition: Memory.hpp:63
MemoryAddr _base
The _base attribute indicates the first address of the memory module, an offset.
Definition: Memory.hpp:80
std::string GetName()
Getter method for the <_name> field.
Definition: Model.cpp:34

§ SetName()

void Model::SetName ( std::string  s)
inherited

Setter method for the <_name> field.

Parameters
sValue to be set

Definition at line 38 of file Model.cpp.

38  {
39  _name = name;
40 }
std::string _name
A name for the model.
Definition: Model.hpp:47

§ Wipe() [1/2]

void Memory::Wipe ( )

Write zeroes to the whole addressable memory space.

Definition at line 189 of file Memory.cpp.

189  {
190  this->Wipe(_base, _length);
191 }
MemoryAddr _length
The _length attribute denotes the number of cells of the memory module.
Definition: Memory.hpp:71
void Wipe()
Write zeroes to the whole addressable memory space.
Definition: Memory.cpp:189
MemoryAddr _base
The _base attribute indicates the first address of the memory module, an offset.
Definition: Memory.hpp:80

§ Wipe() [2/2]

void Memory::Wipe ( MemoryAddr  base,
uint32_t  length 
)

Write zeroes to a region of the memory.

Parameters
baseThe starting address, will be the first address to be writen
lengthNumber of cells to write zeroes to. Cells will follow the position of the base cell.

Definition at line 193 of file Memory.cpp.

193  {
194  #ifdef MEMORY_WRITE_ADDRESS_CHECKING
195  // base must be less than the last address
196  if (base < _base)
197  throw std::runtime_error(this->GetName() +
198  ": unable to wipe from base (" + std::to_string(base) +
199  ") lower than sram base .");
200 
201  // base must be less than the last address
202  if (base > GetLastAddr())
203  throw std::runtime_error(this->GetName() +
204  ": unable to wipe from base (" + std::to_string(base) +
205  ") higher than sram base .");
206 
207  // base + size cannot go further than the last address
208  if (base + size > GetLastAddr())
209  throw std::runtime_error(this->GetName() +
210  ": unable to wipe that much length (" + std::to_string(size) +
211  ") higher than sram base + length.");
212  #endif
213 
214  // get the pointer to the desired base
215  MemoryType* range_ptr = _mem + (base - _base);
216 
217  // write zeros to the data to up to <size> cells.
218  for (uint32_t i = 0; i < size; i++)
219  range_ptr[i] = 0;
220 }
MemoryType * _mem
The _mem attribute is an array of MemoryType elements, where MemoryType is the smalled memory unit th...
Definition: Memory.hpp:63
MemoryAddr GetLastAddr()
Get the address of the last addressable memory cell.
Definition: Memory.cpp:183
MemoryAddr _base
The _base attribute indicates the first address of the memory module, an offset.
Definition: Memory.hpp:80
std::string GetName()
Getter method for the <_name> field.
Definition: Model.cpp:34
#define MemoryType
Definition: MemoryType.hpp:33

§ Write()

void Memory::Write ( uint32_t  addr,
MemoryType data,
uint32_t  length 
)

Writes data to the memory.

Parameters
addrAddress of the first cell to receive the data. If data is largen than a single cell, neighbor cells will be writen.
dataA pointers of MemoryType that indicates the beggining of data to be copied.
lengthNumber of cells to write

Definition at line 75 of file Memory.cpp.

75  {
76  #ifdef MEMORY_WRITE_ADDRESS_CHECKING
77  // check whether we're trying to access an address
78  // lower than the base address
79  if (addr < _sram_base) {
80  stringstream s;
81  s << this->GetName() << ": unable to write to addr (0x" << std::hex
82  << addr << ") lower than sram base.";
83 
84  throw std::runtime_error(s.str());
85  }
86 
87  // check whether we're trying to access and address
88  // higher than the last address
89  if ((addr + length -1) > GetLastAddr()) {
90  stringstream s;
91  s << this->GetName() << ": unable to write to addr (0x" << std::hex
92  << addr << ") higher than last mapped address of (0x" << std::hex
93  << GetLastAddr() << ").";
94  throw std::runtime_error(s.str());
95  }
96  #endif
97 
98  #ifdef MEMORY_ENABLE_COUNTERS
99  // increment number of stores into nstore counter
100  _counter_nstore->Inc(1);
101  #endif
102 
103  // write data to the memory
104  // @TODO(ad): check whether this has the best performance
105  for (uint32_t i = 0; i < length; i++) {
106  _mem[addr - _base] = data[i];
107  addr++;
108  }
109 }
MemoryType * _mem
The _mem attribute is an array of MemoryType elements, where MemoryType is the smalled memory unit th...
Definition: Memory.hpp:63
MemoryAddr GetLastAddr()
Get the address of the last addressable memory cell.
Definition: Memory.cpp:183
MemoryAddr _base
The _base attribute indicates the first address of the memory module, an offset.
Definition: Memory.hpp:80
std::string GetName()
Getter method for the <_name> field.
Definition: Model.cpp:34

Member Data Documentation

§ _base

MemoryAddr orcasim::modeling::Memory::_base
private

The _base attribute indicates the first address of the memory module, an offset.

Since this model can only address contiguous cells, one may use multiple memory models to emulate a non-contiguous space.

Definition at line 80 of file Memory.hpp.

§ _length

MemoryAddr orcasim::modeling::Memory::_length
private

The _length attribute denotes the number of cells of the memory module.

The total number of memory space is given by sizeof(MemoryType) * _length (in bytes).

Definition at line 71 of file Memory.hpp.

§ _mem

MemoryType* orcasim::modeling::Memory::_mem
private

The _mem attribute is an array of MemoryType elements, where MemoryType is the smalled memory unit that can be read or writen to the memory.

Definition at line 63 of file Memory.hpp.


The documentation for this class was generated from the following files: