This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathparallelport.js
520 lines (484 loc) · 16 KB
/
parallelport.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/**
* @fileoverview Implements the PCjs ParallelPort component.
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @version 1.0
* Created 2012-Jul-01
*
* Copyright © 2012-2016 Jeff Parsons <[email protected]>
*
* This file is part of PCjs, which is part of the JavaScript Machines Project (aka JSMachines)
* at <http://jsmachines.net/> and <http://pcjs.org/>.
*
* 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 <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every source code file of every
* copy or modified version of this work, and to display that copyright notice on every screen
* that loads or runs any version of this software (see Computer.COPYRIGHT).
*
* 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 the
* PCjs program 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 web = require("../../shared/lib/weblib");
var Component = require("../../shared/lib/component");
var Messages = require("./messages");
var ChipSet = require("./chipset");
var State = require("./state");
}
/**
* ParallelPort(parmsParallel)
*
* The ParallelPort component has the following component-specific (parmsParallel) properties:
*
* adapter: 1 (port 0x3BC), 2 (port 0x378), or 3 (port 0x278); 0 if not defined
*
* binding: name of a control (based on its "binding" attribute) to bind to this port's I/O
*
* In the future, we may support 'port' and 'irq' properties that allow the machine to define a
* non-standard parallel 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.
*
* DOS typically names the Primary adapter "LPT1" and the Secondary adapter "LPT2", but I prefer
* to stick to adapter numbers, since not all operating systems follow those naming conventions.
*
* @constructor
* @extends Component
* @param {Object} parmsParallel
*/
function ParallelPort(parmsParallel) {
this.iAdapter = parmsParallel['adapter'];
switch (this.iAdapter) {
case 1:
this.portBase = 0x3BC;
this.nIRQ = ChipSet.IRQ.LPT1;
break;
case 2:
this.portBase = 0x378;
this.nIRQ = ChipSet.IRQ.LPT1;
break;
case 3:
this.portBase = 0x278;
this.nIRQ = ChipSet.IRQ.LPT2;
break;
default:
Component.warning("Unrecognized parallel adapter #" + this.iAdapter);
return;
}
/**
* consoleOutput becomes a string that records parallel 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, if any, bound to the port (currently used for output only; see echoByte()).
*
* @type {Object}
*/
this.controlIOBuffer = null;
Component.call(this, "ParallelPort", parmsParallel, ParallelPort, Messages.PARALLEL);
var sBinding = parmsParallel['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, ParallelPort.sIOBuffer);
}
}
/*
* class ParallelPort
* property {number} iAdapter
* property {number} portBase
* property {number} nIRQ
* property {Object} controlIOBuffer is a DOM element, if any, bound to the port (for rudimentary output; see echoByte())
*
* NOTE: This class declaration started as a way of informing the code inspector of the controlIOBuffer property,
* which remained undefined until a setBinding() call set it later, but I've since decided that explicitly
* initializing such properties in the constructor is a better way to go -- even though it's more code -- because
* JavaScript compilers are supposed to be happier when the underlying object structures aren't constantly changing.
*
* Besides, I'm not sure I want to get into documenting every property this way, for this or any/every other class,
* let alone getting into which ones should be considered private or protected, because PCjs isn't really a library
* for third-party apps.
*/
Component.subclass(ParallelPort);
/*
* Internal name used for the I/O buffer control, if any, that we bind to the ParallelPort.
*
* Alternatively, if ParallelPort wants to use another component's control (eg, the Panel's
* "print" control), it can specify the name of that control with the 'binding' property.
*
* For that binding to succeed, we also need to know the target component; for now, that's
* been hard-coded to "Panel", in part because that's one of the few components we can rely
* upon initializing before we do, but it would be a simple matter to include a component type
* or ID as part of the 'binding' property as well, if we need more flexibility later.
*/
ParallelPort.sIOBuffer = "buffer";
/*
* The "Data Register" is an input/output register at offset 0 from portBase. The bit-to-pin mappings are:
*
* Bit Pin
* --- ---
* 0 2 // 0x01 (DATA 1)
* 1 3 // 0x02 (DATA 2)
* 2 4 // 0x04 (DATA 3)
* 3 5 // 0x08 (DATA 4)
* 4 6 // 0x10 (DATA 5)
* 5 7 // 0x20 (DATA 6)
* 6 8 // 0x40 (DATA 7)
* 7 9 // 0x80 (DATA 8)
*/
ParallelPort.DATA = { // (read/write)
REG: 0
};
/*
* The "Status Register" is an input register at offset 1 from portBase. The bit-to-pin mappings are:
*
* Bit Pin
* --- ---
* 0 - // 0x01
* 1 - // 0x02
* 2 - // 0x04
* 3 15 // 0x08 (not used)
* 4 13 // 0x10 (printer is in the selected state)
* 5 12 // 0x20 (out of paper)
* 6 10 // 0x40 (printer not yet ready to accept another character)
* 7 11 // 0x80 (printer cannot receive data; eg, printer off-line, or print operation in progress)
*/
ParallelPort.STATUS = { // (read)
REG: 1,
NOTREADY: 0x40 // when this bit goes clear, interrupt requested
};
/*
* The "Control Register" is an input/output register at offset 2 from portBase. The bit-to-pin mappings are:
*
* Bit Pin
* --- ---
* 0 !1 // 0x01 (read input data)
* 1 !14 // 0x02 (automatically feed paper one line)
* 2 16 // 0x04
* 3 !17 // 0x08
*
* Additionally, bit 4 is the IRQ ENABLE bit, which allows interrupts when pin 10 transitions high to low.
*/
ParallelPort.CONTROL = { // (read/write)
REG: 2,
IRQ_ENABLE: 0x10 // set to enable interrupts
};
/**
* setBinding(sHTMLType, sBinding, control, sValue)
*
* @this {ParallelPort}
* @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
*/
ParallelPort.prototype.setBinding = function(sHTMLType, sBinding, control, sValue)
{
switch (sBinding) {
case ParallelPort.sIOBuffer:
this.bindings[sBinding] = this.controlIOBuffer = control;
return true;
default:
break;
}
return false;
};
/**
* initBus(cmp, bus, cpu, dbg)
*
* @this {ParallelPort}
* @param {Computer} cmp
* @param {Bus} bus
* @param {X86CPU} cpu
* @param {Debugger} dbg
*/
ParallelPort.prototype.initBus = function(cmp, bus, cpu, dbg)
{
this.bus = bus;
this.cpu = cpu;
this.dbg = dbg;
this.chipset = cmp.getMachineComponent("ChipSet");
bus.addPortInputTable(this, ParallelPort.aPortInput, this.portBase);
bus.addPortOutputTable(this, ParallelPort.aPortOutput, this.portBase);
this.setReady();
};
/**
* powerUp(data, fRepower)
*
* @this {ParallelPort}
* @param {Object|null} data
* @param {boolean} [fRepower]
* @return {boolean} true if successful, false if failure
*/
ParallelPort.prototype.powerUp = function(data, fRepower)
{
if (!fRepower) {
if (!data || !this.restore) {
this.reset();
} else {
if (!this.restore(data)) return false;
}
}
return true;
};
/**
* powerDown(fSave, fShutdown)
*
* @this {ParallelPort}
* @param {boolean} [fSave]
* @param {boolean} [fShutdown]
* @return {Object|boolean} component state if fSave; otherwise, true if successful, false if failure
*/
ParallelPort.prototype.powerDown = function(fSave, fShutdown)
{
return fSave? this.save() : true;
};
/**
* reset()
*
* @this {ParallelPort}
*/
ParallelPort.prototype.reset = function()
{
this.initState();
};
/**
* save()
*
* This implements save support for the ParallelPort component.
*
* @this {ParallelPort}
* @return {Object}
*/
ParallelPort.prototype.save = function()
{
var state = new State(this);
state.set(0, this.saveRegisters());
return state.data();
};
/**
* restore(data)
*
* This implements restore support for the ParallelPort component.
*
* @this {ParallelPort}
* @param {Object} data
* @return {boolean} true if successful, false if failure
*/
ParallelPort.prototype.restore = function(data)
{
return this.initState(data[0]);
};
/**
* initState(data)
*
* @this {ParallelPort}
* @param {Array} [data]
* @return {boolean} true if successful, false if failure
*/
ParallelPort.prototype.initState = function(data)
{
var i = 0;
if (data === undefined) {
data = [0, 0, 0];
}
this.bData = data[i++];
this.bStatus = data[i++];
this.bControl = data[i];
return true;
};
/**
* saveRegisters()
*
* @this {ParallelPort}
* @return {Array}
*/
ParallelPort.prototype.saveRegisters = function()
{
var i = 0;
var data = [];
data[i++] = this.bData;
data[i++] = this.bStatus;
data[i] = this.bControl;
return data;
};
/**
* inData(port, addrFrom)
*
* @this {ParallelPort}
* @param {number} port (0x3BC, 0x378, or 0x278)
* @param {number} [addrFrom] (not defined whenever the Debugger tries to read the specified port)
* @return {number} simulated port value
*/
ParallelPort.prototype.inData = function(port, addrFrom)
{
var b = this.bData;
this.printMessageIO(port, null, addrFrom, "DATA", b);
return b;
};
/**
* inStatus(port, addrFrom)
*
* @this {ParallelPort}
* @param {number} port (0x3BD, 0x379, or 0x279)
* @param {number} [addrFrom] (not defined whenever the Debugger tries to read the specified port)
* @return {number} simulated port value
*/
ParallelPort.prototype.inStatus = function(port, addrFrom)
{
var b = this.bStatus;
this.printMessageIO(port, null, addrFrom, "STAT", b);
return b;
};
/**
* inControl(port, addrFrom)
*
* @this {ParallelPort}
* @param {number} port (0x3BE, 0x37A, or 0x27A)
* @param {number} [addrFrom] (not defined whenever the Debugger tries to read the specified port)
* @return {number} simulated port value
*/
ParallelPort.prototype.inControl = function(port, addrFrom)
{
var b = this.bControl;
this.printMessageIO(port, null, addrFrom, "CTRL", b);
return b;
};
/**
* outData(port, bOut, addrFrom)
*
* @this {ParallelPort}
* @param {number} port (0x3BC, 0x378, or 0x278)
* @param {number} bOut
* @param {number} [addrFrom] (not defined whenever the Debugger tries to write the specified port)
*/
ParallelPort.prototype.outData = function(port, bOut, addrFrom)
{
this.printMessageIO(port, bOut, addrFrom, "DATA");
this.bData = bOut;
this.bStatus |= ParallelPort.STATUS.NOTREADY;
if (this.echoByte(bOut)) {
this.bStatus &= ~ParallelPort.STATUS.NOTREADY;
}
this.updateIRR();
};
/**
* outControl(port, bOut, addrFrom)
*
* @this {ParallelPort}
* @param {number} port (0x3BE, 0x37A, or 0x27A)
* @param {number} bOut
* @param {number} [addrFrom] (not defined whenever the Debugger tries to write the specified port)
*/
ParallelPort.prototype.outControl = function(port, bOut, addrFrom)
{
this.printMessageIO(port, bOut, addrFrom, "CTRL");
this.bControl = bOut;
this.updateIRR();
};
/**
* updateIRR()
*
* @this {ParallelPort}
*/
ParallelPort.prototype.updateIRR = function()
{
if (this.chipset && this.nIRQ) {
if ((this.bControl & ParallelPort.CONTROL.IRQ_ENABLE) && !(this.bStatus & ParallelPort.STATUS.NOTREADY)) {
this.chipset.setIRR(this.nIRQ);
} else {
this.chipset.clearIRR(this.nIRQ);
}
}
};
/**
* echoByte(b)
*
* @this {ParallelPort}
* @param {number} b
* @return {boolean} true if echoed, false if not
*/
ParallelPort.prototype.echoByte = function(b)
{
if (this.controlIOBuffer) {
if (b == 0x08) {
this.controlIOBuffer.value = this.controlIOBuffer.value.slice(0, -1);
}
else {
this.controlIOBuffer.value += String.fromCharCode(b);
this.controlIOBuffer.scrollTop = this.controlIOBuffer.scrollHeight;
}
return true;
}
if (this.consoleOutput != null) {
if (b == 0x0A || this.consoleOutput.length >= 1024) {
this.println(this.consoleOutput);
this.consoleOutput = "";
}
if (b != 0x0A) {
this.consoleOutput += String.fromCharCode(b);
}
return true;
}
return false;
};
/*
* Port input notification table
*/
ParallelPort.aPortInput = {
0x0: ParallelPort.prototype.inData,
0x1: ParallelPort.prototype.inStatus,
0x2: ParallelPort.prototype.inControl
};
/*
* Port output notification table
*/
ParallelPort.aPortOutput = {
0x0: ParallelPort.prototype.outData,
0x2: ParallelPort.prototype.outControl
};
/**
* ParallelPort.init()
*
* This function operates on every HTML element of class "parallel", extracting the
* JSON-encoded parameters for the ParallelPort constructor from the element's "data-value"
* attribute, invoking the constructor to create a ParallelPort component, and then binding
* any associated HTML controls to the new component.
*/
ParallelPort.init = function()
{
var aeParallel = Component.getElementsByClass(window.document, PCJSCLASS, "parallel");
for (var iParallel = 0; iParallel < aeParallel.length; iParallel++) {
var eParallel = aeParallel[iParallel];
var parmsParallel = Component.getComponentParms(eParallel);
var parallel = new ParallelPort(parmsParallel);
Component.bindComponentControls(parallel, eParallel, PCJSCLASS);
}
};
/*
* Initialize every ParallelPort module on the page.
*/
web.onInit(ParallelPort.init);
if (NODE) module.exports = ParallelPort;