annotate debug.c @ 651:103d5cabbe14

Fix flags for rra, rrca, rla and rlca. Fix timing for rr, rrc, rl and rlc when using IX or IY. Fix access to I and R registers (R still needs to be made 7-bit though). Fix flags for ld a, i. The fix for access to I fixes PCM playback in Titan Overdrive and music playback in Crackdown.
author Michael Pavone <pavone@retrodev.com>
date Tue, 16 Dec 2014 01:10:54 -0800
parents b76d2a628ab9
children f822d9216968
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
524
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include "debug.h"
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include "blastem.h"
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include "68kinst.h"
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
4 #include <stdlib.h>
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
5 #include <string.h>
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6
525
62860044337d Support single stepping in gdb remote debugger
Mike Pavone <pavone@retrodev.com>
parents: 524
diff changeset
7 static bp_def * breakpoints = NULL;
62860044337d Support single stepping in gdb remote debugger
Mike Pavone <pavone@retrodev.com>
parents: 524
diff changeset
8 static bp_def * zbreakpoints = NULL;
62860044337d Support single stepping in gdb remote debugger
Mike Pavone <pavone@retrodev.com>
parents: 524
diff changeset
9 static uint32_t bp_index = 0;
62860044337d Support single stepping in gdb remote debugger
Mike Pavone <pavone@retrodev.com>
parents: 524
diff changeset
10 static uint32_t zbp_index = 0;
524
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
11
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
12 bp_def ** find_breakpoint(bp_def ** cur, uint32_t address)
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
13 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
14 while (*cur) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
15 if ((*cur)->address == address) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
16 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
17 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
18 cur = &((*cur)->next);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
19 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
20 return cur;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
21 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
22
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
23 bp_def ** find_breakpoint_idx(bp_def ** cur, uint32_t index)
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
25 while (*cur) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26 if ((*cur)->index == index) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29 cur = &((*cur)->next);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
30 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
31 return cur;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
32 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
33
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
34 disp_def * displays = NULL;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
35 disp_def * zdisplays = NULL;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
36 uint32_t disp_index = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
37 uint32_t zdisp_index = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
38
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39 void add_display(disp_def ** head, uint32_t *index, char format_char, char * param)
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
41 disp_def * ndisp = malloc(sizeof(*ndisp));
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 ndisp->format_char = format_char;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43 ndisp->param = strdup(param);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
44 ndisp->next = *head;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
45 ndisp->index = *index++;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46 *head = ndisp;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 void remove_display(disp_def ** head, uint32_t index)
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 while (*head) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 if ((*head)->index == index) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 disp_def * del_disp = *head;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 *head = del_disp->next;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 free(del_disp->param);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 free(del_disp);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58 head = &(*head)->next;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
59 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
60 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
61 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
63 char * find_param(char * buf)
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
64 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
65 for (; *buf; buf++) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
66 if (*buf == ' ') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
67 if (*(buf+1)) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
68 return buf+1;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
69 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
70 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
71 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
72 return NULL;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
75 void strip_nl(char * buf)
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
76 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
77 for(; *buf; buf++) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
78 if (*buf == '\n') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 *buf = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
80 return;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
82 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
83 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
84
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
85 void zdebugger_print(z80_context * context, char format_char, char * param)
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
86 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
87 uint32_t value;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
88 char format[8];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
89 strcpy(format, "%s: %d\n");
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
90 switch (format_char)
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
91 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
92 case 'x':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
93 case 'X':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
94 case 'd':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
95 case 'c':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
96 format[5] = format_char;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
97 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
98 case '\0':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
99 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
100 default:
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
101 fprintf(stderr, "Unrecognized format character: %c\n", format_char);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
102 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
103 switch (param[0])
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
104 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
105 case 'a':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
106 if (param[1] == 'f') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
107 if(param[2] == '\'') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
108 value = context->alt_regs[Z80_A] << 8;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
109 value |= context->alt_flags[ZF_S] << 7;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
110 value |= context->alt_flags[ZF_Z] << 6;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
111 value |= context->alt_flags[ZF_H] << 4;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
112 value |= context->alt_flags[ZF_PV] << 2;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
113 value |= context->alt_flags[ZF_N] << 1;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
114 value |= context->alt_flags[ZF_C];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
115 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
116 value = context->regs[Z80_A] << 8;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
117 value |= context->flags[ZF_S] << 7;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
118 value |= context->flags[ZF_Z] << 6;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
119 value |= context->flags[ZF_H] << 4;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
120 value |= context->flags[ZF_PV] << 2;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
121 value |= context->flags[ZF_N] << 1;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
122 value |= context->flags[ZF_C];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
123 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
124 } else if(param[1] == '\'') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
125 value = context->alt_regs[Z80_A];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
126 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
127 value = context->regs[Z80_A];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
128 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
129 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
130 case 'b':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
131 if (param[1] == 'c') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
132 if(param[2] == '\'') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
133 value = context->alt_regs[Z80_B] << 8;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
134 value |= context->alt_regs[Z80_C];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
135 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
136 value = context->regs[Z80_B] << 8;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
137 value |= context->regs[Z80_C];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
138 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
139 } else if(param[1] == '\'') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
140 value = context->alt_regs[Z80_B];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
141 } else if(param[1] == 'a') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
142 value = context->bank_reg << 15;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
143 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
144 value = context->regs[Z80_B];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
145 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
146 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
147 case 'c':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
148 if(param[1] == '\'') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
149 value = context->alt_regs[Z80_C];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
150 } else if(param[1] == 'y') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
151 value = context->current_cycle;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
152 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
153 value = context->regs[Z80_C];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
154 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
155 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
156 case 'd':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
157 if (param[1] == 'e') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
158 if(param[2] == '\'') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
159 value = context->alt_regs[Z80_D] << 8;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
160 value |= context->alt_regs[Z80_E];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
161 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
162 value = context->regs[Z80_D] << 8;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
163 value |= context->regs[Z80_E];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
164 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
165 } else if(param[1] == '\'') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
166 value = context->alt_regs[Z80_D];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
167 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
168 value = context->regs[Z80_D];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
169 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
170 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
171 case 'e':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
172 if(param[1] == '\'') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
173 value = context->alt_regs[Z80_E];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
174 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
175 value = context->regs[Z80_E];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
176 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
177 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
178 case 'f':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
179 if(param[2] == '\'') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
180 value = context->alt_flags[ZF_S] << 7;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
181 value |= context->alt_flags[ZF_Z] << 6;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
182 value |= context->alt_flags[ZF_H] << 4;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
183 value |= context->alt_flags[ZF_PV] << 2;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
184 value |= context->alt_flags[ZF_N] << 1;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
185 value |= context->alt_flags[ZF_C];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
186 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
187 value = context->flags[ZF_S] << 7;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
188 value |= context->flags[ZF_Z] << 6;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
189 value |= context->flags[ZF_H] << 4;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
190 value |= context->flags[ZF_PV] << 2;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
191 value |= context->flags[ZF_N] << 1;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
192 value |= context->flags[ZF_C];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
193 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
194 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
195 case 'h':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
196 if (param[1] == 'l') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
197 if(param[2] == '\'') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
198 value = context->alt_regs[Z80_H] << 8;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
199 value |= context->alt_regs[Z80_L];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
200 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
201 value = context->regs[Z80_H] << 8;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
202 value |= context->regs[Z80_L];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
203 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
204 } else if(param[1] == '\'') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
205 value = context->alt_regs[Z80_H];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
206 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
207 value = context->regs[Z80_H];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
208 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
209 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
210 case 'l':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
211 if(param[1] == '\'') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
212 value = context->alt_regs[Z80_L];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
213 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
214 value = context->regs[Z80_L];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
215 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
216 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
217 case 'i':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
218 if(param[1] == 'x') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
219 if (param[2] == 'h') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
220 value = context->regs[Z80_IXH];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
221 } else if(param[2] == 'l') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
222 value = context->regs[Z80_IXL];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
223 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
224 value = context->regs[Z80_IXH] << 8;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
225 value |= context->regs[Z80_IXL];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
226 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
227 } else if(param[1] == 'y') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
228 if (param[2] == 'h') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
229 value = context->regs[Z80_IYH];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
230 } else if(param[2] == 'l') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
231 value = context->regs[Z80_IYL];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
232 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
233 value = context->regs[Z80_IYH] << 8;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
234 value |= context->regs[Z80_IYL];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
235 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
236 } else if(param[1] == 'n') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
237 value = context->int_cycle;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
238 } else if(param[1] == 'f' && param[2] == 'f' && param[3] == '1') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
239 value = context->iff1;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
240 } else if(param[1] == 'f' && param[2] == 'f' && param[3] == '2') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
241 value = context->iff2;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
242 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
243 value = context->im;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
244 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
245 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
246 case 's':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
247 if (param[1] == 'p') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
248 value = context->sp;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
249 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
250 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
251 case '0':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
252 if (param[1] == 'x') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
253 uint16_t p_addr = strtol(param+2, NULL, 16);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
254 if (p_addr < 0x4000) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
255 value = z80_ram[p_addr & 0x1FFF];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
256 } else if(p_addr >= 0x8000) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
257 uint32_t v_addr = context->bank_reg << 15;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
258 v_addr += p_addr & 0x7FFF;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
259 if (v_addr < 0x400000) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
260 value = cart[v_addr/2];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
261 } else if(v_addr > 0xE00000) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
262 value = ram[(v_addr & 0xFFFF)/2];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
263 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
264 if (v_addr & 1) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
265 value &= 0xFF;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
266 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
267 value >>= 8;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
268 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
269 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
270 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
271 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
272 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
273 printf(format, param, value);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
274 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
275
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
276 z80_context * zdebugger(z80_context * context, uint16_t address)
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
277 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
278 static char last_cmd[1024];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
279 char input_buf[1024];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
280 static uint16_t branch_t;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
281 static uint16_t branch_f;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
282 z80inst inst;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
283 //Check if this is a user set breakpoint, or just a temporary one
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
284 bp_def ** this_bp = find_breakpoint(&zbreakpoints, address);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
285 if (*this_bp) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
286 printf("Z80 Breakpoint %d hit\n", (*this_bp)->index);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
287 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
288 zremove_breakpoint(context, address);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
289 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
290 uint8_t * pc;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
291 if (address < 0x4000) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
292 pc = z80_ram + (address & 0x1FFF);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
293 } else if (address >= 0x8000) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
294 if (context->bank_reg < (0x400000 >> 15)) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
295 fprintf(stderr, "Entered Z80 debugger in banked memory address %X, which is not yet supported\n", address);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
296 exit(1);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
297 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
298 fprintf(stderr, "Entered Z80 debugger in banked memory address %X, but the bank is not pointed to a cartridge address\n", address);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
299 exit(1);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
300 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
301 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
302 fprintf(stderr, "Entered Z80 debugger at address %X\n", address);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
303 exit(1);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
304 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
305 for (disp_def * cur = zdisplays; cur; cur = cur->next) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
306 zdebugger_print(context, cur->format_char, cur->param);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
307 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
308 uint8_t * after_pc = z80_decode(pc, &inst);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
309 z80_disasm(&inst, input_buf, address);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
310 printf("%X:\t%s\n", address, input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
311 uint16_t after = address + (after_pc-pc);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
312 int debugging = 1;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
313 while(debugging) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
314 fputs(">", stdout);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
315 if (!fgets(input_buf, sizeof(input_buf), stdin)) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
316 fputs("fgets failed", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
317 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
318 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
319 strip_nl(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
320 //hitting enter repeats last command
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
321 if (input_buf[0]) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
322 strcpy(last_cmd, input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
323 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
324 strcpy(input_buf, last_cmd);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
325 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
326 char * param;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
327 char format[8];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
328 uint32_t value;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
329 bp_def * new_bp;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
330 switch(input_buf[0])
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
331 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
332 case 'a':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
333 param = find_param(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
334 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
335 fputs("a command requires a parameter\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
336 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
337 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
338 value = strtol(param, NULL, 16);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
339 zinsert_breakpoint(context, value, (uint8_t *)zdebugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
340 debugging = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
341 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
342 case 'b':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
343 param = find_param(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
344 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
345 fputs("b command requires a parameter\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
346 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
347 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
348 value = strtol(param, NULL, 16);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
349 zinsert_breakpoint(context, value, (uint8_t *)zdebugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
350 new_bp = malloc(sizeof(bp_def));
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
351 new_bp->next = zbreakpoints;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
352 new_bp->address = value;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
353 new_bp->index = zbp_index++;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
354 zbreakpoints = new_bp;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
355 printf("Z80 Breakpoint %d set at %X\n", new_bp->index, value);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
356 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
357 case 'c':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
358 puts("Continuing");
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
359 debugging = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
360 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
361 case 'd':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
362 if (input_buf[1] == 'i') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
363 char format_char = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
364 for(int i = 2; input_buf[i] != 0 && input_buf[i] != ' '; i++) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
365 if (input_buf[i] == '/') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
366 format_char = input_buf[i+1];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
367 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
368 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
369 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
370 param = find_param(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
371 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
372 fputs("display command requires a parameter\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
373 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
374 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
375 zdebugger_print(context, format_char, param);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
376 add_display(&zdisplays, &zdisp_index, format_char, param);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
377 } else if (input_buf[1] == 'e' || input_buf[1] == ' ') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
378 param = find_param(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
379 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
380 fputs("delete command requires a parameter\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
381 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
382 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
383 if (param[0] >= '0' && param[0] <= '9') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
384 value = atoi(param);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
385 this_bp = find_breakpoint_idx(&zbreakpoints, value);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
386 if (!*this_bp) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
387 fprintf(stderr, "Breakpoint %d does not exist\n", value);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
388 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
389 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
390 new_bp = *this_bp;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
391 zremove_breakpoint(context, new_bp->address);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
392 *this_bp = new_bp->next;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
393 free(new_bp);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
394 } else if (param[0] == 'd') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
395 param = find_param(param);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
396 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
397 fputs("delete display command requires a parameter\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
398 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
399 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
400 remove_display(&zdisplays, atoi(param));
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
401 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
402 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
403 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
404 case 'n':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
405 //TODO: Handle conditional branch instructions
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
406 if (inst.op == Z80_JP) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
407 if (inst.addr_mode == Z80_IMMED) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
408 after = inst.immed;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
409 } else if (inst.ea_reg == Z80_HL) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
410 after = context->regs[Z80_H] << 8 | context->regs[Z80_L];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
411 } else if (inst.ea_reg == Z80_IX) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
412 after = context->regs[Z80_IXH] << 8 | context->regs[Z80_IXL];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
413 } else if (inst.ea_reg == Z80_IY) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
414 after = context->regs[Z80_IYH] << 8 | context->regs[Z80_IYL];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
415 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
416 } else if(inst.op == Z80_JR) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
417 after += inst.immed;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
418 } else if(inst.op == Z80_RET) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
419 if (context->sp < 0x4000) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
420 after = z80_ram[context->sp & 0x1FFF] | z80_ram[(context->sp+1) & 0x1FFF] << 8;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
421 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
422 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
423 zinsert_breakpoint(context, after, (uint8_t *)zdebugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
424 debugging = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
425 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
426 case 'p':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
427 param = find_param(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
428 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
429 fputs("p command requires a parameter\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
430 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
431 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
432 zdebugger_print(context, input_buf[1] == '/' ? input_buf[2] : 0, param);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
433 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
434 case 'q':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
435 puts("Quitting");
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
436 exit(0);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
437 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
438 case 's': {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
439 param = find_param(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
440 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
441 fputs("s command requires a file name\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
442 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
443 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
444 FILE * f = fopen(param, "wb");
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
445 if (f) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
446 if(fwrite(z80_ram, 1, sizeof(z80_ram), f) != sizeof(z80_ram)) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
447 fputs("Error writing file\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
448 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
449 fclose(f);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
450 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
451 fprintf(stderr, "Could not open %s for writing\n", param);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
452 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
453 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
454 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
455 default:
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
456 fprintf(stderr, "Unrecognized debugger command %s\n", input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
457 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
458 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
459 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
460 return context;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
461 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
462
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
463 m68k_context * debugger(m68k_context * context, uint32_t address)
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
464 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
465 static char last_cmd[1024];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
466 char input_buf[1024];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
467 static uint32_t branch_t;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
468 static uint32_t branch_f;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
469 m68kinst inst;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
470 //probably not necessary, but let's play it safe
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
471 address &= 0xFFFFFF;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
472 if (address == branch_t) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
473 bp_def ** f_bp = find_breakpoint(&breakpoints, branch_f);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
474 if (!*f_bp) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
475 remove_breakpoint(context, branch_f);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
476 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
477 branch_t = branch_f = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
478 } else if(address == branch_f) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
479 bp_def ** t_bp = find_breakpoint(&breakpoints, branch_t);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
480 if (!*t_bp) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
481 remove_breakpoint(context, branch_t);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
482 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
483 branch_t = branch_f = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
484 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
485 //Check if this is a user set breakpoint, or just a temporary one
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
486 bp_def ** this_bp = find_breakpoint(&breakpoints, address);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
487 if (*this_bp) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
488 printf("68K Breakpoint %d hit\n", (*this_bp)->index);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
489 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
490 remove_breakpoint(context, address);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
491 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
492 uint16_t * pc;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
493 if (address < 0x400000) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
494 pc = cart + address/2;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
495 } else if(address > 0xE00000) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
496 pc = ram + (address & 0xFFFF)/2;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
497 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
498 fprintf(stderr, "Entered 68K debugger at address %X\n", address);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
499 exit(1);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
500 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
501 uint16_t * after_pc = m68k_decode(pc, &inst, address);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
502 m68k_disasm(&inst, input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
503 printf("%X: %s\n", address, input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
504 uint32_t after = address + (after_pc-pc)*2;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
505 int debugging = 1;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
506 while (debugging) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
507 fputs(">", stdout);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
508 if (!fgets(input_buf, sizeof(input_buf), stdin)) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
509 fputs("fgets failed", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
510 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
511 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
512 strip_nl(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
513 //hitting enter repeats last command
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
514 if (input_buf[0]) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
515 strcpy(last_cmd, input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
516 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
517 strcpy(input_buf, last_cmd);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
518 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
519 char * param;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
520 char format[8];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
521 uint32_t value;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
522 bp_def * new_bp;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
523 switch(input_buf[0])
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
524 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
525 case 'c':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
526 puts("Continuing");
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
527 debugging = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
528 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
529 case 'b':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
530 param = find_param(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
531 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
532 fputs("b command requires a parameter\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
533 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
534 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
535 value = strtol(param, NULL, 16);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
536 insert_breakpoint(context, value, (uint8_t *)debugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
537 new_bp = malloc(sizeof(bp_def));
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
538 new_bp->next = breakpoints;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
539 new_bp->address = value;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
540 new_bp->index = bp_index++;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
541 breakpoints = new_bp;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
542 printf("68K Breakpoint %d set at %X\n", new_bp->index, value);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
543 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
544 case 'a':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
545 param = find_param(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
546 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
547 fputs("a command requires a parameter\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
548 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
549 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
550 value = strtol(param, NULL, 16);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
551 insert_breakpoint(context, value, (uint8_t *)debugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
552 debugging = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
553 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
554 case 'd':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
555 param = find_param(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
556 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
557 fputs("d command requires a parameter\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
558 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
559 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
560 value = atoi(param);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
561 this_bp = find_breakpoint_idx(&breakpoints, value);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
562 if (!*this_bp) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
563 fprintf(stderr, "Breakpoint %d does not exist\n", value);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
564 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
565 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
566 new_bp = *this_bp;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
567 *this_bp = (*this_bp)->next;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
568 free(new_bp);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
569 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
570 case 'p':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
571 strcpy(format, "%s: %d\n");
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
572 if (input_buf[1] == '/') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
573 switch (input_buf[2])
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
574 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
575 case 'x':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
576 case 'X':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
577 case 'd':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
578 case 'c':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
579 format[5] = input_buf[2];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
580 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
581 default:
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
582 fprintf(stderr, "Unrecognized format character: %c\n", input_buf[2]);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
583 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
584 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
585 param = find_param(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
586 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
587 fputs("p command requires a parameter\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
588 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
589 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
590 if (param[0] == 'd' && param[1] >= '0' && param[1] <= '7') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
591 value = context->dregs[param[1]-'0'];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
592 } else if (param[0] == 'a' && param[1] >= '0' && param[1] <= '7') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
593 value = context->aregs[param[1]-'0'];
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
594 } else if (param[0] == 'S' && param[1] == 'R') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
595 value = (context->status << 8);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
596 for (int flag = 0; flag < 5; flag++) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
597 value |= context->flags[flag] << (4-flag);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
598 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
599 } else if(param[0] == 'c') {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
600 value = context->current_cycle;
622
b76d2a628ab9 Partially working switch to having a vcounter and hslot counter in the context rather than trying to derive them from the cycle count. This should allow for more accurate handling of mid screen mode switches. Interrupt timing is broken currently though
Michael Pavone <pavone@retrodev.com>
parents: 525
diff changeset
601 } else if ((param[0] == '0' && param[1] == 'x') || param[0] == '$') {
b76d2a628ab9 Partially working switch to having a vcounter and hslot counter in the context rather than trying to derive them from the cycle count. This should allow for more accurate handling of mid screen mode switches. Interrupt timing is broken currently though
Michael Pavone <pavone@retrodev.com>
parents: 525
diff changeset
602 uint32_t p_addr = strtol(param+(param[0] == '0' ? 2 : 1), NULL, 16);
b76d2a628ab9 Partially working switch to having a vcounter and hslot counter in the context rather than trying to derive them from the cycle count. This should allow for more accurate handling of mid screen mode switches. Interrupt timing is broken currently though
Michael Pavone <pavone@retrodev.com>
parents: 525
diff changeset
603 if ((p_addr & 0xFFFFFF) == 0xC00004) {
b76d2a628ab9 Partially working switch to having a vcounter and hslot counter in the context rather than trying to derive them from the cycle count. This should allow for more accurate handling of mid screen mode switches. Interrupt timing is broken currently though
Michael Pavone <pavone@retrodev.com>
parents: 525
diff changeset
604 genesis_context * gen = context->system;
b76d2a628ab9 Partially working switch to having a vcounter and hslot counter in the context rather than trying to derive them from the cycle count. This should allow for more accurate handling of mid screen mode switches. Interrupt timing is broken currently though
Michael Pavone <pavone@retrodev.com>
parents: 525
diff changeset
605 value = vdp_hv_counter_read(gen->vdp);
b76d2a628ab9 Partially working switch to having a vcounter and hslot counter in the context rather than trying to derive them from the cycle count. This should allow for more accurate handling of mid screen mode switches. Interrupt timing is broken currently though
Michael Pavone <pavone@retrodev.com>
parents: 525
diff changeset
606 } else {
b76d2a628ab9 Partially working switch to having a vcounter and hslot counter in the context rather than trying to derive them from the cycle count. This should allow for more accurate handling of mid screen mode switches. Interrupt timing is broken currently though
Michael Pavone <pavone@retrodev.com>
parents: 525
diff changeset
607 value = read_dma_value(p_addr/2);
b76d2a628ab9 Partially working switch to having a vcounter and hslot counter in the context rather than trying to derive them from the cycle count. This should allow for more accurate handling of mid screen mode switches. Interrupt timing is broken currently though
Michael Pavone <pavone@retrodev.com>
parents: 525
diff changeset
608 }
524
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
609 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
610 fprintf(stderr, "Unrecognized parameter to p: %s\n", param);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
611 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
612 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
613 printf(format, param, value);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
614 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
615 case 'n':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
616 if (inst.op == M68K_RTS) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
617 after = (read_dma_value(context->aregs[7]/2) << 16) | read_dma_value(context->aregs[7]/2 + 1);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
618 } else if (inst.op == M68K_RTE || inst.op == M68K_RTR) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
619 after = (read_dma_value((context->aregs[7]+2)/2) << 16) | read_dma_value((context->aregs[7]+2)/2 + 1);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
620 } else if(m68k_is_noncall_branch(&inst)) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
621 if (inst.op == M68K_BCC && inst.extra.cond != COND_TRUE) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
622 branch_f = after;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
623 branch_t = m68k_branch_target(&inst, context->dregs, context->aregs);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
624 insert_breakpoint(context, branch_t, (uint8_t *)debugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
625 } else if(inst.op == M68K_DBCC) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
626 if ( inst.extra.cond == COND_FALSE) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
627 if (context->dregs[inst.dst.params.regs.pri] & 0xFFFF) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
628 after = m68k_branch_target(&inst, context->dregs, context->aregs);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
629 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
630 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
631 branch_t = after;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
632 branch_f = m68k_branch_target(&inst, context->dregs, context->aregs);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
633 insert_breakpoint(context, branch_f, (uint8_t *)debugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
634 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
635 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
636 after = m68k_branch_target(&inst, context->dregs, context->aregs);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
637 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
638 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
639 insert_breakpoint(context, after, (uint8_t *)debugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
640 debugging = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
641 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
642 case 'o':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
643 if (inst.op == M68K_RTS) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
644 after = (read_dma_value(context->aregs[7]/2) << 16) | read_dma_value(context->aregs[7]/2 + 1);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
645 } else if (inst.op == M68K_RTE || inst.op == M68K_RTR) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
646 after = (read_dma_value((context->aregs[7]+2)/2) << 16) | read_dma_value((context->aregs[7]+2)/2 + 1);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
647 } else if(m68k_is_noncall_branch(&inst)) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
648 if (inst.op == M68K_BCC && inst.extra.cond != COND_TRUE) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
649 branch_t = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
650 if (branch_t < after) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
651 branch_t = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
652 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
653 branch_f = after;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
654 insert_breakpoint(context, branch_t, (uint8_t *)debugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
655 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
656 } else if(inst.op == M68K_DBCC) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
657 uint32_t target = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
658 if (target > after) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
659 if (inst.extra.cond == COND_FALSE) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
660 after = target;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
661 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
662 branch_f = target;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
663 branch_t = after;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
664 insert_breakpoint(context, branch_f, (uint8_t *)debugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
665 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
666 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
667 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
668 after = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
669 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
670 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
671 insert_breakpoint(context, after, (uint8_t *)debugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
672 debugging = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
673 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
674 case 's':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
675 if (inst.op == M68K_RTS) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
676 after = (read_dma_value(context->aregs[7]/2) << 16) | read_dma_value(context->aregs[7]/2 + 1);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
677 } else if (inst.op == M68K_RTE || inst.op == M68K_RTR) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
678 after = (read_dma_value((context->aregs[7]+2)/2) << 16) | read_dma_value((context->aregs[7]+2)/2 + 1);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
679 } else if(m68k_is_branch(&inst)) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
680 if (inst.op == M68K_BCC && inst.extra.cond != COND_TRUE) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
681 branch_f = after;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
682 branch_t = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
683 insert_breakpoint(context, branch_t, (uint8_t *)debugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
684 } else if(inst.op == M68K_DBCC && inst.extra.cond != COND_FALSE) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
685 branch_t = after;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
686 branch_f = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
687 insert_breakpoint(context, branch_f, (uint8_t *)debugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
688 } else {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
689 after = m68k_branch_target(&inst, context->dregs, context->aregs) & 0xFFFFFF;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
690 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
691 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
692 insert_breakpoint(context, after, (uint8_t *)debugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
693 debugging = 0;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
694 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
695 case 'v': {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
696 genesis_context * gen = context->system;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
697 //VDP debug commands
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
698 switch(input_buf[1])
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
699 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
700 case 's':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
701 vdp_print_sprite_table(gen->vdp);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
702 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
703 case 'r':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
704 vdp_print_reg_explain(gen->vdp);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
705 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
706 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
707 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
708 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
709 case 'z': {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
710 genesis_context * gen = context->system;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
711 //Z80 debug commands
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
712 switch(input_buf[1])
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
713 {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
714 case 'b':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
715 param = find_param(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
716 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
717 fputs("zb command requires a parameter\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
718 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
719 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
720 value = strtol(param, NULL, 16);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
721 zinsert_breakpoint(gen->z80, value, (uint8_t *)zdebugger);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
722 new_bp = malloc(sizeof(bp_def));
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
723 new_bp->next = zbreakpoints;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
724 new_bp->address = value;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
725 new_bp->index = zbp_index++;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
726 zbreakpoints = new_bp;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
727 printf("Z80 Breakpoint %d set at %X\n", new_bp->index, value);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
728 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
729 case 'p':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
730 param = find_param(input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
731 if (!param) {
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
732 fputs("zp command requires a parameter\n", stderr);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
733 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
734 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
735 zdebugger_print(gen->z80, input_buf[2] == '/' ? input_buf[3] : 0, param);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
736 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
737 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
738 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
739 case 'q':
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
740 puts("Quitting");
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
741 exit(0);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
742 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
743 default:
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
744 fprintf(stderr, "Unrecognized debugger command %s\n", input_buf);
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
745 break;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
746 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
747 }
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
748 return context;
fb39534b6604 Move debugging code outside of main source file
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
749 }