Debug.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // some debug code, just for fun of it
  2. // (c) Copyright 2008 notaz, All rights reserved.
  3. #include "PicoInt.h"
  4. #include "Debug.h"
  5. #define bit(r, x) ((r>>x)&1)
  6. #define MVP dstrp+=strlen(dstrp)
  7. void z80_debug(char *dstr);
  8. static char dstr[1024*8];
  9. char *PDebugMain(void)
  10. {
  11. struct PicoVideo *pv=&Pico.video;
  12. unsigned char *reg=pv->reg, r;
  13. extern int HighPreSpr[];
  14. int i, sprites_lo, sprites_hi;
  15. char *dstrp;
  16. sprites_lo = sprites_hi = 0;
  17. for (i = 0; HighPreSpr[i] != 0; i+=2)
  18. if (HighPreSpr[i+1] & 0x8000)
  19. sprites_hi++;
  20. else sprites_lo++;
  21. dstrp = dstr;
  22. sprintf(dstrp, "mode set 1: %02x spr lo: %2i, spr hi: %2i\n", (r=reg[0]), sprites_lo, sprites_hi); MVP;
  23. sprintf(dstrp, "display_disable: %i, M3: %i, palette: %i, ?, hints: %i\n", bit(r,0), bit(r,1), bit(r,2), bit(r,4)); MVP;
  24. sprintf(dstrp, "mode set 2: %02x hcnt: %i\n", (r=reg[1]), pv->reg[10]); MVP;
  25. sprintf(dstrp, "SMS/gen: %i, pal: %i, dma: %i, vints: %i, disp: %i, TMS: %i\n",bit(r,2),bit(r,3),bit(r,4),bit(r,5),bit(r,6),bit(r,7)); MVP;
  26. sprintf(dstrp, "mode set 3: %02x\n", (r=reg[0xB])); MVP;
  27. sprintf(dstrp, "LSCR: %i, HSCR: %i, 2cell vscroll: %i, IE2: %i\n", bit(r,0), bit(r,1), bit(r,2), bit(r,3)); MVP;
  28. sprintf(dstrp, "mode set 4: %02x\n", (r=reg[0xC])); MVP;
  29. sprintf(dstrp, "interlace: %i%i, cells: %i, shadow: %i\n", bit(r,2), bit(r,1), (r&0x80) ? 40 : 32, bit(r,3)); MVP;
  30. sprintf(dstrp, "scroll size: w: %i, h: %i SRAM: %i; eeprom: %i (%i)\n", reg[0x10]&3, (reg[0x10]&0x30)>>4,
  31. bit(Pico.m.sram_reg, 4), bit(Pico.m.sram_reg, 2), SRam.eeprom_type); MVP;
  32. sprintf(dstrp, "sram range: %06x-%06x, reg: %02x\n", SRam.start, SRam.end, Pico.m.sram_reg); MVP;
  33. sprintf(dstrp, "pend int: v:%i, h:%i, vdp status: %04x\n", bit(pv->pending_ints,5), bit(pv->pending_ints,4), pv->status); MVP;
  34. sprintf(dstrp, "pal: %i, hw: %02x, frame#: %i\n", Pico.m.pal, Pico.m.hardware, Pico.m.frame_count); MVP;
  35. #if defined(EMU_C68K)
  36. sprintf(dstrp, "M68k: PC: %06x, st_flg: %x, cycles: %u\n", SekPc, PicoCpuCM68k.state_flags, SekCyclesDoneT()); MVP;
  37. sprintf(dstrp, "d0=%08x, a0=%08x, osp=%08x, irql=%i\n", PicoCpuCM68k.d[0], PicoCpuCM68k.a[0], PicoCpuCM68k.osp, PicoCpuCM68k.irq); MVP;
  38. sprintf(dstrp, "d1=%08x, a1=%08x, sr=%04x\n", PicoCpuCM68k.d[1], PicoCpuCM68k.a[1], CycloneGetSr(&PicoCpuCM68k)); dstrp+=strlen(dstrp); MVP;
  39. for(r=2; r < 8; r++) {
  40. sprintf(dstrp, "d%i=%08x, a%i=%08x\n", r, PicoCpuCM68k.d[r], r, PicoCpuCM68k.a[r]); MVP;
  41. }
  42. #elif defined(EMU_M68K)
  43. sprintf(dstrp, "M68k: PC: %06x, cycles: %u, irql: %i\n", SekPc, SekCyclesDoneT(), PicoCpuMM68k.int_level>>8); MVP;
  44. #elif defined(EMU_F68K)
  45. sprintf(dstrp, "M68k: PC: %06x, cycles: %u, irql: %i\n", SekPc, SekCyclesDoneT(), PicoCpuFM68k.interrupts[0]); MVP;
  46. #endif
  47. sprintf(dstrp, "z80Run: %i, z80_reset: %i, z80_bnk: %06x\n", Pico.m.z80Run, Pico.m.z80_reset, Pico.m.z80_bank68k<<15); MVP;
  48. z80_debug(dstrp); MVP;
  49. if (strlen(dstr) > sizeof(dstr))
  50. printf("warning: debug buffer overflow (%i/%i)\n", strlen(dstr), sizeof(dstr));
  51. return dstr;
  52. }
  53. char *PDebugSpriteList(void)
  54. {
  55. struct PicoVideo *pvid=&Pico.video;
  56. int table=0,u,link=0;
  57. int max_sprites = 80;
  58. char *dstrp;
  59. if (!(pvid->reg[12]&1))
  60. max_sprites = 64;
  61. dstr[0] = 0;
  62. dstrp = dstr;
  63. table=pvid->reg[5]&0x7f;
  64. if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
  65. table<<=8; // Get sprite table address/2
  66. for (u=0; u < max_sprites; u++)
  67. {
  68. unsigned int *sprite;
  69. int code, code2, sx, sy, height;
  70. sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
  71. // get sprite info
  72. code = sprite[0];
  73. // check if it is on this line
  74. sy = (code&0x1ff)-0x80;
  75. height = ((code>>24)&3)+1;
  76. // masking sprite?
  77. code2 = sprite[1];
  78. sx = ((code2>>16)&0x1ff)-0x80;
  79. sprintf(dstrp, "#%02i x:%4i y:%4i %ix%i %s\n", u, sx, sy, ((code>>26)&3)+1, height,
  80. (code2&0x8000)?"hi":"lo");
  81. MVP;
  82. link=(code>>16)&0x7f;
  83. if(!link) break; // End of sprites
  84. }
  85. return dstr;
  86. }
  87. #define GREEN1 0x0700
  88. #ifdef USE_BGR555
  89. #define YELLOW1 0x071c
  90. #define BLUE1 0xf000
  91. #define RED1 0x001e
  92. #else
  93. #define YELLOW1 0xe700
  94. #define BLUE1 0x001e
  95. #define RED1 0xf000
  96. #endif
  97. static void set16(unsigned short *p, unsigned short d, int cnt)
  98. {
  99. while (cnt-- > 0)
  100. *p++ = d;
  101. }
  102. void PDebugShowSpriteStats(unsigned short *screen, int stride)
  103. {
  104. int lines, i, u, step;
  105. unsigned short *dest;
  106. unsigned char *p;
  107. step = (320-4*4-1) / MAX_LINE_SPRITES;
  108. lines = 240;
  109. if (!Pico.m.pal || !(Pico.video.reg[1]&8))
  110. lines = 224, screen += stride*8;
  111. for (i = 0; i < lines; i++)
  112. {
  113. dest = screen + stride*i;
  114. p = &HighLnSpr[i][0];
  115. // sprite graphs
  116. for (u = 0; u < (p[0] & 0x7f); u++) {
  117. set16(dest, (p[3+u] & 0x80) ? YELLOW1 : GREEN1, step);
  118. dest += step;
  119. }
  120. // flags
  121. dest = screen + stride*i + 320-4*4;
  122. if (p[1] & 0x40) set16(dest+4*0, GREEN1, 4);
  123. if (p[1] & 0x80) set16(dest+4*1, YELLOW1, 4);
  124. if (p[1] & 0x20) set16(dest+4*2, BLUE1, 4);
  125. if (p[1] & 0x10) set16(dest+4*3, RED1, 4);
  126. }
  127. // draw grid
  128. for (i = step*5; i <= 320-4*4-1; i += step*5) {
  129. for (u = 0; u < lines; u++)
  130. screen[i + u*stride] = 0x182;
  131. }
  132. }
  133. void PDebugShowPalette(unsigned short *screen, int stride)
  134. {
  135. unsigned int *spal=(void *)Pico.cram;
  136. unsigned int *dpal=(void *)HighPal;
  137. int x, y, i;
  138. for (i = 0x3f/2; i >= 0; i--)
  139. #ifdef USE_BGR555
  140. dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);
  141. #else
  142. dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7);
  143. #endif
  144. for (i = 0x3f; i >= 0; i--)
  145. HighPal[0x40|i] = (unsigned short)((HighPal[i]>>1)&0x738e);
  146. for (i = 0x3f; i >= 0; i--) {
  147. int t=HighPal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;
  148. HighPal[0x80|i] = (unsigned short)t;
  149. }
  150. screen += 16*stride+8;
  151. for (y = 0; y < 8*4; y++)
  152. for (x = 0; x < 8*16; x++)
  153. screen[x + y*stride] = HighPal[x/8 + (y/8)*16];
  154. screen += 160;
  155. for (y = 0; y < 8*4; y++)
  156. for (x = 0; x < 8*16; x++)
  157. screen[x + y*stride] = HighPal[(x/8 + (y/8)*16) | 0x40];
  158. screen += stride*48;
  159. for (y = 0; y < 8*4; y++)
  160. for (x = 0; x < 8*16; x++)
  161. screen[x + y*stride] = HighPal[(x/8 + (y/8)*16) | 0x80];
  162. Pico.m.dirtyPal = 1;
  163. }
  164. #if defined(DRAW2_OVERRIDE_LINE_WIDTH)
  165. #define DRAW2_LINE_WIDTH DRAW2_OVERRIDE_LINE_WIDTH
  166. #else
  167. #define DRAW2_LINE_WIDTH 328
  168. #endif
  169. void PDebugShowSprite(unsigned short *screen, int stride, int which)
  170. {
  171. struct PicoVideo *pvid=&Pico.video;
  172. int table=0,u,link=0,*sprite=0,*fsprite,oldsprite[2];
  173. int x,y,max_sprites = 80, oldcol, oldreg;
  174. if (!(pvid->reg[12]&1))
  175. max_sprites = 64;
  176. table=pvid->reg[5]&0x7f;
  177. if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
  178. table<<=8; // Get sprite table address/2
  179. for (u=0; u < max_sprites && u <= which; u++)
  180. {
  181. sprite=(int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
  182. link=(sprite[0]>>16)&0x7f;
  183. if (!link) break; // End of sprites
  184. }
  185. if (u >= max_sprites) return;
  186. fsprite = (int *)(Pico.vram+(table&0x7ffc));
  187. oldsprite[0] = fsprite[0];
  188. oldsprite[1] = fsprite[1];
  189. fsprite[0] = (sprite[0] & ~0x007f01ff) | 0x000080;
  190. fsprite[1] = (sprite[1] & ~0x01ff8000) | 0x800000;
  191. oldreg = pvid->reg[7];
  192. oldcol = Pico.cram[0];
  193. pvid->reg[7] = 0;
  194. Pico.cram[0] = 0;
  195. PicoDrawMask = PDRAW_SPRITES_LOW_ON;
  196. PicoFrameFull();
  197. for (y = 0; y < 8*4; y++)
  198. {
  199. unsigned char *ps = PicoDraw2FB + DRAW2_LINE_WIDTH*y + 8;
  200. for (x = 0; x < 8*4; x++)
  201. if (ps[x]) screen[x] = HighPal[ps[x]], ps[x] = 0;
  202. screen += stride;
  203. }
  204. fsprite[0] = oldsprite[0];
  205. fsprite[1] = oldsprite[1];
  206. pvid->reg[7] = oldreg;
  207. Pico.cram[0] = oldcol;
  208. PicoDrawMask = -1;
  209. }
  210. #define dump_ram(ram,fname) \
  211. { \
  212. unsigned short *sram = (unsigned short *) ram; \
  213. FILE *f; \
  214. int i; \
  215. \
  216. for (i = 0; i < sizeof(ram)/2; i++) \
  217. sram[i] = (sram[i]<<8) | (sram[i]>>8); \
  218. f = fopen(fname, "wb"); \
  219. if (f) { \
  220. fwrite(ram, 1, sizeof(ram), f); \
  221. fclose(f); \
  222. } \
  223. for (i = 0; i < sizeof(ram)/2; i++) \
  224. sram[i] = (sram[i]<<8) | (sram[i]>>8); \
  225. }
  226. #define dump_ram_noswab(ram,fname) \
  227. { \
  228. FILE *f; \
  229. f = fopen(fname, "wb"); \
  230. if (f) { \
  231. fwrite(ram, 1, sizeof(ram), f); \
  232. fclose(f); \
  233. } \
  234. }
  235. void PDebugDumpMem(void)
  236. {
  237. dump_ram(Pico.ram, "dumps/ram.bin");
  238. dump_ram_noswab(Pico.zram, "dumps/zram.bin");
  239. dump_ram(Pico.vram, "dumps/vram.bin");
  240. dump_ram(Pico.cram, "dumps/cram.bin");
  241. dump_ram(Pico.vsram,"dumps/vsram.bin");
  242. if (PicoAHW & PAHW_MCD)
  243. {
  244. dump_ram(Pico_mcd->prg_ram, "dumps/prg_ram.bin");
  245. if (Pico_mcd->s68k_regs[3]&4) // 1M mode?
  246. wram_1M_to_2M(Pico_mcd->word_ram2M);
  247. dump_ram(Pico_mcd->word_ram2M, "dumps/word_ram_2M.bin");
  248. wram_2M_to_1M(Pico_mcd->word_ram2M);
  249. dump_ram(Pico_mcd->word_ram1M[0], "dumps/word_ram_1M_0.bin");
  250. dump_ram(Pico_mcd->word_ram1M[1], "dumps/word_ram_1M_1.bin");
  251. if (!(Pico_mcd->s68k_regs[3]&4)) // 2M mode?
  252. wram_2M_to_1M(Pico_mcd->word_ram2M);
  253. dump_ram_noswab(Pico_mcd->pcm_ram,"dumps/pcm_ram.bin");
  254. dump_ram_noswab(Pico_mcd->bram, "dumps/bram.bin");
  255. }
  256. }