ppu.debug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * PPU Debug utilities - The TI-NESulator Project
  3. * ppu.debug.c
  4. *
  5. * Created by Manoel Trapier.
  6. * Copyright 2003-2008 986 Corp. All rights reserved.
  7. *
  8. * $LastChangedDate$
  9. * $Author$
  10. * $HeadURL$
  11. * $Revision$
  12. *
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. /* Allegro includes */
  17. #ifdef __APPLE__
  18. #define USE_CONSOLE
  19. #include <Allegro/allegro.h>
  20. #else
  21. #define USE_CONSOLE
  22. #include <allegro.h>
  23. #endif
  24. #define __TINES_PPU_INTERNAL__
  25. #include <ppu/ppu.h>
  26. #include <ppu/ppu.memory.h>
  27. #include <ppu/ppu.debug.h>
  28. #include <types.h>
  29. extern BITMAP *Buffer;
  30. extern unsigned short ppu_spritePatternTable;
  31. extern short PPU_Reg_S;
  32. void DebugColor()
  33. {
  34. #ifdef TO_MAKE
  35. static unsigned short x = 128;
  36. static unsigned short y = 128;
  37. unsigned char OldDisplayPalette = ppu.DisplayPalette;
  38. byte keyb;
  39. unsigned int i;
  40. unsigned long Color;
  41. NOBLIT = 1;
  42. ppu.DisplayPalette = ~0;
  43. while(!key[KEY_ESC])
  44. {
  45. frame++;
  46. PPUVBlank();
  47. Color = /*Buffer->line[y][x]*/ _getpixel(Buffer, x, y);
  48. textprintf(Buffer, font, 5, 340, GetColor(3), "Pos [%d:%d] Color: %d Bg: %d", x, y, Color, BgColor);
  49. line(Buffer, x-10, y, x+10, y, GetColor(1));
  50. line(Buffer, x, y-10, x, y+10, GetColor(1));
  51. /*
  52. rect(Buffer, 0, 255, 4 * 20 + 2, 255 + 4 * 20 + 2, 0);
  53. rect(Buffer, 90, 255, 90 + 4 * 20 + 2, 255 + 4 * 20 + 2, 0);
  54. for (i = 0; i < 16; i++)
  55. {
  56. rectfill(Buffer, 1 + (i % 4) * 20, 256 + (i / 4) * 20, 1 + (i % 4) * 20 + 20, 256 + (i / 4) * 20 + 20, PPU_Rd(0x3F00 + i]);
  57. rectfill(Buffer, 91 + (i % 4) * 20, 256 + (i / 4) * 20, 91 + (i % 4) * 20 + 20, 256 + (i / 4) * 20 + 20, PPU_Rd(0x3F10 + i]);
  58. }*/
  59. for( i = 0; i < 16; i++)
  60. {
  61. if (GetColor(PPU_Rd(0x3F00 + i]) == Color)
  62. {
  63. line(Buffer, 1+(i%4)*20, 256 + (i / 4) * 20, 1 + (i % 4) * 20 + 20, 256 + (i / 4) * 20 + 20, 0xFFFFFFFF);
  64. line(Buffer, 1+(i%4)*20, 256 + (i / 4) * 20 + 20, 1 + (i % 4) * 20 + 20, 256 + (i / 4) * 20, 0xFFFFFFFF);
  65. }
  66. if (GetColor(PPU_Rd(0x3F10 + i]) == Color)
  67. {
  68. line(Buffer, 91+(i%4)*20, 256 + (i / 4) * 20, 91 + (i % 4) * 20 + 20, 256 + (i / 4) * 20 + 20, 0xFFFFFFFF);
  69. line(Buffer, 91+(i%4)*20, 256 + (i / 4) * 20 + 20, 91 + (i % 4) * 20 + 20, 256 + (i / 4) * 20, 0xFFFFFFFF);
  70. }
  71. }
  72. blit(Buffer, screen, 0, 0, 0, 0, 512 + 256, 480);
  73. if (keypressed())
  74. {
  75. keyb = (readkey() & 0xFF);
  76. if (keyb == '4')
  77. {
  78. x--;
  79. }
  80. if (keyb == '8')
  81. {
  82. y--;
  83. }
  84. if (keyb == '6')
  85. {
  86. x++;
  87. }
  88. if (keyb == '2')
  89. {
  90. y++;
  91. }
  92. }
  93. }
  94. ppu.DisplayPalette = OldDisplayPalette;
  95. NOBLIT = 0;
  96. #endif
  97. }
  98. void DebugSprites()
  99. {
  100. #ifdef TO_MAKE
  101. byte keyb;
  102. static int SelSprite = 0;
  103. PPUSprite sprite;
  104. NOBLIT = 1;
  105. ppu.ControlRegister2.b |= PPU_CR2_SPRTVISIBILITY;
  106. while(!key[KEY_ESC])
  107. {
  108. frame++;
  109. PPUVBlank();
  110. sprite = PPUGetSprite(SelSprite);
  111. if (ppu.ControlRegister1.b & PPU_CR1_SPRTSIZE)
  112. {
  113. rect(Buffer, sprite.x-1, sprite.y-1, sprite.x+9, sprite.y+17, 1);
  114. }
  115. else
  116. {
  117. rect(Buffer, sprite.x-1, sprite.y-1, sprite.x+9, sprite.y+9, 1);
  118. }
  119. textprintf(Buffer, font, 5, 340, GetColor(3), "Sprite %d [%d:%d]", SelSprite, sprite.x, sprite.y);
  120. textprintf(Buffer, font, 5, 349, GetColor(3), "B0: 0x%X B1: 0x%X B2: 0x%X B3: 0x%X",sprite.y,sprite.tileid,sprite.flags.b,sprite.x);
  121. textprintf(Buffer, font, 5, 358, GetColor(3), "Tile Index: %d", sprite.tileid);
  122. textprintf(Buffer, font, 5, 367, GetColor(3), "Vertical Flip: %d", sprite.flags.s.VFlip);
  123. textprintf(Buffer, font, 5, 376, GetColor(3), "Horizontal Flip: %d", sprite.flags.s.HFlip);
  124. textprintf(Buffer, font, 5, 385, GetColor(3), "Background Priority: %d", sprite.flags.s.BgPrio);
  125. textprintf(Buffer, font, 5, 394, GetColor(3), "Upper Color: %d", sprite.flags.s.UpperColor);
  126. blit(Buffer, screen, 0, 0, 0, 0, 512 + 256, 480);
  127. if (keypressed())
  128. {
  129. keyb = (readkey() & 0xFF);
  130. if (keyb == '+')
  131. SelSprite = (SelSprite<63)?SelSprite+1:0;
  132. if (keyb == '-')
  133. SelSprite = (SelSprite>0)?SelSprite-1:63;
  134. if (keyb == 'h')
  135. {
  136. sprite.flags.s.HFlip = ~sprite.flags.s.HFlip;
  137. PPUSetSprite(SelSprite, &sprite);
  138. }
  139. if (keyb == 'b')
  140. {
  141. sprite.flags.s.BgPrio = ~sprite.flags.s.BgPrio;
  142. PPUSetSprite(SelSprite, &sprite);
  143. }
  144. if (keyb == 'v')
  145. {
  146. sprite.flags.s.VFlip = ~sprite.flags.s.VFlip;
  147. PPUSetSprite(SelSprite, &sprite);
  148. }
  149. if (keyb == '4')
  150. {
  151. sprite.x--;
  152. PPUSetSprite(SelSprite, &sprite);
  153. }
  154. if (keyb == '8')
  155. {
  156. sprite.y--;
  157. PPUSetSprite(SelSprite, &sprite);
  158. }
  159. if (keyb == '6')
  160. {
  161. sprite.x++;
  162. PPUSetSprite(SelSprite, &sprite);
  163. }
  164. if (keyb == '2')
  165. {
  166. sprite.y++;
  167. PPUSetSprite(SelSprite, &sprite);
  168. }
  169. }
  170. }
  171. NOBLIT = 0;
  172. #endif
  173. }
  174. #define GetTilePos(addr,x,y) (addr+x+(y*32))
  175. #define GetTileColor(tile,x1,y1) ( ( ppu_readMemory(((tile+y1)>>8)&0xFF, (tile+y1) & 0xFF) & (1<<(7-x1)) ) == 0 ? 0 : 1 ) |\
  176. ( ( ppu_readMemory(((tile+y1+8)>>8) & 0xFF, (tile+y1+8) &0xFF) & (1<<(7-x1)) ) == 0 ? 0 : 1<<1 )
  177. #define PPU_Rd(addr) ppu_readMemory((addr>>8)&0xFF, addr&0xFF)
  178. void ppu_dumpOneNameTable(unsigned short nametable, int xd, int yd)
  179. {
  180. byte x,y, x1, y1, Color;
  181. unsigned short TileID;
  182. for (x = 0; x < 32; x++)
  183. for (y = 0; y < 30; y++)
  184. {
  185. TileID = (PPU_Rd(0x2000 + nametable + x + (y * 32)) << 4) | (PPU_Reg_S);
  186. for (x1 = 0; x1 < 8; x1++)
  187. for (y1 = 0; y1 < 8; y1++)
  188. {
  189. Color = GetTileColor(TileID, x1, y1);
  190. // if (ppu.DisplayAttributeTable != 0)
  191. // Color |= (Buffer->line[(8 * y) + 240 + y1][(8 * x) + 256 + x1] & 0x3) << 2;
  192. Color += (nametable>>8);
  193. Color = ppu_readMemory(0x3F, Color);
  194. _putpixel(Buffer, (8 * x) + xd + x1, (8 * y) + yd + y1, Color);
  195. }
  196. }
  197. }
  198. void ppu_dumpOneAttributeTable(unsigned short nametable, int xd, int yd)
  199. {
  200. int x, x1, y1, Color, AttrByte;
  201. for (x = 0; x < 0x40; x++)
  202. {
  203. AttrByte = PPU_Rd(nametable + 0x23C0 + x);
  204. x1 = x % 8;
  205. y1 = x / 8;
  206. Color = AttrByte & 0x3; // Pattern 1;
  207. // Color = PPU_Rd(0x3F00 + (Color * 4) + 1);
  208. rectfill(Buffer,xd+(x1*32),yd+(y1*32),xd+15+(x1*32),yd+15+(y1*32),Color);
  209. textprintf_ex(Buffer, font, 4+xd+(x1*32), 4+yd+(y1*32), ~Color, Color, "%X", Color);
  210. Color = (AttrByte>>2) & 0x3; // Pattern 2;
  211. // Color = PPU_Rd(0x3F00 + (Color * 4) + 1);
  212. rectfill(Buffer,16+xd+(x1*32),yd+(y1*32),16+xd+15+(x1*32),yd+15+(y1*32),Color);
  213. textprintf_ex(Buffer, font, 4+xd+(x1*32)+16, 4+yd+(y1*32), ~Color, Color, "%X", Color);
  214. Color = (AttrByte>>4) & 0x3; // Pattern 3;
  215. // Color = PPU_Rd(0x3F00 + (Color * 4) + 1);
  216. rectfill(Buffer,xd+(x1*32),16+yd+(y1*32),xd+15+(x1*32),16+yd+15+(y1*32),Color);
  217. textprintf_ex(Buffer, font, 4+xd+(x1*32), 4+yd+(y1*32)+16, ~Color, Color, "%X", Color);
  218. Color = (AttrByte>>6) & 0x3; // Pattern 4;
  219. // Color = PPU_Rd(0x3F00 + (Color * 4) + 1);
  220. rectfill(Buffer,16+xd+(x1*32),16+yd+(y1*32),16+xd+15+(x1*32),16+yd+15+(y1*32),Color);
  221. textprintf_ex(Buffer, font, 4+xd+(x1*32)+16, 4+yd+(y1*32)+16, ~Color, Color, "%X", Color);
  222. rect(Buffer, xd+(x1*32), yd+(y1*32), xd+(x1*32) + 31 , yd+(y1*32) + 31, 1);
  223. line(Buffer, xd+(x1*32)+16, yd+(y1*32), xd+(x1*32) + 16 , yd+(y1*32) + 31, 5);
  224. line(Buffer, xd+(x1*32), yd+(y1*32)+16, xd+(x1*32) + 31 , yd+(y1*32) + 16, 5);
  225. }
  226. }
  227. extern byte *ppu_mem_nameTables;
  228. extern int ppu_screenMode;
  229. void ppu_dumpNameTable(int xd, int yd)
  230. {
  231. // ppu_setPagePtr1k(0x20, ppu_mem_nameTables + 0x000);
  232. // ppu_setPagePtr1k(0x24, ppu_mem_nameTables + 0x400);
  233. // ppu_setPagePtr1k(0x28, ppu_mem_nameTables + 0x800);
  234. // ppu_setPagePtr1k(0x2C, ppu_mem_nameTables + 0xC00);
  235. ppu_dumpOneNameTable(0x800, xd , yd);
  236. ppu_dumpOneNameTable(0xC00, xd + 256, yd);
  237. ppu_dumpOneNameTable(0x000, xd , yd + 240);
  238. ppu_dumpOneNameTable(0x400, xd + 256, yd + 240);
  239. // ppu_setScreenMode(ppu_screenMode);
  240. }
  241. void ppu_dumpAttributeTable(int xd, int yd)
  242. {
  243. ppu_dumpOneAttributeTable(0x800, xd , yd);
  244. ppu_dumpOneAttributeTable(0xC00, xd + 256, yd);
  245. ppu_dumpOneAttributeTable(0x000, xd , yd + 240);
  246. ppu_dumpOneAttributeTable(0x400, xd + 256, yd + 240);
  247. }
  248. void ppu_dumpPattern(int xd, int yd)
  249. {
  250. int x1, y1, x, y, Color, i, TileID;
  251. /* y:346 */
  252. x1 = 0;
  253. y1 = 0;
  254. for (i = 0; i < 256; i++)
  255. {
  256. TileID = 0x0000 + (i << 4);
  257. for (x = 0; x < 8; x++)
  258. for (y = 0; y < 8; y++)
  259. {
  260. Color = GetTileColor(TileID, x, y);
  261. _putpixel(Buffer, 10 + x1 + x, 347 + y1 + y, /*ppu_readMemory(0x3F,*/ Color/*)*/);
  262. }
  263. x1 += 8;
  264. if (x1 >= 128)
  265. {
  266. x1 = 0;
  267. y1 += 8;
  268. }
  269. }
  270. x1 = 0;
  271. y1 = 0;
  272. for (i = 0; i < 256; i++)
  273. {
  274. TileID = 0x1000 + (i << 4);
  275. for (x = 0; x < 8; x++)
  276. for (y = 0; y < 8; y++)
  277. {
  278. Color = GetTileColor(TileID, x, y);
  279. _putpixel(Buffer, 10 + 128 + x1 + x, 347 + y1 + y, /*ppu_readMemory(0x3F,*/ 0x10 | Color/*)*/ );
  280. }
  281. x1 += 8;
  282. if (x1 >= 128)
  283. {
  284. x1 = 0;
  285. y1 += 8;
  286. }
  287. }
  288. }
  289. void ppu_dumpPalette(int x, int y)
  290. {
  291. int i;
  292. textout(Buffer, font, "Bg Palette", x , y, 5);
  293. textout(Buffer, font, "Sprt Palette", x + 90, y, 5);
  294. rect(Buffer, x+0, y+20, x+4 * 20 + 2, y + 4 * 20 + 22, 0);
  295. rect(Buffer, x+90, y+20, x+90 + 4 * 20 + 2, y + 4 * 20 + 22, 0);
  296. for (i = 0; i < 16; i++)
  297. {
  298. rectfill(Buffer, x + 1 + (i % 4) * 20, y + 21 + (i / 4) * 20, x + 1 + (i % 4) * 20 + 20, y + 21 + (i / 4) * 20 + 20, ppu_readMemory(0x3F, i));
  299. rectfill(Buffer, x + 91 + (i % 4) * 20, y + 21 +(i / 4) * 20, x + 91 + (i % 4) * 20 + 20, y + 21 +(i / 4) * 20 + 20, ppu_readMemory(0x3F, i+0x10));
  300. }
  301. }