-
Notifications
You must be signed in to change notification settings - Fork 2
/
mem32.cpp
53 lines (42 loc) · 1.1 KB
/
mem32.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//imem.cpp
#include <vector>
#include <string>
using std::string;
#include "mem32.h"
/**
* reads \c mem32 memory contents from file \c filename.
* The first number in each line of file is added to memory as an 32 bit
* integer.
* The number is in base 16. Lines that do not start with a number are ignored.
* The size of memory is given by the valid lines.
*/
void mem32::init_memory(const char *filename)
{
unsigned int val;
string str;
ifstream data(filename);
if(!data) {
fprintf(stderr,"ERROR: Could not find file %s\n",filename);
exit(1);
}
memory.clear();
std::getline(data, str);
while(!data.eof()) {
if (sscanf(str.c_str(),"%x",&val)==1) {
memory.push_back(val);
}
std::getline(data, str);
}
}
/** return the number of bytes in memory */
unsigned int mem32::size()
{
return memory.size()*4;
}
/** return memory contents at addr address. Considers addr as a byte address.
* Only works if addr is a multiple of 4. */
sc_uint<32> mem32::at(sc_uint<32> addr)
{
assert(addr<size() && addr % 4 == 0);
return memory[addr / 4];
}