-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmemory.js
286 lines (245 loc) · 8.49 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
// apple2e memory mamager
//
// Copyright 2018, John Clark
//
// Released under the GNU General Public License
// https://www.gnu.org/licenses/gpl.html
//
// ref: ftp://ftp.apple.asimov.net/pub/apple_II/documentation/hardware/machines/Apple%20IIe%20Technical%20Reference%20Manual%20(alt%202)_part%201.pdf
// ftp://ftp.apple.asimov.net/pub/apple_II/documentation/hardware/machines/Apple%20IIe%20Technical%20Reference%20Manual%20(alt%202)_part%202.pdf
// ftp://ftp.apple.asimov.net/pub/apple_II/documentation/hardware/machines/Apple%20IIe%20Technical%20Reference%20Manual%20(alt%202)_part%203.pdf
// ftp://ftp.apple.asimov.net/pub/apple_II/documentation/hardware/machines/Apple%20IIe%20Technical%20Reference%20Manual%20(alt%202)_part%204.pdf
//
export class Memory
{
constructor(rom_cd, rom_ef) {
this._rom_cd = rom_cd;
this._rom_ef = rom_ef;
this._main = new Uint8Array(0x10000); // 64k
this._main_bb = new Uint8Array(0x1000); // 4k (dxxx bank2)
this._aux = new Uint8Array(0x10000); // 64k
this._aux_bb = new Uint8Array(0x1000); // 4k (dxxx bank2)
this._bsr_read = false;
this._bsr_write = false;
this._bsr_bank2 = false;
this._aux_zp = false;
this._aux_read = false;
this._aux_write = false;
this._dms_80store = false;
this._dms_page2 = false;
this._dms_hires = false;
this._read_hooks = [];
this._write_hooks = [];
}
get bsr_read() { return this._bsr_read; }
set bsr_read(val) { this._bsr_read = (val!=0); }
get bsr_write() { return this._bsr_write; }
set bsr_write(val) { this._bsr_write = (val!=0); }
get bsr_bank2() { return this._bsr_bank2; }
set bsr_bank2(val) { this._bsr_bank2 = (val!=0); }
get aux_zp() { return this._aux_zp; }
set aux_zp(val) { this._aux_zp = (val!=0); }
get aux_read() { return this._aux_read; }
set aux_read(val) { this._aux_read = (val!=0); }
get aux_write() { return this._aux_write; }
set aux_write(val) { this._aux_write = (val!=0); }
// tech ref p.28
get dms_80store() { return this._dms_80store; }
set dms_80store(val) { this._dms_80store = (val!=0); }
get dms_page2() { return this._dms_page2; }
set dms_page2(val) { this._dms_page2 = (val!=0); }
get dms_hires() { return this._dms_hires; }
set dms_hires(val) { this._dms_hires = (val!=0); }
read(addr) {
addr &= 0xffff;
for(let read_hook of this._read_hooks) {
const res = read_hook(addr);
if(res != undefined) return res & 0xff;
}
// 0000-01ff
if(addr < 0x0200) {
return this._aux_zp ? this._aux[addr] : this._main[addr];
}
if(this._dms_80store) {
// 0400-07ff
if((addr & 0xfc00) == 0x0400) {
return this._dms_page2 ? this._aux[addr] : this._main[addr];
}
// 2000-3fff
if(this._dms_hires && ((addr & 0xe000) == 0x2000)) {
return this._dms_page2 ? this._aux[addr] : this._main[addr];
}
}
// 0200-bfff
if(addr < 0xc000) {
return this._aux_read ? this._aux[addr] : this._main[addr];
}
// c000-cfff
if((addr & 0xf000) == 0xc000) {
return this._rom_cd[addr & 0x0fff];
}
// d000-ffff (filter above): bank switched ram
// ram read
if(this._bsr_read) {
// d000-dfff: bank 2 read
if(this._bsr_bank2 && ((addr & 0xf000) == 0xd000)) {
return this._aux_zp ? this._aux_bb[addr & 0x0fff] : this._main_bb[addr & 0x0fff];
}
// d000-dfff bank 1 & e000-ffff
return this._aux_zp ? this._aux[addr] : this._main[addr];
}
// rom read
// d000-dfff: read dxxx rom
if((addr & 0xf000) == 0xd000) return this._rom_cd[(addr & 0x0fff) | 0x1000];
// e000-ffff: read ef rom
return this._rom_ef[addr & 0x1fff];
}
// little-endian read
read_word(addr) {
return this.read(addr) | this.read(addr+1)<<8;
}
write(addr, val) {
addr &= 0xffff;
val &= 0xff;
for(let write_hook of this._write_hooks) {
const res = write_hook(addr, val);
if(res != undefined) return;
}
// 0000-01ff
if(addr < 0x0200) {
if(this._aux_zp) {
this._aux[addr] = val;
} else {
this._main[addr] = val;
}
return;
}
if(this._dms_80store) {
// 0400-07ff
if((addr & 0xfc00) == 0x0400) {
if(this._dms_page2) {
this._aux[addr] = val;
} else {
this._main[addr] = val;
}
return;
}
// 2000-3fff
if(this._dms_hires && ((addr & 0xe000) == 0x2000)) {
if(this._dms_page2) {
this._aux[addr] = val;
} else {
this._main[addr] = val;
}
return;
}
}
// 0200-bfff
if(addr < 0xc000) {
if(this._aux_write) {
this._aux[addr] = val;
} else {
this._main[addr] = val;
}
return;
}
// c000-cfff || d000-ffff (discard writes to rom areas)
if(((addr & 0xf000) == 0xc000) || !this._bsr_write) return;
// d000-ffff (filter above): bank switched ram
// d000-dfff: bank 2 write
if(this._bsr_bank2 && ((addr & 0xf000) == 0xd000)) {
// write dxxx bank2 (aux a & b)
if(this._aux_zp) {
this._aux_bb[addr & 0x0fff] = val;
} else {
this._main_bb[addr & 0x0fff] = val;
}
return;
}
// d000-dfff bank 1 & e000-ffff
if(this._aux_zp) {
this._aux[addr] = val;
} else {
this._main[addr] = val;
}
}
// memory read callback hook
// function(addr)
// return undefined to contunue processing
add_read_hook(callback) {
if(!this._read_hooks.includes(callback)) {
this._read_hooks.push(callback);
}
}
remove_read_hook(callback) {
if(this._read_hooks.includes(callback)) {
this._read_hooks = this._read_hooks.filter(e => e !== callback);
}
}
// memory write callback hook
// function(addr, val))
// return undefined to contunue processing
add_write_hook(callback) {
if(!this._write_hooks.includes(callback)) {
this._write_hooks.push(callback);
}
}
remove_write_hook(callback) {
if(this._write_hooks.includes(callback)) {
this._write_hooks = this._write_hooks.filter(e => e !== callback);
}
}
fill(val, addr_begin, addr_end) {
val &= 0xff;
addr_begin &= 0xffff;
addr_end &= 0xffff;
for(let addr=addr_begin; addr<=addr_end; addr++) {
this.write(addr, val);
}
}
reset() {
this._main.fill(0);
this._main_bb.fill(0);
this._aux.fill(0);
this._aux_bb.fill(0);
this._bsr_read = false;
this._bsr_write = false;
this._bsr_bank2 = false;
this._aux_zp = false;
this._aux_read = false;
this._aux_write = false;
this._dms_80store = false;
this._dms_page2 = false;
this._dms_hires = false;
}
// export data range in hexdump format
hexdump(addr_begin, addr_end, ascii) {
addr_begin &= 0xffff;
addr_end &= 0xffff;
let out = "";
for(let i=addr_begin; i<=addr_end; i+=16) {
let row = i.toString(16).padStart(4, '0') + " ";
let asc = "";
for(let j=i; j<i+16; j++) {
if(j <= addr_end) {
const val = this.read(j);
row += val.toString(16).padStart(2, '0') + " ";
asc += val<0x20 || val>0x7e && val<0xc0 || val==0xf7 ? "." : String.fromCharCode(val);
}
else {
row += " ";
asc += " ";
}
if(j == i+7) row += " ";
}
if(ascii) {
out += " " + row + " |" + asc + "|\n";
}
else {
out += " " + row + "\n";
}
}
return out;
}
}