diff --git a/js/apple1io.js b/js/apple1io.js deleted file mode 100644 index ab243f4..0000000 --- a/js/apple1io.js +++ /dev/null @@ -1,97 +0,0 @@ -/* Copyright 2010-2019 Will Scullin - * - * 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. - */ - -export default function Apple1IO(text) { - var _key = 0; - var _keyReady = false; - - var _displayReady = false; - var _nextDSP = 0; - var _buffer = []; - - var LOC = { - KBD: 0x10, - KBDRDY: 0x011, - DSP: 0x12, - DSPCTL: 0x13 - }; - - return { - start: function() { return 0xd0; }, - end: function() { return 0xd0; }, - read: function(page, off) { - var result = 0; - off &= 0x13; - switch (off) { - case LOC.KBD: - // Keyboard - if (_buffer.length) { - result = _buffer.shift().toUpperCase().charCodeAt(0) & 0x7f; - _keyReady = (_buffer.length > 0); - } else { - result = _key; - _keyReady = false; - } - result |= 0x80; - break; - case LOC.KBDRDY: - result = _keyReady ? 0x80 : 0x00; - break; - case LOC.DSP: - // Display - // result = (Math.random() > 0.5) ? 0x80 : 0x00; - result = (Date.now() > _nextDSP) ? 0x00 : 0x80; - break; - case LOC.DSPCTL: - break; - } - return result; - }, - write: function(page, off, val) { - off &= 0x13; - switch (off) { - case LOC.KBD: - break; - case LOC.KBDRDY: - break; - case LOC.DSP: - // Display - if (_displayReady) { - text.write(val); - } - _nextDSP = Date.now() + ((_buffer.length > 0) ? 0 : 17); - break; - case LOC.DSPCTL: - // Don't pretend we care what the value was... - _displayReady = true; - break; - } - }, - reset: function apple1io_reset() { - text.clear(); - _buffer = []; - _keyReady = false; - _displayReady = false; - }, - keyUp: function apple1io_keyUp() { - }, - keyDown: function apple1io_keyDown(key) { - _key = key; - _keyReady = true; - }, - paste: function apple1io_paste(buffer) { - buffer = buffer.replace(/\/\/.*\n/g, ''); - buffer = buffer.replace(/\n/g, '\r'); - _buffer = buffer.split(''); - _keyReady = true; - } - }; -} diff --git a/js/apple1io.ts b/js/apple1io.ts new file mode 100644 index 0000000..8e0a359 --- /dev/null +++ b/js/apple1io.ts @@ -0,0 +1,104 @@ +/* Copyright 2010-2023 Will Scullin + * + * 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. + */ + +import { TextPage } from "./canvas1"; +import type { byte } from "./types"; + +const LOC = { + KBD: 0x10, + KBDRDY: 0x011, + DSP: 0x12, + DSPCTL: 0x13, +} as const; + +export default class Apple1IO { + _key = 0; + _keyReady = false; + + _displayReady = false; + _nextDSP = 0; + _buffer: string[] = []; + + constructor(private text: TextPage) {} + + start() { + return 0xd0; + } + end() { + return 0xd0; + } + read(_page: byte, off: byte): byte { + var result = 0; + off &= 0x13; + switch (off) { + case LOC.KBD: + // Keyboard + const key = this._buffer.shift(); + if (key != null) { + result = key.toUpperCase().charCodeAt(0) & 0x7f; + this._keyReady = this._buffer.length > 0; + } else { + result = this._key; + this._keyReady = false; + } + result |= 0x80; + break; + case LOC.KBDRDY: + result = this._keyReady ? 0x80 : 0x00; + break; + case LOC.DSP: + // Display + // result = (Math.random() > 0.5) ? 0x80 : 0x00; + result = Date.now() > this._nextDSP ? 0x00 : 0x80; + break; + case LOC.DSPCTL: + break; + } + return result; + } + write(_page: byte, off: byte, val: byte): void { + off &= 0x13; + switch (off) { + case LOC.KBD: + break; + case LOC.KBDRDY: + break; + case LOC.DSP: + // Display + if (this._displayReady) { + this.text.write(val); + } + this._nextDSP = Date.now() + (this._buffer.length > 0 ? 0 : 17); + break; + case LOC.DSPCTL: + // Don't pretend we care what the value was... + this._displayReady = true; + break; + } + } + reset() { + this.text.clear(); + this._buffer = []; + this._keyReady = false; + this._displayReady = false; + } + keyUp() {} + keyDown(key: byte) { + this._key = key; + this._keyReady = true; + } + paste(buffer: string) { + buffer = buffer.replace(/\/\/.*\n/g, ""); + buffer = buffer.replace(/\n/g, "\r"); + this._buffer = buffer.split(""); + this._keyReady = true; + } +}