-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdebug.c
2625 lines (2430 loc) · 81.4 KB
/
debug.c
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
#include "debug.h"
#include "bbc.h"
#include "bbc_options.h"
#include "cpu_driver.h"
#include "defs_6502.h"
#include "disc_drive.h"
#include "disc_tool.h"
#include "expression.h"
#include "keyboard.h"
#include "log.h"
#include "render.h"
#include "state.h"
#include "state_6502.h"
#include "timing.h"
#include "util.h"
#include "util_string.h"
#include "via.h"
#include "video.h"
#include "os_fault.h"
#include "os_terminal.h"
#include <assert.h>
#include <inttypes.h>
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
k_max_opcode_len = (13 + 1),
k_max_extra_len = 32,
k_max_break = 16,
k_max_input_len = 1024,
k_max_temp_storage = (256 * 1024),
};
struct debug_breakpoint {
int is_in_use;
int is_enabled;
uint64_t num_hits;
int has_exec_range;
int has_memory_range;
int is_memory_read;
int is_memory_write;
int32_t exec_start;
int32_t exec_end;
int32_t memory_start;
int32_t memory_end;
int do_print;
int do_stop;
char* p_command_list_str;
struct util_string_list_struct* p_command_list;
struct expression_struct* p_expression;
};
struct debug_struct {
struct bbc_struct* p_bbc;
struct state_6502* p_state_6502;
uint8_t* p_mem_read;
struct timing_struct* p_timing;
struct video_struct* p_video;
struct render_struct* p_render;
struct via_struct* p_system_via;
struct via_struct* p_user_via;
struct disc_tool_struct* p_tool;
int debug_active;
int debug_running;
int debug_running_print;
uint8_t* p_opcode_types;
uint8_t* p_opcode_modes;
uint8_t* p_opcode_mem;
uint8_t* p_opcode_cycles;
struct util_string_list_struct* p_command_strings;
struct util_string_list_struct* p_pending_commands;
int opt_is_print_ticks;
/* Modifiable register / machine state. */
uint8_t reg_a;
uint8_t reg_x;
uint8_t reg_y;
uint8_t reg_s;
uint16_t reg_pc;
/* Other machine state, some calculated. */
uint8_t reg_flags;
int32_t addr_6502;
int is_read;
int is_write;
/* Breakpointing. */
int32_t next_or_finish_stop_addr;
struct debug_breakpoint breakpoints[k_max_break];
uint32_t max_breakpoint_used_plus_one;
struct util_buffer* p_temp_storage_buf;
int is_sub_instruction_active;
uint32_t timer_id_sub_instruction;
uint32_t sub_instruction_tick;
int32_t breakpoint_continue;
uint32_t breakpoint_continue_count;
/* Stats. */
int stats;
uint64_t count_addr[k_6502_addr_space_size];
uint64_t count_opcode[k_6502_op_num_opcodes];
uint64_t count_optype[k_6502_op_num_types];
uint64_t count_opmode[k_6502_op_num_modes];
uint64_t rom_write_faults;
uint64_t branch_not_taken;
uint64_t branch_taken;
uint64_t branch_taken_page_crossing;
uint64_t abn_reads;
uint64_t abn_reads_with_page_crossing;
uint64_t idy_reads;
uint64_t idy_reads_with_page_crossing;
uint64_t adc_sbc_count;
uint64_t adc_sbc_with_decimal_count;
uint64_t register_reads;
uint64_t register_writes;
/* Other. */
uint8_t warn_at_addr_count[k_6502_addr_space_size];
int32_t timer_id_debug;
char previous_commands[k_max_input_len];
};
static int s_interrupt_received;
static struct debug_struct* s_p_debug;
static void
debug_interrupt_callback(void) {
s_interrupt_received = 1;
}
static void
debug_clear_breakpoint(struct debug_struct* p_debug, uint32_t i) {
struct debug_breakpoint* p_breakpoint = &p_debug->breakpoints[i];
if (p_breakpoint->p_command_list) {
util_free(p_breakpoint->p_command_list_str);
util_string_list_free(p_breakpoint->p_command_list);
}
if (p_breakpoint->p_expression) {
expression_destroy(p_breakpoint->p_expression);
}
(void) memset(p_breakpoint, '\0', sizeof(struct debug_breakpoint));
p_breakpoint->exec_start = -1;
p_breakpoint->exec_end = -1;
p_breakpoint->memory_start = -1;
p_breakpoint->memory_end = -1;
}
static void
debug_timer_callback(void* p) {
struct debug_struct* p_debug = (struct debug_struct*) p;
(void) timing_stop_timer(p_debug->p_timing, p_debug->timer_id_debug);
s_interrupt_received = 1;
}
void
debug_init(struct debug_struct* p_debug) {
struct cpu_driver* p_cpu_driver = bbc_get_cpu_driver(p_debug->p_bbc);
p_cpu_driver->p_funcs->get_opcode_maps(p_cpu_driver,
&p_debug->p_opcode_types,
&p_debug->p_opcode_modes,
&p_debug->p_opcode_mem,
&p_debug->p_opcode_cycles);
}
void
debug_destroy(struct debug_struct* p_debug) {
disc_tool_destroy(p_debug->p_tool);
util_string_list_free(p_debug->p_command_strings);
util_string_list_free(p_debug->p_pending_commands);
util_buffer_destroy(p_debug->p_temp_storage_buf);
free(p_debug);
}
volatile int*
debug_get_interrupt(struct debug_struct* p_debug) {
(void) p_debug;
return &s_interrupt_received;
}
int
debug_subsystem_active(void* p) {
struct debug_struct* p_debug = (struct debug_struct*) p;
return p_debug->debug_active;
}
static void
debug_print_opcode(struct debug_struct* p_debug,
char* buf,
size_t buf_len,
uint8_t opcode,
uint8_t operand1,
uint8_t operand2,
uint16_t reg_pc,
int do_irq) {
uint8_t optype;
uint8_t opmode;
const char* opname;
uint16_t addr;
if (do_irq) {
struct state_6502* p_state_6502 = bbc_get_6502(p_debug->p_bbc);
/* Very close approximation. It's possible a non-NMI IRQ will be reported
* but then an NMI occurs if the NMI is raised within the first few cycles
* of the IRQ BRK.
*/
if (state_6502_check_irq_firing(p_state_6502, k_state_6502_irq_nmi)) {
(void) snprintf(buf, buf_len, "IRQ (NMI)");
} else {
(void) snprintf(buf, buf_len, "IRQ (IRQ)");
}
return;
}
optype = p_debug->p_opcode_types[opcode];
opmode = p_debug->p_opcode_modes[opcode];
opname = g_p_opnames[optype];
addr = (operand1 | (operand2 << 8));
switch (opmode) {
case k_nil:
case k_nil1:
(void) snprintf(buf, buf_len, "%s", opname);
break;
case k_acc:
(void) snprintf(buf, buf_len, "%s A", opname);
break;
case k_imm:
(void) snprintf(buf, buf_len, "%s #$%.2"PRIX8, opname, operand1);
break;
case k_zpg:
(void) snprintf(buf, buf_len, "%s $%.2"PRIX8, opname, operand1);
break;
case k_abs:
(void) snprintf(buf, buf_len, "%s $%.4"PRIX16, opname, addr);
break;
case k_zpx:
(void) snprintf(buf, buf_len, "%s $%.2"PRIX8",X", opname, operand1);
break;
case k_zpy:
(void) snprintf(buf, buf_len, "%s $%.2"PRIX8",Y", opname, operand1);
break;
case k_abx:
(void) snprintf(buf, buf_len, "%s $%.4"PRIX16",X", opname, addr);
break;
case k_aby:
(void) snprintf(buf, buf_len, "%s $%.4"PRIX16",Y", opname, addr);
break;
case k_idx:
(void) snprintf(buf, buf_len, "%s ($%.2"PRIX8",X)", opname, operand1);
break;
case k_idy:
(void) snprintf(buf, buf_len, "%s ($%.2"PRIX8"),Y", opname, operand1);
break;
case k_ind:
(void) snprintf(buf, buf_len, "%s ($%.4"PRIX16")", opname, addr);
break;
case k_rel:
addr = (reg_pc + 2 + (int8_t) operand1);
(void) snprintf(buf, buf_len, "%s $%.4"PRIX16, opname, addr);
break;
case k_iax:
(void) snprintf(buf, buf_len, "%s ($%.4"PRIX16",X)", opname, addr);
break;
case k_id:
(void) snprintf(buf, buf_len, "%s ($%.2"PRIX8")", opname, operand1);
break;
case 0:
(void) snprintf(buf, buf_len, "%s: $%.2"PRIX8, opname, opcode);
break;
default:
assert(0);
break;
}
}
static inline int
debug_flag_z(uint8_t reg_flags) {
return !!(reg_flags & 0x02);
}
static inline int
debug_flag_n(uint8_t reg_flags) {
return !!(reg_flags & 0x80);
}
static inline int
debug_flag_c(uint8_t reg_flags) {
return !!(reg_flags & 0x01);
}
static inline int
debug_flag_o(uint8_t reg_flags) {
return !!(reg_flags & 0x40);
}
static inline int
debug_flag_d(uint8_t reg_flags) {
return !!(reg_flags & 0x08);
}
static inline int
debug_flag_i(uint8_t reg_flags) {
return !!(reg_flags & 0x04);
}
static inline void
debug_get_details(int* p_addr_6502,
int* p_branch_taken,
int* p_is_read,
int* p_is_write,
int* p_is_rom,
int* p_is_register,
int* p_wrapped_8bit,
int* p_wrapped_16bit,
struct debug_struct* p_debug,
uint8_t opmode,
uint8_t optype,
uint8_t opmem,
uint8_t operand1,
uint8_t operand2,
uint8_t* p_mem) {
uint16_t addr_addr;
int32_t addr = -1;
int check_wrap_8bit = 1;
*p_addr_6502 = -1;
*p_branch_taken = -1;
*p_wrapped_8bit = 0;
*p_wrapped_16bit = 0;
/* This is correct for most modes, but needs care as we want to consider
* stack operations reads / writes too.
*/
*p_is_read = !!(opmem & k_opmem_read_flag);
*p_is_write = !!(opmem & k_opmem_write_flag);
switch (opmode) {
case k_zpg:
addr = operand1;
*p_addr_6502 = addr;
break;
case k_zpx:
addr = (operand1 + p_debug->reg_x);
*p_addr_6502 = (uint8_t) addr;
break;
case k_zpy:
addr = (operand1 + p_debug->reg_y);
*p_addr_6502 = (uint8_t) addr;
break;
case k_abs:
check_wrap_8bit = 0;
if (optype != k_jsr && optype != k_jmp) {
addr = (operand1 + (operand2 << 8));
*p_addr_6502 = addr;
}
break;
case k_abx:
addr = (operand1 + (operand2 << 8) + p_debug->reg_x);
check_wrap_8bit = 0;
*p_addr_6502 = (uint16_t) addr;
break;
case k_aby:
addr = (operand1 + (operand2 << 8) + p_debug->reg_y);
check_wrap_8bit = 0;
*p_addr_6502 = (uint16_t) addr;
break;
case k_idx:
addr_addr = (operand1 + p_debug->reg_x);
addr = (uint8_t) addr_addr;
if ((addr != addr_addr) || (addr == 0xFF)) {
*p_wrapped_8bit = 1;
}
*p_addr_6502 = p_mem[(uint8_t) (addr + 1)];
*p_addr_6502 <<= 8;
*p_addr_6502 |= p_mem[addr];
addr = *p_addr_6502;
check_wrap_8bit = 0;
break;
case k_idy:
if (operand1 == 0xFF) {
*p_wrapped_8bit = 1;
}
addr = p_mem[(uint8_t) (operand1 + 1)];
addr <<= 8;
addr |= p_mem[operand1];
addr = (addr + p_debug->reg_y);
check_wrap_8bit = 0;
*p_addr_6502 = (uint16_t) addr;
break;
case k_id:
if (operand1 == 0xFF) {
*p_wrapped_8bit = 1;
}
addr = p_mem[(uint8_t) (operand1 + 1)];
addr <<= 8;
addr |= p_mem[operand1];
*p_addr_6502 = (uint16_t) addr;
break;
case k_rel:
addr_addr = (p_debug->reg_pc + 2);
addr = (uint16_t) (addr_addr + (int8_t) operand1);
switch (optype) {
case k_bpl:
*p_branch_taken = !debug_flag_n(p_debug->reg_flags);
break;
case k_bmi:
*p_branch_taken = debug_flag_n(p_debug->reg_flags);
break;
case k_bvc:
*p_branch_taken = !debug_flag_o(p_debug->reg_flags);
break;
case k_bvs:
*p_branch_taken = debug_flag_o(p_debug->reg_flags);
break;
case k_bcc:
*p_branch_taken = !debug_flag_c(p_debug->reg_flags);
break;
case k_bcs:
*p_branch_taken = debug_flag_c(p_debug->reg_flags);
break;
case k_bne:
*p_branch_taken = !debug_flag_z(p_debug->reg_flags);
break;
case k_beq:
*p_branch_taken = debug_flag_z(p_debug->reg_flags);
break;
case k_bra:
*p_branch_taken = 1;
break;
default:
assert(0);
break;
}
if (*p_branch_taken) {
if ((addr_addr & 0x100) ^ (addr & 0x100)) {
*p_wrapped_8bit = 1;
}
}
break;
default:
switch (optype) {
case k_php:
case k_pha:
case k_phx:
case k_phy:
*p_is_write = 1;
addr = (p_debug->reg_s + k_6502_stack_addr);
*p_addr_6502 = addr;
break;
case k_plp:
case k_pla:
case k_plx:
case k_ply:
*p_is_read = 1;
addr = ((uint8_t) (p_debug->reg_s + 1) + k_6502_stack_addr);
*p_addr_6502 = addr;
break;
default:
break;
}
break;
}
if ((opmode != k_rel) && (addr != *p_addr_6502)) {
if (check_wrap_8bit) {
*p_wrapped_8bit = 1;
} else {
*p_wrapped_16bit = 1;
}
}
bbc_get_address_details(p_debug->p_bbc,
p_is_register,
p_is_rom,
*p_addr_6502);
}
static uint16_t
debug_disass(struct debug_struct* p_debug,
struct cpu_driver* p_cpu_driver,
uint16_t addr_6502) {
size_t i;
uint8_t* p_mem_read = p_debug->p_mem_read;
for (i = 0; i < 20; ++i) {
char opcode_buf[k_max_opcode_len];
uint16_t addr_plus_1 = (addr_6502 + 1);
uint16_t addr_plus_2 = (addr_6502 + 2);
uint8_t opcode = p_mem_read[addr_6502];
uint8_t opmode = p_debug->p_opcode_modes[opcode];
uint8_t oplen = g_opmodelens[opmode];
uint8_t operand1 = p_mem_read[addr_plus_1];
uint8_t operand2 = p_mem_read[addr_plus_2];
const char* p_address_info = "";
if (p_cpu_driver != NULL) {
p_address_info = p_cpu_driver->p_funcs->get_address_info(
p_cpu_driver, addr_6502);
}
debug_print_opcode(p_debug,
opcode_buf,
sizeof(opcode_buf),
opcode,
operand1,
operand2,
addr_6502,
0);
(void) printf("[%s] %.4"PRIX16": %s\n",
p_address_info,
addr_6502,
opcode_buf);
addr_6502 += oplen;
}
return addr_6502;
}
static void
debug_dump_via(struct bbc_struct* p_bbc, int id) {
struct via_struct* p_via;
uint8_t ORA;
uint8_t ORB;
uint8_t DDRA;
uint8_t DDRB;
uint8_t SR;
uint8_t ACR;
uint8_t PCR;
uint8_t IFR;
uint8_t IER;
uint8_t peripheral_a;
uint8_t peripheral_b;
int32_t T1C_raw;
int32_t T1L;
int32_t T2C_raw;
int32_t T2L;
uint8_t t1_oneshot_fired;
uint8_t t2_oneshot_fired;
uint8_t t1_pb7;
int CA1;
int CA2;
int CB1;
int CB2;
if (id == k_via_system) {
(void) printf("System VIA\n");
p_via = bbc_get_sysvia(p_bbc);
} else if (id == k_via_user) {
(void) printf("User VIA\n");
p_via = bbc_get_uservia(p_bbc);
} else {
assert(0);
p_via = NULL;
}
via_get_registers(p_via,
&ORA,
&ORB,
&DDRA,
&DDRB,
&SR,
&ACR,
&PCR,
&IFR,
&IER,
&peripheral_a,
&peripheral_b,
&T1C_raw,
&T1L,
&T2C_raw,
&T2L,
&t1_oneshot_fired,
&t2_oneshot_fired,
&t1_pb7);
via_get_all_CAB(p_via, &CA1, &CA2, &CB1, &CB2);
(void) printf("IFR %.2"PRIX8" IER %.2"PRIX8"\n", IFR, IER);
(void) printf("ORA %.2"PRIX8" DDRA %.2"PRIX8" periph %.2"PRIX8"\n",
ORA,
DDRA,
peripheral_a);
(void) printf("ORB %.2"PRIX8" DDRB %.2"PRIX8" periph %.2"PRIX8"\n",
ORB,
DDRB,
peripheral_b);
(void) printf("SR %.2"PRIX8" ACR %.2"PRIX8" PCR %.2"PRIX8"\n", SR, ACR, PCR);
(void) printf("T1L %.4"PRIX16" T1C %.4"PRIX16" oneshot hit %d PB7 %d\n",
(uint16_t) T1L,
(uint16_t) (T1C_raw >> 1),
(int) t1_oneshot_fired,
(int) t1_pb7);
(void) printf("T2L %.4"PRIX16" T2C %.4"PRIX16" oneshot hit %d\n",
(uint16_t) T2L,
(uint16_t) (T2C_raw >> 1),
(int) t2_oneshot_fired);
(void) printf("CA1 %d CA2 %d CB1 %d CB2 %d\n", CA1, CA2, CB1, CB2);
/* IC32 isn't really a VIA thing but put it here for convenience. */
if (id == k_via_system) {
uint8_t IC32 = bbc_get_IC32(p_bbc);
(void) printf("IC32 %.2"PRIX8"\n", IC32);
}
}
static void
debug_dump_crtc(struct bbc_struct* p_bbc) {
uint32_t i;
uint8_t horiz_counter;
uint8_t scanline_counter;
uint8_t vert_counter;
uint16_t address_counter;
uint64_t crtc_frames;
int is_in_vert_adjust;
int is_in_dummy_raster;
uint8_t regs[k_video_crtc_num_registers];
uint32_t horiz_pos;
uint32_t vert_pos;
struct video_struct* p_video = bbc_get_video(p_bbc);
struct render_struct* p_render = bbc_get_render(p_bbc);
horiz_pos = render_get_horiz_pos(p_render);
vert_pos = render_get_vert_pos(p_render);
(void) printf("beam pos: horiz %d vert %d\n", horiz_pos, vert_pos);
video_get_crtc_state(p_video,
&horiz_counter,
&scanline_counter,
&vert_counter,
&address_counter,
&crtc_frames,
&is_in_vert_adjust,
&is_in_dummy_raster);
video_get_crtc_registers(p_video, ®s[0]);
(void) printf("horiz %"PRId8" scanline %"PRId8" vert %"PRId8
" addr $%.4"PRIX16" frames %"PRIu64"\n",
horiz_counter,
scanline_counter,
vert_counter,
address_counter,
crtc_frames);
(void) printf("is_in_vert_adjust %d is_in_dummy_raster %d\n",
is_in_vert_adjust,
is_in_dummy_raster);
for (i = 0; i < k_video_crtc_num_registers; ++i) {
(void) printf("R%.2d $%.2X ", i, regs[i]);
if ((i & 7) == 7) {
(void) printf("\n");
}
}
(void) printf("\n");
}
static void
debug_dump_bbc(struct bbc_struct* p_bbc) {
uint32_t i;
uint8_t ula_palette[16];
struct video_struct* p_video = bbc_get_video(p_bbc);
uint8_t ula_ctrl = video_get_ula_control(p_video);
video_get_ula_full_palette(p_video, &ula_palette[0]);
(void) printf("ROMSEL $%.2X ACCCON $%.2X IC32 $%.2X\n",
bbc_get_romsel(p_bbc),
bbc_get_acccon(p_bbc),
bbc_get_IC32(p_bbc));
(void) printf("ULA control $%.2X\n", ula_ctrl);
(void) printf("ULA palette ");
for (i = 0; i < 16; ++i) {
(void) printf("$%.2X ", ula_palette[i]);
}
printf("\n");
}
static struct debug_breakpoint*
debug_get_free_breakpoint(struct debug_struct* p_debug) {
uint32_t i;
for (i = 0; i < k_max_break; ++i) {
struct debug_breakpoint* p_breakpoint = &p_debug->breakpoints[i];
if (!p_breakpoint->is_in_use) {
p_breakpoint->is_in_use = 1;
if ((i + 1) > p_debug->max_breakpoint_used_plus_one) {
p_debug->max_breakpoint_used_plus_one = (i + 1);
}
return p_breakpoint;
}
}
return NULL;
}
static inline void
debug_calculate_max_breakpoint(struct debug_struct* p_debug) {
uint32_t i;
p_debug->max_breakpoint_used_plus_one = 0;
for (i = 0; i < k_max_break; ++i) {
struct debug_breakpoint* p_breakpoint = &p_debug->breakpoints[i];
if (p_breakpoint->is_in_use) {
p_debug->max_breakpoint_used_plus_one = (i + 1);
}
}
}
static inline void
debug_check_breakpoints(struct debug_struct* p_debug,
int* p_out_print,
int* p_out_stop,
uint8_t opmem) {
uint32_t i;
uint32_t max_breakpoint_used_plus_one = p_debug->max_breakpoint_used_plus_one;
if (p_debug->reg_pc == p_debug->next_or_finish_stop_addr) {
*p_out_print = 1;
*p_out_stop = 1;
}
for (i = 0; i < max_breakpoint_used_plus_one; ++i) {
struct debug_breakpoint* p_breakpoint = &p_debug->breakpoints[i];
if (!p_breakpoint->is_in_use) {
continue;
}
if (!p_breakpoint->is_enabled) {
continue;
}
if (p_breakpoint->has_exec_range) {
if ((p_debug->reg_pc < p_breakpoint->exec_start) ||
(p_debug->reg_pc > p_breakpoint->exec_end)) {
continue;
}
}
if (p_breakpoint->has_memory_range) {
if ((p_debug->addr_6502 < p_breakpoint->memory_start) ||
(p_debug->addr_6502 > p_breakpoint->memory_end)) {
continue;
}
if (p_breakpoint->is_memory_read && (opmem & k_opmem_read_flag)) {
/* Match. */
} else if (p_breakpoint->is_memory_write &&
(opmem & k_opmem_write_flag)) {
/* Match. */
} else {
continue;
}
}
if (p_breakpoint->p_expression != NULL) {
int64_t expression_ret = expression_execute(p_breakpoint->p_expression);
if (expression_ret == 0) {
continue;
}
}
/* If we arrive here, it's a hit. */
p_breakpoint->num_hits++;
if (p_breakpoint->do_stop && p_breakpoint->do_print) {
(void) printf("breakpoint %"PRIu32" hit %"PRIu64" times\n",
i,
p_breakpoint->num_hits);
}
*p_out_print |= p_breakpoint->do_print;
*p_out_stop |= p_breakpoint->do_stop;
if (p_breakpoint->p_command_list != NULL) {
util_string_list_append_list(p_debug->p_pending_commands,
p_breakpoint->p_command_list);
}
}
}
static int
debug_sort_opcodes(const void* p_op1, const void* p_op2) {
uint8_t op1 = *(uint8_t*) p_op1;
uint8_t op2 = *(uint8_t*) p_op2;
return (s_p_debug->count_opcode[op1] - s_p_debug->count_opcode[op2]);
}
static int
debug_sort_addrs(const void* p_addr1, const void* p_addr2) {
uint16_t addr1 = *(uint16_t*) p_addr1;
uint16_t addr2 = *(uint16_t*) p_addr2;
return (s_p_debug->count_addr[addr1] - s_p_debug->count_addr[addr2]);
}
static void
debug_clear_stats(struct debug_struct* p_debug) {
uint32_t i;
for (i = 0; i < k_6502_addr_space_size; ++i) {
p_debug->count_addr[i] = 0;
}
for (i = 0; i < k_6502_op_num_opcodes; ++i) {
p_debug->count_opcode[i] = 0;
}
for (i = 0; i < k_6502_op_num_types; ++i) {
p_debug->count_optype[i] = 0;
}
for (i = 0; i < k_6502_op_num_modes; ++i) {
p_debug->count_opmode[i] = 0;
}
p_debug->rom_write_faults = 0;
p_debug->branch_not_taken = 0;
p_debug->branch_taken = 0;
p_debug->branch_taken_page_crossing = 0;
p_debug->abn_reads = 0;
p_debug->abn_reads_with_page_crossing = 0;
p_debug->idy_reads = 0;
p_debug->idy_reads_with_page_crossing = 0;
p_debug->adc_sbc_count = 0;
p_debug->adc_sbc_with_decimal_count = 0;
p_debug->register_reads = 0;
p_debug->register_writes = 0;
}
static void
debug_dump_stats(struct debug_struct* p_debug) {
size_t i;
uint8_t sorted_opcodes[k_6502_op_num_opcodes];
uint16_t sorted_addrs[k_6502_addr_space_size];
for (i = 0; i < k_6502_op_num_opcodes; ++i) {
sorted_opcodes[i] = i;
}
qsort(sorted_opcodes,
k_6502_op_num_opcodes,
sizeof(uint8_t),
debug_sort_opcodes);
(void) printf("=== Opcodes ===\n");
for (i = 0; i < k_6502_op_num_opcodes; ++i) {
char opcode_buf[k_max_opcode_len];
uint8_t opcode = sorted_opcodes[i];
uint64_t count = p_debug->count_opcode[opcode];
if (!count) {
continue;
}
debug_print_opcode(p_debug,
opcode_buf,
sizeof(opcode_buf),
opcode,
0,
0,
0xFFFE,
0);
(void) printf("%14s: %"PRIu64"\n", opcode_buf, count);
}
for (i = 0; i < k_6502_addr_space_size; ++i) {
sorted_addrs[i] = i;
}
qsort(sorted_addrs,
k_6502_addr_space_size,
sizeof(uint16_t),
debug_sort_addrs);
(void) printf("=== Addrs ===\n");
for (i = k_6502_addr_space_size - 256; i < k_6502_addr_space_size; ++i) {
uint16_t addr = sorted_addrs[i];
uint64_t count = p_debug->count_addr[addr];
if (!count) {
continue;
}
(void) printf("%4"PRIX16": %"PRIu64"\n", addr, count);
}
(void) printf("--> rom_write_faults: %"PRIu64"\n", p_debug->rom_write_faults);
(void) printf("--> branch (not taken, taken, page cross): "
"%"PRIu64", %"PRIu64", %"PRIu64"\n",
p_debug->branch_not_taken,
p_debug->branch_taken,
p_debug->branch_taken_page_crossing);
(void) printf("--> abn reads (total, page crossing): %"PRIu64", %"PRIu64"\n",
p_debug->abn_reads,
p_debug->abn_reads_with_page_crossing);
(void) printf("--> idy reads (total, page crossing): %"PRIu64", %"PRIu64"\n",
p_debug->idy_reads,
p_debug->idy_reads_with_page_crossing);
(void) printf("--> abc/sbc (total, with decimal flag): "
"%"PRIu64", %"PRIu64"\n",
p_debug->adc_sbc_count,
p_debug->adc_sbc_with_decimal_count);
(void) printf("--> register hits (read / write): %"PRIu64", %"PRIu64"\n",
p_debug->register_reads,
p_debug->register_writes);
}
static void
debug_dump_breakpoints(struct debug_struct* p_debug) {
uint32_t i;
for (i = 0; i < k_max_break; ++i) {
struct debug_breakpoint* p_breakpoint = &p_debug->breakpoints[i];
if (!p_breakpoint->is_in_use) {
continue;
}
(void) printf("breakpoint %"PRIu32":", i);
if (p_breakpoint->has_exec_range) {
(void) printf(" exec @$%.4"PRIX16"-$%.4"PRIX16,
(uint16_t) p_breakpoint->exec_start,
(uint16_t) p_breakpoint->exec_end);
}
if (p_breakpoint->has_memory_range) {
const char* p_name = "read";
if (p_breakpoint->is_memory_write) {
if (p_breakpoint->is_memory_read) {
p_name = "rw";
} else {
p_name = "write";
}
}
(void) printf(" %s @$%.4"PRIX16"-$%.4"PRIX16,
p_name,
(uint16_t) p_breakpoint->memory_start,
(uint16_t) p_breakpoint->memory_end);
}
if (p_breakpoint->p_expression != NULL) {
const char* p_str = expression_get_original_string(
p_breakpoint->p_expression);
(void) printf(" expr '%s'", p_str);
}
if (p_breakpoint->p_command_list != NULL) {
(void) printf(" commands '%s'", p_breakpoint->p_command_list_str);
}
if (!p_breakpoint->do_stop) {
(void) printf(" nostop");
}
if (!p_breakpoint->do_print) {
(void) printf(" noprint");
}
if (!p_breakpoint->is_enabled) {
(void) printf(" disabled");
}
(void) printf(" hit %"PRIu64, p_breakpoint->num_hits);
(void) printf("\n");
}
}
static inline void
debug_check_unusual(struct debug_struct* p_debug,
uint8_t opcode,
uint8_t operand1,
uint8_t optype,
uint8_t opmode,
int is_rom,
int is_register,
int wrapped_8bit,
int wrapped_16bit) {
int warned;
int is_undocumented = 0;
uint8_t warn_count = p_debug->warn_at_addr_count[p_debug->reg_pc];
if (!warn_count) {
return;
}
warned = 0;
if (is_register && ((opmode == k_idx) || (opmode == k_idy))) {
(void) printf("DEBUG (UNUSUAL): "
"Indirect access to register $%.4"PRIX16" at $%.4"PRIX16"\n",
(uint16_t) p_debug->addr_6502,
p_debug->reg_pc);
warned = 1;
}
/* Handled via various means but worth noting. */
if (p_debug->is_write && is_rom) {
(void) printf("DEBUG: Code at $%.4"PRIX16" is writing to ROM "
"at $%.4"PRIX16"\n",
p_debug->reg_pc,
(uint16_t) p_debug->addr_6502);
warned = 1;
}
/* Look for zero page wrap or full address space wraps. */
if (wrapped_8bit) {
if (opmode == k_rel) {
/* Nothing. */
} else if (opmode == k_idy) {
/* Nothing. */
} else if (opmode == k_idx) {
uint16_t unwrapped_addr = (operand1 + p_debug->reg_x);
if (unwrapped_addr >= 0x100) {
(void) printf("DEBUG (VERY UNUSUAL): 8-bit IDX ADDRESS WRAP at "
"$%.4"PRIX16" to $%.4"PRIX16"\n",
p_debug->reg_pc,
(uint16_t) (uint8_t) unwrapped_addr);
warned = 1;
}
} else {
(void) printf("DEBUG (UNUSUAL): 8-bit ZPX/Y ADDRESS WRAP at "
"$%.4"PRIX16" to $%.4"PRIX16"\n",
p_debug->reg_pc,
(uint16_t) p_debug->addr_6502);
warned = 1;
}
}
if (wrapped_16bit) {
(void) printf("DEBUG (VERY UNUSUAL): "
"16-bit ADDRESS WRAP at $%.4"PRIX16" to $%.4"PRIX16"\n",