forked from jeffpar/pcjs.v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumio.js
608 lines (583 loc) · 22.2 KB
/
numio.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/**
* @fileoverview Class with number processing functions
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2020 Jeff Parsons
* @license MIT
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*/
"use strict";
/**
* @class {NumIO}
* @unrestricted
*/
class NumIO extends Defs {
/**
* NumIO()
*
* String to integer conversion:
*
* isInt()
* parseInt()
* parseResource()
* parseSwitches()
*
* Integer to string conversion:
*
* toBase()
*
* Bit operations (for values with more than 32 bits):
*
* clearBits()
* setBits()
* testBits()
*
* Numeric array compression/decompression:
*
* compress()
* decompress()
*
* Initially, this file was going to be called "stdlib.js", since the C runtime library file "stdlib.h"
* defines numeric conversion functions like atoi(). But stdlib has too many other functions that have
* nothing to do with data conversion, and we have many conversion functions that you won't find in stdlib.
* So I settled on "numio.js" instead.
*
* @this {NumIO}
*/
constructor()
{
super();
}
/**
* isInt(s, base)
*
* The built-in parseInt() function has the annoying feature of returning a partial value (ie,
* up to the point where it encounters an invalid character); eg, parseInt("foo", 16) returns 0xf.
*
* So it's best to use our own parseInt() function, which will in turn use this function to validate
* the entire string.
*
* @this {NumIO}
* @param {string} s is the string representation of some number
* @param {number} [base] is the radix to use (default is 10); only 2, 8, 10 and 16 are supported
* @returns {boolean} true if valid, false if invalid (or the specified base isn't supported)
*/
isInt(s, base)
{
if (!base || base == 10) return s.match(/^[+-]?[0-9]+$/) !== null;
if (base == 16) return s.match(/^-?[0-9a-f]+$/i) !== null;
if (base == 8) return s.match(/^-?[0-7]+$/) !== null;
if (base == 2) return s.match(/^-?[01]+$/) !== null;
return false;
}
/**
* parseInt(s, base)
*
* This is a wrapper around the built-in parseInt() function. Our wrapper recognizes certain prefixes
* ('$' or "0x" for hex, '#' or "0o" for octal) and suffixes ('.' for decimal, 'h' for hex, 'y' for
* binary), and then calls isInt() to ensure we don't convert strings that contain partial values;
* see isInt() for details.
*
* The use of multiple prefix/suffix combinations is undefined (although for the record, we process
* prefixes first). We do NOT support the "0b" prefix to indicate binary UNLESS one or more commas are
* also present (because "0b" is also a valid hex sequence), and we do NOT support a single leading zero
* to indicate octal (because such a number could also be decimal or hex). Any number of commas are
* allowed; we remove them all before calling the built-in parseInt().
*
* More recently, we've added support for "^D", "^O", and "^B" prefixes to accommodate the base overrides
* that the PDP-10's MACRO-10 assembly language supports (decimal, octal, and binary, respectively).
* If this support turns out to adversely affect other debuggers, then it will have to be "conditionalized".
* Similarly, we've added support for "K", "M", and "G" MACRO-10-style suffixes that add 3, 6, or 9 zeros
* to the value to be parsed, respectively.
*
* @this {NumIO}
* @param {string} s is the string representation of some number
* @param {number} [base] is the radix to use (default is 10); can be overridden by prefixes/suffixes
* @returns {number|undefined} corresponding value, or undefined if invalid
*/
parseInt(s, base)
{
let value;
if (s) {
if (!base) base = 10;
let ch, chPrefix, chSuffix;
let fCommas = (s.indexOf(',') > 0);
if (fCommas) s = s.replace(/,/g, '');
ch = chPrefix = s.charAt(0);
if (chPrefix == '#') {
base = 8;
chPrefix = '';
}
else if (chPrefix == '$') {
base = 16;
chPrefix = '';
}
if (ch != chPrefix) {
s = s.substr(1);
}
else {
ch = chPrefix = s.substr(0, 2);
if (chPrefix == '0b' && fCommas || chPrefix == '^B') {
base = 2;
chPrefix = '';
}
else if (chPrefix == '0o' || chPrefix == '^O') {
base = 8;
chPrefix = '';
}
else if (chPrefix == '^D') {
base = 10;
chPrefix = '';
}
else if (chPrefix == '0x') {
base = 16;
chPrefix = '';
}
if (ch != chPrefix) s = s.substr(2);
}
ch = chSuffix = s.slice(-1);
if (chSuffix == 'Y' || chSuffix == 'y') {
base = 2;
chSuffix = '';
}
else if (chSuffix == '.') {
base = 10;
chSuffix = '';
}
else if (chSuffix == 'H' || chSuffix == 'h') {
base = 16;
chSuffix = '';
}
else if (chSuffix == 'K') {
chSuffix = '000';
}
else if (chSuffix == 'M') {
chSuffix = '000000';
}
else if (chSuffix == 'G') {
chSuffix = '000000000';
}
if (ch != chSuffix) s = s.slice(0, -1) + chSuffix;
/*
* This adds support for the MACRO-10 binary shifting (Bn) suffix, which must be stripped from the
* number before parsing, and then applied to the value after parsing. If n is omitted, 35 is assumed,
* which is a net shift of zero. If n < 35, then a left shift of (35 - n) is required; if n > 35, then
* a right shift of -(35 - n) is required.
*/
let v, shift = 0;
if (base <= 10) {
let match = s.match(/(-?[0-9]+)B([0-9]*)/);
if (match) {
s = match[1];
shift = 35 - ((match[2] || 35) & 0xff);
}
}
if (this.isInt(s, base) && !isNaN(v = parseInt(s, base))) {
/*
* With the need to support larger (eg, 36-bit) integers, truncating to 32 bits is no longer helpful.
*
* value = v|0;
*/
if (shift) {
/*
* Since binary shifting is a logical operation, and since shifting by division only works properly
* with positive numbers, we must convert a negative value to a positive value, by computing the two's
* complement.
*/
if (v < 0) v += Math.pow(2, 36);
if (shift > 0) {
v *= Math.pow(2, shift);
} else {
v = Math.trunc(v / Math.pow(2, -shift));
}
}
value = v;
}
}
return value;
}
/**
* parseResource(sURL, sData)
*
* This converts a variety of JSON-style data streams into an Object with the following properties:
*
* aBytes
* aSymbols
* addrLoad
* addrExec
*
* If the source data contains a 'bytes' array, it's passed through to 'aBytes'; alternatively, if
* it contains a 'words' array, the values are converted from 16-bit to 8-bit and stored in 'aBytes',
* and if it contains a 'longs' array, the values are converted from 32-bit longs into bytes and
* stored in 'aBytes'.
*
* Alternatively, if the source data contains a 'data' array, we simply pass that through to the output
* object as:
*
* aData
*
* @this {NumIO}
* @param {string} sURL
* @param {string} sData
* @returns {Object|null} (resource)
*/
parseResource(sURL, sData)
{
let i;
let resource = {
aBytes: null,
aSymbols: null,
addrLoad: null,
addrExec: null
};
if (sData.charAt(0) == "[" || sData.charAt(0) == "{") {
try {
let a, ib, data;
if (sData.substr(0, 1) == "<") { // if the "data" begins with a "<"...
/*
* Early server configs reported an error (via the nErrorCode parameter) if a tape URL was invalid,
* but more recent server configs now display a somewhat friendlier HTML error page. The downside,
* however, is that the original error has been buried, and we've received "data" that isn't actually
* tape data. So if the data we've received appears to be "HTML-like", we treat it as an error message.
*/
throw new Error(sData);
}
/*
* TODO: IE9 is rather unfriendly and restrictive with regard to how much data it's willing to
* eval(). In particular, the 10Mb disk image we use for the Windows 1.01 demo config fails in
* IE9 with an "Out of memory" exception. One work-around would be to chop the data into chunks
* (perhaps one track per chunk, using regular expressions) and then manually re-assemble it.
*
* However, it turns out that using JSON.parse(sDiskData) instead of eval("(" + sDiskData + ")")
* is a much easier fix. The only drawback is that we must first quote any unquoted property names
* and remove any comments, because while eval() was cool with them, JSON.parse() is more particular;
* the following RegExp replacements take care of those requirements.
*
* The use of hex values is something else that eval() was OK with, but JSON.parse() is not, and
* while I've stopped using hex values in DumpAPI responses (at least when "format=json" is specified),
* I can't guarantee they won't show up in "legacy" images, and there's no simple RegExp replacement
* for transforming hex values into decimal values, so I cop out and fall back to eval() if I detect
* any hex prefixes ("0x") in the sequence. Ditto for error messages, which appear like so:
*
* ["unrecognized disk path: test.img"]
*/
if (sData.indexOf("0x") < 0 && sData.indexOf("0o") < 0 && sData.substr(0, 2) != '["') {
data = JSON.parse(sData.replace(/([a-z]+):/gm, '"$1":').replace(/\/\/[^\n]*/gm, ""));
} else {
data = eval("(" + sData + ")");
}
resource.addrLoad = data['load'];
resource.addrExec = data['exec'];
let width = data['width'];
let values = data['values'];
if (width && values) {
if (width == 8) {
data['bytes'] = values;
} else if (width == 16) {
data['words'] = values;
} else if (width == 32) {
data['longs'] = values;
} else {
data['data'] = values;
}
}
if ((a = data['bytes'])) {
resource.aBytes = a;
}
else if ((a = data['words'])) {
/*
* Convert all words into bytes
*/
resource.aBytes = new Array(a.length * 2);
for (i = 0, ib = 0; i < a.length; i++) {
resource.aBytes[ib++] = a[i] & 0xff;
resource.aBytes[ib++] = (a[i] >> 8) & 0xff;
this.assert(!(a[i] & ~0xffff));
}
}
else if ((a = data['longs'])) {
/*
* Convert all dwords (longs) into bytes
*/
resource.aBytes = new Array(a.length * 4);
for (i = 0, ib = 0; i < a.length; i++) {
resource.aBytes[ib++] = a[i] & 0xff;
resource.aBytes[ib++] = (a[i] >> 8) & 0xff;
resource.aBytes[ib++] = (a[i] >> 16) & 0xff;
resource.aBytes[ib++] = (a[i] >> 24) & 0xff;
}
}
else if ((a = data['data'])) {
resource.aData = a;
}
else {
resource.aBytes = data;
}
if (resource.aBytes) {
if (!resource.aBytes.length) {
this.error("empty resource: %s", sURL);
resource = null;
}
else if (resource.aBytes.length == 1) {
this.error(resource.aBytes[0]);
resource = null;
}
}
resource.aSymbols = data['symbols'];
} catch (err) {
this.error("resource (%s) exception: %s", sURL, err.message);
resource = null;
}
}
else {
/*
* Parse the data manually; we assume it's a series of hex byte-values separated by whitespace.
*/
let ab = [];
let sHexData = sData.replace(/\n/gm, " ").replace(/ +$/, "");
let asHexData = sHexData.split(" ");
for (i = 0; i < asHexData.length; i++) {
let n = parseInt(asHexData[i], 16);
if (isNaN(n)) {
this.error("resource (%s) contains invalid hex byte (%s)", sURL, asHexData[i]);
break;
}
ab.push(n & 0xff);
}
if (i == asHexData.length) resource.aBytes = ab;
}
return resource;
}
/**
* parseSwitches(sws, switchesDefault)
*
* Parses DIP switch string definitions into numbers.
*
* @this {NumIO}
* @param {string} sws (eg, "00000000", where sws[0] is SW0, sws[1] is SW1, etc.)
* @param {number} [switchesDefault] (use -1 to parse sws as a mask: 0 for any non-digit character)
* @returns {number|undefined}
*/
parseSwitches(sws, switchesDefault)
{
let switches;
if (!sws) {
switches = switchesDefault;
} else {
/*
* NOTE: It's not convenient to use parseInt() with a base of 2, in part because both bit order
* and bit sense are reversed, but also because we use this function to parse switch masks, which
* contain non-digits. See the "switches" defined in invaders.json for examples.
*/
switches = 0;
let bit = 0x1;
for (let i = 0; i < sws.length; i++) {
let ch = sws.charAt(i);
if (switchesDefault == -1) {
switches |= (ch != '0' && ch != '1'? 0 : bit);
}
else {
switches |= (ch == '0'? bit : 0);
}
bit <<= 1;
}
}
return switches;
}
/**
* toBase(n, base, bits, prefix, nGrouping)
*
* Converts the given number (as an unsigned integer) to a string using the specified base (radix).
*
* sprintf() may be a better choice, depending on your needs (eg, signed integers, formatting options, etc.)
* and support for the desired radix (eg, 8, 10, and 16).
*
* @this {NumIO}
* @param {number|*} n
* @param {number} [base] (ie, the radix; 0 or undefined for default)
* @param {number} [bits] (the number of bits in the value, 0 for variable)
* @param {string} [prefix] (prefix is based on radix; use "" for none)
* @param {number} [nGrouping]
* @returns {string}
*/
toBase(n, base, bits = 0, prefix = undefined, nGrouping = 0)
{
/*
* We can't rely entirely on isNaN(), because isNaN(null) returns false, and we can't rely
* entirely on typeof either, because typeof NaN returns "number". Sigh.
*
* Alternatively, we could mask and shift n regardless of whether it's null/undefined/NaN,
* since JavaScript coerces such operands to zero, but I think there's "value" in seeing those
* values displayed differently.
*/
let s = "", suffix = "", cch = -1;
if (!base) base = this.nDefaultRadix || 10;
if (bits) cch = Math.ceil(bits / Math.log2(base));
if (prefix == undefined) {
switch(base) {
case 8:
prefix = "0o";
break;
case 16:
prefix = "0x";
break;
case 10:
suffix = ".";
/* falls through */
default:
prefix = "";
break;
}
}
if (isNaN(n) || typeof n != "number") {
n = undefined;
prefix = suffix = "";
} else {
/*
* Callers that produced an input by dividing by a power of two rather than shifting (in order
* to access more than 32 bits) may produce a fractional result, which ordinarily we would simply
* ignore, but if the integer portion is zero and the sign is negative, we should probably treat
* this value as a sign-extension.
*/
if (n < 0 && n > -1) n = -1;
/*
* Negative values should be twos-complemented to produce a positive value for conversion purposes,
* but we can only do that if/when we're given the number of bits; Math.pow(base, cch) is equivalent
* to Math.pow(2, bits), but less precise for bases that aren't a power of two (eg, base 10).
*/
if (bits) {
if (n < 0) {
n += Math.pow(2, bits);
}
if (n >= Math.pow(2, bits)) {
cch = Math.ceil(Math.log(n) / Math.log(base));
}
}
}
let g = nGrouping || -1;
while (cch--) {
if (!g) {
s = ',' + s;
g = nGrouping;
}
if (n == undefined) {
s = '?' + s;
if (cch < 0) break;
} else {
let d = n % base;
n = Math.trunc(n / base);
d += (d >= 0 && d <= 9? 0x30 : 0x41 - 10);
s = String.fromCharCode(d) + s;
if (!n && cch < 0) break;
}
g--;
}
return prefix + s + suffix;
}
/**
* clearBits(num, bits)
*
* Function for clearing bits in numbers with more than 32 bits.
*
* @this {NumIO}
* @param {number} num
* @param {number} bits
* @returns {number} (num & ~bits)
*/
clearBits(num, bits)
{
let shift = NumIO.TWO_POW32;
let numHi = (num / shift)|0;
let bitsHi = (bits / shift)|0;
return (num & ~bits) + (numHi & ~bitsHi) * shift;
}
/**
* setBits(num, bits)
*
* Function for setting bits in numbers with more than 32 bits.
*
* @this {NumIO}
* @param {number} num
* @param {number} bits
* @returns {number} (num | bits)
*/
setBits(num, bits)
{
let shift = NumIO.TWO_POW32;
let numHi = (num / shift)|0;
let bitsHi = (bits / shift)|0;
return (num | bits) + (numHi | bitsHi) * shift;
}
/**
* testBits(num, bits)
*
* Function for testing bits in numbers with more than 32 bits.
*
* @this {NumIO}
* @param {number} num
* @param {number} bits
* @returns {boolean} (true IFF num & bits == bits)
*/
testBits(num, bits)
{
let shift = NumIO.TWO_POW32;
let numHi = (num / shift)|0;
let bitsHi = (bits / shift)|0;
return ((num & bits) == (bits|0) && (numHi & bitsHi) == bitsHi);
}
/**
* compress(aSrc)
*
* Compresses an array of numbers.
*
* @this {NumIO}
* @param {Array|Uint8Array} aSrc
* @returns {Array|Uint8Array} is either the original array (aSrc), or a smaller array of "count, value" pairs (aComp)
*/
compress(aSrc)
{
let iSrc = 0;
let iComp = 0;
let aComp = [];
while (iSrc < aSrc.length) {
let n = aSrc[iSrc];
this.assert(n !== undefined);
let iCompare = iSrc + 1;
while (iCompare < aSrc.length && aSrc[iCompare] === n) iCompare++;
aComp[iComp++] = iCompare - iSrc;
aComp[iComp++] = n;
iSrc = iCompare;
}
if (aComp.length >= aSrc.length) return aSrc;
return aComp;
}
/**
* decompress(aComp, length)
*
* Decompresses an array of numbers.
*
* @this {NumIO}
* @param {Array} aComp
* @param {number} [length] (expected length of decompressed data)
* @returns {Array}
*/
decompress(aComp, length = 0)
{
if (aComp.length == length) return aComp;
let iDst = 0;
let aDst = length? new Array(length) : [];
let iComp = 0;
while (iComp < aComp.length - 1) {
let c = aComp[iComp++];
let n = aComp[iComp++];
while (c--) aDst[iDst++] = n;
}
this.assert(!length || aDst.length == length);
return aDst;
}
}
/*
* Assorted constants
*/
NumIO.TWO_POW32 = Math.pow(2, 32);
Defs.CLASSES["NumIO"] = NumIO;