-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.cpp
More file actions
161 lines (133 loc) · 3.22 KB
/
Memory.cpp
File metadata and controls
161 lines (133 loc) · 3.22 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
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
//
// Memory.cpp
// Copyright © 2003 William Sheldon Simms III
//
#include <cassert>
#include "Memory.h"
Memory::Memory ()
{
for (int idx = 0; idx < ADDRESS_SPACE_SIZE; idx++)
{
r_ptr[idx] = 0;
w_ptr[idx] = 0;
}
running = false;
num_listeners = 0;
}
bool Memory::IsReadable (unsigned short addr)
{
if (r_ptr[addr] == 0) return false;
return true;
}
bool Memory::IsRom (unsigned short addr)
{
unsigned char * ptr = r_ptr[addr];
// if it's not NULL, and not ram, it must be rom
if (ptr == 0) return false;
if (ptr < ram) return true;
if (ptr < (ram + ADDRESS_SPACE_SIZE)) return false;
return true;
}
void Memory::LoadToRom (unsigned short addr, unsigned char byte)
{
// if the address really is mapped as rom
if (IsRom(addr))
{
// then go in through the out door
*(r_ptr[addr]) = byte;
}
}
unsigned char Memory::Read (unsigned short addr)
{
return *(r_ptr[addr]);
}
void Memory::Write (unsigned short addr, unsigned char byte)
{
if (running)
{
*(w_ptr[addr]) = byte;
}
else
{
unsigned char old = *(r_ptr[addr]);
*(w_ptr[addr]) = byte;
if (old != byte)
{
for (int idx = 0; idx < num_listeners; ++idx)
listeners[idx]->TellNewValue(addr, byte);
}
}
}
ap_reader_t Memory::GetReader (unsigned int idx)
{
assert(idx < 1);
ap_reader_t reader;
reader.ap = this;
reader.read = (ap_rfunc_t)&Memory::Read;
return reader;
}
ap_writer_t Memory::GetWriter (unsigned int idx)
{
assert(idx < 1);
ap_writer_t writer;
writer.ap = this;
writer.write = (ap_wfunc_t)&Memory::Write;
return writer;
}
Memory::~Memory ()
{
num_listeners = 0;
}
bool Memory::AddListener (MemoryListener * new_listener)
{
if (num_listeners == MAX_NUM_LISTENERS)
return false;
listeners[num_listeners++] = new_listener;
return true;
}
void Memory::init_ram_memory_read (unsigned short cpu_address_begin,
unsigned short cpu_address_end,
unsigned short mem_address)
{
init_mem_r(cpu_address_begin, cpu_address_end, mem_address, ram);
}
void Memory::init_ram_memory_write (unsigned short cpu_address_begin,
unsigned short cpu_address_end,
unsigned short mem_address)
{
init_mem_w(cpu_address_begin, cpu_address_end, mem_address, ram);
}
void Memory::init_rom_memory (unsigned short cpu_address_begin,
unsigned short cpu_address_end,
unsigned short mem_address)
{
init_mem_r(cpu_address_begin, cpu_address_end, mem_address, rom);
init_mem_w(cpu_address_begin, cpu_address_end, mem_address, 0);
}
void Memory::init_mem_r (unsigned short cpu_address_begin,
unsigned short cpu_address_end,
unsigned short mem_address,
unsigned char * mem)
{
if (mem == 0) return;
unsigned int count = 0;
for (unsigned int idx = cpu_address_begin; idx <= cpu_address_end; ++idx)
{
r_ptr[mem_address + count] = mem + idx;
++count;
}
}
void Memory::init_mem_w (unsigned short cpu_address_begin,
unsigned short cpu_address_end,
unsigned short mem_address,
unsigned char * mem)
{
if (mem == 0) return;
unsigned int count = 0;
for (unsigned int idx = cpu_address_begin; idx <= cpu_address_end; ++idx)
{
w_ptr[mem_address + count] = mem + idx;
++count;
}
}
// end