-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.cc
More file actions
175 lines (132 loc) · 3.2 KB
/
Copy pathMemory.cc
File metadata and controls
175 lines (132 loc) · 3.2 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dx.h"
Memory::Memory(long mSize) {
memorySize = mSize;
memory.clear();
memory.reserve(memorySize);
//printf("Memory size=%d\n", memorySize);
for (int i=0; i<memorySize; i++) {
memory[i] = new MemoryCell();
}
bigEndian = true;
}
void Memory::dump() {
printf("Memory dump:\n");
for (int bank=0; bank < memorySize; bank += 16) {
bool empty = true;
int i;
for (i=0; i<16; i++) {
if (memory[bank+i]->getType() != UNSET) {
empty = false;
}
}
if (!empty) {
printf("%08x: ", bank);
for (i=0; i<16; i++) {
int val = memory[bank+i]->get();
int type = memory[bank+i]->getType();
if (type == UNSET) {
printf(" --");
}
else if (type == BYTE) {
printf(" BB");
}
else if (type == WORD) {
printf(" WW");
}
else {
printf(" %02x", val);
}
}
putchar('\n');
}
}
}
void Memory::setEndian(bool big) {
bigEndian = big;
}
void Memory::setBigEndian() {
setEndian(true);
}
void Memory::setLittleEndian() {
setEndian(false);
}
void Memory::setAddressMask(long mask) {
addressMask = mask;
}
long Memory::maskAddress(long addr) {
return addr & addressMask;
}
bool Memory::isValidAddress(long addr) {
return ((addr >= 0) && (addr < memorySize));
}
void Memory::assertAddressValid(long addr) {
if (!isValidAddress(addr)) {
throw RangeException();
}
}
long Memory::readFile(const char *fileName, long addr) {
FILE *fd = fopen(fileName, "rb");
int nBytes = 0;
long start = addr;
//printf("Read file '%s' into memory at $%04x\n", fileName, addr);
if (!fd) {
return NO;
}
while(!feof(fd)) {
int c = fgetc(fd);
if (c != EOF) {
if (isValidAddress(addr)) {
memory[maskAddress(addr)]->set(c);
memory[maskAddress(addr)]->setType(UNKNOWN);
addr++;
}
nBytes++;
}
}
fclose(fd);
//printf("%d bytes read into $%04x-$%04x\n", nBytes, start, addr-1);
//printf(" masked to $%04x-$%04x\n", maskAddress(start), maskAddress(addr-1));
return nBytes;
}
int Memory::getByte(long addr) {
assertAddressValid(addr);
return memory[maskAddress(addr)]->get();
}
void Memory::setByte(long addr, int b) {
assertAddressValid(addr);
memory[maskAddress(addr)]->set(b);
}
int Memory::getWord(long addr) {
int b1, b2;
b1 = memory[maskAddress(addr)]->get();
b2 = memory[maskAddress(addr+1)]->get();
return bigEndian ? ((b1<<8) | b2) : ((b2<<8) | b1);
}
int Memory::getDword(long addr) {
dword_t low, lowish, highish, high;
low = memory[maskAddress(addr+3)]->get();
lowish = memory[maskAddress(addr+2)]->get();
highish = memory[maskAddress(addr+1)]->get();
high = memory[maskAddress(addr)]->get();
return low | (lowish << 8) | (highish << 16) | (high << 24);
}
void Memory::setDword(long addr, int val) {
throw exception();
}
int Memory::setType(long addr, int type, int count) {
int size = (type == WORD)?2:1;
count = count * size;
for (int i=0; i<count; i++) {
if (isValidAddress(maskAddress(addr+i))) {
memory[maskAddress(addr+i)]->setType(type);
}
}
return count;
}
int Memory::getType(long addr) {
return memory[maskAddress(addr)]->getType();
}