/** * @fileoverview Implements the PC8080 SerialPort component. * @author Jeff Parsons * @copyright © Jeff Parsons 2012-2017 * * This file is part of PCjs, a computer emulation software project at . * * PCjs is free software: you can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * PCjs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with PCjs. If not, * see . * * You are required to include the above copyright notice in every modified copy of this work * and to display that copyright notice when the software starts running; see COPYRIGHT in * . * * Some PCjs files also attempt to load external resource files, such as character-image files, * ROM files, and disk image files. Those external resource files are not considered part of PCjs * for purposes of the GNU General Public License, and the author does not claim any copyright * as to their contents. */ "use strict"; if (NODE) { var Str = require("../../shared/lib/strlib"); var Web = require("../../shared/lib/weblib"); var Component = require("../../shared/lib/component"); var State = require("../../shared/lib/state"); var PC8080 = require("./defines"); var Messages8080 = require("./messages"); } /** * TODO: The Closure Compiler treats ES6 classes as 'struct' rather than 'dict' by default, * which would force us to declare all class properties in the constructor, as well as prevent * us from defining any named properties. So, for now, we mark all our classes as 'unrestricted'. * * @unrestricted */ class SerialPort8080 extends Component { /** * SerialPort8080(parmsSerial) * * The SerialPort8080 component has the following component-specific (parmsSerial) properties: * * adapter: 0 if not defined * * binding: name of a control (based on its "binding" attribute) to bind to this port's I/O * * tabSize: set to a non-zero number to convert tabs to spaces (applies only to output to * the above binding); default is 0 (no conversion) * * In the future, we may support 'port' and 'irq' properties that allow the machine to define a * non-standard serial port configuration, instead of only our pre-defined 'adapter' configurations. * * NOTE: Since the XSL file defines 'adapter' as a number, not a string, there's no need to use * parseInt(), and as an added benefit, we don't need to worry about whether a hex or decimal format * was used. * * @this {SerialPort8080} * @param {Object} parmsSerial */ constructor(parmsSerial) { super("SerialPort", parmsSerial, Messages8080.SERIAL); this.iAdapter = +parmsSerial['adapter']; switch (this.iAdapter) { case 0: this.portBase = 0; this.nIRQ = 2; break; default: Component.warning("Unrecognized serial adapter #" + this.iAdapter); return; } /** * consoleOutput becomes a string that records serial port output if the 'binding' property is set to the * reserved name "console". Nothing is written to the console, however, until a linefeed (0x0A) is output * or the string length reaches a threshold (currently, 1024 characters). * * @type {string|null} */ this.consoleOutput = null; /** * controlIOBuffer is a DOM element bound to the port (currently used for output only; see transmitByte()). * * @type {Object} */ this.controlIOBuffer = null; /* * If controlIOBuffer is being used AND 'tabSize' is set, then we make an attempt to monitor the characters * being echoed via transmitByte(), maintain a logical column position, and convert any tabs into the appropriate * number of spaces. * * charBOL, if nonzero, is a character to automatically output at the beginning of every line. This probably * isn't generally useful; I use it internally to preformat serial output. */ this.tabSize = parmsSerial['tabSize']; this.charBOL = parmsSerial['charBOL']; this.iLogicalCol = 0; /* * fAutoXOFF enables some experimental auto-XOFF/XON processing. It assumes if the VT100 firmware * issues an XOFF, receiveByte() should stop accepting more data until the firmware issues an XOFF. * * The downside is that this doesn't really do anything to stem the flow of incoming data; it just * prevents the VT100's internal buffer from overflowing. TODO: Eliminate the need for this hack * and add some *real* flow-control interfaces between connected SerialPort components. */ this.fAutoXOFF = true; this.fAutoStop = false; this.fNullModem = true; var sBinding = parmsSerial['binding']; if (sBinding == "console") { this.consoleOutput = ""; } else { /* * NOTE: If sBinding is not the name of a valid Control Panel DOM element, this call does nothing. */ Component.bindExternalControl(this, sBinding, SerialPort8080.sIOBuffer); } /* * No connection until initConnection() is called. */ this.sDataReceived = ""; this.connection = this.sendData = this.updateStatus = null; /* * Export all functions required by initConnection(). */ this['exports'] = { 'connect': this.initConnection, 'receiveData': this.receiveData, 'receiveStatus': this.receiveStatus }; } /** * setBinding(sHTMLType, sBinding, control, sValue) * * @this {SerialPort8080} * @param {string|null} sHTMLType is the type of the HTML control (eg, "button", "list", "text", "submit", "textarea", "canvas") * @param {string} sBinding is the value of the 'binding' parameter stored in the HTML control's "data-value" attribute (eg, "buffer") * @param {Object} control is the HTML control DOM object (eg, HTMLButtonElement) * @param {string} [sValue] optional data value * @return {boolean} true if binding was successful, false if unrecognized binding request */ setBinding(sHTMLType, sBinding, control, sValue) { var serial = this; switch (sBinding) { case SerialPort8080.sIOBuffer: this.bindings[sBinding] = this.controlIOBuffer = control; /* * By establishing an onkeypress handler here, we make it possible for DOS commands like * "CTTY COM1" to more or less work (use "CTTY CON" to restore control to the DOS console). */ control.onkeydown = function onKeyDown(event) { /* * This is required in addition to onkeypress, because it's the only way to prevent * BACKSPACE (keyCode 8) from being interpreted by the browser as a "Back" operation; * moreover, not all browsers generate an onkeypress notification for BACKSPACE. * * A related problem exists for Ctrl-key combinations in most Windows-based browsers * (eg, IE, Edge, Chrome for Windows, etc), because keys like Ctrl-C and Ctrl-S have * special meanings (eg, Copy, Save). To the extent the browser will allow it, we * attempt to disable that default behavior when this control receives an onkeydown * event for one of those keys (probably the only event the browser generates for them). */ event = event || window.event; var keyCode = event.keyCode; if (keyCode === 0x08 || event.ctrlKey && keyCode >= 0x41 && keyCode <= 0x5A) { if (event.preventDefault) event.preventDefault(); if (keyCode > 0x40) keyCode -= 0x40; serial.receiveByte(keyCode); } return true; }; control.onkeypress = function onKeyPress(event) { /* * Browser-independent keyCode extraction; refer to onKeyPress() and the other key event * handlers in keyboard.js. */ event = event || window.event; var keyCode = event.which || event.keyCode; serial.receiveByte(keyCode); /* * Since we're going to remove the "readonly" attribute from the