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
2222 lines (2131 loc) · 82.1 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 This file implements the C1Pjs Debugger component.
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2019 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");
}
/**
* @class C1PDebugger
* @unrestricted
*/
class C1PDebugger extends Component {
/**
* C1PDebugger(parmsDbg)
*
* The C1PDebugger component has no required (parmsDbg) properties.
*
* The C1PDebugger component is an optional component that implements a variety of user
* commands for controlling the CPU, dumping and editing memory, etc.
*
* @this {C1PDebugger}
* @param {Object} parmsDbg
*/
constructor(parmsDbg)
{
super("C1PDebugger", parmsDbg);
if (DEBUGGER) {
this.dbg = this;
/*
* This keeps track of instruction activity, but only when tracing or when
* Debugger checks have been enabled (eg, one or more breakpoints have been set).
*
* This is zeroed by CPU notification handlers reset() and stopped().
* We set it here to -1 to indicate that the CPU has not yet initialized us.
*/
this.cIns = -1;
/*
* Some commands, like the dump (d) command, start at nextAddr when no address
* is given (and they also update nextAddr when they're done).
*/
this.nextAddr = 0;
/*
* When Enter is pressed on an empty input buffer, we default to the previous
* command, which is preserved here.
*/
this.prevCmd = null;
/*
* fAssemble is true when "assemble mode" is active, false when not.
*/
this.fAssemble = false;
this.addrAssembleNext = 0;
/*
* Initialize the lists of breakpoint addresses. aExecBreak is a list (Array) of addresses
* to halt at whenever attempting to execute an instruction at the corresponding address,
* and aReadBreak and aWriteBreak are lists of addresses to halt at whenever a read or write,
* respectively, occurs at the corresponding address.
*/
this.clearBreakpoints();
/*
* Instead of pre-allocating these arrays, we wait until our reset() function is called.
* These arrays are updated in checkInstruction(), but the CPU will never actually call it
* unless checksEnabled() returns true, and that won't happen until one or more breakpoints
* have been set. This ensures that, by default, the CPU runs as fast as possible.
*/
this.iStepHistory = 0;
this.aStepHistory = [];
this.aaOpcodeFreqs = [];
/*
* This "info" buffer is a lightweight logging mechanism that has minimal impact on the
* browser (unlike printing to either window.console.log or an HTML control, which can make
* the browser unusable if printing is too frequent). The Debugger's "i" command dumps
* this buffer. Note that dumping too much at once can also bog things down, but by that
* point, you've presumably already captured the info you need and are willing to wait.
*/
if (DEBUG) {
this.iInfoBuffer = 0;
this.aInfoBuffer = new Array(10000);
}
/*
* Message categories supported by the message() function; they are designed to be combined
* (ie, OR'ed) as needed. The Debugger's "option" command is used to turn message categories
* on and off, like so:
*
* o msg port on
* o msg port off
* ...
*/
this.MESSAGE_PORT = 0x01;
this.MESSAGE_KBD = 0x10;
this.MESSAGE_VIDEO = 0x20;
this.MESSAGE_DISK = 0x40;
this.MESSAGE_SERIAL = 0x80;
this.MESSAGE_NONE = 0x00;
// this.MESSAGE_ALL = 0xff;
this.bitsMessage = this.MESSAGE_NONE;
this.aMessageCategories = {
'port': this.MESSAGE_PORT,
'kbd': this.MESSAGE_KBD,
'video': this.MESSAGE_VIDEO,
'disk': this.MESSAGE_DISK,
'serial': this.MESSAGE_SERIAL
};
/*
* The aaOperations array is indexed by opcode, and each element is a sub-array that
* describes the corresponding opcode. The sub-elements are as follows:
*
* [0]: {number} of the operation code (see OP_*)
* [1]: {number} of additional bytes following the opcode byte, if any
* [2]: {number} of the operation mode operand, if any (see MODE_*)
*
* These sub-elements are all optional. If [0] is not present, the opcode is undefined;
* if [1] is not present (or contains a zero), the opcode is a single-byte opcode; and if
* [2] is not present, the opcode uses no (or implied) operands.
*/
this.OP_ADC = 0;
this.OP_AND = 1;
this.OP_ASL = 2;
this.OP_BCC = 3;
this.OP_BCS = 4;
this.OP_BEQ = 5;
this.OP_BIT = 6;
this.OP_BMI = 7;
this.OP_BNE = 8;
this.OP_BPL = 9;
this.OP_BRK = 10;
this.OP_BVC = 11;
this.OP_BVS = 12;
this.OP_CLC = 13;
this.OP_CLD = 14;
this.OP_CLI = 15;
this.OP_CLV = 16;
this.OP_CMP = 17;
this.OP_CPX = 18;
this.OP_CPY = 19;
this.OP_DEC = 20;
this.OP_DEX = 21;
this.OP_DEY = 22;
this.OP_EOR = 23;
this.OP_INC = 24;
this.OP_INX = 25;
this.OP_INY = 26;
this.OP_JMP = 27;
this.OP_JSR = 28;
this.OP_LDA = 29;
this.OP_LDX = 30;
this.OP_LDY = 31;
this.OP_LSR = 32;
this.OP_NOP = 33;
this.OP_ORA = 34;
this.OP_PHA = 35;
this.OP_PHP = 36;
this.OP_PLA = 37;
this.OP_PLP = 38;
this.OP_ROL = 39;
this.OP_ROR = 40;
this.OP_RTI = 41;
this.OP_RTS = 42;
this.OP_SBC = 43;
this.OP_SEC = 44;
this.OP_SED = 45;
this.OP_SEI = 46;
this.OP_STA = 47;
this.OP_STX = 48;
this.OP_STY = 49;
this.OP_TAX = 50;
this.OP_TAY = 51;
this.OP_TSX = 52;
this.OP_TXA = 53;
this.OP_TXS = 54;
this.OP_TYA = 55;
this.OP_SIM = 56;
this.OP_DB = 57;
this.aOpCodes = [
"ADC","AND","ASL","BCC","BCS","BEQ","BIT","BMI",
"BNE","BPL","BRK","BVC","BVS","CLC","CLD","CLI",
"CLV","CMP","CPX","CPY","DEC","DEX","DEY","EOR",
"INC","INX","INY","JMP","JSR","LDA","LDX","LDY",
"LSR","NOP","ORA","PHA","PHP","PLA","PLP","ROL",
"ROR","RTI","RTS","SBC","SEC","SED","SEI","STA",
"STX","STY","TAX","TAY","TSX","TXA","TXS","TYA",
"SIM",".DB"
];
this.aOpSimCodes = [
"HLT", "MSG"
];
this.setOpModes(true);
this.aaOperations = [
/* 0x00 */ [this.OP_BRK],
/* 0x01 */ [this.OP_ORA, 1, this.MODE_INDX],
/* 0x02 */ [this.OP_SIM, 1],
/* 0x03 */ [],
/* 0x04 */ [],
/* 0x05 */ [this.OP_ORA, 1, this.MODE_ZP],
/* 0x06 */ [this.OP_ASL, 1, this.MODE_ZP],
/* 0x07 */ [],
/* 0x08 */ [this.OP_PHP],
/* 0x09 */ [this.OP_ORA, 1, this.MODE_IMM],
/* 0x0a */ [this.OP_ASL, 0, this.MODE_ACC],
/* 0x0b */ [],
/* 0x0c */ [],
/* 0x0d */ [this.OP_ORA, 2, this.MODE_ABS],
/* 0x0e */ [this.OP_ASL, 2, this.MODE_ABS],
/* 0x0f */ [],
/* 0x10 */ [this.OP_BPL, 1, this.MODE_DISP],
/* 0x11 */ [this.OP_ORA, 1, this.MODE_INDY],
/* 0x12 */ [],
/* 0x13 */ [],
/* 0x14 */ [],
/* 0x15 */ [this.OP_ORA, 1, this.MODE_ZPX],
/* 0x16 */ [this.OP_ASL, 1, this.MODE_ZPX],
/* 0x17 */ [],
/* 0x18 */ [this.OP_CLC],
/* 0x19 */ [this.OP_ORA, 2, this.MODE_ABSY],
/* 0x1a */ [],
/* 0x1b */ [],
/* 0x1c */ [],
/* 0x1d */ [this.OP_ORA, 2, this.MODE_ABSX],
/* 0x1e */ [this.OP_ASL, 2, this.MODE_ABSX],
/* 0x1f */ [],
/* 0x20 */ [this.OP_JSR, 2, this.MODE_IMM16],
/* 0x21 */ [this.OP_AND, 1, this.MODE_INDX],
/* 0x22 */ [],
/* 0x23 */ [],
/* 0x24 */ [this.OP_BIT, 1, this.MODE_ZP],
/* 0x25 */ [this.OP_AND, 1, this.MODE_ZP],
/* 0x26 */ [this.OP_ROL, 1, this.MODE_ZP],
/* 0x27 */ [],
/* 0x28 */ [this.OP_PLP],
/* 0x29 */ [this.OP_AND, 1, this.MODE_IMM],
/* 0x2a */ [this.OP_ROL, 0, this.MODE_ACC],
/* 0x2b */ [],
/* 0x2c */ [this.OP_BIT, 2, this.MODE_ABS],
/* 0x2d */ [this.OP_AND, 2, this.MODE_ABS],
/* 0x2e */ [this.OP_ROL, 2, this.MODE_ABS],
/* 0x2f */ [],
/* 0x30 */ [this.OP_BMI, 1, this.MODE_DISP],
/* 0x31 */ [this.OP_AND, 1, this.MODE_INDY],
/* 0x32 */ [],
/* 0x33 */ [],
/* 0x34 */ [],
/* 0x35 */ [this.OP_AND, 1, this.MODE_ZPX],
/* 0x36 */ [this.OP_ROL, 1, this.MODE_ZPX],
/* 0x37 */ [],
/* 0x38 */ [this.OP_SEC],
/* 0x39 */ [this.OP_AND, 2, this.MODE_ABSY],
/* 0x3a */ [],
/* 0x3b */ [],
/* 0x3c */ [],
/* 0x3d */ [this.OP_AND, 2, this.MODE_ABSX],
/* 0x3e */ [this.OP_ROL, 2, this.MODE_ABSX],
/* 0x3f */ [],
/* 0x40 */ [this.OP_RTI],
/* 0x41 */ [this.OP_EOR, 1, this.MODE_INDX],
/* 0x42 */ [],
/* 0x43 */ [],
/* 0x44 */ [],
/* 0x45 */ [this.OP_EOR, 1, this.MODE_ZP],
/* 0x46 */ [this.OP_LSR, 1, this.MODE_ZP],
/* 0x47 */ [],
/* 0x48 */ [this.OP_PHA],
/* 0x49 */ [this.OP_EOR, 1, this.MODE_IMM],
/* 0x4a */ [this.OP_LSR, 0, this.MODE_ACC],
/* 0x4b */ [],
/* 0x4c */ [this.OP_JMP, 2, this.MODE_IMM16],
/* 0x4d */ [this.OP_EOR, 2, this.MODE_ABS],
/* 0x4e */ [this.OP_LSR, 2, this.MODE_ABS],
/* 0x4f */ [],
/* 0x50 */ [this.OP_BVC, 1, this.MODE_DISP],
/* 0x51 */ [this.OP_EOR, 1, this.MODE_INDY],
/* 0x52 */ [],
/* 0x53 */ [],
/* 0x54 */ [],
/* 0x55 */ [this.OP_EOR, 1, this.MODE_ZPX],
/* 0x56 */ [this.OP_LSR, 1, this.MODE_ZPX],
/* 0x57 */ [],
/* 0x58 */ [this.OP_CLI],
/* 0x59 */ [this.OP_EOR, 2, this.MODE_ABSY],
/* 0x5a */ [],
/* 0x5b */ [],
/* 0x5c */ [],
/* 0x5d */ [this.OP_EOR, 2, this.MODE_ABSX],
/* 0x5e */ [this.OP_LSR, 2, this.MODE_ABSX],
/* 0x5f */ [],
/* 0x60 */ [this.OP_RTS],
/* 0x61 */ [this.OP_ADC, 1, this.MODE_INDX],
/* 0x62 */ [],
/* 0x63 */ [],
/* 0x64 */ [],
/* 0x65 */ [this.OP_ADC, 1, this.MODE_ZP],
/* 0x66 */ [this.OP_ROR, 1, this.MODE_ZP],
/* 0x67 */ [],
/* 0x68 */ [this.OP_PLA],
/* 0x69 */ [this.OP_ADC, 1, this.MODE_IMM],
/* 0x6a */ [this.OP_ROR, 0, this.MODE_ACC],
/* 0x6b */ [],
/* 0x6c */ [this.OP_JMP, 2, this.MODE_ABS16],
/* 0x6d */ [this.OP_ADC, 2, this.MODE_ABS],
/* 0x6e */ [this.OP_ROR, 2, this.MODE_ABS],
/* 0x6f */ [],
/* 0x70 */ [this.OP_BVS, 1, this.MODE_DISP],
/* 0x71 */ [this.OP_ADC, 1, this.MODE_INDY],
/* 0x72 */ [],
/* 0x73 */ [],
/* 0x74 */ [],
/* 0x75 */ [this.OP_ADC, 1, this.MODE_ZPX],
/* 0x76 */ [this.OP_ROR, 1, this.MODE_ZPX],
/* 0x77 */ [],
/* 0x78 */ [this.OP_SEI],
/* 0x79 */ [this.OP_ADC, 2, this.MODE_ABSY],
/* 0x7a */ [],
/* 0x7b */ [],
/* 0x7c */ [],
/* 0x7d */ [this.OP_ADC, 2, this.MODE_ABSX],
/* 0x7e */ [this.OP_ROR, 2, this.MODE_ABSX],
/* 0x7f */ [],
/* 0x80 */ [],
/* 0x81 */ [this.OP_STA, 1, this.MODE_INDX],
/* 0x82 */ [],
/* 0x83 */ [],
/* 0x84 */ [this.OP_STY, 1, this.MODE_ZP],
/* 0x85 */ [this.OP_STA, 1, this.MODE_ZP],
/* 0x86 */ [this.OP_STX, 1, this.MODE_ZP],
/* 0x87 */ [],
/* 0x88 */ [this.OP_DEY],
/* 0x89 */ [],
/* 0x8a */ [this.OP_TXA],
/* 0x8b */ [],
/* 0x8c */ [this.OP_STY, 2, this.MODE_ABS],
/* 0x8d */ [this.OP_STA, 2, this.MODE_ABS],
/* 0x8e */ [this.OP_STX, 2, this.MODE_ABS],
/* 0x8f */ [],
/* 0x90 */ [this.OP_BCC, 1, this.MODE_DISP],
/* 0x91 */ [this.OP_STA, 1, this.MODE_INDY],
/* 0x92 */ [],
/* 0x93 */ [],
/* 0x94 */ [this.OP_STY, 1, this.MODE_ZPX],
/* 0x95 */ [this.OP_STA, 1, this.MODE_ZPX],
/* 0x96 */ [this.OP_STX, 1, this.MODE_ZPY],
/* 0x97 */ [],
/* 0x98 */ [this.OP_TYA],
/* 0x99 */ [this.OP_STA, 2, this.MODE_ABSY],
/* 0x9a */ [this.OP_TXS],
/* 0x9b */ [],
/* 0x9c */ [],
/* 0x9d */ [this.OP_STA, 2, this.MODE_ABSX],
/* 0x9e */ [],
/* 0x9f */ [],
/* 0xa0 */ [this.OP_LDY, 1, this.MODE_IMM],
/* 0xa1 */ [this.OP_LDA, 1, this.MODE_INDX],
/* 0xa2 */ [this.OP_LDX, 1, this.MODE_IMM],
/* 0xa3 */ [],
/* 0xa4 */ [this.OP_LDY, 1, this.MODE_ZP],
/* 0xa5 */ [this.OP_LDA, 1, this.MODE_ZP],
/* 0xa6 */ [this.OP_LDX, 1, this.MODE_ZP],
/* 0xa7 */ [],
/* 0xa8 */ [this.OP_TAY],
/* 0xa9 */ [this.OP_LDA, 1, this.MODE_IMM],
/* 0xaa */ [this.OP_TAX],
/* 0xab */ [],
/* 0xac */ [this.OP_LDY, 2, this.MODE_ABS],
/* 0xad */ [this.OP_LDA, 2, this.MODE_ABS],
/* 0xae */ [this.OP_LDX, 2, this.MODE_ABS],
/* 0xaf */ [],
/* 0xb0 */ [this.OP_BCS, 1, this.MODE_DISP],
/* 0xb1 */ [this.OP_LDA, 1, this.MODE_INDY],
/* 0xb2 */ [],
/* 0xb3 */ [],
/* 0xb4 */ [this.OP_LDY, 1, this.MODE_ZPX],
/* 0xb5 */ [this.OP_LDA, 1, this.MODE_ZPX],
/* 0xb6 */ [this.OP_LDX, 1, this.MODE_ZPY],
/* 0xb7 */ [],
/* 0xb8 */ [this.OP_CLV],
/* 0xb9 */ [this.OP_LDA, 2, this.MODE_ABSY],
/* 0xba */ [this.OP_TSX],
/* 0xbb */ [],
/* 0xbc */ [this.OP_LDY, 2, this.MODE_ABSX],
/* 0xbd */ [this.OP_LDA, 2, this.MODE_ABSX],
/* 0xbe */ [this.OP_LDX, 2, this.MODE_ABSY],
/* 0xbf */ [],
/* 0xc0 */ [this.OP_CPY, 1, this.MODE_IMM],
/* 0xc1 */ [this.OP_CMP, 1, this.MODE_INDX],
/* 0xc2 */ [],
/* 0xc3 */ [],
/* 0xc4 */ [this.OP_CPY, 1, this.MODE_ZP],
/* 0xc5 */ [this.OP_CMP, 1, this.MODE_ZP],
/* 0xc6 */ [this.OP_DEC, 1, this.MODE_ZP],
/* 0xc7 */ [],
/* 0xc8 */ [this.OP_INY],
/* 0xc9 */ [this.OP_CMP, 1, this.MODE_IMM],
/* 0xca */ [this.OP_DEX],
/* 0xcb */ [],
/* 0xcc */ [this.OP_CPY, 2, this.MODE_ABS],
/* 0xcd */ [this.OP_CMP, 2, this.MODE_ABS],
/* 0xce */ [this.OP_DEC, 2, this.MODE_ABS],
/* 0xcf */ [],
/* 0xd0 */ [this.OP_BNE, 1, this.MODE_DISP],
/* 0xd1 */ [this.OP_CMP, 1, this.MODE_INDY],
/* 0xd2 */ [],
/* 0xd3 */ [],
/* 0xd4 */ [],
/* 0xd5 */ [this.OP_CMP, 1, this.MODE_ZPX],
/* 0xd6 */ [this.OP_DEC, 1, this.MODE_ZPX],
/* 0xd7 */ [],
/* 0xd8 */ [this.OP_CLD],
/* 0xd9 */ [this.OP_CMP, 2, this.MODE_ABSY],
/* 0xda */ [],
/* 0xdb */ [],
/* 0xdc */ [],
/* 0xdd */ [this.OP_CMP, 2, this.MODE_ABSX],
/* 0xde */ [this.OP_DEC, 2, this.MODE_ABSX],
/* 0xdf */ [],
/* 0xe0 */ [this.OP_CPX, 1, this.MODE_IMM],
/* 0xe1 */ [this.OP_SBC, 1, this.MODE_INDX],
/* 0xe2 */ [],
/* 0xe3 */ [],
/* 0xe4 */ [this.OP_CPX, 1, this.MODE_ZP],
/* 0xe5 */ [this.OP_SBC, 1, this.MODE_ZP],
/* 0xe6 */ [this.OP_INC, 1, this.MODE_ZP],
/* 0xe7 */ [],
/* 0xe8 */ [this.OP_INX],
/* 0xe9 */ [this.OP_SBC, 1, this.MODE_IMM],
/* 0xea */ [this.OP_NOP],
/* 0xeb */ [],
/* 0xec */ [this.OP_CPX, 2, this.MODE_ABS],
/* 0xed */ [this.OP_SBC, 2, this.MODE_ABS],
/* 0xee */ [this.OP_INC, 2, this.MODE_ABS],
/* 0xef */ [],
/* 0xf0 */ [this.OP_BEQ, 1, this.MODE_DISP],
/* 0xf1 */ [this.OP_SBC, 1, this.MODE_INDY],
/* 0xf2 */ [],
/* 0xf3 */ [],
/* 0xf4 */ [],
/* 0xf5 */ [this.OP_SBC, 1, this.MODE_ZPX],
/* 0xf6 */ [this.OP_INC, 1, this.MODE_ZPX],
/* 0xf7 */ [],
/* 0xf8 */ [this.OP_SED],
/* 0xf9 */ [this.OP_SBC, 2, this.MODE_ABSY],
/* 0xfa */ [],
/* 0xfb */ [],
/* 0xfc */ [],
/* 0xfd */ [this.OP_SBC, 2, this.MODE_ABSX],
/* 0xfe */ [this.OP_INC, 2, this.MODE_ABSX],
/* 0xff */ []
];
} // endif DEBUGGER
}
/**
* @this {C1PDebugger}
* @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, "reset")
* @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.eDebug = /** @type {HTMLInputElement} */ (control);
this.eDebug.focus();
control.onkeypress = function(dbg, e) {
return function(event) {
if (event.keyCode == 13) {
sBinding = e.value;
e.value = "";
C1PDebugger.input(dbg, sBinding);
}
};
}(this, control);
return true;
case "debugEnter":
this.bindings[sBinding] = control;
/*
* I've replaced the standard "onclick" code with a call to our onClickRepeat() helper in
* component.js, so that the "Enter" button can be held to repeat, just like the "Step" button.
*/
Web.onClickRepeat(
control, 500, 100,
function(fRepeat) {
if (dbg.eDebug) {
sBinding = dbg.eDebug.value;
//
// If we want to use the debugEnter button to repeatedly enter the same command,
// then don't clear the command string.
//
// dbg.eDebug.value = "";
//
C1PDebugger.input(dbg, sBinding);
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(fRepeat) {
var fCompleted = false;
if (!dbg.isBusy(true)) {
dbg.setBusy(true);
fCompleted = dbg.step(fRepeat? 1 : 0);
dbg.setBusy(false);
}
return fCompleted;
}
);
return true;
default:
break;
}
return false;
}
/**
* @this {C1PDebugger}
* @param {Array} abMemory
* @param {number} start
* @param {number} end
*/
setBuffer(abMemory, start, end)
{
this.abMem = abMemory;
this.offMem = start;
this.cbMem = end - start + 1;
this.offLimit = this.offMem + this.cbMem;
this.setReady();
}
/**
* @this {C1PDebugger}
* @param {boolean} fOn
* @param {C1PComputer} cmp
*/
setPower(fOn, cmp)
{
if (fOn && !this.flags.powered) {
this.flags.powered = true;
this.cpu = cmp.getComponentByType("cpu");
}
}
/**
* @this {C1PDebugger}
*/
setFocus()
{
this.eDebug.focus();
}
/**
* @this {C1PDebugger}
* @param {boolean} fClassic is true for "classic" operand syntax, or false for "modern" operand syntax
*
* The strings describing the operand(s) also describe the addressing mode, as follows:
*
* bbb mode classic modern description
* --- ---- ------- ------- ----------------
* 000 INDX ($nn,X) [[nn+X]] Indexed Indirect
* 001 ZP $nn [nn] Zero-Page
* 010 IMM #$nn nn Immediate
* 011 ABS $nnnn [nnnn] Absolute
* 100 INDY ($nn),Y [[nn]+Y] Indirect Indexed
* 101 ZPX $nn,X [nn+X] Zero-Page,X
* 110 ABSY $nnnn,Y [nnnn+Y] Absolute,Y
* 111 ABSX $nnnn,X [nnnn+X] Absolute,X
*
* where bbb generally corresponds to bits 2-4 of the opcode. I find that using brackets in the
* descriptors to indicate a memory access (or multiple brackets, in the case of indirect accesses),
* along with "+" and any index register, is more intuitive than the "classic" operand formats;
* the absence of any brackets implies immediate data, eliminating the need for a prepended "#".
* Also, the use of 2-digit instead of 4-digit addresses indicates that a zero-page address is
* being used. Finally, all displacements/addresses and immediate values are displayed in hex by
* default, so there is no need to waste space prepending the traditional "$" to such values.
*
* Other addressing modes:
*
* 101 ZPY $nn,Y Zero-Page,Y (used by LDX and STX only)
*
* ACC A Accumulator
*
* IMM16 $nnnn Used by JSR (0x20) and JMP (0x4C); I consider this an "Immediate" operation
* that uses 16 bits, but it is documented as "Absolute" addressing (see Zaks)
*
* FYI, this is the same operand format used for branch displacements (MODE_DISP),
* except the displacements are 8-bit values that are signed-extended to 16 bits, so
* discriminating between MODE_DISP and MODE_IMM16 also requires checking the operand size
*
* ABS16 ($nnnn) Used by JMP (0x6C); I consider this an "Absolute" operation that fetches
* 16 bits of data, but it is documented as "Indirect" addressing (see Zaks)
*/
setOpModes(fClassic)
{
/*
* NOTE: The modes are arranged within aOpModes so that longer matches are checked before
* any subsets that could also match (eg, check for "$nn,X" before "$nn", "$nnnn,X" before "$nnnn", etc).
*/
this.MODE_ACC = 0;
this.MODE_IMM = 1;
this.MODE_ABSX = 2;
this.MODE_ABSY = 3;
this.MODE_IMM16 = 4;
this.MODE_ABS16 = 5;
this.MODE_ZPX = 6;
this.MODE_ZPY = 7;
this.MODE_INDX = 8;
this.MODE_INDY = 9;
this.MODE_ABS = 10;
this.MODE_ZP = 11;
this.MODE_DISP = this.MODE_IMM16;
var sRegEx = "";
var iMode, sMode;
if (fClassic) {
this.aOpModes = [
"A", // MODE_ACC
/* 010b */ "#$nn", // MODE_IMM
/* 111b */ "$nnnn,X", // MODE_ABSX
/* 110b */ "$nnnn,Y", // MODE_ABSY
"$nnnn", // MODE_IMM16
"($nnnn)", // MODE_ABS16
/* 101b */ "$nn,X", // MODE_ZPX
"$nn,Y", // MODE_ZPY
/* 000b */ "($nn,X)", // MODE_INDX
/* 100b */ "($nn),Y", // MODE_INDY
/* 011b */ "$nnnn", // MODE_ABS
/* 001b */ "$nn" // MODE_ZP
];
for (iMode=0; iMode < this.aOpModes.length; iMode++) {
sMode = this.aOpModes[iMode];
sRegEx += "(" + sMode.replace(/\(/g, "\\(").replace(/\)/g, "\\)").replace(/nnnn/g, "[0-9A-F][0-9A-F][0-9A-F][0-9A-F]?").replace(/nn/g, "[0-9A-F][0-9A-F]?").replace(/\$/g, "\\$") + "|)";
}
this.regexOpModes = new RegExp(sRegEx);
}
else {
this.aOpModes = [
"A", // MODE_ACC
/* 010b */ "nn", // MODE_IMM
/* 111b */ "[nnnn+X]", // MODE_ABSX
/* 110b */ "[nnnn+Y]", // MODE_ABSY
"nnnn", // MODE_IMM16
"[nnnn]", // MODE_ABS16
/* 101b */ "[nn+X]", // MODE_ZPX
"[nn+Y]", // MODE_ZPY
/* 000b */ "[[nn+X]]", // MODE_INDX
/* 100b */ "[[nn]+Y]", // MODE_INDY
/* 011b */ "[nnnn]", // MODE_ABS
/* 001b */ "[nn]" // MODE_ZP
];
for (iMode=0; iMode < this.aOpModes.length; iMode++) {
sMode = this.aOpModes[iMode];
sRegEx += "(" + sMode.replace(/\[/g, "\\[").replace(/]/g, "\\]").replace(/nnnn/g, "[0-9A-F][0-9A-F][0-9A-F][0-9A-F]?").replace(/nn/g, "[0-9A-F][0-9A-F]?").replace(/\+/g, "\\+") + "|)";
}
this.regexOpModes = new RegExp(sRegEx);
}
/*
* Regrettably, if "classic" operand syntax is in effect, then we will have to look at the context of the
* operand (ie, the operation code) whenever we have a MODE_IMM16 (or MODE_DISP) match, because it might actually
* be a MODE_ABS operand. MODE_IMM16 is used with only 2 operations (OP_JSR and OP_JMP), and MODE_DISP only 8
* (OP_BPL, OP_BMI, OP_BVC, OP_BVS, OP_BCC, OP_BCS, OP_BNE, and OP_BEQ), so if the operation isn't one of those
* codes (in the following array), then we should convert MODE_IMM16 (aka MODE_DISP) into MODE_ABS.
*/
this.aImm16Codes = [this.OP_JMP, this.OP_JSR, this.OP_BPL, this.OP_BMI, this.OP_BVC, this.OP_BVS, this.OP_BCC, this.OP_BCS, this.OP_BNE, this.OP_BEQ];
}
/**
* @this {C1PDebugger}
*/
halt()
{
/*
* We ask the CPU to halt, but we can't assume it's stopped until it calls stop()
*/
this.cpu.halt();
}
/**
* @this {C1PDebugger}
* @param {string} s is any diagnostic string that you can print later using the Debugger's "i" command
*/
info(s)
{
if (DEBUG) {
this.aInfoBuffer[this.iInfoBuffer++] = s;
if (this.iInfoBuffer >= this.aInfoBuffer.length)
this.iInfoBuffer = 0;
}
}
/**
* @this {C1PDebugger}
* @param {Component} component
* @param {number} addr
* @param {number|undefined} addrFrom
* @param {boolean} bitsMessage is a Debugger MESSAGE_* category flag
* @param {boolean|undefined} [fWrite] is true if this was a write, false (or undefined) if read
* @param {string|undefined} [name] of the port, if any
*/
messageIO(component, addr, addrFrom, bitsMessage, fWrite, name)
{
if ((this.bitsMessage & bitsMessage) == bitsMessage) {
var b = this.cpu.getByte(addr);
this.message(component.id + "." + (fWrite? "setByte":"getByte") + "(" + Str.toHexWord(addr) + ")" + (addrFrom !== undefined? (" @" + Str.toHexWord(addrFrom)) : "") + ": " + (name? (name + "=") : "") + Str.toHexByte(b));
}
}
/**
* @this {C1PDebugger}
* @param {string} sMessage is any caller-defined message string
*/
message(sMessage)
{
this.println(sMessage);
this.cpu.yieldCPU(); // these print() calls are at risk of being called with high frequency, so we need to yieldCPU() more
}
/**
* @this {C1PDebugger}
*/
init()
{
// this.doHelp();
this.println("Type ? for list of debugger commands\n");
}
/**
* @this {C1PDebugger}
* @return {boolean}
*/
run()
{
if (!this.isCPUOK()) return false;
this.cpu.run();
return true;
}
/**
* @this {C1PDebugger}
* @param {number} n (0 implies a single-step, and therefore breakpoints should be ignored)
* @return {boolean}
*/
step(n)
{
if (!this.isCPUOK()) return false;
var fCompleted;
try {
fCompleted = this.cpu.step(n);
}
catch(e) {
fCompleted = undefined;
this.cpu.setError(e.stack || e.message);
}
if (fCompleted !== undefined) this.cIns++;
/*
* Because we called cpu.step() and not cpu.run(), we must
* nudge the CPU's update code, and then update our own state.
*/
this.cpu.update(true);
this.update(true);
return fCompleted;
}
/**
* @this {C1PDebugger}
* @param {boolean} [fStep]
*/
update(fStep)
{
this.nextAddr = this.cpu.regPC;
if (fStep || this.fStepOver)
this.doUnassemble();
else
this.doRegisters();
}
/**
* @this {C1PDebugger}
* @return {boolean}
*
* Make sure the CPU is ready (finished initializing), not busy (already running), and not in an error state.
*/
isCPUOK()
{
if (!this.cpu)
return false;
if (!this.cpu.isReady())
return false;
if (this.cpu.isBusy())
return false;
return !this.cpu.isError();
}
/**
* @this {C1PDebugger}
*
* This is a notification handler, called by the CPU, to inform us that the CPU has been reset.
*/
reset()
{
var i;
if (!this.aStepHistory.length)
this.aStepHistory = new Array(1000);
for (i = 0; i < this.aStepHistory.length; i++)
this.aStepHistory[i] = -1;
if (!this.aaOpcodeFreqs.length)
this.aaOpcodeFreqs = new Array(256);
for (i = 0; i < this.aaOpcodeFreqs.length; i++)
this.aaOpcodeFreqs[i] = [i, 0];
if (this.cIns) this.update();
this.cIns = 0;
this.cReads = this.cWrites = this.cWritesZP = 0;
}
/**
* @this {C1PDebugger}
*
* This is a notification handler, called by the CPU, to inform us that the CPU has started running.
*/
start()
{
if (!this.fStepOver) this.println("running");
}
/**
* @this {C1PDebugger}
* @param {number} msStart
* @param {number} nCycles
*
* This is a notification handler, called by the CPU, to inform us that the CPU has now stopped running.
*/
stop(msStart, nCycles)
{
if (!this.fStepOver) {
this.println("stopped");
if (nCycles) {
var msTotal = Usr.getTime();
msTotal -= msStart;
this.println(msTotal + "ms (" + nCycles + " cycles)");
if (DEBUG && msTotal > 0) {
nCycles = nCycles * 1000 / msTotal;
this.println("total cycles/second: " + Math.round(nCycles));
var percent = Math.round((this.cIns? this.cReads / this.cIns : 0) * 1000) / 10;
this.println("total reads: " + this.cReads + " (" + percent + "%)");
percent = Math.round((this.cIns? this.cWrites / this.cIns : 0) * 1000) / 10;
this.println("total writes: " + this.cWrites + " (" + percent + "%)");
percent = Math.round((this.cIns? this.cWritesZP / this.cIns : 0) * 1000) / 10;
this.println("total zero-page writes: " + this.cWritesZP + " (" + percent + "%)");
this.println("total instructions: " + this.cIns);
}
}
}
this.update();
this.setFocus();
if (!this.fStepOver) {
this.cIns = 0;
this.cReads = this.cWrites = this.cWritesZP = 0;
}
this.clearTempBreakpoint(this.cpu.regPC);
}
/**
* @this {C1PDebugger}
*
* This is a check function, called by the CPU, indicating whether other instructions need to be checked.
*/
checksEnabled()
{
return (DEBUG? true : (this.aExecBreak.length > 0 || this.aReadBreak.length > 0 || this.aWriteBreak.length > 0));
}
/**
* @this {C1PDebugger}
* @param {number} addr
* @param {number} bOpCode
* @return {boolean} true to proceed, false to halt
*
* This is a check function, called by the CPU, to inform us about the next instruction to be executed, giving
* us an opportunity to look for "exec" breakpoints and update opcode frequencies and instruction history.
*/
checkInstruction(addr, bOpCode)
{
var fBreak = false;
if (this.checkBreakpoint(addr, this.aExecBreak, "exec"))
fBreak = true;
else {
this.cIns++;
this.aaOpcodeFreqs[bOpCode][1]++;
this.aStepHistory[this.iStepHistory++] = this.cpu.regPC;
if (this.iStepHistory >= this.aStepHistory.length)
this.iStepHistory = 0;
}
return !fBreak;
}
/**
* @this {C1PDebugger}
* @param {number} addr
* @return {boolean} true to proceed, false to halt
*
* This is a check function, called by the CPU, to inform us that a memory read occurred, giving us an
* opportunity to track the read if we want, and look for a matching "read" breakpoint, if any.
*/
checkMemoryRead(addr)
{
var fBreak = false;
this.cReads++;
if (this.checkBreakpoint(addr, this.aReadBreak, "read"))
fBreak = true;
return !fBreak;
}
/**
* @this {C1PDebugger}
* @param {number} addr
* @param {number} value written
* @return {boolean} true to proceed, false to halt
*
* This is a check function, called by the CPU, to inform us that a memory write occurred, giving us an
* opportunity to track the write if we want, and look for a matching "write" breakpoint, if any.
*/
checkMemoryWrite(addr, value)
{
var fBreak = false;
this.cWrites++;
/*
* NOTE: We keep track of zero-page writes mainly as a reminder to look into whether it makes sense
* for the CPU to calculate zero-page EAs using a different variable (eg, regEAWriteZP instead of regEAWrite),
* because write-notification handlers never care about page zero accesses, and while write breakpoints *may*
* care, it may not be worth the cost of tracking writes to page zero if there's an associated perf penalty.
*/
if (!(addr & 0xff00))
this.cWritesZP++;
if ((value & 0xff) != value) {
this.println("invalid value at " + Str.toHexWord(addr) + ": " + value);
fBreak = true;
}
if (this.checkBreakpoint(addr, this.aWriteBreak, "write"))
fBreak = true;
return !fBreak;
}
/**
* @this {C1PDebugger}
* @param {number} addr
* @param {number} b
* @return {number}