-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathram.cpp
More file actions
106 lines (74 loc) · 1.51 KB
/
ram.cpp
File metadata and controls
106 lines (74 loc) · 1.51 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
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
#include "ram.hpp"
using namespace std;
Ram::Ram(bus start)
{
for(int i = 0; i < 65536; i++)
{
memory[i] = start;
}
}
void Ram::write(bit cs, reg address, bus data)
{
inputSignals.R = 0;
bus chip_select;
for(int i = 0; i < 16; i++)
{
set_bit(chip_select, i, cs);
}
decode(address);
int index = 0;
for(int i = 0; i < MAX_INT; i++)
{
index = decoded[i] * i;
}
memory[index] = or_bus(and_bus(data, chip_select), and_bus(memory[index], not_bus(chip_select)));
inputSignals.R = 1;
}
bus Ram::read(bit cs, reg address)
{
inputSignals.R = 0;
bus chip_select;
for(int i = 0; i < 16; i++)
{
set_bit(chip_select, i, cs);
}
decode(address);
int index = 0;
for(int i = 0; i < MAX_INT; i++)
{
index = decoded[i] * i;
}
inputSignals.R = 1;
return memory[index];
}
void Ram::write_to_file()
{
ofstream ramfile;
ramfile.open("ram.txt");
for(int i = 0; i < MAX_INT; i++)
{
ramfile << bus_str(memory[i]) << endl;
}
ramfile.close();
}
void Ram::read_from_file()
{
string line;
ifstream ramfile ("ram.txt");
int index = 0;
if(ramfile.is_open())
{
while(getline(ramfile, line))
{
memory[index] = (std::bitset<16> (std::string(line))).to_ullong();
index++;
}
}
}
void Ram::print_to_screen()
{
for(int i = 0; i < MAX_INT; i++)
{
cout << bus_str(memory[i]) << '\n';
}
}