ppu.debug.c 10 KB

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