-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmemory.js
247 lines (194 loc) · 6.41 KB
/
memory.js
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// memory emulation routines for EstyJs
// written by Darren Coles
"use strict";
EstyJs.Memory = function (opts) {
var self = {};
var defaultRamsize = 1024 * 1024;
//var defaultRamsize = 512 * 1024;
var ram = new ArrayBuffer(defaultRamsize);
var ramDataView = new DataView(ram);
var rom = new ArrayBuffer(0);
var romDataView = new DataView(rom);
var io = opts.io;
var bug = opts.bug;
var processor = null;
self.loaded = 0;
function load_binary_resource(url) {
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response; // Note: not oReq.responseText
if (arrayBuffer) {
rom = arrayBuffer;
romDataView = new DataView(rom);
self.loaded++;
}
};
oReq.send(null);
}
load_binary_resource('./tos.img');
self.readByte = function (addr) {
//0 - 00CFFFFF max 14mb of ram
//$00E00000 - 00F0003F system rom
//00F00040 - 00F9FFFF illegal addres
//00FA0000 - 00FBFFFF cartridge rom
//00FC0000 - FEFFFF - system rom
//00FF0000 - FFFFFF - memory mapped registers
addr = addr & 0xffffff;
//mirror first 8 bytes of rom at 0
if (addr < 8) {
return romDataView.getUint8(addr);
}
if (addr < ram.byteLength) {
return ramDataView.getUint8(addr);
}
if (addr >= 0xe00000 & addr < 0xf00040) {
//system rom
return romDataView.getUint8(addr & 0x3ffff);
//return 0xff;
}
if (addr >= 0xf00040 & addr < 0xfa0000) {
//illegal
//bug.say(sprintf('invalid memory read $%08x', addr));
processor.memoryError(addr);
}
if (addr >= 0xfa0000 & addr <= 0xfc0000) {
//cartridge rom
return 0xff;
}
if (addr >= 0xfc0000 & addr < 0xff0000) {
return romDataView.getUint8(addr & 0x3ffff);
}
if (addr >= 0xff0000) {
var m = io.read(addr);
if (m == undefined) processor.memoryError(addr);
return m;
}
return 0xff;
}
self.readWord = function (addr) {
addr = addr & 0xffffff;
//mirror first 8 bytes of rom at 0
/*if (addr < 8) {
return romDataView.getUint16(addr, false);
}*/
if (addr < ram.byteLength) {
return ramDataView.getUint16(addr, false);
}
if (addr >= 0xe00000 & addr < 0xf00040) {
//system rom
return romDataView.getUint16(addr & 0x3ffff);
//return 0xff;
}
if (addr >= 0xfa0000 & addr < 0xfc0000) {
//cartridge rom
return 0xffff;
}
if (addr >= 0xfc0000 & addr < 0xff0000) {
return romDataView.getUint16(addr & 0x3ffff);
}
//default to reading one byte at a time for all other memory addresses
return ((self.readByte(addr) << 8) + (self.readByte(addr + 1))) & 0xffff;
}
self.readLong = function (addr) {
addr = addr & 0xffffff;
//mirror first 8 bytes of rom at 0
if (addr < 8) {
return romDataView.getInt32(addr, false);
}
if (addr < ram.byteLength) {
return ramDataView.getInt32(addr, false);
}
if (addr > 0xe00000 & addr < 0xf00040) {
//system rom
return romDataView.getInt32(addr & 0x3ffff);
//return 0xff;
}
if (addr >= 0xfa0000 & addr <= 0xfc0000) {
//cartridge rom
return 0xffffffff;
}
if (addr >= 0xfc0000 & addr < 0xff0000) {
return romDataView.getInt32(addr & 0x3ffff);
}
//default to reading one byte at a time for all other memory addresses
return ((self.readByte(addr) << 24) + (self.readByte(addr + 1) << 16) + (self.readByte(addr + 2) << 8) + (self.readByte(addr + 3))) & 0xffffffff;
}
self.writeByte = function (addr, val) {
addr = addr & 0xffffff;
if (addr < 8) {
processor.memoryError(addr);
}
if (addr < ram.byteLength) {
ramDataView.setUint8(addr, val);
}
else if ((addr & 0xFF0000) == 0xFF0000) {
try {
io.write(addr, val);
}
catch (Error) {
if (Error == "memory error") processor.memoryError(addr);
}
}
else {
//bug.say(sprintf('invalid memory write $%08x', addr));
}
}
self.writeWord = function (addr, val) {
addr = addr & 0xffffff;
if (addr < 8) {
processor.memoryError(addr);
}
if (addr < ram.byteLength) {
ramDataView.setUint16(addr, val, false);
}
else {
self.writeByte(addr, val >>> 8);
self.writeByte(addr + 1, val & 0xff);
}
}
self.writeLong = function (addr, val) {
addr = addr & 0xffffff;
if (addr < 8) {
processor.memoryError(addr);
}
if (addr < ram.byteLength) {
ramDataView.setInt32(addr, val, false);
}
else {
self.writeByte(addr, val >>> 24);
self.writeByte(addr + 1, (val >>> 16) & 0xff);
self.writeByte(addr + 2, (val >>> 8) & 0xff);
self.writeByte(addr + 3, val & 0xff);
}
}
self.loadSnapshot = function (buffer, offset, datalen) {
//ignore anything that isnt a ram snapshot
if (buffer[offset + 0] != 0) return;
for (var i = 0; i < datalen - 1; i++) {
ram[i] = buffer[offset + i + 1];
}
}
self.reset = function () {
for (var i = 0; i < ram.byteLength; i++) ram[i] = 0;
//mirror 8 bytes of ROM in RAM
for (var i = 0; i < 8; i++) ram[i] = rom[i];
}
self.setProcessor = function (p) {
processor = p;
}
self.setMemSize = function (memSize) {
defaultRamsize = 1024 * memSize;
ram = new ArrayBuffer(defaultRamsize);
ramDataView = new DataView(ram);
}
self.setSnapshotMemory = function (membuff) {
ram = membuff;
ramDataView = new DataView(ram);
}
self.getRamDv = function () {
return ramDataView;
}
return self;
}