-
Notifications
You must be signed in to change notification settings - Fork 1
/
MemoryAccess.h
182 lines (137 loc) · 3.72 KB
/
MemoryAccess.h
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#pragma once
#include <bitset>
#include <map>
#include <fstream>
#include <sstream>
using namespace std;
/* NOTE : LSB occupies lower address */
#define BYTE 8
#define WORD 32
class MemoryAccess {
private:
set <int> occupiedIndex;
map <int , bitset <BYTE> > MEM;
void divide (bitset <32> source , bitset <8> & byte1 , bitset<8> & byte2 , bitset <8> & byte3 , bitset <8> & byte4) {
int k=0,l=0,m=0,n=0;
for(int i = 0 ; i <= 7 ; i++)
byte1[k++] = source[i];
for(int i = 8 ; i <= 15 ; i++)
byte2[l++] = source[i];
for(int i = 16 ; i <= 23 ; i++)
byte3[m++] = source[i];
for(int i = 24 ; i <= 31 ; i++)
byte4[n++] = source[i];
}
void unite (bitset <32> & output , bitset <8> byte1 , bitset<8> byte2 , bitset <8> byte3 , bitset <8> byte4) {
int k=0,l=0,m=0,n=0;
for (int i =0;i< 32 ; i++) {
if (i<8) {
output[i] = byte1[k];k++;
} else if (i<16) {
output[i] = byte2[l];l++;
} else if (i<24) {
output[i] = byte3[m];m++;
} else {
output[i] = byte4[n];n++;
}
}
}
public:
void writeWord(InterStateBuffers &isb) { // WRITES RM value to RZ index
bitset <8> byte1,byte2,byte3,byte4;
bitset <32> source = isb.RM.readBitset();
int index = isb.RZ.readInt();
divide(source,byte1, byte2, byte3 , byte4); // byte1 lsb
occupiedIndex.insert(index);
// Write to its position
MEM [index + 0] = byte4;
MEM [index + 1] = byte3;
MEM [index + 2] = byte2;
MEM [index + 3] = byte1;
}
void writeWord(int address, int data ) { // Writes Data at Index, Basically Memory[index] = data( in bytes)
bitset <8> byte1,byte2,byte3,byte4;
bitset <32> source = data;
int index = address;
occupiedIndex.insert(index);
divide(source,byte1, byte2, byte3 , byte4); // byte1 lsb
// Write to its position
MEM [index + 0] = byte4;
MEM [index + 1] = byte3;
MEM [index + 2] = byte2;
MEM [index + 3] = byte1;
}
void readWord (InterStateBuffers & isb ) { //INDEX at RZ , WRITE to isb.mem_register
bitset <BYTE> byte1, byte2, byte3, byte4;
bitset <WORD> output;
int index = isb.RZ.readInt();
byte4 = MEM [index + 0];
byte3 = MEM [index + 1];
byte2 = MEM [index + 2];
byte1 = MEM [index + 3];
unite (output, byte1, byte2, byte3, byte4);
isb.mem_register = bitsetRead(output);
}
int readWord(int address){
bitset <BYTE> byte1, byte2, byte3, byte4;
bitset <WORD> output;
int index = address;
byte4 = MEM [index + 0];
byte3 = MEM [index + 1];
byte2 = MEM [index + 2];
byte1 = MEM [index + 3];
unite (output, byte1, byte2, byte3, byte4);
return output.to_ulong();
}
int readByte(int address){
bitset <WORD> output;
bitset <BYTE> byte;
int index = address;
byte = MEM[index];
for (int i = 0;i<8;i++) {
output[i] = byte[i];
}
return output.to_ulong();
}
void writeByte ( InterStateBuffers & isb ) { // WRITES RM value to RZ index
bitset <BYTE> byte;
bitset <WORD> source;
source = isb.RZ.readBitset();
int index = isb.RZ.readInt();
for (int i = 0; i<8; i++) {
byte[i] = source[i];
}
MEM[index] = byte;
}
void writeByte ( int address , int data ) { // WRITES RM value to RZ index
bitset <BYTE> byte;
bitset <WORD> source;
source = data;
int index = address;
for (int i = 0; i<8; i++) {
byte[i] = source[i];
}
MEM[index] = byte;
}
void readByte (InterStateBuffers & isb ) {
bitset <WORD> output;
bitset <BYTE> byte;
int index = isb.RZ.readInt();
byte = MEM[index];
for (int i = 0;i<8;i++) {
output[i] = byte[i];
}
isb.mem_register = bitsetRead(output);
}
void dump () {
for (auto elem : occupiedIndex ) {
readWord (elem);
}
}
void Test () {
cout << "Printing Mem" <<endl;
for (auto elem : MEM) {
cout << elem.first << " " <<elem.second<< endl;
}
}
};