-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memory.cpp
70 lines (56 loc) · 1.78 KB
/
Memory.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "Memory.h"
Memory::Memory(int numBytes, uint32_t offset, bool isDataMem) {
this->offset = offset;
this->numBytes = numBytes;
numWords = numBytes >> 2;
this->isDataMem = isDataMem;
type = isDataMem ? "data" : "inst";
mem = new uint32_t[numWords];
if(!mem) {
cerr << "error: out of memory" << endl;
exit(-1);
}
}
void Memory::storeWord(uint32_t data, uint32_t addr) {
if((addr & 3) != 0) {
cerr << "unaligned " << type << " access: 0x" << hex << addr << endl;
exit(-1);
}
uint32_t index = (addr - offset) >> 2;
if(index >= numWords) {
cerr << type << " memory access out of range: 0x" << hex << addr << endl;
exit(-1);
}
D(if(isDataMem) cout << " MEM WR: addr = 0x" << hex << addr << ", data = 0x" << data << dec << endl);
mem[index] = data;
}
uint32_t Memory::loadWord(uint32_t addr) {
if((addr & 3) != 0) {
cerr << "unaligned " << type << " memory access: 0x" << hex << addr << endl;
exit(-1);
}
uint32_t index = (addr - offset) >> 2;
if(index >= numWords) {
cerr << type << " memory access out of range: 0x" << hex << addr << endl;
exit(-1);
}
D(if(isDataMem) cout << " MEM RD: addr = 0x" << hex << addr << ", data = 0x" << mem[index] << dec << endl);
return mem[index];
}
uint32_t Memory::swizzle(uint8_t *bytes) {
return (bytes[0] << 24) | (bytes[1] << 16) | bytes[2] << 8 | bytes[3];
}
void Memory::initFromExe(ifstream &exeFile, int count) {
uint8_t bytes[4];
if(count > numWords) {
cerr << "allocated " << type << " array not big enough for " << count << " words" << endl;
exit(-1);
}
for(int i = 0; i < count; i++) {
if(!exeFile.read((char *)&bytes, 4)) {
cerr << "error: could not read words from file" << endl;
exit(-1);
}
mem[i] = swizzle(bytes);
}
}