apple2js/js/apple2io.js

494 lines
15 KiB
JavaScript
Raw Normal View History

2019-03-01 05:21:18 +00:00
/* Copyright 2010-2019 Will Scullin <scullin@scullinsteel.com>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*/
2019-03-01 05:21:18 +00:00
import { debug } from './util';
2019-03-01 05:21:18 +00:00
export default function Apple2IO(cpu, callbacks)
{
2016-11-28 01:28:49 +00:00
var _slot = [];
var _auxRom = null;
2014-07-30 19:01:57 +00:00
var _hz = 1023000;
2015-07-11 05:55:36 +00:00
var _rate = 44000;
var _sample_size = 4096;
2014-07-30 19:01:57 +00:00
var _cycles_per_sample = _hz / _rate;
var _buffer = [];
var _key = 0;
var _keyDown = false;
var _button = [false, false, false];
var _paddle = [0.0, 0.0, 0.0, 0,0];
var _phase = -1;
var _sample = [];
var _sampleTime = 0;
2017-08-20 17:46:10 +00:00
var _didAudio = false;
2015-07-11 05:55:36 +00:00
var _high = 0.5;
var _low = -0.5;
2015-07-11 05:55:36 +00:00
var _audioListener = null;
2016-11-22 05:17:34 +00:00
var _trigger = 0;
2017-09-23 04:42:57 +00:00
var _annunciators = [false, false, false, false];
2014-07-30 19:01:57 +00:00
var _tape = [];
var _tapeOffset = 0;
var _tapeNext = 0;
2019-01-09 08:27:31 +00:00
var _tapeCurrent = false;
2014-07-30 19:01:57 +00:00
var LOC = {
2016-11-22 05:17:34 +00:00
KEYBOARD: 0x00, // keyboard data (latched) (Read),
CLR80VID: 0x0C, // clear 80 column mode
SET80VID: 0x0D, // set 80 column mode
2014-07-30 19:01:57 +00:00
CLRALTCH: 0x0E, // clear mousetext
SETALTCH: 0x0F, // set mousetext
STROBE: 0x10, // clear bit 7 of keyboard data ($C000)
RDTEXT: 0x1A, // using text mode
RDMIXED: 0x1B, // using mixed mode
RDPAGE2: 0x1C, // using text/graphics page2
RDHIRES: 0x1D, // using Hi-res graphics mode
RDALTCH: 0x1E, // using alternate character set
RD80VID: 0x1F, // using 80-column display mode
TAPEOUT: 0x20, // toggle the cassette output.
SPEAKER: 0x30, // toggle speaker diaphragm
CLRTEXT: 0x50, // display graphics
SETTEXT: 0x51, // display text
CLRMIXED: 0x52, // clear mixed mode- enable full graphics
SETMIXED: 0x53, // enable graphics/text mixed mode
PAGE1: 0x54, // select text/graphics page1
PAGE2: 0x55, // select text/graphics page2
CLRHIRES: 0x56, // select Lo-res
SETHIRES: 0x57, // select Hi-res
CLRAN0: 0x58, // Set annunciator-0 output to 0
SETAN0: 0x59, // Set annunciator-0 output to 1
CLRAN1: 0x5A, // Set annunciator-1 output to 0
SETAN1: 0x5B, // Set annunciator-1 output to 1
CLRAN2: 0x5C, // Set annunciator-2 output to 0
SETAN2: 0x5D, // Set annunciator-2 output to 1
CLRAN3: 0x5E, // Set annunciator-3 output to 0
SETAN3: 0x5F, // Set annunciator-3 output to 1
TAPEIN: 0x60, // bit 7: data from cassette
PB0: 0x61, // game Pushbutton 0 / open apple (command) key data
PB1: 0x62, // game Pushbutton 1 / closed apple (option) key data
PB2: 0x63, // game Pushbutton 2 (read)
PADDLE0: 0x64, // bit 7: status of pdl-0 timer (read)
PADDLE1: 0x65, // bit 7: status of pdl-1 timer (read)
PADDLE2: 0x66, // bit 7: status of pdl-2 timer (read)
PADDLE3: 0x67, // bit 7: status of pdl-3 timer (read)
PDLTRIG: 0x70, // trigger paddles
2016-11-28 01:28:49 +00:00
BANK: 0x73, // Back switched RAM card bank
SETIOUDIS:0x7E, // Enable double hires
CLRIOUDIS:0x7F // Disable double hires
};
2016-11-22 05:17:34 +00:00
function _debug() {
2016-12-06 05:16:21 +00:00
// debug.apply(this, arguments);
}
2015-07-11 05:55:36 +00:00
function _tick() {
var now = cpu.cycles();
var phase = _phase > 0 ? _high : _low;
for (; _sampleTime < now; _sampleTime += _cycles_per_sample) {
_sample.push(phase);
if (_sample.length >= _sample_size) {
if (_audioListener) {
2017-08-20 17:46:10 +00:00
_audioListener(_didAudio ? _sample : []);
2015-07-11 05:55:36 +00:00
}
_sample = [];
2017-08-20 17:46:10 +00:00
_didAudio = false;
2015-07-11 05:55:36 +00:00
}
}
}
2017-03-05 22:51:59 +00:00
function _access(off, val) {
var result = 0;
var now = cpu.cycles();
var delta = now - _trigger;
switch (off) {
case LOC.CLR80VID:
if (callbacks._80col && val !== undefined) {
2017-03-05 22:51:59 +00:00
_debug('80 Column Mode off');
callbacks._80col(false);
}
break;
case LOC.SET80VID:
if (callbacks._80col && val !== undefined) {
2017-03-05 22:51:59 +00:00
_debug('80 Column Mode on');
callbacks._80col(true);
}
break;
case LOC.CLRALTCH:
if (callbacks.altchar && val !== undefined) {
2017-03-05 22:51:59 +00:00
_debug('Alt Char off');
callbacks.altchar(false);
}
break;
case LOC.SETALTCH:
if (callbacks.altchar && val !== undefined) {
2017-03-05 22:51:59 +00:00
_debug('Alt Char on');
callbacks.altchar(true);
}
break;
case LOC.CLRTEXT:
2016-11-22 05:17:34 +00:00
_debug('Graphics Mode');
callbacks.text(false);
break;
case LOC.SETTEXT:
2016-11-22 05:17:34 +00:00
_debug('Text Mode');
callbacks.text(true);
break;
case LOC.CLRMIXED:
2016-11-22 05:17:34 +00:00
_debug('Mixed Mode off');
callbacks.mixed(false);
break;
case LOC.SETMIXED:
2016-11-22 05:17:34 +00:00
_debug('Mixed Mode on');
callbacks.mixed(true);
break;
case LOC.CLRHIRES:
2016-11-22 05:17:34 +00:00
_debug('LoRes Mode');
callbacks.hires(false);
break;
case LOC.SETHIRES:
2016-11-22 05:17:34 +00:00
_debug('HiRes Mode');
callbacks.hires(true);
break;
case LOC.PAGE1:
callbacks.page(1);
break;
case LOC.PAGE2:
callbacks.page(2);
break;
case LOC.RDTEXT:
if (callbacks.isText)
result = callbacks.isText() ? 0x80 : 0x0;
break;
case LOC.RDMIXED:
if (callbacks.isText)
result = callbacks.isText() ? 0x80 : 0x0;
break;
case LOC.RDPAGE2:
if (callbacks.isPage2)
result = callbacks.isPage2() ? 0x80 : 0x0;
break;
case LOC.RDHIRES:
if (callbacks.isHires)
result = callbacks.isHires() ? 0x80 : 0x0;
break;
case LOC.RD80VID:
if (callbacks.is80Col)
result = callbacks.is80Col() ? 0x80 : 0x0;
break;
case LOC.RDALTCH:
if (callbacks.isAltChar)
result = callbacks.isAltChar() ? 0x80 : 0x0;
break;
case LOC.SETAN0:
_debug('Annunciator 0 on');
2017-09-23 04:42:57 +00:00
_annunciators[0] = true;
break;
case LOC.SETAN1:
_debug('Annunciator 1 on');
2017-09-23 04:42:57 +00:00
_annunciators[1] = true;
break;
case LOC.SETAN2:
_debug('Annunciator 2 on');
2017-09-23 04:42:57 +00:00
_annunciators[2] = true;
break;
case LOC.SETAN3:
_debug('Annunciator 3 on');
2017-09-23 04:42:57 +00:00
_annunciators[3] = true;
if (callbacks.doublehires) callbacks.doublehires(false);
break;
case LOC.CLRAN0:
_debug('Annunciator 0 off');
2017-09-23 04:42:57 +00:00
_annunciators[0] = false;
break;
case LOC.CLRAN1:
_debug('Annunciator 1 off');
2017-09-23 04:42:57 +00:00
_annunciators[1] = false;
break;
case LOC.CLRAN2:
_debug('Annunciator 2 off');
2017-09-23 04:42:57 +00:00
_annunciators[2] = false;
break;
case LOC.CLRAN3:
_debug('Annunciator 3 off');
2017-09-23 04:42:57 +00:00
_annunciators[3] = false;
if (callbacks.doublehires) callbacks.doublehires(true);
break;
case LOC.SPEAKER:
2015-07-11 05:55:36 +00:00
_phase = -_phase;
2017-08-20 17:46:10 +00:00
_didAudio = true;
2015-07-11 05:55:36 +00:00
_tick();
break;
case LOC.STROBE:
_key &= 0x7f;
if (_buffer.length > 0) {
2017-03-05 22:51:59 +00:00
val = _buffer.shift();
if (val == '\n') {
val = '\r';
}
_key = val.charCodeAt(0) | 0x80;
}
result = (_keyDown ? 0x80 : 0x00) | _key;
break;
case LOC.KEYBOARD:
result = _key;
break;
case LOC.PB0:
result = _button[0] ? 0x80 : 0;
break;
case LOC.PB1:
result = _button[1] ? 0x80 : 0;
break;
case LOC.PB2:
result = _button[2] ? 0x80 : 0;
break;
case LOC.PADDLE0:
result = (delta < (_paddle[0] * 2756) ? 0x80 : 0x00);
break;
case LOC.PADDLE1:
result = (delta < (_paddle[1] * 2756) ? 0x80 : 0x00);
break;
case LOC.PADDLE2:
result = (delta < (_paddle[2] * 2756) ? 0x80 : 0x00);
break;
case LOC.PADDLE3:
result = (delta < (_paddle[3] * 2756) ? 0x80 : 0x00);
break;
case LOC.PDLTRIG:
_trigger = cpu.cycles();
break;
2017-09-23 04:42:57 +00:00
case LOC.RDDHIRES:
if (callbacks.isDoubleHires) {
2017-09-23 04:42:57 +00:00
result = callbacks.isDoubleHires() ? 0x80 : 0x0;
}
break;
2014-07-30 19:01:57 +00:00
case LOC.TAPEIN:
if (_tapeOffset == -1) {
_tapeOffset = 0;
_tapeNext = now;
}
2019-01-09 08:27:31 +00:00
2014-07-30 19:01:57 +00:00
if (_tapeOffset < _tape.length) {
2019-01-09 08:27:31 +00:00
_tapeCurrent = _tape[_tapeOffset][1];
2014-07-30 19:01:57 +00:00
while (now >= _tapeNext) {
if ((_tapeOffset % 1000) === 0) {
2016-11-22 05:17:34 +00:00
debug('Read ' + (_tapeOffset / 1000));
2014-07-30 19:01:57 +00:00
}
2019-01-09 08:27:31 +00:00
_tapeCurrent = _tape[_tapeOffset][1];
_tapeNext += _tape[_tapeOffset++][0];
2014-07-30 19:01:57 +00:00
}
}
2019-01-09 08:27:31 +00:00
result = _tapeCurrent ? 0x80 : 0x00;
}
2017-08-20 17:46:10 +00:00
if (val !== undefined) {
result = undefined;
}
2016-11-22 05:17:34 +00:00
return result;
}
2016-11-22 05:17:34 +00:00
return {
start: function apple2io_start() {
return 0xc0;
},
2016-12-06 05:16:21 +00:00
end: function apple2io_end() {
2016-11-28 01:28:49 +00:00
return 0xcf;
},
ioSwitch: function apple2io_ioSwitch(off, val) {
var result;
if (off < 0x80) {
result = _access(off, val);
} else {
var slot = (off & 0x70) >> 4;
2016-12-06 05:16:21 +00:00
var card = _slot[slot];
if (card && card.ioSwitch) {
result = card.ioSwitch(off, val);
2016-11-28 01:28:49 +00:00
}
}
return result;
},
2016-11-28 01:28:49 +00:00
2016-12-06 05:16:21 +00:00
reset: function apple2io_reset() {
for (var slot = 0; slot < 8; slot++) {
var card = _slot[slot];
if (card && card.reset) {
card.reset();
}
}
callbacks.reset();
2016-12-06 05:16:21 +00:00
},
2016-11-22 05:17:34 +00:00
read: function apple2io_read(page, off) {
var result = 0;
2016-11-28 01:28:49 +00:00
var slot;
2016-12-06 05:16:21 +00:00
var card;
2016-11-28 01:28:49 +00:00
switch (page) {
case 0xc0:
result = this.ioSwitch(off);
break;
case 0xc1:
case 0xc2:
case 0xc3:
case 0xc4:
case 0xc5:
case 0xc6:
case 0xc7:
slot = page & 0x0f;
2016-12-06 05:16:21 +00:00
card = _slot[slot];
if (_auxRom != card) {
// _debug('Setting auxRom to slot', slot);
_auxRom = card;
}
if (card) {
result = card.read(page, off);
2016-11-28 01:28:49 +00:00
}
break;
default:
if (_auxRom) {
result = _auxRom.read(page, off);
}
break;
}
return result;
},
2016-11-28 01:28:49 +00:00
write: function apple2io_write(page, off, val) {
2016-11-28 01:28:49 +00:00
var slot;
2016-12-06 05:16:21 +00:00
var card;
2016-11-28 01:28:49 +00:00
switch (page) {
case 0xc0:
2017-03-05 22:51:59 +00:00
this.ioSwitch(off, val);
2016-11-28 01:28:49 +00:00
break;
case 0xc1:
case 0xc2:
case 0xc3:
case 0xc4:
case 0xc5:
case 0xc6:
case 0xc7:
slot = page & 0x0f;
2016-12-06 05:16:21 +00:00
card = _slot[slot];
if (_auxRom != card) {
// _debug('Setting auxRom to slot', slot);
_auxRom = card;
}
if (card) {
card.write(page, off, val);
2016-11-28 01:28:49 +00:00
}
break;
default:
if (_auxRom) {
_auxRom.write(page, off, val);
}
break;
}
},
2016-11-28 01:28:49 +00:00
getState: function apple2io_getState() {
return {
annunciators: _annunciators[0]
};
},
setState: function apple2io_setState(state) {
_annunciators = state.annunciators;
},
2016-11-28 01:28:49 +00:00
2016-12-06 05:16:21 +00:00
setSlot: function apple2io_setSlot(slot, card) {
2016-11-28 01:28:49 +00:00
_slot[slot] = card;
},
2016-11-28 01:28:49 +00:00
keyDown: function apple2io_keyDown(ascii) {
_keyDown = true;
_key = ascii | 0x80;
},
2016-11-28 01:28:49 +00:00
2016-11-22 05:17:34 +00:00
keyUp: function apple2io_keyUp() {
_keyDown = false;
},
buttonDown: function apple2io_buttonDown(b) {
_button[b] = true;
},
2016-11-28 01:28:49 +00:00
buttonUp: function apple2io_buttonUp(b) {
_button[b] = false;
},
2016-11-28 01:28:49 +00:00
paddle: function apple2io_paddle(p, v) {
_paddle[p] = v;
},
2016-11-22 05:17:34 +00:00
2014-07-30 19:01:57 +00:00
updateHz: function apple2io_updateHz(hz) {
_hz = hz;
2016-11-22 05:17:34 +00:00
2014-07-30 19:01:57 +00:00
_cycles_per_sample = _hz / _rate;
},
2019-01-09 08:27:31 +00:00
getHz: function apple2io_updateHz() {
return _hz;
},
setKeyBuffer: function apple2io_setKeyBuffer(buffer) {
2015-07-11 05:55:36 +00:00
_buffer = buffer.split('');
if (_buffer.length > 0) {
_keyDown = true;
_key = _buffer.shift().charCodeAt(0) | 0x80;
}
2014-07-30 19:01:57 +00:00
},
setTape: function apple2io_setTape(tape) {
debug('Tape length: ' + tape.length);
_tape = tape;
_tapeOffset = -1;
2015-07-11 05:55:36 +00:00
},
2016-11-22 05:17:34 +00:00
sampleRate: function sampleRate(rate) {
2015-07-11 05:55:36 +00:00
_rate = rate;
_cycles_per_sample = _hz / _rate;
},
2016-11-22 05:17:34 +00:00
tick: function tick() {
2015-07-11 05:55:36 +00:00
_tick();
for (var idx = 0; idx < 8; idx++) {
if (_slot[idx] && _slot[idx].tick) {
_slot[idx].tick();
}
}
2015-07-11 05:55:36 +00:00
},
2016-11-22 05:17:34 +00:00
2015-07-11 05:55:36 +00:00
addSampleListener: function addSampleListener(cb) {
_audioListener = cb;
2017-09-23 04:42:57 +00:00
},
annunciator: function annunciator(idx) {
return _annunciators[idx];
},
cycles: function apple2io_cycles() {
return cpu.cycles();
}
};
}