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 92
/
debugger.js
4369 lines (4098 loc) · 169 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 Implements the PC8080 Debugger component.
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2020 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";
if (typeof module !== "undefined") {
var Str = require("../../shared/lib/strlib");
var Usr = require("../../shared/lib/usrlib");
var Web = require("../../shared/lib/weblib");
var Component = require("../../shared/lib/component");
var DbgLib = require("../../shared/lib/debugger");
var Keys = require("../../shared/lib/keys");
var State = require("../../shared/lib/state");
var PC8080 = require("./defines");
var CPUDef8080 = require("./cpudef");
var CPU8080 = require("./cpu");
var Memory8080 = require("./memory");
var Messages8080 = require("./messages");
}
/**
* Debugger8080 Address Object
*
* addr address
* fTemporary true if this is a temporary breakpoint address
* sCmd set for breakpoint addresses if there's an associated command string
* aCmds preprocessed commands (from sCmd)
*
* @typedef {{
* addr:(number|undefined),
* fTemporary:(boolean|undefined),
* sCmd:(string|undefined),
* aCmds:(Array.<string>|undefined)
* }}
*/
var DbgAddr8080;
/**
* 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 Debugger8080 extends DbgLib {
/**
* Debugger8080(parmsDbg)
*
* The Debugger8080 component supports the following optional (parmsDbg) properties:
*
* commands: string containing zero or more commands, separated by ';'
*
* messages: string containing zero or more message categories to enable;
* multiple categories must be separated by '|' or ';'. Parsed by messageInit().
*
* The Debugger8080 component is an optional component that implements a variety of user
* commands for controlling the CPU, dumping and editing memory, etc.
*
* @this {Debugger8080}
* @param {Object} parmsDbg
*/
constructor(parmsDbg)
{
super("Debugger", parmsDbg, -1);
if (DEBUGGER) {
this.style = Debugger8080.STYLE_8080;
/*
* Most commands that require an address call parseAddr(), which defaults to dbgAddrNextCode
* or dbgAddrNextData when no address has been given. doDump() and doUnassemble(), in turn,
* update dbgAddrNextData and dbgAddrNextCode, respectively, when they're done.
*
* For TEMPORARY breakpoint addresses, we set fTemporary to true, so that they can be automatically
* cleared when they're hit.
*/
this.dbgAddrNextCode = this.newAddr();
this.dbgAddrNextData = this.newAddr();
this.dbgAddrAssemble = this.newAddr();
/*
* aSymbolTable is an array of SymbolTable objects, one per ROM or other chunk of address space,
* where each object contains the following properties:
*
* sModule
* addr (physical address, if any; eg, symbols for a ROM)
* len
* aSymbols
* aOffsets
*
* See addSymbols() for more details, since that's how callers add sets of symbols to the table.
*/
this.aSymbolTable = [];
/*
* clearBreakpoints() initializes the breakpoints lists: aBreakExec is a list of addresses
* to halt on whenever attempting to execute an instruction at the corresponding address,
* and aBreakRead and aBreakWrite are lists of addresses to halt on whenever a read or write,
* respectively, occurs at the corresponding address.
*
* NOTE: Curiously, after upgrading the Google Closure Compiler from v20141215 to v20150609,
* the resulting compiled code would crash in clearBreakpoints(), because the (renamed) aBreakRead
* property was already defined. To eliminate whatever was confusing the Closure Compiler, I've
* explicitly initialized all the properties that clearBreakpoints() (re)initializes.
*/
this.aBreakExec = this.aBreakRead = this.aBreakWrite = [];
this.clearBreakpoints();
/*
* The new "bn" command allows you to specify a number of instructions to execute and then stop;
* "bn 0" disables any outstanding count.
*/
this.nBreakIns = 0;
/*
* Execution history is allocated by historyInit() whenever checksEnabled() conditions change.
* Execution history is updated whenever the CPU calls checkInstruction(), which will happen
* only when checksEnabled() returns true (eg, whenever one or more breakpoints have been set).
* This ensures that, by default, the CPU runs as fast as possible.
*/
this.historyInit();
/*
* Initialize Debugger8080 message support
*/
this.afnDumpers = {};
this.messageInit(parmsDbg['messages']);
this.sInitCommands = parmsDbg['commands'];
/*
* Make it easier to access Debugger commands from an external REPL, like the WebStorm "live" console
* window; eg:
*
* pc8080('r')
* pc8080('dw 0:0')
* pc8080('h')
* ...
*/
var dbg = this;
if (window) {
if (window[PC8080.APPCLASS] === undefined) {
window[PC8080.APPCLASS] = function(s) { return dbg.doCommands(s); };
}
} else {
if (global[PC8080.APPCLASS] === undefined) {
global[PC8080.APPCLASS] = function(s) { return dbg.doCommands(s); };
}
}
} // endif DEBUGGER
}
/**
* initBus(bus, cpu, dbg)
*
* @this {Debugger8080}
* @param {Computer8080} cmp
* @param {Bus8080} bus
* @param {CPUState8080} cpu
* @param {Debugger8080} dbg
*/
initBus(cmp, bus, cpu, dbg)
{
this.bus = bus;
this.cpu = cpu;
this.cmp = cmp;
/*
* Re-initialize Debugger message support if necessary
*/
var sMessages = cmp.getMachineParm('messages');
if (sMessages) this.messageInit(sMessages);
this.aaOpDescs = Debugger8080.aaOpDescs;
this.messageDump(Messages8080.BUS, function onDumpBus(asArgs) { dbg.dumpBus(asArgs); });
this.setReady();
}
/**
* setBinding(sHTMLType, sBinding, control, sValue)
*
* @this {Debugger8080}
* @param {string} 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, "debugInput")
* @param {HTMLElement} 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 dbg = this;
switch (sBinding) {
case "debugInput":
this.bindings[sBinding] = control;
this.controlDebug = /** @type {HTMLInputElement} */ (control);
/*
* For halted machines, this is fine, but for auto-start machines, it can be annoying.
*
* control.focus();
*/
control.onkeydown = function onKeyDownDebugInput(event) {
var sCmd;
if (event.keyCode == Keys.KEYCODE.CR) {
sCmd = dbg.controlDebug.value;
dbg.controlDebug.value = "";
dbg.doCommands(sCmd, true);
}
else if (event.keyCode == Keys.KEYCODE.ESC) {
dbg.controlDebug.value = sCmd = "";
}
else {
if (event.keyCode == Keys.KEYCODE.UP) {
sCmd = dbg.getPrevCommand();
}
else if (event.keyCode == Keys.KEYCODE.DOWN) {
sCmd = dbg.getNextCommand();
}
if (sCmd != null) {
var cch = sCmd.length;
dbg.controlDebug.value = sCmd;
dbg.controlDebug.setSelectionRange(cch, cch);
}
}
if (sCmd != null && event.preventDefault) event.preventDefault();
};
return true;
case "debugEnter":
this.bindings[sBinding] = control;
Web.onClickRepeat(
control,
500, 100,
function onClickDebugEnter(fRepeat) {
if (dbg.controlDebug) {
var sCmds = dbg.controlDebug.value;
dbg.controlDebug.value = "";
dbg.doCommands(sCmds, true);
return true;
}
if (DEBUG) dbg.log("no debugger input buffer");
return false;
}
);
return true;
case "step":
this.bindings[sBinding] = control;
Web.onClickRepeat(
control,
500, 100,
function onClickStep(fRepeat) {
var fCompleted = false;
if (!dbg.isBusy(true)) {
dbg.setBusy(true);
fCompleted = dbg.stepCPU(fRepeat? 1 : 0);
dbg.setBusy(false);
}
return fCompleted;
}
);
return true;
default:
break;
}
return false;
}
/**
* updateFocus()
*
* @this {Debugger8080}
*/
updateFocus()
{
if (this.controlDebug) this.controlDebug.focus();
}
/**
* getAddr(dbgAddr, fWrite, nb)
*
* @this {Debugger8080}
* @param {DbgAddr8080|null|undefined} dbgAddr
* @param {boolean} [fWrite]
* @param {number} [nb] number of bytes to check (1 or 2); default is 1
* @return {number} is the corresponding linear address, or CPUDef8080.ADDR_INVALID
*/
getAddr(dbgAddr, fWrite, nb)
{
var addr = dbgAddr && dbgAddr.addr;
if (addr == null) {
addr = CPUDef8080.ADDR_INVALID;
}
return addr;
}
/**
* getByte(dbgAddr, inc)
*
* We must route all our memory requests through the CPU now, in case paging is enabled.
*
* @this {Debugger8080}
* @param {DbgAddr8080} dbgAddr
* @param {number} [inc]
* @return {number}
*/
getByte(dbgAddr, inc)
{
var b = 0xff;
var addr = this.getAddr(dbgAddr, false, 1);
if (addr !== CPUDef8080.ADDR_INVALID) {
b = this.bus.getByteDirect(addr);
if (inc) this.incAddr(dbgAddr, inc);
}
return b;
}
/**
* getWord(dbgAddr, fAdvance)
*
* @this {Debugger8080}
* @param {DbgAddr8080} dbgAddr
* @param {boolean} [fAdvance]
* @return {number}
*/
getWord(dbgAddr, fAdvance)
{
return this.getShort(dbgAddr, fAdvance? 2 : 0);
}
/**
* getShort(dbgAddr, inc)
*
* @this {Debugger8080}
* @param {DbgAddr8080} dbgAddr
* @param {number} [inc]
* @return {number}
*/
getShort(dbgAddr, inc)
{
var w = 0xffff;
var addr = this.getAddr(dbgAddr, false, 2);
if (addr !== CPUDef8080.ADDR_INVALID) {
w = this.bus.getShortDirect(addr);
if (inc) this.incAddr(dbgAddr, inc);
}
return w;
}
/**
* setByte(dbgAddr, b, inc)
*
* @this {Debugger8080}
* @param {DbgAddr8080} dbgAddr
* @param {number} b
* @param {number} [inc]
*/
setByte(dbgAddr, b, inc)
{
var addr = this.getAddr(dbgAddr, true, 1);
if (addr !== CPUDef8080.ADDR_INVALID) {
this.bus.setByteDirect(addr, b);
if (inc) this.incAddr(dbgAddr, inc);
this.cpu.updateCPU(true); // we set fForce to true in case video memory was the target
}
}
/**
* setShort(dbgAddr, w, inc)
*
* @this {Debugger8080}
* @param {DbgAddr8080} dbgAddr
* @param {number} w
* @param {number} [inc]
*/
setShort(dbgAddr, w, inc)
{
var addr = this.getAddr(dbgAddr, true, 2);
if (addr !== CPUDef8080.ADDR_INVALID) {
this.bus.setShortDirect(addr, w);
if (inc) this.incAddr(dbgAddr, inc);
this.cpu.updateCPU(true); // we set fForce to true in case video memory was the target
}
}
/**
* newAddr(addr)
*
* Returns a NEW DbgAddr8080 object, initialized with specified values and/or defaults.
*
* @this {Debugger8080}
* @param {number} [addr]
* @return {DbgAddr8080}
*/
newAddr(addr)
{
return {addr: addr, fTemporary: false};
}
/**
* setAddr(dbgAddr, addr)
*
* Updates an EXISTING DbgAddr8080 object, initialized with specified values and/or defaults.
*
* @this {Debugger8080}
* @param {DbgAddr8080} dbgAddr
* @param {number} addr
* @return {DbgAddr8080}
*/
setAddr(dbgAddr, addr)
{
dbgAddr.addr = addr;
dbgAddr.fTemporary = false;
return dbgAddr;
}
/**
* packAddr(dbgAddr)
*
* Packs a DbgAddr8080 object into an Array suitable for saving in a machine state object.
*
* @this {Debugger8080}
* @param {DbgAddr8080} dbgAddr
* @return {Array}
*/
packAddr(dbgAddr)
{
return [dbgAddr.addr, dbgAddr.fTemporary];
}
/**
* unpackAddr(aAddr)
*
* Unpacks a DbgAddr8080 object from an Array created by packAddr() and restored from a saved machine state.
*
* @this {Debugger8080}
* @param {Array} aAddr
* @return {DbgAddr8080}
*/
unpackAddr(aAddr)
{
return {addr: aAddr[0], fTemporary: aAddr[1]};
}
/**
* parseAddr(sAddr, fCode, fNoChecks)
*
* Address evaluation and validation (eg, range checks) are no longer performed at this stage. That's
* done later, by getAddr(), which returns CPUDef8080.ADDR_INVALID for invalid segments, out-of-range offsets,
* etc. The Debugger's low-level get/set memory functions verify all getAddr() results, but even if an
* invalid address is passed through to the Bus memory interfaces, the address will simply be masked with
* Bus8080.nBusLimit; in the case of CPUDef8080.ADDR_INVALID, that will generally refer to the top of the physical
* address space.
*
* @this {Debugger8080}
* @param {string|undefined} sAddr
* @param {boolean} [fCode] (true if target is code, false if target is data)
* @param {boolean} [fNoChecks] (true when setting breakpoints that may not be valid now, but will be later)
* @return {DbgAddr8080|null|undefined}
*/
parseAddr(sAddr, fCode, fNoChecks)
{
var dbgAddr;
var dbgAddrNext = (fCode? this.dbgAddrNextCode : this.dbgAddrNextData);
var addr = dbgAddrNext.addr;
if (sAddr !== undefined) {
sAddr = this.parseReference(sAddr) || sAddr;
dbgAddr = this.findSymbolAddr(sAddr);
if (dbgAddr) return dbgAddr;
addr = this.parseExpression(sAddr);
}
if (addr != null) {
dbgAddr = this.newAddr(addr);
}
return dbgAddr;
}
/**
* parseAddrOptions(dbdAddr, sOptions)
*
* @this {Debugger8080}
* @param {DbgAddr8080} dbgAddr
* @param {string} [sOptions]
*/
parseAddrOptions(dbgAddr, sOptions)
{
if (sOptions) {
var a = sOptions.match(/(['"])(.*?)\1/);
if (a) {
dbgAddr.aCmds = this.parseCommand(dbgAddr.sCmd = a[2]);
}
}
}
/**
* incAddr(dbgAddr, inc)
*
* @this {Debugger8080}
* @param {DbgAddr8080} dbgAddr
* @param {number} [inc] contains value to increment dbgAddr by (default is 1)
*/
incAddr(dbgAddr, inc)
{
if (dbgAddr.addr != null) {
dbgAddr.addr += (inc || 1);
}
}
/**
* toHexOffset(off)
*
* @this {Debugger8080}
* @param {number|null|undefined} [off]
* @return {string} the hex representation of off
*/
toHexOffset(off)
{
return Str.toHex(off, 4);
}
/**
* toHexAddr(dbgAddr)
*
* @this {Debugger8080}
* @param {DbgAddr8080} dbgAddr
* @return {string} the hex representation of the address
*/
toHexAddr(dbgAddr)
{
return this.toHexOffset(dbgAddr.addr);
}
/**
* getSZ(dbgAddr, cchMax)
*
* Gets zero-terminated (aka "ASCIIZ") string from dbgAddr. It also stops at the first '$', in case this is
* a '$'-terminated string -- mainly because I'm lazy and didn't feel like writing a separate get() function.
* Yes, a zero-terminated string containing a '$' will be prematurely terminated, and no, I don't care.
*
* @this {Debugger8080}
* @param {DbgAddr8080} dbgAddr
* @param {number} [cchMax] (default is 256)
* @return {string} (and dbgAddr advanced past the terminating zero)
*/
getSZ(dbgAddr, cchMax)
{
var s = "";
cchMax = cchMax || 256;
while (s.length < cchMax) {
var b = this.getByte(dbgAddr, 1);
if (!b || b == 0x24 || b >= 127) break;
s += (b >= 32? String.fromCharCode(b) : '.');
}
return s;
}
/**
* dumpBlocks(aBlocks, sAddr)
*
* @this {Debugger8080}
* @param {Array} aBlocks
* @param {string} [sAddr] (optional block address)
*/
dumpBlocks(aBlocks, sAddr)
{
var addr = 0, i = 0, n = aBlocks.length;
if (sAddr) {
addr = this.getAddr(this.parseAddr(sAddr));
if (addr === CPUDef8080.ADDR_INVALID) {
this.println("invalid address: " + sAddr);
return;
}
i = addr >>> this.bus.nBlockShift;
n = 1;
}
this.println("blockid physical blockaddr used size type");
this.println("-------- --------- ---------- ------ ------ ----");
var typePrev = -1, cPrev = 0;
while (n--) {
var block = aBlocks[i];
if (block.type == typePrev) {
if (!cPrev++) this.println("...");
} else {
typePrev = block.type;
var sType = Memory8080.TYPE.NAMES[typePrev];
if (block) {
this.println(Str.toHex(block.id) + " %" + Str.toHex(i << this.bus.nBlockShift) + " %%" + Str.toHex(block.addr) + " " + Str.toHexWord(block.used) + " " + Str.toHexWord(block.size) + " " + sType);
}
if (typePrev != Memory8080.TYPE.NONE) typePrev = -1;
cPrev = 0;
}
addr += this.bus.nBlockSize;
i++;
}
}
/**
* dumpBus(asArgs)
*
* Dumps Bus allocations.
*
* @this {Debugger8080}
* @param {Array.<string>} asArgs (asArgs[0] is an optional block address)
*/
dumpBus(asArgs)
{
this.dumpBlocks(this.bus.aMemBlocks, asArgs[0]);
}
/**
* dumpHistory(sPrev, sLines)
*
* If sLines is not a number, it can be a instruction filter. However, for the moment, the only
* supported filter is "call", which filters the history buffer for all CALL and RET instructions
* from the specified previous point forward.
*
* @this {Debugger8080}
* @param {string} [sPrev] is a (decimal) number of instructions to rewind to (default is 10)
* @param {string} [sLines] is a (decimal) number of instructions to print (default is, again, 10)
*/
dumpHistory(sPrev, sLines)
{
var sMore = "";
var cHistory = 0;
var iHistory = this.iOpcodeHistory;
var aHistory = this.aOpcodeHistory;
if (aHistory.length) {
var nPrev = +sPrev || this.nextHistory;
var nLines = +sLines || 10;
if (isNaN(nPrev)) {
nPrev = nLines;
} else {
sMore = "more ";
}
if (nPrev > aHistory.length) {
this.println("note: only " + aHistory.length + " available");
nPrev = aHistory.length;
}
iHistory -= nPrev;
if (iHistory < 0) {
/*
* If the dbgAddr of the last aHistory element contains a valid selector, wrap around.
*/
if (aHistory[aHistory.length - 1].addr == null) {
nPrev = iHistory + nPrev;
iHistory = 0;
} else {
iHistory += aHistory.length;
}
}
var aFilters = [];
if (sLines == "call") {
nLines = 100000;
aFilters = ["CALL"];
}
if (sPrev !== undefined) {
this.println(nPrev + " instructions earlier:");
}
/*
* TODO: The following is necessary to prevent dumpHistory() from causing additional (or worse, recursive)
* faults due to segmented addresses that are no longer valid, but the only alternative is to dramatically
* increase the amount of memory used to store instruction history (eg, storing copies of all the instruction
* bytes alongside the execution addresses).
*
* For now, we're living dangerously, so that our history dumps actually work.
*
* this.nSuppressBreaks++;
*
* If you re-enable this protection, be sure to re-enable the decrement below, too.
*/
var sDump = "";
while (nLines > 0 && iHistory != this.iOpcodeHistory) {
var dbgAddr = aHistory[iHistory++];
if (dbgAddr.addr == null) break;
/*
* We must create a new dbgAddr from the address in aHistory, because dbgAddr was
* a reference, not a copy, and we don't want getInstruction() modifying the original.
*/
var dbgAddrNew = this.newAddr(dbgAddr.addr);
var sComment = "history";
var nSequence = nPrev--;
if (MAXDEBUG && dbgAddr.cycleCount != null) {
sComment = "cycles";
nSequence = dbgAddr.cycleCount;
}
var sInstruction = this.getInstruction(dbgAddrNew, sComment, nSequence);
if (!aFilters.length || sInstruction.indexOf(aFilters[0]) >= 0) {
sDump += sInstruction + "\n";
}
/*
* If there were OPERAND or ADDRESS overrides on the previous instruction, getInstruction()
* will have automatically disassembled additional bytes, so skip additional history entries.
*/
if (dbgAddrNew.cOverrides) {
iHistory += dbgAddrNew.cOverrides; nLines -= dbgAddrNew.cOverrides; nPrev -= dbgAddrNew.cOverrides;
}
if (iHistory >= aHistory.length) iHistory = 0;
this.nextHistory = nPrev;
cHistory++;
nLines--;
}
if (sDump) this.println(sDump);
/*
* See comments above.
*
* this.nSuppressBreaks--;
*/
}
if (!cHistory) {
this.println("no " + sMore + "history available");
this.nextHistory = undefined;
}
}
/**
* messageInit(sEnable)
*
* @this {Debugger8080}
* @param {string|undefined} sEnable contains zero or more message categories to enable, separated by '|'
*/
messageInit(sEnable)
{
this.dbg = this;
this.bitsMessage = this.bitsWarning = Messages8080.WARN;
this.sMessagePrev = null;
this.aMessageBuffer = [];
/*
* Internally, we use "key" instead of "keys", since the latter is a method on JavasScript objects,
* but externally, we allow the user to specify "keys"; "kbd" is also allowed as shorthand for "keyboard".
*/
var aEnable = this.parseCommand(sEnable.replace("keys","key").replace("kbd","keyboard"), false, '|');
if (aEnable.length) {
for (var m in Messages8080.CATEGORIES) {
if (Usr.indexOf(aEnable, m) >= 0) {
this.bitsMessage |= Messages8080.CATEGORIES[m];
this.println(m + " messages enabled");
}
}
}
}
/**
* messageDump(bitMessage, fnDumper)
*
* @this {Debugger8080}
* @param {number} bitMessage is one Messages category flag
* @param {function(Array.<string>)} fnDumper is a function the Debugger can use to dump data for that category
* @return {boolean} true if successfully registered, false if not
*/
messageDump(bitMessage, fnDumper)
{
for (var m in Messages8080.CATEGORIES) {
if (bitMessage == Messages8080.CATEGORIES[m]) {
this.afnDumpers[m] = fnDumper;
return true;
}
}
return false;
}
/**
* getRegIndex(sReg, off)
*
* @this {Debugger8080}
* @param {string} sReg
* @param {number} [off] optional offset into sReg
* @return {number} register index, or -1 if not found
*/
getRegIndex(sReg, off)
{
var i;
sReg = sReg.toUpperCase();
if (off == null) {
i = Usr.indexOf(Debugger8080.REGS, sReg);
} else {
i = Usr.indexOf(Debugger8080.REGS, sReg.substr(off, 2));
if (i < 0) i = Usr.indexOf(Debugger8080.REGS, sReg.substr(off, 1));
}
return i;
}
/**
* getRegString(iReg)
*
* @this {Debugger8080}
* @param {number} iReg
* @return {string}
*/
getRegString(iReg)
{
var cch = 0;
var n = this.getRegValue(iReg);
if (n !== undefined) {
switch(iReg) {
case Debugger8080.REG_A:
case Debugger8080.REG_B:
case Debugger8080.REG_C:
case Debugger8080.REG_D:
case Debugger8080.REG_E:
case Debugger8080.REG_H:
case Debugger8080.REG_L:
case Debugger8080.REG_M:
cch = 2;
break;
case Debugger8080.REG_BC:
case Debugger8080.REG_DE:
case Debugger8080.REG_HL:
case Debugger8080.REG_SP:
case Debugger8080.REG_PC:
case Debugger8080.REG_PS:
case Debugger8080.REG_PSW:
cch = 4;
break;
}
}
return cch? Str.toHex(n, cch) : "??";
}
/**
* getRegValue(iReg)
*
* @this {Debugger8080}
* @param {number} iReg
* @return {number|undefined}
*/
getRegValue(iReg)
{
var n;
if (iReg >= 0) {
var cpu = this.cpu;
switch(iReg) {
case Debugger8080.REG_A:
n = cpu.regA;
break;
case Debugger8080.REG_B:
n = cpu.regB;
break;
case Debugger8080.REG_C:
n = cpu.regC;
break;
case Debugger8080.REG_BC:
n = cpu.getBC();
break;
case Debugger8080.REG_D:
n = cpu.regD;
break;
case Debugger8080.REG_E:
n = cpu.regE;
break;
case Debugger8080.REG_DE:
n = cpu.getDE();
break;
case Debugger8080.REG_H:
n = cpu.regH;
break;
case Debugger8080.REG_L:
n = cpu.regL;
break;
case Debugger8080.REG_HL:
n = cpu.getHL();
break;
case Debugger8080.REG_M:
n = cpu.getByte(cpu.getHL());
break;
case Debugger8080.REG_SP:
n = cpu.getSP();
break;
case Debugger8080.REG_PC:
n = cpu.getPC();
break;
case Debugger8080.REG_PS:
n = cpu.getPS();
break;
case Debugger8080.REG_PSW:
n = cpu.getPSW();
break;
default:
break;
}
}
return n;
}
/**
* replaceRegs(s)
*
* @this {Debugger8080}
* @param {string} s
* @return {string}
*/
replaceRegs(s)
{
/*
* Replace any references first; this means that register references inside the reference
* do NOT need to be prefixed with '@'.
*/
s = this.parseReference(s) || s;
/*
* Replace every @XX (or @XXX), where XX (or XXX) is a register, with the register's value.
*/
var i = 0;
var b, sChar, sAddr, dbgAddr, sReplace;
while ((i = s.indexOf('@', i)) >= 0) {
var iReg = this.getRegIndex(s, i + 1);
if (iReg >= 0) {
s = s.substr(0, i) + this.getRegString(iReg) + s.substr(i + 1 + Debugger8080.REGS[iReg].length);
}
i++;
}
/*
* Replace every #XX, where XX is a hex byte value, with the corresponding ASCII character (if printable).
*/
i = 0;
while ((i = s.indexOf('#', i)) >= 0) {
sChar = s.substr(i+1, 2);
b = Str.parseInt(sChar, 16);
if (b != null && b >= 32 && b < 127) {
sReplace = sChar + " '" + String.fromCharCode(b) + "'";
s = s.replace('#' + sChar, sReplace);
i += sReplace.length;
continue;
}
i++;
}
/*
* Replace every $XXXX:XXXX, where XXXX:XXXX is a segmented address, with the zero-terminated string at that address.
*/
i = 0;
while ((i = s.indexOf('$', i)) >= 0) {
sAddr = s.substr(i+1, 9);
dbgAddr = this.parseAddr(sAddr);
if (dbgAddr) {
sReplace = sAddr + ' "' + this.getSZ(dbgAddr) + '"';
s = s.replace('$' + sAddr, sReplace);
i += sReplace.length;
continue;
}
i++;
}
/*
* Replace every ^XXXX:XXXX, where XXXX:XXXX is a segmented address, with the FCB filename stored at that address.
*/
i = 0;
while ((i = s.indexOf('^', i)) >= 0) {
sAddr = s.substr(i+1, 9);
dbgAddr = this.parseAddr(sAddr);
if (dbgAddr) {
this.incAddr(dbgAddr);
sReplace = sAddr + ' "' + this.getSZ(dbgAddr, 11) + '"';
s = s.replace('^' + sAddr, sReplace);
i += sReplace.length;