Debian Codegraph @

mednafen/vb/debug.cpp

raw symbols SCIP
1 /* Mednafen - Multi-system Emulator
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include "vb.h"
19 #include <mednafen/hw_cpu/v810/v810_cpuD.h>
20 #include <string.h>
21 #include <trio/trio.h>
22 #include <stdarg.h>
23 #include <iconv.h>
24
25 #include "debug.h"
26 #include "timer.h"
27 //#include "input.h"
28 #include "vip.h"
29 #include "vsu.h"
30 #include "timer.h"
31
32 extern V810 *VB_V810;
33 extern VSU *VB_VSU;
34
35 static void RedoCPUHook(void);
36 static void (*CPUHook)(uint32, bool bpoint) = NULL;
37 static bool CPUHookContinuous = false;
38 static void (*LogFunc)(const char *, const char *);
39 bool VB_LoggingOn = FALSE;
40
41 typedef struct __VB_BPOINT {
42 uint32 A[2];
43 int type;
44 bool logical;
45 } VB_BPOINT;
46
47 static std::vector<VB_BPOINT> BreakPointsPC, BreakPointsRead, BreakPointsWrite;
48 static bool FoundBPoint = 0;
49
50 struct BTEntry
51 {
52 uint32 from;
53 uint32 to;
54 uint32 branch_count;
55 uint32 ecode;
56 bool valid;
57 };
58
59 #define NUMBT 24
60 static BTEntry BTEntries[NUMBT];
61 static int BTIndex;
62 static bool BTEnabled;
63
64 static void AddBranchTrace(uint32 from, uint32 to, uint32 ecode)
65 {
66 BTEntry *prevbt = &BTEntries[(BTIndex + NUMBT - 1) % NUMBT];
67
68 //if(BTEntries[(BTIndex - 1) & 0xF] == PC) return;
69
70 if(prevbt->from == from && prevbt->to == to && prevbt->ecode == ecode && prevbt->branch_count < 0xFFFFFFFF && prevbt->valid)
71 prevbt->branch_count++;
72 else
73 {
74 BTEntries[BTIndex].from = from;
75 BTEntries[BTIndex].to = to;
76 BTEntries[BTIndex].ecode = ecode;
77 BTEntries[BTIndex].branch_count = 1;
78 BTEntries[BTIndex].valid = true;
79
80 BTIndex = (BTIndex + 1) % NUMBT;
81 }
82 }
83
84 void VBDBG_EnableBranchTrace(bool enable)
85 {
86 BTEnabled = enable;
87 if(!enable)
88 {
89 BTIndex = 0;
90 memset(BTEntries, 0, sizeof(BTEntries));
91 }
92
93 RedoCPUHook();
94 }
95
96 std::vector<BranchTraceResult> VBDBG_GetBranchTrace(void)
97 {
98 BranchTraceResult tmp;
99 std::vector<BranchTraceResult> ret;
100
101 for(int x = 0; x < NUMBT; x++)
102 {
103 const BTEntry *bt = &BTEntries[(x + BTIndex) % NUMBT];
104
105 if(!bt->valid)
106 continue;
107
108 tmp.count = bt->branch_count;
109 trio_snprintf(tmp.from, sizeof(tmp.from), "%08x", bt->from);
110 trio_snprintf(tmp.to, sizeof(tmp.to), "%08x", bt->to);
111
112 tmp.code[0] = 0;
113
114 switch(bt->ecode)
115 {
116 case 0: break;
117 default: trio_snprintf(tmp.code, sizeof(tmp.code), "e");
118 break;
119
120 case 0xFFF0: // Reset
121 trio_snprintf(tmp.code, sizeof(tmp.code), "R");
122 break;
123
124 case 0xFFD0: // NMI
125 trio_snprintf(tmp.code, sizeof(tmp.code), "NMI");
126 break;
127
128 case 0xFFC0: // Address trap
129 trio_snprintf(tmp.code, sizeof(tmp.code), "ADTR");
130 break;
131
132 case 0xFFA0 ... 0xFFBF: // TRAP
133 trio_snprintf(tmp.code, sizeof(tmp.code), "TRAP");
134 break;
135
136 case 0xFF90: // Illegal/invalid instruction code
137 trio_snprintf(tmp.code, sizeof(tmp.code), "ILL");
138 break;
139
140 case 0xFF80: // Zero division
141 trio_snprintf(tmp.code, sizeof(tmp.code), "ZD");
142 break;
143
144 case 0xFF70:
145 trio_snprintf(tmp.code, sizeof(tmp.code), "FIV"); // FIV
146 break;
147
148 case 0xFF68:
149 trio_snprintf(tmp.code, sizeof(tmp.code), "FZD"); // FZD
150 break;
151
152 case 0xFF64:
153 trio_snprintf(tmp.code, sizeof(tmp.code), "FOV"); // FOV
154 break;
155
156 case 0xFF62:
157 trio_snprintf(tmp.code, sizeof(tmp.code), "FUD"); // FUD
158 break;
159
160 case 0xFF61:
161 trio_snprintf(tmp.code, sizeof(tmp.code), "FPR"); // FPR
162 break;
163
164 case 0xFF60:
165 trio_snprintf(tmp.code, sizeof(tmp.code), "FRO"); // FRO
166 break;
167
168 case 0xFE00 ... 0xFEFF:
169 trio_snprintf(tmp.code, sizeof(tmp.code), "INT%d", (bt->ecode >> 4) & 0xF);
170 break;
171 }
172
173 ret.push_back(tmp);
174 }
175 return(ret);
176 }
177
178
179 void VBDBG_CheckBP(int type, uint32 address, uint32 value, unsigned int len)
180 {
181 std::vector<VB_BPOINT>::iterator bpit, bpit_end;
182
183 if(type == BPOINT_READ || type == BPOINT_IO_READ)
184 {
185 bpit = BreakPointsRead.begin();
186 bpit_end = BreakPointsRead.end();
187 }
188 else if(type == BPOINT_WRITE || type == BPOINT_IO_WRITE)
189 {
190 bpit = BreakPointsWrite.begin();
191 bpit_end = BreakPointsWrite.end();
192 }
193 else
194 return;
195
196 for(; bpit != bpit_end; bpit++)
197 {
198 uint32 tmp_address = address;
199 uint32 tmp_len = len;
200
201 while(tmp_len--)
202 {
203 if(tmp_address >= bpit->A[0] && tmp_address <= bpit->A[1])
204 {
205 FoundBPoint = TRUE;
206 break;
207 }
208 tmp_address++;
209 }
210 }
211 }
212
213 static uint16 MDFN_FASTCALL MemPeek8(v810_timestamp_t timestamp, uint32 A)
214 {
215 uint8 ret;
216
217 // TODO: VB_InDebugPeek(implement elsewhere)
218 VB_InDebugPeek++;
219 ret = MemRead8(timestamp, A);
220 VB_InDebugPeek--;
221
222 return(ret);
223 }
224
225 static uint16 MDFN_FASTCALL MemPeek16(v810_timestamp_t timestamp, uint32 A)
226 {
227 uint16 ret;
228
229 // TODO: VB_InDebugPeek(implement elsewhere)
230 VB_InDebugPeek++;
231 ret = MemRead16(timestamp, A);
232 VB_InDebugPeek--;
233
234 return(ret);
235 }
236
237 static void CPUHandler(const v810_timestamp_t timestamp, uint32 PC)
238 {
239 std::vector<VB_BPOINT>::iterator bpit;
240
241 for(bpit = BreakPointsPC.begin(); bpit != BreakPointsPC.end(); bpit++)
242 {
243 if(PC >= bpit->A[0] && PC <= bpit->A[1])
244 {
245 FoundBPoint = TRUE;
246 break;
247 }
248 }
249 VB_V810->CheckBreakpoints(VBDBG_CheckBP, MemPeek16, NULL);
250
251 CPUHookContinuous |= FoundBPoint;
252
253 if(CPUHook && CPUHookContinuous)
254 {
255 ForceEventUpdates(timestamp);
256 CPUHook(PC, FoundBPoint);
257 }
258
259 FoundBPoint = false;
260 }
261
262 static void RedoCPUHook(void)
263 {
264 VB_V810->SetCPUHook((CPUHook || VB_LoggingOn || BreakPointsPC.size() || BreakPointsRead.size() || BreakPointsWrite.size()) ? CPUHandler : NULL,
265 BTEnabled ? AddBranchTrace : NULL);
266 }
267
268 void VBDBG_FlushBreakPoints(int type)
269 {
270 std::vector<VB_BPOINT>::iterator bpit;
271
272 if(type == BPOINT_READ)
273 BreakPointsRead.clear();
274 else if(type == BPOINT_WRITE)
275 BreakPointsWrite.clear();
276 else if(type == BPOINT_PC)
277 BreakPointsPC.clear();
278
279 RedoCPUHook();
280 }
281
282 void VBDBG_AddBreakPoint(int type, unsigned int A1, unsigned int A2, bool logical)
283 {
284 VB_BPOINT tmp;
285
286 tmp.A[0] = A1;
287 tmp.A[1] = A2;
288 tmp.type = type;
289
290 if(type == BPOINT_READ)
291 BreakPointsRead.push_back(tmp);
292 else if(type == BPOINT_WRITE)
293 BreakPointsWrite.push_back(tmp);
294 else if(type == BPOINT_PC)
295 BreakPointsPC.push_back(tmp);
296
297 RedoCPUHook();
298 }
299
300 static uint16 dis_readhw(uint32 A)
301 {
302 int32 timestamp = 0;
303 return(MemPeek16(timestamp, A));
304 }
305
306 void VBDBG_Disassemble(uint32 &a, uint32 SpecialA, char *TextBuf)
307 {
308 return(v810_dis(a, 1, TextBuf, dis_readhw, true));
309 }
310
311 uint32 VBDBG_MemPeek(uint32 A, unsigned int bsize, bool hl, bool logical)
312 {
313 uint32 ret = 0;
314 int32 ws = 0;
315
316 for(unsigned int i = 0; i < bsize; i++)
317 {
318 A &= 0xFFFFFFFF;
319 //ret |= mem_peekbyte(A, ws) << (i * 8);
320 ret |= MemRead8(ws, A) << (i * 8);
321 A++;
322 }
323
324 return(ret);
325 }
326
327 static void GetAddressSpaceBytes(const char *name, uint32 Address, uint32 Length, uint8 *Buffer)
328 {
329 if(!strcmp(name, "cpu"))
330 {
331 while(Length--)
332 {
333 *Buffer = MemPeek8(0, Address);
334
335 Address++;
336 Buffer++;
337 }
338 }
339 else if(!strncmp(name, "vsuwd", 5))
340 {
341 const unsigned int which = name[5] - '0';
342
343 while(Length--)
344 {
345 *Buffer = VB_VSU->PeekWave(which, Address);
346
347 Address++;
348 Buffer++;
349 }
350 }
351 }
352
353 static void PutAddressSpaceBytes(const char *name, uint32 Address, uint32 Length, uint32 Granularity, bool hl, const uint8 *Buffer)
354 {
355 if(!strcmp(name, "cpu"))
356 {
357 while(Length--)
358 {
359 int32 dummy_ts = 0;
360
361 MemWrite8(dummy_ts, Address, *Buffer);
362
363 Address++;
364 Buffer++;
365 }
366 }
367 else if(!strncmp(name, "vsuwd", 5))
368 {
369 const unsigned int which = name[5] - '0';
370
371 while(Length--)
372 {
373 VB_VSU->PokeWave(which, Address, *Buffer);
374
375 Address++;
376 Buffer++;
377 }
378 }
379 }
380
381 uint32 VBDBG_GetRegister(const std::string &name, std::string *special)
382 {
383 if(name == "PC")
384 {
385 return(VB_V810->GetPC());
386 }
387 const char *thestring = name.c_str();
388
389 if(!strncmp(thestring, "PR", 2))
390 {
391 return(VB_V810->GetPR(atoi(thestring + 2)));
392 }
393 else if(!strcmp(thestring, "HSP"))
394 return(VB_V810->GetPR(2));
395 else if(!strcmp(thestring, "SP"))
396 return(VB_V810->GetPR(3));
397 else if(!strcmp(thestring, "GP"))
398 return(VB_V810->GetPR(4));
399 else if(!strcmp(thestring, "TP"))
400 return(VB_V810->GetPR(5));
401 else if(!strcmp(thestring, "LP"))
402 return(VB_V810->GetPR(31));
403 else if(!strcmp(thestring, "TStamp"))
404 return(VB_V810->v810_timestamp);
405 else if(!strncmp(thestring, "SR", 2))
406 {
407 int which_one = atoi(thestring + 2);
408 uint32 val = VB_V810->GetSR(which_one);
409
410 if(special && which_one == PSW)
411 {
412 char buf[256];
413 trio_snprintf(buf, 256, "Z: %d, S: %d, OV: %d, CY: %d, ID: %d, AE: %d, EP: %d, NP: %d, IA: %2d",
414 (int)(bool)(val & PSW_Z), (int)(bool)(val & PSW_S), (int)(bool)(val & PSW_OV), (int)(bool)(val & PSW_CY),
415 (int)(bool)(val & PSW_ID), (int)(bool)(val & PSW_AE), (int)(bool)(val & PSW_EP), (int)(bool)(val & PSW_NP),
416 (val & PSW_IA) >> 16);
417 *special = std::string(buf);
418 }
419 return(val);
420 }
421 uint32 val = 0;
422
423 return(val);
424 }
425
426 void VBDBG_SetRegister(const std::string &name, uint32 value)
427 {
428 if(name == "PC")
429 {
430 VB_V810->SetPC(value & ~1);
431 return;
432 }
433
434 const char *thestring = name.c_str();
435
436 if(!strncmp(thestring, "PR", 2))
437 {
438 VB_V810->SetPR(atoi(thestring + 2), value);
439 }
440 else if(!strcmp(thestring, "HSP"))
441 VB_V810->SetPR(2, value);
442 else if(!strcmp(thestring, "SP"))
443 VB_V810->SetPR(3, value);
444 else if(!strcmp(thestring, "GP"))
445 VB_V810->SetPR(4, value);
446 else if(!strcmp(thestring, "TP"))
447 VB_V810->SetPR(5, value);
448 else if(!strcmp(thestring, "LP"))
449 VB_V810->SetPR(31, value);
450 else if(!strncmp(thestring, "SR", 2))
451 {
452 VB_V810->SetSR(atoi(thestring + 2), value);
453 }
454 }
455
456 void VBDBG_SetCPUCallback(void (*callb)(uint32 PC, bool bpoint), bool continuous)
457 {
458 CPUHook = callb;
459 CPUHookContinuous = continuous;
460 RedoCPUHook();
461 }
462
463 void VBDBG_DoLog(const char *type, const char *format, ...)
464 {
465 if(LogFunc)
466 {
467 char *temp;
468
469 va_list ap;
470 va_start(ap, format);
471
472 temp = trio_vaprintf(format, ap);
473 LogFunc(type, temp);
474 free(temp);
475
476 va_end(ap);
477 }
478 }
479
480 void VBDBG_SetLogFunc(void (*func)(const char *, const char *))
481 {
482 LogFunc = func;
483
484 VB_LoggingOn = func ? TRUE : FALSE;
485
486 RedoCPUHook();
487 }
488
489 static RegType V810Regs[] =
490 {
491 { 0, "PC", "Program Counter", 4 },
492 { 0, "PR1", "Program Register 1", 4 },
493 { 0, "HSP", "Program Register 2(Handler Stack Pointer)", 4 },
494 { 0, "SP", "Program Register 3(Stack Pointer)", 4 },
495 { 0, "GP", "Program Register 4(Global Pointer)", 4 },
496 { 0, "TP", "Program Register 5(Text Pointer)", 4 },
497 { 0, "PR6", "Program Register 6", 4 },
498 { 0, "PR7", "Program Register 7", 4 },
499 { 0, "PR8", "Program Register 8", 4 },
500 { 0, "PR9", "Program Register 9", 4 },
501 { 0, "PR10", "Program Register 10", 4 },
502 { 0, "PR11", "Program Register 11", 4 },
503 { 0, "PR12", "Program Register 12", 4 },
504 { 0, "PR13", "Program Register 13", 4 },
505 { 0, "PR14", "Program Register 14", 4 },
506 { 0, "PR15", "Program Register 15", 4 },
507 { 0, "PR16", "Program Register 16", 4 },
508 { 0, "PR17", "Program Register 17", 4 },
509 { 0, "PR18", "Program Register 18", 4 },
510 { 0, "PR19", "Program Register 19", 4 },
511 { 0, "PR20", "Program Register 20", 4 },
512 { 0, "PR21", "Program Register 21", 4 },
513 { 0, "PR22", "Program Register 22", 4 },
514 { 0, "PR23", "Program Register 23", 4 },
515 { 0, "PR24", "Program Register 24", 4 },
516 { 0, "PR25", "Program Register 25", 4 },
517 { 0, "PR26", "Program Register 26(String Dest Bit Offset)", 4 },
518 { 0, "PR27", "Program Register 27(String Source Bit Offset)", 4 },
519 { 0, "PR28", "Program Register 28(String Length)", 4 },
520 { 0, "PR29", "Program Register 29(String Dest)", 4 },
521 { 0, "PR30", "Program Register 30(String Source)", 4 },
522 { 0, "LP", "Program Register 31(Link Pointer)", 4 },
523 { 0, "SR0", "Exception/Interrupt PC", 4 },
524 { 0, "SR1", "Exception/Interrupt PSW", 4 },
525 { 0, "SR2", "Fatal Error PC", 4 },
526 { 0, "SR3", "Fatal Error PSW", 4 },
527 { 0, "SR4", "Exception Cause Register", 4 },
528 { 0, "SR5", "Program Status Word", 4 },
529 { 0, "SR6", "Processor ID Register", 4 },
530 { 0, "SR7", "Task Control Word", 4 },
531 { 0, "SR24", "Cache Control Word", 4 },
532 { 0, "SR25", "Address Trap Register", 4 },
533
534 //{ 0, "IPEND", "Interrupts Pending", 2 },
535 //{ 0, "IMASK", "Interrupt Mask", 2 },
536 //{ 0, "TCTRL", "Timer Control", 2 },
537 //{ 0, "TPRD", "Timer Period", 2 },
538 //{ 0, "TCNTR", "Timer Counter", 3 },
539
540 { 0, "TStamp", "Timestamp", 3 },
541 { 0, "", "", 0 },
542 };
543
544 static RegGroupType V810RegsGroup =
545 {
546 NULL,
547 V810Regs,
548 NULL,
549 NULL,
550 VBDBG_GetRegister,
551 VBDBG_SetRegister,
552 };
553
554 static uint32 MISC_GetRegister(const unsigned int id, char *special, const uint32 special_len)
555 {
556 return(TIMER_GetRegister(id, special, special_len));
557 }
558
559 static void MISC_SetRegister(const unsigned int id, const uint32 value)
560 {
561 TIMER_SetRegister(id, value);
562 }
563
564 static RegType Regs_Misc[] =
565 {
566 { TIMER_GSREG_TCR, "TCR", "Timer Control Register", 1 },
567 { TIMER_GSREG_DIVCOUNTER, "DivCounter", "Timer Clock Divider Counter", 2 },
568 { TIMER_GSREG_RELOAD_VALUE, "ReloadValue", "Timer Reload Value", 2 },
569 { TIMER_GSREG_COUNTER, "Counter", "Timer Counter Value", 2 },
570 { 0, "", "", 0 },
571 };
572
573 static RegGroupType RegsGroup_Misc =
574 {
575 "Misc",
576 Regs_Misc,
577 MISC_GetRegister,
578 MISC_SetRegister
579 };
580
581 static RegType Regs_VIP[] =
582 {
583 { VIP_GSREG_IPENDING, "IPending", "Interrupts Pending", 2 },
584 { VIP_GSREG_IENABLE, "IEnable", "Interrupts Enabled", 2 },
585
586 { VIP_GSREG_DPCTRL, "DPCTRL", "DPCTRL", 2 },
587
588 { VIP_GSREG_BRTA, "BRTA", "BRTA", 1 },
589 { VIP_GSREG_BRTB, "BRTB", "BRTB", 1 },
590 { VIP_GSREG_BRTC, "BRTC", "BRTC", 1 },
591 { VIP_GSREG_REST, "REST", "REST", 1 },
592 { VIP_GSREG_FRMCYC, "FRMCYC", "FRMCYC", 1 },
593 { VIP_GSREG_XPCTRL, "XPCTRL", "XPCTRL", 2 },
594
595 { VIP_GSREG_SPT0, "SPT0", "SPT0", 2 },
596 { VIP_GSREG_SPT1, "SPT1", "SPT1", 2 },
597 { VIP_GSREG_SPT2, "SPT2", "SPT2", 2 },
598 { VIP_GSREG_SPT3, "SPT3", "SPT3", 2 },
599
600 { VIP_GSREG_GPLT0, "GPLT0", "GPLT0", 1 },
601 { VIP_GSREG_GPLT1, "GPLT1", "GPLT1", 1 },
602 { VIP_GSREG_GPLT2, "GPLT2", "GPLT2", 1 },
603 { VIP_GSREG_GPLT3, "GPLT3", "GPLT3", 1 },
604
605 { VIP_GSREG_JPLT0, "JPLT0", "JPLT0", 1 },
606 { VIP_GSREG_JPLT1, "JPLT1", "JPLT1", 1 },
607 { VIP_GSREG_JPLT2, "JPLT2", "JPLT2", 1 },
608 { VIP_GSREG_JPLT3, "JPLT3", "JPLT3", 1 },
609
610 { VIP_GSREG_BKCOL, "BKCOL", "BKCOL", 1 },
611
612 { 0, "", "", 0 },
613 };
614
615 static RegGroupType RegsGroup_VIP =
616 {
617 "VIP",
618 Regs_VIP,
619 VIP_GetRegister,
620 VIP_SetRegister
621 };
622
623 bool VBDBG_Init(void)
624 {
625 BTEnabled = false;
626 BTIndex = 0;
627 memset(BTEntries, 0, sizeof(BTEntries));
628
629 MDFNDBG_AddRegGroup(&V810RegsGroup);
630 MDFNDBG_AddRegGroup(&RegsGroup_Misc);
631 MDFNDBG_AddRegGroup(&RegsGroup_VIP);
632
633 ASpace_Add(GetAddressSpaceBytes, PutAddressSpaceBytes, "cpu", "CPU Physical", 32);
634 // ASpace_Add(GetAddressSpaceBytes, PutAddressSpaceBytes, "ram", "RAM", 21);
635
636
637 for(int x = 0; x < 5; x++)
638 {
639 AddressSpaceType newt;
640 char tmpname[128], tmpinfo[128];
641
642 trio_snprintf(tmpname, 128, "vsuwd%d", x);
643 trio_snprintf(tmpinfo, 128, "VSU Wave Data %d", x);
644
645 memset(&newt, 0, sizeof(AddressSpaceType));
646
647 newt.GetAddressSpaceBytes = GetAddressSpaceBytes;
648 newt.PutAddressSpaceBytes = PutAddressSpaceBytes;
649
650 newt.name = strdup(tmpname);
651 newt.long_name = strdup(tmpinfo);
652 newt.TotalBits = 5;
653 newt.NP2Size = 0;
654
655 newt.IsWave = TRUE;
656 newt.WaveFormat = ASPACE_WFMT_UNSIGNED;
657 newt.WaveBits = 6;
658 ASpace_Add(newt); //PSG_GetAddressSpaceBytes, PSG_PutAddressSpaceBytes, tmpname, tmpinfo, 5);
659 }
660
661 return(true);
662 }