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 pathtestmon.js
492 lines (457 loc) · 14.7 KB
/
testmon.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
#!/usr/bin/env node
/**
* @fileoverview Node-based Test Monitor
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2018 Jeff Parsons
*
* This file is part of PCjs, a computer emulation software project at <https://www.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 modified copy of this work
* and to display that copyright notice when the software starts running; see COPYRIGHT in
* <https://www.pcjs.org/modules/shared/lib/defines.js>.
*
* 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";
var fs = require("fs");
var path = require("path");
var SerialPort = require("serialport");
var Defines = require("../../../modules/shared/lib/defines");
var Keys = require("../../../modules/shared/lib/keys");
var Str = require("../../../modules/shared/lib/strlib");
var Proc = require("../../../modules/shared/lib/proclib");
var TestMonitor = require("../../../modules/pcx86/lib/testmon.js");
var Machines = require("../../../_data/machines.json");
var fDebug = false;
var args = Proc.getArgs();
var argv = args.argv;
var baudRate = 2400;
var rtscts = true;
if (argv['debug'] !== undefined) {
fDebug = argv['debug'];
}
if (argv['baud'] !== undefined) {
baudRate = +argv['baud'];
console.log("opening with baudRate " + baudRate);
}
if (global.DEBUG !== undefined) {
global.DEBUG = fDebug;
}
if (global.APPVERSION !== undefined) {
global.APPVERSION = Machines['shared']['version'];
}
/**
* The PortController class mimics the PCx86 TestController class, providing wrappers around Node's SerialPort
* interfaces that are compatible with the PCx86 TestMonitor module.
*
* @class PortController
*/
class PortController {
/**
* PortController()
*
* @this {PortController}
* @param {string} path
* @param {Object} options
*/
constructor(path, options)
{
let controller = this;
this.port = new SerialPort(path, options);
this.port.on('data', function(data) {
controller.receiveData(data);
});
// this.port.on('open', function() {
// console.log("Connected to: ",controller.port.path);
// controller.port.get(function(error, status){
// console.log('get() results:');
// console.log(error);
// console.log(status);
// });
// });
this.stdin = process.stdin;
this.stdout = process.stdout;
try {
this.stdin.setRawMode(true);
} catch(err) {
console.log("unable to use stdin.setRawMode()");
}
this.stdin.resume();
this.stdin.setEncoding("utf8");
this.stdin.on('data', function(data) {
controller.receiveInput(data);
});
/*
* Ctrl-F is used to toggle "file prompt" mode. If a valid filename is received from stdin
* in that mode, then we transition to "file transfer" mode.
*/
this.fFilePrompt = false;
this.fFileTransfer = false;
this.sFileName = "";
this.bufFileData = null;
this.iFileData = -1;
this.aFileBlock = [];
this.cbBlockSize = 1024;
this.tests = require("./tests.json");
this.deliverData = this.deliverInput = this.deliverTests = null;
let monitor = new TestMonitor();
monitor.bindController(this, this.sendData, this.sendOutput, this.printf);
}
/**
* bindMonitor(monitor, deliverData, deliverInput, deliverTests)
*
* @this {PortController}
* @param {TestMonitor} monitor
* @param {function(number)} deliverData
* @param {function(number)} deliverInput
* @param {function(Object)} deliverTests
*/
bindMonitor(monitor, deliverData, deliverInput, deliverTests)
{
this.deliverData = deliverData.bind(monitor);
this.deliverInput = deliverInput.bind(monitor);
this.deliverTests = deliverTests.bind(monitor);
if (this.tests && this.deliverTests) {
this.deliverTests(this.tests);
this.tests = null;
}
}
/**
* printf(format, ...args)
*
* @this {PortController}
* @param {string} format
* @param {...} args
*/
printf(format, ...args)
{
this.stdout.write(Str.sprintf(format, ...args));
}
/**
* receiveData(data)
*
* @this {PortController}
* @param {number|string|Array} data
*/
receiveData(data)
{
if (this.fFileTransfer) {
let response = data[0];
if (response == Keys.ASCII.CTRL_M) {
this.sendNextBlock();
}
else if (response == Keys.ASCII.CTRL_R) {
this.sendBlock();
}
else {
this.endTransfer("unexpected transfer response: " + data);
}
}
if (this.deliverInput) {
if (typeof data == "number") {
this.deliverData(data);
}
else if (typeof data == "string") {
for (let i = 0; i < data.length; i++) this.deliverData(data.charCodeAt(i));
}
else {
for (let i = 0; i < data.length; i++) this.deliverData(data[i]);
}
} else {
console.log("data(" + typeof data + "): ", data);
}
}
/**
* receiveInput(ch)
*
* @this {PortController}
* @param {string} ch
*/
receiveInput(ch)
{
let data = ch.charCodeAt(0);
if (data == Keys.ASCII.CTRL_C) {
if (this.fFileTransfer) {
this.endTransfer("transfer aborted");
return;
}
process.exit();
return;
}
if (data == Keys.ASCII.CTRL_F) {
if (!this.fFilePrompt) {
this.printf("\nname of file to transfer: ");
this.fFilePrompt = true;
this.sFileName = "";
} else {
this.printf("\ntransfer cancelled\n");
this.fFilePrompt = false;
}
return;
}
if (this.fFilePrompt) {
if (data == Keys.ASCII.CTRL_M) {
data = 0;
this.fFilePrompt = false;
this.printf("\n");
if (this.sFileName) {
this.fFileTransfer = true;
this.startTransfer();
}
}
else if (data == Keys.ASCII.CTRL_H || data == Keys.ASCII.DEL) {
data = 0;
if (this.sFileName) {
this.sFileName = this.sFileName.slice(0, -1);
this.printf("%s", "\b \b");
}
} else if (data < 32 || data > 127) {
data = 0;
}
if (data) {
this.sFileName += ch;
this.printf("%c", data);
}
return;
}
if (this.deliverInput) {
this.deliverInput(data);
}
/*
* Node defines the first parameter of write() as a "chunk", which can be a string, Buffer, or Uint8Array.
*/
// this.stdout.write(data);
}
/**
* sendData(data)
*
* @this {PortController}
* @param {number|string|Array} data
*/
sendData(data)
{
if (typeof data == "number") {
data = [data];
}
if (typeof data == "string") {
this.port.write(data);
} else {
this.port.write(Buffer.from(data));
}
}
/**
* sendOutput(data)
*
* @this {PortController}
* @param {number|string|Array} data
*/
sendOutput(data)
{
if (typeof data == "number") {
this.printf("%c", data);
}
else if (typeof data == "string") {
this.printf("%s", data);
}
else {
for (let i = 0; i < data.length; i++) this.printf("[0x%02x]", data[i]);
}
}
/**
* buildDateTime(dateMod)
*
* Adapted from the corresponding function in DiskDump.js
*
* @this {PortController}
* @param {Date} dateMod contains the modification time of a file
* @return {number} the time (bits 0-15) and date (bits 16-31) in FAT format
*/
buildDateTime(dateMod)
{
let year = dateMod.getFullYear();
let month = dateMod.getMonth() + 1;
let day = dateMod.getDate();
let time = ((dateMod.getHours() & 0x1F) << 11) | ((dateMod.getMinutes() & 0x3F) << 5) | ((dateMod.getSeconds() >> 1) & 0x1F);
/*
* NOTE: If validateTime() is doing its job, then we should never have to do this. This is simple paranoia.
*/
if (year < 1980) {
year = 1980; month = 1; day = 1; time = 1;
} else if (year > 2099) {
year = 2099; month = 12; day = 31; time = 1;
}
let date = (((year - 1980) & 0x7F) << 9) | (month << 5) | day;
return ((date & 0xffff) << 16) | (time & 0xffff);
}
/**
* buildShortName(sFileName)
*
* Adapted from the corresponding function in DiskDump.js
*
* @this {PortController}
* @param {string} sFileName
* @return {string} containing a corresponding filename in FAT "8.3" format
*/
buildShortName(sFileName)
{
let sName = path.basename(sFileName).toUpperCase();
let iExt = sName.lastIndexOf('.');
let sExt = "";
if (iExt >= 0) {
sExt = sName.substr(iExt+1);
sName = sName.substr(0, iExt);
}
sName = sName.substr(0, 8).trim();
sExt = sExt.substr(0, 3).trim();
let iPeriod = -1;
if (sExt) {
iPeriod = sName.length;
sName += '.' + sExt;
}
for (let i = 0; i < sName.length; i++) {
if (i == iPeriod) continue;
let ch = sName.charAt(i);
if ("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'()-@^_`{}~".indexOf(ch) < 0) {
sName = sName.substr(0, i) + '_' + sName.substr(i+1);
}
}
return sName;
};
/**
* sendBlock()
*
* @this {PortController}
*/
sendBlock()
{
/*
* The initial block is preceded by a "raw" Ctrl-F as a signal to the receiving end.
*/
if (!this.iFileData) {
this.sendData(Keys.ASCII.CTRL_F);
}
let cb = this.aFileBlock.length;
this.sendBlockByte(cb & 0xff);
this.sendBlockByte((cb >> 8) & 0xff);
let crc = 0;
for (let i = 0; i < this.aFileBlock.length; i++) {
let b = this.aFileBlock[i];
this.sendBlockByte(b);
crc += b;
}
this.sendBlockByte(crc & 0xff);
this.printf("block sent (%d bytes)\n", cb);
}
/**
* sendNextBlock()
*
* @this {PortController}
*/
sendNextBlock()
{
this.aFileBlock = [];
for (let i = 0; i < this.cbBlockSize && this.iFileData < this.bufFileData.length; i++) {
let b = this.bufFileData[this.iFileData++];
this.aFileBlock.push(b);
}
if (this.aFileBlock.length) {
this.sendBlock();
} else {
this.endTransfer();
}
}
/**
* sendBlockByte(b)
*
* This converts any "control characters" in the range 0x00 to 0x1B (Ctrl-@ to Ctrl-[) into
* the corresponding ASCII sequence "^@" to "^[". This is a superset of control characters Ctrl-A
* to Ctrl-Z, which will be converted to the corresponding "^A" to "^Z" sequences.
*
* And since '^' has been hijacked for this purpose, we also convert '^' into "^^".
*
* The idea is to reserve ACTUAL control characters for special operations that require immediate
* action. This, of course, is overkill, and introduces far more overhead than we really need, but I'm
* not interested in maximum speed for these occasional file transfers.
*
* @this {PortController}
* @param {number} b
*/
sendBlockByte(b)
{
if (b <= 27) {
this.sendData(Keys.ASCII['^']);
this.sendData(b + Keys.ASCII['@']);
} else if (b == Keys.ASCII['^']) {
this.sendData(Keys.ASCII['^']);
this.sendData(Keys.ASCII['^']);
} else {
this.sendData(b);
}
}
/**
* setBlock(s)
*
* @this {PortController}
* @param {string} s
*/
setBlock(s)
{
this.aFileBlock = [];
for (let i = 0; i < s.length; i++) {
this.aFileBlock.push(s.charCodeAt(i));
}
}
/**
* startTransfer()
*
* @this {PortController}
*/
startTransfer()
{
if (!this.fFileTransfer) return;
try {
let stats = fs.statSync(this.sFileName);
let dateTime = this.buildDateTime(stats.mtime);
let cbSize = stats.size;
this.bufFileData = fs.readFileSync(this.sFileName);
if (this.bufFileData && this.bufFileData.length == cbSize) {
this.iFileData = 0;
this.setBlock('|' + this.buildShortName(this.sFileName) + '|' + Str.toHex(cbSize, 8) + '|' + Str.toHex(dateTime, 8) + '|');
this.sendBlock();
} else {
this.endTransfer("incorrect data length: " + this.bufFileData.length);
}
} catch(err) {
this.endTransfer(err.message);
}
}
/**
* startTransfer(sError)
*
* @this {PortController}
* @param {string} [sError]
*/
endTransfer(sError)
{
this.fFileTransfer = false;
this.bufFileData = null;
this.iFileData = -1;
this.aFileBlock = [];
this.printf("%s\n", sError? ("transfer error (" + sError + ")") : "transfer complete");
}
}
let controller = new PortController("/dev/tty.KeySerial1", {baudRate, rtscts});