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 pathdebugger.js
2757 lines (2606 loc) · 95.7 KB
/
debugger.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileoverview Debugger Services
* @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";
/**
* DebuggerConfig properties
*
* @typedef {Config} DebuggerConfig
* @property {number} [defaultRadix]
*/
/**
* Defines a general-purpose Address structure that will hopefully meet the needs of all our
* machines. "off" is an (up to) 32-bit offset that is assumed to be PHYSICAL unless type is
* VIRTUAL. Normally, "seg" will be -1 (indicating it is unused), unless memory is segmented,
* in which case "seg" must be set to a non-negative identifying the segment, and "off" will be
* interpreted as an offset within that segment. For machines that have different types of
* segments (eg, real-mode vs. protected-mode segments), the address is assumed to be REAL
* unless type is PROTECTED.
*
* @typedef {Object} Address
* @property {number} off
* @property {number} seg
* @property {number} type
* @property {boolean} [disabled] (used with addresses that are used as breakpoints)
*/
/**
* Defines a Symbol object, added to our symbol table with addSymbol().
*
* @typedef {Object} SymbolObj
* @property {Address} address
* @property {number} type (see Debugger.SYMBOL_TYPE values)
* @property {string} name
*/
/**
* Defines a Dump extension, added to our list of extensions with addDumper().
*
* @typedef {Object} Dumper
* @property {Device} device
* @property {string} name
* @property {string} desc
* @property {function()} func
*/
/**
* Debugger Services
*
* @class {Debugger}
* @unrestricted
* @property {Array.<Array.<Address>>} aaBreakAddress
*/
class Debugger extends Device {
/**
* Debugger(idMachine, idDevice, config)
*
* @this {Debugger}
* @param {string} idMachine
* @param {string} idDevice
* @param {DebuggerConfig} [config]
*/
constructor(idMachine, idDevice, config)
{
config['class'] = "Debugger";
super(idMachine, idDevice, config);
/*
* Default radix (base). This is used by our own functions (eg, parseExpression()),
* but not by those we inherited (eg, parseInt()), which still use base 10 by default;
* however, you can always coerce values to any base in any of those functions with
* a prefix (eg, "0x" for hex) or suffix (eg, "." for decimal).
*/
this.nDefaultRadix = this.config['defaultRadix'] || 16;
/*
* Default endian (0 = little, 1 = big).
*/
this.nDefaultEndian = 0; // TODO: Use it or lose it
/*
* Default maximum instruction (opcode) length, overridden by the CPU-specific debugger.
*/
this.maxOpcodeLength = 1;
/*
* Default parsing parameters, subexpression and address delimiters.
*/
this.nASCIIBits = 8; // change to 7 for MACRO-10 compatibility
this.achGroup = ['(',')'];
this.achAddress = ['[',']'];
/*
* Add a new format type ('a') that understands Address objects, where width represents
* the size of the address in bits, and uses the Debugger's default radix.
*
* TODO: Consider adding a 'bits' property to the Address object (or a Bus property so that
* the appropriate addrWidth can be identified), in order to avoid the extra sprintf() width
* parameter, allowing the use of "%a" instead of "%*a".
*
* TODO: Determine if it's worth getting rid of the separate dumpAddress() function.
*/
this.addFormatType('a',
/**
* @param {string} type
* @param {string} flags
* @param {number} width
* @param {number} precision
* @param {Address} address
* @returns {string}
*/
(type, flags, width, precision, address) => this.toBase(address.off, this.nDefaultRadix, width)
);
/*
* Add a new format type ('n') for numbers, where width represents the size of the value in bits,
* and uses the Debugger's default radix.
*/
this.addFormatType('n',
/**
* @param {string} type
* @param {string} flags
* @param {number} width
* @param {number} precision
* @param {number} value
* @returns {string}
*/
(type, flags, width, precision, value) => this.toBase(value, this.nDefaultRadix, width, flags.indexOf('#') < 0? "" : undefined)
);
/*
* This controls how we stop the CPU on a break condition. If fExceptionOnBreak is true, we'll
* throw an exception, which the CPU will catch and halt; however, the downside of that approach
* is that, in some cases, it may leave the CPU in an inconsistent state. It's generally safer to
* leave fExceptionOnBreak false, which will simply stop the clock, allowing the current instruction
* to finish executing.
*/
this.fExceptionOnBreak = false;
/*
* If greater than zero, decremented on every instruction until it hits zero, then CPU is stoppped.
*/
this.counterBreak = 0;
/*
* If set to MESSAGE.ALL, then we break on all messages. It can be set to a subset of message bits,
* but there is currently no UI for that.
*/
this.messagesBreak = MESSAGE.NONE;
/*
* variables is an object with properties that grow as setVariable() assigns more variables;
* each property corresponds to one variable, where the property name is the variable name (ie,
* a string beginning with a non-digit, followed by zero or more symbol characters and/or digits)
* and the property value is the variable's numeric value.
*
* Note that parseValue() parses variables before numbers, so any variable that looks like a
* unprefixed hex value (eg, "a5" as opposed to "0xa5") will trump the numeric value. Unprefixed
* hex values are a convenience of parseValue(), which always calls parseInt() with a default
* base of 16; however, that default be overridden with a variety of explicit prefixes or suffixes
* (eg, a leading "0o" to indicate octal, a trailing period to indicate decimal, etc.)
*
* See parseInt() for more details about supported numbers.
*/
this.variables = {};
/*
* Arrays of Symbol objects, one sorted by name and the other sorted by value; see addSymbols().
*/
this.symbolsByName = [];
this.symbolsByValue = [];
/*
* Get access to the CPU, so that in part so we can connect to all its registers; the Debugger has
* no registers of its own, so we simply replace our registers with the CPU's.
*/
this.cpu = /** @type {CPU} */ (this.findDeviceByClass("CPU"));
this.registers = this.cpu.connectDebugger(this);
/*
* Get access to the Input device, so that we can switch focus whenever we start the machine.
*/
this.input = /** @type {Input} */ (this.findDeviceByClass("Input", false));
/*
* Get access to the Bus devices, so we have access to the I/O and memory address spaces.
* To minimize configuration redundancy, we rely on the CPU's configuration to get the Bus device IDs.
*/
let idBus = this.cpu.config['busMemory'] || this.config['busMemory'];
if (idBus) {
this.busMemory = /** @type {Bus} */ (this.findDevice(idBus));
idBus = this.cpu.config['busIO'] || this.config['busIO'];
if (idBus) {
this.busIO = /** @type {Bus} */ (this.findDevice(idBus, false));
}
if (!this.busIO) this.busIO = this.busMemory;
} else {
this.busMemory = this.busIO = /** @type {Bus} */ (this.findDeviceByClass('Bus'));
}
this.nDefaultBits = this.busMemory.addrWidth;
this.addrMask = (Math.pow(2, this.nDefaultBits) - 1)|0;
/*
* Since we want to be able to clear/disable/enable/list break addresses by index number, we maintain
* an array (aBreakIndexes) that maps index numbers to address array entries. The mapping values are
* a combination of BREAKTYPE (high byte) and break address entry (low byte).
*/
this.cBreaks = 0;
this.cBreakIgnore = 0; // incremented and decremented around internal reads and writes
this.aaBreakAddress = [];
for (let type in Debugger.BREAKTYPE) {
this.aaBreakAddress[Debugger.BREAKTYPE[type]] = [];
}
this.aBreakBuses = [];
this.aBreakBuses[Debugger.BREAKTYPE.READ] = this.busMemory;
this.aBreakBuses[Debugger.BREAKTYPE.WRITE] = this.busMemory;
this.aBreakBuses[Debugger.BREAKTYPE.INPUT] = this.busIO;
this.aBreakBuses[Debugger.BREAKTYPE.OUTPUT] = this.busIO;
this.aBreakChecks = [];
this.aBreakChecks[Debugger.BREAKTYPE.READ] = this.checkRead.bind(this);
this.aBreakChecks[Debugger.BREAKTYPE.WRITE] = this.checkWrite.bind(this)
this.aBreakChecks[Debugger.BREAKTYPE.INPUT] = this.checkInput.bind(this)
this.aBreakChecks[Debugger.BREAKTYPE.OUTPUT] = this.checkOutput.bind(this)
this.aBreakIndexes = [];
this.fStepQuietly = undefined; // when stepping, this informs onUpdate() how "quiet" to be
this.tempBreak = null; // temporary auto-cleared break address managed by setTemp() and clearTemp()
this.cInstructions = 0; // instruction counter (updated only if history is enabled)
/*
* Get access to the Time device, so we can stop and start time as needed.
*/
this.time = /** @type {Time} */ (this.findDeviceByClass("Time"));
this.time.addUpdate(this);
/*
* Initialize additional properties required for our onCommand() handler, including
* support for dump extensions (which we use ourselves to implement the "d state" command).
*/
this.aDumpers = []; // array of dump extensions (aka "Dumpers")
this.sDumpPrev = ""; // remembers the previous "dump" command invoked
this.addDumper(this, "state", "dump machine state", this.dumpState);
this.addressCode = this.newAddress();
this.addressData = this.newAddress();
this.historyForced = false;
this.historyNext = 0;
this.historyBuffer = [];
this.addHandler(Device.HANDLER.COMMAND, this.onCommand.bind(this));
let commands = /** @type {string} */ (this.getMachineConfig("commands"));
if (commands) this.parseCommands(commands);
}
/**
* addDumper(device, name, desc, func)
*
* @this {Debugger}
* @param {Device} device
* @param {string} name
* @param {string} desc
* @param {function(Array.<number>)} func
*/
addDumper(device, name, desc, func)
{
this.aDumpers.push({device, name, desc, func});
}
/**
* checkDumper(option, values)
*
* @this {Debugger}
* @param {string} option
* @param {Array.<number>} values
* @returns {string|undefined}
*/
checkDumper(option, values)
{
let result;
for (let i = 0; i < this.aDumpers.length; i++) {
let dumper = this.aDumpers[i];
if (dumper.name == option) {
result = dumper.func.call(dumper.device, values);
break;
}
}
return result;
}
/**
* addSymbol(address, type, name)
*
* @this {Debugger}
* @param {Address} address
* @param {number} type (see Debugger.SYMBOL_TYPE values)
* @param {string} name
*/
addSymbol(address, type, name)
{
let symbol = {address, type, name};
this.binaryInsert(this.symbolsByName, symbol, this.compareSymbolNames);
this.binaryInsert(this.symbolsByValue, symbol, this.compareSymbolValues);
}
/**
* addSymbols(aSymbols)
*
* This currently supports only symbol arrays, which consist of [address,type,name] triplets; eg:
*
* "0320","=","HF_PORT",
* "0000:0034","4","HDISK_INT",
* "0040:0042","1","CMD_BLOCK",
* "0003","@","DISK_SETUP",
* "0000:004C","4","ORG_VECTOR",
* "0028",";","GET DISKETTE VECTOR"
*
* There are two basic symbol operations: findSymbolByValue(), which takes an address and finds the symbol,
* if any, at that address, and findSymbolByName(), which takes a string and attempts to match it to an address.
*
* @this {Debugger}
* @param {Array|undefined} aSymbols
*/
addSymbols(aSymbols)
{
if (aSymbols && aSymbols.length) {
for (let iSymbol = 0; iSymbol < aSymbols.length-2; iSymbol += 3) {
let address = this.parseAddress(aSymbols[iSymbol]);
if (!address) continue; // ignore symbols with bad addresses
let type = Debugger.SYMBOL_TYPES[aSymbols[iSymbol+1]];
this.assert(type, "unrecognized symbol type: %s", aSymbols[iSymbol+1]);
if (!type) continue; // ignore symbols with unrecognized types
let name = aSymbols[iSymbol+2];
this.addSymbol(address, type, name);
}
}
}
/**
* binaryInsert(a, v, fnCompare)
*
* If element v already exists in array a, the array is unchanged (we don't allow duplicates); otherwise, the
* element is inserted into the array at the appropriate index.
*
* @this {Debugger}
* @param {Array} a is an array
* @param {Object} v is the value to insert
* @param {function(SymbolObj,SymbolObj):number} [fnCompare]
*/
binaryInsert(a, v, fnCompare)
{
let index = this.binarySearch(a, v, fnCompare);
if (index < 0) {
a.splice(-(index + 1), 0, v);
}
}
/**
* binarySearch(a, v, fnCompare)
*
* @this {Debugger}
* @param {Array} a is an array
* @param {Object} v
* @param {function(SymbolObj,SymbolObj):number} [fnCompare]
* @returns {number} the index of matching entry if non-negative, otherwise the index of the insertion point
*/
binarySearch(a, v, fnCompare)
{
let left = 0;
let right = a.length;
let found = 0;
if (fnCompare === undefined) {
fnCompare = function(a, b) { return a > b? 1 : a < b? -1 : 0; };
}
while (left < right) {
let middle = (left + right) >> 1;
let compareResult;
compareResult = fnCompare(v, a[middle]);
if (compareResult > 0) {
left = middle + 1;
} else {
right = middle;
found = !compareResult;
}
}
return found? left : ~left;
}
/**
* compareSymbolNames(symbol1, symbol2)
*
* @this {Debugger}
* @param {SymbolObj} symbol1
* @param {SymbolObj} symbol2
* @returns {number}
*/
compareSymbolNames(symbol1, symbol2)
{
return symbol1.name > symbol2.name? 1 : symbol1.name < symbol2.name? -1 : 0;
}
/**
* compareSymbolValues(symbol1, symbol2)
*
* @this {Debugger}
* @param {SymbolObj} symbol1
* @param {SymbolObj} symbol2
* @returns {number}
*/
compareSymbolValues(symbol1, symbol2)
{
return symbol1.address.off > symbol2.address.off? 1 : symbol1.address.off < symbol2.address.off? -1 : 0;
}
/**
* findSymbolByName(name)
*
* Search symbolsByName for name and return the corresponding symbol (undefined if not found).
*
* @this {Debugger}
* @param {string} name
* @returns {number} the index of matching entry if non-negative, otherwise the index of the insertion point
*/
findSymbolByName(name)
{
let symbol = {address: null, type: 0, name};
return this.binarySearch(this.symbolsByName, symbol, this.compareSymbolNames);
}
/**
* findSymbolByValue(address)
*
* Search symbolsByValue for address and return the corresponding symbol (undefined if not found).
*
* @this {Debugger}
* @param {Address} address
* @returns {number} the index of matching entry if non-negative, otherwise the index of the insertion point
*/
findSymbolByValue(address)
{
let symbol = {address, type: 0, name: undefined};
return this.binarySearch(this.symbolsByValue, symbol, this.compareSymbolValues);
}
/**
* getSymbol(name)
*
* @this {Debugger}
* @param {string} name
* @returns {number|undefined}
*/
getSymbol(name)
{
let value;
let i = this.findSymbolByName(name);
if (i >= 0) {
let symbol = this.symbolsByName[i];
value = symbol.address.off;
}
return value;
}
/**
* getSymbolName(address, type)
*
* @this {Debugger}
* @param {Address} address
* @param {number} [type]
* @returns {string|undefined}
*/
getSymbolName(address, type)
{
let name;
let i = this.findSymbolByValue(address);
if (i >= 0) {
let symbol = this.symbolsByValue[i];
if (!type || symbol.type == type) {
name = symbol.name;
}
}
return name;
}
/**
* delVariable(name)
*
* @this {Debugger}
* @param {string} name
*/
delVariable(name)
{
delete this.variables[name];
}
/**
* getVariable(name)
*
* @this {Debugger}
* @param {string} name
* @returns {number|undefined}
*/
getVariable(name)
{
if (this.variables[name]) {
return this.variables[name].value;
}
name = name.substr(0, 6);
return this.variables[name] && this.variables[name].value;
}
/**
* getVariableFixup(name)
*
* @this {Debugger}
* @param {string} name
* @returns {string|undefined}
*/
getVariableFixup(name)
{
return this.variables[name] && this.variables[name].sUndefined;
}
/**
* isVariable(name)
*
* @this {Debugger}
* @param {string} name
* @returns {boolean}
*/
isVariable(name)
{
return this.variables[name] !== undefined;
}
/**
* resetVariables()
*
* @this {Debugger}
* @returns {Object}
*/
resetVariables()
{
let a = this.variables;
this.variables = {};
return a;
}
/**
* restoreVariables(a)
*
* @this {Debugger}
* @param {Object} a (from previous resetVariables() call)
*/
restoreVariables(a)
{
this.variables = a;
}
/**
* setVariable(name, value, sUndefined)
*
* @this {Debugger}
* @param {string} name
* @param {number} value
* @param {string|undefined} [sUndefined]
*/
setVariable(name, value, sUndefined)
{
this.variables[name] = {value, sUndefined};
}
/**
* addAddress(address, offset, bus)
*
* All this function currently supports are physical (Bus) addresses, but that will change.
*
* @this {Debugger}
* @param {Address} address
* @param {number} offset
* @param {Bus} [bus] (default is busMemory)
* @returns {Address}
*/
addAddress(address, offset, bus = this.busMemory)
{
address.off = (address.off + offset) & bus.addrLimit;
return address;
}
/**
* makeAddress(address)
*
* All this function currently supports are physical (Bus) addresses, but that will change.
*
* @this {Debugger}
* @param {Address|number} address
* @returns {Address}
*/
makeAddress(address)
{
return typeof address == "number"? this.newAddress(address) : address;
}
/**
* newAddress(address)
*
* All this function currently supports are physical (Bus) addresses, but that will change.
*
* @this {Debugger}
* @param {Address|number} [address]
* @returns {Address}
*/
newAddress(address = 0)
{
let seg = -1, type = Debugger.ADDRESS.PHYSICAL;
if (typeof address == "number") return {off: address, seg, type};
return {off: address.off, seg: address.seg, type: address.type};
}
/**
* parseAddress(sAddress, aUndefined)
*
* @this {Debugger}
* @param {string} sAddress
* @param {Array} [aUndefined]
* @returns {Address|undefined|null} (undefined if no address supplied, null if a parsing error occurred)
*/
parseAddress(sAddress, aUndefined)
{
let address;
if (sAddress) {
address = this.newAddress();
let iAddr = 0;
let ch = sAddress.charAt(iAddr);
switch(ch) {
case '&':
iAddr++;
break;
case '#':
iAddr++;
address.type = Debugger.ADDRESS.PROTECTED;
break;
case '%':
iAddr++;
ch = sAddress.charAt(iAddr);
if (ch == '%') {
iAddr++;
} else {
address.type = Debugger.ADDRESS.VIRTUAL;
}
break;
}
let iColon = sAddress.indexOf(':', iAddr);
if (iColon >= 0) {
let seg = this.parseExpression(sAddress.substring(iAddr, iColon), aUndefined);
if (seg == undefined) {
address = null;
} else {
address.seg = seg;
iAddr = iColon + 1;
}
}
if (address) {
let off = this.parseExpression(sAddress.substring(iAddr), aUndefined);
if (off == undefined) {
address = null;
} else {
address.off = off & this.addrMask;
}
}
}
return address;
}
/**
* readAddress(address, advance, bus)
*
* All this function currently supports are physical (Bus) addresses, but that will change.
*
* @this {Debugger}
* @param {Address} address
* @param {number} [advance] (amount to advance address after read, if any)
* @param {Bus} [bus] (default is busMemory)
* @returns {number|undefined}
*/
readAddress(address, advance, bus = this.busMemory)
{
this.cBreakIgnore++;
let value = bus.readDirect(address.off);
if (advance) this.addAddress(address, advance, bus);
this.cBreakIgnore--;
return value;
}
/**
* writeAddress(address, value, bus)
*
* All this function currently supports are physical (Bus) addresses, but that will change.
*
* @this {Debugger}
* @param {Address} address
* @param {number} value
* @param {Bus} [bus] (default is busMemory)
*/
writeAddress(address, value, bus = this.busMemory)
{
this.cBreakIgnore++;
bus.writeDirect(address.off, value);
this.cBreakIgnore--;
}
/**
* setAddress(address, addr)
*
* All this function currently supports are physical (Bus) addresses, but that will change.
*
* @this {Debugger}
* @param {Address} address
* @param {number} addr
*/
setAddress(address, addr)
{
address.off = addr;
}
/**
* evalAND(dst, src)
*
* Adapted from /modules/pdp10/lib/cpuops.js:PDP10.AND().
*
* Performs the bitwise "and" (AND) of two operands > 32 bits.
*
* @this {Debugger}
* @param {number} dst
* @param {number} src
* @returns {number} (dst & src)
*/
evalAND(dst, src)
{
/*
* We AND the low 32 bits separately from the higher bits, and then combine them with addition.
* Since all bits above 32 will be zero, and since 0 AND 0 is 0, no special masking for the higher
* bits is required.
*
* WARNING: When using JavaScript's 32-bit operators with values that could set bit 31 and produce a
* negative value, it's critical to perform a final right-shift of 0, ensuring that the final result is
* positive.
*/
if (this.nDefaultBits <= 32) {
return dst & src;
}
/*
* Negative values don't yield correct results when dividing, so pass them through an unsigned truncate().
*/
dst = this.truncate(dst, 0, true);
src = this.truncate(src, 0, true);
return ((((dst / NumIO.TWO_POW32)|0) & ((src / NumIO.TWO_POW32)|0)) * NumIO.TWO_POW32) + ((dst & src) >>> 0);
}
/**
* evalMUL(dst, src)
*
* I could have adapted the code from /modules/pdp10/lib/cpuops.js:PDP10.doMUL(), but it was simpler to
* write this base method and let the PDP-10 Debugger override it with a call to the *actual* doMUL() method.
*
* @this {Debugger}
* @param {number} dst
* @param {number} src
* @returns {number} (dst * src)
*/
evalMUL(dst, src)
{
return dst * src;
}
/**
* evalIOR(dst, src)
*
* Adapted from /modules/pdp10/lib/cpuops.js:PDP10.IOR().
*
* Performs the logical "inclusive-or" (OR) of two operands > 32 bits.
*
* @this {Debugger}
* @param {number} dst
* @param {number} src
* @returns {number} (dst | src)
*/
evalIOR(dst, src)
{
/*
* We OR the low 32 bits separately from the higher bits, and then combine them with addition.
* Since all bits above 32 will be zero, and since 0 OR 0 is 0, no special masking for the higher
* bits is required.
*
* WARNING: When using JavaScript's 32-bit operators with values that could set bit 31 and produce a
* negative value, it's critical to perform a final right-shift of 0, ensuring that the final result is
* positive.
*/
if (this.nDefaultBits <= 32) {
return dst | src;
}
/*
* Negative values don't yield correct results when dividing, so pass them through an unsigned truncate().
*/
dst = this.truncate(dst, 0, true);
src = this.truncate(src, 0, true);
return ((((dst / NumIO.TWO_POW32)|0) | ((src / NumIO.TWO_POW32)|0)) * NumIO.TWO_POW32) + ((dst | src) >>> 0);
}
/**
* evalXOR(dst, src)
*
* Adapted from /modules/pdp10/lib/cpuops.js:PDP10.XOR().
*
* Performs the logical "exclusive-or" (XOR) of two operands > 32 bits.
*
* @this {Debugger}
* @param {number} dst
* @param {number} src
* @returns {number} (dst ^ src)
*/
evalXOR(dst, src)
{
/*
* We XOR the low 32 bits separately from the higher bits, and then combine them with addition.
* Since all bits above 32 will be zero, and since 0 XOR 0 is 0, no special masking for the higher
* bits is required.
*
* WARNING: When using JavaScript's 32-bit operators with values that could set bit 31 and produce a
* negative value, it's critical to perform a final right-shift of 0, ensuring that the final result is
* positive.
*/
if (this.nDefaultBits <= 32) {
return dst ^ src;
}
/*
* Negative values don't yield correct results when dividing, so pass them through an unsigned truncate().
*/
dst = this.truncate(dst, 0, true);
src = this.truncate(src, 0, true);
return ((((dst / NumIO.TWO_POW32)|0) ^ ((src / NumIO.TWO_POW32)|0)) * NumIO.TWO_POW32) + ((dst ^ src) >>> 0);
}
/**
* evalOps(aVals, aOps, cOps)
*
* Some of our clients want a specific number of bits of integer precision. If that precision is
* greater than 32, some of the operations below will fail; for example, JavaScript bitwise operators
* always truncate the result to 32 bits, so beware when using shift operations. Similarly, it would
* be wrong to always "|0" the final result, which is why we rely on truncate() now.
*
* Note that JavaScript integer precision is limited to 52 bits. For example, in Node, if you set a
* variable to 0x80000001:
*
* foo=0x80000001|0
*
* then calculate foo*foo and display the result in binary using "(foo*foo).toString(2)":
*
* '11111111111111111111111111111100000000000000000000000000000000'
*
* which is slightly incorrect because it has overflowed JavaScript's floating-point precision.
*
* 0x80000001 in decimal is -2147483647, so the product is 4611686014132420609, which is 0x3FFFFFFF00000001.
*
* @this {Debugger}
* @param {Array.<number>} aVals
* @param {Array.<string>} aOps
* @param {number} [cOps] (default is -1 for all)
* @returns {boolean} true if successful, false if error
*/
evalOps(aVals, aOps, cOps = -1)
{
while (cOps-- && aOps.length) {
let chOp = aOps.pop();
if (aVals.length < 2) return false;
let valNew;
let val2 = aVals.pop();
let val1 = aVals.pop();
switch(chOp) {
case '*':
valNew = this.evalMUL(val1, val2);
break;
case '/':
if (!val2) return false;
valNew = Math.trunc(val1 / val2);
break;
case '^/':
if (!val2) return false;
valNew = val1 % val2;
break;
case '+':
valNew = val1 + val2;
break;
case '-':
valNew = val1 - val2;
break;
case '<<':
valNew = val1 << val2;
break;
case '>>':
valNew = val1 >> val2;
break;
case '>>>':
valNew = val1 >>> val2;
break;
case '<':
valNew = (val1 < val2? 1 : 0);
break;
case '<=':
valNew = (val1 <= val2? 1 : 0);
break;
case '>':
valNew = (val1 > val2? 1 : 0);
break;
case '>=':
valNew = (val1 >= val2? 1 : 0);
break;
case '==':
valNew = (val1 == val2? 1 : 0);
break;
case '!=':
valNew = (val1 != val2? 1 : 0);
break;
case '&':
valNew = this.evalAND(val1, val2);
break;
case '!': // alias for MACRO-10 to perform a bitwise inclusive-or (OR)
case '|':
valNew = this.evalIOR(val1, val2);
break;
case '^!': // since MACRO-10 uses '^' for base overrides, '^!' is used for bitwise exclusive-or (XOR)
valNew = this.evalXOR(val1, val2);
break;
case '&&':
valNew = (val1 && val2? 1 : 0);
break;
case '||':
valNew = (val1 || val2? 1 : 0);
break;
case ',,':
valNew = this.truncate(val1, 18, true) * Math.pow(2, 18) + this.truncate(val2, 18, true);
break;
case '_':
case '^_':
valNew = val1;
/*
* While we always try to avoid assuming any particular number of bits of precision, the 'B' shift
* operator (which we've converted to '^_') is unique to the MACRO-10 environment, which imposes the
* following restrictions on the shift count.
*/
if (chOp == '^_') val2 = 35 - (val2 & 0xff);
if (val2) {
/*
* Since binary shifting is a logical (not arithmetic) operation, and since shifting by division only
* works properly with positive numbers, we call truncate() to produce an unsigned value.
*/
valNew = this.truncate(valNew, 0, true);
if (val2 > 0) {
valNew *= Math.pow(2, val2);
} else {
valNew = Math.trunc(valNew / Math.pow(2, -val2));
}
}
break;
default:
return false;
}
aVals.push(this.truncate(valNew));
}
return true;
}
/**
* parseArray(asValues, iValue, iLimit, nBase, aUndefined)
*
* parseExpression() takes a complete expression and divides it into array elements, where even elements
* are values (which may be empty if two or more operators appear consecutively) and odd elements are operators.
*
* For example, if the original expression was "2*{3+{4/2}}", parseExpression() would call parseArray() with:
*
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
* - - - - - - - - - - -- -- -- -- --
* 2 * { 3 + { 4 / 2 } }
*
* This function takes care of recursively processing grouped expressions, by processing subsets of the array,
* as well as handling certain base overrides (eg, temporarily switching to base-10 for binary shift suffixes).
*
* @this {Debugger}
* @param {Array.<string>} asValues
* @param {number} iValue
* @param {number} iLimit
* @param {number} nBase
* @param {Array} [aUndefined]