-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ram.cpp
103 lines (93 loc) · 2.24 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
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <assert.h>
#include "Ram.hpp"
#include "SystemControlCoprocessor.hpp"
static Ram * instance = nullptr;
Ram * Ram::get_instance()
{
if (instance == nullptr)
{
instance = new Ram();
}
return instance;
}
bool Ram::is_address_for_device(unsigned int address)
{
if (address >= 0x0 && address < MAIN_MEMORY_SIZE)
{
return true;
}
else if (address >= 0x80000000 && address < 0x80000000 + MAIN_MEMORY_SIZE)
{
return true;
}
else if (address >= 0xa0000000 && address < 0xa0000000 + MAIN_MEMORY_SIZE)
{
return true;
}
else if (address >= 0x1f800000 && address < 0x1f800000 + SCRATCHPAD_SIZE)
{
return true;
}
else if (address >= 0x9f800000 && address < 0x9f800000 + SCRATCHPAD_SIZE)
{
return true;
}
return false;
}
unsigned char Ram::get_byte(unsigned int address)
{
if ((address >= 0x0 && address < MAIN_MEMORY_SIZE) ||
(address >= 0x80000000 && address < 0x80000000 + MAIN_MEMORY_SIZE) ||
(address >= 0xa0000000 && address < 0xa0000000 + MAIN_MEMORY_SIZE))
{
system_control::status_register sr = SystemControlCoprocessor::get_instance()->get_control_register(system_control::register_names::SR);
if (sr.Isc)
{
return scratchpad[address & 0x3FF];
}
else
{
return memory[address & 0x0FFFFFFF];
}
}
else
{
return scratchpad[address & 0x3FF];
}
}
void Ram::set_byte(unsigned int address, unsigned char value)
{
if ((address >= 0x0 && address < MAIN_MEMORY_SIZE) ||
(address >= 0x80000000 && address < 0x80000000 + MAIN_MEMORY_SIZE) ||
(address >= 0xa0000000 && address < 0xa0000000 + MAIN_MEMORY_SIZE))
{
system_control::status_register sr = SystemControlCoprocessor::get_instance()->get_control_register(system_control::register_names::SR);
if (sr.Isc)
{
scratchpad[address & 0x3FF] = value;
}
else
{
memory[address & 0x0FFFFFFF] = value;
}
}
else
{
scratchpad[address & 0x3FF] = value;
}
}
void Ram::save_state(std::stringstream& file)
{
file.write(reinterpret_cast<char*>(&memory[0]), sizeof(unsigned char) * MAIN_MEMORY_SIZE);
}
void Ram::load_state(std::stringstream& file)
{
file.read(reinterpret_cast<char*>(&memory[0]), sizeof(unsigned char) * MAIN_MEMORY_SIZE);
}
void Ram::reset()
{
memset(memory, 0, MAIN_MEMORY_SIZE);
}