1 |
/** Z80: portable Z80 emulator *******************************/ |
2 |
/** **/ |
3 |
/** ConDebug.c **/ |
4 |
/** **/ |
5 |
/** This file contains a console version of the built-in **/ |
6 |
/** debugger, using EMULib's Console.c. When -DCONDEBUG is **/ |
7 |
/** ommitted, ConDebug.c just includes the default command **/ |
8 |
/** line based debugger (Debug.c). **/ |
9 |
/** **/ |
10 |
/** Copyright (C) Marat Fayzullin 2005-2007 **/ |
11 |
/** You are not allowed to distribute this software **/ |
12 |
/** commercially. Please, notify me, if you make any **/ |
13 |
/** changes to this file. **/ |
14 |
/*************************************************************/ |
15 |
#ifdef DEBUG |
16 |
|
17 |
#ifndef CONDEBUG |
18 |
/** Normal DebugZ80() ****************************************/ |
19 |
/** When CONDEBUG #undefined, we use plain command line. **/ |
20 |
/*************************************************************/ |
21 |
#include "Debug.c" |
22 |
|
23 |
#else |
24 |
/** Console DebugZ80() ***************************************/ |
25 |
/** When CONDEBUG #defined, we use EMULib console. **/ |
26 |
/*************************************************************/ |
27 |
|
28 |
#include "Z80.h" |
29 |
#include "Console.h" |
30 |
#include <stdlib.h> |
31 |
|
32 |
#define DebugZ80 OriginalDebugZ80 |
33 |
#include "Debug.c" |
34 |
#undef DebugZ80 |
35 |
|
36 |
#define CLR_BACK PIXEL(255,255,255) |
37 |
#define CLR_TEXT PIXEL(0,0,0) |
38 |
#define CLR_DIALOG PIXEL(0,100,0) |
39 |
#define CLR_PC PIXEL(255,0,0) |
40 |
#define CLR_SP PIXEL(0,0,100) |
41 |
|
42 |
static byte ChrDump(byte C) |
43 |
{ |
44 |
return((C>=32)&&(C<128)? C:'.'); |
45 |
} |
46 |
|
47 |
/** DebugZ80() ***********************************************/ |
48 |
/** This function should exist if DEBUG is #defined. When **/ |
49 |
/** Trace!=0, it is called after each command executed by **/ |
50 |
/** the CPU, and given the Z80 registers. **/ |
51 |
/*************************************************************/ |
52 |
byte DebugZ80(Z80 *R) |
53 |
{ |
54 |
char S[1024]; |
55 |
word A,Addr,ABuf[20]; |
56 |
int J,I,K,X,Y,MemoryDump,DrawWindow,ExitNow; |
57 |
|
58 |
/* If we don't have enough screen estate... */ |
59 |
if((VideoW<32*8)||(VideoH<23*8)) |
60 |
{ |
61 |
/* Show warning message */ |
62 |
CONMsg( |
63 |
-1,-1,-1,-1,PIXEL(255,255,255),PIXEL(255,0,0), |
64 |
"Error","Screen is\0too small!\0\0" |
65 |
); |
66 |
/* Continue emulation */ |
67 |
return(1); |
68 |
} |
69 |
|
70 |
X = ((VideoW>>3)-32)>>1; |
71 |
Y = ((VideoH>>3)-23)>>1; |
72 |
Addr = R->PC.W; |
73 |
A = ~Addr; |
74 |
K = 0; |
75 |
|
76 |
for(DrawWindow=1,MemoryDump=ExitNow=0;!ExitNow&&VideoImg;) |
77 |
{ |
78 |
if(DrawWindow) |
79 |
{ |
80 |
CONWindow(X,Y,32,23,CLR_TEXT,CLR_BACK,"Z80 Debugger"); |
81 |
|
82 |
sprintf(S,"PC %04X",R->PC.W); |
83 |
CONSetColor(CLR_BACK,CLR_PC); |
84 |
CONPrint(X+24,Y+13,S); |
85 |
sprintf(S,"SP %04X",R->SP.W); |
86 |
CONSetColor(CLR_BACK,CLR_SP); |
87 |
CONPrint(X+24,Y+14,S); |
88 |
|
89 |
CONSetColor(CLR_TEXT,CLR_BACK); |
90 |
sprintf(S, |
91 |
" %c%c%c%c%c%c\n\n" |
92 |
"AF %04X\nBC %04X\nDE %04X\nHL %04X\nIX %04X\nIY %04X\n\n" |
93 |
"IR %02X%02X", |
94 |
R->AF.B.l&0x80? 'S':'.',R->AF.B.l&0x40? 'Z':'.',R->AF.B.l&0x10? 'H':'.', |
95 |
R->AF.B.l&0x04? 'P':'.',R->AF.B.l&0x02? 'N':'.',R->AF.B.l&0x01? 'C':'.', |
96 |
R->AF.W,R->BC.W,R->DE.W,R->HL.W,R->IX.W,R->IY.W,R->I,R->R |
97 |
); |
98 |
CONPrint(X+24,Y+2,S); |
99 |
sprintf(S, |
100 |
"%s %s", |
101 |
R->IFF&0x04? "IM2":R->IFF&0x02? "IM1":"IM0", |
102 |
R->IFF&0x01? "EI":"DI" |
103 |
); |
104 |
CONPrint(X+25,Y+21,S); |
105 |
DrawWindow=0; |
106 |
A=~Addr; |
107 |
} |
108 |
|
109 |
/* If top address has changed... */ |
110 |
if(A!=Addr) |
111 |
{ |
112 |
/* Clear display */ |
113 |
CONBox((X+1)<<3,(Y+2)<<3,23*8,20*8,CLR_BACK); |
114 |
|
115 |
if(MemoryDump) |
116 |
{ |
117 |
/* Draw memory dump */ |
118 |
for(J=0,A=Addr;J<20;J++,A+=4) |
119 |
{ |
120 |
if(A==R->PC.W) CONSetColor(CLR_BACK,CLR_PC); |
121 |
else if(A==R->SP.W) CONSetColor(CLR_BACK,CLR_SP); |
122 |
else CONSetColor(CLR_TEXT,CLR_BACK); |
123 |
sprintf(S,"%04X%c",A,A==R->PC.W? CON_MORE:A==R->SP.W? CON_LESS:':'); |
124 |
CONPrint(X+1,Y+J+2,S); |
125 |
|
126 |
CONSetColor(CLR_TEXT,CLR_BACK); |
127 |
sprintf(S, |
128 |
"%02X %02X %02X %02X %c%c%c%c", |
129 |
RdZ80(A),RdZ80(A+1),RdZ80(A+2),RdZ80(A+3), |
130 |
ChrDump(RdZ80(A)),ChrDump(RdZ80(A+1)), |
131 |
ChrDump(RdZ80(A+2)),ChrDump(RdZ80(A+3)) |
132 |
); |
133 |
CONPrint(X+7,Y+J+2,S); |
134 |
} |
135 |
} |
136 |
else |
137 |
{ |
138 |
/* Draw listing */ |
139 |
for(J=0,A=Addr;J<20;J++) |
140 |
{ |
141 |
if(A==R->PC.W) CONSetColor(CLR_BACK,CLR_PC); |
142 |
else if(A==R->SP.W) CONSetColor(CLR_BACK,CLR_SP); |
143 |
else CONSetColor(CLR_TEXT,CLR_BACK); |
144 |
sprintf(S,"%04X%c",A,A==R->PC.W? CON_MORE:A==R->SP.W? CON_LESS:':'); |
145 |
CONPrint(X+1,Y+J+2,S); |
146 |
|
147 |
ABuf[J]=A; |
148 |
A+=DAsm(S,A); |
149 |
|
150 |
CONSetColor(CLR_TEXT,CLR_BACK); |
151 |
CONPrintN(X+7,Y+J+2,S,23); |
152 |
} |
153 |
} |
154 |
|
155 |
/* Display redrawn */ |
156 |
A=Addr; |
157 |
} |
158 |
|
159 |
/* Draw pointer */ |
160 |
CONChar(X+6,Y+K+2,CON_ARROW); |
161 |
|
162 |
/* Show screen buffer */ |
163 |
ShowVideo(); |
164 |
|
165 |
/* Get key code */ |
166 |
I=WaitKey(); |
167 |
|
168 |
/* Clear pointer */ |
169 |
CONChar(X+6,Y+K+2,' '); |
170 |
|
171 |
/* Get and process key code */ |
172 |
switch(I) |
173 |
{ |
174 |
case 'H': |
175 |
CONMsg( |
176 |
-1,-1,-1,-1, |
177 |
CLR_BACK,CLR_DIALOG, |
178 |
"Debugger Help", |
179 |
"ENTER - Execute next opcode\0" |
180 |
" UP - Previous opcode\0" |
181 |
" DOWN - Next opcode\0" |
182 |
" LEFT - Page up\0" |
183 |
"RIGHT - Page down\0" |
184 |
" H - This help page\0" |
185 |
" G - Go to address\0" |
186 |
" D - Disassembler view\0" |
187 |
" M - Memory dump view\0" |
188 |
" S - Show stack\0" |
189 |
" J - Jump to cursor\0" |
190 |
" R - Run to cursor\0" |
191 |
" C - Continue execution\0" |
192 |
" Q - Quit emulator\0" |
193 |
); |
194 |
DrawWindow=1; |
195 |
break; |
196 |
case CON_UP: |
197 |
if(K) --K; |
198 |
else |
199 |
if(MemoryDump) Addr-=4; |
200 |
else for(--Addr;Addr+DAsm(S,Addr)>A;--Addr); |
201 |
break; |
202 |
case CON_DOWN: |
203 |
if(K<19) ++K; |
204 |
else |
205 |
if(MemoryDump) Addr+=4; |
206 |
else Addr+=DAsm(S,Addr); |
207 |
break; |
208 |
case CON_LEFT: |
209 |
if(MemoryDump) |
210 |
Addr-=4*20; |
211 |
else |
212 |
{ |
213 |
for(I=20,Addr=~A;(Addr>A)||((A^Addr)&~Addr&0x8000);++I) |
214 |
for(J=0,Addr=A-I;J<20;++J) Addr+=DAsm(S,Addr); |
215 |
Addr=A-I+1; |
216 |
} |
217 |
break; |
218 |
case CON_RIGHT: |
219 |
if(MemoryDump) |
220 |
Addr+=4*20; |
221 |
else |
222 |
for(J=0;J<20;++J) Addr+=DAsm(S,Addr); |
223 |
break; |
224 |
case CON_OK: |
225 |
ExitNow=1; |
226 |
break; |
227 |
case 'Q': |
228 |
return(0); |
229 |
case CON_EXIT: |
230 |
case 'C': |
231 |
R->Trap=0xFFFF; |
232 |
R->Trace=0; |
233 |
ExitNow=1; |
234 |
break; |
235 |
case 'R': |
236 |
R->Trap=ABuf[K]; |
237 |
R->Trace=0; |
238 |
ExitNow=1; |
239 |
break; |
240 |
case 'M': |
241 |
MemoryDump=1; |
242 |
A=~Addr; |
243 |
break; |
244 |
case 'S': |
245 |
MemoryDump=1; |
246 |
Addr=R->SP.W; |
247 |
K=0; |
248 |
A=~Addr; |
249 |
break; |
250 |
case 'D': |
251 |
MemoryDump=0; |
252 |
A=~Addr; |
253 |
break; |
254 |
case 'G': |
255 |
if(CONInput(-1,-1,CLR_BACK,CLR_DIALOG,"Go to Address:",S,5|CON_HEX)) |
256 |
{ Addr=strtol(S,0,16);K=0; } |
257 |
DrawWindow=1; |
258 |
break; |
259 |
case 'J': |
260 |
R->PC.W=ABuf[K]; |
261 |
A=~Addr; |
262 |
break; |
263 |
} |
264 |
} |
265 |
|
266 |
/* Continue emulation */ |
267 |
return(1); |
268 |
} |
269 |
|
270 |
#endif /* CONDEBUG */ |
271 |
#endif /* DEBUG */ |