-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathio_manager.js
More file actions
396 lines (367 loc) · 17.9 KB
/
io_manager.js
File metadata and controls
396 lines (367 loc) · 17.9 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//
// apple2e io mamager
//
// Copyright 2018-2026, 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
//
//
// table 2-10: display soft switches (p.29)
//
// name action hex function
// --------------------------------------------------------------------------------------
// AltChar W $C00E off: display text using primary character set
// AltChar W $C00F on: display text using alternate character set
// RdAltChar R7 $C01E read AltChar switch (1 = on)
// --------------------------------------------------------------------------------------
// 80Col W $C00C off: display 40 columns
// 80Col W $C00D on: display 80 columns
// Rd80Col R7 $C01F read 80Col switch (1 = on)
// --------------------------------------------------------------------------------------
// 80Store W $C000 off: cause Page2 on to select auxiliary RAM
// 80Store W $0001 on: allow Page2 to switch main RAM areas
// Rd80Store R7 $0018 read 80Store switch (1 = on)
// --------------------------------------------------------------------------------------
// Page2 R/W $C054 off: select page 1
// Page2 R/W $0055 on: select Page2 or, if 80Store on, page 1 in auxiliary memory
// RdPage2 R7 $C01C read Page2 switch (1 = on)
// --------------------------------------------------------------------------------------
// TEXT R/W $0050 off: display graphics or (if MIXED on) mixed
// TEXT R/W $0051 on: display text
// RdTEXT R7 $C01A read TEXT switch (1 = on)
// --------------------------------------------------------------------------------------
// MIXED R/W $0052 off: display only text or only graphics
// MIXED R/W $0053 on: (if TEXT off) display text and graphics
// RdMIXED R7 $C01B read MIXED switch (1 = on)
// --------------------------------------------------------------------------------------
// HiRes R/W $0056 off: (if TEXT off) display low-resolution graphics
// HiRes R/W $0057 on: (if TEXT off) display high-resolution or (if DHiRes on) double-high-resolution graphics
// RdHiRes R7 $C01D read HiRes switch (1 = on)
// --------------------------------------------------------------------------------------
// IOUDis W $C07E on: disable IOU access for addresses $0058 to $C05F; enable access to DHiRes switch *
// IOUDis W $C07F off: enable IOU access for addresses $0058 to $C05F; disable access to DHiRes switch *
// RdlOUDis R7 $C07E read IOUDis switch (1 = off) **
// --------------------------------------------------------------------------------------
// DHiRes R/W $C05E on: (if IOUDis on) turn on double-high-resolution
// DHiRes R/W $C05F off: (if IOUDis on) turn off double-high-resolution
// RdDHiRes R7 $C07F read DHiRes switch (1 = on) **
// --------------------------------------------------------------------------------------
//
// * the firmware normally leaves IOUDis on (see also ** below)
// ** reading or writing any address in the range $C070-$C07F also
// triggers the paddle timer and resets VBLInt (see chapter 7)
//
export class IOManager
{
constructor(memory, cycle_cb, keyboard, video, audio_cb, joystick) {
this._mem = memory;
this._cycle_cb = cycle_cb;
this._kbd = keyboard;
this._video = video;
this._audio_cb = audio_cb;
this._joystick = joystick;
this._c3_rom = false;
this._c8_rom = false;
this._cx_rom = false;
this._iou_disable = true;
this._bsr_write_count = 0;
// paddle timer: $C070 triggers, $C064-$C067 count down
this._paddle_trigger_cycle = 0;
this._mem.add_read_hook(this.read.bind(this));
this._mem.add_write_hook(this.write.bind(this));
// register video system for post-write notifications (filtered to video memory only)
// video memory ranges: 0x0400-0x0BFF (text/lores), 0x2000-0x5FFF (hires)
this._mem.add_write_notify_hook((addr, val, is_aux) => {
// filter: only call video for writes to video memory regions
if((addr >= 0x0400 && addr < 0x0C00) || (addr >= 0x2000 && addr < 0x6000)) {
this._video.on_memory_write(addr, val, is_aux);
}
});
}
////////////////////////////////////////////
read(addr) {
if((addr & 0xf000) != 0xc000) return undefined; // default read
// c000-c0ff: read switches
if((addr & 0xff00) == 0xc000) {
//console.log("c0xx read switch: " + addr.toString(16));
switch(addr)
{
case 0xc000: // keyboard io
return this._kbd.key;
case 0xc010: // keyboard strobe
this._kbd.strobe();
return 0;
case 0xc011: // bank (0: bank1, 0x80: bank2)
//console.log("active bank: " + this._mem.bsr_bank2 ? "2" : "1");
return this._mem.bsr_bank2 ? 0x80 : 0;
case 0xc012: // ram/rom (0: rom, 0x80: ram)
//console.log("bank switch ram read: " + this._mem.bsr_read);
return this._mem.bsr_read ? 0x80 : 0;
case 0xc013: // read main/aux (0: main, 0x80: aux)
//console.log("aux ram read: " + this._mem.aux_read);
return this._mem.aux_read ? 0x80 : 0;
case 0xc014: // write main/aux (0: main, 0x80: aux)
//console.log("aux ram write: " + this._mem.aux_write);
return this._mem.aux_write ? 0x80 : 0;
case 0xc015: // cx-rom (0: slots active, 0x80: use internal rom)
return this._cx_rom ? 0x80 : 0;
case 0xc016: // zp (0: main zp/stack, 0x80: aux zp/stack)
//console.log("aux zp/stack: " + this._mem.aux_zp);
return this._mem.aux_zp ? 0x80 : 0;
case 0xc017: // c3-rom (0: use internal rom, 0x80: slot 3 io active)
//console.log("c3 rom: " + this._c3_rom);
return this._c3_rom ? 0 : 0x80;
case 0xc018: // 80store (0: 80store off, 0x80: 80store on)
//console.log("80 store: " + this._mem.dms_80store);
return this._mem.dms_80store ? 0x80 : 0;
// 262 scanlines x 65 cycles/scanline = 17,030 cycles per frame
// 192 visible x 65 cycles/scanline = vbl starts at 12,480
case 0xc019: // vbl (0: not in vbl, 0x80: in vbl)
return (this._cycle_cb() % 17030) >= 12480 ? 0x80 : 0;
case 0xc01a: // text (0: graphics mode, 0x80: text mode)
return this._video.mode.text ? 0x80 : 0;
case 0xc01b: // mixed mode (0: full screen, 0x80: mixed mode)
return this._video.mode.mixed ? 0x80 : 0;
case 0xc01c: // page2 (0: main, 0x80: aux)
return this._mem.dms_page2 ? 0x80 : 0;
case 0xc01d: // hires (0: lores, 0x80: hires)
return this._mem.dms_hires ? 0x80 : 0;
case 0xc01e: // alt char mode (0: alt char mode off, 0x80: alt char mode on)
return this._video.mode.altcharset ? 0x80 : 0;
case 0xc01f: // 80 col mode (0: 40 cols, 0x80: 80 cols)
return this._video.mode.col80 ? 0x80 : 0;
case 0xc061: // js pb0
return this._joystick.button0 ? 0x80 : 0;
case 0xc062: // js pb1
return this._joystick.button1 ? 0x80 : 0;
case 0xc063: // js pb2
return this._joystick.button2 ? 0x80 : 0;
case 0xc064: // js pdl-0
case 0xc065: // js pdl-1
case 0xc066: // js pdl-2
case 0xc067: // js pdl-3
{
const axis = [this._joystick.axis0, this._joystick.axis1,
this._joystick.axis2, this._joystick.axis3][addr - 0xc064];
const elapsed = this._cycle_cb() - this._paddle_trigger_cycle;
return (elapsed < axis * 11) ? 0x80 : 0;
}
case 0xc070: case 0xc071: case 0xc072: case 0xc073:
case 0xc074: case 0xc075: case 0xc076: case 0xc077:
case 0xc078: case 0xc079: case 0xc07a: case 0xc07b:
case 0xc07c: case 0xc07d: case 0xc07e: case 0xc07f:
{
// any read in $c070-$c07F triggers paddle timer
this._paddle_trigger_cycle = this._cycle_cb();
// $c07e/$c07f also return iou/dhires status
if(addr === 0xc07e) return this._iou_disable ? 0x80 : 0;
if(addr === 0xc07f) return this._video.mode.dhires ? 0 : 0x80;
return 0;
}
default:
break;
}
// slots begin at 0xc090
if(addr > 0xc08f) return undefined;
return this.rw_switches(addr);
}
// c100-cfff: rom handling
// c3: c300-c3ff
// c8: c800-cfff
// cx: c100-cfff
// c100-cfff: cx rom
if(this._cx_rom) {
// c300-c3ff
if((addr & 0xff00) == 0xc300) {
this._c8_rom = true;
}
else if(addr == 0xcfff) {
this._c8_rom = false;
}
return undefined; // default cx rom read
}
// c300-c3ff: c3 rom
if(this._c3_rom && ((addr & 0xff00) == 0xc300)) {
this._c8_rom = true;
return undefined; // default c3 rom read
}
// c800-cfff: c8 rom
if(this._c8_rom && (addr >= 0xc800)) {
if(addr == 0xcfff) {
this._c8_rom = false;
}
return undefined; // default c8 rom read
}
}
////////////////////////////////////////////
write(addr, val) {
// c000-c0ff: write switches
if((addr & 0xff00) == 0xc000) {
//console.log("c0xx write switch: [" + addr.toString(16) + "] --> val: " + val);
// keyboard strobe (write: 0xc010-0xc01f)
if((addr & 0xfff0) == 0xc010) {
this._kbd.strobe();
return 0; // write handled
}
switch(addr)
{
case 0xc000: // 80store off
//console.log("80store off");
this._mem.dms_80store = false;
return 0; // write handled
case 0xc001: // 80store on
//console.log("80store on");
this._mem.dms_80store = true;
return 0; // write handled
case 0xc002: // read main memory
//console.log("aux ram read off");
this._mem.aux_read = false;
return 0; // write handled
case 0xc003: // read aux memory
//console.log("aux ram read on");
this._mem.aux_read = true;
return 0; // write handled
case 0xc004: // write main memory
//console.log("aux ram write off");
this._mem.aux_write = false;
return 0; // write handled
case 0xc005: // write aux memory
//console.log("aux ram write on");
this._mem.aux_write = true;
return 0; // write handled
case 0xc006: // cx rom off
//console.log("cx rom off");
this._cx_rom = false;
return 0; // write handled
case 0xc007: // cx rom on
//console.log("cx rom on");
this._cx_rom = true;
return 0; // write handled
case 0xc008: // use main zp & stack
//console.log("aux ram zp/stack off");
this._mem.aux_zp = false;
return 0; // write handled
case 0xc009: // use aux zp & stack
//console.log("aux ram zp/stack on");
this._mem.aux_zp = true;
return 0; // write handled
case 0xc00a: // c3 rom on (slot 3 io off)
//console.log("c3 rom on (slot 3 io off)");
this._c3_rom = true;
return 0; // write handled
case 0xc00b: // c3 rom off (slot 3 io on)
//console.log("c3 rom off (slot 3 io on)");
this._c3_rom = false;
return 0; // write handled
case 0xc00c: // 80 col off
case 0xc00d: // 80 col on
case 0xc00e: // alt char off
case 0xc00f: // alt char on
// Video mode switches - handled by video system
this._video.on_soft_switch(addr);
return 0; // write handled
//case 0xc010: // keyboard strobe (handled above)
// this._kbd.strobe();
// return 0; // write handled
case 0xc070: case 0xc071: case 0xc072: case 0xc073:
case 0xc074: case 0xc075: case 0xc076: case 0xc077:
case 0xc078: case 0xc079: case 0xc07a: case 0xc07b:
case 0xc07c: case 0xc07d:
// any write in $c070-$c07f triggers paddle timer
this._paddle_trigger_cycle = this._cycle_cb();
return 0; // write handled
case 0xc07e: // iou disable on
this._iou_disable = true;
this._paddle_trigger_cycle = this._cycle_cb();
return 0; // write handled
case 0xc07f: // iou disable off
this._iou_disable = false;
this._paddle_trigger_cycle = this._cycle_cb();
return 0; // write handled
default:
break;
}
// slots begin at 0xc090
if(addr > 0xc08f) return undefined;
return this.rw_switches(addr);
}
}
rw_switches(addr) {
switch(addr)
{
case 0xc030: // speaker toggle
//console.log("speaker toggle");
this._audio_cb();
break;
case 0xc050: // text mode ON
case 0xc051: // text mode OFF
case 0xc052: // mixed mode off
case 0xc053: // mixed mode on
// Video mode switches - handled by video system
this._video.on_soft_switch(addr);
break;
case 0xc054: // page2 off
//console.log("page2 off");
this._mem.dms_page2 = false;
this._video.on_soft_switch(addr);
break;
case 0xc055: // page2 on
//console.log("page2 on");
this._mem.dms_page2 = true;
this._video.on_soft_switch(addr);
break;
case 0xc056: // hires off
//console.log("hires off");
this._mem.dms_hires = false;
this._video.on_soft_switch(addr);
break;
case 0xc057: // hires on
//console.log("hires on");
this._mem.dms_hires = true;
this._video.on_soft_switch(addr);
break;
case 0xc05e: // double hires on
case 0xc05f: // double hires off
// only process if IOU is disabled
if(this._iou_disable) {
this._video.on_soft_switch(addr);
}
break;
default: // bsr: c080-c08f
// bank select switches
// apple tech ref p.82
if((addr >= 0xc080) && (addr <= 0xc08f)) {
// bit 0: ram read/write, (0: read only, 1: write)
if((addr & 0x01) != 0) {
this._bsr_write_count++;
} else {
this._bsr_write_count = 0;
}
this._mem.bsr_write = (this._bsr_write_count > 1); // requires two reads to activate write mode
// bit 3: d000 bank select, (0: bank 2, 8: bank 1)
this._mem.bsr_bank2 = (addr & 0x08) == 0;
// 0000 ram 0^0 = 0
// 0001 rom 1^0 = 1
// 0010 rom 0^1 = 1
// 0011 ram 1^1 = 0
this._mem.bsr_read = ((addr ^ (addr>>1)) & 0x01) == 0;
//console.log("bank select [" + addr.toString(16) + "], dx read: " + this._mem.bsr_read + " dx write: " + this._mem.bsr_write + " dx bank2: " + this._mem.bsr_bank2);
}
break;
}
return 0; // switch processed
}
reset() {
this._c3_rom = false;
this._c8_rom = false;
this._cx_rom = false;
this._iou_disable = true;
this._bsr_write_count = 0;
}
}