-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.h
More file actions
78 lines (77 loc) · 1.27 KB
/
memory.h
File metadata and controls
78 lines (77 loc) · 1.27 KB
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
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <sstream>
using namespace std;
extern registerman my_register;
class memoryman
{
unsigned *memory;
public:
memoryman()
{
memory = new unsigned[500000];
init_all();
}
~memoryman()
{
delete [] memory;
}
void init_all()
{
for(int i = 0; i < 500000; i++)
memory[i] = 0;
unsigned now_addr = 0;
while(!cin.eof())
{
char str[15];
cin >> str;
stringstream ss(str);
if(str[0] == '@')
{
ss.get();
ss >> hex >> now_addr;
ss.clear();
ss.str("");
}
else
{
unsigned tmphex;
ss >> hex >> tmphex;
ss.clear();
ss.str("");
memory[now_addr] = tmphex;
now_addr++;
}
}
}
unsigned read_memory_by_pc(int len = 4)
{
unsigned pc = my_register.getpc();
unsigned tmpmem = 0;
for(int i = len - 1; i >= 0; i--)
{
tmpmem = tmpmem << 8;
tmpmem += memory[pc+i];
}
return tmpmem;
}
unsigned read_memory_given(int pos, int len = 4)
{
unsigned tmpmem = 0;
for(int i = len - 1; i >= 0; i--)
{
tmpmem = tmpmem << 8;
tmpmem += memory[pos+i];
}
return tmpmem;
}
void store_memory(unsigned pos, unsigned bin, unsigned len)
{
for(int i = 0; i < len; i++)
{
memory[pos+i] = bin & (0b11111111);
bin = bin >> 8;
}
}
};