ppu.debug.c 11 KB

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