mode4.c 7.1 KB

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