mode4.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #include <platform/common/upscale.h>
  18. static void (*FinalizeLineM4)(int line);
  19. static int skip_next_line;
  20. static int screen_offset, line_offset;
  21. static void TileBGM4(int sx, int pal)
  22. {
  23. u32 *pd = (u32 *)(Pico.est.HighCol + sx);
  24. pd[0] = pd[1] = pal ? 0x10101010 : 0;
  25. }
  26. #define PLANAR_PIXELL(x,p) \
  27. t = pack & (0x80808080 >> p); \
  28. t = ((t >> (7-p)) | (t >> (14-p)) | (t >> (21-p)) | (t >> (28-p))) & 0x0f; \
  29. pd[x] = pal|t;
  30. static void TileNormM4Low(int sx, unsigned int pack, int pal)
  31. {
  32. unsigned char *pd = Pico.est.HighCol + sx;
  33. unsigned int t;
  34. PLANAR_PIXELL(0, 0)
  35. PLANAR_PIXELL(1, 1)
  36. PLANAR_PIXELL(2, 2)
  37. PLANAR_PIXELL(3, 3)
  38. PLANAR_PIXELL(4, 4)
  39. PLANAR_PIXELL(5, 5)
  40. PLANAR_PIXELL(6, 6)
  41. PLANAR_PIXELL(7, 7)
  42. }
  43. static void TileFlipM4Low(int sx, unsigned int pack, int pal)
  44. {
  45. unsigned char *pd = Pico.est.HighCol + sx;
  46. unsigned int t;
  47. PLANAR_PIXELL(0, 7)
  48. PLANAR_PIXELL(1, 6)
  49. PLANAR_PIXELL(2, 5)
  50. PLANAR_PIXELL(3, 4)
  51. PLANAR_PIXELL(4, 3)
  52. PLANAR_PIXELL(5, 2)
  53. PLANAR_PIXELL(6, 1)
  54. PLANAR_PIXELL(7, 0)
  55. }
  56. #define PLANAR_PIXEL(x,p) \
  57. t = pack & (0x80808080 >> p); \
  58. if (t) { \
  59. t = ((t >> (7-p)) | (t >> (14-p)) | (t >> (21-p)) | (t >> (28-p))) & 0x0f; \
  60. pd[x] = pal|t; \
  61. }
  62. static void TileNormM4(int sx, unsigned int pack, int pal)
  63. {
  64. unsigned char *pd = Pico.est.HighCol + sx;
  65. unsigned int t;
  66. PLANAR_PIXEL(0, 0)
  67. PLANAR_PIXEL(1, 1)
  68. PLANAR_PIXEL(2, 2)
  69. PLANAR_PIXEL(3, 3)
  70. PLANAR_PIXEL(4, 4)
  71. PLANAR_PIXEL(5, 5)
  72. PLANAR_PIXEL(6, 6)
  73. PLANAR_PIXEL(7, 7)
  74. }
  75. static void TileFlipM4(int sx, unsigned int pack, int pal)
  76. {
  77. unsigned char *pd = Pico.est.HighCol + sx;
  78. unsigned int t;
  79. PLANAR_PIXEL(0, 7)
  80. PLANAR_PIXEL(1, 6)
  81. PLANAR_PIXEL(2, 5)
  82. PLANAR_PIXEL(3, 4)
  83. PLANAR_PIXEL(4, 3)
  84. PLANAR_PIXEL(5, 2)
  85. PLANAR_PIXEL(6, 1)
  86. PLANAR_PIXEL(7, 0)
  87. }
  88. static void draw_sprites(int scanline)
  89. {
  90. struct PicoVideo *pv = &Pico.video;
  91. unsigned int sprites_addr[8];
  92. unsigned int sprites_x[8];
  93. unsigned int pack;
  94. unsigned char *sat;
  95. int xoff = 8; // relative to HighCol, which is (screen - 8)
  96. int sprite_base, addr_mask;
  97. int i, s, h;
  98. if (pv->reg[0] & 8)
  99. xoff = 0;
  100. xoff += line_offset;
  101. sat = (unsigned char *)PicoMem.vram + ((pv->reg[5] & 0x7e) << 7);
  102. if (pv->reg[1] & 2) {
  103. addr_mask = 0xfe; h = 16;
  104. } else {
  105. addr_mask = 0xff; h = 8;
  106. }
  107. sprite_base = (pv->reg[6] & 4) << (13-2-1);
  108. for (i = s = 0; i < 64; i++)
  109. {
  110. int y;
  111. y = sat[MEM_LE2(i)] + 1;
  112. if (y == 0xd1)
  113. break;
  114. if (y + h <= scanline || scanline < y)
  115. continue; // not on this line
  116. if (s >= 8) {
  117. pv->status |= SR_SOVR;
  118. break;
  119. }
  120. sprites_x[s] = xoff + sat[MEM_LE2(0x80 + i*2)];
  121. sprites_addr[s] = sprite_base + ((sat[MEM_LE2(0x80 + i*2 + 1)] & addr_mask) << (5-1)) +
  122. ((scanline - y) << (2-1));
  123. s++;
  124. }
  125. // really half-assed but better than nothing
  126. if (s > 1)
  127. pv->status |= SR_C;
  128. // now draw all sprites backwards
  129. for (--s; s >= 0; s--) {
  130. pack = CPU_LE2(*(u32 *)(PicoMem.vram + sprites_addr[s]));
  131. TileNormM4(sprites_x[s], pack, 0x10);
  132. }
  133. }
  134. // tilex_ty_prio merged to reduce register pressure
  135. static void draw_strip_low(const unsigned short *nametab, int dx, int cells, int tilex_ty_prio)
  136. {
  137. int oldcode = -1;
  138. int addr = 0, pal = 0;
  139. // Draw tiles across screen:
  140. for (; cells > 0; dx += 8, tilex_ty_prio++, cells--)
  141. {
  142. unsigned int pack;
  143. unsigned code;
  144. code = nametab[tilex_ty_prio & 0x1f];
  145. if (code != oldcode) {
  146. oldcode = code;
  147. // Get tile address/2:
  148. addr = (code & 0x1ff) << 4;
  149. addr += tilex_ty_prio >> 16;
  150. if (code & 0x0400)
  151. addr ^= 0xe; // Y-flip
  152. pal = (code>>7) & 0x10;
  153. }
  154. pack = CPU_LE2(*(u32 *)(PicoMem.vram + addr)); /* Get 4 bitplanes / 8 pixels */
  155. if (pack == 0) TileBGM4(dx, pal);
  156. else if (code & 0x0200) TileFlipM4Low(dx, pack, pal);
  157. else TileNormM4Low(dx, pack, pal);
  158. }
  159. }
  160. // tilex_ty_prio merged to reduce register pressure
  161. static void draw_strip_high(const unsigned short *nametab, int dx, int cells, int tilex_ty_prio)
  162. {
  163. int oldcode = -1, blank = -1; // The tile we know is blank
  164. int addr = 0, pal = 0;
  165. // Draw tiles across screen:
  166. for (; cells > 0; dx += 8, tilex_ty_prio++, cells--)
  167. {
  168. unsigned int pack;
  169. unsigned code;
  170. code = nametab[tilex_ty_prio & 0x1f];
  171. if (code == blank)
  172. continue;
  173. if ((code ^ tilex_ty_prio) & 0x1000) // priority differs?
  174. continue;
  175. if (code != oldcode) {
  176. oldcode = code;
  177. // Get tile address/2:
  178. addr = (code & 0x1ff) << 4;
  179. addr += tilex_ty_prio >> 16;
  180. if (code & 0x0400)
  181. addr ^= 0xe; // Y-flip
  182. pal = (code>>7) & 0x10;
  183. }
  184. pack = CPU_LE2(*(u32 *)(PicoMem.vram + addr)); /* Get 4 bitplanes / 8 pixels */
  185. if (pack == 0) {
  186. blank = code;
  187. continue;
  188. }
  189. if (code & 0x0200) TileFlipM4(dx, pack, pal);
  190. else TileNormM4(dx, pack, pal);
  191. }
  192. }
  193. static void DrawDisplayM4(int scanline)
  194. {
  195. struct PicoVideo *pv = &Pico.video;
  196. unsigned short *nametab;
  197. int line, tilex, dx, ty, cells;
  198. int cellskip = 0; // XXX
  199. int maxcells = 32;
  200. // Find the line in the name table
  201. line = pv->reg[9] + scanline; // vscroll + scanline
  202. line &= 0xff;
  203. // Find name table:
  204. nametab = PicoMem.vram;
  205. if ((Pico.video.reg[0] & 6) == 6 && (Pico.video.reg[1] & 0x18))
  206. nametab += ((pv->reg[2] & 0x0c) << (10-1)) + (0x700 >> 1);
  207. else
  208. nametab += (pv->reg[2] & 0x0e) << (10-1);
  209. nametab += (line>>3) << (6-1);
  210. dx = pv->reg[8]; // hscroll
  211. if (scanline < 16 && (pv->reg[0] & 0x40))
  212. dx = 0; // hscroll disabled for top 2 rows
  213. tilex = ((-dx >> 3) + cellskip) & 0x1f;
  214. ty = (line & 7) << 1; // Y-Offset into tile
  215. cells = maxcells - cellskip;
  216. dx = ((dx - 1) & 7) + 1;
  217. if (dx != 8)
  218. cells++; // have hscroll, need to draw 1 cell more
  219. dx += cellskip << 3;
  220. dx += line_offset;
  221. // low priority tiles
  222. if (!(pv->debug_p & PVD_KILL_B))
  223. draw_strip_low(nametab, dx, cells, tilex | 0x0000 | (ty << 16));
  224. // sprites
  225. if (!(pv->debug_p & PVD_KILL_S_LO))
  226. draw_sprites(scanline);
  227. // high priority tiles (use virtual layer switch just for fun)
  228. if (!(pv->debug_p & PVD_KILL_A))
  229. draw_strip_high(nametab, dx, cells, tilex | 0x1000 | (ty << 16));
  230. if (pv->reg[0] & 0x20) {
  231. // first column masked, caculate offset to start of line
  232. dx = (dx&~0x1f) / 4;
  233. ((u32 *)Pico.est.HighCol)[dx+2] = ((u32 *)Pico.est.HighCol)[dx+3] = 0xe0e0e0e0;
  234. }
  235. }
  236. static void FinalizeLineRGB555M4(int line);
  237. void PicoFrameStartMode4(void)
  238. {
  239. int lines = 192, columns = 256, coffs;
  240. skip_next_line = 0;
  241. screen_offset = 24;
  242. Pico.est.rendstatus = PDRAW_32_COLS;
  243. if ((Pico.video.reg[0] & 6) == 6 && (Pico.video.reg[1] & 0x18)) {
  244. if (Pico.video.reg[1] & 0x08) {
  245. screen_offset = 0;
  246. lines = 240;
  247. }
  248. else {
  249. screen_offset = 8;
  250. lines = 224;
  251. }
  252. }
  253. if (PicoIn.opt & POPT_EN_SOFTSCALE) {
  254. line_offset = 0;
  255. columns = 320;
  256. } else
  257. line_offset = PicoIn.opt & POPT_DIS_32C_BORDER ? 0 : 32;
  258. coffs = line_offset;
  259. if (FinalizeLineM4 == FinalizeLineRGB555M4)
  260. line_offset = 0 /* done in FinalizeLine */;
  261. if (Pico.est.rendstatus != rendstatus_old || lines != rendlines) {
  262. emu_video_mode_change(screen_offset, lines, coffs, columns);
  263. rendstatus_old = Pico.est.rendstatus;
  264. rendlines = lines;
  265. }
  266. Pico.est.HighCol = HighColBase + screen_offset * HighColIncrement;
  267. Pico.est.DrawLineDest = (char *)DrawLineDestBase + screen_offset * DrawLineDestIncrement;
  268. }
  269. void PicoLineMode4(int line)
  270. {
  271. if (skip_next_line > 0) {
  272. skip_next_line--;
  273. return;
  274. }
  275. if (PicoScanBegin != NULL)
  276. skip_next_line = PicoScanBegin(line + screen_offset);
  277. // Draw screen:
  278. BackFill(Pico.video.reg[7] & 0x0f, 0, &Pico.est);
  279. if (Pico.video.reg[1] & 0x40)
  280. DrawDisplayM4(line);
  281. if (FinalizeLineM4 != NULL)
  282. FinalizeLineM4(line);
  283. if (PicoScanEnd != NULL)
  284. skip_next_line = PicoScanEnd(line + screen_offset);
  285. Pico.est.HighCol += HighColIncrement;
  286. Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;
  287. }
  288. void PicoDoHighPal555M4(void)
  289. {
  290. unsigned int *spal=(void *)PicoMem.cram;
  291. unsigned int *dpal=(void *)Pico.est.HighPal;
  292. unsigned int t;
  293. int i;
  294. Pico.m.dirtyPal = 0;
  295. /* cram is always stored as shorts, even though real hardware probably uses bytes */
  296. for (i = 0x20/2; i > 0; i--, spal++, dpal++) {
  297. t = *spal;
  298. #if defined(USE_BGR555)
  299. t = ((t & 0x00030003)<< 3) | ((t & 0x000c000c)<<6) | ((t & 0x00300030)<<9);
  300. t |= (t >> 2) | ((t >> 4) & 0x04210421);
  301. #elif defined(USE_BGR565)
  302. t = ((t & 0x00030003)<< 3) | ((t & 0x000c000c)<<7) | ((t & 0x00300030)<<10);
  303. t |= (t >> 2) | ((t >> 4) & 0x08610861);
  304. #else
  305. t = ((t & 0x00030003)<<14) | ((t & 0x000c000c)<<7) | ((t & 0x00300030)>>1);
  306. t |= (t >> 2) | ((t >> 4) & 0x08610861);
  307. #endif
  308. *dpal = t;
  309. }
  310. Pico.est.HighPal[0xe0] = 0;
  311. }
  312. static void FinalizeLineRGB555M4(int line)
  313. {
  314. if (Pico.m.dirtyPal)
  315. PicoDoHighPal555M4();
  316. // standard FinalizeLine can finish it for us,
  317. // with features like scaling and such
  318. FinalizeLine555(0, line, &Pico.est);
  319. }
  320. static void FinalizeLine8bitM4(int line)
  321. {
  322. unsigned char *pd = Pico.est.DrawLineDest + line_offset;
  323. unsigned char *ps = Pico.est.HighCol + line_offset + 8;
  324. if (DrawLineDestIncrement) {
  325. if (PicoIn.opt & POPT_EN_SOFTSCALE)
  326. rh_upscale_nn_4_5(pd, 320, ps, 256, 256, f_nop);
  327. else
  328. memcpy(pd, ps, 256);
  329. }
  330. }
  331. void PicoDrawSetOutputMode4(pdso_t which)
  332. {
  333. switch (which)
  334. {
  335. case PDF_8BIT: FinalizeLineM4 = FinalizeLine8bitM4; break;
  336. case PDF_RGB555: FinalizeLineM4 = FinalizeLineRGB555M4; break;
  337. default: FinalizeLineM4 = NULL;
  338. PicoDrawSetInternalBuf(Pico.est.Draw2FB, 328); break;
  339. }
  340. rendstatus_old = -1;
  341. }
  342. // vim:shiftwidth=2:ts=2:expandtab