mode4.c 9.2 KB

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