-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.cpp
126 lines (99 loc) · 3.04 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "memory.hpp"
#include <iostream>
#include <stdio.h>
#include <string.h>
#include "utility_functions.hpp"
using std::cout;
using std::endl;
Memory::Memory(){
}
uint8_t Memory::readAddress(uint32_t address){
if(memory_map.count(address)){
return memory_map[address];
}
else return 0;
}
void Memory::writeToAddress(uint32_t address, uint8_t data){
memory_map[address] = data;
}
void Memory::store(uint32_t address, uint8_t data){
writeToAddress(address, data);
}
void Memory::store(uint32_t address, uint16_t data){
uint32_t mask = 0b11111111;
uint32_t data_to_write;
for(int i=0; i < 2; i++){
data_to_write = data & mask;
data = data >> 8;
writeToAddress(address, data_to_write);
address ++;
}
}
void Memory::store(uint32_t address, uint32_t data){
uint32_t mask = 0b11111111;
uint32_t data_to_write;
for(int i=0; i < 4; i++){
data_to_write = data & mask;
data = data >> 8;
writeToAddress(address, data_to_write);
address ++;
}
}
void Memory::store(uint32_t address, uint64_t data){
uint32_t mask = 0b11111111;
uint32_t data_to_write;
for(int i=0; i < 8; i++){
data_to_write = data & mask;
data = data >> 8;
writeToAddress(address, data_to_write);
address ++;
}
}
void Memory::read(uint32_t address, uint8_t *data){
uint8_t tmp = readAddress(address);
memcpy(data, &tmp, sizeof(*data));
}
void Memory::read(uint32_t address, uint16_t *data){
uint16_t tmp = 0;
for(int i=0; i < 2; i++){
uint16_t read_byte = (uint16_t)readAddress(address);
tmp = tmp | (read_byte << (i*8));
address ++;
}
memcpy(data, &tmp, sizeof(*data));
}
void Memory::read(uint32_t address, uint32_t *data){
uint32_t tmp = 0;
for(int i=0; i < 4; i++){
uint32_t read_byte = (uint32_t)readAddress(address);
tmp = tmp | (read_byte << (i*8));
address ++;
}
memcpy(data, &tmp, sizeof(*data));
}
void Memory::read(uint32_t address, uint64_t *data){
uint64_t tmp = 0;
for(int i=0; i < 8; i++){
uint64_t read_byte = (uint64_t)readAddress(address);
tmp = tmp | (read_byte << (i*8));
address ++;
}
memcpy(data, &tmp, sizeof(*data));
}
void Memory::dumpMemory(){
std::fstream file;
file.open("memory_dump.out",std::fstream::out);
std::map<uint32_t, uint8_t>::iterator itr;
print(cout, file, "\n********** Memory Dump **********\n\n");
print(cout, file, "(NOTE: Only non-zero values are printed.)\n\n");
print(cout, file, "| Address | Value |\n");
print(cout, file, "|------------|-------------------|\n");
for (itr = memory_map.begin(); itr != memory_map.end(); ++itr) {
if(itr->second == 0){
continue;
}
print(cout, file, "| " + intToHexFmtStr(itr->first, 32) + " | " + intToHexFmtStr(itr->second, 8, 2) + " | " + intToBinFmtStr(itr->second, 8, 8) + " |\n");
}
print(cout, file, "\n");
file.close();
}