ppu.debug.c 11 KB

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