mode4.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * mode4/SMS renderer
  3. * (C) notaz, 2009-2010
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. /*
  9. * TODO:
  10. * - TMS9918 modes?
  11. * - gg mode?
  12. * - column scroll (reg 0 bit7)
  13. * - 224/240 line modes
  14. * - doubled sprites
  15. */
  16. #include "pico_int.h"
  17. static void (*FinalizeLineM4)(int line);
  18. static int skip_next_line;
  19. static int screen_offset;
  20. #define PLANAR_PIXEL(x,p) \
  21. t = pack & (0x80808080 >> p); \
  22. if (t) { \
  23. t = ((t >> (7-p)) | (t >> (14-p)) | (t >> (21-p)) | (t >> (28-p))) & 0x0f; \
  24. pd[x] = pal|t; \
  25. }
  26. static int TileNormM4(int sx, int addr, int pal)
  27. {
  28. unsigned char *pd = Pico.est.HighCol + sx;
  29. unsigned int pack, t;
  30. pack = *(unsigned int *)(PicoMem.vram + addr); /* Get 4 bitplanes / 8 pixels */
  31. if (pack)
  32. {
  33. PLANAR_PIXEL(0, 0)
  34. PLANAR_PIXEL(1, 1)
  35. PLANAR_PIXEL(2, 2)
  36. PLANAR_PIXEL(3, 3)
  37. PLANAR_PIXEL(4, 4)
  38. PLANAR_PIXEL(5, 5)
  39. PLANAR_PIXEL(6, 6)
  40. PLANAR_PIXEL(7, 7)
  41. return 0;
  42. }
  43. return 1; /* Tile blank */
  44. }
  45. static int TileFlipM4(int sx,int addr,int pal)
  46. {
  47. unsigned char *pd = Pico.est.HighCol + sx;
  48. unsigned int pack, t;
  49. pack = *(unsigned int *)(PicoMem.vram + addr); /* Get 4 bitplanes / 8 pixels */
  50. if (pack)
  51. {
  52. PLANAR_PIXEL(0, 7)
  53. PLANAR_PIXEL(1, 6)
  54. PLANAR_PIXEL(2, 5)
  55. PLANAR_PIXEL(3, 4)
  56. PLANAR_PIXEL(4, 3)
  57. PLANAR_PIXEL(5, 2)
  58. PLANAR_PIXEL(6, 1)
  59. PLANAR_PIXEL(7, 0)
  60. return 0;
  61. }
  62. return 1; /* Tile blank */
  63. }
  64. static void draw_sprites(int scanline)
  65. {
  66. struct PicoVideo *pv = &Pico.video;
  67. unsigned int sprites_addr[8];
  68. unsigned int sprites_x[8];
  69. unsigned char *sat;
  70. int xoff = 8; // relative to HighCol, which is (screen - 8)
  71. int sprite_base, addr_mask;
  72. int i, s, h;
  73. if (pv->reg[0] & 8)
  74. xoff = 0;
  75. sat = (unsigned char *)PicoMem.vram + ((pv->reg[5] & 0x7e) << 7);
  76. if (pv->reg[1] & 2) {
  77. addr_mask = 0xfe; h = 16;
  78. } else {
  79. addr_mask = 0xff; h = 8;
  80. }
  81. sprite_base = (pv->reg[6] & 4) << (13-2-1);
  82. for (i = s = 0; i < 64 && s < 8; i++)
  83. {
  84. int y;
  85. y = sat[i] + 1;
  86. if (y == 0xd1)
  87. break;
  88. if (y + h <= scanline || scanline < y)
  89. continue; // not on this line
  90. sprites_x[s] = xoff + sat[0x80 + i*2];
  91. sprites_addr[s] = sprite_base + ((sat[0x80 + i*2 + 1] & addr_mask) << (5-1)) +
  92. ((scanline - y) << (2-1));
  93. s++;
  94. }
  95. // now draw all sprites backwards
  96. for (--s; s >= 0; s--)
  97. TileNormM4(sprites_x[s], sprites_addr[s], 0x10);
  98. }
  99. // tilex_ty_prio merged to reduce register pressure
  100. static void draw_strip(const unsigned short *nametab, int dx, int cells, int tilex_ty_prio)
  101. {
  102. int oldcode = -1, blank = -1; // The tile we know is blank
  103. int addr = 0, pal = 0;
  104. // Draw tiles across screen:
  105. for (; cells > 0; dx += 8, tilex_ty_prio++, cells--)
  106. {
  107. int code, zero;
  108. code = nametab[tilex_ty_prio & 0x1f];
  109. if (code == blank)
  110. continue;
  111. if ((code ^ tilex_ty_prio) & 0x1000) // priority differs?
  112. continue;
  113. if (code != oldcode) {
  114. oldcode = code;
  115. // Get tile address/2:
  116. addr = (code & 0x1ff) << 4;
  117. addr += tilex_ty_prio >> 16;
  118. if (code & 0x0400)
  119. addr ^= 0xe; // Y-flip
  120. pal = (code>>7) & 0x10;
  121. }
  122. if (code&0x0200) zero = TileFlipM4(dx, addr, pal);
  123. else zero = TileNormM4(dx, addr, pal);
  124. if (zero)
  125. blank = code; // We know this tile is blank now
  126. }
  127. }
  128. static void DrawDisplayM4(int scanline)
  129. {
  130. struct PicoVideo *pv = &Pico.video;
  131. unsigned short *nametab;
  132. int line, tilex, dx, ty, cells;
  133. int cellskip = 0; // XXX
  134. int maxcells = 32;
  135. // Find the line in the name table
  136. line = pv->reg[9] + scanline; // vscroll + scanline
  137. if (line >= 224)
  138. line -= 224;
  139. // Find name table:
  140. nametab = PicoMem.vram;
  141. nametab += (pv->reg[2] & 0x0e) << (10-1);
  142. nametab += (line>>3) << (6-1);
  143. dx = pv->reg[8]; // hscroll
  144. if (scanline < 16 && (pv->reg[0] & 0x40))
  145. dx = 0; // hscroll disabled for top 2 rows
  146. tilex = ((-dx >> 3) + cellskip) & 0x1f;
  147. ty = (line & 7) << 1; // Y-Offset into tile
  148. cells = maxcells - cellskip;
  149. dx = ((dx - 1) & 7) + 1;
  150. if (dx != 8)
  151. cells++; // have hscroll, need to draw 1 cell more
  152. dx += cellskip << 3;
  153. // low priority tiles
  154. if (!(pv->debug_p & PVD_KILL_B))
  155. draw_strip(nametab, dx, cells, tilex | 0x0000 | (ty << 16));
  156. // sprites
  157. if (!(pv->debug_p & PVD_KILL_S_LO))
  158. draw_sprites(scanline);
  159. // high priority tiles (use virtual layer switch just for fun)
  160. if (!(pv->debug_p & PVD_KILL_A))
  161. draw_strip(nametab, dx, cells, tilex | 0x1000 | (ty << 16));
  162. if (pv->reg[0] & 0x20)
  163. // first column masked
  164. ((int *)Pico.est.HighCol)[2] = ((int *)Pico.est.HighCol)[3] = 0xe0e0e0e0;
  165. }
  166. void PicoFrameStartMode4(void)
  167. {
  168. int lines = 192;
  169. skip_next_line = 0;
  170. screen_offset = 24;
  171. Pico.est.rendstatus = PDRAW_32_COLS;
  172. if ((Pico.video.reg[0] & 6) == 6 && (Pico.video.reg[1] & 0x18)) {
  173. if (Pico.video.reg[1] & 0x08) {
  174. screen_offset = 0;
  175. lines = 240;
  176. }
  177. else {
  178. screen_offset = 8;
  179. lines = 224;
  180. }
  181. }
  182. if (Pico.est.rendstatus != rendstatus_old || lines != rendlines) {
  183. emu_video_mode_change(screen_offset, lines, 1);
  184. rendstatus_old = Pico.est.rendstatus;
  185. rendlines = lines;
  186. }
  187. Pico.est.DrawLineDest = (char *)DrawLineDestBase + screen_offset * DrawLineDestIncrement;
  188. }
  189. void PicoLineMode4(int line)
  190. {
  191. if (skip_next_line > 0) {
  192. skip_next_line--;
  193. return;
  194. }
  195. if (PicoScanBegin != NULL)
  196. skip_next_line = PicoScanBegin(line + screen_offset);
  197. // Draw screen:
  198. BackFill(Pico.video.reg[7] & 0x0f, 0, &Pico.est);
  199. if (Pico.video.reg[1] & 0x40)
  200. DrawDisplayM4(line);
  201. if (FinalizeLineM4 != NULL)
  202. FinalizeLineM4(line);
  203. if (PicoScanEnd != NULL)
  204. skip_next_line = PicoScanEnd(line + screen_offset);
  205. Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;
  206. }
  207. void PicoDoHighPal555M4(void)
  208. {
  209. unsigned int *spal=(void *)PicoMem.cram;
  210. unsigned int *dpal=(void *)Pico.est.HighPal;
  211. unsigned int t;
  212. int i;
  213. Pico.m.dirtyPal = 0;
  214. /* cram is always stored as shorts, even though real hardware probably uses bytes */
  215. for (i = 0x20/2; i > 0; i--, spal++, dpal++) {
  216. t = *spal;
  217. #ifdef USE_BGR555
  218. t = ((t & 0x00030003)<< 3) | ((t & 0x000c000c)<<7) | ((t & 0x00300030)<<10);
  219. #else
  220. t = ((t & 0x00030003)<<14) | ((t & 0x000c000c)<<7) | ((t & 0x00300030)>>1);
  221. #endif
  222. t |= t >> 2;
  223. t |= (t >> 4) & 0x08610861;
  224. *dpal = t;
  225. }
  226. Pico.est.HighPal[0xe0] = 0;
  227. }
  228. static void FinalizeLineRGB555M4(int line)
  229. {
  230. if (Pico.m.dirtyPal)
  231. PicoDoHighPal555M4();
  232. // standard FinalizeLine can finish it for us,
  233. // with features like scaling and such
  234. FinalizeLine555(0, line, &Pico.est);
  235. }
  236. static void FinalizeLine8bitM4(int line)
  237. {
  238. unsigned char *pd = Pico.est.DrawLineDest;
  239. if (!(PicoOpt & POPT_DIS_32C_BORDER))
  240. pd += 32;
  241. memcpy(pd, Pico.est.HighCol + 8, 256);
  242. }
  243. void PicoDrawSetOutputMode4(pdso_t which)
  244. {
  245. switch (which)
  246. {
  247. case PDF_8BIT: FinalizeLineM4 = FinalizeLine8bitM4; break;
  248. case PDF_RGB555: FinalizeLineM4 = FinalizeLineRGB555M4; break;
  249. default: FinalizeLineM4 = NULL; break;
  250. }
  251. }