-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathram.cpp
117 lines (104 loc) · 2.11 KB
/
ram.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
# include "ram.h"
RAM::RAM() {};
void RAM::write_byte(int address, unsigned char val) {
ram[address] = val;
}
void RAM::write_int(int address, int val) {
unsigned char bytes[4];
memcpy(&bytes, &val, 4);
for (int i = 0; i < 4; i++) {
ram[address + i] = bytes[3 - i];
}
}
void RAM::write_long(int address, long long val) {
unsigned char bytes[8];
memcpy(&bytes, &val, 8);
for (int i = 0; i < 8; i++) {
ram[address + i] = bytes[7 - i];
}
}
void RAM::write_single(int address, float val) {
unsigned char bytes[4];
memcpy(&bytes, &val, 4);
for (int i = 0; i < 4; i++) {
ram[address + i] = bytes[3 - i];
}
}
void RAM::write_double(int address, double val) {
unsigned char bytes[8];
memcpy(&bytes, &val, 8);
for (int i = 0; i < 8; i++) {
ram[address + i] = bytes[7 - i];
}
}
unsigned char RAM::read_byte(int address) {
try {
return ram.at(address);
}
catch (...) {
cout << hex << address;
throw address;
}
}
int RAM::read_int(int address) {
try {
unsigned char bytes[4];
for (int i = 0; i < 4; i++) {
bytes[3 - i] = ram.at(address + i);
}
int ret;
memcpy(&ret, &bytes, 4);
return ret;
}
catch (...) {
cout << hex << address;
throw address;
}
}
float RAM::read_single(int address) {
try {
unsigned char bytes[4];
for (int i = 0; i < 4; i++) {
bytes[3 - i] = ram.at(address + i);
}
float ret;
memcpy(&ret, &bytes, 4);
return ret;
}
catch (...) {
cout << hex << address;
throw address;
}
}
double RAM::read_double(int address) {
try {
unsigned char bytes[8];
for (int i = 0; i < 8; i++) {
bytes[7 - i] = ram.at(address + i);
}
double ret;
memcpy(&ret, &bytes, 8);
return ret;
}
catch (...) {
cout << hex << address;
throw address;
}
}
void RAM::print_ram() {
//no longer ordered, needs sorting
int last_address = 0;
for (auto const& a : ram) {
if (last_address + 1 == a.first) {
cout << setw(2) << setfill('0') << hex << (int)a.second;
last_address = a.first;
}
else {
cout << '\n';
last_address = a.first;
cout << hex << a.first << ": ";
cout << setw(2) << setfill('0') << hex << (int)a.second;
}
}
cout << "\n\n";
}